Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(wrap): stop the launch cwd from shadowing the installed package in the proxy subprocess (#2843)

Abhay Singhc49be269a184
diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.py
index af22dc45c..8911c883f 100644
--- a/headroom/cli/wrap.py
+++ b/headroom/cli/wrap.py
@@ -629,6 +629,15 @@ def _start_proxy(
proxy_env = os.environ.copy()
_scrub_copilot_proxy_seed_env(proxy_env)
proxy_env["PYTHONIOENCODING"] = "utf-8"
+ # `python -m headroom.cli` prepends the launch cwd to sys.path, so running
+ # `wrap` from a directory that contains a `headroom/` folder (most commonly a
+ # clone of this repo, whose package lives at <root>/headroom/) shadows the
+ # installed wheel with the raw source tree, which has no compiled
+ # `headroom._core`. The proxy then dies with "No module named 'headroom._core'"
+ # and wrap silently falls back to launching the client unwrapped (#2793).
+ # PYTHONSAFEPATH disables that cwd prepend (Python 3.11+; a harmless no-op on
+ # 3.10) so the subprocess always resolves the installed package.
+ proxy_env["PYTHONSAFEPATH"] = "1"
# Vertex AI RST_STREAMs HTTP/2 connections (error_code:2). Force HTTP/1.1
# when wrapping a Vertex-mode client so upstream requests succeed.
if os.environ.get("CLAUDE_CODE_USE_VERTEX") or os.environ.get("ANTHROPIC_VERTEX_PROJECT_ID"):
diff --git a/tests/test_cli/test_wrap_claude_vertex_proxy_env.py b/tests/test_cli/test_wrap_claude_vertex_proxy_env.py
index 4d888ba61..546b31cd8 100644
--- a/tests/test_cli/test_wrap_claude_vertex_proxy_env.py
+++ b/tests/test_cli/test_wrap_claude_vertex_proxy_env.py
@@ -370,6 +370,36 @@ def test_start_proxy_clears_inherited_vertex_target_env(
assert "VERTEX_TARGET_API_URL" not in proxy_env
+def test_start_proxy_sets_pythonsafepath_to_avoid_cwd_shadow(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """`python -m headroom.cli` prepends the launch cwd to sys.path, so running
+ wrap from a directory that contains a `headroom/` folder (a clone of this
+ repo) shadows the installed wheel with the raw source tree, which has no
+ compiled `headroom._core`, and the proxy dies importing it (#2793). The
+ subprocess env must set PYTHONSAFEPATH=1 to disable that cwd prepend."""
+ fake_proc = _FakeProxyProcess()
+ captured: dict[str, Any] = {}
+
+ monkeypatch.setattr(wrap_mod, "_get_log_path", lambda: tmp_path / "proxy.log")
+ monkeypatch.setattr(wrap_mod, "_check_proxy", lambda _port: True)
+ monkeypatch.setattr(wrap_mod.time, "sleep", lambda _seconds: None)
+
+ def fake_popen(cmd: list[str], **kwargs: object) -> _FakeProxyProcess:
+ captured["cmd"] = cmd
+ captured["kwargs"] = kwargs
+ return fake_proc
+
+ monkeypatch.setattr(wrap_mod.subprocess, "Popen", fake_popen)
+
+ proc = wrap_mod._start_proxy(8787, agent_type="claude")
+
+ assert proc is fake_proc
+ assert captured["kwargs"]["env"]["PYTHONSAFEPATH"] == "1"
+ # Still launched as a module of the installed package.
+ assert captured["cmd"][:4] == [wrap_mod.sys.executable, "-m", "headroom.cli", "proxy"]
+
+
def test_ensure_proxy_restarts_idle_proxy_for_vertex_api_url_mismatch(
monkeypatch: pytest.MonkeyPatch,
) -> None: