diff --git a/services/flysim/crates/flysim/src/frame.rs b/services/flysim/crates/flysim/src/frame.rs
index d916a56..0eb5cee 100644
--- a/services/flysim/crates/flysim/src/frame.rs
+++ b/services/flysim/crates/flysim/src/frame.rs
@@ -54,6 +54,38 @@ pub struct Parts<'a> {
pub macros: Option<&'a mut MacroLayer>,
}
+/// The ledgers after a boundary as one canonical string: the adapter's export (its lifetime
+/// ledgers), the ratchet's state and whether it holds a snapshot, and the macro layer's observable
+/// state -- scene, bound channels, running macro, outcome counts, "nearer the objective".
+///
+/// This is the one definition both runtimes use: `FLY_TRACE_LEDGERS` digests it here, and the
+/// session runtime's task (`fly-legacy-session::task::ledgers_of`) computes it from its own parts,
+/// which is how the shadow run holds the executor's ledgers -- which no checkpoint carries -- to
+/// the live loop's (SHADOW-01). Reading it changes nothing.
+pub fn ledgers_string(
+ adapter: &dyn GameAdapter,
+ ratchet: &Ratchet,
+ macros: Option<&MacroLayer>,
+) -> String {
+ let executor = match macros {
+ Some(layer) => serde_json::json!({
+ "scene": layer.scene_name(),
+ "bound": layer.bound_channels(),
+ "running": layer.running(),
+ "counts": format!("{:?}", layer.counts()),
+ "nearer": layer.nearer_the_objective(),
+ }),
+ None => serde_json::Value::Null,
+ };
+ serde_json::json!({
+ "adapter": adapter.export_state(),
+ "ratchet": serde_json::to_value(ratchet.state).expect("a ratchet state serializes"),
+ "slot": ratchet.snapshot.is_some(),
+ "executor": executor,
+ })
+ .to_string()
+}
+
/// Where a host may look in, or time a phase. Every method defaults to nothing.
///
/// The stream's loop uses [`FrameObserver::after`] for its per-phase profile and nothing else. A
@@ -654,6 +686,15 @@ impl LegacyFrame {
} else {
None
};
+ if let Some(trace) = self.trace.as_mut()
+ && trace.wants_ledgers()
+ {
+ trace.ledgers(&ledgers_string(
+ &*parts.adapter,
+ parts.ratchet,
+ parts.macros.as_deref(),
+ ));
+ }
Ok(Boundary {
captured: saved,
rollback,
diff --git a/services/flysim/crates/flysim/src/trace.rs b/services/flysim/crates/flysim/src/trace.rs
index 3cd9abf..1455381 100644
--- a/services/flysim/crates/flysim/src/trace.rs
+++ b/services/flysim/crates/flysim/src/trace.rs
@@ -29,6 +29,21 @@
//! Wall time is never written, so a run with the periodic checkpoint intervals pushed out of the
//! way produces the same file twice. Off by default; when off, nothing here is constructed and the
//! loop pays one `Option` test per hook.
+//!
+//! **The shadow run's input (SHADOW-01).** The trace is also what the shadow run
+//! (`fly-legacy-session::shadow`) follows beside the live service, so three switches serve it.
+//! None changes a line's behaviour fields or anything the fly does:
+//!
+//! - `FLY_TRACE_DIR=
` writes one file per process, `trace--.jsonl`,
+//! instead of the one `FLY_TRACE` path a restart would truncate. File names sort in start order;
+//! a process's file ends where the next one begins.
+//! - `FLY_TRACE_MAX_BYTES` caps one file (default 4 GiB in directory mode, none otherwise). At the
+//! cap the recorder writes `{"truncated":true}` and stops, so a shadow that is not consuming the
+//! files cannot fill the disk; the loop runs on untouched.
+//! - `FLY_TRACE_LEDGERS=` adds `ledgersDigest` to every transition whose `step` is a multiple
+//! of `n`: the SHA-256 of [`crate::frame::ledgers_string`] after the boundary (adapter export,
+//! ratchet, slot, executor scene/bound/running/counts/nearer). `1` is every transition. The
+//! header then carries `ledgersEvery`.
use std::fs::File;
use std::io::{BufWriter, Write};
@@ -48,6 +63,24 @@ pub const FORMAT: &str = "flysim-legacy-frame-trace-v1";
/// The environment variable that turns the trace on.
pub const ENV: &str = "FLY_TRACE";
+/// One file per process in this directory (SHADOW-01).
+pub const DIR_ENV: &str = "FLY_TRACE_DIR";
+
+/// The per-file byte cap.
+pub const MAX_BYTES_ENV: &str = "FLY_TRACE_MAX_BYTES";
+
+/// The ledger digest's period in transitions.
+pub const LEDGERS_ENV: &str = "FLY_TRACE_LEDGERS";
+
+/// The default per-file cap in directory mode: about 23 hours of stream.
+pub const DEFAULT_DIR_MAX_BYTES: u64 = 4 << 30;
+
+/// The per-process file name in [`DIR_ENV`] mode: start wall time (13 digits, so names sort in
+/// start order) and pid.
+pub fn dir_file_name(wall_ms: u64, pid: u32) -> String {
+ format!("trace-{wall_ms:013}-{pid}.jsonl")
+}
+
/// The ratchet's one slot, as the legacy composition declares it (legacy-gameboy-v1 section 9).
pub const SLOT: &str = "best";
@@ -122,6 +155,7 @@ struct Record {
rank: u32,
boundary_actions: Vec,
captures: Vec,
+ ledgers: Option,
}
/// The recorder. The loop calls it at fixed points of the frame order; it holds the transition
@@ -137,21 +171,78 @@ pub struct FrameTrace {
admissions: Vec,
/// Brain clock before this transition's ticks, the lower edge of its spike window.
ms_before: f64,
+ /// Bytes written so far, and the cap (`FLY_TRACE_MAX_BYTES`).
+ written: u64,
+ max_bytes: Option,
+ /// Set once the cap was reached: nothing more is written.
+ stopped: bool,
+ /// `FLY_TRACE_LEDGERS`: a ledger digest every this many transitions.
+ ledgers_every: Option,
}
impl FrameTrace {
/// `FLY_TRACE`, if it is set and non-empty. A path that cannot be created is an error rather
/// than a silent run without the trace that was asked for.
+ ///
+ /// `FLY_TRACE_DIR` (one file per process) wins over `FLY_TRACE`; `FLY_TRACE_MAX_BYTES` and
+ /// `FLY_TRACE_LEDGERS` apply to either.
pub fn from_env() -> std::io::Result