Commit Graph
2 Commits
Author SHA1 Message Date
Abhay SinghandJD Davis 0eba65ca4a fix(codex): strip stale compression framing on the Responses subpath passthrough (#3792)
## Description

`handle_chatgpt_codex_responses_subpath` (the ChatGPT-auth passthrough
for `/v1/responses/{id}` subpaths — retrieve, cancel, etc.) forwarded
the upstream response verbatim:

```python
return Response(content=resp.content, status_code=resp.status_code, headers=dict(resp.headers))
```

`resp.content` (httpx) is the **already-decoded** plaintext body, but
`dict(resp.headers)` still carries the upstream's `content-encoding:
gzip` and its (compressed) `content-length`. And
`normalize_codex_responses_headers` dropped only `host`, leaving the
client's `accept-encoding` intact so the upstream was free to gzip.

So a Codex client sending `Accept-Encoding: gzip` that hits a responses
subpath under a ChatGPT-auth account gets a **decoded** body labelled
`content-encoding: gzip` with a **compressed** `content-length` — the
client's gunzip fails, or the shorter length truncates the body. This is
the exact framing-corruption class the codebase already documents (#3019
/ `helpers.FRAMING_RESPONSE_HEADERS`).

The sibling **image** passthrough (`providers/codex/images.py`) already
handles this: `normalize_codex_image_headers` drops `accept-encoding`,
and `sanitize_codex_image_response_headers` drops
`content-encoding`/`content-length`/`server` before returning. The
responses subpath was never given the same treatment.

This mirrors images.py:
- drop `accept-encoding` in `normalize_codex_responses_headers` so the
upstream returns plaintext;
- add `sanitize_codex_responses_response_headers` (drops
`content-encoding`/`content-length`/`server`) and return through it
(Starlette recomputes `content-length`);
- guard the `await request.body()` read with `ClientDisconnect` → `204`,
matching images.py, so a disconnect during body read no longer 500s.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/providers/codex/responses.py`:
`normalize_codex_responses_headers` now drops `accept-encoding`; new
`sanitize_codex_responses_response_headers` drops stale framing headers;
`handle_chatgpt_codex_responses_subpath` returns through the sanitizer
and wraps the body read in a `ClientDisconnect` → 204 guard.
- `tests/test_provider_codex_responses.py`: added a header-drop test
(`accept-encoding` removed), a sanitizer test (drops
`content-encoding`/`content-length`/`server`, case-insensitive, keeps
others), and an end-to-end handler test driving a fake gzip-framing
upstream and asserting the forwarded response has no `content-encoding`,
preserves the plaintext body, and that `accept-encoding` was stripped
from the upstream call.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality

### Test Output

```text
tests/test_provider_codex_responses.py  ->  7 passed
uvx ruff@0.16.8 check headroom/providers/codex/responses.py tests/test_provider_codex_responses.py  ->  All checks passed!
uvx mypy@1.20.2 headroom/providers/codex/responses.py  ->  clean (no error in this file)
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.12.11, project venv, pytest 9.1.1,
ruff 0.16.8 and mypy 1.20.2 via uvx.
- Exact command / steps: drove `handle_chatgpt_codex_responses_subpath`
with a fake upstream returning plaintext JSON under `content-encoding:
gzip` + a compressed `content-length`. The forwarded `Response` carries
the plaintext body with no `content-encoding` header, and the upstream
request was made without `accept-encoding`. Verified the sibling image
passthrough was already correct.
- Observed result: the client receives a well-framed plaintext response
instead of a decoded body mislabelled as gzip.
- Not tested: a live ChatGPT-Codex account round-trip (requires
ChatGPT-auth credentials); the framing behavior is covered by the
fake-upstream handler test.

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: only the forwarded framing headers on
Codex `/v1/responses/{id}` subpath passthrough — stale compression
headers are dropped, matching the image passthrough. The body content is
unchanged.
- Kill switch / disable path: N/A.
- Unsafe override required: no.
- Qualification impact: none.
- Rollback path: revert this commit; the subpath again replays upstream
framing headers.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation (N/A:
internal passthrough framing)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

Kept the sanitizer local to the codex module
(`sanitize_codex_responses_response_headers`) to mirror
`sanitize_codex_image_response_headers` exactly, rather than reaching
for `helpers.sanitize_forwarded_response_headers`, so the two Codex
passthroughs stay symmetric and independently readable.

Co-authored-by: JD Davis <mxjerrett@gmail.com>
2026-09-30 11:15:24 -05:00
JD DavisandCopilot Autofix powered by AI e6243f65c9 refactor(providers): split proxy route adapters (#1934)
## Description
Refactors provider-specific proxy routing into provider-owned helper
modules so `headroom/providers/proxy_routes.py` primarily registers
routes and delegates behavior. This keeps Codex, OpenAI
Responses/images, model metadata, Vertex, Cloud Code, passthrough target
selection, and request path normalization logic testable outside the
route table.

Closes #

## Type of Change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation update
- [ ] Performance improvement
- [x] Code refactoring (no functional changes)

## Changes Made
- Extracted Codex routing helpers for headers, endpoint URLs, image
forwarding, response subpaths, and model metadata.
- Moved provider target selection, route specs, OpenAI Responses/images
helpers, Vertex runtime helpers, Cloud Code path normalization,
passthrough telemetry, and request scope normalization into focused
modules.
- Kept `proxy_routes.py` as route registration/delegation and preserved
current-main `/v1/messages` custom-base behavior.
- Added focused provider/proxy tests for the extracted modules and route
delegation behavior.

## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output
```text
python -m pytest tests/test_package_init_lazy.py::test_codex_package_import_stays_runtime_only tests/test_provider_cloudcode_runtime.py tests/test_provider_codex_endpoints.py tests/test_provider_codex_headers.py tests/test_provider_codex_images.py tests/test_provider_codex_model_metadata.py tests/test_provider_codex_responses.py tests/test_provider_model_metadata.py tests/test_provider_openai_images.py tests/test_provider_openai_responses.py tests/test_provider_proxy_targets.py tests/test_provider_route_specs.py tests/test_provider_vertex_runtime.py tests/test_proxy_request_scope.py tests/test_provider_proxy_routes.py::test_provider_passthrough_routes_forward_expected_targets tests/test_provider_proxy_routes.py::test_proxy_route_helpers_prefer_legacy_targets_and_gemini_passthrough tests/test_provider_proxy_routes.py::test_provider_specific_routes_delegate_to_expected_proxy_handlers tests/test_provider_proxy_routes.py::test_openai_response_websocket_aliases_delegate_to_openai_ws_handler tests/test_provider_proxy_routes.py::test_openai_response_subpath_passthrough_returns_502_on_http_failure tests/test_provider_proxy_routes.py::test_openai_response_subpath_passthrough_uses_openai_target tests/test_provider_proxy_routes.py::test_openai_response_subpath_aliases_and_chatgpt_auth_use_expected_targets tests/test_provider_proxy_routes.py::test_openai_image_routes_use_codex_backend_under_chatgpt_auth tests/test_provider_proxy_routes.py::test_openai_image_codex_response_strips_stale_compression_headers tests/test_provider_proxy_routes.py::test_openai_image_edits_api_key_auth_falls_through_to_openai_passthrough tests/test_provider_proxy_routes.py::test_openai_image_edits_preserves_multipart_body_under_chatgpt_auth tests/test_provider_proxy_routes.py::test_gemini_batch_embed_contents_passthrough_uses_gemini_target tests/test_provider_proxy_routes.py::test_v1_models_fetches_codex_registry_under_chatgpt_auth tests/test_provider_proxy_routes.py::test_v1_models_falls_back_to_synthetic_list_under_chatgpt_auth tests/test_provider_proxy_routes.py::test_v1_models_get_single_dynamic_under_chatgpt_auth tests/test_provider_proxy_routes.py::test_v1_models_still_forwards_under_non_chatgpt_auth tests/test_provider_proxy_routes.py::test_v1_models_routes_claude_code_gateway_discovery_to_anthropic tests/test_provider_proxy_routes.py::test_anthropic_model_metadata_strips_ansi_model_ids tests/test_custom_base_passthrough_telemetry.py tests/test_proxy_passthrough.py tests/test_proxy_google_cloudcode_route_aliases.py tests/test_proxy_project_savings.py::test_with_project_prefix_round_trips_through_split tests/test_vertex_claude_compression.py
============================ 102 passed in 34.83s =============================

python -m ruff check headroom/providers/cloudcode headroom/providers/codex headroom/providers/vertex headroom/providers/model_metadata.py headroom/providers/openai_images.py headroom/providers/openai_responses.py headroom/providers/proxy_targets.py headroom/providers/route_specs.py headroom/providers/proxy_routes.py headroom/proxy/handlers/openai.py headroom/proxy/passthrough.py headroom/proxy/request_scope.py headroom/proxy/project_context.py tests/test_package_init_lazy.py tests/test_provider_cloudcode_runtime.py tests/test_provider_codex_endpoints.py tests/test_provider_codex_headers.py tests/test_provider_codex_images.py tests/test_provider_codex_model_metadata.py tests/test_provider_codex_responses.py tests/test_provider_model_metadata.py tests/test_provider_openai_images.py tests/test_provider_openai_responses.py tests/test_provider_proxy_targets.py tests/test_provider_route_specs.py tests/test_provider_vertex_runtime.py tests/test_proxy_request_scope.py tests/test_provider_proxy_routes.py tests/test_custom_base_passthrough_telemetry.py tests/test_proxy_passthrough.py tests/test_proxy_google_cloudcode_route_aliases.py tests/test_proxy_project_savings.py tests/test_vertex_claude_compression.py
All checks passed!

python -m compileall -q headroom\providers\cloudcode headroom\providers\codex headroom\providers\vertex headroom\providers\model_metadata.py headroom\providers\openai_images.py headroom\providers\openai_responses.py headroom\providers\proxy_targets.py headroom\providers\route_specs.py headroom\providers\proxy_routes.py headroom\proxy\handlers\openai.py headroom\proxy\passthrough.py headroom\proxy\request_scope.py headroom\proxy\project_context.py
# no output; exited 0

git commit -m "refactor(providers): split proxy route adapters"
Sync plugin versions.....................................................Passed
check for merge conflicts................................................Passed
ruff.....................................................................Passed
ruff-format..............................................................Passed
mypy.....................................................................Passed
```

## Real Behavior Proof
- Environment: Windows PowerShell, Python 3.13.13, branch
`jd/provider-route-slices` based on `headroomlabs/main`.
- Exact command / steps: Ran the focused provider/proxy pytest suite,
focused ruff command, compileall over changed Python modules, and commit
hooks.
- Observed result: Provider/proxy route behavior tests passed; lint,
formatting, and mypy passed.
- Not tested: Full pytest suite, live upstream provider calls, and
manual end-to-end proxy traffic.

## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)
N/A

## Additional Notes
Documentation and CHANGELOG updates are N/A for this internal refactor.
The full pytest suite was not run; coverage here is focused on
provider/proxy routing behavior touched by this slice.

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2026-07-12 16:27:45 -05:00