From 08c356ae744fc7d5e2f6fcf50b9837b842d2c960 Mon Sep 17 00:00:00 2001 From: S2D2 Date: Tue, 10 Nov 2026 20:33:09 +0000 Subject: [PATCH 1/2] fix(toolset): keep a timed-out 429 on GET /accounts fatal to a fan-out When GET /accounts times out while a 429 is being retried, the toolset throws a 429 StackOneAPIError whose cause is the raw TimeoutError. isRateLimitFailure only treats a 429 as a rate limit when its cause is a StreamableHTTPError, RateLimitTimeoutError or HttpRateLimitError, so this error did not count. An end-user lookup that hit it was rewrapped as the original refusal, and fetchTools skipped the account and returned a partial catalog instead of failing with the 429. Wrap the cause in RateLimitTimeoutError, as the MCP session already does for the same case, and add a regression test that fails without the fix. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com> --- src/end-user-id.test.ts | 29 ++++++++++++++++++++++++++++- src/mcp-client.ts | 2 +- src/toolsets.ts | 3 ++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/end-user-id.test.ts b/src/end-user-id.test.ts index 3cc3aa04..1438e8ed 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.statusCode).toBe(429); + expect(error.message).toBe( + `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..9d6d9ba6 100644 --- a/src/mcp-client.ts +++ b/src/mcp-client.ts @@ -245,7 +245,7 @@ async function lookUpAfterRefusal( } /** A session that timed out while a 429 was being retried. */ -class RateLimitTimeoutError extends Error { +export class RateLimitTimeoutError extends Error { constructor(options: ErrorOptions) { super('timed out while retrying a 429', options); this.name = 'RateLimitTimeoutError'; diff --git a/src/toolsets.ts b/src/toolsets.ts index 7f32a895..bb210c96 100644 --- a/src/toolsets.ts +++ b/src/toolsets.ts @@ -17,6 +17,7 @@ import { HttpRateLimitError, isRateLimitFailure, listMcpTools, + RateLimitTimeoutError, withEndUser, withEndUsers, } from './mcp-client'; @@ -690,7 +691,7 @@ export class StackOneToolSet { 429, null, undefined, - { cause: error }, + { cause: new RateLimitTimeoutError({ cause: error }) }, ); } throw new ToolSetLoadError(`Could not reach ${url}: ${describeError(error)}`, { From 360b0ffb36146440fdaee2bfde69c2b60835ca6b Mon Sep 17 00:00:00 2001 From: S2D2 Date: Wed, 11 Nov 2026 01:08:40 +0000 Subject: [PATCH 2/2] refactor(toolset): share the 429 timeout error between the MCP and accounts paths - Add rateLimitTimeoutError() so the MCP session and the accounts lookup build the same StackOneAPIError (status 429, RateLimitTimeoutError cause) from one place instead of two copies - Stop exporting RateLimitTimeoutError, which only the helper constructs - Fix the isRateLimitFailure doc line to describe what it checks - Assert the full error message in the end-user lookup timeout test Co-Authored-By: Claude Opus 5.5 Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com> --- src/end-user-id.test.ts | 8 ++++---- src/mcp-client.ts | 27 +++++++++++++++++---------- src/toolsets.ts | 10 ++-------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/end-user-id.test.ts b/src/end-user-id.test.ts index 1438e8ed..b56b2961 100644 --- a/src/end-user-id.test.ts +++ b/src/end-user-id.test.ts @@ -695,10 +695,10 @@ describe('x-end-user-id for an explicit account the API says is not shared', () .catch((caught: unknown) => caught); assert.instanceOf(error, StackOneAPIError); - expect(error.statusCode).toBe(429); - expect(error.message).toBe( - `Listing accounts at ${TEST_BASE_URL}/accounts was rate limited (429) and timed out after 0.3s while retrying`, - ); + 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 () => { diff --git a/src/mcp-client.ts b/src/mcp-client.ts index 9d6d9ba6..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,14 +244,27 @@ async function lookUpAfterRefusal( } } -/** A session that timed out while a 429 was being retried. */ -export class RateLimitTimeoutError extends Error { +class RateLimitTimeoutError extends Error { constructor(options: ErrorOptions) { super('timed out while retrying a 429', options); this.name = 'RateLimitTimeoutError'; } } +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 bb210c96..2e226cc3 100644 --- a/src/toolsets.ts +++ b/src/toolsets.ts @@ -17,7 +17,7 @@ import { HttpRateLimitError, isRateLimitFailure, listMcpTools, - RateLimitTimeoutError, + rateLimitTimeoutError, withEndUser, withEndUsers, } from './mcp-client'; @@ -686,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: new RateLimitTimeoutError({ cause: error }) }, - ); + throw rateLimitTimeoutError(`Listing accounts at ${url}`, this.#timeout, error); } throw new ToolSetLoadError(`Could not reach ${url}: ${describeError(error)}`, { cause: error,