The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): the launch-time completion tail runs in Windows checkout custody (review N10, R6b)
Teknium7d084f135246
diff --git a/hermes_cli/venv_sync.py b/hermes_cli/venv_sync.pyindex 4aac2f56847..d962badd1f9 100644--- a/hermes_cli/venv_sync.py+++ b/hermes_cli/venv_sync.py@@ -524,19 +524,23 @@ def _finish_source_update(root: Path, *, current: bool, pending: Path) -> None:# The tail's progress lines go to stderr: this is an automatic repair in# front of whatever command the user ran, and that command may be# emitting machine-readable stdout (a JSON probe, a piped query).- from hermes_cli.update_lock import checkout_lock_fds-- # The completion child stays in this launch's checkout custody (POSIX: it inherits the- # lock fd), so a contender never sees the checkout free while it builds.- fds = checkout_lock_fds(root)- code = subprocess.call(- [sys.executable, "-I", "-B", "-u",- str(root / "hermes_cli/source_completion.py"),- "--source", str(root), "--finish-update",- *(("--desktop",) if desktop else ())],- cwd=root, env=activation_environment(root), stdout=sys.__stderr__,- **({"pass_fds": fds} if fds else {}),- )+ from hermes_cli.update_custody import CustodyRefused, run++ # The completion child stays in this launch's checkout custody (POSIX: it inherits the lock+ # fd; Windows: created suspended and bound to the lock owner's kill-on-close job), so a+ # contender never sees the checkout free while it builds. A child the job refuses never runs:+ # the tail stays owed.+ try:+ code = run(+ [sys.executable, "-I", "-B", "-u",+ str(root / "hermes_cli/source_completion.py"),+ "--source", str(root), "--finish-update",+ *(("--desktop",) if desktop else ())],+ inherit_lock=True, cwd=root, env=activation_environment(root), stdout=sys.__stderr__,+ ).returncode+ except CustodyRefused as exc:+ print(f"hermes: {exc.reason}", file=sys.stderr, flush=True)+ code = 1if code != 0:_record_completion_attempt(root, failed=True)raise RuntimeError(diff --git a/tests/hermes_cli/test_update_launch_completion.py b/tests/hermes_cli/test_update_launch_completion.pyindex bdc09e08eaf..8b75f3ef6f1 100644--- a/tests/hermes_cli/test_update_launch_completion.py+++ b/tests/hermes_cli/test_update_launch_completion.py@@ -35,12 +35,14 @@ def completion_tail(monkeypatch):spawned = Spawned()- def call(command, **kwargs):+ def run(command, **kwargs):spawned.append(command)spawned.kwargs = kwargs- return spawned.exit_code+ return subprocess.CompletedProcess(command, spawned.exit_code)- monkeypatch.setattr(venv_sync.subprocess, "call", call)+ from hermes_cli import update_custody++ monkeypatch.setattr(update_custody, "run", run)return spawneddiff --git a/tests/hermes_cli/test_venv_sync_update_tree.py b/tests/hermes_cli/test_venv_sync_update_tree.pyindex e7bd191f427..b01fd25b79a 100644--- a/tests/hermes_cli/test_venv_sync_update_tree.py+++ b/tests/hermes_cli/test_venv_sync_update_tree.py@@ -54,3 +54,58 @@ def test_launch_refuses_while_another_process_holds_the_checkout(tmp_path, monkefinally:holder.kill()holder.wait()+++def _tail_root(tmp_path: Path, monkeypatch) -> tuple[Path, Path]:+ """A checkout owing the tail, whose completion child only records that it ran."""+ import pm.environments++ root = tmp_path / "checkout"+ (root / "hermes_cli").mkdir(parents=True)+ started = tmp_path / "tail-ran"+ child = f"from pathlib import Path\nPath({str(started)!r}).touch()\n"+ (root / "hermes_cli" / "source_completion.py").write_text(child, encoding="utf-8")+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))+ monkeypatch.setattr(venv_sync, "_tree_matches_completed_stamp", lambda root: False)+ monkeypatch.setattr(pm.environments, "activation_environment", lambda root: None)+ return root, started+++def test_a_tail_child_custody_refuses_never_runs_and_stays_owed(tmp_path, monkeypatch):+ """N10/R6b: the launch-time tail writes the checkout, so its child is started in the update's+ custody. One custody refuses (Windows: the job would not take it) never runs unfenced: the+ launch fails as a failed tail does, so the tail stays owed."""+ from hermes_cli import update_custody++ root, started = _tail_root(tmp_path, monkeypatch)++ def refused(argv, **kwargs):+ raise update_custody._refuse(argv, OSError(5, "Access is denied"))++ monkeypatch.setattr(update_custody, "run", refused)+ with pytest.raises(RuntimeError, match="source update completion failed"):+ venv_sync._finish_source_update(root, current=True, pending=root / ".update-pending")+ assert not started.exists(), "the tail child ran outside the update's custody"++[email protected]("windows")+def test_a_tail_child_the_job_refuses_never_runs(tmp_path, monkeypatch):+ """N10/R6b native: with the launch holding the checkout lock, the REAL bind is refused (the+ job handle is an event); the tail child must not run outside the job."""+ import ctypes++ from hermes_cli import update_lock++ root, started = _tail_root(tmp_path, monkeypatch)+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)+ kernel32.CreateEventW.restype = ctypes.c_void_p+ event = kernel32.CreateEventW(None, True, False, None)+ monkeypatch.setattr(update_lock, "update_tree_job", lambda: event)+ lock = update_lock.UpdateLock(path=tmp_path / "marker", install_root=root, checkout_first=False)+ assert lock.acquire_checkout(root)+ try:+ with pytest.raises(RuntimeError, match="source update completion failed"):+ venv_sync._finish_source_update(root, current=True, pending=root / ".update-pending")+ finally:+ lock.release()+ assert not started.exists(), "the tail child ran outside the update's job"