Files
fusion/packages/engine/src/__tests__/worktree-reanchor-nested-root.test.ts
gsxdsm b154844286 FN-5772: re-anchor nested task worktree paths
Fix executor worktree invariant handling by re-anchoring nested task worktree paths to the actual git top-level.

- add nested worktree root detection that only re-anchors when the top-level is a registered worktree inside the configured worktrees directory
- update executor liveness gating and verifyWorktreeInvariants to persist re-anchored task.worktree values and retry checks safely
- emit a new run-audit git mutation (worktree:reanchored) and add reliability tests/docs coverage plus a patch changeset

Files changed:
 .changeset/fn-5772-worktree-reanchor.md            |  7 +++
 docs/architecture.md                               |  4 +-
 packages/engine/src/__tests__/reliability-interactions/executor-liveness-gate.test.ts                 | 33 ++++++++++
 packages/engine/src/__tests__/verify-worktree-invariants-missing.test.ts     | 62 +++++++++++++++++-
 packages/engine/src/__tests__/worktree-reanchor-nested-root.test.ts          | 73 ++++++++++++++++++++++
 packages/engine/src/executor.ts                    | 54 ++++++++++++++--
 packages/engine/src/run-audit.ts                   |  1 +
 packages/engine/src/worktree-pool.ts               | 63 +++++++++++++++++++
 8 files changed, 290 insertions(+), 7 deletions(-)

Fusion-Task-Id: FN-5772

Fusion-Task-Lineage: 475de161-7bc1-4359-bb76-20c4c07196d9
2026-05-31 08:32:01 -07:00

74 lines
3.5 KiB
TypeScript

import { beforeEach, describe, expect, it } from "vitest";
import "./executor-test-helpers.js";
import { detectNestedWorktreeRoot } from "../worktree-pool.js";
import { mockedExecSync, mockedExistsSync, resetExecutorMocks } from "./executor-test-helpers.js";
describe("detectNestedWorktreeRoot", () => {
beforeEach(() => {
resetExecutorMocks();
mockedExistsSync.mockReturnValue(true);
});
it("re-anchors when task worktree is a nested subdirectory of a registered worktree root", async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (cmd.includes("rev-parse --show-toplevel")) return Buffer.from("/repo/.worktrees/gentle-flame\n");
if (cmd.includes("worktree list --porcelain")) {
return Buffer.from("worktree /repo\nbranch refs/heads/main\n\nworktree /repo/.worktrees/gentle-flame\nbranch refs/heads/fusion/fn-1\n");
}
return Buffer.from("");
});
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame/packages/core");
expect(result).toEqual({ reanchored: true, root: "/repo/.worktrees/gentle-flame" });
});
it("does not re-anchor when git top-level is repo root", async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (cmd.includes("rev-parse --show-toplevel")) return Buffer.from("/repo\n");
return Buffer.from("");
});
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame/packages/core");
expect(result).toEqual({ reanchored: false, reason: "toplevel_is_repo_root" });
});
it("does not re-anchor when top-level is outside configured worktrees dir", async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (cmd.includes("rev-parse --show-toplevel")) return Buffer.from("/tmp/other\n");
return Buffer.from("");
});
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame/packages/core");
expect(result).toEqual({ reanchored: false, reason: "toplevel_outside_configured_dir" });
});
it("does not re-anchor when top-level worktree is not registered", async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (cmd.includes("rev-parse --show-toplevel")) return Buffer.from("/repo/.worktrees/gentle-flame\n");
if (cmd.includes("worktree list --porcelain")) {
return Buffer.from("worktree /repo\nbranch refs/heads/main\n\nworktree /repo/.worktrees/some-other\nbranch refs/heads/fusion/fn-2\n");
}
return Buffer.from("");
});
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame/packages/core");
expect(result).toEqual({ reanchored: false, reason: "toplevel_not_registered_worktree" });
});
it("does not re-anchor when worktree path is missing", async () => {
mockedExistsSync.mockReturnValue(false);
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame/packages/core");
expect(result).toEqual({ reanchored: false, reason: "worktree_missing" });
});
it("does not re-anchor when worktree path is already at top-level", async () => {
mockedExecSync.mockImplementation((cmd: string) => {
if (cmd.includes("rev-parse --show-toplevel")) return Buffer.from("/repo/.worktrees/gentle-flame\n");
return Buffer.from("");
});
const result = await detectNestedWorktreeRoot("/repo", "/repo/.worktrees/gentle-flame");
expect(result).toEqual({ reanchored: false, reason: "already_at_toplevel" });
});
});