mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-25 03:56:10 +08:00
## Description
VS Code can run a Copilot chat on its "Copilot harness" (Agent Host, the
bundled Copilot SDK runtime) instead of the Local harness. That runtime
ignores `github.copilot.advanced.debug.overrideCapiUrl` /
`overrideProxyUrl`, so the chat goes straight to GitHub. Copilot's small
background calls (title, intent) still reach the proxy, so the proxy
looks connected while the main conversation never arrives. The harness
default (`chat.defaultToCopilotHarness`,
`chat.editor.preferCopilotHarness`) is tagged `experiment: startup` in
VS Code, so it can be flipped per user by rollout.
`headroom wrap vscode` now pins new chats to the Local harness.
## 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/vscode.py`: the managed block also writes
`"chat.defaultToCopilotHarness": false` and
`"chat.editor.preferCopilotHarness": false`. Because they sit inside the
marker block, `headroom unwrap vscode` removes them. A harness setting
the user already set outside the block is left alone (no duplicate key).
New helper `copilot_harness_settings_enabled(path)` reports a user value
of `true`.
- `headroom/cli/wrap.py`: one line saying new chats stay on the Local
harness and existing chats keep their engine; a warning instead when the
user's own setting is `true`. `--no-configure` prints the two extra
settings.
- `docs/content/docs/vscode-copilot.mdx`: documents the harness settings
and the troubleshooting case.
- Blocks written by older versions (including ones with
`overrideAuthType`) are replaced by marker as before.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
tests/test_provider_copilot_vscode_config.py + tests/test_cli/test_wrap_vscode.py: 32 passed.
Against origin/main source: 7 of those fail (block contents, old-block upgrade, user-set key [True]/[False], wrap output, --no-configure output).
ruff 0.17.4 check + format --check on the 5 changed Python files: clean.
mypy on the changed source files: 8 errors, all in headroom/cache/prefix_tracker.py and headroom/ccr/mcp_server.py, identical on origin/main; none in changed files.
```
## Real Behavior Proof
- Environment: macOS 27.2.1, Python 3.12.13, VS Code 1.145.2 with GitHub
Copilot signed in, core run from this branch, real GitHub Copilot
upstream. Real user `settings.json` (backed up and restored afterwards).
- Exact command / steps:
1. `headroom wrap vscode -p 8795` from this branch.
2. `code chat -n -m agent "Reply with the single word OK and nothing
else."` (opens a new VS Code window and sends one Agent-mode chat).
3. Read the proxy's `/stats`, VS Code's `agenthost.log`, then `headroom
unwrap vscode`.
- Observed result:
```text
VS Code Copilot proxy settings updated: ~/Library/Application
Support/Code/User/settings.json
Keep using Copilot's normal model picker; the selected model is
preserved.
New chats stay on VS Code's Local harness; a chat keeps the engine it
started with, so start a new chat.
settings.json after wrap:
{
// --- Headroom Copilot proxy ---
"github.copilot.advanced.debug.overrideProxyUrl":
"http://127.0.0.1:8795/p/win-local",
"github.copilot.advanced.debug.overrideCapiUrl":
"http://127.0.0.1:8795/p/win-local",
"chat.defaultToCopilotHarness": false,
"chat.editor.preferCopilotHarness": false
// --- end Headroom Copilot proxy ---
}
proxy requests before chat: 0 {}
proxy requests after one Agent-mode chat in a new VS Code window: 4
{'gpt-4o-mini-2024-07-18': 3, 'claude-opus-5.5': 1}
agent host turns during the chat: 0
Removed Headroom Copilot proxy settings from ~/Library/Application
Support/Code/User/settings.json
settings.json after unwrap:
{
}
```
Separately, on the same machine with the settings edited by hand (same
VS Code, same proxy, new window each time):
`"chat.defaultToCopilotHarness": true` -> the Agent-mode chat ran in the
Agent Host (provider `copilotcli`) and 0 requests reached the proxy;
`"chat.defaultToCopilotHarness": false` +
`"chat.editor.preferCopilotHarness": false` -> Local harness, 4 requests
reached the proxy including the main `/v1/messages` call.
`"chat.temporaryDefaultToLocalHarness": true` as a user setting had no
effect, so it is not written.
- Not tested: a VS Code where Microsoft's rollout has flipped the
default (the explicit user value is expected to win, but that could not
be reproduced); an organization that enforces managed sandbox settings,
where VS Code can require the Copilot harness regardless; VS Code
profiles, workspace-level settings, Insiders/VSCodium; Windows and
Linux; the persistent `headroom start --persistent --vscode` path in the
Enterprise client.
## Runtime Rollout Safety
- Rollout-managed feature(s): none.
- Minimum rollout channel: n/a.
- Stable/default behavior changed: `headroom wrap vscode` writes two
more VS Code user settings inside its managed block. No proxy behaviour
changes.
- Kill switch / disable path: `headroom unwrap vscode` removes them; a
user who sets either key themselves keeps their value.
- Unsafe override required: no.
- Qualification impact: none expected.
- Rollback path: revert this commit; `unwrap` removes blocks of either
format.
## 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 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)
## Additional Notes
- This keeps users off the Copilot harness; it does not route that
harness through Headroom. Supporting it is separate work.
- Only the configured settings file is checked for a user value. A
`true` in workspace settings, another profile, or policy is not
detected.
- After `unwrap` the keys return to VS Code's default, which a rollout
may move to the Copilot harness.
- Existing chats keep the harness they started with; the wrap output
says to start a new chat.
- `e2e/copilot_live.py` and `e2e/wrap/run.py` were not run.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4899"
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 Fable 5.2 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
283 lines
11 KiB
Python
283 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import click
|
|
import pytest
|
|
|
|
from headroom.providers.copilot.vscode import (
|
|
configure_vscode_proxy_settings,
|
|
copilot_harness_settings_enabled,
|
|
remove_vscode_proxy_settings,
|
|
unrouted_vscode_profiles,
|
|
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.overrideCapiUrl"' in configured
|
|
# `overrideAuthType` is deliberately absent: no such setting exists in the
|
|
# modern Copilot Chat extension, and writing one made VS Code flag an
|
|
# unknown key while doing nothing (#3076).
|
|
assert "overrideAuthType" not 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 _live_settings(path: Path) -> dict[str, object]:
|
|
"""Parse the written JSONC the way VS Code does: comments and trailing commas ignored."""
|
|
lines = path.read_text(encoding="utf-8").splitlines()
|
|
body = "\n".join(line for line in lines if not line.strip().startswith("//"))
|
|
return json.loads(body.replace(",\n}", "\n}"))
|
|
|
|
|
|
def test_configure_keeps_new_chats_on_the_local_harness(tmp_path: Path) -> None:
|
|
path = tmp_path / "settings.json"
|
|
original = '{\n\t"editor.fontSize": 15\n}\n'
|
|
path.write_text(original, encoding="utf-8")
|
|
|
|
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
|
|
|
|
# A chat on the Copilot harness ignores the endpoint overrides, and VS Code
|
|
# can flip either default by rollout, so both are pinned explicitly.
|
|
settings = _live_settings(path)
|
|
assert settings["chat.defaultToCopilotHarness"] is False
|
|
assert settings["chat.editor.preferCopilotHarness"] is False
|
|
assert copilot_harness_settings_enabled(path) == []
|
|
assert remove_vscode_proxy_settings(path) is True
|
|
assert path.read_text(encoding="utf-8") == original
|
|
|
|
|
|
def test_configure_upgrades_a_block_written_before_the_harness_settings(tmp_path: Path) -> None:
|
|
path = tmp_path / "settings.json"
|
|
original = '{\n\t"editor.fontSize": 15\n}\n'
|
|
path.write_text(
|
|
'{\n\t"editor.fontSize": 15,\n'
|
|
"\t// --- Headroom Copilot proxy --- (comma-added)\n"
|
|
'\t"github.copilot.advanced.debug.overrideProxyUrl": "http://127.0.0.1:8787",\n'
|
|
'\t"github.copilot.advanced.debug.overrideAuthType": "token",\n'
|
|
'\t"github.copilot.advanced.debug.overrideCapiUrl": "http://127.0.0.1:8787"\n'
|
|
"\t// --- end Headroom Copilot proxy ---\n}\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert configure_vscode_proxy_settings(path, "http://127.0.0.1:8787") == "updated"
|
|
|
|
settings = _live_settings(path)
|
|
assert settings["chat.defaultToCopilotHarness"] is False
|
|
assert settings["chat.editor.preferCopilotHarness"] is False
|
|
assert "github.copilot.advanced.debug.overrideAuthType" not in settings
|
|
assert remove_vscode_proxy_settings(path) is True
|
|
assert path.read_text(encoding="utf-8") == original
|
|
|
|
|
|
@pytest.mark.parametrize("user_value", [True, False])
|
|
def test_configure_leaves_a_harness_setting_the_user_already_chose(
|
|
tmp_path: Path, user_value: bool
|
|
) -> None:
|
|
path = tmp_path / "settings.json"
|
|
original = f'{{\n\t"chat.defaultToCopilotHarness": {json.dumps(user_value)},\n}}\n'
|
|
path.write_text(original, encoding="utf-8")
|
|
|
|
for _ in range(2): # a re-run must not start writing the key either
|
|
configure_vscode_proxy_settings(path, "http://127.0.0.1:8787")
|
|
configured = path.read_text(encoding="utf-8")
|
|
# A second copy inside the block would win silently: JSONC keeps the last one.
|
|
assert configured.count('"chat.defaultToCopilotHarness"') == 1
|
|
settings = _live_settings(path)
|
|
assert settings["chat.defaultToCopilotHarness"] is user_value
|
|
assert settings["chat.editor.preferCopilotHarness"] is False
|
|
assert copilot_harness_settings_enabled(path) == (
|
|
["chat.defaultToCopilotHarness"] if user_value else []
|
|
)
|
|
|
|
assert remove_vscode_proxy_settings(path) is True
|
|
assert path.read_text(encoding="utf-8") == original
|
|
|
|
|
|
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"}',
|
|
'{"github.copilot.advanced.debug.overrideCapiUrl":"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
|
|
|
|
|
|
def _write_profiles(tmp_path: Path, state: object) -> Path:
|
|
user_dir = tmp_path / "User"
|
|
storage = user_dir / "globalStorage" / "storage.json"
|
|
storage.parent.mkdir(parents=True)
|
|
storage.write_text(state if isinstance(state, str) else json.dumps(state), encoding="utf-8")
|
|
return user_dir
|
|
|
|
|
|
def test_unrouted_profiles_lists_only_profiles_with_their_own_settings(tmp_path: Path) -> None:
|
|
user_dir = _write_profiles(
|
|
tmp_path,
|
|
{
|
|
"userDataProfiles": [
|
|
{"location": "6c87cdb4", "name": "Work"},
|
|
{"location": "-1f2e3d", "name": "Negative hash"},
|
|
{"location": "aa11", "name": "Shared", "useDefaultFlags": {"settings": True}},
|
|
{"location": "../escape", "name": "Traversal"},
|
|
{"location": {"scheme": "vscode-remote", "path": "/p"}, "name": "Remote"},
|
|
"not-a-profile",
|
|
]
|
|
},
|
|
)
|
|
|
|
assert unrouted_vscode_profiles(user_dir) == [
|
|
("Work", user_dir / "profiles" / "6c87cdb4" / "settings.json"),
|
|
("Negative hash", user_dir / "profiles" / "-1f2e3d" / "settings.json"),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("managed", [True, False])
|
|
def test_unrouted_profiles_skips_a_profile_that_overrides_both_endpoints(
|
|
tmp_path: Path, managed: bool
|
|
) -> None:
|
|
user_dir = _write_profiles(
|
|
tmp_path, {"userDataProfiles": [{"location": "6c87cdb4", "name": "Work"}]}
|
|
)
|
|
profile_settings = user_dir / "profiles" / "6c87cdb4" / "settings.json"
|
|
profile_settings.parent.mkdir(parents=True)
|
|
if managed:
|
|
configure_vscode_proxy_settings(profile_settings, "http://127.0.0.1:8787")
|
|
else:
|
|
profile_settings.write_text(
|
|
"""{
|
|
"github.copilot.advanced.debug.overrideProxyUrl": "http://127.0.0.1:8787",
|
|
"github.copilot.advanced.debug.overrideCapiUrl": "http://127.0.0.1:8787",
|
|
}
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert unrouted_vscode_profiles(user_dir) == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"settings",
|
|
[
|
|
# The marker is itself a comment, so on its own it proves nothing.
|
|
"""{
|
|
// --- Headroom Copilot proxy ---
|
|
}
|
|
""",
|
|
"""{
|
|
// --- Headroom Copilot proxy ---
|
|
// "github.copilot.advanced.debug.overrideProxyUrl": "http://127.0.0.1:8787",
|
|
// "github.copilot.advanced.debug.overrideCapiUrl": "http://127.0.0.1:8787"
|
|
// --- end Headroom Copilot proxy ---
|
|
}
|
|
""",
|
|
"""{
|
|
"note": "// --- Headroom Copilot proxy ---",
|
|
"github.copilot.advanced.debug.overrideCapiUrl": "http://127.0.0.1:8787"
|
|
}
|
|
""",
|
|
'{ "github.copilot.advanced.debug.overrideCapiUrl": ',
|
|
],
|
|
ids=["marker-only", "commented-out-keys", "one-key", "unparseable"],
|
|
)
|
|
def test_unrouted_profiles_counts_only_live_override_settings(
|
|
tmp_path: Path, settings: str
|
|
) -> None:
|
|
user_dir = _write_profiles(
|
|
tmp_path, {"userDataProfiles": [{"location": "6c87cdb4", "name": "Work"}]}
|
|
)
|
|
profile_settings = user_dir / "profiles" / "6c87cdb4" / "settings.json"
|
|
profile_settings.parent.mkdir(parents=True)
|
|
profile_settings.write_text(settings, encoding="utf-8")
|
|
|
|
assert unrouted_vscode_profiles(user_dir) == [("Work", profile_settings)]
|
|
|
|
|
|
@pytest.mark.parametrize("state", ["{not json", [], {"userDataProfiles": {"a": 1}}, {}])
|
|
def test_unrouted_profiles_tolerates_unexpected_vscode_state(tmp_path: Path, state: object) -> None:
|
|
assert unrouted_vscode_profiles(_write_profiles(tmp_path, state)) == []
|
|
|
|
|
|
def test_unrouted_profiles_without_vscode_state(tmp_path: Path) -> None:
|
|
assert unrouted_vscode_profiles(tmp_path) == []
|