codetether-agent 4.0.0

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
//! Context tracing for RLM iterations.
//!
//! Tracks token budget and context events per RLM iteration to enable
//! analysis of context window usage and optimization opportunities.
//!
//! # Events Traced
//!
//! - SystemPrompt: Initial system message with context summary
//! - GrepResult: Results from grep operations
//! - LlmQueryResult: Results from sub-LLM calls
//! - AssistantCode: Code generated by the assistant
//! - Final: Final answer returned
//!
//! # Usage
//!
//! ```ignore
//! use codetether_agent::rlm::context_trace::{ContextTrace, ContextEvent};
//!
//! let mut trace = ContextTrace::new(max_tokens);
//!
//! // Log events as they occur
//! trace.log_event(ContextEvent::SystemPrompt {
//!     content: system_prompt.clone(),
//!     tokens: count_tokens(&system_prompt),
//! });
//!
//! trace.log_event(ContextEvent::GrepResult {
//!     pattern: "async fn".to_string(),
//!     matches: 5,
//!     tokens: 150,
//! });
//!
//! // Get summary statistics
//! let stats = trace.summary();
//! println!("Total tokens: {}", stats.total_tokens);
//! println!("Budget used: {:.1}%", stats.budget_used_percent);
//! ```

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// Maximum number of events to keep in the trace buffer.
const MAX_EVENTS: usize = 1000;

/// A context event logged during RLM iteration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ContextEvent {
    /// System prompt with context summary
    SystemPrompt {
        content: String,
        tokens: usize,
    },
    /// Result from a grep operation
    GrepResult {
        pattern: String,
        matches: usize,
        tokens: usize,
    },
    /// Result from a sub-LLM query
    LlmQueryResult {
        query: String,
        response_preview: String,
        tokens: usize,
    },
    /// Code generated by the assistant
    AssistantCode {
        code: String,
        tokens: usize,
    },
    /// Output from code execution
    ExecutionOutput {
        output: String,
        tokens: usize,
    },
    /// Final answer
    Final {
        answer: String,
        tokens: usize,
    },
    /// Tool call
    ToolCall {
        name: String,
        arguments_preview: String,
        tokens: usize,
    },
    /// Tool result
    ToolResult {
        tool_call_id: String,
        result_preview: String,
        tokens: usize,
    },
}

impl ContextEvent {
    /// Get the token count for this event.
    pub fn tokens(&self) -> usize {
        match self {
            Self::SystemPrompt { tokens, .. } => *tokens,
            Self::GrepResult { tokens, .. } => *tokens,
            Self::LlmQueryResult { tokens, .. } => *tokens,
            Self::AssistantCode { tokens, .. } => *tokens,
            Self::ExecutionOutput { tokens, .. } => *tokens,
            Self::Final { tokens, .. } => *tokens,
            Self::ToolCall { tokens, .. } => *tokens,
            Self::ToolResult { tokens, .. } => *tokens,
        }
    }
    
    /// Get a human-readable label for this event type.
    pub fn label(&self) -> &'static str {
        match self {
            Self::SystemPrompt { .. } => "system_prompt",
            Self::GrepResult { .. } => "grep_result",
            Self::LlmQueryResult { .. } => "llm_query_result",
            Self::AssistantCode { .. } => "assistant_code",
            Self::ExecutionOutput { .. } => "execution_output",
            Self::Final { .. } => "final",
            Self::ToolCall { .. } => "tool_call",
            Self::ToolResult { .. } => "tool_result",
        }
    }
}

/// Context trace for a single RLM analysis run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextTrace {
    /// Maximum token budget
    max_tokens: usize,
    /// Events logged during the run
    events: VecDeque<ContextEvent>,
    /// Total tokens accumulated
    total_tokens: usize,
    /// Iteration number
    iteration: usize,
}

impl ContextTrace {
    /// Create a new context trace with the given token budget.
    pub fn new(max_tokens: usize) -> Self {
        Self {
            max_tokens,
            events: VecDeque::with_capacity(64),
            total_tokens: 0,
            iteration: 0,
        }
    }

    /// Log a context event.
    pub fn log_event(&mut self, event: ContextEvent) {
        self.total_tokens += event.tokens();
        
        // Evict oldest events if buffer is full
        while self.events.len() >= MAX_EVENTS {
            if let Some(evicted) = self.events.pop_front() {
                self.total_tokens = self.total_tokens.saturating_sub(evicted.tokens());
            }
        }
        
        self.events.push_back(event);
    }

    /// Increment the iteration counter.
    pub fn next_iteration(&mut self) {
        self.iteration += 1;
    }

    /// Get the current iteration number.
    pub fn iteration(&self) -> usize {
        self.iteration
    }

    /// Get the total tokens used.
    pub fn total_tokens(&self) -> usize {
        self.total_tokens
    }

    /// Get the remaining token budget.
    pub fn remaining_tokens(&self) -> usize {
        self.max_tokens.saturating_sub(self.total_tokens)
    }

    /// Get the percentage of budget used.
    pub fn budget_used_percent(&self) -> f32 {
        if self.max_tokens == 0 {
            0.0
        } else {
            (self.total_tokens as f32 / self.max_tokens as f32) * 100.0
        }
    }

    /// Check if the budget is exceeded.
    pub fn is_over_budget(&self) -> bool {
        self.total_tokens > self.max_tokens
    }

    /// Get all events.
    pub fn events(&self) -> &VecDeque<ContextEvent> {
        &self.events
    }

