mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(anthropic): preserve failed CCR continuations (#3843)
## Description
Anthropic CCR continuations currently parse non-2xx upstream error
bodies as successful model responses. This change treats every non-2xx
continuation as a failed continuation and forwards the original upstream
response without re-serializing it.
Closes #3836
## 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
- Reject non-2xx Anthropic CCR continuation responses before parsing
their bodies.
- Preserve the original response object and raw bytes when a
continuation fails.
- Add regression coverage for 401, 429, and 500 continuation responses.
## Testing
- [x] Unit tests pass (`pytest`)
- [ ] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed
### Test Output
```text
$ uv run --no-sync pytest tests/test_proxy/test_anthropic_ccr_raise.py -q
4 passed, 2 warnings in 4.10s
$ uv run --no-sync pytest tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py tests/test_direct_chat_ccr_resolution.py -q
28 passed, 1 warning in 9.31s
$ uv run --no-sync ruff format --check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_ccr_raise.py
2 files already formatted
$ uv run --no-sync ruff check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_ccr_raise.py
All checks passed!
```
## Real Behavior Proof
- Environment: Linux, Python 3.11.16, local FastAPI TestClient with a
mocked Anthropic upstream, commit
d05cf39b94
- Exact command / steps: `uv run --no-sync pytest
tests/test_proxy/test_anthropic_ccr_raise.py -q`; send a `/v1/messages`
request whose initial response contains `headroom_retrieve` and whose
continuation returns 401, 429, or 500
- Observed result: All three continuation statuses preserve the original
HTTP 200 response body and raw bytes instead of returning the upstream
error object.
- Not tested: Live Anthropic provider/network behavior and full
repository CI.
## Runtime Rollout Safety
- Rollout-managed feature(s): None
- Minimum rollout channel: Not applicable
- Stable/default behavior changed: Failed non-2xx CCR continuations now
retain the original upstream response.
- Kill switch / disable path: Disable CCR response handling with the
existing configuration.
- Unsafe override required: No
- Qualification impact: None
- Rollback path: Revert this PR.
## Review Readiness
- [x] I have performed a self-review
- [ ] 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
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] 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
- [x] I did **not** edit `CHANGELOG.md` - it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
---------
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
@@ -4396,6 +4396,11 @@ class AnthropicHandlerMixin:
|
||||
f"CCR: Got response status={cont_response.status_code}, "
|
||||
f"content-encoding={cont_response.headers.get('content-encoding')}"
|
||||
)
|
||||
if not 200 <= cont_response.status_code < 300:
|
||||
raise RuntimeError(
|
||||
"CCR continuation returned non-success status "
|
||||
f"{cont_response.status_code}"
|
||||
)
|
||||
result: dict[str, Any] = cont_response.json()
|
||||
logger.info("CCR: Parsed JSON successfully")
|
||||
return result
|
||||
@@ -4419,52 +4424,83 @@ class AnthropicHandlerMixin:
|
||||
api_call_fn,
|
||||
provider="anthropic",
|
||||
)
|
||||
final_resp_json = preserve_opaque_response_fields(
|
||||
resp_json, final_resp_json
|
||||
)
|
||||
# Update response content with final response
|
||||
resp_json = final_resp_json
|
||||
# Remove encoding headers since content is now uncompressed JSON
|
||||
ccr_response_headers = {
|
||||
k: v
|
||||
for k, v in response.headers.items()
|
||||
if k.lower() not in ("content-encoding", "content-length")
|
||||
}
|
||||
try:
|
||||
ccr_content = json.dumps(final_resp_json).encode()
|
||||
except (TypeError, ValueError) as json_err:
|
||||
logger.warning(
|
||||
f"[{request_id}] CCR: JSON serialization failed: {json_err}"
|
||||
if final_resp_json is resp_json:
|
||||
# The handler intentionally returns the same
|
||||
# object for both a failed continuation and
|
||||
# an intentional mixed-tool skip. Identity is
|
||||
# still the right signal for whether to
|
||||
# re-serialize, but not for the diagnostic
|
||||
# message; classify the residual tools first.
|
||||
from headroom.ccr.response_handler import (
|
||||
RESIDUAL_CCR_ERROR,
|
||||
RESIDUAL_CCR_SKIPPED_MIXED,
|
||||
)
|
||||
ccr_content = json.dumps(resp_json).encode()
|
||||
response = httpx.Response(
|
||||
status_code=200,
|
||||
content=ccr_content,
|
||||
headers=ccr_response_headers,
|
||||
)
|
||||
# Only claim success when no headroom_retrieve remains.
|
||||
# On an intentional mixed-tool skip (#839) the response
|
||||
# still carries headroom_retrieve for the client to
|
||||
# resolve — logging "handled successfully" there is
|
||||
# misleading. Classify via the shared, provider-generic
|
||||
# residual-CCR signal.
|
||||
from headroom.ccr.response_handler import (
|
||||
RESIDUAL_CCR_SKIPPED_MIXED,
|
||||
)
|
||||
|
||||
residual_status = self.ccr_response_handler.residual_ccr_status(
|
||||
final_resp_json, "anthropic"
|
||||
)
|
||||
if residual_status == RESIDUAL_CCR_SKIPPED_MIXED:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Skipped retrieval — "
|
||||
"headroom_retrieve returned alongside a client "
|
||||
"tool for the client to resolve"
|
||||
residual_status = self.ccr_response_handler.residual_ccr_status(
|
||||
final_resp_json, "anthropic"
|
||||
)
|
||||
if residual_status == RESIDUAL_CCR_SKIPPED_MIXED:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Skipped retrieval — "
|
||||
"headroom_retrieve returned alongside a client "
|
||||
"tool for the client to resolve"
|
||||
)
|
||||
elif residual_status == RESIDUAL_CCR_ERROR:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Continuation failed; "
|
||||
"forwarding the original upstream response"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Retrieval handled successfully"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Retrieval handled successfully"
|
||||
final_resp_json = preserve_opaque_response_fields(
|
||||
resp_json, final_resp_json
|
||||
)
|
||||
# Update response content with final response
|
||||
resp_json = final_resp_json
|
||||
# Remove encoding headers since content is now uncompressed JSON
|
||||
ccr_response_headers = {
|
||||
k: v
|
||||
for k, v in response.headers.items()
|
||||
if k.lower() not in ("content-encoding", "content-length")
|
||||
}
|
||||
try:
|
||||
ccr_content = json.dumps(final_resp_json).encode()
|
||||
except (TypeError, ValueError) as json_err:
|
||||
logger.warning(
|
||||
f"[{request_id}] CCR: JSON serialization failed: {json_err}"
|
||||
)
|
||||
ccr_content = json.dumps(resp_json).encode()
|
||||
response = httpx.Response(
|
||||
status_code=200,
|
||||
content=ccr_content,
|
||||
headers=ccr_response_headers,
|
||||
)
|
||||
# Only claim success when no headroom_retrieve remains.
|
||||
# On an intentional mixed-tool skip (#839) the response
|
||||
# still carries headroom_retrieve for the client to
|
||||
# resolve — logging "handled successfully" there is
|
||||
# misleading. Classify via the shared, provider-generic
|
||||
# residual-CCR signal.
|
||||
from headroom.ccr.response_handler import (
|
||||
RESIDUAL_CCR_SKIPPED_MIXED,
|
||||
)
|
||||
|
||||
residual_status = self.ccr_response_handler.residual_ccr_status(
|
||||
final_resp_json, "anthropic"
|
||||
)
|
||||
if residual_status == RESIDUAL_CCR_SKIPPED_MIXED:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Skipped retrieval — "
|
||||
"headroom_retrieve returned alongside a client "
|
||||
"tool for the client to resolve"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"[{request_id}] CCR: Retrieval handled successfully"
|
||||
)
|
||||
except Exception as e:
|
||||
import traceback
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ the raw ``headroom_retrieve`` tool-call body to the client.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -19,6 +20,7 @@ httpx = pytest.importorskip("httpx")
|
||||
|
||||
from fastapi.testclient import TestClient # noqa: E402
|
||||
|
||||
from headroom.cache.compression_store import get_compression_store # noqa: E402
|
||||
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
|
||||
|
||||
|
||||
@@ -36,7 +38,7 @@ def _make_config() -> ProxyConfig:
|
||||
)
|
||||
|
||||
|
||||
def _tool_call_response() -> dict:
|
||||
def _tool_call_response(hash_key: str = "badhash") -> dict:
|
||||
"""Anthropic-shaped response containing a headroom_retrieve tool use block."""
|
||||
return {
|
||||
"id": "msg_ccr_test",
|
||||
@@ -47,7 +49,7 @@ def _tool_call_response() -> dict:
|
||||
"type": "tool_use",
|
||||
"id": "toolu_bad",
|
||||
"name": "headroom_retrieve",
|
||||
"input": {"hash": "badhash"},
|
||||
"input": {"hash": hash_key},
|
||||
}
|
||||
],
|
||||
"stop_reason": "tool_use",
|
||||
@@ -110,3 +112,62 @@ def test_anthropic_ccr_exception_reraises_not_swallowed():
|
||||
"proxy silently forwarded the raw CCR tool call — silent fallback not fixed"
|
||||
)
|
||||
assert "error" in body
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", [401, 429, 500])
|
||||
def test_anthropic_ccr_non_2xx_continuation_keeps_original_response(status: int):
|
||||
"""A failed CCR continuation must not become a synthetic HTTP 200 error."""
|
||||
config = _make_config()
|
||||
hash_key = get_compression_store().store(
|
||||
original="retrieved content",
|
||||
compressed="compressed content",
|
||||
original_item_count=1,
|
||||
)
|
||||
initial_response = _tool_call_response(hash_key)
|
||||
initial_bytes = json.dumps(initial_response, separators=(",", ":")).encode()
|
||||
continuation_error = {
|
||||
"type": "error",
|
||||
"error": {"type": "authentication_error", "message": "invalid api key"},
|
||||
}
|
||||
|
||||
class _ContinuationClient:
|
||||
def __init__(self) -> None:
|
||||
self.post_calls: list[dict] = []
|
||||
|
||||
async def post(self, url, *, content=None, headers=None, timeout=None): # noqa: ANN001
|
||||
self.post_calls.append({"url": url, "content": content, "headers": headers})
|
||||
return httpx.Response(status, json=continuation_error)
|
||||
|
||||
async def aclose(self) -> None:
|
||||
return None
|
||||
|
||||
with patch("headroom.proxy.server.AnyLLMBackend"):
|
||||
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
|
||||
return httpx.Response(
|
||||
200,
|
||||
content=initial_bytes,
|
||||
headers={"content-type": "application/json"},
|
||||
)
|
||||
|
||||
continuation_client = _ContinuationClient()
|
||||
proxy._retry_request = _fake_retry
|
||||
proxy.http_client = continuation_client
|
||||
|
||||
response = client.post(
|
||||
"/v1/messages",
|
||||
headers={"x-api-key": "test-key", "anthropic-version": "2023-06-01"},
|
||||
json={
|
||||
"model": "claude-sonnet-4-6",
|
||||
"max_tokens": 64,
|
||||
"messages": [{"role": "user", "content": "retrieve it"}],
|
||||
},
|
||||
)
|
||||
|
||||
assert len(continuation_client.post_calls) == 1
|
||||
assert response.status_code == 200
|
||||
assert response.json() == initial_response
|
||||
assert response.content == initial_bytes
|
||||
|
||||
Reference in New Issue
Block a user