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, burin-code) can display execution traces
66/// 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    SchemaRetry {
120        attempt: usize,
121        errors: Vec<String>,
122        nudge_used: bool,
123    },
124    NativeToolFallback {
125        iteration: usize,
126        accepted: bool,
127        policy: String,
128        fallback_index: usize,
129        tool_call_count: usize,
130    },
131    EmptyCompletionRetry {
132        iteration: usize,
133        attempt: usize,
134        error: String,
135    },
136}
137
138thread_local! {
139    static AGENT_TRACE: RefCell<Vec<AgentTraceEvent>> = const { RefCell::new(Vec::new()) };
140}
141
142/// Emit an agent trace event.
143pub(crate) fn emit_agent_event(event: AgentTraceEvent) {
144    AGENT_TRACE.with(|v| v.borrow_mut().push(event));
145}
146
147/// Get and clear the agent trace log.
148pub fn take_agent_trace() -> Vec<AgentTraceEvent> {
149    AGENT_TRACE.with(|v| std::mem::take(&mut *v.borrow_mut()))
150}
151
152/// Clone the current agent trace log without consuming it.
153pub fn peek_agent_trace() -> Vec<AgentTraceEvent> {
154    AGENT_TRACE.with(|v| v.borrow().clone())
155}
156
157/// Produce a rolled-up summary of agent trace events as JSON.
158pub fn agent_trace_summary() -> serde_json::Value {
159    AGENT_TRACE.with(|v| {
160        let events = v.borrow();
161        let mut llm_calls = 0usize;
162        let mut tool_executions = 0usize;
163        let mut tool_rejections = 0usize;
164        let mut interventions = 0usize;
165        let mut compactions = 0usize;
166        let mut native_text_tool_fallbacks = 0usize;
167        let mut native_text_tool_fallback_rejections = 0usize;
168        let mut empty_completion_retries = 0usize;
169        let mut total_input_tokens = 0i64;
170        let mut total_output_tokens = 0i64;
171        let mut total_llm_duration_ms = 0u64;
172        let mut total_tool_duration_ms = 0u64;
173        let mut tools_used: Vec<String> = Vec::new();
174        let mut status = "unknown".to_string();
175        let mut iterations = 0usize;
176        let mut total_duration_ms = 0u64;
177
178        for event in events.iter() {
179            match event {
180                AgentTraceEvent::LlmCall {
181                    input_tokens,
182                    output_tokens,
183                    duration_ms,
184                    ..
185                } => {
186                    llm_calls += 1;
187                    total_input_tokens += input_tokens;
188                    total_output_tokens += output_tokens;
189                    total_llm_duration_ms += duration_ms;
190                }
191                AgentTraceEvent::ToolExecution {
192                    tool_name,
193                    duration_ms,
194                    ..
195                } => {
196                    tool_executions += 1;
197                    total_tool_duration_ms += duration_ms;
198                    if !tools_used.contains(tool_name) {
199                        tools_used.push(tool_name.clone());
200                    }
201                }
202                AgentTraceEvent::ToolRejected { .. } => {
203                    tool_rejections += 1;
204                }
205                AgentTraceEvent::LoopIntervention { .. } => {
206                    interventions += 1;
207                }
208                AgentTraceEvent::ContextCompaction { .. } => {
209                    compactions += 1;
210                }
211                AgentTraceEvent::PhaseChange { .. } => {}
212                AgentTraceEvent::LoopComplete {
213                    status: s,
214                    iterations: i,
215                    total_duration_ms: d,
216                    ..
217                } => {
218                    status = s.clone();
219                    iterations = *i;
220                    total_duration_ms = *d;
221                }
222                AgentTraceEvent::SchemaRetry { .. } => {}
223                AgentTraceEvent::NativeToolFallback { accepted, .. } => {
224                    native_text_tool_fallbacks += 1;
225                    if !accepted {
226                        native_text_tool_fallback_rejections += 1;
227                    }
228                }
229                AgentTraceEvent::EmptyCompletionRetry { .. } => {
230                    empty_completion_retries += 1;
231                }
232            }
233        }
234
235        serde_json::json!({
236            "status": status,
237            "iterations": iterations,
238            "total_duration_ms": total_duration_ms,
239            "llm_calls": llm_calls,
240            "tool_executions": tool_executions,
241            "tool_rejections": tool_rejections,
242            "interventions": interventions,
243            "compactions": compactions,
244            "native_text_tool_fallbacks": native_text_tool_fallbacks,
245            "native_text_tool_fallback_rejections": native_text_tool_fallback_rejections,
246            "empty_completion_retries": empty_completion_retries,
247            "total_input_tokens": total_input_tokens,
248            "total_output_tokens": total_output_tokens,
249            "total_llm_duration_ms": total_llm_duration_ms,
250            "total_tool_duration_ms": total_tool_duration_ms,
251            "tools_used": tools_used,
252        })
253    })
254}
255
256/// Reset agent trace state. Call between test runs.
257pub(crate) fn reset_agent_trace_state() {
258    AGENT_TRACE.with(|v| v.borrow_mut().clear());
259}