Skip to content

Commit 687bd55

Browse files
TonybynMp4claudejuliusmarminge
authored
fix(web): project actions no longer reuse the right-panel terminal (#18340)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 3a78a2d commit 687bd55

3 files changed

Lines changed: 83 additions & 17 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ import {
205205
} from "../proposedPlan";
206206
import {
207207
DEFAULT_INTERACTION_MODE,
208-
DEFAULT_THREAD_TERMINAL_ID,
209208
MAX_TERMINALS_PER_GROUP,
210209
type ChatMessage,
211210
type SessionPhase,
@@ -306,6 +305,7 @@ import {
306305
import { confirmTerminalClose } from "../lib/terminalCloseConfirm";
307306
import { isPreviewFocused } from "../lib/previewFocus";
308307
import { getTerminalFocusOwner } from "../lib/terminalFocus";
308+
import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";
309309
import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions";
310310
import {
311311
deriveLogicalProjectKeyFromSettings,
@@ -1401,7 +1401,6 @@ export default function ChatView(props: ChatViewProps) {
14011401
const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);
14021402
const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);
14031403
const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);
1404-
const storeSetActiveTerminal = useTerminalUiStateStore((s) => s.setActiveTerminal);
14051404
const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);
14061405
const serverThreadRefs = useThreadRefs();
14071406
const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);
@@ -1570,9 +1569,6 @@ export default function ChatView(props: ChatViewProps) {
15701569
const activeServerOrderedTerminalIds = activeThreadKnownSessions.map(
15711570
(session) => session.target.terminalId,
15721571
);
1573-
const activeKnownTerminalIds = [
1574-
...new Set([...activeServerOrderedTerminalIds, ...terminalUiState.terminalIds]),
1575-
];
15761572
const activeTerminalLabelsById = (() => {
15771573
const labels = new Map<string, string>();
15781574
for (const session of activeThreadKnownSessions) {
@@ -3450,35 +3446,43 @@ export default function ChatView(props: ChatViewProps) {
34503446
});
34513447
}
34523448
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 ||
34593451
!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+
});
34613464
const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
34623465

34633466
setTerminalUiLaunchContext({
34643467
threadId: activeThreadId,
34653468
cwd: targetCwd,
34663469
worktreePath: targetWorktreePath,
34673470
});
3468-
setTerminalOpen(true);
34693471
if (!activeThreadRef) {
34703472
return;
34713473
}
34723474
setTerminalFocusRequestId((value) => value + 1);
34733475

34743476
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.
34753479
place: (terminalId) =>
3476-
shouldCreateNewTerminal
3480+
reusableTerminalId === null
34773481
? storeNewTerminal(activeThreadRef, terminalId)
3478-
: storeSetActiveTerminal(activeThreadRef, terminalId),
3479-
...(shouldCreateNewTerminal
3482+
: storeEnsureTerminal(activeThreadRef, terminalId, { open: true }),
3483+
...(reusableTerminalId === null
34803484
? { size: { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS } }
3481-
: { terminalId: baseTerminalId }),
3485+
: { terminalId: reusableTerminalId }),
34823486
location: { cwd: targetCwd, worktreePath: targetWorktreePath },
34833487
...(options?.env ? { extraEnv: options.env } : {}),
34843488
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)