headroom/tests/test_proxy
Lucas Santos 43494ff526
fix(proxy): stop re-compressing headroom_retrieve output and emitting unredeemable markers (#1323)
## Description

Two related CCR problems that both end in unreadable content.

The first one (#1077) is an infinite loop. Any tool output over ~500
bytes gets replaced with a `<<ccr:hash>>` marker, and you call
`headroom_retrieve` to get the original back. But the proxy then
compresses the *retrieve response too*, so what comes back is a brand
new marker. Retrieve that one and you get another marker.

The second one (#1006), the proxy makes two independent decisions per
request: SmartCrusher compresses, and the `headroom_retrieve` tool gets
injected. The injection is deferred when there's a frozen message prefix
(`frozen_message_count > 0`), but compression keeps running anyway. So
the agent receives `[... compressed to N. Retrieve more: hash=...]`
markers with no `headroom_retrieve` tool to redeem them.

For #1077, SmartCrusher now skips `headroom_retrieve` results. Before
crushing a tool message (OpenAI `role=tool`) or tool-result block
(Anthropic `type=tool_result`), it checks whether that tool id maps to
the CCR tool, and if so leaves it alone. Retrieved content stays
readable.

For #1006, compression and injection are no longer decided in isolation.
The injection decision is extracted into `should_inject_ccr_tool`, which
the Anthropic handler calls: when injection was deferred because of a
frozen prefix but compression just emitted new markers, it injects the
tool anyway, so a marker is never handed to an agent that can't act on
it. The existing session-sticky dedup means sessions that already have
the tool don't get it re-injected and don't lose their cache.

Closes #1077
Closes #1006

## 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/transforms/smart_crusher.py`: exempt `headroom_retrieve`
results from compression on both the OpenAI `role=tool` and Anthropic
`type=tool_result` paths.
- `headroom/proxy/helpers.py`: add `should_inject_ccr_tool`, the
deferral-plus-override decision the handler used to inline, so the #1006
behaviour is testable at the decision point.
- `headroom/proxy/handlers/anthropic.py`: call `should_inject_ccr_tool`
to couple injection with compression; rename the misleading
`frozen_prefix=` log key to `frozen_message_count=`.
- `tests/test_transforms/test_smart_crusher_ccr_retrieve_exemption.py`
and `tests/test_proxy/test_ccr_frozen_prefix_coupling.py`: new tests;
the frozen-prefix test now drives `should_inject_ccr_tool` so it would
fail if the override were removed.

## 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
$ uv run --extra dev python -m pytest tests/test_proxy/test_ccr_frozen_prefix_coupling.py tests/test_transforms/test_smart_crusher_ccr_retrieve_exemption.py -q
5 passed, 1 skipped
ruff: All checks passed!
mypy: Success: no issues found
```

The SmartCrusher test skips locally because the Rust extension `.so` is
built for a different OS, the same skip the existing SmartCrusher tests
take locally. It runs in CI where the extension is built.

## Real Behavior Proof

- Environment: macOS, Python 3.13, this branch.
- Exact command / steps: `uv run --extra dev python -m pytest
tests/test_proxy/test_ccr_frozen_prefix_coupling.py
tests/test_transforms/test_smart_crusher_ccr_retrieve_exemption.py -q`.
The frozen-prefix test calls `should_inject_ccr_tool` (the function the
Anthropic handler now uses) with a frozen prefix and freshly emitted
markers, then drives `apply_session_sticky_ccr_tool` end to end and
asserts `headroom_retrieve` lands in the outbound tools. The exemption
test runs a `headroom_retrieve` tool result through SmartCrusher on both
the OpenAI and Anthropic shapes.
- Observed result: 5 passed, 1 skipped. The retrieve tool is injected
even under a frozen prefix once markers exist, and is not injected when
no markers were emitted. Removing the handler override flips
`should_inject_ccr_tool` and fails the test.
- Not tested: a full live proxy session. The behaviours are covered at
the decision, transform, and handler-call level by the new tests.

## 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
- [x] I have updated the CHANGELOG.md if applicable

## Additional Notes

This one touches compression gating, so it's worth a careful read on the
injection coupling, that's the part where a wrong call would
re-introduce data loss.

1. Tool results with no id mapping still compress, marked with `#
ponytail:` comments. Only ids we can positively identify as the CCR tool
are exempted.
2. The injection coupling keys off `injector.has_compressed_content`, so
the tool only shows up when there's actually something to retrieve.

---------

Co-authored-by: JD Davis <mxjerrett@gmail.com>
2026-06-25 10:11:42 -05: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(ccr): skip Anthropic marker emission when tool injection is deferred (#1273) 2026-06-23 12:48:05 -05: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