checkpoint_manager_profile_rename.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Rekey profile-local checkpoint projects after a named profile directory moves (#112973).
  2. The checkpoint store lives under ``HERMES_HOME/checkpoints`` and therefore moves with a renamed
  3. profile, but project identity inside it (ref, ``projects/<hash>.json``, agent-write ledger) is a
  4. hash of the absolute workdir. Every workdir beneath the profile home gets a new hash after the
  5. rename, so its history stays on disk yet unreachable from the new path until it is rekeyed here.
  6. """
  7. from __future__ import annotations
  8. import json
  9. import logging
  10. import shutil
  11. from pathlib import Path
  12. from typing import Dict
  13. from tools import checkpoint_manager as cm
  14. from tools import checkpoint_maintenance as maintenance
  15. logger = logging.getLogger(__name__)
  16. def _rebase_ledger_paths(ledger: dict, old_workdir: Path, new_workdir: Path) -> dict:
  17. """Move absolute ledger keys under ``old_workdir`` to the corresponding new path."""
  18. rebased = {}
  19. for raw_path, entry in ledger.items():
  20. try:
  21. relative = Path(raw_path).relative_to(old_workdir)
  22. except (TypeError, ValueError):
  23. rebased[raw_path] = entry
  24. else:
  25. rebased[str(new_workdir / relative)] = entry
  26. return rebased
  27. def _rekey_project(store: Path, meta: dict, old_workdir: Path, new_workdir: Path) -> None:
  28. """Install the project under its new hash, then drop the old identity.
  29. A retry (``hermes profile migrate-identity``) after a mid-way failure finds the new identity
  30. already populated, possibly with checkpoints taken under the new name since. Those must win:
  31. the new ref is only created when absent, and only when the surviving old tip is one of its
  32. ancestors do we drop the old identity and fold the old ledger under the new one. A new ref
  33. that does not descend from the old tip is a different project's history, so fail closed rather
  34. than overwrite either side. The old metadata goes last because it keeps the project visible to
  35. the retry; the per-project git index is a rebuildable cache and is discarded.
  36. """
  37. old_hash, new_hash = meta["_hash"], cm._project_hash(str(new_workdir))
  38. old_ref, new_ref = cm._ref_name(old_hash), cm._ref_name(new_hash)
  39. old_tip = cm._ref_tip(store, str(new_workdir), old_ref)
  40. new_tip = cm._ref_tip(store, str(new_workdir), new_ref)
  41. if old_tip and new_tip:
  42. ok, _, _ = cm._run_git(["merge-base", "--is-ancestor", old_tip, new_tip], store, str(new_workdir),
  43. allowed_returncodes={1})
  44. if not ok:
  45. raise OSError(f"target identity already exists: {new_ref} does not descend from {old_ref}")
  46. meta_path = cm._project_meta_path(store, new_hash)
  47. if not meta_path.exists():
  48. new_meta = {k: v for k, v in meta.items() if k != "_hash"}
  49. new_meta.update({"workdir": str(new_workdir), **cm._volume_evidence(new_workdir)})
  50. meta_path.parent.mkdir(parents=True, exist_ok=True)
  51. meta_path.write_text(json.dumps(new_meta), encoding="utf-8")
  52. old_ledger_path = cm._ledger_path(store, old_hash)
  53. if old_ledger_path.exists():
  54. ledger = _rebase_ledger_paths(cm._load_ledger(store, old_hash), old_workdir, new_workdir)
  55. ledger.update(cm._load_ledger(store, new_hash)) # writes under the new name are newer
  56. cm._save_ledger(store, new_hash, ledger)
  57. if old_tip:
  58. if not new_tip:
  59. ok, _, err = cm._run_git(["update-ref", new_ref, old_tip], store, str(new_workdir))
  60. if not ok:
  61. raise OSError(f"could not create {new_ref}: {err}")
  62. if not maintenance._delete_ref(store, old_ref):
  63. raise OSError(f"could not delete {old_ref}")
  64. cm._unlink_quiet(cm._project_meta_path(store, old_hash))
  65. cm._unlink_quiet(old_ledger_path)
  66. cm._unlink_quiet(cm._index_path(store, old_hash))
  67. def migrate_profile_checkpoint_projects(old_profile_dir: Path, new_profile_dir: Path) -> dict[str, int]:
  68. """Rekey checkpoint projects whose workdirs moved with a profile rename.
  69. Only workdirs beneath ``old_profile_dir`` are affected: an external workdir keeps its absolute
  70. path (and hash) even though the store holding its history moved. A workdir that no longer
  71. exists under the new profile dir did not move and keeps its (orphaned) identity.
  72. """
  73. old_root = cm._normalize_path(str(old_profile_dir))
  74. new_root = cm._normalize_path(str(new_profile_dir))
  75. store = cm._store_path(new_root / "checkpoints")
  76. result = {"scanned": 0, "migrated": 0, "errors": 0}
  77. if not cm._store_has_head(store):
  78. return result
  79. if shutil.which("git") is None:
  80. # Without git the ref cannot move; rekeying only the metadata would orphan the history.
  81. logger.warning("Cannot migrate checkpoint projects after profile rename: git not found")
  82. result["errors"] = 1
  83. return result
  84. for meta in cm._list_projects(store):
  85. result["scanned"] += 1
  86. old_workdir = cm._normalize_path(str(meta.get("workdir") or ""))
  87. if not meta.get("workdir") or not old_workdir.is_relative_to(old_root):
  88. continue
  89. new_workdir = new_root / old_workdir.relative_to(old_root)
  90. if not new_workdir.is_dir():
  91. continue
  92. try:
  93. _rekey_project(store, meta, old_workdir, new_workdir)
  94. result["migrated"] += 1
  95. except OSError as exc:
  96. result["errors"] += 1
  97. logger.warning("Cannot migrate checkpoint project %s -> %s after profile rename: %s",
  98. old_workdir, new_workdir, exc)
  99. return result