feat(gateway): wire safe-point runtime cancellation for agent.cancel

This commit is contained in:
William Valentin
2026-02-13 08:51:14 -08:00
parent 9f81c01603
commit 46099664f0
7 changed files with 182 additions and 26 deletions
+37
View File
@@ -73,6 +73,43 @@ describe('NativeAgent', () => {
expect(mockSession.addMessage).toHaveBeenNthCalledWith(1, { role: 'user', content: 'Hi' });
expect(mockSession.addMessage).toHaveBeenNthCalledWith(2, { role: 'assistant', content: 'Hello!' });
});
it('supports cancellation during single-turn model wait', async () => {
let release!: () => void;
const blocked = new Promise<void>((resolve) => {
release = resolve;
});
const mockClient: ModelClient = {
chat: vi.fn(async () => {
await blocked;
return {
content: 'Late response',
stopReason: 'end_turn',
usage: { inputTokens: 10, outputTokens: 5 },
} satisfies ChatResponse;
}),
};
const agent = new NativeAgent({
modelClient: mockClient,
systemPrompt: 'You are helpful.',
});
const pending = agent.process('Please wait');
await new Promise<void>((resolve) => queueMicrotask(resolve));
expect(agent.isCancellable()).toBe(true);
agent.cancel();
release();
const response = await pending;
expect(response).toBe('Operation cancelled by user.');
expect(agent.isCancellable()).toBe(false);
const history = agent.getHistory();
expect(history[history.length - 1]).toEqual({ role: 'assistant', content: 'Operation cancelled by user.' });
});
});
// Simple test tool