headroom/tests/test_proxy
Rod Boev 62cd3072a2
feat(ccr): wire retrieve-tool interception into OpenAI Responses handler (#1898)
## Description

Refs #1877. `handle_openai_responses` (the `/v1/responses` HTTP handler)
had zero CCR / `headroom_retrieve` wiring, so a retrieve `function_call`
in a Responses API reply passed straight through to the client instead
of being resolved server-side, unlike the parallel chat-completions
backend path (`handle_openai_chat`, ~2775-2848), which already
intercepts `headroom_retrieve` tool calls via
`ccr_response_handler.has_ccr_tool_calls()` / `handle_response()`.

This PR is scoped to the core interception gap only. The issue's
proposals A (egress scrubber) and B/C (event-level SSE parsing/splicing
for true mid-stream interception) are out of scope here; the streaming
case is instead handled by forcing a buffered (non-streaming) upstream
call when `headroom_retrieve` is offered, matching the existing
buffered-CCR pattern in the Anthropic handler.

## Type of Change

- [ ] Bug fix (non-breaking change that fixes an issue)
- [x] 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/ccr/response_handler.py`: added an `"openai_responses"`
provider branch to `CCRResponseHandler`; `_extract_tool_calls` reads
flat `function_call` items from the top-level `output[]` array,
tool-call IDs key off `call_id`, and `_extract_assistant_message` /
`_create_tool_result_message` return sentinel-keyed item lists that
`handle_response()` extends into the running item history.
- `headroom/ccr/tool_injection.py`: added a `parse_tool_call` branch for
`"openai_responses"` where name and arguments are flat on the item.
- `headroom/proxy/handlers/openai.py`: detects non-streaming
`headroom_retrieve` function calls and runs
`ccr_response_handler.handle_response()` with a stateless continuation
that resends the full `input[]` item history.
- `headroom/proxy/handlers/openai.py`: forces `stream:true` requests
with `headroom_retrieve` available through a buffered `stream:false`
upstream call, resolves retrieval server-side, then reconstructs a
minimal Responses SSE stream for the client.
- `headroom/proxy/handlers/openai.py`: treats `ccr_response_handler` as
optional on `OpenAIHandlerMixin` consumers, so handlers without CCR
support keep the existing Codex routing, streaming, header stripping,
memory timeout, and compression fail-open behavior.
- Non-CCR streaming requests are unaffected; they still go through
`_stream_response()` as before.

## Testing

- [x] Unit tests pass (`uv run pytest
tests/test_ccr_response_handler_openai_responses.py -q`)
- [x] Integration tests pass (`uv run pytest
tests/test_proxy/test_openai_responses_ccr.py -q`)
- [x] Regression tests pass (`uv run pytest
tests/test_openai_codex_routing.py -q`)
- [x] Linting passes (`uv run ruff check
headroom/proxy/handlers/openai.py tests/test_openai_codex_routing.py
tests/test_proxy/test_openai_responses_ccr.py
tests/test_ccr_response_handler_openai_responses.py`)
- [x] New tests added for new functionality when applicable
- [x] Manual testing performed

### Test Output

```text
$ uv run pytest tests/test_openai_codex_routing.py -q
tests\test_openai_codex_routing.py ....................                  [100%]
20 passed in 0.51s

$ uv run pytest tests/test_proxy/test_openai_responses_ccr.py tests/test_ccr_response_handler_openai_responses.py -q
tests\test_proxy\test_openai_responses_ccr.py ....                       [ 25%]
tests\test_ccr_response_handler_openai_responses.py ............         [100%]
16 passed, 1 warning in 27.51s

$ uv run ruff check headroom/proxy/handlers/openai.py tests/test_openai_codex_routing.py tests/test_proxy/test_openai_responses_ccr.py tests/test_ccr_response_handler_openai_responses.py
All checks passed!
```

## Real Behavior Proof

- Environment: Windows, Python 3.12 via uv-managed venv, local PR
worktree on branch `pr/1877-ccr-responses-interception`, no live LLM
provider needed because the tests stub upstream HTTP and CCR
continuation behavior.
- Exact command / steps: Ran `uv run pytest
tests/test_openai_codex_routing.py -q` to reproduce the CI-failing Codex
routing surface after the optional-handler fix; ran `uv run pytest
tests/test_proxy/test_openai_responses_ccr.py
tests/test_ccr_response_handler_openai_responses.py -q` to cover the
positive Responses CCR interception path; ran targeted Ruff on the
touched handler and related tests.
- Observed result: Codex routing tests that previously failed with
`AttributeError: '_DummyOpenAIHandler' object has no attribute
'ccr_response_handler'` now pass; Responses CCR still detects and
resolves `headroom_retrieve` when a real proxy installs
`ccr_response_handler`; non-CCR streaming requests still route through
`_stream_response()`.
- Not tested: true event-level mid-stream Responses SSE splicing and
client-bound egress marker scrubbing are out of scope for this PR and
remain future work from issue #1877's broader proposals.

## 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

## Additional Notes

No documentation or changelog update was made because this is internal
proxy CCR behavior, not a user-facing command or configuration change.
2026-07-09 09:41:06 -04:00
..
test_anthropic_buffered_timeout.py fix(proxy): add an Anthropic buffered read-timeout override (#1331) 2026-06-23 22:46:31 -05:00
test_anthropic_ccr_deferred_injection.py feat(cache): provider-agnostic cache-mode delta + cc-agnostic prefix comparison (#1868) 2026-07-08 13:29:35 -07:00
test_anthropic_ccr_raise.py fix(anthropic): CCR exception must re-raise, not silently swallow (#838) 2026-06-10 21:11:46 -05:00
test_anthropic_streaming_ccr_retrieve.py fix(proxy): handle streaming CCR retrieval (#1451) 2026-06-30 13:46:34 -05:00
test_background_compression.py perf(compression): take large cold-start contexts off the synchronous kompress path (#1171) (#1298) 2026-06-23 10:48:06 -05:00
test_bedrock_passthrough.py feat(proxy): compress AWS Bedrock InvokeModel requests via configurable upstream (#720) 2026-06-16 14:56:16 -05:00
test_cc_switch_reconciler.py feat(proxy): cc-switch reconciler — keep Headroom in the request path alongside cc-switch (#1030) 2026-06-17 08:41:16 -05:00
test_ccr_frozen_prefix_coupling.py fix(proxy): stop re-compressing headroom_retrieve output and emitting unredeemable markers (#1323) 2026-06-25 10:11:42 -05:00
test_compression_failure_action.py fix(codex): fail open for proxy compression timeout 2026-06-04 14:04:49 +05:30
test_compression_timeout_config.py feat(proxy): make COMPRESSION_TIMEOUT_SECONDS configurable via env (#946) (#991) 2026-06-16 21:10:01 -05:00
test_compute_turn_id.py fix(proxy): strip cache_control before hashing turn_id 2026-04-23 16:04:57 +02:00
test_header_safe_transforms.py feat(transforms): attribute read_lifecycle + smart_crush tags (#249) 2026-06-11 11:51:26 -05:00
test_mcp_stats_aggregation.py fix(proxy): Strands MCP bundle + backend path fixes + Codex fail-closed protection 2026-05-21 11:00:14 -07:00
test_openai_backend_path.py fix(proxy): Strands MCP bundle + backend path fixes + Codex fail-closed protection 2026-05-21 11:00:14 -07:00
test_openai_chat_savings_profile.py fix(proxy/openai): thread savings-profile kwargs into chat completions (#1606) 2026-07-07 11:27:54 -05:00
test_openai_responses_ccr.py feat(ccr): wire retrieve-tool interception into OpenAI Responses handler (#1898) 2026-07-09 09:41:06 -04:00
test_openai_transport_path_prefix.py fix(codex): OpenCode Zen telemetry attribution (#1648) 2026-07-07 11:35:21 -05:00
test_openai_upstream_header.py fix(proxy): honor x-headroom-base-url in dedicated OpenAI handlers (#1502) 2026-07-01 22:24:39 -05:00
test_phase3_byte_identity.py perf(compression): take large cold-start contexts off the synchronous kompress path (#1171) (#1298) 2026-06-23 10:48:06 -05:00
test_request_logger.py feat(proxy): log compressed messages alongside original request (#261) 2026-06-11 19:02:54 -05:00
test_transformations_feed.py fix(proxy): scope CORS to loopback + gate operator/content endpoints (#1226) 2026-06-21 00:50:55 -07:00