mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 16:45:34 +08:00
## Description `headroom wrap copilot --native` exited with `This Copilot CLI build does not reference COPILOT_API_URL; refusing a native launch that could silently bypass Headroom.` on Copilot CLI 1.0.93, 1.0.101 and 1.0.102. The launch worked on 1.0.83. `native_api_url_supported` read only `app.js` (`headroom/providers/copilot/wrap.py:211`). From 1.0.93, `app.js` is a 44 KB loader, and the `process.env.COPILOT_API_URL` read sits in `cli-main.js`. The probe found `app.js`, found no match, returned False, and the launch was refused. The probe now scans `app.js` and `cli-main.js`. A directory with either file counts as a bundle. The probe returns False when no bundle references the variable, and None when no bundle exists. The caller in `headroom/cli/wrap.py` is unchanged. @Kvas1r reported this with a table of four Copilot CLI versions and posted a diff. I started from that diff and changed it so a directory with `cli-main.js` and no `app.js` also counts as a bundle. Closes #4432 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `headroom/providers/copilot/wrap.py`: `native_api_url_supported` scans `app.js` and `cli-main.js` in each package directory. The fixed file list stays; scanning every top-level `*.js` file would read more files on every launch. Question for the maintainers: do you want that instead? - `tests/test_copilot_native_mode.py`: added `test_native_support_probe_scans_cli_main_loader_layout`. - Unchanged by choice: the probe still passes when any installed version directory has the string, and the 1 MiB chunked read can still miss a string that spans a chunk boundary. I will split or drop either on request. ## Testing - [x] Unit tests pass (`pytest`) — `tests/test_copilot_native_mode.py` only - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) — ran `mypy` on `headroom/providers/copilot/wrap.py` only - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ uv run pytest tests/test_copilot_native_mode.py -q 11 passed in 3.62s # same file, pre-fix wrap.py (git checkout HEAD~1 -- headroom/providers/copilot/wrap.py) FAILED tests/test_copilot_native_mode.py::test_native_support_probe_scans_cli_main_loader_layout 1 failed, 10 passed in 1.98s $ uv run ruff check . All checks passed! $ uv run ruff format --check . 1812 files already formatted $ uv run mypy headroom/providers/copilot/wrap.py Success: no issues found in 1 source file ``` ## Real Behavior Proof - Environment: macOS, Apple Silicon, Python 3.12.13. Copilot CLI is not installed. I built a fake 1.0.102 tree at `copilot/pkg/darwin-arm64/1.0.102/` with `app.js` = `loader only` and `cli-main.js` = `let g=process.env.COPILOT_API_URL`. I loaded `native_api_url_supported` from the file with `click` and `with_project_prefix` stubbed. - Exact command / steps: `python3 -I probe.py <wrap.py> <fake home>`, where `probe.py` prints `native_api_url_supported(environ={"LOCALAPPDATA": <fake home>})`. I ran it against `wrap.py` at `HEAD~1` and at this commit, and again after rewriting the fixture files. - Observed result: `app.js` loader + `cli-main.js` with the string: `False` at `HEAD~1`, `True` at this commit. `cli-main.js` without the string: `False`. `cli-main.js` with the string and no `app.js`: `True`. - Not tested: a live `headroom wrap copilot --native` run, proxy traffic, Windows, Linux. @Kvas1r reported that with the same change, 1.0.102 on Windows 11 launched natively and the proxy log showed `GET /models` and `POST /responses` with status 200. ## Runtime Rollout Safety - Rollout-managed feature(s): none; the change touches only the `wrap copilot --native` support probe. - Minimum rollout channel: n/a, no rollout-managed feature. - Stable/default behavior changed: yes for Copilot CLI >= 1.0.93, where the probe now returns True instead of False. No change for builds with the string in `app.js`. - Kill switch / disable path: n/a, no flag; omit `--native` to skip the probe. - Unsafe override required: no. - Qualification impact: none; no model or provider qualification input changes. - Rollback path: revert this commit. ## Review Readiness - [x] I have performed a self-review - [ ] 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 — no documentation covers the probe's file list - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes — ran `tests/test_copilot_native_mode.py` only - [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) n/a, no UI change. ## Additional Notes Branch has one commit. The title is `fix(copilot): detect COPILOT_API_URL in cli-main.js for Copilot CLI >= 1.0.93`. Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
161 lines
6.3 KiB
Python
161 lines
6.3 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from headroom.providers.copilot.wrap import (
|
|
COPILOT_BYOK_ENV_VARS,
|
|
COPILOT_NATIVE_API_URL_ENV,
|
|
build_launch_env,
|
|
build_native_launch_env,
|
|
native_api_url_supported,
|
|
)
|
|
|
|
|
|
def test_native_env_redirects_api_and_clears_all_byok_state() -> None:
|
|
seeded = dict.fromkeys(COPILOT_BYOK_ENV_VARS, "stale")
|
|
seeded["UNRELATED"] = "preserved"
|
|
env, _ = build_native_launch_env(port=8890, environ=seeded, project="repo name")
|
|
|
|
assert env[COPILOT_NATIVE_API_URL_ENV] == "http://127.0.0.1:8890/p/repo%20name"
|
|
assert env["UNRELATED"] == "preserved"
|
|
assert not any(variable in env for variable in COPILOT_BYOK_ENV_VARS)
|
|
|
|
|
|
def test_byok_builder_remains_disjoint_from_native_mode() -> None:
|
|
env, _ = build_launch_env(
|
|
port=8787,
|
|
provider_type="openai",
|
|
wire_api="responses",
|
|
environ={},
|
|
)
|
|
assert env["COPILOT_PROVIDER_BASE_URL"] == "http://127.0.0.1:8787/v1"
|
|
assert env["COPILOT_PROVIDER_WIRE_API"] == "responses"
|
|
assert COPILOT_NATIVE_API_URL_ENV not in env
|
|
|
|
|
|
def test_native_support_probe_distinguishes_unknown_and_unsupported(tmp_path) -> None:
|
|
local = tmp_path / "local"
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is None
|
|
|
|
bundle = local / "copilot" / "pkg" / "platform" / "1.0" / "app.js"
|
|
bundle.parent.mkdir(parents=True)
|
|
bundle.write_text("no override here", encoding="utf-8")
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is False
|
|
|
|
bundle.write_text("process.env.COPILOT_API_URL", encoding="utf-8")
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is True
|
|
|
|
|
|
def test_native_support_probe_scans_cli_main_loader_layout(tmp_path) -> None:
|
|
local = tmp_path / "local"
|
|
version = local / "copilot" / "pkg" / "platform" / "1.0.102"
|
|
version.mkdir(parents=True)
|
|
(version / "app.js").write_text("loader only", encoding="utf-8")
|
|
(version / "cli-main.js").write_text("no override here", encoding="utf-8")
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is False
|
|
|
|
(version / "cli-main.js").write_text("process.env.COPILOT_API_URL", encoding="utf-8")
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is True
|
|
|
|
|
|
def test_native_support_probe_skips_unreadable_bundle(monkeypatch, tmp_path) -> None:
|
|
local = tmp_path / "local"
|
|
bundle = local / "copilot" / "pkg" / "platform" / "1.0" / "app.js"
|
|
bundle.parent.mkdir(parents=True)
|
|
bundle.write_text("process.env.COPILOT_API_URL", encoding="utf-8")
|
|
|
|
def _unreadable(*_args, **_kwargs):
|
|
raise OSError("synthetic unreadable bundle")
|
|
|
|
monkeypatch.setattr("builtins.open", _unreadable)
|
|
assert native_api_url_supported(environ={"LOCALAPPDATA": str(local)}) is False
|
|
|
|
|
|
def _invoke_native(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
extra: list[str] | None = None,
|
|
*,
|
|
support: bool | None = True,
|
|
):
|
|
from headroom.cli import wrap as wrap_mod
|
|
from headroom.cli.main import main
|
|
|
|
captured: dict[str, object] = {}
|
|
|
|
class Resolution:
|
|
token = "copilot-token"
|
|
api_url = "https://api.business.githubcopilot.com"
|
|
refresh_oauth_token = "refresh-token"
|
|
api_token_expires_at = 123.0
|
|
|
|
monkeypatch.setattr(wrap_mod.shutil, "which", lambda _name: "/usr/bin/copilot")
|
|
monkeypatch.setattr(wrap_mod, "_check_proxy", lambda _port: False)
|
|
monkeypatch.setattr(wrap_mod, "_require_copilot_subscription_resolution", lambda: Resolution())
|
|
monkeypatch.setattr(wrap_mod, "_native_api_url_supported", lambda **_kwargs: support)
|
|
monkeypatch.setattr(wrap_mod, "_launch_tool", lambda **kwargs: captured.update(kwargs))
|
|
result = CliRunner().invoke(
|
|
main,
|
|
["wrap", "copilot", "--native", "--port", "8890", *(extra or [])],
|
|
)
|
|
return result, captured
|
|
|
|
|
|
def test_implicit_oauth_uses_native_routing_without_flag(monkeypatch) -> None:
|
|
from headroom.cli import wrap as wrap_mod
|
|
from headroom.cli.main import main
|
|
|
|
captured: dict[str, object] = {}
|
|
monkeypatch.setattr(wrap_mod.shutil, "which", lambda _name: "/usr/bin/copilot")
|
|
monkeypatch.setattr(wrap_mod, "_check_proxy", lambda _port: False)
|
|
monkeypatch.setattr(wrap_mod, "has_oauth_auth", lambda: True)
|
|
monkeypatch.setattr(wrap_mod, "resolve_client_bearer_token", lambda: "oauth-token")
|
|
monkeypatch.setattr(
|
|
wrap_mod, "resolve_copilot_api_url", lambda _token: "https://api.githubcopilot.com"
|
|
)
|
|
monkeypatch.setattr(wrap_mod, "_native_api_url_supported", lambda **_kwargs: True)
|
|
monkeypatch.setattr(wrap_mod, "_launch_tool", lambda **kwargs: captured.update(kwargs))
|
|
|
|
result = CliRunner().invoke(
|
|
main,
|
|
["wrap", "copilot", "--port", "8890", "--", "--model", "claude-sonnet-5"],
|
|
)
|
|
|
|
assert result.exit_code == 0, result.output
|
|
env = captured["env"]
|
|
assert isinstance(env, dict)
|
|
assert COPILOT_NATIVE_API_URL_ENV in env
|
|
assert not any(variable in env for variable in COPILOT_BYOK_ENV_VARS)
|
|
|
|
|
|
def test_native_cli_routes_both_protocols_to_tenant_host(monkeypatch) -> None:
|
|
result, captured = _invoke_native(monkeypatch)
|
|
assert result.exit_code == 0, result.output
|
|
assert captured["openai_api_url"] == "https://api.business.githubcopilot.com"
|
|
assert captured["anthropic_api_url"] == "https://api.business.githubcopilot.com"
|
|
env = captured["env"]
|
|
assert isinstance(env, dict)
|
|
assert COPILOT_NATIVE_API_URL_ENV in env
|
|
assert not any(variable in env for variable in COPILOT_BYOK_ENV_VARS)
|
|
|
|
|
|
@pytest.mark.parametrize("extra", [["--wire-api", "responses"], ["--provider-type", "anthropic"]])
|
|
def test_native_cli_rejects_byok_only_options(monkeypatch, extra) -> None:
|
|
result, captured = _invoke_native(monkeypatch, extra)
|
|
assert result.exit_code != 0
|
|
assert not captured
|
|
|
|
|
|
def test_native_cli_refuses_known_unsupported_bundle(monkeypatch) -> None:
|
|
result, captured = _invoke_native(monkeypatch, support=False)
|
|
assert result.exit_code != 0
|
|
assert "COPILOT_API_URL" in result.output
|
|
assert not captured
|
|
|
|
|
|
def test_native_cli_reports_unknown_support_in_verbose_mode(monkeypatch) -> None:
|
|
result, captured = _invoke_native(monkeypatch, ["--verbose"], support=None)
|
|
assert result.exit_code == 0, result.output
|
|
assert "could not verify" in result.output
|
|
assert captured
|