agent_base/engine/
react_loop_guard.rs1use async_trait::async_trait;
2
3pub use agent_types::{GuardCtx, GuardDecision};
5
6use crate::engine::runtime::NoticeHandle;
7
8#[async_trait]
13pub trait ReactLoopGuard: Send + Sync {
14 async fn on_turn(&self, ctx: &GuardCtx) -> GuardDecision;
16
17 async fn on_tool_call(&self, _ctx: &GuardCtx) -> GuardDecision {
27 GuardDecision::Complete
28 }
29
30 fn set_notice(&self, _handle: NoticeHandle) {}
36}
37
38pub struct NoopGuard;
44
45#[async_trait]
46impl ReactLoopGuard for NoopGuard {
47 async fn on_turn(&self, ctx: &GuardCtx) -> GuardDecision {
48 if ctx.is_reasoning_only || ctx.is_empty_response {
49 GuardDecision::Fail {
50 error: if ctx.is_reasoning_only {
51 "model produced only reasoning, no output".to_string()
52 } else {
53 "model returned empty response".to_string()
54 },
55 }
56 } else {
57 GuardDecision::Complete
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65 use agent_types::{FinishReason, SessionId};
66
67 #[tokio::test]
68 async fn test_noop_guard_returns_complete() {
69 let guard = NoopGuard;
70 let ctx = GuardCtx {
71 session_id: SessionId {
72 id: 1,
73 external_id: None,
74 },
75 turn_count: 1,
76 user_input: "test".to_string(),
77 model_response: "response".to_string(),
78 finish_reason: FinishReason::Stop,
79 available_tools: vec![],
80 reasoning_only_strikes: 0,
81 empty_response_strikes: 0,
82 run_has_actions: false,
83 last_tool_calls_invalid: false,
84 all_user_inputs: vec!["test".to_string()],
85 is_reasoning_only: false,
86 is_empty_response: false,
87 is_text_only: false,
88 awaiting_human: false,
89 thinking_disabled: false,
90 original_thinking_enabled: true,
91 remaining_turns: 50,
92 };
93
94 assert!(matches!(guard.on_turn(&ctx).await, GuardDecision::Complete));
95 }
96
97 #[tokio::test]
98 async fn test_noop_guard_handles_degenerate_states() {
99 let guard = NoopGuard;
100
101 let ctx = GuardCtx {
103 session_id: SessionId::new(1),
104 turn_count: 1,
105 user_input: "test".to_string(),
106 model_response: "".to_string(),
107 finish_reason: FinishReason::Stop,
108 available_tools: vec![],
109 reasoning_only_strikes: 1,
110 empty_response_strikes: 0,
111 run_has_actions: false,
112 last_tool_calls_invalid: false,
113 all_user_inputs: vec!["test".to_string()],
114 is_reasoning_only: true,
115 is_empty_response: false,
116 is_text_only: false,
117 awaiting_human: false,
118 thinking_disabled: false,
119 original_thinking_enabled: true,
120 remaining_turns: 50,
121 };
122 assert!(matches!(
123 guard.on_turn(&ctx).await,
124 GuardDecision::Fail { .. }
125 ));
126
127 let mut ctx2 = ctx.clone();
129 ctx2.is_reasoning_only = false;
130 ctx2.is_empty_response = true;
131 assert!(matches!(
132 guard.on_turn(&ctx2).await,
133 GuardDecision::Fail { .. }
134 ));
135
136 let mut ctx3 = ctx.clone();
138 ctx3.is_reasoning_only = false;
139 ctx3.is_empty_response = false;
140 ctx3.is_text_only = true;
141 assert!(matches!(
142 guard.on_turn(&ctx3).await,
143 GuardDecision::Complete
144 ));
145
146 let mut ctx4 = ctx.clone();
148 ctx4.is_reasoning_only = false;
149 ctx4.is_text_only = false;
150 assert!(matches!(
151 guard.on_turn(&ctx4).await,
152 GuardDecision::Complete
153 ));
154 }
155}