Skip to content

Commit 33ee6d0

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

3 files changed

Lines changed: 36 additions & 61 deletions

File tree

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ import {
353353
import { confirmTerminalClose, isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm";
354354
import { isPreviewFocused } from "../lib/previewFocus";
355355
import { getTerminalFocusOwner } from "../lib/terminalFocus";
356-
import { resolveProjectScriptTerminal } from "../lib/projectScriptTerminal";
356+
import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";
357357
import {
358358
preventRepeatedTerminalCloseShortcut,
359359
preventTerminalCloseShortcut,
@@ -5000,16 +5000,16 @@ export default function ChatView(props: ChatViewProps) {
50005000
});
50015001
}
50025002
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
5003-
const projectScriptTerminal = resolveProjectScriptTerminal({
5004-
drawerTerminalIds: terminalUiState.terminalIds,
5005-
activeTerminalId: terminalUiState.activeTerminalId,
5006-
panelTerminalIds,
5007-
allocatableTerminalIds: allocatableActiveTerminalIds,
5008-
runningTerminalIds,
5009-
preferNewTerminal: Boolean(options?.preferNewTerminal),
5010-
});
5003+
const reusableTerminalId = options?.preferNewTerminal
5004+
? null
5005+
: findReusableProjectScriptTerminal({
5006+
drawerTerminalIds: terminalUiState.terminalIds,
5007+
activeTerminalId: terminalUiState.activeTerminalId,
5008+
panelTerminalIds,
5009+
runningTerminalIds,
5010+
});
50115011
const shouldCreateNewTerminal =
5012-
projectScriptTerminal.isNew ||
5012+
reusableTerminalId === null ||
50135013
!canReuseTerminal ||
50145014
!readEnvironmentScope(environmentId, AuthTerminalReadScope);
50155015
const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
@@ -5031,9 +5031,7 @@ export default function ChatView(props: ChatViewProps) {
50315031
worktreePath: targetWorktreePath,
50325032
...(options?.env ? { extraEnv: options.env } : {}),
50335033
});
5034-
const targetTerminalId = shouldCreateNewTerminal
5035-
? allocateTerminalId()
5036-
: projectScriptTerminal.terminalId;
5034+
const targetTerminalId = shouldCreateNewTerminal ? allocateTerminalId() : reusableTerminalId;
50375035
const openTerminalInput: TerminalOpenInput = shouldCreateNewTerminal
50385036
? {
50395037
threadId: activeThreadId,
@@ -5126,7 +5124,6 @@ export default function ChatView(props: ChatViewProps) {
51265124
openPreview,
51275125
hasTerminalWriteAccess,
51285126
panelTerminalIds,
5129-
allocatableActiveTerminalIds,
51305127
canReuseTerminal,
51315128
allocateTerminalId,
51325129
runningTerminalIds,
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)