Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions orchestrator/src/gateway/activity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 14 additions & 3 deletions orchestrator/src/gateway/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -172,15 +182,16 @@ 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.
* @param harnessId The harness whose adapter sent the update.
*/
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;
Expand All @@ -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);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions orchestrator/src/gateway/upstream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>((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: {} } : {},
Expand Down
2 changes: 2 additions & 0 deletions orchestrator/src/gateway/upstream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down