nmbrs_runtime/checkpoint/events.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! SRD-44a — Checkpoint event taxonomy.
5//!
6//! `CheckpointData` is the on-disk record type for the
7//! append-only `checkpoint.jsonl` event log. Every state-
8//! changing observation is one variant; serde tags each line
9//! with a `"type"` discriminator so a reader can drop
10//! unrecognised types without poisoning the rest of the
11//! stream.
12//!
13//! See SRD-44a §"Event taxonomy" for the schema.
14
15use serde::{Deserialize, Serialize};
16
17use super::identity::PhaseIdentity;
18use super::storage::OpCounts;
19
20/// One record in the JSONL event log. Tagged on `type` so the
21/// stream is forward-extensible: adding a new variant is a
22/// no-op for older readers (they ignore unknown types per
23/// SRD-44a §"Reader behaviour").
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(tag = "type", rename_all = "snake_case")]
26pub enum CheckpointData {
27 /// First line of every fresh invocation's section. Resume
28 /// increments `invocation` and writes a fresh `session_start`
29 /// to **continue** the same JSONL — no separate file rotation.
30 SessionStart {
31 /// RFC 3339 UTC timestamp of when the event was written.
32 at: String,
33 /// Format version. `1` until we ship a `2`.
34 version: u32,
35 /// Session id (matches `logs/<session>/`).
36 session: String,
37 /// RFC 3339 first-invocation start.
38 started_at: String,
39 /// 1-based invocation counter.
40 invocation: u32,
41 },
42
43 /// Written when the workload completes. Optional — its
44 /// absence means the invocation was interrupted, which the
45 /// resume planner uses to distinguish clean exit from crash.
46 SessionEnd {
47 at: String,
48 /// `"completed"` / `"errored"` / `"stopped"`.
49 outcome: String,
50 /// Top-level error message when `outcome == "errored"`.
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 error: Option<String>,
53 },
54
55 /// One per phase per invocation, written during pre-map.
56 /// Carries identity and eligibility flags so the reader can
57 /// build the planned phase index without reading the
58 /// workload YAML.
59 PhaseDeclared {
60 at: String,
61 identity: PhaseIdentity,
62 skip_eligible: bool,
63 },
64
65 /// Mark a declared phase as Running.
66 PhaseStarted { at: String, identity: PhaseIdentity },
67
68 /// Periodic op-count + cursor-state update for a Running
69 /// phase. Replaces the in-place mutation of
70 /// `PhaseEntry::op_counts` and `cursor_state` from SRD-44.
71 /// The reader keeps **only the most recent** progress
72 /// record per (identity, current invocation) when folding.
73 PhaseProgress {
74 at: String,
75 identity: PhaseIdentity,
76 op_counts: OpCounts,
77 /// Tier-2 opaque snapshot from the active source factory.
78 #[serde(default, skip_serializing_if = "Option::is_none")]
79 cursor_state: Option<serde_json::Value>,
80 },
81
82 /// Phase reached the Completed state. Per SRD-44 §"Status
83 /// Completed is load-bearing", this is the call that makes
84 /// the phase eligible to skip on a future resume.
85 PhaseCompleted {
86 at: String,
87 identity: PhaseIdentity,
88 duration_secs: f64,
89 op_counts: OpCounts,
90 },
91
92 /// Phase failed terminally. The error message is preserved
93 /// for resume diagnostics.
94 PhaseFailed {
95 at: String,
96 identity: PhaseIdentity,
97 error: String,
98 #[serde(default, skip_serializing_if = "Option::is_none")]
99 op_counts: Option<OpCounts>,
100 },
101
102 /// Update on a previously-declared phase: bind the
103 /// program-canonical hash. Called once per phase the first
104 /// time it compiles. Folds into the existing entry's
105 /// identity so a future resume can detect program drift via
106 /// `PhaseIdentity::matches_full`.
107 PhaseHash {
108 at: String,
109 identity: PhaseIdentity,
110 /// 32-byte program hash, hex-encoded for human
111 /// readability of the on-disk records. SRD-107: the
112 /// BASE hash (chain below the session node + config
113 /// digest); param values ride in `params_consumed`.
114 hash_hex: String,
115 /// SRD-107 — the consumed-params map as canonical JSON
116 /// (`{"name":"<value sha256 hex>",…}`). Absent on
117 /// records written before the field existed.
118 #[serde(default, skip_serializing_if = "Option::is_none")]
119 params_consumed: Option<String>,
120 },
121
122 /// Reserved for SRD-44a Push 3. The reader already
123 /// understands these variants so the writer can emit them
124 /// in a future patch without a schema bump.
125 ScopeEnter {
126 at: String,
127 kind: String,
128 coords: std::collections::BTreeMap<String, serde_json::Value>,
129 path: Vec<std::collections::BTreeMap<String, serde_json::Value>>,
130 },
131
132 /// Reserved for SRD-44a Push 3.
133 ScopeExit {
134 at: String,
135 kind: String,
136 coords: std::collections::BTreeMap<String, serde_json::Value>,
137 path: Vec<std::collections::BTreeMap<String, serde_json::Value>>,
138 outcome: String,
139 },
140
141 /// An adapter's own resumable position (SRD-117 §9.6): where a
142 /// shared resource of `adapter`, known to it by `key`, had got to.
143 /// The payload is the adapter's to define and read back. A resumed
144 /// session hands the latest one per `(adapter, key)` back to the
145 /// adapter; the fold ignores it.
146 AdapterPosition {
147 at: String,
148 adapter: String,
149 key: String,
150 position: serde_json::Value,
151 },
152}
153
154impl CheckpointData {
155 /// RFC 3339 timestamp this event was tagged with. All
156 /// variants carry one; the helper avoids a match in the
157 /// fold loop.
158 pub fn at(&self) -> &str {
159 match self {
160 Self::SessionStart { at, .. }
161 | Self::SessionEnd { at, .. }
162 | Self::PhaseDeclared { at, .. }
163 | Self::PhaseStarted { at, .. }
164 | Self::PhaseProgress { at, .. }
165 | Self::PhaseCompleted { at, .. }
166 | Self::PhaseFailed { at, .. }
167 | Self::PhaseHash { at, .. }
168 | Self::ScopeEnter { at, .. }
169 | Self::ScopeExit { at, .. }
170 | Self::AdapterPosition { at, .. } => at,
171 }
172 }
173
174 /// The lifecycle kind-tag this durable record corresponds
175 /// to, tying an on-disk [`CheckpointData`] record back to
176 /// the in-memory [`crate::lifecycle::EventType`] that the
177 /// readout binder fires on. `None` for records that have no
178 /// lifecycle fire point — structural / metadata records
179 /// (a pre-map `PhaseDeclared` declaration, a `PhaseHash`
180 /// binding) are written to the log but never fire a readout
181 /// slot, so they have no kind-tag.
182 pub fn event_type(&self) -> Option<crate::lifecycle::EventType> {
183 use crate::lifecycle::EventType;
184 match self {
185 Self::SessionStart { .. } => Some(EventType::SessionStart),
186 Self::SessionEnd { .. } => Some(EventType::SessionEnd),
187 // Pre-map declaration record — no lifecycle fire point.
188 Self::PhaseDeclared { .. } => None,
189 Self::PhaseStarted { .. } => Some(EventType::PhaseStart),
190 Self::PhaseProgress { .. } => Some(EventType::Update),
191 // Both terminal states fire the phase-end slot
192 // (`phase_outcome` / `error_readout` bind there).
193 Self::PhaseCompleted { .. } | Self::PhaseFailed { .. } => Some(EventType::PhaseEnd),
194 // Metadata update — binds the program hash, no fire.
195 Self::PhaseHash { .. } => None,
196 Self::ScopeEnter { .. } => Some(EventType::ScopeStart),
197 Self::ScopeExit { .. } => Some(EventType::ScopeEnd),
198 // An adapter's own bookkeeping — no lifecycle fire point.
199 Self::AdapterPosition { .. } => None,
200 }
201 }
202}
203
204/// Hex-encode a 32-byte program hash for storage in
205/// `PhaseHash` events. Lowercase, no separators.
206pub fn hash_to_hex(bytes: &[u8; 32]) -> String {
207 let mut s = String::with_capacity(64);
208 for b in bytes {
209 s.push_str(&format!("{b:02x}"));
210 }
211 s
212}
213
214/// Decode a 32-byte program hash from its hex form. Returns
215/// `None` on any malformed input — the reader logs a Warn and
216/// continues folding.
217pub fn hex_to_hash(hex: &str) -> Option<[u8; 32]> {
218 if hex.len() != 64 {
219 return None;
220 }
221 let mut out = [0u8; 32];
222 for (i, byte) in out.iter_mut().enumerate() {
223 let pair = &hex[i * 2..i * 2 + 2];
224 *byte = u8::from_str_radix(pair, 16).ok()?;
225 }
226 Some(out)
227}
228
229#[cfg(test)]
230mod tests {
231 use super::*;
232
233 #[test]
234 fn hash_round_trip() {
235 let h = [0xabu8; 32];
236 let hex = hash_to_hex(&h);
237 assert_eq!(hex.len(), 64);
238 assert_eq!(hex_to_hash(&hex), Some(h));
239 }
240
241 #[test]
242 fn hex_to_hash_rejects_malformed() {
243 assert!(hex_to_hash("").is_none());
244 assert!(hex_to_hash("ab").is_none()); // wrong length
245 assert!(hex_to_hash(&"z".repeat(64)).is_none()); // non-hex
246 }
247
248 #[test]
249 fn session_start_round_trip_via_serde() {
250 let e = CheckpointData::SessionStart {
251 at: "2026-05-07T12:00:00Z".into(),
252 version: 1,
253 session: "test".into(),
254 started_at: "2026-05-07T12:00:00Z".into(),
255 invocation: 1,
256 };
257 let line = serde_json::to_string(&e).unwrap();
258 assert!(line.contains("\"type\":\"session_start\""), "line: {line}");
259 let parsed: CheckpointData = serde_json::from_str(&line).unwrap();
260 match parsed {
261 CheckpointData::SessionStart { invocation, .. } => {
262 assert_eq!(invocation, 1);
263 }
264 _ => panic!("expected SessionStart"),
265 }
266 }
267
268 #[test]
269 fn unknown_type_fails_to_parse() {
270 // Confirms serde rejects unknown discriminators —
271 // the reader catches the Err and logs Debug per the
272 // forward-compat policy. (The "ignore unknown types"
273 // semantics live in the reader, not in serde.)
274 let line = r#"{"type":"future_event","at":"2026-05-07T12:00:00Z"}"#;
275 let r: Result<CheckpointData, _> = serde_json::from_str(line);
276 assert!(r.is_err(), "unknown type must error at parse");
277 }
278}