| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995 |
- import { describe, expect, it, vi } from "vitest";
- import type { StreamFn } from "@earendil-works/pi-agent-core";
- import type {
- AssistantMessage,
- AssistantMessageEvent,
- AssistantMessageEventStream,
- Context,
- Model,
- } from "@earendil-works/pi-ai";
- import { LLM_ERROR_CODES } from "@mavis/shared/llm-error-classifier";
- import {
- DEFAULT_LLM_RETRY_POLICY,
- LLM_RETRY_REQUEST_SETTLED_OBSERVER,
- withLLMRetry,
- type LLMCallSettledEvent,
- type LLMRequestSettledEvent,
- type LLMRetryEvent,
- } from "../../../src/pi-turn-runner/llm-retry.js";
- function fakeModel(provider = "fake-provider"): Model<any> {
- return {
- id: "fake-model",
- name: "fake-model",
- api: "test-messages",
- provider,
- baseUrl: "https://example.invalid",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 200_000,
- maxTokens: 8_192,
- } as Model<any>;
- }
- const CONTEXT: Context = { messages: [] };
- function assistant(
- stopReason: AssistantMessage["stopReason"],
- content: AssistantMessage["content"],
- errorMessage?: string,
- usage?: Partial<AssistantMessage["usage"]>,
- ): AssistantMessage {
- return {
- role: "assistant",
- content,
- api: "test-messages",
- provider: "fake-provider",
- model: "fake-model",
- usage: {
- input: 0,
- output: 0,
- cacheRead: 0,
- cacheWrite: 0,
- totalTokens: 0,
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
- ...(usage ?? {}),
- },
- stopReason,
- ...(errorMessage ? { errorMessage } : {}),
- timestamp: 0,
- } as AssistantMessage;
- }
- function stream(
- events: AssistantMessageEvent[],
- final: AssistantMessage,
- ): AssistantMessageEventStream {
- return {
- [Symbol.asyncIterator]() {
- let index = 0;
- return {
- async next() {
- if (index < events.length)
- return { value: events[index++], done: false };
- return { value: undefined, done: true };
- },
- } as AsyncIterableIterator<AssistantMessageEvent>;
- },
- async result() {
- return final;
- },
- } as unknown as AssistantMessageEventStream;
- }
- function successStream(text: string): AssistantMessageEventStream {
- const empty = assistant("stop", []);
- const partial = assistant("stop", [{ type: "text", text }]);
- const final = assistant("stop", [{ type: "text", text }]);
- return stream(
- [
- { type: "start", partial: empty },
- { type: "text_start", contentIndex: 0, partial: empty },
- { type: "text_delta", contentIndex: 0, delta: text, partial },
- { type: "text_end", contentIndex: 0, content: text, partial },
- { type: "done", reason: "stop", message: final },
- ],
- final,
- );
- }
- function errorStream(
- message: string,
- partialText = "",
- usage?: Partial<AssistantMessage["usage"]>,
- ): AssistantMessageEventStream {
- const empty = assistant("error", []);
- const content = partialText
- ? [{ type: "text" as const, text: partialText }]
- : [];
- const partial = assistant("error", content);
- const final = assistant("error", content, message, usage);
- return stream(
- [
- { type: "start", partial: empty },
- ...(partialText
- ? ([
- { type: "text_start", contentIndex: 0, partial: empty },
- {
- type: "text_delta",
- contentIndex: 0,
- delta: partialText,
- partial,
- },
- ] satisfies AssistantMessageEvent[])
- : []),
- { type: "error", reason: "error", error: final },
- ],
- final,
- );
- }
- async function collectEvents(
- value: Awaited<ReturnType<StreamFn>>,
- ): Promise<AssistantMessageEvent[]> {
- const events: AssistantMessageEvent[] = [];
- for await (const event of value) events.push(event);
- return events;
- }
- function retryOptions(overrides: Record<string, unknown> = {}) {
- return {
- sessionId: "session-1",
- turnId: "turn-1",
- scope: "agent" as const,
- generateCallId: () => "call-1",
- random: () => 0,
- nowMs: () => 1_000,
- sleep: vi.fn(async () => {}),
- ...overrides,
- };
- }
- describe("withLLMRetry", () => {
- it("observes each physical provider request, including a retry that fails first", async () => {
- let attempts = 0;
- const physical: LLMRequestSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- if (attempts === 1) {
- return errorStream("[Error] 429 Too Many Requests", "", {
- input: 11,
- output: 2,
- cacheRead: Number.NaN,
- cacheWrite: 4,
- });
- }
- const final = assistant(
- "stop",
- [{ type: "text", text: "ok" }],
- undefined,
- {
- input: 17,
- output: 3,
- cacheRead: 5,
- cacheWrite: 7,
- },
- );
- return stream([{ type: "done", reason: "stop", message: final }], final);
- }) as StreamFn;
- const wrapped = withLLMRetry(inner, retryOptions());
- const result = await wrapped(fakeModel(), CONTEXT, {
- [LLM_RETRY_REQUEST_SETTLED_OBSERVER]: (event: LLMRequestSettledEvent) => {
- physical.push(event);
- },
- });
- await collectEvents(result);
- expect(physical).toEqual([
- {
- requestAttempt: 1,
- outcome: "error",
- usage: { input: 11, output: 2, cacheRead: 0, cacheWrite: 4 },
- usageComplete: false,
- },
- {
- requestAttempt: 2,
- outcome: "success",
- usage: { input: 17, output: 3, cacheRead: 5, cacheWrite: 7 },
- usageComplete: true,
- },
- ]);
- });
- it("disables provider-native retries so the framework owns the request budget", async () => {
- const maxRetries: Array<number | undefined> = [];
- const inner = ((_model, _context, options) => {
- maxRetries.push(options?.maxRetries);
- return successStream("ok");
- }) as StreamFn;
- const wrapped = withLLMRetry(inner, retryOptions());
- const result = await wrapped(fakeModel(), CONTEXT, { maxRetries: 9 });
- await collectEvents(result);
- expect(maxRetries).toEqual([0]);
- });
- it("discards a pre-commit 429 attempt and exposes only the recovered stream", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return attempts === 1
- ? errorStream("[Error] 429 Too Many Requests")
- : successStream("ok");
- }) as StreamFn;
- const sleep = vi.fn(async () => {});
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- sleep,
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- const events = await collectEvents(result);
- expect(attempts).toBe(2);
- expect(events.map((event) => event.type)).toEqual([
- "start",
- "text_start",
- "text_delta",
- "text_end",
- "done",
- ]);
- expect(sleep).toHaveBeenCalledWith(500, undefined);
- expect(observed).toMatchObject([
- {
- callId: "call-1",
- status: "waiting",
- retryAttempt: 1,
- maxRetries: 5,
- requestAttempt: 2,
- delayMs: 500,
- error: { reason: "rate_limited" },
- },
- {
- callId: "call-1",
- status: "recovered",
- retryAttempt: 1,
- maxRetries: 5,
- requestAttempt: 2,
- },
- ]);
- await result.result();
- expect(settled).toMatchObject([
- {
- sessionId: "session-1",
- turnId: "turn-1",
- callId: "call-1",
- scope: "agent",
- provider: "fake-provider",
- model: "fake-model",
- final: { outcome: "success", errorKind: "none" },
- retryTriggered: true,
- retryReason: "rate_limited",
- requestAttempts: 2,
- // Successful calls carry the raw provider buckets; this fixture reports
- // an all-zero usage.
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- },
- ]);
- });
- it("settles a successful logical Call with only its normalized tool identities", async () => {
- const final = assistant("toolUse", [
- {
- type: "toolCall",
- id: "tool-1",
- name: "read",
- arguments: { path: "README.md" },
- },
- ]);
- const settled: LLMCallSettledEvent[] = [];
- const wrapped = withLLMRetry(
- (async () =>
- stream(
- [{ type: "done", reason: "toolUse", message: final }],
- final,
- )) as StreamFn,
- retryOptions({
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- await (await wrapped(fakeModel(), CONTEXT, {})).result();
- expect(settled[0]?.expectedTools).toEqual([
- { toolCallId: "tool-1", toolName: "read" },
- ]);
- expect(settled[0]).not.toHaveProperty("assistantMessage");
- expect(JSON.stringify(settled[0])).not.toContain("README.md");
- });
- it("does not count a committed retry stream as recovered when its final result fails", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return attempts === 1
- ? errorStream("[Error] 429 Too Many Requests")
- : errorStream("ECONNRESET", "partial");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- await result.result();
- expect(observed.map((event) => event.status)).toEqual([
- "waiting",
- "recovered",
- ]);
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: "network" },
- retryTriggered: true,
- retryReason: "rate_limited",
- requestAttempts: 2,
- },
- ]);
- });
- it.each([2056, 2067])(
- "never retries Token Plan exhaustion status %i even when the outer status looks transient",
- async (statusCode) => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return errorStream(
- `[Error] 429 {"status_code":${statusCode},"status_msg":"plan exhausted"}`,
- );
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- const events = await collectEvents(result);
- expect(attempts).toBe(1);
- expect(events.at(-1)?.type).toBe("error");
- expect(observed).toEqual([]);
- await result.result();
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: "usage_limit" },
- retryTriggered: false,
- requestAttempts: 1,
- },
- ]);
- },
- );
- it("does not report recovered when a retry ends in a non-retryable quota error", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return attempts === 1
- ? errorStream("[Error] 429 Too Many Requests")
- : errorStream(
- '[Error] 500 {"status_code":2056,"status_msg":"plan exhausted"}',
- );
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(attempts).toBe(2);
- expect(observed.map((event) => event.status)).toEqual([
- "waiting",
- "exhausted",
- ]);
- expect(observed.at(-1)?.error).toEqual({
- reason: "usage_limit",
- code: LLM_ERROR_CODES.USAGE_LIMIT_EXCEEDED,
- message: "LLM usage limit reached",
- });
- await expect(result.result()).resolves.toMatchObject({
- errorMessage:
- '[Error] 500 {"status_code":2056,"status_msg":"plan exhausted"}',
- });
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: "usage_limit" },
- retryTriggered: true,
- retryReason: "rate_limited",
- requestAttempts: 2,
- },
- ]);
- });
- it.each([
- {
- finalError: "[Error] 401 invalid api key",
- expectedReason: "auth",
- expectedCode: LLM_ERROR_CODES.LLM_AUTH_ERROR,
- expectedMessage: "LLM provider authentication failed",
- },
- {
- finalError: "[Error] 503 upstream unavailable",
- expectedReason: "server_error",
- expectedCode: LLM_ERROR_CODES.LLM_UPSTREAM_ERROR,
- expectedMessage: "LLM provider request failed",
- },
- ])(
- "attributes an exhausted retry to the final $expectedReason failure",
- async ({ finalError, expectedReason, expectedCode, expectedMessage }) => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return attempts === 1
- ? errorStream("[Error] 429 Too Many Requests")
- : errorStream(finalError);
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(attempts).toBe(2);
- expect(observed.at(-1)).toMatchObject({
- status: "exhausted",
- error: {
- reason: expectedReason,
- code: expectedCode,
- message: expectedMessage,
- },
- });
- await expect(result.result()).resolves.toMatchObject({
- errorMessage: finalError,
- });
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: expectedReason },
- retryTriggered: true,
- retryReason: "rate_limited",
- requestAttempts: 2,
- },
- ]);
- },
- );
- it("retries a network error thrown before a stream is returned", async () => {
- let attempts = 0;
- const inner = (async () => {
- attempts += 1;
- if (attempts === 1)
- throw Object.assign(new Error("socket reset"), { code: "ECONNRESET" });
- return successStream("ok");
- }) as StreamFn;
- const wrapped = withLLMRetry(inner, retryOptions());
- const result = await wrapped(fakeModel(), CONTEXT, {});
- expect((await collectEvents(result)).at(-1)?.type).toBe("done");
- expect(attempts).toBe(2);
- });
- it("uses five retries by default and emits exhausted after the sixth request", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const sleep = vi.fn(async () => {});
- const inner = (async () => {
- attempts += 1;
- return errorStream("[Error] 529 overloaded");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- sleep,
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(DEFAULT_LLM_RETRY_POLICY.maxRetries).toBe(5);
- expect(attempts).toBe(6);
- expect(sleep.mock.calls.map(([delayMs]) => delayMs)).toEqual([
- 500, 1_000, 2_000, 4_000, 8_000,
- ]);
- expect(observed.filter((event) => event.status === "waiting")).toHaveLength(
- 5,
- );
- expect(observed.at(-1)).toMatchObject({
- status: "exhausted",
- retryAttempt: 5,
- maxRetries: 5,
- requestAttempt: 6,
- error: { reason: "overloaded" },
- });
- await result.result();
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: "overloaded" },
- retryTriggered: true,
- retryReason: "overloaded",
- requestAttempts: 6,
- },
- ]);
- });
- it("limits BYOK errors to five retries regardless of error classification", async () => {
- let attempts = 0;
- const inner = (async () => {
- attempts += 1;
- return errorStream("[Error] 401 invalid api key");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({ sleep: vi.fn(async () => {}) }),
- );
- const result = await wrapped(fakeModel("minimax_api"), CONTEXT, {});
- await collectEvents(result);
- expect(attempts).toBe(6);
- });
- it("keeps the exhausted attempt open when returning its terminal failure stream", async () => {
- const attemptReturns: Array<ReturnType<typeof vi.fn>> = [];
- const inner = (async () => {
- const failed = errorStream("[Error] 529 overloaded");
- const iterator = failed[Symbol.asyncIterator]();
- let cancelled = false;
- const closeAttempt = vi.fn(async () => {
- cancelled = true;
- return { value: undefined, done: true as const };
- });
- attemptReturns.push(closeAttempt);
- return {
- [Symbol.asyncIterator]() {
- return { next: () => iterator.next(), return: closeAttempt };
- },
- async result() {
- if (cancelled) throw new Error("provider stream was cancelled");
- return failed.result();
- },
- } as unknown as AssistantMessageEventStream;
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({ policy: { maxRetries: 1 }, sleep: vi.fn(async () => {}) }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- const events = await collectEvents(result);
- expect(events.at(-1)?.type).toBe("error");
- await expect(result.result()).resolves.toMatchObject({
- stopReason: "error",
- errorMessage: "[Error] 529 overloaded",
- });
- expect(attemptReturns).toHaveLength(2);
- expect(attemptReturns[0]).toHaveBeenCalledOnce();
- expect(attemptReturns[1]).not.toHaveBeenCalled();
- });
- it("settles result-only calls when every attempt disconnects before the first token", async () => {
- const disconnect = Object.assign(
- new Error("socket reset before first token"),
- {
- code: "ECONNRESET",
- },
- );
- const settled: LLMCallSettledEvent[] = [];
- let attempts = 0;
- const inner = (async () => {
- attempts += 1;
- return {
- [Symbol.asyncIterator]() {
- return {
- async next(): Promise<IteratorResult<AssistantMessageEvent>> {
- throw disconnect;
- },
- };
- },
- result: () => new Promise<AssistantMessage>(() => {}),
- } as unknown as AssistantMessageEventStream;
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- policy: { maxRetries: 1 },
- sleep: vi.fn(async () => {}),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- const timeout = Symbol("result timeout");
- const outcome = await Promise.race([
- result.result().catch((error: unknown) => error),
- new Promise<symbol>((resolve) => setTimeout(() => resolve(timeout), 100)),
- ]);
- expect(attempts).toBe(2);
- expect(outcome).toBe(disconnect);
- expect(settled).toMatchObject([
- {
- final: { outcome: "error", errorKind: "network" },
- retryTriggered: true,
- retryReason: "network",
- requestAttempts: 2,
- },
- ]);
- });
- it("stops before a backoff that would exceed the retry elapsed-time budget", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const sleep = vi.fn(async () => {});
- const inner = (async () => {
- attempts += 1;
- return errorStream("[Error] 429 Too Many Requests");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- sleep,
- observer: (event: LLMRetryEvent) => observed.push(event),
- policy: { maxRetryElapsedMs: 499 },
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(attempts).toBe(1);
- expect(sleep).not.toHaveBeenCalled();
- expect(observed).toMatchObject([
- { status: "exhausted", retryAttempt: 0, requestAttempt: 1 },
- ]);
- });
- it("initiates cleanup for a discarded attempt before the next provider request", async () => {
- let attempts = 0;
- const discardedReturn = vi.fn(async () => ({
- value: undefined,
- done: true as const,
- }));
- const inner = (async () => {
- attempts += 1;
- if (attempts > 1) return successStream("ok");
- const failed = errorStream("[Error] 429 Too Many Requests");
- return {
- [Symbol.asyncIterator]() {
- const iterator = failed[Symbol.asyncIterator]();
- return { next: () => iterator.next(), return: discardedReturn };
- },
- result: () => failed.result(),
- } as unknown as AssistantMessageEventStream;
- }) as StreamFn;
- const wrapped = withLLMRetry(inner, retryOptions());
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(discardedReturn).toHaveBeenCalledOnce();
- expect(attempts).toBe(2);
- });
- it("does not retry a disconnect after a visible text delta has committed", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return errorStream("ECONNRESET", "partial");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- }),
- );
- const result = await wrapped(fakeModel(), CONTEXT, {});
- const events = await collectEvents(result);
- expect(attempts).toBe(1);
- expect(events.some((event) => event.type === "text_delta")).toBe(true);
- expect(events.at(-1)?.type).toBe("error");
- expect(observed).toEqual([]);
- });
- it("commits BYOK streaming after visible output in every host composition", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return errorStream("[Error] 401 invalid api key", "partial");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- sleep: vi.fn(async () => {}),
- }),
- );
- const result = await wrapped(
- fakeModel("custom_provider:work"),
- CONTEXT,
- {},
- );
- const events = await collectEvents(result);
- expect(attempts).toBe(1);
- expect(events.some((event) => event.type === "text_delta")).toBe(true);
- expect(events.at(-1)?.type).toBe("error");
- expect(observed).toEqual([]);
- });
- it("retries a BYOK provider stream that ended without any message event", async () => {
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- return attempts === 1
- ? errorStream(
- "Provider stream ended without any message events (rawEventCount=0; messageEventCount=0; firstEventAtMs=none; lastEventAtMs=none; eofAtMs=61000)",
- )
- : successStream("recovered");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- sleep: vi.fn(async () => {}),
- }),
- );
- const result = await wrapped(
- fakeModel("custom_provider:byok-gateway"),
- CONTEXT,
- {},
- );
- const events = await collectEvents(result);
- expect(attempts).toBe(2);
- expect(events.at(-1)?.type).toBe("done");
- expect(events.some((event) => event.type === "error")).toBe(false);
- expect((await result.result()).stopReason).toBe("stop");
- expect(observed).toMatchObject([
- {
- status: "waiting",
- retryAttempt: 1,
- requestAttempt: 2,
- error: { reason: "network" },
- },
- { status: "recovered", retryAttempt: 1, requestAttempt: 2 },
- ]);
- });
- it("returns a BYOK stream before the provider finishes after its first visible delta", async () => {
- const empty = assistant("error", []);
- const partial = assistant("error", [
- { type: "thinking", thinking: "live" },
- ]);
- const final = assistant(
- "error",
- [{ type: "thinking", thinking: "live" }],
- "late disconnect",
- );
- let releaseTail = () => undefined;
- const tail = new Promise<void>((resolve) => {
- releaseTail = resolve;
- });
- let attempts = 0;
- const inner = (async () => {
- attempts += 1;
- let index = 0;
- return {
- [Symbol.asyncIterator]() {
- return {
- async next(): Promise<IteratorResult<AssistantMessageEvent>> {
- index += 1;
- if (index === 1)
- return {
- value: { type: "start", partial: empty },
- done: false,
- };
- if (index === 2) {
- return {
- value: {
- type: "thinking_delta",
- contentIndex: 0,
- delta: "live",
- partial,
- },
- done: false,
- };
- }
- await tail;
- if (index === 3) {
- return {
- value: { type: "error", reason: "error", error: final },
- done: false,
- };
- }
- return { value: undefined, done: true };
- },
- };
- },
- async result() {
- await tail;
- return final;
- },
- } as AssistantMessageEventStream;
- }) as StreamFn;
- const order: string[] = [];
- const wrapped = withLLMRetry(
- inner,
- retryOptions({ sleep: vi.fn(async () => {}) }),
- );
- const committed = wrapped(
- fakeModel("custom_provider:work"),
- CONTEXT,
- {},
- ).then((result) => {
- order.push("committed");
- return result;
- });
- setTimeout(() => {
- order.push("tail");
- releaseTail();
- }, 0);
- const result = await committed;
- expect(order[0]).toBe("committed");
- expect(
- (await collectEvents(result)).some(
- (event) => event.type === "thinking_delta",
- ),
- ).toBe(true);
- expect(attempts).toBe(1);
- });
- it("honors a bounded Retry-After response header", async () => {
- let attempts = 0;
- const sleep = vi.fn(async () => {});
- const inner = (async (model, _context, options) => {
- attempts += 1;
- if (attempts === 1) {
- await options?.onResponse?.(
- { status: 429, headers: { "retry-after": "300" } },
- model,
- );
- return errorStream("provider throttled");
- }
- return successStream("ok");
- }) as StreamFn;
- const wrapped = withLLMRetry(inner, retryOptions({ sleep }));
- const result = await wrapped(fakeModel(), CONTEXT, {});
- await collectEvents(result);
- expect(sleep).toHaveBeenCalledWith(30_000, undefined);
- });
- it("cancels an in-flight backoff without starting another request", async () => {
- const controller = new AbortController();
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const settled: LLMCallSettledEvent[] = [];
- const sleep = vi.fn(
- async (_delayMs: number, signal?: AbortSignal) =>
- new Promise<void>((_resolve, reject) => {
- signal?.addEventListener(
- "abort",
- () => reject(new Error("sleep aborted")),
- { once: true },
- );
- }),
- );
- const inner = (async () => {
- attempts += 1;
- return errorStream("[Error] 429 Too Many Requests");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- sleep,
- observer: (event: LLMRetryEvent) => observed.push(event),
- onCallSettled: (event: LLMCallSettledEvent) => settled.push(event),
- }),
- );
- const pending = wrapped(fakeModel(), CONTEXT, {
- signal: controller.signal,
- });
- await vi.waitFor(() => expect(sleep).toHaveBeenCalledOnce());
- controller.abort("user_stop");
- await expect(pending).rejects.toThrow("sleep aborted");
- expect(attempts).toBe(1);
- expect(observed.at(-1)).toMatchObject({
- status: "cancelled",
- retryAttempt: 1,
- requestAttempt: 2,
- });
- expect(settled).toMatchObject([
- {
- final: { outcome: "abort", errorKind: "abort" },
- retryTriggered: true,
- retryReason: "rate_limited",
- requestAttempts: 1,
- },
- ]);
- });
- it("reports cancellation when the user aborts an active retry attempt", async () => {
- const controller = new AbortController();
- let attempts = 0;
- const observed: LLMRetryEvent[] = [];
- const inner = (async () => {
- attempts += 1;
- if (attempts === 1) return errorStream("[Error] 429 Too Many Requests");
- controller.abort("user_stop");
- throw new DOMException("The operation was aborted", "AbortError");
- }) as StreamFn;
- const wrapped = withLLMRetry(
- inner,
- retryOptions({
- observer: (event: LLMRetryEvent) => observed.push(event),
- }),
- );
- await expect(
- wrapped(fakeModel(), CONTEXT, { signal: controller.signal }),
- ).rejects.toThrow("aborted");
- expect(attempts).toBe(2);
- expect(observed.map((event) => event.status)).toEqual([
- "waiting",
- "cancelled",
- ]);
- });
- });
|