use super::*;
use pretty_assertions::assert_eq;
fn state(running: bool, listed: &[&'static str]) -> v2::StateUpdate {
work_state(
running,
&listed
.iter()
.map(|id| Work::Command(id))
.collect::<Vec<_>>(),
)
}
#[derive(Clone, Copy)]
enum Work {
Command(&'static str),
ToolCall(&'static str),
Subagent(&'static str),
}
fn work_state(running: bool, listed: &[Work]) -> v2::StateUpdate {
let background = listed
.iter()
.map(|work| match *work {
Work::Command(id) => v2::BackgroundWork::Terminal(v2::BackgroundTerminal::new(id)),
Work::ToolCall(id) => v2::BackgroundWork::ToolCall(v2::BackgroundToolCall::new(id)),
Work::Subagent(id) => v2::BackgroundWork::Subagent(v2::BackgroundSubagent::new(id)),
})
.collect::<Vec<_>>();
if running {
v2::StateUpdate::Running(v2::RunningStateUpdate::new().background(background))
} else {
v2::StateUpdate::Idle(v2::IdleStateUpdate::new().background(background))
}
}
enum Heard {
State(v2::StateUpdate),
Call(&'static str, v2::ToolCallStatus),
}
#[test]
fn the_run_keeps_the_agent_while_background_work_may_report() {
use v2::ToolCallStatus::{Completed, Pending};
use Work::{Command, Subagent, ToolCall};
let start = Instant::now();
let budget = start + BACKGROUND_BUDGET;
let follow_up = start + FOLLOW_UP_WAIT;
let s = |running, listed: &[Work]| Heard::State(work_state(running, listed));
let opened = |id| Heard::Call(id, Pending);
let finished = |id| Heard::Call(id, Completed);
let cases = [
("no background work", vec![s(true, &[])], None),
(
"a command still runs",
vec![s(true, &[Command("t1")]), s(false, &[Command("t1")])],
Some(budget),
),
(
"the command ended: its report is owed",
vec![s(true, &[Command("t1")]), s(false, &[])],
Some(follow_up),
),
(
"a command ended in the turn's last step: its report is owed too",
vec![s(true, &[Command("t1")]), s(true, &[]), s(false, &[])],
Some(follow_up),
),
(
"a call that finished after the command ended took it in",
vec![
s(true, &[Command("t1")]),
opened("t2"),
s(true, &[]),
finished("t2"),
s(false, &[]),
],
None,
),
(
"a call first heard finished opened no step",
vec![
s(true, &[Command("t1")]),
s(true, &[]),
finished("t2"),
s(false, &[]),
],
Some(follow_up),
),
(
"ended work but a command stays owed past a call",
vec![
s(true, &[Subagent("a1"), ToolCall("m1")]),
opened("t2"),
s(true, &[]),
finished("t2"),
s(false, &[]),
],
Some(follow_up),
),
(
"a call that finishes between turns is no step",
vec![
s(true, &[Command("t1")]),
opened("t2"),
s(false, &[Command("t1")]),
s(false, &[]),
finished("t2"),
],
Some(follow_up),
),
(
"the turn that reports it settled it",
vec![
s(true, &[Command("t1")]),
s(false, &[]),
s(true, &[]),
s(false, &[]),
],
None,
),
];
for (case, heard, expected) in cases {
let mut background = Background::default();
for heard in heard {
match heard {
Heard::State(state) => background.observe(&state),
Heard::Call(id, status) => {
background.tool_call(&v2::ToolCallUpdate::new(id).status(status));
}
}
}
assert_eq!(background.idle(start), expected, "{case}");
}
}
#[test]
fn the_budget_names_what_still_ran() {
let start = Instant::now();
let mut background = Background::default();
background.observe(&state(false, &["toolu_1"]));
background.idle(start);
background.observe(&state(true, &["toolu_1"]));
let later = start + Duration::from_secs(60);
background.observe(&state(false, &["toolu_1"]));
assert_eq!(background.idle(later), Some(start + BACKGROUND_BUDGET));
background.observe(&state(true, &["toolu_1"]));
assert_eq!(
background.expired(start + BACKGROUND_BUDGET),
Some("acp: background work budget of 10 min after the prompted turn is spent; stopping the agent with still running: a turn the agent started on its own, command toolu_1".to_owned())
);
let mut owed = Background::default();
owed.observe(&state(true, &["toolu_1"]));
owed.observe(&state(false, &[]));
let deadline = owed.idle(start).unwrap();
assert_eq!(owed.expired(deadline), None);
}