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 Autonomous Device follow-up input delivery (#294)
Leo458e28ccc304
diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 5376fb9c0..39a7169f5 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -161,7 +161,7 @@ import { createWindowRouter } from './cable/windowRoute.js'import { RemoteRelayPool } from './lib/remoteRelay.js'import { TERMINAL_BINARY_VERSION } from './lib/terminalBinary.js'import { foldTranscript, lastTurnTextFromRawLines, lineToEvents, newTurnState, type LiveEvent, type TurnState } from './lib/normalize.js'-import { AskQuestionController, pollsQuestions, QuestionWatcher } from './lib/askQuestion.js'+import { AskQuestionController, parseEngineQuestionPane, pollsQuestions, QuestionWatcher } from './lib/askQuestion.js'import { CommanderMirror, SUBAGENT_IDLE_MS, type CommanderMirrorOpts } from './lib/commander.js'import {setSummaryPoolDeviceConnected,@@ -223,6 +223,7 @@ import { probeGridAssignment, sameGridAssignment } from './lib/gridAssignment.jsimport { agentFrame, type AgentFrame } from './lib/agentFrame.js'import { agentTokenUsage } from './lib/agentTokenUsage.js'import { SessionInputController } from './lib/sessionInput.js'+import { AutonomousDeviceInput, isDeviceInputBoundary } from './lib/autonomous-device/input.js'import { adaptSlashCommand } from './lib/goalCommand.js'import { RuntimeProfileManager, parseRuntimeProfile } from './lib/runtimeProfile.js'import { RuntimeProfileController, inspectRuntimePane } from './lib/runtimeProfileController.js'@@ -2315,15 +2316,15 @@ async function runForeground(session: AuthSession | null): Promise<void> {replayFromStart = false,): Promise<boolean> =>attaches.attach(session, reset, () => attachSessionNow(session, reset, replayCursorFromStart, replayFromStart))- const input = new SessionInputController({+ const input: SessionInputController = new SessionInputController({getSession: (id) => registry.resolve(id),onDelivery: (event) => {autonomousDeviceService?.delivery(event)backend.orchestratorDelivery(event)},validateRuntime: validateTerminal,- inject: submitTerminalAction,- sendKey: keyTerminalAction,+ inject: (id, text) => deviceInput.legacyWrite(id, () => submitTerminalAction(id, text)),+ sendKey: (id, key) => deviceInput.legacyWrite(id, () => keyTerminalAction(id, key)),capture: captureTerminal,onError: (sessionId, message) => {backend.send({ type: 'error', agentId: agentIdFor(sessionId), dbSessionId: sessionId, payload: { message } })@@ -2344,6 +2345,24 @@ async function runForeground(session: AuthSession | null): Promise<void> {emitSessionEvents(session.sessionId, normalizer.openTurn(content))},})+ const deviceInput: AutonomousDeviceInput = new AutonomousDeviceInput({+ getSession: id => registry.resolve(id),+ validateRuntime: validateTerminal,+ inject: submitTerminalAction,+ sendKey: keyTerminalAction,+ capture: captureTerminal,+ isAwaitingUser: async session => {+ const pane = await captureTerminal(session.agentId)+ return pane === null || parseEngineQuestionPane(session.engine, pane) !== null+ },+ acquireControl: id => input.acquireControl(id, { forAnswer: true }),+ legacySubmit: (id, text, deliveryId) => input.submit(id, text, deliveryId),+ legacyCancel: id => input.cancelDelivery(id),+ onDelivery: event => autonomousDeviceService?.delivery(event),+ onInputStatus: event => autonomousDeviceService?.inputStatus(event),+ onForget: id => autonomousDeviceService?.agentGone(id),+ })+/*** agy only: close a turn whose final `Stop` never came.*@@ -2441,6 +2460,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {hasDevice: () => someoneCanAnswer(),isDriving: (sessionId) => questions.isDriving(sessionId),onQuestion: (sessionId, requestId, shaped) => {+ deviceInput.setUserAction(agentIdFor(sessionId), true)questions.remember(requestId, sessionId)showAwaitingAnswer(sessionId)const asked = {@@ -2468,6 +2488,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {// waiting, down the SAME path the question itself took, so the dial and the WiFi device cannot// disagree about whether a question is still open.onQuestionGone: (sessionId, requestId) => {+ deviceInput.setUserAction(agentIdFor(sessionId), false)const closed = {type: 'commander_question_close',agentId: agentIdFor(sessionId),@@ -2655,7 +2676,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {if (!events.length || !registry.bySession(sessionId)?.active) returnconst usageSession = registry.bySession(sessionId)if (usageSession?.engine === 'opencode') agentTokenUsage.changed(usageSession)- for (const event of events) {+ for (const [eventIndex, event] of events.entries()) {const agentId = agentIdFor(sessionId)const frame = correlateAgentEvent(event, sessionId, agentId)// A `turn_started` that is not a turn starting NOW — a turn picked back up at attach, or a prompt@@ -2669,6 +2690,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {turnStartedAt.set(sessionId, Date.now())console.log(`[turn] ${sid(sessionId)} started · engine=${registry.bySession(sessionId)?.engine ?? 'claude'} · bytes=${Buffer.byteLength(event.payload.userMessage, 'utf8')}`)input.onTurnStarted(agentIdFor(sessionId), event.payload.userMessage)+ deviceInput.onTurnStarted(agentId, event.payload.userMessage)autonomousDeviceService?.turnStarted(agentId)startHeartbeat(sessionId)questionWatcher.start(sessionId) // Claude opens its dialog INSIDE a turn@@ -2685,7 +2707,11 @@ async function runForeground(session: AuthSession | null): Promise<void> {`[turn] ${sid(sessionId)} ended${event.payload.aborted ? ' · aborted (interrupted)' : ''}` +`${startedAt ? ` · ${Date.now() - startedAt}ms` : ''}`,)- autonomousDeviceService?.turnEnded(agentId, event.payload.aborted === true)+ // Filter only the Device receipt view; shared normalizers, mirror, and local input stay unchanged.+ if (!isDeviceInputBoundary(usageSession?.engine ?? '', events, eventIndex)) {+ autonomousDeviceService?.turnEnded(agentId, event.payload.aborted === true)+ deviceInput.onTurnEnded(agentId)+ }input.onTurnEnded(agentIdFor(sessionId))// Command Code asks AFTER the turn: `ask_user_question` ends the turn (its Stop hook fires), the// dialog goes up, and the answer opens a NEW turn. Stopping the watcher here is what left the@@ -2776,6 +2802,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {void watcher.removeSession(sessionId)stopHeartbeat(sessionId)input.forget(doomed?.agentId ?? sessionId)+ deviceInput.forget(doomed?.agentId ?? sessionId)if (!opts.keepAgent) detachDsh(announceId)mirror.forget(sessionId) // aborts any in-flight recap + clears busy; KEEPS the persisted summaryif (opts.keepAgent) return@@ -2792,7 +2819,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {// A terminal is never the dial's business, and `syncSession` forces that for it anyway.announceSession: session => announceSession(session, { device: false }),invalidateTerminalControl,- forgetInput: agentId => input.forget(agentId),+ forgetInput: agentId => { input.forget(agentId); deviceInput.forget(agentId) },detachDsh,syncRecapPool,warn: (message, error) => console.warn(message, error),@@ -3033,6 +3060,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {if (!result || !result.isNew) returnif (previousOwner && previousOwner.agentId !== result.entry.agentId) {input.forget(previousOwner.agentId)+ deviceInput.forget(previousOwner.agentId)announceSession(previousOwner)}lastRepairAttempt.delete(agent.agentId)@@ -3178,6 +3206,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {if (!agent.active) returninvalidateTerminalControl(agent.agentId)input.forget(agent.agentId)+ deviceInput.forget(agent.agentId)if (agent.sessionId) {questionWatcher.stop(agent.sessionId)stopHeartbeat(agent.sessionId)@@ -5663,8 +5692,11 @@ async function runForeground(session: AuthSession | null): Promise<void> {runtime: evidence.get(s.agentId)?.runtime ?? 'unavailable',state: turnStartedAt.has(s.sessionId) ? 'running' : 'idle' }))},- submit: submitAgent,- cancelDelivery: id => input.cancelDelivery(id),+ submit: (id, text, deliveryId) => {+ const session = registry.resolve(id)+ deviceInput.submit(session?.agentId ?? id, adaptSlashCommand(text, session?.engine ?? 'claude'), deliveryId)+ },+ cancelDelivery: id => deviceInput.cancelDelivery(id),stop: id => cancelAgent(id, true),answer: (agentId, requestId, answers) => questions.answer({ agentId, requestId, answers, allowPermissions: false }),recent: (id, n) => mirror.recent(registry.byAgent(id)?.sessionId ?? id, n),diff --git a/cli/src/lib/autonomous-device/input.spec.ts b/cli/src/lib/autonomous-device/input.spec.tsnew file mode 100644index 000000000..35c188fc9--- /dev/null+++ b/cli/src/lib/autonomous-device/input.spec.ts@@ -0,0 +1,370 @@+import { CodexNormalizer } from '../../engines/codex/normalizer.js'+import { lineToEvents, newTurnState, type LiveEvent } from '../normalize.js'+import { randomUUID } from 'node:crypto'+import { afterEach, describe, expect, it, vi } from 'vitest'+import { AutonomousDeviceInput, isDeviceInputBoundary, type DeviceInputDeps } from './input.js'+import { SessionInputController } from '../sessionInput.js'+import { AutonomousDeviceService } from './service.js'+import type { RegisteredSession } from '../registry.js'+import type { TerminalActionResult } from '../terminalTypes.js'++function makeDevice(overrides: Partial<DeviceInputDeps>): AutonomousDeviceInput {+ return new AutonomousDeviceInput({+ getSession: () => ({ agentId: 'agent', engine: 'claude' }) as RegisteredSession,+ validateRuntime: async () => true, inject: async () => true, sendKey: async () => true,+ capture: async () => '› ', acquireControl: () => () => {}, legacySubmit: vi.fn(), legacyCancel: () => false,+ onDelivery: vi.fn(), onInputStatus: vi.fn(), ...overrides,+ })+}++function fixture(engine: 'claude' | 'codex' | 'commandcode' = 'codex') {+ let available = true+ const session = { agentId: 'agent', engine, cliVersion: '0.106.0' } as RegisteredSession+ const inject = vi.fn(async (_target: string, _text: string): Promise<TerminalActionResult> => ({ state: 'succeeded', dispatch: 'executed' }))+ const capture = vi.fn(async (): Promise<string | null> => '› \nWorking…')+ const sendKey = vi.fn(async (_id: string, _key: string) => true)+ const events: Record<string, unknown>[] = []+ const service = new AutonomousDeviceService({+ machineId: 'machine', agents: () => available ? [{ agentId: 'agent', engine, name: 'Agent', state: 'working' }] : [],+ submit: (id, text, delivery) => controller.submit(id, text, delivery), cancelDelivery: id => controller.cancelDelivery(id),+ stop: async () => true, answer: async () => true, recent: () => [], emit: frame => events.push(frame),+ })+ const legacy: SessionInputController = new SessionInputController({+ getSession: () => available ? session : undefined, validateRuntime: async () => available,+ inject: (id, text) => controller.legacyWrite(id, () => inject(id, text)),+ sendKey: (id, key) => controller.legacyWrite(id, () => sendKey(id, key)), capture, onError: vi.fn(),+ onDelivery: event => service.delivery(event),+ })+ const controller: AutonomousDeviceInput = new AutonomousDeviceInput({+ acquireControl: id => legacy.acquireControl(id, { forAnswer: true }),+ legacySubmit: (id, text, delivery) => legacy.submit(id, text, delivery),+ legacyCancel: id => legacy.cancelDelivery(id),+ onForget: id => service.agentGone(id),+ getSession: () => available ? session : undefined, validateRuntime: async () => available,+ inject, capture, sendKey, onDelivery: event => service.delivery(event),+ onInputStatus: event => service.inputStatus(event),+ })+ const send = (key: string, text = key, focusRevision?: string) => service.request('device', {+ type: 'turn.send', requestId: randomUUID(), machineId: 'machine', agentId: 'agent', idempotencyKey: key, text,+ ...(focusRevision ? { focusRevision } : {}),+ })+ const start = (text: string) => { legacy.onTurnStarted('agent', text); controller.onTurnStarted('agent', text); service.turnStarted('agent') }+ const end = () => { service.turnEnded('agent'); legacy.onTurnEnded('agent'); controller.onTurnEnded('agent') }+ return { controller, legacy, service, inject, capture, sendKey, send, start, end, events,+ gone: () => { available = false; controller.forget('agent') }, receipt: (key: string) => service.receipt('device', key)! }+}++afterEach(() => vi.useRealTimers())++describe('in-flight input through the Autonomous Device contract', () => {+ it.each(['harness-use', 'Harness-only voice'])('%s delivers B before A ends, preserving retry and focus guards', async caller => {+ vi.useFakeTimers()+ const f = fixture()+ f.service.appFocus('machine', 'agent', 'desktop')+ const revision = caller === 'Harness-only voice' ? f.service.focusSnapshot().focusRevision : undefined+ await f.send('A', 'Create a house', revision)+ await vi.advanceTimersByTimeAsync(0)+ f.start('Create a house')+ await f.send('B', 'Make the roof red', revision)+ await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['Create a house', 'Make the roof red'])+ expect(f.receipt('A').state).toBe('started')+ expect(f.receipt('B')).toMatchObject({ state: 'delivered', input: { mode: 'steering', phase: 'accepted' } })+ // Lost RPC acknowledgment + reconnect: same key returns its own reservation.+ const before = f.receipt('B').deliveryId+ const cursor = f.service.resume().cursor+ const replay: unknown[] = []+ f.service.replay({ serverInstanceId: f.service.serverInstanceId, cursor: 0 }, event => replay.push(event))+ expect(replay.length).toBe(cursor)+ expect(await f.send('B', 'Make the roof red', revision)).toMatchObject({ status: 'duplicate', receipt: { deliveryId: before } })+ expect(f.inject).toHaveBeenCalledTimes(2)+ f.end()+ expect(f.receipt('A').state).toBe('completed')+ expect(f.receipt('B').state).toBe('delivered')+ f.start('Make the roof red')+ f.end()+ expect(f.receipt('B').state).toBe('completed')+ expect(f.receipt('B').turnId).not.toBe(f.receipt('A').turnId)+ f.controller.forget('agent')+ })++ it('serializes rapid Device A/B/C writes, while the agent stays busy', async () => {+ vi.useFakeTimers()+ const f = fixture()+ const finish: Array<() => void> = []+ f.inject.mockImplementation(() => new Promise(resolve => finish.push(() => resolve({ state: 'succeeded', dispatch: 'executed' }))))+ f.start('existing task')+ await f.send('A')+ await f.send('B')+ await f.send('C')+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['A'])+ expect(f.legacy.acquireControl('agent', { forAnswer: true })).toBeNull()+ finish[0]()+ await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['A', 'B'])+ finish[1]()+ await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['A', 'B', 'C'])+ finish[2]()+ await vi.advanceTimersByTimeAsync(0)+ expect(f.sendKey).not.toHaveBeenCalled()+ f.controller.forget('agent')+ })++ it('keeps native-queue receipts independent across the old turn ending', async () => {+ vi.useFakeTimers()+ const f = fixture('claude')+ f.start('existing task')+ await f.send('B')+ await f.send('C')+ await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['B', 'C'])+ expect(f.receipt('B').input).toEqual({ mode: 'native_queue', phase: 'accepted' })+ f.end()+ expect(f.receipt('B').state).toBe('delivered')+ expect(f.receipt('C').state).toBe('delivered')+ f.start('B'); f.end()+ expect(f.receipt('B').state).toBe('completed')+ expect(f.receipt('C').state).toBe('delivered')+ f.start('C'); f.end()+ expect(f.receipt('C').state).toBe('completed')+ f.controller.forget('agent')+ })++ it('does not assign one session done or summary to overlapping steered inputs', async () => {+ vi.useFakeTimers()+ const f = fixture()+ await f.send('A'); await vi.advanceTimersByTimeAsync(0); f.start('A')+ await f.send('B'); await vi.advanceTimersByTimeAsync(0); f.start('B')+ f.end()+ f.service.commander({ type: 'commander_event', agentId: 'agent', payload: { kind: 'summary', text: 'A answer' } })+ expect(f.receipt('A')).toMatchObject({ state: 'unknown', error: { code: 'OVERLAPPING_INPUTS' } })+ expect(f.receipt('B')).toMatchObject({ state: 'unknown', error: { code: 'OVERLAPPING_INPUTS' } })+ expect(f.events.findLast(event => event.kind === 'turn.done')).toMatchObject({ payload: {} })+ expect(await f.send('B')).toMatchObject({ status: 'duplicate' })+ expect(f.inject).toHaveBeenCalledTimes(2)+ f.controller.forget('agent')+ })++ it('does not attribute an untracked running task completion to a steered message', async () => {+ vi.useFakeTimers()+ const f = fixture()+ f.start('local task')+ await f.send('B'); await vi.advanceTimersByTimeAsync(0); f.start('B'); f.end()+ expect(f.receipt('B').state).toBe('unknown')+ f.controller.forget('agent')+ })++ it('never retries a timed-out native write or appends to an unconfirmed composer', async () => {+ vi.useFakeTimers()+ const f = fixture()+ f.inject.mockResolvedValue({ state: 'unknown', dispatch: 'possibly_executed', reason: 'ack lost' })+ f.capture.mockResolvedValue('› A')+ await f.send('A'); await f.send('B')+ await vi.advanceTimersByTimeAsync(30_000)+ expect(f.receipt('A').state).toBe('unknown')+ expect(f.receipt('B').state).toBe('queued')+ expect(f.inject).toHaveBeenCalledTimes(1)+ expect(f.sendKey).not.toHaveBeenCalled()+ expect(await f.send('A')).toMatchObject({ status: 'duplicate' })+ // A later transcript match is evidence; a timer was never permission to resend.+ f.start('A')+ await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['A', 'B'])+ f.controller.forget('agent')+ })++ it('does not treat an unreadable or dialog-only screen as an empty composer', async () => {+ vi.useFakeTimers()+ const f = fixture()+ f.capture.mockResolvedValue('Allow this tool?')+ await f.send('A'); await f.send('B'); await vi.advanceTimersByTimeAsync(30_000)+ expect(f.inject).toHaveBeenCalledTimes(1)+ expect(f.receipt('A').input?.phase).toBe('unconfirmed')+ f.controller.forget('agent')+ })++ it('queues unsupported engines in the daemon, with an explicit disposition', async () => {+ vi.useFakeTimers()+ const f = fixture('commandcode')+ f.start('existing task')+ await f.send('B')+ expect(f.inject).not.toHaveBeenCalled()+ expect(f.receipt('B').input).toEqual({ mode: 'daemon_queue', phase: 'waiting_for_turn' })+ f.end(); await vi.advanceTimersByTimeAsync(0)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['B'])+ f.controller.forget('agent')+ })++ it('holds new prompts for a user action but lets the explicit answer acquire control', async () => {+ vi.useFakeTimers()+ const f = fixture()+ f.start('existing task')+ f.controller.setUserAction('agent', true)+ await f.send('B')+ expect(f.receipt('B').input).toEqual({ mode: 'daemon_queue', phase: 'waiting_for_user' })+ const release = f.legacy.acquireControl('agent', { forAnswer: true })+ expect(release).not.toBeNull()+ f.controller.setUserAction('agent', false)+ expect(f.inject).not.toHaveBeenCalled()+ release!(); await vi.advanceTimersByTimeAsync(1500)+ expect(f.inject.mock.calls.map(call => call[1])).toEqual(['B'])+ f.controller.forget('agent')+ })++ it('marks pasted work unknown and unsent work rejected when the session disappears', async () => {+ vi.useFakeTimers()+ const f = fixture()+ let finish!: () => void+ f.inject.mockImplementation(() => new Promise(resolve => { finish = () => resolve({ state: 'succeeded', dispatch: 'executed' }) }))+ await f.send('A'); await f.send('B')+ f.gone(); finish(); await vi.advanceTimersByTimeAsync(0)+ expect(f.receipt('A')).toMatchObject({ state: 'unknown', error: { code: 'AGENT_GONE' } })+ expect(f.receipt('B')).toMatchObject({ state: 'rejected', error: { code: 'agent_gone' } })+ expect(f.inject).toHaveBeenCalledTimes(1)+ })+})++describe('native input write evidence', () => {+ it('waits for a detected permission dialog to close before touching the composer', async () => {+ vi.useFakeTimers()+ let blocked = true+ const inject = vi.fn(async (_target: string, _text: string) => true)+ const statuses: unknown[] = []+ const controller = makeDevice({+ getSession: () => ({ agentId: 'agent', engine: 'claude' }) as RegisteredSession,+ validateRuntime: async () => true, isAwaitingUser: async () => blocked,+ inject, sendKey: async () => true, onInputStatus: event => statuses.push(event),+ })+ controller.onTurnStarted('agent', 'existing task')+ controller.submit('agent', 'B', 'B')+ await vi.advanceTimersByTimeAsync(0)+ expect(inject).not.toHaveBeenCalled()+ expect(statuses).toContainEqual({ sessionId: 'agent', deliveryId: 'B', mode: 'daemon_queue', phase: 'waiting_for_user' })+ blocked = false+ await vi.advanceTimersByTimeAsync(1500)+ expect(inject).toHaveBeenCalledExactlyOnceWith('agent', 'B')+ controller.forget('agent')+ })++ it('does not downgrade a transcript-confirmed start when the write acknowledgment is lost', async () => {+ vi.useFakeTimers()+ const f = fixture()+ let fail!: () => void+ f.inject.mockImplementation(() => new Promise((_resolve, reject) => { fail = () => reject(new Error('ack lost')) }))+ await f.send('A'); await vi.advanceTimersByTimeAsync(0)+ f.start('A'); fail(); await vi.advanceTimersByTimeAsync(0)+ expect(f.receipt('A').state).toBe('started')+ expect(f.receipt('A').input?.phase).toBe('accepted')+ expect(await f.send('A')).toMatchObject({ status: 'duplicate' })+ expect(f.inject).toHaveBeenCalledTimes(1)+ f.end()+ expect(f.receipt('A').state).toBe('completed')+ f.controller.forget('agent')+ })++ it('marks an observed running delivery unknown when its session disappears', async () => {+ vi.useFakeTimers()+ const f = fixture()+ await f.send('A'); await vi.advanceTimersByTimeAsync(0); f.start('A')+ f.gone()+ expect(f.receipt('A')).toMatchObject({ state: 'unknown', error: { code: 'AGENT_GONE' } })+ f.end()+ expect(f.receipt('A').state).toBe('unknown')+ })++ it('holds the next writer across an evidence-backed Enter retry and never repeats a lost key acknowledgment', async () => {+ vi.useFakeTimers()+ const f = fixture()+ f.capture.mockResolvedValue('› A')+ let resolveKey!: (value: boolean) => void+ f.sendKey.mockImplementation(() => new Promise(resolve => { resolveKey = resolve }))+ await f.send('A'); await f.send('B')+ await vi.advanceTimersByTimeAsync(1500)+ expect(f.sendKey).toHaveBeenCalledExactlyOnceWith('agent', 'Enter')+ expect(f.inject).toHaveBeenCalledTimes(1)+ resolveKey(false) // no proof whether the key took effect+ await vi.advanceTimersByTimeAsync(30_000)+ expect(f.sendKey).toHaveBeenCalledTimes(1)+ expect(f.inject).toHaveBeenCalledTimes(1)+ expect(f.receipt('A').state).toBe('unknown')+ expect(f.receipt('B').state).toBe('queued')+ f.controller.forget('agent')+ })+})++it('preserves A/B order if a dialog appears during the first preflight check', async () => {+ vi.useFakeTimers()+ let release!: (blocked: boolean) => void+ const probe = vi.fn<() => Promise<boolean>>().mockImplementationOnce(() => new Promise(resolve => { release = resolve })).mockResolvedValue(false)+ const inject = vi.fn(async (_target: string, _text: string) => true)+ const controller = makeDevice({+ getSession: () => ({ agentId: 'agent', engine: 'claude' }) as RegisteredSession,+ validateRuntime: async () => true, isAwaitingUser: probe, inject, sendKey: async () => true,+ })+ controller.submit('agent', 'A', 'A')+ await vi.advanceTimersByTimeAsync(0)+ controller.submit('agent', 'B', 'B')+ release(true)+ await vi.advanceTimersByTimeAsync(1500)+ expect(inject.mock.calls.map(call => call[1])).toEqual(['A', 'B'])+ controller.forget('agent')+})+++it.each(['codex', 'claude'] as const)('%s Device filter rejects an inferred completion without changing normalizer output', async engine => {+ vi.useFakeTimers()+ const f = fixture(engine)+ const codex = new CodexNormalizer('live')+ const claude = newTurnState()+ const normalizeUser = (text: string) => engine === 'codex'+ ? codex.ingest(JSON.stringify({ type: 'event_msg', payload: { type: 'user_message', message: text } }))+ : lineToEvents(JSON.stringify({ type: 'user', message: { role: 'user', content: [{ type: 'text', text }] } }), claude)+ const observe = (events: LiveEvent[]) => {+ for (const [index, event] of events.entries()) {+ if (isDeviceInputBoundary(engine, events, index)) continue+ if (event.type === 'turn_started') f.start(event.payload.userMessage)+ if (event.type === 'turn_ended') f.end()+ }+ }+ await f.send('A'); await vi.advanceTimersByTimeAsync(0); observe(normalizeUser('A'))+ if (engine === 'claude') claude.pendingTools.add('running-tool')+ await f.send('B'); await vi.advanceTimersByTimeAsync(0)+ const inputEvents = normalizeUser('B')+ expect(inputEvents.map(event => event.type)).toEqual(['turn_ended', 'turn_started'])+ if (engine === 'claude') expect(claude.pendingTools.has('running-tool')).toBe(false) // unchanged shared normalizer+ observe(inputEvents)+ expect(f.receipt('A').state).toBe('unknown')+ expect(f.receipt('B').state).toBe('unknown')+ const endEvents = engine === 'codex'+ ? codex.ingest(JSON.stringify({ type: 'event_msg', payload: { type: 'task_complete' } }))+ : (() => {+ claude.pendingTools.delete('running-tool')+ return lineToEvents(JSON.stringify({ type: 'assistant', message: { role: 'assistant', content: [{ type: 'text', text: 'done' }], stop_reason: 'end_turn' } }), claude)+ })()+ expect(endEvents.some(event => event.type === 'turn_ended')).toBe(true)+ observe(endEvents)+ expect(f.receipt('A').state).toBe('unknown')+ expect(f.receipt('B').state).toBe('unknown')+ f.controller.forget('agent')+})++it('excludes legacy writes only during Device ownership of the same pane', async () => {+ vi.useFakeTimers()+ const writes: string[] = []+ let finish!: () => void+ const controller = makeDevice({+ inject: async () => { writes.push('device'); await new Promise<void>(resolve => { finish = resolve }); return true },+ })+ await controller.legacyWrite('agent', async () => { writes.push('local-before') })+ controller.submit('agent', 'A', 'delivery-A')+ await vi.advanceTimersByTimeAsync(0)+ const local = controller.legacyWrite('agent', async () => { writes.push('local-after') })+ await controller.legacyWrite('other', async () => { writes.push('other-pane') })+ expect(writes).toEqual(['local-before', 'device', 'other-pane'])+ finish()+ await vi.advanceTimersByTimeAsync(0)+ await local+ expect(writes).toEqual(['local-before', 'device', 'other-pane', 'local-after'])+ controller.forget('agent')+})diff --git a/cli/src/lib/autonomous-device/input.ts b/cli/src/lib/autonomous-device/input.tsnew file mode 100644index 000000000..6f391fd76--- /dev/null+++ b/cli/src/lib/autonomous-device/input.ts@@ -0,0 +1,302 @@+import { createHash } from 'node:crypto'+import type { RegisteredSession } from '../registry.js'+import type { SessionInputDelivery } from '../sessionInput.js'+import type { TerminalActionResult } from '../terminalTypes.js'+import type { LiveEvent } from '../normalize.js'++const NATIVE = new Set(['claude', 'codex'])+const VERIFY_MS = 1500+const TTL_MS = 5 * 60_000+export interface DeviceInputStatus {+ sessionId: string+ deliveryId: string+ mode: 'direct' | 'steering' | 'native_queue' | 'native_input' | 'daemon_queue'+ phase: 'waiting_for_writer' | 'waiting_for_user' | 'waiting_for_turn' | 'submitted' | 'accepted' | 'unconfirmed'+}+interface Input {+ deliveryId: string+ content: string+ fingerprint: string+ expires: number+ started: boolean+ dispatched: boolean+ cancelled: boolean+ mode: DeviceInputStatus['mode']+ retry: boolean+ retries: number+ observes: number+}+interface State {+ busy: boolean+ userAction: boolean+ queue: Input[]+ pending: Input[]+ active?: Input+ writing: boolean+ release?: () => void+ timer?: NodeJS.Timeout+}+interface Writer {+ held: boolean+ legacy: number+ waiters: Array<() => void>+}+export interface DeviceInputDeps {+ getSession: (id: string) => RegisteredSession | undefined+ validateRuntime: (session: RegisteredSession) => Promise<boolean>+ inject: (id: string, text: string) => Promise<boolean | TerminalActionResult>+ sendKey: (id: string, key: string) => Promise<boolean | TerminalActionResult>+ capture: (id: string) => Promise<string | null>+ isAwaitingUser?: (session: RegisteredSession) => Promise<boolean>+ acquireControl: (id: string) => (() => void) | null+ legacySubmit: (id: string, text: string, deliveryId: string) => void+ legacyCancel: (deliveryId: string) => boolean+ onDelivery: (event: SessionInputDelivery) => void+ onInputStatus: (event: DeviceInputStatus) => void+ onForget?: (id: string) => void+}+const fingerprint = (text: string) => createHash('sha256').update(text.replace(/\r\n/g, '\n').trim()).digest('hex')+const visible = (text: string) => text.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '')+const normalize = (text: string) => text.replace(/\s+/g, ' ').trim()+function draft(capture: string, content: string): 'pending' | 'clear' | 'unreadable' {+ const lines = visible(capture).split('\n')+ const index = lines.findLastIndex(line => /[›❯]/u.test(line) && !/[›❯]\s*\d+\.\s/u.test(line))+ if (index < 0) return 'unreadable'+ return normalize(lines.slice(index).join('\n')).includes(normalize(content)) ? 'pending' : 'clear'+}+const executed = (result: boolean | TerminalActionResult) => result === true || (typeof result !== 'boolean' && result.dispatch === 'executed')+const rejected = (result: boolean | TerminalActionResult) => result === false || (typeof result !== 'boolean' && (result.dispatch === 'not_started' || result.dispatch === 'rejected'))++/** A separate route, used exclusively by Autonomous Device turn.send. */+export class AutonomousDeviceInput {+ private states = new Map<string, State>()+ private writers = new Map<string, Writer>()+ constructor(private readonly deps: DeviceInputDeps) {}+ private state(id: string): State {+ let state = this.states.get(id)+ if (!state) { state = { busy: false, userAction: false, queue: [], pending: [], writing: false }; this.states.set(id, state) }+ return state+ }+ private writer(id: string): Writer {+ let writer = this.writers.get(id)+ if (!writer) { writer = { held: false, legacy: 0, waiters: [] }; this.writers.set(id, writer) }+ return writer+ }+ /** Legacy writes run unchanged unless a Device write currently owns this same pane.+ * This also covers a local validation that began before Device acquired the existing control lock.+ */+ legacyWrite<T>(id: string, operation: () => Promise<T>): Promise<T> {+ const writer = this.writer(id)+ const run = async () => {+ writer.legacy+++ try { return await operation() }+ finally { writer.legacy-- }+ }+ if (!writer.held) return run()+ return new Promise<T>((resolve, reject) => writer.waiters.push(() => { void run().then(resolve, reject) }))+ }+ private delivery(id: string, item: Input, state: SessionInputDelivery['state'], reason?: string): void {+ this.deps.onDelivery({ sessionId: id, deliveryId: item.deliveryId, state, ...(reason ? { reason } : {}) })+ }+ private status(id: string, item: Input, phase: DeviceInputStatus['phase'], mode = item.mode): void {+ this.deps.onInputStatus({ sessionId: id, deliveryId: item.deliveryId, mode, phase })+ }+ submit(id: string, content: string, deliveryId: string): void {+ const session = this.deps.getSession(id)+ if (!session) { this.deps.onDelivery({ sessionId: id, deliveryId, state: 'rejected', reason: 'agent_gone' }); return }+ const state = this.state(id)+ if (!NATIVE.has(session.engine)) {+ this.deps.legacySubmit(id, content, deliveryId)+ this.deps.onInputStatus({ sessionId: id, deliveryId, mode: state.busy ? 'daemon_queue' : 'direct', phase: state.busy ? 'waiting_for_turn' : 'waiting_for_writer' })+ return+ }+ this.expire(id, state)+ const item: Input = { deliveryId, content, fingerprint: fingerprint(content), expires: Date.now() + TTL_MS,+ started: false, dispatched: false, cancelled: false, mode: 'direct', retry: false, retries: 0, observes: 0 }+ if (state.pending.length >= 64 || state.queue.length >= 8 || state.queue.reduce((bytes, next) => bytes + Buffer.byteLength(next.content), Buffer.byteLength(content)) > 24 * 1024) {+ this.delivery(id, item, 'rejected', 'queue_full'); return+ }+ state.queue.push(item)+ this.delivery(id, item, 'queued')+ this.status(id, item, state.userAction ? 'waiting_for_user' : 'waiting_for_writer', 'daemon_queue')+ this.pump(id, state)+ }+ private expire(id: string, state: State): void {+ state.queue = state.queue.filter(item => {+ if (item.expires > Date.now()) return true+ this.delivery(id, item, 'rejected', 'queue_expired'); return false+ })+ }+ private later(id: string, state: State, delay = VERIFY_MS): void {+ if (this.states.get(id) !== state || state.timer) return+ state.timer = setTimeout(() => {+ state.timer = undefined+ if (state.active && !state.writing) void this.verify(id, state, state.active)+ else this.pump(id, state)+ }, delay)+ }+ private pump(id: string, state: State): void {+ if (this.states.get(id) !== state || state.active) return+ this.expire(id, state)+ if (!state.queue.length || state.pending.length >= 64) return+ const session = this.deps.getSession(id)+ if (!session) { this.forget(id); return }+ const writer = this.writer(id)+ if (writer.held || writer.legacy) { this.later(id, state, 100); return }+ const release = this.deps.acquireControl(id)+ if (!release) { this.later(id, state, 100); return }+ writer.held = true+ state.release = () => {+ writer.held = false+ // Register delayed legacy writes before another Device write can acquire the pane.+ for (const resume of writer.waiters.splice(0)) resume()+ release()+ }+ const item = state.queue.shift()!+ state.active = item+ state.writing = true+ void this.dispatch(id, state, session, item)+ }+ private finishWrite(id: string, state: State, item: Input): void {+ if (state.active !== item) return+ if (state.timer) clearTimeout(state.timer)+ state.timer = undefined+ state.active = undefined+ state.writing = false+ state.release?.()+ state.release = undefined+ this.pump(id, state)+ }+ private async dispatch(id: string, state: State, session: RegisteredSession, item: Input): Promise<void> {+ try {+ if (!await this.deps.validateRuntime(session)) { this.delivery(id, item, 'rejected', 'runtime_gone_pre_paste'); this.finishWrite(id, state, item); return }+ if (this.states.get(id) !== state || item.cancelled) { this.finishWrite(id, state, item); return }+ const blocked = this.deps.isAwaitingUser ? await this.deps.isAwaitingUser(session) : state.userAction+ if (this.states.get(id) !== state || item.cancelled) { this.finishWrite(id, state, item); return }+ state.userAction = blocked+ if (blocked) {+ state.queue.unshift(item)+ this.status(id, item, 'waiting_for_user', 'daemon_queue')+ // Do not immediately pump the item returned to the queue.+ state.active = undefined; state.writing = false; state.release?.(); state.release = undefined+ this.later(id, state)+ return+ }+ const version = session.cliVersion?.match(/(\d+)\.(\d+)\.(\d+)/)+ item.mode = !state.busy ? 'direct' : session.engine === 'claude' ? 'native_queue'+ : version && (Number(version[1]) > 0 || Number(version[2]) >= 106) ? 'steering' : 'native_input'+ item.dispatched = true+ state.pending.push(item)+ const result = await this.deps.inject(session.agentId, item.content)+ if (this.states.get(id) !== state) return+ if (item.started) { this.finishWrite(id, state, item); return }+ if (rejected(result)) {+ state.pending = state.pending.filter(next => next !== item)+ this.delivery(id, item, 'rejected', 'paste_failed'); this.finishWrite(id, state, item); return+ }+ item.retry = executed(result)+ this.status(id, item, 'submitted')+ this.delivery(id, item, item.retry ? 'delivered' : 'unknown', item.retry ? undefined : 'dispatch_ambiguous')+ state.writing = false+ await this.verify(id, state, item, false)+ } catch {+ if (this.states.get(id) !== state || item.cancelled) return+ if (item.started) { this.finishWrite(id, state, item); return }+ this.delivery(id, item, item.dispatched ? 'unknown' : 'rejected', item.dispatched ? 'dispatch_ambiguous' : 'runtime_gone_pre_paste')+ state.writing = false+ if (item.dispatched) this.later(id, state)+ else this.finishWrite(id, state, item)+ } finally {+ if (this.states.get(id) !== state) { state.writing = false; state.release?.(); state.release = undefined }+ }+ }+ private async verify(id: string, state: State, item: Input, allowRetry = true): Promise<void> {+ try {+ const pane = await this.deps.capture(id)+ if (this.states.get(id) !== state || state.active !== item) return+ const evidence = pane ? draft(pane, item.content) : 'unreadable'+ if (item.started || evidence === 'clear') {+ this.status(id, item, 'accepted'); this.finishWrite(id, state, item); return+ }+ if (allowRetry && evidence === 'pending' && item.retry && item.retries < 2 && !state.userAction) {+ state.writing = true+ const session = this.deps.getSession(id)+ if (!session || !await this.deps.validateRuntime(session)) { this.delivery(id, item, 'unknown', 'runtime_gone_post_paste'); return }+ const blocked = await this.deps.isAwaitingUser?.(session)+ if (this.states.get(id) !== state || state.active !== item || item.started) return+ if (!blocked) {+ item.retry = false+ item.retries+++ item.retry = executed(await this.deps.sendKey(id, 'Enter'))+ }+ }+ if (++item.observes <= 7) this.later(id, state)+ else { this.delivery(id, item, 'unknown', 'input_acceptance_unconfirmed'); this.status(id, item, 'unconfirmed') }+ } catch {+ if (this.states.get(id) !== state || item.started) return+ this.delivery(id, item, 'unknown', 'dispatch_ambiguous')+ if (++item.observes <= 7) this.later(id, state)+ else this.status(id, item, 'unconfirmed')+ } finally {+ if (state.active === item) state.writing = false+ if (this.states.get(id) !== state) { state.release?.(); state.release = undefined }+ if (item.started) this.finishWrite(id, state, item)+ }+ }+ onTurnStarted(id: string, content: string): void {+ const state = this.state(id)+ state.busy = true+ const item = state.pending.find(next => next.fingerprint === fingerprint(content))+ if (!item) return+ item.started = true+ state.pending = state.pending.filter(next => next !== item)+ this.status(id, item, 'accepted')+ this.delivery(id, item, 'started')+ if (state.active === item && !state.writing) this.finishWrite(id, state, item)+ else this.pump(id, state)+ }+ onTurnEnded(id: string): void {+ const state = this.state(id)+ state.busy = false+ if (state.active && !state.writing) this.later(id, state)+ else this.pump(id, state)+ }+ setUserAction(id: string, open: boolean): void {+ const state = this.state(id)+ state.userAction = open+ if (!open) this.pump(id, state)+ }+ cancelDelivery(deliveryId: string): boolean {+ for (const [id, state] of this.states) {+ const item = state.queue.find(next => next.deliveryId === deliveryId)+ ?? (state.active?.deliveryId === deliveryId && !state.active.dispatched ? state.active : undefined)+ if (!item) continue+ item.cancelled = true+ state.queue = state.queue.filter(next => next !== item)+ this.delivery(id, item, 'rejected', 'cancelled')+ return true+ }+ return this.deps.legacyCancel(deliveryId)+ }+ forget(id: string): void {+ const state = this.states.get(id)+ this.states.delete(id)+ if (state) {+ if (state.timer) clearTimeout(state.timer)+ for (const item of new Set([...state.queue, ...state.pending, ...(state.active ? [state.active] : [])])) {+ item.cancelled = true+ this.delivery(id, item, item.dispatched ? 'unknown' : 'rejected', 'agent_gone')+ }+ // An in-progress terminal write cannot be recalled; release only when its promise settles.+ if (!state.writing) { state.release?.(); state.release = undefined }+ }+ this.deps.onForget?.(id)+ }+}++/** Filter only the Autonomous Device view. Shared normalizers and other consumers stay unchanged.+ * A same-batch end immediately preceding new input is not sufficient completion evidence.+ */+export function isDeviceInputBoundary(engine: string, events: readonly LiveEvent[], index: number): boolean {+ return NATIVE.has(engine) && events[index]?.type === 'turn_ended' && events[index + 1]?.type === 'turn_started'+}diff --git a/cli/src/lib/autonomous-device/service.ts b/cli/src/lib/autonomous-device/service.tsindex 6ac85294f..6d9f0bfd5 100644--- a/cli/src/lib/autonomous-device/service.ts+++ b/cli/src/lib/autonomous-device/service.ts@@ -1,3 +1,4 @@+import type { DeviceInputStatus } from './input.js'import type { AutonomousDeviceStore } from './store.js'import { DEVICE_STORE_CAPABILITIES, DeviceStoreError } from './storeContract.js'import { createHash, randomUUID } from 'node:crypto'@@ -7,6 +8,7 @@ export type ReceiptState = 'queued' | 'delivered' | 'started' | 'completed' | 'rexport interface AutonomousDeviceReceipt {idempotencyKey: string; deliveryId: string; operation: string; state: ReceiptStateagentId: string; machineId: string; serverInstanceId: string; turnId: string | null+ input?: Pick<DeviceInputStatus, 'mode' | 'phase'>error: { code: string; message: string } | null; at: number}export interface AutonomousDeviceAgent { agentId: string; name: string; engine: string; state: string; packageId?: string | null; workspace?: string | null; runtime?: string }@@ -75,6 +77,11 @@ export class AutonomousDeviceService {private readonly entries = new Map<string, Entry>()private readonly deliveries = new Map<string, Entry>()private readonly turns = new Map<string, Entry>()+ // Transcript adapters expose session boundaries, not engine input/turn IDs. Once+ // inputs overlap, a session-wide done cannot prove which request finished.+ private readonly ambiguousTurns = new Set<string>()+ private readonly openTurns = new Set<string>()+ private readonly pendingStarts = new Set<string>()private readonly questions = new Map<string, { requestId: string; questions: unknown }>()private events: AutonomousDeviceFrame[] = []private sequence = 0@@ -213,19 +220,46 @@ export class AutonomousDeviceService {}private update(entry: Entry, state: ReceiptState, code?: string): void {if (['completed', 'rejected'].includes(entry.receipt.state)) return- if (state === 'delivered' && entry.receipt.state === 'started') return+ if ((state === 'delivered' || state === 'started') && entry.receipt.state === 'started') returnentry.receipt.state = stateentry.receipt.at = this.now()entry.receipt.error = code ? { code, message: code === 'NOT_CONFIRMED' ? 'Delivery could not be confirmed; inspect the agent before retrying.' : code } : nullif (state === 'started') {entry.receipt.turnId ??= randomUUID()- this.turns.set(entry.receipt.agentId, entry)+ const agentId = entry.receipt.agentId+ const previous = this.turns.get(agentId)+ if (this.openTurns.has(agentId) || this.ambiguousTurns.has(agentId) || (previous && previous !== entry)) {+ this.ambiguousTurns.add(agentId)+ this.turns.delete(agentId)+ if (previous) this.update(previous, 'unknown', 'OVERLAPPING_INPUTS')+ this.update(entry, 'unknown', 'OVERLAPPING_INPUTS')+ return+ }+ this.turns.set(agentId, entry)+ this.pendingStarts.add(agentId)}this.event('receipt.updated', entry.receipt.agentId, { receipt: structuredClone(entry.receipt), idempotencyKey: entry.receipt.idempotencyKey })}+ agentGone(agentId: string): void {+ this.turns.delete(agentId)+ this.openTurns.delete(agentId)+ this.pendingStarts.delete(agentId)+ this.ambiguousTurns.delete(agentId)+ for (const entry of this.deliveries.values()) {+ if (entry.receipt.agentId === agentId && entry.receipt.operation === 'turn.send'+ && !['completed', 'rejected'].includes(entry.receipt.state)) this.update(entry, 'unknown', 'AGENT_GONE')+ }+ }+ inputStatus(event: DeviceInputStatus): void {+ const entry = this.deliveries.get(event.deliveryId)+ if (!entry || entry.receipt.agentId !== event.sessionId) return+ if (['completed', 'rejected'].includes(entry.receipt.state)) return+ entry.receipt.input = { mode: event.mode, phase: event.phase }+ this.event('receipt.updated', event.sessionId, { receipt: structuredClone(entry.receipt), idempotencyKey: entry.receipt.idempotencyKey })+ }delivery(event: AutonomousDeviceDelivery): void {const entry = this.deliveries.get(event.deliveryId)- if (!entry) return+ if (!entry || entry.receipt.agentId !== event.sessionId) returnthis.update(entry, event.state, event.reason)}revoke(deviceId: string): void {@@ -261,10 +295,22 @@ export class AutonomousDeviceService {for (const event of this.events) if (Number(event.eventId) > Number(resume?.cursor)) send(event)}turnStarted(agentId: string): void {+ this.openTurns.add(agentId)+ if (!this.pendingStarts.delete(agentId)) {+ const previous = this.turns.get(agentId)+ if (previous) {+ this.turns.delete(agentId)+ this.ambiguousTurns.add(agentId)+ this.update(previous, 'unknown', 'OVERLAPPING_INPUTS')+ }+ }const entry = this.turns.get(agentId)this.event('turn.started', agentId, entry ? { turnId: entry.receipt.turnId, idempotencyKey: entry.receipt.idempotencyKey } : {})}turnEnded(agentId: string, aborted = false): void {+ this.ambiguousTurns.delete(agentId)+ this.openTurns.delete(agentId)+ this.pendingStarts.delete(agentId)const entry = this.turns.get(agentId)if (entry) {this.update(entry, aborted ? 'unknown' : 'completed', aborted ? 'TURN_INTERRUPTED' : undefined)@@ -286,14 +332,14 @@ export class AutonomousDeviceService {this.event(p.kind === 'summary' ? 'turn.summary' : p.kind === 'tool' ? 'turn.tool' : 'agent.error', agentId, full ? { ...p, fullText: full } : p)}}- get capabilities(): string[] { return [...AUTONOMOUS_DEVICE_CAPABILITIES, ...(this.options.store ? DEVICE_STORE_CAPABILITIES : [])] }+ get capabilities(): string[] { return [...AUTONOMOUS_DEVICE_CAPABILITIES, 'input.status.v1', ...(this.options.store ? DEVICE_STORE_CAPABILITIES : [])] }async request(deviceId: string, req: Record<string, unknown>): Promise<AutonomousDeviceFrame> {const type = typeof req.type === 'string' ? req.type : 'invalid'const response = (data: Record<string, unknown>): AutonomousDeviceFrame => ({ type: `${type}_result`, requestId: req.requestId, ...data })let reserved: Entry | undefinedtry {if (!UUID.test(String(req.requestId))) fail('INVALID_REQUEST', 'requestId must be a UUIDv4')- if (!this.capabilities.includes(type)) fail('UNSUPPORTED_CAPABILITY', 'Operation is not supported')+ if (type === 'input.status.v1' || !this.capabilities.includes(type)) fail('UNSUPPORTED_CAPABILITY', 'Operation is not supported')if ((DEVICE_STORE_CAPABILITIES as readonly string[]).includes(type)) return response(await this.options.store!.request(deviceId, req))const allowed = ['type', 'requestId', ...(['agents.list', 'focus.get', 'focus.ensure'].includes(type) ? [] : type === 'receipt.get' ? ['idempotencyKey'] : type === 'focus.step' ? ['direction', 'idempotencyKey', 'focusRevision'] : type === 'scroll' ? ['phase', 'dy', 'velocity'] : ['machineId', 'agentId']),...(MUTATIONS.has(type) ? ['idempotencyKey'] : []), ...(type === 'turn.send' ? ['text', 'focusRevision'] : type === 'question.answer' ? ['questionRequestId', 'answers', 'focusRevision'] : type === 'recap' ? ['n'] : [])]diff --git a/docs/autonomous-device-integration.md b/docs/autonomous-device-integration.mdindex cc38fda46..517e74d2d 100644--- a/docs/autonomous-device-integration.md+++ b/docs/autonomous-device-integration.md@@ -338,3 +338,12 @@ nothing to retry — a lost `move` is a shorter scroll. Success is an empty `scrterminal Desktop has focused scrolls; a viewer pane does not. Errors: `FOCUS_UNAVAILABLE` when noDesktop window is connected; `INVALID_REQUEST` for a bad phase, a non-integer or out-of-range value,or any other field. Older CLIs do not list `scroll` in hello capabilities.++## Input during a running task++Claude/Codex `turn.send` now uses native input while the agent is working, with a serialized+terminal writer and independent delivery tracking. `input.status.v1` advertises optional+`receipt.input` scheduling/acceptance details. Existing receipt states retain their meanings;+acceptance is not completion. Overlapping starts without engine correlation remain unknown.+See [engine behavior, tests, and required OS coordination](in-flight-agent-input.md), especially+the prohibition on assigning an uncorrelated session summary/latest recap to a pending message.diff --git a/docs/autonomous-device-result-correlation.md b/docs/autonomous-device-result-correlation.mdnew file mode 100644index 000000000..db238d9aa--- /dev/null+++ b/docs/autonomous-device-result-correlation.md@@ -0,0 +1,118 @@+# Autonomous Device grouped results — turn.correlation.v2++Status: Harness-side contract decision for OS coordination, 2026-09-24.+**Not implemented or advertised by PR #294.** Both sides must implement and test this+contract before enabling it. PR #294 delivers follow-up input; it does not complete the+merged-result → Lamp flow. Existing single-input fields keep their meaning.++## Decision++One engine result may cover several inputs. Publish one result with explicit membership,+not one copy per input. Never select an arbitrary request key for a merged summary.+Input acceptance, result membership, and task completion are separate facts.++Negotiate `turn.correlation.v2` as a capability on both peers. Send a new+`turn.result` event only to an opted-in peer. Do not overload `turn.summary`, `turnId`,+`runId`, or receipt states. Older peers continue to receive existing session events;+uncorrelated overlapping results cannot complete or speak a pending run.++```json+{+ "type": "event",+ "kind": "turn.result",+ "agentId": "agent-123",+ "payload": {+ "serverInstanceId": "instance-123",+ "resultId": "result-456",+ "correlation": {+ "scope": "group",+ "inputs": [+ { "deliveryId": "delivery-A", "idempotencyKey": "original-key-A" },+ { "deliveryId": "delivery-B", "idempotencyKey": "original-key-B" }+ ],+ "engineTurnId": "optional-engine-native-id"+ },+ "outcome": "completed",+ "fullText": "Created the house with a garden and a red roof."+ }+}+```++Required: agentId, serverInstanceId, resultId, correlation.scope, correlation.inputs,+outcome, fullText. Scope is `input` (exactly one member) or `group` (two or more).+Each member contains both original idempotencyKey and deliveryId, with no duplicates.+Outcome is `completed`, `failed`, or `cancelled`. fullText is the complete immutable result+for this membership, never a pointer to latest recap. engineTurnId is optional evidence,+not the DEVICE run ID. Do not put a singular runId/idempotencyKey on a group event.+The existing transport sequence/replay envelope still applies.++## Harness responsibilities++1. Preserve each request reservation and receipt independently. Include only inputs whose+ consumption and association with this result are established by engine evidence.+ A clear composer, native queue insertion, proximity in time, or a session end alone+ cannot establish result membership. Queued but unconsumed inputs stay pending.+2. Build the immutable result at an actual engine completion boundary. Generate resultId+ once; replay and reconnect reuse the same ID, text and membership. Never emit a result+ when only input acceptance is known. Missing evidence retains `unknown` receipts and+ session-scoped information; it does not produce a guessed group.+3. Only proven members may transition to completed (for outcome completed). Failed or+ cancelled work must not be labelled completed; existing receipt states remain unchanged+ until a separately negotiated receipt outcome extension exists. Do not close other runs.+4. Partition delivery to the authenticated originating device. Do not disclose another+ device's request keys or private result through a shared agent subscription. Grouping+ across owners requires separate authorization and is outside this contract.+5. Retain result records alongside receipt/replay retention and publish retention limits.+ Instance change is reconciliation, never permission to resubmit automatically.++This requires Device-specific engine evidence extraction; synthetic group IDs do not+solve missing evidence. Shared chat/orchestrator normalizers need not change for this PR.++## OS responsibilities++1. Register original request key, deliveryId, serverInstanceId, agentId and local DEVICE+ run ID before routing a result. Resolve every member to the same authenticated device,+ instance and agent. Any explicit mismatch or unresolved member prevents application+ of the whole event; reconcile receipts, without agent/latest-run fallback.+2. Atomically store result and membership, then resolve exactly the listed runs with the+ stated outcome. Pending runs not listed remain pending. Store a reference to the one+ shared result rather than copying it into independent per-input responses.+3. Deduplicate by (device identity, serverInstanceId, resultId). An identical replay is a+ no-op. Same ID with different content/membership is a protocol error, never a new result.+4. Enqueue a single Lamp utterance per result, using fullText from this event. Do not speak+ once per member or again on turn.done/summary/receipt.updated. Suppress results belonging+ to expired voice targets or incompatible reply routes; retain the result for retrieval.+ A group spanning distinct reply destinations must not choose the latest destination.+5. Persist the TTS outbox and its dedupe identity before playback. If playback acknowledgment+ is lost, retain an uncertain playback state and avoid automatic replay: exactly-once+ audible playback cannot be guaranteed by network dedupe alone.+6. Reconnect replays results and reconciles original receipts/keys; it never resends input+ with a new key. Consume receipt.input separately for steering/native/daemon queue UI.++## Current OS compatibility++The OS patch supplied on base `30e034f84` supports one event → one input/run, using+agentId + original idempotencyKey (top-level, payload, or payload.receipt), or DEVICE+runId/run_id. If both exist they must match the same route. Explicit mismatch does not+fall back; overlapping runs do not use agent-only/latest recap inference. fullText in the+event is preferred. This is compatible with proven single-input summaries, **not groups**.++OS does not yet consume this capability, group membership, result dedupe, or receipt.input.+Do not send the example above to today's OS or duplicate a merged summary under every key.+No Autonomous OS source is changed by this PR.++## Joint acceptance tests before enablement++- A then steering B: one proven merged result resolves A/B and speaks once.+- Claude queues B but result covers only A: B remains pending until consumed and completed.+- A/B/C with B included and C not consumed: only A/B resolve.+- Unknown membership: no fabricated key, completion or voice response.+- Duplicate/reordered events, lost acknowledgment, reconnect, and restart: no duplicate input+ or TTS enqueue; changed instance is reconciled explicitly.+- Wrong key, deliveryId, agent, owner, or instance: no partial application or fallback.+- Mixed voice destinations, cancelled runs, expired routes and failed results: no wrong Lamp+ response; outcomes remain distinct from input acceptance.++Rollout order: OS parser/storage/outbox and Harness evidence/result producer behind the+capability; shared fixtures for the cases above; real engine + physical Lamp test; then+mutual enablement. Until then PR #294 is an input-delivery fix only.diff --git a/docs/in-flight-agent-input.md b/docs/in-flight-agent-input.mdnew file mode 100644index 000000000..3d9d207db--- /dev/null+++ b/docs/in-flight-agent-input.md@@ -0,0 +1,133 @@+# Input while an agent is working++`turn.send` uses the same session for follow-up requests. A `deliveryId` no longer makes+Claude/Codex wait for the previous task to finish. The Device-only input adapter owns a short write lock+(runtime validation, paste, Enter, and acceptance verification), not a lock over model+reasoning or tool execution. Device messages preserve FIFO order. Existing local/orchestrator scheduling stays unchanged;+CLI wiring excludes simultaneous terminal writes only while Device owns the same pane.+The shared SessionInputController and Claude/Codex normalizers are unchanged.++## Engine behavior++| Adapter | While busy | What Harness reports |+| --- | --- | --- |+| Codex >= 0.106 | Native Enter; steering is always enabled in these versions | `steering` |+| Claude Code | Native Enter; message appears in Claude's editable queue | `native_queue` |+| Older/unknown-version Codex | Existing native Enter path; precise semantics are not certified by the adapter | `native_input` (never claim confirmed steering) |+| Other current adapters | No verified non-interrupting busy-input implementation in Harness | `daemon_queue`, released at turn end |++Codex's visible pending steer may wait for the next tool boundary. Claude may consume a+queued message after tool calls within the same turn or at a later turn. Neither path sends+Escape, Ctrl+C, stop/restart, or Claude's interrupt-and-send shortcut. Native acceptance+does not establish that the model has processed the message or finished its task.++Upstream references: [Codex 0.106 release](https://github.com/openai/codex/releases/tag/rust-v0.106.0)+removed the steer feature flag; [Claude interactive input](https://code.claude.com/docs/en/interactive-mode#queue-messages-while-claude-works)+describes its queue and tool-boundary consumption. The version threshold deliberately does+not infer runtime feature configuration on older Codex builds.++## State and evidence++- Each native input retains its own fingerprint/delivery ID until a matching user-message+ event arrives. An unrelated start does not erase later inputs. Identical texts match FIFO.+- One paste/Enter transaction holds the writer lock. Subsequent input is released after a+ matching transcript event or a visible composer no longer containing that draft. A missing+ capture or a dialog-only screen is not evidence of acceptance.+- An exact visible draft can authorize a bounded Enter retry only after the previous terminal+ operation returned confirmed execution. An ambiguous acknowledgment disables key retries.+ The prompt body is never repasted. If evidence remains insufficient, mark `unknown` and+ retain the composer barrier so the next message cannot concatenate with the draft.+- Question/permission dialogs hold prompts in the daemon. Explicit answers can acquire control+ while prompts are queued; the control lock still excludes other writers. Native preflight+ inspects dialogs again, and polls for their disappearance before releasing queued prompts.+- The existing eight-item/24-KiB waiting queue and five-minute expiration remain. An active+ input returned to the front after detecting a dialog retains its separate reserved slot.+ At most 64 unresolved native inputs can be dispatched per session; further requests are+ rejected with `queue_full`, and already-waiting input waits for capacity.+- Session removal rejects unsent work and marks dispatched/running work unknown. A vanished+ session cannot allow an old async completion to start another write.++The Device event observer ignores an end immediately followed by a new input in the same+Claude/Codex batch: that inferred boundary is insufficient completion evidence. Shared+normalizers and other event consumers retain their existing behavior.++The receipt service keeps every request reservation independently. Only an unambiguous+observed start followed by its session end can complete a receipt. If starts overlap before+an end (including an untracked local task), there is no engine-native turn ID in today's+adapter event. The affected tracked receipts become `unknown / OVERLAPPING_INPUTS` instead+of replacing each other. A `turn.done` never completes every outstanding delivery. A summary+never updates receipt completion. This conservative result is intentional: native acceptance+is proven, per-message task completion is not.++## Backward-compatible input status extension++The CLI advertises **`input.status.v1`** in hello capabilities. This is a feature marker,+not an RPC. Existing request fields, protocol version, states, `turnId`, errors, and+idempotency semantics retain their meanings. Clients that understand the marker may read+optional `receipt.input` from the original response, `receipt.get`, or `receipt.updated`:++```json+{+ "state": "delivered",+ "input": { "mode": "native_queue", "phase": "accepted" }+}+```++`mode` is `direct`, `steering`, `native_queue`, `native_input`, or `daemon_queue`.+`phase` is `waiting_for_writer`, `waiting_for_user`, `waiting_for_turn`, `submitted`,+`accepted`, or `unconfirmed`. `submitted` means the terminal write completed, not that+Enter was consumed. `accepted` means the native input left the composer or was observed+in the transcript, **not task completion**. The dispatch mode remains attached to the+input even if the previous task ends while acceptance is being observed. Before dispatch,+mode is the intended route and phase explains the current wait.++Clients must tolerate an absent `input`, unknown future fields, and `unknown` completion+while `input.phase` remains `accepted`. Only `receipt.state:completed` is receipt completion.+Do not interpret an optional field or an extra `receipt.updated` as a new task.++Dedupe remains `(device identity, idempotencyKey)`, reserved before dispatch. A lost reply,+socket reconnect, receipt poll, or same-key retry cannot create a second submission in+the same server instance. `unknown` reservations are retained. Cache retention and daemon+restart limitations in [the existing contract](autonomous-device-integration.md) still apply:+a changed `serverInstanceId` is reconciliation, never permission to replay automatically.++## OS coordination (no Autonomous OS changes in this PR)++Both callers use this Device route: harness-use retains its taskKey; Harness-only voice+uses its original voice request key and focus revision. The supplied OS patch on base+`30e034f84` adds single-input correlation and avoids agent/latest recap fallback after+overlap. It does not support merged results or consume receipt.input yet.++The concrete handoff is [Grouped result contract: turn.correlation.v2](autonomous-device-result-correlation.md).+It defines the event schema, evidence requirements, run closure, result/TTS dedupe,+compatibility and joint rollout tests. This is the Harness-side contract decision, not an+implemented or advertised capability. No OS repository changes are included. PR #294 fixes+input delivery; the complete overlapping-result → Lamp flow remains follow-up work.++## Validation (2026-09-24)++Mock/component coverage: both caller request shapes (with/without focus revision), A then B+before A ends, rapid Device A/B/C, exact-draft retry, lost key/write acknowledgments,+same-key retry, reconnect replay, separate receipts, native queue, daemon queue, writer/control+locks, dialog detection/release, session disappearance, and overlapping done/summary events.+The scoped CLI regression run passed 474 tests across 18 files (including Autonomous Device+transport/receipts, Claude/Codex normalizers, dialogs, orchestrator, and voice router); TypeScript typecheck passed.+The Python harness-use helper passed 28 tests, and `go test ./harness -run Voice -count=1` passed. OS helper/voice tests ran read-only; no OS source files were changed.++Earlier real engine smoke tests (before isolating the Device adapter) used isolated tmux panes and a temporary directory, the modified+`SessionInputController`, and production `sendToTmux`. No Harness daemon installation or+restart and no existing agent interruption were performed:++| Engine | Observed while A was still working | Final response |+| --- | --- | --- |+| Codex 0.156.1 | B appeared in “Messages to be submitted after next tool call”; empty composer available | `GARDEN_RED` after `sleep 20` |+| Claude Code 2.1.280 | B appeared above “Press up to edit queued messages” while `Bash(sleep 20)` was running | Response included `GARDEN_RED` and respected the no-file-modification instruction |++These earlier live smoke tests verify native engine behavior, but do not validate the final+Device-only adapter wiring. The final adapter is covered by mocks/component tests.+The live smoke tests verified terminal delivery and native behavior, not the full daemon+transcript watcher, Device transport, microphone/STT, or OS TTS. Start observation was supplied+by the test driver after seeing the busy terminal. Receipt correlation/reconnect/error paths+were tested with deterministic mocks. Other engine versions, engines, and Herdr were not+live-tested. A first diagnostic Codex run left an unsent draft; acceptance verification exposed+it, and the final run used a fresh pane to avoid that draft contaminating its input.diff --git a/docs/vi/autonomous-device-integration_vi.md b/docs/vi/autonomous-device-integration_vi.mdindex 9156704e2..caa195790 100644--- a/docs/vi/autonomous-device-integration_vi.md+++ b/docs/vi/autonomous-device-integration_vi.md@@ -180,3 +180,12 @@ không có gì để retry — mất một `move` chỉ là cuộn ngắn hơn.Chỉ terminal Desktop đang focus mới cuộn; pane viewer thì không. Lỗi: `FOCUS_UNAVAILABLE` khi khôngcó cửa sổ Desktop; `INVALID_REQUEST` khi phase sai, giá trị không phải số nguyên hoặc ngoài khoảng,hoặc có field lạ. CLI cũ không liệt kê `scroll` trong capabilities của hello.++## Input bổ sung khi agent đang chạy++`turn.send` tới Claude/Codex dùng input native ngay khi agent đang làm việc; chỉ serialize thao+tác ghi terminal. Capability `input.status.v1` bổ sung `receipt.input` để phân biệt steering,+native queue, daemon queue và trạng thái xác nhận input. Không đổi nghĩa receipt cũ: input đã+nhận không có nghĩa task hoàn tất. Các lượt chồng nhau thiếu correlation phải giữ `unknown`.+Xem [chi tiết engine, kiểm thử và phần OS cần phối hợp](../in-flight-agent-input.md). OS cần lưu+nhiều delivery và không gán summary/recap theo session cho câu bổ sung mới nhất.