From fae9183687b09b1e45fc5a4c3648b59470a7da6f Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:15:10 -0500 Subject: [PATCH] fix(web): a new worktree's row names its real base, not the checkout's branch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the "New worktrees from" setting's branch was still loading (the branch list can take seconds), the row under the message box showed the checkout's current branch. The worktree then started from the setting's branch, which can be a different one. The row now shows, in order: a picked branch; the setting's branch once found; while the lookup runs, the branch the setting will give when that's knowable without the list (predictNewWorktreeBaseBranch: the checkout's branch for "current", the name for "named", the checkout's branch for "default" only when git status says it is the default); otherwise "the default branch" / "the current branch" in words, or "…" before the computer's settings arrive; and "pick a branch" when nothing fits, including when the lookup failed (which now counts as no base rather than loading). resolveBranchToolbarValue no longer falls back to the checkout's branch for a new worktree; its test that expected that fallback now expects the opposite. --- .../components/BranchToolbar.logic.test.ts | 17 +++++- .../web/src/components/BranchToolbar.logic.ts | 9 ++- .../BranchToolbarBranchSelector.tsx | 53 +++++++++++------ .../components/ChatView.sending.browser.tsx | 9 ++- .../web/src/hooks/useNewWorktreeBaseBranch.ts | 57 ++++++++++++++++--- packages/shared/src/git.test.ts | 20 +++++++ packages/shared/src/git.ts | 19 +++++++ 7 files changed, 155 insertions(+), 29 deletions(-) diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.ts index 1e347610d..1f50eec06 100644 --- a/apps/web/src/components/BranchToolbar.logic.test.ts +++ b/apps/web/src/components/BranchToolbar.logic.test.ts @@ -73,15 +73,26 @@ describe("resolveDraftEnvModeAfterBranchChange", () => { }); describe("resolveBranchToolbarValue", () => { - it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => { + it("shows the setting's base for a new worktree, not the checkout's branch", () => { expect( resolveBranchToolbarValue({ envMode: "worktree", activeWorktreePath: null, activeThreadBranch: null, - currentGitBranch: "main", + currentGitBranch: "feature/current", + newWorktreeBase: "main", }), ).toBe("main"); + // Not known yet: nothing, rather than the checkout's branch as a guess. + expect( + resolveBranchToolbarValue({ + envMode: "worktree", + activeWorktreePath: null, + activeThreadBranch: null, + currentGitBranch: "feature/current", + newWorktreeBase: null, + }), + ).toBeNull(); }); it("keeps an explicitly selected worktree base ref", () => { @@ -91,6 +102,7 @@ describe("resolveBranchToolbarValue", () => { activeWorktreePath: null, activeThreadBranch: "feature/base", currentGitBranch: "main", + newWorktreeBase: "develop", }), ).toBe("feature/base"); }); @@ -102,6 +114,7 @@ describe("resolveBranchToolbarValue", () => { activeWorktreePath: null, activeThreadBranch: "feature/base", currentGitBranch: "main", + newWorktreeBase: null, }), ).toBe("main"); }); diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.ts index 899bce645..a47ad0ddc 100644 --- a/apps/web/src/components/BranchToolbar.logic.ts +++ b/apps/web/src/components/BranchToolbar.logic.ts @@ -158,15 +158,22 @@ export function resolveDraftEnvModeAfterBranchChange(input: { return "local"; } +/** + * The branch shown for a thread. A new worktree shows its base: the one + * picked, else the one the "New worktrees from" setting gives + * (`newWorktreeBase`), never the checkout's branch in its place, which the + * worktree won't start from unless the setting says so. + */ export function resolveBranchToolbarValue(input: { envMode: EnvMode; activeWorktreePath: string | null; activeThreadBranch: string | null; currentGitBranch: string | null; + newWorktreeBase: string | null; }): string | null { const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input; if (envMode === "worktree" && !activeWorktreePath) { - return activeThreadBranch ?? currentGitBranch; + return activeThreadBranch ?? input.newWorktreeBase; } return currentGitBranch ?? activeThreadBranch; } diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 14d0f03c6..043b359a8 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -312,12 +312,41 @@ export function BranchToolbarBranchSelector({ [branchStatusQuery.data?.sourceControlProvider], ); const SourceControlIcon = sourceControlPresentation.Icon; + // A new worktree starts from the computer's "New worktrees from" branch + // until the user picks another. A thread that already exists holds it + // beside its pending mode rather than recording it as the thread's branch: + // only a pick the user makes is recorded. + const needsWorktreeBase = + effectiveEnvMode === "worktree" && + !activeWorktreePath && + (!activeThreadBranch || activeThreadBranchFromSetting); + const newWorktreeBase = useNewWorktreeBaseBranch({ + environmentId, + cwd: refsCwd, + currentGitBranch, + currentIsDefault: branchStatusQuery.data?.isDefaultRef === true, + canStartWorktree: !envLocked && !activeWorktreePath, + needed: needsWorktreeBase, + }); + const newWorktreeBaseBranch = newWorktreeBase.branch; const canonicalActiveBranch = resolveBranchToolbarValue({ envMode: effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch, + newWorktreeBase: needsWorktreeBase ? newWorktreeBase.expected : null, }); + // The setting's branch is still being looked up and can't be named yet: + // the row says which kind it is rather than guessing a name, or "…" while + // even the setting hasn't arrived. + const pendingWorktreeBaseLabel = + needsWorktreeBase && newWorktreeBase.branch === undefined && newWorktreeBase.expected === null + ? newWorktreeBase.from === undefined + ? "…" + : newWorktreeBase.from === "current" + ? "current branch" + : "default branch" + : null; const branchNames = useMemo(() => refs.map((refName) => refName.name), [refs]); const branchByName = useMemo( () => new Map(refs.map((refName) => [refName.name, refName] as const)), @@ -551,21 +580,6 @@ export function BranchToolbarBranchSelector({ runSwitchRef(pending.refName, selectionTarget.checkoutCwd, selectionTarget.nextWorktreePath); }; - // A new worktree starts from the computer's "New worktrees from" branch - // until the user picks another. A thread that already exists holds it - // beside its pending mode rather than recording it as the thread's branch: - // only a pick the user makes is recorded. - const needsWorktreeBase = - effectiveEnvMode === "worktree" && - !activeWorktreePath && - (!activeThreadBranch || activeThreadBranchFromSetting); - const newWorktreeBaseBranch = useNewWorktreeBaseBranch({ - environmentId, - cwd: refsCwd, - currentGitBranch, - canStartWorktree: !envLocked && !activeWorktreePath, - needed: needsWorktreeBase, - }); useEffect(() => { if (!needsWorktreeBase || !newWorktreeBaseBranch) { return; @@ -665,11 +679,16 @@ export function BranchToolbarBranchSelector({ const triggerLabel = annotateMissingCheckoutLabel( presentation === "sentence" - ? (resolvedActiveBranch ?? "pick a branch") + ? (resolvedActiveBranch ?? + (pendingWorktreeBaseLabel === null + ? "pick a branch" + : pendingWorktreeBaseLabel === "…" + ? "…" + : `the ${pendingWorktreeBaseLabel}`)) : getBranchTriggerLabel({ activeWorktreePath, effectiveEnvMode, - resolvedActiveBranch, + resolvedActiveBranch: resolvedActiveBranch ?? pendingWorktreeBaseLabel, }), selectedCheckoutMissing, ); diff --git a/apps/web/src/components/ChatView.sending.browser.tsx b/apps/web/src/components/ChatView.sending.browser.tsx index 868f6cdb9..d7d2e75f5 100644 --- a/apps/web/src/components/ChatView.sending.browser.tsx +++ b/apps/web/src/components/ChatView.sending.browser.tsx @@ -1421,6 +1421,10 @@ describe("ChatView (full app): first sends and worktree setup", () => { try { (await waitForButtonByText("Current checkout")).click(); await clickSelectItemContainingText("New worktree"); + // Until the list says which branch is the default, the row says that + // much, not the branch the checkout happens to be on. + await waitForButtonByText("From default branch"); + expect(findButtonByText("From feature/current")).toBeFalsy(); useComposerDraftStore.getState().setPrompt(THREAD_REF, "Ship it"); await waitForLayout(); (await waitForSendButton()).click(); @@ -1575,8 +1579,9 @@ describe("ChatView (full app): first sends and worktree setup", () => { (await waitForSendButton()).click(); await waitForLayout(); - // The send has its place while it waits for the setting's branch. - expect((await waitForButtonByText("From main")).disabled).toBe(true); + // The send has its place while it waits for the setting's branch, which + // the row already names (not the checkout's branch, main). + expect((await waitForButtonByText("From develop")).disabled).toBe(true); releaseSearch(); await vi.waitFor( diff --git a/apps/web/src/hooks/useNewWorktreeBaseBranch.ts b/apps/web/src/hooks/useNewWorktreeBaseBranch.ts index 9f2ce1cae..180c17953 100644 --- a/apps/web/src/hooks/useNewWorktreeBaseBranch.ts +++ b/apps/web/src/hooks/useNewWorktreeBaseBranch.ts @@ -1,5 +1,9 @@ -import type { EnvironmentId } from "@threadlines/contracts"; -import { findRefForBranchName, resolveNewWorktreeBaseBranch } from "@threadlines/shared/git"; +import type { EnvironmentId, NewWorktreeBaseSettings } from "@threadlines/contracts"; +import { + findRefForBranchName, + predictNewWorktreeBaseBranch, + resolveNewWorktreeBaseBranch, +} from "@threadlines/shared/git"; import { type QueryClient, useInfiniteQuery } from "@tanstack/react-query"; import { gitBranchSearchInfiniteQueryOptions } from "../lib/gitReactQuery"; @@ -50,21 +54,53 @@ export async function fetchNewWorktreeBaseBranch(input: { /** * The branch a new worktree starts from until the user picks one: the * computer's "New worktrees from" setting (Settings › Threads) applied to - * `cwd`'s refs. Undefined until it is `needed` and what it rests on is known + * `cwd`'s refs. + * + * `branch` is undefined until it is `needed` and what it rests on is known * (the computer's settings, and those refs unless the setting is the current - * branch). A lookup that fails leaves it undefined, so the user picks a base + * branch). A lookup that fails makes it null, so the user picks a base * rather than getting a fallback the setting didn't ask for. The lookups load * while the thread only `canStartWorktree`, so switching to a new worktree * finds the base ready instead of showing another branch first. + * + * `expected` is what to show meanwhile (predictNewWorktreeBaseBranch), and + * `from` the setting's kind, for naming the branch while neither is known. */ export function useNewWorktreeBaseBranch(input: { readonly environmentId: EnvironmentId; readonly cwd: string | null; readonly currentGitBranch: string | null; + /** The checkout is on the repository's default branch. */ + readonly currentIsDefault: boolean; readonly canStartWorktree: boolean; readonly needed: boolean; -}): string | null | undefined { +}): { + readonly branch: string | null | undefined; + readonly expected: string | null; + readonly from: NewWorktreeBaseSettings["from"] | undefined; +} { const base = useComputerConfig(input.environmentId)?.settings.newWorktreeBase; + const branch = useResolvedNewWorktreeBaseBranch(input, base); + return { + branch, + // A prediction only while the lookup runs: once it has answered, even + // with no branch, its answer stands. + expected: + branch !== undefined + ? branch + : predictNewWorktreeBaseBranch({ + base, + currentGitBranch: input.currentGitBranch, + currentIsDefault: input.currentIsDefault, + }), + from: base?.from, + }; +} + +function useResolvedNewWorktreeBaseBranch( + input: Parameters[0], + base: NewWorktreeBaseSettings | undefined, +): string | null | undefined { const namedBranch = base?.from === "named" ? base.branch : ""; const load = input.canStartWorktree || input.needed; // The unfiltered list leads with the current and default branches; a @@ -91,8 +127,15 @@ export function useNewWorktreeBaseBranch(input: { if (base.from === "current" && input.currentGitBranch !== null) { return input.currentGitBranch; } - // A lookup still loading, or one that failed without a past answer, says - // nothing about where the branch is: no base yet rather than a fallback. + // A lookup that failed without a past answer finds no base: the user picks + // one rather than getting a fallback the setting didn't ask for. + if ( + (listed.data === undefined && listed.isError) || + (namedBranch.length > 0 && named.data === undefined && named.isError) + ) { + return null; + } + // One still loading says nothing about where the branch is yet. if (listed.data === undefined || (namedBranch.length > 0 && named.data === undefined)) { return undefined; } diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts index 699d09a32..0a67f2aba 100644 --- a/packages/shared/src/git.test.ts +++ b/packages/shared/src/git.test.ts @@ -2,6 +2,7 @@ import type { VcsStatusRemoteResult, VcsStatusResult } from "@threadlines/contra import { describe, expect, it } from "vite-plus/test"; import { + predictNewWorktreeBaseBranch, applyGitStatusStreamEvent, buildSshRemoteUrl, buildTemporaryWorktreeBranchName, @@ -373,6 +374,25 @@ describe("worktreeFolderNameForBranch", () => { }); }); +describe("predictNewWorktreeBaseBranch", () => { + const predict = ( + base: Parameters[0]["base"], + currentIsDefault: boolean, + ) => predictNewWorktreeBaseBranch({ base, currentGitBranch: "feature/x", currentIsDefault }); + + it("names the branch the setting will give when it is known without the branch list", () => { + expect(predict({ from: "current", branch: "" }, false)).toBe("feature/x"); + expect(predict({ from: "named", branch: "develop" }, false)).toBe("develop"); + expect(predict({ from: "default", branch: "" }, true)).toBe("feature/x"); + }); + + it("names nothing when only the branch list can say", () => { + // The default branch, while the checkout is on another one. + expect(predict({ from: "default", branch: "" }, false)).toBeNull(); + expect(predict(undefined, true)).toBeNull(); + }); +}); + describe("resolveNewWorktreeBaseBranch", () => { const refs = [ { name: "threadlines/2d841c42", isDefault: false }, diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts index 3d1f576f6..81fd12746 100644 --- a/packages/shared/src/git.ts +++ b/packages/shared/src/git.ts @@ -719,6 +719,25 @@ export function resolveNewWorktreeBaseBranch(input: { return preferred ?? defaultBranch ?? input.currentBranch; } +/** + * The branch the "New worktrees from" setting will give, from what is known + * before the branch list loads (which takes seconds on a busy repository): + * the checkout's branch when the setting is the current branch, or when it is + * the default branch and the checkout is on it; the named branch. Null when + * only the list can say. + */ +export function predictNewWorktreeBaseBranch(input: { + readonly base: NewWorktreeBaseSettings | undefined; + readonly currentGitBranch: string | null; + readonly currentIsDefault: boolean; +}): string | null { + const { base, currentGitBranch, currentIsDefault } = input; + if (base === undefined) return null; + if (base.from === "current") return currentGitBranch; + if (base.from === "named" && base.branch.length > 0) return base.branch; + return currentIsDefault ? currentGitBranch : null; +} + export function detectSourceControlProviderFromGitRemoteUrl( remoteUrl: string, ): SourceControlProviderInfo | null {