Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(pty): keep terminal live + typeable across model change / restart

Vyapak Goyal352e4aa30e9a
diff --git a/src/main/pty.ts b/src/main/pty.ts
index 6506add7..d1c956b5 100644
--- a/src/main/pty.ts
+++ b/src/main/pty.ts
@@ -209,14 +209,28 @@ export class PtyManager {
} as Record<string, string>
});
- this.sessions.set(opts.id, { id: opts.id, proc, cwd: opts.cwd, command: resolved, lastOutputAt: Date.now() });
+ // Capture THIS session object so the proc's callbacks can tell whether the
+ // id still belongs to them. A model change / restart does kill()+spawn()
+ // reusing the SAME id: the old process's kill is asynchronous, so its
+ // onData/onExit can fire AFTER the replacement session is already in the
+ // map. Without the identity guard the dying process would (a) spray its
+ // final bytes into the new agent's fresh TUI frame — scattered/overlapping
+ // text — and (b) on exit delete the replacement session and emit a false
+ // `pty:exit`, killing input to the agent that just started.
+ const session: PtySession = { id: opts.id, proc, cwd: opts.cwd, command: resolved, lastOutputAt: Date.now() };
+ this.sessions.set(opts.id, session);
proc.onData((data) => {
- const s = this.sessions.get(opts.id);
- if (s) s.lastOutputAt = Date.now();
+ // Drop trailing output from a process whose id was already reclaimed by
+ // a respawn (or killed) — it would corrupt the new session's screen.
+ if (this.sessions.get(opts.id) !== session) return;
+ session.lastOutputAt = Date.now();
this.safeSend(`pty:data:${opts.id}`, data);
});
proc.onExit(({ exitCode, signal }) => {
+ // Stale exit from a process whose id was reclaimed (kill()+respawn) — do
+ // NOT touch the live session or tell the renderer the new pty died.
+ if (this.sessions.get(opts.id) !== session) return;
this.safeSend(`pty:exit:${opts.id}`, { exitCode, signal });
this.sessions.delete(opts.id);
// Natural exit must run the same lifecycle teardown as an explicit kill.
diff --git a/src/renderer/src/components/CommandCenterPanel.tsx b/src/renderer/src/components/CommandCenterPanel.tsx
index c7d10fa4..8bcb330b 100644
--- a/src/renderer/src/components/CommandCenterPanel.tsx
+++ b/src/renderer/src/components/CommandCenterPanel.tsx
@@ -8,7 +8,7 @@ import { MessageQueueComposer } from './MessageQueueComposer';
import { TasksKanban } from './TasksKanban';
import { AskMeTab } from './AskMeTab';
import { SchedulesTab } from './SchedulesTab';
-import { disposeTerminal } from './terminalPool';
+import { acquireTerminal, resetTerminal } from './terminalPool';
import { Icon } from './Icon';
import { MemoryGraphPanel } from './MemoryGraphPanel';
import { useFleetTelemetry } from '@/hooks/useTelemetry';
@@ -235,7 +235,12 @@ function FloorTab({ seed }: { seed: { text: string; seq: number } }) {
try {
const cfg = await window.cth.getConfig();
await window.cth.killPty(a.ptyId);
- disposeTerminal(a.ptyId);
+ // Soft-reset the pooled terminal in place rather than disposing it: the
+ // same ptyId is reused, so its data subscription, opened xterm and keyboard
+ // wiring all survive the restart. (Disposing dropped the live terminal while
+ // the view — keyed on the unchanged ptyId — never re-attached a replacement,
+ // leaving a dead pane that swallowed every keystroke.)
+ resetTerminal(a.ptyId);
// Respawn on the same CLI this agent already runs on (inferred from its
// command if not explicitly tagged) so an Antigravity/Codex worker stays
// on its own binary. tokenizeCommand keeps quoted model labels one arg.
@@ -247,7 +252,13 @@ function FloorTab({ seed }: { seed: { text: string; seq: number } }) {
: a.isAssistant
? { id: a.id, name: a.name, cwd: a.cwd, provider, isAssistant: true, role: "Michael's prep assistant" }
: { id: a.id, name: a.name, cwd: a.cwd, provider, role: a.description };
- const res = await window.cth.spawnPty({ id: a.ptyId, cwd: a.cwd, command: exe, args, provider, cols: 100, rows: 30, hive });
+ // Spawn the replacement at the terminal's REAL grid, not the fixed 100×30
+ // default. A size mismatch makes the Claude TUI's absolute cursor moves land
+ // in the wrong cells, so the screen renders as scattered/overlapping text.
+ const entry = acquireTerminal(a.ptyId);
+ let cols = 100, rows = 30;
+ try { entry.fit.fit(); cols = entry.term.cols; rows = entry.term.rows; } catch { /* host not sized yet */ }
+ const res = await window.cth.spawnPty({ id: a.ptyId, cwd: a.cwd, command: exe, args, provider, cols, rows, hive });
if (res.ok) updateAgent(a.id, { command: command.trim(), provider, model, status: 'idle', action: 'restarting…' });
} catch { /* noop */ } finally {
setRestarting(null);
diff --git a/src/renderer/src/components/terminalPool.ts b/src/renderer/src/components/terminalPool.ts
index 0e73ccde..e9b35b56 100644
--- a/src/renderer/src/components/terminalPool.ts
+++ b/src/renderer/src/components/terminalPool.ts
@@ -184,6 +184,29 @@ export function attachTerminal(entry: TerminalEntry, container: HTMLElement): vo
}
}
+/**
+ * Soft-reset a pooled terminal for an IN-PLACE pty respawn (the same ptyId is
+ * reused — e.g. a model change or agent restart). Clears the screen + scrollback
+ * and re-arms input while keeping the SAME Terminal, its live data subscription
+ * and its DOM attachment, so the mounted view stays visible and typeable across
+ * the restart.
+ *
+ * Why not disposeTerminal here: the view (PtyTerminalView) keys its attach effect
+ * on the ptyId, which doesn't change on a restart — so it never re-attaches a
+ * replacement terminal. Disposing therefore left a dead, detached pane that
+ * swallowed every keystroke. Resetting in place avoids that entirely.
+ */
+export function resetTerminal(ptyId: string): void {
+ const entry = pool.get(ptyId);
+ if (!entry) return;
+ // Re-arm input — a prior exit (or the kill that precedes the respawn) may have
+ // latched `exited`, which otherwise makes onData drop keystrokes silently.
+ entry.exited = false;
+ // Wipe the old frame + the "process exited" line so the restarted TUI paints
+ // onto a clean grid instead of overlapping stale content.
+ try { entry.term.reset(); } catch { /* not yet open */ }
+}
+
/** Tear down a pty's terminal (call when the agent/pty is gone for good). */
export function disposeTerminal(ptyId: string): void {
const entry = pool.get(ptyId);