fix: abort model retries immediately on user cancellation

This commit is contained in:
William Valentin
2026-02-18 11:21:57 -08:00
parent a76c5ae346
commit 55cde541ea
6 changed files with 177 additions and 3 deletions
+32
View File
@@ -476,6 +476,38 @@ describe('setClient and labels', () => {
expect(router.isTierStrict('default')).toBe(true);
});
it('requestAbort interrupts retry loop before fallback chain', async () => {
const primary = {
chat: vi.fn().mockRejectedValue(new Error('temporary failure')),
} as unknown as ModelClient;
const fallback = {
chat: vi.fn().mockResolvedValue({
content: 'fallback',
stopReason: 'end_turn',
usage: { inputTokens: 1, outputTokens: 1 },
}),
} as unknown as ModelClient;
const router = new ModelRouter({
default: primary,
fallbackChain: [fallback],
retryConfig: {
maxRetries: 3,
initialDelayMs: 80,
backoffMultiplier: 1,
maxDelayMs: 80,
nonRetryablePatterns: [],
},
});
const run = router.chat({ messages: [{ role: 'user', content: 'hi' }] });
setTimeout(() => router.requestAbort(), 10);
await expect(run).rejects.toMatchObject({ name: 'AbortError' });
expect(primary.chat).toHaveBeenCalledTimes(1);
expect(fallback.chat).not.toHaveBeenCalled();
});
it('setOnTierChange does not replace existing listeners', () => {
const router = new ModelRouter({
default: { chat: vi.fn() } as unknown as ModelClient,