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.

fix(cli): a Command Code turn with no tool never showed as working

0xken158b71c0a3460e
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index f0aade3ba..a1e75bf62 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -1147,6 +1147,19 @@ async function runForeground(token: string): Promise<void> {
const engine = registry.resolve(sessionId)?.engine
backend.sendCommander({ type: 'commander_event', agentId: agentIdFor(sessionId), dbSessionId: sessionId, payload: { kind: 'error', text: deviceErrorText(message, engine) } })
},
+ // Command Code writes its transcript only once the turn is OVER, so a turn that calls no tool has
+ // nothing to announce it: measured on 1.28.4, "hi" produced turn_started and turn_ended 1ms apart
+ // and neither web nor device ever showed the agent working. Our own paste is the one moment a turn
+ // is known to have started — and the only one that also knows the text.
+ onSubmitted: (id, content) => {
+ // `id` is whatever the caller addressed the agent by — in the inject path it is the AGENT id, not
+ // the session id, and the normalizer map is keyed by session. Resolve before looking anything up.
+ const session = registry.resolve(id)
+ if (session?.engine !== 'commandcode' || !session.sessionId) return
+ const normalizer = commandcodeNormalizers.get(session.sessionId)
+ if (!normalizer) return
+ emitSessionEvents(session.sessionId, normalizer.openTurn(content))
+ },
})
/**
* agy only: close a turn whose final `Stop` never came.
diff --git a/cli/src/engines/README.md b/cli/src/engines/README.md
index 238d7d71a..7ca3d7343 100644
--- a/cli/src/engines/README.md
+++ b/cli/src/engines/README.md
@@ -300,6 +300,24 @@ conversation reports the last turn as still open and nothing is ever coming to c
`turn_heartbeat` every second, indefinitely). Its pane knows — `? for shortcuts` when idle,
`esc to cancel` while busy — so attach reads the pane once and closes the folded turn.
+**When the engine tells you nothing, use the signal you own.** Command Code writes its transcript
+ONCE, at the end of the turn — measured by watching the file: 1 line at t=0, then 3 lines in a single
+step at t=4.3s of a 5s turn. There is nothing to stream, and nothing announces the start either: its
+`PreToolUse` hook only fires if the turn uses a TOOL, so a plain question opened and closed a turn 1ms
+apart and neither web nor device ever showed it working.
+
+Harness's own paste is the missing signal — it is the one moment a turn is known to have begun, and
+unlike a hook it also knows the TEXT. Two traps came with it:
+
+- the id `SessionInput` hands its callbacks is the AGENT id, while the normalizer map is keyed by
+ SESSION id: the wrong key made the fix a silent no-op that logged exactly like the bug;
+- the flushed user line then arrives late and the shared rule ("a user line starts a turn") closed the
+ live turn and opened a second that died 1ms later. Remember what the turn was opened with and
+ recognise that line as the same turn.
+
+A turn the user types directly into the pane still has no signal until it calls a tool. That limit is
+the engine's, and it is better stated than papered over.
+
**A dialog you can read is not a dialog you can judge.** Copilot names the SUBJECT of a permission
prompt above the question, not in it: "Copilot is attempting to access the following URL:" over a
boxed value, then "Do you want to allow this access?" with a bare "Yes" as option 1. The shared parser
diff --git a/cli/src/engines/commandcode/normalizer.spec.ts b/cli/src/engines/commandcode/normalizer.spec.ts
index 07357b307..edfd8033f 100644
--- a/cli/src/engines/commandcode/normalizer.spec.ts
+++ b/cli/src/engines/commandcode/normalizer.spec.ts
@@ -185,3 +185,53 @@ describe('Command Code normalizer — run-error records', () => {
expect(commandCodeRunErrorSummary('Insufficient credits')).toBe('Insufficient credits')
})
})
+
+describe('opening a turn that calls no tool', () => {
+ it('carries the text we pasted, so the prompt renders at once', () => {
+ // PreToolUse only fires when the turn uses a tool. Measured on 1.28.4, "hi" calls nothing, the
+ // transcript is flushed only at the end, and the turn opened and closed 1ms apart with no working
+ // state anywhere. Our own injection is the other opener — and it knows the text.
+ const normalizer = new CommandCodeNormalizer('live')
+ const opened = normalizer.openTurn('hi')
+ expect(opened).toEqual([{ type: 'turn_started', payload: { userMessage: 'hi' } }])
+ expect(normalizer.turnOpen).toBe(true)
+ })
+
+ it('is idempotent: a tool call after the paste must not open a second turn', () => {
+ const normalizer = new CommandCodeNormalizer('live')
+ expect(normalizer.openTurn('hi')).toHaveLength(1)
+ expect(normalizer.openTurn()).toEqual([])
+ })
+})
+
+describe('the flush that arrives after we opened the turn', () => {
+ // The real shape, copied from a live transcript: `meta.source` is what separates a typed prompt from
+ // a tool_result echo, and a line without it is not recognised as a prompt at all.
+ const userLine = (text: string) => JSON.stringify({
+ type: 'message', id: '26fbb7e5', parentId: null, timestamp: '2026-08-20T15:20:13.675Z',
+ message: {
+ role: 'user',
+ content: [{ type: 'text', text }],
+ meta: { source: 'user', createdAt: 1787239210783, messageId: '8195243f-836a-4562-9421-72833e10e9df' },
+ },
+ })
+
+ it('does not rotate the turn we already opened with the same prompt', () => {
+ // Command Code writes the user AND assistant lines together at the END of the turn. The shared rule
+ // "a user line starts a turn" then closed the live turn and opened a second one that died 1ms later
+ // — measured as turn_ended 2050ms followed by turn_started/turn_ended 1ms apart.
+ const normalizer = new CommandCodeNormalizer('live')
+ expect(normalizer.openTurn('say hello in five words')).toHaveLength(1)
+ const events = normalizer.ingest(userLine('say hello in five words'))
+ expect(events.some((e) => e.type === 'turn_started')).toBe(false)
+ expect(events.some((e) => e.type === 'turn_ended')).toBe(false)
+ expect(normalizer.turnOpen).toBe(true)
+ })
+
+ it('still opens a turn for a prompt we did NOT send', () => {
+ const normalizer = new CommandCodeNormalizer('live')
+ normalizer.openTurn('say hello in five words')
+ const events = normalizer.ingest(userLine('something the user typed instead'))
+ expect(events.some((e) => e.type === 'turn_started')).toBe(true)
+ })
+})
diff --git a/cli/src/engines/commandcode/normalizer.ts b/cli/src/engines/commandcode/normalizer.ts
index 8532eae33..83e4f05ba 100644
--- a/cli/src/engines/commandcode/normalizer.ts
+++ b/cli/src/engines/commandcode/normalizer.ts
@@ -186,13 +186,19 @@ function reshapeAll(rawLines: string[]): string[] {
}
export class CommandCodeNormalizer implements EngineNormalizer {
+ /** The prompt `openTurn` was called with, until the transcript catches up with it. */
+ private openedWith: string | null = null
private readonly state: TurnState = newTurnState()
constructor(private readonly mode: 'live' | 'replay' = 'live') {}
get turnOpen(): boolean { return this.state.turnOpen }
- closeTurn(): void { this.state.turnOpen = false; this.state.pendingTools.clear() }
+ closeTurn(): void {
+ this.openedWith = null
+ this.state.turnOpen = false
+ this.state.pendingTools.clear()
+ }
/**
* Open a turn from OUTSIDE the transcript, driven by the PreToolUse hook.
@@ -203,15 +209,26 @@ export class CommandCodeNormalizer implements EngineNormalizer {
* by the time anything is readable the turn is already over, and the device went idle → recap with no
* "working" in between.
*
- * Idempotent because PreToolUse fires per tool call: only the first one in a turn opens it. The user
- * text is not available yet — the transcript has not been written — so the ask arrives later with the
- * recap, which is where the device reads it from anyway.
+ * Idempotent because PreToolUse fires per tool call: only the first one in a turn opens it.
+ *
+ * PreToolUse is not enough on its own, though: it only fires when the turn USES A TOOL. Measured on
+ * 1.28.4, a plain question ("hi") calls nothing, so nothing opened the turn and the flush at the end
+ * emitted turn_started and turn_ended 1ms apart — the device jumped from idle to recap and the web
+ * pane showed no working state at all.
+ *
+ * The other opener is our own injection: when Harness pastes a message into the pane it knows a turn
+ * has begun, and unlike PreToolUse it also knows the TEXT, so the prompt renders immediately instead
+ * of arriving later with the recap. A turn the user types directly into the pane still has neither
+ * signal until it calls a tool — Command Code offers nothing else to read.
*/
- openTurn(): LiveEvent[] {
+ openTurn(userMessage = ''): LiveEvent[] {
if (this.state.turnOpen) return []
this.state.turnOpen = true
this.state.pendingTools.clear()
- return [{ type: 'turn_started', payload: { userMessage: '' } }]
+ // Remember the text so the flush that arrives minutes later is recognised as THIS turn rather than
+ // a new one — see `ingest`.
+ this.openedWith = userMessage || null
+ return [{ type: 'turn_started', payload: { userMessage } }]
}
ingest(line: string): LiveEvent[] {
@@ -224,7 +241,21 @@ export class CommandCodeNormalizer implements EngineNormalizer {
return [{ type: 'turn_ended', payload: {} }]
}
const reshaped = reshapeLine(line)
- return reshaped ? lineToEvents(reshaped, this.state) : []
+ if (!reshaped) return []
+ const events = lineToEvents(reshaped, this.state)
+ // Command Code flushes the user AND assistant lines together when the turn ENDS. If we already
+ // opened this turn from the paste, that flushed user line is the same prompt arriving late — and
+ // the shared rule ("a user line rotates the turn") would close the live turn and open a second one
+ // that dies 1ms later. Measured: turn_started, turn_ended 2050ms, then turn_started/turn_ended 1ms.
+ if (this.openedWith !== null) {
+ const started = events.find((event) => event.type === 'turn_started')
+ const text = started?.type === 'turn_started' ? started.payload.userMessage : null
+ if (text !== null && text.trim() === this.openedWith.trim()) {
+ this.openedWith = null
+ return events.filter((event) => event.type !== 'turn_started' && event.type !== 'turn_ended')
+ }
+ }
+ return events
}
finishReplay(): LiveEvent[] {
diff --git a/cli/src/lib/sessionInput.ts b/cli/src/lib/sessionInput.ts
index 95ef6534b..d2d3b558a 100644
--- a/cli/src/lib/sessionInput.ts
+++ b/cli/src/lib/sessionInput.ts
@@ -82,6 +82,12 @@ export interface SessionInputDeps {
sendKey: (terminalTarget: string, key: string) => Promise<boolean | TerminalActionResult>
capture?: (terminalTarget: string) => Promise<string | null>
onError: (sessionId: string, message: string) => void
+ /**
+ * A message was accepted by the pane. Not every engine needs this — most announce the turn
+ * themselves — but an engine that only writes its transcript once the turn is OVER has no other
+ * moment at which a turn is known to have started, and this one is exact: we sent it.
+ */
+ onSubmitted?: (sessionId: string, content: string) => void
}
function fingerprint(content: string): string {
@@ -304,6 +310,7 @@ export class SessionInputController {
state.retries = 0
state.observes = 0
state.ambiguousDispatch = typeof delivery !== 'boolean' && delivery.dispatch === 'possibly_executed'
+ this.deps.onSubmitted?.(sessionId, content)
this.armSubmitCheck(sessionId, session, state)
}