headroom/tests/test_cli/test_init_enable_tool_search.py
JD Davis 1aa701adaa
fix(vscode): persist compatible Claude modes and route Copilot CAPI (#2986)
## Description

Fixes #2492, #2028, and #2827.

Claude daemon workers consume project settings rather than reliably
inheriting wrapper environment state, while the Claude VS Code webview
cannot render deferred-tool response blocks. Separately, recent Copilot
Chat versions use the whole CAPI override for generation; the legacy
proxy override alone only sends model discovery through Headroom.

This PR carries both integrations through to the actual consumers
instead of only changing their launch-time surface configuration.

## Type of Change

- [x] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
- [ ] Build / CI

## Changes Made

- Persist the resolved Claude ENABLE_TOOL_SEARCH value into project
settings for daemon workers and restore it transactionally after wrap
exits.
- Use compatibility-safe Foundry and Claude VS Code defaults while
preserving explicit user choices.
- Configure both Copilot overrideProxyUrl and overrideCapiUrl in the
reversible managed VS Code settings block.
- Route Copilot unprefixed POST /chat/completions and HTTP /responses
requests through the real compression handlers.
- Keep /responses out of the Codex WebSocket aliases because Copilot and
Codex use different WebSocket wire protocols.
- Extend wrap E2E assertions for both the Claude webview mode and
Copilot CAPI routing.

## Testing

- [x] 127 combined Claude, Copilot, route-integration, and MCP
dependency-contract tests pass.
- [x] Ruff check passes on all changed Python files.
- [x] Ruff format check passes.
- [x] Python compilation and git diff --check pass.

## Runtime Safety

Standalone Claude CLI defaults remain unchanged. Explicit Claude
tool-search values retain precedence, and project settings are restored
through the existing cleanup path. Copilot model/session helper
endpoints continue through generic passthrough, while only validated
HTTP generation paths receive explicit compression routes. Existing
Codex WebSocket behavior is unchanged.

## Review Readiness

- [x] Current main and MCP v1 compatibility retained
- [x] Worker-facing Claude persistence covered
- [x] Reversible Copilot and Claude settings behavior covered
- [x] Copilot generation routes covered at registration and proxy
integration layers
- [x] Ready for review
2026-08-13 15:06:41 -05:00

53 lines
2.1 KiB
Python

"""`headroom init claude` must set ENABLE_TOOL_SEARCH (GH #746).
Claude Code disables on-demand tool loading when ANTHROPIC_BASE_URL is a custom
host and ENABLE_TOOL_SEARCH is unset, materializing all MCP/system tool schemas
into the context window — which breaks sub-agent spawns and forces compaction.
`headroom wrap claude` already sets it; `init` (and the persistent install) must
too, otherwise init-wired users silently get eager tools.
"""
from __future__ import annotations
import json
from pathlib import Path
from headroom.cli import init as init_cli
from headroom.providers.claude import install as claude_install
def test_ensure_claude_hooks_sets_enable_tool_search(tmp_path: Path, monkeypatch) -> None:
monkeypatch.delenv("CLAUDE_CODE_USE_FOUNDRY", raising=False)
settings = tmp_path / "settings.json"
init_cli._ensure_claude_hooks(settings, profile="init-user", port=8787)
env = json.loads(settings.read_text(encoding="utf-8"))["env"]
assert env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:8787"
assert env["ENABLE_TOOL_SEARCH"] == "true"
def test_ensure_claude_hooks_disables_tool_search_for_foundry(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setenv("CLAUDE_CODE_USE_FOUNDRY", "1")
settings = tmp_path / "settings.json"
init_cli._ensure_claude_hooks(settings, profile="init-user", port=8787)
env = json.loads(settings.read_text(encoding="utf-8"))["env"]
assert env["ENABLE_TOOL_SEARCH"] == "false"
def test_ensure_claude_hooks_respects_user_tool_search_value(tmp_path: Path) -> None:
settings = tmp_path / "settings.json"
settings.write_text(
json.dumps({"env": {"ENABLE_TOOL_SEARCH": "auto"}}) + "\n", encoding="utf-8"
)
init_cli._ensure_claude_hooks(settings, profile="init-user", port=8787)
env = json.loads(settings.read_text(encoding="utf-8"))["env"]
assert env["ENABLE_TOOL_SEARCH"] == "auto" # setdefault preserves the user's choice
def test_build_install_env_includes_enable_tool_search() -> None:
env = claude_install.build_install_env(port=8787, backend="anthropic")
assert env["ENABLE_TOOL_SEARCH"] == "true"
assert env["ANTHROPIC_BASE_URL"].endswith(":8787")