[M1-DET-03] State hashing + replay runner (World::stateHash, runReplay, laige-replay) - #38
Merged
Merged
Conversation
…y, laige-replay) M1-DET-03 (roadmap/M1-heartbeat.md, FR-1.4/FR-11.3, TEST-004, ADR 0002, ARCH-010, CORE-008): the replay EXECUTION half, on top of M1-DET-02's recording and M1-ECS-05's deterministic iteration. - src/laige-sim/include/laige/sim/entity.h + state_hash.cpp: World::stateHash(tick) — the deterministic 64-bit FNV-1a hash of the live sim state: tick, live handles (slot+generation), per non-empty archetype in lexicographic signature order (component ids ascending, row count, raw component bytes row-major in signature column order), per-system PRNG substream state (seed + state words). A pure function of the live state — convergent worlds hash identically (dead-slot generations, the free-list order, empty archetypes, the registry, capacity, presentation, timing, guardrail counters, and the master seed are deliberately out — the scope in/out lists are documented in the header). Word-stream FNV-1a 64, big-endian per u64 (the house convention); component bytes raw memory order. Cold path: O(capacity + live bytes + kMaxArchetypes^2), no allocation, const, no logging. - src/laige-core/include/laige/prng.h: Prng::statePart1()/statePart2() read-only accessors (the substream state words that join stateHash). - src/laige-sim/include/laige/sim/replay.h + replay.cpp: ReplayIdentityDiff + replayIdentityDiff (ADR 0002 enforced field by field — seed, tick rate, component schema hash, math backend id, config hash — a mismatch is a rejected replay, never silent) and runReplay (identity check -> determinism check -> schedule once -> one beginFrame()+runSystems per recorded tick; the result is the per-tick state-hash stream, size frameCount+1, tick 0 first). Structured events: replay/identity_mismatch, replay/determinism_disabled. - tools/replay/laige-replay.cpp + CMakeLists.txt: the runner. laige-replay --log LOG --config CFG [--expect BASELINE]; stdout is ONLY the "<tick> <hash>" lines (16 lowercase hex; a redirect captures a clean baseline); summary and diagnostics on stderr. Exit 0 = ok/matched, 1 = first-divergence report (the baseline and replay values at the tick) or stream-length report, 2 = usage/IO/ config/log/identity/baseline-grammar errors. Bounded reads (config 1 MiB, baseline 8 MiB / 65536 lines / 64 bytes per line). - tests/laige-sim/replay_replay_tests.cpp (ctest -R replay_replay): StateHash.* — the KAT, capacity/handle/component/archetype/PRNG sensitivity, convergent-world equality, the zero-allocation proof (alloc counter, LAIGE_ALLOC_COUNTER builds). DetReplay.* — the 500-tick record -> replay integration (identical hash streams), a perturbed world diverging at the exact tick, the per-field identity rejection, the determinism-disabled rejection, and an engine round trip (record under run_headless; replay through two fresh engines; world-level twin state comparison). - tests/replay/ (ctest -R "^replay_"): nine generated check scripts driving the real runner binary — smoke (stream contract), deterministic (double-run), --expect match / perturbed / truncated / malformed, identity mismatch (field report), usage error, missing log — asserting exit codes 0/1/2, the stdout contract, and the report fragments. - docs: api/replay.md (the execution half: state hash, identity check, runReplay, the runner, the hash line contract, the testing), api/entity.md (the stateHash section), api/prng.md (statePart1/2), api/engine.md, api/detcheck.md, api/iteration_order.md, concepts/determinism.md, getting-started/building.md (the runner row), docs/README.md, src/laige-sim/README.md; laige-api.json regenerated (644 symbols); roadmap M1-DET-03 checked. Verified: ctest -R replay_replay green (13 tests); the perturbed baseline makes laige-replay --expect fail at the correct tick with the first-divergence report (replay_expect_perturbed + DetReplay.PerturbedDivergesAtTick7); full ctest green on all three trees — build/ 67/67, build-asan (ASan+UBSan) 67/67, build-tsan 67/67. No hot-path work added: the engine per-tick path is unchanged (stateHash is cold, called only by the replay/detcheck paths); the recording null-check cost is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
M1-DET-03 · State hashing + replay runner
The replay execution half, on top of M1-DET-02's recording and M1-ECS-05's deterministic iteration (roadmap
M1-DET-03; FR-1.4/FR-11.3, TEST-004, ADR 0002, ARCH-010, CORE-008).What lands
World::stateHash(tick)(entity.h+state_hash.cpp) — the deterministic 64-bit FNV-1a hash of the live sim state: tick, live handles (slot+generation), per non-empty archetype in lexicographic signature order (component ids ascending, row count, raw component bytes row-major in signature column order), per-system PRNG substream state (seed + state words). A pure function of the live state — convergent worlds (same live state, different histories) hash identically; dead-slot generations, the free-list order, empty archetypes, the registry, capacity, presentation, timing, guardrail counters, and the master seed are deliberately excluded (scope in/out documented in the header). Word-stream FNV-1a 64, big-endian per u64 (house convention); cold path: no allocation,const, no logging, never on the engine per-tick path.Prng::statePart1()/statePart2()— read-only accessors for the substream state words that join the hash.replayIdentityDiff+runReplay(replay.h/replay.cpp) — ADR 0002 enforced field by field (seed, tick rate, component schema hash, math backend id, config hash): a mismatch is a rejected replay (replay/identity_mismatchwarn naming the fields), never a silent divergence; then determinism check → schedule once → onebeginFrame()+runSystemsper recorded tick; the result is the per-tick state-hash stream (sizeframeCount+1, tick 0 first).laige-replay(tools/replay/) —laige-replay --log LOG --config CFG [--expect BASELINE]. stdout is only the<tick> <hash>lines (16 lowercase hex — a redirect captures a clean baseline); summary/diagnostics on stderr. Exit0ok/matched,1first-divergence or stream-length report,2usage/IO/config/log/identity/baseline-grammar errors. Bounded reads (config 1 MiB; baseline 8 MiB / 65536 lines / 64 bytes per line).tests/laige-sim/replay_replay_tests.cpp(ctest -R replay_replay, 13 tests: the state-hash KAT, sensitivity matrix, convergent-world equality, zero-allocation proof; the 500-tick record → replay integration, perturbed-world divergence at the exact tick, per-field identity rejection, determinism-disabled rejection, engine round trip) +tests/replay/(9 CTest check scripts driving the real binary: stream contract, double-run,--expectmatch/perturbed/truncated/malformed, identity mismatch, usage, missing log).api/replay.md(execution half + hash line contract),api/entity.md,api/prng.md,api/engine.md,api/detcheck.md,api/iteration_order.md,concepts/determinism.md,getting-started/building.md(runner row),docs/README.md,src/laige-sim/README.md;laige-api.jsonregenerated (644 symbols); roadmap box ticked.Verification
ctest -R replay_replaygreen (13/13).laige-replay --expectfails at the correct tick with the actionable first-divergence report (replay_expect_perturbed— tick 5;DetReplay.PerturbedDivergesAtTick7).api-real-tree,include-lint-real-tree,determinism-lint-real-treegreen.Performance
No hot-path work added: the engine per-tick path is unchanged (
stateHashis cold — called only by the replay/detcheck paths, once per tick there); the opt-in recording null-check cost is unchanged. The new per-tick cost exists only when actively replaying (the sim tick itself + one cold state hash).Out of scope (next steps)
World::stateHashwith that wiring).laige-replay --diff.