mod support;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use serde_json::json;
use support::{McpSession, Sandbox};
fn hooked_sandbox() -> (Sandbox, PathBuf) {
let sandbox = Sandbox::new();
let log = sandbox.dir.path().join("questions.log");
sandbox.write_config(&format!(
"question_hook = \"echo \\\"$HIRD_EVENT $HIRD_TASK $HIRD_TITLE: $HIRD_QUESTION ($HIRD_ASKED_BY)\\\" >> {}\"\n",
log.display()
));
(sandbox, log)
}
fn wait_for(log: &Path, needle: &str) -> String {
let deadline = Instant::now() + Duration::from_secs(10);
loop {
let contents = std::fs::read_to_string(log).unwrap_or_default();
if contents.contains(needle) {
return contents;
}
assert!(
Instant::now() < deadline,
"the hook never wrote {needle:?}; log so far:\n{contents}"
);
std::thread::sleep(Duration::from_millis(25));
}
}
#[test]
fn a_raised_question_summons_the_human_with_the_question_itself() {
let (sandbox, log) = hooked_sandbox();
let seq: i64 = sandbox
.run(&["add", "migrate the config"])
.trim()
.parse()
.unwrap();
let mut codex = McpSession::start(&sandbox, "codex");
codex.call("task_claim", json!({"seq": seq})).unwrap();
codex
.call(
"task_release",
json!({
"seq": seq,
"reason": "the migration branch is isolated",
"question": "Must the old format remain readable?"
}),
)
.unwrap();
wait_for(
&log,
&format!("asked {seq} migrate the config: Must the old format remain readable? (codex:"),
);
codex.shutdown();
}
#[test]
fn an_ordinary_release_stays_quiet() {
let (sandbox, log) = hooked_sandbox();
let seq: i64 = sandbox.run(&["add", "contended"]).trim().parse().unwrap();
let mut codex = McpSession::start(&sandbox, "codex");
codex.call("task_claim", json!({"seq": seq})).unwrap();
codex
.call(
"task_release",
json!({"seq": seq, "reason": "out of context"}),
)
.unwrap();
codex.call("task_claim", json!({"seq": seq})).unwrap();
codex
.call(
"task_release",
json!({"seq": seq, "reason": "needs a policy call", "question": "Which way?"}),
)
.unwrap();
let contents = wait_for(&log, &format!("asked {seq} contended: Which way?"));
assert_eq!(
contents.lines().count(),
1,
"the ordinary release must not have fired the hook:\n{contents}"
);
codex.shutdown();
}