From 9c98281f4a1b66b22d1ba40593180d0f24f08847 Mon Sep 17 00:00:00 2001 From: acamilo Date: Tue, 29 Sep 2026 16:26:07 +0000 Subject: [PATCH] fly-session: legacy_checkpoint, the session runtime's FLYSIM01 export, import, store policy and restore selection (STATE-02) The export assembles a FLYSIM01 file from the agent's FLYAGT01 and the world's FLYENV01 capture payloads, the task's {reward, ratchet} and the host's fields, through flysim-store's encoder. The import reads a file back into those halves. The agent payload is the shipped form of AGENT-01's prototype: learning.updates starts at plasticity.updates, and the file's framebuffer goes in an inputFrame chunk that StageRestore installs, as the legacy restore does. LegacyCheckpointer keeps the legacy loop's generations, hot and durable intervals, per-process milestone archives and writer thread. restore_from_store walks the legacy candidate order and takes the next candidate on any refusal. The agent worker's payload encoder is now one shared function, and legacy_parity seeds through the shipped import. --- services/flysim/Cargo.lock | 1 + services/flysim/crates/fly-session/Cargo.toml | 2 + services/flysim/crates/fly-session/README.md | 1 + .../crates/fly-session/src/legacy_agent.rs | 139 +- .../fly-session/src/legacy_checkpoint.rs | 1151 +++++++++++++++++ .../crates/fly-session/src/legacy_parity.rs | 100 +- services/flysim/crates/fly-session/src/lib.rs | 1 + 7 files changed, 1319 insertions(+), 76 deletions(-) create mode 100644 services/flysim/crates/fly-session/src/legacy_checkpoint.rs diff --git a/services/flysim/Cargo.lock b/services/flysim/Cargo.lock index d356eee..3c6e925 100644 --- a/services/flysim/Cargo.lock +++ b/services/flysim/Cargo.lock @@ -444,6 +444,7 @@ dependencies = [ "flybrain-core", "flybrain-gb", "flybus", + "flysim-store", "serde_json", "tempfile", "tokio", diff --git a/services/flysim/crates/fly-session/Cargo.toml b/services/flysim/crates/fly-session/Cargo.toml index ee1e7d4..1af661b 100644 --- a/services/flysim/crates/fly-session/Cargo.toml +++ b/services/flysim/crates/fly-session/Cargo.toml @@ -32,6 +32,8 @@ flybrain-core = { path = "../flybrain-core" } flybrain-gb = { path = "../flybrain-gb" } # The toy connectome fixture is written as gzipped `.binz` arrays, the dataset loader's format. flate2 = { workspace = true } +# STATE-02: the FLYSIM01 store and the sugar journal, the same code the legacy loop writes with. +flysim-store = { path = "../flysim-store" } serde_json = { workspace = true } # `rt-multi-thread` is not only for the tests: a worker process and a dedicated-thread diff --git a/services/flysim/crates/fly-session/README.md b/services/flysim/crates/fly-session/README.md index 46ef664..639ae58 100644 --- a/services/flysim/crates/fly-session/README.md +++ b/services/flysim/crates/fly-session/README.md @@ -49,6 +49,7 @@ Ready(k) ─ Prepare all agents concurrently ─────────── | `harness` | The runnable composition: router, the flies, one arena, one coordinator | | `legacy_env` | ENV-01: the legacy Game Boy environment worker over binjgb -- the one-frame setup scaffold, the joypad batch, `f32le` audio, the per-boundary memory image, `gameboy-slots-v1`, the capture payload and the world's half of `FLYSIM01` | | `legacy_env_parity` | ENV-01's parity harness: the toy cartridge, recorded scripts, the emulator driven directly in `LegacyFrame` order, the worker driven over the bus, and the replay of the service's own `FLY_TRACE` | +| `legacy_checkpoint` | STATE-02: the legacy composition's `FLYSIM01` checkpoints -- the export from the agent's and the world's capture payloads plus the task's and the host's halves, the import back (with the `learning.updates` rule), the legacy store policy (generations, hot/durable intervals, milestone archives) and the restore selection over the live store. The store code is the `flysim-store` crate, shared with `flysim` | ## Execution modes and the launcher diff --git a/services/flysim/crates/fly-session/src/legacy_agent.rs b/services/flysim/crates/fly-session/src/legacy_agent.rs index a846993..876fc8c 100644 --- a/services/flysim/crates/fly-session/src/legacy_agent.rs +++ b/services/flysim/crates/fly-session/src/legacy_agent.rs @@ -73,6 +73,10 @@ f767d48ff556fceae996e2ac73c0d0871df71101db0af414a0ffcd0bfb678489:\ /// The magic of the agent's capture payload: a `flybrain-core` checkpoint envelope whose /// manifest and chunks are exactly `agent_to_chunks`, plus one `session` manifest member. pub const PAYLOAD_MAGIC: &str = "FLYAGT01"; +/// An optional payload chunk: the frame to install as the next input when the payload is +/// staged. Only the `FLYSIM01` import writes it (STATE-02), because the legacy restore +/// re-projects the saved framebuffer rather than trusting the saved visual drive. +pub const INPUT_FRAME_CHUNK: &str = "inputFrame"; /// The layout version of the `session` member. pub const PAYLOAD_VERSION: u64 = 1; /// The attachment a Commit reply carries the transition's spike bitset under. @@ -243,6 +247,79 @@ pub fn rational_remainder_to_legacy(remainder: &RationalNs) -> Result { + pub agent_id: &'a Id, + pub checkpoint_id: &'a Id, + pub source_scope: &'a Scope, + pub committed_step: u64, + pub profile: &'a AssetRef, + pub seed: i32, + /// Reinforcement calls applied since the fly's fresh start (`learning.updates`). + pub reinforcements: u64, + /// The composition's `executor.macroChannels`, in composition order; empty in raw mode. + pub macro_channels: &'a [String], +} + +/// Encodes a `FLYAGT01` capture payload: `agent_to_chunks(state)` in `flybrain-core`'s envelope, +/// plus the `session` manifest member `State.StageRestore` reads back. `state.remainder` must +/// already be the accumulator's, as a capture sets it. +/// +/// The worker's own `State.Capture` writes exactly this, and so does the `FLYSIM01` import +/// (`crate::legacy_checkpoint`), which is why it is one function. +pub fn encode_payload( + state: &AgentState, + identity: &PayloadIdentity<'_>, + accumulator: &TickAccumulator, + context: &TypedValue, + input_frame: Option<&[u8]>, +) -> Result, String> { + let mut chunks = agent_to_chunks(state); + if let Some(frame) = input_frame { + chunks.chunks.push((INPUT_FRAME_CHUNK.to_owned(), frame.to_vec())); + } + let channels: Vec<&str> = identity.macro_channels.iter().map(String::as_str).collect(); + let session = json!({ + "payloadVersion": PAYLOAD_VERSION, + "kind": "legacy-agent", + "agentId": identity.agent_id, + "checkpointId": identity.checkpoint_id, + "sourceScope": identity.source_scope.to_json(), + "committedStep": identity.committed_step.to_string(), + "profile": identity.profile.to_json(), + "seed": identity.seed, + "reinforcements": identity.reinforcements.to_string(), + "kernelVersion": gameboy::KERNEL_VERSION, + "plasticityVersion": gameboy::PLASTICITY_VERSION, + "macroChannels": identity.macro_channels, + "decoderConfigDigest": decoder_config_digest(&gameboy_decoder_config_with_macros(&channels)), + "accumulator": { + "tickDuration": accumulator.tick_duration().to_json(), + "remainder": accumulator.remainder().to_json(), + "executedTicks": accumulator.executed_ticks().to_string(), + "warmupOffset": accumulator.warmup_offset().to_string(), + }, + "context": context.to_json(), + }); + let mut manifest = chunks.manifest; + let session = JsonValue::parse(&session.to_string()).map_err(|e| e.to_string())?; + manifest.set("session", session); + encode_envelope(PAYLOAD_MAGIC, &manifest, &chunks.chunks).map_err(|e| e.to_string()) +} + +/// The agent state a `FLYAGT01` capture payload carries, with the accumulator's remainder in it. +/// The `session` member is not validated here; `State.StageRestore` does that. +pub fn decode_payload_state(bytes: &[u8]) -> Result { + let parts = decode_envelope(bytes, PAYLOAD_MAGIC) + .map_err(|e| format!("not a {PAYLOAD_MAGIC} envelope: {e}"))?; + agent_from_chunks(&parts.manifest, &parts).map_err(|e| e.to_string()) +} + // ------------------------------------------------------------------------------------------- // The readout transient @@ -1191,43 +1268,23 @@ impl LegacyAgentWorker { // The session owns the frame remainder, as the legacy loop does (`Sim::checkpoint`). state.remainder = rational_remainder_to_legacy(&accumulator.remainder()) .map_err(|e| DomainError::new(ErrorCode::Internal, e, MutationCertainty::None))?; - let chunks = agent_to_chunks(&state); - let channels: Vec<&str> = self - .config - .macro_channels - .iter() - .map(String::as_str) - .collect(); - let session = json!({ - "payloadVersion": PAYLOAD_VERSION, - "kind": "legacy-agent", - "agentId": self.config.agent_id, - "checkpointId": checkpoint_id, - "sourceScope": scope.to_json(), - "committedStep": k.to_string(), - "profile": self.profile.asset.to_json(), - "seed": self.seed, - "reinforcements": self.reinforcements.to_string(), - "kernelVersion": gameboy::KERNEL_VERSION, - "plasticityVersion": gameboy::PLASTICITY_VERSION, - "macroChannels": self.config.macro_channels, - "decoderConfigDigest": decoder_config_digest(&gameboy_decoder_config_with_macros(&channels)), - "accumulator": { - "tickDuration": accumulator.tick_duration().to_json(), - "remainder": accumulator.remainder().to_json(), - "executedTicks": accumulator.executed_ticks().to_string(), - "warmupOffset": accumulator.warmup_offset().to_string(), + encode_payload( + &state, + &PayloadIdentity { + agent_id: &self.config.agent_id, + checkpoint_id, + source_scope: scope, + committed_step: k, + profile: &self.profile.asset, + seed: self.seed, + reinforcements: self.reinforcements, + macro_channels: &self.config.macro_channels, }, - "context": context.typed.to_json(), - }); - let mut manifest = chunks.manifest; - let session = JsonValue::parse(&session.to_string()).map_err(|e| { - DomainError::new(ErrorCode::Internal, e.to_string(), MutationCertainty::None) - })?; - manifest.set("session", session); - encode_envelope(PAYLOAD_MAGIC, &manifest, &chunks.chunks).map_err(|e| { - DomainError::new(ErrorCode::Internal, e.to_string(), MutationCertainty::None) - }) + accumulator, + &context.typed, + None, + ) + .map_err(|e| DomainError::new(ErrorCode::Internal, e, MutationCertainty::None)) } async fn state_capture(&mut self, ctx: &HandlerCtx<'_>) -> DomainResult { @@ -1493,6 +1550,16 @@ impl LegacyAgentWorker { agent .import_state(&state) .map_err(|e| incompatible(format!("the agent state does not import: {e}")))?; + // A payload imported from `FLYSIM01` carries the frame on screen as its next input, and + // the restore installs it, as `LegacyFrame::restore` re-projects the saved framebuffer + // (`legacy_checkpoint::agent_payload`). A worker's own capture carries none. + if let Some(frame) = parts.chunk(INPUT_FRAME_CHUNK) { + let (width, height) = (agent.frame.width, agent.frame.height); + if frame.len() != width as usize * height as usize * 4 { + return Err(incompatible("the staged input frame is not the profile's view")); + } + agent.network.set_visual_frame(frame, width, height); + } if let Some(staged) = &self.staged { return Err(DomainError::before( ErrorCode::Conflict, diff --git a/services/flysim/crates/fly-session/src/legacy_checkpoint.rs b/services/flysim/crates/fly-session/src/legacy_checkpoint.rs new file mode 100644 index 0000000..8ff27ad --- /dev/null +++ b/services/flysim/crates/fly-session/src/legacy_checkpoint.rs @@ -0,0 +1,1151 @@ +//! STATE-02: the legacy Game Boy composition's checkpoints, in `FLYSIM01`, the format of record. +//! +//! `legacy-gameboy-v1` section 16 and the operator's decision of 2026-09-23: `FLYSIM01` stays +//! the format of record until RETIRE-01, and the session runtime exports a `FLYSIM01` envelope on +//! every durable save, which the current `flysim` reads under its unchanged compatibility string. +//! The deploy gate, `fly-reset-to-milestone` and `fly-loop-reset` keep working on those files. This +//! module is that export, the import back, the store policy around them and the restore selection: +//! +//! | `FLYSIM01` | Owner in the session runtime | Here | +//! | --- | --- | --- | +//! | the seven agent chunks and `remainder` | the agent (`FLYAGT01` capture payload) | [`agent_state`], [`agent_payload`] | +//! | `emulator`, `framebuffer`, `emulatorFrame`, `buttons`, `romHash`, `ratchetGame`, `ratchetFrame` | the environment (`FLYENV01`; the ratchet's snapshot is slot `best`) | [`world_state`], [`world_payload`] | +//! | `reward`, `ratchet` | the task's ledger | [`TaskHalf`] | +//! | `generation`, `wallMs`, `compatibility`, `speed`, `rankSinceMs`, `lastEventId` | the host (store, feed, rank tracking) | [`HostHalf`], [`LegacyCheckpointer`] | +//! +//! **Byte compatibility is by construction.** The envelope is written by +//! `flysim_store::store::encode`, the one function the legacy loop writes with, from the same +//! values: [`export`] builds a `RuntimeState` field for field as `Sim::snapshot_state` does. The +//! store is `flysim_store::store::Store`, so generations, `manifest.json`, `milestone-` +//! archives, `keep_generations` rotation and the restore order are the legacy store's own code. +//! The tests prove the rest on real checkpoints: legacy -> session -> legacy and session -> +//! legacy -> session round trips, and restore-then-continue against the legacy loop's +//! `FLY_TRACE`. +//! +//! **Restore is `legacy-transient-reset`** (`legacy-gameboy-v1` section 14): what `FLYSIM01` +//! carries is restored; the executor's ledgers, the running macro, the readout transient and the +//! adapter's transient observations are not in it and start cleared. +//! +//! **Declared difference, archive order** (`legacy-gameboy-v1` sections 4 and 16, operator +//! 2026-09-23): a milestone archive written by the session runtime is captured *after* that +//! boundary's slot save, so it holds the post-capture ratchet and slot (`best = r`, rung `r`'s +//! snapshot), where the legacy loop's holds `best = r-1` and rung `r-1`'s. Both are ordinary +//! `FLYSIM01` files and each runtime restores the other's. + +use std::sync::mpsc; +use std::thread::JoinHandle; +use std::time::{Duration, Instant}; + +use fly_session_types::gameboy; +use flybrain_core::agent::AgentState; +use flybrain_gb::RatchetState; +use flysim_store::store::{self, Candidate, Checkpoint, RuntimeState, Store}; +use serde_json::Value; +use tokio::sync::oneshot; + +use crate::clock::TickAccumulator; +use crate::legacy_agent::{self, PayloadIdentity, legacy_remainder_to_rational}; +use crate::legacy_env::{Flysim01World, PayloadHeader, WorldState}; +use crate::types::*; + +pub use flysim_store::journal; +pub use flysim_store::store as flysim01; + +/// The source epoch of a payload made from a `FLYSIM01` file: the legacy file has no session +/// identity, so the imported payloads say where they came from rather than invent one. +pub const LEGACY_SOURCE_EPOCH: &str = "legacy"; + +// ------------------------------------------------------------------------------------------- +// The halves + +/// The task's half of a `FLYSIM01` checkpoint: the manifest members `reward` and `ratchet`. +/// +/// This is the `pokered-macros-v1` task ledger (`legacy-gameboy-v1` section 10: "the adapter +/// state (FLYSIM01's `reward` chunk) and the ratchet state"). The executor's ledgers are not in +/// it (`legacy-transient-reset`). +#[derive(Clone, Debug, PartialEq)] +pub struct TaskHalf { + /// `GameAdapter::export_state`, exactly as `FLYSIM01` records it; `null` for an adapter that + /// has none, which a restore then leaves alone. + pub reward: Value, + pub ratchet: RatchetState, +} + +impl TaskHalf { + /// `{reward, ratchet, slotFilled}`: the ledger value the task captures and installs. + /// `slotFilled` says whether the ratchet has a snapshot, which lives in the environment's + /// slot `best`; it is derived, never stored in `FLYSIM01`. + pub fn to_ledger(&self, slot_filled: bool) -> Value { + serde_json::json!({ + "reward": self.reward, + "ratchet": serde_json::to_value(self.ratchet).expect("a ratchet state serializes"), + "slotFilled": slot_filled, + }) + } + + pub fn from_ledger(value: &Value) -> Result { + let ratchet = value.get("ratchet").ok_or("the task ledger has no ratchet")?; + Ok(TaskHalf { + reward: value.get("reward").cloned().unwrap_or(Value::Null), + ratchet: serde_json::from_value(ratchet.clone()) + .map_err(|e| format!("the task ledger's ratchet: {e}"))?, + }) + } +} + +/// The host's half: what neither the agent, the world nor the task owns. +#[derive(Clone, Debug, PartialEq)] +pub struct HostHalf { + pub generation: u64, + pub wall_ms: u64, + /// The legacy compatibility string (`flysim --print-compatibility`), recorded, not + /// reinterpreted; it is still the restore gate (`legacy-gameboy-v1` section 12). + pub compatibility: String, + pub speed: f64, + /// Brain milliseconds at which the current ladder rank was reached. + pub rank_since_ms: f64, + /// The feed event log's watermark: the last event id issued. + pub last_event_id: u64, +} + +/// A `FLYSIM01` checkpoint read into its owners' halves. +#[derive(Clone, Debug)] +pub struct Halves { + pub agent: AgentState, + pub world: WorldState, + pub task: TaskHalf, + pub host: HostHalf, +} + +// ------------------------------------------------------------------------------------------- +// Export + +/// The agent state a `FLYAGT01` capture payload carries (its remainder is the accumulator's). +pub fn agent_state(agent_payload: &[u8]) -> Result { + legacy_agent::decode_payload_state(agent_payload) +} + +/// The world a `FLYENV01` capture payload carries. +pub fn world_state(world_payload: &[u8]) -> Result { + WorldState::decode(world_payload).map(|(_, state)| state) +} + +/// The `FLYSIM01` runtime fields of one boundary, exactly as `Sim::snapshot_state` fills them. +pub fn runtime_state( + world: &WorldState, + slot_id: &Id, + task: &TaskHalf, + host: &HostHalf, +) -> RuntimeState { + let Flysim01World { + rom_hash, + emulator_frame, + buttons, + emulator, + framebuffer, + ratchet_game, + ratchet_frame, + } = world.flysim01_chunks(slot_id); + RuntimeState { + generation: host.generation, + wall_ms: host.wall_ms, + rom_sha256: rom_hash, + emulator_frame, + compatibility: host.compatibility.clone(), + speed: host.speed, + buttons, + rank_since_ms: host.rank_since_ms, + last_event_id: host.last_event_id, + reward: task.reward.clone(), + ratchet: task.ratchet, + emulator, + framebuffer, + ratchet_game, + ratchet_frame, + } +} + +/// The `FLYSIM01` envelope of one boundary, from the agent's state, the world, the task's half +/// and the host's. +pub fn export_states( + agent: &AgentState, + world: &WorldState, + slot_id: &Id, + task: &TaskHalf, + host: &HostHalf, +) -> Result, String> { + store::encode(agent, &runtime_state(world, slot_id, task, host)).map_err(|e| format!("{e:#}")) +} + +/// The `FLYSIM01` envelope of one boundary, from the two participants' capture payloads. +pub fn export( + agent_payload: &[u8], + world_payload: &[u8], + slot_id: &Id, + task: &TaskHalf, + host: &HostHalf, +) -> Result, String> { + let agent = agent_state(agent_payload)?; + let world = world_state(world_payload)?; + export_states(&agent, &world, slot_id, task, host) +} + +// ------------------------------------------------------------------------------------------- +// Import + +/// Reads a decoded `FLYSIM01` checkpoint into its owners' halves. The world is at boundary +/// `emulatorFrame - setupFrames` with the ratchet's snapshot as slot `slot_id`. +pub fn halves( + checkpoint: &Checkpoint, + episode_id: &Id, + slot_id: &Id, + audio_sample_rate: u64, +) -> Result { + let runtime = &checkpoint.runtime; + let world = Flysim01World { + rom_hash: runtime.rom_sha256.clone(), + emulator_frame: runtime.emulator_frame, + buttons: runtime.buttons, + emulator: runtime.emulator.clone(), + framebuffer: runtime.framebuffer.clone(), + ratchet_game: runtime.ratchet_game.clone(), + ratchet_frame: runtime.ratchet_frame.clone(), + } + .into_state(episode_id, slot_id, audio_sample_rate)?; + Ok(Halves { + agent: checkpoint.agent.clone(), + world, + task: TaskHalf { + reward: runtime.reward.clone(), + ratchet: runtime.ratchet, + }, + host: HostHalf { + generation: runtime.generation, + wall_ms: runtime.wall_ms, + compatibility: runtime.compatibility.clone(), + speed: runtime.speed, + rank_since_ms: runtime.rank_since_ms, + last_event_id: runtime.last_event_id, + }, + }) +} + +/// Decodes `FLYSIM01` bytes into halves. +pub fn split( + bytes: &[u8], + episode_id: &Id, + slot_id: &Id, + audio_sample_rate: u64, +) -> Result { + let checkpoint = store::decode(bytes).map_err(|e| format!("{e:#}"))?; + halves(&checkpoint, episode_id, slot_id, audio_sample_rate) +} + +/// The reinforcement calls to start a legacy agent state's `learning.updates` at. +/// +/// `FLYSIM01` records `plasticity.updates`, the reinforcements that moved a gain, but not every +/// call, and the contract requires `learning.changed <= learning.updates`. An import that starts +/// the call count at zero makes the first telemetry unreadable (found by AGENT-01 on the row-58 +/// stream checkpoint: `changed` is in the thousands), so it starts at the recorded count, a +/// lower bound on the calls actually made (`legacy-gameboy-v1` section 13, amendment of +/// 2026-09-29). +pub fn legacy_reinforcements(state: &AgentState) -> u64 { + state.network.plasticity.updates as u64 +} + +/// The session accumulator of a legacy agent state: its exact remainder, every tick it has run +/// (`network.ms`, warm-up included) and the profile's warm-up as the offset. +pub fn legacy_accumulator(state: &AgentState) -> Result { + let ms = state.network.ms; + if !ms.is_finite() || ms < 0.0 || ms.fract() != 0.0 { + return Err(format!("the agent's brain clock {ms} ms is not a whole tick count")); + } + let ticks = ms as u64; + let warmup = if state.warmed_up { gameboy::WARMUP_MS.min(ticks) } else { 0 }; + TickAccumulator::restored( + gameboy::tick_duration(), + legacy_remainder_to_rational(state.remainder)?, + ticks, + warmup, + ) +} + +/// What the agent's imported payload is stamped with besides its state. +#[derive(Clone, Debug)] +pub struct AgentImport { + pub agent_id: Id, + pub profile: AssetRef, + pub seed: i32, + pub macro_channels: Vec, +} + +/// A `FLYAGT01` payload for `State.StageRestore` from a legacy agent state, the shipped form of +/// AGENT-01's `seed_from_legacy_state` prototype. `context` is the decision context the task +/// gives the restored agent for its next Prepare (`{boot, bound, location}` after the executor +/// observed the restored world). `frame_on_screen` is the file's `framebuffer`: the restore +/// installs it as the next input, as `LegacyFrame::restore` does, so a file whose saved visual +/// drive is not its framebuffer's projection (a survey checkpoint, say) restores to the legacy +/// outcome and not to its own bytes (`legacy-gameboy-v1` section 16 amendment of 2026-09-29). +pub fn agent_payload( + state: &AgentState, + frame_on_screen: &[u8], + import: &AgentImport, + checkpoint_id: &Id, + source_scope: &Scope, + context: &TypedValue, +) -> Result, String> { + let accumulator = legacy_accumulator(state)?; + legacy_agent::encode_payload( + state, + &PayloadIdentity { + agent_id: &import.agent_id, + checkpoint_id, + source_scope, + committed_step: source_scope.step, + profile: &import.profile, + seed: import.seed, + reinforcements: legacy_reinforcements(state), + macro_channels: &import.macro_channels, + }, + &accumulator, + context, + Some(frame_on_screen), + ) +} + +/// A `FLYENV01` payload for `State.StageRestore` from a world read out of `FLYSIM01`. +pub fn world_payload( + world: &WorldState, + worker_id: &Id, + checkpoint_id: &Id, + source_scope: &Scope, + configuration_digest: &Digest, +) -> Vec { + world.encode(&PayloadHeader { + worker_id: worker_id.clone(), + checkpoint_id: checkpoint_id.clone(), + source_scope: source_scope.clone(), + configuration_digest: configuration_digest.clone(), + }) +} + +/// The scope a payload made from a `FLYSIM01` file records as its source: the session, the +/// [`LEGACY_SOURCE_EPOCH`] and the world's boundary. +pub fn legacy_source_scope(session_id: &Id, boundary: u64) -> Scope { + scope_at(session_id, LEGACY_SOURCE_EPOCH, boundary) +} + +// ------------------------------------------------------------------------------------------- +// Restore selection + +/// What a candidate must be to be restored: the legacy loop's `try_restore` gate. +#[derive(Clone, Debug)] +pub struct RestoreGate { + pub rom_sha256: String, + /// This build's compatibility string. + pub compatibility: String, + /// The adapter ids this build's adapter migrates from (`GameAdapter::migrates_from`). + pub migrates_from: Vec, + /// `FLY_ACCEPT_ADAPTERS`, parsed (`flybrain_gb::compatibility::accepted_adapters`). + pub accepted: Vec, +} + +impl RestoreGate { + /// The gate with `FLY_ACCEPT_ADAPTERS` read from the process environment, as `try_restore` + /// reads it: a property of a deploy, not of a run. + pub fn from_env(rom_sha256: &str, compatibility: &str, migrates_from: &[&str]) -> RestoreGate { + RestoreGate { + rom_sha256: rom_sha256.to_owned(), + compatibility: compatibility.to_owned(), + migrates_from: migrates_from.iter().map(|s| (*s).to_owned()).collect(), + accepted: flybrain_gb::compatibility::accepted_adapters( + std::env::var(flybrain_gb::compatibility::ACCEPT_ADAPTERS_ENV).ok().as_deref(), + ), + } + } + + /// `Ok` for a restorable checkpoint (with the adapter it migrates from, if any), or why not. + pub fn check(&self, checkpoint: &Checkpoint) -> Result, String> { + let runtime = &checkpoint.runtime; + if runtime.rom_sha256 != self.rom_sha256 { + return Err(format!("checkpoint is for another cartridge ({})", runtime.rom_sha256)); + } + let migrates: Vec<&str> = self.migrates_from.iter().map(String::as_str).collect(); + match flybrain_gb::compatibility::decide( + &runtime.compatibility, + &self.compatibility, + &migrates, + &self.accepted, + ) { + flybrain_gb::compatibility::RestoreDecision::Exact => Ok(None), + flybrain_gb::compatibility::RestoreDecision::MigrateAdapter { from } => Ok(Some(from)), + flybrain_gb::compatibility::RestoreDecision::Refuse(reason) => Err(format!( + "compatibility mismatch: {reason}\n checkpoint: {}\n this build: {}", + runtime.compatibility, self.compatibility + )), + } + } +} + +/// One restored candidate. +#[derive(Clone, Debug)] +pub struct Restored { + pub candidate: Candidate, + /// Candidates refused before this one, with why: the legacy `restore_fallback` metric. + pub skipped: Vec<(Candidate, String)>, + pub migrated_from: Option, + pub checkpoint: Checkpoint, + pub installed: T, +} + +/// The outcome of a restore over the live store. +#[derive(Debug)] +pub enum Selection { + /// No candidate exists: warm up a fresh fly. + Fresh, + Restored(Box>), + /// Candidates exist and every one failed. The legacy loop exits non-zero here rather than + /// silently start a fresh fly over them, and so must the session runtime. + Refused(Vec<(Candidate, String)>), +} + +/// Restores from the live `FLYSIM01` stores in the legacy order -- hot latest, hot previous, +/// durable latest, durable previous, then the milestone archives by descending rank +/// (`store::restore_order`) -- trying the next candidate on *any* failure: unreadable, +/// another cartridge, refused compatibility, or `install` refusing it (a participant's +/// `State.StageRestore` or the task's validation). This is `Sim::restore_or_warm_up`. +pub async fn restore_from_store( + hot: &Store, + durable: &Store, + gate: &RestoreGate, + mut install: F, +) -> Selection +where + F: FnMut(Checkpoint) -> Fut, + Fut: std::future::Future>, +{ + let candidates = store::restore_order(hot, durable); + if candidates.is_empty() { + return Selection::Fresh; + } + let mut skipped: Vec<(Candidate, String)> = Vec::new(); + for candidate in candidates { + let checkpoint = match store::load(&candidate.path) { + Ok(checkpoint) => checkpoint, + Err(e) => { + skipped.push((candidate, format!("{e:#}"))); + continue; + } + }; + let migrated_from = match gate.check(&checkpoint) { + Ok(from) => from, + Err(reason) => { + skipped.push((candidate, reason)); + continue; + } + }; + match install(checkpoint.clone()).await { + Ok(installed) => { + return Selection::Restored(Box::new(Restored { + candidate, + skipped, + migrated_from, + checkpoint, + installed, + })); + } + Err(reason) => skipped.push((candidate, reason)), + } + } + Selection::Refused(skipped) +} + +// ------------------------------------------------------------------------------------------- +// The store policy + +/// Why a save is taken. Each maps onto the legacy loop's call site. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SaveKind { + /// `checkpoint_if_due`, the hot interval: `hot_dir`, every `hot_seconds`. + Hot, + /// A durable save: the interval (`checkpoint_seconds`), startup/restore, after a rollback, + /// a forced checkpoint, a pause and shutdown. + Durable, + /// `track_rank`: a climb to a new rank. Durable, and archived as `milestone-` when the + /// rank is above every rank this process archived. + Milestone(u32), +} + +/// A rank change the task reported, as `Sim::track_rank` sees it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RankChange { + Climbed(u32), + FellBack(u32), +} + +/// The configuration the legacy loop reads from `[paths]` and `[loop]`. +#[derive(Clone, Debug)] +pub struct CheckpointerConfig { + pub hot_dir: std::path::PathBuf, + pub durable_dir: std::path::PathBuf, + pub keep_generations: usize, + pub hot_seconds: f64, + pub checkpoint_seconds: f64, + pub compatibility: String, + pub speed: f64, + /// The environment's slot that holds the ratchet's snapshot (`best`). + pub slot_id: Id, +} + +/// One participant-coherent capture of a boundary, ready to be written. +#[derive(Clone, Debug)] +pub struct LegacyCapture { + pub boundary: u64, + pub agent_payload: Vec, + pub world_payload: Vec, + pub task: TaskHalf, +} + +/// What a committed save wrote. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SaveReport { + pub generation: u64, + pub durable: bool, + pub archive_rank: Option, + pub bytes: usize, +} + +/// A save that was queued; its reply resolves when the writer committed it (or failed). +pub struct SaveTicket { + pub generation: u64, + pub durable: bool, + pub archive_rank: Option, + pub reply: oneshot::Receiver>, +} + +struct WriteJob { + durable: bool, + archive_rank: Option, + capture: LegacyCapture, + host: HostHalf, + slot_id: Id, + reply: oneshot::Sender>, +} + +/// The session runtime's `FLYSIM01` store policy: the legacy loop's, call site for call site. +/// +/// - generations come from one counter over both stores, starting above the highest either +/// has ever allocated (`Sim::boot`); +/// - a hot copy every `hot_seconds`, a durable one every `checkpoint_seconds`, the durable +/// interval resetting the hot one (`checkpoint_if_due`); +/// - the first durable commit at a new best rank of *this process* is also written as +/// `milestone-.checkpoint` (`checkpoint_with_reply`'s `best_archived_rank`); +/// - the encoding and the fsyncs run on a writer thread, off the session's runtime. +pub struct LegacyCheckpointer { + config: CheckpointerConfig, + hot: Store, + durable: Store, + next_generation: u64, + best_archived_rank: Option, + rank: u32, + rank_since_ms: f64, + next_hot: Instant, + next_durable: Instant, + writer: Option>, + thread: Option>, +} + +impl LegacyCheckpointer { + /// Creates both store directories and starts the writer. + pub fn open(config: CheckpointerConfig) -> Result { + let durable = Store::new(&config.durable_dir, config.keep_generations); + let hot = Store::new(&config.hot_dir, config.keep_generations); + durable.create().map_err(|e| format!("{e:#}"))?; + hot.create().map_err(|e| format!("{e:#}"))?; + let next_generation = durable.highest_generation().max(hot.highest_generation()) + 1; + let (tx, rx) = mpsc::channel::(); + let (writer_hot, writer_durable) = (hot.clone(), durable.clone()); + let thread = std::thread::Builder::new() + .name("legacy-checkpoint".to_owned()) + .spawn(move || { + while let Ok(job) = rx.recv() { + let store = if job.durable { &writer_durable } else { &writer_hot }; + let generation = job.host.generation; + let result = export( + &job.capture.agent_payload, + &job.capture.world_payload, + &job.slot_id, + &job.capture.task, + &job.host, + ) + .and_then(|bytes| { + store + .commit(generation, &bytes, job.archive_rank) + .map(|_| bytes.len()) + .map_err(|e| format!("{e:#}")) + }) + .map(|bytes| SaveReport { + generation, + durable: job.durable, + archive_rank: job.archive_rank, + bytes, + }); + let _ = job.reply.send(result); + } + }) + .map_err(|e| format!("spawning the checkpoint writer: {e}"))?; + let now = Instant::now(); + Ok(LegacyCheckpointer { + config, + hot, + durable, + next_generation, + best_archived_rank: None, + rank: 0, + rank_since_ms: 0.0, + next_hot: now, + next_durable: now, + writer: Some(tx), + thread: Some(thread), + }) + } + + pub fn hot(&self) -> &Store { + &self.hot + } + + pub fn durable(&self) -> &Store { + &self.durable + } + + pub fn config(&self) -> &CheckpointerConfig { + &self.config + } + + /// The generation the next save will take. + pub fn next_generation(&self) -> u64 { + self.next_generation + } + + pub fn rank(&self) -> u32 { + self.rank + } + + pub fn rank_since_ms(&self) -> f64 { + self.rank_since_ms + } + + /// After a restore: the ladder rank the restored task reports and the checkpoint's + /// `rankSinceMs` (`Sim::try_restore`). A fresh start keeps rank 0. + pub fn restored(&mut self, rank: u32, rank_since_ms: f64) { + self.rank = rank; + self.rank_since_ms = rank_since_ms; + } + + /// Both intervals run from now: called right after the startup durable commit, as + /// `Sim::boot` does, so the loop's first boundary does not write a second one. + pub fn start_intervals(&mut self, now: Instant) { + self.next_hot = now + Duration::from_secs_f64(self.config.hot_seconds); + self.next_durable = now + Duration::from_secs_f64(self.config.checkpoint_seconds); + } + + /// `Sim::track_rank`: the rank after a transition. A climb is a milestone save. + pub fn observe_rank(&mut self, rank: u32, brain_ms: f64) -> Option { + if rank == self.rank { + return None; + } + let climbed = rank > self.rank; + self.rank = rank; + self.rank_since_ms = brain_ms; + Some(if climbed { RankChange::Climbed(rank) } else { RankChange::FellBack(rank) }) + } + + /// `Sim::checkpoint_if_due`: the interval save due now, if any. + pub fn due(&mut self, now: Instant) -> Option { + let hot_period = Duration::from_secs_f64(self.config.hot_seconds); + let durable_period = Duration::from_secs_f64(self.config.checkpoint_seconds); + if now >= self.next_durable { + self.next_durable = now + durable_period; + self.next_hot = now + hot_period; + Some(SaveKind::Durable) + } else if now >= self.next_hot { + self.next_hot = now + hot_period; + Some(SaveKind::Hot) + } else { + None + } + } + + /// Queues one save of `capture`. The generation is allocated here, on the caller's thread, + /// in call order, and a milestone archive is decided here too, exactly as + /// `Sim::checkpoint_with_reply` decides both before handing the write to its thread. + pub fn save( + &mut self, + kind: SaveKind, + capture: LegacyCapture, + last_event_id: u64, + wall_ms: u64, + ) -> Result { + let durable = kind != SaveKind::Hot; + let archive_rank = match kind { + SaveKind::Milestone(rank) => Some(rank), + _ => None, + } + .filter(|rank| durable && self.best_archived_rank.is_none_or(|best| *rank > best)); + let generation = self.next_generation; + self.next_generation += 1; + if let Some(rank) = archive_rank { + self.best_archived_rank = Some(rank); + } + let host = HostHalf { + generation, + wall_ms, + compatibility: self.config.compatibility.clone(), + speed: self.config.speed, + rank_since_ms: self.rank_since_ms, + last_event_id, + }; + let (tx, rx) = oneshot::channel(); + let job = WriteJob { + durable, + archive_rank, + capture, + host, + slot_id: self.config.slot_id.clone(), + reply: tx, + }; + self.writer + .as_ref() + .ok_or("the checkpoint writer is gone")? + .send(job) + .map_err(|_| "the checkpoint writer stopped".to_owned())?; + Ok(SaveTicket { + generation, + durable, + archive_rank, + reply: rx, + }) + } + + /// Restore candidates in the legacy order. + pub fn candidates(&self) -> Vec { + store::restore_order(&self.hot, &self.durable) + } + + /// Stops the writer after every queued save committed. + pub fn close(&mut self) { + self.writer = None; + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} + +impl Drop for LegacyCheckpointer { + fn drop(&mut self) { + self.close(); + } +} + +/// The current wall clock in milliseconds, for `wallMs`. +pub fn now_wall_ms() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis() as u64) + .unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::legacy_env::{SlotState, sample_at, world_time_at}; + use flybrain_gb::emulator::FRAMEBUFFER_LEN; + + fn agent(ms: f64, remainder: f64, updates: f64) -> AgentState { + use flybrain_core::decoder::DecoderState; + use flybrain_core::lif::LifState; + use flybrain_core::ordered::NumberMap; + use flybrain_core::plasticity::PlasticityState; + AgentState { + version: 1, + remainder, + warmed_up: true, + network: LifState { + membrane: vec![0.5, -0.25], + refractory: vec![0, 3], + last_spike_ms: vec![-1_000_000.0, 12.0], + visual_drive: vec![0.1], + rng: -12_345, + reward_remaining: 40.0, + ms, + population_rate: 1.5, + rates: NumberMap::from_pairs([("forward", 2.0), ("reward_pam", 0.5)]), + plasticity: PlasticityState { + version: "fly-kc-mbon-rstdp-v2".to_string(), + topology: 42, + enabled: true, + updates, + signal: 0.25, + gains: vec![1.0, 0.9], + traces: vec![0.0, 0.1], + touched: vec![0.0, 8_000.0], + }, + }, + decoder: DecoderState { + version: 4, + calibrated: true, + baseline: NumberMap::from_pairs([("forward", 1.0)]), + held_until: NumberMap::new(), + next_allowed: NumberMap::new(), + next_decision: 100.0, + current: Some("a".to_string()), + fatigue: NumberMap::new(), + macro_next_decision: 0.0, + macro_current: None, + macro_fatigue: NumberMap::new(), + }, + } + } + + fn world_at(boundary: u64, slot: bool) -> WorldState { + let world_time = world_time_at(boundary).unwrap(); + WorldState { + rom_digest: "ab".repeat(32), + episode_id: id("ep1"), + boundary, + audio_next_sample: sample_at(&world_time, 48_000), + world_time, + engine_frame: boundary + 1, + buttons: 0b0001_0000, + emulator: vec![7; 64], + framebuffer: vec![9; FRAMEBUFFER_LEN], + slots: if slot { + vec![( + id("best"), + SlotState { game: vec![1, 2, 3], frame: vec![4; FRAMEBUFFER_LEN], boundary }, + )] + } else { + Vec::new() + }, + } + } + + fn task() -> TaskHalf { + TaskHalf { + reward: serde_json::json!({ "version": 3, "total": 1.25 }), + ratchet: RatchetState { best: 3, attempts: 1, ..Default::default() }, + } + } + + fn host(generation: u64) -> HostHalf { + HostHalf { + generation, + wall_ms: 1_700_000_000_000, + compatibility: "kernel/adapter/fingerprint".to_owned(), + speed: 1.0, + rank_since_ms: 4_242.0, + last_event_id: 77, + } + } + + #[test] + fn the_export_is_the_legacy_encoder_on_the_same_values_and_splits_back_into_its_halves() { + let agent = agent(9_000.0, 0.75, 3.0); + let world = world_at(12_344, true); + let bytes = export_states(&agent, &world, &id("best"), &task(), &host(4)).unwrap(); + // Field for field what `Sim::snapshot_state` builds, through the one encoder. + let legacy = store::encode( + &agent, + &RuntimeState { + generation: 4, + wall_ms: 1_700_000_000_000, + rom_sha256: "ab".repeat(32), + emulator_frame: 12_345, + compatibility: "kernel/adapter/fingerprint".to_owned(), + speed: 1.0, + buttons: 0b0001_0000, + rank_since_ms: 4_242.0, + last_event_id: 77, + reward: task().reward, + ratchet: task().ratchet, + emulator: vec![7; 64], + framebuffer: vec![9; FRAMEBUFFER_LEN], + ratchet_game: vec![1, 2, 3], + ratchet_frame: vec![4; FRAMEBUFFER_LEN], + }, + ) + .unwrap(); + assert_eq!(bytes, legacy); + let back = split(&bytes, &id("ep1"), &id("best"), 48_000).unwrap(); + assert_eq!(back.agent, agent); + assert_eq!(back.world, world); + assert_eq!(back.task, task()); + assert_eq!(back.host, host(4)); + // And no slot is an empty ratchet snapshot, both ways. + let bytes = export_states(&agent, &world_at(5, false), &id("best"), &task(), &host(1)).unwrap(); + let back = split(&bytes, &id("ep1"), &id("best"), 48_000).unwrap(); + assert!(back.world.slots.is_empty()); + } + + #[test] + fn the_export_from_capture_payloads_equals_the_export_from_states() { + let agent = agent(9_000.0, 0.75, 3.0); + let world = world_at(700, true); + let import = AgentImport { + agent_id: id("fly"), + profile: gameboy::profile_asset_ref(), + seed: 22_222, + macro_channels: vec!["macro_talk".to_owned()], + }; + let scope = legacy_source_scope(&id("s1"), world.boundary); + let context = gameboy::ReadoutContext { boot: false, bound: vec![], location: None }.to_typed(); + let agent_bytes = agent_payload(&agent, &world.framebuffer, &import, &id("c1"), &scope, &context).unwrap(); + let world_bytes = world_payload(&world, &id("world"), &id("c1"), &scope, &"cd".repeat(32)); + assert_eq!(agent_state(&agent_bytes).unwrap(), agent); + assert_eq!(world_state(&world_bytes).unwrap(), world); + assert_eq!( + export(&agent_bytes, &world_bytes, &id("best"), &task(), &host(9)).unwrap(), + export_states(&agent, &world, &id("best"), &task(), &host(9)).unwrap() + ); + assert!(export(&world_bytes, &agent_bytes, &id("best"), &task(), &host(9)).is_err()); + } + + #[test] + fn the_import_starts_the_accumulator_and_the_reinforcement_count_from_the_legacy_state() { + let state = agent(108_246_189.0, 0.75, 4_321.0); + let accumulator = legacy_accumulator(&state).unwrap(); + assert_eq!(accumulator.brain_ticks(), 108_246_189); + assert_eq!(accumulator.warmup_offset(), gameboy::WARMUP_MS); + assert_eq!( + crate::legacy_agent::rational_remainder_to_legacy(&accumulator.remainder()).unwrap(), + 0.75 + ); + assert_eq!(legacy_reinforcements(&state), 4_321); + // A clock that is not whole ticks, or a remainder that is not k/32768 ms, is refused. + assert!(legacy_accumulator(&agent(10.5, 0.0, 0.0)).is_err()); + assert!(legacy_accumulator(&agent(10.0, 0.1, 0.0)).is_err()); + let session = { + let import = AgentImport { + agent_id: id("fly"), + profile: gameboy::profile_asset_ref(), + seed: 22_222, + macro_channels: Vec::new(), + }; + let context = + gameboy::ReadoutContext { boot: true, bound: vec![], location: None }.to_typed(); + let bytes = agent_payload( + &state, + &[0; FRAMEBUFFER_LEN], + &import, + &id("c1"), + &legacy_source_scope(&id("s1"), 40), + &context, + ) + .unwrap(); + let parts = flybrain_core::envelope::decode_envelope( + &bytes, + crate::legacy_agent::PAYLOAD_MAGIC, + ) + .unwrap(); + let session: Value = + serde_json::from_str(&parts.manifest.get("session").unwrap().stringify()).unwrap(); + session + }; + assert_eq!(session["reinforcements"], "4321"); + assert_eq!(session["committedStep"], "40"); + assert_eq!(session["sourceScope"]["epoch"], LEGACY_SOURCE_EPOCH); + assert_eq!(session["accumulator"]["executedTicks"], "108246189"); + assert_eq!(session["accumulator"]["warmupOffset"], "2500"); + } + + #[test] + fn the_task_ledger_carries_exactly_the_reward_and_the_ratchet() { + let ledger = task().to_ledger(true); + assert_eq!(ledger["slotFilled"], true); + assert_eq!(TaskHalf::from_ledger(&ledger).unwrap(), task()); + assert_eq!( + ledger["ratchet"], + serde_json::to_value(task().ratchet).unwrap(), + "the FLYSIM01 manifest's own spelling" + ); + assert!(TaskHalf::from_ledger(&serde_json::json!({ "reward": null })).is_err()); + } + + fn capture(boundary: u64) -> LegacyCapture { + let agent = agent(9_000.0 + boundary as f64, 0.75, 3.0); + let world = world_at(boundary, boundary > 1); + let scope = legacy_source_scope(&id("s1"), boundary); + let import = AgentImport { + agent_id: id("fly"), + profile: gameboy::profile_asset_ref(), + seed: 22_222, + macro_channels: Vec::new(), + }; + let context = gameboy::ReadoutContext { boot: false, bound: vec![], location: None }.to_typed(); + LegacyCapture { + boundary, + agent_payload: agent_payload(&agent, &world.framebuffer, &import, &id("c"), &scope, &context) + .unwrap(), + world_payload: world_payload(&world, &id("world"), &id("c"), &scope, &"cd".repeat(32)), + task: task(), + } + } + + fn config(root: &std::path::Path) -> CheckpointerConfig { + CheckpointerConfig { + hot_dir: root.join("hot"), + durable_dir: root.join("state"), + keep_generations: 2, + hot_seconds: 5.0, + checkpoint_seconds: 300.0, + compatibility: "kernel/adapter/fingerprint".to_owned(), + speed: 1.0, + slot_id: id("best"), + } + } + + fn commit(checkpointer: &mut LegacyCheckpointer, kind: SaveKind, boundary: u64) -> SaveReport { + let ticket = checkpointer.save(kind, capture(boundary), boundary, 1).unwrap(); + ticket.reply.blocking_recv().unwrap().unwrap() + } + + #[test] + fn the_checkpointer_keeps_the_legacy_generations_archives_and_rotation() { + let dir = tempfile::tempdir().unwrap(); + // A store the legacy loop already wrote to: generation numbers are never reused. + let durable = Store::new(dir.path().join("state"), 2); + durable.create().unwrap(); + durable.stage_for_test(41, b"a crashed commit").unwrap(); + std::fs::rename( + dir.path().join("state/41.checkpoint.tmp"), + dir.path().join("state/41.checkpoint"), + ) + .unwrap(); + let mut checkpointer = LegacyCheckpointer::open(config(dir.path())).unwrap(); + assert_eq!(checkpointer.next_generation(), 42); + + let first = commit(&mut checkpointer, SaveKind::Durable, 10); + assert_eq!((first.generation, first.durable, first.archive_rank), (42, true, None)); + let hot = commit(&mut checkpointer, SaveKind::Hot, 11); + assert_eq!((hot.generation, hot.durable), (43, false)); + assert_eq!(checkpointer.hot().manifest().unwrap().latest, Some(43)); + + // A climb: rank and rankSinceMs as track_rank keeps them, and the first archive. + assert_eq!(checkpointer.observe_rank(0, 1.0), None); + assert_eq!(checkpointer.observe_rank(4, 500.0), Some(RankChange::Climbed(4))); + let archived = commit(&mut checkpointer, SaveKind::Milestone(4), 12); + assert_eq!(archived.archive_rank, Some(4)); + assert!(checkpointer.durable().archive_path(4).is_file()); + let decoded = store::load(&checkpointer.durable().archive_path(4)).unwrap(); + assert_eq!(decoded.runtime.generation, 44); + assert_eq!(decoded.runtime.rank_since_ms, 500.0); + assert_eq!(decoded.runtime.last_event_id, 12); + // Falling back and climbing to the same rank again is not a new best for this process. + assert_eq!(checkpointer.observe_rank(3, 600.0), Some(RankChange::FellBack(3))); + assert_eq!(checkpointer.observe_rank(4, 700.0), Some(RankChange::Climbed(4))); + let again = commit(&mut checkpointer, SaveKind::Milestone(4), 13); + assert_eq!((again.durable, again.archive_rank), (true, None)); + assert_eq!(store::load(&checkpointer.durable().archive_path(4)).unwrap().runtime.generation, 44); + + for boundary in 14..20 { + commit(&mut checkpointer, SaveKind::Durable, boundary); + } + let manifest = checkpointer.durable().manifest().unwrap(); + assert_eq!(manifest.archives.get(&4), Some(&44)); + assert!(checkpointer.durable().generation_path(44).is_file(), "an archived generation stays"); + assert!(!checkpointer.durable().generation_path(46).exists(), "rotated away"); + assert_eq!(manifest.previous.map(|p| p + 1), manifest.latest); + checkpointer.close(); + + // The next process allocates above everything either store holds. + let checkpointer = LegacyCheckpointer::open(config(dir.path())).unwrap(); + assert_eq!(checkpointer.next_generation(), manifest.latest.unwrap() + 1); + } + + #[test] + fn the_intervals_are_the_legacy_loops() { + let dir = tempfile::tempdir().unwrap(); + let mut checkpointer = LegacyCheckpointer::open(config(dir.path())).unwrap(); + let start = Instant::now(); + checkpointer.start_intervals(start); + assert_eq!(checkpointer.due(start + Duration::from_secs(1)), None); + assert_eq!(checkpointer.due(start + Duration::from_secs(5)), Some(SaveKind::Hot)); + assert_eq!(checkpointer.due(start + Duration::from_secs(6)), None); + assert_eq!(checkpointer.due(start + Duration::from_secs(10)), Some(SaveKind::Hot)); + // The durable interval wins and restarts the hot one. + let at = start + Duration::from_secs(300); + assert_eq!(checkpointer.due(at), Some(SaveKind::Durable)); + assert_eq!(checkpointer.due(at + Duration::from_secs(4)), None); + assert_eq!(checkpointer.due(at + Duration::from_secs(5)), Some(SaveKind::Hot)); + } + + fn gate() -> RestoreGate { + RestoreGate { + rom_sha256: "ab".repeat(32), + compatibility: "kernel/adapter/fingerprint".to_owned(), + migrates_from: Vec::new(), + accepted: Vec::new(), + } + } + + fn select( + checkpointer: &LegacyCheckpointer, + gate: &RestoreGate, + refuse: &[u64], + ) -> Selection { + let refuse = refuse.to_vec(); + let runtime = tokio::runtime::Builder::new_current_thread().build().unwrap(); + runtime.block_on(restore_from_store(checkpointer.hot(), checkpointer.durable(), gate, |c| { + let refuse = refuse.clone(); + async move { + if refuse.contains(&c.runtime.generation) { + Err(format!("participant refused generation {}", c.runtime.generation)) + } else { + Ok(c.runtime.generation) + } + } + })) + } + + #[test] + fn restore_walks_the_legacy_order_and_takes_the_next_candidate_on_any_refusal() { + let dir = tempfile::tempdir().unwrap(); + let mut checkpointer = LegacyCheckpointer::open(config(dir.path())).unwrap(); + assert!(matches!(select(&checkpointer, &gate(), &[]), Selection::Fresh)); + commit(&mut checkpointer, SaveKind::Milestone(3), 10); // 1, archive 3 + commit(&mut checkpointer, SaveKind::Durable, 11); // 2 + commit(&mut checkpointer, SaveKind::Durable, 12); // 3 + commit(&mut checkpointer, SaveKind::Hot, 13); // 4 + commit(&mut checkpointer, SaveKind::Hot, 14); // 5 + + let Selection::Restored(first) = select(&checkpointer, &gate(), &[]) else { panic!() }; + assert_eq!((first.installed, first.candidate.origin.as_str()), (5, "hot latest generation 5")); + assert!(first.skipped.is_empty()); + + // A torn hot latest, then a participant that refuses hot previous: durable latest. + std::fs::write(checkpointer.hot().generation_path(5), b"torn").unwrap(); + let Selection::Restored(next) = select(&checkpointer, &gate(), &[4]) else { panic!() }; + assert_eq!(next.installed, 3); + assert_eq!(next.skipped.len(), 2); + // Everything above the archive refused: the archive's generation, then its own file. + let Selection::Restored(deep) = select(&checkpointer, &gate(), &[2, 3, 4]) else { panic!() }; + assert_eq!(deep.installed, 1); + assert_eq!(deep.candidate.origin, "durable archived generation 1 (rank 3)"); + + // Another cartridge or another brain: refused by name, and nothing restores. + let other = RestoreGate { rom_sha256: "cd".repeat(32), ..gate() }; + let Selection::Refused(reasons) = select(&checkpointer, &other, &[]) else { panic!() }; + assert!(reasons.iter().all(|(_, why)| why.contains("another cartridge") || why.contains("decoding"))); + let newer = RestoreGate { compatibility: "kernel/adapter-v2/fingerprint".to_owned(), ..gate() }; + assert!(matches!(select(&checkpointer, &newer, &[]), Selection::Refused(_))); + // The operator's adapter migration is the legacy decision, unchanged. + let migrating = RestoreGate { + compatibility: "kernel/adapter-v2/fingerprint".to_owned(), + migrates_from: vec!["adapter".to_owned()], + accepted: vec!["adapter".to_owned()], + ..gate() + }; + let Selection::Restored(migrated) = select(&checkpointer, &migrating, &[]) else { panic!() }; + assert_eq!(migrated.migrated_from.as_deref(), Some("adapter")); + } +} diff --git a/services/flysim/crates/fly-session/src/legacy_parity.rs b/services/flysim/crates/fly-session/src/legacy_parity.rs index 785b7b7..07af25f 100644 --- a/services/flysim/crates/fly-session/src/legacy_parity.rs +++ b/services/flysim/crates/fly-session/src/legacy_parity.rs @@ -1450,10 +1450,10 @@ impl AgentDriver { /// the loop's frame remainder in it -- instead of from a warm-up, the way the stream restores /// after a restart (`legacy-transient-reset`). /// - /// This is the test side of an import nothing ships yet: the worker is initialized, its own - /// capture is taken as the template for the session member, the agent chunks and the clock - /// are replaced with `state`'s, and the result is restored into a replacement worker through - /// the ordinary `State.StageRestore` / `ActivateRestore`, which validates it like any other + /// The payload is the shipped `FLYSIM01` import's (`legacy_checkpoint::agent_payload`, STATE-02, + /// which replaced AGENT-01's prototype here): the worker is initialized, its capture names the + /// checkpoint and scope, and the import is restored into a replacement worker through the + /// ordinary `State.StageRestore` / `ActivateRestore`, which validates it like any other /// payload. `learning.updates` starts at [`legacy_reinforcements`], because `FLYSIM01` does /// not record every reinforcement call. pub async fn seed_from_legacy_state( @@ -1465,7 +1465,25 @@ impl AgentDriver { ) -> Result { self.initialize(initial_frame).await?; let template = self.capture().await?; - let bytes = legacy_state_payload(&template.bytes, state, &self.context)?; + // The shipped import (STATE-02, `legacy_checkpoint::agent_payload`); the template capture + // only supplies the checkpoint, the scope and the composition's macro channels. + let macro_channels: Vec = + serde_json::from_value(captured_session(&template.bytes)?["macroChannels"].clone()) + .map_err(|e| format!("the template's macro channels: {e}"))?; + let import = crate::legacy_checkpoint::AgentImport { + agent_id: self.agent_id.clone(), + profile: self.profile.asset.clone(), + seed: LEGACY_SEED, + macro_channels, + }; + let bytes = crate::legacy_checkpoint::agent_payload( + state, + &self.frames[initial_frame], + &import, + &template.result.checkpoint_id, + &template.scope, + &self.context, + )?; let digest = digest_of_bytes(&bytes); let artifact = crate::state::seal_payload(&self.client, &bytes, &digest) .await @@ -1483,7 +1501,11 @@ impl AgentDriver { self.restore_into(target, &seeded).await?; let after = self.capture().await?; let restored = payload_state_digest(&after.bytes)?; - if restored != state_digest(state) { + // Exact but for the visual drive, which the restore re-projects from the frame on screen + // as `LegacyFrame::restore` does (the direct reference does the same). + let mut expected = crate::legacy_checkpoint::agent_state(&after.bytes)?; + expected.network.visual_drive = state.network.visual_drive.clone(); + if state_digest(&expected) != state_digest(state) { return Err("the seeded worker's state is not the legacy state".to_owned()); } self.remainder = legacy_remainder_to_rational(state.remainder)?; @@ -1502,39 +1524,8 @@ impl AgentDriver { } } -/// The reinforcement calls to start a legacy agent state's `learning.updates` at. -/// -/// `FLYSIM01` records `plasticity.updates`, the reinforcements that moved a gain, but not every -/// call, and the contract requires `learning.changed <= learning.updates`. An import that starts -/// the call count at zero makes the first telemetry unreadable (found on the row-58 stream -/// checkpoint: `changed` is in the thousands), so it starts at the recorded count, a lower bound -/// on the calls actually made. -pub fn legacy_reinforcements(state: &flybrain_core::agent::AgentState) -> u64 { - state.network.plasticity.updates as u64 -} - -/// A worker capture with its agent chunks and clock replaced by a legacy agent state, and its -/// context by `context`: see [`AgentDriver::seed_from_legacy_state`]. -pub fn legacy_state_payload( - template: &[u8], - state: &flybrain_core::agent::AgentState, - context: &TypedValue, -) -> Result, String> { - let mut session = captured_session(template)?; - let remainder = legacy_remainder_to_rational(state.remainder)?; - session["accumulator"]["remainder"] = remainder.to_json(); - session["accumulator"]["executedTicks"] = json!((state.network.ms as u64).to_string()); - session["reinforcements"] = json!(legacy_reinforcements(state).to_string()); - session["context"] = context.to_json(); - let chunks = agent_to_chunks(state); - let mut manifest = chunks.manifest; - manifest.set( - "session", - flybrain_core::json::JsonValue::parse(&session.to_string()).map_err(|e| e.to_string())?, - ); - flybrain_core::envelope::encode_envelope(PAYLOAD_MAGIC, &manifest, &chunks.chunks) - .map_err(|e| e.to_string()) -} +/// The shipped import's rule (`crate::legacy_checkpoint`), re-exported for the harness. +pub use crate::legacy_checkpoint::legacy_reinforcements; /// Runs a script against the rig's agent, recording what the coordinator observes. pub async fn run_on_worker( @@ -1553,6 +1544,30 @@ pub async fn run_on_worker_from( script: &LegacyScript, start: Option<&flybrain_core::agent::AgentState>, ) -> Result, String> { + run_script(rig, agent_id, script, start, false) + .await + .map(|(records, _)| records) +} + +/// [`run_on_worker_from`], then one `State.Capture` at the boundary the script ends on: the +/// records and the captured payload (STATE-02 writes a `FLYSIM01` export from it). +pub async fn run_on_worker_from_then_capture( + rig: &mut LegacyRig, + agent_id: &Id, + script: &LegacyScript, + start: Option<&flybrain_core::agent::AgentState>, +) -> Result<(Vec, Vec), String> { + let (records, captured) = run_script(rig, agent_id, script, start, true).await?; + Ok((records, captured.expect("a capture was asked for"))) +} + +async fn run_script( + rig: &mut LegacyRig, + agent_id: &Id, + script: &LegacyScript, + start: Option<&flybrain_core::agent::AgentState>, + capture_at_end: bool, +) -> Result<(Vec, Option>), String> { let mut driver = rig.driver(agent_id, script); let first = match start { None => driver.initialize(script.initial_frame).await?, @@ -1604,7 +1619,12 @@ pub async fn run_on_worker_from( } records.push(record); } - Ok(records) + let captured = if capture_at_end { + Some(driver.capture().await?.bytes) + } else { + None + }; + Ok((records, captured)) } fn captured_session(payload: &[u8]) -> Result { diff --git a/services/flysim/crates/fly-session/src/lib.rs b/services/flysim/crates/fly-session/src/lib.rs index 51ae391..63edd03 100644 --- a/services/flysim/crates/fly-session/src/lib.rs +++ b/services/flysim/crates/fly-session/src/lib.rs @@ -30,6 +30,7 @@ pub mod environment; pub mod harness; pub mod launcher; pub mod legacy_agent; +pub mod legacy_checkpoint; pub mod legacy_env; pub mod legacy_env_parity; pub mod legacy_parity;