use super::*;
pub(super) struct Fixture {
pub d: Arc<Daemon>,
home: PathBuf,
}
impl Drop for Fixture {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.home).unwrap();
}
}
pub(super) fn fixture() -> Fixture {
fixture_with(&[])
}
fn options(home: &Path) -> Options {
Options {
home: home.to_path_buf(),
idle: Duration::from_secs(10),
concurrency: 50,
stall: Duration::from_secs(60),
approval_timeout: Duration::from_secs(60),
agents: config::Agents::default(),
}
}
pub(super) fn fixture_with(more: &[Value]) -> Fixture {
let home = std::env::temp_dir().join(format!("axiopera-test-{}", uuid::Uuid::new_v4()));
let base = json!({
"id": "test", "parent": "codex:host", "agent": "codex", "tier": "read",
"cwd": ".", "status": "idle", "turns": 0, "delivered": 0,
"changed_files": [], "stalled": false, "detached": false,
"created_ms": 0, "updated_ms": 0,
});
for changes in std::iter::once(&json!({})).chain(more) {
let mut fields = base.clone();
let fields_map = fields.as_object_mut().unwrap();
fields_map.extend(changes.as_object().unwrap().clone());
let task: Task = serde_json::from_value(fields).unwrap();
Store::new(&home).save(&task).unwrap();
}
Fixture {
d: Arc::new(Daemon::load(options(&home), None).unwrap()),
home,
}
}
#[test]
fn blocked_is_logged_without_waking_and_counted_once_per_turn() {
let f = fixture();
f.d.emit("test", "blocked", json!({"detail": "denied"}));
f.d.emit("test", "blocked", json!({"detail": "denied again"}));
assert!(f.d.lock().tasks["test"].deliverable.is_empty());
assert_eq!(f.d.lock().tasks["test"].blocked, 2);
assert_eq!(f.d.store.events("test").len(), 2);
for kind in [
"spawned",
"turn_started",
"steered",
"input_read",
"tool",
"said",
"blocked",
] {
assert!(!store::deliverable(kind));
}
for kind in [
"question",
"approval_request",
"stalled",
"failed",
"cancelled",
"turn_ended",
] {
assert!(store::deliverable(kind));
}
}
#[test]
fn a_message_given_is_saved_whole_by_its_event() {
let f = fixture();
let message = "Check it again.\n\nAll of it.";
f.d.emit_message("test", "steered", json!({"queued": false}), message);
let events = f.d.store.events("test");
let path = f.d.store.message_path("test", events[0].seq);
assert_eq!(std::fs::read_to_string(path).unwrap(), message);
}
#[tokio::test]
async fn send_changes_tier_and_preserves_the_session_across_reload() {
for status in [
Status::Idle,
Status::Waiting,
Status::Failed,
Status::Interrupted,
Status::Cancelled,
] {
let f = fixture();
f.d.lock().stopping = true;
f.d.update("test", |t| {
t.session = Some("native-session".into());
t.turns = 3;
t.delivered = 2;
t.status = status;
});
for tier in [ipc::Tier::Full, ipc::Tier::Work, ipc::Tier::Read] {
let mut buf = Vec::new();
f.d.send(
"codex:host",
"test",
"continue".into(),
Some(tier),
None,
&mut Out::new(&mut buf),
)
.await
.unwrap();
assert!(replies(&buf).contains("queued for a new turn"));
let again = Daemon::load(options(&f.home), None).unwrap();
let saved = again.lock().tasks["test"].task.clone();
assert_eq!(saved.tier, tier);
assert_eq!(saved.session.as_deref(), Some("native-session"));
let recovered = if status.is_stopped() {
Status::Interrupted
} else {
status
};
assert_eq!(
(saved.turns, saved.delivered, saved.status),
(3, 2, recovered)
);
let mut st = f.d.lock();
assert_eq!(st.tasks["test"].pending, ["continue"]);
st.tasks.get_mut("test").unwrap().pending.clear();
st.tasks
.get_mut("test")
.unwrap()
.task
.pending_inputs
.clear();
}
}
}
#[tokio::test]
async fn tier_changes_reject_busy_and_closed_tasks_without_accepting_input() {
for case in ["running", "releasing", "pending", "detached"] {
let f = fixture();
f.d.lock().stopping = true;
let (tx, mut rx) = mpsc::unbounded_channel();
{
let mut st = f.d.lock();
let e = st.tasks.get_mut("test").unwrap();
match case {
"running" | "releasing" => {
e.actor = Some(tx);
e.releasing = case == "releasing";
}
"pending" => e.pending.push("previous".into()),
"detached" => e.task.detached = true,
_ => unreachable!(),
}
}
let error =
f.d.input(
"user",
"test",
"new input".into(),
Some(ipc::Tier::Full),
None,
)
.await
.unwrap_err()
.to_string();
let expected = match case {
"detached" => "taken over",
_ => "still running or has queued input",
};
assert!(error.contains(expected), "{case}: {error}");
assert_eq!(f.d.lock().tasks["test"].task.tier, ipc::Tier::Read);
assert!(
!f.d.lock().tasks["test"]
.pending
.iter()
.any(|m| m == "new input")
);
assert!(rx.try_recv().is_err());
}
}
#[tokio::test]
async fn tier_changes_respect_task_ownership_and_parent_child_limits() {
let native = "task:test/grok:native";
let f = fixture_with(&[
json!({"id": "kid", "parent": native}),
json!({"id": "other", "parent": "task:test"}),
]);
f.d.lock().stopping = true;
for (caller, id, expected) in [
(native, "test", "not your sub-task"),
("task:kid", "other", "not your sub-task"),
("task:test", "kid", "not your sub-task"),
(native, "kid", "exceeds its parent"),
("user", "kid", "exceeds its parent"),
] {
let error =
f.d.input(caller, id, "continue".into(), Some(ipc::Tier::Full), None)
.await
.unwrap_err()
.to_string();
assert!(error.contains(expected), "{error}");
assert!(f.d.lock().tasks[id].pending.is_empty());
}
f.d.input(
"user",
"test",
"continue".into(),
Some(ipc::Tier::Full),
None,
)
.await
.unwrap();
f.d.lock().tasks.get_mut("test").unwrap().pending.clear();
f.d.try_update("test", |t| t.pending_inputs.clear())
.unwrap();
f.d.input(
native,
"kid",
"continue".into(),
Some(ipc::Tier::Full),
None,
)
.await
.unwrap();
let error =
f.d.input(
"user",
"test",
"continue".into(),
Some(ipc::Tier::Read),
None,
)
.await
.unwrap_err()
.to_string();
assert!(error.contains("sub-tasks above read"), "{error}");
assert_eq!(f.d.lock().tasks["test"].task.tier, ipc::Tier::Full);
f.d.lock().tasks.get_mut("kid").unwrap().pending.clear();
f.d.try_update("kid", |t| t.pending_inputs.clear()).unwrap();
assert!(
f.d.input(
"user",
"test",
"continue".into(),
Some(ipc::Tier::Read),
None
)
.await
.is_err()
);
f.d.set_status("kid", Status::Cancelled);
assert!(
f.d.input(
"user",
"test",
"continue".into(),
Some(ipc::Tier::Read),
None
)
.await
.is_err()
);
f.d.try_update("kid", |t| t.detached = true).unwrap();
f.d.input(
"user",
"test",
"continue".into(),
Some(ipc::Tier::Read),
None,
)
.await
.unwrap();
}
#[tokio::test]
async fn send_without_a_tier_change_still_steers_a_running_turn() {
for tier in [None, Some(ipc::Tier::Read)] {
let f = fixture();
let (tx, mut rx) = mpsc::unbounded_channel();
f.d.lock().tasks.get_mut("test").unwrap().actor = Some(tx);
let receive = async {
let Some(Cmd::Input(input)) = rx.recv().await else {
panic!("expected input")
};
assert_eq!(input.text, "continue");
input.reply.send("sent to its running turn").unwrap();
};
let send =
f.d.input("codex:host", "test", "continue".into(), tier, None);
let (result, ()) = tokio::join!(send, receive);
assert_eq!(result.unwrap(), "sent to its running turn");
}
}
fn replies(buf: &[u8]) -> String {
buf.split(|b| *b == b'\n')
.filter_map(|l| serde_json::from_slice::<Reply>(l).ok())
.filter_map(|r| match r {
Reply::Out { text, .. } => Some(text),
_ => None,
})
.collect::<Vec<_>>()
.join("\n")
}
#[tokio::test]
async fn native_child_inboxes_are_separate_and_the_task_session_keeps_its_identity() {
for (agent, host) in [
(Agent::Claude, "claude"),
(Agent::Codex, "codex"),
(Agent::Grok, "grok"),
] {
let native = format!("task:test/{host}:native");
let root = format!("task:test/{host}:root");
let f = fixture_with(&[
json!({"id": "root_job", "parent": "task:test", "name": "job", "turns": 1}),
json!({"id": "native_job", "parent": native, "name": "job", "turns": 1}),
]);
f.d.update("test", |t| {
t.agent = agent;
t.session = Some("root".into());
});
assert_eq!(f.d.resolve_caller(&root), "task:test");
assert_eq!(f.d.resolve_caller("task:test"), "task:test");
assert_eq!(f.d.resolve_caller(&native), native);
assert_eq!(
f.d.resolve_caller("task:missing/codex:root"),
"task:missing/codex:root"
);
let other = format!(
"task:test/{}:root",
if host == "grok" { "codex" } else { "grok" }
);
assert_eq!(f.d.resolve_caller(&other), other);
assert_eq!(
f.d.find(&f.d.resolve_caller(&root), "job").unwrap(),
"root_job"
);
assert_eq!(f.d.find(&native, "job").unwrap(), "native_job");
for (id, body) in [("root_job", "ROOT_RESULT"), ("native_job", "NATIVE_RESULT")] {
f.d.store.save_turn(id, 1, body).unwrap();
f.d.emit(id, "turn_ended", json!({"turn": 1, "ok": true}));
}
for (caller, own, other) in [
(&root, "ROOT_RESULT", "NATIVE_RESULT"),
(&native, "NATIVE_RESULT", "ROOT_RESULT"),
] {
let mut buf = Vec::new();
f.d.dispatch(
Envelope {
protocol: ipc::PROTOCOL,
version: env!("CARGO_PKG_VERSION").into(),
caller: caller.clone(),
cwd: f.home.clone(),
env: None,
request: Request::Inbox { tasks: vec![] },
},
&mut Out::new(&mut buf),
)
.await
.unwrap();
let text = replies(&buf);
assert!(text.contains(own) && !text.contains(other), "{text}");
}
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.resolve_caller(&root), "task:test");
assert_eq!(again.resolve_caller(&native), native);
assert!(again.unseen(&again.lock(), &native, &[]).is_empty());
assert!(again.unseen(&again.lock(), "task:test", &[]).is_empty());
}
}
#[tokio::test]
async fn native_callers_keep_the_owning_tasks_depth_and_tier_limits() {
let native = "task:test/grok:native";
let f = fixture_with(&[json!({"id": "kid", "parent": native})]);
assert_eq!(f.d.depth(native), 1);
assert_eq!(f.d.depth("task:kid/codex:native"), 2);
let spawn = ipc::Spawn {
agent: Agent::Grok,
tier: ipc::Tier::Work,
task: "unreachable".into(),
cwd: f.home.clone(),
name: None,
model: None,
effort: None,
timeout_secs: None,
};
for (caller, expected) in [
(native, "cannot spawn a work task"),
("task:kid/codex:native", "two levels deep"),
] {
let error =
f.d.spawn(
caller,
spawn.clone(),
vec![],
&mut Out::new(&mut Vec::new()),
)
.await
.unwrap_err();
assert!(error.to_string().contains(expected), "{error}");
}
assert_eq!(f.d.lock().tasks.len(), 2);
let st = f.d.lock();
assert!(within(&st, &st.tasks["kid"].task, "test"));
}
#[tokio::test]
async fn native_callers_receive_approvals_under_the_owning_tasks_tier() {
let native = "task:test/codex:native";
let f = fixture_with(&[json!({"id": "kid", "parent": native, "tier": "work"})]);
assert_eq!(approver(&f.d.lock(), "kid"), "codex:host");
f.d.update("test", |t| t.tier = ipc::Tier::Full);
assert_eq!(approver(&f.d.lock(), "kid"), native);
let (reply, decision) = oneshot::channel();
f.d.request_approval(
"kid",
agent::Approval {
summary: "review".into(),
detail: "NATIVE_APPROVAL".into(),
reply,
withdrawn: oneshot::channel().1,
},
);
let rid = f.d.lock().approvals.keys().next().unwrap().clone();
let mut buf = Vec::new();
f.d.inbox("task:test", &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert!(!replies(&buf).contains("NATIVE_APPROVAL"));
assert!(
f.d.resolve(&rid, Some("task:test"), agent::Decision::Approve)
.is_err()
);
let mut buf = Vec::new();
f.d.inbox(native, &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert!(replies(&buf).contains("NATIVE_APPROVAL"));
f.d.resolve(&rid, Some(native), agent::Decision::Approve)
.unwrap();
assert!(matches!(decision.await.unwrap(), agent::Decision::Approve));
}
#[tokio::test]
async fn only_sessions_and_the_user_stop_the_daemon() {
let f = fixture();
let stop = |caller: &str| Envelope {
protocol: ipc::PROTOCOL,
version: env!("CARGO_PKG_VERSION").into(),
caller: caller.into(),
cwd: f.home.clone(),
env: None,
request: Request::Stop { force: true },
};
for caller in ["task:test", "task:test/codex:native"] {
let error =
f.d.dispatch(stop(caller), &mut Out::new(&mut Vec::new()))
.await
.unwrap_err()
.to_string();
assert!(error.contains("not sub-agents"), "{error}");
}
let mut buf = Vec::new();
f.d.dispatch(stop("user"), &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(replies(&buf), "stopping");
}
#[tokio::test]
async fn question_receipts_match_the_task_answer_route() {
for caller in ["task:test", "task:test/codex:child", "task:test/grok:child"] {
let f = fixture();
f.d.lock().stopping = true;
let mut output = Vec::new();
f.d.ask(caller, "Which port?".into(), &mut Out::new(&mut output))
.await
.unwrap();
let qid = f.d.lock().tasks["test"].task.questions[0].id.clone();
let receipt = replies(&output);
if caller == "task:test" {
assert_eq!(receipt, format!("asked {qid}."));
} else {
assert!(receipt.contains("this task's main session"), "{receipt}");
let tool = if caller.contains("/codex:") {
"the `answers` tool"
} else {
"`axiopera answers`"
};
assert!(receipt.contains(tool), "{receipt}");
}
f.d.reply(
"codex:host",
&qid,
"8080".into(),
Some(vec![]),
&mut Out::new(&mut Vec::new()),
)
.await
.unwrap();
assert!(f.d.lock().tasks["test"].pending[0].contains(&format!("Answer to question {qid}")));
output.clear();
f.d.answers(caller, &mut Out::new(&mut output))
.await
.unwrap();
assert!(replies(&output).contains("answer: 8080"));
}
}
#[tokio::test]
async fn grok_spawn_receipts_use_the_callers_monitor() {
let mut f = fixture_with(&[json!({"id": "grok", "agent": "grok"})]);
Arc::get_mut(&mut f.d).unwrap().opts.agents.claude.path =
Some(std::env::current_exe().unwrap());
f.d.lock().stopping = true;
for (caller, monitor) in [
("grok:host", true),
("task:grok", true),
("task:test/grok:child", true),
("task:grok/codex:child", false),
("user", false),
] {
let spawn = ipc::Spawn {
agent: Agent::Claude,
tier: ipc::Tier::Read,
task: "fixture only".into(),
cwd: f.home.clone(),
name: None,
model: None,
effort: None,
timeout_secs: None,
};
let mut output = Vec::new();
f.d.spawn(caller, spawn, vec![], &mut Out::new(&mut output))
.await
.unwrap();
let receipt = replies(&output);
assert_eq!(
receipt.contains("watch monitor"),
monitor,
"{caller}: {receipt}"
);
assert_eq!(
receipt.contains("`axiopera wait`"),
!monitor,
"{caller}: {receipt}"
);
}
}
#[test]
fn cancelling_an_owner_also_cancels_tasks_spawned_by_its_native_children() {
let f = fixture_with(&[
json!({"id": "native_job", "parent": "task:test/grok:native"}),
json!({"id": "root_job", "parent": "task:test"}),
json!({"id": "unrelated"}),
]);
assert_eq!(
f.d.cancel_tree("test", "cancelled by request", "codex:host"),
3
);
let st = f.d.lock();
for id in ["test", "native_job", "root_job"] {
assert_eq!(st.tasks[id].task.status, Status::Cancelled);
}
assert_eq!(st.tasks["unrelated"].task.status, Status::Idle);
}
#[tokio::test]
async fn wait_lists_news_without_taking_it_and_inbox_takes_all() {
let f = fixture();
f.d.emit("test", "message", json!({"to": "other", "text": "a note"}));
let mut buf = Vec::new();
let exit =
f.d.wait("codex:host", &[], None, &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(exit, ipc::exit::NOTHING_RUNNING);
f.d.emit(
"test",
"question",
json!({"id": "q1", "text": "Which port?"}),
);
let mut buf = Vec::new();
let exit =
f.d.wait("codex:host", &[], None, &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(exit, ipc::exit::OK);
let text = replies(&buf);
assert!(
text.contains("asks q1") && !text.contains("a note"),
"{text}"
);
assert_eq!(f.d.unseen(&f.d.lock(), "codex:host", &[]).len(), 2);
let mut buf = Vec::new();
let exit =
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(exit, ipc::exit::OK);
let text = replies(&buf);
let (question, note) = (text.find("asks q1").unwrap(), text.find("a note").unwrap());
assert!(question < note, "{text}");
assert!(f.d.unseen(&f.d.lock(), "codex:host", &[]).is_empty());
let mut buf = Vec::new();
let exit =
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(exit, ipc::exit::NOTHING_RUNNING);
}
#[tokio::test]
async fn what_inbox_and_result_hand_over_is_on_record_in_each_tasks_log() {
let f = fixture_with(&[json!({"id": "other"})]);
let records = |task: &str| -> Vec<Value> {
f.d.store
.events(task)
.into_iter()
.filter(|e| e.kind == "delivered")
.map(|e| Value::Object(e.data))
.collect()
};
f.d.update("test", |t| t.turns = 1);
f.d.store.save_turn("test", 1, "the report").unwrap();
f.d.emit("test", "turn_ended", json!({"turn": 1, "ok": true}));
f.d.emit("other", "question", json!({"id": "q1", "text": "Which?"}));
let seq = |task: &str| f.d.lock().tasks[task].deliverable[0].seq;
let (ended, asked) = (seq("test"), seq("other"));
f.d.wait("codex:host", &[], None, &mut Out::new(&mut Vec::new()))
.await
.unwrap();
assert!(records("test").is_empty() && records("other").is_empty());
f.d.inbox("codex:host", &[], &mut Out::new(&mut Vec::new()))
.await
.unwrap();
let (of_test, of_other) = (records("test"), records("other"));
let batch = of_test[0]["batch"].as_str().unwrap();
assert!(batch.starts_with("axiopera:"), "{batch}");
assert_eq!(
of_test,
[
json!({"to": "codex:host", "via": "inbox", "batch": batch, "events": [ended], "results": [1]})
]
);
assert_eq!(
of_other,
[
json!({"to": "codex:host", "via": "inbox", "batch": batch, "events": [asked], "results": []})
]
);
let exit =
f.d.inbox("codex:host", &[], &mut Out::new(&mut Vec::new()))
.await
.unwrap();
assert_eq!(exit, ipc::exit::NOTHING_RUNNING);
assert_eq!(records("test").len() + records("other").len(), 2);
let mut buf = Vec::new();
f.d.result("user", "test", None, false, &mut Out::new(&mut buf))
.await
.unwrap();
assert!(replies(&buf).contains("already delivered"));
assert_eq!(records("test").len(), 1);
f.d.result("user", "test", None, true, &mut Out::new(&mut Vec::new()))
.await
.unwrap();
let taken = &records("test")[1];
assert_eq!(
(
&taken["to"],
&taken["via"],
&taken["events"],
&taken["results"]
),
(&json!("user"), &json!("result"), &json!([]), &json!([1]))
);
assert_ne!(taken["batch"], json!(batch));
let agent = f.d.lock().tasks["test"].task.agent;
let lines: Vec<String> =
f.d.store
.events("test")
.iter()
.filter(|e| e.kind == "delivered")
.map(|e| render::event_line(e, agent))
.collect();
assert_eq!(
lines,
[
"[test codex] handed to codex:host by inbox: 1 message(s), the result of turn 1",
"[test codex] handed to user by result: the result of turn 1",
]
);
}
#[test]
fn a_reply_takes_whole_events_and_leaves_the_rest() {
let f = fixture();
let finish = |turn: u32, body: &str| {
f.d.update("test", |t| t.turns = turn);
f.d.store.save_turn("test", turn, body).unwrap();
f.d.emit("test", "turn_ended", json!({"turn": turn, "ok": true}));
};
let half = "x".repeat(render::RESPONSE_TOKENS * 2);
finish(1, &half);
finish(2, &half);
let events = f.d.unseen(&f.d.lock(), "codex:host", &[]);
let batch = f.d.batch(&[], &events);
assert_eq!(batch.items.len(), 1);
assert!(batch.items[0].1.ends_with(&format!("{half}\n</result>")));
assert_eq!(batch.turns, [("test".to_string(), 1)]);
assert_eq!(batch.left, 1);
let f = fixture();
let huge = format!("{}END", "y".repeat(render::RESPONSE_TOKENS * 8));
f.d.update("test", |t| t.turns = 1);
f.d.store.save_turn("test", 1, &huge).unwrap();
f.d.emit("test", "turn_ended", json!({"turn": 1, "ok": true}));
let events = f.d.unseen(&f.d.lock(), "codex:host", &[]);
let batch = f.d.batch(&[], &events);
let text = &batch.items[0].1;
assert!(text.contains("[truncated") && text.ends_with("END\n</result>"));
assert!(render::tokens(text) <= render::RESPONSE_TOKENS);
assert_eq!(batch.left, 0);
}
#[tokio::test]
async fn long_questions_show_their_ends_and_where_the_rest_is() {
let f = fixture();
let long = format!("START{}END", "q".repeat(render::WHOLE_TOKENS * 8));
f.d.ask("task:test", long.clone(), &mut Out::new(&mut Vec::new()))
.await
.unwrap();
let mut buf = Vec::new();
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
let text = replies(&buf);
assert!(text.contains("<question>\nSTART") && text.contains("END\n</question>"));
assert!(render::tokens(&text) <= render::RESPONSE_TOKENS);
let file = text
.split("read all of it in ")
.nth(1)
.and_then(|rest| rest.split(" before answering").next())
.unwrap();
assert_eq!(std::fs::read_to_string(file).unwrap(), long);
}
#[tokio::test]
async fn approvals_go_ahead_of_earlier_news_and_reach_the_approver_once() {
let f = fixture();
f.d.update("test", |t| t.tier = ipc::Tier::Work);
let finish = |turn: u32, body: &str| {
f.d.update("test", |t| t.turns = turn);
f.d.store.save_turn("test", turn, body).unwrap();
f.d.emit("test", "turn_ended", json!({"turn": turn, "ok": true}));
};
let request = |summary: &str| {
let (reply, decision) = oneshot::channel();
f.d.request_approval(
"test",
agent::Approval {
summary: summary.into(),
detail: format!("Bash\ncommand: {summary}"),
reply,
withdrawn: oneshot::channel().1,
},
);
decision
};
let half = "x".repeat(render::RESPONSE_TOKENS * 2);
finish(1, &half);
finish(2, &half);
let _decision = request("rm -rf build");
let rid = f.d.lock().approvals.keys().next().unwrap().clone();
let mut buf = Vec::new();
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
let first = replies(&buf);
assert!(first.starts_with("[test codex] needs approval"), "{first}");
assert!(
first.ends_with("1 more: run `axiopera inbox` again."),
"{first}"
);
f.d.resolve(&rid, Some("codex:host"), agent::Decision::Approve)
.unwrap();
let (left, _) = f.d.reminder("codex:host").unwrap();
assert!(!left.contains("approval"), "{left}");
let mut buf = Vec::new();
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
let second = replies(&buf);
assert!(
second.contains("turn 2 finished") && !second.contains("approval"),
"{second}"
);
let _decision = request("ls");
let rid = f.d.lock().approvals.keys().next().unwrap().clone();
f.d.resolve(&rid, Some("user"), agent::Decision::Approve)
.unwrap();
assert!(f.d.reminder("codex:host").is_none());
let mut buf = Vec::new();
let exit =
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert_eq!(exit, ipc::exit::NOTHING_RUNNING);
assert!(f.d.unseen(&f.d.lock(), "codex:host", &[]).is_empty());
}
#[test]
fn progress_tells_the_turn_its_tool_calls_and_what_it_waits_for() {
let f = fixture();
let (actor, _commands) = mpsc::unbounded_channel();
let (reply, _decision) = oneshot::channel();
f.d.update("test", |t| t.status = Status::Running);
f.d.emit("test", "turn_started", json!({"turn": 1}));
f.d.emit(
"test",
"tool",
json!({"name": "Bash", "detail": "cargo test"}),
);
f.d.emit("test", "tool", json!({"name": "Read", "detail": ""}));
let mut st = f.d.lock();
st.tasks.get_mut("test").unwrap().actor = Some(actor);
st.approvals.insert(
"r1".into(),
Pending {
task: "test".into(),
approver: "codex:host".into(),
summary: "Bash: rm -rf build".into(),
delivered: false,
reply,
},
);
let progress = f.d.progress(&st, &st.tasks["test"], "codex:host");
assert!(
progress.starts_with("turn 1, 0s, 2 tool call(s), last 0s ago: Read;"),
"{progress}"
);
assert!(
progress.ends_with("waiting for your approval of r1"),
"{progress}"
);
let other = f.d.progress(&st, &st.tasks["test"], "user");
assert!(
other.ends_with("waiting for approval of r1 from codex:host"),
"{other}"
);
drop(st);
f.d.emit("test", "turn_ended", json!({"turn": 1, "ok": true}));
f.d.update("test", |t| t.turns = 1);
let st = f.d.lock();
let between = f.d.progress(&st, &st.tasks["test"], "codex:host");
drop(st);
assert!(
between.starts_with("turn 1 ended 0s ago; its background work still runs"),
"{between}"
);
f.d.emit("test", "tool", json!({"name": "monitor", "detail": ""}));
let st = f.d.lock();
let next = f.d.progress(&st, &st.tasks["test"], "codex:host");
assert!(
next.starts_with("turn 2, 0s, 1 tool call(s), last 0s ago: monitor;"),
"{next}"
);
drop(st);
f.d.emit("test", "turn_ended", json!({"turn": 2, "ok": true}));
let mut st = f.d.lock();
st.tasks.get_mut("test").unwrap().releasing = true;
let done = f.d.progress(&st, &st.tasks["test"], "codex:host");
assert!(!done.contains("turn"), "{done}");
}
#[test]
fn what_an_agent_says_is_logged_on_one_line() {
let f = fixture();
f.d.emit(
"test",
"said",
json!({"text": "Running the tests.\nThen the docs."}),
);
let events = f.d.store.events("test");
assert!(f.d.lock().tasks["test"].deliverable.is_empty());
assert_eq!(
render::event_line(&events[0], Agent::Codex),
"[test codex] says: Running the tests. ↵ Then the docs."
);
}
#[test]
fn durations_read_at_a_glance() {
assert_eq!(render::duration(45_000), "45s");
assert_eq!(render::duration(252_000), "4m12s");
assert_eq!(render::duration(3_900_000), "1h05m");
}
#[tokio::test]
async fn agents_approve_only_requests_they_have_read_in_full() {
let f = fixture();
f.d.update("test", |t| t.tier = ipc::Tier::Work);
let request = |summary: &str| {
let (reply, decision) = oneshot::channel();
f.d.request_approval(
"test",
agent::Approval {
summary: summary.into(),
detail: "Bash\ncommand:\nrm -rf build\nrm -rf ~".into(),
reply,
withdrawn: oneshot::channel().1,
},
);
let rid = f.d.lock().approvals.keys().next().unwrap().clone();
(rid, decision)
};
let (rid, decision) = request("Bash: rm -rf build ↵ rm -rf ~");
let refused =
f.d.resolve(&rid, Some("codex:host"), agent::Decision::Approve)
.unwrap_err();
assert!(refused.to_string().contains("read"), "{refused}");
let mut buf = Vec::new();
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
let text = replies(&buf);
assert!(
text.contains("<request>\nBash\ncommand:\nrm -rf build\nrm -rf ~\n</request>"),
"{text}"
);
f.d.resolve(&rid, Some("codex:host"), agent::Decision::Approve)
.unwrap();
assert!(matches!(decision.await.unwrap(), agent::Decision::Approve));
let (rid, _decision) = request("Bash: ls");
f.d.resolve(&rid, Some("user"), agent::Decision::Approve)
.unwrap();
let (rid, _decision) = request("Bash: ls");
f.d.resolve(&rid, Some("codex:host"), agent::Decision::Deny("no".into()))
.unwrap();
}
#[tokio::test]
async fn a_withdrawn_request_is_dropped_undecided() {
let f = fixture();
f.d.update("test", |t| t.tier = ipc::Tier::Work);
let (reply, decision) = oneshot::channel();
let (withdraw, withdrawn) = oneshot::channel();
f.d.request_approval(
"test",
agent::Approval {
summary: "Bash: curl example.com".into(),
detail: "WITHDRAWN_REQUEST".into(),
reply,
withdrawn,
},
);
let rid = f.d.lock().approvals.keys().next().unwrap().clone();
withdraw.send(()).unwrap();
assert!(decision.await.is_err());
assert!(f.d.lock().approvals.is_empty());
let last = f.d.store.events("test").pop().unwrap();
assert_eq!(last.kind, "withdrawn");
assert_eq!(last.data["id"], json!(rid));
let mut buf = Vec::new();
f.d.inbox("codex:host", &[], &mut Out::new(&mut buf))
.await
.unwrap();
assert!(!replies(&buf).contains("WITHDRAWN_REQUEST"));
let late = f.d.resolve(&rid, Some("user"), agent::Decision::Approve);
assert!(late.unwrap_err().to_string().contains("withdrawn"));
}
#[test]
fn reminders_list_news_but_not_for_hosts_that_get_it_pushed() {
let f = fixture();
f.d.emit("test", "message", json!({"to": "other", "text": "a note"}));
assert!(f.d.reminder("codex:host").is_none());
f.d.emit(
"test",
"question",
json!({"id": "q1", "text": "Which port?"}),
);
let (text, _) = f.d.reminder("codex:host").unwrap();
assert_eq!(
text,
"- test (codex) asks q1: Which port?\nTake them with `axiopera inbox`."
);
let host = store::CodexHost {
exe: "codex".into(),
codex_home: None,
thread: "host".into(),
doorbell: false,
};
f.d.lock()
.ledger
.codex_hosts
.insert("codex:host".into(), host);
assert!(f.d.reminder("codex:host").is_none());
}
#[tokio::test]
async fn watch_merges_news_that_comes_together() {
let f = fixture();
f.d.emit(
"test",
"question",
json!({"id": "q1", "text": "Which port?"}),
);
f.d.emit("test", "stalled", json!({"quiet_secs": 600}));
f.d.emit("test", "message", json!({"to": "other", "text": "a note"}));
let (client, mut server) = tokio::io::duplex(1 << 16);
let d = f.d.clone();
let watcher =
tokio::spawn(async move { d.watch("codex:host", &mut Out::new(&mut server)).await });
let mut reader = ipc::Reader::new(client);
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no line");
};
assert_eq!(text, "test (codex) asks q1: Which port? (+1 more)");
f.d.emit("test", "stalled", json!({"quiet_secs": 900}));
tokio::time::sleep(Duration::from_millis(100)).await;
f.d.inbox("codex:host", &[], &mut Out::new(&mut Vec::new()))
.await
.unwrap();
tokio::time::sleep(WATCH_GAP).await;
f.d.emit(
"test",
"question",
json!({"id": "q2", "text": "Which host?"}),
);
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no line");
};
assert_eq!(text, "test (codex) asks q2: Which host?");
f.d.emit("test", "failed", json!({"error": "gone"}));
tokio::time::sleep(Duration::from_millis(100)).await;
f.d.lock().tasks.remove("test");
tokio::time::sleep(WATCH_GAP + Duration::from_millis(500)).await;
assert!(!watcher.is_finished());
watcher.abort();
assert!(reader.read::<Reply>().await.unwrap().is_none());
}
#[tokio::test(start_paused = true)]
async fn a_sub_agents_watch_ends_a_minute_after_its_work_is_done_and_taken() {
let f = fixture();
f.d.update("test", |t| t.parent = "task:boss".into());
let (actor, _rx) = mpsc::unbounded_channel();
f.d.lock().tasks.get_mut("test").unwrap().actor = Some(actor);
let (client, mut server) = tokio::io::duplex(1 << 16);
let d = f.d.clone();
let watcher =
tokio::spawn(async move { d.watch("task:boss", &mut Out::new(&mut server)).await });
let host = {
let d = f.d.clone();
tokio::spawn(async move { d.watch("codex:host", &mut Out::new(&mut Vec::new())).await })
};
tokio::time::sleep(Duration::from_secs(120)).await;
assert!(!watcher.is_finished(), "a sub-task still runs");
f.d.emit(
"test",
"question",
json!({"id": "q1", "text": "Which port?"}),
);
f.d.lock().tasks.get_mut("test").unwrap().actor = None;
let mut reader = ipc::Reader::new(client);
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no line");
};
assert_eq!(text, "test (codex) asks q1: Which port?");
tokio::time::sleep(Duration::from_secs(120)).await;
assert!(!watcher.is_finished(), "the question is not taken yet");
f.d.inbox("task:boss", &[], &mut Out::new(&mut Vec::new()))
.await
.unwrap();
tokio::time::sleep(WATCH_IDLE - Duration::from_secs(1)).await;
assert!(!watcher.is_finished());
tokio::time::sleep(WATCH_GAP * 2).await;
assert_eq!(watcher.await.unwrap().unwrap(), ipc::exit::NOTHING_RUNNING);
assert!(!host.is_finished(), "a session's watch runs until stopped");
host.abort();
}
#[tokio::test(start_paused = true)]
async fn status_watch_tracks_work_without_taking_news_and_exits_after_release() {
let f = fixture_with(&[
json!({"id": "old"}),
json!({"id": "other", "parent": "codex:other"}),
]);
f.d.update("test", |t| t.status = Status::Queued);
f.d.lock()
.tasks
.get_mut("test")
.unwrap()
.pending
.push("start".into());
let (other_actor, _other_rx) = mpsc::unbounded_channel();
f.d.lock().tasks.get_mut("other").unwrap().actor = Some(other_actor);
let (client, mut server) = tokio::io::duplex(1 << 16);
let d = f.d.clone();
let watcher = tokio::spawn(async move {
d.watch_status("codex:host", &mut Out::new(&mut server))
.await
});
let mut reader = ipc::Reader::new(client);
let Some(Reply::Out { text, data }) = reader.read::<Reply>().await.unwrap() else {
panic!("no queued status");
};
assert!(text.contains("queued"), "{text}");
assert_eq!(data["id"], "test");
let (actor, _rx) = mpsc::unbounded_channel();
{
let mut st = f.d.lock();
let e = st.tasks.get_mut("test").unwrap();
e.pending.clear();
e.actor = Some(actor);
}
f.d.update("test", |t| t.status = Status::Running);
f.d.emit(
"test",
"tool",
json!({"name": "Bash", "detail": "cargo test"}),
);
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no progress");
};
assert!(
text.contains("1 tool call(s)") && text.contains("cargo test"),
"{text}"
);
f.d.update("test", |t| {
t.status = Status::Idle;
t.turns = 1;
});
f.d.emit("test", "turn_ended", json!({"turn": 1, "status": "idle"}));
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no background work status");
};
assert!(text.contains("background work still runs"), "{text}");
assert!(!watcher.is_finished());
assert_eq!(f.d.unseen(&f.d.lock(), "codex:host", &[]).len(), 1);
let events = f.d.unseen(&f.d.lock(), "codex:host", &[]);
f.d.mark_seen("codex:host", &events, &[("test".into(), 1)], &[])
.unwrap();
f.d.emit("test", "stalled", json!({"quiet_secs": 600}));
f.d.lock().tasks.get_mut("test").unwrap().actor = None;
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no final status");
};
assert!(
text.contains("idle") && !text.contains("background work"),
"{text}"
);
let Some(Reply::Out { text, .. }) = reader.read::<Reply>().await.unwrap() else {
panic!("no end marker");
};
assert_eq!(text, "no running sub-tasks");
assert_eq!(watcher.await.unwrap().unwrap(), ipc::exit::OK);
assert_eq!(f.d.unseen(&f.d.lock(), "codex:host", &[]).len(), 1);
assert_eq!(f.d.lock().tasks["test"].task.delivered, 1);
assert!(reader.read::<Reply>().await.unwrap().is_none());
}
#[tokio::test]
async fn status_watch_with_no_work_exits_immediately() {
let f = fixture();
let mut buf = Vec::new();
assert_eq!(
f.d.watch_status("codex:host", &mut Out::new(&mut buf))
.await
.unwrap(),
ipc::exit::OK
);
assert_eq!(replies(&buf), "no running sub-tasks");
}
#[test]
fn the_request_log_keeps_argument_names_and_ages_out_with_gc() {
let f = fixture();
std::fs::create_dir_all(f.home.join("daemon")).unwrap();
let request = Request::Wait {
tasks: vec!["secret-name".into()],
timeout_ms: None,
};
let mut entry = f.d.request_entry("task:test", &request);
entry.as_mut().unwrap().insert("ts".into(), json!(1_000));
f.d.record(entry, Some(ipc::exit::NOTHING_NEW), None, 42);
assert!(f.d.request_entry("codex:host", &Request::Pending).is_none());
let request = Request::Send {
task: "test".into(),
message: "do the secret thing".into(),
tier: None,
};
let mut entry = f.d.request_entry("codex:host", &request);
entry.as_mut().unwrap().insert("ts".into(), json!(2_000));
f.d.record(entry, Some(ipc::exit::ERROR), Some(ErrorCode::NotFound), 0);
let path = f.home.join("daemon").join("requests.jsonl");
let text = std::fs::read_to_string(&path).unwrap();
assert!(!text.contains("secret"));
let lines: Vec<Value> = text
.lines()
.map(|l| serde_json::from_str(l).unwrap())
.collect();
assert_eq!(lines[0]["caller"], "codex sub-agent");
assert_eq!(lines[0]["op"], "wait");
assert_eq!(lines[0]["args"], json!(["tasks"]));
assert_eq!(lines[0]["exit"], 3);
assert_eq!(lines[0]["bytes"], 42);
assert_eq!(lines[1]["caller"], "codex");
assert_eq!(lines[1]["args"], json!(["message", "task"]));
assert_eq!(lines[1]["error"], "not_found");
f.d.trim_requests(lines[1]["ts"].as_u64().unwrap()).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap().lines().count(), 1);
f.d.trim_requests(u64::MAX).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), "");
}
#[test]
fn gc_takes_children_before_parents_and_leaves_running_and_recent_tasks() {
let now = store::now_ms();
let f = fixture_with(&[
json!({"id": "kid", "parent": "task:test/grok:native"}),
json!({"id": "busy"}),
json!({"id": "mom"}),
json!({"id": "baby", "parent": "task:mom/codex:native", "updated_ms": now}),
]);
f.d.lock().tasks.get_mut("busy").unwrap().task.status = Status::Running;
let left = |d: &Daemon| {
let mut ids: Vec<String> = d.lock().tasks.keys().cloned().collect();
ids.sort();
ids
};
assert_eq!(f.d.collect(1_000).unwrap(), 2);
assert_eq!(left(&f.d), ["baby", "busy", "mom"]);
assert!(!f.home.join("tasks").join("kid").exists());
assert_eq!(f.d.collect(u64::MAX).unwrap(), 2);
assert_eq!(left(&f.d), ["busy"]);
}
#[tokio::test]
async fn a_note_kept_for_a_stopped_task_gives_it_time_before_gc() {
let f = fixture_with(&[json!({"id": "sib"})]);
f.d.tell(
"task:sib",
"test",
"the API moved".into(),
&mut Out::new(&mut Vec::new()),
)
.await
.unwrap();
assert_eq!(f.d.lock().tasks["test"].mail.len(), 1);
f.d.collect(1_000).unwrap();
assert!(f.d.lock().tasks.contains_key("test"));
}
#[test]
fn top_level_spawns_stop_at_the_limit_while_nested_ones_go_on() {
let f = fixture_with(&[
json!({"id": "a"}),
json!({"id": "b", "parent": "claude:other"}),
json!({"id": "kid", "parent": "task:a"}),
]);
let d = Daemon::load(
Options {
concurrency: 2,
..options(&f.home)
},
None,
)
.unwrap();
{
let mut st = d.lock();
for id in ["a", "kid"] {
st.tasks.get_mut(id).unwrap().actor = Some(mpsc::unbounded_channel().0);
}
st.tasks.get_mut("b").unwrap().pending.push("go".into());
}
let why = d.at_limit(&d.lock(), "codex:host").unwrap();
assert!(why.starts_with("2 top-level tasks"), "{why}");
assert!(why.contains("config.toml"), "{why}");
assert!(d.at_limit(&d.lock(), "task:a").is_none());
assert!(d.at_limit(&d.lock(), "task:a/grok:native").is_none());
d.lock().tasks.get_mut("b").unwrap().pending.clear();
assert!(d.at_limit(&d.lock(), "codex:host").is_none());
}
#[test]
fn sequence_numbers_go_on_after_gc_removed_every_task() {
let f = fixture();
f.d.emit("test", "turn_ended", json!({"turn": 1}));
let before = f.d.lock().seq;
assert_eq!(f.d.collect(u64::MAX).unwrap(), 1);
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.lock().seq, before);
}
#[test]
fn a_reply_passes_over_news_of_a_task_gc_removed_meanwhile() {
let f = fixture();
let event = |task: &str, seq: u64| -> Event {
serde_json::from_value(json!({
"seq": seq, "ts": 0, "task": task, "kind": "failed", "error": "boom",
}))
.unwrap()
};
let b = f.d.batch(&[], &[event("gone", 1), event("test", 2)]);
assert_eq!(b.seen.len(), 2);
assert_eq!(b.items.len(), 1);
assert_eq!(b.items[0].0.task, "test");
f.d.mark_seen(
"codex:host",
&b.seen,
&[("gone".into(), 1), ("test".into(), 2)],
&[],
)
.unwrap();
assert_eq!(f.d.lock().tasks["test"].task.delivered, 2);
assert_eq!(f.d.lock().ledger.cursors["codex:host|test"], 2);
assert!(
!f.d.lock()
.ledger
.cursors
.keys()
.any(|k| k.ends_with("|gone"))
);
}
#[test]
fn task_files_from_before_worktrees_went_still_load() {
let task: Task = serde_json::from_value(json!({
"id": "old", "parent": "user", "agent": "claude", "tier": "work",
"cwd": ".", "status": "idle", "turns": 1, "delivered": 1,
"changed_files": [], "worktree": null, "stalled": false, "detached": false,
"created_ms": 0, "updated_ms": 0,
}))
.unwrap();
assert_eq!(task.id, "old");
}
#[test]
fn the_daemon_log_drops_entries_before_the_cutoff_with_their_extra_lines() {
let dir = std::env::temp_dir().join(format!("axiopera-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("daemon.log");
assert!(super::trim_log(&path, 2_000).is_ok());
std::fs::write(
&path,
"1000 old\nmore of old\n2000 kept\nmore of kept\n3000 new\n",
)
.unwrap();
super::trim_log(&path, 1_500).unwrap();
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"2000 kept\nmore of kept\n3000 new\n"
);
super::trim_log(&path, 9_000).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), "");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn a_running_daemon_trims_logs_and_keeps_writing() {
let f = fixture();
let path = f.d.opts.home.join("daemon/daemon.log");
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, "1000 old\n2000 retained\n").unwrap();
std::fs::write(path.with_extension("log.1"), "1000 previous\n").unwrap();
*f.d.log.lock().unwrap() = Some(
std::fs::OpenOptions::new()
.append(true)
.open(&path)
.unwrap(),
);
f.d.trim_daemon_log(1500).unwrap();
f.d.log("after trim");
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.starts_with("2000 retained\n") && text.ends_with("after trim\n"));
assert_eq!(
std::fs::read_to_string(path.with_extension("log.1")).unwrap(),
""
);
}
#[test]
fn daemon_logging_recovers_after_a_temporary_open_failure() {
let f = fixture();
let path = f.d.opts.home.join("daemon/daemon.log");
std::fs::create_dir_all(&path).unwrap();
assert!(f.d.trim_daemon_log(0).is_err());
f.d.log("cannot open yet");
std::fs::remove_dir(&path).unwrap();
f.d.log("logging recovered");
assert!(
std::fs::read_to_string(path)
.unwrap()
.ends_with("logging recovered\n")
);
}
fn scanned_unseen(d: &Daemon, st: &State, caller: &str, only: &[String]) -> Vec<Event> {
let ids = d.children(st, caller, only);
let mut ready = Vec::new();
for (id, entry) in &st.tasks {
let child = ids.contains(id);
let below = only.is_empty() || only.iter().any(|root| within(st, &entry.task, root));
if !child && !below {
continue;
}
let cursor = st
.ledger
.cursors
.get(&cursor_key(caller, id))
.copied()
.unwrap_or(0);
ready.extend(
entry
.deliverable
.iter()
.filter(|e| {
e.seq > cursor
&& if e.kind == "approval_request" {
e.data.get("approver").and_then(Value::as_str) == Some(caller)
} else {
child
}
})
.cloned(),
);
}
ready.sort_by_key(|e| e.seq);
ready
}
#[test]
fn indexed_delivery_preserves_historical_routes_filters_and_cursors_after_reload() {
let f = fixture_with(&[
json!({"id": "child", "parent": "task:test"}),
json!({"id": "native", "parent": "task:child/grok:native"}),
json!({"id": "other", "parent": "codex:elsewhere"}),
]);
for _ in 0..3 {
for task in ["test", "child", "native", "other"] {
f.d.emit(task, "question", json!({"id": "q", "text": "hi"}));
for approver in ["codex:host", "task:test", "codex:elsewhere"] {
f.d.emit(
task,
"approval_request",
json!({"id": "historic", "approver": approver}),
);
}
}
}
let reloaded = Daemon::load(options(&f.home), None).unwrap();
for d in [f.d.as_ref(), &reloaded] {
let mut st = d.lock();
for caller in [
"codex:host",
"task:test",
"codex:elsewhere",
"task:child/grok:native",
] {
for only in [
vec![],
vec!["test".to_string()],
vec!["child".into(), "other".into()],
] {
for cursor in [0, 1, 8, 24, 48, 100] {
for task in ["test", "child", "native", "other"] {
st.ledger.cursors.insert(cursor_key(caller, task), cursor);
}
assert_eq!(
serde_json::to_value(d.unseen(&st, caller, &only)).unwrap(),
serde_json::to_value(scanned_unseen(d, &st, caller, &only)).unwrap()
);
}
}
}
}
}
#[test]
#[ignore = "small repeatable performance measurement; run with --release --ignored --nocapture"]
fn delivery_performance() {
use std::hint::black_box;
use std::io::{BufWriter, Write};
let f = fixture_with(
&(0..80)
.map(|i| {
json!({"id": format!("t{i}"),
"parent": if i < 40 { "codex:host" } else { "codex:other" }})
})
.collect::<Vec<_>>(),
);
{
let mut st = f.d.lock();
for entry in st.tasks.values_mut() {
entry.deliverable = (1..=1000)
.map(|seq| Event {
seq,
ts: 0,
task: entry.task.id.clone(),
kind: "question".into(),
data: Map::new(),
})
.collect();
entry.approval_events = delivery::approval_index(&entry.deliverable);
}
for id in st.tasks.keys().cloned().collect::<Vec<_>>() {
st.ledger
.cursors
.insert(cursor_key("codex:host", &id), 1000);
}
let at = Instant::now();
for _ in 0..200 {
black_box(scanned_unseen(&f.d, &st, "codex:host", &[]));
}
let before = at.elapsed();
let at = Instant::now();
for _ in 0..200 {
black_box(f.d.unseen(&st, "codex:host", &[]));
}
println!(
"unseen: 81 tasks x 1000 consumed events, 200 queries: scan={before:?}, indexed={:?}",
at.elapsed()
);
}
f.d.file_changed("test", "repeat.rs").unwrap();
let at = Instant::now();
for _ in 0..200 {
f.d.update("test", |t| {
if !t.changed_files.iter().any(|p| p == "repeat.rs") {
t.changed_files.push("repeat.rs".into());
}
});
}
let before = at.elapsed();
let at = Instant::now();
for _ in 0..200 {
f.d.file_changed("test", "repeat.rs").unwrap();
}
println!(
"FileChanged: 200 duplicate updates: saved={before:?}, skipped={:?}",
at.elapsed()
);
let path = f.d.store.events_path("test");
{
let mut file = BufWriter::new(File::create(&path).unwrap());
for seq in 1..=20_000 {
let event = Event {
seq,
ts: 0,
task: "test".into(),
kind: "said".into(),
data: json!({"text": "x".repeat(128)})
.as_object()
.unwrap()
.clone(),
};
serde_json::to_writer(&mut file, &event).unwrap();
writeln!(file).unwrap();
}
}
let at = Instant::now();
for _ in 0..20 {
black_box(
f.d.store
.events("test")
.into_iter()
.rev()
.take(10)
.collect::<Vec<_>>(),
);
}
let before = at.elapsed();
let at = Instant::now();
for _ in 0..20 {
black_box(delivery::tail_events(&path, 10).unwrap());
}
println!(
"log: 20000 events, 20 tail(10) reads: full={before:?}, tail={:?}",
at.elapsed()
);
for turn in 1..=20 {
f.d.store
.save_turn("test", turn, &"x".repeat(128 * 1024))
.unwrap();
}
let news: Vec<_> = (1..=20)
.map(|turn| Event {
seq: turn,
ts: 0,
task: "test".into(),
kind: "turn_ended".into(),
data: json!({"turn": turn, "ok": true})
.as_object()
.unwrap()
.clone(),
})
.collect();
let at = Instant::now();
for _ in 0..20 {
let lines = news
.iter()
.map(|e| {
let body =
std::fs::read_to_string(f.d.store.turn_path("test", e.seq as u32)).unwrap();
format!(
"test finished turn {}: {}",
e.seq,
agent::clip(&body, render::PREVIEW)
)
})
.collect::<Vec<_>>();
black_box(render::summary(lines.into_iter()));
}
let before = at.elapsed();
let at = Instant::now();
for _ in 0..20 {
black_box(f.d.news_text(&f.d.lock(), &news));
}
println!(
"reminder: 20 results x 128KiB, 20 renders (including cold cache): full={before:?}, limited_cached={:?}",
at.elapsed()
);
}
#[test]
fn failed_gc_keeps_the_task_and_cursor_for_retry() {
let f = fixture();
let path = f.home.join("tasks/test");
let backup = f.home.join("saved-task");
std::fs::rename(&path, &backup).unwrap();
std::fs::write(&path, "occupied").unwrap();
let key = cursor_key("codex:host", "test");
f.d.lock().ledger.cursors.insert(key.clone(), 0);
assert!(f.d.collect(u64::MAX).is_err());
assert!(f.d.lock().tasks.contains_key("test"));
assert!(f.d.lock().ledger.cursors.contains_key(&key));
std::fs::remove_file(&path).unwrap();
std::fs::rename(backup, &path).unwrap();
assert_eq!(f.d.collect(u64::MAX).unwrap(), 1);
}
#[test]
fn missing_directory_gc_is_idempotent() {
let f = fixture();
std::fs::remove_dir_all(f.home.join("tasks/test")).unwrap();
assert_eq!(f.d.collect(u64::MAX).unwrap(), 1);
}
#[test]
fn invalid_tasks_are_quarantined_and_valid_events_after_corruption_recover() {
let f = fixture();
let root = f.home.join("tasks");
for name in ["missing", "broken", "mismatch"] {
std::fs::create_dir(root.join(name)).unwrap();
}
std::fs::write(root.join("missing/prompt.md"), "keep me").unwrap();
std::fs::write(root.join("broken/task.json"), "{bad").unwrap();
std::fs::copy(root.join("test/task.json"), root.join("mismatch/task.json")).unwrap();
let path = f.d.store.events_path("test");
let data = b"{\"seq\":1,\"ts\":0,\"task\":\"test\",\"kind\":\"tool\"}\n\xff\n{\"seq\":2,\"ts\":0,\"task\":\"test\",\"kind\":\"turn_ended\",\"turn\":1}\n";
std::fs::write(&path, data).unwrap();
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.lock().tasks.len(), 1);
assert_eq!(again.lock().tasks["test"].deliverable[0].kind, "turn_ended");
assert_eq!(again.lock().seq, 2);
let dirs = std::fs::read_dir(f.home.join("quarantine"))
.unwrap()
.map(|d| d.unwrap().path())
.collect::<Vec<_>>();
assert_eq!(dirs.len(), 3);
assert_eq!(std::fs::read(&path).unwrap(), data);
}
#[test]
fn torn_event_tail_is_backed_up_and_next_append_stays_readable() {
for tail in [b"{\"seq\":9".as_slice(), b"\xe4\xbd".as_slice()] {
let f = fixture();
f.d.try_emit("test", "failed", json!({"error":"first"}))
.unwrap();
let path = f.d.store.events_path("test");
std::fs::OpenOptions::new()
.append(true)
.open(&path)
.unwrap()
.write_all(tail)
.unwrap();
let again = Daemon::load(options(&f.home), None).unwrap();
again
.try_emit("test", "failed", json!({"error":"second"}))
.unwrap();
assert_eq!(again.store.events("test").len(), 2);
let backups = std::fs::read_dir(f.home.join("tasks/test"))
.unwrap()
.filter_map(|d| {
let p = d.unwrap().path();
p.file_name()
.unwrap()
.to_string_lossy()
.starts_with("events.tail-")
.then_some(p)
})
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
assert_eq!(std::fs::read(&backups[0]).unwrap(), tail);
}
}
#[test]
fn complete_event_without_newline_is_recovered_once() {
let f = fixture();
std::fs::write(
f.d.store.events_path("test"),
br#"{"seq":42,"ts":0,"task":"test","kind":"failed"}"#,
)
.unwrap();
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.lock().seq, 42);
again.try_emit("test", "failed", json!({})).unwrap();
assert_eq!(
again
.store
.events("test")
.iter()
.map(|e| e.seq)
.collect::<Vec<_>>(),
[42, 43]
);
}
#[test]
fn restart_removes_missing_malformed_and_future_cursors() {
let f = fixture();
f.d.try_emit("test", "failed", json!({})).unwrap();
let mut ledger = Ledger::default();
for (key, seq) in [
("codex:host|test", 1),
("codex:other|gone", 1),
("bad", 0),
("|test", 0),
("user|test", 99),
] {
ledger.cursors.insert(key.into(), seq);
}
ledger.save(&f.home).unwrap();
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.lock().ledger.cursors.len(), 1);
assert_eq!(Ledger::load(&f.home).unwrap().cursors.len(), 1);
}
#[test]
fn a_damaged_ledger_stops_recovery_without_overwriting_it() {
let f = fixture();
let path = f.home.join("ledger.json");
for data in [b"{\"cursors\":".as_slice(), b"\xff".as_slice()] {
std::fs::write(&path, data).unwrap();
let error = Daemon::load(options(&f.home), None).err().unwrap();
assert!(format!("{error:#}").contains("ledger.json"));
assert_eq!(std::fs::read(&path).unwrap(), data);
}
}
#[cfg(windows)]
#[test]
fn an_unreadable_ledger_is_preserved_for_retry() {
use std::os::windows::fs::OpenOptionsExt;
let f = fixture();
f.d.try_emit("test", "failed", json!({})).unwrap();
let events = f.d.lock().tasks["test"].deliverable.clone();
f.d.mark_seen("codex:host", &events, &[], &[]).unwrap();
let path = f.home.join("ledger.json");
let before = std::fs::read(&path).unwrap();
let held = std::fs::OpenOptions::new()
.read(true)
.share_mode(0)
.open(&path)
.unwrap();
assert!(Daemon::load(options(&f.home), None).is_err());
drop(held);
assert_eq!(std::fs::read(&path).unwrap(), before);
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(
again.lock().ledger.cursors["codex:host|test"],
events[0].seq
);
}
#[tokio::test]
async fn accepted_input_and_mail_survive_restart_without_starting_an_agent() {
let f = fixture_with(&[json!({"id":"sibling"})]);
f.d.lock().stopping = true;
f.d.input("codex:host", "test", "first".into(), None, None)
.await
.unwrap();
f.d.input("codex:host", "test", "second".into(), None, None)
.await
.unwrap();
f.d.tell(
"task:sibling",
"test",
"note".into(),
&mut Out::new(&mut Vec::new()),
)
.await
.unwrap();
let mut opts = options(&f.home);
opts.concurrency = 1;
let again = Arc::new(Daemon::load(opts, None).unwrap());
{
let st = again.lock();
assert!(st.tasks["test"].pending.is_empty());
assert_eq!(st.tasks["test"].task.pending_inputs, ["first", "second"]);
assert!(again.at_limit(&st, "codex:other").is_none());
assert!(!again.live(&st, "codex:host", &[]));
}
assert!(
again
.input(
"codex:host",
"test",
"change tier".into(),
Some(ipc::Tier::Work),
Some(Vec::new())
)
.await
.is_err()
);
assert_eq!(
again.lock().tasks["test"].task.pending_inputs,
["first", "second"]
);
assert_eq!(again.lock().tasks["test"].mail.len(), 1);
again.schedule("test");
assert!(again.lock().tasks["test"].actor.is_none());
again.lock().stopping = true;
again
.input("codex:host", "test", "third".into(), None, Some(Vec::new()))
.await
.unwrap();
assert_eq!(
again.lock().tasks["test"].pending,
["first", "second", "third"]
);
assert_eq!(again.collect(u64::MAX).unwrap(), 1);
assert!(again.lock().tasks.contains_key("test"));
}
#[tokio::test]
async fn input_during_interrupted_release_schedules_a_fresh_actor() {
let f = fixture();
let (tx, _rx) = mpsc::unbounded_channel();
{
let mut st = f.d.lock();
st.running = 1;
let entry = st.tasks.get_mut("test").unwrap();
entry.actor = Some(tx);
entry.releasing = true;
entry.task.status = Status::Running;
entry.task.pending_inputs.push("earlier".into());
}
f.d.stop_tree("test", "parent timed out", "timeout", true)
.unwrap();
f.d.input(
"codex:host",
"test",
"continue".into(),
None,
Some(Vec::new()),
)
.await
.unwrap();
assert_eq!(f.d.lock().tasks["test"].task.status, Status::Queued);
f.d.actor_done("test");
let mut st = f.d.lock();
assert!(st.tasks["test"].actor.is_some());
assert_eq!(st.tasks["test"].pending, ["earlier", "continue"]);
assert_eq!(st.running, 1);
st.tasks.get_mut("test").unwrap().env = None;
}
#[tokio::test]
async fn answers_and_their_input_are_committed_together() {
let f = fixture();
f.d.lock().stopping = true;
f.d.try_update("test", |t| {
t.questions.push(store::Question {
id: "qtest".into(),
text: "Which option?".into(),
answer: None,
asked_ms: 0,
});
})
.unwrap();
let tmp = f.home.join("tasks/test/task.json.tmp");
std::fs::create_dir(&tmp).unwrap();
let mut output = Vec::new();
assert!(
f.d.reply(
"codex:host",
"qtest",
"first".into(),
None,
&mut Out::new(&mut output)
)
.await
.is_err()
);
{
let st = f.d.lock();
assert!(st.tasks["test"].task.questions[0].answer.is_none());
assert!(st.tasks["test"].task.pending_inputs.is_empty());
}
assert!(output.is_empty());
std::fs::remove_dir(tmp).unwrap();
let (first, second) = tokio::join!(
f.d.input_with_answer(
"codex:host",
"test",
"first".into(),
None,
None,
Some(("qtest", "first"))
),
f.d.input_with_answer(
"codex:host",
"test",
"second".into(),
None,
None,
Some(("qtest", "second"))
)
);
assert_ne!(first.is_ok(), second.is_ok());
let again = Daemon::load(options(&f.home), None).unwrap();
let st = again.lock();
let task = &st.tasks["test"].task;
assert_eq!(task.pending_inputs.len(), 1);
assert_eq!(
task.questions[0].answer.as_ref(),
task.pending_inputs.first()
);
}
#[tokio::test]
async fn cancelling_a_queued_continuation_prevents_scheduling() {
let f = fixture();
f.d.lock().stopping = true;
f.d.try_cancel_tree("test", "cancelled", "user").unwrap();
f.d.input("user", "test", "resume".into(), None, Some(Vec::new()))
.await
.unwrap();
assert_eq!(f.d.lock().tasks["test"].task.status, Status::Queued);
f.d.try_cancel_tree("test", "cancelled again", "user")
.unwrap();
f.d.lock().stopping = false;
f.d.schedule("test");
let st = f.d.lock();
assert!(st.tasks["test"].pending.is_empty());
assert!(st.tasks["test"].actor.is_none());
assert_eq!(st.tasks["test"].task.status, Status::Cancelled);
}
#[tokio::test]
async fn cancelled_tree_can_resume_after_restart_with_input_mail_and_answers() {
let f = fixture_with(&[
json!({"id":"kid", "parent":"task:test"}),
json!({"id":"sibling"}),
]);
f.d.lock().stopping = true;
for id in ["test", "kid"] {
f.d.try_update(id, |t| {
t.session = Some(format!("session-{id}"));
t.pending_mail.push("saved note".into());
})
.unwrap();
f.d.input("user", id, "earlier".into(), None, None)
.await
.unwrap();
}
f.d.try_update("test", |t| {
t.questions.push(store::Question {
id: "qresume".into(),
text: "Continue?".into(),
answer: None,
asked_ms: 0,
})
})
.unwrap();
f.d.try_cancel_tree("test", "cancelled by request", "user")
.unwrap();
let again = Arc::new(Daemon::load(options(&f.home), None).unwrap());
again.lock().stopping = true;
for id in ["test", "kid"] {
assert_eq!(again.lock().tasks[id].task.status, Status::Cancelled);
assert!(again.lock().tasks[id].pending.is_empty());
assert_eq!(again.lock().tasks[id].mail, ["saved note"]);
}
assert_eq!(again.collect(u64::MAX).unwrap(), 1);
again
.reply(
"user",
"qresume",
"yes".into(),
None,
&mut Out::new(&mut Vec::new()),
)
.await
.unwrap();
assert_eq!(again.lock().tasks["test"].pending.len(), 2);
assert_eq!(
again.lock().tasks["test"].task.questions[0]
.answer
.as_deref(),
Some("yes")
);
assert!(again.lock().tasks["kid"].pending.is_empty());
again
.input("task:test", "kid", "resume child".into(), None, None)
.await
.unwrap();
assert_eq!(
again.lock().tasks["kid"].pending,
["earlier", "resume child"]
);
assert_eq!(
again.lock().tasks["kid"].task.session.as_deref(),
Some("session-kid")
);
}
#[tokio::test]
async fn failed_input_and_question_saves_do_not_acknowledge_or_mutate_state() {
let f = fixture();
std::fs::create_dir(f.home.join("tasks/test/task.json.tmp")).unwrap();
assert!(
f.d.input("codex:host", "test", "lost".into(), None, None)
.await
.is_err()
);
let mut output = Vec::new();
assert!(
f.d.ask("task:test", "question".into(), &mut Out::new(&mut output))
.await
.is_err()
);
assert!(output.is_empty());
let st = f.d.lock();
assert!(st.tasks["test"].pending.is_empty());
assert!(st.tasks["test"].task.pending_inputs.is_empty());
assert!(st.tasks["test"].task.questions.is_empty());
}
#[test]
fn failed_event_and_message_writes_are_not_published() {
let f = fixture();
let mut rx = f.d.events.subscribe();
std::fs::create_dir(f.d.store.events_path("test")).unwrap();
assert!(f.d.try_emit("test", "failed", json!({})).is_err());
assert!(f.d.lock().tasks["test"].deliverable.is_empty());
assert!(rx.try_recv().is_err());
std::fs::remove_dir(f.d.store.events_path("test")).unwrap();
std::fs::write(f.home.join("tasks/test/messages"), "occupied").unwrap();
assert!(
f.d.emit_with("test", "turn_started", json!({}), Some("input"))
.is_err()
);
assert!(!f.d.lock().tasks["test"].in_turn);
assert!(f.d.store.events("test").is_empty());
}
#[test]
fn failed_ledger_save_does_not_advance_in_memory_cursors() {
let f = fixture();
f.d.try_emit("test", "failed", json!({})).unwrap();
let events = f.d.lock().tasks["test"].deliverable.clone();
std::fs::create_dir(f.home.join("ledger.json.tmp")).unwrap();
assert!(f.d.mark_seen("codex:host", &events, &[], &[]).is_err());
assert!(f.d.lock().ledger.cursors.is_empty());
std::fs::remove_dir(f.home.join("ledger.json.tmp")).unwrap();
f.d.mark_seen("codex:host", &events, &[], &[]).unwrap();
assert_eq!(
Ledger::load(&f.home).unwrap().cursors["codex:host|test"],
events[0].seq
);
}
#[test]
fn only_legacy_timeouts_are_migrated_to_resumable_tasks() {
let f = fixture_with(&[json!({"id":"manual"}), json!({"id":"unknown"})]);
for id in ["test", "manual", "unknown"] {
f.d.try_update(id, |t| t.status = Status::Cancelled)
.unwrap();
}
f.d.try_emit("test", "cancelled", json!({"by":"timeout"}))
.unwrap();
f.d.try_emit("manual", "cancelled", json!({"by":"user"}))
.unwrap();
let again = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(again.lock().tasks["test"].task.status, Status::Interrupted);
assert_eq!(again.lock().tasks["manual"].task.status, Status::Cancelled);
assert_eq!(again.lock().tasks["unknown"].task.status, Status::Cancelled);
let twice = Daemon::load(options(&f.home), None).unwrap();
assert_eq!(twice.lock().tasks["test"].deliverable.len(), 2);
}
#[cfg(windows)]
#[test]
fn transient_task_or_event_read_failure_preserves_cursors_for_retry() {
use std::os::windows::fs::OpenOptionsExt;
let f = fixture();
f.d.try_emit("test", "failed", json!({})).unwrap();
let events = f.d.lock().tasks["test"].deliverable.clone();
f.d.mark_seen("codex:host", &events, &[], &[]).unwrap();
let ledger = std::fs::read(f.home.join("ledger.json")).unwrap();
for file in ["task.json", "events.jsonl"] {
let path = f.home.join("tasks/test").join(file);
let held = std::fs::OpenOptions::new()
.read(true)
.share_mode(0)
.open(&path)
.unwrap();
assert!(Daemon::load(options(&f.home), None).is_err());
assert!(path.exists());
assert!(!f.home.join("quarantine").exists());
assert_eq!(std::fs::read(f.home.join("ledger.json")).unwrap(), ledger);
drop(held);
let again = Daemon::load(options(&f.home), None).unwrap();
assert!(again.unseen(&again.lock(), "codex:host", &[]).is_empty());
assert_eq!(again.lock().seq, events[0].seq);
}
}