use super::*;
use agent_base::StreamIntegrity;
fn dirty_ctx() -> GuardCtx {
let mut ctx = make_ctx(0, 0, false, false, false, false);
ctx.integrity = StreamIntegrity {
invalid_args: 1,
orphan_args_bytes: 0,
};
ctx
}
#[tokio::test]
async fn dirty_silent_round_requests_retry() {
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let decision = guard.on_tool_call(&dirty_ctx()).await;
assert!(
matches!(decision, GuardDecision::RetryTurn),
"expected RetryTurn, got {decision:?}"
);
}
#[tokio::test]
async fn visible_text_blocks_silent_retry() {
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let mut ctx = dirty_ctx();
ctx.has_visible_text = true;
let decision = guard.on_tool_call(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"expected Complete, got {decision:?}"
);
}
#[tokio::test]
async fn clean_stream_never_retries() {
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let ctx = make_ctx(0, 0, false, false, false, false);
let decision = guard.on_tool_call(&ctx).await;
assert!(matches!(decision, GuardDecision::Complete));
}
#[tokio::test]
async fn budget_exhausted_falls_through() {
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let mut ctx = dirty_ctx();
ctx.silent_retries_in_round = 1;
let decision = guard.on_tool_call(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"expected Complete past budget, got {decision:?}"
);
}
#[tokio::test]
async fn zero_budget_disables_gate() {
let cfg = DefaultGuardConfig {
max_silent_retries: 0,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::new(cfg);
let decision = guard.on_tool_call(&dirty_ctx()).await;
assert!(matches!(decision, GuardDecision::Complete));
}
#[tokio::test]
async fn thinking_visibility_knob() {
let mut ctx = dirty_ctx();
ctx.has_thinking = true;
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let decision = guard.on_tool_call(&ctx).await;
assert!(
matches!(decision, GuardDecision::RetryTurn),
"default tolerates thinking, expected RetryTurn, got {decision:?}"
);
let cfg = DefaultGuardConfig {
thinking_is_visible: true,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::new(cfg);
let decision = guard.on_tool_call(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"thinking_is_visible must block discard, got {decision:?}"
);
}
#[tokio::test]
async fn retry_wins_over_restore_thinking() {
let guard = DefaultGuard::new(DefaultGuardConfig::default());
let mut ctx = dirty_ctx();
ctx.thinking_disabled = true;
ctx.original_thinking_enabled = true;
let decision = guard.on_tool_call(&ctx).await;
assert!(
matches!(decision, GuardDecision::RetryTurn),
"expected RetryTurn over RestoreThinking, got {decision:?}"
);
}