headroom/tests/test_provider_copilot_vscode_config.py
JD Davis 007446c73a
feat(copilot): proxy VS Code models transparently (#2687)
## Description

Make Headroom a transparent proxy for VS Code GitHub Copilot. Users keep
using Copilot's normal model picker—GPT-4.1, Claude Sonnet, Claude Opus,
and other models in their entitlement—while Headroom silently forwards
the selected model instead of registering or requiring a separate
"Headroom" model.

This also fixes GitHub's device OAuth exchange by sending form-encoded
request bodies, matching the endpoint contract.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [x] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Add `headroom wrap vscode` to start a Copilot-seeded subscription
proxy and safely configure VS Code's shipped Copilot proxy override.
- Add `headroom unwrap vscode` for reversible cleanup.
- Preserve VS Code's selected model by changing only the proxy URL/auth
override; no custom model is registered and no model preference is
written.
- Support stable VS Code settings locations on macOS, Windows, and
Linux, plus `--settings-file` for Insiders, portable, and other
installations.
- Edit JSONC settings with a marker-owned block while preserving
unrelated bytes, comments, ordering, and trailing commas.
- Refuse malformed markers, invalid JSONC, or unmanaged existing Copilot
overrides instead of overwriting user configuration.
- Fix SIGINT cleanup so the managed settings block is removed and normal
shutdown exits successfully.
- Fix Copilot device OAuth start/poll requests to use
`application/x-www-form-urlencoded`.
- Add a compatibility matrix, setup/removal flow, credential behavior,
remote-development guidance, enterprise notes, troubleshooting, and
verification documentation.

## Testing

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

### Test Output

```text
$ .venv/bin/pytest -q tests/test_provider_copilot_vscode_config.py tests/test_cli/test_wrap_vscode.py tests/test_cli/test_wrap_helpers.py tests/test_copilot_auth.py tests/test_cli/test_copilot_auth.py tests/test_provider_copilot_wrap.py tests/test_cli/test_wrap_copilot.py tests/test_copilot_provider_label.py tests/test_copilot_subscription_smoke.py
244 passed in 0.59s

$ .venv/bin/ruff check <changed Python files and tests>
All checks passed!

$ .venv/bin/mypy headroom/providers/copilot/vscode.py
Success: no issues found in 1 source file

$ cd docs && npm run types:check
fumadocs-mdx && next typegen && tsc --noEmit
# exited 0

$ git diff --check
# exited 0
```

The full 10,179-test suite was also sampled through approximately 83%,
but was stopped because of its runtime. It exposed existing failures in
`test_recover_codex.py`, `test_wrap_stale_marker.py`, and
`test_proxy_health.py`; therefore the broad `pytest`, repository-wide
Ruff, and repository-wide mypy boxes are intentionally not checked.

## Real Behavior Proof

- Environment: macOS arm64, VS Code 1.131.0, built-in GitHub Copilot
0.59.0, Headroom 0.33.1-dev.
- Exact command / steps:
  1. Completed `headroom copilot login` with GitHub's device flow.
  2. Ran `.venv/bin/headroom wrap vscode --port 8788`.
3. Confirmed VS Code retained its ordinary Copilot model catalog and
made `GET /models` through Headroom with `GitHubCopilotChat/0.59.0` and
`editor-version: vscode/1.131.0`.
4. Sent native Copilot `/p/headroom/chat/completions` requests through
the same endpoint using `gpt-4.1`, `claude-sonnet-4.6`, and
`claude-opus-4.7`.
- Observed result:
  - All three completion requests returned HTTP 200.
- GPT-4.1 resolved upstream to `gpt-4.1-2025-04-14`; Sonnet and Opus
retained their exact selected IDs.
  - All returned the requested exact marker content.
- VS Code's settings contained only the Headroom proxy URL and token
auth override—no Headroom model or model-selection setting.
- The proxy health endpoint remained ready with `openai_api_url` set to
`https://api.githubcopilot.com`.
- Not tested:
- Physical Windows or Linux hosts (their path/config behavior is covered
by unit tests).
- WSL, dev containers, SSH remotes, VS Code Insiders, or enterprise
Copilot deployments end-to-end.
  - Every model in the live Copilot catalog.
- A fully submitted chat from VS Code's UI automation; the real
extension's catalog request and native completion paths were verified
separately.

## 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
- [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 or that my
feature works
- [x] New and existing targeted unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

Not applicable; this integration intentionally has no separate UI or
model entry.

## Additional Notes

The integration uses VS Code Copilot's shipped advanced/debug proxy
endpoint seam. The managed settings block is deliberately narrow and
reversible. Remote extension hosts may need their own reachable
proxy/configuration as documented.

---------

Co-authored-by: JerrettDavis <2610199+JerrettDavis@users.noreply.github.com>
2026-08-03 04:42:48 -07:00

104 lines
3.8 KiB
Python

from __future__ import annotations
from pathlib import Path
import click
import pytest
from headroom.providers.copilot.vscode import (
configure_vscode_proxy_settings,
remove_vscode_proxy_settings,
vscode_proxy_url,
vscode_settings_path,
)
@pytest.mark.parametrize(
("platform", "env", "expected"),
[
(
"darwin",
{"HOME": "/Users/a"},
"/Users/a/Library/Application Support/Code/User/settings.json",
),
(
"win32",
{"APPDATA": "C:/Users/A/AppData/Roaming"},
"C:/Users/A/AppData/Roaming/Code/User/settings.json",
),
("linux", {"HOME": "/home/a"}, "/home/a/.config/Code/User/settings.json"),
("linux", {"HOME": "/home/a", "XDG_CONFIG_HOME": "/cfg"}, "/cfg/Code/User/settings.json"),
],
)
def test_vscode_settings_path_is_cross_platform(
platform: str, env: dict[str, str], expected: str
) -> None:
assert str(vscode_settings_path(platform=platform, environ=env)).replace("\\", "/") == expected
def test_proxy_url_carries_project_without_changing_model() -> None:
assert vscode_proxy_url(8787, "my project") == "http://127.0.0.1:8787/p/my%20project"
def test_configure_update_and_remove_preserve_jsonc_verbatim(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
original = '{\n\t// user comment\n\t"editor.fontSize": 15,\n}\n'
path.write_text(original, encoding="utf-8")
assert configure_vscode_proxy_settings(path, "http://127.0.0.1:8787") == "added"
configured = path.read_text(encoding="utf-8")
assert "editor.fontSize" in configured
assert "user comment" in configured
assert '"github.copilot.advanced.debug.overrideProxyUrl"' in configured
assert '"github.copilot.advanced.debug.overrideAuthType": "token"' in configured
assert configure_vscode_proxy_settings(path, "http://127.0.0.1:9999") == "updated"
assert "9999" in path.read_text(encoding="utf-8")
assert "8787" not in path.read_text(encoding="utf-8")
assert remove_vscode_proxy_settings(path) is True
assert path.read_text(encoding="utf-8") == original
assert remove_vscode_proxy_settings(path) is False
def test_configure_refuses_malformed_and_unmanaged_override(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
for original in (
"{broken",
'{"github.copilot.advanced.debug.overrideProxyUrl":"http://other"}',
):
path.write_text(original, encoding="utf-8")
with pytest.raises(click.ClickException, match="did not overwrite|refusing"):
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
assert path.read_text(encoding="utf-8") == original
@pytest.mark.parametrize("prefix", [b"", b"\xef\xbb\xbf"])
def test_configure_and_remove_preserve_windows_line_endings_and_bom(
tmp_path: Path, prefix: bytes
) -> None:
path = tmp_path / "settings.json"
original = prefix + b'{\r\n\t"editor.fontSize": 15\r\n}\r\n'
path.write_bytes(original)
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
assert path.read_bytes().startswith(prefix + b"{\r\n")
assert remove_vscode_proxy_settings(path) is True
assert path.read_bytes() == original
def test_configure_refuses_duplicate_managed_markers(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
original = (
"{\n"
"// --- Headroom Copilot proxy ---\n"
"// --- end Headroom Copilot proxy ---\n"
"// --- Headroom Copilot proxy ---\n"
"// --- end Headroom Copilot proxy ---\n"
"}\n"
)
path.write_text(original, encoding="utf-8")
with pytest.raises(click.ClickException, match="marker block"):
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
assert path.read_text(encoding="utf-8") == original