@devries-cameron20 / t3code Mirror · read-only / commits / 687bd55533
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 03:00 UTC

fix(web): project actions no longer reuse the right-panel terminal (#18340)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Antony committed Oct 19, 2026 at 06:48 UTC 687bd555333fd44bb7eef27110b6c08b19e23b23
3 files changed +83 -17
apps/web/src/components/ChatView.tsx
+21 -17
@@ -205,7 +205,6 @@ import {
205 } from "../proposedPlan";
206 import {
207 DEFAULT_INTERACTION_MODE,
208 - DEFAULT_THREAD_TERMINAL_ID,
208 MAX_TERMINALS_PER_GROUP,
209 type ChatMessage,
210 type SessionPhase,
@@ -306,6 +305,7 @@ import {
305 import { confirmTerminalClose } from "../lib/terminalCloseConfirm";
306 import { isPreviewFocused } from "../lib/previewFocus";
307 import { getTerminalFocusOwner } from "../lib/terminalFocus";
308 +import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";
309 import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions";
310 import {
311 deriveLogicalProjectKeyFromSettings,
@@ -1401,7 +1401,6 @@ export default function ChatView(props: ChatViewProps) {
1401 const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);
1402 const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);
1403 const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);
1404 - const storeSetActiveTerminal = useTerminalUiStateStore((s) => s.setActiveTerminal);
1404 const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);
1405 const serverThreadRefs = useThreadRefs();
1406 const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);
@@ -1570,9 +1569,6 @@ export default function ChatView(props: ChatViewProps) {
1569 const activeServerOrderedTerminalIds = activeThreadKnownSessions.map(
1570 (session) => session.target.terminalId,
1571 );
1573 - const activeKnownTerminalIds = [
1574 - ...new Set([...activeServerOrderedTerminalIds, ...terminalUiState.terminalIds]),
1575 - ];
1572 const activeTerminalLabelsById = (() => {
1573 const labels = new Map<string, string>();
1574 for (const session of activeThreadKnownSessions) {
@@ -3450,14 +3446,21 @@ export default function ChatView(props: ChatViewProps) {
3446 });
3447 }
3448 const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
3453 - const baseTerminalId =
3454 - terminalUiState.activeTerminalId || activeKnownTerminalIds[0] || DEFAULT_THREAD_TERMINAL_ID;
3455 - const isBaseTerminalBusy = runningTerminalIds.includes(baseTerminalId);
3456 - const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;
3457 - const shouldCreateNewTerminal =
3458 - wantsNewTerminal ||
3449 + const reusableTerminalId =
3450 + options?.preferNewTerminal ||
3451 !canReuseTerminal ||
3460 - !readEnvironmentScope(environmentId, AuthTerminalReadScope);
3452 + !readEnvironmentScope(environmentId, AuthTerminalReadScope)
3453 + ? null
3454 + : findReusableProjectScriptTerminal({
3455 + // Server sessions cover drawer terminals this client has not opened yet, such as a
3456 + // worktree setup shell; the helper drops the panel-owned ones.
3457 + drawerTerminalIds: [
3458 + ...new Set([...terminalUiState.terminalIds, ...activeServerOrderedTerminalIds]),
3459 + ],
3460 + activeTerminalId: terminalUiState.activeTerminalId,
3461 + panelTerminalIds,
3462 + runningTerminalIds,
3463 + });
3464 const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
3465
3466 setTerminalUiLaunchContext({
@@ -3465,20 +3468,21 @@ export default function ChatView(props: ChatViewProps) {
3468 cwd: targetCwd,
3469 worktreePath: targetWorktreePath,
3470 });
3468 - setTerminalOpen(true);
3471 if (!activeThreadRef) {
3472 return;
3473 }
3474 setTerminalFocusRequestId((value) => value + 1);
3475
3476 const launched = terminalActions.launchTerminal({
3477 + // The drawer opens with the target already placed: opening it empty would claim
3478 + // DEFAULT_THREAD_TERMINAL_ID, which a right-panel terminal may already own.
3479 place: (terminalId) =>
3476 - shouldCreateNewTerminal
3480 + reusableTerminalId === null
3481 ? storeNewTerminal(activeThreadRef, terminalId)
3478 - : storeSetActiveTerminal(activeThreadRef, terminalId),
3479 - ...(shouldCreateNewTerminal
3482 + : storeEnsureTerminal(activeThreadRef, terminalId, { open: true }),
3483 + ...(reusableTerminalId === null
3484 ? { size: { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS } }
3481 - : { terminalId: baseTerminalId }),
3485 + : { terminalId: reusableTerminalId }),
3486 location: { cwd: targetCwd, worktreePath: targetWorktreePath },
3487 ...(options?.env ? { extraEnv: options.env } : {}),
3488 });
apps/web/src/lib/projectScriptTerminal.test.ts new
+44
@@ -0,0 +1,44 @@
1 +import { describe, expect, it } from "vite-plus/test";
2 +
3 +import { findReusableProjectScriptTerminal } from "./projectScriptTerminal";
4 +
5 +describe("findReusableProjectScriptTerminal", () => {
6 + it.each([
7 + {
8 + name: "never reuses a right-panel terminal, even when stale in drawer state",
9 + drawerTerminalIds: ["term-1"],
10 + activeTerminalId: "term-1",
11 + panelTerminalIds: ["term-1"],
12 + runningTerminalIds: [],
13 + expected: null,
14 + },
15 + {
16 + name: "reuses the active idle drawer terminal",
17 + drawerTerminalIds: ["term-1", "term-2"],
18 + activeTerminalId: "term-2",
19 + panelTerminalIds: [],
20 + runningTerminalIds: [],
21 + expected: "term-2",
22 + },
23 + {
24 + name: "falls back to another idle drawer terminal when the active one is busy",
25 + drawerTerminalIds: ["term-1", "term-2"],
26 + activeTerminalId: "term-1",
27 + panelTerminalIds: [],
28 + runningTerminalIds: ["term-1"],
29 + expected: "term-2",
30 + },
31 + {
32 + name: "finds nothing when every drawer terminal is busy",
33 + drawerTerminalIds: ["term-1"],
34 + activeTerminalId: "term-1",
35 + panelTerminalIds: [],
36 + runningTerminalIds: ["term-1"],
37 + expected: null,
38 + },
39 + ])("$name", ({ panelTerminalIds, expected, ...input }) => {
40 + expect(
41 + findReusableProjectScriptTerminal({ ...input, panelTerminalIds: new Set(panelTerminalIds) }),
42 + ).toBe(expected);
43 + });
44 +});
apps/web/src/lib/projectScriptTerminal.ts new
+18
@@ -0,0 +1,18 @@
1 +/**
2 + * Project actions reuse only an idle drawer terminal (the active one first). Right-panel ids
3 + * are excluded even when drawer state lists them: reusing one renders a single PTY in both
4 + * surfaces, and closing the drawer copy kills the panel shell.
5 + */
6 +export function findReusableProjectScriptTerminal(input: {
7 + readonly drawerTerminalIds: ReadonlyArray<string>;
8 + readonly activeTerminalId: string;
9 + readonly panelTerminalIds: ReadonlySet<string>;
10 + readonly runningTerminalIds: ReadonlyArray<string>;
11 +}): string | null {
12 + const idleDrawerTerminalIds = input.drawerTerminalIds.filter(
13 + (terminalId) =>
14 + !input.panelTerminalIds.has(terminalId) && !input.runningTerminalIds.includes(terminalId),
15 + );
16 + if (idleDrawerTerminalIds.includes(input.activeTerminalId)) return input.activeTerminalId;
17 + return idleDrawerTerminalIds[0] ?? null;
18 +}