The best way to control Claude Code, Codex and more from any computer or phone
fix(server): a default branch the remote deleted no longer starts worktree threads (#36268)
Julius Marminge committed
Nov 13, 2026 at 12:50 UTC
1da05a8a9833549b4ca1ef009cb27f81041e7427
5 files changed
+62
-11
apps/server/src/mcp/toolkits/project/handlers.ts
+1
-1
@@ -65,7 +65,7 @@ function worktreeCheckHint(
65
}
66
return `Pass a branch, tag, or commit that exists there${
67
strategy.startFromOrigin === true
68
- ? "; the project has no usable remote to fetch it from."
68
+ ? "; no remote of the project has it either."
69
: ", or set startFromOrigin:true if it exists only on a remote."
70
}`;
71
}
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+7
@@ -2781,6 +2781,12 @@ it.effect.each([
2781
startFromOrigin: true,
2782
origin: false,
2783
},
2784
+ {
2785
+ name: "was deleted on its remote",
2786
+ baseRef: "origin/development",
2787
+ startFromOrigin: true,
2788
+ origin: true,
2789
+ },
2790
{
2791
name: "is a previous checkout there never was",
2792
baseRef: "-",
@@ -2791,6 +2797,7 @@ it.effect.each([
2797
Effect.gen(function* () {
2798
const harness = makeHarness({
2799
hasCommit: ({ refName }) => Effect.succeed(refName === "HEAD"),
2800
+ fetchWorktreeBaseCommit: () => Effect.succeed(null),
2801
git: {
2802
resolveWorktreeBaseRemote: () =>
2803
Effect.succeed(testCase.origin ? { remoteName: "origin", branchName: "main" } : null),
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+9
-5
@@ -377,11 +377,15 @@ const make = Effect.gen(function* () {
377
// `git worktree add` reads `-` as the previous checkout.
378
if (yield* git.hasCommit({ cwd, refName: baseRef === "-" ? "@{-1}" : baseRef }))
379
return true;
380
- if (
381
- strategy.startFromOrigin === true &&
382
- (yield* git.resolveWorktreeBaseRemote({ cwd, refName: baseRef })) !== null
383
- )
384
- return true;
380
+ if (strategy.startFromOrigin === true) {
381
+ const remote = yield* git.resolveWorktreeBaseRemote({ cwd, refName: baseRef });
382
+ // The remote may have deleted the branch; ask it rather than trusting the name.
383
+ if (
384
+ remote !== null &&
385
+ (yield* git.fetchWorktreeBaseCommit({ cwd, refName: baseRef, remote })) !== null
386
+ )
387
+ return true;
388
+ }
389
// git worktree add accepts remote-tracking branches; let Git decide when any matching ref exists.
390
if (yield* git.hasRefNamed({ cwd, refName: baseRef })) return true;
391
return !(yield* git.hasCommit({ cwd, refName: "HEAD" }));
apps/server/src/vcs/GitVcsDriverCore.test.ts
+22
@@ -4690,6 +4690,28 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4690
}),
4691
);
4692
4693
+ it.effect("does not report a default branch the remote deleted after the clone", () =>
4694
+ Effect.gen(function* () {
4695
+ const cwd = yield* makeTmpDir();
4696
+ const remote = yield* makeTmpDir("git-vcs-driver-origin-");
4697
+ yield* initRepoWithCommit(cwd);
4698
+ yield* git(remote, ["init", "--bare"]);
4699
+ yield* git(cwd, ["branch", "-M", "development"]);
4700
+ yield* git(cwd, ["remote", "add", "origin", remote]);
4701
+ yield* git(cwd, ["push", "origin", "development", "development:main"]);
4702
+ yield* git(cwd, ["remote", "set-head", "origin", "development"]);
4703
+ yield* git(remote, ["--git-dir=.", "symbolic-ref", "HEAD", "refs/heads/main"]);
4704
+ yield* git(cwd, ["push", "origin", "--delete", "development"]);
4705
+ yield* git(cwd, ["fetch", "--prune", "origin"]);
4706
+
4707
+ const driver = yield* GitVcsDriver.GitVcsDriver;
4708
+ const defaultBranch = yield* driver.resolveDefaultBranchName(cwd, "origin");
4709
+
4710
+ assert.notEqual(defaultBranch, "development");
4711
+ assert.isNull(yield* driver.resolveWorktreeBaseRemote({ cwd, refName: "origin/HEAD" }));
4712
+ }),
4713
+ );
4714
+
4715
it.effect.each(["checkout", "bare-backed worktree"] as const)(
4716
"uses the only remote's HEAD for default-branch detection without origin in a %s",
4717
(layout) =>
apps/server/src/vcs/GitVcsDriverCore.ts
+23
-5
@@ -551,7 +551,7 @@ function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string):
551
return null;
552
}
553
const refName = trimmed.slice(prefix.length).trim();
554
- return refName.length > 0 ? refName : null;
554
+ return refName.length > 0 && refName !== "HEAD" ? refName : null;
555
}
556
557
function isMissingGitCwdError(error: GitCommandError): boolean {
@@ -1719,7 +1719,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1719
executeGit(
1720
"GitVcsDriver.resolveDefaultBranchName",
1721
cwd,
1722
- [...gitDirArgs, "symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
1722
+ // Unlike `symbolic-ref`, this fails when the remote deleted the branch HEAD points at.
1723
+ [
1724
+ ...gitDirArgs,
1725
+ "rev-parse",
1726
+ "--verify",
1727
+ "--quiet",
1728
+ "--symbolic-full-name",
1729
+ `refs/remotes/${remoteName}/HEAD`,
1730
+ ],
1731
{ allowNonZeroExit: true },
1732
).pipe(
1733
Effect.map((result) => {
@@ -4220,7 +4228,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4228
executeGit(
4229
"GitVcsDriver.listRefs.defaultRef",
4230
fetchCwd,
4223
- [...gitDirArgs, "symbolic-ref", "refs/remotes/origin/HEAD"],
4231
+ [
4232
+ ...gitDirArgs,
4233
+ "rev-parse",
4234
+ "--verify",
4235
+ "--quiet",
4236
+ "--symbolic-full-name",
4237
+ "refs/remotes/origin/HEAD",
4238
+ ],
4239
{
4240
timeoutMs: 5_000,
4241
allowNonZeroExit: true,
@@ -4248,7 +4263,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4263
}
4264
const defaultBranch =
4265
defaultRefResult.exitCode === 0
4251
- ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
4266
+ ? parseDefaultBranchFromRemoteHeadRef(defaultRefResult.stdout, "origin")
4267
: null;
4268
const parsedWorktreeEntries =
4269
worktreeListResult.exitCode === 0
@@ -5050,7 +5065,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5065
remoteName: "origin",
5066
branchName: input.refName,
5067
};
5053
- return remoteNames.includes(remoteName) ? { remoteName, branchName } : null;
5068
+ // `<remote>/HEAD` is the remote's default-branch pointer, not a branch it can serve.
5069
+ return remoteNames.includes(remoteName) && branchName !== "HEAD"
5070
+ ? { remoteName, branchName }
5071
+ : null;
5072
});
5073
5074
const fetchWorktreeBaseCommit: GitVcsDriver.GitVcsDriver["Service"]["fetchWorktreeBaseCommit"] =