Files
headroom/tests/test_cli/test_wrap_vscode_claude.py
Rod BoevandJerrettDavis fc5a09e1b3 feat(wrap): support 1m context in vscode claude (#3361)
## Description

`headroom wrap vscode-claude` now supports an opt-in `--1m` path. It
stores Claude Code's `[1m]` model selector in the existing top-level
`model` setting and records the prior value in the reversible settings
sidecar. Reconfiguration remains idempotent, malformed sidecars fail
with a clear error, and user changes to Headroom-managed values remain
protected.

The branch also pins npm 11.16.0 in the shared Docker wrap test image.
npm 10.9.8 crashed while installing the local OpenClaw plugin with
`Cannot read properties of null (reading 'edgesOut')`.

Closes #3360

## Type of Change

- [ ] 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)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Added `--1m` to `headroom wrap vscode-claude`.
- Reused one Claude-specific resolver for standalone and VS Code model
selection.
- Extended the reversible VS Code sidecar to preserve, validate, and
restore the top-level model.
- Refreshes fallback-selected models when `HEADROOM_1M_MODEL` changes
between repeated `--1m` runs.
- Preserved exact prior values and refuses to overwrite user edits.
- Added read-only `--no-configure --1m` output for manual settings
setup.
- Added focused tests for model persistence, fallback refresh, malformed
state, conflict handling, and restoration.
- Pinned npm 11.16.0 in `e2e/wrap/Dockerfile` for both Docker wrap jobs.
- Updated the VS Code Claude documentation.

## Testing

- [x] Focused unit and integration tests pass
- [x] Linting and formatting checks pass for changed Python files
- [ ] Type checking passes
- [x] New tests added for the changed behavior
- [ ] Manual testing performed

### Test Output

```text
uv run pytest tests/test_cli/test_wrap_vscode_claude.py tests/test_provider_claude.py tests/test_provider_claude_vscode_config.py -q
48 passed in 1.15s

uv run ruff check headroom/providers/claude/vscode.py headroom/cli/wrap.py tests/test_provider_claude_vscode_config.py
All checks passed!

uv run ruff format --check headroom/providers/claude/vscode.py headroom/cli/wrap.py tests/test_provider_claude_vscode_config.py
3 files already formatted
```

## Real Behavior Proof

- Environment: Windows, Python development environment, temporary Claude
settings file.
- Exact command / steps: run `headroom wrap vscode-claude --1m
--settings-file <temporary-settings.json>`, inspect the settings and
sidecar, then run `headroom unwrap vscode-claude --settings-file
<temporary-settings.json>`.
- Observed result: the `[1m]` model persisted, the sidecar recorded the
prior model, and unwrap restored the exact prior value. Repeated `--1m`
runs picked up a changed `HEADROOM_1M_MODEL` fallback when no user model
existed.
- GitHub CI result: `docker-wrap-e2e`, `docker-native-e2e`, and all
other required checks passed after the npm pin.
- Not tested: live Claude account entitlement for the 1M context window.

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: next release containing the VS Code Claude
wrapper change.
- Stable/default behavior changed: `--1m` remains opt-in; default VS
Code Claude behavior is unchanged.
- Kill switch / disable path: omit `--1m`, run `headroom unwrap
vscode-claude`, or revert the change before release.
- Unsafe override required: no.
- Qualification impact: Claude account entitlement still controls access
to the 1M context window.
- Rollback path: run `headroom unwrap vscode-claude`, reload VS Code,
and revert the release if needed.

