codetether-agent 4.5.2

A2A-native AI coding agent for the CodeTether ecosystem
Documentation
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Integration tests for the RLM oracle system.

use codetether_agent::rlm::RlmStats;
use codetether_agent::rlm::context_trace::{ContextEvent, ContextTrace};
use codetether_agent::rlm::oracle::{
    FinalAnswerFormat, GrepOracle, GrepVerification, OracleResult, QueryType, TraceValidator,
    TreeSitterOracle,
};
use codetether_agent::rlm::repl::RlmAnalysisResult;

fn sample_rust_code() -> String {
    r#"
use anyhow::Result;
use std::collections::HashMap;

/// Configuration struct for the application
pub struct Config {
    pub debug: bool,
    pub max_retries: usize,
    pub timeout_ms: u64,
}

impl Config {
    /// Create a new config with defaults
    pub fn new() -> Self {
        Self {
            debug: false,
            max_retries: 3,
            timeout_ms: 5000,
        }
    }
    
    /// Enable debug mode
    pub fn with_debug(mut self) -> Self {
        self.debug = true;
        self
    }
}

/// Process input data
pub async fn process(input: &str) -> Result<String> {
    let data = parse(input)?;
    let result = transform(&data)?;
    Ok(result)
}

/// Parse input string
fn parse(input: &str) -> Result<String> {
    if input.is_empty() {
        return Err(anyhow::anyhow!("Empty input"));
    }
    Ok(input.trim().to_string())
}

/// Transform the data
fn transform(data: &str) -> Result<String> {
    Ok(data.to_uppercase())
}

pub enum Status {
    Active,
    Inactive,
    Pending,
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_process() {
        let result = process("hello");
        assert!(result.is_ok());
    }
    
    #[tokio::test]
    async fn test_async_process() {
        let result = process("test").await;
        assert!(result.is_ok());
    }
}
"#
    .to_string()
}

fn make_analysis_result(answer: &str) -> RlmAnalysisResult {
    RlmAnalysisResult {
        answer: answer.to_string(),
        iterations: 2,
        sub_queries: vec![],
        stats: RlmStats {
            input_tokens: 150,
            output_tokens: 80,
            iterations: 2,
            subcalls: 0,
            elapsed_ms: 500,
            compression_ratio: 1.0,
        },
    }
}

// ============================================================================
// Grep Oracle Tests
// ============================================================================

#[test]
fn grep_oracle_finds_async_functions() {
    let source = sample_rust_code();
    let oracle = GrepOracle::new(source);

    let matches = oracle.grep(r"\basync\s+fn\b").unwrap();
    assert!(matches.len() >= 1);

    // Should find the async process function
    assert!(matches.iter().any(|(_, line)| line.contains("process")));
}

#[test]
fn grep_oracle_finds_public_functions() {
    let source = sample_rust_code();
    let oracle = GrepOracle::new(source);

    let matches = oracle.grep(r"\bpub\s+fn\b").unwrap();
    assert!(matches.len() >= 2);
}

#[test]
fn grep_oracle_infers_async_pattern() {
    let pattern = GrepOracle::infer_pattern("Find all async functions");
    assert_eq!(pattern, Some(r"\basync\s+fn\b".to_string()));
}

#[test]
fn grep_oracle_infers_pub_pattern() {
    let pattern = GrepOracle::infer_pattern("List all public functions");
    assert_eq!(pattern, Some(r"\bpub\s+fn\b".to_string()));
}

#[test]
fn grep_oracle_infers_struct_pattern() {
    let pattern = GrepOracle::infer_pattern("Find all structs");
    assert_eq!(pattern, Some(r"\bstruct\b".to_string()));
}

#[test]
fn grep_oracle_classifies_pattern_match_query() {
    assert_eq!(
        GrepOracle::classify_query("Find all async functions"),
        QueryType::PatternMatch
    );
    assert_eq!(
        GrepOracle::classify_query("Count occurrences of TODO"),
        QueryType::PatternMatch
    );
    assert_eq!(
        GrepOracle::classify_query("Search for error handling"),
        QueryType::PatternMatch
    );
}

