fix(copilot): send VS Code next-edit requests to the completions host (#4188)

## Description

VS Code Copilot Chat serves ghost text and next edits by posting
`copilot-nes-*` models (seen live: `copilot-nes-pandia-25`, Copilot Chat
0.69) to
`{github.copilot.advanced.debug.overrideProxyUrl}/chat/completions`.
`headroom wrap vscode` points that override and the CAPI override at the
same proxy, so these requests land on the OpenAI chat handler and were
forwarded to CAPI. CAPI answers `400 model_not_supported` for them on
both the per-plan host and the generic host; only the Copilot
completions host serves them. Result: behind Headroom, VS Code shows no
ghost text and no next-edit suggestions, with nothing in the UI saying
why.

Found during the live Copilot Business end-to-end run for #4155 / #4154;
it is on `main` and independent of both. #4144 does not cover it (it
handles the legacy `/v1/engines/*/completions` path only).

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `headroom/copilot_auth.py`: `copilot_nes_base_url(model,
upstream_base_url)` returns the completions host for a `copilot-nes-*`
request aimed at a public CAPI host, via the existing
`copilot_completions_base_url()` resolver (operator
`GITHUB_COPILOT_PROXY_URL` > advertised `endpoints.proxy` >
`copilot-proxy.githubusercontent.com`), the same resolver legacy
`/v1/engines` completions routing uses. It returns `None` for any other
model, for an upstream that already is a completions host, and for GHE /
custom deployments, whose traffic must stay inside them.
- `headroom/proxy/handlers/openai.py`: the chat-completions handler
applies it right after resolving the upstream, unless the caller chose
one with `x-headroom-base-url` (same rule as
`select_passthrough_base_url`). URL, auth, cache key, and both buffered
and streaming arms use the rerouted base.
- `tests/test_proxy/test_copilot_nes_routing.py`: through `create_app`
at the send seams: NES goes to the completions host with minted Copilot
credentials (buffered and streaming); follows an advertised
`endpoints.proxy`; chat models stay on the plan host; a GHE tenant keeps
its NES traffic; a completions-host target is not moved; a caller-chosen
base URL is left alone.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ pytest -q tests/test_proxy/test_copilot_nes_routing.py
7 passed
# on main (fix reverted): the two NES routing tests fail with
#   assert 'https://api.business.githubcopilot.com/chat/completions' == 'https://copilot-proxy.githubusercontent.com/chat/completions'

$ pytest -q tests/test_proxy/ tests/test_copilot_vscode_completions_routing.py tests/test_copilot_auth.py \
    tests/test_proxy_copilot_auth_hooks.py tests/test_provider_proxy_routes.py tests/test_copilot_native_mode.py \
    tests/test_copilot_subscription_smoke.py tests/test_cli/test_wrap_vscode.py
698 passed

