Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(cli): bring back the conversation typed into a terminal after a reboot

bubu-dudub395fc7d618b
diff --git a/cli/src/lib/restoreAgents.spec.ts b/cli/src/lib/restoreAgents.spec.ts
index e25c45c0d..566c1a9a1 100644
--- a/cli/src/lib/restoreAgents.spec.ts
+++ b/cli/src/lib/restoreAgents.spec.ts
@@ -573,8 +573,8 @@ describe('restoreAgents — terminals', () => {
expect(h.launches).toEqual([{ agentId: 'agent-a', resumeSessionId: 'session-a' }, { agentId: 'agent-a' }])
})
- it('brings a terminal whose adopted engine AND pane are gone back as a shell, never as the engine', async () => {
- const h = harness([terminal({ engine: 'claude', sessionId: 'session-t', boundAt: 1 })])
+ it('brings a terminal whose adopted engine left no conversation id back as a shell', async () => {
+ const h = harness([terminal({ engine: 'claude', sessionId: '', boundAt: 1 })])
const summary = await restoreAgents(h.deps)
expect(summary).toEqual({ restored: ['term-1'], skipped: [], failed: [], unsurveyed: [] })
expect(h.calls[0]).toBe('releaseEngine:term-1')
@@ -582,6 +582,36 @@ describe('restoreAgents — terminals', () => {
expect(h.launched[0].launch.argv[0]).toBe('terminal')
expect(h.rows.get('term-1')).toMatchObject({ engine: 'terminal', launch: { state: 'ready' } })
})
+
+ it.each(['opencode', 'claude'] as const)('brings a terminal back resuming the %s conversation typed into it', async (engine) => {
+ // Harness OS's welcome types OpenCode into a terminal; after a reboot it came back a bare prompt.
+ const h = harness([terminal({ engine, sessionId: 'ses_kept', boundAt: 1 })])
+ h.probes.set('%0', [identity(9)])
+ const summary = await restoreAgents(h.deps)
+ expect(summary).toEqual({ restored: ['term-1'], skipped: [], failed: [], unsurveyed: [] })
+ expect(h.calls).not.toContain('releaseEngine:term-1')
+ expect(h.launches).toEqual([{ agentId: 'term-1', resumeSessionId: 'ses_kept' }])
+ expect(h.launched[0].launch.argv).toEqual([engine, '--resume', 'ses_kept'])
+ await settled(h, 1, 1)
+ expect(h.rows.get('term-1')).toMatchObject({ engine, terminalHost: true, sessionId: 'ses_kept' })
+ expect(h.respawns).toBe(0)
+ })
+
+ it('reopens the terminal, never a fresh engine, when the conversation it was resuming is refused', async () => {
+ const h = harness([terminal({ engine: 'opencode', sessionId: 'ses_gone', boundAt: 1 })])
+ const kept: string[] = []
+ h.deps.keepAbandoned = (left) => { kept.push(left.sessionId) }
+ h.states.set('%0', [{ dead: true }])
+ const summary = await restoreAgents(h.deps)
+ expect(summary.restored).toEqual(['term-1'])
+ await settled(h, 1)
+ expect(kept).toEqual(['ses_gone'])
+ expect(h.calls).toContain('releaseEngine:term-1')
+ expect(h.launches).toEqual([{ agentId: 'term-1', resumeSessionId: 'ses_gone' }, { agentId: 'term-1' }])
+ expect(h.calls).toContain('respawn:%0:terminal')
+ expect(h.calls).not.toContain('unbind:ses_gone')
+ expect(h.rows.get('term-1')).toMatchObject({ engine: 'terminal', launch: { state: 'ready' } })
+ })
})
diff --git a/cli/src/lib/restoreAgents.ts b/cli/src/lib/restoreAgents.ts
index 71525572b..6032c3225 100644
--- a/cli/src/lib/restoreAgents.ts
+++ b/cli/src/lib/restoreAgents.ts
@@ -31,6 +31,7 @@ import { isTerminalEngine, type AgentEngine } from '../engines/types.js'
import type { AgentLaunch, ProcessIdentity, RegisteredSession } from './registry.js'
import type { TerminalRuntimeRef, TmuxRuntimeRef } from './terminalTypes.js'
import { terminalRouteKey } from './terminalRuntime.js'
+import { resumesConversation } from './resumeCapability.js'
export interface RestoreLaunch {
argv: string[]
@@ -251,9 +252,13 @@ export async function restoreAgents(deps: RestoreAgentsDeps): Promise<RestoreSum
summary.skipped.push({ agentId: entry.agentId, reason: 'saved conversation awaits explicit Open' })
continue
}
- // A terminal whose pane is gone comes back as a terminal — never as the engine that was once
- // typed into it: that engine's session went with the pane.
- if (entry.terminalHost) {
+ // A terminal whose pane is gone comes back as a terminal — unless the engine typed into it keeps
+ // its conversation on disk under a recorded id. Then the session did NOT go with the pane, and
+ // the engine comes back resuming it, in a shell pane as before (it drops to that shell on exit).
+ // Measured on Harness OS: OpenCode, which its welcome flow types into a terminal, came back from
+ // every reboot as a bare prompt with the conversation unbound, while Claude came back. A resume
+ // the engine refuses falls back to the shell, never a fresh engine (`relaunchFresh`).
+ if (entry.terminalHost && !resumesConversation(entry.engine, entry.sessionId)) {
if (!isTerminalEngine(entry.engine)) deps.registry.releaseEngine(entry.agentId)
missing.push({ entry: deps.registry.byAgent(entry.agentId) ?? entry, runtime })
continue
@@ -373,6 +378,27 @@ async function watchRestoredPane(
fail('ENGINE_DID_NOT_START', `${engine} exited before its engine process became ready. See the terminal output for details.`)
return false
}
+ // A terminal that was resuming an engine typed into it goes back to being that terminal: nobody
+ // asked this pane for a new conversation. The one it held is kept as a stopped harness.
+ if (entry.terminalHost) {
+ mayRetryFresh = false
+ deps.log(`[restore] ${engine} · agent ${agentId} · did not come back up resuming its session — back to the terminal`)
+ deps.keepAbandoned?.({ ...entry })
+ deps.registry.releaseEngine(agentId)
+ const launch = await deps.buildLaunch(deps.registry.byAgent(agentId) ?? { ...entry, engine: 'terminal' }, {})
+ if ('error' in launch) {
+ fail(launch.error, launch.detail)
+ return false
+ }
+ const spawned = await deps.respawn(runtime, launch)
+ if (!spawned.ok) {
+ fail('ENGINE_DID_NOT_START', `The terminal could not be reopened: ${spawned.reason ?? 'unknown reason'}`)
+ return false
+ }
+ deps.registry.setLaunch(agentId, { state: 'ready' })
+ await deps.clearRemainOnExit(runtime)
+ return false
+ }
// A resume id the engine no longer honours is not worth a dead agent: the row's name comes
// along to the agent itself, the stale binding goes, and the engine gets one fresh start.
mayRetryFresh = false