Skip to content

fix(bridge): provider-agnostic rate-limit / not-found errors + hardening#47

Merged
chr1syy merged 3 commits into
RunMaestro:mainfrom
jSydorowicz21:fix/typed-bridge-errors
Jun 7, 2026
Merged

fix(bridge): provider-agnostic rate-limit / not-found errors + hardening#47
chr1syy merged 3 commits into
RunMaestro:mainfrom
jSydorowicz21:fix/typed-bridge-errors

Conversation

@jSydorowicz21

Copy link
Copy Markdown
Collaborator

@

Summary

Replaces fragile string-prefix error matching in the kernel with typed, provider-agnostic bridge errors (RateLimitError, AgentNotFoundError), and advertises Retry-After on 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-agnostic RateLimitError (ms backoff) and AgentNotFoundError. Prototype chain restored via Object.setPrototypeOf so instanceof survives transpilation / module boundaries (the kernel dispatches entirely on instanceof).
  • src/core/api.ts: dispatch on instanceof instead of string prefixes; emit Retry-After (whole seconds, min 1) on 429. In-request retry backoff clamped to [100ms, 5000ms] so a 0 never 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: toRateLimitError factories converting each platform unit to ms. Discord guard requires a numeric retryAfter to 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

maestro-relay and others added 3 commits June 5, 2026 21:52
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
@coderabbitai

coderabbitai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@jSydorowicz21, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 631aa3ad-7401-4cf4-9674-af307a6703ef

📥 Commits

Reviewing files that changed from the base of the PR and between 86aa703 and b2157a2.

📒 Files selected for processing (6)
  • src/__tests__/errors.test.ts
  • src/__tests__/server.test.ts
  • src/core/api.ts
  • src/core/errors.ts
  • src/providers/discord/adapter.ts
  • src/providers/slack/adapter.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chr1syy chr1syy merged commit 0f78db3 into RunMaestro:main Jun 7, 2026
3 checks passed
chr1syy added a commit that referenced this pull request Jun 7, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants