Skip to content

The agent that grows with you

fix(update): keep every pause's tree baseline in the paused-gateway gate

Tekniumd83906e05820
diff --git a/hermes_cli/update_pause_record.py b/hermes_cli/update_pause_record.py
index ed0ac38dbbc..cc113894ba0 100644
--- a/hermes_cli/update_pause_record.py
+++ b/hermes_cli/update_pause_record.py
@@ -13,8 +13,10 @@ says nothing about the other tree.
Resuming is gated on a whole tree: no interrupted-pull marker; at the pre-update HEAD, no tracked
change beyond the ones present at pause time (git died before moving HEAD); at a moved HEAD,
-dependencies current for it (a build step may rewrite tracked files there). Otherwise the record
-stays for the next launch, which runs after the interrupted-pull restore and the dependency sync.
+dependencies current for it (a build step may rewrite tracked files there). Every pause folded into
+a record keeps its own baseline (``baselines``): each one taken at the current HEAD must hold.
+Otherwise the record stays for the next launch, which runs after the interrupted-pull restore and
+the dependency sync.
Custody: every mutation (write, claim, retire, discharge) happens while holding a kernel lock on
``<record dir>/.hermes-update-paused-gateways.lock`` (flock / msvcrt, released by the kernel when
@@ -165,12 +167,13 @@ def tracked_changes(root: Path) -> list[str] | None:
def stamp_tree(token: dict, root: Path | None = None) -> dict:
- """Record what "the tree before this update" was, for the whole-tree gate."""
+ """Record what "the tree before this update" was, for the whole-tree gate: one baseline per
+ pause (idempotent for this token's ``pause_id``), next to the baselines of the pauses it absorbed."""
root = root or install_root()
- if (root / ".git").exists():
- token.setdefault("pre_sha", head_sha(root))
- token.setdefault("dirty_at_pause", tracked_changes(root))
- token.setdefault("pause_id", uuid.uuid4().hex)
+ pause_id = token.setdefault("pause_id", uuid.uuid4().hex)
+ baselines = token.setdefault("baselines", [])
+ if (root / ".git").exists() and not any(b.get("pause_id") == pause_id for b in baselines):
+ baselines.append({"pause_id": pause_id, "pre_sha": head_sha(root), "dirty_at_pause": tracked_changes(root)})
return token
@@ -192,14 +195,18 @@ def tree_is_whole(token: dict, root: Path | None = None) -> tuple[bool, str]:
head = head_sha(root)
if head is None:
return False, "the checkout HEAD is unreadable"
- if head == token.get("pre_sha"):
- # HEAD never moved: a new tracked change is git's half-written checkout.
+ at_head = [b for b in token.get("baselines") or [] if b.get("pre_sha") == head]
+ if at_head:
+ # HEAD never moved for these pauses: a tracked change one of them did not see is git's
+ # half-written checkout. Each is judged on its own set — a killed run that moved HEAD
+ # elsewhere vouches for nothing here, and this run's set never certifies an older one's.
changes = tracked_changes(root)
if changes is None:
return False, "git cannot read the checkout state"
- unexpected = sorted(set(changes) - set(token.get("dirty_at_pause") or []))
- if unexpected:
- return False, f"the checkout has {len(unexpected)} file(s) git left half-written (e.g. {unexpected[0]})"
+ for baseline in at_head:
+ unexpected = sorted(set(changes) - set(baseline.get("dirty_at_pause") or []))
+ if unexpected:
+ return False, f"the checkout has {len(unexpected)} file(s) git left half-written (e.g. {unexpected[0]})"
return True, ""
if not _venv_is_current(root):
return False, "dependencies are not current for the updated code yet"
@@ -514,7 +521,7 @@ def record_pause(token: dict, adopted: dict | None, claims: list[Path]) -> dict:
return token
-_CARRIED = ("pause_id", "pre_sha", "dirty_at_pause", "identities", "stop_requested", "stop_sent", "stopper_pid",
+_CARRIED = ("pause_id", "baselines", "identities", "stop_requested", "stop_sent", "stopper_pid",
"stop_markers", "absorbed")
@@ -533,7 +540,7 @@ def abandon_pause(intended: dict, adopted: dict | None) -> None:
if adopted is None:
discharge(intended)
return
- carried = {key: intended[key] for key in ("pause_id", "pre_sha", "dirty_at_pause", "absorbed") if key in intended}
+ carried = {key: intended[key] for key in ("pause_id", "baselines", "absorbed") if key in intended}
write({**adopted, **carried}, owner=UNOWNED)
@@ -604,11 +611,11 @@ def merge_into(token: dict | None, adopted: dict) -> dict:
obligation id (and every id it absorbed) is recorded so a leftover copy is never resumed twice."""
token = token if token is not None else {"resume_needed": True, "profiles": {}, "unmapped_pids": [], "unmapped": []}
token["resume_needed"] = True
- # The oldest tree evidence wins: stamping the current tree instead would certify whatever a
- # killed update left half-written as "dirty before the pause".
- for key in ("pre_sha", "dirty_at_pause"):
- if key in adopted:
- token.setdefault(key, adopted[key])
+ # Every pause keeps its own tree evidence: the adopted baseline stops this run's stamp from
+ # certifying what a killed update left half-written as "dirty before the pause", and this
+ # run's own baseline still guards the HEAD a killed update moved to.
+ baselines = token.setdefault("baselines", [])
+ baselines.extend(b for b in adopted.get("baselines") or [] if b not in baselines)
absorbed = token.setdefault("absorbed", [])
absorbed.extend(i for i in [adopted.get("pause_id"), *(adopted.get("absorbed") or [])] if i and i not in absorbed)
profiles = token.setdefault("profiles", {})
diff --git a/tests/hermes_cli/test_update_pause_record.py b/tests/hermes_cli/test_update_pause_record.py
index 4a31f40b38f..7353f5389ef 100644
--- a/tests/hermes_cli/test_update_pause_record.py
+++ b/tests/hermes_cli/test_update_pause_record.py
@@ -134,6 +134,31 @@ def test_an_update_adopting_an_orphan_never_certifies_the_tree_it_left_torn(tmp_
assert not whole and "a.py" in why, "adoption certified the torn tree as the pre-update baseline"
+def test_an_adopted_baseline_never_replaces_this_runs_own(tmp_path, monkeypatch):
+ """Review W1: the killed run moved HEAD X->Y; this run starts at Y and its git dies mid-checkout
+ without moving HEAD. The orphan's X baseline says nothing about Y — this run's own must still gate."""
+ root = tmp_path / "checkout"
+ root.mkdir()
+ _git(root, "init", "-q")
+ (root / "a.py").write_text("v1\n", encoding="utf-8")
+ _git(root, "add", ".")
+ _git(root, "commit", "-qm", "X")
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))
+ monkeypatch.setattr(pause_record, "install_root", lambda: root)
+ pause_record.write(pause_record.stamp_tree({"resume_needed": True, "profiles": {"default": 4242}}),
+ owner=pause_record.UNOWNED)
+ (root / "a.py").write_text("v2\n", encoding="utf-8")
+ _git(root, "commit", "-qam", "Y") # the killed update moved HEAD, synced Y's dependencies, then died
+ monkeypatch.setattr(pause_record, "_venv_is_current", lambda root: True)
+
+ adopted, claims = pause_record.adopt_orphans()
+ token = pause_record.record_pause({"resume_needed": True, "profiles": {"beta": 99}}, adopted, claims)
+ assert pause_record.tree_is_whole(token, root) == (True, ""), "a clean tree at this run's own HEAD must pass"
+ (root / "a.py").write_text("half-written\n", encoding="utf-8") # this run's git died at HEAD Y
+ whole, why = pause_record.tree_is_whole(pause_record.read()["token"], root)
+ assert not whole and "a.py" in why, "this run's own baseline was dropped for the adopted one"
+
+
def _orphan(tmp_path: Path, profiles: dict) -> None:
"""A record whose owner — a real ``hermes update`` stand-in — was SIGKILLed after writing it."""
owner = _child("""