headroom/tests/test_proxy
Omar Garcia 8e0dadfe02
fix: restore token-mode compression on frozen prefixes (#1489)
## Description

Fixes token-mode compression for continued Claude Code turns with a
frozen prefix when the client has not already supplied
`headroom_retrieve`.

The previous guard returned before request-side compression could run in
token mode. This keeps the non-token safety behavior, but lets token
mode use the existing marker-triggered CCR tool injection override so
emitted markers stay redeemable.

Closes #1487.

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

- Let Anthropic token mode run request-side compression even when the
client did not pre-register `headroom_retrieve`.
- Kept the deferred-injection skip for cache-mode coverage.
- Added a regression for the frozen-prefix token-mode path.
- Updated `CHANGELOG.md` for the user-facing behavior change.

## Testing

<!-- Check what you actually ran, then paste the real command output
below. -->

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

### Test Output

```text
$ rtk uv run pytest -q tests/test_proxy/test_anthropic_ccr_deferred_injection.py
15 passed, 1 warning in 2.73s

$ rtk uv run ruff check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_ccr_deferred_injection.py
All checks passed!

$ rtk uv run ruff format --check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_ccr_deferred_injection.py
2 files already formatted
```

## Real Behavior Proof

- Environment: macOS, Python 3.12.9, local FastAPI `TestClient`,
Anthropic proxy path, `mode=token`, `ccr_inject_tool=True`, frozen
prefix count = 1, no client-supplied `headroom_retrieve`.
- Exact command / steps: ran a local `rtk uv run python` repro that
builds `create_app(ProxyConfig(...))`, forces compression on the
Anthropic path, simulates a frozen prefix, and posts `/v1/messages`.
- Observed result: local `TestClient` request returned `STATUS=200`;
token-mode frozen-prefix compression ran once with
`FROZEN_MESSAGE_COUNT=1`; the forwarded message was the CCR marker;
forwarded tools included `headroom_retrieve`.

```text
STATUS= 200
FROZEN_MESSAGE_COUNT= 1
COMPRESSION_CALLS= 1
FORWARDED_MESSAGES= [{'role': 'user', 'content': '[100 items compressed to 10. Retrieve more: hash=abc123def456abc123def456]'}]
FORWARDED_TOOLS= ['headroom_retrieve']
```

- Not tested: live Claude Code session against a real Anthropic
upstream, full repo-wide `uv run pytest`, and `mypy headroom`.

## 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
(N/A: no new hard-to-follow block needed)
- [x] I have made corresponding changes to the documentation (N/A:
changelog update covers this user-facing bug fix)
- [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 have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)

N/A; proxy behavior only.

## Additional Notes

The pytest run still emits the existing Starlette/httpx deprecation
warning from `fastapi.testclient`; this PR does not touch that
dependency path.
2026-06-28 13:21:52 -07: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 fix: restore token-mode compression on frozen prefixes (#1489) 2026-06-28 13:21:52 -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_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_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