Status: done (2026-11-15)
Priority: 3
Size: S
Arch: yardstick
Phase: implement
Design ref: none (code structure). AGENTS.md, Rules for cost, 8 (bound every cache).
Goal: refreshRoutes (src/sim/routes.js) tests every cached route each night against the tiles whose walking cost changed, with unrolled hex arithmetic and a test in two bounds, to drop about 130 of 30,000. Emptying the cache when any tile changed takes the same time a game day at the late fixtures, with a tenth of the routes held. Replace the selective refresh with the clear, and lower ROUTE.cacheMax to what the clear leaves in use.
Acceptance criteria:
refreshRoutes empties st.pathCache when st.routeDirty is not empty, and clears st.routeDirty; MAX_STEP, the unrolled hex arithmetic and the two-bound test go. route, findRoute, costChanged and the LRU are unchanged.ROUTE.cacheMax falls from 60,000 to the bound the clear needs: the largest cache seen over 48 days at medium-ref and small-y40, with a margin (about 2,700 routes were held at medium-ref; node tools/route-bench.js), and its comment in sim/rules.js gives the measurement in place of the hit rate.medium-ref and small-y40 no higher than before beyond the runs’ spread (paired runs); the worst 6-minute step and the number of steps over the 60 ms target no higher, residuals reported (node tools/bench.js). If either is worse, the task stops and reports: the selective refresh stays.tests/routes.js holds that a route is not served from the cache after a tile on it changed cost; the tests of the selective bounds go with them.tests/golden.json and tests/counters.json (routeRefresh, routeSearch, routeHit, routeExpand, routeEvict) are re-recorded, and the PR lists the changed entries. Numbers before and after: populous and founders, small map, seeds 3, 7, 11, 42, 15 years: living, households and st.routeFallbacks; node tools/bands.js reports every band as before.npm run verify’s hard checks pass, its targets reported with their residuals; the PR says what fell (lines, the save’s size at medium-ref).
Notes:fb35e75 on the fixtures then published (made at 9f130e6; a local cache since VB-272), with refreshRoutes replaced as tools/route-bench.js replaces it:
medium-ref (1,026 living), 48 days, three runs each: as it is 210, 215, 217 ms a game day; cleared on a change 209, 200, 211. The refresh itself 5.9 ms a day against 0.0; routes held about 32,000 against about 2,700; searches a day 128 to 176 against 1,405 to 1,628; tiles expanded a day 3,553 against 13,633. Worst step over 24 days, two runs: 119 and 105 ms against 110 and 86; steps over 60 ms 3 and 3 against 2 and 3 (VB-201’s subject either way).small-y40 (695 living), 48 days, one run: 138 ms a day against 136; refresh 4.4 ms against 0.0.medium-ref (5 a day) and 45 of 48 at small-y40, so the refresh runs nearly every night, over 30,000 routes, to drop about 130.hexSteps and routeCost, six lines) gives the identical state and costs 11.5 ms a day against 5.9: if the selective refresh stays, its unrolling is earned.medium-ref’s 37.7 MB save (3%).route: st.pathCache.delete(k); st.pathCache.set(k, p)) goes too, in this task: a CPU profile of 48 days at medium-ref puts 84% of route’s own time on that line (3.4% of all samples), and the cache holds 32,000 of its 60,000, so nothing is ever evicted by age. Without the bump (first in, first out) the final state is identical; simulation ms a day, three runs: 186, 181, 199 as it is, 172, 182, 193 without (within the runs’ spread).Implementation and validation (14-15 Nov 2026, agent)
refreshRoutes now clears st.pathCache and st.routeDirty when any tile changed; MAX_STEP, the unrolled arithmetic and the two-bound test are gone (about 48 lines fewer in sim/routes.js). The recency bump on a hit is gone too, as the notes say, so the cache is first in first out; findRoute, costChanged and routeCost are unchanged.ROUTE.cacheMax 60,000 to 8,000: the largest cache over 48 days, after the first emptying, sampled every 6-minute step: 4,028 at medium-ref, 2,779 at small-y40; 8,000 is about twice the larger, for a run of quiet days.tools/route-bench.js, 48 days, three runs each (this machine is slower than the one of the notes). Ms a game day, as it was, then cleared: medium-ref 375.7, 342.0, 357.0 against 358.0, 351.2, 353.2; small-y40 245.0, 253.0, 240.2 against 208.8, 203.9, 184.9. Refresh 16 to 18 ms and 9 ms a day against 0.0. Routes held after: 31,514 and 20,751 as it was against 548 and 364; searches a day 580 and 353 against 1,986 and 1,208; hit rate 0.88 and 0.90 against 0.59 and 0.63.| Worst 6-minute step, steps over 60 ms, 24 days, two paired runs (before | after): medium-ref 56 and 68 ms, 0 and 1 over |
80 and 50 ms, 2 and 0 over; small-y40 44 and 41, 0 |
29 and 37, 0. Within the runs’ spread (the machine was noisy); I did not run tools/bench.js, which writes docs/bench/history.jsonl and budget.md, shared files. |
routeFallbacks 22, 7, 11, 27 before, 30, 9, 15, 32 after. Founders: identical, 15, 19, 23, 23 living, households 4, 5, 4, 3, no fallbacks. node tests/bands.js (the reduced bands): 0 targets out, growth median 0.022. node tools/bands.js not run (it appends to docs/bench/bands.jsonl, shared).medium-ref: 21.74 MB JSON to 20.59 MB after 48 days (the cache 1.25 MB to 0.02 MB).tests/routes.js: the selective-bounds test is replaced by one that a route is not served from the cache after a tile on it changed cost; the LRU test follows FIFO and the new bound. tests/counters.json re-recorded with --update: routeSearch 0.33 to 1.06, routeExpand 1.50 to 4.23, routeHit 3.78 to 3.21, routeRefresh 0.84 to 1.38 (it now counts the routes cleared), routeEvict 0; other counters moved with the changed run (the baseline held 9 counters out before the update). tests/golden.json is retired (VB-408); the numbers above stand instead.