Files
headroom/tests/test_buffered_ccr_upstream_stream.py
7dc20b6fe0 fix(proxy): make handler failures debuggable from the logs (#4127)
## Description

Several failures on the proxy request path could not be diagnosed from
the logs, and some of the existing lines could write request content
into proxy.log. This PR changes log output only; request handling is
unchanged.

- **Catch-all 502s.** The provider catch-alls logged `<ErrorType>:
<exception text>` with no provider or model. This covers Anthropic
messages, Anthropic batch, OpenAI chat, OpenAI responses, Gemini
generateContent, Gemini countTokens and Google batch. The exception text
can come from a compressor, a router or a hook, so it can quote request
content.
- **Compression hooks.** Hook failures (`pre_compress`,
`compute_biases`, `protect_messages`, `post_compress`) were logged at
debug, so a broken operator hook silently turned itself off on every
request. On the Anthropic path, `compute_biases` and `protect_messages`
failures surfaced only as a generic "Optimization failed".
- **CCR continuation (Anthropic buffered path).** These logs had no
request id. On failure they dumped every upstream response header at
error.
- **Prefix-mismatch skip line.** It said only
`reason=cache_mode_prefix_mismatch`, with no model or sizes.
- **Silent swallows.** These swallowed exceptions with no log (`except
Exception: pass`):
  - the prefix-mismatch debug dump;
  - the OpenAI cache observation;
  - five savings token counts;
  - the OpenAI Responses tool-description compaction step.

Refs #4136

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

No line this PR adds or changes, at any level, carries an exception
message or a traceback. Hooks, compressors, routers and tool schemas can
quote request content in their errors, and logs are kept and attached to
bug reports. Failures are described with `describe_exception` from
`headroom/log_safety.py` (#4138): the exception types and innermost code
locations of the cause chain. The full traceback appears only when the
operator sets `HEADROOM_DEBUG_DUMP=full`, the existing content opt-in.

- **New `headroom/proxy/handlers/_failure_logging.py`** with two
functions.
- **`log_request_failure()`.** Used by the 7 catch-alls and the CCR
continuation.
- It logs one ERROR line: request id, `key=value` context (through
`safe_id`) and `describe_exception`, e.g. `[<rid>] OpenAI request
failed: provider='xai' model='grok-4' ValueError at headroom/...:12 in
f`.
- The provider is the resolved backend (`openai_chat_outcome_provider` /
`provider_name`, the same value passed to `record_failed`).
- Upstream transport errors follow the same rule. An httpx `ProxyError`
quotes the reason phrase a proxy sent back (`502 <peer text>`), which a
peer controls, so the raw httpx text appears only under
`HEADROOM_DEBUG_DUMP=full`.
- The `Request failed` wording is kept, because
`tests/test_system_compaction_handler.py` asserts on it.
  - **`log_hook_failure()`.** Used for all hook stages.
- The first failure per **hook class, stage and exception type** is a
WARNING. Repeats log at DEBUG.
- The guard is `WarnOnce(256, ...)`. A key is not used up while WARNING
is disabled, so a suppressed warning is still emitted later. Past 256
keys it logs one overflow warning, then stays quiet.
- The warning states what happens next. Anthropic
`compute_biases`/`protect_messages` failures say `skipping compression
for this turn`; every other stage says `continuing without it`.
- **Stage tracking.**
- OpenAI chat reports which of `pre_compress`, `compute_biases` or
`protect_messages` raised. All three still run inside one try, as
before.
- Anthropic `compute_biases` and `protect_messages` failures are logged
by stage and re-raised, so they still abort optimization for that turn
as before. Compressing without the protect veto would be unsafe.
- The Anthropic `Optimization failed` warning, which those re-raised
failures reach, now logs `describe_exception` instead of `{type}:
{message}`.
- **CCR continuation logs** (`anthropic.py`) now have a `[request_id]`
prefix. On failure they log `status=` and `upstream_request_id=` instead
of the full response-header dict.
- **Prefix-mismatch skip line** now logs `model=`, `current_len=` and
`previous_len=`.
- **Former silent swallows:**
- The prefix-mismatch dump catches `OSError`, `TypeError`, `ValueError`
and `RecursionError` and warns with the dump directory and
`describe_exception`, which keeps an `OSError`'s errno text.
- The cache-observation and savings token-count failures log at debug
with `describe_exception`.
- The tool-description compaction step warns with `describe_exception`.

## Testing

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

### Test Output

```text
$ uvx ruff@0.16.8 check headroom/proxy/handlers tests/test_hook_failure_logging.py tests/test_proxy_anthropic_compression_diagnostics.py
All checks passed!
$ uvx ruff@0.16.8 format --check (same files)
10 files already formatted
$ python -m mypy headroom/proxy/handlers/_failure_logging.py headroom/proxy/handlers/anthropic.py headroom/proxy/handlers/openai.py headroom/proxy/handlers/gemini.py headroom/proxy/handlers/batch.py headroom/log_safety.py --ignore-missing-imports
Success: no issues found
$ pytest -q tests/test_hook_failure_logging.py tests/test_log_safety.py tests/test_proxy_anthropic_compression_diagnostics.py tests/test_transforms_log_compressor.py tests/test_system_compaction_handler.py tests/test_proxy_handler_helpers.py tests/test_anthropic_buffered_ccr_usage.py tests/test_ccr_inline_resolve_handlers.py tests/test_openai_chat_turn_hooks.py tests/test_gemini_nonjson_status.py tests/test_proxy_handlers_batch.py tests/test_hook_protect_messages.py tests/test_openai_response_cache_key.py tests/test_anthropic_compaction_transforms.py tests/test_prefix_tracker_client_rewrote_reply.py tests/test_cache_prefix_safety.py tests/test_cache_mode_cold_start.py tests/test_original_snapshot_in_place_hook.py tests/test_openai_chat_tool_desc_compaction.py tests/test_hooks.py tests/test_proxy_anthropic_cache_stability.py tests/test_proxy_gemini_integration.py tests/test_proxy_openai_responses_integration.py tests/test_ccr_buffered_stream_signed_thinking.py tests/test_openai_responses_context_compaction.py tests/test_grok_shared_proxy_call_sites.py tests/test_litellm_openai_passthrough.py tests/test_proxy_batch_integration.py
374 passed, 37 skipped in 106.13s (also tests/test_openai_codex_ws_timings.py and tests/test_openai_responses_compression_units.py)
```

`tests/test_hook_failure_logging.py` renders every captured record with
`logging.Formatter` (traceback included) at DEBUG and WARNING. It
asserts that a content canary and a credential canary appear in none of
them, with no opt-in set. Two more use a real local HTTP proxy that
rejects CONNECT with a canary reason phrase, so httpx raises its real
`ProxyError`, which goes through `log_request_failure`. The canary is
absent from every formatted record without the opt-in, and present with
`HEADROOM_DEBUG_DUMP=full`. Two tests drive the real handlers: OpenAI
chat, where a `compute_biases` failure still warns after an earlier
`pre_compress` failure of the same type, and Anthropic, where the
warning says compression is skipped. Run against the code before
1531ac59, 4 of the then 6 tests fail: the canaries leak through the
DEBUG traceback, the `Optimization failed` warning and the transport
URL. Run against the code before 41c72cbf, the CONNECT test fails
because the ERROR line ends `(502 CANARY-USER-CONTENT)`.

`tests/test_proxy_anthropic_compression_diagnostics.py` now formats the
`Optimization failed` call's arguments before checking for
`TimeoutError`, because the message moved from an f-string to `%s`
arguments.

## Real Behavior Proof

- Environment: macOS, Python 3.12, repo venv, branch
`cleanup/proxy-handlers-debuggable-logs` @ be9a8e9c, temp
`HOME`/`HEADROOM_WORKSPACE_DIR`/`CODEX_HOME`, `HEADROOM_OFFLINE=1`,
`HEADROOM_DEBUG_DUMP` unset
- Exact command / steps: (1) the shared smoke harness, which starts a
real `headroom proxy` from this checkout against a local fake upstream
and sends one Anthropic and one OpenAI request with a compressible tool
result; (2) a direct run of the app (`create_app` + TestClient), with
all `headroom.proxy` records at DEBUG printed. It uses a hook whose
`compute_biases` raises `ValueError("cannot bias SECRET-USER-CONTENT")`
and an OpenAI upstream on a port nothing listens on, and sends two chat
requests whose message is `SECRET-USER-CONTENT`.
- Observed result: (1) both requests return 200 with savings, and no new
WARNING/ERROR lines appear. (2) The first request gives one hook
WARNING; the second gives the same line at DEBUG. Each request also
gives a 502 ERROR with provider, model and the exception chain's
locations. The marker appears in no output line at any level.
- Not tested: a real hosted upstream or a third-party hook package

```text
$ bash smoke.sh <worktree>
branch: cleanup/proxy-handlers-debuggable-logs @ be9a8e9c
health: 200
POST /v1/messages        -> HTTP200 smoke-ok
POST /v1/chat/completions -> HTTP200 smoke-ok
stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82
proxy.log WARNING/ERROR lines:
   (only the pre-existing offline-mode notice and Kompress offline-prefetch warnings)

$ HOME=$T/home HEADROOM_WORKSPACE_DIR=$T/ws CODEX_HOME=$T/codex HEADROOM_OFFLINE=1 python proof_4127.py
WARNING [hr_1791649130_000001] hook __main__.BrokenBiases compute_biases failed; continuing without it: ValueError at proof_4127.py:21 in compute_biases <- headroom/proxy/handlers/openai.py:3553 in handle_openai_chat (repeats log at debug)
ERROR [hr_1791649130_000001] OpenAI request failed: provider='openai' model='gpt-4o' ConnectError at default.py:118 in map_httpcore_exceptions <- contextlib.py:158 in __exit__ <- default.py:393 in handle_async_request; caused by ConnectError at _exceptions.py:14 in map_exceptions <- contextlib.py:158 in __exit__ <- anyio.py:113 in connect_tcp
attempt 1: HTTP 502
DEBUG [...000002] hook __main__.BrokenBiases compute_biases failed; continuing without it: ValueError at ...
ERROR [...000002] OpenAI request failed: provider='openai' model='gpt-4o' ConnectError at ...
attempt 2: HTTP 502
$ grep -c SECRET out.txt
0
```

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: stable
- Stable/default behavior changed: log output only. The catch-all ERROR
lines gain provider and model. At every level they show exception types,
code locations and errno instead of exception text, transport errors
included. Hook failures move from debug to a
once-per-hook-stage-and-type warning. The Anthropic `Optimization
failed` warning no longer prints exception text. No request or response
changes.
- Kill switch / disable path: standard log-level configuration for the
`headroom.proxy` logger
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert the PR's commits

## 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 did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

N/A

## Additional Notes

Part of maintainer-requested cleanup #4136 (dead code, duplication,
debuggability), split into small PRs for review.

Depends on #4138; its two files show in this diff until it merges.

- Tradeoff: a failure line shows exception types and code locations, not
the message. The message and the full traceback need
`HEADROOM_DEBUG_DUMP=full`. This keeps request content out of proxy.log
at every level.
- Documentation: the new and changed log lines are recorded for the
follow-up docs PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-10-16 14:31:05 -07:00

649 lines
23 KiB
Python

"""The buffered CCR request streams from upstream, and the proxy folds the reply.
The buffered CCR path flips a streaming turn to ``stream: false`` so the whole
reply is in hand before server-side retrieval. Sent that way, upstream sends
nothing until the generation is complete, and a network that drops HTTP
connections idle for ~60s (TLS-inspecting antivirus, VPN, corporate proxy)
cut every long turn: three attempts failed at ~60s each and the client got a
502 it resent forever. The request now goes out as a stream (as do the CCR,
memory and turn-hook continuations built from it) and the event stream is
folded back into the Message the buffered path reads.
"""
from __future__ import annotations
import inspect
import json
from typing import Any
import pytest
fastapi = pytest.importorskip("fastapi")
httpx = pytest.importorskip("httpx")
from fastapi.testclient import TestClient # noqa: E402
from headroom.cache.backends import InMemoryBackend # noqa: E402
from headroom.cache.compression_store import ( # noqa: E402
get_compression_store,
reset_compression_store,
)
from headroom.ccr.tool_injection import create_ccr_tool_definition # noqa: E402
from headroom.proxy.anthropic_wire import fold_anthropic_stream # noqa: E402
from headroom.proxy.server import HeadroomProxy, ProxyConfig, create_app # noqa: E402
URL = "https://api.anthropic.com/v1/messages"
def _sse(*events: dict[str, Any]) -> bytes:
return b"".join(
f"event: {event['type']}\ndata: {json.dumps(event)}\n\n".encode() for event in events
)
def _message_start(model: str = "claude-opus-5") -> dict[str, Any]:
return {
"type": "message_start",
"message": {
"id": "msg_1",
"type": "message",
"role": "assistant",
"model": model,
"content": [],
"stop_reason": None,
"stop_sequence": None,
"usage": {
"input_tokens": 25,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 100,
"output_tokens": 1,
},
},
}
def _text_turn(text: str) -> list[dict[str, Any]]:
return [
_message_start(),
{"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
{"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": text}},
{"type": "content_block_stop", "index": 0},
{
"type": "message_delta",
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
"usage": {"output_tokens": 7},
},
{"type": "message_stop"},
]
# A turn exercising every delta the fold accumulates, with the members only a
# stream carries: a thinking block's ``estimated_tokens`` placeholder, a delta's
# progress count, interleaved pings, and a null usage count in message_delta.
EVERY_DELTA = [
_message_start(),
{
"type": "content_block_start",
"index": 0,
"content_block": {
"type": "thinking",
"thinking": "",
"signature": "",
"estimated_tokens": None,
},
},
{"type": "ping"},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "thinking_delta", "thinking": "Let me ", "estimated_tokens": 3},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "thinking_delta", "thinking": "check."},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "signature_delta", "signature": "sig-1"},
},
{"type": "content_block_stop", "index": 0},
{
"type": "content_block_start",
"index": 1,
"content_block": {"type": "redacted_thinking", "data": "opaque"},
},
{"type": "content_block_stop", "index": 1},
{"type": "content_block_start", "index": 2, "content_block": {"type": "text", "text": ""}},
{"type": "content_block_delta", "index": 2, "delta": {"type": "text_delta", "text": "Paris"}},
{
"type": "content_block_delta",
"index": 2,
"delta": {
"type": "citations_delta",
"citation": {
"type": "char_location",
"cited_text": "Paris",
"document_index": 0,
"document_title": "Atlas",
"start_char_index": 0,
"end_char_index": 5,
},
},
},
{"type": "content_block_delta", "index": 2, "delta": {"type": "text_delta", "text": " it is."}},
{"type": "content_block_stop", "index": 2},
{
"type": "content_block_start",
"index": 3,
"content_block": {"type": "tool_use", "id": "toolu_1", "name": "Read", "input": {}},
},
{
"type": "content_block_delta",
"index": 3,
"delta": {"type": "input_json_delta", "partial_json": '{"file_pa'},
},
{
"type": "content_block_delta",
"index": 3,
"delta": {"type": "input_json_delta", "partial_json": 'th": "a.py"}'},
},
{"type": "content_block_stop", "index": 3},
{"type": "ping"},
{
"type": "message_delta",
"delta": {"stop_reason": "tool_use", "stop_sequence": None},
"usage": {"output_tokens": 42, "input_tokens": None},
},
{"type": "message_stop"},
]
# --------------------------------------------------------------------------- #
# The fold
# --------------------------------------------------------------------------- #
def test_fold_matches_the_anthropic_sdk_accumulator() -> None:
streaming = pytest.importorskip("anthropic.lib.streaming._messages")
accumulate = streaming.accumulate_event
# anthropic 1.x keeps each tool block's partial JSON in a buffer the caller
# owns (``json_bufs``); 0.x kept it on the block. Same accumulation either way.
buffers: dict[str, Any] = (
{"json_bufs": {}} if "json_bufs" in inspect.signature(accumulate).parameters else {}
)
snapshot = None
for event in EVERY_DELTA:
snapshot = accumulate(event=event, current_snapshot=snapshot, **buffers)
assert snapshot is not None
folded = fold_anthropic_stream(_sse(*EVERY_DELTA))
assert folded is not None
# The SDK keeps nulls (``stop_sequence``, the thinking placeholder); the
# non-streaming Message drops the placeholder, so compare without them.
assert {k: v for k, v in folded.items() if v is not None} == snapshot.model_dump(
exclude_none=True
)
assert folded["usage"]["input_tokens"] == 25, "a null delta count must not erase it"
def test_fold_keeps_stream_only_members_out_of_blocks() -> None:
"""The proxy resends folded blocks; Anthropic 400s on any extra member."""
folded = fold_anthropic_stream(_sse(*EVERY_DELTA))
assert folded is not None
assert folded["content"][0] == {
"type": "thinking",
"thinking": "Let me check.",
"signature": "sig-1",
}
assert folded["content"][1] == {"type": "redacted_thinking", "data": "opaque"}
assert "estimated_tokens" not in json.dumps(folded)
def test_fold_reports_a_truncated_stream() -> None:
assert fold_anthropic_stream(_sse(*EVERY_DELTA[:-2])) is None
assert fold_anthropic_stream(_sse(*EVERY_DELTA[:5])) is None # a block left open
def test_fold_returns_an_in_band_error_event() -> None:
error = {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
assert fold_anthropic_stream(_sse(*EVERY_DELTA[:7], error)) == error
def test_fold_refuses_a_delta_it_does_not_know() -> None:
unknown = {"type": "content_block_delta", "index": 2, "delta": {"type": "future_delta"}}
with pytest.raises(ValueError):
fold_anthropic_stream(_sse(*EVERY_DELTA[:10], unknown, *EVERY_DELTA[10:]))
# --------------------------------------------------------------------------- #
# The send helper
# --------------------------------------------------------------------------- #
@pytest.fixture()
def proxy() -> HeadroomProxy:
return HeadroomProxy(ProxyConfig(optimize=False, cache_enabled=False, image_optimize=False))
class _Upstream:
"""Answers a stream:true request with ``stream_reply``, anything else with JSON."""
def __init__(self, stream_reply: httpx.Response) -> None:
self.stream_reply = stream_reply
self.calls: list[tuple[dict[str, str], dict[str, Any]]] = []
async def __call__(self, headers: dict[str, str], body: dict[str, Any]) -> httpx.Response:
self.calls.append((dict(headers), json.loads(json.dumps(body))))
if body.get("stream"):
return self.stream_reply
return httpx.Response(200, json={"type": "message", "content": [], "via": "json"})
def _stream_reply(*events: dict[str, Any]) -> httpx.Response:
return httpx.Response(200, content=_sse(*events), headers={"content-type": "text/event-stream"})
async def _send(
proxy: HeadroomProxy, upstream: _Upstream, *, url: str = URL, body: dict | None = None
) -> httpx.Response:
return await proxy._send_buffered_as_stream(
upstream,
{"Accept": "application/json", "x-api-key": "k"},
body if body is not None else {"model": "m", "stream": False, "messages": []},
url=url,
request_id="t",
)
@pytest.mark.asyncio
async def test_a_buffered_request_streams_and_folds(proxy) -> None:
upstream = _Upstream(_stream_reply(*_text_turn("hello")))
body = {"model": "m", "stream": False, "messages": []}
response = await _send(proxy, upstream, body=body)
assert response.status_code == 200
assert response.json()["content"] == [{"type": "text", "text": "hello"}]
assert len(upstream.calls) == 1
headers, sent = upstream.calls[0]
assert sent["stream"] is True
assert [v for k, v in headers.items() if k.lower() == "accept"] == ["text/event-stream"]
assert body["stream"] is False, "the caller's body is not mutated"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("env", "url", "stream"),
[
("0", URL, False), # kill switch
("1", "https://gateway.example/v1/messages", False), # not first-party
("1", URL, True), # already a stream
],
)
async def test_requests_it_should_not_touch_go_out_unchanged(
proxy, monkeypatch, env: str, url: str, stream: bool
) -> None:
monkeypatch.setenv("HEADROOM_BUFFERED_UPSTREAM_STREAM", env)
upstream = _Upstream(_stream_reply(*_text_turn("hello")))
body = {"model": "m", "stream": stream, "messages": []}
await _send(proxy, upstream, url=url, body=body)
assert upstream.calls == [({"Accept": "application/json", "x-api-key": "k"}, body)]
@pytest.mark.asyncio
async def test_a_propagated_kill_switch_keeps_the_request_non_streaming(proxy) -> None:
# `headroom wrap` pushes this knob to a reused proxy, so the rollback works
# without the proxy's own environment or a restart.
from headroom.proxy import runtime_env
assert runtime_env.explicit_env({"HEADROOM_BUFFERED_UPSTREAM_STREAM": "0"}) == {
"HEADROOM_BUFFERED_UPSTREAM_STREAM": "0"
}
upstream = _Upstream(_stream_reply(*_text_turn("hello")))
body = {"model": "m", "stream": False, "messages": []}
runtime_env.set_overrides({"HEADROOM_BUFFERED_UPSTREAM_STREAM": "0"})
try:
response = await _send(proxy, upstream, body=body)
finally:
runtime_env.clear_overrides()
assert upstream.calls == [({"Accept": "application/json", "x-api-key": "k"}, body)]
assert response.json()["via"] == "json"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"reply",
[
httpx.Response(529, json={"type": "error", "error": {"type": "overloaded_error"}}),
httpx.Response(200, json={"type": "message", "content": []}), # answered JSON anyway
],
)
async def test_a_reply_that_is_not_an_event_stream_is_returned_as_is(proxy, reply) -> None:
upstream = _Upstream(reply)
assert await _send(proxy, upstream) is reply
@pytest.mark.asyncio
async def test_an_in_band_error_comes_back_with_its_status(proxy) -> None:
error = {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
upstream = _Upstream(_stream_reply(_message_start(), error))
response = await _send(proxy, upstream)
assert response.status_code == 529
assert response.json() == error
@pytest.mark.asyncio
async def test_a_truncated_stream_is_a_502(proxy) -> None:
upstream = _Upstream(_stream_reply(*_text_turn("hello")[:-2]))
response = await _send(proxy, upstream)
assert response.status_code == 502
assert response.json()["error"]["type"] == "upstream_protocol_error"
assert len(upstream.calls) == 1
@pytest.mark.asyncio
async def test_a_stream_it_cannot_fold_is_sent_again_as_stream_false(proxy) -> None:
unknown = {"type": "content_block_delta", "index": 0, "delta": {"type": "future_delta"}}
events = _text_turn("hello")
upstream = _Upstream(_stream_reply(*events[:3], unknown, *events[3:]))
response = await _send(proxy, upstream)
assert response.json()["via"] == "json"
assert [sent["stream"] for _, sent in upstream.calls] == [True, False]
assert upstream.calls[1][0]["Accept"] == "application/json"
# --------------------------------------------------------------------------- #
# Through the handler
# --------------------------------------------------------------------------- #
@pytest.fixture(autouse=True)
def _store():
reset_compression_store()
get_compression_store(backend=InMemoryBackend())
try:
yield
finally:
reset_compression_store()
def _config() -> ProxyConfig:
return ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
memory_enabled=False,
ccr_inject_tool=True,
ccr_handle_responses=True,
ccr_context_tracking=False,
image_optimize=False,
)
class _ContinuationClient:
"""Stands in for ``http_client``; the CCR continuation posts through it."""
def __init__(self, reply: httpx.Response) -> None:
self.reply = reply
self.calls: list[tuple[dict[str, str], dict[str, Any]]] = []
async def post(self, url, *, content=None, headers=None, timeout=None): # noqa: ANN001
self.calls.append((dict(headers or {}), json.loads(content)))
return self.reply
async def aclose(self) -> None:
return None
def _accept(headers: dict[str, str]) -> list[str]:
return [v for k, v in headers.items() if k.lower() == "accept"]
def test_the_buffered_turn_and_its_ccr_continuation_stream_upstream() -> None:
store = get_compression_store()
marker = store.store(
original=json.dumps({"secret": "retrieved answer"}),
compressed="{}",
original_item_count=1,
)
retrieve_turn = [
_message_start(),
{
"type": "content_block_start",
"index": 0,
"content_block": {
"type": "thinking",
"thinking": "",
"signature": "",
"estimated_tokens": None,
},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "thinking_delta", "thinking": "I need it."},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "signature_delta", "signature": "sig"},
},
{"type": "content_block_stop", "index": 0},
{
"type": "content_block_start",
"index": 1,
"content_block": {
"type": "tool_use",
"id": "toolu_ccr",
"name": "headroom_retrieve",
"input": {},
},
},
{
"type": "content_block_delta",
"index": 1,
"delta": {"type": "input_json_delta", "partial_json": json.dumps({"hash": marker})},
},
{"type": "content_block_stop", "index": 1},
{
"type": "message_delta",
"delta": {"stop_reason": "tool_use", "stop_sequence": None},
"usage": {"output_tokens": 9},
},
{"type": "message_stop"},
]
sent: list[tuple[dict[str, str], dict[str, Any]]] = []
app = create_app(_config())
with TestClient(app) as client:
proxy = client.app.state.proxy
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
sent.append((dict(headers), json.loads(json.dumps(body))))
return _stream_reply(*retrieve_turn)
proxy._retry_request = _fake_retry # type: ignore[assignment]
continuation = _ContinuationClient(_stream_reply(*_text_turn("the answer")))
proxy.http_client = continuation
resp = client.post(
"/v1/messages",
headers={"x-api-key": "k", "anthropic-version": "2023-06-01"},
json={
"model": "claude-opus-5",
"max_tokens": 64,
"stream": True,
"tools": [create_ccr_tool_definition("anthropic")],
"messages": [{"role": "user", "content": f"use <<ccr:{marker}>>"}],
},
)
assert resp.status_code == 200, resp.text
assert "the answer" in resp.text
assert "headroom_retrieve" not in resp.text
[(headers, body)] = sent
assert body["stream"] is True
assert _accept(headers) == ["text/event-stream"]
[(cont_headers, cont_body)] = continuation.calls
assert cont_body["stream"] is True
assert _accept(cont_headers) == ["text/event-stream"]
# The folded turn is resent as history: no stream-only member may ride along.
assert cont_body["messages"][-2]["content"][0] == {
"type": "thinking",
"thinking": "I need it.",
"signature": "sig",
}
class _FailingContinuationClient:
"""A CCR continuation that dies on the wire or comes back non-2xx."""
def __init__(self, reply: httpx.Response | None) -> None:
self.reply = reply
async def post(self, url, *, content=None, headers=None, timeout=None): # noqa: ANN001
if self.reply is None:
raise httpx.ConnectError("peer said CONTENT_CANARY")
return self.reply
async def aclose(self) -> None:
return None
@pytest.mark.parametrize("failure", ["transport", "status"])
def test_a_failed_ccr_continuation_logs_only_safe_metadata(monkeypatch, failure) -> None:
import logging
monkeypatch.delenv("HEADROOM_DEBUG_DUMP", raising=False)
marker = get_compression_store().store(
original=json.dumps({"secret": "retrieved answer"}), compressed="{}", original_item_count=1
)
retrieve_turn = [
_message_start(),
{
"type": "content_block_start",
"index": 0,
"content_block": {
"type": "tool_use",
"id": "t1",
"name": "headroom_retrieve",
"input": {},
},
},
{
"type": "content_block_delta",
"index": 0,
"delta": {"type": "input_json_delta", "partial_json": json.dumps({"hash": marker})},
},
{"type": "content_block_stop", "index": 0},
{
"type": "message_delta",
"delta": {"stop_reason": "tool_use", "stop_sequence": None},
"usage": {"output_tokens": 9},
},
{"type": "message_stop"},
]
reply = (
None
if failure == "transport"
else httpx.Response(
503,
content=b"CONTENT_CANARY",
headers={"request-id": "req_upstream_1", "x-private": "HEADER_CANARY"},
)
)
records: list[logging.LogRecord] = []
class _Collect(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
records.append(record)
app = create_app(_config())
with TestClient(app) as client:
# Attached after startup: startup reconfigures headroom.* logging.
logger = logging.getLogger("headroom.proxy")
handler = _Collect(level=logging.DEBUG)
logger.addHandler(handler)
proxy = client.app.state.proxy
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
return _stream_reply(*retrieve_turn)
proxy._retry_request = _fake_retry # type: ignore[assignment]
proxy.http_client = _FailingContinuationClient(reply)
try:
client.post(
"/v1/messages",
headers={"x-api-key": "k", "anthropic-version": "2023-06-01"},
json={
"model": "claude-opus-5",
"max_tokens": 64,
"stream": True,
"tools": [create_ccr_tool_definition("anthropic")],
"messages": [{"role": "user", "content": f"use <<ccr:{marker}>>"}],
},
)
finally:
logger.removeHandler(handler)
[failure_log] = [
logging.Formatter().format(r)
for r in records
if "CCR continuation failed" in r.getMessage()
]
if failure == "transport":
assert "ConnectError" in failure_log
assert "status=None" in failure_log
else:
assert "status=503" in failure_log
assert "upstream_request_id='req_upstream_1'" in failure_log
for record in records:
rendered = logging.Formatter().format(record)
assert "CONTENT_CANARY" not in rendered
assert "HEADER_CANARY" not in rendered
def test_a_client_stream_false_request_is_left_alone() -> None:
marker = get_compression_store().store(
original=json.dumps({"earlier": "tool output"}),
compressed="{}",
original_item_count=1,
)
sent: list[tuple[dict[str, str], dict[str, Any], dict[str, Any]]] = []
app = create_app(_config())
with TestClient(app) as client:
proxy = client.app.state.proxy
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
sent.append((dict(headers), json.loads(json.dumps(body)), kwargs))
return httpx.Response(200, json=fold_anthropic_stream(_sse(*_text_turn("ok"))))
proxy._retry_request = _fake_retry # type: ignore[assignment]
resp = client.post(
"/v1/messages",
headers={"x-api-key": "k", "anthropic-version": "2023-06-01"},
json={
"model": "claude-opus-5",
"max_tokens": 64,
"stream": False,
"tools": [create_ccr_tool_definition("anthropic")],
"messages": [{"role": "user", "content": f"use <<ccr:{marker}>>"}],
},
)
assert resp.status_code == 200, resp.text
[(headers, body, kwargs)] = sent
assert body["stream"] is False
assert _accept(headers) == ["application/json"]
assert kwargs["original_body_bytes"] is not None