mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(copilot): honour GitHub's advertised Business/Enterprise host by default (#4155)
## Description
Supersedes #3403 (parked, stacked on the conflicting #3402) as a
standalone change on `main`.
GitHub's token exchange advertises a per-plan chat host in
`endpoints.api`: `api.business.githubcopilot.com` for Business seats and
`api.enterprise.githubcopilot.com` for Enterprise seats, and GitHub's
own clients route with it. Since #2441 Headroom has folded both into the
generic `api.githubcopilot.com`, because the segmented hosts lagged on
newer Claude models at the time.
That fold breaks enterprises on GitHub's [subscription-based network
routing](https://docs.github.com/en/copilot/how-tos/administer-copilot/manage-for-enterprise/manage-access/manage-network-access):
their firewall allows only the plan host, the generic host is
unreachable, and the failure looks like a random connection error. It
also made the VS Code guide's Enterprise section wrong, which said
Headroom uses the advertised URL.
This PR honours the Business and Enterprise advertised hosts by default
and adds `GITHUB_COPILOT_USE_ADVERTISED_HOST=0` (also
`false`/`no`/`off`) to restore the generic-host fold for seats where the
segmented host still lags. An explicit `GITHUB_COPILOT_API_URL` or
enterprise domain still wins over both. The individual host keeps being
folded (#610 is unchanged).
Refs #2441, #610, #3352 (an Enterprise seat behind a corporate firewall;
the inline-completions 401 there is #4144).
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [x] Documentation update
## Changes Made
- `headroom/copilot_auth.py`:
`_subscription_api_url_from_user_info_payload` returns the advertised
host for the two plan hosts unless `GITHUB_COPILOT_USE_ADVERTISED_HOST`
opts out, and logs the chosen host with the opt-out next to it. That
line is emitted where the host is resolved: the `wrap` process for the
subscription lanes, which does not write to `proxy.log`. The dedicated
proxy's `/health` (`config.openai_api_url`) is where the chosen host
shows up. The generic and individual hosts still normalize to
`DEFAULT_API_URL`. `resolve_copilot_api_url`'s docstring no longer
claims the advertised host is ignored on purpose. Every subscription
lane (`wrap vscode`, `wrap copilot --subscription`, OpenCode
`--copilot-subscription`) already pins `resolution.api_url` into the
dedicated proxy as `--openai-api-url` and `GITHUB_COPILOT_API_URL`, so
they pick this up without changes; `is_copilot_api_url`,
`copilot_completions_base_url` and `_github_host` already treat
`*.githubcopilot.com` as a public Copilot host (verified by an
independent review of the diff).
- `headroom/cli/wrap.py`: `wrap copilot` help text describes the
per-plan routing for `--subscription`; the implicit OAuth lane still
uses the generic host (it performs no token exchange), which the text
now says.
- Docs: `docs/content/docs/vscode-copilot.mdx` Enterprise section is now
true and names the opt-out and the firewall case;
`docs/content/docs/corporate-networks.mdx` lists the plan hosts;
`TESTING-copilot-subscription.md` host section split by lane:
`--subscription` / `wrap vscode` / OpenCode honour the plan host, the
implicit OAuth `wrap copilot` lane is explicitly generic-host (it
performs no token exchange at launch, and `resolve_copilot_api_url` is
pinned network-free by an existing test), and its "open an issue for
auto-detection" paragraph removed.
- Tests: Business and Enterprise honoured; individual and generic still
folded; opt-out restores the fold; explicit pin wins; `wrap copilot
--subscription` pins the advertised host; the implicit OAuth lane stays
on the generic host even when user-info advertises the Enterprise host;
OpenCode subscription lane expectation updated. Three existing tests
that asserted the fold were updated to the new contract.
Follow-ups, not in this PR: `headroom doctor --network` still probes
only `api.githubcopilot.com` (it should probe the resolved plan host);
the implicit OAuth `wrap copilot` lane could resolve the plan host
through the token exchange too.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy`)
- [x] New tests added for new functionality
- [x] Manual testing performed (live Copilot Business seat, 2026-10-13;
see Live verification)
### Test Output
```text
$ pytest tests/test_copilot_auth.py tests/test_cli/test_wrap_copilot.py tests/test_cli/test_wrap_vscode.py \
tests/test_copilot_vscode_completions_routing.py tests/test_provider_copilot_vscode_config.py \
tests/test_cli/test_wrap_opencode.py tests/test_proxy_copilot_auth_hooks.py tests/test_provider_proxy_routes.py \
tests/test_copilot_integration_id_hmac.py tests/test_proxy/test_anthropic_copilot_upstream_auth.py \
tests/test_copilot_native_mode.py tests/test_copilot_subscription_smoke.py tests/test_cli/test_copilot_auth.py \
tests/test_cli/test_wrap_persistent.py
449 passed, 4 deselected
# the 4 deselected tests (test_wrap_persistent.py::*recover*) fail identically on the base commit 976aa714
# with AttributeError: 'TaskManifest' object has no attribute 'runtime_kind'; unrelated to this change
$ uvx ruff@0.16.8 check . && uvx ruff@0.16.8 format --check .
All checks passed! / 1777 files already formatted
$ mypy headroom/copilot_auth.py headroom/cli/wrap.py --ignore-missing-imports
Success: no issues found in 2 source files
```
## Real Behavior Proof
- Environment: macOS, Python 3.12, repo venv, native core loaded.
- Exact command / steps: the suites above, with the new regressions
failing when `_subscription_api_url_from_user_info_payload` is reverted
to the fold.
- Observed result: the token-exchange and user-info producers, `wrap
vscode`, `wrap copilot --subscription` and the OpenCode lane all hand
the dedicated proxy `https://api.enterprise.githubcopilot.com` (or
`.business.`) as both `--openai-api-url` and `GITHUB_COPILOT_API_URL`;
completions still resolve to `copilot-proxy.githubusercontent.com`.
- Live seat: see "Live verification" below. Not tested: an
Enterprise-plan seat (`api.enterprise.`; same code branch as Business),
and the `wrap copilot --subscription` lane (the test seat has the
Copilot CLI policy off).
### Verification needed before release
No Business or Enterprise seat is available to us. Carrier (Enterprise
seats behind a subscription-routed firewall) runs, on this branch, with
`SSL_CERT_FILE` unset and no `GITHUB_COPILOT_API_URL` exported:
```bash
headroom copilot-auth login # device flow; approve in the browser
headroom wrap vscode # leave running; note the port it prints
curl -s http://127.0.0.1:<port>/health | python3 -m json.tool | grep openai_api_url
# expected: "openai_api_url": "https://api.enterprise.githubcopilot.com"
# then send one Copilot Chat turn in VS Code (any model) and one inline completion
headroom wrap copilot --subscription -- --model claude-sonnet-4.6 -s -p "Reply with exactly: OK"
# expected: the banner shows COPILOT_PROVIDER_API_URL=https://api.enterprise.githubcopilot.com and the reply is OK
```
`curl -s http://127.0.0.1:<port>/health` on the dedicated proxy should
report `"openai_api_url": "https://api.enterprise.githubcopilot.com"`
(the "advertised plan host" log line is printed by the wrap process, not
written to `proxy.log`). If a model that the generic host serves is
rejected on the plan host (`400 ... model is not supported`), the #2455
condition still holds for that seat: set
`GITHUB_COPILOT_USE_ADVERTISED_HOST=0`, restart the wrapper, and report
the model and the response body on this PR before it is released.
### Live verification (2026-10-13, Copilot Business seat)
Branch merged with #4154 on current `main`, isolated
`HEADROOM_WORKSPACE_DIR`, `SSL_CERT_FILE` and `GITHUB_COPILOT_API_URL`
unset, `headroom copilot-auth login` device flow.
- `/copilot_internal/user` for the seat: `copilot_plan: business`,
`endpoints.api: https://api.business.githubcopilot.com`. (For about
three hours after the seat was assigned GitHub still advertised
`api.individual.`; the plan host appeared on its own.)
- Resolution: default → `https://api.business.githubcopilot.com`;
`GITHUB_COPILOT_USE_ADVERTISED_HOST=0` →
`https://api.githubcopilot.com`; explicit `GITHUB_COPILOT_API_URL` wins.
`main`'s `copilot_auth.py` resolves the same seat to
`https://api.githubcopilot.com`.
- `headroom wrap vscode`: dedicated proxy started with `--openai-api-url
https://api.business.githubcopilot.com`; `/health` reports it.
- Model sweep through the proxy, one turn per picker model on each
endpoint it lists (plus `gpt-4o-mini`): **36/36 returned 200** on the
plan host (Claude Haiku/Sonnet/Opus 4.x–5.5 on `/v1/messages` and
`/chat/completions`, Gemini 3.7/3.8/4, GPT-5.x/6.x and Grok on
`/responses`, `gpt-5-mini`, `gpt-4o-mini`). No `model_not_supported`, so
the #2441/#2455 lag does not reproduce on Business today.
- Real VS Code (Copilot Chat 0.69): Ask mode on Auto (`gpt-6-luna`),
Claude Sonnet 5.5 chat (`/v1/messages`), and Agent mode with tool calls
(wrote and ran a pytest file) all went to
`api.business.githubcopilot.com`; every Copilot upstream call in the
proxy log used the plan host, none the generic host.
- Found while testing, not caused by this PR and present on `main`:
ghost text / next edits (`copilot-nes-*` on `/chat/completions`) are
sent to CAPI and get `400 model_not_supported` on both the plan host and
the generic host. Fixed separately in #4188.
## Runtime Rollout Safety
- Rollout-managed feature(s): none; env-gated.
- Minimum rollout channel: stable.
- Stable/default behavior changed: yes. Business and Enterprise
subscription sessions now route to their advertised plan host instead of
`api.githubcopilot.com`. Individual seats, explicit pins and GHE domains
are unchanged.
- Kill switch / disable path: `GITHUB_COPILOT_USE_ADVERTISED_HOST=0`
restores the previous fold without a rollback; `GITHUB_COPILOT_API_URL`
pins any host.
- Unsafe override required: no.
- Qualification impact: adds plan-host routing coverage to the Copilot
auth and wrap suites.
- Rollback path: revert the commit.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
JerrettDavis
parent
feea130f81
commit
1dc7a183f5
@@ -26,17 +26,25 @@ default — with your token.)
|
||||
|
||||
## API host & Enterprise / data-residency
|
||||
|
||||
Headroom routes wrapped Copilot traffic to GitHub's **generic public host**,
|
||||
`https://api.githubcopilot.com`, for both `--subscription` and the implicit
|
||||
OAuth path. That host serves the full model set (including newer models on the
|
||||
responses API) and matches the routing that worked before 0.23.
|
||||
Which host a wrapped session talks to depends on the lane:
|
||||
|
||||
Headroom deliberately does **not** auto-select a per-account host from
|
||||
`/copilot_internal/user`. That endpoint advertises a segmented host (e.g.
|
||||
`api.individual.githubcopilot.com`) that does **not** serve newer models on the
|
||||
responses API and is not the host the official Copilot client routes with — using
|
||||
it regressed `headroom wrap copilot` after 0.22.4
|
||||
([#610](https://github.com/headroomlabs-ai/headroom/issues/610)).
|
||||
- **`--subscription`, `headroom wrap vscode`, and OpenCode `--copilot-subscription`**
|
||||
resolve the host from GitHub's token exchange. Business and Enterprise seats
|
||||
route to the plan host GitHub advertises (`api.business.githubcopilot.com` /
|
||||
`api.enterprise.githubcopilot.com`), which is what GitHub's own clients use and
|
||||
the only host an enterprise firewall on GitHub's subscription-based network
|
||||
routing allows. If that host rejects a model the generic host serves, set
|
||||
`GITHUB_COPILOT_USE_ADVERTISED_HOST=0` to fold back to the generic host.
|
||||
Individual seats route to the generic host: `api.individual.githubcopilot.com`
|
||||
does **not** serve newer models on the responses API, and using it regressed
|
||||
`headroom wrap copilot` after 0.22.4
|
||||
([#610](https://github.com/headroomlabs-ai/headroom/issues/610)).
|
||||
- **The implicit OAuth path** (plain `headroom wrap copilot` with a GitHub login
|
||||
and no provider key) performs no token exchange at launch and always uses the
|
||||
**generic public host** `https://api.githubcopilot.com`. On a firewall that
|
||||
allows only the plan host, use `--subscription` or pin the host as below.
|
||||
|
||||
An explicit `GITHUB_COPILOT_API_URL` wins over all of the above.
|
||||
|
||||
**Enterprise / data-residency:** if your organization is provisioned on a
|
||||
dedicated Copilot API host (GitHub Enterprise Cloud with data residency, or an
|
||||
@@ -48,11 +56,8 @@ export GITHUB_COPILOT_API_URL=https://api.<your-host>.githubcopilot.com
|
||||
headroom wrap copilot --subscription -- --model gpt-5.4
|
||||
```
|
||||
|
||||
If you operate such an environment and would like Headroom to **auto-detect** the
|
||||
correct host instead of pinning it, please [open an issue](https://github.com/headroomlabs-ai/headroom/issues/new) —
|
||||
the intended path is to resolve it from GitHub's token-exchange endpoint (the
|
||||
source the official Copilot client uses), and we'd want to validate it against a
|
||||
real enterprise tenant.
|
||||
The Business and Enterprise plan hosts are detected from GitHub's token-exchange
|
||||
endpoint (see above); only data-residency and egress-proxy hosts still need the pin.
|
||||
|
||||
## Status
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ Headroom runs on the developer's machine and talks to the same services the AI t
|
||||
|---|---|---|
|
||||
| `api.anthropic.com` | Claude / Claude Code | yes, for Anthropic |
|
||||
| `api.openai.com`, `chatgpt.com` | OpenAI API, Codex | yes, for OpenAI / Codex |
|
||||
| `api.githubcopilot.com`, `api.github.com` | GitHub Copilot | for Copilot |
|
||||
| `api.githubcopilot.com` (or `api.business.githubcopilot.com` / `api.enterprise.githubcopilot.com` for those plans), `api.github.com` | GitHub Copilot | for Copilot |
|
||||
| `huggingface.co` and the storage hosts it redirects to (`cdn-lfs.hf.co`, `cas-bridge.xethub.hf.co`) | Compression models (downloaded once) | recommended |
|
||||
| `openaipublic.blob.core.windows.net` | Tokenizer vocabulary (downloaded once) | recommended |
|
||||
|
||||
|
||||
@@ -144,10 +144,19 @@ remote, and profile-specific settings should use `--settings-file` explicitly.
|
||||
|
||||
## Enterprise
|
||||
|
||||
GitHub.com Enterprise Cloud normally requires no override; Headroom uses the API
|
||||
URL advertised during token exchange. For GitHub Enterprise Server/custom domains,
|
||||
set `GITHUB_COPILOT_ENTERPRISE_URL` or `GITHUB_COPILOT_ENTERPRISE_DOMAIN` before
|
||||
both `copilot-auth login` and `wrap vscode`.
|
||||
GitHub.com Enterprise Cloud normally requires no override. Copilot Business and
|
||||
Enterprise seats are routed to the host GitHub advertises during token exchange
|
||||
(`api.business.githubcopilot.com` or `api.enterprise.githubcopilot.com`), which is
|
||||
the host GitHub's own clients use. This matters on networks that follow GitHub's
|
||||
[subscription-based network routing](https://docs.github.com/en/copilot/how-tos/administer-copilot/manage-for-enterprise/manage-access/manage-network-access):
|
||||
their firewall allows only the plan host, and the generic `api.githubcopilot.com`
|
||||
is blocked. If the plan host rejects a model the generic host serves, set
|
||||
`GITHUB_COPILOT_USE_ADVERTISED_HOST=0` to route through the generic host again.
|
||||
An explicit `GITHUB_COPILOT_API_URL` still wins over both.
|
||||
|
||||
For GitHub Enterprise Server/custom domains, set `GITHUB_COPILOT_ENTERPRISE_URL`
|
||||
or `GITHUB_COPILOT_ENTERPRISE_DOMAIN` before both `copilot-auth login` and
|
||||
`wrap vscode`.
|
||||
|
||||
## Verification and troubleshooting
|
||||
|
||||
|
||||
@@ -6727,11 +6727,13 @@ def copilot(
|
||||
headroom wrap copilot --subscription -- --model gpt-4.1
|
||||
|
||||
\b
|
||||
Copilot hosted API (--subscription and the implicit OAuth path) routes to the
|
||||
generic host https://api.githubcopilot.com, which serves the full model set.
|
||||
Enterprise / data-residency accounts provisioned on a dedicated host pin it
|
||||
explicitly with GITHUB_COPILOT_API_URL (the override flows through to upstream).
|
||||
See TESTING-copilot-subscription.md for details.
|
||||
Copilot hosted API: --subscription routes Business / Enterprise seats to
|
||||
the plan host GitHub advertises (GITHUB_COPILOT_USE_ADVERTISED_HOST=0 folds
|
||||
them back to the generic host) and individual seats to the generic host
|
||||
https://api.githubcopilot.com; the implicit OAuth path always uses the
|
||||
generic host. Data-residency accounts provisioned on a dedicated host pin
|
||||
it explicitly with GITHUB_COPILOT_API_URL (the override flows through to
|
||||
upstream). See TESTING-copilot-subscription.md for details.
|
||||
"""
|
||||
copilot_bin = _require_binary(
|
||||
"copilot",
|
||||
|
||||
+45
-14
@@ -1065,6 +1065,28 @@ def _api_url_from_payload(payload: dict[str, Any] | None) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
#: Opt-out: ``0``/``false``/``no``/``off`` folds the Business and Enterprise
|
||||
#: hosts below back into the generic host, for seats where the segmented host
|
||||
#: still lags on model availability (#2455 folded them after a Business seat
|
||||
#: got "model not supported" for Claude models on its plan host).
|
||||
_USE_ADVERTISED_HOST_ENV = "GITHUB_COPILOT_USE_ADVERTISED_HOST"
|
||||
|
||||
#: Per-plan chat hosts GitHub advertises in ``endpoints.api`` for Business and
|
||||
#: Enterprise seats. Honoured by default: they are the hosts GitHub's own
|
||||
#: clients route with, and an enterprise on GitHub's subscription-based network
|
||||
#: routing allows only its own plan host, so the generic host is unreachable
|
||||
#: there and the failure looks like a random connection error. The individual
|
||||
#: host is still folded (#610: it did not serve newer models on the Responses
|
||||
#: API). An explicit ``GITHUB_COPILOT_API_URL`` or enterprise domain wins over
|
||||
#: both.
|
||||
_ADVERTISED_PLAN_HOSTS: frozenset[str] = frozenset(
|
||||
{
|
||||
"api.business.githubcopilot.com",
|
||||
"api.enterprise.githubcopilot.com",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _subscription_api_url_from_user_info_payload(payload: dict[str, Any] | None) -> str:
|
||||
configured = _configured_api_url_override()
|
||||
if configured:
|
||||
@@ -1075,12 +1097,21 @@ def _subscription_api_url_from_user_info_payload(payload: dict[str, Any] | None)
|
||||
return DEFAULT_API_URL
|
||||
|
||||
host = urlparse(api_url).netloc.lower()
|
||||
if host in {
|
||||
"api.githubcopilot.com",
|
||||
"api.individual.githubcopilot.com",
|
||||
"api.business.githubcopilot.com",
|
||||
"api.enterprise.githubcopilot.com",
|
||||
}:
|
||||
if host in _ADVERTISED_PLAN_HOSTS:
|
||||
opt_out = os.environ.get(_USE_ADVERTISED_HOST_ENV, "").strip().lower()
|
||||
if opt_out in {"0", "false", "no", "off"}:
|
||||
return DEFAULT_API_URL
|
||||
# Named in the log so a "model not supported" 400 from the plan host
|
||||
# sits next to the escape hatch instead of looking like a proxy fault.
|
||||
logger.info(
|
||||
"Routing Copilot subscription traffic to the advertised plan host %s "
|
||||
"(set %s=0 to use %s instead)",
|
||||
api_url,
|
||||
_USE_ADVERTISED_HOST_ENV,
|
||||
DEFAULT_API_URL,
|
||||
)
|
||||
return api_url
|
||||
if host in {"api.githubcopilot.com", "api.individual.githubcopilot.com"}:
|
||||
return DEFAULT_API_URL
|
||||
if host.endswith(".githubcopilot.com"):
|
||||
return api_url
|
||||
@@ -1406,14 +1437,14 @@ def resolve_copilot_api_url(oauth_token: str | None = None) -> str:
|
||||
(corporate proxy, enterprise / data-residency host, tests).
|
||||
2. The generic public host ``https://api.githubcopilot.com``.
|
||||
|
||||
The account-specific ``endpoints.api`` advertised by ``/copilot_internal/user``
|
||||
is intentionally NOT used to route. It returns a segmented host (e.g.
|
||||
``api.individual.githubcopilot.com``) that does not serve newer models on the
|
||||
responses API — wrapping such a request regressed after 0.22.4 (#610) — and it
|
||||
is not the host the official Copilot client routes with (that comes from the
|
||||
token-exchange endpoint, not user info). Accounts that genuinely require a
|
||||
dedicated host set ``GITHUB_COPILOT_API_URL`` explicitly. ``oauth_token`` is
|
||||
accepted for call-site compatibility but no longer triggers a network lookup.
|
||||
This path performs no network lookup, so it cannot see the per-plan host a
|
||||
token exchange advertises. The subscription lanes (``wrap vscode``,
|
||||
``wrap copilot --subscription``, OpenCode) resolve that host through
|
||||
:func:`resolve_subscription_bearer_token_details` and pin it into the proxy
|
||||
as ``GITHUB_COPILOT_API_URL``, which step 1 then honours. The
|
||||
``api.individual.githubcopilot.com`` host is never routed to (#610: it did
|
||||
not serve newer models on the Responses API). ``oauth_token`` is accepted
|
||||
for call-site compatibility but no longer triggers a network lookup.
|
||||
"""
|
||||
|
||||
del oauth_token # reserved; routing no longer depends on a user-info lookup
|
||||
|
||||
@@ -6,7 +6,7 @@ import importlib
|
||||
import sys
|
||||
import types
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
from urllib.parse import quote
|
||||
|
||||
import click
|
||||
@@ -876,6 +876,48 @@ def test_wrap_copilot_oauth_keeps_generic_endpoint_when_account_advertised(
|
||||
assert env["GITHUB_COPILOT_API_URL"] == DEFAULT_API_URL
|
||||
|
||||
|
||||
def test_wrap_copilot_implicit_oauth_stays_generic_and_does_no_account_lookup(
|
||||
runner: CliRunner,
|
||||
wrap_modules: tuple[types.ModuleType, click.Group],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""The implicit OAuth lane performs no user-info or token-exchange lookup at
|
||||
launch, so it cannot see a plan host and always routes to the generic host.
|
||||
Pinned so the guide's split between the lanes stays true: an Enterprise seat
|
||||
that needs its plan host must use --subscription or wrap vscode."""
|
||||
_wrap_cli, main = wrap_modules
|
||||
_clear_copilot_env(monkeypatch)
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
def fake_launch_tool(**kwargs): # noqa: ANN003
|
||||
captured.update(kwargs)
|
||||
|
||||
enterprise = {"endpoints": {"api": "https://api.enterprise.githubcopilot.com"}}
|
||||
user_info = MagicMock(return_value=enterprise)
|
||||
exchange = MagicMock(return_value={"token": "copilot-api", **enterprise})
|
||||
|
||||
with (
|
||||
patch("headroom.cli.wrap.shutil.which", return_value="copilot"),
|
||||
patch("headroom.cli.wrap.resolve_client_bearer_token", return_value="gho-oauth"),
|
||||
patch("headroom.cli.wrap.has_oauth_auth", return_value=True),
|
||||
patch("headroom.copilot_auth._fetch_copilot_user_info", user_info),
|
||||
patch(
|
||||
"headroom.copilot_auth.CopilotTokenProvider._exchange_token_sync",
|
||||
staticmethod(exchange),
|
||||
),
|
||||
patch("headroom.cli.wrap._launch_tool", side_effect=fake_launch_tool),
|
||||
):
|
||||
result = runner.invoke(main, ["wrap", "copilot", "--", "--model", "gpt-5.4"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert captured["openai_api_url"] == DEFAULT_API_URL
|
||||
# The lane never asks GitHub which host the seat is on: neither the
|
||||
# user-info lookup nor the token exchange that would advertise the
|
||||
# Enterprise host is consulted.
|
||||
assert user_info.call_count == 0
|
||||
assert exchange.call_count == 0
|
||||
|
||||
|
||||
def test_wrap_copilot_oauth_honors_api_url_override(
|
||||
runner: CliRunner,
|
||||
wrap_modules: tuple[types.ModuleType, click.Group],
|
||||
@@ -982,11 +1024,13 @@ def test_wrap_copilot_subscription_uses_resolved_subscription_endpoint(
|
||||
assert env["COPILOT_PROVIDER_BEARER_TOKEN"] == "copilot-api"
|
||||
|
||||
|
||||
def test_wrap_copilot_subscription_normalizes_enterprise_host(
|
||||
def test_wrap_copilot_subscription_honours_advertised_enterprise_host(
|
||||
runner: CliRunner,
|
||||
wrap_modules: tuple[types.ModuleType, click.Group],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""An Enterprise seat routes to the host its token exchange advertised; a
|
||||
firewall on GitHub's subscription-based routing allows only that host."""
|
||||
_wrap_cli, main = wrap_modules
|
||||
_clear_copilot_env(monkeypatch)
|
||||
captured: dict[str, object] = {}
|
||||
@@ -1028,9 +1072,10 @@ def test_wrap_copilot_subscription_normalizes_enterprise_host(
|
||||
assert result.exit_code == 0, result.output
|
||||
env = captured["env"]
|
||||
assert isinstance(env, dict)
|
||||
assert captured["openai_api_url"] == DEFAULT_API_URL
|
||||
assert env["OPENAI_TARGET_API_URL"] == DEFAULT_API_URL
|
||||
assert env["GITHUB_COPILOT_API_URL"] == DEFAULT_API_URL
|
||||
enterprise_api = "https://api.enterprise.githubcopilot.com"
|
||||
assert captured["openai_api_url"] == enterprise_api
|
||||
assert env["OPENAI_TARGET_API_URL"] == enterprise_api
|
||||
assert env["GITHUB_COPILOT_API_URL"] == enterprise_api
|
||||
|
||||
|
||||
def test_wrap_copilot_subscription_honors_api_url_override(
|
||||
|
||||
@@ -73,7 +73,7 @@ def _subscription_resolution() -> CopilotSubscriptionTokenResolution:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_wrap_opencode_copilot_subscription_normalizes_enterprise_host_and_handoffs_seed_after_actual_port(
|
||||
def test_wrap_opencode_copilot_subscription_honours_advertised_enterprise_host_and_handoffs_seed_after_actual_port(
|
||||
runner: CliRunner,
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -144,7 +144,7 @@ def test_wrap_opencode_copilot_subscription_normalizes_enterprise_host_and_hando
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
ensure = captured["ensure"]
|
||||
assert ensure["openai_api_url"] == "https://api.githubcopilot.com"
|
||||
assert ensure["openai_api_url"] == "https://api.enterprise.githubcopilot.com"
|
||||
assert ensure["copilot_api_token"] == "copilot-api-secret"
|
||||
assert ensure["copilot_refresh_oauth_token"] == "gho-oauth"
|
||||
assert ensure["copilot_api_token_expires_at"] == 123.5
|
||||
|
||||
@@ -541,7 +541,7 @@ def test_subscription_enterprise_host_repro(
|
||||
assert resolution.token == "copilot-api"
|
||||
assert resolution.source == "headroom-copilot-auth:/tmp/copilot_auth.json:token-exchange"
|
||||
assert resolution.confidence == "copilot-token-exchange"
|
||||
assert resolution.api_url == copilot_auth.DEFAULT_API_URL
|
||||
assert resolution.api_url == "https://api.enterprise.githubcopilot.com"
|
||||
assert resolution.token_fingerprint == copilot_auth.token_fingerprint("copilot-api")
|
||||
assert resolution.refresh_oauth_token == "gho-oauth"
|
||||
assert isinstance(resolution.api_token_expires_at, float)
|
||||
@@ -589,7 +589,7 @@ def test_resolve_subscription_exchange_uses_cloud_enterprise_advertised_api(
|
||||
resolution = copilot_auth.resolve_subscription_bearer_token_details()
|
||||
|
||||
assert resolution is not None
|
||||
assert resolution.api_url == copilot_auth.DEFAULT_API_URL
|
||||
assert resolution.api_url == "https://api.enterprise.githubcopilot.com"
|
||||
assert copilot_auth._token_exchange_url() == "https://api.github.com/copilot_internal/v2/token"
|
||||
|
||||
|
||||
@@ -610,7 +610,8 @@ def test_api_url_from_exchange_payload_rejects_non_copilot_host(
|
||||
oauth_token="gho-oauth",
|
||||
)
|
||||
|
||||
assert resolved == copilot_auth.DEFAULT_API_URL
|
||||
# The foreign host is ignored; the user-info lookup's Business host is used.
|
||||
assert resolved == "https://api.business.githubcopilot.com"
|
||||
|
||||
|
||||
def _resolve_subscription_producer_path(
|
||||
@@ -718,11 +719,14 @@ def test_subscription_unknown_host_passthrough(monkeypatch: pytest.MonkeyPatch)
|
||||
[
|
||||
"https://api.githubcopilot.com",
|
||||
"https://api.individual.githubcopilot.com",
|
||||
"https://api.business.githubcopilot.com",
|
||||
"https://api.enterprise.githubcopilot.com",
|
||||
],
|
||||
)
|
||||
def test_subscription_known_hosts_normalize_to_default(payload_host: str) -> None:
|
||||
def test_subscription_individual_host_normalizes_to_default(
|
||||
monkeypatch: pytest.MonkeyPatch, payload_host: str
|
||||
) -> None:
|
||||
"""#610: the individual host lags on newer models, so it is never routed to."""
|
||||
monkeypatch.delenv("GITHUB_COPILOT_API_URL", raising=False)
|
||||
monkeypatch.delenv("GITHUB_COPILOT_USE_ADVERTISED_HOST", raising=False)
|
||||
assert (
|
||||
copilot_auth._subscription_api_url_from_user_info_payload(
|
||||
{"endpoints": {"api": payload_host}}
|
||||
@@ -731,6 +735,53 @@ def test_subscription_known_hosts_normalize_to_default(payload_host: str) -> Non
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload_host",
|
||||
[
|
||||
"https://api.business.githubcopilot.com",
|
||||
"https://api.enterprise.githubcopilot.com",
|
||||
],
|
||||
)
|
||||
def test_subscription_business_and_enterprise_hosts_are_honoured(
|
||||
monkeypatch: pytest.MonkeyPatch, payload_host: str
|
||||
) -> None:
|
||||
"""A firewall on GitHub's subscription-based routing allows only the plan host."""
|
||||
monkeypatch.delenv("GITHUB_COPILOT_API_URL", raising=False)
|
||||
monkeypatch.delenv("GITHUB_COPILOT_USE_ADVERTISED_HOST", raising=False)
|
||||
assert (
|
||||
copilot_auth._subscription_api_url_from_user_info_payload(
|
||||
{"endpoints": {"api": payload_host + "/"}}
|
||||
)
|
||||
== payload_host
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("opt_out", ["0", "false", "no", "off", " OFF "])
|
||||
def test_subscription_advertised_host_opt_out_restores_generic_fold(
|
||||
monkeypatch: pytest.MonkeyPatch, opt_out: str
|
||||
) -> None:
|
||||
"""#2455: seats whose plan host lags on models can fold back to the generic host."""
|
||||
monkeypatch.delenv("GITHUB_COPILOT_API_URL", raising=False)
|
||||
monkeypatch.setenv("GITHUB_COPILOT_USE_ADVERTISED_HOST", opt_out)
|
||||
assert (
|
||||
copilot_auth._subscription_api_url_from_user_info_payload(
|
||||
{"endpoints": {"api": "https://api.enterprise.githubcopilot.com"}}
|
||||
)
|
||||
== copilot_auth.DEFAULT_API_URL
|
||||
)
|
||||
|
||||
|
||||
def test_subscription_advertised_host_explicit_pin_wins(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.delenv("GITHUB_COPILOT_USE_ADVERTISED_HOST", raising=False)
|
||||
monkeypatch.setenv("GITHUB_COPILOT_API_URL", "https://api.githubcopilot.com")
|
||||
assert (
|
||||
copilot_auth._subscription_api_url_from_user_info_payload(
|
||||
{"endpoints": {"api": "https://api.enterprise.githubcopilot.com"}}
|
||||
)
|
||||
== "https://api.githubcopilot.com"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user