Skip to content

Commit c2f990f

Browse files
committed
fix(core): narrow oversized recovery to generic rejections
1 parent 291c922 commit c2f990f

2 files changed

Lines changed: 63 additions & 13 deletions

File tree

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,42 @@ const RESULT_MISSING = { type: "tool.result-missing", message: "Provider did not
6868
const isRequestOverflowFailure = (failure: unknown) =>
6969
isContextOverflowFailure(failure) || isPayloadTooLargeFailure(failure)
7070

71+
// 400s that name a concrete client bug must never trigger size recovery, even on
72+
// huge requests: corrupt attachments (#53014), bad tool schemas, unsupported
73+
// params. The #54690 gateway returns a generic message with null code, so only
74+
// generic rejections qualify.
75+
const NON_SIZE_REJECTION_PATTERNS = [
76+
/corrupt/i,
77+
/badly formatted/i,
78+
/media[- ]?rejected/i,
79+
/invalid (?:tool|function|arguments?|schema)/i,
80+
/missing (?:key|parameter|argument|required)/i,
81+
/validation (?:error|failed)/i,
82+
/unsupported/i,
83+
/unknown (?:tool|model|parameter|param)/i,
84+
/content[- ]?policy|violating our usage policy|safety system/i,
85+
/incorrect api key|permission_denied|permission_error|api_key_invalid/i,
86+
/rate[-_\s]?limit|too[_\s]?many[_\s]?requests|throttl/i,
87+
/server[-_\s]?error|overloaded|temporarily unavailable|try again/i,
88+
]
89+
7190
/**
7291
* Proxies and gateways often reject oversized bodies with an unclassified 4xx instead of a size
73-
* signal, while media counts flat toward tokens and dominates bytes. An unclassified client
74-
* 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. Only thrown
76-
* failures carry HTTP status, so streamed provider errors are left to the classified paths.
92+
* signal, while media counts flat toward tokens and dominates bytes. An unclassified, generic
93+
* client rejection against a huge request is most likely size-caused, so it recovers like a
94+
* payload rejection. Named client bugs (corrupt file, bad tool schema, unsupported param) and
95+
* small requests never qualify. Only thrown failures carry HTTP status, so streamed provider
96+
* errors are left to the classified paths.
7797
*/
78-
const isOversizedRejection = (failure: unknown, requestBytes: number) =>
79-
requestBytes >= OVERSIZED_REQUEST_BYTES &&
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
98+
const isOversizedRejection = (failure: unknown, requestBytes: number) => {
99+
if (requestBytes < OVERSIZED_REQUEST_BYTES) return false
100+
if (!(failure instanceof AIError)) return false
101+
if (failure.reason._tag !== "InvalidRequest" || failure.reason.classification !== undefined) return false
102+
const http = failure.reason.http
103+
if (http === undefined || http.status < 400 || http.status >= 500) return false
104+
const text = [failure.reason.message, failure.reason.body ?? ""].join("\n")
105+
return !NON_SIZE_REJECTION_PATTERNS.some((pattern) => pattern.test(text))
106+
}
86107

87108
/** Captures Location-scoped dependencies without introducing another service or execution loop. */
88109
export const make = Effect.gen(function* () {
@@ -135,7 +156,7 @@ export const make = Effect.gen(function* () {
135156
if (!publisher.hasStarted()) yield* Fiber.join(pendingStartSnapshot)
136157
if (
137158
LLMEvent.is.providerError(event) &&
138-
(isRequestOverflowFailure(event) || isOversizedRejection(event, requestBytes())) &&
159+
isRequestOverflowFailure(event) &&
139160
!publisher.record().outputStarted
140161
) {
141162
overflowFailure = event

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,35 @@ const oversizedImageUri = () => {
34443444
expect(yield* s.context).not.toContainEqual(expect.objectContaining({ type: "compaction" }))
34453445
}, 30_000)
34463446

3447+
scenario("does not recover a named client bug on an oversized request", function* (s) {
3448+
yield* setupOverflowRecovery(s)
3449+
yield* s.session.prompt({
3450+
sessionID,
3451+
text: "Inspect the screenshot",
3452+
files: [{ uri: oversizedImageUri() }],
3453+
resume: false,
3454+
})
3455+
// Corrupt attachments (#53014) and bad tool schemas name the bug: size
3456+
// recovery must not silently summarize the session for those.
3457+
for (const message of [
3458+
"The file you uploaded is badly formatted or corrupted.",
3459+
'Invalid arguments for tool "shell": command: Missing key',
3460+
]) {
3461+
yield* s.llm.push(
3462+
Stream.fail(
3463+
new AIError({
3464+
reason: new InvalidRequestError({
3465+
message,
3466+
http: new HttpContext({ url: "https://gateway.example/v1/responses", status: 400, headers: {} }),
3467+
}),
3468+
}),
3469+
),
3470+
)
3471+
expect((yield* s.runPrompt("Continue").pipe(Effect.flip)).message).toBe(message)
3472+
expect(yield* s.context).not.toContainEqual(expect.objectContaining({ type: "compaction" }))
3473+
}
3474+
}, 30_000)
3475+
34473476
scenario("refreshes preparation after overflow compaction without promoting new input", function* (s) {
34483477
yield* setupOverflowRecovery(s)
34493478

0 commit comments

Comments
 (0)