Skip to content

Refuse a save whose id counters lag its ids or whose stockpile overflows 32 bits - #37

Draft
dmccoystephenson wants to merge 2 commits into
mainfrom
feat/refuse-lagging-counters-wide-stockpile
Draft

dmccoystephenson wants to merge 2 commits into
mainfrom
feat/refuse-lagging-counters-wide-stockpile

Conversation

@dmccoystephenson

Copy link
Copy Markdown
Member

Summary

Two more range checks are added to bridge::fromWorld, in the "Range checks the rules rely on" block. Both refuse a save that hashes correctly but holds an impossible value. The live colony and the slot are left untouched, and the save format does not change.

  • Id counters (A save whose id counters lag its ids loads, then crashes at the first birth or hut #29). A save is refused with id counter behind ids when colony:nextSettler or colony:nextBuilding is negative, or is not greater than the last id in its table. Before this change such a save loaded, and the first birth (systems::growth) or hut (systems::build, planHut) then threw an uncaught std::logic_error from Settlers::add/Buildings::add. The negative-counter condition covers an empty table, where "greater than the last id" says nothing. This follows the corrected expectation in the issue's comment. No check is added for strictly increasing ids, because Strata's hydrate already sorts rows by id and refuses duplicates.
  • Stockpile bounds at 64 bits (A save with a stockpile position beyond 32 bits passes the bounds check truncated #31). colony:stockX/colony:stockY are checked with fits before the 32-bit cast. A value such as 2^32 + 5 therefore no longer wraps back onto the map, and it is refused with the existing stockpile out of bounds error.

Saves written by the game satisfy both conditions: every id comes from the counters, and generate() places the stockpile in bounds. test_save_fixtures checks this against the released fixtures in CI.

Regression evidence

The tests were pushed in a commit of their own (af02172) before the fix. The game on Strata CI run on that commit is expected to fail on exactly the new CHECKs, and the fix commit is expected to turn it green. Both runs are linked from the self-review comment.

Test plan

  • rules (gcc-13), rules (clang-18), rules (asan-ubsan), rules (msvc) green (no rules code changes)
  • game on Strata (saves soak, headless smoke) red on the tests-only commit, naming the new checks
  • game on Strata green on the fix commit: test_saves (lossless section with the new cases, 1,000-tick soak) and test_save_fixtures
  • No existing CHECK removed or weakened; schema.h, strata.ref and the pinned fingerprint are untouched

The local build could not be run: the sandbox for this session blocks cmake. CI on the PR head SHA is the only verification, and all changes are UNVERIFIED-locally.

Closes #29
Closes #31

Deferred this cycle: #24 (F9 reload and --force-mods, in app/main.cpp), #25 (CLI exit codes, across three tools) and #30 (NaN prefab stats, in readStats). Each touches different code from this load-validation batch and is left for a later cycle. Draft PR #35 (Strata pin bump) is waiting for the owner and was not touched.

This PR description was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

dmccoystephenson and others added 2 commits November 20, 2026 03:00
These cases fail against the current fromWorld: a save whose
nextSettler/nextBuilding counter is at or behind its ids (or negative)
loads, and a stockpile 2^32 off the map wraps back onto it. Pushed
ahead of the fix so CI records the failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fromWorld now refuses a save whose nextSettler/nextBuilding counter is
negative or not greater than the last id in its table ("id counter
behind ids"), which would otherwise load and then throw at the first
birth or hut. The stockpile position is checked with fits() before its
32-bit cast, so a value 2^32 off the map no longer wraps back onto it.
The save format is unchanged.

Closes #29
Closes #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dmccoystephenson

Copy link
Copy Markdown
Member Author

Self-review rubric (head 3ed06c4, all CI jobs green in run 44716357271):

  • Scope: PASS. Two files are changed: src/bridge/bridge.cpp (+8/−1, the range-check block in fromWorld) and tests/test_saves.cpp (+38). Both are needed for A save whose id counters lag its ids loads, then crashes at the first birth or hut #29/A save with a stockpile position beyond 32 bits passes the bounds check truncated #31, and the diff has no unrelated churn.
  • Tests-new: PASS. No new public function is added. The new checks are exercised through bridge::fromWorld in the lossless section of test_saves.
  • Tests-fix: PASS, shown empirically on CI with the tests committed first. On the tests-only commit af02172 (run 44715609381), game on Strata failed on exactly the new checks, 18 failures: the four lagging-counter cases, the empty-table negative-counter case, and the four stockpile values off by ±2^32 (test_saves.cpp:91-92, :101-102, :112-113). The two 9999 off-map stockpile cases passed before the fix, as A save with a stockpile position beyond 32 bits passes the bounds check truncated #31 predicted. On the fix commit 3ed06c4, the same job passes with 100% tests passed, 0 tests failed out of 9 on both Linux and Windows MSVC. Rung 1 of the ladder (a temporary revert) was not used. The tests went red first and were not reverted afterwards, so no head on the branch ever undid the fix.
  • Sibling structure: PASS. The new cases follow the existing bad = bridge::toWorld(c); ...; CHECK(!fromWorld); CHECK_MSG(why == ...) pattern in the same section. The new error string matches the style of its siblings ("stockpile out of bounds", "settler state out of range").
  • Sibling renames: PASS. Nothing is renamed.
  • Docs: PASS. The Phase 7 table was checked. README.md documents no load-refusal strings, and no control, flag, rule or mod key changed. src/mods/manifest.h, schema.h, content/prefabs.usda, mods/*/mod.toml and LICENSE are untouched. No correction was needed.
  • Issue resolution: PASS. A save whose id counters lag its ids loads, then crashes at the first birth or hut #29: counters are refused when they are negative or not greater than the last id, as amended in the issue's comment. The strictly-increasing-ids check was left out on purpose, because Strata's hydrate already guarantees it. A save with a stockpile position beyond 32 bits passes the bounds check truncated #31: fits runs on stockX/stockY before the 32-bit cast, and the error string is unchanged.
  • CI: PASS. On 3ed06c4, rules (gcc-13/clang-18/asan-ubsan/msvc) (6 tests each), game on Strata (saves soak, headless smoke) and game on Strata (Windows, MSVC) (9 tests each), and the web build are all green.
  • Privacy: PASS. The diff, commits and PR text name only Preponderous-Software/steading and Strata (both public), and contain no employer or job references.
  • Save integrity: PASS. test_saves (lossless section, 1,000-tick soak, restart, fallback) and test_save_fixtures (released fixtures still load) are green. The diff removes no CHECK (grep -c '^-.*CHECK' → 0).
  • Fingerprint: PASS. tests/test_determinism.cpp is untouched.
  • Schema untouched: PASS. git diff origin/main...HEAD -- src/colony/schema.h strata.ref is empty.
  • Layering: PASS for this diff, which touches nothing under src/colony or src/mods. The rubric's grep -rn "strata/" src/colony does print two lines, from src/colony/generate.cpp:7/:14 (strata/sim/noise.hpp). These are already on main (Cut terrain and forest from Strata's fractal noise #33) and are outside this PR.
  • Docs follow the change: PASS. Nothing user-facing changed.

The local build was not run: the session sandbox blocks cmake. CI on the exact head SHA serves as the anchor. Valid saves are unaffected: the game takes every id from its counter (G.nextSettlerId++, G.nextBuildingId++) and places the stockpile with generate(), and the released fixtures load.

This review was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

@dmccoystephenson

Copy link
Copy Markdown
Member Author

This PR meets the merge bar for this loop: every CI job is green on 3ed06c4, the regression was shown red before the fix and green after it, the self-review is posted above, and no protected path is touched. It is still a draft only because the PR was opened as a draft (so CI could run on the tests-only commit), and gh pr ready was not available in the session that opened it. Marking it ready for review and squash-merging is all that remains.

This comment was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant