death.hazard as its first ruleStatus: done
Priority: 2
Size: M
Arch: yardstick
Phase: implement
Design ref: none (code structure). docs/architecture/target-v2.md, sections 2 (principle 11, rules combine contributions) and 4.1 (the rules registry); docs/roadmap.md, sections 3.0 and 5.3 (rule 6, compile what is hot).
Goal: Target-v2 makes the rules registry the kernel’s one new piece. A rule is a quantity one domain owns; other domains contribute factors or terms to it, each declared with a range; a panel can explain it. Today’s deathRoll (sim/deaths) is already one rule in small: a base hazard by age, times one plus hunger, times frailty, with one draw naming the cause. Build the registry with it as the first rule, behaviour unchanged. Then value() is measured from the start, and slice 1’s allowance and later contributions (disease, traits, the urban hazard) have a door.
Acceptance criteria:
sim/registry in the kernel, under 200 lines, following the module conventions (VB-012, an @sys line, VB-013). It is listed in src/manifest.json after sim/rules and before sim/deaths.
declare(spec) and contribute(c) run at load only; a call after the first Sim.seed throws.value(st, rule, ctx) returns the number; explain(st, rule, ctx) returns {value, base, parts: [{key, source, label, cause, factor}]}; list() returns the rules and their contributions.
Explanations also include combine, unclamped and clamp (null without bounds), and diagnose non-finite or out-of-range results.combine is product or sum; min and max come when a rule needs them.compile waits for its first consumer (move.cost, slice 10).value returns its factor; one that does not apply returns 1 (or 0 in a sum), so no list is built per call.death.hazard, owned by sim/deaths:
hazard(age) / YEAR_DAYS;hunger: 1 + HUNGER.k(floor(age)) * hunger, cause hunger, tunables HUNGER.k;frailty: the health factor, also cause hunger, as today’s roll names it, tunables HEALTH.riskBelow and HEALTH.riskSlope;deathRoll asks value and names the cause as now (the base’s cause by age if the draw falls under the base, hunger above it). Starvation at health 0 stays a check before the roll. One random draw per pawn-day, as now; tests/deaths.js passes unchanged.tests/registry.js in npm test:
value creates no per-call containers or closures; 1,000,000 calls under node --expose-gc retain under 1 MB of added heap.
The GC check measures retention, not allocation rate; callbacks and the hot path also need source review;explain’s value equals value, and its parts multiply to it;tools/rules.js prints every rule with its owner, combine, and its contributions (source, range, tunables). --explain <pawn> on a run or a save (--load FILE) explains one pawn’s death hazard today.VB.count.ruleValue counts reads, if VB-192 is done; if VB-194 is done, the bench gains Rules.value in ns per call (otherwise VB-194 adds it).tests/golden.json unchanged: the registry is reached as VB['sim/registry'], not through Sim, so the API’s hash stays. npm run verify passes.AGENTS.md, Architecture: one line on the registry and how a module contributes to a rule.
Cost: one value per living pawn a day, about 1,000 calls at the late reference; under 0.1 ms a game day if a call stays under 100 ns. explain runs only in panels and tools.
Notes:sim/rules, which holds the tunables.5737d5c): impact: sim/registry owns only load-time code, no simulation state.
sim/deaths reads st.t, pawn.born, hunger and health through ordered rule callbacks and retains its existing death stream.
sim/scenarios and sim/saves seal registration when creating/loading a run. Sim’s public API and the save format are unchanged.
The registry is the declared contribution seam required by target-v2, rather than another special case in the death roll.
Product and sum rules support clamps, immutable copied declarations, neutral factors/terms and duplicate/unknown-rule checks.
value uses indexed loops over a fixed load-time contribution list; explain allocates only when requested by a tool or panel.
There is no new loop over simulation entities or history. The first rule has exactly two contributions.
tools/rules.js lists owners, ranges and tunables and explains a pawn in a new run or a plain/gzipped save; dead or missing pawns
produce a diagnostic. The registry stays off Sim, and seeding through makeState seals low-level scenario/test entry points too.
Tests compare all 1,000 pawns in a reduced populous state against the old expression and roll, plus age/hunger/health and exact-roll
boundaries; they check one random draw even at zero health, summed/clamped rules, defensive copies and sealing on a fresh load.
The only counter baseline change is ruleValue: 0.9999999999998703 reads per living pawn-day in the existing two-village fixture;
every existing counter remains bit-identical. The counter-cost report includes the new direct increment.
npm test passed all 62 scripts; npm run verify passed all 71 (270.9 s in this environment), including unchanged golden
states/API hashes, save/load continuity, five long invariant runs and both page smoke tests. Chromium reported no console or page errors.Architecture/design review at this base: the current manifest-ordered VB modules and plain-data saves give deterministic, testable simulation boundaries; target-v2’s five domain layers are the destination, rather than a description of today’s layout. The new ownership policy catches writes past the chosen owners, with existing debt explicitly baselined. The principal remaining structural risks match slice 0: loops over historical records, unbounded route/event storage, and late-game step spikes. The page’s split files still share one closure, which leaves presentation coupled by names and load order. The painterly terrain, compact farm clusters, family records and customs support the Nordic generational design. The current inspection-oriented controls still need the already-planned life slice (pawn control and browser saves) to fulfil the inhabited-pawn design. No design decisions changed. Visual inspection used Chromium, populous tiny seed 7, paused on spring day 1: both the whole island and the largest village with the land overlay; local PixiJS and fallback fonts. Both page smoke tests cover the rebuilt page independently of these screenshots.
Final cost check, isolated after verification: seven batches of one million reads over 1,024 varying age/hunger/health contexts;
median old expression 47.1 ns/read, registry 73.8 ns/read (including its work counter). At 1,000 living pawn-day reads this is
0.074 ms/day in total, an added 0.027 ms/day. The million-call GC test retained no additional heap (limit under 1 MB).
These are microbenchmarks, not a new late-save simulation budget measurement; VB-194 owns that bench. Initial registry source: 69 lines.
Rebuilt index.html; tests/golden.json unchanged, existing counters unchanged, no ownership baseline allowances added.
explain checks that the base, contribution
results and intermediate aggregates are finite, and that each contribution lies in its declared range. Errors name the rule and
provider/key; both sum and product overflow are diagnosed before a clamp can hide them. The hot value evaluator is unchanged:
providers must return valid numbers; diagnostic checks run in tools/tests, not on each simulation read.
Explanations now report the combination method, raw result and copied clamp bounds; listings include the bounds too. Tests cover
upper/lower clamps and a result within them, immutable declaration bounds and mutable explanation copies, NaN/infinities/non-numbers,
range violations and endpoints, overflow, and the CLI’s extra explanation fields. Every valid result still equals value exactly.
The memory test and documentation explicitly call the GC measurement retained-heap growth, not proof of zero temporary allocations.
Source review covers value, ruleOf, clamped and both death contributions: no per-call containers or closures are created there.
Registry source after the follow-up: 79 lines. The follow-up’s npm test passed all 62 scripts (112.1 s).
Final follow-up verification: all 71 npm run verify scripts passed (252.1 s), including unchanged golden states/API hashes,
both page smoke tests, five invariant runs and save/load continuity. python3 build.py --check and diff whitespace checks pass.