#[test]
fn grep_oracle_verifies_count_result() {
    let source = sample_rust_code();
    let oracle = GrepOracle::new(source);

    // This fixture currently contains two async functions.
    let result = oracle.verify("Found 1 async function", "Count async functions");
    assert_eq!(
        result,
        GrepVerification::SubsetMatch {
            claimed: 1,
            actual: 2
        }
    );
}

// ============================================================================
// Tree-sitter Oracle Tests
// ============================================================================

#[test]
fn tree_sitter_oracle_gets_functions() {
    let mut oracle = TreeSitterOracle::new(sample_rust_code());
    let functions = oracle.get_functions().unwrap();

    assert!(functions.len() >= 4);

    let names: Vec<&str> = functions.iter().map(|f| f.name.as_str()).collect();
    assert!(names.contains(&"new"));
    assert!(names.contains(&"with_debug"));
    assert!(names.contains(&"process"));
    assert!(names.contains(&"parse"));
    assert!(names.contains(&"transform"));
}

#[test]
fn tree_sitter_oracle_gets_structs() {
    let mut oracle = TreeSitterOracle::new(sample_rust_code());
    let structs = oracle.get_structs().unwrap();

    assert!(structs.len() >= 1);

    let config = structs.iter().find(|s| s.name == "Config").unwrap();
    assert!(config.fields.contains(&"debug".to_string()));
    assert!(config.fields.contains(&"max_retries".to_string()));
    assert!(config.fields.contains(&"timeout_ms".to_string()));
}

#[test]
fn tree_sitter_oracle_gets_enums() {
    let mut oracle = TreeSitterOracle::new(sample_rust_code());
    let enums = oracle.get_enums().unwrap();

    assert!(enums.len() >= 1);

    let status = enums.iter().find(|e| e.name == "Status").unwrap();
    assert!(status.variants.contains(&"Active".to_string()));
    assert!(status.variants.contains(&"Inactive".to_string()));
    assert!(status.variants.contains(&"Pending".to_string()));
}

#[test]
fn tree_sitter_oracle_counts_error_patterns() {
    let mut oracle = TreeSitterOracle::new(sample_rust_code());
    let counts = oracle.count_error_patterns().unwrap();

    // Should find Result<T> types
    assert!(counts.result_types >= 3);

    // Should find ? operators
    assert!(counts.try_operators >= 2);
}

#[test]
fn tree_sitter_oracle_query() {
    let mut oracle = TreeSitterOracle::new(sample_rust_code());

    let result = oracle
        .query("(function_item name: (identifier) @name)")
        .unwrap();

    assert!(!result.matches.is_empty());

    // All matches should have a "name" capture
    for m in &result.matches {
        assert!(m.captures.contains_key("name"));
    }
}

// ============================================================================
// Trace Validator Tests
// ============================================================================

#[test]
fn trace_validator_validates_grep_match() {
    let validator = TraceValidator::new();
    let source = sample_rust_code();
    let result = make_analysis_result("30:pub async fn process(input: &str) -> Result<String> {");

    match validator.validate(&result, &source, Some("test.rs"), None, None) {
        OracleResult::Golden(trace) => {
            assert!(trace.answer.contains("async"));
        }
        OracleResult::Consensus { .. } => {}
        OracleResult::Unverified { .. } => {}
        OracleResult::Failed { .. } => {}
    }
}

#[test]
fn trace_validator_marks_semantic_as_unverified() {
    let validator = TraceValidator::new();
    let source = sample_rust_code();
    let result = make_analysis_result(
        r#"{"kind":"semantic","file":"test.rs","answer":"This function processes input by parsing and transforming it"}"#,
    );

    match validator.validate(&result, &source, Some("test.rs"), None, None) {
        OracleResult::Unverified { reason, .. } => {
            assert!(reason.contains("Semantic"));
        }
        OracleResult::Consensus { .. } => panic!("Expected Unverified for semantic query"),
        _ => panic!("Expected Unverified for semantic query"),
    }
}

