Skip to main content

aria_engine_core/
trace.rs

1use serde::{Deserialize, Serialize};
2
3use crate::action::Action;
4
5/// A single trace entry for JSONL export.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct TraceEntry {
8    /// Discrete step counter
9    pub t: u64,
10    /// Action taken
11    pub action: String,
12    /// Residual after step
13    pub res: f64,
14    /// Field energy after step
15    pub energy: f64,
16    /// Graph size |G| = |V| + |E|
17    pub graph_size: usize,
18    /// Conditioning
19    pub condition: String,
20}
21
22/// Full trace: a sequence of entries.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct Trace {
25    pub config_n_modes: usize,
26    pub config_latent_dim: usize,
27    pub config_eps: f64,
28    pub entries: Vec<TraceEntry>,
29}
30
31impl Trace {
32    pub fn new(n_modes: usize, latent_dim: usize, eps: f64) -> Self {
33        Trace {
34            config_n_modes: n_modes,
35            config_latent_dim: latent_dim,
36            config_eps: eps,
37            entries: Vec::new(),
38        }
39    }
40
41    pub fn push(
42        &mut self,
43        t: u64,
44        action: Action,
45        residual: f64,
46        energy: f64,
47        graph_size: usize,
48        condition: &str,
49    ) {
50        self.entries.push(TraceEntry {
51            t,
52            action: action.symbol().to_string(),
53            res: residual,
54            energy,
55            graph_size,
56            condition: condition.to_string(),
57        });
58    }
59
60    /// Export as JSONL string.
61    pub fn to_jsonl(&self) -> String {
62        let mut out = String::new();
63        // Header line with config
64        out.push_str(
65            &serde_json::to_string(&serde_json::json!({
66                "type": "config",
67                "n_modes": self.config_n_modes,
68                "latent_dim": self.config_latent_dim,
69                "eps": self.config_eps,
70            }))
71            .unwrap(),
72        );
73        out.push('\n');
74        for entry in &self.entries {
75            out.push_str(&serde_json::to_string(entry).unwrap());
76            out.push('\n');
77        }
78        out
79    }
80
81    /// Action symbol sequence for trace pattern matching.
82    pub fn action_sequence(&self) -> String {
83        self.entries
84            .iter()
85            .map(|e| e.action.as_str())
86            .collect::<Vec<_>>()
87            .join("")
88    }
89}