Skip to content

feat(cli): carry 429 retry timing in the CLI and mcpdo error envelopes - #2795

Merged
cliffhall merged 3 commits into
v2/mainfrom
v2/feat/2772-rate-limit-error-envelope
Oct 25, 2026
Merged

cliffhall merged 3 commits into
v2/mainfrom
v2/feat/2772-rate-limit-error-envelope

Conversation

@cliffhall

@cliffhall cliffhall commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Closes #2772

What changed

The one-shot CLI and mcpdo JSON error envelopes now keep the structured rate-limit data that #2737 attaches to a throttled request's error:

{"error":{"code":"rate_limited","message":"Rate limited by the server (HTTP 429) on tools/list. …","cause":"…","status":429,"method":"tools/list","retryAfter":"30","retryAfterMs":30000}}
  • core/cli/error-handler.ts (shared by both CLIs): a 429 now classifies as rate_limited. The handler reads method, retryAfter and retryAfterMs off error.data and checks each one's type before using it. retryAfter is the header exactly as the server sent it, so a malformed value still shows up. retryAfterMs appears only when the header parsed. A missing header leaves both fields out instead of emitting null, the same way the envelope already handles cause, status and url. The 429 check runs before the unreachable pattern, because a throttled error's message includes the server's response body, which could match that pattern by accident.
  • mcpdo daemon wire: DaemonErrorBody used to carry only code, message and exitCode. That is why mcpdo's envelope had no status at all. The daemon now also sends status and the rate-limit fields over the socket. The client rebuilds them into the CliExitCodeError envelope and sanitizes the server-supplied retryAfter text the same way it sanitizes the message.
  • Docs: the CLI README's exit-code table and envelope section.

Decision: a dedicated code and exit code

The issue left this open. I added rate_limited with exit code 11 (EXIT_CODES.RATE_LIMITED; 10 was taken by LOAD_THRESHOLD_EXCEEDED, #2745, while this PR was open) instead of keeping error / 1. A 429 is the one failure where the right response is "wait, then run the same command again", and the envelope now says how long. With exit 1, a script would have to read stderr just to tell throttling apart from a real failure, and the exit-code map exists to save callers from doing that. Codes 7–10 already set the precedent of one code per failure class a caller would act on differently.

Tests

The issue asked for tests in both CLIs, because each client sends its output a different way:

  • clients/cli/__tests__/rate-limit-envelope.test.ts: end to end against a small HTTP server that answers 429. It covers a parsed header, a malformed header, and no header, all through runCli.
  • clients/cli/__tests__/error-handler.test.ts: unit tests for the classification, wrong-type and non-object data, an unannotated 429, the unreachable-pattern precedence, and the pre-classified relay path.
  • clients/mcpdo/__tests__/rate-limit-envelope.test.ts: the daemon side of the relay (both the classified and the pre-classified branch), the client-side rebuild including sanitization, and a full connect → tools/list run through the auto-spawned daemon.
  • The throttling server is a shared helper (clients/cli/__tests__/helpers/throttling-server.ts). mcpdo's tests already import CLI test helpers.

npm run local:gate passed.

🤖 Generated with Claude Code

#2772)

A throttled request now classifies as `rate_limited` with exit code 10,
and the envelope lifts `method`, `retryAfter` and `retryAfterMs` off the
annotated error's data, so a script can wait and re-run without parsing
the prose message. Absent fields are omitted, matching the envelope's
other optional fields.

mcpdo's daemon error body only carried code/message/exitCode, which is
why its envelope lacked even `status`; it now relays `status` and the
rate-limit fields across the socket, sanitizing the header text on the
client side.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Oct 25, 2026
@cliffhall
cliffhall requested a balanced review from Copilot October 25, 2026 02:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Retry headers bypass secret redaction, streaming subscription errors lose metadata, and overflowing delays serialize as null.

3 open issues
What changed in this PR

This PR exposes structured HTTP 429 retry information in the shared CLI error envelope and carries it across mcpdo’s daemon connection, building on the rate-limit annotations from #2737.

Changes:

  • Adds rate_limited classification, exit code 10, and optional method and retry-timing fields.
  • Relays HTTP status and retry metadata through mcpdo’s daemon protocol.
  • Documents the envelope and adds shared fixtures, unit tests, and end-to-end coverage.
File Description
core/​cli/​error-handler.ts Classifies 429 responses and preserves retry metadata.
clients/​mcpdo/​src/​daemon/​server.ts Sends structured error fields over the daemon socket.
clients/​mcpdo/​src/​daemon/​protocol.ts Defines the additional daemon error fields.
clients/​mcpdo/​src/​daemon/​client.ts Rebuilds relayed envelopes and sanitizes header text.
clients/​mcpdo/​__tests__/​rate-limit-envelope.test.ts Tests daemon relay and end-to-end error output.
clients/​cli/​README.md Documents exit code 10 and retry fields.
clients/​cli/​__tests__/​rate-limit-envelope.test.ts Tests parsed, malformed, and missing retry headers.
clients/​cli/​__tests__/​helpers/​throttling-server.ts Provides a shared HTTP 429 test server.
clients/​cli/​__tests__/​error-handler.test.ts Tests classification, field validation, and pre-classified errors.

Review effort: Balanced

Comment thread core/cli/error-handler.ts Resolved
// before it reaches a terminal via the shared error handler.
sanitizeText(response.error.message),
{ code: response.error.code },
relayedEnvelope(response.error),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bfead90 — streamDaemon's setup-refusal path now builds its envelope with relayedEnvelope, same as callDaemon. Added a regression test: a socket server refuses a stream with a rate_limited body and the rejected CliExitCodeError keeps status, method, retryAfter and retryAfterMs.

Comment thread core/cli/error-handler.ts Resolved
…streams (#2772)

- retryAfter is server text kept verbatim when malformed, so it now goes
  through the envelope's URL redaction like message.
- A delta-seconds header too long for a double parses to Infinity, which
  serialized as null; retryAfterMs is now included only when finite.
- streamDaemon's setup-refusal path rebuilt only `code`; it now uses
  relayedEnvelope, so a throttled resources/subscribe keeps its fields.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1 — all three findings fixed in bfead90 (replied in each thread):

  • Redact retryAfter — now scrubbed by redactEnvelope() on every path.
  • Stream subscription errors — streamDaemon reuses relayedEnvelope; regression test added.
  • Non-finite delays — retryAfterMs requires a finite number; serialized-overflow test added.

npm run local:gate green. Requesting round 2.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused changes address all previously reported defects with regression coverage, and no unresolved blocking issues remain.

1 open issue
2 resolved since last review

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 2 was clean — "Approval recommended", no inline comments, no new findings. The one item it still lists as open (stream subscription metadata) is the round-1 thread, fixed in bfead90 (stream-client.ts now uses relayedEnvelope, with a regression test) and answered in-thread; it is only awaiting resolution.

Exit code 10 was taken on v2/main by LOAD_THRESHOLD_EXCEEDED (#2745);
RATE_LIMITED moves to 11.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Merged v2/main (28325db). Conflict in core/cli/error-handler.ts and clients/cli/README.md: exit code 10 was taken on v2/main by LOAD_THRESHOLD_EXCEEDED (#2745), so RATE_LIMITED is now 11. Tests reference the constant and needed no change; description updated. npm run local:gate green.

@cliffhall
cliffhall merged commit ececbd0 into v2/main Oct 25, 2026
12 checks passed
@cliffhall
cliffhall deleted the v2/feat/2772-rate-limit-error-envelope branch October 25, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI and mcpdo JSON error envelopes drop the structured Retry-After data from a 429

2 participants