Skip to content

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

A cancelled turn settles idle after the aborted tool's late output, not unknown for good (#850)

Dee59a740b1c057
diff --git a/cli/e2e/harness/fakeEngine.mjs b/cli/e2e/harness/fakeEngine.mjs
index 09da02bc5..e54927dd6 100644
--- a/cli/e2e/harness/fakeEngine.mjs
+++ b/cli/e2e/harness/fakeEngine.mjs
@@ -14,7 +14,8 @@
// 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, `!ask` asks the person which drink they would like in the engine's own
+// open until the next prompt, `!holdtool <command>` leaves it open with that tool still running (an
+// interrupt then writes the tool's aborted output before the turn's end, as the CLIs do), `!ask` asks the person which drink they would like in the engine's own
// dialog and answers with their choice, `!permit <command>` asks permission to run a command the way
// the engine does and runs it only if allowed, `!flood <KiB>` prints that much to the terminal, in
// numbered lines, the way a build log or a long diff does, `!clear` starts a new conversation in the
@@ -528,6 +529,8 @@ export async function run(engine, config = {}, { native = false } = {}) {
let turn = 0
let open = null
+ // The tool `!holdtool` left running: an interrupt flushes its output, marked aborted, before the turn ends.
+ let runningTool = 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.
@@ -955,6 +958,7 @@ export async function run(engine, config = {}, { native = false } = {}) {
await runHooks('SessionStart', { source: 'clear' })
return
}
+ runningTool = null
if (open) await finish('(interrupted by a new prompt)')
// Both CLIs run their UserPromptSubmit hooks on every prompt, before the prompt is taken: notify.mjs
// sends it through the same door as SessionStart, the catch hook that re-registers a session whose
@@ -1018,6 +1022,15 @@ export async function run(engine, config = {}, { native = false } = {}) {
await new Promise((resolve) => setTimeout(resolve, 300))
}
if (directive?.[1] === 'hold') return
+ if (directive?.[1] === 'holdtool') {
+ // A tool still running when the person interrupts. Both CLIs write its output, marked aborted, only
+ // AFTER the interrupt and just before the turn's end: what a real Codex 0.160 did in daemon QA.
+ const id = `call_${turn}`
+ if (engine === 'claude') claude({ type: 'assistant', message: { id: `msg_${turn}_u`, role: 'assistant', model: config.claudeModel ?? 'claude-opus-5-5', content: [{ type: 'tool_use', id, name: 'Bash', input: { command: directive[2] } }], stop_reason: 'tool_use' } })
+ else codex('response_item', { type: 'function_call', call_id: id, name: 'exec_command', arguments: JSON.stringify({ cmd: directive[2] }) })
+ runningTool = id
+ return
+ }
if (directive?.[1] === 'permitnext') {
// The turn goes on; its request arrives with the next paste, between it and its Enter.
armed = { command: directive[2] || 'printf hi', turn }
@@ -1273,6 +1286,11 @@ export async function run(engine, config = {}, { native = false } = {}) {
// Ctrl-C ends a running turn, and empties the composer either way.
buffer = ''
if (open) {
+ if (runningTool) {
+ if (engine === 'claude') claude({ type: 'user', message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: runningTool, content: '[Request interrupted by user for tool use]', is_error: true }] } })
+ else codex('response_item', { type: 'function_call_output', call_id: runningTool, output: 'aborted by user' })
+ runningTool = null
+ }
if (engine === 'claude') claude({ type: 'user', message: { role: 'user', content: [{ type: 'text', text: '[Request interrupted by user]' }] } })
else codex('event_msg', { type: 'turn_aborted', turn_id: open })
open = null
diff --git a/cli/e2e/turns.e2e.ts b/cli/e2e/turns.e2e.ts
index 34bcc9a3a..592d23f79 100644
--- a/cli/e2e/turns.e2e.ts
+++ b/cli/e2e/turns.e2e.ts
@@ -4,6 +4,7 @@
*/
import { mkdirSync } from 'node:fs'
import { join } from 'node:path'
+import { WORK_EVIDENCE_MS } from '../src/lib/turnActivity.js'
import { afterEach, describe, expect, it, onTestFailed } from 'vitest'
import { LocalClient, type Frame } from './harness/client.js'
import { IsolatedDaemon, until } from './harness/daemon.js'
@@ -65,6 +66,25 @@ describe('how turns end', () => {
client.close()
})
+ it.each(engines)('%s: a turn cancelled while a tool runs settles idle after the aborted tool\'s late output, not unknown for good', async (engine) => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const agent = await create(d, client, engine, `cancel-tool-${engine}`)
+ const started = client.next(isTurn('turn_started', agent.id), 30_000, 'turn_started')
+ client.send('message', { agentId: agent.id, content: '!holdtool sleep 45' })
+ await started
+ await until('the agent to read as working', async () => (await row(client, agent.id))?.activity?.state === 'working' || null, 15_000, 250)
+ // The CLI writes the aborted tool's output only after the interrupt, and it reads as work for one
+ // lease. With no turn open nothing probes the agent again: it stuck at unknown until the next prompt
+ // (a real Codex 0.160, found by daemon QA). It must settle idle once that late evidence runs out.
+ const late = client.next(isTurn('tool_end', agent.id), 30_000, 'the aborted tool\'s late output')
+ client.send('cancel', { agentId: agent.id })
+ await late
+ await until('the agent to settle idle', async () => (await row(client, agent.id))?.activity?.state === 'idle' || null, WORK_EVIDENCE_MS + 20_000, 500)
+ await turn(client, agent.id, 'after the cancel')
+ client.close()
+ }, 150_000)
+
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)
diff --git a/cli/src/lib/turnActivity.spec.ts b/cli/src/lib/turnActivity.spec.ts
index 699a98005..7e99f3750 100644
--- a/cli/src/lib/turnActivity.spec.ts
+++ b/cli/src/lib/turnActivity.spec.ts
@@ -35,6 +35,18 @@ describe('verified turn activity', () => {
t.activity.observe('s', 'turn_ended')
expect(t.frame()?.state).toBe('idle')
})
+ it('settles an expired claim idle when no turn is open, since nothing probes a closed turn', async () => {
+ // A cancel closes the turn and reads idle; the aborted tool's output lands after it and reads as work.
+ const t = setup(); t.runtime({ key: 'agent:session:pid:start', turnOpen: false })
+ t.activity.observe('s', 'turn_ended'); expect(t.frame()?.state).toBe('idle')
+ t.activity.observe('s', 'tool_end'); expect(t.frame()?.state).toBe('working')
+ t.advance(WORK_EVIDENCE_MS - 1); expect(t.frame()?.state).toBe('working')
+ t.advance(1); expect(t.frame()?.state).toBe('idle')
+ // Fresh evidence still reads as work, and an open turn still expires into unknown for the probe.
+ t.activity.observe('s', 'text_delta'); expect(t.frame()?.state).toBe('working')
+ t.runtime({ key: 'agent:session:pid:start', turnOpen: true })
+ t.advance(); expect(t.frame()?.state).toBe('unknown')
+ })
it.each(['turn_started', 'text_delta', 'thinking_delta', 'thinking_title', 'tool_start', 'tool_end', 'subagent_finished'])(
'renews evidence for live %s, but not a replay of it', type => {
const t = setup(); t.activity.observe('s', type)
diff --git a/cli/src/lib/turnActivity.ts b/cli/src/lib/turnActivity.ts
index ca842b37b..721fdb9d2 100644
--- a/cli/src/lib/turnActivity.ts
+++ b/cli/src/lib/turnActivity.ts
@@ -42,7 +42,12 @@ export class TurnActivity {
entry = { runtime: runtime.key, state: runtime.turnOpen ? 'unknown' : 'idle', revision: ++this.revision, until: 0 }
this.entries.set(sessionId, entry)
}
- if (entry.state === 'working' && this.now() >= entry.until) this.set(entry, 'unknown')
+ // A Working claim whose evidence ran out reads as unknown while a turn is open: the heartbeat probes
+ // open turns and settles it. With no turn open nothing probes it again, so unknown stuck until the
+ // next prompt. That is what a cancel left when the aborted tool's output landed after it and read as
+ // work (a real Codex 0.160, found by daemon QA). The transcript says no turn is open and nothing
+ // renewed the claim: idle.
+ if (entry.state === 'working' && this.now() >= entry.until) this.set(entry, runtime.turnOpen ? 'unknown' : 'idle')
return entry
}
private set(entry: Entry, state: ActivityState): void {