|
| 1 | +// Engine-level checks of when a failed turn is retried automatically. Every case drives the real |
| 2 | +// `Engine#runBotTurn` with a fake Agent and counts how many times the Agent was invoked. |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { test } from 'node:test'; |
| 5 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 6 | +import { tmpdir } from 'node:os'; |
| 7 | +import { join } from 'node:path'; |
| 8 | +import { Store } from '../miniapp/node/store.mjs'; |
| 9 | +import { MemoryStore } from '../miniapp/node/memory.mjs'; |
| 10 | +import { Engine } from '../miniapp/node/engine.mjs'; |
| 11 | +import { setLocale } from '../miniapp/node/i18n.mjs'; |
| 12 | + |
| 13 | +/** Three short retries: a turn that is retried every time runs 4 times. */ |
| 14 | +const SHORT_DELAYS = [1, 1, 1]; |
| 15 | + |
| 16 | +async function fixture(t, run) { |
| 17 | + const dir = await mkdtemp(join(tmpdir(), 'bot-turn-retry-')); |
| 18 | + const store = new Store(dir); |
| 19 | + const memory = new MemoryStore(dir); |
| 20 | + await store.init(); |
| 21 | + await memory.init(); |
| 22 | + setLocale('en'); |
| 23 | + const engine = new Engine({ store, memory, logger: { warn() {} }, broadcast() {}, retryDelaysMs: SHORT_DELAYS }); |
| 24 | + const calls = { count: 0 }; |
| 25 | + engine.agent = { |
| 26 | + async run(input, options) { |
| 27 | + calls.count += 1; |
| 28 | + return run(calls.count, input, options); |
| 29 | + }, |
| 30 | + }; |
| 31 | + t.after(async () => { |
| 32 | + engine.dispose(); |
| 33 | + await store.writeChain; |
| 34 | + await rm(dir, { recursive: true, force: true }); |
| 35 | + }); |
| 36 | + const bot = store.state.bots[0]; |
| 37 | + const chatId = `bot:${bot.id}`; |
| 38 | + const turn = () => engine.runBotTurn({ bot, chatId, text: 'Hello', kind: 'private' }); |
| 39 | + return { store, engine, calls, turn, chatId }; |
| 40 | +} |
| 41 | + |
| 42 | +function agentError(fields) { |
| 43 | + return Object.assign(new Error(fields.message), fields); |
| 44 | +} |
| 45 | + |
| 46 | +test('a thrown AGENT_OUTCOME_UNKNOWN with retryable:false is not retried even if its text names a timeout', async (t) => { |
| 47 | + const { calls, turn } = await fixture(t, () => { |
| 48 | + throw agentError({ code: 'AGENT_OUTCOME_UNKNOWN', retryable: false, message: 'connect ETIMEDOUT 10.0.0.1:443' }); |
| 49 | + }); |
| 50 | + const result = await turn(); |
| 51 | + assert.equal(calls.count, 1); |
| 52 | + assert.equal(result.status, 'error'); |
| 53 | +}); |
| 54 | + |
| 55 | +test('a thrown error with retryable:false is not retried even if its text says "rate limit"', async (t) => { |
| 56 | + const { calls, turn } = await fixture(t, () => { |
| 57 | + throw agentError({ code: 'AGENT_UNAVAILABLE', retryable: false, message: 'rate limit: Token Plan quota' }); |
| 58 | + }); |
| 59 | + await turn(); |
| 60 | + assert.equal(calls.count, 1); |
| 61 | +}); |
| 62 | + |
| 63 | +test('a thrown AGENT_OUTCOME_UNKNOWN is never retried, even when it claims to be retryable', async (t) => { |
| 64 | + const { calls, turn } = await fixture(t, () => { |
| 65 | + throw agentError({ code: 'AGENT_OUTCOME_UNKNOWN', retryable: true, message: 'Abort not confirmed' }); |
| 66 | + }); |
| 67 | + await turn(); |
| 68 | + assert.equal(calls.count, 1); |
| 69 | +}); |
| 70 | + |
| 71 | +test('an error result with retryable:false is not retried even if its text names a 5xx', async (t) => { |
| 72 | + const { calls, turn } = await fixture(t, (n, input, options) => { |
| 73 | + options.onEvent({ type: 'turn.started', sessionId: 's1', turnId: `t${n}` }); |
| 74 | + return { sessionId: 's1', turnId: `t${n}`, status: 'error', error: { code: 'LLM_UPSTREAM_ERROR', message: 'Internal server error (500)', retryable: false } }; |
| 75 | + }); |
| 76 | + await turn(); |
| 77 | + assert.equal(calls.count, 1); |
| 78 | +}); |
| 79 | + |
| 80 | +test('an error result coded AGENT_OUTCOME_UNKNOWN is not retried', async (t) => { |
| 81 | + const { calls, turn } = await fixture(t, (n, input, options) => { |
| 82 | + options.onEvent({ type: 'turn.started', sessionId: 's1', turnId: `t${n}` }); |
| 83 | + return { sessionId: 's1', turnId: `t${n}`, status: 'error', error: { code: 'AGENT_OUTCOME_UNKNOWN', message: 'Outcome unknown' } }; |
| 84 | + }); |
| 85 | + await turn(); |
| 86 | + assert.equal(calls.count, 1); |
| 87 | +}); |
| 88 | + |
| 89 | +test('a thrown retryable error is retried, and the retry stops at the first success', async (t) => { |
| 90 | + const { calls, turn, store, chatId } = await fixture(t, (n, input, options) => { |
| 91 | + if (n < 3) throw agentError({ code: 'AGENT_UNAVAILABLE', retryable: true, message: 'Unavailable' }); |
| 92 | + options.onEvent({ type: 'turn.started', sessionId: 's1', turnId: `t${n}` }); |
| 93 | + return { sessionId: 's1', turnId: `t${n}`, status: 'finished', text: 'Done' }; |
| 94 | + }); |
| 95 | + const result = await turn(); |
| 96 | + assert.equal(calls.count, 3); |
| 97 | + assert.equal(result.status, 'done'); |
| 98 | + assert.equal((await store.getMessages(chatId)).at(-1).text, 'Done'); |
| 99 | +}); |
| 100 | + |
| 101 | +test('a failed result with no retry verdict is retried until the window runs out', async (t) => { |
| 102 | + const { calls, turn, store, chatId } = await fixture(t, (n, input, options) => { |
| 103 | + options.onEvent({ type: 'turn.started', sessionId: 's1', turnId: `t${n}` }); |
| 104 | + return { sessionId: 's1', turnId: `t${n}`, status: 'error', error: { code: 'TURN_FAILED', message: 'The Agent turn failed.' } }; |
| 105 | + }); |
| 106 | + await turn(); |
| 107 | + assert.equal(calls.count, SHORT_DELAYS.length + 1); |
| 108 | + assert.equal((await store.getMessages(chatId)).at(-1).retried, SHORT_DELAYS.length); |
| 109 | +}); |
| 110 | + |
| 111 | +test('a failed result whose reason a retry cannot fix is not retried', async (t) => { |
| 112 | + const { calls, turn } = await fixture(t, (n, input, options) => { |
| 113 | + options.onEvent({ type: 'turn.started', sessionId: 's1', turnId: `t${n}` }); |
| 114 | + return { sessionId: 's1', turnId: `t${n}`, status: 'error', error: { code: 'LLM_QUOTA', message: 'rate limit: quota used up', reason: 'quota' } }; |
| 115 | + }); |
| 116 | + await turn(); |
| 117 | + assert.equal(calls.count, 1); |
| 118 | +}); |
0 commit comments