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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(cli): wait for restored engines before rejecting input (#866)
Deeb19492e281ef
diff --git a/cli/e2e/restoreInput.e2e.ts b/cli/e2e/restoreInput.e2e.tsnew file mode 100644index 000000000..36514623e--- /dev/null+++ b/cli/e2e/restoreInput.e2e.ts@@ -0,0 +1,63 @@+/** Messages sent as restored agents become visible must reach the engines after shell startup. */+import { existsSync, mkdirSync, writeFileSync } from 'node:fs'+import { join } from 'node:path'+import { describe, expect, it, onTestFailed } from 'vitest'+import { shellSingleQuote } from '../src/lib/engineLaunch.js'+import { LocalClient } from './harness/client.js'+import { IsolatedDaemon, until } from './harness/daemon.js'++describe('input during agent restore', () => {+ it.each(['claude', 'codex'] as const)('%s accepts a message before its restored engine starts', async (engine) => {+ const d = await IsolatedDaemon.create({ env: { SHELL: '/bin/bash' } })+ const entered = join(d.root, 'shell-started')+ const release = join(d.root, 'start-engine')+ let client: LocalClient | undefined+ onTestFailed(() => { console.log(`---- daemon log\n${d.log()}`) })+ try {+ await d.start()+ client = await LocalClient.connect(d)+ const cwd = join(d.projectsDir, engine)+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 60_000)+ expect(created.error).toBeUndefined()+ const agentId = created.agent.id+ const original = await until('the original conversation to bind', async () => {+ const { agents } = await client!.request('agents_list')+ return agents.find((agent: any) => agent.id === agentId && agent.sessionId) ?? null+ }, 30_000)+ const before = client.next((frame) => frame.type === 'turn_ended' && frame.agentId === agentId)+ client.send('message', { agentId, content: 'before the restart' })+ await before+ client.close()+ await d.stop()+ await d.tmux.run('kill-server')++ // Found by QA on a quiet machine: restore exposed an active Codex agent before its process existed.+ // Hold the interactive shell before exec, so the test needs no scheduling race or fake-engine change.+ writeFileSync(join(d.env.HOME!, '.bashrc'), `if [ -n "\${TMUX_PANE:-}" ]; then+ : > ${shellSingleQuote(entered)}+ while [ ! -e ${shellSingleQuote(release)} ]; do sleep 0.05; done+fi+`)+ await d.start()+ await until('the restored shell to reach the startup gate', async () => existsSync(entered), 15_000)+ client = await LocalClient.connect(d)+ const { agents } = await client.request('agents_list')+ expect(agents.find((agent: any) => agent.id === agentId)).toMatchObject({+ status: 'active', sessionId: original.sessionId,+ })+ const outcome = client.next((frame) => frame.agentId === agentId+ && (frame.type === 'turn_ended' || frame.type === 'error'), 30_000, 'the message sent during restore')+ client.send('message', { agentId, content: 'sent while the restored shell was starting' })+ await new Promise((resolve) => setTimeout(resolve, 1_000))+ writeFileSync(release, '')+ expect(await outcome).toMatchObject({ type: 'turn_ended', agentId })+ expect(client.frames.filter((frame) => frame.type === 'turn_started' && frame.agentId === agentId))+ .toEqual([expect.objectContaining({ payload: expect.objectContaining({ userMessage: 'sent while the restored shell was starting' }) })])+ } finally {+ writeFileSync(release, '')+ client?.close()+ await d.close()+ }+ })+})diff --git a/cli/src/lib/terminalBackendCoordinator.spec.ts b/cli/src/lib/terminalBackendCoordinator.spec.tsindex a7e6f7ab2..2e4498d90 100644--- a/cli/src/lib/terminalBackendCoordinator.spec.ts+++ b/cli/src/lib/terminalBackendCoordinator.spec.ts@@ -286,6 +286,41 @@ describe('TerminalBackendCoordinator', () => {expect(waits).toEqual([10])})+ it('waits for a restored agent whose launch is starting before an engine process exists', async () => {+ // Found by QA on a quiet machine: a message after restore was rejected before Codex started.+ const answers = [{ state: 'gone' as const, reason: 'no codex process under pane %1' }, { state: 'alive' as const }]+ const { coordinator } = patient(async () => answers.shift()!)+ const current = session()+ current.engine = 'codex'+ current.runtimes = [tmux]+ current.processIdentity = null+ current.launch = { state: 'starting' }+ await expect(coordinator.validate(current)).resolves.toEqual({ state: 'alive' })+ expect(waits).toEqual([10])+ })++ it('bounds the wait for a restored engine that never starts', async () => {+ const { coordinator, tmuxBackend } = patient(async () => ({ state: 'gone', reason: 'no engine process' }))+ const current = session()+ current.runtimes = [tmux]+ current.launch = { state: 'starting' }+ await expect(coordinator.acquireLease(current)).resolves.toMatchObject({ state: 'failed' })+ expect(waits).toEqual([10, 20, 30])+ expect(tmuxBackend.validate).toHaveBeenCalledTimes(4)+ })++ it('stops waiting when a restored launch fails', async () => {+ const current = session()+ current.runtimes = [tmux]+ current.launch = { state: 'starting' }+ const { coordinator } = patient(async () => {+ if (waits.length) current.launch = { state: 'failed', error: 'ENGINE_EXITED' }+ return { state: 'gone', reason: 'no engine process' }+ })+ await expect(coordinator.validate(current)).resolves.toMatchObject({ state: 'gone' })+ expect(waits).toEqual([10])+ })+it('gives the last answer once the waits run out, and never waits on a terminal known to be gone', async () => {const unknown = patient(async () => ({ state: 'unknown' as const, reason: 'still failing' }))const current = session()diff --git a/cli/src/lib/terminalBackendCoordinator.ts b/cli/src/lib/terminalBackendCoordinator.tsindex bd92e6588..ceca5dac5 100644--- a/cli/src/lib/terminalBackendCoordinator.ts+++ b/cli/src/lib/terminalBackendCoordinator.ts@@ -93,7 +93,8 @@ export class TerminalBackendCoordinator {* that saw the agent still active, was dropped the same way (e2e/races.e2e.ts, a restart nobody* waited for).* - any `gone` while a lifecycle operation is replacing the engine: between the old engine's exit and- * the new one's start there is no engine in the pane at all.+ * the new one's start there is no engine in the pane at all. Found by QA on a quiet machine:+ * restore has the same gap while its launch is `starting`, before the engine process exists.** The row is read again on every ask: `session` is the registry's own, and a restart records the new* process on it.@@ -102,7 +103,8 @@ export class TerminalBackendCoordinator {const ask = () => backend.validate(runtime, { engine: session.engine, processIdentity: session.processIdentity ?? undefined })let result = await ask()for (const ms of this.unknownRetryMs) {- if (result.state === 'alive' || (result.state === 'gone' && !result.replaced && !this.changing(session.agentId))) return result+ if (result.state === 'alive' || (result.state === 'gone' && !result.replaced+ && session.launch?.state !== 'starting' && !this.changing(session.agentId))) return resultawait this.sleep(ms)result = await ask()}