fix(proxy): do not rebuild a failed turn-hook re-drive as HTTP 200

The Anthropic buffered turn-hook re-drive parsed the continuation body
with no status check, so an upstream 4xx/5xx error envelope was returned
to the client as a successful 200 response. Raise on a non-2xx status,
as the CCR continuation path already does; run_response_hooks then keeps
the original response.

Fixes #4394

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 19:14:41 +07:00
co-authored by Claude Opus 5.5
parent e995eb9f1d
commit e6d16cdc2f
2 changed files with 44 additions and 0 deletions
+5
View File
@@ -4989,6 +4989,11 @@ class AnthropicHandlerMixin:
url=url,
request_id=request_id,
)
if not 200 <= continuation_response.status_code < 300:
raise RuntimeError(
"turn hook re-drive returned non-success status "
f"{continuation_response.status_code}"
)
continuation_json = continuation_response.json()
_hook_usage.record(continuation_json)
return continuation_json
+39
View File
@@ -465,3 +465,42 @@ def test_anthropic_bills_original_plus_hook_redrive(monkeypatch, _no_hooks) -> N
assert outcome.cache_read_tokens == 120
assert outcome.cache_write_tokens == 55
assert outcome.uncached_input_tokens == 250
@respx.mock
def test_anthropic_hook_redrive_error_is_not_rebuilt_as_200(monkeypatch, _no_hooks) -> None:
"""An upstream error on the re-drive must not replace the good first response."""
register_turn_hook(_RedriveOnce())
app, _outcomes = _app_and_outcomes(monkeypatch)
first = {
"id": "a",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-5",
"content": [{"type": "text", "text": "a"}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 100, "output_tokens": 10},
}
error = {"type": "error", "error": {"type": "api_error", "message": "Internal server error"}}
# The first call succeeds; every later call (the re-drive, plus its retries) fails.
route = respx.post("https://api.anthropic.com/v1/messages").mock(
side_effect=[httpx.Response(200, json=first), httpx.Response(500, json=error)]
+ [httpx.Response(500, json=error)] * 5
)
with TestClient(app) as client:
result = client.post(
"/v1/messages",
json={
"model": "claude-sonnet-4-5",
"max_tokens": 64,
"messages": [{"role": "user", "content": "hi"}],
},
headers={"x-api-key": "sk-ant-test", "anthropic-version": "2023-06-01"},
)
assert route.call_count >= 2
assert result.status_code == 200
assert result.json()["id"] == "a"
assert result.json().get("type") != "error"