From eb39c1482f0a01ea79f6938e71b36361991f2cb6 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 1 Dec 2026 22:29:19 +0000 Subject: [PATCH] Stop a finished call from keeping a thread busy forever A thread stayed busy without end when a tool call was open and no final status came for it. The gateway opened a call on every tool call update that had no final status. The adapter also sends updates without any status for a call that is over, such as the exit of the terminal of a background command or the report of a PostToolUse hook. Such an update after the end of the turn opened the call again, and nothing closed it. The stop button then stayed in every browser until the next turn ended. An update without a status now opens no call. The return of the last prompt on a thread now ends the turn, which also closes a call that got no final status. --- orchestrator/src/gateway/activity.test.ts | 51 +++++++++++++++++++++++ orchestrator/src/gateway/activity.ts | 17 ++++++-- orchestrator/src/gateway/upstream.test.ts | 46 ++++++++++++++++++++ orchestrator/src/gateway/upstream.ts | 2 + 4 files changed, 113 insertions(+), 3 deletions(-) diff --git a/orchestrator/src/gateway/activity.test.ts b/orchestrator/src/gateway/activity.test.ts index 3429d156..e3530748 100644 --- a/orchestrator/src/gateway/activity.test.ts +++ b/orchestrator/src/gateway/activity.test.ts @@ -302,6 +302,57 @@ test('a cycle that ends with a call still open ends anyway', () => { assert.equal(a.speaking('t1'), false); }); +test('an update without a status does not open a finished call again', () => { + const { a, pass } = activity(); + a.observe('t1', toolCall('call_1'), 'claude'); + a.observe( + 't1', + update('tool_call_update', { toolCallId: 'call_1', status: 'completed' }), + 'claude', + ); + a.observe('t1', cycleEnd(), 'claude'); + + // The terminal of a background command exits long after its call completed. + a.observe( + 't1', + update('tool_call_update', { + toolCallId: 'call_1', + _meta: { terminal_exit: { terminal_id: 'call_1', exit_code: null, signal: null } }, + }), + 'claude', + ); + assert.equal(a.speaking('t1'), true); + pass(QUIET); + assert.equal(a.speaking('t1'), false); +}); + +test('an update without a status leaves an open call open', () => { + const { a, pass } = activity(); + a.observe('t1', toolCall('call_1'), 'claude'); + a.observe( + 't1', + update('tool_call_update', { toolCallId: 'call_1', title: 'npm test' }), + 'claude', + ); + for (let i = 0; i < 40; i++) pass(QUIET); + assert.equal(a.speaking('t1'), true); +}); + +test('the end of the prompt ends a turn that left a call open', () => { + const { a, pass, changes, settled } = activity(); + a.begin('t1'); + // The call of a failed sub agent gets no final status. + a.observe('t1', toolCall('call_1'), 'claude'); + a.end('t1'); + assert.equal(a.speaking('t1'), false); + for (let i = 0; i < 40; i++) pass(QUIET); + assert.deepEqual(changes, [ + ['t1', true], + ['t1', false], + ]); + assert.deepEqual(settled, ['t1']); +}); + test('the harness waking the agent starts it talking again, and ends again', () => { const { a, pass, changes, settled } = activity(); a.observe('t1', update('agent_message_chunk'), 'claude'); diff --git a/orchestrator/src/gateway/activity.ts b/orchestrator/src/gateway/activity.ts index cf015b5a..90e31e51 100644 --- a/orchestrator/src/gateway/activity.ts +++ b/orchestrator/src/gateway/activity.ts @@ -132,6 +132,16 @@ export class Activity { this.mark(acpThreadId); } + /** + * Marks a thread as stopped because the last prompt on it has returned. A tool call that got no final status + * counts as over. + * + * @param acpThreadId The adapter's id for the thread. + */ + end(acpThreadId: string): void { + this.stop(acpThreadId); + } + /** * Reads one live `session/update` for what it says about the agent. * @@ -172,7 +182,8 @@ export class Activity { * the agent having stopped. * * A call that starts background work is dropped from the open set, because - * the agent does not wait for it. + * the agent does not wait for it. An update without a status opens no call, because the adapter also sends such + * updates for a call that is over, e.g., the exit of the terminal of a background command. * * @param acpThreadId The adapter's id for the thread. * @param call The tool call update. @@ -180,7 +191,7 @@ export class Activity { */ private track( acpThreadId: string, - call: ToolCallUpdate & { toolCallId?: string; status?: string }, + call: ToolCallUpdate & { sessionUpdate?: string; toolCallId?: string; status?: string }, harnessId: HarnessId, ): void { if (!call.toolCallId) return; @@ -190,7 +201,7 @@ export class Activity { state.open.delete(call.toolCallId); return; } - state.open.add(call.toolCallId); + if (call.sessionUpdate === UPDATE_KIND.toolCall || call.status) state.open.add(call.toolCallId); } /** diff --git a/orchestrator/src/gateway/upstream.test.ts b/orchestrator/src/gateway/upstream.test.ts index 5ccbab5e..f26ac1c0 100644 --- a/orchestrator/src/gateway/upstream.test.ts +++ b/orchestrator/src/gateway/upstream.test.ts @@ -2647,6 +2647,52 @@ test('a prompt held open for background work is not the agent still talking', as await prompt; }); +test('a prompt that returns ends a turn that left a tool call open', async () => { + let finish!: (result: unknown) => void; + const held = new Promise((resolve) => { + finish = resolve; + }); + const adapter = new FakeAdapter((msg) => { + if (msg.method === 'initialize') return { protocolVersion: 1, agentCapabilities: {} }; + if (msg.method === 'session/prompt') return held; + return {}; + }); + fakeDocker(adapter); + + const up = manager.upstream('s1'); + await up.ensureStarted(); + const watcher = fakeHandle(1, 'acp-gone'); + up.attach(watcher); + + const prompt = up.forwardRequest('session/prompt', { + sessionId: 'acp-gone', + prompt: [{ type: 'text', text: 'run the tests' }], + }); + await expect.poll(() => up.threadState('acp-gone').speaking).toBe(true); + + // The call gets no final status, and the turn sends no end-of-cycle update. + adapter.notify('session/update', { + sessionId: 'acp-gone', + update: { + sessionUpdate: 'tool_call', + toolCallId: 'toolu_1', + title: 'npm test', + status: 'in_progress', + _meta: { claudeCode: { toolName: 'Bash' } }, + }, + }); + // A request and its answer pass the adapter after the update, so the gateway has read the update. + await up.forwardRequest('session/set_mode', { sessionId: 'acp-gone', modeId: 'default' }); + + finish({ stopReason: 'end_turn' }); + await prompt; + assert.equal(up.threadState('acp-gone').speaking, false); + const told = watcher.told.filter( + (params) => typeof (params as { speaking?: unknown }).speaking === 'boolean', + ) as TurnStateParams[]; + assert.equal(told.at(-1)?.speaking, false); +}); + test("the adapter's own end-of-cycle update ends the turn without waiting", async () => { const adapter = new FakeAdapter((msg) => msg.method === 'initialize' ? { protocolVersion: 1, agentCapabilities: {} } : {}, diff --git a/orchestrator/src/gateway/upstream.ts b/orchestrator/src/gateway/upstream.ts index ff0f7a5d..1b7c0eaa 100644 --- a/orchestrator/src/gateway/upstream.ts +++ b/orchestrator/src/gateway/upstream.ts @@ -1228,6 +1228,8 @@ export class UpstreamBox implements AdapterHost { if (isPrompt) { this.setTurnActive(thread, false); this.downstreams.endPrompt(params); + // A queued prompt keeps the agent at work. + if (!this.downstreams.isPrompting(thread)) this.activity.end(thread); // The settle callback of the Activity sends the idle announcement, because a prompt can stay open until the // turn's background subagents settle. }