#[test]
fn trace_validator_batch_validate() {
    let validator = TraceValidator::new();
    let source = sample_rust_code();

    let traces = vec![
        (
            make_analysis_result("1 async function"),
            source.as_str(),
            None,
        ),
        (
            make_analysis_result("Explanation text"),
            source.as_str(),
            None,
        ),
    ];

    let stats = validator.batch_validate(traces);

    assert!(stats.total() == 2);
}

// ============================================================================
// Context Trace Tests
// ============================================================================

#[test]
fn context_trace_tracks_tokens() {
    let mut trace = ContextTrace::new(1000);

    trace.log_event(ContextEvent::SystemPrompt {
        content: "System prompt".to_string(),
        tokens: 100,
    });

    trace.log_event(ContextEvent::GrepResult {
        pattern: "async".to_string(),
        matches: 5,
        tokens: 50,
    });

    assert_eq!(trace.total_tokens(), 150);
    assert_eq!(trace.remaining_tokens(), 850);
    assert!((trace.budget_used_percent() - 15.0).abs() < 0.01);
}

#[test]
fn context_trace_detects_over_budget() {
    let mut trace = ContextTrace::new(100);

    trace.log_event(ContextEvent::Final {
        answer: "Big answer".to_string(),
        tokens: 150,
    });

    assert!(trace.is_over_budget());
}

#[test]
fn context_trace_filters_by_type() {
    let mut trace = ContextTrace::new(1000);

    trace.log_event(ContextEvent::SystemPrompt {
        content: "test".to_string(),
        tokens: 100,
    });

    trace.log_event(ContextEvent::GrepResult {
        pattern: "async".to_string(),
        matches: 5,
        tokens: 50,
    });

    trace.log_event(ContextEvent::SystemPrompt {
        content: "test2".to_string(),
        tokens: 75,
    });

    let system_events = trace.events_of_type("system_prompt");
    assert_eq!(system_events.len(), 2);

    let grep_events = trace.events_of_type("grep_result");
    assert_eq!(grep_events.len(), 1);
}

#[test]
fn context_trace_summary() {
    let mut trace = ContextTrace::new(1000);

    trace.log_event(ContextEvent::SystemPrompt {
        content: "test".to_string(),
        tokens: 100,
    });

    trace.log_event(ContextEvent::GrepResult {
        pattern: "async".to_string(),
        matches: 5,
        tokens: 50,
    });

    trace.next_iteration();

    let summary = trace.summary();
    assert_eq!(summary.total_tokens, 150);
    assert_eq!(summary.iteration, 1);
    assert_eq!(summary.events_len, 2);
}

// ============================================================================
// Final Answer Format Tests
// ============================================================================

#[test]
fn parse_line_numbered_matches() {
    let answer = "42:async fn foo()\n100:pub struct Bar";
    let format = FinalAnswerFormat::parse(answer);

    match format {
        FinalAnswerFormat::LineNumberedMatches { matches } => {
            assert_eq!(matches.len(), 2);
            assert_eq!(matches[0], (42, "async fn foo()".to_string()));
            assert_eq!(matches[1], (100, "pub struct Bar".to_string()));
        }
        _ => panic!("Expected LineNumberedMatches"),
    }
}

#[test]
fn parse_count_result() {
    let answer = "Found 15 async functions";
    let format = FinalAnswerFormat::parse(answer);

    match format {
        FinalAnswerFormat::CountResult { count } => {
            assert_eq!(count, 15);
        }
        _ => panic!("Expected CountResult"),
    }
}

#[test]
fn parse_structured_data() {
    let answer = r#"{"functions": ["foo", "bar"]}"#;
    let format = FinalAnswerFormat::parse(answer);

    match format {
        FinalAnswerFormat::StructuredData { data } => {
            assert!(data["functions"].is_array());
        }
        _ => panic!("Expected StructuredData"),
    }
}

#[test]
fn parse_free_form_text() {
    let answer = "This function handles errors by using the ? operator";
    let format = FinalAnswerFormat::parse(answer);

    match format {
        FinalAnswerFormat::FreeFormText { text } => {
            assert!(text.contains("? operator"));
        }
        _ => panic!("Expected FreeFormText"),
    }
}