Skip to content

Commit 0a1c9a3

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

3 files changed

Lines changed: 36 additions & 60 deletions

File tree

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ import {
360360
import { confirmTerminalClose, isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm";
361361
import { isPreviewFocused } from "../lib/previewFocus";
362362
import { getTerminalFocusOwner } from "../lib/terminalFocus";
363-
import { resolveProjectScriptTerminal } from "../lib/projectScriptTerminal";
363+
import { findReusableProjectScriptTerminal } from "../lib/projectScriptTerminal";
364364
import {
365365
preventRepeatedTerminalCloseShortcut,
366366
preventTerminalCloseShortcut,
@@ -4819,16 +4819,16 @@ export default function ChatView(props: ChatViewProps) {
48194819
});
48204820
}
48214821
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
4822-
const projectScriptTerminal = resolveProjectScriptTerminal({
4823-
drawerTerminalIds: terminalUiState.terminalIds,
4824-
activeTerminalId: terminalUiState.activeTerminalId,
4825-
panelTerminalIds,
4826-
allocatableTerminalIds: allocatableActiveTerminalIds,
4827-
runningTerminalIds,
4828-
preferNewTerminal: Boolean(options?.preferNewTerminal),
4829-
});
4822+
const reusableTerminalId = options?.preferNewTerminal
4823+
? null
4824+
: findReusableProjectScriptTerminal({
4825+
drawerTerminalIds: terminalUiState.terminalIds,
4826+
activeTerminalId: terminalUiState.activeTerminalId,
4827+
panelTerminalIds,
4828+
runningTerminalIds,
4829+
});
48304830
const shouldCreateNewTerminal =
4831-
projectScriptTerminal.isNew ||
4831+
reusableTerminalId === null ||
48324832
!canReuseTerminal ||
48334833
!readEnvironmentScope(environmentId, AuthTerminalReadScope);
48344834
const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
@@ -4850,9 +4850,7 @@ export default function ChatView(props: ChatViewProps) {
48504850
worktreePath: targetWorktreePath,
48514851
...(options?.env ? { extraEnv: options.env } : {}),
48524852
});
4853-
const targetTerminalId = shouldCreateNewTerminal
4854-
? allocateTerminalId()
4855-
: projectScriptTerminal.terminalId;
4853+
const targetTerminalId = shouldCreateNewTerminal ? allocateTerminalId() : reusableTerminalId;
48564854
const openTerminalInput: TerminalOpenInput = shouldCreateNewTerminal
48574855
? {
48584856
threadId: activeThreadId,
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)