Skip to content

Reset dir singletons at file boundaries in isolate:false projects#1046

Merged
pavlovic-ivan merged 1 commit into
G-Research:masterfrom
dev-milos:fix/v2-core-fs-isolation
Jul 23, 2026
Merged

Reset dir singletons at file boundaries in isolate:false projects#1046
pavlovic-ivan merged 1 commit into
G-Research:masterfrom
dev-milos:fix/v2-core-fs-isolation

Conversation

@dev-milos

Copy link
Copy Markdown
Contributor

mcp-meta-policy.test.ts fails intermittently on CI with a real mkdir under /memfs/... (EACCES/ENOENT), while passing on the dev box.

Cause: under isolate: false (v2-core, v2-integration) files in a worker fork share one module graph, so bobbit-dir's project root and the agent-dir runtime state are shared. docker-args.test.ts sets the root to a memfs fixture path and never restores it, so a later file that relies on the default resolves bobbitStateDir() under the leaked root. Which file loses depends on worker sharding, so it moved between the dev box and CI, and retry: 3 can't mask it — the retry reruns in the same fork.

Fix: a custom runner resets the project root and agent-dir state in onCollectStart, the one hook that runs per file before its module loads. beforeAll in a setup file fires once per worker, and beforeEach fires after the file's own beforeAll, so neither works. process.env is left alone — the fork-scoped gateway owns BOBBIT_*.

Deterministic repro (fails on master, passes here):

VITEST_MAX_WORKERS=1 vitest run --project v2-core \
  tests2/core/docker-args.test.ts tests2/core/mcp-meta-policy.test.ts

Full test:unit green at 2 and 3 workers (965 files); no test files or production code changed.

Under isolate:false (v2-core, v2-integration) files in a worker fork share
one module graph, so bobbit-dir's project root and the agent-dir runtime
state are shared. docker-args.test.ts points the root at memfs fixture paths
and never restores it, so a later file that relies on the defaults resolves
bobbitStateDir() under the leaked root and fails on a real mkdir under
/memfs (mcp-meta-policy: EACCES/ENOENT). The victim moves with worker
sharding, which is why it passed on the dev box but failed on CI, and retry
cannot mask it — the retry reruns in the same fork.

A setup-file beforeAll runs once per worker, not per file, and beforeEach
runs after the file's own beforeAll. onCollectStart runs per file before its
module loads, so the runner resets the baseline there. env is left alone —
the fork-scoped gateway owns BOBBIT_* as intentional fork-wide state.

Repro (fails on master, passes here):
  VITEST_MAX_WORKERS=1 vitest run --project v2-core \
    tests2/core/docker-args.test.ts tests2/core/mcp-meta-policy.test.ts
@dev-milos
dev-milos marked this pull request as ready for review July 23, 2026 10:24
@dev-milos
dev-milos requested a review from SuuBro as a code owner July 23, 2026 10:24
@greptile-apps

greptile-apps Bot commented Jul 23, 2026

Copy link
Copy Markdown

Greptile Summary

Adds a custom per-file Vitest runner that resets shared directory state before collecting tests, and enables it for the non-isolated core and integration projects.

Confidence Score: 5/5

The PR appears safe to merge, with the reset scoped to the two non-isolated Vitest projects that share directory singleton state.

The runner restores both implicated in-memory directory states at each file boundary, while projects using module isolation do not require the custom runner.

Important Files Changed

Filename Overview
tests2/harness/file-boundary-runner.ts Introduces a per-file collection hook that restores the project-root and agent-directory singletons before each non-isolated test file loads.
vitest.config.ts Configures the runner for the two projects that inherit isolate:false while leaving isolated projects unchanged.

Sequence Diagram

sequenceDiagram
  participant V as Vitest worker
  participant R as FileBoundaryRunner
  participant D as Directory singletons
  participant T as Test file
  V->>R: onCollectStart(file)
  R->>D: setProjectRoot(BASELINE_CWD)
  R->>D: resetAgentDirStateForTests()
  R->>V: super.onCollectStart(file)
  V->>T: collect and execute file
Loading

Reviews (1): Last reviewed commit: "Reset dir singletons at file boundaries ..." | Re-trigger Greptile

@pavlovic-ivan
pavlovic-ivan merged commit d53e950 into G-Research:master Jul 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants