Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { isTransientModelError, retryTransientErrors } from '../src'
3import type { ModelErrorContext } from '../src'
4
5afterEach(() => {
6 vi.useRealTimers()
7 vi.restoreAllMocks()
8})
9
10const context = (message: string, retries = 0): ModelErrorContext => ({
11 // The policy reads only the error, the retries, the partial flag, and the
12 // signal.
13 session: undefined as never,
14 adapter: undefined as never,
15 model: 'mock/test-model',
16 operationId: 'op-1',
17 error: { message },
18 retries,
19 partial: false,
20 signal: new AbortController().signal,
21})
22
23describe('isTransientModelError', () => {
24 it.each([
25 'Overloaded',
26 'Rate limit exceeded',
27 'Too Many Requests',
28 '429 status code',
29 '500 Internal Server Error',
30 '502 Bad Gateway',
31 '503 Service Unavailable',
32 '504 Gateway Timeout',
33 'network error',
34 'connection reset by peer',
35 'socket hang up',
36 'fetch failed',
37 'request timed out',
38 'terminated',
39 'Provider finish_reason: error',
40 ])('treats "%s" as transient', (message) => {
41 expect(isTransientModelError({ message })).toBe(true)
42 })
43
44 it.each([
45 'prompt is too long: 250000 tokens > 200000 maximum',
46 'Invalid API key',
47 'Provider finish_reason: error_quota',
48 'Provider finish_reason: content_filter',
49 ])('treats "%s" as terminal', (message) => {
50 expect(isTransientModelError({ message })).toBe(false)
51 })
52
53 it('reads the error code too', () => {
54 expect(
55 isTransientModelError({ message: 'failed', code: 'rate_limit_exceeded' }),
56 ).toBe(true)
57 })
58})
59
60describe('retryTransientErrors', () => {
61 it('waits base * 2^retries, times the jitter, then retries', async () => {
62 vi.useFakeTimers()
63 vi.spyOn(Math, 'random').mockReturnValue(1)
64 const policy = retryTransientErrors({ maxRetries: 3, baseDelayMs: 1_000 })
65
66 let answer: string | undefined = 'pending'
67 void policy(context('503 Service Unavailable', 1)).then((value) => {
68 answer = value
69 })
70 await vi.advanceTimersByTimeAsync(1_999)
71 expect(answer).toBe('pending')
72 await vi.advanceTimersByTimeAsync(1)
73 expect(answer).toBe('retry')
74 })
75
76 it('uses the smallest jitter of 0.75', async () => {
77 vi.useFakeTimers()
78 vi.spyOn(Math, 'random').mockReturnValue(0)
79 const policy = retryTransientErrors({ baseDelayMs: 2_000 })
80
81 let answer: string | undefined = 'pending'
82 void policy(context('overloaded')).then((value) => {
83 answer = value
84 })
85 await vi.advanceTimersByTimeAsync(1_499)
86 expect(answer).toBe('pending')
87 await vi.advanceTimersByTimeAsync(1)
88 expect(answer).toBe('retry')
89 })
90
91 it('gives up after maxRetries, and at once for a terminal error', async () => {
92 const policy = retryTransientErrors({ maxRetries: 2, baseDelayMs: 1 })
93
94 expect(await policy(context('overloaded', 2))).toBeUndefined()
95 expect(await policy(context('Invalid API key'))).toBeUndefined()
96 })
97
98 it('stops the wait when the turn is cancelled', async () => {
99 const policy = retryTransientErrors({ baseDelayMs: 60_000 })
100 const controller = new AbortController()
101 const answer = policy({
102 ...context('overloaded'),
103 signal: controller.signal,
104 })
105
106 controller.abort()
107
108 expect(await answer).toBeUndefined()
109 })
110
111 it('takes its own transient check', async () => {
112 const policy = retryTransientErrors({
113 baseDelayMs: 1,
114 isTransient: (error) => error.message.includes('[retry-me]'),
115 })
116
117 expect(await policy(context('boom [retry-me]'))).toBe('retry')
118 expect(await policy(context('overloaded'))).toBeUndefined()
119 })
120})