flysim: trace switches for the shadow run (SHADOW-01)

FLY_TRACE_DIR writes one FLY_TRACE file per process (a restart truncated
the single path), FLY_TRACE_MAX_BYTES caps a file (4 GiB in directory
mode: a trace nobody consumes cannot fill the disk), FLY_TRACE_LEDGERS=<n>
adds ledgersDigest every n transitions. frame::ledgers_string is now the
one definition of the ledger string both runtimes digest. Recording only:
no behaviour field and nothing the fly does changes; compatibility string
unchanged.
This commit is contained in:
acamilo 2026-09-29 21:19:40 +00:00
parent eaf588d2e2
commit 105f526200
2 changed files with 240 additions and 6 deletions

View file

@ -54,6 +54,38 @@ pub struct Parts<'a> {
pub macros: Option<&'a mut MacroLayer>, 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. /// 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 /// The stream's loop uses [`FrameObserver::after`] for its per-phase profile and nothing else. A
@ -654,6 +686,15 @@ impl LegacyFrame {
} else { } else {
None 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 { Ok(Boundary {
captured: saved, captured: saved,
rollback, rollback,

View file

@ -29,6 +29,21 @@
//! Wall time is never written, so a run with the periodic checkpoint intervals pushed out of the //! 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 //! way produces the same file twice. Off by default; when off, nothing here is constructed and the
//! loop pays one `Option` test per hook. //! 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=<dir>` writes one file per process, `trace-<wall ms, 13 digits>-<pid>.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=<n>` 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::fs::File;
use std::io::{BufWriter, Write}; 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. /// The environment variable that turns the trace on.
pub const ENV: &str = "FLY_TRACE"; 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). /// The ratchet's one slot, as the legacy composition declares it (legacy-gameboy-v1 section 9).
pub const SLOT: &str = "best"; pub const SLOT: &str = "best";
@ -122,6 +155,7 @@ struct Record {
rank: u32, rank: u32,
boundary_actions: Vec<Value>, boundary_actions: Vec<Value>,
captures: Vec<Value>, captures: Vec<Value>,
ledgers: Option<String>,
} }
/// The recorder. The loop calls it at fixed points of the frame order; it holds the transition /// 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<Value>, admissions: Vec<Value>,
/// Brain clock before this transition's ticks, the lower edge of its spike window. /// Brain clock before this transition's ticks, the lower edge of its spike window.
ms_before: f64, ms_before: f64,
/// Bytes written so far, and the cap (`FLY_TRACE_MAX_BYTES`).
written: u64,
max_bytes: Option<u64>,
/// 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<u64>,
} }
impl FrameTrace { impl FrameTrace {
/// `FLY_TRACE`, if it is set and non-empty. A path that cannot be created is an error rather /// `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. /// 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<Option<Self>> { pub fn from_env() -> std::io::Result<Option<Self>> {
match std::env::var_os(ENV) { let var = |name: &str| std::env::var_os(name).filter(|value| !value.is_empty());
Some(path) if !path.is_empty() => Self::create(Path::new(&path)).map(Some), let number = |name: &str| -> std::io::Result<Option<u64>> {
_ => Ok(None), match var(name) {
None => Ok(None),
Some(value) => value
.to_str()
.and_then(|v| v.trim().parse::<u64>().ok())
.filter(|v| *v > 0)
.map(Some)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("{name} must be a positive integer"),
)
}),
}
};
let ledgers_every = number(LEDGERS_ENV)?;
let max_bytes = number(MAX_BYTES_ENV)?;
if let Some(dir) = var(DIR_ENV) {
let dir = Path::new(&dir);
std::fs::create_dir_all(dir)?;
let wall_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_millis() as u64);
let path = dir.join(dir_file_name(wall_ms, std::process::id()));
return Self::create_with(
&path,
max_bytes.or(Some(DEFAULT_DIR_MAX_BYTES)),
ledgers_every,
)
.map(Some);
}
match var(ENV) {
Some(path) => Self::create_with(Path::new(&path), max_bytes, ledgers_every).map(Some),
None => Ok(None),
} }
} }
pub fn create(path: &Path) -> std::io::Result<Self> { pub fn create(path: &Path) -> std::io::Result<Self> {
Self::create_with(path, None, None)
}
/// A trace with a byte cap and a ledger digest period (see the module notes).
pub fn create_with(
path: &Path,
max_bytes: Option<u64>,
ledgers_every: Option<u64>,
) -> std::io::Result<Self> {
let mut out = BufWriter::with_capacity(1 << 20, File::create(path)?); let mut out = BufWriter::with_capacity(1 << 20, File::create(path)?);
writeln!(out, "{}", json!({ "format": FORMAT }))?; let header = match ledgers_every {
Some(every) => json!({ "format": FORMAT, "ledgersEvery": every }),
None => json!({ "format": FORMAT }),
};
let header = header.to_string();
writeln!(out, "{header}")?;
Ok(Self { Ok(Self {
out, out,
initial: Vec::new(), initial: Vec::new(),
@ -159,13 +250,50 @@ impl FrameTrace {
open: None, open: None,
admissions: Vec::new(), admissions: Vec::new(),
ms_before: 0.0, ms_before: 0.0,
written: header.len() as u64 + 1,
max_bytes,
stopped: false,
ledgers_every,
}) })
} }
fn write(&mut self, value: &Value) { fn write(&mut self, value: &Value) {
if let Err(error) = writeln!(self.out, "{value}") { if self.stopped {
return;
}
let line = value.to_string();
let len = line.len() as u64 + 1;
if let Some(cap) = self.max_bytes
&& self.written + len > cap
{
// The cap: one marker, then silence. The loop is unaffected.
self.stopped = true;
if let Err(error) = writeln!(self.out, "{}", json!({ "truncated": true })) {
tracing::warn!(%error, "could not write the frame trace"); tracing::warn!(%error, "could not write the frame trace");
} }
let _ = self.out.flush();
tracing::warn!(cap, "the frame trace reached its byte cap and stopped");
return;
}
self.written += len;
if let Err(error) = writeln!(self.out, "{line}") {
tracing::warn!(%error, "could not write the frame trace");
}
}
/// Whether the open transition takes a ledger digest (`FLY_TRACE_LEDGERS`).
pub fn wants_ledgers(&self) -> bool {
match (self.ledgers_every, self.open.as_ref()) {
(Some(every), Some(record)) => !self.stopped && record.step % every == 0,
_ => false,
}
}
/// The ledgers after the boundary, as [`crate::frame::ledgers_string`] writes them.
pub fn ledgers(&mut self, ledgers: &str) {
if let Some(record) = self.open.as_mut() {
record.ledgers = Some(sha256_hex(ledgers.as_bytes()));
}
} }
/// Sugar admitted at the top of the frame, before the brain ticks. /// Sugar admitted at the top of the frame, before the brain ticks.
@ -309,7 +437,7 @@ impl FrameTrace {
let Some(record) = self.open.take() else { let Some(record) = self.open.take() else {
return; return;
}; };
let line = json!({ let mut line = json!({
"behaviour": { "behaviour": {
"step": record.step.to_string(), "step": record.step.to_string(),
"admissions": record.admissions, "admissions": record.admissions,
@ -331,6 +459,9 @@ impl FrameTrace {
}, },
"operational": { "captures": record.captures }, "operational": { "captures": record.captures },
}); });
if let Some(digest) = record.ledgers {
line["behaviour"]["ledgersDigest"] = Value::String(digest);
}
self.write(&line); self.write(&line);
} }
@ -353,6 +484,68 @@ impl Drop for FrameTrace {
mod tests { mod tests {
use super::*; use super::*;
fn lines(path: &Path) -> Vec<Value> {
std::fs::read_to_string(path)
.unwrap()
.lines()
.map(|l| serde_json::from_str(l).unwrap())
.collect()
}
#[test]
fn directory_files_sort_in_start_order() {
assert!(dir_file_name(999, 70_000) < dir_file_name(1000, 5));
assert_eq!(dir_file_name(1, 2), "trace-0000000000001-2.jsonl");
}
#[test]
fn the_byte_cap_ends_the_file_with_a_marker_and_nothing_after() {
let dir = std::env::temp_dir().join(format!("fly-trace-cap-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("t.jsonl");
let mut trace = FrameTrace::create_with(&path, Some(1500), None).unwrap();
for step in 0..40 {
trace.begin(step, 0.0);
}
trace.finish();
drop(trace);
let all = lines(&path);
assert_eq!(all.first().unwrap()["format"], FORMAT);
assert_eq!(all.last().unwrap(), &json!({ "truncated": true }));
assert!(all.len() > 2 && all.len() < 40);
let size = std::fs::metadata(&path).unwrap().len();
assert!(size <= 1500 + 20, "{size}");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn ledger_digests_every_nth_step() {
let dir = std::env::temp_dir().join(format!("fly-trace-ledgers-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("t.jsonl");
let mut trace = FrameTrace::create_with(&path, None, Some(2)).unwrap();
for step in 10..14 {
trace.begin(step, 0.0);
if trace.wants_ledgers() {
trace.ledgers("the ledgers");
}
}
trace.finish();
drop(trace);
let all = lines(&path);
assert_eq!(all[0]["ledgersEvery"], 2);
let digests: Vec<bool> = all[1..]
.iter()
.map(|l| l["behaviour"].get("ledgersDigest").is_some())
.collect();
assert_eq!(digests, vec![true, false, true, false]);
assert_eq!(
all[1]["behaviour"]["ledgersDigest"],
sha256_hex(b"the ledgers").as_str()
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test] #[test]
fn every_legacy_remainder_is_exact_nanoseconds() { fn every_legacy_remainder_is_exact_nanoseconds() {
// The first twelve legacy frames' remainders, from the clock the fixture records. // The first twelve legacy frames' remainders, from the clock the fixture records.