mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(proxy): keep tool deferral on Copilot Business/Enterprise/Individual hosts (#4608)
## Description #4356 stopped stripping `defer_loading` for `api.githubcopilot.com`, but since #4155 Business, Enterprise and Individual seats go to their plan host (`api.<plan>.githubcopilot.com`), which the exact-host check misses. VS Code Copilot Chat defers 80 of its 104 tools itself, so on those seats the strip inlined all of them on every Claude turn. Measured live against `api.business.githubcopilot.com`: a 40-tool request bills 12,524 input tokens eager vs 656 deferred, and the model still searches for and calls a deferred tool. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - Match any public `*.githubcopilot.com` API host through the existing `is_public_copilot_api_host`, except the `proxy.*` completions hosts. Lookalike hosts still downgrade. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] New tests added for new functionality - [x] Manual testing performed (live request above) ### Test Output ``` tests/test_tool_search_client_deferral.py: 74 passed ``` ## Real Behavior Proof - Environment: a Copilot Business seat, direct requests to `api.business.githubcopilot.com`. - Exact command / steps: the same 40-tool Claude request sent once with every tool eager, and once with `defer_loading` set. Then a prompt that needs a deferred tool. - Observed result: 12,524 input tokens eager, 656 deferred. The model searched for the deferred tool and called it. - Not tested: live requests to the Enterprise and Individual hosts (same match, unit tests only). This measurement predates the `proxy.*` exclusion, which has unit tests only. The `proxy.*` hosts have never been measured, which is why they now downgrade. ## Runtime Rollout Safety - Rollout-managed feature(s): none - Minimum rollout channel: n/a - Stable/default behavior changed: Business, Enterprise and Individual `api.*` Copilot hosts keep tool deferral; `proxy.*` hosts still downgrade - Kill switch / disable path: `HEADROOM_TOOL_SEARCH=0` turns off built-in tool search - Unsafe override required: no - Qualification impact: none known - Rollback path: revert this PR 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
74277d47ec
commit
be456d4e3f
@@ -157,7 +157,7 @@ def _github_host() -> str:
|
||||
configured_url = os.environ.get("GITHUB_COPILOT_API_URL", "").strip()
|
||||
if configured_url:
|
||||
hostname = _configured_url_hostname(configured_url)
|
||||
if _is_public_copilot_api_host(hostname):
|
||||
if is_public_copilot_api_host(hostname):
|
||||
return DEFAULT_GITHUB_HOST
|
||||
for prefix in ("copilot-api.", "api."):
|
||||
if hostname.startswith(prefix):
|
||||
@@ -231,7 +231,7 @@ def _copilot_subdomain_enterprise_host(enterprise_url: str) -> str | None:
|
||||
if (
|
||||
not host
|
||||
or host in {"github.com", "www.github.com", "api.github.com"}
|
||||
or _is_public_copilot_api_host(host)
|
||||
or is_public_copilot_api_host(host)
|
||||
):
|
||||
return None
|
||||
return host
|
||||
@@ -361,7 +361,7 @@ def copilot_nes_base_url(model: Any, upstream_base_url: str) -> str | None:
|
||||
|
||||
if not (isinstance(model, str) and model.startswith("copilot-nes-")):
|
||||
return None
|
||||
if not _is_public_copilot_api_host(_url_host(upstream_base_url)):
|
||||
if not is_public_copilot_api_host(_url_host(upstream_base_url)):
|
||||
return None
|
||||
if is_copilot_completions_host(upstream_base_url):
|
||||
return None
|
||||
@@ -401,7 +401,7 @@ def copilot_completions_base_url() -> str:
|
||||
if _observed_completions_base_url:
|
||||
return _observed_completions_base_url
|
||||
configured = _configured_api_url_override()
|
||||
if configured and not _is_public_copilot_api_host(_url_host(configured)):
|
||||
if configured and not is_public_copilot_api_host(_url_host(configured)):
|
||||
return configured
|
||||
return DEFAULT_COMPLETIONS_PROXY_URL
|
||||
|
||||
@@ -1318,7 +1318,7 @@ def is_copilot_api_url(url: str | None) -> bool:
|
||||
if configured_host and host == configured_host:
|
||||
return True
|
||||
hostname = (parsed.hostname or host.split("/", 1)[0]).lower()
|
||||
return _is_public_copilot_api_host(hostname) or _is_ghe_copilot_api_host(hostname)
|
||||
return is_public_copilot_api_host(hostname) or _is_ghe_copilot_api_host(hostname)
|
||||
|
||||
|
||||
def is_copilot_upstream_url(url: str | None) -> bool:
|
||||
@@ -1340,7 +1340,7 @@ def is_copilot_upstream_url(url: str | None) -> bool:
|
||||
return is_copilot_api_url(url) or is_copilot_completions_host(url)
|
||||
|
||||
|
||||
def _is_public_copilot_api_host(host: str) -> bool:
|
||||
def is_public_copilot_api_host(host: str) -> bool:
|
||||
"""Return True for GitHub-hosted Copilot API domains."""
|
||||
|
||||
return host == "githubcopilot.com" or host.endswith(".githubcopilot.com")
|
||||
|
||||
@@ -4119,20 +4119,29 @@ def _warn_deferred_without_search_tool(tools: Any) -> None:
|
||||
)
|
||||
|
||||
|
||||
#: Non-Anthropic upstreams that accept the first-party deferred tool shape.
|
||||
#: These bill deferred schemas compactly, so downgrading deferral for them
|
||||
#: inlines the whole catalog and multiplies billed prompt tokens.
|
||||
_DEFERRAL_CAPABLE_HOSTS: frozenset[str] = frozenset({"api.githubcopilot.com"})
|
||||
|
||||
|
||||
def anthropic_first_party_tool_search_supported(api_base_url: str | None) -> bool:
|
||||
"""Return whether Anthropic server-side tool search is valid for this upstream."""
|
||||
"""Return whether Anthropic server-side tool search is valid for this upstream.
|
||||
|
||||
GitHub's Copilot API also accepts the first-party deferred shape and bills
|
||||
deferred schemas compactly, so downgrading deferral there inlines the whole
|
||||
catalog and multiplies billed prompt tokens (#4356). That holds for the
|
||||
generic host and for the plan hosts Business/Enterprise/Individual seats
|
||||
are routed to (``api.<plan>.githubcopilot.com``, #4155), so any public
|
||||
``*.githubcopilot.com`` host qualifies. GHE data-residency hosts
|
||||
(``copilot-api.<tenant>.ghe.com``) still downgrade: nobody has verified
|
||||
that they accept the shape.
|
||||
"""
|
||||
from headroom.copilot_auth import is_copilot_completions_host, is_public_copilot_api_host
|
||||
from headroom.providers.claude.runtime import (
|
||||
_gate_view_host,
|
||||
is_custom_anthropic_base_url,
|
||||
)
|
||||
|
||||
if _gate_view_host((api_base_url or "").strip()) in _DEFERRAL_CAPABLE_HOSTS:
|
||||
# The ``proxy.*`` hosts are the inline-completions surface: nobody has measured
|
||||
# the deferred shape there.
|
||||
if is_public_copilot_api_host(
|
||||
_gate_view_host((api_base_url or "").strip())
|
||||
) and not is_copilot_completions_host(api_base_url):
|
||||
return True
|
||||
|
||||
return not is_custom_anthropic_base_url(api_base_url)
|
||||
|
||||
@@ -411,21 +411,35 @@ def test_nothing_to_strip_returns_the_same_list() -> None:
|
||||
assert out is tools
|
||||
|
||||
|
||||
def test_github_copilot_upstream_keeps_deferral() -> None:
|
||||
"""Copilot accepts the deferred shape and bills deferred schemas compactly."""
|
||||
@pytest.mark.parametrize(
|
||||
"url",
|
||||
[
|
||||
"https://api.githubcopilot.com",
|
||||
"https://api.business.githubcopilot.com",
|
||||
"https://api.enterprise.githubcopilot.com",
|
||||
"https://api.individual.githubcopilot.com/v1",
|
||||
"https://api.business.githubcopilot.com:443",
|
||||
],
|
||||
)
|
||||
def test_copilot_hosts_keep_deferral(url: str) -> None:
|
||||
"""Copilot accepts the deferred shape and bills deferred schemas compactly,
|
||||
on the generic host and on the plan hosts seats are sent to (#4155)."""
|
||||
tools = [ANTHROPIC_SEARCH_REGEX, {"name": "mcp__srv__a", "defer_loading": True}]
|
||||
|
||||
out = strip_first_party_tool_search_tools_for_third_party_upstream(
|
||||
tools, "https://api.githubcopilot.com"
|
||||
)
|
||||
|
||||
assert out is tools
|
||||
assert anthropic_first_party_tool_search_supported("https://api.githubcopilot.com")
|
||||
assert strip_first_party_tool_search_tools_for_third_party_upstream(tools, url) is tools
|
||||
|
||||
|
||||
def test_copilot_host_match_is_exact() -> None:
|
||||
"""A lookalike host must not inherit the exemption."""
|
||||
assert not anthropic_first_party_tool_search_supported("https://api.githubcopilot.com.evil.com")
|
||||
@pytest.mark.parametrize(
|
||||
"url",
|
||||
[
|
||||
"https://api.githubcopilot.com.evil.com",
|
||||
"https://api.business.githubcopilot.com.evil.com",
|
||||
"https://evilgithubcopilot.com",
|
||||
"https://copilot-api.acme.ghe.com",
|
||||
],
|
||||
)
|
||||
def test_copilot_lookalike_hosts_still_downgrade(url: str) -> None:
|
||||
assert not anthropic_first_party_tool_search_supported(url)
|
||||
|
||||
|
||||
def test_other_third_party_upstreams_still_downgrade() -> None:
|
||||
@@ -727,3 +741,12 @@ def test_the_orphan_warning_is_throttled(caplog: pytest.LogCaptureFixture) -> No
|
||||
|
||||
lines = [r for r in caplog.records if "tool_search_deferred_orphan" in r.getMessage()]
|
||||
assert len(lines) == 1
|
||||
|
||||
|
||||
def test_copilot_completions_hosts_do_not_keep_deferral() -> None:
|
||||
tools = [ANTHROPIC_SEARCH_REGEX, {"name": "mcp__srv__a", "defer_loading": True}]
|
||||
|
||||
out = strip_first_party_tool_search_tools_for_third_party_upstream(
|
||||
tools, "https://proxy.business.githubcopilot.com"
|
||||
)
|
||||
assert out is not tools
|
||||
|
||||
Reference in New Issue
Block a user