Status: done (2026-11-12)
Priority: 2
Size: M
Arch: between
Phase: implement
Design ref: none (code structure). docs/architecture/target-v2.md, section 3.3 (The calendar). Kim, 10 Nov 2026 (code review).
Goal: onHour, endOfDay and stepEnds (each six-minute step of advance; src/sim/clock.js) call each system in a fixed order, and each system then tests the date for itself (c.doy === HERD.springDay, c.season === 2 && c.sday === SEASON_DAYS - 1, c.sday === 0), as stepEnds does for the moving round and the meetings’ stages. The order decides outcomes and is written nowhere. Target-v2 (3.3) asks for one declared schedule; Kim asked on 10 November that it be formal: entries by step, hour, day, season or year, or at a named time.
Acceptance criteria:
sim/clock holds one table, CALENDAR, in the order things run. An entry: {every: 'step' | 'hour' | 'day' | 'season' | 'year', at: {season?, sday?, doy?, hour?, tick?} (the place within that cadence; absent: every time), when?: (st, c) => boolean (a guard that is not a date, such as st.cenv), owner: '<module>', run: (st, c) => void, note: '<what it is, in a few words>'}. It is code, never saved; it is frozen with the rules registry’s seal.stepEnds, onHour, endOfDay and newYear become one loop over the table for what the step reached. Every date test that decides whether a system’s yearly, seasonal or daily work runs moves from its module into its entry’s at: the moving round (stepEnds, clock.js:111), the meetings’ stages (stepEnds, clock.js:110, one entry a stage, by tick), the meeting’s call (callMeetings, clock.js:53, autumn day 10 at midnight), spring in sim/herds (herdsDay, herds.js:168), the autumn count (byreDay, byre.js:213), the season’s last day in sim/fields and sim/meadows (fieldsDay, fields.js:150; meadowsDay, meadows.js:125-126), fadeRelations (dailyLife, lifecycle.js:336), Stores.prune (newYear). The entries added to the clock since 10 November are in the table too: occasionsDue each step (VB-126), nightCulture on the steps after midnight (VB-201; its test of the step, lifecycle.js:351), dueOffers and takeIn at midnight (VB-293, VB-308; takeIn guarded by shareFood’s short list), and foldHistory daily (VB-199; its c.doy === 0, records.js:46). A date test that is a condition of the work itself, not of when it runs, stays where it is (the sowing window, the hay days, a job’s urgency): list in the PR which were moved and which were left, and why.(day + id) % period (dailyTies, relations.js:69-70) stays in its module: it is one daily entry. Things due at a time for one thing (a queue in st, Rules for cost, 4) are not built here: the two queues there are (st.offers, st.occasions) stay their modules’, each read by one entry (dueOffers, occasionsDue).node tools/sim.js --report calendar (or tools/rules.js) prints the table: cadence, place, owner, note, in order. docs/architecture/modules.md and ownership.md (section 1, the rules sim/clock holds) follow; target-v2 3.3 gets a line that the table exists.npm test holds the table’s order against a recorded list (an intended change of order re-records it and says so in the PR), and that every entry’s owner is a module of the manifest.BACKLOG.md: behaviour unchanged, tests/golden.json and tests/counters.json unchanged, npm run verify’s hard checks pass, its targets reported with their residuals. node tools/bench.js: simulation ms a day and the longest step no higher than before beyond the runs’ spread (the loop over the table must not allocate a step).stepEnds, and the clock’s entries added since (VB-126, VB-199, VB-293, VB-308) listed.design.md, The year).fb35e75: clock.js:28, 29, 38, 46, 49, 99; herds.js:166; byre.js:181; fields.js:145; meadows.js:121, 122; lifecycle.js:285; relations.js:72, 73. On 11 November at 7706bf7: clock.js:30, 31, 37-40, 49, 53, 54, 108-111; herds.js:168; byre.js:213; fields.js:150; meadows.js:125, 126; lifecycle.js:336, 351; records.js:46; relations.js:69, 70.advance into a loop of steps, each stepEnds; an entry’s tick is exact. VB-242’s tasks add the per-village steps as entries.sim/clock.js.CALENDAR in src/sim/clock.js, 44 entries (6 hourly, 21 daily, 1 seasonal, 14 yearly of which 8
are the meetings’ stages and the round’s phases by tick, 2 each step). Each entry is compiled at load into a mask of the day’s 240 steps and
its date (doy, or season and sday, or sday for a seasonal one); stepEnds walks the compiled table once a step and computes the step’s
cal() only when an entry the step reached reads a date or has a guard (an undated ‘step’ entry gets c = null): the walk allocates
nothing else. A malformed entry (unknown cadence or place, a date on a short cadence, doy with season) throws at load.
onHour, endOfDay and newYear are gone into the table; endOfDay’s parts are each an entry, in the same order.
at: the meetings’ stages and the round’s phases (stepEnds, one entry each, by tick from noon), the meeting’s
call (autumn day 10 at midnight), spring (herdsDay -> entry spring, doy HERD.springDay), the autumn count (byreDay -> new
autumnCounts, doy HERD.countDay, run right after byreDay), the frost (fieldsDay -> new fieldsFrost; meadowsDay -> new
meadowsFrost, both season 2 sday 11), the meadows’ spring (meadowsDay -> new meadowsSpring, doy 0), fadeRelations (out of
dailyLife: a ‘season’ entry between dailyLife and the new dailyDeaths), nightCulture’s step test (tick 1 to 40),
foldHistory’s c.doy === 0 (new startFold, a yearly entry before the daily foldHistory), the weather and Stores.prune (two yearly entries).
The hour tests of onHour are at.hour ranges (talk 6-21, review 5-18, cooks 14-17, lookUp 6-17, release 18); the dark’s end, which
moves with the date, is a when.byreDay (stalled(c), the herds are
fed in the byre; the daily entry must run for the spoilage anyway), stallLeft, the manure window (fields.js:94), the hay and raking
days (meadows.js:91, 102), help’s ask days (help.js:49-50), the plans’ and kinds’ seasons and urgencies, building’s slack season,
and dailyTies’ (day + id) % period (one daily entry, as the criterion says). startFold keeps its !F.pass (a condition, not a date).kc test in
stepEnds); endOfDay, onHour and newYear (and meadowsDay); lifecycle’s unused env. Lines: src/sim +246 -130, of which
the clock +187 -83 (the table, its doc comment and its compiler, about 45 lines); the modules outside it +59 -47 (the split functions’ heads and comments).node tests/golden.js matches; node tests/counters.js prints the same output byte for byte as the base
commit (tests/golden.json, tests/counters.json unchanged; the one counter out, oppAsk +0.29, is out on the base too). The whole
state (every key but the world) and the record hash alike before and after each year: populous seed 7 tiny 4 years, populous seed 11
small 6 years. Single hard scripts pass: lint, ownership, dependencies, plaindata, saves, clock (with the new order test), spread,
crops, hay, living, byre, herds, meadows, relations, commands, record; and the full npm test.
node tools/bench.js needs both medium fixtures for two source keys, about hours on a machine shared by five agents).
Instead node tools/sim.js --scenario populous --seed 7 --map small --years 3 --report perf, alternated, before / after: 102.7 and
64.1 / 74.5 and 89.1 ms a day (the machine’s spread is wider than any difference); the clock’s own remainder (“rest of Sim.advance”)
4.21 and 2.16 / 3.22 and 3.26 ms a day. nightCulture is now called on its 40 steps a day only (calls in ‘daily life and births’
34,848 -> 6,192). The bench at medium-ref is for the merged wave.npm test: 109 scripts pass; 4 targets out, as on the base (scaling.routeRefresh +0.53, scaling.roundPairs +0.85, scaling.takeInWeighed +0.44, counters/oppAsk +0.29).sim/clock loads, not at the registry’s seal: sim/clock is the last module before Sim, so nothing could
register into it later, and an entry is added by editing the table.at.hour and at.tick may be ranges [from, to); tick counts steps from the start of at.hour (hour 0 when absent) and may run
past it (the night’s culture, tick 1 to 40). A date (season, sday, doy) is allowed only on ‘season’ and ‘year’ entries: a
once-a-year step, such as a meeting stage, is every: 'year' with hour and tick.run gets (st, c, n), n the step of the day (the night’s culture needs it); an undated, unguarded ‘step’ entry gets c = null, so
the walk computes no date on steps where only such entries run.shareFood reaches takeIn (both at midnight, entries apart) through one variable in the clock’s closure,
written and read within the same step, so never seen across a save; not a state field.byreDay’s loop to its own entry right after it: the state after the day is the same (on the count
day nothing in the byre’s loop after the count runs, the herds not being stalled, and the count reads no other household’s hay or
larder), but on that day the record lists all the households’ spoilt meat before the counts rather than household by household.
The whole-state comparisons above, the record included, came out identical.fieldsDay rather than before its untended-fields pass: the two touch disjoint farms (held / empty).sim/agents, the meals and fire sim/hunger, the review of plans sim/jobs, its hours checked against sim/plans.reviewHour by the
test); the weather, empty-farm decay and fence decay stay sim/clock’s, as before.tools/perf.js adds the split functions to their systems (dailyDeaths, spring, autumnCounts, meadowsSpring, meadowsFrost)
so the profile’s categories stay as they were.