fix(cloud-agent-next): classify failures from APIError response body - #6796
Conversation
…esponse body The AI SDK's APIError often carries no statusCode, only the raw provider responseBody. Parse that body (bounded, JSON object only) to recover the gateway status code and error_type token, and recognize AI_InvalidResponseDataError as a provider_unavailable failure.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe zod-based provider-body parser in Files Reviewed (2 files)
Previous Review Summary (commit e16cfba)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit e16cfba)Status: No Issues Found | Recommendation: Merge Executive SummaryThe provider-body failure classification is bounded, prototype-safe, precedence-tested, and does not surface the raw Files Reviewed (2 files)
Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0 Review guidance: REVIEW.md from base branch |
chrarnoldus
left a comment
There was a problem hiding this comment.
maybe some of this can be simplified using zod?
Replace the hand-rolled object and numeric-status guards with a small zod schema. Each field is parsed independently via .catch(() => undefined) so a malformed sibling (e.g. a string code) does not discard a valid error_type; a test pins that per-field tolerance.
Summary
The AI SDK's
APIErrorfrequently carries nostatusCode, only the raw providerresponseBody. Without reading it, known provider failures collapsed into the generic "Assistant request failed" (unknown) bucket.classifySdkStatusnow parses the response body (bounded to 64 KiB, JSON object only) to recover the cause:error_typetoken, at the top level or undermetadata, mapped through a bounded allowliststatus/statusCode/codenamesuch asAI_InvalidResponseDataErrorAn explicit, well-formed
statusCodestill wins over the body. An unusable body (not json,'',[],null) staysunknown.Verification
pnpm exec vitest run src/session/safe-failure-projection.test.ts— 187 passed, including new cases for 502 provider bodies, context-overflow tokens, top-levelerror_type, status precedence, and unusable bodies.pnpm run typecheck(tsgo + wrapper) — passed.pnpm run lint— 0 warnings, 0 errors.pnpm run format:check— clean.Visual Changes
N/A
Reviewer Notes
The
error_typemap uses aMaprather than an object so a caller-suppliedconstructorkey cannot resolve an inherited member. The classification stays in the single shared owner (assistant-failure.ts) used by both the safe-failure projection and the control-plane run classifier.