use super::*;
use crate::agent_loop::telemetry;
struct BudgetSink {
snapshots: Mutex<Vec<ContextBudgetSnapshot>>,
}
impl BudgetSink {
fn new() -> Self {
Self {
snapshots: Mutex::new(Vec::new()),
}
}
fn snapshots(&self) -> Vec<ContextBudgetSnapshot> {
self.snapshots.lock().expect("lock poisoned").clone()
}
}
#[async_trait]
impl ToolEventSink for BudgetSink {
async fn tool_started(
&self,
_agent: &str,
_agent_id: &str,
_call_id: &str,
_tool: &str,
_args_preview: &str,
) {
}
async fn tool_finished(
&self,
_agent: &str,
_agent_id: &str,
_c: &str,
_t: &str,
_s: bool,
_r: &str,
) {
}
async fn tool_error(
&self,
_agent: &str,
_agent_id: &str,
_call_id: &str,
_tool: &str,
_error: &str,
) {
}
async fn context_budget(&self, snapshot: &ContextBudgetSnapshot) {
self.snapshots
.lock()
.expect("lock poisoned")
.push(*snapshot);
}
}
fn cadence_script() -> Vec<Value> {
let mut fixtures: Vec<Value> = (0..3)
.map(|i| tool_call_response(&format!("call_{i}"), &format!("turn-{i}")))
.collect();
fixtures.push(stop_response("all done"));
fixtures
}
#[tokio::test]
async fn cadence_emits_context_budget_snapshot() {
let llm = Arc::new(ScriptedLlm::from_json(&cadence_script()));
let registry = registry_with_echo(false);
let sink = Arc::new(BudgetSink::new());
let dir = telemetry::test_temp_dir("cadence-emits-budget-snapshot");
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
cadence: Some(CadenceConfig::default()),
telemetry_data_dir: Some(dir.clone()),
..AgentLoopConfig::default()
},
)
.with_tool_event_sink(sink.clone());
agent
.run("system prompt", "the task")
.await
.expect("run completes");
std::fs::remove_dir_all(&dir).ok();
let snapshots = sink.snapshots();
assert_eq!(
snapshots.len(),
4,
"one budget snapshot per turn boundary, got {snapshots:?}"
);
for s in &snapshots {
assert!(s.context_window > 0, "window must be resolved: {s:?}");
assert!(
s.overhead_tokens > 0,
"a seeded transcript always costs SOME tokens — a 0 here means the \
snapshot is a placeholder, not a measurement: {s:?}"
);
assert_eq!(
s.overhead_cap_tokens,
s.context_window * 40 / 100,
"cap must be the epic's 40% arithmetic: {s:?}"
);
assert_eq!(
s.working_context_pct as usize + s.overhead_pct as usize,
100,
"the two percentages a budget meter renders must sum to 100: {s:?}"
);
assert!(
s.within_budget,
"a tiny test transcript is far under cap: {s:?}"
);
}
}
#[tokio::test]
async fn no_context_budget_event_when_cadence_disabled() {
let llm = Arc::new(ScriptedLlm::from_json(&cadence_script()));
let registry = registry_with_echo(false);
let sink = Arc::new(BudgetSink::new());
let agent =
make_loop(llm, registry, AgentLoopConfig::default()).with_tool_event_sink(sink.clone());
agent
.run("system prompt", "the task")
.await
.expect("run completes");
assert!(
sink.snapshots().is_empty(),
"cadence: None (the delegated engineer's config) must emit no budget \
events, got {:?}",
sink.snapshots()
);
}
#[tokio::test]
async fn no_context_budget_event_in_parity_mode() {
let llm = Arc::new(ScriptedLlm::from_json(&cadence_script()));
let registry = registry_with_echo(false);
let sink = Arc::new(BudgetSink::new());
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
mode: crate::mode::HarnessMode::Parity,
cadence: Some(CadenceConfig {
cadence_turns: 1,
..CadenceConfig::default()
}),
..AgentLoopConfig::default()
},
)
.with_tool_event_sink(sink.clone());
agent
.run("system prompt", "the task")
.await
.expect("run completes");
assert!(
sink.snapshots().is_empty(),
"Parity must never emit a budget event"
);
}