fix(anthropic): reject ping-only SSE from buffered CCR (#3682)

## Description

Buffered CCR deliberately converts a client `stream:true` request into
an upstream `stream:false` request. The SSE recovery gate used the
client-facing `stream` value, however, so an upstream that answered the
JSON request with `200 text/event-stream` bypassed SSE-to-JSON
adaptation. A body containing only ping keepalives could therefore be
relayed as a successful but contentless stream.

Fixes #3266

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- Treat the upstream contract as non-streaming while a streaming client
is in buffered CCR mode.
- Let `_adapt_event_stream_to_json()` handle non-compliant upstream SSE
replies in that path.
- A ping-only SSE body now becomes a deterministic `502
upstream_protocol_error` response instead of an empty `200` stream.
- Added a regression test with a `200` SSE response containing only
`event: ping`.

## Testing

- [x] New tests added for the bug fix
- [x] Linting passes (`ruff check`)
- [x] Relevant unit tests pass locally

### Test Output

```text
# before the fix
test_buffered_ccr_rejects_ping_only_sse_as_502 failed:
assert 200 == 502

# after the fix
$ pytest tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py tests/test_nonstream_sse_policy.py -q
30 passed

$ pytest tests/test_proxy_response_cache_replay.py tests/test_proxy/test_anthropic_buffered_timeout.py -q
18 passed

$ ruff check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py
All checks passed!

$ ruff format --check headroom/proxy/handlers/anthropic.py tests/test_proxy/test_anthropic_streaming_ccr_retrieve.py
2 files already formatted
```

## Real Behavior Proof

- Environment: Windows x64, Python 3.13.13, pytest 9.1.1.
- Before: the new regression test simulated a `200 text/event-stream`
reply containing only `event: ping`; the buffered CCR path returned HTTP
200 instead of reporting an upstream protocol error.
- After: the same reply produces HTTP 502 with JSON error type
`upstream_protocol_error`, so the proxy no longer presents a
keepalive-only stream as a successful answer.
- Not tested here: every upstream provider's actual SSE behavior. The
test models the non-compliant ping-only reply described in the linked
issue.

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: stable.
- Stable/default behavior changed: yes, a previously accepted invalid
upstream response shell is now reported as HTTP 502.
- Kill switch / disable path: not needed; normal JSON and SSE recovery
paths are unchanged.
- Unsafe override required: no.
- Qualification impact: clients receive an explicit recoverable upstream
failure instead of waiting on or accepting an empty response.
- Rollback path: revert this PR.

## 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 added tests that prove my fix is effective
- [x] Relevant new and existing unit tests pass locally with my changes
- [x] My changes generate no new warnings
- [x] I did **not** edit `CHANGELOG.md`

## AI Disclosure

This change and its tests were prepared with AI assistance and reviewed
by the submitter.
This commit is contained in:
iuiuiu
2026-09-24 10:53:28 -05:00
committed by GitHub
parent 558b066b3d
commit 000fefceaa
2 changed files with 42 additions and 2 deletions
+6 -2
View File
@@ -4171,9 +4171,13 @@ class AnthropicHandlerMixin:
# ``stream`` is what the *client* asked for, not what
# went upstream: a buffered CCR turn deliberately
# requests JSON on behalf of a streaming client and
# re-emits SSE further down, and must keep doing so.
# re-emits SSE further down. That same flip means the
# upstream's stream contract is JSON, so its SSE reply
# must still be reconstructed before CCR processing;
# otherwise a keepalive-only stream can be relayed as a
# successful but contentless response (#3266).
if should_recover_sse_reply(
client_requested_stream=bool(stream),
client_requested_stream=bool(stream and not buffered_stream_ccr),
status_code=response.status_code,
content_type=response.headers.get("content-type"),
body_is_event_stream=_looks_like_sse_response(response),
@@ -628,6 +628,42 @@ def test_buffered_ccr_rejects_malformed_success_as_502() -> None:
assert b"gateway timeout" not in response.content
def test_buffered_ccr_rejects_ping_only_sse_as_502() -> None:
"""A 200 SSE body is not a successful answer to a stream:false request.
A provider that ignores the buffered flip can return only keepalives. The
client asked for SSE, but the upstream did not, so relaying the shell as a
successful stream loses the turn behind a valid-looking 200 (#3266).
"""
config = _make_config()
with patch("headroom.proxy.server.AnyLLMBackend"):
app = create_app(config)
with TestClient(app) as client:
proxy = app.state.proxy
proxy._retry_request = AsyncMock(
return_value=httpx.Response(
200,
content=b'event: ping\ndata: {"type":"ping"}\n\n',
headers={"content-type": "text/event-stream"},
)
)
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,
"stream": True,
"tools": [create_ccr_tool_definition("anthropic")],
"messages": [{"role": "user", "content": _buffered("ping only")}],
},
)
assert response.status_code == 502
assert "text/event-stream" not in response.headers["content-type"]
assert response.json()["error"]["type"] == "upstream_protocol_error"
@pytest.mark.asyncio
async def test_buffered_ccr_late_failure_returns_sanitized_json_error() -> None:
"""A slow crash gets the same 502 the fast one does, not a downgraded 200."""