use super::*;
use crate::agent_loop::telemetry;
#[tokio::test]
async fn cadence_disabled_by_default() {
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"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let agent = make_loop(llm, registry, AgentLoopConfig::default());
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
assert_eq!(transcript.cadence_turn_count(), 0);
}
#[tokio::test]
async fn cadence_never_fires_in_parity_mode() {
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"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
mode: crate::mode::HarnessMode::Parity,
cadence: Some(CadenceConfig {
cadence_turns: 1,
max_overhead_fraction_pct: 40,
}),
..AgentLoopConfig::default()
},
);
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
assert_eq!(transcript.cadence_turn_count(), 0);
}
#[tokio::test]
async fn cadence_fires_in_daily_driver_when_configured() {
let mut fixtures: Vec<Value> = (0..6)
.map(|i| tool_call_response(&format!("call_{i}"), &format!("turn-{i}")))
.collect();
fixtures.push(stop_response("all done"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let dir = telemetry::test_temp_dir("cadence-fires-daily-driver");
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
max_turns: 10,
mode: crate::mode::HarnessMode::DailyDriver,
compaction: CompactionConfig {
token_threshold: 1_000_000, keep_last_messages: 1,
},
cadence: Some(CadenceConfig {
cadence_turns: 1,
max_overhead_fraction_pct: 99,
}),
telemetry_data_dir: Some(dir.clone()),
..AgentLoopConfig::default()
},
);
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
assert_eq!(transcript.cadence_turn_count(), 7);
assert!(
transcript.cadence_fire_count() > 0,
"expected at least one cadence fire to actually compact something"
);
std::fs::remove_dir_all(&dir).ok();
}
#[tokio::test]
async fn cadence_fire_logs_info() {
crate::test_support::begin_capture();
let mut fixtures: Vec<Value> = (0..6)
.map(|i| tool_call_response(&format!("call_{i}"), &format!("turn-{i}")))
.collect();
fixtures.push(stop_response("all done"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let dir = telemetry::test_temp_dir("cadence-fire-logs-info");
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
max_turns: 10,
mode: crate::mode::HarnessMode::DailyDriver,
compaction: CompactionConfig {
token_threshold: 1_000_000,
keep_last_messages: 1,
},
cadence: Some(CadenceConfig {
cadence_turns: 1,
max_overhead_fraction_pct: 99,
}),
telemetry_data_dir: Some(dir.clone()),
..AgentLoopConfig::default()
},
);
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
let messages = crate::test_support::captured_at_least(tracing::Level::INFO);
assert!(
messages
.iter()
.any(|m| m.contains("cadence compression fired")),
"expected an info-level cadence-fire log, got: {messages:?}"
);
std::fs::remove_dir_all(&dir).ok();
}
fn overwhelmed_cadence() -> CadenceConfig {
CadenceConfig {
cadence_turns: 1_000_000, max_overhead_fraction_pct: 99,
}
}
#[tokio::test]
async fn forced_degradation_increments_counter_and_logs_error() {
crate::test_support::begin_capture();
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"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let dir = telemetry::test_temp_dir("forced-degradation");
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
max_turns: 10,
mode: crate::mode::HarnessMode::DailyDriver,
compaction: aggressive_compaction(),
cadence: Some(overwhelmed_cadence()),
telemetry_data_dir: Some(dir.clone()),
..AgentLoopConfig::default()
},
);
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
assert!(
transcript.compaction_events() > 0,
"threshold compaction should have fired at least once under the tiny token_threshold"
);
let messages = crate::test_support::captured_at_least(tracing::Level::ERROR);
assert!(
messages
.iter()
.any(|m| m.contains("regression signal") && m.contains("cadence")),
"expected an error-level regression-signal log, got: {messages:?}"
);
let jsonl_path = crate::agent_loop::telemetry::compression_log_path(&dir);
let jsonl_contents = std::fs::read_to_string(&jsonl_path)
.unwrap_or_else(|e| panic!("expected {jsonl_path:?} to exist: {e}"));
let threshold_events: Vec<crate::agent_loop::telemetry::CompressionEvent> = jsonl_contents
.lines()
.map(|l| serde_json::from_str(l).expect("valid CompressionEvent JSONL line"))
.filter(|e: &crate::agent_loop::telemetry::CompressionEvent| {
e.surface == crate::agent_loop::telemetry::SURFACE_TCODE_THRESHOLD
})
.collect();
assert!(
!threshold_events.is_empty(),
"expected a tcode-threshold JSONL line from the same fire, got: {jsonl_contents:?}"
);
assert!(
threshold_events.iter().all(|e| e.compaction_event),
"every tcode-threshold record must have compaction_event: true: {threshold_events:?}"
);
assert!(
crate::agent_loop::telemetry::lifetime_compaction_alarm_count(&dir) >= 1,
"the same cadence: Some(_) fire must also record the durable alarm line"
);
std::fs::remove_dir_all(&dir).ok();
}
#[tokio::test]
async fn cadence_none_threshold_fire_does_not_log_error() {
crate::test_support::begin_capture();
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"));
let llm = Arc::new(ScriptedLlm::from_json(&fixtures));
let registry = registry_with_echo(false);
let dir = telemetry::test_temp_dir("cadence-none-threshold-fire");
let agent = make_loop(
llm,
registry,
AgentLoopConfig {
max_turns: 10,
mode: crate::mode::HarnessMode::DailyDriver,
compaction: aggressive_compaction(),
cadence: None,
telemetry_data_dir: Some(dir.clone()),
..AgentLoopConfig::default()
},
);
let mut transcript = Transcript::seed("system prompt", "the task");
agent
.run_with_transcript(&mut transcript, "the task")
.await
.expect("run completes");
assert!(
transcript.compaction_events() > 0,
"the counter itself must still increment regardless of cadence"
);
let messages = crate::test_support::captured_at_least(tracing::Level::ERROR);
assert!(
messages.is_empty(),
"cadence: None must never emit the error-level regression signal, got: {messages:?}"
);
std::fs::remove_dir_all(&dir).ok();
}