fix(bridge): provider-agnostic rate-limit / not-found errors + hardening#47
Conversation
The kernel was reading Discord-specific error shapes (status === 429, retryAfter field, 'Agent not found:' string match) directly in core/api.ts, leaking platform concerns into the provider-agnostic layer. Introduce two typed errors in core/errors.ts: - RateLimitError: carries retryAfterMs; adapters translate their platform's unit (Discord ms, Slack seconds) into ms. - AgentNotFoundError: carries agentId; thrown by findOrCreateAgentChannel when the maestro registry lookup misses. core/api.ts now branches on instanceof instead of string sniffing and the 429/retryAfter structural check. Discord and Slack adapters expose small toRateLimitError(err) helpers (testable, exported) that map platform errors into the kernel shape; their send() methods catch and rethrow the typed error so the kernel retry loop can react.
When the kernel gives up after 3 retries against a RateLimitError, the HTTP response was a bare 429 with a static body — clients had no machine-readable hint about when to retry. Carry the error's retryAfterMs into a Retry-After header so well-behaved clients (including any maestro agent that calls /api/send) can back off without guessing. Conversion: Math.ceil(retryAfterMs / 1000), clamped to a minimum of 1 second. The clamp avoids advertising a 0-second Retry-After after a sub-second backoff that the kernel already waited through and still hit the limit on — that would just trigger an immediate retry loop. sendJson() now accepts an optional headers record so future endpoints can attach custom response headers without further refactors. Adds 2 server tests: - sub-second backoff (250ms) advertises Retry-After: 1 - fractional backoff (1500ms) advertises Retry-After: 2 Also extends the request() test helper to surface response headers so future tests can assert on them. 221 pass, same 4 pre-existing Windows-path failures as main.
- Restore prototype chain on RateLimitError/AgentNotFoundError so instanceof survives transpilation and module boundaries (kernel dispatches on instanceof) - Clamp in-request retry backoff to [100ms, 5000ms]; large backoffs are still surfaced via the Retry-After response header - Require numeric retryAfter in Discord toRateLimitError to avoid misclassifying unrelated errors
|
Warning Review limit reached
More reviews will be available in 59 minutes and 47 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bundles four merged PRs since v0.2.0: - #46 OS-agnostic auto-run + attachments tests (Windows CI fix) - #47 typed bridge errors + `Retry-After` header on 429 - #48 leveled logger consolidation + `LOG_LEVEL` env var - #49 wallclock ISO timestamps on console output (macOS launchd gap) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@
Summary
Replaces fragile string-prefix error matching in the kernel with typed, provider-agnostic bridge errors (
RateLimitError,AgentNotFoundError), and advertisesRetry-Afteron 429 responses. Adapters (Discord, Slack) translate their platform-specific error shapes into these classes so the kernel reacts without leaking platform types.Changes
src/core/errors.ts: provider-agnosticRateLimitError(ms backoff) andAgentNotFoundError. Prototype chain restored viaObject.setPrototypeOfsoinstanceofsurvives transpilation / module boundaries (the kernel dispatches entirely oninstanceof).src/core/api.ts: dispatch oninstanceofinstead of string prefixes; emitRetry-After(whole seconds, min 1) on 429. In-request retry backoff clamped to[100ms, 5000ms]so a0never busy-spins and a large value never hangs the connection - larger backoffs are surfaced to the caller via the header.src/providers/discord/adapter.ts+src/providers/slack/adapter.ts:toRateLimitErrorfactories converting each platform unit to ms. Discord guard requires a numericretryAfterto avoid misclassifying unrelated errors.Merge order
Independent of the logger PR. Best merged after #46 (Windows path test fixes) so CI is fully green on Windows; the only failures on this branch are those 4 pre-existing path tests, which #46 resolves.
Test plan
npm test-> 221 pass (4 failures are the pre-existing Windows path tests fixed by test: make auto-run-command and attachments tests OS-agnostic #46)tsc)@