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.

feat(question): tell every client when a question is answered elsewhere

0xdiego25ec3dca16e162
diff --git a/cli/src/cable/cableHost.ts b/cli/src/cable/cableHost.ts
index d08b2ecd8..5ac7f978f 100644
--- a/cli/src/cable/cableHost.ts
+++ b/cli/src/cable/cableHost.ts
@@ -600,3 +600,20 @@ export function cableQuestionFor(
if (typeof requestId !== 'string' || !requestId || !Array.isArray(questions)) return null
return { agentId: frame.agentId, requestId, questions }
}
+
+/**
+ * Translate one device-bound `commander_question_close` into the dial's `question.close`.
+ *
+ * The other half of `cableQuestionFor`. A question is a TUI dialog in a pane, not a server-side object,
+ * so "somebody answered it" reaches this daemon as "the dialog is no longer on the pane" — and every
+ * client still drawing it has to be told, or it sits waiting for an answer nobody can give any more.
+ */
+export function cableQuestionCloseFor(
+ frame: { type?: string; agentId?: string; payload?: { requestId?: string } },
+): { agentId: string; requestId: string } | null {
+ if (frame.type !== 'commander_question_close' || !frame.agentId) return null
+ const requestId = frame.payload?.requestId
+ if (typeof requestId !== 'string' || !requestId) return null
+ return { agentId: frame.agentId, requestId }
+}
+
diff --git a/cli/src/cable/cableSession.ts b/cli/src/cable/cableSession.ts
index 072071c73..2f4baddfc 100644
--- a/cli/src/cable/cableSession.ts
+++ b/cli/src/cable/cableSession.ts
@@ -28,7 +28,7 @@ import { FirmwareTransfer } from './fwPush.js'
import { SerialLink, findDialPort } from './serial.js'
/** Bumped when the VOCABULARY changes. Separate from the frame version, which is the envelope. */
-export const CABLE_PROTO_VERSION = 2
+export const CABLE_PROTO_VERSION = 3 // 3: + question.close (a question answered on another client)
const PING_EVERY_MS = 5_000
/** No bytes of any kind for this long → the handle is dead. Longer than the dial's own 15 s window, so a
@@ -1020,6 +1020,18 @@ export class CableSession {
async question(agentId: string, id: string, questions: unknown): Promise<void> {
await this.send({ t: 'question', agentId, id, questions })
}
+
+ /**
+ * That question is no longer waiting — answered in the app, or in the pane by hand.
+ *
+ * Carries the id so a dial showing a DIFFERENT question cannot be closed by a stale message: the two
+ * screens are minutes apart on a slow question, and closing the wrong one loses an answer the user was
+ * halfway through giving.
+ */
+ async questionClose(agentId: string, id: string): Promise<void> {
+ await this.send({ t: 'question.close', agentId, id })
+ }
+
async focusAgent(agentId: string): Promise<void> {
this.dialFocus = agentId
await this.send({ t: 'focus', agentId })
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 758206f91..f99033bd6 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -28,7 +28,8 @@ import { sqlitePreflightMessage } from './lib/sqliteAvailability.js'
import { warmLoginShellEnvironment } from './lib/loginShellEnv.js'
import { ensureUtf8Locale } from './lib/childLocale.js'
import { CableSession } from './cable/cableSession.js'
-import { DaemonCableHost, cableEventFor, cableQuestionFor } from './cable/cableHost.js'
+import { DaemonCableHost, cableEventFor, cableQuestionFor, cableQuestionCloseFor } from './cable/cableHost.js'
+
import { MachineListCache } from './device/machineList.js'
import { DeviceLink } from './device/deviceLink.js'
import { DeviceFleet } from './device/deviceFleet.js'
@@ -1547,8 +1548,21 @@ async function runForeground(session: AuthSession): Promise<void> {
})
console.log(`[question] ${sid(sessionId)} asking the user · "${preview(shaped[0]?.q ?? '')}" · req=${requestId}`)
},
+ // Answered somewhere else — the app, or the pane by hand. Every client drawing it is told to stop
+ // 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) => {
+ backend.sendCommander({
+ type: 'commander_question_close',
+ agentId: agentIdFor(sessionId),
+ dbSessionId: sessionId,
+ payload: { requestId },
+ })
+ console.log(`[question] ${sid(sessionId)} answered elsewhere · closing on every client · req=${requestId}`)
+ },
})
+
const mirror = new CommanderMirror({
send: (frame) => backend.sendCommander(frame),
sendWeb: (frame) => backend.send(frame), // turn_summary_pending / turn_summary → web indicator
@@ -3113,7 +3127,10 @@ async function runForeground(session: AuthSession): Promise<void> {
// THIS COMPUTER'S cards, by definition — and every one of them belongs to a tile that is on the
// carousel, because the carousel now spans machines. The old guard dropped them whenever the wheel
// was pointed elsewhere, which would now silence this machine's own agents.
+ const close = cableQuestionCloseFor(frame as { type?: string; agentId?: string; payload?: { requestId?: string } })
+ if (close) { void cable.questionClose(close.agentId, close.requestId); return }
const question = cableQuestionFor
+
(frame as { type?: string; agentId?: string; payload?: { requestId?: string; questions?: unknown } })
if (question) { void cable.question(question.agentId, question.requestId, question.questions); return }
const event = cableEventFor(frame as { type?: string; agentId?: string; payload?: { kind?: string; text?: string; recap?: string } })
diff --git a/cli/src/lib/askQuestion.spec.ts b/cli/src/lib/askQuestion.spec.ts
index 59d1a5e2b..0d504cd60 100644
--- a/cli/src/lib/askQuestion.spec.ts
+++ b/cli/src/lib/askQuestion.spec.ts
@@ -250,22 +250,74 @@ describe('QuestionWatcher', () => {
function watcher(captures: string[], opts: { hasDevice?: boolean } = {}): {
seen: Array<{ requestId: string; questions: ReturnType<typeof shapeQuestions> }>
+ gone: string[]
tick: () => Promise<void>
instance: QuestionWatcher
} {
let i = 0
const seen: Array<{ requestId: string; questions: ReturnType<typeof shapeQuestions> }> = []
+ const gone: string[] = []
const instance = new QuestionWatcher({
getSession: () => session,
capture: async () => captures[Math.min(i++, captures.length - 1)],
hasDevice: () => opts.hasDevice !== false,
onQuestion: (_s, requestId, questions) => { seen.push({ requestId, questions }) },
+ onQuestionGone: (_s, requestId) => { gone.push(requestId) },
})
// tick() is private — exercised the way the interval does.
const tick = (): Promise<void> => (instance as unknown as { tick: (s: string) => Promise<void> }).tick('s1')
- return { seen, tick, instance }
+ return { seen, gone, tick, instance }
}
+ it('announces a close once the dialog has really left the pane', async () => {
+ // The whole point of the feature: answered in the app, so every other client has to stop waiting.
+ const w = watcher([fixture('single'), '', ''])
+ await w.tick()
+ expect(w.seen).toHaveLength(1)
+ await w.tick()
+ expect(w.gone).toEqual([]) // one empty capture is not yet evidence
+ await w.tick()
+ expect(w.gone).toEqual([w.seen[0].requestId])
+ })
+
+ it('does NOT close on a single unparseable capture', async () => {
+ // A capture taken mid-repaint reads as no-dialog. Closing on that would pull a LIVE question off the
+ // dial — the bug this feature exists to prevent, inverted, and far harder to notice.
+ const w = watcher([fixture('single'), '', fixture('single'), ''])
+ await w.tick()
+ await w.tick() // the flicker
+ await w.tick() // dialog is back
+ await w.tick() // first real miss
+ expect(w.gone).toEqual([])
+ })
+
+ it('closes an outstanding question when the watcher stops', async () => {
+ // The case that made the whole mechanism look broken on hardware: the turn ends BECAUSE the question
+ // was answered, so stop() lands a second or two after the dialog left — well inside the two-tick
+ // confirmation, which would otherwise swallow the close entirely.
+ const w = watcher([fixture('single')])
+ await w.tick()
+ expect(w.seen).toHaveLength(1)
+ w.instance.stop('s1')
+ expect(w.gone).toEqual([w.seen[0].requestId])
+ })
+
+ it('does not close twice when the dialog was already confirmed gone', async () => {
+ const w = watcher([fixture('single'), '', ''])
+ await w.tick(); await w.tick(); await w.tick()
+ expect(w.gone).toHaveLength(1)
+ w.instance.stop('s1')
+ expect(w.gone).toHaveLength(1)
+ })
+
+ it('says nothing about a question it never announced', async () => {
+
+ const w = watcher(['', '', ''])
+ await w.tick(); await w.tick(); await w.tick()
+ expect(w.gone).toEqual([])
+ })
+
+
it('announces an open dialog in the device question shape', async () => {
const w = watcher([fixture('single')])
await w.tick()
diff --git a/cli/src/lib/askQuestion.ts b/cli/src/lib/askQuestion.ts
index c81cb587a..be68a3efa 100644
--- a/cli/src/lib/askQuestion.ts
+++ b/cli/src/lib/askQuestion.ts
@@ -720,11 +720,29 @@ export interface QuestionWatcherDeps {
hasDevice: () => boolean
/** A dialog is open on screen. Fires ONCE per distinct question (until it changes or closes). */
onQuestion: (sessionId: string, requestId: string, questions: ShapedQuestion[]) => void
+ /**
+ * An announced dialog LEFT the screen — answered somewhere else, or abandoned.
+ *
+ * This is the other half of `onQuestion`, and it was missing: a question answered in the app (or in the
+ * pane by hand) simply stopped being on screen, the watcher forgot it, and every OTHER client went on
+ * showing it. On the dial that is a screen you cannot leave without dismissing a question that no longer
+ * exists. The pane is the only source of truth here — there is no server-side question object — so
+ * "gone from the pane" is the only signal there is.
+ */
+ onQuestionGone?: (sessionId: string, requestId: string) => void
+
/** True while that session's dialog is being keyed by an answer already in flight. */
isDriving?: (sessionId: string) => boolean
}
const POLL_MS = 1500
+// Consecutive empty polls before a question is declared gone.
+//
+// NOT 1. A capture taken mid-repaint parses as no-dialog, and announcing a close on that would yank a
+// live question off the dial's screen — the exact failure this feature exists to prevent, inverted.
+// Two ticks costs 1.5s of delay on a real close and makes a flicker unable to cause one.
+const GONE_TICKS = 2
+
// Amp and codex are here for their PERMISSION prompt, not a question tool — neither has one. That prompt
// is drawn only in the pane and recorded nowhere, so polling the pane is the only way it is ever seen.
// Codex needs no parser of its own: it draws numbered rows under a `Press enter to confirm or esc to
@@ -750,8 +768,11 @@ export function pollsQuestions(engine: AgentEngine): boolean {
export class QuestionWatcher {
private timers = new Map<string, NodeJS.Timeout>()
private last = new Map<string, string>() // sessionId → fingerprint of the announced question
+ private lastId = new Map<string, string>() // sessionId → requestId of the announced question
+ private misses = new Map<string, number>() // sessionId → consecutive polls with no dialog
private readonly blocked = new Map<string, string>()
+
constructor(private readonly deps: QuestionWatcherDeps) {}
/** Poll this session's pane while its turn is open (called on turn_started). */
@@ -764,23 +785,66 @@ export class QuestionWatcher {
this.timers.set(sessionId, setInterval(() => { void this.tick(sessionId) }, POLL_MS))
}
+ /**
+ * Stop polling — and CLOSE any question still outstanding on this session.
+ *
+ * ⚠️ THIS IS THE COMMON CASE, NOT THE EDGE ONE, and leaving it out made the whole close mechanism look
+ * like it did not work. `stop()` is called on turn_ended, and answering the question is precisely what
+ * lets the turn end — so the dialog leaving the pane and the watcher being torn down happen within a
+ * second or two of each other, far inside the two-tick confirmation. Measured on hardware: the answer
+ * landed at ~16:50:19 and the turn ended at 16:50:29 with no close ever announced.
+ *
+ * No confirmation is needed here and none is wanted: the turn is over, so whatever we announced is
+ * definitively not waiting for anybody any more. The same holds for the other callers — an agent that
+ * was removed cannot answer either.
+ */
stop(sessionId: string): void {
const timer = this.timers.get(sessionId)
if (timer) { clearInterval(timer); this.timers.delete(sessionId) }
- this.last.delete(sessionId)
+ const requestId = this.lastId.get(sessionId)
+ this.forget(sessionId)
+ if (requestId) this.deps.onQuestionGone?.(sessionId, requestId)
}
+
stopAll(): void {
for (const timer of this.timers.values()) clearInterval(timer)
this.timers.clear()
this.last.clear()
+ this.lastId.clear()
+ this.misses.clear()
}
/** A device (re)joined: forget what we announced so an open question is pushed again. */
reset(): void {
this.last.clear()
+ this.lastId.clear()
+ this.misses.clear()
}
+ private forget(sessionId: string): void {
+ this.last.delete(sessionId)
+ this.lastId.delete(sessionId)
+ this.misses.delete(sessionId)
+ }
+
+ /**
+ * A poll found no dialog. Announce the close once the miss is CONFIRMED, and only if we announced the
+ * question in the first place — a session nobody was told about has nothing to take back.
+ */
+ private noteGone(sessionId: string): void {
+ this.last.delete(sessionId) // unchanged: a dialog that comes back announces fresh
+ const requestId = this.lastId.get(sessionId)
+ if (!requestId) return
+ const misses = (this.misses.get(sessionId) ?? 0) + 1
+ this.misses.set(sessionId, misses)
+ if (misses < GONE_TICKS) return
+ this.lastId.delete(sessionId)
+ this.misses.delete(sessionId)
+ this.deps.onQuestionGone?.(sessionId, requestId)
+ }
+
+
private async tick(sessionId: string): Promise<void> {
const session = this.deps.getSession(sessionId)
const terminalTarget = session?.agentId || session?.sessionId
@@ -796,21 +860,28 @@ export class QuestionWatcher {
const engine = this.deps.getSession(sessionId)?.engine ?? 'claude'
const view = parseEngineQuestionPane(engine, await this.deps.capture(terminalTarget, CAPTURE_LINES) ?? '')
if (!view || view.kind !== 'question' || !view.question || view.rows.length === 0) {
- this.last.delete(sessionId) // dialog closed (or moved to review) → the next one announces fresh
+ // Dialog closed, or moved to review. Either way it is no longer waiting on anybody, so the clients
+ // showing it are told to stop — see noteGone for why this is not announced on the first miss.
+ this.noteGone(sessionId)
return
}
+ this.misses.delete(sessionId) // a dialog on screen ends any run of misses
const fingerprint = `${view.question}|${view.rows.map((r) => r.label).join('|')}|${view.multi}`
if (this.last.get(sessionId) === fingerprint) return
this.last.set(sessionId, fingerprint)
+
// A pane-derived question has no tool_use id. The key only has to round-trip through the device and
// back (the answer is keyed into the pane, not matched to a tool call), so the question's own text
// serves as both — and the device dedups a repeated push by this id.
- this.deps.onQuestion(sessionId, `q_${hash(sessionId + fingerprint)}`, [{
+ const requestId = `q_${hash(sessionId + fingerprint)}`
+ this.lastId.set(sessionId, requestId)
+ this.deps.onQuestion(sessionId, requestId, [{
key: view.question,
q: view.question,
options: view.rows.map((r) => r.label),
multi: view.multi,
}])
+
}
}