Skip to main content

spice_framework/
trace.rs

1use crate::agent::AgentOutput;
2use crate::error::SpiceError;
3use serde::{Deserialize, Serialize};
4use std::path::Path;
5
6/// A trace record for a single test execution.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Trace {
9    pub test_id: String,
10    pub user_message: String,
11    pub output: AgentOutput,
12    pub timestamp: chrono::DateTime<chrono::Utc>,
13}
14
15impl Trace {
16    pub fn new(test_id: String, user_message: String, output: AgentOutput) -> Self {
17        Self {
18            test_id,
19            user_message,
20            output,
21            timestamp: chrono::Utc::now(),
22        }
23    }
24
25    /// Save trace to a JSON file.
26    pub fn save_to_file(&self, path: &Path) -> Result<(), SpiceError> {
27        if let Some(parent) = path.parent() {
28            std::fs::create_dir_all(parent)?;
29        }
30        let json = serde_json::to_string_pretty(self)?;
31        std::fs::write(path, json)?;
32        Ok(())
33    }
34}