openAIRequestBodyHasImageGenerationDeclaration and
openAIRequestBodyImageGenerationToolNeedsNormalization each validate the whole
body up front before looking at tools/input/tool_choice. Both branches only
match image_gen literals, so the same byte-level necessary condition applies
and lets them skip the validation for bodies that cannot match.
Refs #8865
The handler validates the request body at gateway_handler_responses.go:66 and
ParseGatewayRequest validates it again at gateway_request.go:240. For bodies
that do carry image declarations the byte-level precheck cannot fire, so the
intent classifier still ran gjson.ValidBytes over the same bytes a third time.
That is the dominant remaining cost for Codex traffic, because Codex
advertises the image_gen namespace on ordinary responses requests.
Add IsImageGenerationIntentForPlatformValidated for callers that have already
established the body is valid JSON, and use it at the two call sites where
that holds:
- gateway_handler_responses.go: the body is validated at the top of the
handler, and afterwards it is only rewritten through sjson
(applyOpenAIReasoningEffortPolicyForRequest), which preserves validity.
- openai_ws_forwarder_ingress.go: the payload is validated in
parseClientPayload, and every later rewrite is sjson, a decode/re-marshal
round trip, or a no-op.
Both call sites carry the reasoning in a comment. The validating entry points
are unchanged, so invalid-JSON semantics stay exactly as they were for every
other caller.
Benchmark (Apple M5 Pro, 1MiB Codex body, 0 allocs):
intent classifier: 562us -> 50us
Refs #8865
Every /v1/responses request runs IsImageGenerationIntentForPlatform to decide
whether to mark the context as image generation (capability routing and image
billing). After the two O(1) checks (dedicated image endpoint, image model) the
function unconditionally runs gjson.ValidBytes over the whole body and then
walks the root object with a ForEach.
Both steps are pure overhead for requests that cannot match. Every branch that
can set the intent requires one of two things in the body: the literal
"image_gen" (image_generation tools carry that prefix, and the namespace,
tool_choice and function-reference checks all use image_gen), or an image model
name (gpt-image-*, grok-imagine*) as the "model" value. The same body is also
validated by the handler and again by ParseGatewayRequest, so this pass is a
third redundant validation of the same bytes.
Add a byte-level necessary condition ahead of gjson.ValidBytes:
bytes.Contains(body, "image_gen")
|| containsASCIIFold(body, "imag") // image model names
|| bytes.Contains(body, "\\u") // unicode-escaped literal
All three are required:
- "image_gen" alone misses the model branch. {"model":"gpt-image-2"} returns
true today and does not contain "image_gen"; dropping it would let image
model requests bypass the group image-generation gate and image billing.
- isOpenAIImageGenerationModel lowercases its argument while bytes.Contains
does not, so the model token is matched case-insensitively.
- JSON strings may spell any byte as a unicode escape, so a body containing
"image_generation" only reveals the literal after decoding. Escaped
bodies therefore fall back to the full scan. None of the target literals
contain a character JSON escapes, so a "\u" probe is exhaustive.
containsASCIIFold had to be rewritten as well. It compared byte by byte, which
is a full body scan on its own, and a first-byte memchr variant that re-probes
both cases of the first byte per candidate degrades quadratically. The new
implementation keeps one cursor per case and advances only the cursor it just
consumed, so it stays linear. Without this the precheck would cost more than
the scan it replaces (~930us vs 783us on a 1MiB body).
The three scan bodies collapse into two root walkers plus one entry point; the
grok and explicit rules were byte-identical copies.
Codex clients advertise the image_gen namespace on ordinary requests, so those
bodies still take the full scan. Skipping the duplicate validation for callers
that already validated the same bytes is a separate change.
Benchmark (Apple M5 Pro, 1MiB body, 0 allocs):
no image declarations 783us -> 48us
codex namespace 560us -> 564us (gate cannot fire, unchanged)
Fixes#8865
Keep the Responses hot path on raw request bytes until complex mutation or retry branches need a decoded map, reducing large-body retention under high concurrency.