The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: desktop promotion rides out a transient Windows file lock on release/win-unpacked
teknium1bbdb279a6839
diff --git a/hermes_cli/main_desktop.py b/hermes_cli/main_desktop.pyindex ce72d8a16ab..4fe1189c825 100644--- a/hermes_cli/main_desktop.py+++ b/hermes_cli/main_desktop.py@@ -174,6 +174,27 @@ _DESKTOP_STAGING_PREFIX = ".staging-"_DESKTOP_PREVIOUS_SUFFIX = ".previous"+# A real-time file scanner (AV/EDR) holds a short exclusive handle on a freshly packed+# release/win-unpacked tree; the promotion rename then fails with a sharing violation+# (WinError 32 / 5 -> PermissionError) and succeeds a moment later on identical input (#112544).+# Only PermissionError is retried: EXDEV/ENOENT-class failures are permanent.+_DESKTOP_SWAP_RENAME_RETRY_DELAYS_S = (0.5, 1.0, 1.0, 1.0)+++def _rename_riding_out_file_lock(src: Path, dst: Path) -> None:+ """``os.rename`` that retries a transient PermissionError with bounded backoff; re-raises the last one."""+ for attempt, delay in enumerate(_DESKTOP_SWAP_RENAME_RETRY_DELAYS_S, start=1):+ try:+ os.rename(src, dst)+ return+ except PermissionError as exc:+ logger.warning(+ "desktop promotion rename %s -> %s hit a file lock (attempt %d/%d), retrying in %.1fs: %s",+ src.name, dst.name, attempt, len(_DESKTOP_SWAP_RENAME_RETRY_DELAYS_S) + 1, delay, exc,+ )+ _time_mod.sleep(delay)+ os.rename(src, dst)+def _desktop_staging_dir(desktop_dir: Path) -> Path:"""Fresh staging dir ``apps/desktop/.staging-<pid>-<ts>``: a sibling of ``release/`` (same fs → the@@ -213,12 +234,12 @@ def _swap_staged_desktop_app(desktop_dir: Path, staging_dir: Path) -> Optional[Pstopped = _stop_desktop_processes_locking_build(desktop_dir)if stopped:logger.info("stopped desktop processes before staged app promotion: %s", stopped)- os.rename(live_root, previous)+ _rename_riding_out_file_lock(live_root, previous)try:- os.rename(staged_root, live_root)+ _rename_riding_out_file_lock(staged_root, live_root)except OSError:if moved_aside:- os.rename(previous, live_root) # restore; live app back as it was+ _rename_riding_out_file_lock(previous, live_root) # restore; live app back as it wasraiseif moved_aside:shutil.rmtree(previous, ignore_errors=True)diff --git a/tests/hermes_cli/test_desktop_swap_file_lock_retry.py b/tests/hermes_cli/test_desktop_swap_file_lock_retry.pynew file mode 100644index 00000000000..5f9d35fb5e2--- /dev/null+++ b/tests/hermes_cli/test_desktop_swap_file_lock_retry.py@@ -0,0 +1,82 @@+"""Desktop stage-and-swap promotion rides out a transient Windows file lock (#112544).++A real-time scanner (AV/EDR) briefly holds ``release/win-unpacked`` right after the pack; the+promotion rename then raises PermissionError and the updater used to report "Could not install the+rebuilt desktop app" even though a retry a second later would have succeeded.+"""++from __future__ import annotations++import os+import sys+from pathlib import Path++from hermes_cli import main_desktop+++def _packaged_exe_rel() -> Path:+ if sys.platform == "darwin":+ return Path("mac-arm64") / "Hermes.app" / "Contents" / "MacOS" / "Hermes"+ if sys.platform == "win32":+ return Path("win-unpacked") / "Hermes.exe"+ return Path("linux-unpacked") / "hermes"+++def _staged_over_live(tmp_path: Path, monkeypatch):+ desktop_dir = tmp_path / "apps" / "desktop"+ live_exe = desktop_dir / "release" / _packaged_exe_rel()+ live_exe.parent.mkdir(parents=True)+ live_exe.write_text("old", encoding="utf-8")+ staging = main_desktop._desktop_staging_dir(desktop_dir)+ staged_exe = staging / _packaged_exe_rel()+ staged_exe.parent.mkdir(parents=True)+ staged_exe.write_text("new", encoding="utf-8")+ monkeypatch.setattr(main_desktop, "_stop_desktop_processes_locking_build", lambda d: [])+ slept: list[float] = []+ monkeypatch.setattr(main_desktop._time_mod, "sleep", slept.append)+ return desktop_dir, staging, live_exe, slept+++def test_swap_retries_transient_permission_error_then_promotes(tmp_path, monkeypatch, caplog):+ desktop_dir, staging, live_exe, slept = _staged_over_live(tmp_path, monkeypatch)+ real_rename = os.rename+ locked = {"n": 0}++ def scanner_locked_rename(src, dst):+ if Path(dst) == live_exe.parent and locked["n"] < 2:+ locked["n"] += 1+ raise PermissionError(32, "being used by another process")+ return real_rename(src, dst)++ monkeypatch.setattr(main_desktop.os, "rename", scanner_locked_rename)+ with caplog.at_level("WARNING", logger=main_desktop.logger.name):+ promoted = main_desktop._swap_staged_desktop_app(desktop_dir, staging)++ assert promoted == live_exe+ assert live_exe.read_text(encoding="utf-8") == "new"+ assert slept == [0.5, 1.0]+ assert sum("hit a file lock" in r.message for r in caplog.records) == 2+ assert not staging.exists()+++def test_swap_gives_up_after_bounded_retries_and_keeps_live_app(tmp_path, monkeypatch, caplog):+ """A lock that never clears: bounded attempts, real OSError surfaced in the log, live app untouched."""+ desktop_dir, staging, live_exe, slept = _staged_over_live(tmp_path, monkeypatch)+ real_rename = os.rename+ attempts = {"n": 0}++ def always_locked_rename(src, dst):+ if Path(src) == staging / _packaged_exe_rel().parts[0]:+ attempts["n"] += 1+ raise PermissionError(5, "Access is denied")+ return real_rename(src, dst)++ monkeypatch.setattr(main_desktop.os, "rename", always_locked_rename)+ with caplog.at_level("WARNING", logger=main_desktop.logger.name):+ assert main_desktop._swap_staged_desktop_app(desktop_dir, staging) is None++ assert attempts["n"] == len(main_desktop._DESKTOP_SWAP_RENAME_RETRY_DELAYS_S) + 1+ assert len(slept) == len(main_desktop._DESKTOP_SWAP_RENAME_RETRY_DELAYS_S)+ assert live_exe.read_text(encoding="utf-8") == "old"+ assert not (live_exe.parent.parent / (live_exe.parent.name + ".previous")).exists()+ assert any("Access is denied" in r.message and "live app kept" in r.message for r in caplog.records)