Status: done (2026-11-12)
Priority: 3
Size: S
Arch: yardstick
Phase: implement
Design ref: none (test infrastructure)
Goal: A misspelt field (hh.grian), an argument in the wrong place or a holder of the wrong kind is silent in plain JavaScript until it misbehaves. Kim decided on 10 November (code review) to move the code to TypeScript, by a plan: real modules first, then .ts and types for the state, then strictness; and with the same sweep a standard formatter, and measures of a module’s width in place of the cap on its length. Survey what the move touches and write the implement tasks. This replaces the first version of this task (checkJs with JSDoc and a hand-written types.d.ts), which sees little: every module is reached through VB['<key>'], which has no type, so no call between modules is checked.
Acceptance criteria:
src/manifest.json and what each import and export becomes; the 16 late lookups (later(...), “loads after this module”) and whether each is a cycle between modules that stays or one to untie; the tests and tools that replace a module’s function to time or count it (about 22 files: tools/perf.js, tools/route-bench.js, tools/bench.js and the like), since an export cannot be reassigned: one hook for them all (a registry of timed functions, or the build’s own instrumenting), chosen and justified; the page script (src/page/*.js, one closure sharing about 340 names), and whether it is left as it is at first; the tools that parse the source (tools/writes.js with acorn, the lints by line, tools/wrap.js) and what each needs.tests/golden.json with it: import and export in place of the VB namespace and the manifest’s order, still JavaScript; a bundler (one dev dependency) in place of build.py’s inlining, with index.html still one self-contained file and --check kept; the loader of the 96 tests and tools changed in one place; the dependency check (tests/dependencies.js, VB-230’s layers) read from the import graph..ts, run by Node as they are (type stripping; name the least Node version and what the Checks workflow uses) with erasable syntax only, so nothing but the page’s bundle is built; the state’s shapes as types (State, Pawn, Household, Farmstead, Village, Job, Agent), made from the 77 @state tags, which then go or point to the type; st is plain data as a type (no function, no class instance); a holder in sim/stores is a union of three (VB-237). tsc --noEmit in npm test.strict module by module, with a baseline of errors that only falls, as the lint’s..git-blame-ignore-revs, in the same task that renames the files, so every file is rewritten once; tools/wrap.js and the lint’s 160 columns go. Measured on 10 November at width 120: sim/relations.js 399 lines becomes 667, sim/heirs.js 400 becomes 644, the simulation and generation modules 8,554 become 14,100.sim/rules 42, sim/records 42, sim/relations 32, sim/herds 28, sim/agents 26, the median 8; heirRanking and movingRound among the functions). AGENTS.md and the refactor rules of BACKLOG.md (“stays under 400 lines”) follow.src/), and name the slice boundary at which step 1 runs. The cost named: files and lines touched, the agent sessions expected, and what npm test and verify gain in time (tsc took 2 s over the modules on 10 November).docs/architecture/target-v2.md (its interface sketches are already TypeScript-style types), modules.md and AGENTS.md (Finding things; Conventions) get a line each on what changes, written by the tasks, not here.
Notes / questions:tsc 5.6 with allowJs and checkJs over the modules as they are, no types added: 35 errors in 2 seconds, 18 of them properties it cannot see, because VB[...] is untyped. Node 22.23 runs a .ts file directly.type(holder) in sim/stores taking any object for a pawn (VB-237); heirRanking’s seven positional parameters and post’s three optional ones. What they do not touch: the float clock (VB-235), the caches (VB-236, VB-242), cost. Types are erased, so speed and the golden file do not move.sim/relations holds nine concerns in 400 lines by being dense (13% of code lines hold two or more statements, 6% three or more), and sim/records, at 279 lines, exports 42 names to 32 modules. The cap packed the one and did not see the other.yardstick as it was, and writes its tasks outside the slices, as VB-228 to VB-232 are.npm test.docs/roadmap.md, section 3.0; the slices Decided by Kim): moved to slice 0, arch yardstick (from tooling).1e9b93b; every count has its command)The tasks it produced are in docs/typescript-plan.md (P1 to P14). The questions are in QUESTIONS.md, “VB-055: the move to TypeScript”. Nothing under src/, tests/ or the build changed; no dependency was installed in the repository (scratch installs of TypeScript 5.6.3, esbuild 0.25.0 and Prettier 3.3.3 were made outside it).
Tried here, on Node v22.23.4 (node --version; .github/workflows/checks.yml uses node-version: 22 in both jobs, so the latest 22.x), in a scratch directory:
node b.ts runs a file with annotations and import { f, type P } from "./a.ts" with no flag. An import without the extension fails (ERR_MODULE_NOT_FOUND). enum, namespace and constructor parameter properties fail with ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX (the last: “not supported in strip-only mode”).require("./a.ts") from a .cts file returned the exports of a .ts file with export syntax.module.stripTypeScriptTypes(src, {mode: 'strip'}) exists, warns (experimental), and keeps every position (types become spaces), so acorn parses its output with the original lines and columns.M.bump after import * as M throws TypeError in a .ts run by Node, so an export cannot be replaced, as the task says..ts files into one IIFE (--bundle --format=iife), types removed.require() of .js files written as ES modules, a bundler over the real modules, Prettier’s or tsc’s behaviour on the real .ts files.src/manifest.json: 63 modules (node -e on the file): vb, 5 in gen/, gen, culture, 54 in sim/ (ls src/sim | wc -l), sim. 11,980 lines (wc -l); 3 over 400 lines: gen 548, sim/relations 401, sim/heirs 401. Each registers as VB['key'] = (() => { ...; return {...}; })() and reads others with const {a, b} = VB['x'] at its top; the manifest’s order is the only order.tools/writes.js’ build() finds 416 imports at load and 75 lookups inside functions; grep -rohE "VB\['[a-z/]+'\]" src | wc -l gives 506 references in the modules (the counts differ because one import reads several names).import { a, b } from './x.ts' for the first, export on each name of the return list for the second. sim.js is the facade over the others (106 exports). The page reads Sim (252 uses) and has 64 matches of \bVB\b (some are task ids such as VB-341 in comments: how many are the object is not checked).src/load.js (requires the manifest in order and returns globalThis.VB): 120 tests and tools load through it (grep -lE "src/load|/load(\.js)?'"); the task text said 96, a recount gives 120; 137 files mention VB[ or globalThis.VB. Keeping load.js as the one place that builds VB leaves them unchanged: the plan’s choice (P3, P6).later('...'), all in sim/commands.js (grep -rn "later(" src), on nine modules (lifecycle 5, heirs 2, settle 2, occasions 2, farmclaims 2, service, membership, cottage, clock 1 each); plus four one-line shims const issue = (st, cmd) => VB['sim/commands'].issue(st, cmd) in sim/relations, sim/help, sim/expansion, sim/farmclaims. tools/writes.js counts 19 late lookups and 19 cycles; node tools/dependencies.js prints 20 upward edges of 500.tests/dependencies.js asserts that every cycle passes through sim/commands, command dispatch, the one upward door (VB-230; docs/architecture/target-v2.md, 3.2). ES modules allow a cycle when no module uses the cyclic import at load; these lookups are used inside functions only (that is why they are late), so they become plain imports (P6). None is to untie. To watch: uses at load, such as KINDS.dig = VB['sim/dig'].KIND in sim/kinds.js:376, depend on evaluation order; P5 lists them.tests/, tests/lib/ and tools/ (a regex for assignments with a function on the right, then reading the hits): 10 files wrap or replace a function or a table entry of a module: tools/perf.js (the SYSTEMS table: 25 functions in 12 modules), tools/moving-profile.js, tools/route-bench.js (refreshRoutes), tools/wood.js (fellTree, clearTile), tests/spread.js (settlementStep, roundPhase), tests/plans.js (R.value), tests/lib/owner-caches.js (refreshRoles), tests/lib/line.js (each verb’s run: a data table, which stays assignable), tests/lib/record.js (a state field, not a module function) and tests/bench.js (fixtures.loadFixture, a test library’s function). Seven read src/manifest.json to require each module and wrap after it registers: tools/perf.js, tools/moving-profile.js, tools/route-bench.js, tools/wood.js, tools/stocks.js, tests/spread.js, tests/lib/owner-caches.js. The “about 22” may count files that set a constant table (CLAIMS.outPerFarm, GOODWILL.halfLife, SOCIAL.seek, ROUTE.cacheMax, FIELDS.room, SLOPE.perRise: 8 files), which an export does not prevent, the table being an object; not rechecked.tools/perf.js is built on “nothing is added to src/”; production code pays nothing; the one place a tool changes is the loader. Against it: the plugin must rewrite exports, and is not prototyped (P3’s first step). The fallback is a registry in the source (probe(key, name, fn), an identity unless a hook is installed before load). A Node module-customisation hook was not chosen: it acts on the module graph, not on a binding (behaviour not tried here).src/page/*.js: 32 files, 5,035 lines, spliced by build.py into (async () => { <!--__PAGE__--> })(); in src/body.html, after PixiJS (a CDN <script>) and the module scripts. Parsed as one script (acorn over the concatenation): 623 top-level names, not “about 340”: the task text is out of date. 344,440 characters.st through Sim, and turning it into modules is its own plan (P14 writes the follow-up). The formatter reaches it in P8 (same width); renaming it is not needed.tools/writes.js (516 lines, acorn), used by tests/dependencies.js, tools/stocks.js and tools/write-scopes.js (the three files that mention acorn, besides itself): acorn 8.15.0 parses no types, so P1 gives them readSource (type stripping keeps positions, tried here). Its edges come from VB['key'] today and from imports after P6 (P7).tests/lint.js (the header, 400 lines and 160 columns, the clock, contribute({source}), the @state tags), tests/lintrules.js, tests/ownershiprules.js, tests/record.js, tests/commands.js, tests/registry.js. grep -lE "readFileSync\([^)]*(src|root)" lists 28 tests and tools that read files of the repository; the ones that read module sources need readSource only where they parse or where annotations would defeat a regex (P1 checks each).tools/wrap.js and the 160 columns go with the formatter (P8). tests/lint.js reads the @state tags to check that every top-level key of w and st is tagged: P10 replaces that with a type check.src/manifest.json (tests/lib/fixtures.js), so P6 and P8 make the local fixtures stale (about 2 + 1.5 minutes to make).@state tags@state lines in 24 files of src/ (grep -rn "@state" src | wc -l): the task says 77. The biggest: sim/scenarios 33, gen 21, sim/records 7, sim/plans 7, sim/relations 6, gen/geology 6, sim/stores 5. A further 16 mentions in tests/review-tools.js, tests/lint.js and tools/stocks.js.allowJs and checkJs over the 62 modules (not src/load.js), types: []: 24 errors in 5.4 s of wall time on this loaded machine (the task’s 35 errors in 2 s was 10 November, with other options; not reproduced). Not tried: erasableSyntaxOnly (from memory TypeScript 5.8; to verify in P8) and satisfies for the key list (P10).src/load.js, constants included: Object.keys(VB[key]).length): 62 modules export, median 9 (the task: 8), 18 over 15: sim 106, sim/rules 56 (task: 42), sim/records 52 (42), sim/agents 34 (26), sim/relations 32, sim/stores 32, sim/herds 30 (28), sim/fields 22, culture 20, sim/lifecycle 19, sim/layout, sim/claims, sim/plans 18, sim/hunger, sim/meadows, sim/heirs, sim/commands 17, sim/farmclaims 16. The numbers are higher than on 10 November, or the way of counting differs; not settled.--print-width 120 --single-quote, over a copy of src/sim, src/gen, src/gen.js, src/culture.js, src/sim.js, src/vb.js (62 files; the task’s “8,554 become 14,100” counted the simulation and generation modules): 11,917 lines become 19,722 (x1.65); sim/relations 401 become 691 (the task: 399 to 667), sim/heirs 401 to 651 (400 to 644), sim/records 725, gen.js 1,044. Not measured: src/page, tests, tools.gen.js:generate 257 to 486, gen/geology.lay 106 to 186, sim/scenarios.makeState 121 to 186, sim/lifecycle.marriageRound 96 to 159, sim/saves.encode 87 to 134, sim/heirs.heirRanking 71 to 116). movingRound did not appear among the longest (not checked further).node tools/backlog.js list): nearly all edit src/ (the plans and surveys do not). The module-joining tasks (P4 to P6) and the formatter (P8) rewrite every module, so no task that edits src/ can run beside them; the rest of the plan edits tools, tests, the build or signatures and can. Proposed boundary: a slice boundary, with P4 to P6 in one window and P8 in another (question 5 in QUESTIONS.md).docs/typescript-plan.md, last section. Sessions: 14 tasks and an estimated 3 to 6 retries or splits.target-v2.md, modules.md and AGENTS.md get their line from the task that changes the thing (P1, P3, P6, P7, P9, P10, P12, P14 each name one); not written here.index.html, the golden file or the counters.ready and backlog.js done not run (the orchestrator creates the tasks and marks it done at the merge).