1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
//! Completion gate parsing for autonomous loop mode.
//!
//! This module parses COMPLETION_GATE blocks from Claude's output to determine
//! if the task is truly complete. The format is inspired by Ralph's autonomous
//! development framework.
//!
//! Format:
//! ```text
//! <COMPLETION_GATE>
//! ready: true|false
//! reason: "explanation of completion status"
//! progress: "summary of what was accomplished"
//! blockers: ["list", "of", "blockers"] (optional, only when ready: false)
//! </COMPLETION_GATE>
//! ```
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Represents a parsed COMPLETION_GATE block from Claude's output.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CompletionGate {
/// Whether the task is ready to complete.
pub ready: bool,
/// Explanation of the completion status.
pub reason: Option<String>,
/// Summary of what was accomplished.
pub progress: Option<String>,
/// List of blockers if not ready.
pub blockers: Option<Vec<String>>,
/// Any additional fields that were parsed.
#[serde(flatten)]
pub extra: HashMap<String, serde_json::Value>,
}
impl CompletionGate {
/// Parse a COMPLETION_GATE block from text output.
///
/// Returns None if no valid COMPLETION_GATE is found.
pub fn parse(text: &str) -> Option<Self> {
// Find the COMPLETION_GATE block
let start_tag = "<COMPLETION_GATE>";
let end_tag = "</COMPLETION_GATE>";
let start_idx = text.find(start_tag)?;
let end_idx = text.find(end_tag)?;
if end_idx <= start_idx {
return None;
}
let content = &text[start_idx + start_tag.len()..end_idx];
let content = content.trim();
// Try to parse as JSON first
if content.starts_with('{') {
if let Ok(gate) = serde_json::from_str::<CompletionGate>(content) {
return Some(gate);
}
}
// Fall back to YAML-like parsing
Self::parse_yaml_like(content)
}
/// Parse a YAML-like format (key: value lines).
fn parse_yaml_like(content: &str) -> Option<Self> {
let mut gate = CompletionGate::default();
for line in content.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Some((key, value)) = line.split_once(':') {
let key = key.trim().to_lowercase();
let value = value.trim();
match key.as_str() {
"ready" => {
gate.ready = value.to_lowercase() == "true"
|| value == "yes"
|| value == "1";
}
"reason" => {
gate.reason = Some(Self::unquote(value));
}
"progress" => {
gate.progress = Some(Self::unquote(value));
}
"blockers" => {
// Try to parse as JSON array
if let Ok(blockers) = serde_json::from_str::<Vec<String>>(value) {
gate.blockers = Some(blockers);
} else {
// Single blocker as string
gate.blockers = Some(vec![Self::unquote(value)]);
}
}
_ => {
// Store unknown fields
if let Ok(json_val) = serde_json::from_str(value) {
gate.extra.insert(key, json_val);
} else {
gate.extra.insert(
key,
serde_json::Value::String(Self::unquote(value)),
);
}
}
}
}
}
Some(gate)
}
/// Remove surrounding quotes from a string value.
fn unquote(s: &str) -> String {
let s = s.trim();
if (s.starts_with('"') && s.ends_with('"'))
|| (s.starts_with('\'') && s.ends_with('\''))
{
s[1..s.len() - 1].to_string()
} else {
s.to_string()
}
}
/// Find all COMPLETION_GATE blocks in the output and return the last one.
///
/// This is useful when Claude produces multiple completion gates during
/// a long-running task, and we want to use the final status.
pub fn parse_last(text: &str) -> Option<Self> {
let start_tag = "<COMPLETION_GATE>";
let end_tag = "</COMPLETION_GATE>";
// Find the last occurrence of the start tag
let start_idx = text.rfind(start_tag)?;
let remaining = &text[start_idx..];
// Find the end tag after the last start tag
let end_idx = remaining.find(end_tag)?;
// Parse just this last gate
Self::parse(&remaining[..end_idx + end_tag.len()])
}
}
/// State tracking for the circuit breaker in autonomous loop mode.
#[derive(Debug, Clone, Default)]
pub struct CircuitBreaker {
/// Number of consecutive runs without file changes.
pub runs_without_changes: u32,
/// Threshold for opening circuit due to no changes (default: 3).
pub no_change_threshold: u32,
/// Number of consecutive runs with the same error.
pub same_error_count: u32,
/// Threshold for opening circuit due to same error (default: 5).
pub same_error_threshold: u32,
/// Last error message seen.
pub last_error: Option<String>,
/// Total number of loop iterations.
pub iteration_count: u32,
/// Maximum allowed iterations (default: 10).
pub max_iterations: u32,
/// Whether the circuit is open (task should stop).
pub is_open: bool,
/// Reason why circuit was opened.
pub open_reason: Option<String>,
}
impl CircuitBreaker {
/// Create a new circuit breaker with default thresholds.
pub fn new() -> Self {
Self {
no_change_threshold: 3,
same_error_threshold: 5,
max_iterations: 10,
..Default::default()
}
}
/// Create with custom thresholds.
pub fn with_thresholds(no_change: u32, same_error: u32, max_iterations: u32) -> Self {
Self {
no_change_threshold: no_change,
same_error_threshold: same_error,
max_iterations,
..Default::default()
}
}
/// Record a new iteration. Returns true if circuit should remain closed.
pub fn record_iteration(&mut self, had_changes: bool, error: Option<&str>) -> bool {
self.iteration_count += 1;
// Check max iterations
if self.iteration_count >= self.max_iterations {
self.is_open = true;
self.open_reason = Some(format!(
"Maximum iterations ({}) reached",
self.max_iterations
));
return false;
}
// Track file changes
if had_changes {
self.runs_without_changes = 0;
} else {
self.runs_without_changes += 1;
if self.runs_without_changes >= self.no_change_threshold {
self.is_open = true;
self.open_reason = Some(format!(
"No file changes for {} consecutive runs",
self.runs_without_changes
));
return false;
}
}
// Track errors
match (error, &self.last_error) {
(Some(err), Some(last)) if err == last => {
self.same_error_count += 1;
if self.same_error_count >= self.same_error_threshold {
self.is_open = true;
self.open_reason = Some(format!(
"Same error repeated {} times: {}",
self.same_error_count, err
));
return false;
}
}
(Some(err), _) => {
self.last_error = Some(err.to_string());
self.same_error_count = 1;
}
(None, _) => {
self.same_error_count = 0;
self.last_error = None;
}
}
true // Circuit remains closed
}
/// Check if the circuit breaker is open.
pub fn should_stop(&self) -> bool {
self.is_open
}
/// Reset the circuit breaker.
pub fn reset(&mut self) {
*self = Self::with_thresholds(
self.no_change_threshold,
self.same_error_threshold,
self.max_iterations,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_yaml_format() {
let text = r#"
Some output before
<COMPLETION_GATE>
ready: true
reason: "All tests pass"
progress: "Implemented feature X"
</COMPLETION_GATE>
More output after
"#;
let gate = CompletionGate::parse(text).unwrap();
assert!(gate.ready);
assert_eq!(gate.reason.as_deref(), Some("All tests pass"));
assert_eq!(gate.progress.as_deref(), Some("Implemented feature X"));
}
#[test]
fn test_parse_not_ready() {
let text = r#"
<COMPLETION_GATE>
ready: false
reason: "Tests are failing"
blockers: ["Fix test_foo", "Fix test_bar"]
</COMPLETION_GATE>
"#;
let gate = CompletionGate::parse(text).unwrap();
assert!(!gate.ready);
assert_eq!(gate.reason.as_deref(), Some("Tests are failing"));
assert_eq!(
gate.blockers,
Some(vec!["Fix test_foo".to_string(), "Fix test_bar".to_string()])
);
}
#[test]
fn test_parse_json_format() {
let text = r#"
<COMPLETION_GATE>
{
"ready": true,
"reason": "Done",
"progress": "All good"
}
</COMPLETION_GATE>
"#;
let gate = CompletionGate::parse(text).unwrap();
assert!(gate.ready);
assert_eq!(gate.reason.as_deref(), Some("Done"));
}
#[test]
fn test_parse_last_gate() {
let text = r#"
<COMPLETION_GATE>
ready: false
reason: "Still working"
</COMPLETION_GATE>
Some more work...
<COMPLETION_GATE>
ready: true
reason: "Finally done"
</COMPLETION_GATE>
"#;
let gate = CompletionGate::parse_last(text).unwrap();
assert!(gate.ready);
assert_eq!(gate.reason.as_deref(), Some("Finally done"));
}
#[test]
fn test_no_gate() {
let text = "No completion gate here";
assert!(CompletionGate::parse(text).is_none());
}
#[test]
fn test_circuit_breaker_max_iterations() {
let mut cb = CircuitBreaker::with_thresholds(3, 5, 5);
for _ in 0..4 {
assert!(cb.record_iteration(true, None));
}
assert!(!cb.record_iteration(true, None)); // 5th iteration should trip
assert!(cb.is_open);
assert!(cb.open_reason.as_ref().unwrap().contains("Maximum iterations"));
}
#[test]
fn test_circuit_breaker_no_changes() {
let mut cb = CircuitBreaker::with_thresholds(3, 5, 10);
assert!(cb.record_iteration(false, None)); // 1st no change
assert!(cb.record_iteration(false, None)); // 2nd no change
assert!(!cb.record_iteration(false, None)); // 3rd no change - trips
assert!(cb.is_open);
assert!(cb.open_reason.as_ref().unwrap().contains("No file changes"));
}
#[test]
fn test_circuit_breaker_same_error() {
let mut cb = CircuitBreaker::with_thresholds(10, 3, 10);
let err = "Test failed";
assert!(cb.record_iteration(true, Some(err)));
assert!(cb.record_iteration(true, Some(err)));
assert!(!cb.record_iteration(true, Some(err))); // 3rd same error - trips
assert!(cb.is_open);
assert!(cb.open_reason.as_ref().unwrap().contains("Same error"));
}
#[test]
fn test_circuit_breaker_different_errors_ok() {
let mut cb = CircuitBreaker::with_thresholds(10, 3, 10);
assert!(cb.record_iteration(true, Some("error 1")));
assert!(cb.record_iteration(true, Some("error 2")));
assert!(cb.record_iteration(true, Some("error 3")));
// Different errors don't trip the circuit
assert!(!cb.is_open);
}
#[test]
fn test_circuit_breaker_changes_reset() {
let mut cb = CircuitBreaker::with_thresholds(3, 5, 10);
assert!(cb.record_iteration(false, None)); // 1 no change
assert!(cb.record_iteration(false, None)); // 2 no changes
assert!(cb.record_iteration(true, None)); // has changes - resets
assert!(cb.record_iteration(false, None)); // 1 no change again
assert!(cb.record_iteration(false, None)); // 2 no changes
// Still shouldn't trip because we had a change in between
assert!(!cb.is_open);
}
}
|