$ uvx ruff@0.16.8 check <touched files> && uvx ruff@0.16.8 format --check <touched files>
All checks passed! / 3 files already formatted
$ mypy headroom/copilot_auth.py headroom/proxy/handlers/openai.py --ignore-missing-imports
Success: no issues found in 2 source files
```

## Real Behavior Proof

- Environment: macOS, Python 3.12, native core, live GitHub Copilot
Business seat (2026-10-13), VS Code with Copilot Chat 0.69, isolated
`HEADROOM_WORKSPACE_DIR`, branch merged with #4155 and #4154 on current
`main`.
- Exact command / steps: `headroom copilot-auth login`; `headroom wrap
vscode --configure` (dedicated proxy on the plan host
`api.business.githubcopilot.com`); in VS Code, type in a Python function
body and wait for ghost text. Then the same `copilot-nes-pandia-25`
request sent directly to each host with the exchanged Copilot token.
- Observed result: without the fix, the proxy forwarded NES to
`https://api.business.githubcopilot.com/chat/completions` → `400
{"error":{"message":"The requested model is not
supported.","code":"model_not_supported"}}` (3 of 3), no ghost text.
Direct probe: `api.business.githubcopilot.com` 400,
`api.githubcopilot.com` 400, `proxy.business.githubcopilot.com` 200,
`copilot-proxy.githubusercontent.com` 200. With the fix, NES went to
`https://copilot-proxy.githubusercontent.com/chat/completions` → 200 (4
of 4), ghost text appeared in VS Code and was accepted; chat, Claude and
Agent-mode turns stayed on the plan host.
- Not tested: an Enterprise-plan seat (same code path); a GHE
(`*.ghe.com`) tenant live (covered by unit test).

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: stable.
- Stable/default behavior changed: `copilot-nes-*` chat requests aimed
at a public CAPI host now go to the Copilot completions host instead of
failing with 400.
- Kill switch / disable path: `GITHUB_COPILOT_PROXY_URL` pins the
completions host; revert the commit.
- Unsafe override required: no.
- Qualification impact: Copilot routing and proxy suites.
- Rollback path: revert.

## 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 performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] 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 or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

- Docs: no doc change; `docs/content/docs/vscode-copilot.mdx` does not
describe NES routing.
- Follow-ups, not in this PR: (1) in the `wrap vscode` lane the
dedicated proxy does no token exchange of its own, so no
`endpoints.proxy` is recorded and NES uses
`copilot-proxy.githubusercontent.com` rather than the plan's
`proxy.business.githubcopilot.com`; GitHub's subscription-routing
guidance only blocks `*.individual.githubcopilot.com`, so this should
pass a plan-routed firewall, but wrap could pin the advertised proxy
host into the dedicated proxy as it does the API host in #4155. (2) NES
requests now run through the chat pipeline (compression, memory
injection when `--memory` is on, turn hooks); next-edit prompts are
latency-sensitive and memory context is probably unwanted there.
- Independent review (code-reviewer subagent) found no blockers; its
three SHOULDs (GHE tenant leak, caller-chosen base URL override,
completions-host target moved) are fixed and each has a test.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4188"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
Tejas Chopra
2026-10-13 15:49:02 -07:00
committed by GitHub
co-authored by Claude Opus 5.5 JerrettDavis
parent 1dc7a183f5
commit 22a1296cfa
3 changed files with 216 additions and 0 deletions
+22
View File
@@ -346,6 +346,28 @@ def is_copilot_completions_host(url: str | None) -> bool:
return host.startswith("proxy.") and host.endswith(".githubcopilot.com")
def copilot_nes_base_url(model: Any, upstream_base_url: str) -> str | None:
"""Return the completions host for a next-edit request aimed at public CAPI.
Copilot Chat serves ghost text and next edits by posting ``copilot-nes-*``
models to ``{overrideProxyUrl}/chat/completions``. ``headroom wrap vscode``
points that override at the same proxy as CAPI, but only the completions
host serves these models; CAPI answers ``400 model_not_supported``.
Returns ``None`` (keep the upstream) for any other model, for an upstream
that already is a completions host, and for a GHE or custom deployment,
whose traffic must not leave it (see :func:`copilot_completions_base_url`).
"""
if not (isinstance(model, str) and model.startswith("copilot-nes-")):
return None
if not _is_public_copilot_api_host(_url_host(upstream_base_url)):
return None
if is_copilot_completions_host(upstream_base_url):
return None
return copilot_completions_base_url()
def copilot_completions_base_url() -> str:
"""Return the base URL serving Copilot's inline-completions endpoint.
+11
View File
@@ -69,6 +69,7 @@ from headroom.config import is_tool_excluded, unwrap_tool_call_name
from headroom.copilot_auth import (
apply_copilot_api_auth,
build_copilot_upstream_url,
copilot_nes_base_url,
is_copilot_api_url,
)
from headroom.pipeline import PipelineStage, summarize_routing_markers
@@ -3145,6 +3146,16 @@ class OpenAIHandlerMixin:
)
custom_upstream_base_url = _resolve_openai_upstream_base(request.headers)
upstream_base_url = self._resolve_openai_upstream(request)
# Ghost text and next edits: CAPI does not serve these models. A
# caller-chosen x-headroom-base-url is left alone, as in
# select_passthrough_base_url.
nes_base_url = (
copilot_nes_base_url(model, upstream_base_url)
if custom_upstream_base_url is None
else None
)
if nes_base_url:
upstream_base_url = nes_base_url
handler_path_suffix = _resolve_openai_chat_handler_path(
upstream_base_url,
model,
@@ -0,0 +1,183 @@
"""VS Code's ghost text and next edits must reach Copilot's completions host.
Copilot Chat posts ``copilot-nes-*`` models to ``{overrideProxyUrl}/chat/completions``.
``headroom wrap vscode`` points that override and the CAPI override at the same
proxy, so these requests arrive on the chat handler next to ordinary chat turns.
CAPI answers them with ``400 model_not_supported`` on both the generic and the
per-plan host, so VS Code showed no ghost text at all behind Headroom. Pinned at
the send seams: the URL that was built and the credentials sent.
"""
from __future__ import annotations
import pytest
fastapi = pytest.importorskip("fastapi")
httpx = pytest.importorskip("httpx")
from fastapi.testclient import TestClient # noqa: E402
from headroom import copilot_auth # noqa: E402
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
BUSINESS_CAPI = "https://api.business.githubcopilot.com"
DEFAULT_COMPLETIONS = "https://copilot-proxy.githubusercontent.com"
NES_MODEL = "copilot-nes-pandia-25"
MINTED = "tid_minted_for_test"
class _Send:
"""Capture what the buffered or streaming arm was about to send."""
def __init__(self) -> None:
self.url: str | None = None
self.headers: dict[str, str] = {}
async def __call__(self, method, url, headers, body, **kwargs):
self.url = url
self.headers = {k.lower(): v for k, v in dict(headers).items()}
return httpx.Response(
200,
json={
"id": "c1",
"object": "chat.completion",
"model": "m",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "ok"},
"finish_reason": "stop",
}
],
"usage": {"prompt_tokens": 5, "completion_tokens": 1, "total_tokens": 6},
},
request=httpx.Request(method, url),
)
async def send_stream(self, request, **kwargs):
"""Stand in for ``http_client.send``: the streaming arm's wire, after auth."""
self.url = str(request.url)
self.headers = {k.lower(): v for k, v in request.headers.items()}
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
content=b'data: {"choices":[{"index":0,"delta":{"content":"ok"}}]}\n\ndata: [DONE]\n\n',
request=request,
)
@pytest.fixture(autouse=True)
def _isolated_copilot_state(monkeypatch: pytest.MonkeyPatch):
class _Token:
token = MINTED
class _Provider:
async def get_api_token(self, integration_id: str | None = None):
return _Token()
monkeypatch.setattr(copilot_auth, "get_copilot_token_provider", lambda: _Provider())
for name in (
"GITHUB_COPILOT_PROXY_URL",
"GITHUB_COPILOT_API_URL",
"GITHUB_COPILOT_ENTERPRISE_URL",
):
monkeypatch.delenv(name, raising=False)
copilot_auth.reset_observed_completions_endpoint()
yield
copilot_auth.reset_observed_completions_endpoint()
def _post(
model: str,
*,
openai_api_url: str = BUSINESS_CAPI,
stream: bool = False,
headers: dict[str, str] | None = None,
) -> _Send:
send = _Send()
config = ProxyConfig(
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
mode="token",
openai_api_url=openai_api_url,
)
with TestClient(create_app(config)) as client:
proxy = client.app.state.proxy
proxy._retry_request = send
proxy.http_client.send = send.send_stream
resp = client.post(
"/chat/completions",
json={
"model": model,
"stream": stream,
"max_tokens": 16,
"messages": [{"role": "user", "content": "def add(a, b):\n "}],
},
headers={"Authorization": "Bearer vscode-session-token", **(headers or {})},
)
assert resp.status_code == 200
return send
def test_next_edit_models_go_to_the_completions_host_with_copilot_credentials() -> None:
send = _post(NES_MODEL)
assert send.url == f"{DEFAULT_COMPLETIONS}/chat/completions"
assert send.headers["authorization"] == f"Bearer {MINTED}"
def test_streamed_next_edits_go_to_the_completions_host_with_copilot_credentials() -> None:
send = _post(NES_MODEL, stream=True)
assert send.url == f"{DEFAULT_COMPLETIONS}/chat/completions"
assert send.headers["authorization"] == f"Bearer {MINTED}"
def test_next_edit_models_follow_the_completions_host_github_advertised() -> None:
copilot_auth._remember_completions_endpoint(
{"endpoints": {"proxy": "https://proxy.business.githubcopilot.com"}}
)
send = _post(NES_MODEL)
assert send.url == "https://proxy.business.githubcopilot.com/chat/completions"
def test_chat_models_stay_on_the_plan_host() -> None:
send = _post("gpt-4o-mini")
assert send.url == f"{BUSINESS_CAPI}/chat/completions"
def test_a_ghe_tenant_keeps_its_next_edits_inside_its_deployment() -> None:
send = _post(NES_MODEL, openai_api_url="https://copilot-api.acme.ghe.com")
assert send.url is not None
assert send.url.startswith("https://copilot-api.acme.ghe.com/")
def test_a_configured_enterprise_deployment_keeps_next_edits_with_a_public_upstream(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("GITHUB_COPILOT_ENTERPRISE_URL", "https://acme.ghe.com")
send = _post(NES_MODEL, openai_api_url="https://api.githubcopilot.com")
assert send.url == "https://copilot-api.acme.ghe.com/chat/completions"
def test_a_completions_host_target_is_not_moved() -> None:
send = _post(NES_MODEL, openai_api_url="https://proxy.enterprise.githubcopilot.com")
assert send.url == "https://proxy.enterprise.githubcopilot.com/chat/completions"
def test_a_caller_chosen_base_url_is_left_alone() -> None:
send = _post(
NES_MODEL,
headers={"x-headroom-base-url": "https://api.githubcopilot.com"},
)
assert send.url is not None
assert send.url.startswith("https://api.githubcopilot.com/")