TypeScript
fix(web): project actions no longer reuse the right-panel terminal (#18340)
Antony687bd555333f
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 063e9ecd086..9885e70275a 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -205,7 +205,6 @@ import {} from "../proposedPlan";import {DEFAULT_INTERACTION_MODE,- DEFAULT_THREAD_TERMINAL_ID,MAX_TERMINALS_PER_GROUP,type ChatMessage,type SessionPhase,@@ -306,6 +305,7 @@ import {import { confirmTerminalClose } from "../lib/terminalCloseConfirm";import { isPreviewFocused } from "../lib/previewFocus";import { getTerminalFocusOwner } from "../lib/terminalFocus";+import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions";import {deriveLogicalProjectKeyFromSettings,@@ -1401,7 +1401,6 @@ export default function ChatView(props: ChatViewProps) {const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);- const storeSetActiveTerminal = useTerminalUiStateStore((s) => s.setActiveTerminal);const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);const serverThreadRefs = useThreadRefs();const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);@@ -1570,9 +1569,6 @@ export default function ChatView(props: ChatViewProps) {const activeServerOrderedTerminalIds = activeThreadKnownSessions.map((session) => session.target.terminalId,);- const activeKnownTerminalIds = [- ...new Set([...activeServerOrderedTerminalIds, ...terminalUiState.terminalIds]),- ];const activeTerminalLabelsById = (() => {const labels = new Map<string, string>();for (const session of activeThreadKnownSessions) {@@ -3450,14 +3446,21 @@ export default function ChatView(props: ChatViewProps) {});}const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;- const baseTerminalId =- terminalUiState.activeTerminalId || activeKnownTerminalIds[0] || DEFAULT_THREAD_TERMINAL_ID;- const isBaseTerminalBusy = runningTerminalIds.includes(baseTerminalId);- const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;- const shouldCreateNewTerminal =- wantsNewTerminal ||+ const reusableTerminalId =+ options?.preferNewTerminal ||!canReuseTerminal ||- !readEnvironmentScope(environmentId, AuthTerminalReadScope);+ !readEnvironmentScope(environmentId, AuthTerminalReadScope)+ ? null+ : findReusableProjectScriptTerminal({+ // Server sessions cover drawer terminals this client has not opened yet, such as a+ // worktree setup shell; the helper drops the panel-owned ones.+ drawerTerminalIds: [+ ...new Set([...terminalUiState.terminalIds, ...activeServerOrderedTerminalIds]),+ ],+ activeTerminalId: terminalUiState.activeTerminalId,+ panelTerminalIds,+ runningTerminalIds,+ });const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;setTerminalUiLaunchContext({@@ -3465,20 +3468,21 @@ export default function ChatView(props: ChatViewProps) {cwd: targetCwd,worktreePath: targetWorktreePath,});- setTerminalOpen(true);if (!activeThreadRef) {return;}setTerminalFocusRequestId((value) => value + 1);const launched = terminalActions.launchTerminal({+ // The drawer opens with the target already placed: opening it empty would claim+ // DEFAULT_THREAD_TERMINAL_ID, which a right-panel terminal may already own.place: (terminalId) =>- shouldCreateNewTerminal+ reusableTerminalId === null? storeNewTerminal(activeThreadRef, terminalId)- : storeSetActiveTerminal(activeThreadRef, terminalId),- ...(shouldCreateNewTerminal+ : storeEnsureTerminal(activeThreadRef, terminalId, { open: true }),+ ...(reusableTerminalId === null? { size: { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS } }- : { terminalId: baseTerminalId }),+ : { terminalId: reusableTerminalId }),location: { cwd: targetCwd, worktreePath: targetWorktreePath },...(options?.env ? { extraEnv: options.env } : {}),});diff --git a/apps/web/src/lib/projectScriptTerminal.test.ts b/apps/web/src/lib/projectScriptTerminal.test.tsnew file mode 100644index 00000000000..3fe3f260691--- /dev/null+++ b/apps/web/src/lib/projectScriptTerminal.test.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from "vite-plus/test";++import { findReusableProjectScriptTerminal } from "./projectScriptTerminal";++describe("findReusableProjectScriptTerminal", () => {+ it.each([+ {+ name: "never reuses a right-panel terminal, even when stale in drawer state",+ drawerTerminalIds: ["term-1"],+ activeTerminalId: "term-1",+ panelTerminalIds: ["term-1"],+ runningTerminalIds: [],+ expected: null,+ },+ {+ name: "reuses the active idle drawer terminal",+ drawerTerminalIds: ["term-1", "term-2"],+ activeTerminalId: "term-2",+ panelTerminalIds: [],+ runningTerminalIds: [],+ expected: "term-2",+ },+ {+ name: "falls back to another idle drawer terminal when the active one is busy",+ drawerTerminalIds: ["term-1", "term-2"],+ activeTerminalId: "term-1",+ panelTerminalIds: [],+ runningTerminalIds: ["term-1"],+ expected: "term-2",+ },+ {+ name: "finds nothing when every drawer terminal is busy",+ drawerTerminalIds: ["term-1"],+ activeTerminalId: "term-1",+ panelTerminalIds: [],+ runningTerminalIds: ["term-1"],+ expected: null,+ },+ ])("$name", ({ panelTerminalIds, expected, ...input }) => {+ expect(+ findReusableProjectScriptTerminal({ ...input, panelTerminalIds: new Set(panelTerminalIds) }),+ ).toBe(expected);+ });+});diff --git a/apps/web/src/lib/projectScriptTerminal.ts b/apps/web/src/lib/projectScriptTerminal.tsnew file mode 100644index 00000000000..ebf3e9694f8--- /dev/null+++ b/apps/web/src/lib/projectScriptTerminal.ts@@ -0,0 +1,18 @@+/**+ * Project actions reuse only an idle drawer terminal (the active one first). Right-panel ids+ * are excluded even when drawer state lists them: reusing one renders a single PTY in both+ * surfaces, and closing the drawer copy kills the panel shell.+ */+export function findReusableProjectScriptTerminal(input: {+ readonly drawerTerminalIds: ReadonlyArray<string>;+ readonly activeTerminalId: string;+ readonly panelTerminalIds: ReadonlySet<string>;+ readonly runningTerminalIds: ReadonlyArray<string>;+}): string | null {+ const idleDrawerTerminalIds = input.drawerTerminalIds.filter(+ (terminalId) =>+ !input.panelTerminalIds.has(terminalId) && !input.runningTerminalIds.includes(terminalId),+ );+ if (idleDrawerTerminalIds.includes(input.activeTerminalId)) return input.activeTerminalId;+ return idleDrawerTerminalIds[0] ?? null;+}