refactor(typescript): project references + tsc -b typecheck; retire the R11 branches tsc enforces
Summary
All 26 workspace packages declare references derived from the real workspace dependency graph (each package's manifest workspace:* edges; verified programmatically against every tsconfig). pnpm typecheck becomes tsc -b tsconfig.json examples/sdk/tsconfig.json: the root project references the 26 packages, and examples/sdk is a composite build node referencing them too (it keeps its paths mapping into src/sdk/ and self-lists the src/ closure those paths pull in).
R11 package-boundaries (scripts/layering/package-boundaries.ts + rule-registry prose only) keeps exactly what tsc -b cannot express, with planted proofs:
Rebased onto main after #3118 merged provider-testmu as the 26th package. Its wiring is derived like the rest; the one edge with no expressible reference is its dependency on the root agent-device package (agent-device/plugins imports) — the root tsconfig is a noEmit entry point and TS6310 forbids referencing it, so that import type-resolves through the package's own paths mapping. The guard test encodes exactly that rule: root-named workspace deps drop out of the references equality and must have a paths mapping, or it fails (mutation-proven by deleting the mapping).
- Kept (tsc accepts the violation): an import of a workspace sibling the manifest never declares —
tsc -bexits 0 because pnpm's root link farm resolves the specifier anyway, and the declaration is what the published bundle externalizes against; and the root→packages/*/srcrelative tunnel —tsc -btypechecks it (exit 0); the failure it prevents is Node's runtime double instantiation, which no compiler check sees. Plus the full sweep forscripts/and package-harness files, which sit outside the type graph by design (insideCompiledSources()names the set). - Deleted (tsc fails them natively): unknown workspace package, non-exported subpath — for compiled sources only.
- Restored after maintainer review (
63fd2976a): the relative-escape branch fires for every package source, compiled or not. Planted:packages/host-kit/srcimporting'../../kernel/src/errors.ts'→tsc -b packages/host-kit --forceexit 0 (the reference redirects kernel to itsdist-types, so rootDir never sees a foreign file — TS6059/TS6307 only fire on the root escape, where no project redirects). The damage is runtime module double-instantiation, invisible to tsc. The root-escape TS6059+TS6307 proof below stands; it never covered sibling tunnels.
Planted proofs on this tree (TS 7.0.2, exact errors):
packages/xmlfile importing'../../../src/daemon/session-state.ts':error TS6059: File '.../src/daemon/session-state.ts' is not under 'rootDir' '.../packages/xml/src'+error TS6307: File ... is not listed within the file list of project '.../packages/xml/tsconfig.json'(exit 2).packages/xmlimporting'@agent-device/kernel/internal-nope'(un-exported subpath) or'@agent-device/nonexistent-pkg/x'(unknown package):error TS2307: Cannot find module ... or its corresponding type declarations.(exit 2).packages/xmlimporting'@agent-device/kernel/errors'with no declaration and no reference:tsc -bexit 0 (buildinfo showskernel/src/errors.tsconsumed as source) → R11's undeclared-dependency branch stays; same for dropping host-kit's references entirely (exit 0, source fallback). This is the "an import from a package lacking a reference" case: references alone don't fence imports here because the exports maps point at.tssources; the fences that work are the ones listed above.- Root file importing
'../packages/xml/src/index.ts':tsc -bexit 0 → R11's tunnel branch stays.
Typecheck wall time (this host, 12 cores): cold 13.2s → 9.1s; warm 14.2s → 0.5s. Incremental cone proven: touching packages/host-kit/src/archive.ts rebuilds exactly 18 projects — host-kit plus its dependents (capture-kit, device-selection, provision-kit, proxy, session-journal, the platform/provider packages that consume them, replay-port, maestro, managed-allocation, root, examples/sdk); kernel, contracts, xml, ad-script, selectors, etc. report up-to-date.
Root program now consumes 698 package .d.ts outputs and zero foreign .ts sources. Scope note: no gate-catalog or .fallowrc edits were needed (the typecheck gate keeps running pnpm typecheck), so there is no chore(gates) commit beyond the freerange one; every file is references wiring, the R11 pair, or the guard tests.
Repo Guards Check numeric ranges OOM (exit 134) — root cause and fix. freerange's loader (@chenglou/freerange/src/typescript/project.ts) walks the resolved tsconfig's projectReferences recursively and keeps every project's ts.Program alive simultaneously. On main the root config had no references, so fr loaded 1 program (~87s, green). With this PR's graph it loads 26 programs / 13,461 file slots ≈ 2.8 GB heap before analysis begins → Node's default 4 GB heap dies (~27s, reproduced locally and bisected: main's tsconfigs + this branch's code = green). Removing only the root references array makes it pass (112s). Upstream 0.0.5 bundles the identical loader, so there is nothing to pin away. Fix is at the owning config, not the CI heap or a skipped gate: scripts/freerange/tsconfig.json extends the root tsconfig — and extends does not inherit references (planted with --showConfig) — so fr gets exactly the single root program it analyzed on main (verified byte-identical: No lint findings, coverage 971/13495 fully analyzed), while tsc -b keeps the full graph. The gate ran before references existed and analyzed this same scope; R11/tsc -b deliver their value with the tool as shipped, unpinned and ungutted. The reference-free + extends-to-root properties are pinned by a guard test whose mutation is the OOM itself.
Closes #3279
Validation
- What
cd scripts/freerange && frreads relative to cwd (confirmed for review). The ONLY cwd-relative input is the tsconfig, found by upward search fromprocess.cwd():findTypeScriptConfig(process.cwd())→ts.findConfigFile(resolve(cwd), ts.sys.fileExists, 'tsconfig.json')(@chenglou/freerange/src/project.ts:426,src/typescript/project.ts:16-17). There is no baseline, ignore-file, or cache concept at all: the shippeddist/fr.jsbundle's entirenode:fssurface is oneexistsSync(file-mode argument existence) and it contains zeroreadFileSync/writeFileSync/mkdirSync/homedir/tmpdir references — coverage numbers are recomputed from the program every run, which is also why the byte-identical coverage claim was verifiable across configs. The remainingprocess.cwd()uses are the report'sbaseDirectory(src/lower/program.ts:14,src/ir/program.ts:434), a display-onlyrelative(baseDirectory, file)for pretty paths. So thecdexists solely to anchor the upward tsconfig search atscripts/freerange/tsconfig.json(the reference-free wrapper); it reads no config state from the directory it runs in, and moving the wrapper file itself — not the cwd — is the only way to change whatfrloads. - Head
77f3a47db(review nits: compact references + uniform tsbuildinfo):AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed. Coldtsc -bfrom wipeddist-types/.tmpalso verified exit 0 at this head (the compaction touched all 26 reference lists plus testmu's buildinfo name). - Head
f105bbf5d(cubic re-review):AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed;check:layering297/297. The include pin now matches exact arrays (["src"],["src","../command-registry/src/global.d.ts"]) instead of a*.global.d.tssuffix class — mutation: a made-upgenerated-env.global.d.tsinclude in xml goes red. The escape-branch rationale states the dual-route duplication condition, not an unconditional double load. - Head
63fd2976a(maintainer-review fix):AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed;check:layering297/297 with the restored escape branch mutation-proven (re-addingif (compiled) continue;turns three tests red) and the new include/exclude pins mutation-proven (xmlexclude→ red, second rootscripts/include → red). - Head
f75da8ba8(cubic P2 fix):AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed. The facade exhaustiveness gate now matches per source module (readFacadeReExportEdgesByModuleinfacade-exports.ts, alias-aware,export * as nswhole-surface); the planted single-source narrowing behind a duplicated name fails the new rule and passed the old collapsed-name rule (proof in the thread reply).check:layering293/293. - Head
0f1967e9c:AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed (unit lanes cover the test-file-size ratchet). The CI Coverage ratchet failure at8d5df4fb8was real: the guard-test round pushedpackage-boundaries.test.tsfrom 972 to 1,016 lines, over the 1,000-line tripwire. Fixed by splitting along the mirrored source module, move-only: the two real-tree facade-surface gates (explicit exports, exhaustive re-exports) share the facade question withfacade-exports.ts, not the R11 specifier/declaration rules this file mirrors, so they moved verbatim toscripts/layering/facade-surface.test.ts(103 lines). No pin raised: the original file is now 928 lines, below its 972 merge-base length. Discovery proven at the new path — thecheck:layeringglob picks it up and the suite stays 292 tests. - Head
8d5df4fb8(rebased onto main0c1ebb33a): freshpnpm install --frozen-lockfile+pnpm build, wipeddist-types/.tmp→pnpm typecheckexit 0 cold across the 26-project graph;pnpm check:layering292/292;pnpm check:freerangeexit 0 (No lint findings, coverage971/13547— denominator moved upstream);pnpm format/lintclean;AGENT_DEVICE_VITEST_MAX_WORKERS=4 pnpm check:affected --run— all runnable checks passed (full-set selection from the lockfile delta). - Head
e60436198:pnpm check:affected --run— all runnable checks passed;pnpm gate freerange(the exact CI invocation) passes in ~90s. Heads7c00b5636anddeea35c4apassedcheck:affected --runbefore the freerange fix landed. - Focused:
node --test scripts/layering/package-boundaries.test.ts— 16/16, including a mutation-proven test that keeps the compiled/uncompiled boundary honest;pnpm check:layeringgreen (R11 summary line updated). - New structural gates planted-verified per testing.md (violation → intended error above → removed).
- Unresolved risk: none local; CI confirms the full set. Device lanes untouched (pure tooling change).