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
Core boundary, part 11: the core under pressure, and two failures found for Claude Code (#737)
Dee1843a48b5eb9
diff --git a/cli/e2e/core.e2e.ts b/cli/e2e/core.e2e.tsnew file mode 100644index 000000000..1849c93dc--- /dev/null+++ b/cli/e2e/core.e2e.ts@@ -0,0 +1,250 @@+/**+ * The core's own flows under pressure, for Claude Code and Codex, on the real daemon under a throwaway+ * home and a private tmux server: several agents at once, an engine that exits, stopping and restarting+ * in the middle of a turn, requests for agents that are not there, clients that come and go by the+ * dozen, a storm of hooks, and a transcript deleted under its tail.+ */+import { mkdirSync, readdirSync, rmSync } from 'node:fs'+import { join } from 'node:path'+import { afterEach, describe, expect, it, onTestFailed } from 'vitest'+import { LocalClient, type Frame } from './harness/client.js'+import { IsolatedDaemon, until } from './harness/daemon.js'++type Engine = 'claude' | 'codex'+const engines: Engine[] = ['claude', 'codex']++const rows = async (client: LocalClient) =>+ (await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents+const row = async (client: LocalClient, agentId: string) => (await rows(client)).find((agent) => agent.id === agentId)++async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, name: string): Promise<string> {+ const cwd = join(daemon.projectsDir, name)+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)+ expect(created.error, `${name}: ${JSON.stringify(created)}`).toBeUndefined()+ return created.agent.id+}+const bound = (client: LocalClient, agentId: string, ms = 60_000) =>+ until(`${agentId.slice(0, 8)} to bind its conversation`, async () => {+ const agent = await row(client, agentId)+ return agent?.sessionId && agent.status === 'active' ? agent : null+ }, ms, 500)++const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId+async function turn(client: LocalClient, agentId: string, content: string, ms = 45_000): Promise<void> {+ const started = client.next(isTurn('turn_started', agentId), ms, `turn_started (${content})`)+ const ended = client.next(isTurn('turn_ended', agentId), ms, `turn_ended (${content})`)+ client.send('message', { agentId, content })+ await started+ await ended+}++describe('the core under pressure', () => {+ let daemon: IsolatedDaemon | undefined+ afterEach(async () => { await daemon?.close(); daemon = undefined })+ const fresh = async () => {+ const d = await IsolatedDaemon.create()+ daemon = d+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-150).join('\n')}`) })+ await d.start()+ return d+ }++ it('six agents at once, both engines: all bind, run turns together, come back after a restart and stop together', async () => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const plan = [...engines, ...engines, ...engines].map((engine, i) => ({ engine, name: `many-${engine}-${i}` }))+ const ids = await Promise.all(plan.map(({ engine, name }) => create(d, client, engine, name)))+ const first = await Promise.all(ids.map((id) => bound(client, id, 90_000)))+ await Promise.all(ids.map((id, i) => turn(client, id, `together ${i}`, 60_000)))++ await d.restart()+ const again = await LocalClient.connect(d)+ await Promise.all(ids.map((id, i) => until(`${id.slice(0, 8)} back after the restart`, async () => {+ const agent = await row(again, id)+ return agent?.status === 'active' && agent.sessionId === first[i].sessionId ? agent : null+ }, 60_000, 500)))+ await Promise.all(ids.map((id, i) => turn(again, id, `again ${i}`, 60_000)))++ const stopped = await Promise.all(ids.map((id) => again.request('agent_delete', { agentId: id }, 90_000)))+ for (const result of stopped) expect(result.error, JSON.stringify(result)).toBeUndefined()+ await until('every agent to stop', async () => (await rows(again)).filter((agent) => ids.includes(agent.id)).every((agent) => agent.status === 'stopped') || null, 60_000, 500)+ expect(d.coresStarted()).toBe(2)+ again.close()+ client.close()+ })++ it.each(engines)('%s: stopping in the middle of a turn, then resuming: the conversation goes on', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const id = await create(d, client, engine, `stop-mid-turn-${engine}`)+ const before = await bound(client, id)+ const started = client.next(isTurn('turn_started', id), 30_000, 'turn_started')+ client.send('message', { agentId: id, content: '!slow 8000' })+ await started+ const stopped = await client.request('agent_delete', { agentId: id }, 60_000)+ expect(stopped.error, JSON.stringify(stopped)).toBeUndefined()+ await until('the agent to stop', async () => (await row(client, id))?.status === 'stopped' || null, 45_000, 500)+ const resumed = await client.request('agent_resume', { agentId: id }, 90_000)+ expect(resumed.error, JSON.stringify(resumed)).toBeUndefined()+ const after = await bound(client, id)+ expect(after.sessionId).toBe(before.sessionId)+ await turn(client, id, 'after the resume')+ client.close()+ })++ it.each(engines)('%s: restarting in the middle of a turn: one answer, and the agent takes the next message', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const id = await create(d, client, engine, `restart-mid-turn-${engine}`)+ const before = await bound(client, id)+ const started = client.next(isTurn('turn_started', id), 30_000, 'turn_started')+ client.send('message', { agentId: id, content: '!slow 8000' })+ await started+ const restarted = await client.request('agent_restart', { agentId: id }, 90_000)+ expect(restarted.error, JSON.stringify(restarted)).toBeUndefined()+ const after = await bound(client, id)+ expect(after.sessionId).toBe(before.sessionId)+ expect(after.status).toBe('active')+ await turn(client, id, 'after the restart')+ client.close()+ })++ it.each(engines)('%s: an engine that exits takes its agent out of active, and a restart brings it back', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const id = await create(d, client, engine, `exits-${engine}`)+ await bound(client, id)+ client.send('message', { agentId: id, content: '!exit' })+ const gone = await until('the agent to stop counting as active', async () => {+ const agent = await row(client, id)+ return agent && agent.status !== 'active' ? agent : null+ }, 45_000, 500)+ expect(['offline', 'stopped']).toContain(gone.status)+ // A message to it now is not an error anyone has to handle, and nothing breaks.+ client.send('message', { agentId: id, content: 'anyone there?' })+ const restarted = await client.request('agent_restart', { agentId: id }, 90_000)+ const resumed = restarted.error ? await client.request('agent_resume', { agentId: id }, 90_000) : restarted+ expect(resumed.error, `restart: ${JSON.stringify(restarted)} resume: ${JSON.stringify(resumed)}`).toBeUndefined()+ await bound(client, id)+ await turn(client, id, 'back again')+ client.close()+ })++ it('requests for agents that are not there are refused, and nothing else is disturbed', async () => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const ghost = '00000000-0000-4000-8000-000000000000'+ client.send('message', { agentId: ghost, content: 'hello?' })+ client.send('cancel', { agentId: ghost })+ for (const [type, payload] of [+ ['agent_restart', { agentId: ghost }], ['agent_resume', { agentId: ghost }],+ ['agent_restart', {}], ['agent_delete', {}], ['agent_fork', { agentId: ghost }],+ ] as const) {+ const answer = await client.request(type, payload, 30_000)+ expect(answer.error, `${type} ${JSON.stringify(payload)}`).toBeTruthy()+ }+ // Stopping an agent that is not there is a stop that has nothing left to do: idempotent, not an error.+ expect(await client.request('agent_delete', { agentId: ghost }, 30_000)).toBeTruthy()+ expect((await rows(client)).find((agent) => agent.id === ghost)).toBeUndefined()+ const id = await create(d, client, 'claude', 'after-the-ghosts')+ await bound(client, id)+ expect((await client.request('agent_delete', { agentId: id }, 60_000)).error).toBeUndefined()+ // Stopping it again, and resuming it twice at once, answer rather than race.+ const twice = await client.request('agent_delete', { agentId: id }, 60_000)+ expect(twice).toBeTruthy()+ const [a, b] = await Promise.all([client.request('agent_resume', { agentId: id }, 90_000), client.request('agent_resume', { agentId: id }, 90_000)])+ expect([a.error, b.error].filter((error) => !error).length).toBeGreaterThanOrEqual(1)+ await bound(client, id)+ await turn(client, id, 'resumed once')+ expect(d.coresStarted()).toBe(1)+ client.close()+ })++ it('fifty clients coming and going while an agent runs turns: every turn reaches the client that stayed', async () => {+ const d = await fresh()+ const steady = await LocalClient.connect(d)+ const id = await create(d, steady, 'codex', 'client-churn')+ await bound(steady, id)+ const rssBefore = await d.rssMiB()+ let churning = true+ const churn = (async () => {+ let n = 0+ while (churning && n < 50) {+ const batch = await Promise.all(Array.from({ length: 5 }, () => LocalClient.connect(d)))+ await Promise.all(batch.map((c) => c.request('agents_list', {}, 15_000)))+ for (const c of batch) c.close()+ n += batch.length+ }+ return n+ })()+ for (let i = 0; i < 3; i++) await turn(steady, id, `while clients churn ${i}`)+ churning = false+ expect(await churn).toBeGreaterThan(0)+ const rssAfter = await d.rssMiB()+ expect(rssAfter - rssBefore, `rss ${rssBefore} → ${rssAfter} MiB`).toBeLessThan(200)+ expect((await steady.request('agents_list', {})).agents.length).toBe(1)+ steady.close()+ })++ it('a storm of hooks: every one is answered, and the daemon answers clients through it', async () => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const token = d.hookCredential()+ const post = (i: number) => fetch(`http://127.0.0.1:${d.port}/api/hook/session-start`, {+ method: 'POST',+ headers: { 'content-type': 'application/json', 'x-harness-hook-token': token },+ body: JSON.stringify({ engine: i % 2 ? 'claude' : 'codex', sessionId: `storm-${i}`, tmuxPane: `%${1000 + i}`, cwd: '/nowhere', callerPid: 1 }),+ }).then((response) => response.status).catch(() => 0)+ const storm = Promise.all(Array.from({ length: 300 }, (_, i) => post(i)))+ const startedAt = Date.now()+ expect(Array.isArray((await client.request('agents_list', {}, 15_000)).agents)).toBe(true)+ expect(Date.now() - startedAt).toBeLessThan(15_000)+ const statuses = await storm+ expect(statuses.filter((status) => status === 0), 'a hook left unanswered').toEqual([])+ expect(statuses.filter((status) => status >= 500), 'a hook that failed').toEqual([])+ const id = await create(d, client, 'claude', 'after-the-storm')+ await bound(client, id)+ await turn(client, id, 'after the storm')+ client.close()+ })++ it.each(engines)('%s: a transcript deleted under its tail: the agent goes on, and its turns are seen again', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const id = await create(d, client, engine, `deleted-transcript-${engine}`)+ const agent = await bound(client, id)+ await turn(client, id, 'first')+ const transcript: string | undefined = agent.transcriptPath ?? (await row(client, id))?.transcriptPath+ const files = transcript ? [transcript] : findTranscripts(d)+ expect(files.length, 'a transcript to delete').toBeGreaterThan(0)+ for (const file of files) rmSync(file, { force: true })+ // The engine writes its next turn into a new file at the same path. A file that shrank under the+ // tail is read as a rewrite of history (watcher.ts: one agent was once credited with 42 turns in a+ // second by a rewrite replayed as live), so what the new file holds when the tail first sees it is+ // history — and everything written after that is live again.+ client.send('message', { agentId: id, content: 'into a new file' })+ await until('the engine to write the new file', () => files.some((file) => { try { return readdirSync(join(file, '..')).includes(file.split('/').pop()!) } catch { return false } }) || null, 30_000, 250)+ await new Promise((resolve) => setTimeout(resolve, 1_500))+ await turn(client, id, 'seen live again', 60_000)+ expect((await row(client, id))?.status).toBe('active')+ expect(d.coresStarted()).toBe(1)+ client.close()+ })+})++function findTranscripts(daemon: IsolatedDaemon): string[] {+ const found: string[] = []+ const walk = (dir: string): void => {+ let entries: ReturnType<typeof readdirSync>+ try { entries = readdirSync(dir, { withFileTypes: true }) } catch { return }+ for (const entry of entries) {+ const path = join(dir, entry.name)+ if (entry.isDirectory()) walk(path)+ else if (entry.name.endsWith('.jsonl')) found.push(path)+ }+ }+ walk(join(daemon.root, 'claude'))+ walk(join(daemon.root, 'codex'))+ return found+}diff --git a/cli/src/core/agents/bind.spec.ts b/cli/src/core/agents/bind.spec.tsindex bdc874d92..45d86423b 100644--- a/cli/src/core/agents/bind.spec.ts+++ b/cli/src/core/agents/bind.spec.ts@@ -62,7 +62,7 @@ function setup(over: Partial<BindDeps> = {}) {clients: { send: vi.fn() },attachSession: vi.fn(async () => true),announceSession: vi.fn(),- stoppedAgents: { save: vi.fn(), finishResume: vi.fn() },+ stoppedAgents: { save: vi.fn(), finishResume: vi.fn(), get: vi.fn(() => null) },syncRecapPool: vi.fn(),teams: { forget: vi.fn() },input: { forget: vi.fn() },@@ -167,6 +167,25 @@ describe('binding a registered session to its agent', () => {await run.binding.handleRegistered(agent({ transcriptPath: join(dir, 'missing.jsonl'), registeredAt: Date.now() - 60_000 }), meta())expect(vi.mocked(run.deps.attachSession).mock.calls.map((call) => call[3])).toEqual([true, false])})++ it('never replays the conversation a resume put back, and judges a new session in that agent like any other', async () => {+ const run = setup()+ const dir = mkdtempSync(join(tmpdir(), 'core-bind-'))+ dirs.push(dir)+ const born = join(dir, 'session.jsonl')+ writeFileSync(born, '{}\n')+ // A resume keeps the row's original registeredAt, so the transcript reads as born after its agent.+ const resumed = agent({ transcriptPath: born, registeredAt: Date.now() - 60_000, resumeOnly: true } as Partial<RegisteredSession>)+ vi.mocked(run.deps.stoppedAgents.get).mockReturnValue(agent({ sessionId: 's1' }))+ await run.binding.handleRegistered(resumed, meta())+ expect(run.deps.stoppedAgents.get).toHaveBeenCalledWith('a1')+ // `/clear` after the resume: a session the stopped record does not hold.+ await run.binding.handleRegistered({ ...resumed, sessionId: 's2' }, meta())+ // A resumed row with no stopped record left to compare against.+ vi.mocked(run.deps.stoppedAgents.get).mockReturnValue(null)+ await run.binding.handleRegistered(resumed, meta())+ expect(vi.mocked(run.deps.attachSession).mock.calls.map((call) => call[3])).toEqual([false, true, true])+ })})describe('binding a running process to its session', () => {diff --git a/cli/src/core/agents/bind.ts b/cli/src/core/agents/bind.tsindex 10e25a916..332de9043 100644--- a/cli/src/core/agents/bind.ts+++ b/cli/src/core/agents/bind.ts@@ -59,7 +59,7 @@ export interface BindDeps {clients: { send(frame: { type: string; payload: Record<string, unknown> }): void }attachSession: (session: RegisteredSession, reset?: boolean, replayCursorFromStart?: boolean, replayFromStart?: boolean) => Promise<boolean>announceSession: (session: RegisteredSession) => void- stoppedAgents: Pick<StoppedAgentStore, 'save' | 'finishResume'>+ stoppedAgents: Pick<StoppedAgentStore, 'save' | 'finishResume' | 'get'>syncRecapPool: () => voidteams: Pick<SwarmPromptScopes, 'forget'>input: Pick<SessionInputController, 'forget'>@@ -142,7 +142,15 @@ export function createBinding({//// And only while the file is new (`transcriptIsFirstTurn`): a long session's transcript was born after// its agent too, and after a daemon restart its next SessionStart replayed the whole history live.- const bornAfterAgent = transcriptIsFirstTurn(+ //+ // And never for the conversation a resume put back: its transcript existed before the row did,+ // whatever its birth says against the row's ORIGINAL `registeredAt`, which a resume keeps. Replaying+ // it sent the whole first turn out again as live, and a turn the stopped process had left open was+ // announced as running with nothing left to end it (found end to end: stop in the middle of a first+ // turn, resume, and the agent read as working for good). A new session started in the agent after+ // the resume is not this conversation, and is judged like any other.+ const resumedConversation = !!entry.resumeOnly && stoppedAgents.get(entry.agentId)?.sessionId === entry.sessionId+ const bornAfterAgent = !resumedConversation && transcriptIsFirstTurn(entry,entry.transcriptPath ? await statBirthMs(entry.transcriptPath) : 0,{ rebound: !!meta.rebound, now: Date.now() },diff --git a/cli/src/core/terminals/control.ts b/cli/src/core/terminals/control.tsindex 02acbbce8..2a4c62647 100644--- a/cli/src/core/terminals/control.ts+++ b/cli/src/core/terminals/control.ts@@ -9,7 +9,7 @@*/import type { RegisteredSession } from '../../lib/registry.js'import type { TerminalBackendCoordinator } from '../../lib/terminalBackendCoordinator.js'-import { terminalActionNotStarted, type TerminalActionResult } from '../../lib/terminalTypes.js'+import { TERMINAL_LEASE_REFUSED, terminalActionNotStarted, type TerminalActionResult } from '../../lib/terminalTypes.js'export type TerminalControlBackend = Pick<TerminalBackendCoordinator,'acquireLease' | 'validateLease' | 'capture' | 'captureLease' | 'submitText' | 'submitTextLease'@@ -85,7 +85,7 @@ export function createTerminalControl({ resolve, terminals }: TerminalControlDepconst session = terminalSession(target)if (!session) return terminalActionNotStarted('terminal agent is unavailable')const lease = await leasedTerminal(session)- if (!lease) return terminalActionNotStarted('terminal control lease is unavailable or changed')+ if (!lease) return terminalActionNotStarted(TERMINAL_LEASE_REFUSED)const result = pinnedControls.has(session.agentId)? await terminals.submitTextLease(lease.value, text): await terminals.submitTextForLease(session, lease.value, text)@@ -108,7 +108,7 @@ export function createTerminalControl({ resolve, terminals }: TerminalControlDepconst session = terminalSession(target)if (!session) return terminalActionNotStarted('terminal session is unavailable')const lease = await leasedTerminal(session)- if (!lease) return terminalActionNotStarted('terminal control lease is unavailable or changed')+ if (!lease) return terminalActionNotStarted(TERMINAL_LEASE_REFUSED)const result = await terminals.sendLegacyKeyLease(lease.value, key)if (result.state !== 'succeeded' && pinnedControls.has(session.agentId)) invalidateTerminalControl(session.agentId)return resultdiff --git a/cli/src/lib/sessionInput.spec.ts b/cli/src/lib/sessionInput.spec.tsindex fc8f439d4..181d9ca4a 100644--- a/cli/src/lib/sessionInput.spec.ts+++ b/cli/src/lib/sessionInput.spec.ts@@ -1,7 +1,7 @@import { afterEach, describe, expect, it, vi } from 'vitest'import { SessionInputController } from './sessionInput.js'import type { RegisteredSession } from './registry.js'-import type { TerminalActionResult } from './terminalTypes.js'+import { TERMINAL_LEASE_REFUSED, type TerminalActionResult } from './terminalTypes.js'function session(engine: 'claude' | 'codex' | 'cursor' | 'commandcode' = 'codex'): RegisteredSession {return {@@ -17,6 +17,82 @@ function session(engine: 'claude' | 'codex' | 'cursor' | 'commandcode' = 'codex'describe('SessionInputController', () => {afterEach(() => vi.useRealTimers())+ describe('a paste the control lease refuses before a byte is written', () => {+ const refused: TerminalActionResult = { state: 'failed', dispatch: 'not_started', reason: TERMINAL_LEASE_REFUSED }+ const done: TerminalActionResult = { state: 'succeeded', dispatch: 'executed' }+ /** A clock that moves only when the controller sleeps. */+ const clock = () => {+ let at = 0+ return { now: () => at, sleep: vi.fn(async (ms: number) => { at += ms }) }+ }++ it('waits for the lease — a resume whose new process is not confirmed yet — and then delivers, once', async () => {+ const time = clock(), onError = vi.fn(), onSubmitted = vi.fn()+ const inject = vi.fn<() => Promise<TerminalActionResult>>().mockResolvedValueOnce(refused).mockResolvedValueOnce(refused).mockResolvedValue(done)+ const controller = new SessionInputController({ getSession: () => session('claude'), validateRuntime: async () => true,+ inject, sendKey: async () => true, onError, onSubmitted, ...time })+ controller.submit('s1', 'right after the resume')+ await vi.waitFor(() => expect(onSubmitted).toHaveBeenCalledWith('s1', 'right after the resume'))+ expect(inject).toHaveBeenCalledTimes(3)+ expect(time.sleep).toHaveBeenCalledWith(250)+ expect(onError).not.toHaveBeenCalled()+ controller.forget('s1')+ })++ it('gives up at fifteen seconds, says the message was not delivered, and wrote nothing', async () => {+ const time = clock(), onError = vi.fn()+ const inject = vi.fn(async () => refused)+ const controller = new SessionInputController({ getSession: () => session('claude'), validateRuntime: async () => true,+ inject, sendKey: async () => true, onError, ...time })+ controller.submit('s1', 'never leased')+ await vi.waitFor(() => expect(onError).toHaveBeenCalledWith('s1', 'The message could not be delivered to the agent.'))+ expect(time.now()).toBeGreaterThanOrEqual(15_000)+ expect(inject.mock.calls.length).toBe(61)+ controller.forget('s1')+ })++ it('stops waiting when the agent goes, and never waits on any other refusal', async () => {+ const time = clock(), onError = vi.fn()+ let present = true+ const inject = vi.fn(async () => { present = false; return refused })+ const gone = new SessionInputController({ getSession: () => (present ? session('claude') : undefined), validateRuntime: async () => true,+ inject, sendKey: async () => true, onError, ...time })+ gone.submit('s1', 'to an agent that left')+ await vi.waitFor(() => expect(onError).toHaveBeenCalled())+ expect(inject).toHaveBeenCalledTimes(1)++ const other = vi.fn(async (): Promise<TerminalActionResult> => ({ state: 'failed', dispatch: 'not_started', reason: 'terminal agent is unavailable' }))+ const elsewhere = new SessionInputController({ getSession: () => session('claude'), validateRuntime: async () => true,+ inject: other, sendKey: async () => true, onError: vi.fn(), ...time })+ elsewhere.submit('s1', 'to an agent with no terminal')+ await vi.waitFor(() => expect(other).toHaveBeenCalledTimes(1))+ expect(time.sleep).toHaveBeenCalledTimes(1)+ elsewhere.forget('s1')+ })++ it('waits the same way for a message that wants a receipt, until it is cancelled', async () => {+ const time = clock(), onDelivery = vi.fn()+ const inject = vi.fn<() => Promise<TerminalActionResult>>().mockResolvedValueOnce(refused).mockResolvedValue(done)+ const controller = new SessionInputController({ getSession: () => session('codex'), validateRuntime: async () => true,+ inject, sendKey: async () => true, onError: vi.fn(), onDelivery, ...time })+ controller.submit('s1', 'with a receipt', 'delivery-1')+ await vi.waitFor(() => expect(inject).toHaveBeenCalledTimes(2))+ controller.forget('s1')++ const cancelled = vi.fn(async () => refused)+ let cancel: () => void = () => {}+ const waiting = new SessionInputController({ getSession: () => session('codex'), validateRuntime: async () => true,+ inject: cancelled, sendKey: async () => true, onError: vi.fn(), onDelivery,+ now: time.now, sleep: async (ms) => { time.sleep(ms); cancel() } })+ cancel = () => waiting.forget('s1')+ waiting.submit('s1', 'cancelled while waiting', 'delivery-2')+ await vi.waitFor(() => expect(cancelled).toHaveBeenCalledTimes(1))+ await new Promise((resolve) => setTimeout(resolve, 10))+ expect(cancelled).toHaveBeenCalledTimes(1)+ })+ })++it('retains the submitted swarm through the input queue and records it only at dispatch', async () => {const beforeSubmit = vi.fn(() => vi.fn())const controller = new SessionInputController({ getSession: () => session('cursor'), validateRuntime: async () => true,diff --git a/cli/src/lib/sessionInput.ts b/cli/src/lib/sessionInput.tsindex a0bb2dbda..23a967c31 100644--- a/cli/src/lib/sessionInput.ts+++ b/cli/src/lib/sessionInput.ts@@ -1,7 +1,7 @@import { createHash } from 'crypto'import type { RegisteredSession } from './registry.js'import { sid } from './log.js'-import type { TerminalActionResult } from './terminalTypes.js'+import { TERMINAL_LEASE_REFUSED, type TerminalActionResult } from './terminalTypes.js'const MAX_QUEUE_ITEMS = 8const MAX_QUEUE_BYTES = 24 * 1024@@ -50,6 +50,10 @@ const AGY_SUBMIT_VERIFY_MS = 8_000const COPILOT_SUBMIT_VERIFY_MS = 6_000const CURSOR_TURN_SETTLE_MS = 750const SUBMIT_MAX_RETRIES = 2+/** How long a paste the control lease refused, before a byte was written, waits for the lease, and how+ * often it asks again. */+const LEASE_WAIT_MS = 15_000+const LEASE_RETRY_MS = 250// Re-observe briefly while a submitted prompt awaits a transcript event. Reaching this limit is not// evidence of rejection: Claude can hold an accepted follow-up while background agents finish.const SUBMIT_MAX_OBSERVES = 5@@ -118,6 +122,15 @@ export interface SessionInputDeps {* moment at which a turn is known to have started, and this one is exact: we sent it.*/onSubmitted?: (sessionId: string, content: string) => void+ /** Clock and timer for the lease wait; real ones by default. */+ now?: () => number+ sleep?: (ms: number) => Promise<void>+}++/** A write the pane's control lease refused before a byte of it was written. */+function leaseRefused(delivery: boolean | TerminalActionResult): boolean {+ return typeof delivery !== 'boolean' && delivery.state === 'failed' && delivery.dispatch === 'not_started'+ && delivery.reason === TERMINAL_LEASE_REFUSED}function fingerprint(content: string): string {@@ -135,6 +148,30 @@ export class SessionInputController {return this.deps.getSession(id)}+ /**+ * A paste the pane's control lease refused before a byte was written: the agent is there, but its+ * process is not yet the one the registry holds — a resume or restart moments ago, before the new+ * process was confirmed — or another writer holds the pane. Wait for the lease, up to+ * LEASE_WAIT_MS, rather than fail the message; nothing was written, so asking again can never type+ * it twice. Found end to end: a message sent as a resume was confirmed was refused 140 ms later and+ * lost, and a real engine takes seconds to confirm.+ */+ private async injectWhenLeased(+ write: () => Promise<boolean | TerminalActionResult>,+ current: () => boolean,+ ): Promise<boolean | TerminalActionResult> {+ const now = this.deps.now ?? Date.now+ const sleep = this.deps.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)))+ const deadline = now() + LEASE_WAIT_MS+ let delivery = await write()+ while (leaseRefused(delivery) && now() < deadline) {+ await sleep(LEASE_RETRY_MS)+ if (!current()) break+ delivery = await write()+ }+ return delivery+ }+private state(sessionId: string): InputState {let value = this.states.get(sessionId)if (!value) {@@ -396,7 +433,8 @@ export class SessionInputController {await this.deps.sendKey(session.agentId, 'C-u')}const forgetScope = this.deps.beforeSubmit?.(session.agentId, content, tabId)- const delivery = await this.deps.inject(session.agentId, content)+ const delivery = await this.injectWhenLeased(() => this.deps.inject(session.agentId, content),+ () => this.controlSession(session.agentId) !== undefined)const accepted = typeof delivery === 'boolean'? delivery: delivery.state === 'succeeded' || delivery.dispatch === 'possibly_executed'@@ -461,8 +499,9 @@ export class SessionInputController {if (state.cancelled || this.states.get(sessionId) !== state) returnstate.writing = trueconst forgetScope = this.deps.beforeSubmit?.(session.agentId, content, tabId, deliveryId)- const delivery = await (deliveryId.startsWith('team:') && this.deps.injectTeam- ? this.deps.injectTeam(session.agentId, content, deliveryId) : this.deps.inject(session.agentId, content))+ const delivery = await this.injectWhenLeased(() => (deliveryId.startsWith('team:') && this.deps.injectTeam+ ? this.deps.injectTeam(session.agentId, content, deliveryId) : this.deps.inject(session.agentId, content)),+ () => !state.cancelled && this.states.get(sessionId) === state)state.writing = falseif (delivery === false || (typeof delivery !== 'boolean' && delivery.dispatch === 'not_started')) forgetScope?.()if (state.cancelled || this.states.get(sessionId) !== state) returndiff --git a/cli/src/lib/terminalTypes.ts b/cli/src/lib/terminalTypes.tsindex 7cb1b66de..fe7a11ea1 100644--- a/cli/src/lib/terminalTypes.ts+++ b/cli/src/lib/terminalTypes.ts@@ -180,6 +180,10 @@ export const TERMINAL_ACTION_SUCCEEDED: TerminalActionResult = {dispatch: 'executed',}+/** The reason a write is refused when the pane's control lease cannot be taken: the agent's process is+ * not the one the registry holds (just relaunched, not yet confirmed) or another writer holds it. */+export const TERMINAL_LEASE_REFUSED = 'terminal control lease is unavailable or changed'+export function terminalActionNotStarted(reason: string): {state: 'failed'; dispatch: 'not_started'; reason: string} {diff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.mdindex fd2c0bab3..16bf61b60 100644--- a/docs/design/2026-10-03-harnessd.md+++ b/docs/design/2026-10-03-harnessd.md@@ -522,6 +522,17 @@ Found while mapping, and fixed on their own rather than inside a move:- The hook server answered nothing when a handler threw outside its proxy routes, which held anengine's turn until its hook timed out. Every request is answered now.- One throwing teams or device consumer cost an event its heartbeat, question watcher and recaps.+- Found by the second round (`e2e/core.e2e.ts`: six agents of both engines at once, stop and restart+ mid-turn, an engine that exits, missing agents, client churn, a hook storm, a deleted transcript),+ and fixed (part 11):+ - A resumed conversation was replayed live as its agent's first turn for ten minutes after the agent+ was created, so a Claude Code agent stopped in the middle of its first turn and resumed read as+ working for good. A resumed conversation is history now.+ - A message sent as a resume or restart was confirmed was refused by the pane's control lease and+ lost. A write the lease refuses before a byte is written now waits for it, up to fifteen seconds.+ - Kept as it is, on purpose: a transcript deleted and recreated at the same path is read as a+ rewrite, so the first burst in the new file is history and every turn after it is live. Reading+ a shrunken file as new content once replayed a rewrite as fresh turns (42 in one second).## Testing