The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a retarget or a later update keeps the host restart debt that stood before it
Teknium2e1493f73ec8
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 18a5c1c2060..63222e7578c 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -1712,6 +1712,7 @@ def _cmd_update_impl(args, gateway_mode: bool):# _old_updater.in_historical_update(); historical on-disk updaters do not# declare this local, so only they hand off through retired shims._hermes_current_updater_frame = True+ _commit.begin_update_attempt() # nothing an earlier update in this process armed is ours (O5)git_operation = git_operation_in_progress(_m().PROJECT_ROOT)if git_operation:root = _m().PROJECT_ROOTdiff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.pyindex e406952c905..7ff0aae6bfc 100644--- a/hermes_cli/update_cmd_commit.py+++ b/hermes_cli/update_cmd_commit.py@@ -568,6 +568,21 @@ def checkout_untouched(git_cmd, root: Path, start: tuple[str, str] | None) -> boreturn head_and_branch(git_cmd, root) == start+def begin_update_attempt() -> None:+ """A new ``hermes update`` attempt owns nothing yet: no snapshot, no stake, no run start.++ The state above is per attempt, not per process: a second update in one interpreter (a+ long-lived caller) that inherited the first one's snapshot and owner would hand back the+ first update's committed debt as its own undo when its move is refused (review O5).+ """+ global _armed_snapshot, _run_start, _obligation_root, _owner+ _owner = ""+ _armed_snapshot = None+ _armed_bytes.clear()+ _run_start = None+ _obligation_root = None++def record_run_start(git_cmd, root: Path) -> None:global _run_start, _obligation_root_run_start = (list(git_cmd), head_and_branch(git_cmd, root))@@ -606,9 +621,4 @@ def preflight_refusal(git_cmd, root: Path, target_ref: str, critical_files) -> sdef reset_for_tests() -> None:- global _armed_snapshot, _run_start, _obligation_root, _owner- _owner = ""- _armed_snapshot = None- _armed_bytes.clear()- _run_start = None- _obligation_root = None+ begin_update_attempt()diff --git a/hermes_cli/update_host_obligation.py b/hermes_cli/update_host_obligation.pyindex 33320a23ea6..a31996fecec 100644--- a/hermes_cli/update_host_obligation.py+++ b/hermes_cli/update_host_obligation.py@@ -110,12 +110,13 @@ def write_host_obligation(``owner`` (an update run's commit-point token) joins the record's ``owners``: a run thatfails before its move hands back only its own stake (``release_host_obligation``), neveranother install's debt for the same SHA. The first owner also stores what it found there- (``found``), which the last owner to leave puts back.+ (``found``), which the last owner to leave puts back; an owner retargeting to another SHA+ keeps that baseline (``_baseline_for``)."""path = host_obligation_path()existing = read_host_obligation()try:- found = _found_field(path) if owner and not _owners(existing or {}) else None+ found = _baseline_for(owner, existing, path) if owner else Noneexcept OSError as exc: # unreadable is not absent: a guessed ``found`` would delete it laterlogger.debug("Could not read the host update-restart obligation: %s", exc)return False@@ -166,6 +167,25 @@ def _owners(record: dict) -> list[str]:return [str(o) for o in owners] if isinstance(owners, list) else []+def _baseline_for(owner: str, existing: Optional[dict], path: Path) -> Optional[str]:+ """What ``owner``'s release must put back (base64; ``None`` = absent): the record as it stood+ before ``owner``'s first stake in it (review R1).++ A run re-arms with one owner as it retargets (CP0 branch, origin pull, upstream fork ff), so+ "the record already has owners" never means "nothing was here": a new owner keeps the bytes+ there now, other owners' stakes included; an owner already in the record carries the baseline+ it saved then, or, when it shared the record with others, the shared record minus its own stake.+ """+ owners = _owners(existing or {})+ if owner not in owners:+ return _found_field(path)+ others = [o for o in owners if o != owner]+ if not others:+ return (existing or {}).get("found")+ shared = json.dumps({**(existing or {}), "owners": others}, indent=2, ensure_ascii=False)+ return base64.b64encode(shared.encode("utf-8")).decode("ascii")++def _found_field(path: Path) -> Optional[str]:"""The record's current bytes (base64), ``None`` when absent: what a first owner puts back.Any other read error raises: custody is never guessed."""diff --git a/tests/hermes_cli/test_update_commit_obligations.py b/tests/hermes_cli/test_update_commit_obligations.pyindex c0434bc38be..6f44eea1b7a 100644--- a/tests/hermes_cli/test_update_commit_obligations.py+++ b/tests/hermes_cli/test_update_commit_obligations.py@@ -271,3 +271,101 @@ def test_a_branch_that_moves_during_the_switch_leaves_head_and_debt_agreeing(tmpassert _git(clone, "rev-parse", "HEAD") == bassert (read_host_obligation() or {}).get("expected_sha") == bassert not interrupted_pull_marker(clone).exists()+++def _repo_at_a(tmp_path, monkeypatch) -> tuple[Path, str]:+ repo = tmp_path / "repo"+ repo.mkdir()+ _git(repo, "init", "-q", "-b", "main")+ (repo / "f").write_text("A", encoding="utf-8")+ _git(repo, "add", "-A")+ _git(repo, "commit", "-qm", "A")+ monkeypatch.setattr(commit, "_owns_live_checkout", lambda _root: False)+ return repo, _git(repo, "rev-parse", "HEAD")++[email protected](shutil.which("git") is None, reason="needs git")+def test_a_retarget_within_one_run_keeps_the_host_records_saved_baseline(tmp_path, monkeypatch):+ """One run arms B, then retargets to C (CP0 then the pull), and the checkout ends back on its+ start commit A. The second arm used to overwrite the saved baseline with "absent", so the+ disarm deleted the restart debt that stood before the run (review R1)."""+ host = host_obligation_path()+ host.parent.mkdir(parents=True, exist_ok=True)+ host.write_bytes(b"PRIOR-DEBT")+ repo, _a = _repo_at_a(tmp_path, monkeypatch)+ commit.record_run_start(["git"], repo)++ commit.arm_commit_obligations(repo, "b" * 40)+ commit.arm_commit_obligations(repo, "c" * 40)+ commit.disarm_commit_obligations()++ assert host.read_bytes() == b"PRIOR-DEBT"+++def test_a_later_runs_failed_retarget_puts_back_the_earlier_runs_debt(root):+ """Run 1 committed B and left its restart owed; run 2 (a new owner) armed C and failed before+ its move. Its release must put back run 1's record byte for byte, not delete it (review R1)."""+ host = host_obligation_path()+ host.parent.mkdir(parents=True, exist_ok=True)+ host.write_bytes(b"PRIOR-DEBT")+ commit.arm_commit_obligations(root, "b" * 40) # run 1: its move commits, nothing hands back+ run_one_record = host.read_bytes()+ commit.begin_update_attempt()++ commit.arm_commit_obligations(root, "c" * 40) # run 2+ commit.disarm_commit_obligations()++ assert host.read_bytes() == run_one_record+++def test_an_owner_leaving_a_shared_record_for_another_target_hands_the_others_stake_back(root, tmp_path):+ """Installs X and Y joined one record for SHA S; X then retargets to T and fails. Y's stake+ (and the first owner's baseline) comes back, and Y's own release then restores what stood+ before either of them (review R1)."""+ from hermes_cli.update_host_obligation import read_host_obligation++ host = host_obligation_path()+ host.parent.mkdir(parents=True, exist_ok=True)+ host.write_bytes(b"PRIOR-DEBT")+ other = tmp_path / "other-install"+ other.mkdir()+ commit.arm_commit_obligations(other, "a" * 40) # install Y+ run_y = _run_state()+ commit.begin_update_attempt()+ commit.arm_commit_obligations(root, "a" * 40) # install X joins+ commit.arm_commit_obligations(root, "d" * 40) # X retargets+ commit.disarm_commit_obligations()++ record = read_host_obligation() or {}+ assert record.get("expected_sha") == "a" * 40 and record.get("owners") == [run_y["_owner"]]+ _enter_run(run_y)+ commit.disarm_commit_obligations()+ assert host.read_bytes() == b"PRIOR-DEBT"+++def test_a_second_update_in_one_process_never_hands_back_the_first_updates_debt(root, monkeypatch):+ """Update 1 moved to B and left its tail and restart owed; update 2, in the same interpreter,+ armed C and was refused before its move. It used to reuse update 1's snapshot and owner and+ delete B's debt as its own undo (review O5). ``_cmd_update_impl`` starts a fresh attempt."""+ from types import SimpleNamespace++ from hermes_cli import update_cmd+ from hermes_cli.venv_sync import completion_pending_path++ commit.arm_commit_obligations(root, "b" * 40) # update 1: committed, nothing handed back+ tail, host = completion_pending_path(root), host_obligation_path()+ owed = {tail: tail.read_bytes(), host: host.read_bytes()}++ class _Entered(Exception):+ pass++ def stop(_root):+ raise _Entered++ monkeypatch.setattr(update_cmd, "git_operation_in_progress", stop)+ with pytest.raises(_Entered): # update 2's entry, stopped right after the attempt begins+ update_cmd._cmd_update_impl(SimpleNamespace(), gateway_mode=False)+ commit.arm_commit_obligations(root, "c" * 40)+ commit.disarm_commit_obligations()++ assert {path: path.read_bytes() if path.exists() else None for path in owed} == owed