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 Codex WebSocket HTTP 500: ChatGPT auth routing, correct beta header, HTTP fallback (#71)
chopratejasf4160f9e4703
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 949e8be92..83e17df88 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -751,7 +751,12 @@ class OpenAIHandlerMixin:f"(backend '{self.anthropic_backend.name}' not used for Responses API)")- url = f"{self.OPENAI_API_URL}/v1/responses"+ # Route to correct endpoint based on auth mode.+ # ChatGPT session auth (codex login) uses chatgpt.com, not api.openai.com.+ if headers.get("chatgpt-account-id"):+ url = "https://chatgpt.com/backend-api/codex/responses"+ else:+ url = f"{self.OPENAI_API_URL}/v1/responses"try:if stream:@@ -870,11 +875,6 @@ class OpenAIHandlerMixin:else:await websocket.accept()- # Build upstream WebSocket URL (http→ws, https→wss)- base = self.OPENAI_API_URL- ws_base = base.replace("https://", "wss://").replace("http://", "ws://")- upstream_url = f"{ws_base}/v1/responses"-# Forward all client headers except hop-by-hop / per-connection headers.# These are WebSocket handshake mechanics that the `websockets` library# generates fresh for the upstream connection — forwarding them would conflict.@@ -898,10 +898,28 @@ class OpenAIHandlerMixin:if k.lower() not in _skip_headers:upstream_headers[k] = v+ # Detect ChatGPT session auth vs API key auth.+ # Codex sends `ChatGPT-Account-ID` header when using `codex login`+ # (ChatGPT OAuth), which requires routing to chatgpt.com, not api.openai.com.+ _lower_headers = {k.lower(): v for k, v in upstream_headers.items()}+ is_chatgpt_auth = "chatgpt-account-id" in _lower_headers++ # Build upstream WebSocket URL based on auth mode+ if is_chatgpt_auth:+ # ChatGPT session auth → route to chatgpt.com backend+ upstream_url = "wss://chatgpt.com/backend-api/codex/responses"+ logger.debug(+ f"[{request_id}] WS: ChatGPT session auth detected, routing to chatgpt.com"+ )+ else:+ # API key auth → route to configured OpenAI API URL+ base = self.OPENAI_API_URL+ ws_base = base.replace("https://", "wss://").replace("http://", "ws://")+ upstream_url = f"{ws_base}/v1/responses"+# Ensure Authorization header is present — fall back to OPENAI_API_KEY env var.# Safety net for clients that don't forward auth headers via WebSocket upgrade.- _has_auth = "authorization" in {k.lower() for k in upstream_headers}- if not _has_auth:+ if "authorization" not in _lower_headers:api_key = os.environ.get("OPENAI_API_KEY")if api_key:upstream_headers["Authorization"] = f"Bearer {api_key}"@@ -912,9 +930,10 @@ class OpenAIHandlerMixin:f"OPENAI_API_KEY not set — upstream will likely reject")- # Ensure the required beta header is present — OpenAI returns 500 without it- if "openai-beta" not in {k.lower() for k in upstream_headers}:- upstream_headers["OpenAI-Beta"] = "responses-api=v1"+ # Ensure the required beta header is present — OpenAI returns 500 without it.+ # Codex sends `responses_websockets=2026-02-06`; only inject if missing entirely.+ if "openai-beta" not in _lower_headers:+ upstream_headers["OpenAI-Beta"] = "responses_websockets=2026-02-06"logger.debug(f"[{request_id}] WS upstream headers: "@@ -927,6 +946,7 @@ class OpenAIHandlerMixin:first_msg_raw = await websocket.receive_text()# --- Optional: compress the input in the first message ---+ body: dict[str, Any] = {}try:body = json.loads(first_msg_raw)input_data = body.get("input")@@ -997,55 +1017,78 @@ class OpenAIHandlerMixin:# Windows system cert store, causing HTTP 500 on wss:// connections.use_ssl: bool | None = True if upstream_url.startswith("wss://") else None- async with websockets.connect(- upstream_url,- additional_headers=upstream_headers,- subprotocols=(- [websockets.Subprotocol(p) for p in client_subprotocols]- if client_subprotocols and hasattr(websockets, "Subprotocol")- else client_subprotocols or None- ),- ssl=use_ssl,- ) as upstream:- # Send (potentially compressed) first message- await upstream.send(first_msg_raw)-- # Bidirectional relay- async def _client_to_upstream() -> None:- try:- while True:- msg = await websocket.receive_text()- await upstream.send(msg)- except Exception as relay_err:- if "WebSocketDisconnect" not in type(relay_err).__name__:- logger.debug(- f"[{request_id}] WS client→upstream relay ended: {relay_err}"- )- with contextlib.suppress(Exception):- await upstream.close()-- async def _upstream_to_client() -> None:- try:- async for msg in upstream:- if isinstance(msg, str):- await websocket.send_text(msg)- elif isinstance(msg, bytes):- await websocket.send_bytes(msg)- else:- await websocket.send_text(str(msg))- except Exception as relay_err:- if "WebSocketDisconnect" not in type(relay_err).__name__:- logger.debug(- f"[{request_id}] WS upstream→client relay ended: {relay_err}"- )- finally:- with contextlib.suppress(Exception):- await websocket.close()+ ws_connected = False+ try:+ async with websockets.connect(+ upstream_url,+ additional_headers=upstream_headers,+ subprotocols=(+ [websockets.Subprotocol(p) for p in client_subprotocols]+ if client_subprotocols and hasattr(websockets, "Subprotocol")+ else client_subprotocols or None+ ),+ ssl=use_ssl,+ ) as upstream:+ ws_connected = True+ # Send (potentially compressed) first message+ await upstream.send(first_msg_raw)++ # Bidirectional relay+ async def _client_to_upstream() -> None:+ try:+ while True:+ msg = await websocket.receive_text()+ await upstream.send(msg)+ except Exception as relay_err:+ if "WebSocketDisconnect" not in type(relay_err).__name__:+ logger.debug(+ f"[{request_id}] WS client→upstream relay ended: {relay_err}"+ )+ with contextlib.suppress(Exception):+ await upstream.close()++ async def _upstream_to_client() -> None:+ try:+ async for msg in upstream:+ if isinstance(msg, str):+ await websocket.send_text(msg)+ elif isinstance(msg, bytes):+ await websocket.send_bytes(msg)+ else:+ await websocket.send_text(str(msg))+ except Exception as relay_err:+ if "WebSocketDisconnect" not in type(relay_err).__name__:+ logger.debug(+ f"[{request_id}] WS upstream→client relay ended: {relay_err}"+ )+ finally:+ with contextlib.suppress(Exception):+ await websocket.close()++ await asyncio.gather(+ _client_to_upstream(),+ _upstream_to_client(),+ return_exceptions=True,+ )- await asyncio.gather(- _client_to_upstream(),- _upstream_to_client(),- return_exceptions=True,+ except Exception as ws_err:+ if ws_connected:+ # WS was established but broke mid-stream — re-raise+ raise+ # WS upgrade failed (HTTP 500 from OpenAI is common).+ # Fall back to HTTP POST streaming and relay SSE events+ # back over the client WebSocket transparently.+ _ws_detail = str(ws_err)+ if hasattr(ws_err, "response"):+ resp_body = getattr(getattr(ws_err, "response", None), "body", b"")+ if resp_body:+ _ws_detail += f" | {resp_body[:300].decode('utf-8', errors='replace')}"+ logger.warning(+ f"[{request_id}] WS upstream failed ({_ws_detail}), "+ f"falling back to HTTP POST streaming"+ )+ await self._ws_http_fallback(+ websocket, body, first_msg_raw, upstream_headers, request_id)# Record metrics@@ -1078,6 +1121,126 @@ class OpenAIHandlerMixin:with contextlib.suppress(Exception):await websocket.close(code=1011, reason=str(e)[:120])+ async def _ws_http_fallback(+ self,+ websocket: WebSocket,+ body: dict[str, Any],+ first_msg_raw: str,+ upstream_headers: dict[str, str],+ request_id: str,+ ) -> None:+ """Fall back to HTTP POST streaming when upstream WS fails.++ Converts the WS ``response.create`` message to an HTTP POST to+ ``/v1/responses?stream=true``, reads SSE events, and relays each+ ``data:`` line as a WS text message to the client. This makes+ Codex work immediately instead of exhausting its WS retry budget.+ """+ # Route to correct endpoint based on auth mode+ _lower = {k.lower() for k in upstream_headers}+ if "chatgpt-account-id" in _lower:+ http_url = "https://chatgpt.com/backend-api/codex/responses"+ else:+ http_url = f"{self.OPENAI_API_URL}/v1/responses"++ # Build HTTP body from the WS response.create payload.+ # WS messages use {"type": "response.create", "response": {...}} wrapper.+ # The HTTP POST endpoint expects the inner response object directly.+ http_body: dict[str, Any]+ try:+ parsed = json.loads(first_msg_raw) if isinstance(first_msg_raw, str) else body+ except (json.JSONDecodeError, TypeError):+ parsed = body++ # Unwrap the response.create envelope if present+ if isinstance(parsed.get("response"), dict):+ http_body = parsed["response"]+ else:+ http_body = parsed++ # Ensure streaming is enabled so we get SSE events+ http_body["stream"] = True++ # Build HTTP headers from the upstream headers (already stripped of WS+ # hop-by-hop headers by the caller).+ http_headers = dict(upstream_headers)+ http_headers["content-type"] = "application/json"++ logger.debug(f"[{request_id}] WS→HTTP fallback POST to {http_url}")++ try:+ async with self.http_client.stream(+ "POST",+ http_url,+ headers=http_headers,+ json=http_body,+ timeout=120.0,+ ) as response:+ if response.status_code != 200:+ error_body = b""+ async for chunk in response.aiter_bytes():+ error_body += chunk+ if len(error_body) > 2000:+ break+ error_text = error_body.decode("utf-8", errors="replace")+ logger.error(+ f"[{request_id}] WS→HTTP fallback got {response.status_code}: "+ f"{error_text[:500]}"+ )+ # Send error as WS message so client sees it+ error_event = {+ "type": "error",+ "error": {+ "type": "server_error",+ "message": f"Upstream returned {response.status_code}",+ },+ }+ await websocket.send_text(json.dumps(error_event))+ return++ # Relay SSE events as WS text messages+ buffer = ""+ async for chunk in response.aiter_text():+ buffer += chunk+ while "\n" in buffer:+ line, buffer = buffer.split("\n", 1)+ line = line.strip()+ if not line:+ continue+ if line.startswith("data: "):+ data = line[6:]+ if data == "[DONE]":+ continue+ try:+ await websocket.send_text(data)+ except Exception:+ return+ elif line.startswith("event: "):+ # SSE event type — skip, the data line contains the type+ continue++ # Flush any remaining data in buffer+ for line in buffer.strip().splitlines():+ line = line.strip()+ if line.startswith("data: ") and line[6:] != "[DONE]":+ with contextlib.suppress(Exception):+ await websocket.send_text(line[6:])++ except Exception as http_err:+ logger.error(f"[{request_id}] WS→HTTP fallback failed: {http_err}")+ error_event = {+ "type": "error",+ "error": {+ "type": "server_error",+ "message": f"HTTP fallback failed: {http_err!s}"[:200],+ },+ }+ with contextlib.suppress(Exception):+ await websocket.send_text(json.dumps(error_event))+ finally:+ with contextlib.suppress(Exception):+ await websocket.close()+async def handle_compress(self, request: Request) -> JSONResponse:"""Compress messages without calling an LLM.diff --git a/tests/test_proxy_scalability.py b/tests/test_proxy_scalability.pyindex 6498fb5bf..9cb103509 100644--- a/tests/test_proxy_scalability.py+++ b/tests/test_proxy_scalability.py@@ -43,23 +43,23 @@ class TestConnectionPoolConfig:assert timeout.write == 300.0assert timeout.pool == 10.0- @pytest.mark.asyncio- async def test_async_client_with_limits(self):+ def test_async_client_with_limits(self):"""Test AsyncClient accepts connection pool limits."""- limits = httpx.Limits(- max_connections=500,- max_keepalive_connections=100,- )- async with httpx.AsyncClient(- limits=limits,- timeout=httpx.Timeout(10.0),- ) as client:- # Verify client was created successfully with our limits- # (httpx doesn't expose limits directly, but creation succeeds)- assert client is not None- # The limits object we passed should have our values- assert limits.max_connections == 500- assert limits.max_keepalive_connections == 100++ async def _run():+ limits = httpx.Limits(+ max_connections=500,+ max_keepalive_connections=100,+ )+ async with httpx.AsyncClient(+ limits=limits,+ timeout=httpx.Timeout(10.0),+ ) as client:+ assert client is not None+ assert limits.max_connections == 500+ assert limits.max_keepalive_connections == 100++ asyncio.run(_run())class TestHTTP2Config:@@ -72,29 +72,28 @@ class TestHTTP2Config:h2_available = importlib.util.find_spec("h2") is not Noneif h2_available:- # Should work if h2 is installedclient = httpx.Client(http2=True)assert client._base_url is not Noneclient.close()else:- # Should raise if h2 not installedwith pytest.raises(ImportError):httpx.Client(http2=True)- @pytest.mark.asyncio- async def test_async_client_http2(self):+ def test_async_client_http2(self):"""Test AsyncClient with HTTP/2 enabled."""import importlib.utilif not importlib.util.find_spec("h2"):pytest.skip("h2 package not installed")- async with httpx.AsyncClient(- http2=True,- limits=httpx.Limits(max_connections=100),- ) as client:- # Client should be configured for HTTP/2- assert client is not None+ async def _run():+ async with httpx.AsyncClient(+ http2=True,+ limits=httpx.Limits(max_connections=100),+ ) as client:+ assert client is not None++ asyncio.run(_run())class TestProxyConfigDataclass:@@ -144,79 +143,81 @@ class TestProxyConfigDataclass:class TestConcurrencyPatterns:"""Test async concurrency patterns used in proxy."""- @pytest.mark.asyncio- async def test_semaphore_for_backpressure(self):+ def test_semaphore_for_backpressure(self):"""Test semaphore pattern for limiting concurrent requests."""- semaphore = asyncio.Semaphore(3)- active = []- completed = []-- async def task(task_id: int):- async with semaphore:- active.append(task_id)- # Verify we never exceed semaphore limit- assert len(active) <= 3- await asyncio.sleep(0.01)- active.remove(task_id)- completed.append(task_id)-- # Run 10 tasks with max 3 concurrent- tasks = [task(i) for i in range(10)]- await asyncio.gather(*tasks)-- assert len(completed) == 10-- @pytest.mark.asyncio- async def test_connection_reuse_pattern(self):++ async def _run():+ semaphore = asyncio.Semaphore(3)+ active = []+ completed = []++ async def task(task_id: int):+ async with semaphore:+ active.append(task_id)+ assert len(active) <= 3+ await asyncio.sleep(0.01)+ active.remove(task_id)+ completed.append(task_id)++ tasks = [task(i) for i in range(10)]+ await asyncio.gather(*tasks)+ assert len(completed) == 10++ asyncio.run(_run())++ def test_connection_reuse_pattern(self):"""Test that single client instance is reused (not recreated)."""- clients_created = []- class MockProxyWithClient:- def __init__(self):- self.http_client = None+ async def _run():+ clients_created = []- async def startup(self):- self.http_client = httpx.AsyncClient(- limits=httpx.Limits(max_connections=100),- )- clients_created.append(self.http_client)+ class MockProxyWithClient:+ def __init__(self):+ self.http_client = None- async def shutdown(self):- if self.http_client:- await self.http_client.aclose()+ async def startup(self):+ self.http_client = httpx.AsyncClient(+ limits=httpx.Limits(max_connections=100),+ )+ clients_created.append(self.http_client)- async def make_request(self, url: str):- # Should reuse the same client, not create new one- return self.http_client+ async def shutdown(self):+ if self.http_client:+ await self.http_client.aclose()- proxy = MockProxyWithClient()- await proxy.startup()+ async def make_request(self, url: str):+ return self.http_client- # Multiple requests should return same client instance- client1 = await proxy.make_request("http://example1.com")- client2 = await proxy.make_request("http://example2.com")- client3 = await proxy.make_request("http://example3.com")+ proxy = MockProxyWithClient()+ await proxy.startup()- assert client1 is client2 is client3- assert len(clients_created) == 1 # Only one client created+ client1 = await proxy.make_request("http://example1.com")+ client2 = await proxy.make_request("http://example2.com")+ client3 = await proxy.make_request("http://example3.com")- await proxy.shutdown()+ assert client1 is client2 is client3+ assert len(clients_created) == 1++ await proxy.shutdown()++ asyncio.run(_run())class TestTimeoutOverrides:"""Test per-request timeout overrides."""- @pytest.mark.asyncio- async def test_request_level_timeout_override(self):+ def test_request_level_timeout_override(self):"""Test that timeout can be overridden per-request."""- async with httpx.AsyncClient(- timeout=httpx.Timeout(10.0), # Default timeout- ):- # Per-request override should work- override_timeout = httpx.Timeout(120.0)- # Just verify the timeout object is valid- assert override_timeout.read == 120.0- assert override_timeout.connect == 120.0++ async def _run():+ async with httpx.AsyncClient(+ timeout=httpx.Timeout(10.0),+ ):+ override_timeout = httpx.Timeout(120.0)+ assert override_timeout.read == 120.0+ assert override_timeout.connect == 120.0++ asyncio.run(_run())class TestWorkerConfiguration:@@ -226,7 +227,6 @@ class TestWorkerConfiguration:"""Test that uvicorn accepts workers parameter."""uvicorn = pytest.importorskip("uvicorn")- # Verify the Config class accepts workersconfig = uvicorn.Config(app="app:app",workers=4,@@ -240,5 +240,4 @@ class TestWorkerConfiguration:uvicorn = pytest.importorskip("uvicorn")config = uvicorn.Config(app="app:app")- # Default should be None (single process)assert config.workers is None or config.workers == 1diff --git a/tests/test_ws_http_fallback.py b/tests/test_ws_http_fallback.pynew file mode 100644index 000000000..47e40b800--- /dev/null+++ b/tests/test_ws_http_fallback.py@@ -0,0 +1,224 @@+"""Tests for WebSocket HTTP fallback in the OpenAI handler.++When the upstream WebSocket connection to OpenAI fails (HTTP 500),+the proxy should transparently fall back to HTTP POST streaming+and relay SSE events over the client WebSocket.+"""++from __future__ import annotations++import asyncio+import json+++class FakeWebSocket:+ """Minimal WebSocket mock for testing."""++ def __init__(self):+ self.sent_texts: list[str] = []+ self.closed = False++ async def send_text(self, data: str) -> None:+ self.sent_texts.append(data)++ async def close(self, code: int = 1000, reason: str = "") -> None:+ self.closed = True+++class FakeStreamResponse:+ """Mock httpx streaming response."""++ def __init__(self, status_code: int = 200, sse_events: list[str] | None = None):+ self.status_code = status_code+ self._events = sse_events or []++ async def aiter_text(self):+ for event in self._events:+ yield event++ async def aiter_bytes(self):+ yield b"error body"++ async def __aenter__(self):+ return self++ async def __aexit__(self, *args):+ pass+++class FakeHttpClient:+ """Mock httpx.AsyncClient with stream support."""++ def __init__(self, response: FakeStreamResponse):+ self._response = response++ def stream(self, method, url, **kwargs):+ return self._response+++def _make_handler():+ """Create a minimal OpenAIHandlerMixin-like object."""+ from headroom.proxy.handlers.openai import OpenAIHandlerMixin++ obj = object.__new__(OpenAIHandlerMixin)+ obj.OPENAI_API_URL = "https://api.openai.com"+ obj.http_client = None+ return obj+++class TestWsHttpFallback:+ def test_fallback_relays_sse_events(self):+ """HTTP fallback should relay SSE data lines as WS text messages."""+ handler = _make_handler()+ ws = FakeWebSocket()+ sse_lines = [+ 'event: response.created\ndata: {"type":"response.created","response":{"id":"r1"}}\n\n',+ 'event: response.output_item.added\ndata: {"type":"response.output_item.added"}\n\n',+ 'event: response.completed\ndata: {"type":"response.completed"}\n\n',+ "data: [DONE]\n\n",+ ]+ response = FakeStreamResponse(200, sse_lines)+ handler.http_client = FakeHttpClient(response)++ body = {"model": "gpt-5.4", "input": "hi"}+ first_msg_raw = json.dumps({"type": "response.create", "response": body})++ asyncio.run(+ handler._ws_http_fallback(+ ws, body, first_msg_raw, {"Authorization": "Bearer test"}, "req_1"+ )+ )++ assert len(ws.sent_texts) == 3 # 3 data events, [DONE] skipped+ assert '"response.created"' in ws.sent_texts[0]+ assert '"response.output_item.added"' in ws.sent_texts[1]+ assert '"response.completed"' in ws.sent_texts[2]+ assert ws.closed++ def test_fallback_sends_error_on_non_200(self):+ """HTTP fallback should send error event on non-200 response."""+ handler = _make_handler()+ ws = FakeWebSocket()+ response = FakeStreamResponse(status_code=401)+ handler.http_client = FakeHttpClient(response)++ body = {"model": "gpt-5.4", "input": "hi"}+ asyncio.run(+ handler._ws_http_fallback(+ ws, body, json.dumps(body), {"Authorization": "Bearer bad"}, "req_2"+ )+ )++ assert len(ws.sent_texts) == 1+ event = json.loads(ws.sent_texts[0])+ assert event["type"] == "error"+ assert "401" in event["error"]["message"]++ def test_fallback_sets_stream_true(self):+ """HTTP fallback should force stream=True in request body."""+ handler = _make_handler()+ ws = FakeWebSocket()+ captured_kwargs: dict = {}++ class CapturingClient:+ def stream(self, method, url, **kwargs):+ captured_kwargs.update(kwargs)+ return FakeStreamResponse(200, ["data: [DONE]\n\n"])++ handler.http_client = CapturingClient()++ body = {"model": "gpt-5.4", "input": "test", "stream": False}+ asyncio.run(handler._ws_http_fallback(ws, body, json.dumps(body), {}, "req_3"))++ assert captured_kwargs["json"]["stream"] is True++ def test_fallback_unwraps_response_create_envelope(self):+ """HTTP fallback should unwrap WS response.create wrapper for HTTP POST."""+ handler = _make_handler()+ ws = FakeWebSocket()+ captured_kwargs: dict = {}++ class CapturingClient:+ def stream(self, method, url, **kwargs):+ captured_kwargs.update(kwargs)+ return FakeStreamResponse(200, ["data: [DONE]\n\n"])++ handler.http_client = CapturingClient()++ # WS sends wrapped format: {"type": "response.create", "response": {...}}+ inner = {+ "model": "gpt-5.4",+ "input": [{"role": "user", "content": [{"type": "input_text", "text": "hi"}]}],+ }+ ws_msg = {"type": "response.create", "response": inner}+ asyncio.run(handler._ws_http_fallback(ws, ws_msg, json.dumps(ws_msg), {}, "req_unwrap"))++ posted = captured_kwargs["json"]+ # Should be the inner response, not the wrapper+ assert "type" not in posted # no "response.create" type field+ assert posted["model"] == "gpt-5.4"+ assert posted["stream"] is True+ assert "input" in posted++ def test_fallback_handles_http_exception(self):+ """HTTP fallback should send error event when HTTP request fails."""+ handler = _make_handler()+ ws = FakeWebSocket()++ class FailingClient:+ def stream(self, method, url, **kwargs):+ raise ConnectionError("upstream unreachable")++ handler.http_client = FailingClient()++ body = {"model": "gpt-5.4", "input": "test"}+ asyncio.run(handler._ws_http_fallback(ws, body, json.dumps(body), {}, "req_4"))++ assert len(ws.sent_texts) == 1+ event = json.loads(ws.sent_texts[0])+ assert event["type"] == "error"+ assert "unreachable" in event["error"]["message"]++ def test_fallback_routes_chatgpt_auth_to_chatgpt_domain(self):+ """ChatGPT session auth should route to chatgpt.com, not api.openai.com."""+ handler = _make_handler()+ ws = FakeWebSocket()+ captured_url = {}++ class CapturingClient:+ def stream(self, method, url, **kwargs):+ captured_url["url"] = url+ return FakeStreamResponse(200, ["data: [DONE]\n\n"])++ handler.http_client = CapturingClient()++ body = {"model": "gpt-5.4", "input": "test"}+ # ChatGPT session auth includes this header+ headers = {+ "Authorization": "Bearer chatgpt-session-token",+ "ChatGPT-Account-ID": "acct_abc123",+ }+ asyncio.run(handler._ws_http_fallback(ws, body, json.dumps(body), headers, "req_5"))++ assert "chatgpt.com" in captured_url["url"]+ assert "api.openai.com" not in captured_url["url"]++ def test_fallback_routes_api_key_to_openai(self):+ """API key auth should route to api.openai.com."""+ handler = _make_handler()+ ws = FakeWebSocket()+ captured_url = {}++ class CapturingClient:+ def stream(self, method, url, **kwargs):+ captured_url["url"] = url+ return FakeStreamResponse(200, ["data: [DONE]\n\n"])++ handler.http_client = CapturingClient()++ body = {"model": "gpt-5.4", "input": "test"}+ # API key auth — no ChatGPT-Account-ID header+ headers = {"Authorization": "Bearer sk-abc123"}+ asyncio.run(handler._ws_http_fallback(ws, body, json.dumps(body), headers, "req_6"))++ assert "api.openai.com" in captured_url["url"]