fix(FN-4811): unblock @fusion/engine typecheck so verification bootstrap can run

Symptom found while investigating 'tasks are still struggling': every
in-review task hitting pre-merge deterministic verification failed with

  [verification:bootstrap] bootstrap preamble failed (exit 2):
  [test-bootstrap] FAILED: workspace dist artifact rebuild did not complete.
  [test-bootstrap] command: pnpm --filter @fusion/engine build

Because pnpm --filter @fusion/engine build hit 17 TS errors from a prior
autonomous-agent refactor introducing a RemovalReason enum-like object
and two new audit event types. The bootstrap preamble is run by the
merger before every direct-merge verification, so a broken engine
typecheck blocked EVERY task from merging.

Fixes:

1. Duplicate RemovalReason re-export in worktree-pool.ts

   Both  and
    were present for the same identifier,
   producing TS2300 'Duplicate identifier'. RemovalReason is a const
   object with derived type (typeof-keyof pattern), so a single value
   export covers both kinds; the type-only re-export was redundant.

2. GitMutationType union missing the FN-4811 audit event types

   merger.ts and worktree-backend.ts were emitting
   'worktree:removal-refused-active-session' and
   'worktree:removal-forced-over-active-session' audit events, but the
   union in run-audit.ts didn't include them. Added both.

3. self-healing.test.ts vi.mock had wrong RemovalReason keys

   The mock only exposed 5 keys (SelfHealing*) but production code
   references HardCancel, Executor*, Merger*, PoolPrune, etc. Calls
   like removeWorktree({ reason: RemovalReason.MergerPostMerge }) were
   getting reason=undefined, producing confusing 'cannot remove
   worktree: [vitest] No RemovalReason export is defined on mock'
   error messages. Updated the mock to mirror the production const
   exactly.

4. worktree-backend.test.ts removeWorktree calls missing required reason

   The new contract makes reason: RemovalReason a required field on
   removeWorktree's input. Five existing test cases were missing it;
   added reason: RemovalReason.MergerCleanup to each.

5. integrity-warning-persisted-dedup.test.ts Settings cast

   The test's makeStore helper cast a partial settings object to
   Settings; TS rejected the narrowed type. Cast through unknown.

Verification:

  - pnpm --filter @fusion/engine build: clean
  - pnpm lint: clean
  - pnpm build (full workspace): clean
  - pnpm --filter @fusion/engine test: 5045 pass, 1 pre-existing
    aiMergeTask real-git timeout flake, 1 skipped

With this fix, the verification bootstrap can complete and the merger
can finalize tasks again.

Fusion-Task-Id: FN-4811
This commit is contained in:
Fusion
2026-05-16 17:23:15 -07:00
parent be9baad3bb
commit 86237c9a50
6 changed files with 42 additions and 2 deletions

View File

@@ -38,7 +38,7 @@ function makeStore(task: Task, events: unknown[] = []): TaskStore & EventEmitter
commitAuthorEnabled: false,
useAiMergeCommitSummary: false,
baseBranch: "main",
} as Settings;
} as unknown as Settings;
return Object.assign(emitter, {
getSettings: vi.fn(async () => mergedSettings),
getTask: vi.fn(async () => task),

View File

@@ -51,6 +51,25 @@ vi.mock("node:fs", async (importOriginal) => {
vi.mock("../worktree-pool.js", () => ({
WorktreePool: vi.fn(),
// FN-4811: Must mirror the production `RemovalReason` const in worktree-backend.ts
// exactly — every key referenced as `RemovalReason.X` in production code (self-healing,
// executor, merger) needs to resolve here, otherwise removeWorktree({ reason: undefined })
// gets passed through and the gate logic fails with confusing 'reason is undefined' errors.
RemovalReason: {
HardCancel: "hard-cancel",
ExecutorTransientRetry: "executor-transient-retry",
ExecutorStuckKilled: "executor-stuck-killed",
ExecutorDispose: "executor-dispose",
StepSessionCleanup: "step-session-cleanup",
MergerPostMerge: "merger-post-merge",
MergerCleanup: "merger-cleanup",
SelfHealingReclaim: "self-healing-reclaim",
SelfHealingStaleActiveBranch: "self-healing-stale-active-branch",
SelfHealingBranchConflict: "self-healing-branch-conflict",
SelfHealingOrphanRescue: "self-healing-orphan-rescue",
SelfHealingIdleSweep: "self-healing-idle-sweep",
PoolPrune: "pool-prune",
},
scanIdleWorktrees: vi.fn().mockResolvedValue([]),
cleanupOrphanedWorktrees: vi.fn().mockResolvedValue(0),
scanOrphanedBranches: vi.fn().mockResolvedValue([]),

View File

@@ -5,6 +5,7 @@ import {
WorktrunkWorktreeBackend,
removeWorktree,
resolveWorktreeBackend,
RemovalReason,
} from "../worktree-backend.js";
const { execMock, accessMock, existsSyncMock } = vi.hoisted(() => {
@@ -527,6 +528,7 @@ describe("removeWorktree", () => {
worktreePath: "/repo/.worktrees/fn-1",
settings: {},
audit,
reason: RemovalReason.SelfHealingReclaim,
});
expect(execMock).toHaveBeenCalledWith(
@@ -546,6 +548,7 @@ describe("removeWorktree", () => {
settings: { worktrunk: { enabled: true, binaryPath: "worktrunk", onFailure: "fail" } as any },
audit,
taskId: "FN-1",
reason: RemovalReason.SelfHealingReclaim,
});
expect(audit.git).toHaveBeenCalledWith({ type: "worktree:worktrunk-remove", target: "/repo/.worktrees/fn-1" });
@@ -562,6 +565,7 @@ describe("removeWorktree", () => {
worktreePath: "/repo/.worktrees/fn-1",
settings: { worktrunk: { enabled: true, binaryPath: "worktrunk", onFailure: "fallback-native" } as any },
audit,
reason: RemovalReason.SelfHealingReclaim,
});
expect(audit.git).toHaveBeenCalledWith(
@@ -580,6 +584,7 @@ describe("removeWorktree", () => {
rootDir: "/repo",
worktreePath: "/repo/.worktrees/fn-1",
settings: { worktrunk: { enabled: true, binaryPath: "worktrunk", onFailure: "fail" } as any },
reason: RemovalReason.SelfHealingReclaim,
}),
).rejects.toMatchObject({ code: "worktrunk_operation_failed", operation: "remove" });
});
@@ -590,6 +595,7 @@ describe("removeWorktree", () => {
rootDir: "/repo",
worktreePath: "/repo/.worktrees/fn-1",
settings: { worktrunk: { enabled: true, onFailure: "fail" } as any },
reason: RemovalReason.SelfHealingReclaim,
}),
).rejects.toMatchObject({ code: "worktrunk_binary_missing", operation: "remove" });
});

View File

@@ -92,6 +92,8 @@ export type GitMutationType =
| "worktree:worktrunk-fallback"
| "worktree:worktrunk-failure"
| "worktree:worktrunk-fallback-native"
| "worktree:removal-refused-active-session"
| "worktree:removal-forced-over-active-session"
| "branch:create"
| "branch:delete"
| "branch:checkout"

View File

@@ -22,7 +22,7 @@ export {
removeWorktree,
resolveWorktreeBackend,
} from "./worktree-backend.js";
export type { WorktreeBackend, WorktreeBackendKind, RemovalReason } from "./worktree-backend.js";
export type { WorktreeBackend, WorktreeBackendKind } from "./worktree-backend.js";
export { RemovalReason } from "./worktree-backend.js";
// Re-export worktrunk installer types for convenience.