Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): classify Pi Codex Responses alias (#2583)
Hyunsik Kanga493f559a1a8
diff --git a/headroom/proxy/auth_policy.py b/headroom/proxy/auth_policy.pyindex 7e55cc95a..2bc1be668 100644--- a/headroom/proxy/auth_policy.py+++ b/headroom/proxy/auth_policy.py@@ -46,6 +46,10 @@ CLIENT_UA_MAP: tuple[tuple[str, str], ...] = (CODEX_RESPONSES_PATH = "/v1/responses"+CODEX_RESPONSES_PATHS: tuple[str, ...] = (+ CODEX_RESPONSES_PATH,+ "/v1/codex/responses",+)@dataclass(frozen=True)@@ -111,8 +115,8 @@ def classify_client_signals(signals: AuthSignals, *, default: str | None = None)def is_codex_responses_path(path: str) -> bool:- """Return True for the OpenAI Responses endpoint and its subpaths."""- return path == CODEX_RESPONSES_PATH or path.startswith(CODEX_RESPONSES_PATH + "/")+ """Return True for OpenAI Responses endpoints and their subpaths."""+ return any(root == path or path.startswith(root + "/") for root in CODEX_RESPONSES_PATHS)def should_stamp_codex_client_signals(path: str, signals: AuthSignals) -> bool:diff --git a/tests/test_codex_client_stamp.py b/tests/test_codex_client_stamp.pyindex 3925f9960..38a870d04 100644--- a/tests/test_codex_client_stamp.py+++ b/tests/test_codex_client_stamp.py@@ -1,9 +1,9 @@"""Tests for ``should_stamp_codex_client`` — the path-based ``X-Client: codex``stamp on the Responses endpoint.-The stamp fires only for an unidentified caller on the Responses endpoint, so-Codex Desktop (whose User-Agent isn't a known codex UA) takes the codex-fail-open path instead of being refused with a 413 on a compression timeout.+The stamp fires only for an unidentified caller on a Responses endpoint, so+Codex Desktop and Pi-compatible Codex callers take the codex fail-open path+instead of being refused with a 413 on a compression timeout."""from __future__ import annotations@@ -13,12 +13,17 @@ from headroom.proxy.auth_mode import classify_client, should_stamp_codex_clientCODEX_DESKTOP_UA = ("Codex Desktop/0.140.0-alpha.2 (Mac OS 15.7.7; arm64) unknown (Codex Desktop; 26.609.71450)")+PI_UA = "pi/0.11.11 (darwin 25.5.0; arm64)"def test_unidentified_codex_desktop_on_responses_is_stamped() -> None:assert should_stamp_codex_client("/v1/responses", {"user-agent": CODEX_DESKTOP_UA})+def test_unidentified_pi_on_codex_responses_alias_is_stamped() -> None:+ assert should_stamp_codex_client("/v1/codex/responses", {"user-agent": PI_UA})++def test_stamp_then_classify_yields_codex() -> None:# End-to-end of what the HTTP middleware and the WS handler both do:# stamp the header, after which classify_client must read "codex".@@ -36,6 +41,13 @@ def test_responses_subpath_is_stamped() -> None:assert should_stamp_codex_client("/v1/responses/foo", {"user-agent": CODEX_DESKTOP_UA})+def test_codex_responses_alias_subpath_is_stamped() -> None:+ assert should_stamp_codex_client(+ "/v1/codex/responses/compact",+ {"user-agent": PI_UA},+ )++def test_other_path_is_not_stamped() -> None:# Scoped to the Responses endpoint; unknown callers elsewhere are untouched.assert not should_stamp_codex_client("/v1/chat/completions", {"user-agent": CODEX_DESKTOP_UA})diff --git a/tests/test_openai_codex_ws_lifecycle.py b/tests/test_openai_codex_ws_lifecycle.pyindex 877187097..2b8e9d9d6 100644--- a/tests/test_openai_codex_ws_lifecycle.py+++ b/tests/test_openai_codex_ws_lifecycle.py@@ -1792,6 +1792,35 @@ async def test_ws_recognized_client_with_real_path_is_not_restamped():assert handler.ws_sessions.active_count() == 0+async def test_ws_pi_codex_responses_alias_is_stamped():+ """The Pi-compatible Codex alias gets the same client stamp as /v1/responses."""+ upstream_events = [+ json.dumps({"type": "response.created", "response": {"id": "r_1"}}),+ json.dumps({"type": "response.completed", "response": {"id": "r_1"}}),+ ]+ connect_calls: list[tuple[tuple, dict]] = []+ upstream = _FakeUpstream(upstream_events)+ fake_ws_mod = _make_fake_websockets_module(upstream, connect_calls=connect_calls)+ client_ws = _FakeWebSocket(+ frames=[_first_frame()],+ headers={+ "authorization": "Bearer test",+ "user-agent": "pi/0.11.11 (darwin 25.5.0; arm64)",+ },+ )+ client_ws.url = SimpleNamespace(path="/v1/codex/responses")+ handler = _DummyOpenAIHandler()++ with patch.dict(sys.modules, {"websockets": fake_ws_mod}):+ await handler.handle_openai_responses_ws(client_ws)++ assert len(connect_calls) == 1+ forwarded_headers = connect_calls[0][1]["additional_headers"]+ assert forwarded_headers["x-client"] == "codex"+ assert handler.ws_sessions.active_count() == 0++@pytest.mark.asyncio@pytest.mark.parametrize("store", [True, False])@pytest.mark.parametrize(