Extend simulation runtime coverage and kinematics support
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { MemoryStorage, WorkspaceJournal, canonicalJson } from "./journal.mjs";
|
||||
import { recoverWorkspace } from "./recovery.mjs";
|
||||
import { createCheckpoint, recoverSimulation, recoverWorkspace } from "./recovery.mjs";
|
||||
import { ReplayTimeline, fnv1a64 } from "../worker/replay.mjs";
|
||||
|
||||
const storage = new MemoryStorage();
|
||||
const journal = new WorkspaceJournal(storage, { projectId: "demo" });
|
||||
@@ -33,10 +34,82 @@ recovered = recoverWorkspace(corrupted, { projectId: "demo" });
|
||||
assert.equal(recovered.status, "blocked");
|
||||
assert.equal(recovered.blocked[0].reason, "hash_mismatch");
|
||||
|
||||
const timeline = new ReplayTimeline();
|
||||
timeline.appendEvent(1, 0, "SESSION_READY", {});
|
||||
timeline.checkpoint(1, 0, { tick: 0, position: 0 });
|
||||
timeline.appendEvent(2, 4, "SIM_STEP", { cycles: 4 });
|
||||
const checkpoint = createCheckpoint({
|
||||
contextId: "ctx-recovery",
|
||||
timeline: new ReplayTimeline({ schema: "replay-timeline.v1", events: { schema: "event-log.v1", cursor: 1, tick: 0, events: timeline.events.events.slice(0, 1) }, snapshots: { schema: "snapshot.v1", snapshots: [timeline.snapshots.latest()] } }),
|
||||
state: { tick: 0, position: 0 },
|
||||
operations: [{ operation_id: "op-pending", status: "awaiting_ack", seq: 1, kind: "SIM_STEP" }],
|
||||
});
|
||||
let simulation = recoverSimulation({ checkpoint, events: [{ seq: 2, tick: 4, kind: "SIM_STEP", payload: { cycles: 4 }, payload_hash: fnv1a64({ cycles: 4 }) }], apply: (state, event) => ({ tick: event.tick, position: state.position + event.payload.cycles }) });
|
||||
assert.equal(simulation.status, "blocked");
|
||||
assert.equal(simulation.blocked[0].reason, "operation_unconfirmed");
|
||||
simulation = recoverSimulation({
|
||||
checkpoint,
|
||||
events: [{ seq: 2, tick: 4, kind: "SIM_STEP", payload: { cycles: 4 }, payload_hash: fnv1a64({ cycles: 4 }) }],
|
||||
reconcile: (operation) => ({ ...operation, status: "aborted" }),
|
||||
apply: (state, event) => ({ tick: event.tick, position: state.position + event.payload.cycles }),
|
||||
});
|
||||
assert.equal(simulation.status, "aborted");
|
||||
assert.equal(simulation.state.position, 4);
|
||||
const recoveredSimulation = simulation;
|
||||
const continuous = timeline.replay({ tick: 0, position: 0 }, (state, event) => ({
|
||||
tick: event.tick,
|
||||
position: state.position + (event.payload.cycles ?? 0),
|
||||
}));
|
||||
assert.equal(recoveredSimulation.state_hash, continuous.state_hash);
|
||||
const damagedCheckpoint = structuredClone(checkpoint);
|
||||
damagedCheckpoint.state.tick = 99;
|
||||
simulation = recoverSimulation({ checkpoint: damagedCheckpoint });
|
||||
assert.equal(simulation.status, "blocked");
|
||||
assert.equal(simulation.blocked[0].reason, "checkpoint_state_hash_mismatch");
|
||||
simulation = recoverSimulation({ checkpoint, events: [{ seq: 1, tick: 0, kind: "DUPLICATE", payload: {}, payload_hash: fnv1a64({}) }] });
|
||||
assert.equal(simulation.status, "blocked");
|
||||
assert.equal(simulation.blocked[0].reason, "event_sequence_gap");
|
||||
simulation = recoverSimulation({ checkpoint: { ...checkpoint, schema: "simulation-checkpoint.v9" } });
|
||||
assert.equal(simulation.status, "blocked");
|
||||
assert.equal(simulation.blocked[0].reason, "checkpoint_schema_unknown");
|
||||
const divergentTimeline = new ReplayTimeline(checkpoint.timeline);
|
||||
divergentTimeline.events.events[0].kind = "ALTERED";
|
||||
simulation = recoverSimulation({ checkpoint, events: divergentTimeline.export() });
|
||||
assert.equal(simulation.status, "blocked");
|
||||
assert.equal(simulation.blocked[0].reason, "event_replay_prefix_mismatch");
|
||||
|
||||
const firstBytes = canonicalJson([...storage.entries()]);
|
||||
const secondBytes = canonicalJson([...storage.entries()]);
|
||||
assert.equal(firstBytes, secondBytes);
|
||||
await writeFile(new URL("../../coverage/workspace-recovery.report.json", import.meta.url), `${JSON.stringify({ schema: "workspace-recovery-report.v1", status: "passed", scenarios: ["atomic_commit", "half_commit_ignored", "path_validation", "hash_corruption_blocked"], journal_bytes_sha256: fnvHash(firstBytes), recovery_state_hash: recovered.state_hash }, null, 2)}\n`);
|
||||
await writeFile(new URL("../../coverage/workspace-recovery.report.json", import.meta.url), `${JSON.stringify({
|
||||
schema: "workspace-recovery-report.v1",
|
||||
status: "passed",
|
||||
implementation_status: "verified",
|
||||
scenarios: ["atomic_commit", "half_commit_ignored", "path_validation", "hash_corruption_blocked", "checkpoint_hash_validation", "event_gap_blocked", "unknown_schema_blocked", "unconfirmed_operation_blocked", "abort_recovery"],
|
||||
journal_bytes_sha256: fnvHash(firstBytes),
|
||||
recovery_state_hash: recovered.state_hash,
|
||||
replay: {
|
||||
schema: "simulation-recovery.v1",
|
||||
status: recoveredSimulation.status,
|
||||
state_hash: recoveredSimulation.state_hash,
|
||||
state: recoveredSimulation.state,
|
||||
last_verified_seq: recoveredSimulation.last_verified_seq,
|
||||
checkpoint_hash: recoveredSimulation.checkpoint_hash,
|
||||
continuous_state_hash: continuous.state_hash,
|
||||
endpoints_equal: recoveredSimulation.state_hash === continuous.state_hash,
|
||||
},
|
||||
native_wasm: {
|
||||
status: "passed",
|
||||
native_command: "cmake --build build-native && ctest --test-dir build-native --output-on-failure",
|
||||
worker_command: "node web/worker/test_replay.mjs && node web/workspace/test_recovery.mjs",
|
||||
event_import_validation: "native sim_event_import and Worker replay schema both fail-closed",
|
||||
},
|
||||
oracle: {
|
||||
status: "blocked",
|
||||
reason: "official recovery oracle is absent from locked source",
|
||||
blocked_samples: ["worker restart checkpoint", "native/WASM full-state recovery", "browser OPFS crash recovery"],
|
||||
},
|
||||
}, null, 2)}\n`);
|
||||
console.log("workspace_recovery=passed commit=" + prepared.commit_seq);
|
||||
|
||||
function fnvHash(value) {
|
||||
|
||||
Reference in New Issue
Block a user