fix(ccr): make HEADROOM_BUFFERED_UPSTREAM_STREAM a runtime-env knob

The kill switch read the proxy's own environment, so `headroom wrap`
reusing a running proxy never propagated it: runtime_env.explicit_env
dropped the key and buffered turns kept streaming upstream. The knob is now
registered in RUNTIME_ENV_KNOBS and read through runtime_env.getenv, the
security-model allowlist lists it, and a test shows a propagated override
sends the original stream:false request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-14 14:46:10 +02:00
co-authored by Claude Opus 5.5
parent 3304d00a29
commit 61b4246551
4 changed files with 32 additions and 2 deletions
+1
View File
@@ -284,6 +284,7 @@ accept arbitrary environment variables — writes are restricted to a hardcoded
| `HEADROOM_INTERCEPT_READ_MIN_CHARS` | ast-grep read-interception threshold |
| `HEADROOM_VERIFY_TRUNCATION_ON_DISK` | Verify ast-grep Read truncation against disk when no client banner is found. Always returns `UNKNOWN` on Windows, where `dir_fd` / `O_NOFOLLOW` support is unavailable. |
| `HEADROOM_VERIFY_TRUNCATION_MAX_BYTES` | Max on-disk file size (bytes) the disk-verify fallback will read |
| `HEADROOM_BUFFERED_UPSTREAM_STREAM` | Send buffered CCR requests (and their continuations) upstream as `stream: true` and fold the reply (default on; `0` sends `stream: false`) |
Keys outside this list are ignored, and `/admin/upstream` is read-only — there is no
write route on the admin surface, specifically so a caller cannot redirect
+4 -2
View File
@@ -438,14 +438,16 @@ class AnthropicHandlerMixin:
First-party Anthropic only: a gateway's event stream is not something
the fold has been checked against. Kill switch:
``HEADROOM_BUFFERED_UPSTREAM_STREAM=0``.
``HEADROOM_BUFFERED_UPSTREAM_STREAM=0``, a runtime-env knob, so
``headroom wrap`` also pushes it to a reused proxy.
"""
from headroom.providers.claude.runtime import is_custom_anthropic_base_url
from headroom.proxy import runtime_env
if (
body.get("stream")
or is_custom_anthropic_base_url(url)
or os.environ.get("HEADROOM_BUFFERED_UPSTREAM_STREAM", "1").strip().lower()
or runtime_env.getenv("HEADROOM_BUFFERED_UPSTREAM_STREAM", "1").strip().lower()
in ("0", "false", "no", "off")
):
return await send(headers, body)
+6
View File
@@ -84,6 +84,12 @@ RUNTIME_ENV_KNOBS: tuple[Knob, ...] = (
"int",
"Max on-disk file size (bytes) the disk-verify fallback will read.",
),
Knob(
"HEADROOM_BUFFERED_UPSTREAM_STREAM",
"bool",
"Send buffered CCR requests upstream as stream:true and fold the reply "
"(default on; 0 sends stream:false).",
),
)
_KNOBS_BY_ENV: dict[str, Knob] = {k.env: k for k in RUNTIME_ENV_KNOBS}
@@ -294,6 +294,27 @@ async def test_requests_it_should_not_touch_go_out_unchanged(
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",