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(install): tolerate absent Windows tasks in stop_supervisor (#4465) (#4468)

Parideboy50af2ff7a81e
diff --git a/headroom/install/supervisors.py b/headroom/install/supervisors.py
index 35c01e9e0..c46b68875 100644
--- a/headroom/install/supervisors.py
+++ b/headroom/install/supervisors.py
@@ -694,6 +694,11 @@ def start_supervisor(manifest: DeploymentManifest) -> None:
subprocess.run(["schtasks", "/Run", "/TN", f"{manifest.service_name}-startup"], check=True)
+# `/End` and `/Delete` report a missing task as "cannot find the file specified";
+# `/Change` reports it as "does not exist in the system".
+_SCHTASKS_ABSENT_TEXT = ("cannot find the file specified", "does not exist in the system")
+
+
def stop_supervisor(manifest: DeploymentManifest) -> None:
"""Stop the installed supervisor for a deployment."""
@@ -730,10 +735,32 @@ def stop_supervisor(manifest: DeploymentManifest) -> None:
)
return
if _is_windows():
+ # Session-scoped fallback installs never create the `-startup` task,
+ # and the health task may already be absent, so tolerate missing
+ # tasks (schtasks reports "cannot find the file specified") while
+ # still surfacing real failures such as access denied.
health = f"{manifest.service_name}-health"
- subprocess.run(["schtasks", "/Change", "/TN", health, "/DISABLE"], check=True)
+ result = run(
+ ["schtasks", "/Change", "/TN", health, "/DISABLE"],
+ capture_output=True,
+ text=True,
+ )
+ _require_removal_success(
+ result,
+ "schtasks health disable",
+ absent_text=_SCHTASKS_ABSENT_TEXT,
+ )
subprocess.run(["schtasks", "/End", "/TN", health], check=False)
- subprocess.run(["schtasks", "/End", "/TN", f"{manifest.service_name}-startup"], check=True)
+ result = run(
+ ["schtasks", "/End", "/TN", f"{manifest.service_name}-startup"],
+ capture_output=True,
+ text=True,
+ )
+ _require_removal_success(
+ result,
+ "schtasks startup end",
+ absent_text=_SCHTASKS_ABSENT_TEXT,
+ )
def remove_supervisor(manifest: DeploymentManifest) -> None:
@@ -828,7 +855,7 @@ def remove_supervisor(manifest: DeploymentManifest) -> None:
_require_removal_success(
result,
f"schtasks {suffix} delete",
- absent_text="cannot find the file specified",
+ absent_text=_SCHTASKS_ABSENT_TEXT,
)
@@ -843,7 +870,7 @@ def _require_removal_success(
operation: str,
*,
absent_codes: set[int] | None = None,
- absent_text: str | None = None,
+ absent_text: str | tuple[str, ...] | None = None,
) -> None:
returncode = int(getattr(result, "returncode", 0))
detail = _result_text(result)
@@ -851,6 +878,7 @@ def _require_removal_success(
return
if absent_codes and returncode in absent_codes:
return
- if absent_text and absent_text in detail.lower():
+ texts = (absent_text,) if isinstance(absent_text, str) else absent_text or ()
+ if any(text in detail.lower() for text in texts):
return
raise click.ClickException(f"{operation} failed: {detail}")
diff --git a/tests/test_install/test_supervisors.py b/tests/test_install/test_supervisors.py
index 8d43c528d..546b0244e 100644
--- a/tests/test_install/test_supervisors.py
+++ b/tests/test_install/test_supervisors.py
@@ -948,6 +948,63 @@ def test_start_and_stop_supervisor_darwin_windows_and_none(monkeypatch) -> None:
]
+ "stderr",
+ [
+ "ERROR: The system cannot find the file specified.",
+ 'ERROR: The specified task name "headroom-default-health" does not exist in the system.',
+ ],
+)
+def test_windows_stop_tolerates_absent_tasks(monkeypatch: pytest.MonkeyPatch, stderr: str) -> None:
+ # Session-scoped fallback installs never create the `-startup` task, and
+ # the health task can be missing too, so stop must not raise when
+ # schtasks reports the task as missing.
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
+ calls: list[list[str]] = []
+ monkeypatch.setattr(
+ "headroom.install.supervisors.subprocess.run",
+ lambda command, **kwargs: _LaunchctlResult(0),
+ )
+
+ def fake_run(command: list[str], **kwargs: object) -> object:
+ calls.append(command)
+ return type(
+ "Result",
+ (),
+ {
+ "returncode": 1,
+ "stdout": "",
+ "stderr": stderr,
+ },
+ )()
+
+ monkeypatch.setattr("headroom.install.supervisors.run", fake_run)
+ stop_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
+ assert calls == [
+ ["schtasks", "/Change", "/TN", "headroom-default-health", "/DISABLE"],
+ ["schtasks", "/End", "/TN", "headroom-default-startup"],
+ ]
+
+
+def test_windows_stop_surfaces_access_denied(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("headroom.install.supervisors.sys.platform", "win32")
+
+ def fake_run(command: list[str], **kwargs: object) -> object:
+ return type(
+ "Result",
+ (),
+ {
+ "returncode": 1,
+ "stdout": "",
+ "stderr": "ERROR: Access is denied.",
+ },
+ )()
+
+ monkeypatch.setattr("headroom.install.supervisors.run", fake_run)
+ with pytest.raises(click.ClickException, match="Access is denied"):
+ stop_supervisor(_manifest(supervisor=SupervisorKind.SERVICE.value))
+
+
def test_macos_start_bootstraps_when_job_not_registered(monkeypatch, tmp_path: Path) -> None:
# Post-`stop`/`restart` state: the job was booted out, so `kickstart` fails
# (launchctl 113) and start must bootstrap the plist instead.