Skip to content

Commit 2c69c22

Browse files
TonybynMp4claude
andcommitted
refactor(web): project actions allocate new terminals through allocateTerminalId
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 48caa2e commit 2c69c22

3 files changed

Lines changed: 39 additions & 65 deletions

File tree

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ import {
299299
import { confirmTerminalClose } from "../lib/terminalCloseConfirm";
300300
import { isPreviewFocused } from "../lib/previewFocus";
301301
import { getTerminalFocusOwner } from "../lib/terminalFocus";
302-
import { resolveProjectScriptTerminal } from "../lib/projectScriptTerminal";
302+
import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";
303303
import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions";
304304
import {
305305
deriveLogicalProjectKeyFromSettings,
@@ -1540,9 +1540,6 @@ export default function ChatView(props: ChatViewProps) {
15401540
const activeServerOrderedTerminalIds = activeThreadKnownSessions.map(
15411541
(session) => session.target.terminalId,
15421542
);
1543-
const activeKnownTerminalIds = [
1544-
...new Set([...activeServerOrderedTerminalIds, ...terminalUiState.terminalIds]),
1545-
];
15461543
const activeTerminalLabelsById = (() => {
15471544
const labels = new Map<string, string>();
15481545
for (const session of activeThreadKnownSessions) {
@@ -3420,18 +3417,17 @@ export default function ChatView(props: ChatViewProps) {
34203417
});
34213418
}
34223419
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
3423-
const projectScriptTerminal = resolveProjectScriptTerminal({
3424-
drawerTerminalIds: terminalUiState.terminalIds,
3425-
activeTerminalId: terminalUiState.activeTerminalId,
3426-
panelTerminalIds,
3427-
allocatableTerminalIds: activeKnownTerminalIds,
3428-
runningTerminalIds,
3429-
preferNewTerminal: Boolean(options?.preferNewTerminal),
3430-
});
3431-
const shouldCreateNewTerminal =
3432-
projectScriptTerminal.isNew ||
3420+
const reusableTerminalId =
3421+
options?.preferNewTerminal ||
34333422
!canReuseTerminal ||
3434-
!readEnvironmentScope(environmentId, AuthTerminalReadScope);
3423+
!readEnvironmentScope(environmentId, AuthTerminalReadScope)
3424+
? null
3425+
: findReusableProjectScriptTerminal({
3426+
drawerTerminalIds: terminalUiState.terminalIds,
3427+
activeTerminalId: terminalUiState.activeTerminalId,
3428+
panelTerminalIds,
3429+
runningTerminalIds,
3430+
});
34353431
const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
34363432

34373433
setTerminalUiLaunchContext({
@@ -3448,16 +3444,16 @@ export default function ChatView(props: ChatViewProps) {
34483444
// Apply the target before opening: opening an empty drawer would otherwise claim
34493445
// DEFAULT_THREAD_TERMINAL_ID, which a right-panel terminal may already own.
34503446
place: (terminalId) => {
3451-
if (shouldCreateNewTerminal) {
3447+
if (reusableTerminalId === null) {
34523448
storeNewTerminal(activeThreadRef, terminalId);
34533449
return;
34543450
}
34553451
storeSetActiveTerminal(activeThreadRef, terminalId);
34563452
setTerminalOpen(true);
34573453
},
3458-
...(shouldCreateNewTerminal
3454+
...(reusableTerminalId === null
34593455
? { size: { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS } }
3460-
: { terminalId: projectScriptTerminal.terminalId }),
3456+
: { terminalId: reusableTerminalId }),
34613457
location: { cwd: targetCwd, worktreePath: targetWorktreePath },
34623458
...(options?.env ? { extraEnv: options.env } : {}),
34633459
});
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,61 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { resolveProjectScriptTerminal } from "./projectScriptTerminal";
3+
import { findReusableProjectScriptTerminal } from "./projectScriptTerminal";
44

55
const base = {
66
drawerTerminalIds: [],
77
activeTerminalId: "",
88
panelTerminalIds: new Set<string>(),
9-
allocatableTerminalIds: [],
109
runningTerminalIds: [],
11-
preferNewTerminal: false,
1210
};
1311

14-
describe("resolveProjectScriptTerminal", () => {
15-
it("allocates a new drawer terminal instead of reusing an idle right-panel terminal", () => {
12+
describe("findReusableProjectScriptTerminal", () => {
13+
it("does not reuse an idle right-panel terminal", () => {
1614
expect(
17-
resolveProjectScriptTerminal({
18-
...base,
19-
panelTerminalIds: new Set(["term-1"]),
20-
allocatableTerminalIds: ["term-1"],
21-
}),
22-
).toEqual({ terminalId: "term-2", isNew: true });
15+
findReusableProjectScriptTerminal({ ...base, panelTerminalIds: new Set(["term-1"]) }),
16+
).toBeNull();
2317
});
2418

2519
it("never reuses a panel terminal left in stale drawer state", () => {
2620
expect(
27-
resolveProjectScriptTerminal({
21+
findReusableProjectScriptTerminal({
2822
...base,
2923
drawerTerminalIds: ["term-1"],
3024
activeTerminalId: "term-1",
3125
panelTerminalIds: new Set(["term-1"]),
32-
allocatableTerminalIds: ["term-1"],
3326
}),
34-
).toEqual({ terminalId: "term-2", isNew: true });
27+
).toBeNull();
3528
});
3629

3730
it("reuses the active idle drawer terminal", () => {
3831
expect(
39-
resolveProjectScriptTerminal({
32+
findReusableProjectScriptTerminal({
4033
...base,
4134
drawerTerminalIds: ["term-1", "term-2"],
4235
activeTerminalId: "term-2",
43-
allocatableTerminalIds: ["term-1", "term-2"],
4436
}),
45-
).toEqual({ terminalId: "term-2", isNew: false });
37+
).toBe("term-2");
4638
});
4739

4840
it("reuses another idle drawer terminal when the active one is busy", () => {
4941
expect(
50-
resolveProjectScriptTerminal({
42+
findReusableProjectScriptTerminal({
5143
...base,
5244
drawerTerminalIds: ["term-1", "term-2"],
5345
activeTerminalId: "term-1",
54-
allocatableTerminalIds: ["term-1", "term-2"],
5546
runningTerminalIds: ["term-1"],
5647
}),
57-
).toEqual({ terminalId: "term-2", isNew: false });
48+
).toBe("term-2");
5849
});
5950

60-
it("allocates a new terminal when the drawer terminal is busy", () => {
51+
it("finds nothing when every drawer terminal is busy", () => {
6152
expect(
62-
resolveProjectScriptTerminal({
53+
findReusableProjectScriptTerminal({
6354
...base,
6455
drawerTerminalIds: ["term-1"],
6556
activeTerminalId: "term-1",
66-
panelTerminalIds: new Set(["term-2"]),
67-
allocatableTerminalIds: ["term-1", "term-2"],
6857
runningTerminalIds: ["term-1"],
6958
}),
70-
).toEqual({ terminalId: "term-3", isNew: true });
59+
).toBeNull();
7160
});
7261
});
Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
import { nextTerminalId } from "@t3tools/shared/terminalLabels";
2-
31
/**
4-
* Picks the drawer terminal a project action runs in. Reuses the active (or first) idle drawer
5-
* terminal, otherwise allocates a fresh id. Right-panel terminals are never reused, so one PTY
6-
* never renders in both the drawer and the panel.
2+
* Picks the drawer terminal a project action can reuse: the active idle drawer terminal, else the
3+
* first idle one, else null. Right-panel terminals are never reused, so one PTY never renders in
4+
* both the drawer and the panel.
75
*/
8-
export function resolveProjectScriptTerminal(input: {
6+
export function findReusableProjectScriptTerminal(input: {
97
readonly drawerTerminalIds: ReadonlyArray<string>;
108
readonly activeTerminalId: string;
119
readonly panelTerminalIds: ReadonlySet<string>;
12-
readonly allocatableTerminalIds: ReadonlyArray<string>;
1310
readonly runningTerminalIds: ReadonlyArray<string>;
14-
readonly preferNewTerminal: boolean;
15-
}): { readonly terminalId: string; readonly isNew: boolean } {
16-
if (!input.preferNewTerminal) {
17-
const idleDrawerTerminalIds = input.drawerTerminalIds.filter(
18-
(terminalId) =>
19-
!input.panelTerminalIds.has(terminalId) && !input.runningTerminalIds.includes(terminalId),
20-
);
21-
const baseTerminalId = idleDrawerTerminalIds.includes(input.activeTerminalId)
22-
? input.activeTerminalId
23-
: idleDrawerTerminalIds[0];
24-
if (baseTerminalId !== undefined) {
25-
return { terminalId: baseTerminalId, isNew: false };
26-
}
27-
}
28-
return { terminalId: nextTerminalId(input.allocatableTerminalIds), isNew: true };
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;
2918
}

0 commit comments

Comments
 (0)