Skip to content

Commit 5a6c32c

Browse files
committed
fix(core): restrict oversized recovery to thrown failures with HTTP status
1 parent 86b67f8 commit 5a6c32c

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎packages/ai/test/provider-error.test.ts‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2-
import { AIError, isContextOverflow, isContextOverflowFailure, isPayloadTooLargeFailure } from "../src/index.js"
2+
import { isContextOverflow } from "../src/index.js"
33
import { classifyProviderFailure, providerErrorMessage } from "../src/provider-error.js"
44

55
describe("provider error classification", () => {
@@ -43,18 +43,6 @@ describe("provider error classification", () => {
4343
)
4444
})
4545

46-
test("leaves generic client rejections unclassified", () => {
47-
const reason = classifyProviderFailure({
48-
message: "The request contains invalid parameters. Check the request body for any errors or inconsistencies.",
49-
status: 400,
50-
rawBody:
51-
'{"model":"example","error":{"code":null,"message":"The request contains invalid parameters.","param":null,"type":"invalid_request_error"}}',
52-
})
53-
// Recovery of these goes through the oversized-body gate in the runner, not classification.
54-
expect(isPayloadTooLargeFailure(new AIError({ reason }))).toBe(false)
55-
expect(isContextOverflowFailure(new AIError({ reason }))).toBe(false)
56-
})
57-
5846
test("does not classify rate limits as context overflow", () => {
5947
const messages = [
6048
"Throttling error: Too many tokens, please wait before trying again.",

‎packages/core/src/session/runner/step.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ const isRequestOverflowFailure = (failure: unknown) =>
7272
* Proxies and gateways often reject oversized bodies with an unclassified 4xx instead of a size
7373
* signal, while media counts flat toward tokens and dominates bytes. An unclassified client
7474
* rejection against a huge request is most likely size-caused, so it recovers like a payload
75-
* rejection. Genuine client bugs stay untouched: small requests never qualify.
75+
* rejection. Genuine client bugs stay untouched: small requests never qualify. Only thrown
76+
* failures carry HTTP status, so streamed provider errors are left to the classified paths.
7677
*/
7778
const isOversizedRejection = (failure: unknown, requestBytes: number) =>
7879
requestBytes >= OVERSIZED_REQUEST_BYTES &&
79-
((failure instanceof AIError &&
80-
failure.reason._tag === "InvalidRequest" &&
81-
failure.reason.classification === undefined &&
82-
failure.reason.http !== undefined &&
83-
failure.reason.http.status >= 400 &&
84-
failure.reason.http.status < 500) ||
85-
(LLMEvent.is.providerError(failure) && failure.classification === undefined))
80+
failure instanceof AIError &&
81+
failure.reason._tag === "InvalidRequest" &&
82+
failure.reason.classification === undefined &&
83+
failure.reason.http !== undefined &&
84+
failure.reason.http.status >= 400 &&
85+
failure.reason.http.status < 500
8686

8787
/** Captures Location-scoped dependencies without introducing another service or execution loop. */
8888
export const make = Effect.gen(function* () {

‎packages/core/test/session-runner.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,26 @@ const oversizedImageUri = () => {
33503350
expect(yield* s.context).not.toContainEqual(expect.objectContaining({ type: "compaction" }))
33513351
}, 30_000)
33523352

3353+
scenario("does not recover a streamed unclassified error for an oversized request", function* (s) {
3354+
yield* setupOverflowRecovery(s)
3355+
yield* s.session.prompt({
3356+
sessionID,
3357+
text: "Inspect the screenshot",
3358+
files: [{ uri: oversizedImageUri() }],
3359+
resume: false,
3360+
})
3361+
// Streamed provider errors carry no HTTP status, so the size gate cannot tell them
3362+
// apart from server-side failures: they must not trigger compaction.
3363+
yield* s.llm.push([
3364+
LLMEvent.providerError({ message: "The request contains invalid parameters. Check the request body for any errors or inconsistencies." }),
3365+
])
3366+
expect((yield* s.runPrompt("Continue").pipe(Effect.flip)).message).toBe(
3367+
"The request contains invalid parameters. Check the request body for any errors or inconsistencies.",
3368+
)
3369+
expect(s.requests).toHaveLength(1)
3370+
expect(yield* s.context).not.toContainEqual(expect.objectContaining({ type: "compaction" }))
3371+
}, 30_000)
3372+
33533373
scenario("refreshes preparation after overflow compaction without promoting new input", function* (s) {
33543374
yield* setupOverflowRecovery(s)
33553375

0 commit comments

Comments
 (0)