Skip to content

The agent that grows with you

fix(update): the Windows bootstrap completion child leases the checkout before it writes it

Tekniumaff4a85d3bda
diff --git a/hermes_cli/update_completion.py b/hermes_cli/update_completion.py
index f5e34590173..0480388f2e4 100644
--- a/hermes_cli/update_completion.py
+++ b/hermes_cli/update_completion.py
@@ -320,6 +320,18 @@ def _prepare(request: dict, request_path: Path, result_path: Path) -> int:
root = Path(request["source"])
update_id = request["receipt"]["update_id"]
+ if sys.platform == "win32":
+ # Windows (review L3): a bootstrap child the update's job refused runs outside the job
+ # and outlives a killed owner, so it joins the checkout lock before anything below
+ # writes the checkout (uv's sync children, the generation collector): the join takes a
+ # lease byte of its own (R5b, as the --prepared child does in complete_source_checkout).
+ # Held until this bootstrap exits, after the --prepared child; the kernel drops it.
+ from hermes_cli.update_lock import _acquire_checkout
+
+ refused = _acquire_checkout(root)
+ if refused is not None:
+ raise RuntimeError("could not join the update's checkout lock "
+ f"({refused.reason or f'held by process {refused.pid}'})")
from hermes_cli.venv_sync import (
arm_completion, collect_superseded_generations, refuse_foreign_owned_venv,
)
diff --git a/tests/hermes_cli/test_update_completion_lease.py b/tests/hermes_cli/test_update_completion_lease.py
new file mode 100644
index 00000000000..b6adbc2b468
--- /dev/null
+++ b/tests/hermes_cli/test_update_completion_lease.py
@@ -0,0 +1,70 @@
+"""L3: the Windows bootstrap completion child joins the checkout lock (with its own R5b lease)
+before it writes the checkout."""
+
+import os
+import sys
+from pathlib import Path
+
+from hermes_cli import update_completion
+
+
+class _Win32Sys:
+ """``sys`` as update_completion sees it on Windows; everything but ``platform`` is real."""
+
+ platform = "win32"
+
+ def __getattr__(self, name):
+ return getattr(sys, name)
+
+
+def _fake_windows_prepare(tmp_path, monkeypatch, acquire):
+ """Run the bootstrap's ``_prepare`` as on Windows, with PM, venv_sync and the --prepared
+ child replaced by recorders; returns the ordered events."""
+ from contextlib import nullcontext
+ import pm
+ import pm.client
+ import pm.environments
+ import pm.receipt
+ from hermes_cli import update_lock, venv_sync
+
+ events = []
+ monkeypatch.setattr(update_completion, "sys", _Win32Sys())
+ monkeypatch.setattr(update_lock, "_acquire_checkout", lambda root: acquire(events, Path(root)))
+ for name in ("refuse_foreign_owned_venv", "arm_completion", "collect_superseded_generations"):
+ monkeypatch.setattr(venv_sync, name, lambda root, _name=name: events.append(_name))
+ monkeypatch.setattr(pm.client, "ensure_tools_for_sync", lambda: events.append("tools"))
+ monkeypatch.setattr(pm, "sync_venv", lambda **kw: events.append("sync_venv"))
+ monkeypatch.setattr(pm.receipt, "worker_context", lambda update_id: nullcontext())
+ monkeypatch.setattr(pm.receipt, "last_for_update", lambda update_id: None)
+ monkeypatch.setattr(pm.environments, "project_python", lambda root: Path(sys.executable))
+ monkeypatch.setattr(pm.environments, "activation_environment", lambda root: dict(os.environ))
+ monkeypatch.setattr(update_completion.subprocess, "call", lambda *a, **kw: events.append("prepared") or 0)
+ root = tmp_path / "checkout"
+ root.mkdir()
+ request = {"source": str(root), "receipt": {"update_id": "u-l3"}, "bytecode_cache": str(tmp_path / "bc")}
+ try:
+ update_completion._prepare(request, tmp_path / "request.json", tmp_path / "result.json")
+ except RuntimeError as exc:
+ events.append(f"raised: {exc}")
+ return root, events
+
+
+def test_windows_bootstrap_holds_a_checkout_lease_before_it_writes_the_checkout(tmp_path, monkeypatch):
+ """L3: a bootstrap child the job refused runs outside the kill-on-close job and outlives a
+ killed owner; arm_completion, the sync's uv children and the generation collector must
+ not run before it joined the checkout lock with a lease of its own (R5b)."""
+ root, events = _fake_windows_prepare(
+ tmp_path, monkeypatch, lambda events, root: events.append(("lease", root)))
+ assert events[0] == ("lease", root), events
+ assert events[1:] == ["refuse_foreign_owned_venv", "arm_completion", "tools", "sync_venv",
+ "collect_superseded_generations", "prepared"]
+
+
+def test_windows_bootstrap_that_cannot_join_the_checkout_lock_writes_nothing(tmp_path, monkeypatch):
+ """L3: refused the join (a foreign update owns the checkout), the bootstrap stops before
+ arming the completion or touching the venv, never an unfenced writer."""
+ from hermes_cli.update_lock import UpdateHolder
+
+ _root, events = _fake_windows_prepare(
+ tmp_path, monkeypatch, lambda events, root: UpdateHolder(pid=4242, age_seconds=0.0))
+ assert events == ["raised: could not join the update's checkout lock (held by process 4242)"]