Skip to content

Commit a2cd749

Browse files
fix(web): a new worktree thread opened during a clone bases on the real default branch
While a project is still cloning, git reports an unborn HEAD (`master`, `main` or init.defaultBranch) that the repository may never have. Two places recorded that name as the draft's branch: the live status sync in Current checkout mode, whose value New worktree mode then kept as the base, and the base seeding in New worktree mode. Nothing revisited it after the clone, so the first send failed with ref_not_found. The live sync now waits until the clone is done, and the base is seeded only from a ref the list actually holds. When the clone finishes, the picker reloads its refs past the server's snapshot of the empty repository, so the real default branch is seeded without a reload. Fixes #25719 Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent c83e78f commit a2cd749

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
resolveDraftEnvModeAfterBranchChange,
99
resolveEffectiveEnvMode,
1010
resolveBranchToolbarPrBranch,
11+
resolveDefaultWorktreeBaseBranch,
1112
resolveLocalCheckoutBranchMismatch,
1213
resolveLockedWorkspaceLabel,
1314
sanitizeNewRefName,
@@ -47,6 +48,24 @@ describe("resolveBranchToolbarPrBranch", () => {
4748
});
4849
});
4950

51+
describe("resolveDefaultWorktreeBaseBranch", () => {
52+
it("offers nothing while a clone has no refs, then the default branch once they are listed", () => {
53+
expect(resolveDefaultWorktreeBaseBranch([])).toBeNull();
54+
expect(
55+
resolveDefaultWorktreeBaseBranch([
56+
{ name: "feature", isDefault: false, current: true },
57+
{ name: "staging", isDefault: true, current: false },
58+
]),
59+
).toBe("staging");
60+
expect(
61+
resolveDefaultWorktreeBaseBranch([
62+
{ name: "other", isDefault: false, current: false },
63+
{ name: "feature", isDefault: false, current: true },
64+
]),
65+
).toBe("feature");
66+
});
67+
});
68+
5069
describe("resolveLocalCheckoutBranchMismatch", () => {
5170
it("detects when a local thread is associated with a different branch than the checkout", () => {
5271
expect(

‎apps/web/src/components/BranchToolbar.logic.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ export function resolveBranchToolbarValue(input: {
199199
return currentGitBranch ?? activeThreadBranch;
200200
}
201201

202+
/**
203+
* The base a New worktree thread gets without the user picking one. Only a listed ref qualifies:
204+
* during a clone the status names an unborn HEAD that no ref backs.
205+
*/
206+
export function resolveDefaultWorktreeBaseBranch(
207+
refs: ReadonlyArray<Pick<VcsRef, "name" | "isDefault" | "current">>,
208+
): string | null {
209+
return (refs.find((ref) => ref.isDefault) ?? refs.find((ref) => ref.current))?.name ?? null;
210+
}
211+
202212
export function resolveBranchTriggerLabel(input: {
203213
activeWorktreePath: string | null;
204214
effectiveEnvMode: EnvMode;

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { GitBranchIcon } from "lucide-react";
1818
import {
1919
useEffect,
20+
useEffectEvent,
2021
useImperativeHandle,
2122
useOptimistic,
2223
useState,
@@ -30,6 +31,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
3031
import { readLocalApi } from "../localApi";
3132
import { useOpenPrLink } from "../lib/openPullRequestLink";
3233
import { usePaginatedBranches } from "../state/queries";
34+
import { useProjectClone } from "../state/projectClones";
3335
import { readThreadShell, useProject, useQuietThreadShell } from "../state/entities";
3436
import { useEnvironmentQuery } from "../state/query";
3537
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
@@ -49,6 +51,7 @@ import {
4951
resolveBranchToolbarPrBranch,
5052
resolveBranchSelectionTarget,
5153
resolveBranchToolbarValue,
54+
resolveDefaultWorktreeBaseBranch,
5255
resolveDraftEnvModeAfterBranchChange,
5356
resolveEffectiveEnvMode,
5457
sanitizeNewRefName,
@@ -290,7 +293,6 @@ export function BranchToolbarBranchSelector({
290293
const createBranchItemValue = canCreateBranch
291294
? `__create_new_branch__:${trimmedBranchQuery}`
292295
: null;
293-
const defaultBranchName = refs.find((refName) => refName.isDefault)?.name ?? null;
294296
const pickerCreateBranchItemValue = hasExactBranchMatch ? null : createBranchItemValue;
295297
const branchPickerItems = [
296298
...(checkoutPullRequestItemValue ? [checkoutPullRequestItemValue] : []),
@@ -534,7 +536,20 @@ export function BranchToolbarBranchSelector({
534536

535537
const worktreeBaseBranchCandidate = isInitialBranchesLoadPending
536538
? null
537-
: (defaultBranchName ?? currentGitBranch);
539+
: resolveDefaultWorktreeBaseBranch(refs);
540+
541+
const projectCloneDone = useProjectClone(activeProjectRef)?.phase === "done";
542+
const refreshRefsAfterClone = useEffectEvent(() => {
543+
if (branchCwd === null) return;
544+
void refreshLocalStatusForPicker({
545+
environmentId,
546+
input: { cwd: branchCwd, localOnly: true },
547+
});
548+
});
549+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- refs listed while the project was cloning are empty; reload them past the server's snapshot when the clone finishes
550+
useEffect(() => {
551+
if (projectCloneDone) refreshRefsAfterClone();
552+
}, [projectCloneDone]);
538553

539554
// The server sets started threads' branches; defaulting here would detach the agent from its worktree.
540555
const threadStarted = serverSession !== null;

‎apps/web/src/components/GitActionsControl.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import {
9292
useVcsPullAction,
9393
} from "../state/sourceControlActions";
9494
import { useQuietThreadProjection, useQuietThreadShell } from "~/state/entities";
95+
import { useProjectClone } from "~/state/projectClones";
9596
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
9697
import { useEnvironmentQuery } from "~/state/query";
9798
import { useFilesystemReadAccess } from "~/state/filesystem";
@@ -124,6 +125,7 @@ import {
124125
type OfferedSourceControlProviders,
125126
} from "@t3tools/client-runtime/operations/projects";
126127
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
128+
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
127129
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
128130
import { useOpenLink } from "~/browser/useOpenLink";
129131

@@ -1148,9 +1150,17 @@ export default function GitActionsControl({
11481150
activeDraftThread?.envMode === "worktree" &&
11491151
activeDraftThread.worktreePath === null;
11501152

1153+
// Until a clone finishes, the status names an unborn HEAD that the repository may never have.
1154+
const activeProjectClone = useProjectClone(
1155+
activeDraftThread
1156+
? scopeProjectRef(activeDraftThread.environmentId, activeDraftThread.projectId)
1157+
: null,
1158+
);
1159+
const isProjectCloning = activeProjectClone !== null && activeProjectClone.phase !== "done";
1160+
11511161
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- persists the draft thread branch when the live git status subscription reports a different branch
11521162
useEffect(() => {
1153-
if (isGitActionRunning || isSelectingWorktreeBase || activeServerThread) {
1163+
if (isGitActionRunning || isSelectingWorktreeBase || activeServerThread || isProjectCloning) {
11541164
return;
11551165
}
11561166

@@ -1168,6 +1178,7 @@ export default function GitActionsControl({
11681178
activeDraftThread?.branch,
11691179
gitStatusForActions,
11701180
isGitActionRunning,
1181+
isProjectCloning,
11711182
isSelectingWorktreeBase,
11721183
]);
11731184

0 commit comments

Comments
 (0)