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.

test: avoid global platform leaks in install tests

JerrettDavisd4574f4ae2d5
diff --git a/headroom/install/runtime.py b/headroom/install/runtime.py
index 5a4dae53f..de7cb8fcd 100644
--- a/headroom/install/runtime.py
+++ b/headroom/install/runtime.py
@@ -41,6 +41,10 @@ PASSTHROUGH_ENV_PREFIXES = (
)
+def _is_windows() -> bool:
+ return sys.platform.startswith("win")
+
+
def _deployment_env(manifest: DeploymentManifest) -> dict[str, str]:
return {
"HEADROOM_DEPLOYMENT_PROFILE": manifest.profile,
@@ -73,7 +77,7 @@ def _ensure_host_dirs() -> None:
def _mount_source(home: str, subdir: str) -> str:
- if os.name == "nt":
+ if _is_windows():
return f"{home}\\{subdir}"
return f"{home}/{subdir}"
@@ -115,7 +119,7 @@ def build_runtime_command(manifest: DeploymentManifest) -> list[str]:
"--volume",
f"{_mount_source(home, '.gemini')}:{container_home}/.gemini",
]
- if os.name != "nt":
+ if not _is_windows():
getuid = getattr(os, "getuid", None)
getgid = getattr(os, "getgid", None)
if callable(getuid) and callable(getgid):
@@ -198,7 +202,7 @@ def start_detached_agent(profile: str) -> subprocess.Popen[str]:
log_file = open(log_file_path, "a", encoding="utf-8", errors="replace") # noqa: SIM115
kwargs: dict[str, Any] = {"stdout": log_file, "stderr": log_file}
- if os.name == "nt":
+ if _is_windows():
kwargs["creationflags"] = getattr(subprocess, "DETACHED_PROCESS", 0) | getattr(
subprocess, "CREATE_NEW_PROCESS_GROUP", 0
)
diff --git a/headroom/install/supervisors.py b/headroom/install/supervisors.py
index 376f74596..edd991c8a 100644
--- a/headroom/install/supervisors.py
+++ b/headroom/install/supervisors.py
@@ -23,6 +23,10 @@ from .paths import (
from .runtime import resolve_headroom_command
+def _is_windows() -> bool:
+ return sys.platform.startswith("win")
+
+
def _command_for_script(*parts: str) -> list[str]:
return [*resolve_headroom_command(), *parts]
@@ -60,7 +64,7 @@ def _render_windows_runner(
def render_runner_scripts(manifest: DeploymentManifest) -> list[ArtifactRecord]:
"""Render runner/watchdog scripts for the deployment profile."""
- if os.name == "nt":
+ if _is_windows():
records = []
records.extend(
_render_windows_runner(
@@ -230,7 +234,7 @@ def install_supervisor(manifest: DeploymentManifest) -> list[ArtifactRecord]:
records.append(ArtifactRecord(kind="plist", path=str(plist_path)))
return records
- if os.name == "nt" and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
+ if _is_windows() and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
service_bin = f'cmd.exe /c "{windows_run_cmd_path(manifest.profile)}"'
subprocess.run(
["sc.exe", "create", manifest.service_name, f"binPath= {service_bin}", "start= auto"],
@@ -243,7 +247,7 @@ def install_supervisor(manifest: DeploymentManifest) -> list[ArtifactRecord]:
records.append(ArtifactRecord(kind="windows-service", path=manifest.service_name))
return records
- if os.name == "nt" and manifest.supervisor_kind == SupervisorKind.TASK.value:
+ if _is_windows() and manifest.supervisor_kind == SupervisorKind.TASK.value:
startup_name = f"{manifest.service_name}-startup"
health_name = f"{manifest.service_name}-health"
startup_cmd = str(windows_ensure_cmd_path(manifest.profile))
@@ -308,7 +312,7 @@ def start_supervisor(manifest: DeploymentManifest) -> None:
)
subprocess.run(["launchctl", "kickstart", "-k", f"{domain}/{label}"], check=True)
return
- if os.name == "nt" and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
+ if _is_windows() and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
subprocess.run(["sc.exe", "start", manifest.service_name], check=True)
@@ -331,7 +335,7 @@ def stop_supervisor(manifest: DeploymentManifest) -> None:
)
subprocess.run(["launchctl", "bootout", f"{domain}/{label}"], check=True)
return
- if os.name == "nt" and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
+ if _is_windows() and manifest.supervisor_kind == SupervisorKind.SERVICE.value:
subprocess.run(["sc.exe", "stop", manifest.service_name], check=True)
@@ -392,7 +396,7 @@ def remove_supervisor(manifest: DeploymentManifest) -> None:
plist_path.unlink()
return
- if os.name == "nt":
+ if _is_windows():
if manifest.supervisor_kind == SupervisorKind.SERVICE.value:
subprocess.run(
["sc.exe", "stop", manifest.service_name], capture_output=True, text=True
diff --git a/tests/test_install/test_runtime.py b/tests/test_install/test_runtime.py
index 205dedc0a..481c63265 100644
--- a/tests/test_install/test_runtime.py
+++ b/tests/test_install/test_runtime.py
@@ -131,9 +131,9 @@ def test_runtime_env_and_mount_source(monkeypatch) -> None:
assert _runtime_env(manifest)["EXTRA"] == "1"
assert _runtime_env(manifest)["HEADROOM_DEPLOYMENT_PROFILE"] == "default"
- monkeypatch.setattr("headroom.install.runtime.os.name", "nt")
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "win32")
assert _mount_source("C:\\Users\\me", ".headroom") == "C:\\Users\\me\\.headroom"
- monkeypatch.setattr("headroom.install.runtime.os.name", "posix")
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "linux")
assert _mount_source("/home/me", ".headroom") == "/home/me/.headroom"
@@ -164,7 +164,7 @@ def test_build_runtime_command_python_and_docker_user(monkeypatch, tmp_path: Pat
]
monkeypatch.setattr(Path, "home", lambda: tmp_path)
- monkeypatch.setattr("headroom.install.runtime.os.name", "posix")
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "linux")
monkeypatch.setattr("headroom.install.runtime.os.getuid", lambda: 1000, raising=False)
monkeypatch.setattr("headroom.install.runtime.os.getgid", lambda: 1001, raising=False)
docker_manifest = DeploymentManifest(
@@ -263,7 +263,7 @@ def test_run_foreground_and_detached_helpers(monkeypatch, tmp_path: Path) -> Non
assert _read_pid("default") is None
monkeypatch.setattr("headroom.install.runtime.resolve_headroom_command", lambda: ["headroom"])
- monkeypatch.setattr("headroom.install.runtime.os.name", "nt")
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "win32")
monkeypatch.setattr("headroom.install.runtime.subprocess.DETACHED_PROCESS", 1, raising=False)
monkeypatch.setattr(
"headroom.install.runtime.subprocess.CREATE_NEW_PROCESS_GROUP", 2, raising=False
@@ -274,7 +274,7 @@ def test_run_foreground_and_detached_helpers(monkeypatch, tmp_path: Path) -> Non
)
assert start_detached_agent("demo") is fake_proc_nt
- monkeypatch.setattr("headroom.install.runtime.os.name", "posix")
+ monkeypatch.setattr("headroom.install.runtime.sys.platform", "linux")
fake_proc_posix = FakeProc()
monkeypatch.setattr(
"headroom.install.runtime.subprocess.Popen", lambda command, **kwargs: fake_proc_posix
diff --git a/tests/test_install/test_supervisors.py b/tests/test_install/test_supervisors.py
index 8f39c128d..80af6c957 100644
--- a/tests/test_install/test_supervisors.py
+++ b/tests/test_install/test_supervisors.py
@@ -129,7 +129,7 @@ def test_render_windows_runner_writes_ps1_and_cmd_wrappers(tmp_path: Path) -> No
def test_render_runner_scripts_writes_unix_scripts(monkeypatch, tmp_path: Path) -> None:
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "linux")
monkeypatch.setattr(
"headroom.install.supervisors.resolve_headroom_command", lambda: ["headroom"]
)
@@ -145,7 +145,7 @@ def test_render_runner_scripts_writes_unix_scripts(monkeypatch, tmp_path: Path)
def test_render_runner_scripts_writes_windows_scripts(monkeypatch, tmp_path: Path) -> None:
- monkeypatch.setattr("headroom.install.supervisors.os.name", "nt")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
monkeypatch.setattr(
"headroom.install.supervisors.resolve_headroom_command", lambda: ["headroom.exe"]
)
@@ -177,7 +177,7 @@ def test_render_runner_scripts_writes_windows_scripts(monkeypatch, tmp_path: Pat
def test_install_supervisor_none_returns_runner_records(monkeypatch, tmp_path: Path) -> None:
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "linux")
monkeypatch.setattr(
"headroom.install.supervisors.resolve_headroom_command", lambda: ["headroom"]
)
@@ -210,7 +210,7 @@ def test_start_and_stop_supervisor_use_linux_systemctl(monkeypatch) -> None:
def test_install_supervisor_linux_service_and_tasks(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "linux")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "linux")
run_script = tmp_path / "run-headroom.sh"
ensure_script = tmp_path / "ensure-headroom.sh"
monkeypatch.setattr(
@@ -286,7 +286,7 @@ def test_install_supervisor_darwin_windows_and_unsupported(monkeypatch, tmp_path
lambda manifest, script, interval=None: (plist_path, f"plist-{interval}"),
)
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "darwin")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "darwin")
service_records = install_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
task_records = install_supervisor(_manifest(supervisor=SupervisorKind.TASK.value))
assert plist_path.read_text(encoding="utf-8") == "plist-300"
@@ -295,7 +295,7 @@ def test_install_supervisor_darwin_windows_and_unsupported(monkeypatch, tmp_path
assert ["launchctl", "bootstrap", "gui/123", str(plist_path)] in calls
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "nt")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
monkeypatch.setattr(
"headroom.install.supervisors.windows_run_cmd_path",
lambda profile: Path(f"C:\\tmp\\{profile}\\run-headroom.cmd"),
@@ -330,7 +330,7 @@ def test_install_supervisor_darwin_windows_and_unsupported(monkeypatch, tmp_path
] in calls
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "plan9")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "plan9")
with pytest.raises(click.ClickException, match="not supported"):
install_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
@@ -348,7 +348,7 @@ def test_start_and_stop_supervisor_darwin_windows_and_none(monkeypatch) -> None:
assert calls == []
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "darwin")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "darwin")
start_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
stop_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
assert calls == [
@@ -358,7 +358,7 @@ def test_start_and_stop_supervisor_darwin_windows_and_none(monkeypatch) -> None:
calls.clear()
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "nt")
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
start_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
stop_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
assert calls == [
@@ -455,14 +455,12 @@ def test_remove_supervisor_darwin_and_windows(monkeypatch, tmp_path: Path) -> No
lambda manifest, script, interval=None: (plist_path, "plist"),
)
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "darwin")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "posix")
remove_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
assert not plist_path.exists()
assert calls[0] == ["launchctl", "bootout", "gui/55/com.headroom.default"]
calls.clear()
monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
- monkeypatch.setattr("headroom.install.supervisors.os.name", "nt")
remove_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
remove_supervisor(_manifest(supervisor=SupervisorKind.TASK.value))
assert calls == [