    /// Get events by type.
    pub fn events_of_type(&self, label: &str) -> Vec<&ContextEvent> {
        self.events
            .iter()
            .filter(|e| e.label() == label)
            .collect()
    }

    /// Get a summary of the trace.
    pub fn summary(&self) -> ContextTraceSummary {
        let mut event_counts = std::collections::HashMap::new();
        let mut event_tokens = std::collections::HashMap::new();
        
        for event in &self.events {
            let label = event.label().to_string();
            *event_counts.entry(label.clone()).or_insert(0) += 1;
            *event_tokens.entry(label).or_insert(0) += event.tokens();
        }
        
        ContextTraceSummary {
            total_tokens: self.total_tokens,
            max_tokens: self.max_tokens,
            budget_used_percent: self.budget_used_percent(),
            iteration: self.iteration,
            event_counts,
            event_tokens,
            events_len: self.events.len(),
        }
    }

    /// Estimate token count from text (rough approximation).
    ///
    /// Uses ~4 characters per token as a rough estimate.
    pub fn estimate_tokens(text: &str) -> usize {
        (text.chars().count() / 4).max(1)
    }

    /// Create an event from text with automatic token estimation.
    pub fn event_from_text(event: ContextEvent, text: &str) -> ContextEvent {
        let tokens = Self::estimate_tokens(text);
        match event {
            ContextEvent::SystemPrompt { content, .. } => {
                ContextEvent::SystemPrompt { content, tokens }
            }
            ContextEvent::GrepResult { pattern, matches, .. } => {
                ContextEvent::GrepResult { pattern, matches, tokens }
            }
            ContextEvent::LlmQueryResult { query, response_preview, .. } => {
                ContextEvent::LlmQueryResult { query, response_preview, tokens }
            }
            ContextEvent::AssistantCode { code, .. } => {
                ContextEvent::AssistantCode { code, tokens }
            }
            ContextEvent::ExecutionOutput { output, .. } => {
                ContextEvent::ExecutionOutput { output, tokens }
            }
            ContextEvent::Final { answer, .. } => {
                ContextEvent::Final { answer, tokens }
            }
            ContextEvent::ToolCall { name, arguments_preview, .. } => {
                ContextEvent::ToolCall { name, arguments_preview, tokens }
            }
            ContextEvent::ToolResult { tool_call_id, result_preview, .. } => {
                ContextEvent::ToolResult { tool_call_id, result_preview, tokens }
            }
        }
    }
}

/// Summary statistics for a context trace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextTraceSummary {
    /// Total tokens used
    pub total_tokens: usize,
    /// Maximum token budget
    pub max_tokens: usize,
    /// Percentage of budget used
    pub budget_used_percent: f32,
    /// Current iteration number
    pub iteration: usize,
    /// Count of events by type
    pub event_counts: std::collections::HashMap<String, usize>,
    /// Tokens by event type
    pub event_tokens: std::collections::HashMap<String, usize>,
    /// Total number of events
    pub events_len: usize,
}

impl ContextTraceSummary {
    /// Format as a human-readable string.
    pub fn format(&self) -> String {
        let mut lines = vec![
            format!("Context Trace Summary (iteration {})", self.iteration),
            format!("  Budget: {}/{} tokens ({:.1}%)", 
                self.total_tokens, self.max_tokens, self.budget_used_percent),
            format!("  Events: {}", self.events_len),
        ];
        
        if !self.event_counts.is_empty() {
            lines.push("  By type:".to_string());
            for (label, count) in &self.event_counts {
                let tokens = self.event_tokens.get(label).copied().unwrap_or(0);
                lines.push(format!("    {}: {} events, {} tokens", label, count, tokens));
            }
        }
        
        lines.join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_trace_has_zero_tokens() {
        let trace = ContextTrace::new(1000);
        assert_eq!(trace.total_tokens(), 0);
        assert_eq!(trace.remaining_tokens(), 1000);
    }

    #[test]
    fn log_event_adds_tokens() {
        let mut trace = ContextTrace::new(1000);
        trace.log_event(ContextEvent::SystemPrompt {
            content: "test".to_string(),
            tokens: 100,
        });
        assert_eq!(trace.total_tokens(), 100);
        assert_eq!(trace.remaining_tokens(), 900);
    }

    #[test]
    fn budget_exceeded_check() {
        let mut trace = ContextTrace::new(100);
        trace.log_event(ContextEvent::SystemPrompt {
            content: "test".to_string(),
            tokens: 150,
        });
        assert!(trace.is_over_budget());
    }

    #[test]
    fn event_type_filtering() {
        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 summary_statistics() {
        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.budget_used_percent, 15.0);
    }

    #[test]
    fn estimate_tokens_approximation() {
        // ~4 chars per token
        assert_eq!(ContextTrace::estimate_tokens("test"), 1);
        assert_eq!(ContextTrace::estimate_tokens("test test test test"), 4);
        assert_eq!(ContextTrace::estimate_tokens("12345678"), 2);
    }

    #[test]
    fn evict_old_events_when_full() {
        let mut trace = ContextTrace::new(10000);
        
        // Add more than MAX_EVENTS
        for i in 0..(MAX_EVENTS + 100) {
            trace.log_event(ContextEvent::SystemPrompt {
                content: format!("event {}", i),
                tokens: 1,
            });
        }
        
        // Should be capped at MAX_EVENTS
        assert!(trace.events.len() <= MAX_EVENTS);
        
        // Total tokens should be bounded
        assert!(trace.total_tokens <= MAX_EVENTS);
    }
}