fix(proxy): keep Copilot requests on Copilot when an extension advises another provider

BackendResolver.for_request built a translating LiteLLM backend for any
route advice naming a provider that is not native to the handler, without
looking at where the proxy sends traffic. A proxy in front of GitHub
Copilot holds only a Copilot token, so a GPT-family call advised onto a
Claude model went to Anthropic directly and failed
(failed_by_provider: litellm-anthropic).

The resolver now takes the request's upstream and, when it is a Copilot
host, returns the default backend and leaves the body's model alone. The
chat-stream CCR eligibility check asks the resolver instead of looking at
the raw advice, so ignored advice no longer disables CCR tool injection.

Assay-Kind: bugfix
Assay-Ask: Copilot requests fail with litellm-anthropic errors when a routing extension publishes cross-provider advice.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-11-16 00:43:04 -08:00
co-authored by Claude Fable 5.2
parent 5023c84e52
commit fcfa965bf3
4 changed files with 112 additions and 10 deletions
+14 -2
View File
@@ -1646,7 +1646,13 @@ class AnthropicHandlerMixin:
request, headers, routed_to_omlx=routed_to_omlx
)
backend = (
None if routed_to_omlx else resolver_for(self).for_request(request, body=body)
None
if routed_to_omlx
else resolver_for(self).for_request(
request,
body=body,
upstream_url=upstream_base_url or self.ANTHROPIC_API_URL,
)
)
if backend is not None:
if model_override and not body.get("model"):
@@ -4255,7 +4261,13 @@ class AnthropicHandlerMixin:
from headroom.proxy.route_advice import resolver_for
request_backend = (
None if force_direct else resolver_for(self).for_request(request, body=body)
None
if force_direct
else resolver_for(self).for_request(
request,
body=body,
upstream_url=upstream_base_url or self.ANTHROPIC_API_URL,
)
)
if request_backend is not None:
# Route through Bedrock backend
+13 -8
View File
@@ -3784,7 +3784,10 @@ class OpenAIHandlerMixin:
COMPRESSION_BODY_MAX_BYTES,
)
backend = resolver_for(self).for_request(
request, body=body, native_providers=("openai",)
request,
body=body,
native_providers=("openai",),
upstream_url=upstream_base_url,
)
passthrough_model = body.get("model", "unknown")
if not isinstance(passthrough_model, str):
@@ -4572,7 +4575,7 @@ class OpenAIHandlerMixin:
# Decided before injection: a turn that can buffer can answer the tool,
# so it may carry it even though the client asked for a stream. Whether
# it actually buffers is settled at dispatch, from the final body.
from headroom.proxy.route_advice import advice_from
from headroom.proxy.route_advice import resolver_for
_ccr_handler = getattr(self, "ccr_response_handler", None)
chat_stream_ccr_eligible = _is_openai_chat_stream_ccr_eligible(
@@ -4582,10 +4585,12 @@ class OpenAIHandlerMixin:
_ccr_handler and getattr(_ccr_handler.config, "enabled", True)
),
ccr_inject_tool=self.config.ccr_inject_tool,
routed_to_backend=(
getattr(self, "anthropic_backend", None) is not None
or advice_from(request) is not None
),
# Ask the resolver, not the advice: advice it ignores (a Copilot
# upstream) leaves this request on the direct path.
routed_to_backend=resolver_for(self).for_request(
request, native_providers=("openai",), upstream_url=upstream_base_url
)
is not None,
upstream_base_url=upstream_base_url,
)
can_inject_ccr_tool = _should_inject_openai_chat_ccr_tool(
@@ -5162,7 +5167,7 @@ class OpenAIHandlerMixin:
_normalize_openai_max_tokens(
body,
backend_owns_translation=resolver_for(self).for_request(
request, native_providers=("openai",)
request, native_providers=("openai",), upstream_url=upstream_base_url
)
is not None,
)
@@ -5233,7 +5238,7 @@ class OpenAIHandlerMixin:
# litellm backend; same-family (openai) advice still resolves to
# `self.anthropic_backend`, unchanged.
request_backend = resolver_for(self).for_request(
request, body=body, native_providers=("openai",)
request, body=body, native_providers=("openai",), upstream_url=upstream_base_url
)
if request_backend is not None:
try:
+16
View File
@@ -34,6 +34,7 @@ import logging
from dataclasses import dataclass
from typing import Any
from headroom.copilot_auth import is_copilot_upstream_url
from headroom.offline import pin_litellm_cost_map_local
log = logging.getLogger(__name__)
@@ -110,6 +111,7 @@ class BackendResolver:
*,
body: dict | None = None,
native_providers: tuple[str, ...] = _NATIVE,
upstream_url: str | None = None,
) -> Any:
"""The backend to serve this request. Falls back to the default.
@@ -122,6 +124,12 @@ class BackendResolver:
short-circuited to the default backend. One proxy instance serves both
wires from a single shared resolver, so native-ness cannot live on the
resolver; it must be supplied per dispatch site.
``upstream_url`` is where the caller sends this request when no backend
is used. When it is GitHub Copilot, cross-provider advice is ignored and
the body is left alone: Copilot serves every model family on the one
upstream, and the proxy holds a Copilot token, not a key for the
provider a translating backend would call.
"""
advice = advice_from(request)
if advice is None:
@@ -133,6 +141,14 @@ class BackendResolver:
# rewrite is enough and the extension has already done it. Nothing
# to switch.
return self._default
if is_copilot_upstream_url(upstream_url):
if body is not None:
log.info(
"route advice ignored: %s via %s; the upstream is GitHub Copilot",
advice.model,
provider,
)
return self._default
if provider in self._failed:
return self._default
@@ -12,7 +12,13 @@ from __future__ import annotations
from types import SimpleNamespace
import httpx
import pytest
from fastapi.testclient import TestClient
from headroom import copilot_auth
from headroom.proxy.route_advice import BackendResolver, RouteAdvice
from headroom.proxy.server import ProxyConfig, create_app
class _Sentinel:
@@ -79,3 +85,66 @@ def test_failed_provider_falls_back_to_default() -> None:
assert r1 == "DEFAULT_BACKEND"
r2 = resolver.for_request(_request("claude-opus-5", "anthropic"), native_providers=("openai",))
assert r2 == "DEFAULT_BACKEND"
def test_copilot_upstream_ignores_cross_provider_advice() -> None:
# A Copilot proxy holds only a Copilot token, so advice naming another
# provider must not move the request or rewrite its model. A GHE host
# counts as Copilot too.
resolver = _resolver()
body: dict = {"model": "gpt-5-mini"}
result = resolver.for_request(
_request("claude-opus-5", "anthropic"),
body=body,
native_providers=("openai",),
upstream_url="https://copilot-api.acme.ghe.com",
)
assert result == "DEFAULT_BACKEND"
assert body["model"] == "gpt-5-mini"
def test_copilot_chat_stays_on_copilot_despite_cross_provider_advice(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# A proxy in front of GitHub Copilot holds a Copilot token only, and Copilot
# serves every model family on its own /chat/completions. Advice naming
# another provider used to send the request to Anthropic through a
# translating backend, where it failed for want of a key.
class _Provider:
async def get_api_token(self, *, integration_id=None): # noqa: ANN001, ANN202
return SimpleNamespace(token="test-copilot-token")
monkeypatch.setattr(copilot_auth, "get_copilot_token_provider", lambda: _Provider())
built: list[str] = []
monkeypatch.setattr(BackendResolver, "_build", lambda self, provider: built.append(provider))
sent: list[tuple[str, str]] = []
async def fake_retry(method, url, headers, body, *args, **kwargs): # noqa: ANN001, ANN002, ANN003, ANN202
sent.append((url, body["model"]))
return httpx.Response(200, json={"choices": [], "usage": {}})
app = create_app(
ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
openai_api_url="https://api.business.githubcopilot.com",
)
)
@app.middleware("http")
async def publish_advice(request, call_next): # noqa: ANN001, ANN202
request.state.headroom_route = RouteAdvice(model="claude-sonnet-5-5", provider="anthropic")
return await call_next(request)
with TestClient(app) as client:
client.app.state.proxy._retry_request = fake_retry
response = client.post(
"/v1/chat/completions",
json={"model": "gpt-5-mini", "messages": [{"role": "user", "content": "hi"}]},
headers={"Authorization": "Bearer test-key"},
)
assert response.status_code == 200, response.text
assert built == [], "no translating backend may be built for a Copilot upstream"
assert sent == [("https://api.business.githubcopilot.com/chat/completions", "gpt-5-mini")]