@@ -68,21 +68,42 @@ const RESULT_MISSING = { type: "tool.result-missing", message: "Provider did not
6868const 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+ / c o r r u p t / i,
77+ / b a d l y f o r m a t t e d / i,
78+ / m e d i a [ - ] ? r e j e c t e d / i,
79+ / i n v a l i d (?: t o o l | f u n c t i o n | a r g u m e n t s ? | s c h e m a ) / i,
80+ / m i s s i n g (?: k e y | p a r a m e t e r | a r g u m e n t | r e q u i r e d ) / i,
81+ / v a l i d a t i o n (?: e r r o r | f a i l e d ) / i,
82+ / u n s u p p o r t e d / i,
83+ / u n k n o w n (?: t o o l | m o d e l | p a r a m e t e r | p a r a m ) / i,
84+ / c o n t e n t [ - ] ? p o l i c y | v i o l a t i n g o u r u s a g e p o l i c y | s a f e t y s y s t e m / i,
85+ / i n c o r r e c t a p i k e y | p e r m i s s i o n _ d e n i e d | p e r m i s s i o n _ e r r o r | a p i _ k e y _ i n v a l i d / i,
86+ / r a t e [ - _ \s ] ? l i m i t | t o o [ _ \s ] ? m a n y [ _ \s ] ? r e q u e s t s | t h r o t t l / i,
87+ / s e r v e r [ - _ \s ] ? e r r o r | o v e r l o a d e d | t e m p o r a r i l y u n a v a i l a b l e | t r y a g a i n / 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. */
88109export 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
0 commit comments