Skip to main content

harn_vm/llm/
trace.rs

1use std::cell::RefCell;
2
3/// A single LLM call trace entry.
4#[derive(Debug, Clone)]
5pub struct LlmTraceEntry {
6    pub model: String,
7    pub input_tokens: i64,
8    pub output_tokens: i64,
9    pub duration_ms: u64,
10}
11
12thread_local! {
13    static LLM_TRACE: RefCell<Vec<LlmTraceEntry>> = const { RefCell::new(Vec::new()) };
14    static LLM_TRACING_ENABLED: RefCell<bool> = const { RefCell::new(false) };
15}
16
17/// Enable LLM tracing for the current thread.
18pub fn enable_tracing() {
19    LLM_TRACING_ENABLED.with(|v| *v.borrow_mut() = true);
20}
21
22/// Get and clear the trace log.
23pub fn take_trace() -> Vec<LlmTraceEntry> {
24    LLM_TRACE.with(|v| std::mem::take(&mut *v.borrow_mut()))
25}
26
27/// Clone the current trace log without consuming it.
28pub fn peek_trace() -> Vec<LlmTraceEntry> {
29    LLM_TRACE.with(|v| v.borrow().clone())
30}
31
32/// Summarize trace usage without consuming entries.
33pub fn peek_trace_summary() -> (i64, i64, i64, i64) {
34    LLM_TRACE.with(|v| {
35        let entries = v.borrow();
36        let mut input = 0i64;
37        let mut output = 0i64;
38        let mut duration = 0i64;
39        let count = entries.len() as i64;
40        for e in entries.iter() {
41            input += e.input_tokens;
42            output += e.output_tokens;
43            duration += e.duration_ms as i64;
44        }
45        (input, output, duration, count)
46    })
47}
48
49/// Reset thread-local trace state. Call between test runs.
50pub(crate) fn reset_trace_state() {
51    LLM_TRACE.with(|v| v.borrow_mut().clear());
52    LLM_TRACING_ENABLED.with(|v| *v.borrow_mut() = false);
53}
54
55pub(crate) fn trace_llm_call(entry: LlmTraceEntry) {
56    LLM_TRACING_ENABLED.with(|enabled| {
57        if *enabled.borrow() {
58            LLM_TRACE.with(|v| v.borrow_mut().push(entry));
59        }
60    });
61}
62
63/// Fine-grained event emitted during agent loop execution. Captures tool
64/// calls, LLM calls, interventions, compaction, and phase changes so
65/// downstream consumers (portal, IDE hosts, cloud runners) can display
66/// execution traces without reconstructing them from raw JSON.
67#[derive(Debug, Clone, serde::Serialize)]
68#[serde(tag = "type", rename_all = "snake_case")]
69pub enum AgentTraceEvent {
70    LlmCall {
71        call_id: String,
72        model: String,
73        input_tokens: i64,
74        output_tokens: i64,
75        cache_tokens: i64,
76        duration_ms: u64,
77        iteration: usize,
78    },
79    ToolExecution {
80        tool_name: String,
81        tool_use_id: String,
82        duration_ms: u64,
83        status: String,
84        classification: String,
85        iteration: usize,
86    },
87    ToolRejected {
88        tool_name: String,
89        reason: String,
90        iteration: usize,
91    },
92    LoopIntervention {
93        tool_name: String,
94        kind: String,
95        count: usize,
96        iteration: usize,
97    },
98    ContextCompaction {
99        archived_messages: usize,
100        new_summary_len: usize,
101        iteration: usize,
102    },
103    PhaseChange {
104        from_phase: String,
105        to_phase: String,
106        iteration: usize,
107    },
108    LoopComplete {
109        status: String,
110        iterations: usize,
111        total_duration_ms: u64,
112        tools_used: Vec<String>,
113        successful_tools: Vec<String>,
114    },
115    /// Emitted when `llm_call` re-prompts the model after the previous
116    /// response failed `output_schema` validation. One event per retry;
117    /// `attempt` counts retries (the initial call is attempt 0 and
118    /// produces no event; the first retry emits `attempt: 1`).
119    ///
120    /// The retry does **not** persist the invalid response — the
121    /// original messages are replayed with a single appended user-role
122    /// correction that cites the validation errors and schema. That
123    /// correction text is surfaced here as `correction_prompt` so
124    /// transcripts show both why the retry happened and what was sent.
125    SchemaRetry {
126        attempt: usize,
127        errors: Vec<String>,
128        nudge_used: bool,
129        correction_prompt: String,
130    },
131    /// Emitted when `llm_call` aborts a streaming provider response
132    /// because the partial JSON content can no longer satisfy
133    /// `output_schema`. `chunks_consumed` counts text-delta chunks seen
134    /// before the abort; `provider` / `model` track the route that fired
135    /// so cost dashboards can attribute the savings.
136    SchemaStreamAborted {
137        provider: String,
138        model: String,
139        reason: String,
140        path: String,
141        chunks_consumed: usize,
142    },
143    TypedCheckpoint {
144        name: String,
145        status: String,
146        checkpoint_attempts: usize,
147        llm_attempts: usize,
148        error_category: Option<String>,
149        errors: Vec<String>,
150        repaired: bool,
151        final_accepted: bool,
152        raw_text: String,
153    },
154    NativeToolFallback {
155        iteration: usize,
156        accepted: bool,
157        policy: String,
158        fallback_index: usize,
159        tool_call_count: usize,
160    },
161    EmptyCompletionRetry {
162        iteration: usize,
163        attempt: usize,
164        error: String,
165    },
166    /// Emitted when a `models:`/`ladder:` model ladder advances from one rung
167    /// to the next because the current rung hit a transport-class failure
168    /// (connection/timeout/429/5xx/circuit_open). Schema-validation failures
169    /// never emit this — they re-ask the SAME rung's model. `from_index` is
170    /// the 0-based ladder position that failed; `category` is the failover
171    /// error category that drove the advance.
172    ModelsAdvance {
173        from_index: usize,
174        from_model: String,
175        to_model: String,
176        category: String,
177    },
178}
179
180thread_local! {
181    static AGENT_TRACE: RefCell<Vec<AgentTraceEvent>> = const { RefCell::new(Vec::new()) };
182}
183
184/// Emit an agent trace event.
185pub(crate) fn emit_agent_event(event: AgentTraceEvent) {
186    AGENT_TRACE.with(|v| v.borrow_mut().push(event));
187}
188
189/// Get and clear the agent trace log.
190pub fn take_agent_trace() -> Vec<AgentTraceEvent> {
191    AGENT_TRACE.with(|v| std::mem::take(&mut *v.borrow_mut()))
192}
193
194/// Clone the current agent trace log without consuming it.
195pub fn peek_agent_trace() -> Vec<AgentTraceEvent> {
196    AGENT_TRACE.with(|v| v.borrow().clone())
197}
198
199/// Produce a rolled-up summary of agent trace events as JSON.
200pub fn agent_trace_summary() -> serde_json::Value {
201    AGENT_TRACE.with(|v| {
202        let events = v.borrow();
203        let mut llm_calls = 0usize;
204        let mut tool_executions = 0usize;
205        let mut tool_rejections = 0usize;
206        let mut interventions = 0usize;
207        let mut compactions = 0usize;
208        let mut native_text_tool_fallbacks = 0usize;
209        let mut native_text_tool_fallback_rejections = 0usize;
210        let mut empty_completion_retries = 0usize;
211        let mut models_advances = 0usize;
212        let mut schema_stream_aborts = 0usize;
213        let mut typed_checkpoints = 0usize;
214        let mut typed_checkpoint_failures = 0usize;
215        let mut total_input_tokens = 0i64;
216        let mut total_output_tokens = 0i64;
217        let mut total_llm_duration_ms = 0u64;
218        let mut total_tool_duration_ms = 0u64;
219        let mut tools_used: Vec<String> = Vec::new();
220        let mut status = "unknown".to_string();
221        let mut iterations = 0usize;
222        let mut total_duration_ms = 0u64;
223
224        for event in events.iter() {
225            match event {
226                AgentTraceEvent::LlmCall {
227                    input_tokens,
228                    output_tokens,
229                    duration_ms,
230                    ..
231                } => {
232                    llm_calls += 1;
233                    total_input_tokens += input_tokens;
234                    total_output_tokens += output_tokens;
235                    total_llm_duration_ms += duration_ms;
236                }
237                AgentTraceEvent::ToolExecution {
238                    tool_name,
239                    duration_ms,
240                    ..
241                } => {
242                    tool_executions += 1;
243                    total_tool_duration_ms += duration_ms;
244                    if !tools_used.contains(tool_name) {
245                        tools_used.push(tool_name.clone());
246                    }
247                }
248                AgentTraceEvent::ToolRejected { .. } => {
249                    tool_rejections += 1;
250                }
251                AgentTraceEvent::LoopIntervention { .. } => {
252                    interventions += 1;
253                }
254                AgentTraceEvent::ContextCompaction { .. } => {
255                    compactions += 1;
256                }
257                AgentTraceEvent::PhaseChange { .. } => {}
258                AgentTraceEvent::LoopComplete {
259                    status: s,
260                    iterations: i,
261                    total_duration_ms: d,
262                    ..
263                } => {
264                    status = s.clone();
265                    iterations = *i;
266                    total_duration_ms = *d;
267                }
268                AgentTraceEvent::SchemaRetry { .. } => {}
269                AgentTraceEvent::SchemaStreamAborted { .. } => {
270                    schema_stream_aborts += 1;
271                }
272                AgentTraceEvent::TypedCheckpoint { final_accepted, .. } => {
273                    typed_checkpoints += 1;
274                    if !final_accepted {
275                        typed_checkpoint_failures += 1;
276                    }
277                }
278                AgentTraceEvent::NativeToolFallback { accepted, .. } => {
279                    native_text_tool_fallbacks += 1;
280                    if !accepted {
281                        native_text_tool_fallback_rejections += 1;
282                    }
283                }
284                AgentTraceEvent::EmptyCompletionRetry { .. } => {
285                    empty_completion_retries += 1;
286                }
287                AgentTraceEvent::ModelsAdvance { .. } => {
288                    models_advances += 1;
289                }
290            }
291        }
292
293        serde_json::json!({
294            "status": status,
295            "iterations": iterations,
296            "total_duration_ms": total_duration_ms,
297            "llm_calls": llm_calls,
298            "tool_executions": tool_executions,
299            "tool_rejections": tool_rejections,
300            "interventions": interventions,
301            "compactions": compactions,
302            "native_text_tool_fallbacks": native_text_tool_fallbacks,
303            "native_text_tool_fallback_rejections": native_text_tool_fallback_rejections,
304            "empty_completion_retries": empty_completion_retries,
305            "models_advances": models_advances,
306            "schema_stream_aborts": schema_stream_aborts,
307            "typed_checkpoints": typed_checkpoints,
308            "typed_checkpoint_failures": typed_checkpoint_failures,
309            "total_input_tokens": total_input_tokens,
310            "total_output_tokens": total_output_tokens,
311            "total_llm_duration_ms": total_llm_duration_ms,
312            "total_tool_duration_ms": total_tool_duration_ms,
313            "tools_used": tools_used,
314        })
315    })
316}
317
318/// Reset agent trace state. Call between test runs.
319pub(crate) fn reset_agent_trace_state() {
320    AGENT_TRACE.with(|v| v.borrow_mut().clear());
321}