Status: done (2026-11-10)
Priority: 2
Size: M
Design ref: none (code structure). AGENTS.md, Conventions (Determinism; Time is in game hours).
Goal: Sim.advance (src/sim/clock.js) adds 0.1 to the float st.t and finds the hour as Math.floor(st.t + 1e-9). The float drifts past that tolerance in year 2, and left -= step drifts with it, so the same seed gives different runs by the size of the chunks advance is given. Keep an integer count of six-minute ticks as the clock’s source of truth and derive the hour, the day and st.t from it.
Acceptance criteria:
st.tick, an integer count of six-minute steps since the run began, is the clock’s source of truth; st.t is written in one place, from it (st.tick / TICKS, plus the part of a step the page has asked for so far). An hour begins when st.tick % TICKS === 0; nothing finds the hour, the tick or the year by adding a tolerance to a float: the six sites go (clock.js:89-93, agents.js:180, stores.js:8, page/animate.js:69). An animal’s age in years is likewise compared from whole ticks: the four tolerances of 1e-6 in sim/herds go (herds.js:75, 87, 105, 293; Kim, 10 Nov). The eight tolerances on quantities (help, hunger, claims, exchange, fields) are not this task’s.Sim.advance(st, hours) with a whole number of ticks runs exactly that many full steps. A test in npm test holds that populous, seed 7, tiny map, 2 years is the same state (the canonical hash of tests/lib/canon.js) by chunks of a year, a day, an hour and 0.1 hours, and that every onHour runs in the step that reaches its hour, over 60 simulated years of the clock’s arithmetic alone.speed.js still passes frame-sized hours, and a part of a step still moves the agents (the Flag below is not solved here).st.tick; a save without it loads with st.tick = Math.round(st.t * TICKS). Invariant in tests/lib/invariants.js: st.tick is a non-negative integer and st.t agrees with it to within one step.tests/golden.json and tests/counters.json are re-recorded, and the PR lists the changed entries. Numbers before and after in the PR and here: populous and founders, small map, seeds 3, 7, 11, 42, 15 years (tools/sim.js, tools/balance.js): living, households, deaths by hunger, grain per household at the lowest decile, so that households and not only the mean are compared; node tools/bands.js reports every band as before (in or out).talkHour (relations.js:219-222; start from a random place in st.living), “the original order and random draws” in the moving round (lifecycle.js:218), “the original multiplication, including its floating-point rounding” in Stores.villageDay (stores.js:83), and SPREAD in dailyTies (relations.js:31; (day + id) % every). registry.js:51 stays: a fixed order of contributions is the rule, not a fossil.npm run verify passes; node tests/perf.js --counter-cost unchanged within noise.
Notes:src/page/speed.js:25 passes dt * speed / HOUR_SECONDS), so the frame rate changes the run: populous, seed 7, 96 by 64, 3 years in chunks of 0.0731 hours ends with 171 living against 167 in whole steps. A seed reported from the page cannot be replayed headless, and it sits badly with Rules for cost, 7 (only the page interpolates). Whole steps in the page need the page to draw agents between two steps (at 1× a step lasts 0.2 s; page/animate.js draws a.x, a.y as they are). Kim decided on 10 November: the page runs whole steps and draws pawns between them, VB-244, after this one.fb35e75:
advance alone (t += 0.1): t is off by 2.4e-8 hours at year 10, 3.9e-7 at year 40, 8.8e-7 at year 80. Math.floor(t + 1e-9) first misses an hour at year 2.00; over 60 years about 52,000 of 69,120 hours (75%) run onHour one step late. Math.floor(t * TICKS + 1e-4) never fails in 80 years, so the two clocks in one loop disagree: the autumn meetings’ stages go by ticks, movingRound at noon by hours.advance(YEAR) 3 times gives 3,785 record entries, 11,103.8 grain in the live households and 326 open jobs; advance(DAY) 144 times 3,752, 10,875.7 and 324; advance(1) 3,456 times 3,783, 10,839.1 and 326. All end with 167 living. Chunks of 1 and of 0.1 give the same state.tests/golden.json depends today on how each test chunks advance; after this task it does not.sim/clock.js or sim/agents.js.vb-235 from aea615a):
sim/clock gains st.tick (integer, the source of truth) and st.tickPart (the part of a step a frame-sized advance
has asked for, 0 headless); st.t = (st.tick + st.tickPart) / TICKS is written only in ticking (sim/clock), which advance drives
with the step’s work. TICKS moves to sim/rules. Readers of the tick: sim/agents (the awake hour), sim/stores (yearNow,
the year of the totals, replacing yearOf(t)), sim/herds (an animal’s age, from Math.round(born * TICKS)), sim/plans (the
review hour), sim/relations (the hour’s idlers), page/animate (the 5-minute clock). sim/saves: two lines, a save without a tick
loads with Math.round(st.t * TICKS). No new coupling between systems. A request of hours whose steps are a whole number up to 1e-6
is taken as that whole number (0.7 * 10 is 7.000000000000001); the clock itself has no tolerance.1e-6 in sim/herds are gone; also Math.floor(st.t) in idlersBy (relations) reads the tick.
recordFold (VB-199) reads st.t only at midnight, now a whole hour exactly; left as it is.tests/clock.js (npm test): populous 7 tiny, 2 years, one canonical hash by chunks of a year, a day, an hour and 0.1 h; 60 years of
the clock’s arithmetic (ticking), by whole and frame-sized chunks: every hour begins once, in order, with st.t that hour exactly.
tests/lib/clock.js moves a test state’s clock by hand (14 tests set st.t directly); tests/spread.js checks the round’s phases
by step (each exactly on step 120 + n of its day). tests/relations.js: stand and idleAt release the agents’ jobs (they had
nulled job and left the claims), and clear the hour’s idlers cache, which the drifted float used to invalidate by luck.2ee72a6 the clock: all twelve snapshots change; every one in agents, cenv, farmsteads, households, idlers, jobs, pathCache,
pawns, record, rng, talks, villages, wear, and the new tick and tickPart; most in biomeDirty, pathDirty, jobSeq, owner, rigDirty,
used; claims (founders 7 and heirs 42, years 2-3), herds (year 3; heirs from year 1), headSeq, claimSeq, landDirty (heirs 42),
recordFold and recordYears (year 3), meetingLog (heirs 7 year 3). Counters re-recorded: routeSearch 0.364 to 0.380, routeExpand 1.634
to 1.730, routeHit 5.202 to 5.293, routeRefresh 1.556 to 1.611, talkWeighed 10.764 to 10.830, walkStep 113.72 to 114.26, idleStroll
5.643 to 5.664, companyWeighed 10.373 to 10.591, walkPoint 8.479 to 8.424, walkTies 0.0533 to 0.0500, tiePairs 2.902 to 2.942,
ruleValue 1.000 to 1.001, oppAsk 38.79 to 39.11, matchBid 9.631 to 9.469, lookUpJob 5.659 to 5.591; the rest equal (the old values’
tails, as 5.999999999999222 for walkDaily, were the drifted clock in the pawn-days). tests/record.js heirs-7 anchor 219 events
(fbcf6c158073bd96) to 221 (ee92c66ecf39a79d).1510285 talkHour from a random place in st.living (the binary search goes): all twelve but founders year 1, in agents, cenv,
households, pathCache, pawns, record, rng, talks, wear, most in idlers, jobs, jobSeq, villages, farmsteads; heirs in biomeDirty,
pathDirty, herds (heirs 7); recordFold and recordYears (heirs, year 3). Counters unchanged. Record anchor 221 to 216
(bb7ed9eb8ef5d338).67e08c0 SPREAD goes from dailyTies: cenv and pawns in all twelve. Written (day - id) % every, not (day + id): with +,
ids under 30 fall due only from day 162, so the 30-day fixtures of tests/counters.js and tests/scaling.js walk no ties and fail
their “every counter exercised”; with - pawn id is due on day id. Counters: walkTies 0.050 to 0.333 (the fixture’s ids 1 to 30
are all due in its 30 days).67cda46 Stores.villageDay takes the day’s share with Stores.take: no golden entry changes (equal to twelve digits); counters equal.7202e25 the moving round’s “original order and random draws”: no code was kept for old results there (VB-201 left the
candidate filter, which skips only pairs whose homeFor is null, before any draw); comment reworded, no change.Numbers, small map, 15 years (a script on Sim as tools/sim.js; grain = hh.grain of live households; before aea615a, after 7202e25):
| scenario | seed | living | households | deaths by hunger | grain, lowest decile | grain, mean |
|---|---|---|---|---|---|---|
| populous | 3 | 448 → 454 | 114 → 116 | 0 → 0 | 81.2 → 90.3 | 251.9 → 271.8 |
| populous | 7 | 430 → 425 | 95 → 96 | 2 → 1 | 83.8 → 93.7 | 247.4 → 253.1 |
| populous | 11 | 466 → 455 | 109 → 109 | 0 → 0 | 87.3 → 90.7 | 326.4 → 343.3 |
| populous | 42 | 476 → 481 | 105 → 110 | 0 → 0 | 113.9 → 106.7 | 283.1 → 290.3 |
| founders | 3 | 14 → 16 | 3 → 4 | 1 → 1 | 252.1 → 142.3 | 366.3 → 366.7 |
| founders | 7 | 21 → 20 | 5 → 5 | 1 → 1 | 58 → 59.1 | 295 → 304.2 |
| founders | 11 | 25 → 20 | 5 → 5 | 0 → 1 | 211.2 → 80.3 | 364.9 → 303.8 |
| founders | 42 | 18 → 18 | 4 → 3 | 3 → 2 | 297.4 → 187.9 | 405.8 → 452 |
Populous households at the lowest decile are better off at three seeds of four; founders’ “decile” is the poorest of 3 to 5
households, as noisy as one run. tools/balance.js populous, seeds 3, 7, 11, 42, years 2-15: made/need by household size 1 to 8
2.26/1.81/1.49/1.61/1.59/1.40/1.38/1.31 before, 2.04/1.72/1.54/1.64/1.65/1.45/1.38/1.40 after; hungry % of the one-person
households 18 to 20, two 9 to 11, three 7 to 5, larger the same.
node tools/bands.js, full runs): 31 in, 19 out before and after. Changed: hunger-deaths populous 7 out (0.028) to in
(0.015); villages heirs 42 out (1) to in (0); hunger-deaths populous 42 in (0.015) to out, 0.024, residual 0.004 over 0.020;
hungry-households founders 3 in (0.250) to out, 0.563, residual 0.263 over 0.300. The others as before (in or out); a changed run,
no bug seen.npm run verify (fixtures of d196f68, stale, saved without st.tick: they load by the new path): all pass but tests/history.js,
six ratios over target 1.25 (seven before): routeExpand 1.501 (1.462 before), routeRefresh 2.070 (1.856), roundPairs 10.24 (10.24),
heirRank 1.472 (1.473), recordFold 1.557 (1.558), roundRank 1.413 (1.414); walkTies 1.436 to 0.970, now within. Not raised. The last
criterion stays unticked for that residual only.node tests/perf.js --counter-cost: bound 0.1288 ms a day (0.107%) before, 0.1300 (0.108%) after: unchanged within noise.docs/img/vb235-clock-close1.jpg (the page’s clock, from the step count).