use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use regex::Regex;
use serde_json::Value;
use super::storage::{StackFrameRecord, StackTraceRecord, StructuralStore, ToolTraceRecord};
use crate::error::Result;
use crate::types::TenantId;
pub struct TraceCapture;
impl TraceCapture {
#[allow(clippy::too_many_arguments)]
pub fn capture_tool_call(
tenant_id: TenantId,
tool_name: &str,
input: &Value,
output: Option<&Value>,
error: Option<&Value>,
session_id: Option<&str>,
context_tags: Vec<String>,
duration_ms: i64,
) -> ToolTraceRecord {
let timestamp_ms = Self::current_timestamp_ms();
let mut trace = ToolTraceRecord::new(tenant_id, tool_name, timestamp_ms);
trace.input_json = Some(serde_json::to_string(input).unwrap_or_default());
trace.output_json = output.map(|v| serde_json::to_string(v).unwrap_or_default());
trace.error_json = error.map(|v| serde_json::to_string(v).unwrap_or_default());
trace.session_id = session_id.map(String::from);
trace.context_tags = context_tags;
trace.duration_ms = Some(duration_ms);
trace
}
pub fn current_timestamp_ms() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
}
#[derive(Debug, Clone, Default)]
pub struct ParsedFrame {
pub function_name: Option<String>,
pub file_path: Option<String>,
pub line_number: Option<u32>,
pub col_number: Option<u32>,
}
pub struct StackTraceParser;
impl StackTraceParser {
pub fn parse_rust_backtrace(trace: &str) -> (String, Vec<ParsedFrame>) {
let mut frames = Vec::new();
let mut error_signature = String::new();
if let Some(first_line) = trace.lines().next() {
if first_line.contains("panicked at") {
if let Some(start) = first_line.find("panicked at '") {
if let Some(end) = first_line[start + 13..].find('\'') {
error_signature =
format!("panic: {}", &first_line[start + 13..start + 13 + end]);
}
}
} else if first_line.starts_with("Error:") || first_line.starts_with("error[") {
error_signature = first_line.to_string();
}
}
if error_signature.is_empty() {
error_signature = "rust_panic".to_string();
}
let frame_num_re = Regex::new(r"^\s*(\d+):\s*(.+)$").unwrap();
let at_re = Regex::new(r"^\s+at\s+(.+?):(\d+):(\d+)$").unwrap();
let at_re_no_col = Regex::new(r"^\s+at\s+(.+?):(\d+)$").unwrap();
let lines: Vec<&str> = trace.lines().collect();
let mut i = 0;
while i < lines.len() {
if let Some(caps) = frame_num_re.captures(lines[i]) {
let function_name = caps.get(2).map(|m| m.as_str().trim().to_string());
let mut frame = ParsedFrame {
function_name,
..Default::default()
};
if i + 1 < lines.len() {
if let Some(at_caps) = at_re.captures(lines[i + 1]) {
frame.file_path = at_caps.get(1).map(|m| m.as_str().to_string());
frame.line_number = at_caps.get(2).and_then(|m| m.as_str().parse().ok());
frame.col_number = at_caps.get(3).and_then(|m| m.as_str().parse().ok());
i += 1;
} else if let Some(at_caps) = at_re_no_col.captures(lines[i + 1]) {
frame.file_path = at_caps.get(1).map(|m| m.as_str().to_string());
frame.line_number = at_caps.get(2).and_then(|m| m.as_str().parse().ok());
i += 1;
}
}
frames.push(frame);
}
i += 1;
}
(error_signature, frames)
}
pub fn parse_python_traceback(trace: &str) -> (String, Vec<ParsedFrame>) {
let mut frames = Vec::new();
let mut error_signature = String::new();
let file_re = Regex::new(r#"^\s*File\s+"([^"]+)",\s+line\s+(\d+),\s+in\s+(\S+)"#).unwrap();
for line in trace.lines() {
if let Some(caps) = file_re.captures(line) {
let frame = ParsedFrame {
file_path: caps.get(1).map(|m| m.as_str().to_string()),
line_number: caps.get(2).and_then(|m| m.as_str().parse().ok()),
function_name: caps.get(3).map(|m| m.as_str().to_string()),
col_number: None,
};
frames.push(frame);
}
}
for line in trace.lines().rev() {
let trimmed = line.trim();
if !trimmed.is_empty()
&& !trimmed.starts_with("File ")
&& !trimmed.starts_with("Traceback")
{
if let Some(colon_pos) = trimmed.find(':') {
let error_type = &trimmed[..colon_pos];
if !error_type.contains('/')
&& !error_type.contains('\\')
&& error_type.chars().all(|c| c.is_alphanumeric() || c == '_')
{
error_signature = trimmed.to_string();
break;
}
}
}
}
if error_signature.is_empty() {
error_signature = "python_exception".to_string();
}
frames.reverse();
(error_signature, frames)
}
pub fn parse_javascript_stack(trace: &str) -> (String, Vec<ParsedFrame>) {
let mut frames = Vec::new();
let mut error_signature = String::new();
let at_re = Regex::new(r"^\s+at\s+(?:(.+?)\s+\()?([^:]+):(\d+):(\d+)\)?$").unwrap();
let at_simple_re = Regex::new(r"^\s+at\s+(?:(.+?)\s+\()?([^:]+):(\d+)\)?$").unwrap();
for (i, line) in trace.lines().enumerate() {
if i == 0 {
error_signature = line.trim().to_string();
continue;
}
if let Some(caps) = at_re.captures(line) {
let frame = ParsedFrame {
function_name: caps.get(1).map(|m| m.as_str().to_string()),
file_path: caps.get(2).map(|m| m.as_str().to_string()),
line_number: caps.get(3).and_then(|m| m.as_str().parse().ok()),
col_number: caps.get(4).and_then(|m| m.as_str().parse().ok()),
};
frames.push(frame);
} else if let Some(caps) = at_simple_re.captures(line) {
let frame = ParsedFrame {
function_name: caps.get(1).map(|m| m.as_str().to_string()),
file_path: caps.get(2).map(|m| m.as_str().to_string()),
line_number: caps.get(3).and_then(|m| m.as_str().parse().ok()),
col_number: None,
};
frames.push(frame);
}
}
if error_signature.is_empty() {
error_signature = "javascript_error".to_string();
}
(error_signature, frames)
}
pub fn parse_generic(trace: &str) -> (String, Vec<ParsedFrame>) {
let mut frames = Vec::new();
let file_line_re = Regex::new(r"([^\s:]+\.[a-zA-Z]+):(\d+)(?::(\d+))?").unwrap();
for caps in file_line_re.captures_iter(trace) {
let frame = ParsedFrame {
file_path: caps.get(1).map(|m| m.as_str().to_string()),
line_number: caps.get(2).and_then(|m| m.as_str().parse().ok()),
col_number: caps.get(3).and_then(|m| m.as_str().parse().ok()),
function_name: None,
};
frames.push(frame);
}
let error_signature = trace
.lines()
.next()
.map(|l| l.trim().to_string())
.unwrap_or_else(|| "unknown_error".to_string());
(error_signature, frames)
}
pub fn auto_detect_and_parse(trace: &str) -> (String, Vec<ParsedFrame>) {
if trace.contains("Traceback (most recent call last)") || trace.contains("File \"") {
return Self::parse_python_traceback(trace);
}
if trace.lines().any(|l| l.trim().starts_with("at ")) {
return Self::parse_javascript_stack(trace);
}
if trace.contains("panicked at") || trace.contains("stack backtrace:") {
return Self::parse_rust_backtrace(trace);
}
Self::parse_generic(trace)
}
}
pub fn normalize_error_signature(error: &str) -> String {
let mut normalized = error.to_string();
let hex_re = Regex::new(r"0x[0-9a-fA-F]+").unwrap();
normalized = hex_re.replace_all(&normalized, "0x...").to_string();
let timestamp_re = Regex::new(r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}").unwrap();
normalized = timestamp_re
.replace_all(&normalized, "<timestamp>")
.to_string();
let uuid_re =
Regex::new(r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
.unwrap();
normalized = uuid_re.replace_all(&normalized, "<uuid>").to_string();
if normalized.len() > 200 {
normalized.truncate(200);
normalized.push_str("...");
}
normalized
}
pub trait TraceIndexer {
fn index_tool_call(&self, trace: ToolTraceRecord) -> Result<i64>;
fn index_stack_trace(
&self,
raw_trace: &str,
tenant_id: &TenantId,
session_id: Option<&str>,
) -> Result<i64>;
}
pub struct DefaultTraceIndexer {
store: Arc<StructuralStore>,
}
impl DefaultTraceIndexer {
pub fn new(store: Arc<StructuralStore>) -> Self {
Self { store }
}
}
impl TraceIndexer for DefaultTraceIndexer {
fn index_tool_call(&self, trace: ToolTraceRecord) -> Result<i64> {
self.store.insert_tool_trace(&trace).map_err(Into::into)
}
fn index_stack_trace(
&self,
raw_trace: &str,
tenant_id: &TenantId,
session_id: Option<&str>,
) -> Result<i64> {
let timestamp_ms = TraceCapture::current_timestamp_ms();
let (error_sig, parsed_frames) = StackTraceParser::auto_detect_and_parse(raw_trace);
let normalized_sig = normalize_error_signature(&error_sig);
let mut trace_record = StackTraceRecord::new(
tenant_id.clone(),
timestamp_ms,
normalized_sig,
error_sig,
raw_trace,
);
trace_record.session_id = session_id.map(String::from);
let frame_records: Vec<StackFrameRecord> = parsed_frames
.into_iter()
.enumerate()
.map(|(idx, pf)| StackFrameRecord {
frame_id: None,
trace_id: 0, frame_idx: idx as u32,
file_path: pf.file_path,
function_name: pf.function_name,
line_number: pf.line_number,
col_number: pf.col_number,
context: None,
})
.collect();
self.store
.insert_stack_trace(&trace_record, &frame_records)
.map_err(Into::into)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_rust_backtrace() {
let trace = r#"thread 'main' panicked at 'index out of bounds', src/main.rs:10:5
stack backtrace:
0: std::panicking::begin_panic
at /rustc/abc123/library/std/src/panicking.rs:519:12
1: test::main
at ./src/main.rs:10:5
2: std::rt::lang_start
at /rustc/abc123/library/std/src/rt.rs:134:17
"#;
let (sig, frames) = StackTraceParser::parse_rust_backtrace(trace);
assert!(sig.contains("index out of bounds"));
assert_eq!(frames.len(), 3);
assert_eq!(
frames[0].function_name.as_deref(),
Some("std::panicking::begin_panic")
);
assert_eq!(frames[1].function_name.as_deref(), Some("test::main"));
assert_eq!(frames[1].line_number, Some(10));
}
#[test]
fn test_parse_python_traceback() {
let trace = r#"Traceback (most recent call last):
File "main.py", line 10, in main
do_something()
File "utils.py", line 5, in do_something
raise ValueError("invalid input")
ValueError: invalid input
"#;
let (sig, frames) = StackTraceParser::parse_python_traceback(trace);
assert_eq!(sig, "ValueError: invalid input");
assert_eq!(frames.len(), 2);
assert_eq!(frames[0].file_path.as_deref(), Some("utils.py"));
assert_eq!(frames[0].function_name.as_deref(), Some("do_something"));
assert_eq!(frames[0].line_number, Some(5));
assert_eq!(frames[1].file_path.as_deref(), Some("main.py"));
}
#[test]
fn test_parse_javascript_stack() {
let trace = r#"Error: Something went wrong
at processData (/app/src/handler.js:25:10)
at handleRequest (/app/src/server.js:50:5)
at Object.<anonymous> (/app/src/index.js:10:1)
"#;
let (sig, frames) = StackTraceParser::parse_javascript_stack(trace);
assert_eq!(sig, "Error: Something went wrong");
assert_eq!(frames.len(), 3);
assert_eq!(frames[0].function_name.as_deref(), Some("processData"));
assert_eq!(frames[0].file_path.as_deref(), Some("/app/src/handler.js"));
assert_eq!(frames[0].line_number, Some(25));
assert_eq!(frames[0].col_number, Some(10));
}
#[test]
fn test_auto_detect_python() {
let trace = r#"Traceback (most recent call last):
File "test.py", line 1, in module
KeyError: 'missing'
"#;
let (sig, frames) = StackTraceParser::auto_detect_and_parse(trace);
assert!(sig.contains("KeyError"));
assert!(!frames.is_empty());
}
#[test]
fn test_auto_detect_javascript() {
let trace = r#"TypeError: Cannot read property 'x' of undefined
at foo (/bar.js:1:1)
"#;
let (sig, frames) = StackTraceParser::auto_detect_and_parse(trace);
assert!(sig.contains("TypeError"));
assert!(!frames.is_empty());
}
#[test]
fn test_normalize_error_signature() {
let sig1 = "Error at 0x7f1234567890: something failed";
let normalized1 = normalize_error_signature(sig1);
assert!(normalized1.contains("0x..."));
assert!(!normalized1.contains("0x7f1234567890"));
let sig2 = "Error at 2024-01-15T10:30:00: timeout";
let normalized2 = normalize_error_signature(sig2);
assert!(normalized2.contains("<timestamp>"));
let sig3 = "Failed for ID: 550e8400-e29b-41d4-a716-446655440000";
let normalized3 = normalize_error_signature(sig3);
assert!(normalized3.contains("<uuid>"));
}
#[test]
fn test_capture_tool_call() {
let tenant = TenantId::new("test").unwrap();
let input = serde_json::json!({"path": "/test.rs"});
let output = serde_json::json!({"content": "fn main() {}"});
let trace = TraceCapture::capture_tool_call(
tenant,
"read_file",
&input,
Some(&output),
None,
Some("session_1"),
vec!["rust".to_string()],
100,
);
assert_eq!(trace.tool_name, "read_file");
assert!(trace.input_json.is_some());
assert!(trace.output_json.is_some());
assert!(trace.error_json.is_none());
assert_eq!(trace.session_id.as_deref(), Some("session_1"));
assert_eq!(trace.context_tags, vec!["rust"]);
assert_eq!(trace.duration_ms, Some(100));
}
}