Copilot CLI 1.0.106+ and VS Code Copilot Chat send request bodies with
`Content-Encoding: zstd`. AI Gateway never decoded them, so it parsed
the compressed bytes as JSON and returned 500 on every request to
`/v1/messages` and `/responses`. Because interception creation failed,
no sessions were recorded.
`newInterceptionProcessor` now calls a new `routing.DecodeRequestBody`
before anything reads the body, including client and session detection.
It decodes `zstd`, `gzip`, `br` and `deflate`, caps the decoded body at
`MaxRequestBodyBytes` (the existing 32 MiB limit only applies to the
compressed bytes), and removes `Content-Encoding` and `Content-Length`
so the re-serialised JSON is not forwarded upstream with a stale
encoding. Unknown or stacked encodings return 415, corrupt data returns
400, and a body that decodes past the limit returns 413.
Passthrough routes, the experimental reverse-proxy mode
(`aibridge/x/proxy`) and AI Gateway Proxy forward raw bytes without
parsing them, so they are unchanged.
Fixes#32208
Closes AIGOV-812
<details>
<summary>Plan and decisions</summary>
**Findings**
- All bridged routes for every provider pass through
`newInterceptionProcessor` in `aibridge/bridge.go`, so one call site
covers Copilot, OpenAI, Anthropic and Bedrock.
- `headers.PrepareClientHeaders` forwards `Content-Encoding`, so the
header must be deleted after decoding.
- `http.MaxBytesHandler` limits compressed bytes only, so the decoded
output needs its own cap.
- `klauspost/compress` and `andybalholm/brotli` are already direct
dependencies.
- Bedrock SigV4 hashes the SDK-built body after decoding, so signing is
unaffected.
- If AIGOV-786 (replayable body capture in `x/proxy`) starts parsing
bodies, it will need the same decoding.
**Decisions**
- Support `zstd`, `gzip`, `br` and `deflate`, as the reporter requested.
`deflate` is decoded as zlib per RFC 9110.
- Reject unknown or stacked encodings (comma-separated or on repeated
header lines) with 415 instead of passing them through, because the
gateway cannot parse the body.
- Decode before `GuessSessionID`, which reads the body for Claude Code.
**Tests**
- `routing/body_test.go`: each encoding, case-insensitive names, and
unsupported, comma-separated and repeated-header encodings.
- `TestCompressedRequestBody`: zstd bodies on Copilot `/v1/messages` and
`/responses` reach upstream as plain JSON with no `Content-Encoding`,
and the interception is recorded.
- `TestCompressedRequestBodyRejected`: 415, 400 and 413 at the bridge,
and nothing reaches upstream.
</details>
---
Code written by @mollymaefraser, with Coder Agents used as a guide.