The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(checkpoints): retry of a partial profile-rename rekey keeps checkpoints taken under the new name
teknium1b1b0cb230b2f
diff --git a/tests/hermes_cli/test_profile_rename_checkpoints.py b/tests/hermes_cli/test_profile_rename_checkpoints.pyindex 888663bab60..957da2b5f6c 100644--- a/tests/hermes_cli/test_profile_rename_checkpoints.py+++ b/tests/hermes_cli/test_profile_rename_checkpoints.py@@ -5,8 +5,10 @@ from unittest.mock import patchimport pytest+from hermes_cli.profile_identity import migrate_profile_identityfrom hermes_cli.profiles import create_profile, rename_profilefrom hermes_constants import reset_hermes_home_override, set_hermes_home_override+import tools.checkpoint_manager as cmfrom tools.checkpoint_manager import CheckpointManager@@ -76,3 +78,50 @@ def test_rename_preserves_profile_local_checkpoint_history(profile_env, tmp_pathassert new_tracked.read_text(encoding="utf-8") == "before\n"finally:reset_hermes_home_override(token)+++def test_retry_after_partial_rekey_keeps_checkpoints_taken_under_new_name(profile_env):+ """``migrate-identity`` after a mid-way failure must not rewind history to the old tip.++ Between the failed rename and the retry the user keeps working under the new profile, so the+ new ref already carries checkpoints (and ledger entries) the old identity never saw.+ """+ old_dir = create_profile("oldname", no_alias=True)+ workdir = old_dir / "project"+ workdir.mkdir()+ (workdir / "pyproject.toml").write_text("[project]\nname = 'retry'\n", encoding="utf-8")+ note = workdir / "note.txt"+ note.write_text("v1\n", encoding="utf-8")++ token = set_hermes_home_override(old_dir)+ try:+ assert CheckpointManager(enabled=True, max_snapshots=5).ensure_checkpoint(str(workdir), "c1") is True+ finally:+ reset_hermes_home_override(token)++ with patch("hermes_cli.profiles.check_alias_collision", return_value="skip"), \+ patch("hermes_cli.profiles._live_default_multiplexer", return_value=False), \+ patch.object(cm, "_delete_ref", return_value=False): # old ref survives: partial rekey+ new_dir = rename_profile("oldname", "newname")++ new_workdir = new_dir / "project"+ new_note = new_workdir / "note.txt"+ token = set_hermes_home_override(new_dir)+ try:+ new_note.write_text("v2\n", encoding="utf-8")+ manager = CheckpointManager(enabled=True, max_snapshots=5)+ assert manager.ensure_checkpoint(str(new_workdir), "c2") is True+ manager.record_agent_write(str(new_note))+ before = [entry["hash"] for entry in manager.list_checkpoints(str(new_workdir))]+ assert len(before) == 2+ store = cm._store_path(new_dir / "checkpoints")+ ledger_before = cm._load_ledger(store, cm._project_hash(str(new_workdir)))++ assert migrate_profile_identity("oldname", "newname") is True++ manager = CheckpointManager(enabled=True, max_snapshots=5)+ assert [entry["hash"] for entry in manager.list_checkpoints(str(new_workdir))] == before+ assert cm._load_ledger(store, cm._project_hash(str(new_workdir))) == ledger_before+ assert cm._list_project_refs(store, str(new_workdir)) == [cm._ref_name(cm._project_hash(str(new_workdir)))]+ finally:+ reset_hermes_home_override(token)diff --git a/tools/checkpoint_manager_profile_rename.py b/tools/checkpoint_manager_profile_rename.pyindex 57c2ffbaa9d..c0fb593b7f9 100644--- a/tools/checkpoint_manager_profile_rename.py+++ b/tools/checkpoint_manager_profile_rename.py@@ -35,25 +35,39 @@ def _rebase_ledger_paths(ledger: Dict, old_workdir: Path, new_workdir: Path) ->def _rekey_project(store: Path, meta: Dict, old_workdir: Path, new_workdir: Path) -> None:"""Install the project under its new hash, then drop the old identity.- Every step overwrites, so a retry (``hermes profile migrate-identity``) after a mid-way failure- simply redoes the same writes; the old metadata goes last because it is what keeps the project- visible to that retry. The per-project git index is a rebuildable cache and is discarded.+ A retry (``hermes profile migrate-identity``) after a mid-way failure finds the new identity+ already populated, possibly with checkpoints taken under the new name since. Those must win:+ the new ref is only created when absent, and only when the surviving old tip is one of its+ ancestors do we drop the old identity and fold the old ledger under the new one. A new ref+ that does not descend from the old tip is a different project's history, so fail closed rather+ than overwrite either side. The old metadata goes last because it keeps the project visible to+ the retry; the per-project git index is a rebuildable cache and is discarded."""old_hash, new_hash = meta["_hash"], cm._project_hash(str(new_workdir))- new_meta = {k: v for k, v in meta.items() if k != "_hash"}- new_meta.update({"workdir": str(new_workdir), **cm._volume_evidence(new_workdir)})+ old_ref, new_ref = cm._ref_name(old_hash), cm._ref_name(new_hash)+ old_tip = cm._ref_tip(store, str(new_workdir), old_ref)+ new_tip = cm._ref_tip(store, str(new_workdir), new_ref)+ if old_tip and new_tip:+ ok, _, _ = cm._run_git(["merge-base", "--is-ancestor", old_tip, new_tip], store, str(new_workdir),+ allowed_returncodes={1})+ if not ok:+ raise OSError(f"target identity already exists: {new_ref} does not descend from {old_ref}")meta_path = cm._project_meta_path(store, new_hash)- meta_path.parent.mkdir(parents=True, exist_ok=True)- meta_path.write_text(json.dumps(new_meta), encoding="utf-8")+ if not meta_path.exists():+ new_meta = {k: v for k, v in meta.items() if k != "_hash"}+ new_meta.update({"workdir": str(new_workdir), **cm._volume_evidence(new_workdir)})+ meta_path.parent.mkdir(parents=True, exist_ok=True)+ meta_path.write_text(json.dumps(new_meta), encoding="utf-8")old_ledger_path = cm._ledger_path(store, old_hash)if old_ledger_path.exists():- cm._save_ledger(store, new_hash, _rebase_ledger_paths(cm._load_ledger(store, old_hash), old_workdir, new_workdir))- old_ref, new_ref = cm._ref_name(old_hash), cm._ref_name(new_hash)- old_tip = cm._ref_tip(store, str(new_workdir), old_ref)+ ledger = _rebase_ledger_paths(cm._load_ledger(store, old_hash), old_workdir, new_workdir)+ ledger.update(cm._load_ledger(store, new_hash)) # writes under the new name are newer+ cm._save_ledger(store, new_hash, ledger)if old_tip:- ok, _, err = cm._run_git(["update-ref", new_ref, old_tip], store, str(new_workdir))- if not ok:- raise OSError(f"could not create {new_ref}: {err}")+ if not new_tip:+ ok, _, err = cm._run_git(["update-ref", new_ref, old_tip], store, str(new_workdir))+ if not ok:+ raise OSError(f"could not create {new_ref}: {err}")if not cm._delete_ref(store, old_ref):raise OSError(f"could not delete {old_ref}")cm._unlink_quiet(cm._project_meta_path(store, old_hash))