The best way to control Claude Code, Codex and more from any computer or phone
fix(server): t3_thread_launch refuses a defaulted worktree launch when Git cannot read HEAD (#38459)
Julius Marminge committed
Nov 19, 2026 at 12:16 UTC
178a2f1cd4cd708532be5caa90bacc0d96c4fb6a
2 files changed
+88
-11
apps/server/src/mcp/toolkits/project/handlers.test.ts
+64
@@ -758,6 +758,70 @@ it.effect("a launch that omits its workspace follows the new-threads-in-a-worktr
758
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
759
);
760
761
+it.effect("a defaulted worktree launch stays shared only when Git says HEAD has no commit", () =>
762
+ Effect.gen(function* () {
763
+ const fileSystem = yield* FileSystem.FileSystem;
764
+ const commitArgs = ["-c", "user.email=t@t", "-c", "user.name=t", "commit", "--allow-empty"];
765
+ for (const [checkout, expected] of [
766
+ ["not a repository", "root"],
767
+ ["no commit yet", "root"],
768
+ ["branch tip object missing", "worktree"],
769
+ ["git refuses the repository", "refused"],
770
+ ] as const) {
771
+ const repo = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-launch-head-" });
772
+ const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
773
+ const { projectId, dependencies } = clientLaunchHarness({
774
+ runtimeModeCeiling: "auto",
775
+ launched,
776
+ workspaceRoot: repo,
777
+ defaultThreadEnvMode: "worktree",
778
+ });
779
+ const git = yield* GitVcsDriver.GitVcsDriver.pipe(Effect.provide(dependencies));
780
+ if (checkout !== "not a repository")
781
+ yield* git.execute({
782
+ operation: "test.setupRepo",
783
+ cwd: repo,
784
+ args: ["init", "-b", "main"],
785
+ });
786
+ if (checkout === "branch tip object missing")
787
+ yield* fileSystem.writeFileString(`${repo}/.git/refs/heads/main`, `${"1".repeat(40)}\n`);
788
+ if (checkout === "git refuses the repository") {
789
+ yield* git.execute({
790
+ operation: "test.setupRepo",
791
+ cwd: repo,
792
+ args: [...commitArgs, "-m", "init"],
793
+ });
794
+ // Git quotes the broken file's path in its refusal; that path must not read as "not a repository".
795
+ yield* fileSystem.writeFileString(`${repo}/not a git repository`, "[core\n");
796
+ yield* fileSystem.writeFileString(
797
+ `${repo}/.git/config`,
798
+ `[include]\n\tpath = ${repo}/not a git repository\n`,
799
+ );
800
+ }
801
+ const toolkit = yield* ProjectToolkit.pipe(
802
+ Effect.provide(
803
+ McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(
804
+ Layer.provide(dependencies),
805
+ ),
806
+ ),
807
+ );
808
+ const result = (yield* toolkit
809
+ .handle("t3_thread_launch", { title: "Fix", projectId })
810
+ .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies))).at(-1)?.result;
811
+
812
+ if (expected === "refused") {
813
+ expect(result, checkout).toMatchObject({ code: "orchestration_error" });
814
+ expect(launched, checkout).toHaveLength(0);
815
+ } else {
816
+ expect(
817
+ launched.map((launch) => launch.workspaceStrategy.type),
818
+ checkout,
819
+ ).toEqual([expected]);
820
+ }
821
+ }
822
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
823
+);
824
+
825
it.effect("a launch into a new worktree creates nothing when its branch collides", () =>
826
Effect.gen(function* () {
827
const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
apps/server/src/mcp/toolkits/project/handlers.ts
+24
-11
@@ -116,6 +116,10 @@ const projectFileThreadEnvMode = Effect.fn("mcp.projectFileThreadEnvMode")(funct
116
).value;
117
});
118
119
+// Anchored to the fatal line: other diagnostics, such as a dubious-ownership refusal, quote the path.
120
+const NON_REPOSITORY_FATAL =
121
+ /^fatal: (?:not a git repository|gitfile does not point to a valid repository)/im;
122
+
123
/** Branches from the checkout's current branch, as the composer does; a checkout with no commit or a detached HEAD stays shared. */
124
const defaultWorktreeStrategy = Effect.fn("mcp.defaultWorktreeStrategy")(function* (
125
workspaceRoot: string,
@@ -125,11 +129,25 @@ const defaultWorktreeStrategy = Effect.fn("mcp.defaultWorktreeStrategy")(functio
129
GitVcsDriver.GitVcsDriver
130
> {
131
const git = yield* GitVcsDriver.GitVcsDriver;
128
- const hasCommit = yield* git.resolveCommit({ cwd: workspaceRoot, revision: "HEAD" }).pipe(
129
- Effect.as(true),
130
- Effect.orElseSucceed(() => false),
131
- );
132
- if (!hasCommit) return undefined;
132
+ const unreadable = new OrchestratorMcpFailure({
133
+ code: "orchestration_error",
134
+ message:
135
+ "Could not read the project's current branch for the new worktree. No thread was created. Retry, or pass workspaceStrategy.",
136
+ });
137
+ const commit = yield* git
138
+ .execute({
139
+ operation: "mcp.defaultWorktreeStrategy",
140
+ cwd: workspaceRoot,
141
+ // Not peeled: a branch whose tip object is missing still resolves, so only an unborn HEAD exits 1.
142
+ args: ["rev-parse", "--verify", "--quiet", "HEAD"],
143
+ allowNonZeroExit: true,
144
+ timeoutMs: 5_000,
145
+ })
146
+ .pipe(Effect.orElseSucceed(() => null));
147
+ // Only Git saying "no commit yet" (exit 1) or "not a repository" keeps the launch shared.
148
+ if (commit?.exitCode === 1 || (commit !== null && NON_REPOSITORY_FATAL.test(commit.stderr)))
149
+ return undefined;
150
+ if (commit?.exitCode !== 0) return yield* unreadable;
151
// symbolic-ref skips the index, so a held index lock cannot drop the launch into the shared checkout.
152
const head = yield* git
153
.execute({
@@ -142,12 +160,7 @@ const defaultWorktreeStrategy = Effect.fn("mcp.defaultWorktreeStrategy")(functio
160
.pipe(Effect.orElseSucceed(() => null));
161
if (head?.exitCode === 1) return undefined;
162
const branch = head?.exitCode === 0 ? head.stdout.trim() : "";
145
- if (branch.length === 0)
146
- return yield* new OrchestratorMcpFailure({
147
- code: "orchestration_error",
148
- message:
149
- "Could not read the project's current branch for the new worktree. No thread was created. Retry, or pass workspaceStrategy.",
150
- });
163
+ if (branch.length === 0) return yield* unreadable;
164
return { type: "worktree" as const, baseRef: branch };
165
});
166