checkpoint_pruning.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """Checked history rewrites shared by snapshot limits and store maintenance."""
  2. from __future__ import annotations
  3. from contextlib import contextmanager
  4. from dataclasses import dataclass
  5. import os
  6. from pathlib import Path
  7. from typing import Callable
  8. class PruneError(RuntimeError):
  9. """Pruning stopped without permission to discard another snapshot."""
  10. GC_PENDING_NAME = ".gc-pending"
  11. def store_lock_path(base: Path) -> Path:
  12. base = base.resolve()
  13. return base.with_name(f".{base.name}.lock")
  14. @contextmanager
  15. def store_lock(base: Path):
  16. """Serialize whole operations, including GC and clear, across processes."""
  17. from hermes_cli.runtime_state import _lock
  18. base.parent.mkdir(parents=True, exist_ok=True)
  19. # Outside base so clear_all and legacy migration cannot replace its inode.
  20. fd = os.open(store_lock_path(base), os.O_CREAT | os.O_RDWR, 0o600)
  21. try:
  22. if not _lock(fd, wait=False):
  23. raise PruneError(f"checkpoint store is busy: {base}")
  24. yield
  25. finally:
  26. os.close(fd)
  27. @dataclass(frozen=True)
  28. class Pruner:
  29. run_git: Callable
  30. store: Path
  31. cwd: str
  32. timeout: int
  33. dir_size: Callable[[Path], int]
  34. refs_prefix: str
  35. def _git(self, args: list[str], **kwargs) -> str:
  36. ok, output, error = self.run_git(args, self.store, self.cwd, **kwargs)
  37. if not ok:
  38. raise PruneError(f"checkpoint {args[0]} failed: {error}")
  39. return output
  40. def reclaim(self) -> None:
  41. """Full ``git gc`` — tens of seconds on a GB store, so only the periodic prune calls it.
  42. A ref rewrite that ran without it leaves a marker so the next prune reclaims."""
  43. self._git(["reflog", "expire", "--expire=now", "--all"])
  44. self._git(["gc", "--prune=now", "--quiet"], timeout=self.timeout * 3)
  45. # Bare stores must remain usable when gc packs all refs.
  46. for name in ("refs/heads", "branches"):
  47. (self.store / name).mkdir(parents=True, exist_ok=True)
  48. try:
  49. (self.store / GC_PENDING_NAME).unlink()
  50. except OSError:
  51. pass
  52. def gc_pending(self) -> bool:
  53. return (self.store / GC_PENDING_NAME).exists()
  54. def _mark_gc_pending(self) -> None:
  55. try:
  56. (self.store / GC_PENDING_NAME).touch()
  57. except OSError:
  58. pass
  59. def _commits(self, ref: str) -> list[str]:
  60. commits = self._git(["rev-list", "--reverse", ref]).splitlines()
  61. if not commits:
  62. raise PruneError(f"checkpoint ref has no readable history: {ref}")
  63. return commits
  64. def _rewrite(self, ref: str, original: list[str], keep: list[str], *, gc: bool) -> None:
  65. parent = None
  66. for sha in keep:
  67. metadata = self._git(["log", "-1", "--format=%T%x00%aI%x00%cI%x00%s", sha])
  68. fields = metadata.split("\x00", 3)
  69. if len(fields) != 4:
  70. raise PruneError(f"invalid checkpoint metadata: {sha}")
  71. tree, author_date, committer_date, message = fields
  72. args = ["commit-tree", tree, "--no-gpg-sign", "-m", message]
  73. if parent is not None:
  74. args += ["-p", parent]
  75. parent = self._git(args, extra_env={"GIT_AUTHOR_DATE": author_date, "GIT_COMMITTER_DATE": committer_date})
  76. if not parent:
  77. raise PruneError("commit-tree returned no checkpoint identity")
  78. if parent is None:
  79. raise PruneError("cannot remove a project's last snapshot")
  80. # Never overwrite a snapshot published since we read this history.
  81. self._git(["update-ref", ref, parent, original[-1]])
  82. if gc:
  83. self.reclaim()
  84. else:
  85. self._mark_gc_pending()
  86. def trim(self, ref: str, keep_count: int) -> None:
  87. """Snapshot-count budget on the checkpoint-take path: rewrites the ref, defers the gc."""
  88. commits = self._commits(ref)
  89. keep_count = max(1, keep_count)
  90. if len(commits) > keep_count:
  91. self._rewrite(ref, commits, commits[-keep_count:], gc=False)
  92. def drop_one_round(self, cap_bytes: int) -> bool:
  93. """Over the cap on the take path: drop the oldest snapshot of every project once and
  94. defer the gc. One round per checkpoint converges over turns; the full loop measured
  95. against an unchanging pack once flattened every project to a single snapshot."""
  96. if cap_bytes <= 0 or self.dir_size(self.store) <= cap_bytes:
  97. return False
  98. changed = False
  99. for ref in self._git(["for-each-ref", "--format=%(refname)", self.refs_prefix]).splitlines():
  100. commits = self._commits(ref)
  101. if len(commits) > 1:
  102. self._rewrite(ref, commits, commits[1:], gc=False)
  103. changed = True
  104. return changed
  105. def enforce_size(self, cap_bytes: int) -> bool:
  106. """Periodic prune: gc per round (the measurement only moves after a repack) until the
  107. store fits. Returns whether it did; a failed Git step raises immediately."""
  108. if cap_bytes <= 0 or self.dir_size(self.store) <= cap_bytes:
  109. return True
  110. self.reclaim()
  111. if self.dir_size(self.store) <= cap_bytes:
  112. return True
  113. refs = self._git(["for-each-ref", "--format=%(refname)", self.refs_prefix]).splitlines()
  114. # Keep the existing round-robin policy, remeasure after EACH rewrite.
  115. # Twenty rounds bound maintenance when many large histories remain.
  116. for _ in range(20):
  117. changed = False
  118. for ref in refs:
  119. commits = self._commits(ref)
  120. if len(commits) <= 1:
  121. continue
  122. self._rewrite(ref, commits, commits[1:], gc=True)
  123. changed = True
  124. if self.dir_size(self.store) <= cap_bytes:
  125. return True
  126. if not changed:
  127. break
  128. return False