The best way to control Claude Code, Codex and more from any computer or phone
Handle optional terminal dimensions and preserve active terminal size
- make `cols`/`rows` optional in terminal open contracts - default new/restarted sessions to 120x30 when size hints are missing - reuse existing terminal dimensions unless a new size is explicitly requested - only send script terminal size hints when creating a new terminal
Julius Marminge committed
Feb 17, 2026 at 23:19 UTC
1a94fcbd294ab69709a3895b30941537d415764a
4 files changed
+92
-16
apps/server/src/terminalManager.test.ts
+59
@@ -196,6 +196,65 @@ describe("TerminalManager", () => {
196
manager.dispose();
197
});
198
199
+ it("resizes running terminal on open when a different size is requested", async () => {
200
+ const { manager, ptyAdapter } = makeManager();
201
+ await manager.open(openInput({ cols: 100, rows: 24 }));
202
+ const process = ptyAdapter.processes[0];
203
+ expect(process).toBeDefined();
204
+ if (!process) return;
205
+
206
+ await manager.open(openInput({ cols: 140, rows: 40 }));
207
+
208
+ expect(process.resizeCalls).toEqual([{ cols: 140, rows: 40 }]);
209
+
210
+ manager.dispose();
211
+ });
212
+
213
+ it("preserves existing terminal size on open when size is omitted", async () => {
214
+ const { manager, ptyAdapter } = makeManager();
215
+ await manager.open(openInput({ cols: 100, rows: 24 }));
216
+ const ptyProcess = ptyAdapter.processes[0];
217
+ expect(ptyProcess).toBeDefined();
218
+ if (!ptyProcess) return;
219
+
220
+ await manager.open({
221
+ threadId: "thread-1",
222
+ cwd: globalThis.process.cwd(),
223
+ });
224
+
225
+ expect(ptyProcess.resizeCalls).toEqual([]);
226
+
227
+ ptyProcess.emitExit({ exitCode: 0, signal: 0 });
228
+ await manager.open({
229
+ threadId: "thread-1",
230
+ cwd: globalThis.process.cwd(),
231
+ });
232
+
233
+ const resumedSpawn = ptyAdapter.spawnInputs[1];
234
+ expect(resumedSpawn).toBeDefined();
235
+ if (!resumedSpawn) return;
236
+ expect(resumedSpawn.cols).toBe(100);
237
+ expect(resumedSpawn.rows).toBe(24);
238
+
239
+ manager.dispose();
240
+ });
241
+
242
+ it("uses default dimensions when opening a new terminal without size hints", async () => {
243
+ const { manager, ptyAdapter } = makeManager();
244
+ await manager.open({
245
+ threadId: "thread-1",
246
+ cwd: process.cwd(),
247
+ });
248
+
249
+ const spawned = ptyAdapter.spawnInputs[0];
250
+ expect(spawned).toBeDefined();
251
+ if (!spawned) return;
252
+ expect(spawned.cols).toBe(120);
253
+ expect(spawned.rows).toBe(30);
254
+
255
+ manager.dispose();
256
+ });
257
+
258
it("supports multiple terminals per thread with isolated sessions", async () => {
259
const { manager, ptyAdapter } = makeManager();
260
await manager.open(openInput({ terminalId: "default" }));
apps/server/src/terminalManager.ts
+25
-12
@@ -26,6 +26,8 @@ import { runProcess } from "./processRunner";
26
const DEFAULT_HISTORY_LINE_LIMIT = 5_000;
27
const DEFAULT_PERSIST_DEBOUNCE_MS = 40;
28
const DEFAULT_SUBPROCESS_POLL_INTERVAL_MS = 1_000;
29
+const DEFAULT_OPEN_COLS = 120;
30
+const DEFAULT_OPEN_ROWS = 30;
31
const TERMINAL_ENV_BLOCKLIST = new Set(["PORT", "ELECTRON_RENDERER_PORT", "ELECTRON_RUN_AS_NODE"]);
32
33
type TerminalSubprocessChecker = (terminalPid: number) => Promise<boolean>;
@@ -67,6 +69,8 @@ interface ShellCandidate {
69
args?: string[];
70
}
71
72
+type TerminalStartInput = TerminalOpenInput & { cols: number; rows: number };
73
+
74
function defaultShellResolver(): string {
75
if (process.platform === "win32") {
76
return process.env.ComSpec ?? "cmd.exe";
@@ -334,6 +338,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
338
if (!existing) {
339
await this.flushPersistQueue(input.threadId, input.terminalId);
340
const history = await this.readHistory(input.threadId, input.terminalId);
341
+ const cols = input.cols ?? DEFAULT_OPEN_COLS;
342
+ const rows = input.rows ?? DEFAULT_OPEN_ROWS;
343
const session: TerminalSessionState = {
344
threadId: input.threadId,
345
terminalId: input.terminalId,
@@ -344,8 +350,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
350
exitCode: null,
351
exitSignal: null,
352
updatedAt: new Date().toISOString(),
347
- cols: input.cols,
348
- rows: input.rows,
353
+ cols,
354
+ rows,
355
process: null,
356
unsubscribeData: null,
357
unsubscribeExit: null,
@@ -353,12 +359,14 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
359
runtimeEnv: normalizedRuntimeEnv(input.env),
360
};
361
this.sessions.set(sessionKey, session);
356
- this.startSession(session, input, "started");
362
+ this.startSession(session, { ...input, cols, rows }, "started");
363
return this.snapshot(session);
364
}
365
366
const nextRuntimeEnv = normalizedRuntimeEnv(input.env);
367
const currentRuntimeEnv = existing.runtimeEnv;
368
+ const targetCols = input.cols ?? existing.cols;
369
+ const targetRows = input.rows ?? existing.rows;
370
const runtimeEnvChanged =
371
JSON.stringify(currentRuntimeEnv) !== JSON.stringify(nextRuntimeEnv);
372
@@ -377,14 +385,14 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
385
}
386
387
if (!existing.process) {
380
- this.startSession(existing, input, "started");
388
+ this.startSession(existing, { ...input, cols: targetCols, rows: targetRows }, "started");
389
return this.snapshot(existing);
390
}
391
384
- if (existing.cols !== input.cols || existing.rows !== input.rows) {
385
- existing.cols = input.cols;
386
- existing.rows = input.rows;
387
- existing.process.resize(input.cols, input.rows);
392
+ if (existing.cols !== targetCols || existing.rows !== targetRows) {
393
+ existing.cols = targetCols;
394
+ existing.rows = targetRows;
395
+ existing.process.resize(targetCols, targetRows);
396
existing.updatedAt = new Date().toISOString();
397
}
398
@@ -441,6 +449,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
449
const sessionKey = toSessionKey(input.threadId, input.terminalId);
450
let session = this.sessions.get(sessionKey);
451
if (!session) {
452
+ const cols = input.cols ?? DEFAULT_OPEN_COLS;
453
+ const rows = input.rows ?? DEFAULT_OPEN_ROWS;
454
session = {
455
threadId: input.threadId,
456
terminalId: input.terminalId,
@@ -451,8 +461,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
461
exitCode: null,
462
exitSignal: null,
463
updatedAt: new Date().toISOString(),
454
- cols: input.cols,
455
- rows: input.rows,
464
+ cols,
465
+ rows,
466
process: null,
467
unsubscribeData: null,
468
unsubscribeExit: null,
@@ -466,9 +476,12 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
476
session.runtimeEnv = normalizedRuntimeEnv(input.env);
477
}
478
479
+ const cols = input.cols ?? session.cols;
480
+ const rows = input.rows ?? session.rows;
481
+
482
session.history = "";
483
await this.persistHistory(input.threadId, input.terminalId, session.history);
471
- this.startSession(session, input, "restarted");
484
+ this.startSession(session, { ...input, cols, rows }, "restarted");
485
return this.snapshot(session);
486
});
487
}
@@ -517,7 +530,7 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
530
531
private startSession(
532
session: TerminalSessionState,
520
- input: TerminalOpenInput,
533
+ input: TerminalStartInput,
534
eventType: "started" | "restarted",
535
): void {
536
this.stopProcess(session);
apps/web/src/components/ChatView.tsx
+6
-2
@@ -725,9 +725,13 @@ export default function ChatView() {
725
threadId: activeThreadId,
726
terminalId: targetTerminalId,
727
cwd: targetCwd,
728
- cols: SCRIPT_TERMINAL_COLS,
729
- rows: SCRIPT_TERMINAL_ROWS,
728
env: runtimeEnv,
729
+ ...(shouldCreateNewTerminal
730
+ ? {
731
+ cols: SCRIPT_TERMINAL_COLS,
732
+ rows: SCRIPT_TERMINAL_ROWS,
733
+ }
734
+ : {}),
735
});
736
await api.terminal.write({
737
threadId: activeThreadId,
packages/contracts/src/terminal.ts
+2
-2
@@ -21,8 +21,8 @@ export const terminalSessionInputSchema = terminalThreadInputSchema.extend({
21
22
export const terminalOpenInputSchema = terminalSessionInputSchema.extend({
23
cwd: z.string().trim().min(1),
24
- cols: terminalColsSchema,
25
- rows: terminalRowsSchema,
24
+ cols: terminalColsSchema.optional(),
25
+ rows: terminalRowsSchema.optional(),
26
env: terminalEnvSchema.optional(),
27
});
28