Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/end-user-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 16 additions & 9 deletions src/mcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -244,14 +244,27 @@ 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);
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;

Expand Down Expand Up @@ -315,13 +328,7 @@ async function withMcpSession<T>(
// 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 {
Expand Down
9 changes: 2 additions & 7 deletions src/toolsets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
HttpRateLimitError,
isRateLimitFailure,
listMcpTools,
rateLimitTimeoutError,
withEndUser,
withEndUsers,
} from './mcp-client';
Expand Down Expand Up @@ -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,
Expand Down
Loading