The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: same-HEAD gateway admission is judged on the move the update recorded
Tekniumc853032a79cb
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex ffffa799d1a..8c8c68ebe06 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -29,6 +29,7 @@ from pm.receipt import accept_worker_receipt as _accept_completion_pm_receiptfrom hermes_cli import update_receipt as _completion_receipt, update_cmd_config as _completion_configfrom hermes_cli._old_updater import stop_for_relaunchfrom hermes_cli._early_recovery import git_operation_in_progress, interrupted_pull_marker+from hermes_cli import update_pause_record as _pause_recordfrom hermes_cli import update_cmd_check as _check# Re-exports: every split-module name stays reachable (and monkeypatchable) as update_cmd.<name>.@@ -985,6 +986,8 @@ def _pull_updates(encoding="utf-8")try:try:+ # The paused gateways' tree gate must know this move's target before git writes a file.+ _pause_record.mark_move(_windows_gateway_resume, target_sha)# merge --ff-only the already-fetched ref instead of `git pull`, which would do a# SECOND network fetch; identical in effect given the fresh tracking ref.if merge_ref != f"origin/{branch}":diff --git a/hermes_cli/update_pause_record.py b/hermes_cli/update_pause_record.pyindex aede61c822d..6d9deb67584 100644--- a/hermes_cli/update_pause_record.py+++ b/hermes_cli/update_pause_record.py@@ -210,18 +210,51 @@ def stamp_tree(token: dict, root: Path | None = None) -> dict: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)})+ dirty = tracked_changes(root)+ # The bytes, not just the names: an autostashed edit and git's half-written bytes share a path.+ baselines.append({"pause_id": pause_id, "pre_sha": head_sha(root), "dirty_at_pause": dirty,+ "dirty_digests": {path: _digest(root / path) for path in dirty or []}})return token+def _digest(path: Path) -> str | None:+ try:+ return hashlib.sha256(path.read_bytes()).hexdigest()+ except OSError: # deleted (or unreadable) at pause: it must read the same way to count as unchanged+ return None+++def mark_move(token: dict | None, target: str) -> None:+ """Persist, BEFORE git writes the tree, the commit this run's checkout move goes to, on this+ pause's own baseline: an unmoved HEAD is later judged against what THAT move could write, never+ against refs a later fetch replaced. Raises when it cannot be recorded: the move must not run."""+ if not token or not token.get("pause_id") or not target:+ return+ for baseline in token.get("baselines") or []:+ if baseline.get("pause_id") == token["pause_id"]:+ baseline["move_targets"] = sorted({*baseline.get("move_targets", []), target})+ write({**token, "resume_needed": True})++_MAX_MOVE_TARGETS = 16+def _paths_between(root: Path, targets) -> set[str] | None:+ """Tracked paths that differ between HEAD and any of *targets* — all a fast-forward, reset or+ merge toward them can write; ``None`` when git cannot say."""+ paths: set[str] = set()+ for target in sorted(targets):+ diff = _git(root, "diff", "--name-only", "-z", "--no-renames", "HEAD", target, "--")+ if diff is None or diff.returncode != 0:+ return None+ paths.update(filter(None, diff.stdout.split("\0")))+ return paths++def _paths_a_move_could_write(root: Path) -> set[str] | None:- """Tracked paths an update's checkout move could have written while HEAD stayed put: the ones- that differ between HEAD and a commit the checkout fetched (``FETCH_HEAD``) or tracks- (``refs/remotes``) — a fast-forward, reset or merge only writes those. ``None`` when git cannot- say or nothing was fetched (the move's target is then unknown)."""+ """For a pause with no recorded move (:func:`mark_move`: none attempted, or a record from before+ it was kept): the paths between HEAD and a commit the checkout fetched (``FETCH_HEAD``) or tracks+ (``refs/remotes``). ``None`` when git cannot say or nothing was fetched (target unknown)."""where = _git(root, "rev-parse", "--git-path", "FETCH_HEAD")refs = _git(root, "for-each-ref", "--format=%(objectname)", "refs/remotes")if where is None or where.returncode != 0 or refs is None or refs.returncode != 0:@@ -235,13 +268,16 @@ def _paths_a_move_could_write(root: Path) -> set[str] | None:targets = {line.split()[0] for line in fetched if line.split()} | set(refs.stdout.split())if not targets or len(targets) > _MAX_MOVE_TARGETS:return None- paths: set[str] = set()- for target in sorted(targets):- diff = _git(root, "diff", "--name-only", "-z", "--no-renames", "HEAD", target, "--")- if diff is None or diff.returncode != 0:- return None- paths.update(filter(None, diff.stdout.split("\0")))- return paths+ return _paths_between(root, targets)+++def _seen_at_pause(root: Path, baseline: dict, path: str) -> bool:+ """*path* is as the pause saw it: dirty then, with the same bytes now (a baseline from before+ digests were kept has only the name)."""+ if path not in (baseline.get("dirty_at_pause") or []):+ return False+ digests = baseline.get("dirty_digests")+ return digests is None or digests.get(path) == _digest(root / path)def _half_written(root: Path, changes: list[str], at_head: list[dict]) -> list[str]:@@ -249,13 +285,14 @@ def _half_written(root: Path, changes: list[str], at_head: list[dict]) -> list[sbaseline that has any. A change the pause did not see counts only on a path the update's movecould write: a build/sync step (or the user) rewriting any other tracked file is not git'sdoing, and holding the set for it would keep gateways stopped until someone cleans the file.- When the move's target is unknown every unseen change still counts."""- writable: set[str] | None = None- probed = False+ The move is the one recorded before it ran, so a later fetch never narrows it; with none+ recorded the fetched refs stand in, and when even those are unknown every unseen change counts."""for baseline in at_head:- unexpected = sorted(set(changes) - set(baseline.get("dirty_at_pause") or []))- if unexpected and not probed:- writable, probed = _paths_a_move_could_write(root), True+ unexpected = sorted(path for path in changes if not _seen_at_pause(root, baseline, path))+ if not unexpected:+ continue+ targets = baseline.get("move_targets")+ writable = _paths_between(root, targets) if targets else _paths_a_move_could_write(root)torn = unexpected if writable is None else [path for path in unexpected if path in writable]if torn:return torndiff --git a/tests/hermes_cli/test_update_pause_record.py b/tests/hermes_cli/test_update_pause_record.pyindex ed0c499a0de..9d9795a49f8 100644--- a/tests/hermes_cli/test_update_pause_record.py+++ b/tests/hermes_cli/test_update_pause_record.py@@ -840,3 +840,56 @@ def test_an_unmoved_head_holds_gateways_only_for_paths_the_updates_move_could_wrtoken = pause_record.stamp_tree({"resume_needed": True}, root)(root / "b.lock").write_text("rewritten by the dependency sync\n", encoding="utf-8")assert pause_record.tree_is_whole(token, root) == (True, "")+++def _cloned_checkout(tmp_path: Path) -> tuple[Path, Path]:+ origin, root = tmp_path / "origin", tmp_path / "checkout"+ origin.mkdir()+ _git(origin, "init", "-q")+ for name in ("a.py", "b.py"):+ (origin / name).write_text("v1\n", encoding="utf-8")+ _git(origin, "add", ".")+ _git(origin, "commit", "-qm", "X")+ _git(tmp_path, "clone", "-q", str(origin), str(root))+ (origin / "a.py").write_text("v2\n", encoding="utf-8")+ _git(origin, "commit", "-qam", "B") # the update's target changes a.py+ return origin, root+++def test_a_later_fetch_never_certifies_bytes_an_earlier_recorded_move_left(tmp_path, monkeypatch):+ """Review D: the move's target goes on the record before git writes; a later fetch whose refs+ no longer touch a.py must not turn the gate from false to true on the same partial bytes."""+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))+ origin, root = _cloned_checkout(tmp_path)+ token = pause_record.stamp_tree({"resume_needed": True, "profiles": {"default": 4242}}, root)+ _git(root, "fetch", "-q", "origin")+ pause_record.mark_move(token, _git(root, "rev-parse", "origin/HEAD"))+ (root / "a.py").write_text("v2 half\n", encoding="utf-8") # git wrote a.py, died before HEAD+ assert not pause_record.tree_is_whole(token, root)[0], "premise: the torn path holds the set"++ (origin / "a.py").write_text("v1\n", encoding="utf-8")+ (origin / "b.py").write_text("v2\n", encoding="utf-8")+ _git(origin, "commit", "-qam", "C") # a.py back to X's bytes: no fetched ref touches it now+ _git(root, "fetch", "-q", "origin")+ saved = pause_record.read()["token"]+ for judged in (token, saved):+ whole, why = pause_record.tree_is_whole(judged, root)+ assert not whole and "a.py" in why, "a later fetch certified the earlier move's partial bytes"+++def test_an_autostashed_edit_vouches_only_for_its_own_bytes(tmp_path, monkeypatch):+ """Review D / F3: a.py was dirty at pause, the update stashed it and its move wrote a.py partly.+ The pathname alone no longer admits the different bytes; the restored edit itself still passes."""+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))+ _origin, root = _cloned_checkout(tmp_path)+ (root / "a.py").write_text("user edit\n", encoding="utf-8")+ token = pause_record.stamp_tree({"resume_needed": True, "profiles": {"default": 4242}}, root)+ _git(root, "stash", "-q")+ _git(root, "fetch", "-q", "origin")+ pause_record.mark_move(token, _git(root, "rev-parse", "origin/HEAD"))+ (root / "a.py").write_text("v2 half\n", encoding="utf-8")+ whole, why = pause_record.tree_is_whole(token, root)+ assert not whole and "a.py" in why, "the dirty pathname admitted git's half-written bytes"+ _git(root, "checkout", "-q", "--", "a.py")+ _git(root, "stash", "pop", "-q") # the user's own edit back, byte for byte+ assert pause_record.tree_is_whole(token, root) == (True, "")