mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description
Codex on a custom model provider (how `headroom wrap codex` configures
it) compacts locally: it resends the whole history to `/v1/responses`
with `tools: []`. When that history holds a hosted `web_search_call`
item, the ChatGPT backend answers 200 and then sends the in-band error
"response protection is unavailable". Codex ignores in-band `error`
events and reports "stream closed before response.completed". It retries
five times, then runs the same compaction before every later prompt, so
the session never answers again. Headroom forwards that body unchanged
today, on HTTP and on the Codex WebSocket.
The backend behaviour comes from a user report. A compaction body
captured from the failing session held 10 `web_search_call` items. Sent
straight to `chatgpt.com/backend-api/codex/responses`, with no Headroom
in the path, it failed every time. The same body completed with the 10
items removed, and also completed with `tools: [{"type":
"web_search"}]`. A normal turn on the same history, which declares the
tool, is unaffected.
The same failure appears without Headroom:
- openai/codex#51962: Codex's built-in provider, remote compaction of a
resumed history. Removing only the `web_search_call` items fixes it
there too.
- openai/codex#52016: the same two error strings with direct ChatGPT
login.
This PR drops `web_search_call` items from a Responses `input` when the
request declares no web_search tool, before the body leaves the proxy,
and marks the body mutated so the edited bytes are sent. Only the search
action (query or URL) leaves the history. The search results were never
in it. The answer the model gave afterwards is a separate `message`
item, and it stays. A bypass request (`x-headroom-bypass: true`) is left
untouched. Every other body is forwarded exactly as before.
This works around a backend behaviour; it does not fix a Headroom bug.
It should be removed once the backend accepts such a body.
Closes #4201
## 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
- **The drop.** `_drop_undeclared_web_search_calls(payload)`
(`headroom/proxy/handlers/openai.py`, next to
`_ensure_chatgpt_responses_store_false`) drops every `web_search_call`
item from `input`. It logs `event=web_search_history request_id=...
dropped=N reason=no_web_search_tool`. It does nothing in these cases:
- A tool whose `type` starts with `web_search` is declared
(`web_search`, `web_search_preview`, the dated variants), in top-level
`tools` or in a Codex `additional_tools` input item. Codex 0.149 and
later sends tools that way, with no top-level `tools` (#3185). Without
this check a normal Codex turn would lose its search history.
- The request continues a `previous_response_id`, because its
declarations may live server-side.
- `input` is not a list, or holds no `web_search_call`.
- `HEADROOM_WEB_SEARCH_HISTORY=0`.
- **HTTP `/v1/responses`** (`handle_openai_responses`): the drop runs
before the handler first reads `input`, so compression, memory and
telemetry see the same body that is sent. It is skipped when `_bypass`
is set (`x-headroom-bypass: true` or `x-headroom-mode: passthrough`). A
drop marks the `BodyMutationTracker` with
`responses_web_search_history`, and both the stream and the buffered
forwarder pick canonical bytes from that flag. With no drop, the
client's bytes go out verbatim, as before.
- **Codex WebSocket `/v1/responses`:** the drop is called from
`_normalize_ws_response_create_for_upstream`. Every `response.create`
frame passes through there on its way upstream: the first frame, the
WS-to-HTTP fallback that replays it, later frames, and memory
continuations.
- On a bypass session (`_ws_bypass`, the same header check on the
WebSocket handshake headers) the drop is skipped and every frame goes
out as the client sent it.
- `_ws_bypass` is now resolved just before that helper is defined,
instead of a few lines later, so the helper can read it. Its value and
log line are unchanged.
- `_drop_undeclared_web_search_calls_from_frame` returns the same string
object unless it dropped something. A frame is parsed only if it holds
the literal `"web_search_call"` or any `\u` escape, since JSON can spell
the type as `"web_search\u005fcall"` and a letter or underscore can only
be escaped that way; other frames skip the parse.
- **With `optimize` off:** the drop still runs, like the forced
`store=false`, because it is a repair the request needs in order to
succeed, not an optimization.
- **Kill switch:** `HEADROOM_WEB_SEARCH_HISTORY=0`, read per request
through `runtime_env.getenv`.
- It is registered in `RUNTIME_ENV_KNOBS`, so `headroom wrap` pushes it
to a reused proxy through `POST /admin/runtime-env`, and `/health`
reports it under `config.runtime_env`.
- The allowlist table in `docs/content/docs/security-model.mdx` gets the
matching row.
- **Tests in `tests/test_openai_responses_web_search_history.py` (22
tests):**
- The decision. Drops when `tools` is empty, absent, function-only, or
holds a function named `web_search`. Keeps with each web_search type
spelling, with a web_search tool in `additional_tools`, with
`previous_response_id`, with string `input`, with no search history, and
under the kill switch.
- The frame helper: wrapped and flattened frames, and the same object
back for anything it leaves alone.
- Through the app:
- HTTP stream and buffered: upstream receives the body minus those
items, and the outbound log names the reason.
- HTTP byte-identical passthrough for a declared tool and for a history
with no searches.
- HTTP bypass, stream and buffered: byte-identical, no
`web_search_history` line.
- WS first frame, and a WS later frame after a normal turn that is
forwarded unchanged.
- WS bypass session: first and later frame unchanged.
- The WS-to-HTTP fallback.
- **Test in `tests/test_runtime_env.py`:**
`test_override_disables_web_search_history_drop_without_env` checks that
a runtime-env override of `0` turns the drop off with no environment
variable set, and that `explicit_env` puts the knob in the wrap payload.
## 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
What was run:
- Unit tests: the new file, `tests/test_runtime_env.py`, and every
existing test file for the Responses handler, the Codex WebSocket, Codex
routing and byte-faithful forwarding. Not the whole suite.
- Lint and types: `ruff check`, `ruff format --check` (ruff 0.16.8, the
pre-commit pin) and `mypy` on the changed files. Not the whole tree.
- Every run used a temporary `HOME`, with `HEADROOM_SAVINGS_EVENTS_PATH`
in that directory and `HEADROOM_BEACON=off`.
### Test Output
With the two drop call sites removed from `openai.py`, which is upstream
`main` behaviour (the helpers stay so the test module imports):
```text
$ python -m pytest tests/test_openai_responses_web_search_history.py -q -p no:cacheprovider
FAILED tests/test_openai_responses_web_search_history.py::test_http_compaction_request_reaches_upstream_without_web_search_calls[stream]
FAILED tests/test_openai_responses_web_search_history.py::test_http_compaction_request_reaches_upstream_without_web_search_calls[buffered]
FAILED tests/test_openai_responses_web_search_history.py::test_ws_first_frame_reaches_upstream_without_web_search_calls
FAILED tests/test_openai_responses_web_search_history.py::test_ws_later_frame_reaches_upstream_without_web_search_calls
FAILED tests/test_openai_responses_web_search_history.py::test_ws_http_fallback_gets_the_frame_without_web_search_calls
=================== 5 failed, 17 passed, 1 warning in 2.17s ====================
```
With the bypass gates and the knob registration removed, which is this
PR's first revision, 45d134db:
```text
$ python -m pytest tests/test_openai_responses_web_search_history.py tests/test_runtime_env.py -q -p no:cacheprovider
E assert b'{"model":"g...store":false}' == b'{\n "model...re": false\n}'
E assert b'{"model":"g...store":false}' == b'{\n "model...re": false\n}'
E assert ['{"type": "r...re": false}}'] == ['{"type": "r...re": false}}']
E AssertionError: assert 1 == 0
FAILED tests/test_openai_responses_web_search_history.py::test_http_bypass_request_is_forwarded_byte_identical[stream]
FAILED tests/test_openai_responses_web_search_history.py::test_http_bypass_request_is_forwarded_byte_identical[buffered]
FAILED tests/test_openai_responses_web_search_history.py::test_ws_bypass_session_frames_reach_upstream_unchanged
FAILED tests/test_runtime_env.py::test_override_disables_web_search_history_drop_without_env
=================== 4 failed, 42 passed, 1 warning in 3.54s ====================
```
With the fix (418faa61):
```text
$ python -m pytest tests/test_openai_responses_web_search_history.py tests/test_runtime_env.py -q -p no:cacheprovider
======================== 46 passed, 1 warning in 8.57s =========================
$ python -m pytest tests/test_openai_responses_web_search_history.py tests/test_runtime_env.py tests/test_proxy_byte_faithful_forwarding.py tests/test_ccr_response_handler_openai_responses.py tests/test_codex_responses_passthrough_bytes.py tests/test_codex_responses_waste_signals.py tests/test_codex_ws_compression_scheduler.py tests/test_codex_ws_per_frame_memory.py tests/test_codex_ws_savings_deferral.py tests/test_openai_codex_routing.py tests/test_openai_codex_ws_lifecycle.py tests/test_openai_codex_ws_timings.py tests/test_openai_responses_additional_tools.py tests/test_openai_responses_buffered_sse.py tests/test_openai_responses_compression_units.py tests/test_openai_responses_context_compaction.py tests/test_openai_responses_null_arguments.py tests/test_openai_responses_output_shaper.py tests/test_openai_responses_read_protection.py tests/test_openai_responses_t3_replay_regression.py tests/test_openai_responses_traffic_learner.py tests/test_output_shaper_responses.py tests/test_provider_codex_responses.py tests/test_provider_openai_responses.py tests/test_proxy_openai_cache_key_integration.py tests/test_proxy_openai_cache_stability.py tests/test_proxy_openai_rate_limit_key.py tests/test_proxy_openai_responses_bypass.py tests/test_proxy_openai_responses_integration.py tests/test_proxy_openai_responses_stream_ccr.py tests/test_proxy_openai.py tests/test_responses_cross_turn_dedup.py tests/test_responses_pyo3_compression.py tests/test_responses_savings_denominator.py tests/test_responses_ws_pyo3_compression.py -q -p no:cacheprovider
================= 552 passed, 14 skipped, 2 warnings in 24.94s =================
$ ruff check headroom/proxy/handlers/openai.py headroom/proxy/runtime_env.py tests/test_openai_responses_web_search_history.py tests/test_runtime_env.py && ruff format --check headroom/proxy/handlers/openai.py headroom/proxy/runtime_env.py tests/test_openai_responses_web_search_history.py tests/test_runtime_env.py # ruff 0.16.8
All checks passed!
4 files already formatted
$ mypy headroom/proxy/handlers/openai.py headroom/proxy/runtime_env.py
Success: no issues found in 2 source files
```
## Real Behavior Proof
- Environment: macOS 27.0.1 arm64, Python 3.11.13, a temporary `HOME`.
Upstream `main` f1bc2609 (exported with `git archive`) compared with
this branch at 418faa61, in the same venv.
- Exact command / steps: `repro_web_search_history.py` (below), run as
`PYTHONPATH=<tree> LITELLM_LOCAL_MODEL_COST_MAP=True python
repro_web_search_history.py`.
- It boots the real app with `create_app(ProxyConfig(optimize=False,
openai_api_url=<mock>))` inside a lifespan `TestClient`.
- The OpenAI upstream points at a local mock that records the bytes it
receives: an HTTP server for the POST and a `websockets` server for the
WebSocket.
- It sends a Codex-shaped compaction body (`tools: []`, two
`web_search_call` items, one `search` and one `open_page`) over HTTP
`POST /v1/responses` (stream) and over the Codex WebSocket.
- Each transport also gets two controls: the same history with `tools:
[{"type": "web_search"}]`, and the compaction body sent with
`x-headroom-bypass: true`.
- Observed result: main forwards all six requests with their
`web_search_call` items; this branch drops them from the `tools: []`
compaction body on HTTP and WebSocket and leaves both controls
byte-identical. Details:
- On `main`, all six requests reach upstream byte-identical,
`web_search_call` items included.
- On the branch, the compaction body loses its two items on both
transports and the drop is logged.
- On the branch, both controls go out byte-identical on both transports,
including the bypass request.
```text
=== main @ f1bc2609
== HTTP POST /v1/responses ==
-- compaction, tools: []
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: event=outbound_request forwarder=streaming method=POST body_bytes=739 body_mutated=false mutation_reasons= source=passthrough
-- control, web_search declared
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: event=outbound_request forwarder=streaming method=POST body_bytes=761 body_mutated=false mutation_reasons= source=passthrough
-- control, tools: [] with x-headroom-bypass: true
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: event=outbound_request forwarder=streaming method=POST body_bytes=739 body_mutated=false mutation_reasons= source=passthrough
== WebSocket /v1/responses (response.create) ==
-- compaction, tools: []
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: (no matching log line)
-- control, web_search declared
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: (no matching log line)
-- control, tools: [] with x-headroom-bypass: true
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: (no matching log line)
=== branch @ 418faa61
== HTTP POST /v1/responses ==
-- compaction, tools: []
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'message', 'message']
byte-identical: False
log: event=web_search_history dropped=2 reason=no_web_search_tool
log: event=outbound_request forwarder=streaming method=POST body_bytes=443 body_mutated=true mutation_reasons=responses_web_search_history source=canonical
-- control, web_search declared
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: event=outbound_request forwarder=streaming method=POST body_bytes=761 body_mutated=false mutation_reasons= source=passthrough
-- control, tools: [] with x-headroom-bypass: true
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: event=outbound_request forwarder=streaming method=POST body_bytes=739 body_mutated=false mutation_reasons= source=passthrough
== WebSocket /v1/responses (response.create) ==
-- compaction, tools: []
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'message', 'message']
byte-identical: False
log: event=web_search_history dropped=2 reason=no_web_search_tool
-- control, web_search declared
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: (no matching log line)
-- control, tools: [] with x-headroom-bypass: true
client sent : ['message', 'web_search_call', 'web_search_call', 'message', 'message']
upstream got: ['message', 'web_search_call', 'web_search_call', 'message', 'message']
byte-identical: True
log: (no matching log line)
```
The backend side was measured before this PR, from a user report. The
captured compaction body was sent straight to `chatgpt.com` with no
proxy in the path. As sent, it failed with "response protection is
unavailable". With its 10 `web_search_call` items removed, or with a
web_search tool declared, it completed. Headroom Desktop has carried the
same drop as a downstream patch since 2026-10-08, on the HTTP forwarders
only. Through that patch, the forked session that had failed six
compactions in a row compacted on the first try
(`event=web_search_history dropped=10`).
- Not tested: a live ChatGPT backend run of this branch, among others:
- This branch was not run against the live ChatGPT backend, and no live
Codex session went through it. The request shape and the backend A/B
above come from the downstream patch, which makes the same edit to the
same body.
- The WebSocket path has unit and mock coverage only. The downstream
verification ran over HTTP, because Headroom Desktop configures Codex
with `supports_websockets = false`. `headroom wrap codex` sets
`supports_websockets = true`, so most upstream users take the WebSocket
path.
- No captured Codex 0.149+ compaction body with `additional_tools` items
was available.
- The `headroom wrap` push of the knob to a reused proxy was not run end
to end. The test covers `explicit_env` and the override reaching the
reader.
- Linux and Windows were not run.
<details>
<summary>repro_web_search_history.py</summary>
```python
"""Repro: web_search_call history on a Responses request with no web_search tool.
Boots the real Headroom proxy app (create_app + ProxyConfig, optimize off so
compression cannot be the variable) in-process, points its OpenAI upstream at
a local mock, and sends a Codex local-compaction body (full history,
``tools: []``, two hosted ``web_search_call`` items) over:
1. HTTP POST /v1/responses (stream)
2. the Codex WebSocket /v1/responses (first response.create frame)
plus two controls on each transport that must reach upstream unchanged: the
same history with a web_search tool declared (a normal Codex turn), and the
compaction body sent with ``x-headroom-bypass: true``.
The mock records what upstream received. No network beyond 127.0.0.1, no
real provider call.
Run against a source tree:
PYTHONPATH=<tree> LITELLM_LOCAL_MODEL_COST_MAP=True <venv>/bin/python repro_web_search_history.py
"""
from __future__ import annotations
import asyncio
import json
import logging
import os
import re
import sys
import tempfile
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
_TMP = tempfile.mkdtemp(prefix="wsh-repro-")
os.environ["HOME"] = _TMP
os.environ["HEADROOM_SAVINGS_EVENTS_PATH"] = os.path.join(_TMP, "savings_events.jsonl")
os.environ.setdefault("HEADROOM_BEACON", "off")
os.environ.setdefault("HEADROOM_TELEMETRY", "off")
import headroom # noqa: E402
import websockets # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
HEADERS = {
"authorization": "Bearer sk-test-not-a-real-key",
"user-agent": "codex_cli_rs/0.161.0",
"originator": "codex_cli_rs",
"content-type": "application/json",
}
BYPASS_HEADERS = {**HEADERS, "x-headroom-bypass": "true"}
CASES = (
(False, HEADERS, "compaction, tools: []"),
(True, HEADERS, "control, web_search declared"),
(False, BYPASS_HEADERS, "control, tools: [] with x-headroom-bypass: true"),
)
def compaction_body(*, declare_web_search: bool) -> dict:
return {
"model": "gpt-5.5",
"instructions": "Codex base instructions.",
"input": [
{
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "what changed in the release?"}],
},
{
"type": "web_search_call",
"id": "ws_1",
"status": "completed",
"action": {"type": "search", "query": "release notes"},
},
{
"type": "web_search_call",
"id": "ws_2",
"status": "completed",
"action": {"type": "open_page", "url": "https://example.com/notes"},
},
{
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": "Three fixes shipped."}],
},
{
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "Summarize the conversation."}],
},
],
"tools": [{"type": "web_search"}] if declare_web_search else [],
"tool_choice": "auto",
"stream": True,
"store": False,
}
def types(payload: dict) -> list[str]:
return [item["type"] for item in payload["input"]]
SSE = (
b'event: response.created\ndata: {"type":"response.created","response":{"id":"r_1"}}\n\n'
b'event: response.completed\ndata: {"type":"response.completed","response":{"id":"r_1",'
b'"usage":{"input_tokens":10,"output_tokens":2,"total_tokens":12}}}\n\n'
)
class _HttpUpstream(BaseHTTPRequestHandler):
received: list[bytes] = []
def do_POST(self) -> None: # noqa: N802
self.received.append(self.rfile.read(int(self.headers["content-length"])))
self.send_response(200)
self.send_header("content-type", "text/event-stream")
self.send_header("content-length", str(len(SSE)))
self.end_headers()
self.wfile.write(SSE)
def log_message(self, *args) -> None:
pass
class _WsUpstream:
def __init__(self) -> None:
self.received: list[str] = []
self.port = 0
ready = threading.Event()
async def handler(conn) -> None:
async for frame in conn:
self.received.append(frame)
await conn.send(json.dumps({"type": "response.created", "response": {"id": "r_1"}}))
await conn.send(
json.dumps({"type": "response.completed", "response": {"id": "r_1"}})
)
async def main() -> None:
async with websockets.serve(handler, "127.0.0.1", 0) as server:
self.port = server.sockets[0].getsockname()[1]
ready.set()
await asyncio.Future()
threading.Thread(target=lambda: asyncio.run(main()), daemon=True).start()
ready.wait(5)
def make_client(upstream_url: str) -> TestClient:
config = ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
log_requests=False,
ccr_inject_tool=False,
ccr_handle_responses=False,
ccr_context_tracking=False,
image_optimize=False,
openai_api_url=upstream_url,
)
return TestClient(create_app(config))
class _Lines(logging.Handler):
def __init__(self) -> None:
super().__init__(logging.INFO)
self.lines: list[str] = []
def emit(self, record: logging.LogRecord) -> None:
msg = record.getMessage()
if "web_search_history" in msg or (
"event=outbound_request" in msg and "/v1/responses" in msg
):
self.lines.append(msg)
def report(label: str, sent_bytes: bytes | str, got: bytes | str, logs: _Lines) -> None:
sent = json.loads(sent_bytes)
wire = json.loads(got)
sent_payload = sent.get("response", sent)
wire_payload = wire.get("response", wire)
print(f"-- {label}")
print(f" client sent : {types(sent_payload)}")
print(f" upstream got: {types(wire_payload)}")
print(f" byte-identical: {sent_bytes == got}")
for line in logs.lines or ["(no matching log line)"]:
print(" log: " + re.sub(r" (path|request_id)=\S+", "", line))
logs.lines.clear()
def main() -> int:
proxy_logger = logging.getLogger("headroom.proxy")
proxy_logger.setLevel(logging.INFO)
logs = _Lines()
proxy_logger.addHandler(logs)
print(f"headroom source: {os.path.dirname(headroom.__file__)}")
http_server = ThreadingHTTPServer(("127.0.0.1", 0), _HttpUpstream)
threading.Thread(target=http_server.serve_forever, daemon=True).start()
print("== HTTP POST /v1/responses ==")
with make_client(f"http://127.0.0.1:{http_server.server_port}") as client:
for declare, headers, label in CASES:
body = json.dumps(compaction_body(declare_web_search=declare)).encode()
with client.stream("POST", "/v1/responses", headers=headers, content=body) as response:
assert response.status_code == 200, response.status_code
response.read()
report(label, body, _HttpUpstream.received[-1], logs)
ws_upstream = _WsUpstream()
print("== WebSocket /v1/responses (response.create) ==")
with make_client(f"http://127.0.0.1:{ws_upstream.port}") as client:
for declare, headers, label in CASES:
frame = json.dumps(
{"type": "response.create", "response": compaction_body(declare_web_search=declare)}
)
before = len(ws_upstream.received)
with client.websocket_connect("/v1/responses", headers=headers) as ws:
ws.send_text(frame)
while json.loads(ws.receive_text()).get("type") != "response.completed":
pass
assert len(ws_upstream.received) == before + 1
report(label, frame, ws_upstream.received[-1], logs)
return 0
if __name__ == "__main__":
sys.exit(main())
```
</details>
## Runtime Rollout Safety
- Rollout-managed feature(s): None.
- Minimum rollout channel: Stable.
- Stable/default behavior changed: Yes, for `/v1/responses` requests
(HTTP and Codex WebSocket) that meet all of these:
- `input` holds `web_search_call` items;
- neither `tools` nor an `additional_tools` item declares a web_search
tool;
- no `previous_response_id` is set;
- the request does not carry `x-headroom-bypass: true` or
`x-headroom-mode: passthrough`.
Those requests now go upstream without the `web_search_call` items,
re-serialized: canonical bytes on HTTP, `json.dumps` of the frame on the
WebSocket. Any other edit already made to the body rides along, as it
does whenever the body is mutated. One example is the forced
`store=false` on ChatGPT auth. Every other request is forwarded as
before, byte-identical when nothing else changed it. Bypass requests
stay byte-identical. Chat Completions, Anthropic, Gemini and the
`/v1/responses/{subpath}` passthrough are not touched.
- Kill switch / disable path: `HEADROOM_WEB_SEARCH_HISTORY=0` (also
`false`, `no`, `off`). It is read per request and registered as a
runtime-env knob, so there are three ways to set it:
- in the proxy's environment, which takes effect when the proxy next
starts;
- exported in the shell of a `headroom wrap` that reuses a running proxy
(wrap pushes it, no restart);
- posted to `POST /admin/runtime-env` (no restart).
A single request can also opt out with `x-headroom-bypass: true`.
- Unsafe override required: No.
- Qualification impact: only requests the backend was already failing
are changed, so no completed request's compression or cache result
moves. Such a request now completes, without reusing the previous turn's
cached prefix past the first dropped item.
- Over HTTP the drop runs before compression, so token counts and
savings are computed on the edited body, and the removed items are not
counted as saved.
- On the WebSocket it runs after compression, so the items appear in
both the before and after counts, and tokens saved is unchanged.
- Rollback path: set the kill switch, or revert these commits. Either
restores byte-identical forwarding of these bodies, and the failing
compactions with it.
## 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
- [x] 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
- Review follow-ups:
- 2099603a: bypass requests are no longer rewritten, on HTTP or on the
WebSocket.
- 418faa61: registers the kill switch as a runtime-env knob, so a reused
proxy honours it.
- Documentation: the `POST /admin/runtime-env` allowlist table in
`security-model.mdx` lists the new knob. No other page lists per-path
proxy repairs.
- Why not `body_forwarding.select_outbound_body`: it is the one place
both HTTP forwarders pick their bytes, and the downstream patch wraps it
there. But it is provider-agnostic byte policy and never sees a
WebSocket frame. A Responses-specific edit belongs in the Responses
handler, and the WebSocket needs it too.
- Why not gate on the ChatGPT backend: the failure was measured on
`chatgpt.com`, but the rule is upstream-neutral: the history references
a hosted tool the request does not declare. Dropping the item costs only
the search action. The drop also applies to API-key and custom-upstream
Responses traffic. A reviewer who prefers ChatGPT-auth only can gate the
HTTP call on `is_chatgpt_auth`; the WebSocket closure has the same flag
in scope.
- The `previous_response_id` skip is conservative. No Codex compaction
sets it, and an incremental request's earlier `additional_tools`
declarations are not in its body.
- Related: #3185 / #3186 (`additional_tools`), openai/codex#51962,
openai/codex#52016.
- Remove this workaround once a `tools: []` Responses body carrying a
`web_search_call` completes on the ChatGPT backend. The kill switch
makes that check a one-variable replay.
- Review fix fa7cea7d: the WebSocket prescreen no longer skips a frame
that spells the type with JSON escapes (`"web_search\u005fcall"`).
`test_frame_helper_decodes_escaped_type_spellings` and
`test_ws_escaped_web_search_call_frame[normal|bypass]` cover the helper
and the WS handler; both fail on 418faa61.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
481 lines
17 KiB
Python
481 lines
17 KiB
Python
"""Tests for the live runtime-env registry, override store, hot-reload endpoint,
|
|
and the wrap-side push that keeps a reused proxy in sync without a restart.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from headroom.proxy import runtime_env as rt
|
|
|
|
pytest.importorskip("fastapi")
|
|
pytest.importorskip("httpx")
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
from headroom.proxy import server as proxy_server # noqa: E402
|
|
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
|
|
from headroom.rollout import resolve_rollout # noqa: E402
|
|
|
|
_RUNTIME_ENV_BODY_CAP = 64 * 1024
|
|
|
|
|
|
def _json_object_of_size(total_bytes: int) -> bytes:
|
|
"""Build ``{"BOGUS":"...padding..."}`` at an exact byte length.
|
|
|
|
``BOGUS`` is not a registered knob, so a request built with this is
|
|
expected to be *accepted* (200, ``applied == {}``) once it clears the
|
|
size gate -- it isolates the size check from override semantics.
|
|
"""
|
|
prefix, suffix = b'{"BOGUS":"', b'"}'
|
|
pad_len = total_bytes - len(prefix) - len(suffix)
|
|
assert pad_len >= 0, f"{total_bytes} bytes is too small for the JSON skeleton"
|
|
return prefix + b"x" * pad_len + suffix
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_runtime_env(monkeypatch):
|
|
"""Each test starts with no overrides and no knob env vars set."""
|
|
for knob in rt.RUNTIME_ENV_KNOBS:
|
|
monkeypatch.delenv(knob.env, raising=False)
|
|
rt.clear_overrides()
|
|
yield
|
|
rt.clear_overrides()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Registry + override store
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_getenv_falls_back_to_environment(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_OUTPUT_SHAPER", "1")
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") == "1"
|
|
assert rt.getenv("HEADROOM_VERBOSITY_LEVEL", "2") == "2" # unset -> default
|
|
assert rt.getenv("HEADROOM_VERBOSITY_LEVEL") is None
|
|
|
|
|
|
def test_getenv_override_wins_over_environment(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_OUTPUT_SHAPER", "0")
|
|
rt.set_overrides({"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") == "1"
|
|
|
|
|
|
def test_set_overrides_ignores_unknown_keys_and_non_strings():
|
|
applied = rt.set_overrides(
|
|
{
|
|
"HEADROOM_OUTPUT_SHAPER": "1",
|
|
"NOT_A_KNOB": "x",
|
|
"HEADROOM_VERBOSITY_LEVEL": 3, # non-string ignored
|
|
}
|
|
)
|
|
assert applied == {"HEADROOM_OUTPUT_SHAPER": "1"}
|
|
assert rt.getenv("NOT_A_KNOB") is None
|
|
# The rejected non-string did not become an override.
|
|
assert rt.getenv("HEADROOM_VERBOSITY_LEVEL") is None
|
|
|
|
|
|
def test_explicit_env_returns_only_explicitly_set_knobs():
|
|
environ = {
|
|
"HEADROOM_OUTPUT_SHAPER": "1",
|
|
"HEADROOM_VERBOSITY_LEVEL": " ", # blank -> not "explicitly set"
|
|
"PATH": "/usr/bin", # not a knob
|
|
}
|
|
assert rt.explicit_env(environ) == {
|
|
"HEADROOM_OUTPUT_SHAPER": "1",
|
|
}
|
|
|
|
|
|
def test_effective_runtime_env_reports_override_or_none(monkeypatch):
|
|
rt.set_overrides({"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
eff = rt.effective_runtime_env()
|
|
assert eff["HEADROOM_OUTPUT_SHAPER"] == "1" # from override
|
|
assert eff["HEADROOM_VERBOSITY_LEVEL"] is None # unset
|
|
# Every registered knob is reported.
|
|
assert set(eff) == {knob.env for knob in rt.RUNTIME_ENV_KNOBS}
|
|
|
|
|
|
def test_clear_overrides_resets(monkeypatch):
|
|
rt.set_overrides({"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
rt.clear_overrides()
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Overrides reach the live readers (the whole point)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_override_enables_output_shaper_without_env():
|
|
from headroom.proxy.output_shaper import OutputShaperSettings
|
|
|
|
assert OutputShaperSettings.from_env().enabled is False
|
|
rt.set_overrides({"HEADROOM_OUTPUT_SHAPER": "1", "HEADROOM_VERBOSITY_LEVEL": "3"})
|
|
settings = OutputShaperSettings.from_env()
|
|
assert settings.enabled is True
|
|
assert settings.verbosity_level == 3
|
|
|
|
|
|
def test_override_changes_astgrep_threshold_without_env():
|
|
from headroom.proxy.interceptors import astgrep
|
|
|
|
assert astgrep._min_chars_to_rewrite() == 500
|
|
rt.set_overrides({"HEADROOM_INTERCEPT_READ_MIN_CHARS": "999"})
|
|
assert astgrep._min_chars_to_rewrite() == 999
|
|
# Bad value falls back to the documented default rather than raising.
|
|
rt.set_overrides({"HEADROOM_INTERCEPT_READ_MIN_CHARS": "not-an-int"})
|
|
assert astgrep._min_chars_to_rewrite() == 500
|
|
|
|
|
|
def test_override_disables_web_search_history_drop_without_env():
|
|
from headroom.proxy.handlers.openai import _drop_undeclared_web_search_calls
|
|
|
|
def body() -> dict:
|
|
return {"input": [{"type": "web_search_call", "id": "ws_1"}], "tools": []}
|
|
|
|
assert _drop_undeclared_web_search_calls(body()) == 1
|
|
rt.set_overrides({"HEADROOM_WEB_SEARCH_HISTORY": "0"})
|
|
assert _drop_undeclared_web_search_calls(body()) == 0
|
|
assert rt.explicit_env({"HEADROOM_WEB_SEARCH_HISTORY": "0"}) == {
|
|
"HEADROOM_WEB_SEARCH_HISTORY": "0"
|
|
}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# /health surface + /admin/runtime-env hot-reload endpoint
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.fixture
|
|
def loopback_client(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as c:
|
|
yield c
|
|
|
|
|
|
def test_health_exposes_runtime_env(loopback_client):
|
|
config = loopback_client.get("/health").json()["config"]
|
|
assert "runtime_env" in config
|
|
assert set(config["runtime_env"]) == {knob.env for knob in rt.RUNTIME_ENV_KNOBS}
|
|
assert config["runtime_env"]["HEADROOM_OUTPUT_SHAPER"] is None
|
|
|
|
|
|
def test_admin_runtime_env_applies_and_reflects_in_health(loopback_client):
|
|
resp = loopback_client.post(
|
|
"/admin/runtime-env",
|
|
json={"HEADROOM_OUTPUT_SHAPER": "1", "HEADROOM_VERBOSITY_LEVEL": "3", "BOGUS": "x"},
|
|
)
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body["applied"] == {"HEADROOM_OUTPUT_SHAPER": "1", "HEADROOM_VERBOSITY_LEVEL": "3"}
|
|
assert body["runtime_env"]["HEADROOM_OUTPUT_SHAPER"] == "1"
|
|
# And it is observable on the live /health surface.
|
|
health = loopback_client.get("/health").json()["config"]["runtime_env"]
|
|
assert health["HEADROOM_OUTPUT_SHAPER"] == "1"
|
|
assert health["HEADROOM_VERBOSITY_LEVEL"] == "3"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("rollout", "expected_enabled", "expected_reason"),
|
|
[
|
|
(resolve_rollout({"HEADROOM_ROLLOUT_CHANNEL": "beta"}), True, "legacy_alias"),
|
|
# Was ``(resolve_rollout({}), False, "blocked_by_channel")`` while
|
|
# ``proxy_output_shaper`` was BETA: on the default channel the admin
|
|
# POST could not enable it. The feature is now STABLE and on by
|
|
# default, so the same POST is honoured. The escalation-refusal
|
|
# property this case used to cover cannot be reproduced through this
|
|
# endpoint any more — ``/admin/runtime-env`` re-resolves exactly one
|
|
# rollout alias, ``HEADROOM_OUTPUT_SHAPER`` (see server.py), so there
|
|
# is no second, still-gated feature to point it at. Channel gating
|
|
# itself stays covered in test_rollout.py.
|
|
(resolve_rollout({}), True, "legacy_alias"),
|
|
(
|
|
resolve_rollout(
|
|
{
|
|
"HEADROOM_ROLLOUT_CHANNEL": "beta",
|
|
"HEADROOM_DISABLE_FEATURES": "proxy_output_shaper",
|
|
}
|
|
),
|
|
False,
|
|
"disabled",
|
|
),
|
|
],
|
|
)
|
|
def test_admin_runtime_env_reresolves_running_rollout_without_weakening_policy(
|
|
rollout, expected_enabled, expected_reason
|
|
):
|
|
app = create_app(
|
|
ProxyConfig(
|
|
rollout=rollout,
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
)
|
|
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
|
|
before = client.get("/stats?cached=1").json()["rollout"]
|
|
response = client.post("/admin/runtime-env", json={"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
after = client.get("/stats?cached=1").json()["rollout"]
|
|
|
|
decision = next(item for item in after["features"] if item["name"] == "proxy_output_shaper")
|
|
assert response.status_code == 200
|
|
assert response.json()["rollout"] == after
|
|
assert decision["enabled"] is expected_enabled
|
|
assert decision["decision"] == expected_reason
|
|
assert after["snapshot_digest"] != before["snapshot_digest"]
|
|
|
|
|
|
def test_admin_runtime_env_rejects_non_object(loopback_client):
|
|
resp = loopback_client.post("/admin/runtime-env", json=["not", "a", "dict"])
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_admin_runtime_env_rejects_process_local_update_with_multiple_workers(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
rollout = resolve_rollout({"HEADROOM_ROLLOUT_CHANNEL": "beta"})
|
|
config = ProxyConfig(
|
|
worker_processes=2,
|
|
rollout=rollout,
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
before_digest = rollout.snapshot_digest
|
|
|
|
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:
|
|
response = client.post("/admin/runtime-env", json={"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
after = client.get("/stats").json()["rollout"]
|
|
|
|
assert response.status_code == 409
|
|
assert response.json()["worker_processes"] == 2
|
|
assert "restart" in response.json()["error"]
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") is None
|
|
assert after["snapshot_digest"] == before_digest
|
|
|
|
|
|
def test_admin_runtime_env_is_loopback_only():
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
with TestClient(app, base_url="http://127.0.0.1", client=("10.0.0.1", 54321)) as external:
|
|
resp = external.post("/admin/runtime-env", json={"HEADROOM_OUTPUT_SHAPER": "1"})
|
|
assert resp.status_code == 404 # invisible to non-loopback callers
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") is None # nothing applied
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# request body size limit is enforced against streamed bytes, not
|
|
# Content-Length (V-001 follow-up: the header is client-controlled and must
|
|
# never be trusted as the enforcement boundary)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _streaming_client(app):
|
|
"""An httpx client that drives ``app`` over ASGI without TestClient's
|
|
requests-based transport -- needed so a generator body can be sent
|
|
without httpx computing a Content-Length for us.
|
|
"""
|
|
import httpx
|
|
|
|
transport = httpx.ASGITransport(app=app, client=("127.0.0.1", 12345))
|
|
return httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1")
|
|
|
|
|
|
async def _chunked(body: bytes, chunk_size: int = 4096):
|
|
for i in range(0, len(body), chunk_size):
|
|
yield body[i : i + chunk_size]
|
|
|
|
|
|
async def test_admin_runtime_env_accepts_body_at_the_cap_without_content_length(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
body = _json_object_of_size(_RUNTIME_ENV_BODY_CAP)
|
|
|
|
async with _streaming_client(app) as client:
|
|
resp = await client.post(
|
|
"/admin/runtime-env",
|
|
content=_chunked(body),
|
|
headers={"content-type": "application/json"},
|
|
)
|
|
|
|
assert "content-length" not in resp.request.headers
|
|
assert resp.status_code == 200
|
|
assert resp.json()["applied"] == {}
|
|
|
|
|
|
async def test_admin_runtime_env_rejects_oversized_chunked_body(monkeypatch):
|
|
"""No Content-Length at all (the chunked/streamed case) must still be capped."""
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
body = _json_object_of_size(_RUNTIME_ENV_BODY_CAP + 1000)
|
|
|
|
json_loads_calls = []
|
|
real_loads = json.loads
|
|
monkeypatch.setattr(
|
|
proxy_server.json,
|
|
"loads",
|
|
lambda *a, **k: json_loads_calls.append(a) or real_loads(*a, **k),
|
|
)
|
|
|
|
async with _streaming_client(app) as client:
|
|
resp = await client.post(
|
|
"/admin/runtime-env",
|
|
content=_chunked(body),
|
|
headers={"content-type": "application/json"},
|
|
)
|
|
|
|
assert "content-length" not in resp.request.headers
|
|
assert resp.status_code == 413
|
|
assert real_loads(resp.content) == {"error": "request body too large"}
|
|
assert not json_loads_calls # the oversized body was never parsed
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") is None
|
|
|
|
|
|
async def test_admin_runtime_env_rejects_body_exceeding_declared_content_length(monkeypatch):
|
|
"""A Content-Length that understates the real body must not let it through.
|
|
|
|
httpx does not recompute Content-Length for an explicit header, so this
|
|
sends a deliberately wrong ``Content-Length: 1`` alongside a body that
|
|
actually streams well past the 64 KiB cap -- exactly the mismatch the
|
|
original Content-Length-only guard was blind to.
|
|
"""
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
app = create_app(config)
|
|
body = _json_object_of_size(_RUNTIME_ENV_BODY_CAP + 1000)
|
|
|
|
json_loads_calls = []
|
|
real_loads = json.loads
|
|
monkeypatch.setattr(
|
|
proxy_server.json,
|
|
"loads",
|
|
lambda *a, **k: json_loads_calls.append(a) or real_loads(*a, **k),
|
|
)
|
|
|
|
async with _streaming_client(app) as client:
|
|
resp = await client.post(
|
|
"/admin/runtime-env",
|
|
content=_chunked(body),
|
|
headers={"content-type": "application/json", "content-length": "1"},
|
|
)
|
|
|
|
assert resp.request.headers["content-length"] == "1"
|
|
assert resp.status_code == 413
|
|
assert real_loads(resp.content) == {"error": "request body too large"}
|
|
assert not json_loads_calls # the oversized body was never parsed
|
|
assert rt.getenv("HEADROOM_OUTPUT_SHAPER") is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# wrap-side push
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_push_runtime_env_posts_explicit_env(monkeypatch):
|
|
import urllib.request
|
|
|
|
from headroom.cli import wrap
|
|
|
|
monkeypatch.setenv("HEADROOM_OUTPUT_SHAPER", "1")
|
|
monkeypatch.setenv("HEADROOM_VERBOSITY_LEVEL", "3")
|
|
|
|
captured = {}
|
|
|
|
class _Resp:
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
def read(self):
|
|
return b"{}"
|
|
|
|
def fake_urlopen(request, timeout=None):
|
|
captured["url"] = request.full_url
|
|
captured["body"] = request.data
|
|
return _Resp()
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
|
|
wrap._push_runtime_env(8787, no_proxy=False)
|
|
|
|
assert captured["url"] == "http://127.0.0.1:8787/admin/runtime-env"
|
|
import json
|
|
|
|
assert json.loads(captured["body"]) == {
|
|
"HEADROOM_OUTPUT_SHAPER": "1",
|
|
"HEADROOM_VERBOSITY_LEVEL": "3",
|
|
}
|
|
|
|
|
|
def test_push_runtime_env_noop_when_nothing_set(monkeypatch):
|
|
import urllib.request
|
|
|
|
from headroom.cli import wrap
|
|
|
|
def boom(*a, **k): # must never be called
|
|
raise AssertionError("should not POST when nothing is explicitly set")
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", boom)
|
|
wrap._push_runtime_env(8787, no_proxy=False) # no env set -> no-op
|
|
|
|
|
|
def test_push_runtime_env_noop_when_no_proxy(monkeypatch):
|
|
import urllib.request
|
|
|
|
from headroom.cli import wrap
|
|
|
|
monkeypatch.setenv("HEADROOM_OUTPUT_SHAPER", "1")
|
|
monkeypatch.setattr(
|
|
urllib.request, "urlopen", lambda *a, **k: (_ for _ in ()).throw(AssertionError("no POST"))
|
|
)
|
|
wrap._push_runtime_env(8787, no_proxy=True) # --no-proxy -> no-op
|
|
|
|
|
|
def test_push_runtime_env_swallows_unreachable_proxy(monkeypatch):
|
|
import urllib.request
|
|
|
|
from headroom.cli import wrap
|
|
|
|
monkeypatch.setenv("HEADROOM_OUTPUT_SHAPER", "1")
|
|
|
|
def refused(*a, **k):
|
|
raise OSError("connection refused")
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", refused)
|
|
# Best-effort: an unreachable / old proxy must not raise.
|
|
wrap._push_runtime_env(8787, no_proxy=False)
|