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
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(terminal): recreate xterm on resume
Vyapak Goyal24d482af97a3
diff --git a/src/renderer/src/components/AgentDetailPanel.tsx b/src/renderer/src/components/AgentDetailPanel.tsxindex 8f59450c..2e6eed1c 100644--- a/src/renderer/src/components/AgentDetailPanel.tsx+++ b/src/renderer/src/components/AgentDetailPanel.tsx@@ -4,6 +4,7 @@ import { PixelBadge } from './PixelBadge';import { PixelButton } from './PixelButton';import { SpritePortrait } from './SpritePortrait';import { PtyTerminalView } from './PtyTerminalView';+import { terminalInstanceKey } from './terminalRecovery';import { MessageQueueComposer } from './MessageQueueComposer';import { CommandCenterPanel } from './CommandCenterPanel';import { disposeTerminal } from './terminalPool';@@ -156,7 +157,7 @@ export function AgentDetailPanel({ agent }: AgentDetailPanelProps) {<div style={{ flex: 1, minWidth: 0, minHeight: 0, display: 'flex', flexDirection: 'column' }}><div style={{ flex: 1, minHeight: 0, display: 'flex' }}><PtyTerminalView- key={agent.ptyId}+ key={terminalInstanceKey(agent.ptyId, agent.terminalGeneration)}ptyId={agent.ptyId}onStreamData={onPtyStream}onUserPrompt={(t) => {diff --git a/src/renderer/src/components/CommandCenterPanel.tsx b/src/renderer/src/components/CommandCenterPanel.tsxindex f03a931a..3f13271e 100644--- a/src/renderer/src/components/CommandCenterPanel.tsx+++ b/src/renderer/src/components/CommandCenterPanel.tsx@@ -8,7 +8,8 @@ import { MessageQueueComposer } from './MessageQueueComposer';import { TasksKanban } from './TasksKanban';import { AskMeTab } from './AskMeTab';import { SchedulesTab } from './SchedulesTab';-import { acquireTerminal, resetTerminal } from './terminalPool';+import { acquireTerminal, disposeTerminal, resetTerminal } from './terminalPool';+import { terminalInstanceKey } from './terminalRecovery';import { Icon } from './Icon';import { MemoryGraphPanel } from './MemoryGraphPanel';import { useFleetTelemetry } from '@/hooks/useTelemetry';@@ -158,7 +159,7 @@ export function CommandCenterPanel({ agent }: { agent: Agent }) {<><div style={{ flex: 1, minHeight: 0, display: 'flex' }}><PtyTerminalView- key={agent.ptyId}+ key={terminalInstanceKey(agent.ptyId, agent.terminalGeneration)}ptyId={agent.ptyId}onStreamData={onPtyStream}onUserPrompt={(t) => {@@ -274,14 +275,35 @@ function FloorTab({ seed }: { seed: { text: string; seq: number } }) {throw new Error('Session transcript not found; current process was left running.');}}+ // Capture the live grid before replacing anything. Restart & Continue+ // recreates only this agent's xterm; model changes retain the old+ // in-place reset behavior.+ const oldEntry = acquireTerminal(a.ptyId);+ let cols = oldEntry.term.cols || 100;+ let rows = oldEntry.term.rows || 30;+ try {+ oldEntry.fit.fit();+ cols = oldEntry.term.cols;+ rows = oldEntry.term.rows;+ } catch { /* host not sized yet */ }+const killed = await window.cth.killPty(a.ptyId);if (!killed.ok) throw new Error(killed.error ?? 'Could not stop the current process.');- // 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, { preserveScrollback: resume });+ if (resume) {+ // A blank xterm can retain corrupt renderer/DOM/subscription state even+ // after its PTY is healthy. Throw that one terminal away, acquire its+ // replacement BEFORE spawning (so startup output has a listener), then+ // bump the key so React remounts only this agent's terminal card.+ disposeTerminal(a.ptyId);+ acquireTerminal(a.ptyId);+ updateAgent(a.id, {+ terminalGeneration: (a.terminalGeneration ?? 0) + 1,+ status: 'idle',+ action: 'recreating terminal…'+ });+ } else {+ resetTerminal(a.ptyId);+ }const command = buildSpawnCommand(cfg, model, provider);const [exe, ...args] = tokenizeCommand(command.trim());const hive = a.isGod@@ -289,12 +311,6 @@ 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 };- // 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,diff --git a/src/renderer/src/components/FullscreenTerminal.tsx b/src/renderer/src/components/FullscreenTerminal.tsxindex 3f54a4a5..92ddb8f7 100644--- a/src/renderer/src/components/FullscreenTerminal.tsx+++ b/src/renderer/src/components/FullscreenTerminal.tsx@@ -2,6 +2,7 @@ import { useEffect } from 'react';import { PixelBadge } from './PixelBadge';import { PixelButton } from './PixelButton';import { PtyTerminalView } from './PtyTerminalView';+import { terminalInstanceKey } from './terminalRecovery';import { MessageQueueComposer } from './MessageQueueComposer';import { disposeTerminal } from './terminalPool';import { Icon } from './Icon';@@ -137,7 +138,7 @@ export function FullscreenTerminal() {<div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column' }}><div style={{ flex: 1, minHeight: 0, display: 'flex' }}><PtyTerminalView- key={agent.ptyId}+ key={terminalInstanceKey(agent.ptyId, agent.terminalGeneration)}ptyId={agent.ptyId}onStreamData={parser}onUserPrompt={(t) => {diff --git a/src/renderer/src/components/terminalPool.ts b/src/renderer/src/components/terminalPool.tsindex 09900884..41db4549 100644--- a/src/renderer/src/components/terminalPool.ts+++ b/src/renderer/src/components/terminalPool.ts@@ -19,6 +19,7 @@ import { WebglAddon } from '@xterm/addon-webgl';import { Unicode11Addon } from '@xterm/addon-unicode11';import {createTerminalRecoveryState,+ normalizePtyChunk,requestInitialPtyRedraw,scheduleWebglRecovery,type TerminalRecoveryState@@ -117,7 +118,9 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):// Subscribe to the pty stream ONCE for the terminal's whole lifetime, so the// buffer keeps filling even while this terminal isn't mounted in any view.- entry.unsub.push(window.cth.onPtyData(ptyId, (chunk) => {+ entry.unsub.push(window.cth.onPtyData(ptyId, (rawChunk) => {+ const chunk = normalizePtyChunk(rawChunk);+ if (!chunk) return;const active = term.buffer.active;const follow = shouldFollowTerminalOutput(active.viewportY, active.baseY);term.write(chunk, () => {diff --git a/src/renderer/src/components/terminalRecovery.ts b/src/renderer/src/components/terminalRecovery.tsindex 58351811..10018445 100644--- a/src/renderer/src/components/terminalRecovery.ts+++ b/src/renderer/src/components/terminalRecovery.ts@@ -7,6 +7,22 @@ export function createTerminalRecoveryState(): TerminalRecoveryState {return { initialRedrawRequested: false, webglRecoveryPending: false };}+/** React key for one disposable xterm instance attached to a stable PTY id. */+export function terminalInstanceKey(ptyId: string, generation = 0): string {+ return `${ptyId}:${generation}`;+}++/** Accept output from the current string protocol and the short-lived replay+ * protocol so a renderer hot reload stays usable until the app next exits. */+export function normalizePtyChunk(value: unknown): string {+ if (typeof value === 'string') return value;+ if (value && typeof value === 'object' && 'data' in value) {+ const data = (value as { data?: unknown }).data;+ if (typeof data === 'string') return data;+ }+ return '';+}+/** Request exactly one redraw after the renderer has subscribed to PTY output. */export function requestInitialPtyRedraw(state: TerminalRecoveryState,diff --git a/src/renderer/src/store/store.ts b/src/renderer/src/store/store.tsindex e081b81c..05d43e56 100644--- a/src/renderer/src/store/store.ts+++ b/src/renderer/src/store/store.ts@@ -52,6 +52,9 @@ export interface Agent {blockReason?: BlockReason;/** present iff this agent has a real PTY in the main process */ptyId?: string;+ /** Incremented by Restart & Continue to remount this agent's xterm without+ * changing its durable PTY/session identity. */+ terminalGeneration?: number;/** the command being run in the PTY (e.g. 'claude' or 'agy') */command?: string;/** which agent CLI preset owns this PTY recipe; drives the model picker +diff --git a/test/terminal-recovery.test.cjs b/test/terminal-recovery.test.cjsindex 174680ab..e8dc9166 100644--- a/test/terminal-recovery.test.cjs+++ b/test/terminal-recovery.test.cjs@@ -6,10 +6,23 @@ const loadTs = require('./load-ts.cjs');const {createTerminalRecoveryState,+ normalizePtyChunk,requestInitialPtyRedraw,- scheduleWebglRecovery+ scheduleWebglRecovery,+ terminalInstanceKey} = loadTs('src/renderer/src/components/terminalRecovery.ts');+test('terminal generation creates a new instance for the same PTY', () => {+ assert.equal(terminalInstanceKey('pty-agent', 0), 'pty-agent:0');+ assert.equal(terminalInstanceKey('pty-agent', 1), 'pty-agent:1');+});++test('terminal output accepts current and pre-revert message shapes', () => {+ assert.equal(normalizePtyChunk('current'), 'current');+ assert.equal(normalizePtyChunk({ seq: 7, data: 'pre-revert' }), 'pre-revert');+ assert.equal(normalizePtyChunk({ data: 7 }), '');+});+test('initial attach requests exactly one PTY redraw', () => {const state = createTerminalRecoveryState();let redraws = 0;