use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::sync::{Arc, Mutex};
use vitrify_core::model::UtcTime;
use crate::out::{J, diag};
#[derive(Debug, Clone, PartialEq)]
pub struct RunEvent {
pub at: UtcTime,
pub kind: String,
pub unit: Option<String>,
pub claim_seq: Option<i64>,
pub detail: String,
pub fields: Vec<(String, J)>,
}
const COMMON: [&str; 6] = ["at", "event", "unit", "claim_seq", "detail", "replay"];
impl RunEvent {
pub fn new(
at: UtcTime,
kind: &str,
unit: Option<&str>,
claim_seq: Option<i64>,
detail: impl Into<String>,
) -> RunEvent {
RunEvent {
at,
kind: kind.to_string(),
unit: unit.map(str::to_string),
claim_seq,
detail: detail.into(),
fields: Vec::new(),
}
}
pub fn with(mut self, key: &str, value: J) -> RunEvent {
self.fields.push((key.to_string(), value));
self
}
pub fn field(&self, key: &str) -> Option<&J> {
self.fields.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
pub fn to_json(&self, replay: Option<bool>) -> J {
let mut pairs = vec![
("at".to_string(), J::s(self.at.format())),
("event".to_string(), J::s(self.kind.clone())),
("unit".to_string(), J::opt_str(self.unit.clone())),
(
"claim_seq".to_string(),
self.claim_seq.map(J::Int).unwrap_or(J::Null),
),
("detail".to_string(), J::s(self.detail.clone())),
];
let mut fields = self.fields.clone();
fields.sort_by(|a, b| a.0.cmp(&b.0));
pairs.extend(fields);
if let Some(replay) = replay {
pairs.push(("replay".to_string(), J::Bool(replay)));
}
J::Obj(pairs)
}
pub fn from_json(v: &serde_json::Value) -> Option<RunEvent> {
let obj = v.as_object()?;
let at = UtcTime::parse(obj.get("at")?.as_str()?)?;
let kind = obj.get("event")?.as_str()?.to_string();
let mut fields: Vec<(String, J)> = obj
.iter()
.filter(|(k, _)| !COMMON.contains(&k.as_str()))
.map(|(k, v)| (k.clone(), J::from_value(v)))
.collect();
fields.sort_by(|a, b| a.0.cmp(&b.0));
Some(RunEvent {
at,
kind,
unit: obj.get("unit").and_then(|u| u.as_str()).map(str::to_string),
claim_seq: obj.get("claim_seq").and_then(|c| c.as_i64()),
detail: obj
.get("detail")
.and_then(|d| d.as_str())
.unwrap_or_default()
.to_string(),
fields,
})
}
}
#[derive(Clone)]
pub struct EventLog(Arc<Mutex<Option<File>>>);
impl EventLog {
pub fn open(logs_dir: &Path) -> EventLog {
let path = logs_dir.join("events.jsonl");
match std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)
{
Ok(file) => EventLog(Arc::new(Mutex::new(Some(file)))),
Err(e) => {
diag(&format!("warning: events.jsonl not written: {e}"));
EventLog::off()
}
}
}
pub fn off() -> EventLog {
EventLog(Arc::new(Mutex::new(None)))
}
pub fn write(&self, e: &RunEvent) {
let line = format!("{}\n", e.to_json(None).dumps_default());
let mut file = self.0.lock().unwrap_or_else(|p| p.into_inner());
if let Some(f) = file.as_mut() {
let _ = f.write_all(line.as_bytes());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> RunEvent {
RunEvent::new(
UtcTime::parse("2026-11-16T14:08:01Z").unwrap(),
"outcome",
Some("U-004"),
Some(1),
"rejected-gate — failing gate: `pytest -q`",
)
.with("outcome", J::s("rejected-gate"))
.with("duration_s", J::Int(660))
}
#[test]
fn fields_follow_the_common_keys_in_key_order() {
let line = sample().to_json(Some(false)).dumps_default();
assert_eq!(
line,
r#"{"at": "2026-11-16T14:08:01Z", "event": "outcome", "unit": "U-004", "claim_seq": 1, "detail": "rejected-gate \u2014 failing gate: `pytest -q`", "duration_s": 660, "outcome": "rejected-gate", "replay": false}"#
);
assert!(!sample().to_json(None).dumps_default().contains("replay"));
}
#[test]
fn an_event_survives_a_round_trip_field_order_included() {
let mut e = sample();
e.fields.sort_by(|a, b| a.0.cmp(&b.0));
let back: serde_json::Value =
serde_json::from_str(&e.to_json(None).dumps_default()).unwrap();
assert_eq!(RunEvent::from_json(&back), Some(e));
let run =
RunEvent::new(UtcTime(0), "run-ended", None, None, "").with("state", J::s("complete"));
let back: serde_json::Value =
serde_json::from_str(&run.to_json(Some(true)).dumps_default()).unwrap();
assert_eq!(RunEvent::from_json(&back), Some(run));
}
#[test]
fn a_line_that_is_not_an_event_is_skipped() {
for text in [
r#"{"event": "gate"}"#,
r#"{"at": "2026-11-16T14:08:01Z"}"#,
"[]",
] {
let v: serde_json::Value = serde_json::from_str(text).unwrap();
assert_eq!(RunEvent::from_json(&v), None, "{text}");
}
}
#[test]
fn the_log_appends_one_line_per_event() {
let dir = tempfile::tempdir().unwrap();
let log = EventLog::open(dir.path());
log.write(&sample());
log.clone().write(&sample());
EventLog::off().write(&sample());
let text = std::fs::read_to_string(dir.path().join("events.jsonl")).unwrap();
assert_eq!(text.lines().count(), 2);
assert!(text.ends_with("}\n"));
}
}