feat(trace): trace format v1 with TraceCollector on the compiled engine

Adds trace format v1 to the compiled engine, recorded by explicit trace
recorders and written natively. The interpreted engine keeps the legacy
trace.

- EvaluationOptions.trace is a TraceFormat (off, v1, legacy). Legacy
  traces are produced directly from the same recording.
- zen_engine::trace exposes convert, restore and inspect for
  stored documents, with redaction and truncation markers.
- The trace header takes evaluatedAt, timezone and seed from the
  evaluation context, so an evaluation replays from its own trace.
- Batch and columnar evaluations record events as columns and write one
  document per row.
- Bindings accept the trace format (true, 'v1', 'legacy'); the Node
  binding ships TypeScript types for the v1 document.
- Golden, fixture and damaged-document tests cover byte identity with the
  legacy trace, restore, interop with the reference fixtures and
  malformed input.
This commit is contained in:
Stefan
2026-11-17 18:57:34 +01:00
parent cd6a3f3adb
commit ae431abe37
210 changed files with 30491 additions and 1140 deletions
+4
View File
@@ -135,6 +135,9 @@ impl PortableResponse {
Some(EvaluationTrace::Policy(policy)) => {
PortableTrace::Json(serde_json::to_value(&policy).unwrap_or_default())
}
Some(EvaluationTrace::V1(document)) => {
PortableTrace::Arena(Self::add(&mut nodes, &mut memo, &document.to_variable()))
}
};
Self {
@@ -285,6 +288,7 @@ pub fn response_to_py(py: Python<'_>, response: DecisionGraphResponse) -> PyResu
let value = serde_json::to_value(&policy).unwrap_or_default();
value_to_object(py, &value)?
}
EvaluationTrace::V1(document) => converter.convert(&document.to_variable())?,
};
dict.set_item("trace", trace_obj)?;
+18 -5
View File
@@ -1,3 +1,4 @@
use std::str::FromStr;
use std::sync::Arc;
use crate::content::PyZenDecisionContentJson;
@@ -16,6 +17,7 @@ use pyo3_async_runtimes::{tokio, TaskLocals};
use pythonize::{depythonize, pythonize};
use serde_json::Value;
use zen_engine::loader::{DynamicLoader, LoaderConfig};
use zen_engine::trace::TraceFormat;
use zen_engine::{DecisionEngine, EvaluationOptions};
#[pyclass]
@@ -26,7 +28,7 @@ pub struct PyZenEngine {
#[derive(Default)]
pub struct PyZenEvaluateOptions {
pub trace: Option<bool>,
pub trace: Option<TraceFormat>,
pub max_depth: Option<u8>,
pub now: Option<zen_engine::context::Instant>,
pub timezone: Option<zen_engine::context::Timezone>,
@@ -105,10 +107,21 @@ impl<'py> FromPyObject<'py> for PyZenEvaluateOptions {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let dict = ob.downcast::<PyDict>()?;
let trace = dict
.get_item("trace")?
.map(|v| v.extract::<bool>())
.transpose()?;
let trace = match dict.get_item("trace")? {
None => None,
Some(v) if v.is_none() => None,
Some(v) => match v.extract::<bool>() {
Ok(flag) => Some(TraceFormat::from(flag)),
Err(_) => {
let name = v.extract::<String>()?;
Some(TraceFormat::from_str(&name).map_err(|_| {
pyo3::exceptions::PyValueError::new_err(format!(
"invalid trace format '{name}', expected true, false, 'v1' or 'legacy'"
))
})?)
}
},
};
let max_depth = dict
.get_item("max_depth")?