mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(proxy): leave a bypass request's web_search_call history untouched
A request sent with `x-headroom-bypass: true` (or `x-headroom-mode: passthrough`) asked Headroom to stay out of its bytes, but the web_search_call drop still rewrote it. Skip the drop for such a request: - HTTP /v1/responses: the drop is gated on the handler's `_bypass`. - Codex WebSocket: `_ws_bypass` is resolved before the frame helpers that read it, and `_normalize_ws_response_create_for_upstream` skips the drop on a bypass session, so every frame goes out as the client sent it. Tests: a bypass compaction body goes upstream byte-identical over HTTP (stream and buffered) and over the WebSocket (first and later frame), with no web_search_history log line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1108,8 +1108,10 @@ def _drop_undeclared_web_search_calls(
|
||||
live server-side.
|
||||
|
||||
Mutates *payload* in place and returns the number of items dropped
|
||||
(0 = untouched). Disable with ``HEADROOM_WEB_SEARCH_HISTORY=0``. Remove
|
||||
once the backend accepts such a body.
|
||||
(0 = untouched). Callers skip it for a bypass request
|
||||
(``x-headroom-bypass: true``), which asked for its bytes untouched.
|
||||
Disable with ``HEADROOM_WEB_SEARCH_HISTORY=0``. Remove once the backend
|
||||
accepts such a body.
|
||||
"""
|
||||
items = payload.get("input")
|
||||
if not isinstance(items, list) or payload.get("previous_response_id"):
|
||||
@@ -5680,7 +5682,8 @@ class OpenAIHandlerMixin:
|
||||
#
|
||||
# Before anything reads `input`: drop web_search_call history from a
|
||||
# request that declares no web_search tool (Codex local compaction).
|
||||
if _drop_undeclared_web_search_calls(body, request_id=request_id):
|
||||
# A bypass request asked for its bytes untouched, so it is left alone.
|
||||
if not _bypass and _drop_undeclared_web_search_calls(body, request_id=request_id):
|
||||
body_mutation_tracker.mark_mutated("responses_web_search_history")
|
||||
input_data = body.get("input", "")
|
||||
instructions = body.get("instructions")
|
||||
@@ -7539,6 +7542,14 @@ class OpenAIHandlerMixin:
|
||||
model or "unknown",
|
||||
)
|
||||
|
||||
# Resolved before the frame helpers below, which read it.
|
||||
_ws_bypass = self._headroom_bypass_enabled(ws_headers)
|
||||
if _ws_bypass:
|
||||
logger.info(
|
||||
"[%s] WS /v1/responses passthrough reason=bypass_header mutation=disabled",
|
||||
request_id,
|
||||
)
|
||||
|
||||
def _normalize_ws_response_create_for_upstream(raw_msg: str) -> str:
|
||||
"""Optionally flatten Codex WS response.create frames for gateways.
|
||||
|
||||
@@ -7551,11 +7562,13 @@ class OpenAIHandlerMixin:
|
||||
the HTTP fallback also replays, later frames, memory
|
||||
continuations) passes through here, so it also drops
|
||||
web_search_call history the frame declares no tool for (see
|
||||
``_drop_undeclared_web_search_calls``).
|
||||
``_drop_undeclared_web_search_calls``), except on a bypass
|
||||
session, whose frames stay as the client sent them.
|
||||
"""
|
||||
raw_msg = _drop_undeclared_web_search_calls_from_frame(
|
||||
raw_msg, request_id=request_id
|
||||
)
|
||||
if not _ws_bypass:
|
||||
raw_msg = _drop_undeclared_web_search_calls_from_frame(
|
||||
raw_msg, request_id=request_id
|
||||
)
|
||||
if os.environ.get(
|
||||
"HEADROOM_OPENAI_WS_FLATTEN_RESPONSE_CREATE", ""
|
||||
).strip().lower() not in (
|
||||
@@ -7645,12 +7658,6 @@ class OpenAIHandlerMixin:
|
||||
ws_recorded_compression_timing_totals: dict[str, float] = {}
|
||||
ws_ttfb_ms: float | None = None
|
||||
ws_recorded_ttfb_ms = False
|
||||
_ws_bypass = self._headroom_bypass_enabled(ws_headers)
|
||||
if _ws_bypass:
|
||||
logger.info(
|
||||
"[%s] WS /v1/responses passthrough reason=bypass_header mutation=disabled",
|
||||
request_id,
|
||||
)
|
||||
|
||||
capture_codex_wire_debug(
|
||||
"ws_inbound_first_frame",
|
||||
|
||||
@@ -184,19 +184,22 @@ _CODEX_HEADERS = {
|
||||
}
|
||||
|
||||
|
||||
def _post(body_bytes: bytes, *, stream: bool) -> tuple[bytes, list[str]]:
|
||||
def _post(
|
||||
body_bytes: bytes, *, stream: bool, extra_headers: dict[str, str] | None = None
|
||||
) -> tuple[bytes, list[str]]:
|
||||
headers = {**_CODEX_HEADERS, **(extra_headers or {})}
|
||||
client, transport = _make_no_optimize_app()
|
||||
proxy_logger, handler, prev_level, records = _start_proxy_log_capture()
|
||||
try:
|
||||
if stream:
|
||||
with client.stream(
|
||||
"POST", "/v1/responses", headers=_CODEX_HEADERS, content=body_bytes
|
||||
"POST", "/v1/responses", headers=headers, content=body_bytes
|
||||
) as response:
|
||||
assert response.status_code == 200
|
||||
for _ in response.iter_bytes():
|
||||
pass
|
||||
else:
|
||||
response = client.post("/v1/responses", headers=_CODEX_HEADERS, content=body_bytes)
|
||||
response = client.post("/v1/responses", headers=headers, content=body_bytes)
|
||||
assert response.status_code == 200, response.text
|
||||
finally:
|
||||
_stop_proxy_log_capture(proxy_logger, handler, prev_level)
|
||||
@@ -237,6 +240,17 @@ def test_http_unaffected_request_is_forwarded_byte_identical(body: dict[str, Any
|
||||
assert not any("event=web_search_history" in m for m in logs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stream", [True, False], ids=["stream", "buffered"])
|
||||
def test_http_bypass_request_is_forwarded_byte_identical(stream: bool) -> None:
|
||||
# A bypass client asked Headroom to stay out of its bytes, so even a body
|
||||
# the backend will refuse goes out exactly as sent.
|
||||
body_bytes = json.dumps(_compaction_body(stream=stream), indent=2).encode()
|
||||
sent, logs = _post(body_bytes, stream=stream, extra_headers={"x-headroom-bypass": "true"})
|
||||
|
||||
assert sent == body_bytes
|
||||
assert not any("event=web_search_history" in m for m in logs)
|
||||
|
||||
|
||||
# --- Codex WebSocket /v1/responses ------------------------------------------
|
||||
|
||||
|
||||
@@ -268,6 +282,33 @@ async def test_ws_first_frame_reaches_upstream_without_web_search_calls() -> Non
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ws_bypass_session_frames_reach_upstream_unchanged() -> None:
|
||||
compaction = _frame(_compaction_body())
|
||||
upstream = _FakeUpstream([], hold_after_events=True)
|
||||
client_ws = _FakeWebSocket(
|
||||
frames=[compaction, compaction],
|
||||
headers={"authorization": "Bearer test", "x-headroom-bypass": "true"},
|
||||
hold_after_initial=True,
|
||||
)
|
||||
|
||||
async def _disconnect_soon() -> None:
|
||||
await asyncio.sleep(0.05)
|
||||
client_ws.trigger_disconnect()
|
||||
|
||||
with patch.dict(sys.modules, {"websockets": _make_fake_websockets_module(upstream)}):
|
||||
trigger = asyncio.create_task(_disconnect_soon())
|
||||
try:
|
||||
await asyncio.wait_for(
|
||||
_DummyOpenAIHandler().handle_openai_responses_ws(client_ws), timeout=2.0
|
||||
)
|
||||
finally:
|
||||
trigger.cancel()
|
||||
|
||||
# First frame and a later frame both go out as the client sent them.
|
||||
assert upstream.sent == [compaction, compaction]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ws_later_frame_reaches_upstream_without_web_search_calls() -> None:
|
||||
normal_turn = _frame(_compaction_body(tools=[{"type": "web_search"}]))
|
||||
|
||||
Reference in New Issue
Block a user