Repository navigation
fix(tests): gate the unit project's shared-global invariant, and stop the fetch leak that breaks the network-escape ledger - #8538
Conversation
… unit worker
The `unit` project runs `isolate: false` — one module graph and one global
object per worker — and `vitest.config.mts` writes out the ComponentRegistry
invariant that pays for it, enforced by
`scripts/__tests__/unit-registry-absence-collision.test.ts`. `globalThis` is the
second invariant of the same class and had no gate:
a global the setup files install must be the same value at the END of a file
as it was at the START of it.
It was already false. Four files replaced `globalThis.fetch` with a bare
`vi.fn()` by assignment, and a fifth left a `vi.stubGlobal('localStorage', ...)`
standing, so the network-escape guard's `guardedFetch` wrapper was gone for
every later file in the worker and `scripts/__tests__/network-escape-ledger.test.ts`
— a file that did nothing wrong — went red on a full `--project unit` run
reading `Mock` where the wrapper belonged.
Both halves:
- the writers hand the global back: `vi.stubGlobal` in, `vi.unstubAllGlobals()`
out. A bare assignment is undone by neither `vi.unstubAllGlobals()` nor
`vi.restoreAllMocks()`, because neither knows the value was ever replaced.
- `vitest.setup.shared-global-leak-guard.ts` arms a per-file `afterAll` in every
`isolate: false` project: it snapshots what the setup files installed, reds the
file that left one replaced, and puts the value back so no innocent file
inherits it. The wiring and the detector are pinned by
`scripts/__tests__/shared-global-leak-guard.test.ts`.
It has to be a setup file: measured, Vitest re-executes `setupFiles` per test
file even under `isolate: false`, while a module those setup files IMPORT is
evaluated once per worker — so a hook registered in such a module attaches to the
first test file of the worker and to no other.
The new guard immediately found a second leak that nothing could previously see:
`unit-registry-absence-collision.test.ts` imports the project's closures into a
private module graph, and one of the specifiers it resolves is
`vitest.setup.network-escape-guard`, so `vi.resetModules()` made it re-execute
and install a SECOND `guardedFetch` over the first. Both wrappers carry the same
name, so nothing downstream could detect the swap. That gate now snapshots and
restores the globals around its measurement.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FhBNJcLRZLe8M87VcUgpKr
❌ Console Performance Budget
The eager closure is every chunk the entry reaches through static imports — what the browser fetches and parses before the app renders. The entry chunk on its own is a small fraction of it. Which half objected:
📦 Bundle Size Report
Size Limits
|
|
The attribution, structural rather than statistical⭐ This PR's own base commit fails the same gate, with no PR diff at all. Supporting, and independent of that: the failing leg is ⛔ What will not be done about it here
Generated by Claude Code |
|
LANDED — Landing probe, on a re-fetched
|
| probe | expect | read |
|---|---|---|
vitest.setup.shared-global-leak-guard referenced in vitest.config.mts |
> 0 | 2 (the setupFiles entry and the comment naming its pin) |
installSharedGlobalLeakGuard in the guard setup file |
> 0 | 3 |
installSharedGlobalLeakGuard in the detector module |
> 0 | 2 |
snapshotGlobals in unit-registry-absence-collision.test.ts — the second leak's repair |
> 0 | 2 |
⭐ the blind-spot pin: expected: 'function guardedFetch', found: 'function guardedFetch' |
> 0 | 1 |
⭐ firing control — isolate: false in vitest.config.mts |
> 0 | 1 |
⭐ nonsense control — zzq-not-a-real-token-8500 |
0 | 0 |
⇒ The zero is a reading, not a broken pattern.
What is now on main
The unit project's second shared-state invariant is enforced rather than written down: a global the setup files install must be the same value at the end of a file as it was at the start of it. The guard reds the file that leaked and heals the global before throwing, so the leak no longer cascades onto innocent files — which was the whole failure shape.
⭐ And the pin that survives longest is the one on the blind spot. The second leak this PR fixed — a re-executed setup module installing a second guardedFetch over the first — was invisible to globalThis.fetch.name because both wrappers carry the same name. That case is now pinned by content on main: a leak is reported when expected and found are both function guardedFetch. ⇒ The next same-named replacement cannot hide the way this one did for however long it had been there.
Generated by Claude Code
Fixes #8500
The defect, re-measured on today's tree
origin/mainc30026715, dedicated worktree, fresh install, nothing else:The card's premise holds: the failure still reproduces, and the writer is real.
What was actually leaking
The
unitproject runsisolate: false— one module graph and one global object per worker.vitest.config.mtsalready writes out the first invariant that pays for that (ComponentRegistry keys) and hands enforcement toscripts/__tests__/unit-registry-absence-collision.test.ts.globalThisis the second invariant of the same class, and it had no gate:Five files broke it. Four replaced
globalThis.fetchwith a barevi.fn()by assignment; one left avi.stubGlobal('localStorage', ...)standing, which underisolate: falseis undone by whichever LATER file callsvi.unstubAllGlobals()first, restoring a value that file never installed.packages/core/src/actions/__tests__/ActionRunner.test.tspackages/core/src/actions/__tests__/ActionRunner.bodyExtra.test.tspackages/core/src/actions/__tests__/ActionRunner.bodyShape.test.tspackages/core/src/actions/__tests__/UndoManager.test.tspackages/app-shell/src/preview/__tests__/commitHistory.test.tsA bare assignment is undone by neither
vi.unstubAllGlobals()norvi.restoreAllMocks(), because neither knows the value was ever replaced. That is whycommitHistory.test.ts, which already calledvi.restoreAllMocks()in anafterEach, still leaked.Direction 1, both halves — as the triage seat's routing opinion asked
The writers hand the global back.
vi.stubGlobalin,vi.unstubAllGlobals()out.The next writer is caught, not discovered.
vitest.setup.shared-global-leak-guard.tsarms a per-fileafterAllin everyisolate: falseproject. It snapshots what the setup files installed, reds the file that left one replaced, and — before it throws — puts the value back, so the file that leaked is the only one that goes red instead of the failure surfacing three files later in one that did nothing wrong.It has to be a setup file, and that is measured rather than stylistic. Vitest re-executes
setupFilesfor every test file even underisolate: false, while a module those setup files import is evaluated once per worker — so a hook registered in such a module attaches to the first test file of the worker and to no other:afterAlland notafterEach, because the invariant is about what crosses a FILE boundary: the network-escape guard's ownFix:text prescribes leaving a module-scope double up for a whole file when the component can read after the test body returns, and anafterEachhere would red exactly that prescribed shape.The guard is wired only into projects that set
isolate: false.dom/dom-heavy/distkeepisolate: true, where each file gets its own global object and nothing crosses.A second leak the new guard found immediately
scripts/__tests__/unit-registry-absence-collision.test.tsimports the project's closures into a private module graph aftervi.resetModules(). One of the specifiers it resolves isvitest.setup.network-escape-guard—scripts/__tests__/network-escape-ledger.test.tsimports it by path — so the guard RE-EXECUTED there and installed a secondguardedFetchover the first. Both wrappers are namedguardedFetch, so the ledger test could never see the swap, and every file after that one in the worker ran with a doubled wrapper whoseafterEachbelonged to a module instance the run had already discarded. That gate now snapshots and restores the globals around its measurement.Evidence
Full
unitproject, before and after, same worktree, both through the shared verify lock:@object-ui/consoleproject (it merges this config): 90 files / 1072 tests passed.Reverse verification 1 — the guard can fail. The stub in
ActionRunner.bodyShape.test.tswas reverted to the raw assignment on disk (git hash-objectmovedfd231cdc->8daf94e0; the anchored marker counts inverted both ways: stub-form 1->0, raw-form 0->1, unstub 1->0), and the file alone was run:The 7 tests of the file still pass; only the leak reds, and it reds on the writer. Restored under a trap and proved by state, not by an exit code: blob back to
fd231cdc,git diff HEADempty.Reverse verification 2 — the wiring pin is not vacuous. The guard was removed from the
unitproject'ssetupFileson disk (blob37c1635d->ce585009; armed-count 2->1 while base-count held at 3), andshared-global-leak-guard.test.tswent red on exactly the two wiring assertions:Restored the same way: blob back to
37c1635d,git diff HEADempty.Gates:
pnpm type-check:vitest-setupexit 0 (after building the four packages its program resolves through),pnpm type-check:scriptsexit 0,turbo run type-check --filter @object-ui/core --filter @object-ui/app-shell31/31,node scripts/check-changeset-presence.mjsexit 0,node scripts/check-changeset-no-major.mjsexit 0,node scripts/check-governed-queue-guard.mjs --testNOT GOVERNED.ESLint, narrowed and declared:
pnpm lint:rootexit 0 (32 warnings, 0 errors) plusturbo run lint --filter @object-ui/core --filter @object-ui/app-shell3/3 successful (2925 warnings, 0 errors), andeslint --no-inline-configover the 10 changed files, exit 0, 9 warnings and 0 errors — all pre-existingno-explicit-anyon lines the diff did not touch. Type-aware linting is not enabled ineslint.config.js(noparserOptions.project, noprojectService), so this diff cannot move the verdict on any file it does not contain. Ratchet counts read on51ee1fdff. The repo-wideeslint .sweep is CI's.Filed, not fixed here
objectui#8537 — the same guard file's OTHER half is inert: its
afterEach, the one its header calls "theafterEachthat fails ANY escape in ANY file", registers only for the first test file each worker runs in theunitproject, by exactly the mechanism the probe above shows. Two byte-identical escaping files run together: only the first reds. Repairing it is a separate blast radius — escapes invisible today would all turn red at once, and how many there are has not been measured — so it is filed unlabelled for triage rather than ridden in here.Notes
Test (shard 2/4)is flaky in this repository onObjectKanban.contractEnvelope-6839.test.tsx:134, tracked as objectui#8493 and unrelated to this diff.🤖 Generated with Claude Code
https://claude.ai/code/session_01FhBNJcLRZLe8M87VcUgpKr
Generated by Claude Code