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 run_actions: vec![],
82 session_id: SessionId {
83 id: 1,
84 external_id: None,
85 },
86 turn_count: 1,
87 user_input: "test".to_string(),
88 model_response: "response".to_string(),
89 finish_reason: FinishReason::Stop,
90 available_tools: vec![],
91 reasoning_only_strikes: 0,
92 empty_response_strikes: 0,
93 run_has_actions: false,
94 last_tool_calls_invalid: false,
95 all_user_inputs: vec!["test".to_string()],
96 is_reasoning_only: false,
97 is_empty_response: false,
98 is_text_only: false,
99 awaiting_human: false,
100 thinking_disabled: false,
101 original_thinking_enabled: true,
102 remaining_turns: 50,
103 };
104
105 assert!(matches!(guard.on_turn(&ctx).await, GuardDecision::Complete));
106 }
107
108 #[tokio::test]
109 async fn test_noop_guard_handles_degenerate_states() {
110 let guard = NoopGuard;
111
112 let ctx = GuardCtx {
114 session_id: SessionId::new(1),
115 turn_count: 1,
116 user_input: "test".to_string(),
117 model_response: "".to_string(),
118 finish_reason: FinishReason::Stop,
119 available_tools: vec![],
120 reasoning_only_strikes: 1,
121 empty_response_strikes: 0,
122 run_has_actions: false,
123 run_actions: vec![],
124 last_tool_calls_invalid: false,
125 all_user_inputs: vec!["test".to_string()],
126 is_reasoning_only: true,
127 is_empty_response: false,
128 is_text_only: false,
129 awaiting_human: false,
130 thinking_disabled: false,
131 original_thinking_enabled: true,
132 remaining_turns: 50,
133 };
134 assert!(matches!(
135 guard.on_turn(&ctx).await,
136 GuardDecision::Fail { .. }
137 ));
138
139 let mut ctx2 = ctx.clone();
141 ctx2.is_reasoning_only = false;
142 ctx2.is_empty_response = true;
143 assert!(matches!(
144 guard.on_turn(&ctx2).await,
145 GuardDecision::Fail { .. }
146 ));
147
148 let mut ctx3 = ctx.clone();
150 ctx3.is_reasoning_only = false;
151 ctx3.is_empty_response = false;
152 ctx3.is_text_only = true;
153 assert!(matches!(
154 guard.on_turn(&ctx3).await,
155 GuardDecision::Complete
156 ));
157
158 let mut ctx4 = ctx.clone();
160 ctx4.is_reasoning_only = false;
161 ctx4.is_text_only = false;
162 assert!(matches!(
163 guard.on_turn(&ctx4).await,
164 GuardDecision::Complete
165 ));
166 }
167}