use super::*;
use agent_base::types::NoticeKind;
struct GarbageJudgeClient;
#[async_trait::async_trait]
impl LlmProvider for GarbageJudgeClient {
async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
let chunks = vec![
Ok(StreamChunk::Text("total garbage !!!".to_string())),
Ok(StreamChunk::Stop {
finish_reason: Some("stop".to_string()),
}),
];
Ok(ChatStream::new(Box::pin(futures_util::stream::iter(
chunks,
))))
}
async fn chat(&self, _request: ChatRequest) -> Result<ChatResponse, LlmError> {
Ok(ChatResponse {
content: "total garbage !!!".to_string(),
tool_calls: vec![],
usage: agent_base::UsageInfo::default(),
finish_reason: LlmFinishReason::Stop,
raw: None,
reasoning_content: None,
thinking_signature: None,
})
}
fn capabilities(&self) -> Capabilities {
Capabilities::default()
}
fn info(&self) -> agent_base::llm_trait::ProviderInfo {
agent_base::llm_trait::ProviderInfo {
name: "garbage-judge".to_string(),
model: "garbage".to_string(),
version: None,
}
}
}
fn judge_fail_open_ctx() -> GuardCtx {
make_ctx_with_text(
"a user input that is not long enough for short-response detection",
"done!",
true,
true,
)
}
fn judge_fail_open_short_response_ctx() -> GuardCtx {
let long_input = "please analyze the repository structure in exhaustive detail ".repeat(3); make_ctx_with_text(&long_input, "done!", true, true)
}
fn fail_open_guard_with_judge() -> DefaultGuard {
let config = DefaultGuardConfig {
judge_fail_open: true,
..DefaultGuardConfig::default()
};
DefaultGuard::with_llm_client(config, Arc::new(GarbageJudgeClient))
}
#[tokio::test]
async fn judge_fail_open_emits_warning_notice_via_handle() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let guard = fail_open_guard_with_judge();
guard.set_notice(handle);
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete, got: {:?}",
decision
);
let notice = rx.try_recv().expect("notice must be sent on degradation");
assert_eq!(notice.kind, NoticeKind::Warning);
assert_eq!(notice.source, "guard");
assert_eq!(notice.text, "guard judge unparsed — treating as complete");
}
#[tokio::test]
async fn judge_fail_open_short_response_path_emits_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let guard = fail_open_guard_with_judge();
guard.set_notice(handle);
let ctx = judge_fail_open_short_response_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete, got: {:?}",
decision
);
let notice = rx
.try_recv()
.expect("notice must be sent on the short-response degradation");
assert_eq!(notice.kind, NoticeKind::Warning);
assert_eq!(notice.source, "guard");
assert_eq!(notice.text, "guard judge unparsed — treating as complete");
}
#[tokio::test]
async fn judge_fail_open_without_handle_behaves_as_before() {
let guard = fail_open_guard_with_judge();
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete regardless of handle presence, got: {:?}",
decision
);
}
#[tokio::test]
async fn judge_success_emits_no_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let config = DefaultGuardConfig {
judge_fail_open: true,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::with_llm_client(
config,
Arc::new(MockJudgeClient::new(serde_json::json!({
"done": true,
"reason": "answered"
}))),
);
guard.set_notice(handle);
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(matches!(decision, GuardDecision::Complete));
assert!(rx.try_recv().is_err(), "no notice on the success path");
}
#[tokio::test]
async fn non_judge_paths_emit_no_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let guard = DefaultGuard::new(DefaultGuardConfig::default());
guard.set_notice(handle);
let ctx = make_ctx(0, 0, false, false, true, false); let decision = guard.on_turn(&ctx).await;
assert!(matches!(decision, GuardDecision::Continue { .. }));
assert!(rx.try_recv().is_err(), "no notice on non-judge paths");
}
#[tokio::test]
async fn judge_transport_failure_emits_call_failed_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let config = DefaultGuardConfig {
judge_fail_open: true,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::with_llm_client(config, Arc::new(MockErrorClient));
guard.set_notice(handle);
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete, got: {:?}",
decision
);
let notice = rx
.try_recv()
.expect("notice must be sent on transport degradation");
assert_eq!(notice.kind, NoticeKind::Warning);
assert_eq!(notice.source, "guard");
assert_eq!(
notice.text,
"guard judge call failed — treating as complete"
);
}
#[tokio::test]
async fn judge_timeout_emits_call_failed_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let config = DefaultGuardConfig {
judge_fail_open: true,
judge_timeout_secs: 1,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::with_llm_client(config, Arc::new(MockTimeoutClient::new()));
guard.set_notice(handle);
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete, got: {:?}",
decision
);
let notice = rx
.try_recv()
.expect("notice must be sent on timeout degradation");
assert_eq!(notice.kind, NoticeKind::Warning);
assert_eq!(notice.source, "guard");
assert_eq!(
notice.text,
"guard judge call failed — treating as complete"
);
}
#[tokio::test]
async fn no_client_fail_open_emits_unavailable_notice() {
let (handle, mut rx) = agent_base::NoticeHandle::new_channel();
let config = DefaultGuardConfig {
judge_fail_open: true,
..DefaultGuardConfig::default()
};
let guard = DefaultGuard::new(config); guard.set_notice(handle);
let ctx = judge_fail_open_ctx();
let decision = guard.on_turn(&ctx).await;
assert!(
matches!(decision, GuardDecision::Complete),
"fail-open → Complete, got: {:?}",
decision
);
let notice = rx
.try_recv()
.expect("no-client fail-open must also be visible");
assert_eq!(notice.kind, NoticeKind::Warning);
assert_eq!(notice.source, "guard");
assert_eq!(
notice.text,
"guard judge unavailable — treating as complete"
);
}