Files
plan2code/plan2code-app/src-tauri/tests/agent_manager.rs
T

723 lines
23 KiB
Rust

//! The agent lifecycle against the fake agent in a real pseudo-terminal. Real
//! agents are never started here: `PLAN2CODE_APP_AGENT_BIN` points every agent
//! kind at the fake one.
use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError};
use std::sync::{Arc, Mutex, Once};
use std::time::{Duration, Instant};
use plan2code_app_lib::agent_manager::{AgentEvent, AgentManager, AgentSnapshot, AgentState};
use plan2code_app_lib::agents::{
agent_launch_spec, builtin_entry, parse_agents_file, Agent, PermissionMode, AGENT_BIN_ENV,
};
mod common;
use common::process_alive;
const TIMEOUT: Duration = Duration::from_secs(20);
const READY: &str = "FAKE-AGENT ready";
struct Harness {
manager: AgentManager,
rx: Receiver<AgentEvent>,
/// Everything the agents printed, in order.
seen: String,
/// Every state change, in order.
states: Vec<AgentSnapshot>,
/// How many of `states` `wait_state` has already looked at.
cursor: usize,
}
impl Harness {
fn new() -> Harness {
static BIN: Once = Once::new();
BIN.call_once(|| std::env::set_var(AGENT_BIN_ENV, common::fake_agent()));
let (tx, rx) = channel();
let tx = Mutex::new(tx);
let sink = Arc::new(move |event| {
let _ = tx.lock().unwrap().send(event);
});
Harness {
manager: AgentManager::new(PathBuf::from("."), sink),
rx,
seen: String::new(),
states: Vec::new(),
cursor: 0,
}
}
fn take(&mut self, event: AgentEvent) {
match event {
AgentEvent::Output(bytes) => {
let text = String::from_utf8_lossy(&bytes).into_owned();
// Windows' pseudo-terminal asks where the cursor is (ESC [ 6 n)
// and holds the output until told, as xterm does on its own.
for _ in text.matches("[6n") {
self.manager.input(b"\x1b[1;1R").unwrap();
}
self.seen.push_str(&text);
}
AgentEvent::State(snapshot) => self.states.push(snapshot),
}
}
/// Handle events for `how_long`, answering the terminal's questions.
fn pump_for(&mut self, how_long: Duration) {
let deadline = Instant::now() + how_long;
loop {
let left = deadline.saturating_duration_since(Instant::now());
match self.rx.recv_timeout(left) {
Ok(event) => self.take(event),
Err(_) => return,
}
}
}
fn wait_for_output(&mut self, needle: &str, times: usize) {
let deadline = Instant::now() + TIMEOUT;
while self.seen.matches(needle).count() < times {
let left = deadline.saturating_duration_since(Instant::now());
match self.rx.recv_timeout(left) {
Ok(event) => self.take(event),
Err(RecvTimeoutError::Timeout) | Err(RecvTimeoutError::Disconnected) => {
panic!("never saw {needle:?} {times} time(s); got {:?}", self.seen)
}
}
}
}
/// The next state change (after the last one this returned) that matches.
fn wait_state(
&mut self,
what: &str,
matches: impl Fn(&AgentSnapshot) -> bool,
) -> AgentSnapshot {
let deadline = Instant::now() + TIMEOUT;
loop {
while self.cursor < self.states.len() {
let snapshot = self.states[self.cursor].clone();
self.cursor += 1;
if matches(&snapshot) {
return snapshot;
}
}
let left = deadline.saturating_duration_since(Instant::now());
match self.rx.recv_timeout(left) {
Ok(event) => self.take(event),
Err(_) => panic!("never saw the state {what}; got {:?}", self.states),
}
}
}
fn start_and_wait_ready(&mut self) {
self.manager
.start(
Agent::Builtin(builtin_entry("claude").unwrap()),
None,
None,
PermissionMode::Bypass,
None,
)
.expect("start the fake agent");
self.wait_for_output(READY, 1);
}
fn type_line(&self, line: &str) {
self.manager.input(format!("{line}\r").as_bytes()).unwrap();
}
}
fn is_running(snapshot: &AgentSnapshot) -> bool {
snapshot.state == AgentState::Running
}
#[test]
fn a_second_start_while_running_is_refused() {
let mut h = Harness::new();
h.start_and_wait_ready();
let generation = h.manager.snapshot().generation;
let refused = h.manager.start(
Agent::Builtin(builtin_entry("devin").unwrap()),
None,
None,
PermissionMode::Bypass,
None,
);
assert!(refused.is_err(), "a second start must be refused");
let after = h.manager.snapshot();
assert_eq!(after.state, AgentState::Running);
assert_eq!(
after.generation, generation,
"the running agent was replaced"
);
h.manager.stop();
}
#[test]
fn restart_keeps_the_previous_agent_kind_mode_model_and_effort() {
let mut h = Harness::new();
h.manager
.start(
Agent::Builtin(builtin_entry("codex").unwrap()),
Some("gpt-5".to_string()),
Some("high".to_string()),
PermissionMode::Checked,
None,
)
.expect("start the fake agent");
h.wait_for_output(READY, 1);
let before = h.manager.snapshot();
assert_eq!(before.kind, Some("codex".to_string()));
assert_eq!(before.mode, Some(PermissionMode::Checked));
assert_eq!(
h.manager.model_and_effort(),
(Some("gpt-5".to_string()), Some("high".to_string()))
);
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
let after = h.manager.snapshot();
assert_eq!(
after.kind,
Some("codex".to_string()),
"restart must keep the same agent"
);
assert_eq!(
after.mode,
Some(PermissionMode::Checked),
"restart must keep the same permission mode"
);
assert_eq!(
h.manager.model_and_effort(),
(Some("gpt-5".to_string()), Some("high".to_string())),
"restart must keep the same model and effort"
);
h.manager.stop();
}
#[test]
fn snapshot_carries_the_model_and_effort_for_the_bar_only_when_known() {
let mut h = Harness::new();
h.manager
.start(
Agent::Builtin(builtin_entry("codex").unwrap()),
Some("gpt-5".to_string()),
Some("high".to_string()),
PermissionMode::Checked,
None,
)
.expect("start the fake agent");
h.wait_for_output(READY, 1);
let known = h.manager.snapshot();
assert_eq!(known.model.as_deref(), Some("gpt-5"));
assert_eq!(known.effort.as_deref(), Some("high"));
let json = serde_json::to_value(&known).unwrap();
assert_eq!(json["model"], "gpt-5");
assert_eq!(json["effort"], "high");
h.manager.stop();
h.manager
.start(
Agent::Builtin(builtin_entry("codex").unwrap()),
None,
None,
PermissionMode::Checked,
None,
)
.expect("start the fake agent on its own model");
h.wait_for_output(READY, 2);
let unknown = h.manager.snapshot();
assert_eq!(unknown.model, None);
assert_eq!(unknown.effort, None);
let json = serde_json::to_value(&unknown).unwrap();
assert!(json.get("model").is_none(), "an unknown model is left out");
assert!(
json.get("effort").is_none(),
"an unknown effort is left out"
);
h.manager.stop();
}
#[test]
fn restart_between_steps_replaces_the_agent_at_once() {
let mut h = Harness::new();
h.start_and_wait_ready();
let generation = h.manager.snapshot().generation;
h.manager.restart().unwrap();
let now = h.manager.snapshot();
assert_eq!(now.state, AgentState::Running);
assert!(now.generation > generation, "a fresh agent was not started");
h.wait_for_output(READY, 2);
h.manager.stop();
}
#[test]
fn restart_mid_step_kills_the_step_at_once_instead_of_waiting_for_it() {
let mut h = Harness::new();
h.start_and_wait_ready();
h.type_line("STEP 30");
h.wait_for_output("working 1/30", 1);
let started = Instant::now();
h.manager.restart().unwrap();
// Running again straight away: no wait for the step to finish.
h.wait_state("Running again", is_running);
h.wait_for_output(READY, 2);
assert!(
started.elapsed() < Duration::from_secs(5),
"restart must not wait on the step; took {:?}",
started.elapsed()
);
assert!(
!h.seen.contains("STEP DONE"),
"the step should have been cut short, not finished"
);
h.manager.stop();
}
#[test]
fn an_agent_that_exits_by_itself_ends_with_its_code_and_nothing_restarts() {
let mut h = Harness::new();
h.start_and_wait_ready();
h.type_line("EXIT 7");
let ended = h.wait_state("Ended", |s| matches!(s.state, AgentState::Ended { .. }));
match &ended.state {
AgentState::Ended { reason } => assert!(reason.contains('7'), "reason was {reason:?}"),
other => panic!("expected Ended, got {other:?}"),
}
// Nothing starts without a press.
h.pump_for(Duration::from_millis(1500));
assert!(matches!(
h.manager.snapshot().state,
AgentState::Ended { .. }
));
assert_eq!(
h.seen.matches(READY).count(),
1,
"an agent started by itself"
);
assert!(!h.manager.is_active());
// A press of Restart starts a fresh one on the same brief.
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
assert_eq!(h.manager.snapshot().state, AgentState::Running);
h.manager.stop();
}
#[test]
fn quitting_with_a_running_agent_stops_it() {
let mut h = Harness::new();
h.start_and_wait_ready();
assert!(h.manager.is_active());
h.type_line("PID");
h.wait_for_output("PID ", 1);
let pid = h
.seen
.rsplit("PID ")
.next()
.unwrap()
.trim_start()
.split(|c: char| !c.is_ascii_digit())
.next()
.unwrap()
.parse::<u32>()
.expect("the agent's pid");
assert!(
process_alive(pid),
"the agent should be running before the stop"
);
h.manager.stop();
assert!(!process_alive(pid), "the agent's process outlived stop()");
assert_eq!(h.manager.snapshot().state, AgentState::Idle);
assert!(!h.manager.is_active());
// The stopped agent does not come back or print again.
let before = h.seen.len();
h.pump_for(Duration::from_millis(1000));
assert!(
!h.seen[before..].contains(READY),
"an agent started after the stop"
);
assert!(
h.manager.restart().is_err(),
"there is nothing left to restart"
);
}
#[test]
fn the_replay_buffer_holds_recent_output_and_clears_on_a_fresh_agent() {
let mut h = Harness::new();
h.start_and_wait_ready();
let replay = String::from_utf8_lossy(&h.manager.replay()).into_owned();
assert!(
replay.contains(READY),
"replay should hold what was just printed; got {replay:?}"
);
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
let replay = String::from_utf8_lossy(&h.manager.replay()).into_owned();
assert_eq!(
replay.matches(READY).count(),
1,
"a fresh agent's replay must not carry the old one's output"
);
h.manager.stop();
}
#[test]
fn a_restart_runs_the_brief_hook_and_the_bar_gets_its_note() {
let mut h = Harness::new();
let runs = Arc::new(Mutex::new(0));
let counter = Arc::clone(&runs);
h.manager.set_before_fresh(Arc::new(move || {
*counter.lock().unwrap() += 1;
Some("Brief ready on the dashboard".to_string())
}));
h.start_and_wait_ready();
assert_eq!(*runs.lock().unwrap(), 0, "a first start leaves no brief");
assert_eq!(h.manager.snapshot().brief_note, None);
h.manager.restart().unwrap();
assert_eq!(
*runs.lock().unwrap(),
1,
"the restart did not run the brief once"
);
assert_eq!(
h.manager.snapshot().brief_note.as_deref(),
Some("Brief ready on the dashboard")
);
h.manager.stop();
}
#[test]
fn the_agent_gets_the_in_app_signal_and_a_restart_keeps_it() {
let mut h = Harness::new();
h.manager.set_env(vec![
("PLAN2CODE_IN_APP".to_string(), "1".to_string()),
(
"PLAN2CODE_CONSOLE_SID".to_string(),
"sid-window-a".to_string(),
),
]);
h.start_and_wait_ready();
h.type_line("ENV PLAN2CODE_IN_APP");
h.wait_for_output("ENV PLAN2CODE_IN_APP=1", 1);
h.type_line("ENV PLAN2CODE_CONSOLE_SID");
h.wait_for_output("ENV PLAN2CODE_CONSOLE_SID=sid-window-a", 1);
// The signal changes with the console; a Restart starts on the newest.
h.manager.set_env(vec![(
"PLAN2CODE_CONSOLE_SID".to_string(),
"sid-window-b".to_string(),
)]);
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
h.type_line("ENV PLAN2CODE_CONSOLE_SID");
h.wait_for_output("ENV PLAN2CODE_CONSOLE_SID=sid-window-b", 1);
h.type_line("ENV PLAN2CODE_IN_APP");
h.wait_for_output("ENV PLAN2CODE_IN_APP=\r", 1);
h.manager.stop();
}
#[test]
fn an_agent_with_no_signal_set_gets_none() {
let mut h = Harness::new();
h.start_and_wait_ready();
h.type_line("ENV PLAN2CODE_IN_APP");
h.wait_for_output("ENV PLAN2CODE_IN_APP=", 1);
assert!(!h.seen.contains("ENV PLAN2CODE_IN_APP=1"));
h.manager.stop();
}
#[test]
fn a_custom_agent_runs_the_override_too_and_restart_keeps_its_id() {
let mut h = Harness::new();
// Its command is nowhere on PATH: the override replaces it, as for a built-in.
let (mut agents, warnings) = parse_agents_file(
r#"{"schema":1,"agents":[{"id":"goose","name":"Goose","command":"goose-not-installed","bypassArgs":["--yes"]}]}"#,
);
assert!(warnings.is_empty(), "{warnings:?}");
let goose = Agent::Custom(agents.remove(0));
let spec = agent_launch_spec(
&goose,
&std::env::temp_dir(),
Some("m"),
None,
PermissionMode::Bypass,
"/plan2code",
);
assert_eq!(PathBuf::from(&spec.program), common::fake_agent());
assert!(spec.args.is_empty());
h.manager
.start(goose, None, None, PermissionMode::Checked, None)
.expect("start the custom agent");
h.wait_for_output(READY, 1);
assert_eq!(h.manager.snapshot().kind, Some("goose".to_string()));
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
assert_eq!(
h.manager.snapshot().kind,
Some("goose".to_string()),
"restart must keep the custom agent"
);
h.manager.stop();
}
#[test]
fn a_phase_start_runs_on_its_model_and_env_and_leaves_the_launch_choice_alone() {
let mut h = Harness::new();
h.manager
.start(
Agent::Builtin(builtin_entry("claude").unwrap()),
Some("opus".to_string()),
Some("high".to_string()),
PermissionMode::Checked,
None,
)
.expect("start the fake agent");
h.wait_for_output(READY, 1);
h.manager
.start_phase_on(
"/plan2code phase".to_string(),
"sonnet".to_string(),
Some("low".to_string()),
vec![("PLAN2CODE_LOOP_STRENGTH".to_string(), "thrifty".to_string())],
)
.expect("start the phase on its strength");
h.wait_for_output(READY, 2);
// The agent-bin override drops every argument, so the fake agent sees the
// env only; the model is checked through the stored choice below.
h.type_line("ENV PLAN2CODE_LOOP_STRENGTH");
h.wait_for_output("ENV PLAN2CODE_LOOP_STRENGTH=thrifty", 1);
// The stored choice, kind and mode are what they were.
let (kind, model, effort, mode) = h.manager.launch_choice().unwrap();
assert_eq!(kind.id(), "claude");
assert_eq!(model.as_deref(), Some("opus"));
assert_eq!(effort.as_deref(), Some("high"));
assert_eq!(mode, PermissionMode::Checked);
// The next plain start runs on the original choice, with no strength.
h.manager
.start_fresh("/plan2code next".to_string())
.unwrap();
h.wait_for_output(READY, 3);
h.type_line("ENV PLAN2CODE_LOOP_STRENGTH");
h.wait_for_output("ENV PLAN2CODE_LOOP_STRENGTH=\r", 1);
h.manager.stop();
}
/// Counts the manager's `ended` calls.
fn count_ends(h: &Harness) -> Arc<Mutex<usize>> {
let ends = Arc::new(Mutex::new(0));
let counter = Arc::clone(&ends);
h.manager.set_ended(Arc::new(move || {
*counter.lock().unwrap() += 1;
}));
ends
}
/// Wait (briefly) for `ends` to reach `want`, then make sure it stays there.
fn expect_ends(h: &mut Harness, ends: &Arc<Mutex<usize>>, want: usize, what: &str) {
let deadline = Instant::now() + Duration::from_secs(5);
while *ends.lock().unwrap() < want && Instant::now() < deadline {
h.pump_for(Duration::from_millis(50));
}
// A late exit of an agent already counted must not count again.
h.pump_for(Duration::from_millis(500));
assert_eq!(*ends.lock().unwrap(), want, "{what}");
}
#[test]
fn ended_runs_once_for_an_agent_that_exits_by_itself() {
let mut h = Harness::new();
let ends = count_ends(&h);
h.start_and_wait_ready();
h.type_line("EXIT 7");
h.wait_state("Ended", |s| matches!(s.state, AgentState::Ended { .. }));
expect_ends(&mut h, &ends, 1, "one natural end");
// Nothing is running now: a stop ends nothing more.
h.manager.stop();
expect_ends(&mut h, &ends, 1, "a stop with no running agent");
}
#[test]
fn ended_runs_once_for_a_stopped_agent_and_never_for_a_stop_of_none() {
let mut h = Harness::new();
let ends = count_ends(&h);
h.manager.stop();
expect_ends(&mut h, &ends, 0, "a stop before any start");
h.start_and_wait_ready();
h.manager.stop();
expect_ends(&mut h, &ends, 1, "one stop of a running agent");
}
#[test]
fn ended_runs_once_for_the_old_agent_on_restart_and_on_a_fresh_start() {
let mut h = Harness::new();
let ends = count_ends(&h);
h.start_and_wait_ready();
h.manager.restart().unwrap();
h.wait_for_output(READY, 2);
expect_ends(&mut h, &ends, 1, "the restarted agent's old one");
h.manager
.start_fresh("/plan2code next".to_string())
.unwrap();
h.wait_for_output(READY, 3);
expect_ends(&mut h, &ends, 2, "the old agent a fresh start replaced");
h.manager.stop();
expect_ends(&mut h, &ends, 3, "the last agent, stopped");
}
#[test]
fn an_ended_agent_takes_the_consoles_it_announced_and_only_those() {
use plan2code_app_lib::console_supervisor::{stop_announced_on_end, AnnouncedConsoles};
let mut h = Harness::new();
let announced = Arc::new(AnnouncedConsoles::default());
let stopped = Arc::new(Mutex::new(Vec::<String>::new()));
let log = Arc::clone(&stopped);
h.manager.set_ended(stop_announced_on_end(
Arc::clone(&announced),
|| None,
move |sid| {
log.lock().unwrap().push(sid.to_string());
},
));
h.start_and_wait_ready();
announced.record("s1");
h.manager.stop();
let deadline = Instant::now() + Duration::from_secs(5);
while stopped.lock().unwrap().is_empty() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(20));
}
// Recorded after the end: it belongs to whatever comes next.
announced.record("s2");
std::thread::sleep(Duration::from_millis(300));
assert_eq!(*stopped.lock().unwrap(), vec!["s1"]);
assert_eq!(announced.take_all(), vec!["s2"], "s2 was never taken");
}
/// Starts the harness's agent, has it leave a child that ignores the
/// terminal's hangup in its process group, and returns that child's pid.
#[cfg(unix)]
fn start_with_stray(h: &mut Harness) -> u32 {
h.start_and_wait_ready();
h.type_line("STRAY");
h.wait_for_output("STRAY ", 1);
let stray: u32 = h
.seen
.rsplit("STRAY ")
.next()
.unwrap()
.split(|c: char| !c.is_ascii_digit())
.next()
.unwrap()
.parse()
.expect("the stray child's pid");
assert!(process_alive(stray), "the stray child should be running");
stray
}
/// Waits out the hangup grace for `stray` to go, then kills it if it is
/// still there, so a failing test leaves nothing running. True if it went.
#[cfg(unix)]
fn stray_gone(stray: u32) -> bool {
use plan2code_app_lib::pty_host::HANGUP_GRACE;
let deadline = Instant::now() + HANGUP_GRACE + Duration::from_secs(2);
while process_alive(stray) && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(50));
}
let gone = !process_alive(stray);
if !gone {
let _ = std::process::Command::new("kill")
.args(["-9", &stray.to_string()])
.status();
}
gone
}
/// An agent that ends by itself leaves its process group alone: work the
/// person asked it to leave running (a dev server it backgrounded) stays up,
/// as it did before the app ended process groups at all.
#[cfg(unix)]
#[test]
fn an_agent_that_exits_by_itself_leaves_its_process_group_running() {
let mut h = Harness::new();
let stray = start_with_stray(&mut h);
h.type_line("EXIT 0");
h.wait_state("Ended", |s| matches!(s.state, AgentState::Ended { .. }));
assert!(
!stray_gone(stray),
"the stray child was ended with an agent that exited by itself"
);
}
/// An agent the app stops (quit, a project switch) takes its process group
/// with it, a child that ignores the terminal's hangup included.
#[cfg(unix)]
#[test]
fn a_stopped_agent_takes_its_process_group_with_it() {
let mut h = Harness::new();
let stray = start_with_stray(&mut h);
h.manager.stop();
assert!(
stray_gone(stray),
"the stray child outlived its stopped agent"
);
}
/// A Restart or New Session replaces the agent, and its process group goes
/// with the old one.
#[cfg(unix)]
#[test]
fn a_replaced_agent_takes_its_process_group_with_it() {
let mut h = Harness::new();
let stray = start_with_stray(&mut h);
h.manager.restart().unwrap();
assert!(
stray_gone(stray),
"the stray child outlived its replaced agent"
);
h.manager.stop();
}
/// Quitting right after New Session: the replaced agent's grace is still
/// running on a thread that would end with the app.
#[cfg(unix)]
#[test]
fn a_stop_right_after_a_restart_waits_for_the_replaced_agents_process_group() {
let mut h = Harness::new();
let stray = start_with_stray(&mut h);
h.manager.restart().expect("restart");
h.manager.stop();
let left = process_alive(stray);
assert!(stray_gone(stray), "the stray child outlived its grace");
assert!(!left, "the stray child was still there when stop returned");
}
#[test]
fn a_phase_start_with_no_launch_choice_is_refused() {
let h = Harness::new();
let refused = h
.manager
.start_phase_on("p".to_string(), "sonnet".to_string(), None, Vec::new());
assert!(refused.is_err());
}