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 13: a cancel reads as idle everywhere, and an agent's question end to end (#741)
Deeba3272a1237b
diff --git a/cli/e2e/harness/fakeEngine.mjs b/cli/e2e/harness/fakeEngine.mjsindex 1b9d24ad0..bf0af59ae 100644--- a/cli/e2e/harness/fakeEngine.mjs+++ b/cli/e2e/harness/fakeEngine.mjs@@ -11,7 +11,9 @@// A prompt can carry directives that script the turn: `!slow <ms>` holds the answer back,// `!tool <command>` runs a tool call first, `!grow <MiB>` appends that much compaction history// before answering (the transcripts that crashed the daemon on 2026-10-03), `!hold` leaves the turn-// open until the next prompt, `!exit` ends the process. Everything else is echoed as the answer.+// open until the next prompt, `!ask` asks the person which drink they would like in the engine's own+// dialog and answers with their choice, `!exit` ends the process. Everything else is echoed as the+// answer.import { randomUUID } from 'node:crypto'import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'import { dirname, join } from 'node:path'@@ -90,6 +92,48 @@ export async function run(engine, config) {let turn = 0let open = null+ // The question dialog `!ask` draws, as the real CLIs draw theirs (the parser's fixtures,+ // src/lib/__fixtures__/question-single.txt and question-codex.txt). Claude takes a digit as the+ // choice; Codex moves its cursor on a digit and takes Enter; Esc cancels either.+ let dialog = null+ const QUESTION = 'Which drink would you like?'+ const CHOICES = [['Tea', 'Lighter, steeped leaves.'], ['Coffee', 'Stronger, roasted beans.']]+ const drawDialog = () => {+ const rule = '─'.repeat(60)+ const mark = (i, on) => (dialog.cursor === i ? on : ' ')+ const lines = engine === 'claude'+ ? [rule, ' ☐ Drink', '', QUESTION, '',+ ...CHOICES.flatMap(([label, description], i) => [`${mark(i, '❯')} ${i + 1}. ${label}`, ` ${description}`]),+ ' 3. Type something.', rule, ' 4. Chat about this', '', 'Enter to select · ↑/↓ to navigate · Esc to cancel']+ : [' Question 1/1 (1 unanswered)', ` ${QUESTION}`,+ ...CHOICES.map(([label, description], i) => ` ${mark(i, '›')} ${i + 1}. ${label.padEnd(7)} ${description}`),+ ` ${mark(2, '›')} 3. None of the above Optionally, add details in notes (tab).`,+ ` option ${dialog.cursor + 1}/3 | tab to add notes`, ' enter to submit answer | esc to interrupt']+ // Repainted in place, as a TUI does: a redraw replaces the dialog, it does not stack another below.+ eraseDialog()+ process.stdout.write(`\r\n${lines.join('\r\n')}\r\n`)+ dialog.drawn = lines.length + 1+ }+ const eraseDialog = () => {+ if (dialog?.drawn) process.stdout.write(`\x1b[${dialog.drawn}A\r\x1b[0J`)+ if (dialog) dialog.drawn = 0+ }+ const dialogKeys = (chunk) => {+ for (const key of chunk.match(/\x1b\[[AB]|\x1b|\r|\n|./gs) ?? []) {+ if (!dialog) return+ const settle = (choice) => { eraseDialog(); const asked = dialog; dialog = null; asked.resolve(choice) }+ const rows = engine === 'claude' ? CHOICES.length : CHOICES.length + 1+ if (key === '\x1b[B') { dialog.cursor = Math.min(dialog.cursor + 1, rows - 1); drawDialog() }+ else if (key === '\x1b[A') { dialog.cursor = Math.max(dialog.cursor - 1, 0); drawDialog() }+ else if (key === '\x1b') settle(null)+ else if (key === '\r' || key === '\n') settle(CHOICES[dialog.cursor]?.[0] ?? 'None of the above')+ else if (/^[1-9]$/.test(key) && Number(key) <= rows) {+ dialog.cursor = Number(key) - 1+ if (engine === 'claude') settle(CHOICES[dialog.cursor][0])+ else drawDialog()+ }+ }+ }const finish = (text) => {if (engine === 'claude') {claude({ type: 'assistant', message: { id: `msg_${turn}`, role: 'assistant', model: config.claudeModel ?? 'claude-opus-5-5', content: [{ type: 'text', text }], stop_reason: 'end_turn' } })@@ -143,6 +187,32 @@ export async function run(engine, config) {}}if (directive?.[1] === 'hold') return+ if (directive?.[1] === 'ask') {+ // A real engine thinks before it asks; a dialog already on screen when its turn began reads to the+ // daemon as the previous turn's (askQuestion.ts `noteTurnStart`).+ await new Promise((resolve) => setTimeout(resolve, 2_000))+ const choice = await new Promise((resolve) => { dialog = { cursor: 0, drawn: 0, resolve }; drawDialog() })+ if (choice === null) {+ process.stdout.write('\r\n(question cancelled)\r\n')+ finish('(question cancelled)')+ return+ }+ // The tool call is written once it is answered, as the real CLIs flush it.+ const questions = [{ question: QUESTION, header: 'Drink', multiSelect: false, options: CHOICES.map(([label, description]) => ({ label, description })) }]+ if (engine === 'claude') {+ const id = `toolu_${turn}`+ claude({ type: 'assistant', message: { id: `msg_${turn}_q`, role: 'assistant', model: config.claudeModel ?? 'claude-opus-5-5', content: [{ type: 'tool_use', id, name: 'AskUserQuestion', input: { questions } }], stop_reason: 'tool_use' } })+ claude({ type: 'user', message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: id, content: `User has answered your questions: "${QUESTION}"="${choice}"` }] } })+ process.stdout.write(`\r\n⏺ User answered Claude's questions:\r\n ⎿ · ${QUESTION} → ${choice}\r\n`)+ } else {+ const id = `call_${turn}_q`+ codex('response_item', { type: 'function_call', call_id: id, name: 'request_user_input', arguments: JSON.stringify({ questions }) })+ codex('response_item', { type: 'function_call_output', call_id: id, output: JSON.stringify({ answers: { [QUESTION]: choice } }) })+ process.stdout.write(`\r\n• ${QUESTION} → ${choice}\r\n`)+ }+ finish(`you chose ${choice}`)+ return+ }if (directive?.[1] === 'slow') await new Promise((resolve) => setTimeout(resolve, Number(directive[2]) || 1000))finish(`answer ${turn}: ${prompt}`)}@@ -153,6 +223,7 @@ export async function run(engine, config) {let buffer = ''let queue = Promise.resolve()process.stdin.on('data', (chunk) => {+ if (dialog) { dialogKeys(chunk); return }for (const part of chunk.replace(/\x1b\[20[01]~/g, '').split(/(\r|\n|\x03)/)) {if (part === '\x03') {if (open) {diff --git a/cli/e2e/questions.e2e.ts b/cli/e2e/questions.e2e.tsnew file mode 100644index 000000000..af4b3900a--- /dev/null+++ b/cli/e2e/questions.e2e.ts@@ -0,0 +1,93 @@+/**+ * An agent's question, end to end, for Claude Code and Codex: the dialog the engine draws reaches the+ * window as a question, the answer chosen there is typed into the dialog and is the one the agent gets,+ * and an answer to a question that is no longer the one on screen types nothing.+ */+import { mkdirSync } 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 row = async (client: LocalClient, agentId: string) =>+ ((await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents)+ .find((agent) => agent.id === agentId)+async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, folder: string): Promise<Record<string, any>> {+ const cwd = join(daemon.projectsDir, folder)+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)+ expect(created.error, `${folder}: ${JSON.stringify(created)}`).toBeUndefined()+ return until(`${folder} to bind its conversation`, async () => {+ const agent = await row(client, created.agent.id)+ return agent?.sessionId && agent.status === 'active' ? agent : null+ }, 60_000, 500)+}+const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId+async function turn(client: LocalClient, agentId: string, content: string): Promise<void> {+ const started = client.next(isTurn('turn_started', agentId), 45_000, 'turn_started')+ const ended = client.next(isTurn('turn_ended', agentId), 45_000, 'turn_ended')+ client.send('message', { agentId, content })+ await started+ await ended+}+/** An answer goes back under the question's own request id, which is how the client knows which. */+async function answer(client: LocalClient, agentId: string, requestId: string, answers: Record<string, string>): Promise<Record<string, any>> {+ const reply = client.next((frame) => frame.type === 'question_response_result' && frame.payload?.requestId === requestId, 45_000, 'question_response_result')+ client.send('question_response', { requestId, agentId, answers })+ return (await reply).payload as Record<string, any>+}++describe('an agent asks a question', () => {+ 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(-120).join('\n')}`) })+ await d.start()+ return d+ }++ it.each(engines)('%s: the question reaches the window, and the answer chosen there is the one the agent gets', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const agent = await create(d, client, engine, `ask-${engine}`)+ const asked = client.next((frame) => frame.type === 'commander_question' && frame.agentId === agent.id, 30_000, 'commander_question')+ client.send('message', { agentId: agent.id, content: '!ask' })+ const question = await asked+ const shaped = question.payload?.questions?.[0]+ expect(shaped?.q).toBe('Which drink would you like?')+ expect(shaped?.options).toEqual(expect.arrayContaining(['Tea', 'Coffee']))+ expect((await row(client, agent.id))?.status).toBe('active')++ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')+ const result = await answer(client, agent.id, question.payload.requestId, { [shaped.q]: 'Coffee' })+ expect(result.error, JSON.stringify(result)).toBeUndefined()+ await ended+ expect(await d.capture(agent.tmuxPane)).toContain('you chose Coffee')+ await turn(client, agent.id, 'after the question')+ client.close()+ })++ it.each(engines)('%s: an answer to a question that is not the one on screen types nothing', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const agent = await create(d, client, engine, `stale-${engine}`)+ const asked = client.next((frame) => frame.type === 'commander_question' && frame.agentId === agent.id, 30_000, 'commander_question')+ client.send('message', { agentId: agent.id, content: '!ask' })+ const question = await asked+ const shaped = question.payload?.questions?.[0]+ const stale = await answer(client, agent.id, 'q_not_this_one', { [shaped.q]: 'Tea' })+ expect(stale.error, JSON.stringify(stale)).toBeTruthy()+ expect(await d.capture(agent.tmuxPane)).not.toContain('you chose')+ // The real question is still open, and still answerable.+ const ended = client.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended')+ expect((await answer(client, agent.id, question.payload.requestId, { [shaped.q]: 'Tea' })).error).toBeUndefined()+ await ended+ expect(await d.capture(agent.tmuxPane)).toContain('you chose Tea')+ client.close()+ })+})diff --git a/cli/e2e/turns.e2e.ts b/cli/e2e/turns.e2e.tsnew file mode 100644index 000000000..34bcc9a3a--- /dev/null+++ b/cli/e2e/turns.e2e.ts@@ -0,0 +1,98 @@+/**+ * Turns ending the ways people end them, for Claude Code and Codex, on the real daemon: cancelled from+ * the app, interrupted with Ctrl-C in the terminal itself, and watched by more than one window at once.+ */+import { mkdirSync } 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 row = async (client: LocalClient, agentId: string) =>+ ((await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents)+ .find((agent) => agent.id === agentId)+const bound = (client: LocalClient, agentId: string) =>+ until(`${agentId.slice(0, 8)} to bind its conversation`, async () => {+ const agent = await row(client, agentId)+ return agent?.sessionId && agent.status === 'active' ? agent : null+ }, 60_000, 500)+async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, folder: string): Promise<Record<string, any>> {+ const cwd = join(daemon.projectsDir, folder)+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)+ expect(created.error, `${folder}: ${JSON.stringify(created)}`).toBeUndefined()+ return bound(client, created.agent.id)+}+const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId+async function turn(client: LocalClient, agentId: string, content: string): Promise<void> {+ const started = client.next(isTurn('turn_started', agentId), 45_000, 'turn_started')+ const ended = client.next(isTurn('turn_ended', agentId), 45_000, 'turn_ended')+ client.send('message', { agentId, content })+ await started+ await ended+}+const settled = (client: LocalClient, agentId: string) =>+ until('the agent to read as not working', async () => {+ const agent = await row(client, agentId)+ return agent && agent.activity?.state !== 'working' ? agent : null+ }, 30_000, 250)++describe('how turns end', () => {+ 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(-120).join('\n')}`) })+ await d.start()+ return d+ }++ it.each(engines)('%s: a turn cancelled from the app stops reading as working, and the next message is a fresh turn', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const agent = await create(d, client, engine, `cancel-${engine}`)+ const started = client.next(isTurn('turn_started', agent.id), 30_000, 'turn_started')+ client.send('message', { agentId: agent.id, content: '!hold' })+ await started+ await until('the agent to read as working', async () => (await row(client, agent.id))?.activity?.state === 'working' || null, 15_000, 250)+ client.send('cancel', { agentId: agent.id })+ await settled(client, agent.id)+ await turn(client, agent.id, 'after the cancel')+ client.close()+ })++ it.each(engines)('%s: Ctrl-C typed in the terminal itself ends the turn, and the agent goes on', async (engine) => {+ const d = await fresh()+ const client = await LocalClient.connect(d)+ const agent = await create(d, client, engine, `ctrl-c-${engine}`)+ const started = client.next(isTurn('turn_started', agent.id), 30_000, 'turn_started')+ client.send('message', { agentId: agent.id, content: '!hold' })+ await started+ await d.tmux.run('send-keys', '-t', agent.tmuxPane, 'C-c')+ await settled(client, agent.id)+ await turn(client, agent.id, 'after Ctrl-C in the pane')+ client.close()+ })++ it('every window watching sees the same turn, and one that connects in the middle sees it running', async () => {+ const d = await fresh()+ const first = await LocalClient.connect(d)+ const second = await LocalClient.connect(d)+ const agent = await create(d, first, 'claude', 'two-windows')+ const startedThere = second.next(isTurn('turn_started', agent.id), 30_000, 'turn_started in the second window')+ const endedThere = second.next(isTurn('turn_ended', agent.id), 45_000, 'turn_ended in the second window')+ const startedHere = first.next(isTurn('turn_started', agent.id), 30_000, 'turn_started')+ first.send('message', { agentId: agent.id, content: '!slow 4000' })+ await startedHere+ expect((await startedThere).payload?.userMessage).toBe('!slow 4000')+ const late = await LocalClient.connect(d)+ expect((await row(late, agent.id))?.activity?.state).toBe('working')+ await endedThere+ await settled(late, agent.id)+ for (const client of [first, second, late]) client.close()+ })+})diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex d63cdb1cf..edce9a4b7 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -3442,6 +3442,10 @@ async function runForeground(session: AuthSession | null): Promise<void> {stopHeartbeat,questionWatcher,mirror,+ turnActivity,+ turnStartedAt,+ agentIdFor,+ clients: backend,})backend.onCancel = id => { void cancelAgent(id) }diff --git a/cli/src/core/turns/cancel.spec.ts b/cli/src/core/turns/cancel.spec.tsindex ad3ad8058..d7b7abab6 100644--- a/cli/src/core/turns/cancel.spec.ts+++ b/cli/src/core/turns/cancel.spec.ts@@ -14,6 +14,10 @@ function setup(over: Partial<CancelDeps> = {}) {stopHeartbeat: vi.fn(),questionWatcher: { stop: vi.fn() },mirror: { cancel: vi.fn() },+ turnActivity: { observe: vi.fn(), snapshot: vi.fn(() => ({ state: 'idle' as const, epoch: 'e', revision: 2, validForMs: 0 })) },+ turnStartedAt: new Map([['s1', 1]]),+ agentIdFor: (sessionId) => (sessionId === 's1' ? 'a1' : sessionId),+ clients: { send: vi.fn() },...over,}return { deps, service, calls, cancelAgent: createCancel(deps) }@@ -33,6 +37,21 @@ describe('cancelling a turn', () => {expect(deps.mirror.cancel).toHaveBeenCalledWith('s1')})+ it('tells every window the agent is idle now, without a turn_ended that would recap a killed turn', async () => {+ const { deps, cancelAgent } = setup()+ await cancelAgent('a1')+ expect(deps.turnStartedAt.has('s1')).toBe(false)+ expect(deps.turnActivity.observe).toHaveBeenCalledWith('s1', 'turn_ended')+ expect(deps.clients.send).toHaveBeenCalledTimes(1)+ expect(deps.clients.send).toHaveBeenCalledWith(expect.objectContaining({+ type: 'agent_activity', agentId: 'a1', payload: expect.objectContaining({ activity: expect.objectContaining({ state: 'idle' }) }),+ }))+ // An agent the tracker no longer knows has no activity to report.+ const unknown = setup({ turnActivity: { observe: vi.fn(), snapshot: vi.fn(() => undefined) } })+ await unknown.cancelAgent('a1')+ expect(unknown.deps.clients.send).not.toHaveBeenCalled()+ })+it('waits for input to confirm a cancel a stop depends on, and answers with what it said', async () => {const { deps, cancelAgent } = setup()vi.mocked(deps.input.cancelConfirmed).mockResolvedValueOnce(false)diff --git a/cli/src/core/turns/cancel.ts b/cli/src/core/turns/cancel.tsindex 255e1e5bc..6b0701615 100644--- a/cli/src/core/turns/cancel.ts+++ b/cli/src/core/turns/cancel.ts@@ -1,16 +1,18 @@/*** Cancelling a turn: the person interrupted it (C-c from the app, or a stop that must not wait). The* turn is marked closed in whichever engine holds it, input is told, the heartbeat and the question- * watcher stop, and the device's tile closes without a recap.+ * watcher stop, the device's tile closes without a recap, and every window reads the agent as idle.** Moved verbatim out of `runForeground` (the core boundary, step 10: docs/design/2026-10-03-harnessd.md).*/import type { CursorSubagentManager } from '../../engines/cursor/subagent.js'+import { correlateAgentEvent } from '../../lib/agentEvent.js'import type { QuestionWatcher } from '../../lib/askQuestion.js'import type { AutonomousDeviceService } from '../../lib/autonomous-device/service.js'import type { CommanderMirror } from '../../lib/commander.js'import type { RegisteredSession } from '../../lib/registry.js'import type { SessionInputController } from '../../lib/sessionInput.js'+import type { TurnActivity } from '../../lib/turnActivity.js'import type { SessionNormalizers } from '../transcripts/normalizers.js'export interface CancelDeps {@@ -23,9 +25,18 @@ export interface CancelDeps {stopHeartbeat: (sessionId: string) => voidquestionWatcher: Pick<QuestionWatcher, 'stop'>mirror: Pick<CommanderMirror, 'cancel'>+ turnActivity: Pick<TurnActivity, 'observe' | 'snapshot'>+ /** When each open turn started (the heartbeats'): the WiFi device reads an agent as running by it. */+ turnStartedAt: Map<string, number>+ agentIdFor: (sessionId: string) => string+ /** The app. */+ clients: { send(frame: ReturnType<typeof correlateAgentEvent>): void }}-export function createCancel({ resolve, normalizers, cursorSubagents, input, device, stopHeartbeat, questionWatcher, mirror }: CancelDeps) {+export function createCancel({+ resolve, normalizers, cursorSubagents, input, device, stopHeartbeat, questionWatcher, mirror, turnActivity, turnStartedAt,+ agentIdFor, clients,+}: CancelDeps) {// Web cancel (C-c) interrupts the turn — claude writes no end_turn line to close it, so stop the// heartbeat and mark the turn closed here (mirrors the hosted runtime stopping its heartbeat on cancel). We do// NOT emit turn_ended: the web clears its own dots on cancel, and a turn_ended would fire a device@@ -40,6 +51,13 @@ export function createCancel({ resolve, normalizers, cursorSubagents, input, devstopHeartbeat(sessionId)questionWatcher.stop(sessionId)mirror.cancel(sessionId) // close the device's "Working…" tile (bare done, no recap) — a cancel emits no turn_ended+ // ...but the turn IS over, and every window — not only the one that cancelled — reads it so at once,+ // rather than as working until the 30-second lease runs out and as unknown after (found end to end).+ // An engine that ignores the interrupt says so with its next output, which reads as working again.+ turnStartedAt.delete(sessionId)+ turnActivity.observe(sessionId, 'turn_ended')+ const activity = turnActivity.snapshot(sessionId)+ if (activity) clients.send(correlateAgentEvent({ type: 'agent_activity', payload: { activity } }, sessionId, agentIdFor(sessionId)))return cancelled}return cancelAgentdiff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.mdindex b5e962d2b..c3cd9a850 100644--- a/docs/design/2026-10-03-harnessd.md+++ b/docs/design/2026-10-03-harnessd.md@@ -543,6 +543,12 @@ Found while mapping, and fixed on their own rather than inside a move:received prompts made of two messages, out of order (five messages became two turns). Eachagent's messages are now written one after another, in the order they arrived; they still neverwait for a turn.+ - Part 13, found by the fourth round (`e2e/turns.e2e.ts`): a turn cancelled from the app left every+ other window, and every agents_list, reading the agent as working for thirty seconds and then as+ unknown, because a cancel never told the activity tracker. It reads as idle at once now, still+ with no `turn_ended` (which would recap a killed turn). The fifth round+ (`e2e/questions.e2e.ts`) proves an agent's question end to end for both engines: the dialog reaches+ the window, the answer chosen there is the one the agent gets, and a stale answer types nothing.## Testing