Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(codex): strip stale compression framing on the Responses subpath passthrough (#3792)

Abhay Singh0eba65ca4a8f
diff --git a/headroom/providers/codex/responses.py b/headroom/providers/codex/responses.py
index 34423ac75..45fce1037 100644
--- a/headroom/providers/codex/responses.py
+++ b/headroom/providers/codex/responses.py
@@ -8,6 +8,7 @@ from typing import Any, Protocol
from fastapi import Request
from fastapi.responses import Response
+from starlette.requests import ClientDisconnect
from .endpoints import codex_backend_url, codex_backend_ws_url
from .headers import drop_header, header_name
@@ -41,10 +42,31 @@ def normalize_codex_responses_headers(headers: Mapping[str, str]) -> tuple[dict[
"""Prepare inbound headers for ChatGPT Codex Responses passthrough."""
upstream_headers = dict(headers)
drop_header(upstream_headers, "host")
+ # Drop the client's Accept-Encoding so the upstream returns plaintext. httpx
+ # decodes the body before we read ``resp.content``, so if we let the upstream
+ # gzip we would forward a decoded body under a ``content-encoding: gzip`` /
+ # compressed ``content-length`` (see sanitize below) -- the #3019 framing
+ # corruption. Mirrors the image passthrough.
+ drop_header(upstream_headers, "accept-encoding")
decision = resolve_codex_routing(upstream_headers)
return decision.headers, decision.is_chatgpt_auth
+def sanitize_codex_responses_response_headers(headers: Mapping[str, str]) -> dict[str, str]:
+ """Drop stale compression/framing headers after materializing response bytes.
+
+ ``resp.content`` is already-decoded plaintext, so the upstream's
+ ``content-encoding`` and ``content-length`` (measured on the compressed
+ bytes) no longer describe what we forward; replaying them makes the client
+ fail to gunzip or truncate the body. Starlette recomputes ``content-length``.
+ """
+ response_headers = dict(headers)
+ drop_header(response_headers, "content-encoding")
+ drop_header(response_headers, "content-length")
+ drop_header(response_headers, "server")
+ return response_headers
+
+
def codex_responses_subpath_url(sub_path: str, query: str = "") -> str:
"""Return the ChatGPT Codex Responses upstream URL for a route subpath."""
return codex_backend_url(f"/responses/{sub_path}", query)
@@ -75,7 +97,11 @@ async def handle_chatgpt_codex_responses_subpath(
if not is_chatgpt_auth:
return None
- body = await request.body()
+ try:
+ body = await request.body()
+ except ClientDisconnect:
+ logger.debug("Client disconnected during body read for /v1/responses passthrough")
+ return Response(status_code=204)
try:
resp = await http_client.request(
request.method,
@@ -87,7 +113,7 @@ async def handle_chatgpt_codex_responses_subpath(
return Response(
content=resp.content,
status_code=resp.status_code,
- headers=dict(resp.headers),
+ headers=sanitize_codex_responses_response_headers(resp.headers),
)
except Exception:
logger.exception("Passthrough /v1/responses/%s failed", sub_path)
diff --git a/tests/test_provider_codex_responses.py b/tests/test_provider_codex_responses.py
index 5e67fb5a0..119dda390 100644
--- a/tests/test_provider_codex_responses.py
+++ b/tests/test_provider_codex_responses.py
@@ -1,9 +1,13 @@
+import pytest
+
from headroom.providers.codex.responses import (
codex_responses_http_url,
codex_responses_subpath_url,
codex_responses_websocket_url,
+ handle_chatgpt_codex_responses_subpath,
has_chatgpt_account_header,
normalize_codex_responses_headers,
+ sanitize_codex_responses_response_headers,
)
@@ -57,3 +61,112 @@ def test_codex_responses_headers_return_false_for_regular_openai_auth() -> None:
assert is_chatgpt_auth is False
assert headers == {"authorization": "Bearer sk-proj-test"}
assert has_chatgpt_account_header(headers) is False
+
+
+def test_codex_responses_headers_drop_accept_encoding() -> None:
+ """accept-encoding must be dropped so the upstream returns plaintext; httpx
+ decodes the body before we read it, so a gzip upstream would corrupt framing."""
+ headers, _ = normalize_codex_responses_headers(
+ {
+ "Host": "localhost:8787",
+ "authorization": "Bearer token",
+ "chatgpt-account-id": "acct",
+ "Accept-Encoding": "gzip, deflate, br",
+ }
+ )
+ assert "accept-encoding" not in {k.lower() for k in headers}
+
+
+def test_codex_responses_response_headers_drop_stale_framing_case_insensitive() -> None:
+ assert sanitize_codex_responses_response_headers(
+ {
+ "Content-Encoding": "gzip",
+ "Content-Length": "9999",
+ "Server": "cloudflare",
+ "Content-Type": "application/json",
+ "x-request-id": "kept",
+ }
+ ) == {
+ "Content-Type": "application/json",
+ "x-request-id": "kept",
+ }
+
+
+class _FakeResp:
+ def __init__(self, content: bytes, status_code: int, headers: dict[str, str]):
+ self.content = content
+ self.status_code = status_code
+ self.headers = headers
+
+
+class _FakeClient:
+ def __init__(self, resp: _FakeResp):
+ self._resp = resp
+ self.last_headers: dict[str, str] | None = None
+
+ async def request(self, method, url, *, headers, content, timeout):
+ self.last_headers = headers
+ return self._resp
+
+
+def _request(method: str, path: str, headers: dict[str, str], body: bytes):
+ from starlette.requests import Request
+
+ raw = [(k.lower().encode(), v.encode()) for k, v in headers.items()]
+ scope = {
+ "type": "http",
+ "method": method,
+ "path": path,
+ "query_string": b"",
+ "headers": raw,
+ }
+ sent = False
+
+ async def receive():
+ nonlocal sent
+ if sent:
+ return {"type": "http.request", "body": b"", "more_body": False}
+ sent = True
+ return {"type": "http.request", "body": body, "more_body": False}
+
+ return Request(scope, receive)
+
+
+async def test_subpath_passthrough_strips_upstream_compression_framing() -> None:
+ """The upstream body is already-decoded plaintext, so the forwarded response
+ must not replay content-encoding / a compressed content-length (#3019)."""
+ plaintext = b'{"id":"resp_1","status":"completed"}'
+ upstream = _FakeResp(
+ content=plaintext,
+ status_code=200,
+ headers={
+ "content-encoding": "gzip", # stale: content is already decoded
+ "content-length": "17", # stale: compressed length
+ "content-type": "application/json",
+ "x-request-id": "req-123",
+ },
+ )
+ client = _FakeClient(upstream)
+ request = _request(
+ "GET",
+ "/v1/responses/resp_1",
+ {
+ "host": "localhost:8787",
+ "authorization": "Bearer token",
+ "chatgpt-account-id": "acct",
+ "accept-encoding": "gzip",
+ },
+ body=b"",
+ )
+
+ response = await handle_chatgpt_codex_responses_subpath(client, request, "resp_1")
+
+ assert response is not None
+ assert response.status_code == 200
+ assert response.body == plaintext
+ out = {k.lower() for k, _ in response.raw_headers}
+ assert b"content-encoding" not in out
+ # accept-encoding was stripped before the upstream call.
+ assert client.last_headers is not None
+ assert "accept-encoding" not in {k.lower() for k in client.last_headers}