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