diff --git a/packages/engine/src/__tests__/reliability-interactions/_helpers.ts b/packages/engine/src/__tests__/reliability-interactions/_helpers.ts index b003931736..82a7416a3d 100644 --- a/packages/engine/src/__tests__/reliability-interactions/_helpers.ts +++ b/packages/engine/src/__tests__/reliability-interactions/_helpers.ts @@ -12,6 +12,22 @@ export function git(cwd: string, command: string): string { return execSync(command, { cwd, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }).trim(); } +export function reliabilityTestTempParent(): string { + /* + FNXC:ReliabilityFixtures 2026-06-20-21:24: + FN-6817 traced merge-reuse-task-worktree flakes to reliability fixtures escaping the per-invocation Vitest worker root. + Keep project roots and their `-worktrees` siblings under FUSION_TEST_WORKER_ROOT so concurrent package lanes and teardown cannot collide through the shared OS temp root. + */ + return process.env.FUSION_TEST_WORKER_ROOT ?? tmpdir(); +} + +function assertInitializedGitRepository(rootDir: string): void { + const insideWorkTree = git(rootDir, "git rev-parse --is-inside-work-tree"); + if (insideWorkTree !== "true") { + throw new Error(`Reliability fixture git init did not create a usable repository at ${rootDir}`); + } +} + export type ReliabilityFixture = { rootDir: string; store: TaskStore; @@ -40,8 +56,10 @@ export async function makeReliabilityFixture(input: { task?: Partial; settings?: Partial; } = {}): Promise { - const rootDir = await mkdtemp(join(tmpdir(), "fusion-reliability-")); + const rootDir = await mkdtemp(join(reliabilityTestTempParent(), "fusion-reliability-")); + const worktreeRoot = `${rootDir}-worktrees`; git(rootDir, "git init -b main"); + assertInitializedGitRepository(rootDir); git(rootDir, 'git config user.email "test@example.com"'); git(rootDir, 'git config user.name "Test User"'); await writeFile(join(rootDir, "README.md"), "# fixture\n", "utf-8"); @@ -87,6 +105,7 @@ export async function makeReliabilityFixture(input: { manager.stop(); store.close(); await rm(rootDir, { recursive: true, force: true }); + await rm(worktreeRoot, { recursive: true, force: true }); }, writeAndCommit: async (file, content, message) => { const absolute = join(rootDir, file); diff --git a/packages/engine/src/__tests__/reliability-interactions/merge-runner-spawn-enoent-prevention.test.ts b/packages/engine/src/__tests__/reliability-interactions/merge-runner-spawn-enoent-prevention.test.ts index 51de441fbe..596da79f74 100644 --- a/packages/engine/src/__tests__/reliability-interactions/merge-runner-spawn-enoent-prevention.test.ts +++ b/packages/engine/src/__tests__/reliability-interactions/merge-runner-spawn-enoent-prevention.test.ts @@ -1,5 +1,7 @@ -import { mkdir, rm, writeFile } from "node:fs/promises"; -import { join } from "node:path"; +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, sep } from "node:path"; import { beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("../../pi.js", () => ({ @@ -82,6 +84,35 @@ describe("FN-6278 reliability interactions: merge runner cwd preflight", () => { executingTaskLock._clearForTest(); }); + it.skipIf(!hasGit)("FN-6817: roots the shared reliability fixture under the Vitest worker root", async () => { + const previousWorkerRoot = process.env.FUSION_TEST_WORKER_ROOT; + const workerRoot = await mkdtemp(join(tmpdir(), "fn-6817-worker-root-")); + process.env.FUSION_TEST_WORKER_ROOT = workerRoot; + let fixture: Awaited> | undefined; + + try { + fixture = await makeReliabilityFixture({ taskId: "FN-6817-RI-WORKER-ROOT" }); + const worktreeRoot = `${fixture.rootDir}-worktrees`; + await mkdir(worktreeRoot, { recursive: true }); + + expect(fixture.rootDir.startsWith(`${workerRoot}${sep}`)).toBe(true); + expect(worktreeRoot.startsWith(`${workerRoot}${sep}`)).toBe(true); + expect(git(fixture.rootDir, "git rev-parse --is-inside-work-tree")).toBe("true"); + + await fixture.cleanup(); + fixture = undefined; + expect(existsSync(worktreeRoot)).toBe(false); + } finally { + if (fixture) await fixture.cleanup(); + if (previousWorkerRoot === undefined) { + delete process.env.FUSION_TEST_WORKER_ROOT; + } else { + process.env.FUSION_TEST_WORKER_ROOT = previousWorkerRoot; + } + await rm(workerRoot, RM); + } + }); + it.skipIf(!hasGit)("reacquires before spawning git when the reuse worktree cwd vanished", async () => { const { fixture, rootDir, store, taskId, branch, worktreeRoot, worktreePath } = await setupReuseMergeFixture({ taskId: "FN-6278-RI-VANISHED",