## 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
- [ ] 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 the feature works
- [x] Focused tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`; release-please generates it
from the conventional PR title.

## Additional Notes

Claude Code and Anthropic control model support and account eligibility.
Local tests prove persisted configuration and restoration, not access to
a live 1M context window.

---------

Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-09-24 23:50:27 -05:00

169 lines
6.3 KiB
Python

"""CLI coverage for Claude Code inside VS Code."""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from click.testing import CliRunner
from headroom.cli.main import main
def test_wrap_vscode_claude_configures_actual_port(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
captured = {}
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
captured.update(kwargs)
kwargs["print_setup_lines"](9999)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(main, ["wrap", "vscode-claude", "--settings-file", str(path)])
assert result.exit_code == 0, result.output
env = json.loads(path.read_text(encoding="utf-8"))["env"]
assert env["ANTHROPIC_BASE_URL"].startswith("http://127.0.0.1:9999/p/")
assert env["ENABLE_TOOL_SEARCH"] == "false"
assert "Reload VS Code" in result.output
assert captured["agent_type"] == "claude"
def test_wrap_vscode_claude_help_exposes_1m() -> None:
result = CliRunner().invoke(main, ["wrap", "vscode-claude", "--help"])
assert result.exit_code == 0, result.output
assert "--1m" in result.output
def test_wrap_vscode_claude_no_configure_prints_settings(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(
main,
["wrap", "vscode-claude", "--no-configure", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
assert not path.exists()
assert "ANTHROPIC_BASE_URL" in result.output
assert "ENABLE_TOOL_SEARCH" in result.output
def test_wrap_vscode_claude_1m_persists_model(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
path.write_text(
json.dumps({"model": "claude-sonnet-5", "custom": {"keep": True}}), encoding="utf-8"
)
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(
main,
["wrap", "vscode-claude", "--1m", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
configured = json.loads(path.read_text(encoding="utf-8"))
assert configured["model"] == "claude-sonnet-5[1m]"
assert configured["custom"] == {"keep": True}
assert "1M context model persisted: claude-sonnet-5[1m]" in result.output
def test_wrap_vscode_claude_default_leaves_model_unchanged(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
path.write_text(
json.dumps({"model": "claude-sonnet-5", "custom": {"keep": True}}), encoding="utf-8"
)
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(
main,
["wrap", "vscode-claude", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
assert json.loads(path.read_text(encoding="utf-8"))["model"] == "claude-sonnet-5"
assert "1M context model persisted:" not in result.output
def test_wrap_vscode_claude_no_configure_1m_is_write_free(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
original = '{"model":"claude-opus-5","custom":{"keep":true}}'
path.write_text(original, encoding="utf-8")
state_path = tmp_path / ".headroom-vscode-claude.json"
state_path.write_text('{"sentinel":true}', encoding="utf-8")
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(
main,
["wrap", "vscode-claude", "--no-configure", "--1m", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
assert '"model": "claude-opus-5[1m]"' in result.output
assert (
' "ENABLE_TOOL_SEARCH": "true"\n'
f" Add this top-level setting to {path}:\n"
' "model": "claude-opus-5[1m]"'
) in result.output
assert path.read_text(encoding="utf-8") == original
assert state_path.read_text(encoding="utf-8") == '{"sentinel":true}'
@pytest.mark.parametrize("contents", ["{broken", '{"model":{"name":"opus"}}'])
def test_wrap_vscode_claude_no_configure_1m_falls_back_for_invalid_settings(
tmp_path: Path, contents: str
) -> None:
path = tmp_path / "settings.json"
path.write_text(contents, encoding="utf-8")
state_path = tmp_path / ".headroom-vscode-claude.json"
state_path.write_text('{"sentinel":true}', encoding="utf-8")
def fake_watcher(**kwargs): # noqa: ANN003, ANN202
kwargs["print_setup_lines"](8787)
with patch("headroom.cli.wrap._run_proxy_only_watcher", side_effect=fake_watcher):
result = CliRunner().invoke(
main,
["wrap", "vscode-claude", "--no-configure", "--1m", "--settings-file", str(path)],
)
assert result.exit_code == 0, result.output
assert (
' "ENABLE_TOOL_SEARCH": "true"\n'
f" Add this top-level setting to {path}:\n"
' "model": "claude-opus-5[1m]"'
) in result.output
assert ' "ENABLE_TOOL_SEARCH": "true",\n' not in result.output
assert path.read_text(encoding="utf-8") == contents
assert state_path.read_text(encoding="utf-8") == '{"sentinel":true}'
def test_unwrap_vscode_claude_restores_previous_settings(tmp_path: Path) -> None:
path = tmp_path / "settings.json"
original = {"env": {"KEEP": "1"}, "permissions": {"allow": ["Read"]}}
path.write_text(json.dumps(original), encoding="utf-8")
from headroom.providers.claude.vscode import configure_vscode_claude_settings
configure_vscode_claude_settings(path, "http://127.0.0.1:8787/p/demo")
result = CliRunner().invoke(main, ["unwrap", "vscode-claude", "--settings-file", str(path)])
assert result.exit_code == 0, result.output
assert json.loads(path.read_text(encoding="utf-8")) == original