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(copilot-auth): stop discarding the caller's valid Copilot auth token (#1879)
yehsuff52ca19db1c2
diff --git a/headroom/copilot_auth.py b/headroom/copilot_auth.pyindex 67654f5cd..aa1d0edd9 100644--- a/headroom/copilot_auth.py+++ b/headroom/copilot_auth.py@@ -1077,7 +1077,13 @@ def _is_copilot_api_token(token: str) -> bool:Copilot API tokens currently use the "tid_" prefix.GitHub OAuth tokens (for example "gho_", "ghs_", "ghp_", "github_pat_")- should be exchanged and must not be forwarded directly.+ should be exchanged and must not be forwarded directly to the Copilot+ *subscription/user-info* APIs (see resolve_subscription_bearer_token_details()),+ which is the only caller of this helper. It intentionally stays narrow:+ broadening it here would also change subscription-resolution behavior,+ which is a separate concern from forwarding a bearer token for+ chat-completion/inference requests -- see _is_forwardable_copilot_bearer_token()+ for that case."""normalized = token.strip()if not normalized:@@ -1094,6 +1100,39 @@ def _is_copilot_api_token(token: str) -> bool:return normalized.startswith("tid_")+def _is_forwardable_copilot_bearer_token(token: str) -> bool:+ """Return True when a bearer token should be forwarded as-is for Copilot inference.++ Unlike _is_copilot_api_token() (used only for subscription/user-info+ resolution), this accepts both short-lived Copilot API tokens (`tid_`)+ AND GitHub OAuth tokens (`gho_`, `ghs_`, `ghp_`, `github_pat_`) as valid,+ forwardable Copilot bearer credentials for chat-completion/inference+ requests.++ Verified live in two independent reports that a caller-supplied `gho_`+ token is already valid and correctly entitled when sent directly to+ the real Copilot inference API, and that replacing it with Headroom's+ own independently-fetched/exchanged token is actively harmful:++ - A live Copilot CLI session's own `gho_`-prefixed token worked+ end-to-end for model "claude-sonnet-5" when forwarded unchanged, but+ got `400 model_not_supported` once Headroom swapped in a different,+ less-entitled re-exchanged token for the exact same request.+ - headroomlabs-ai/headroom#1813: OpenCode's native Copilot integration+ sends its own `gho_` token directly; replacing it changes the+ effective client/integrator lane Copilot's backend sees, breaking+ model discovery/inference parity with native (non-proxied) behavior.++ Only genuinely non-Copilot-shaped or blank/whitespace-only tokens fall+ through to replacement.+ """+ normalized = token.strip()+ if not normalized:+ return False++ return normalized.startswith(("tid_", "gho_", "ghs_", "ghp_", "github_pat_"))++def _token_kind(token: str) -> str:"""Return a non-sensitive label for the token type, safe to log."""t = token.strip()@@ -1115,7 +1154,11 @@ async def apply_copilot_api_auth(headers: dict[str, str], *, url: str) -> dict[sincoming_auth = next((v for k, v in resolved.items() if k.lower() == "authorization"), None)if incoming_auth:scheme, _, raw_token = incoming_auth.partition(" ")- if scheme.lower() == "bearer" and raw_token and _is_copilot_api_token(raw_token):+ if (+ scheme.lower() == "bearer"+ and raw_token+ and _is_forwardable_copilot_bearer_token(raw_token)+ ):logger.info("apply_copilot_api_auth: passing through client token kind=%s",_token_kind(raw_token),diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 2823de0b9..8b8d3c328 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -4696,7 +4696,6 @@ class OpenAIHandlerMixin:for key, value in upstream_headers.items()if key.lower() != _CODEX_RESPONSES_LITE_HEADER}- _lower_headers = {k.lower(): v for k, v in upstream_headers.items()}# Build upstream WebSocket URL based on auth modeif is_chatgpt_auth:@@ -4711,6 +4710,12 @@ class OpenAIHandlerMixin:ws_base = base.replace("https://", "wss://").replace("http://", "ws://")upstream_url = build_copilot_upstream_url(ws_base, "/v1/responses")+ # Resolve Copilot auth for this upstream FIRST, before any generic+ # OpenAI-key fallback below -- ensures a real client-supplied Copilot+ # credential is used/preserved, and Headroom's own credential fetch+ # only kicks in when the client truly sent none.+ upstream_headers = await apply_copilot_api_auth(upstream_headers, url=upstream_url)+capture_codex_wire_debug("ws_upstream_handshake",request_id=request_id,@@ -4735,7 +4740,10 @@ class OpenAIHandlerMixin:# 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.- if "authorization" not in _lower_headers:+ # Resolved AFTER apply_copilot_api_auth (moved earlier, see below) so a+ # real client-supplied Copilot credential is never mistaken for, or+ # clobbered by, this unrelated OpenAI-key fallback.+ if not any(k.lower() == "authorization" for k in upstream_headers):api_key = os.environ.get("OPENAI_API_KEY")if api_key:upstream_headers["Authorization"] = f"Bearer {api_key}"@@ -4746,8 +4754,6 @@ class OpenAIHandlerMixin:f"OPENAI_API_KEY not set — upstream will likely reject")- upstream_headers = await apply_copilot_api_auth(upstream_headers, url=upstream_url)-# Ensure the required beta header is present — OpenAI returns 500 without it.# PR-A6 (P5-50): use the deterministic `merge_openai_beta` helper# so the auto-injected `responses_websockets=2026-02-06` isdiff --git a/tests/test_copilot_auth.py b/tests/test_copilot_auth.pyindex 4b22c9997..f766da4db 100644--- a/tests/test_copilot_auth.py+++ b/tests/test_copilot_auth.py@@ -677,31 +677,44 @@ def test_apply_copilot_api_auth_passes_through_existing_api_token(assert "x-api-key" not in headers-def test_apply_copilot_api_auth_replaces_github_oauth_bearer(+def test_apply_copilot_api_auth_passes_through_github_oauth_bearer(monkeypatch: pytest.MonkeyPatch,) -> None:- async def fake_get_api_token() -> copilot_auth.CopilotAPIToken:- return copilot_auth.CopilotAPIToken(- token="copilot-session",- expires_at=time.time() + 3600,- api_url=copilot_auth.DEFAULT_API_URL,+ """A caller-supplied GitHub OAuth (gho_/ghs_/ghp_/github_pat_) bearer+ token must be forwarded unchanged, not replaced.++ Regression test for headroomlabs-ai/headroom#1813: this function used+ to treat any gho_-prefixed token as "not a suitable Copilot API+ token" and silently replace it with Headroom's own independently+ fetched/exchanged credential. That broke both:+ - a live Copilot CLI session (its own gho_ token worked directly for+ model "claude-sonnet-5", but got 400 model_not_supported once+ Headroom substituted a differently-entitled token), and+ - OpenCode's native GitHub Copilot integration (#1813): replacing its+ gho_ token changed the effective client/integrator lane Copilot's+ backend sees, breaking model discovery/inference parity with+ native (non-proxied) behavior.+ """++ async def fail_if_called() -> copilot_auth.CopilotAPIToken:+ raise AssertionError(+ "get_api_token() must not be called for a forwardable gho_ bearer token")monkeypatch.setattr(copilot_auth.get_copilot_token_provider(),"get_api_token",- fake_get_api_token,+ fail_if_called,)headers = asyncio.run(copilot_auth.apply_copilot_api_auth(- {"authorization": "Bearer gho_downstream_oauth"},+ {"authorization": "Bearer gho_liveClientToken123"},url="https://api.githubcopilot.com/v1/chat/completions",))- assert headers["Authorization"] == "Bearer copilot-session"- assert "authorization" not in headers+ assert headers["authorization"] == "Bearer gho_liveClientToken123"def test_apply_copilot_api_auth_replaces_non_bearer_auth(@@ -740,6 +753,25 @@ def test_is_copilot_api_token_matches_expected_prefixes() -> None:assert copilot_auth._is_copilot_api_token("Bearer maybe") is False+def test_is_forwardable_copilot_bearer_token_matches_expected_prefixes() -> None:+ """The inference-path helper (unlike _is_copilot_api_token, which is+ scoped only to subscription/user-info resolution) accepts BOTH+ short-lived Copilot API tokens (tid_) AND GitHub OAuth tokens+ (gho_/ghs_/ghp_/github_pat_) as forwardable -- see+ _is_forwardable_copilot_bearer_token()'s docstring and+ headroomlabs-ai/headroom#1813 for why GitHub OAuth tokens must be+ forwardable for chat-completion/inference requests.+ """+ assert copilot_auth._is_forwardable_copilot_bearer_token("tid_session_token") is True+ assert copilot_auth._is_forwardable_copilot_bearer_token("gho_oauth") is True+ assert copilot_auth._is_forwardable_copilot_bearer_token("ghs_oauth") is True+ assert copilot_auth._is_forwardable_copilot_bearer_token("ghp_oauth") is True+ assert copilot_auth._is_forwardable_copilot_bearer_token("github_pat_example") is True+ assert copilot_auth._is_forwardable_copilot_bearer_token("sk-unrelated-anthropic-key") is False+ assert copilot_auth._is_forwardable_copilot_bearer_token("") is False+ assert copilot_auth._is_forwardable_copilot_bearer_token(" ") is False++def test_apply_copilot_api_auth_injects_required_headers(monkeypatch: pytest.MonkeyPatch,) -> None: