@devries-cameron20 / t3code-1 / commits / 80498f1709
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Handle terminal exit writes and auto-close exited terminal view

- Ignore late write calls after a terminal session has already exited - Add regression test for trailing writes after exit - Close terminal drawer session once an exit event is handled

Julius Marminge committed Feb 27, 2026 at 17:55 UTC 80498f1709e05caa744716b740fdcaa884bef8e0
3 files changed +36
apps/server/src/terminal/Layers/Manager.test.ts
+15
@@ -394,6 +394,21 @@ describe("TerminalManager", () => {
394 manager.dispose();
395 });
396
397 + it("ignores trailing writes after terminal exit", async () => {
398 + const { manager, ptyAdapter } = makeManager();
399 + await manager.open(openInput());
400 + const process = ptyAdapter.processes[0];
401 + expect(process).toBeDefined();
402 + if (!process) return;
403 +
404 + process.emitExit({ exitCode: 0, signal: 0 });
405 +
406 + await expect(manager.write({ threadId: "thread-1", data: "\r" })).resolves.toBeUndefined();
407 + expect(process.writes).toEqual([]);
408 +
409 + manager.dispose();
410 + });
411 +
412 it("emits subprocess activity events when child-process state changes", async () => {
413 let hasRunningSubprocess = false;
414 const { manager } = makeManager(5, {
apps/server/src/terminal/Layers/Manager.ts
+3
@@ -438,6 +438,9 @@ export class TerminalManagerRuntime extends EventEmitter<TerminalManagerEvents>
438 const input = decodeTerminalWriteInput(raw);
439 const session = this.requireSession(input.threadId, input.terminalId);
440 if (!session.process || session.status !== "running") {
441 + if (session.status === "exited") {
442 + return;
443 + }
444 throw new Error(
445 `Terminal is not running for thread: ${input.threadId}, terminal: ${input.terminalId}`,
446 );
apps/web/src/components/ThreadTerminalDrawer.tsx
+18
@@ -112,6 +112,7 @@ interface TerminalViewportProps {
112 terminalId: string;
113 cwd: string;
114 runtimeEnv?: Record<string, string>;
115 + onSessionExited: () => void;
116 focusRequestId: number;
117 autoFocus: boolean;
118 resizeEpoch: number;
@@ -123,6 +124,7 @@ function TerminalViewport({
124 terminalId,
125 cwd,
126 runtimeEnv,
127 + onSessionExited,
128 focusRequestId,
129 autoFocus,
130 resizeEpoch,
@@ -131,6 +133,12 @@ function TerminalViewport({
133 const containerRef = useRef<HTMLDivElement>(null);
134 const terminalRef = useRef<Terminal | null>(null);
135 const fitAddonRef = useRef<FitAddon | null>(null);
136 + const onSessionExitedRef = useRef(onSessionExited);
137 + const hasHandledExitRef = useRef(false);
138 +
139 + useEffect(() => {
140 + onSessionExitedRef.current = onSessionExited;
141 + }, [onSessionExited]);
142
143 useEffect(() => {
144 const mount = containerRef.current;
@@ -306,6 +314,7 @@ function TerminalViewport({
314 }
315
316 if (event.type === "started" || event.type === "restarted") {
317 + hasHandledExitRef.current = false;
318 activeTerminal.write("\u001bc");
319 if (event.snapshot.history.length > 0) {
320 activeTerminal.write(event.snapshot.history);
@@ -335,6 +344,13 @@ function TerminalViewport({
344 activeTerminal,
345 details.length > 0 ? `Process exited (${details})` : "Process exited",
346 );
347 + if (hasHandledExitRef.current) {
348 + return;
349 + }
350 + hasHandledExitRef.current = true;
351 + window.setTimeout(() => {
352 + onSessionExitedRef.current();
353 + }, 0);
354 }
355 });
356
@@ -783,6 +799,7 @@ export default function ThreadTerminalDrawer({
799 terminalId={terminalId}
800 cwd={cwd}
801 {...(runtimeEnv ? { runtimeEnv } : {})}
802 + onSessionExited={() => onCloseTerminal(terminalId)}
803 focusRequestId={focusRequestId}
804 autoFocus={terminalId === resolvedActiveTerminalId}
805 resizeEpoch={resizeEpoch}
@@ -800,6 +817,7 @@ export default function ThreadTerminalDrawer({
817 terminalId={resolvedActiveTerminalId}
818 cwd={cwd}
819 {...(runtimeEnv ? { runtimeEnv } : {})}
820 + onSessionExited={() => onCloseTerminal(resolvedActiveTerminalId)}
821 focusRequestId={focusRequestId}
822 autoFocus
823 resizeEpoch={resizeEpoch}