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(update): let Windows self-update replace headroom.exe (#2016)

Rod Boev2678bb1db621
diff --git a/headroom/cli/update.py b/headroom/cli/update.py
index ee1a84367..9b907f126 100644
--- a/headroom/cli/update.py
+++ b/headroom/cli/update.py
@@ -133,6 +133,18 @@ def _format_cmd(argv: list[str]) -> str:
return shlex.join(argv)
+def _windows_pip_update_needs_handoff(method: InstallMethod) -> bool:
+ return sys.platform.startswith("win") and method.kind in {"pip", "pip-user"}
+
+
+def _build_windows_handoff_argv(argv: list[str]) -> list[str]:
+ helper = (
+ "import subprocess,sys,time; time.sleep(1); "
+ "raise SystemExit(subprocess.run([sys.executable,*sys.argv[1:]], check=False).returncode)"
+ )
+ return [sys.executable, "-c", helper, *argv[1:]]
+
+
def _is_externally_managed() -> bool:
"""Detect a PEP 668 ``EXTERNALLY-MANAGED`` marker (Homebrew, Debian, etc.)."""
try:
@@ -466,6 +478,18 @@ def update(check_only: bool, assume_yes: bool, allow_pre: bool, extras: str | No
return
click.echo(f"Running: {cmd_str}")
+ if _windows_pip_update_needs_handoff(method):
+ handoff_argv = _build_windows_handoff_argv(method.argv)
+ try:
+ subprocess.Popen(handoff_argv) # noqa: S603 - fixed allowlist plus helper argv
+ except FileNotFoundError:
+ raise click.ClickException(
+ f"`{handoff_argv[0]}` was not found on PATH. Upgrade manually: {cmd_str}"
+ ) from None
+ click.echo("Upgrade started in a child process so pip can replace headroom.exe.")
+ click.echo("Wait for the pip output to finish, then rerun `headroom --version`.")
+ return
+
try:
returncode = safe_update(method.argv)
except FileNotFoundError:
@@ -481,4 +505,10 @@ def update(check_only: bool, assume_yes: bool, allow_pre: bool, extras: str | No
click.echo("Restart any running `headroom proxy` to pick up the new version.")
-__all__ = ["InstallMethod", "detect_install_method", "update"]
+__all__ = [
+ "InstallMethod",
+ "_build_windows_handoff_argv",
+ "_windows_pip_update_needs_handoff",
+ "detect_install_method",
+ "update",
+]
diff --git a/tests/test_cli_update.py b/tests/test_cli_update.py
index 55826487e..372649461 100644
--- a/tests/test_cli_update.py
+++ b/tests/test_cli_update.py
@@ -126,6 +126,7 @@ def test_update_runs_upgrade_with_yes(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
+ monkeypatch.setattr(up.sys, "platform", "linux")
monkeypatch.setattr(up, "_find_core_pyd", lambda: None) # Skip integrity checks
class _Result:
@@ -142,6 +143,80 @@ def test_update_runs_upgrade_with_yes(monkeypatch):
assert "upgraded to 0.27.0" in res.output
+def test_update_windows_pip_handoff_uses_popen(monkeypatch):
+ calls = {}
+
+ monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
+ monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
+ monkeypatch.setattr(up.sys, "platform", "win32")
+
+ def _detect(extras=None):
+ calls["extras"] = extras
+ return up.InstallMethod(
+ kind="pip",
+ can_self_update=True,
+ argv=[
+ r"C:\Python313\python.exe",
+ "-m",
+ "pip",
+ "install",
+ "-U",
+ "headroom-ai[foo&calc]",
+ ],
+ )
+
+ monkeypatch.setattr(up, "detect_install_method", _detect)
+
+ def _run(*a, **k):
+ raise AssertionError("Windows pip handoff must not call subprocess.run")
+
+ def _popen(argv, *a, **k):
+ calls["argv"] = argv
+ return object()
+
+ monkeypatch.setattr(up.subprocess, "run", _run)
+ monkeypatch.setattr(up.subprocess, "Popen", _popen)
+ res = CliRunner().invoke(main, ["update", "--yes", "--extras", "foo&calc"])
+ assert res.exit_code == 0
+ assert calls["extras"] == "foo&calc"
+ assert calls["argv"][:2] == [sys.executable, "-c"]
+ assert "subprocess.run" in calls["argv"][2]
+ assert calls["argv"][3:] == [
+ "-m",
+ "pip",
+ "install",
+ "-U",
+ "headroom-ai[foo&calc]",
+ ]
+ assert "headroom.exe" in res.output
+
+
+def test_update_windows_non_pip_path_stays_synchronous(monkeypatch):
+ calls = {}
+
+ monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
+ monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
+ monkeypatch.setattr(up.sys, "platform", "win32")
+ monkeypatch.setattr(
+ up,
+ "detect_install_method",
+ lambda extras=None: up.InstallMethod(
+ kind="pipx",
+ can_self_update=True,
+ argv=["pipx", "upgrade", "headroom-ai"],
+ ),
+ )
+
+ def _popen(*a, **k):
+ raise AssertionError("pipx must stay on the synchronous path")
+
+ monkeypatch.setattr(up, "safe_update", lambda argv: calls.setdefault("safe_update", argv) and 0)
+ monkeypatch.setattr(up.subprocess, "Popen", _popen)
+ res = CliRunner().invoke(main, ["update", "--yes"])
+ assert res.exit_code == 0
+ assert calls["safe_update"] == ["pipx", "upgrade", "headroom-ai"]
+
+
def test_update_refuses_in_checkout(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
@@ -168,6 +243,7 @@ def test_update_upgrade_failure_surfaces_command(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
+ monkeypatch.setattr(up.sys, "platform", "linux")
monkeypatch.setattr(up, "_find_core_pyd", lambda: None) # Skip file operations in test
class _Result:
diff --git a/tests/test_update_helpers.py b/tests/test_update_helpers.py
index 96f3a0818..bb313b2d1 100644
--- a/tests/test_update_helpers.py
+++ b/tests/test_update_helpers.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import importlib.metadata as md
+import sys
import sysconfig
import pytest
@@ -130,6 +131,46 @@ def test_format_cmd_windows(monkeypatch):
assert "Program Files" in out and out.endswith("-m pip")
+def test_windows_pip_update_needs_handoff(monkeypatch):
+ monkeypatch.setattr(up.sys, "platform", "win32")
+ assert up._windows_pip_update_needs_handoff(
+ up.InstallMethod(kind="pip", can_self_update=True, argv=["python"])
+ )
+ assert up._windows_pip_update_needs_handoff(
+ up.InstallMethod(kind="pip-user", can_self_update=True, argv=["python"])
+ )
+ assert not up._windows_pip_update_needs_handoff(
+ up.InstallMethod(kind="pipx", can_self_update=True, argv=["pipx"])
+ )
+
+
+def test_windows_pip_update_handoff_false_off_windows(monkeypatch):
+ monkeypatch.setattr(up.sys, "platform", "linux")
+ assert not up._windows_pip_update_needs_handoff(
+ up.InstallMethod(kind="pip", can_self_update=True, argv=["python"])
+ )
+
+
[email protected]("operator", ["&", "|", ">"])
+def test_build_windows_handoff_argv_preserves_pip_argv(monkeypatch, operator):
+ monkeypatch.setattr(up.sys, "platform", "win32")
+ pip_argv = [
+ r"C:\\Program Files\\Python\\python.exe",
+ "-m",
+ "pip",
+ "install",
+ "-U",
+ f"headroom-ai[foo{operator}calc]",
+ ]
+
+ handoff_argv = up._build_windows_handoff_argv(pip_argv)
+
+ assert handoff_argv[:2] == [sys.executable, "-c"]
+ assert "subprocess.run" in handoff_argv[2]
+ assert "cmd.exe" not in handoff_argv
+ assert handoff_argv[3:] == pip_argv[1:]
+
+
def test_externally_managed_true(tmp_path, monkeypatch):
(tmp_path / "EXTERNALLY-MANAGED").write_text("[externally-managed]")
monkeypatch.setattr(sysconfig, "get_path", lambda key: str(tmp_path))