From 8fe97e2760cb69932fc0c62dc646b665dd7b2dfe Mon Sep 17 00:00:00 2001 From: Saikrishna1876 Date: Sat, 14 Nov 2026 08:21:31 +0530 Subject: [PATCH 1/2] feat(web): add bindable new-worktree composer command --- .../web/src/components/ChatView.logic.test.ts | 34 +++++++++++++++++++ apps/web/src/components/ChatView.logic.ts | 15 ++++++++ apps/web/src/components/ChatView.tsx | 22 ++++++++++-- .../KeybindingsSettings.logic.test.ts | 10 ++++++ docs/user/keybindings.md | 2 ++ packages/contracts/src/keybindings.test.ts | 13 ++++++- packages/contracts/src/keybindings.ts | 1 + 7 files changed, 94 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index dbbb445a5ec9..29f0fc4df37d 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -17,6 +17,7 @@ import { withMessageContext, deriveAvailableRevertTurnCounts, deriveThreadPendingBackgroundTasks, + canSelectNewWorktree, } from "./ChatView.logic"; import { isThreadEnvironmentLocked, @@ -798,6 +799,39 @@ describe("checkout Git memory", () => { }); }); +describe("canSelectNewWorktree", () => { + const localDraft = { + envLocked: false, + isMultiModel: false, + isLocalDraftThread: true, + canOverrideServerThreadEnvMode: false, + }; + + it("keeps a non-Git draft out of worktree mode before its first status arrives", () => { + expect(canSelectNewWorktree({ ...localDraft, isGitRepo: undefined })).toBe(false); + expect(canSelectNewWorktree({ ...localDraft, isGitRepo: false })).toBe(false); + }); + + it("allows a draft once live or remembered status confirms a Git repository", () => { + expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true })).toBe(true); + }); + + it("keeps locked and multi-model drafts out of worktree mode", () => { + expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true, envLocked: true })).toBe(false); + expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true, isMultiModel: true })).toBe( + false, + ); + }); + + it("allows only overridable server threads", () => { + const serverThread = { ...localDraft, isGitRepo: true, isLocalDraftThread: false }; + expect(canSelectNewWorktree(serverThread)).toBe(false); + expect(canSelectNewWorktree({ ...serverThread, canOverrideServerThreadEnvMode: true })).toBe( + true, + ); + }); +}); + describe("worktree setup visibility", () => { const stage = ( id: "fetch" | "checkout" | "submodules" | "setup-script" | "agent", diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index cc9e43b847df..bf2635fd0793 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -831,6 +831,21 @@ export function recallCheckoutIsRepo( : sessionCheckoutIsRepo.get(checkoutIsRepoKey(environmentId, cwd)); } +export function canSelectNewWorktree(input: { + readonly isGitRepo: boolean | undefined; + readonly envLocked: boolean; + readonly isMultiModel: boolean; + readonly isLocalDraftThread: boolean; + readonly canOverrideServerThreadEnvMode: boolean; +}): boolean { + return ( + input.isGitRepo === true && + !input.envLocked && + !input.isMultiModel && + (input.isLocalDraftThread || input.canOverrideServerThreadEnvMode) + ); +} + // Imported history lacks a session until its first prompt; resolve its instance before locking the driver. export function deriveLockedProvider(input: { thread: Thread | null | undefined; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 1a50e7fdfa6b..9d7c12d87960 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -40,7 +40,11 @@ import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar"; import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner"; import { isThreadEnvironmentLocked, resolveDraftHeroState } from "./ChatView.logic"; import { useQueuedRunEdit } from "./chat/useQueuedRunEdit"; -import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic"; +import { + canSelectNewWorktree, + recallCheckoutIsRepo, + rememberCheckoutIsRepo, +} from "./ChatView.logic"; import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments"; import { useStableCallback } from "../hooks/useStableCallback"; import { useProjectScriptEditing } from "./chat/useProjectScriptEditing"; @@ -1763,7 +1767,8 @@ export default function ChatView(props: ChatViewProps) { rememberCheckoutIsRepo(environmentId, gitStatusCwd, liveIsGitRepo); } }, [environmentId, gitStatusCwd, liveIsGitRepo]); - const isGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd) ?? true; + const knownIsGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd); + const isGitRepo = knownIsGitRepo ?? true; // Provider-native subagents cannot take messages; approvals and questions go to the top-level parent. const showProviderSubagentBar = isProviderSubagent; const composerMounted = !showProviderSubagentBar; @@ -2780,6 +2785,19 @@ export default function ChatView(props: ChatViewProps) { const next = logicalProjectEnvironments[(index + 1) % logicalProjectEnvironments.length]; if (next) onEnvironmentChangeStable(next.environmentId); }, + "composer.newWorktree": ({ event }) => { + if ( + !canSelectNewWorktree({ + isGitRepo: knownIsGitRepo, + envLocked, + isMultiModel: multipleModelSelections !== null, + isLocalDraftThread, + canOverrideServerThreadEnvMode, + }) + ) + return false; + if (!event.repeat) onEnvModeChangeStable("worktree"); + }, "thread.steerQueuedMessage": ({ event }) => routeKind !== "draft" && queuedRunsControlRef.current?.steerNext(event.repeat) === true, "thread.editQueuedMessage": ({ event }) => diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts index ff02abc44ec6..b015bc52f74f 100644 --- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts +++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts @@ -2,14 +2,17 @@ import { assert, describe, expect, it } from "vite-plus/test"; import type { ResolvedKeybindingsConfig } from "@t3tools/contracts"; import { compileResolvedKeybindingsConfig, + DEFAULT_RESOLVED_KEYBINDINGS, parseKeybindingShortcut, } from "@t3tools/shared/keybindings"; import { resolveShortcutCommand } from "../../keybindings"; import { + buildKeybindingCommandOptions, buildKeybindingRows, buildWhenVariableOptions, + commandLabel, keybindingConflictLabels, keybindingFromKeyboardEvent, parseWhenExpressionDraft, @@ -19,6 +22,13 @@ import { } from "./KeybindingsSettings.logic"; describe("KeybindingsSettings.logic", () => { + it("offers new worktree as a bindable composer command without a default shortcut", () => { + const command = buildKeybindingCommandOptions([]).find((id) => id === "composer.newWorktree"); + expect(command).toBe("composer.newWorktree"); + expect(commandLabel(command!)).toBe("Composer: New Worktree"); + expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command)).toBe(false); + }); + it("keeps sibling row identities when a saved binding moves to the end", () => { const bindings = compileResolvedKeybindingsConfig([ { command: "chat.new", key: "mod+n" }, diff --git a/docs/user/keybindings.md b/docs/user/keybindings.md index 308d7d934c1f..cc77178b2b0e 100644 --- a/docs/user/keybindings.md +++ b/docs/user/keybindings.md @@ -37,6 +37,8 @@ Use `mod+shift+e` for effort, `mod+shift+a` for permissions, `mod+shift+x` for t workspace, and `mod+shift+g` for the Git branch. The workspace menu includes the current checkout, a new worktree, and the previous worktree when available. Use `mod+shift+l` to reuse the previous worktree directly. +To select a new worktree directly, bind **Composer: New Worktree** in Keybindings. +It has no default shortcut and uses the same rules as the workspace menu. To change effort one step without opening the picker, bind **Composer: Increase Effort** and **Composer: Decrease Effort** in Keybindings. diff --git a/packages/contracts/src/keybindings.test.ts b/packages/contracts/src/keybindings.test.ts index 9ea9272e6c3d..77ef621068c6 100644 --- a/packages/contracts/src/keybindings.test.ts +++ b/packages/contracts/src/keybindings.test.ts @@ -2,7 +2,7 @@ import { assert, it } from "@effect/vitest"; import * as Schema from "effect/Schema"; import * as Effect from "effect/Effect"; -import { ResolvedKeybindingsConfig } from "./keybindings.ts"; +import { KeybindingsConfig, ResolvedKeybindingsConfig } from "./keybindings.ts"; const decode = ( schema: S, @@ -23,6 +23,17 @@ const shortcut = { modKey: true, }; +it.effect("accepts a custom shortcut for selecting a new worktree", () => + Effect.gen(function* () { + const config = [ + { command: "composer.newWorktree", key: "mod+alt+w", when: "!terminalFocus" }, + ] as const; + assert.deepEqual(yield* decode(KeybindingsConfig, config), config); + const resolved = [{ command: "composer.newWorktree", shortcut }] as const; + assert.deepEqual(yield* decode(ResolvedKeybindingsConfig, resolved), resolved); + }), +); + it.effect("drops resolved rules with commands this build does not know", () => Effect.gen(function* () { const parsed = yield* decode(ResolvedKeybindingsConfig, [ diff --git a/packages/contracts/src/keybindings.ts b/packages/contracts/src/keybindings.ts index eda5138b760b..12b40878c85f 100644 --- a/packages/contracts/src/keybindings.ts +++ b/packages/contracts/src/keybindings.ts @@ -100,6 +100,7 @@ export const STATIC_KEYBINDING_COMMANDS = [ "composer.decreaseEffort", "composer.mode", "composer.workspace", + "composer.newWorktree", "composer.previousWorktree", "composer.branch", "chat.new", From a1c4ea7e4a46a67209dac7bf5cf8f1fdc29daaa8 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 03:23:51 +0000 Subject: [PATCH 2/2] refactor(web): route the new-worktree shortcut through the branch toolbar The workspace menu and the previous-worktree shortcut already gate on the branch toolbar's lock state; the new command reuses that guard instead of a parallel helper in ChatView. Co-authored-by: Saikrishna1876 Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/BranchToolbar.tsx | 8 +++++ .../web/src/components/ChatView.logic.test.ts | 34 ------------------- apps/web/src/components/ChatView.logic.ts | 15 -------- apps/web/src/components/ChatView.tsx | 22 ++---------- .../src/components/chat/composerShortcuts.ts | 3 ++ .../KeybindingsSettings.logic.test.ts | 10 ------ packages/contracts/src/keybindings.test.ts | 13 +------ 7 files changed, 14 insertions(+), 91 deletions(-) diff --git a/apps/web/src/components/BranchToolbar.tsx b/apps/web/src/components/BranchToolbar.tsx index eae9e43459f4..f8bbda418d9b 100644 --- a/apps/web/src/components/BranchToolbar.tsx +++ b/apps/web/src/components/BranchToolbar.tsx @@ -72,6 +72,7 @@ import { observeResize } from "~/lib/observeResize"; export interface BranchToolbarHandle { openBranchPicker: () => void; usePreviousWorktree: () => void; + selectNewWorktree: () => void; } interface BranchToolbarProps { @@ -652,10 +653,17 @@ export const BranchToolbar = memo(function BranchToolbar({ onUsePreviousWorktree(); onComposerFocusRequest?.(); }, + selectNewWorktree: () => { + if (!showGitControls || envModeLocked || forceNewWorktree) return; + onEnvModeChange("worktree"); + }, }), [ canUsePreviousWorktree, + envModeLocked, + forceNewWorktree, onComposerFocusRequest, + onEnvModeChange, onUsePreviousWorktree, previousWorktreeSeed, showGitControls, diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 29f0fc4df37d..dbbb445a5ec9 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -17,7 +17,6 @@ import { withMessageContext, deriveAvailableRevertTurnCounts, deriveThreadPendingBackgroundTasks, - canSelectNewWorktree, } from "./ChatView.logic"; import { isThreadEnvironmentLocked, @@ -799,39 +798,6 @@ describe("checkout Git memory", () => { }); }); -describe("canSelectNewWorktree", () => { - const localDraft = { - envLocked: false, - isMultiModel: false, - isLocalDraftThread: true, - canOverrideServerThreadEnvMode: false, - }; - - it("keeps a non-Git draft out of worktree mode before its first status arrives", () => { - expect(canSelectNewWorktree({ ...localDraft, isGitRepo: undefined })).toBe(false); - expect(canSelectNewWorktree({ ...localDraft, isGitRepo: false })).toBe(false); - }); - - it("allows a draft once live or remembered status confirms a Git repository", () => { - expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true })).toBe(true); - }); - - it("keeps locked and multi-model drafts out of worktree mode", () => { - expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true, envLocked: true })).toBe(false); - expect(canSelectNewWorktree({ ...localDraft, isGitRepo: true, isMultiModel: true })).toBe( - false, - ); - }); - - it("allows only overridable server threads", () => { - const serverThread = { ...localDraft, isGitRepo: true, isLocalDraftThread: false }; - expect(canSelectNewWorktree(serverThread)).toBe(false); - expect(canSelectNewWorktree({ ...serverThread, canOverrideServerThreadEnvMode: true })).toBe( - true, - ); - }); -}); - describe("worktree setup visibility", () => { const stage = ( id: "fetch" | "checkout" | "submodules" | "setup-script" | "agent", diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index bf2635fd0793..cc9e43b847df 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -831,21 +831,6 @@ export function recallCheckoutIsRepo( : sessionCheckoutIsRepo.get(checkoutIsRepoKey(environmentId, cwd)); } -export function canSelectNewWorktree(input: { - readonly isGitRepo: boolean | undefined; - readonly envLocked: boolean; - readonly isMultiModel: boolean; - readonly isLocalDraftThread: boolean; - readonly canOverrideServerThreadEnvMode: boolean; -}): boolean { - return ( - input.isGitRepo === true && - !input.envLocked && - !input.isMultiModel && - (input.isLocalDraftThread || input.canOverrideServerThreadEnvMode) - ); -} - // Imported history lacks a session until its first prompt; resolve its instance before locking the driver. export function deriveLockedProvider(input: { thread: Thread | null | undefined; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 9d7c12d87960..1a50e7fdfa6b 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -40,11 +40,7 @@ import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar"; import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner"; import { isThreadEnvironmentLocked, resolveDraftHeroState } from "./ChatView.logic"; import { useQueuedRunEdit } from "./chat/useQueuedRunEdit"; -import { - canSelectNewWorktree, - recallCheckoutIsRepo, - rememberCheckoutIsRepo, -} from "./ChatView.logic"; +import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic"; import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments"; import { useStableCallback } from "../hooks/useStableCallback"; import { useProjectScriptEditing } from "./chat/useProjectScriptEditing"; @@ -1767,8 +1763,7 @@ export default function ChatView(props: ChatViewProps) { rememberCheckoutIsRepo(environmentId, gitStatusCwd, liveIsGitRepo); } }, [environmentId, gitStatusCwd, liveIsGitRepo]); - const knownIsGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd); - const isGitRepo = knownIsGitRepo ?? true; + const isGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd) ?? true; // Provider-native subagents cannot take messages; approvals and questions go to the top-level parent. const showProviderSubagentBar = isProviderSubagent; const composerMounted = !showProviderSubagentBar; @@ -2785,19 +2780,6 @@ export default function ChatView(props: ChatViewProps) { const next = logicalProjectEnvironments[(index + 1) % logicalProjectEnvironments.length]; if (next) onEnvironmentChangeStable(next.environmentId); }, - "composer.newWorktree": ({ event }) => { - if ( - !canSelectNewWorktree({ - isGitRepo: knownIsGitRepo, - envLocked, - isMultiModel: multipleModelSelections !== null, - isLocalDraftThread, - canOverrideServerThreadEnvMode, - }) - ) - return false; - if (!event.repeat) onEnvModeChangeStable("worktree"); - }, "thread.steerQueuedMessage": ({ event }) => routeKind !== "draft" && queuedRunsControlRef.current?.steerNext(event.repeat) === true, "thread.editQueuedMessage": ({ event }) => diff --git a/apps/web/src/components/chat/composerShortcuts.ts b/apps/web/src/components/chat/composerShortcuts.ts index 30beb9771968..fb5f52854f4a 100644 --- a/apps/web/src/components/chat/composerShortcuts.ts +++ b/apps/web/src/components/chat/composerShortcuts.ts @@ -34,5 +34,8 @@ export function composerShortcutCommands( "composer.previousWorktree": ({ event }) => { if (!event.repeat) branchToolbarRef.current?.usePreviousWorktree(); }, + "composer.newWorktree": ({ event }) => { + if (!event.repeat) branchToolbarRef.current?.selectNewWorktree(); + }, } satisfies ChatShortcutCommands; } diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts index b015bc52f74f..ff02abc44ec6 100644 --- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts +++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts @@ -2,17 +2,14 @@ import { assert, describe, expect, it } from "vite-plus/test"; import type { ResolvedKeybindingsConfig } from "@t3tools/contracts"; import { compileResolvedKeybindingsConfig, - DEFAULT_RESOLVED_KEYBINDINGS, parseKeybindingShortcut, } from "@t3tools/shared/keybindings"; import { resolveShortcutCommand } from "../../keybindings"; import { - buildKeybindingCommandOptions, buildKeybindingRows, buildWhenVariableOptions, - commandLabel, keybindingConflictLabels, keybindingFromKeyboardEvent, parseWhenExpressionDraft, @@ -22,13 +19,6 @@ import { } from "./KeybindingsSettings.logic"; describe("KeybindingsSettings.logic", () => { - it("offers new worktree as a bindable composer command without a default shortcut", () => { - const command = buildKeybindingCommandOptions([]).find((id) => id === "composer.newWorktree"); - expect(command).toBe("composer.newWorktree"); - expect(commandLabel(command!)).toBe("Composer: New Worktree"); - expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === command)).toBe(false); - }); - it("keeps sibling row identities when a saved binding moves to the end", () => { const bindings = compileResolvedKeybindingsConfig([ { command: "chat.new", key: "mod+n" }, diff --git a/packages/contracts/src/keybindings.test.ts b/packages/contracts/src/keybindings.test.ts index 77ef621068c6..9ea9272e6c3d 100644 --- a/packages/contracts/src/keybindings.test.ts +++ b/packages/contracts/src/keybindings.test.ts @@ -2,7 +2,7 @@ import { assert, it } from "@effect/vitest"; import * as Schema from "effect/Schema"; import * as Effect from "effect/Effect"; -import { KeybindingsConfig, ResolvedKeybindingsConfig } from "./keybindings.ts"; +import { ResolvedKeybindingsConfig } from "./keybindings.ts"; const decode = ( schema: S, @@ -23,17 +23,6 @@ const shortcut = { modKey: true, }; -it.effect("accepts a custom shortcut for selecting a new worktree", () => - Effect.gen(function* () { - const config = [ - { command: "composer.newWorktree", key: "mod+alt+w", when: "!terminalFocus" }, - ] as const; - assert.deepEqual(yield* decode(KeybindingsConfig, config), config); - const resolved = [{ command: "composer.newWorktree", shortcut }] as const; - assert.deepEqual(yield* decode(ResolvedKeybindingsConfig, resolved), resolved); - }), -); - it.effect("drops resolved rules with commands this build does not know", () => Effect.gen(function* () { const parsed = yield* decode(ResolvedKeybindingsConfig, [