Skip to content

Commit 8c679e5

Browse files
committed
fix(mcp): fail the handoff when the worktree HEAD cannot be read
Use symbolic-ref --quiet so a detached HEAD exits 1 and any other git failure exits 128. Only the former maps to a null branch.
1 parent 53ea48c commit 8c679e5

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎apps/server/src/mcp/toolkits/worktree/handlers.test.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,20 @@ const repositories: Record<string, VcsRepositoryIdentity> = {
109109
};
110110

111111
const makeHarness = Effect.fn("makeWorktreeToolkitHarness")(function* (
112-
options: { readonly headBranch?: string | null } = {},
112+
options: { readonly headBranch?: string | null; readonly headExitCode?: number } = {},
113113
) {
114114
const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
115115
const headBranch = options.headBranch === undefined ? "feature" : options.headBranch;
116+
// `git symbolic-ref --quiet` exits 1 for a detached HEAD and 128 for other failures.
117+
const headExitCode = options.headExitCode ?? (headBranch === null ? 1 : 0);
116118
const dispatch: OrchestrationEngineShape["dispatch"] = (command) =>
117119
Ref.update(commands, (recorded) => [...recorded, command]).pipe(Effect.as({ sequence: 1 }));
118120
const driver = {
119121
execute: () =>
120122
Effect.succeed({
121-
exitCode: headBranch === null ? 128 : 0,
122-
stdout: headBranch === null ? "" : `${headBranch}\n`,
123-
stderr: headBranch === null ? "fatal: ref HEAD is not a symbolic ref\n" : "",
123+
exitCode: headExitCode,
124+
stdout: headExitCode === 0 ? `${headBranch}\n` : "",
125+
stderr: headExitCode === 128 ? "fatal: not a git repository\n" : "",
124126
stdoutTruncated: false,
125127
stderrTruncated: false,
126128
}),
@@ -226,6 +228,15 @@ describe("worktree toolkit handlers", () => {
226228
}),
227229
);
228230

231+
it.effect("fails without touching the thread when HEAD cannot be read", () =>
232+
Effect.gen(function* () {
233+
const harness = yield* makeHarness({ headExitCode: 128 });
234+
const error = yield* harness.call({ path: LINKED_WORKTREE }).pipe(Effect.flip);
235+
expect(error).toMatchObject({ _tag: "WorktreeHandoffFailedError" });
236+
expect(yield* Ref.get(harness.commands)).toEqual([]);
237+
}),
238+
);
239+
229240
it.effect.each([
230241
{ path: "relative/worktree", detail: "Pass the worktree's absolute path." },
231242
{ path: "/workspace/missing", detail: "does not exist or is not inside a git worktree" },

‎apps/server/src/mcp/toolkits/worktree/handlers.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { CommandId, type ThreadId, type VcsRepositoryIdentity } from "@t3tools/contracts";
1+
import {
2+
CommandId,
3+
VcsProcessExitError,
4+
type ThreadId,
5+
type VcsRepositoryIdentity,
6+
} from "@t3tools/contracts";
27
import * as Cause from "effect/Cause";
38
import * as Crypto from "effect/Crypto";
49
import * as Effect from "effect/Effect";
@@ -100,15 +105,28 @@ const make = Effect.gen(function* () {
100105
});
101106
}
102107

108+
// With --quiet a detached HEAD exits 1 and any other failure exits 128.
103109
// A detached HEAD has no branch; the thread then shows the worktree without one.
110+
const operation = "WorktreeToolkit.headBranch";
104111
const head = yield* target.driver
105112
.execute({
106-
operation: "WorktreeToolkit.headBranch",
113+
operation,
107114
cwd: worktreePath,
108-
args: ["symbolic-ref", "--short", "HEAD"],
115+
args: ["symbolic-ref", "--quiet", "--short", "HEAD"],
109116
allowNonZeroExit: true,
110117
})
111118
.pipe(Effect.mapError((cause) => new WorktreeHandoffFailedError({ cause })));
119+
if (head.exitCode !== 0 && head.exitCode !== 1) {
120+
return yield* new WorktreeHandoffFailedError({
121+
cause: new VcsProcessExitError({
122+
operation,
123+
command: "git symbolic-ref",
124+
cwd: worktreePath,
125+
exitCode: head.exitCode,
126+
detail: head.stderr.trim() || "Could not read the worktree's HEAD.",
127+
}),
128+
});
129+
}
112130
const branch = head.exitCode === 0 ? head.stdout.trim() || null : null;
113131

114132
yield* engine

0 commit comments

Comments
 (0)