diff --git a/src/end-user-id.test.ts b/src/end-user-id.test.ts index 3cc3aa04..b56b2961 100644 --- a/src/end-user-id.test.ts +++ b/src/end-user-id.test.ts @@ -3,7 +3,7 @@ * account's end user, which only `GET /accounts` reports. Every assertion here is on the headers * that actually reached the mock server. */ -import { http, HttpResponse } from 'msw'; +import { delay, http, HttpResponse } from 'msw'; import { TEST_BASE_URL } from '../mocks/constants'; import { accountMcpTools, createMcpApp, jsonRpcMethod } from '../mocks/mcp-server'; import { server } from '../mocks/node'; @@ -674,6 +674,33 @@ describe('x-end-user-id for an explicit account the API says is not shared', () expect(error.statusCode).toBe(429); }); + it('stays fatal to a fan-out when the lookup times out while a 429 is retried', async () => { + serve(ACCOUNTS, { acc1: 'alice' }); + let accountRequests = 0; + server.use( + http.get(`${TEST_BASE_URL}/accounts`, async () => { + if (accountRequests++ === 0) { + return HttpResponse.json( + { statusCode: 429, message: 'Too many requests' }, + { status: 429, headers: { 'Retry-After': '0' } }, + ); + } + await delay('infinite'); + return HttpResponse.json([]); + }), + ); + + const error = await newToolSet({ accountIds: ['acc1', 'acc2'], timeout: 300 }) + .fetchTools() + .catch((caught: unknown) => caught); + + assert.instanceOf(error, StackOneAPIError); + expect(error).toMatchObject({ + statusCode: 429, + message: `Listing accounts at ${TEST_BASE_URL}/accounts was rate limited (429) and timed out after 0.3s while retrying`, + }); + }); + it('is looked up whatever the refusal names the account as', async () => { serve(ACCOUNTS, { acc1: 'alice' }); server.use( diff --git a/src/mcp-client.ts b/src/mcp-client.ts index b2944752..e8abcf09 100644 --- a/src/mcp-client.ts +++ b/src/mcp-client.ts @@ -107,7 +107,7 @@ function describeInnermostCause(error: unknown): string { } /** - * Whether an MCP request was still rate limited after its retries: an HTTP 429 from the endpoint. + * Whether a request was still rate limited after its retries: an HTTP 429 from the API. * * Unlike a dead account, this says nothing about one account and everything about the API key, * so a fan-out must fail on it rather than skip the account and return a partial catalog. A @@ -244,7 +244,6 @@ async function lookUpAfterRefusal( } } -/** A session that timed out while a 429 was being retried. */ class RateLimitTimeoutError extends Error { constructor(options: ErrorOptions) { super('timed out while retrying a 429', options); @@ -252,6 +251,20 @@ class RateLimitTimeoutError extends Error { } } +export function rateLimitTimeoutError( + request: string, + timeout: number, + cause: unknown, +): StackOneAPIError { + return new StackOneAPIError( + `${request} was rate limited (429) and timed out after ${timeout / 1000}s while retrying`, + 429, + null, + undefined, + { cause: new RateLimitTimeoutError({ cause }) }, + ); +} + /** The MCP request-timeout error code, as the number an {@link McpError} carries. */ const REQUEST_TIMEOUT_CODE: number = ErrorCode.RequestTimeout; @@ -315,13 +328,7 @@ async function withMcpSession( // reported as a timeout, it would skip the account and return a partial result, which a // 429 must never do. if (rateLimited > 0 && isTimeout(error)) { - throw new StackOneAPIError( - `MCP request to ${endpoint} was rate limited (429) and timed out after ${timeout / 1000}s while retrying`, - 429, - null, - undefined, - { cause: new RateLimitTimeoutError({ cause: error }) }, - ); + throw rateLimitTimeoutError(`MCP request to ${endpoint}`, timeout, error); } throw describeMcpFailure(error, endpoint, timeout); } finally { diff --git a/src/toolsets.ts b/src/toolsets.ts index 7f32a895..2e226cc3 100644 --- a/src/toolsets.ts +++ b/src/toolsets.ts @@ -17,6 +17,7 @@ import { HttpRateLimitError, isRateLimitFailure, listMcpTools, + rateLimitTimeoutError, withEndUser, withEndUsers, } from './mcp-client'; @@ -685,13 +686,7 @@ export class StackOneToolSet { } catch (error) { // Timing out while a 429 is retried is still the rate limit, as it is over MCP. if (rateLimited && error instanceof DOMException && error.name === 'TimeoutError') { - throw new StackOneAPIError( - `Listing accounts at ${url} was rate limited (429) and timed out after ${this.#timeout / 1000}s while retrying`, - 429, - null, - undefined, - { cause: error }, - ); + throw rateLimitTimeoutError(`Listing accounts at ${url}`, this.#timeout, error); } throw new ToolSetLoadError(`Could not reach ${url}: ${describeError(error)}`, { cause: error,