From 8e60ea79978a64670b058b9081017b4e55af6079 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 1 Apr 2026 11:34:15 -0700 Subject: [PATCH] Sync sidebar PR state with active branch context Co-authored-by: codex --- .../GitActionsControl.logic.test.ts | 32 ++++++++++ .../src/components/GitActionsControl.logic.ts | 17 +++++ apps/web/src/components/GitActionsControl.tsx | 50 ++++++++++++--- apps/web/src/components/Sidebar.logic.test.ts | 63 +++++++++++++++++++ apps/web/src/components/Sidebar.logic.ts | 40 ++++++++++++ apps/web/src/components/Sidebar.tsx | 32 +++++++++- 6 files changed, 221 insertions(+), 13 deletions(-) diff --git a/apps/web/src/components/GitActionsControl.logic.test.ts b/apps/web/src/components/GitActionsControl.logic.test.ts index fb5cb1e072..070dbba393 100644 --- a/apps/web/src/components/GitActionsControl.logic.test.ts +++ b/apps/web/src/components/GitActionsControl.logic.test.ts @@ -6,6 +6,7 @@ import { requiresDefaultBranchConfirmation, resolveAutoFeatureBranchName, resolveDefaultBranchActionDialogCopy, + resolveLiveThreadBranchUpdate, resolveQuickAction, resolveThreadBranchUpdate, summarizeGitResult, @@ -1028,6 +1029,37 @@ describe("resolveThreadBranchUpdate", () => { }); }); +describe("resolveLiveThreadBranchUpdate", () => { + it("returns a branch update when live git status differs from stored thread metadata", () => { + const update = resolveLiveThreadBranchUpdate({ + threadBranch: "feature/old-branch", + gitStatus: status({ branch: "effect-atom" }), + }); + + assert.deepEqual(update, { + branch: "effect-atom", + }); + }); + + it("returns null when live git status is unavailable", () => { + const update = resolveLiveThreadBranchUpdate({ + threadBranch: "feature/old-branch", + gitStatus: null, + }); + + assert.equal(update, null); + }); + + it("returns null when the stored thread branch already matches git status", () => { + const update = resolveLiveThreadBranchUpdate({ + threadBranch: "effect-atom", + gitStatus: status({ branch: "effect-atom" }), + }); + + assert.equal(update, null); + }); +}); + describe("resolveAutoFeatureBranchName", () => { it("uses semantic preferred branch names when available", () => { const branch = resolveAutoFeatureBranchName(["main", "feature/other"], "fix toast copy"); diff --git a/apps/web/src/components/GitActionsControl.logic.ts b/apps/web/src/components/GitActionsControl.logic.ts index 7bc2c69b6d..25ab8eddd2 100644 --- a/apps/web/src/components/GitActionsControl.logic.ts +++ b/apps/web/src/components/GitActionsControl.logic.ts @@ -358,5 +358,22 @@ export function resolveThreadBranchUpdate( }; } +export function resolveLiveThreadBranchUpdate(input: { + threadBranch: string | null; + gitStatus: GitStatusResult | null; +}): { branch: string | null } | null { + if (!input.gitStatus) { + return null; + } + + if (input.threadBranch === input.gitStatus.branch) { + return null; + } + + return { + branch: input.gitStatus.branch, + }; +} + // Re-export from shared for backwards compatibility in this module's exports export { resolveAutoFeatureBranchName } from "@t3tools/shared/git"; diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsx index c11aca31e9..f8dc69ab35 100644 --- a/apps/web/src/components/GitActionsControl.tsx +++ b/apps/web/src/components/GitActionsControl.tsx @@ -18,6 +18,7 @@ import { type DefaultBranchConfirmableAction, requiresDefaultBranchConfirmation, resolveDefaultBranchActionDialogCopy, + resolveLiveThreadBranchUpdate, resolveQuickAction, resolveThreadBranchUpdate, summarizeGitResult, @@ -238,18 +239,13 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions }); }, [threadToastData]); - const syncThreadBranchAfterGitAction = useCallback( - (result: GitRunStackedActionResult) => { - const branchUpdate = resolveThreadBranchUpdate(result); - if (!branchUpdate || !activeThreadId || !activeServerThread) { + const persistThreadBranchSync = useCallback( + (branch: string | null) => { + if (!activeThreadId || !activeServerThread || activeServerThread.branch === branch) { return; } const worktreePath = activeServerThread.worktreePath; - if (activeServerThread.branch === branchUpdate.branch) { - return; - } - const api = readNativeApi(); if (api) { void api.orchestration @@ -257,17 +253,29 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions type: "thread.meta.update", commandId: newCommandId(), threadId: activeThreadId, - branch: branchUpdate.branch, + branch, worktreePath, }) .catch(() => undefined); } - setThreadBranch(activeThreadId, branchUpdate.branch, worktreePath); + setThreadBranch(activeThreadId, branch, worktreePath); }, [activeServerThread, activeThreadId, setThreadBranch], ); + const syncThreadBranchAfterGitAction = useCallback( + (result: GitRunStackedActionResult) => { + const branchUpdate = resolveThreadBranchUpdate(result); + if (!branchUpdate) { + return; + } + + persistThreadBranchSync(branchUpdate.branch); + }, + [persistThreadBranchSync], + ); + const { data: gitStatus = null, error: gitStatusError } = useQuery(gitStatusQueryOptions(gitCwd)); const { data: branchList = null } = useQuery(gitBranchesQueryOptions(gitCwd)); @@ -304,6 +312,28 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions useIsMutating({ mutationKey: gitMutationKeys.runStackedAction(gitCwd) }) > 0; const isPullRunning = useIsMutating({ mutationKey: gitMutationKeys.pull(gitCwd) }) > 0; const isGitActionRunning = isRunStackedActionRunning || isPullRunning; + + useEffect(() => { + if (isGitActionRunning) { + return; + } + + const branchUpdate = resolveLiveThreadBranchUpdate({ + threadBranch: activeServerThread?.branch ?? null, + gitStatus: gitStatusForActions, + }); + if (!branchUpdate) { + return; + } + + persistThreadBranchSync(branchUpdate.branch); + }, [ + activeServerThread?.branch, + gitStatusForActions, + isGitActionRunning, + persistThreadBranchSync, + ]); + const isDefaultBranch = useMemo(() => { const branchName = gitStatusForActions?.branch; if (!branchName) return false; diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index d4cd25db4c..7c83d36536 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -11,6 +11,7 @@ import { isContextMenuPointerDown, orderItemsByPreferredIds, resolveProjectStatusIndicator, + resolveSidebarNewThreadSeedContext, resolveSidebarNewThreadEnvMode, resolveThreadRowClassName, resolveThreadStatusPill, @@ -164,6 +165,68 @@ describe("resolveSidebarNewThreadEnvMode", () => { }); }); +describe("resolveSidebarNewThreadSeedContext", () => { + it("inherits the active server thread context when creating a new thread in the same project", () => { + expect( + resolveSidebarNewThreadSeedContext({ + projectId: "project-1", + defaultEnvMode: "local", + activeThread: { + projectId: "project-1", + branch: "effect-atom", + worktreePath: null, + }, + activeDraftThread: null, + }), + ).toEqual({ + branch: "effect-atom", + worktreePath: null, + envMode: "local", + }); + }); + + it("prefers the active draft thread context when it matches the target project", () => { + expect( + resolveSidebarNewThreadSeedContext({ + projectId: "project-1", + defaultEnvMode: "local", + activeThread: { + projectId: "project-1", + branch: "effect-atom", + worktreePath: null, + }, + activeDraftThread: { + projectId: "project-1", + branch: "feature/new-draft", + worktreePath: "/repo/worktree", + envMode: "worktree", + }, + }), + ).toEqual({ + branch: "feature/new-draft", + worktreePath: "/repo/worktree", + envMode: "worktree", + }); + }); + + it("falls back to the default env mode when there is no matching active thread context", () => { + expect( + resolveSidebarNewThreadSeedContext({ + projectId: "project-2", + defaultEnvMode: "worktree", + activeThread: { + projectId: "project-1", + branch: "effect-atom", + worktreePath: null, + }, + activeDraftThread: null, + }), + ).toEqual({ + envMode: "worktree", + }); + }); +}); + describe("orderItemsByPreferredIds", () => { it("keeps preferred ids first, skips stale ids, and preserves the relative order of remaining items", () => { const ordered = orderItemsByPreferredIds({ diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 16c7752f74..c606853392 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -161,6 +161,46 @@ export function resolveSidebarNewThreadEnvMode(input: { return input.requestedEnvMode ?? input.defaultEnvMode; } +export function resolveSidebarNewThreadSeedContext(input: { + projectId: string; + defaultEnvMode: SidebarNewThreadEnvMode; + activeThread?: { + projectId: string; + branch: string | null; + worktreePath: string | null; + } | null; + activeDraftThread?: { + projectId: string; + branch: string | null; + worktreePath: string | null; + envMode: SidebarNewThreadEnvMode; + } | null; +}): { + branch?: string | null; + worktreePath?: string | null; + envMode: SidebarNewThreadEnvMode; +} { + if (input.activeDraftThread?.projectId === input.projectId) { + return { + branch: input.activeDraftThread.branch, + worktreePath: input.activeDraftThread.worktreePath, + envMode: input.activeDraftThread.envMode, + }; + } + + if (input.activeThread?.projectId === input.projectId) { + return { + branch: input.activeThread.branch, + worktreePath: input.activeThread.worktreePath, + envMode: input.activeThread.worktreePath ? "worktree" : "local", + }; + } + + return { + envMode: input.defaultEnvMode, + }; +} + export function orderItemsByPreferredIds(input: { items: readonly TItem[]; preferredIds: readonly TId[]; diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index c819ce81cd..1e3340cb24 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -111,6 +111,7 @@ import { resolveAdjacentThreadId, isContextMenuPointerDown, resolveProjectStatusIndicator, + resolveSidebarNewThreadSeedContext, resolveSidebarNewThreadEnvMode, resolveThreadRowClassName, resolveThreadStatusPill, @@ -460,7 +461,7 @@ export default function Sidebar() { const isOnSettings = pathname.startsWith("/settings"); const appSettings = useSettings(); const { updateSettings } = useUpdateSettings(); - const { handleNewThread } = useHandleNewThread(); + const { activeDraftThread, activeThread, handleNewThread } = useHandleNewThread(); const { archiveThread, deleteThread } = useThreadActions(); const routeThreadId = useParams({ strict: false, @@ -1680,10 +1681,35 @@ export default function Sidebar() { onClick={(event) => { event.preventDefault(); event.stopPropagation(); - void handleNewThread(project.id, { - envMode: resolveSidebarNewThreadEnvMode({ + const seedContext = resolveSidebarNewThreadSeedContext({ + projectId: project.id, + defaultEnvMode: resolveSidebarNewThreadEnvMode({ defaultEnvMode: appSettings.defaultThreadEnvMode, }), + activeThread: + activeThread && activeThread.projectId === project.id + ? { + projectId: activeThread.projectId, + branch: activeThread.branch, + worktreePath: activeThread.worktreePath, + } + : null, + activeDraftThread: + activeDraftThread && activeDraftThread.projectId === project.id + ? { + projectId: activeDraftThread.projectId, + branch: activeDraftThread.branch, + worktreePath: activeDraftThread.worktreePath, + envMode: activeDraftThread.envMode, + } + : null, + }); + void handleNewThread(project.id, { + ...(seedContext.branch !== undefined ? { branch: seedContext.branch } : {}), + ...(seedContext.worktreePath !== undefined + ? { worktreePath: seedContext.worktreePath } + : {}), + envMode: seedContext.envMode, }); }} >