Skip to content

feat(driver): surface structured rate-limit/credits errors from SDK message stream (#732) - #735

Merged
admarble merged 3 commits into
mainfrom
feature/732-feat-driver-surface-structured-rate-limit-credits-
Jun 21, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/732-feat-driver-surface-structured-rate-limit-credits-

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • The Claude Agent SDK already emits structured rate_limit_event (SDKRateLimitInfo) and assistant error signals, but sequant dropped them at the ClaudeCodeDriver stream chokepoint and guessed rate limits via regex-on-stderr. The driver now reads these signals and constructs a typed RateLimitError (retryable) or BillingError (non-retryable).
  • The typed cause propagates through AgentPhaseResult.structuredError → PhaseResult.structuredError, so a failed phase names the real cause ("Out of credits — purchasable", "Rate limited — resets at HH:MM") instead of the generic, misleading Phase failed with MCP enabled, retrying without MCP... noise (fix(run): pre-flight guard misses in_progress issues already merged on GitHub (gap in #305/#423) #592).
  • A billing/credits failure now skips the no-MCP retry — a retry cannot refill credits. The stderr-regex classifier is retained as the subprocess (run.ts) fallback, so non-SDK classification is unchanged.

Pre-PR AC Verification

AC Source Description Status Evidence
AC-1 Original Stream loop reads rate_limit_event + assistant error instead of discarding ✅ claude-code.ts capture vars + claude-code.test.ts
AC-2 Original RateLimitError/BillingError extend SequantError, carry resetsAt+overageDisabledReason, correct isRetryable ✅ errors.ts; rate-limit-errors.test.ts
AC-3 Original User-facing message names the real cause ✅ driver sets error: structuredError.message; phase-executor.test.ts
AC-4 Original Billing/credits failure does NOT trigger "retrying without MCP" (#592) ✅ BillingError guard at MCP fallback; phase-executor.test.ts (2 tests)
AC-5 Original Stderr-regex classifier still works for subprocess path — no regression ✅ error-types.test.ts + error-capture.test.ts unchanged & green
AC-6 Original Unit coverage drives new message types through the loop ✅ claude-code.test.ts (rate_limit_event, assistant.error, api_retry)
AC-7 Original ≥0.3.181 enrichment (canUserPurchaseCredits/hasChargeableSavedPaymentMethod) with graceful gating ✅ formatRateLimitMessage; rate-limit-errors.test.ts
AC-8 Part 3 Working tree synced to origin/main, SDK 0.3.181 installed ✅ Precondition — already met (SDK sdk.d.ts exposes the 0.3.181 fields)

Test plan

  • npm run build — passes (tsc clean)
  • npm run lint — passes (0 warnings)
  • Targeted tests: rate-limit-errors, claude-code, phase-executor, error-types, error-capture — 218 passed
  • Workflow-layer sweep — 41 files, 1003 passed / 2 skipped

Closes #732

🤖 Generated with Claude Code

…tream (#732)

Read the SDK's rate_limit_event + assistant error signals at the
ClaudeCodeDriver stream chokepoint instead of discarding them and
guessing rate limits via regex-on-stderr.

- Add RateLimitError (retryable) and BillingError (non-retryable)
  SequantError subclasses carrying resetsAt, overageDisabledReason,
  and (SDK >=0.3.181) canUserPurchaseCredits /
  hasChargeableSavedPaymentMethod.
- Driver captures rate_limit_event / assistant.error / api_retry and
  builds the typed error; propagate via AgentPhaseResult.structuredError
  -> PhaseResult.structuredError.
- phase-executor prefers structuredError over stderr classification and
  skips the misleading no-MCP retry (#592) for billing failures.
- Stderr-regex classifier retained as the subprocess (run.ts) fallback.

Entire-Checkpoint: dde8c86f8a63
admarble added 2 commits June 20, 2026 11:47
…ling guard var (#732)

Addresses the non-blocking QA suggestions on PR #735:

- ClaudeCodeDriver catch block now builds and attaches structuredError
  from captured rate_limit_event / assistant.error signals, so a
  mid-stream throw after a rejected rate-limit/billing event surfaces the
  real cause instead of the raw thrown message. Abort/timeout is still
  handled first, so a genuine timeout is never masked.
- Rename the local `isBillingFailure` guard in phase-executor to
  `failureIsBilling` to avoid name overlap with the exported
  isBillingFailure(info) predicate.
- Add driver tests for both catch-path branches (structured cause vs
  abort-wins).

Entire-Checkpoint: c4ff63eb64f0
… over transient throttle (#732)

QA follow-up addressing two gaps surfaced in re-review:

1. formatResetTime now renders "MM-DD HH:MM" when a reset falls on a
   different local calendar day, and bare "HH:MM" when it is today. A
   seven_day rate-limit window can reset days out; the previous bare
   HH:MM read as "later today" and misled the user.

2. buildStructuredError now lets a billing_error assistant signal win
   over a co-occurring transient rate_limit_event, so a non-retryable
   billing failure is never downgraded to a retryable RateLimitError
   (which would wrongly re-enable the retry / MCP-fallback path). When
   the rate_limit_event is itself a billing failure, its richer
   metadata (canUserPurchaseCredits, etc.) is preserved.

Adds same-day/multi-day reset tests and a billing-precedence driver test.

Entire-Checkpoint: a023631cff45
@admarble
admarble merged commit 27f8106 into main Jun 21, 2026
4 checks passed
admarble added a commit that referenced this pull request Sep 9, 2026
…ling guard var (#732)

Addresses the non-blocking QA suggestions on PR #735:

- ClaudeCodeDriver catch block now builds and attaches structuredError
  from captured rate_limit_event / assistant.error signals, so a
  mid-stream throw after a rejected rate-limit/billing event surfaces the
  real cause instead of the raw thrown message. Abort/timeout is still
  handled first, so a genuine timeout is never masked.
- Rename the local `isBillingFailure` guard in phase-executor to
  `failureIsBilling` to avoid name overlap with the exported
  isBillingFailure(info) predicate.
- Add driver tests for both catch-path branches (structured cause vs
  abort-wins).

Entire-Checkpoint: c4ff63eb64f0
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.

feat(driver): surface structured rate-limit/credits errors from SDK message stream (replaces #592 fallback noise)

1 participant