Commit Graph
2 Commits
Author SHA1 Message Date
danieldutuandJerrettDavis 043fe09d8c fix(copilot): detect COPILOT_API_URL in cli-main.js for Copilot CLI >= 1.0.93 (#4450)
## 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>
2026-10-24 08:37:51 -07:00
JD Davis 997a47992c fix(copilot): preserve native enterprise model routing (#2998)
## Description

GitHub Copilot Enterprise/Business users without a BYOK provider key
were routed through Copilot CLI's single-model provider override. Native
model aliases and runtime `/model` switches were therefore forwarded
literally to the override and rejected with `400 model not supported`.
This change routes implicit GitHub OAuth through Copilot's native API
surface while retaining explicit subscription and provider-key behavior.

Closes #1910

## 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

- Added explicit `--native` routing and made it automatic for implicit
GitHub OAuth without BYOK.
- Clears every Copilot BYOK variable before native launch.
- Routes both OpenAI and Anthropic protocol targets through the resolved
tenant Copilot host.
- Preserves Enterprise/Business native aliases and runtime model
switching.
- Rejects BYOK-only options when native routing is selected.
- Refuses known Copilot bundles that do not reference `COPILOT_API_URL`,
avoiding silent proxy bypass.
- Preserves explicit `--subscription` and provider-key BYOK semantics.
- Added coverage for unreadable and unverifiable Copilot CLI bundles.

## 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
884 passed, 4 skipped in 103.11s
ruff check .: All checks passed
ruff format --check .: 1412 files already formatted
mypy headroom/providers/copilot/wrap.py headroom/cli/wrap.py:
Success: no issues found in 2 source files
```

Exact-head CI is entirely green on
`0aca48c096485cb7825aa2239d27db7783962f9d`.

## Real Behavior Proof

- Environment: macOS arm64/Python 3.13 locally; GitHub-hosted macOS and
Ubuntu native-wrap jobs.
- Exact command / steps: invoke `headroom wrap copilot` with implicit
OAuth and an Enterprise model alias; inspect the captured child/proxy
environment and resolved target URLs; exercise explicit native conflicts
and bundle-support probes.
- Observed result: native launch uses `COPILOT_API_URL`, clears all BYOK
state, and points both protocol targets at the tenant host. Native-wrap
jobs are green on macOS and Ubuntu for the refreshed head.
- Not tested: live request against a real Enterprise tenant; the
repository has no organization Enterprise credential available to CI.

## Runtime Rollout Safety

- Rollout-managed feature(s): implicit native Copilot routing for GitHub
OAuth sessions without BYOK.
- Minimum rollout channel: normal patch release.
- Stable/default behavior changed: implicit OAuth now uses native
routing; explicit subscription and BYOK paths are unchanged.
- Kill switch / disable path: use an explicit supported provider-key
BYOK configuration; native mode also fails closed when CLI support is
known absent.
- Unsafe override required: none.
- Qualification impact: native-wrap macOS/Ubuntu, Docker wrapper, full
Python matrix, and Copilot focused suites must pass.
- Rollback path: human revert of this PR restores the fixed-wire OAuth
behavior; no configuration migration is persisted.

## 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 — CLI help
and inline routing documentation; no separate guide required
- [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` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

Not applicable; CLI routing change.

## Additional Notes

Human review only. No merge or auto-merge is configured. Refreshed from
main after #2996; the MCP cap `mcp>=1.28.1,<2.0.0` is preserved.
2026-08-25 21:46:31 -05:00