Skip to content

The agent that grows with you

fix(gateway): sweep a stale Startup-folder staging file on uninstall and re-install

teknium1cae25086b926
diff --git a/hermes_cli/gateway_windows.py b/hermes_cli/gateway_windows.py
index 6661693a95c..fc6325dccb5 100644
--- a/hermes_cli/gateway_windows.py
+++ b/hermes_cli/gateway_windows.py
@@ -259,6 +259,11 @@ def _legacy_startup_entry_path() -> Path:
return _startup_dir() / f"{_sanitize_filename(get_task_name())}.cmd"
+def _startup_staging_path() -> Path:
+ """The Startup-folder staging file; also the debris a pre-fix failed swap left behind (#114093)."""
+ return get_startup_entry_path().with_suffix(".tmp")
+
+
def _stable_gateway_working_dir(project_root: Path) -> str:
"""Stable cwd for detached/startup runs: anchor at HERMES_HOME when it exists (mirrors the POSIX
service invariant) so a moved checkout/worktree can't fail the ``cd`` step; else the checkout."""
@@ -528,7 +533,7 @@ def _install_startup_entry(script_path: Path) -> Path:
"""Write the Startup-folder fallback launcher. Returns its path."""
entry = get_startup_entry_path()
entry.parent.mkdir(parents=True, exist_ok=True)
- _atomic_write(entry, _build_startup_launcher(script_path), entry.with_suffix(".tmp"))
+ _atomic_write(entry, _build_startup_launcher(script_path), _startup_staging_path())
legacy_entry = _legacy_startup_entry_path()
try:
if legacy_entry.exists():
@@ -784,6 +789,12 @@ def install(
task_name = get_task_name()
script_path = _write_task_script()
+ # A pre-fix install that failed its Startup-folder swap left `Hermes_Gateway.tmp` there, and the
+ # Scheduled Task path below never touches that folder — sweep it so a re-run clears the debris.
+ try:
+ _startup_staging_path().unlink(missing_ok=True)
+ except OSError:
+ pass
# On locked-down accounts schtasks can sit for the full timeout before returning Access Denied.
# All intent questions were asked above, so ask for UAC before touching schtasks.
@@ -1168,6 +1179,7 @@ def uninstall() -> None:
for path, label in (
(get_startup_entry_path(), "Windows login item"), (_legacy_startup_entry_path(), "legacy Windows login item"),
+ (_startup_staging_path(), "Windows login item staging file"),
(script_path, "Task script"), (script_path.with_suffix(".vbs"), "Task launcher"),
):
try:
diff --git a/tests/hermes_cli/test_gateway_windows.py b/tests/hermes_cli/test_gateway_windows.py
index cd94e9d0d09..4155ec1e248 100644
--- a/tests/hermes_cli/test_gateway_windows.py
+++ b/tests/hermes_cli/test_gateway_windows.py
@@ -340,6 +340,51 @@ def test_gateway_vbs_script_is_console_less(monkeypatch):
assert content.endswith("\r\n")
+def test_atomic_write_leaves_no_staging_file_when_swap_fails(monkeypatch, tmp_path):
+ """The Startup folder is the staging dir: a leftover .tmp there is opened by Windows at every login."""
+ startup = tmp_path / "Startup"
+ startup.mkdir()
+ entry, staging = startup / "Hermes_Gateway.vbs", startup / "Hermes_Gateway.tmp"
+
+ def _denied(self, target):
+ raise PermissionError(5, "Access is denied")
+
+ monkeypatch.setattr(Path, "replace", _denied)
+ with pytest.raises(PermissionError):
+ gateway_windows._atomic_write(entry, "launcher\r\n", staging)
+
+ assert sorted(p.name for p in startup.iterdir()) == []
+
+
+def test_uninstall_and_reinstall_sweep_stale_startup_staging_file(monkeypatch, tmp_path):
+ """Debris from a pre-fix failed swap is removed by uninstall() and by an install() that takes the
+ Scheduled Task path (which never rewrites the Startup folder itself)."""
+ startup = tmp_path / "Startup"
+ startup.mkdir()
+ entry, staging = startup / "Hermes_Gateway_alice.vbs", startup / "Hermes_Gateway_alice.tmp"
+ script = tmp_path / "task" / "Hermes_Gateway_alice.cmd"
+
+ monkeypatch.setattr(gateway_windows, "_assert_windows", lambda: None)
+ monkeypatch.setattr(gateway_windows, "get_task_name", lambda: "Hermes_Gateway_alice")
+ monkeypatch.setattr(gateway_windows, "get_task_script_path", lambda: script)
+ monkeypatch.setattr(gateway_windows, "get_startup_entry_path", lambda: entry)
+ monkeypatch.setattr(gateway_windows, "_legacy_startup_entry_path", lambda: startup / "Hermes_Gateway_alice.cmd")
+ monkeypatch.setattr(gateway_windows, "is_task_registered", lambda: False)
+
+ staging.write_text("stale", encoding="utf-8")
+ gateway_windows.uninstall()
+ assert not staging.exists()
+
+ staging.write_text("stale", encoding="utf-8")
+ monkeypatch.setattr(gateway_windows, "_prompt_install_choices", lambda *a, **k: (False, True))
+ monkeypatch.setattr(gateway_windows, "_write_task_script", lambda: script)
+ monkeypatch.setattr(gateway_windows, "_is_running_as_admin", lambda: True)
+ monkeypatch.setattr(gateway_windows, "_install_scheduled_task", lambda name, path: (True, "created"))
+ monkeypatch.setattr(gateway_windows, "_print_next_steps", lambda: None)
+ gateway_windows.install()
+ assert not staging.exists()
+
+