Status: done (2026-11-09)
Priority: 1
Size: S
Arch: yardstick
Phase: implement
Design ref: none (code structure). docs/roadmap.md, sections 5.2 (save and load under 0.5 s) and 5.3 (rule 9, bound history: routine events fold into yearly counts); VB-174 (sim/stores), docs/review-vb174.md.
Goal: VB-174 made sim/stores write a ledger entry for every food movement and keep it three years. By its own measure, at year 8 of populous small:
Counted on 7 November (populous small, seed 7, at year 4: 87 occupied households, 15 villages), 63,640 entries were kept, 4.3 MB of JSON. 95% of them are routine:
Who reads the ledgers:
tools/balance.js: harvest, gift, meat, milk and butter by kind over a year;sim/herds: milk and butter this year;tests/stores.js reconciles each holder’s year, and help, milk, regress and weeding read entries.Fold the routine kinds into yearly totals and keep the others in full. Acceptance criteria:
meal, cooking, spoilage, thresh, milk, butter) add to a yearly total per holder, kind and good (an amount and a count), instead of writing an entry each. Other kinds stay entries, as today. The totals are kept for the same three years as the entries and pruned with them. sim/stores owns them (src/owners.json), with an @state line and invariants in checkState.tests/stores.js still reconciles every holder’s year: the change in its quantities equals its entries plus its totals.tools/balance.js --json (populous, seeds 7 and 42, small, 8 years) and the herd summary give the same numbers before and after.medium-ref if VB-202 is done.npm run verify passes.
Cost: one addition per routine movement, into a total found by kind, with no allocation per movement. About a dozen totals per holder-year instead of about 200 entries.
Notes:sim/stores: ROUTINE kinds (meal, cooking, spoilage, thresh, milk, butter) of a household or village add to holder.totals, {year, kind, good, n, count}: the calendar year of the hour the clock is working through (yearOf, as cal(hour).year, so midnight’s work at the year’s turn belongs to the new year). The search runs back over this year’s totals only; a total is allocated once per holder, year, kind and good. A pawn’s stake movement of a routine kind would stay an entry (none exist today).prune walks st.liveHouseholds and the villages, and drops entries and totals older than the last three years. close empties a household’s ledger and totals when it dissolves (sim/heirs, extinct): nothing can reach a dissolved holder afterwards, since type refuses it.totals is owned by sim/stores (src/owners.json), with @state lines and invariants in checkState (routine kinds only, one per year, kind and good, in year order, within the last three years; a dissolved household holds none). Older saves load with empty totals and keep their routine entries until they are pruned; their dissolved households’ ledgers are emptied on load (tests/fixtures.js found them in the published fixtures).sim/herds) and tools/balance.js (Stores.yearSum) read milk and butter from the totals; tests/milk.js counts milking days from a total’s count.tests/stores.js reconciles every holder from the seed to noon of each of three new years’ first days (away from midnight at the year’s turn): its change in grain, rations and sheaves equals its entries plus its totals. Before, it reconciled each year from a snapshot at the year’s turn, which a yearly total cannot split.tools/balance.js --scenario populous --seeds 7,42 --map small --years 8 --json and tools/sim.js --report herds --json (populous 7, small, 8 years): byte-identical before and after. The yearly tools/sim.js lines are identical but for ms a day.households and villages of all 16 states (founders and heirs, seeds 7 and 42, the seed and years 1 to 3). With the ledger and totals lines left out, the full dumps are identical.medium-ref, remade at this commit (year 12, 1,026 living, as before): 155,173 entries, 228 per occupied household-year, became 9,420 entries and 5,727 totals, 22. Household ledgers 10.2 MB, now 1.0 MB with the totals. Save 39.3 MB of JSON and 6.9 MB gzipped, now 27.8 and 5.4. Four alternating paired runs of the same fixtures (medians of three each): main-thread save 834 to 1,053 ms before and 576 to 987 after (medians of the four, about 904 and 777); gzip about 557 and 432 ms; parse and load about 398 and 334 ms. The save’s main thread fell less than its size: VB-226 measures where the rest goes.npm test and npm run verify pass.