Build the duty roster on first ask, so a duty imported first is never read half-loaded (#17155)
Fixes #17155: seven keeper files are red on every full suite with "Cannot access 'duty' before initialization" at src/maintain/registry.ts:21. No open pull fixes it (checked 20:49 UTC).
Cause: the keeper tests import src/maintain/duties/flow_keeper.ts first. Its static graph reaches registry.ts (likely via src/maintain/runner.ts), and registry.ts builds the roster at import, so rosterOf's require() of flow_keeper.ts returns its half-evaluated namespace and duty is still in its TDZ.
Change, kept narrow:
In src/maintain/registry.ts, replace the module-level others and export const duties with a memoized function, so no duty module is required at import:
let built:readonlyDuty[]|undefined;/** The roster, built on first ask and never at import: a duty whose own graph reaches the registry cannot see its `duty` uninitialised (#17155). */exportfunctionduties():readonlyDuty[]{if(built ===undefined){const others =rosterOf(DUTY_MODULES);
built =[...others,dutyIdle(others)];}return built;}
Grep src/, scripts/ and test/ for every import of duties from registry.ts and call duties() there (runner.ts: options.duties ?? registered(), import kept as duties as registered). Change nothing else.
Add test/duty-roster-order.test.ts:
import{ expect, test }from"bun:test";test("a duty imported before the registry still joins the roster (#17155)",async()=>{const p =Bun.spawn([process.execPath,"-e",` await import("./src/maintain/duties/flow_keeper.ts"); const { duties } = await import("./src/maintain/registry.ts"); const { DUTY_MODULES } = await import("./src/generated/duties.ts"); if (duties().length !== DUTY_MODULES.length + 1) process.exit(2); `],{ cwd:`${import.meta.dir}/..`, stdout:"pipe", stderr:"pipe"});const err =awaitnewResponse(p.stderr).text();expect(err).not.toContain("before initialization");expect(await p.exited).toBe(0);});
Run it on main first: it must fail there, and pass with the change.
Run the new test and test/keeper-holds, keeper-handed, keeper-down-two-vantages, keeper-stuck, keeper-rebase-loop, keeper-lull and own-source. No skip, no retry, no raised limit or timeout.
Pull body: a Before paragraph, an After paragraph, and "Closes #17155".
Build the duty roster on first ask, so a duty imported first is never read half-loaded (#17155) Fixes #17155: seven keeper files are red on every full suite with "Cannot access 'duty' before initialization" at src/maintain/registry.ts:21. No open pull fixes it (checked 20:49 UTC).
Cause: the keeper tests import src/maintain/duties/flow_keeper.ts first. Its static graph reaches registry.ts (likely via src/maintain/runner.ts), and registry.ts builds the roster at import, so rosterOf's require() of flow_keeper.ts returns its half-evaluated namespace and
dutyis still in its TDZ.Change, kept narrow:
othersandexport const dutieswith a memoized function, so no duty module is required at import:dutiesfrom registry.ts and callduties()there (runner.ts:options.duties ?? registered(), import kept asduties as registered). Change nothing else.Run it on main first: it must fail there, and pass with the change.
Pull body: a Before paragraph, an After paragraph, and "Closes #17155".
Told from /trevato/forge/issues/17155