Skip to content

The agent that grows with you

refactor(checkpoints): derive the agent-write ledger key in one place

kshitijk4poorc8176fdc4cc6
diff --git a/tools/checkpoint_manager.py b/tools/checkpoint_manager.py
index bee7ab758d2..2875148b2d2 100644
--- a/tools/checkpoint_manager.py
+++ b/tools/checkpoint_manager.py
@@ -620,7 +620,7 @@ class CheckpointManager:
digest = _hash_file(path)
if digest is None:
return
- store, dir_hash = _store_path(), _project_hash(self.get_working_dir_for_path(str(path)))
+ store, dir_hash = _store_path(), self._ledger_key(str(path))
_save_ledger(store, dir_hash, {**_load_ledger(store, dir_hash), str(path): {"sha256": digest, "ts": time.time()}})
except Exception as exc:
logger.debug("record_agent_write failed for %s: %s", file_path, exc)
@@ -637,20 +637,9 @@ class CheckpointManager:
if not ok:
return {"success": False, "error": f"Could not compute changed files: {err}"}
- ledger = _load_ledger(p.store, p.dir_hash)
- if not ledger:
- # record_agent_write keys the ledger by the marker-walk result
- # (get_working_dir_for_path), which can diverge from the dir this
- # restore names — e.g. a markerless project dir under an ancestor
- # that carries a generic project marker, or a restore of a repo
- # subdir while the write was recorded at the repo root. Fall back
- # to the walked key so safe restore never silently degrades to a
- # full restore (which overwrites user edits) just because the
- # ledger landed under a sibling key.
- walked = self.get_working_dir_for_path(working_dir)
- walk_key = _project_hash(walked)
- if walk_key != p.dir_hash:
- ledger = _load_ledger(p.store, walk_key)
+ # p.dir_hash is the exact restore dir; the ledger was written under the walked key. Reading
+ # the wrong key looked like "no ledger" and degraded to a full restore over user edits.
+ ledger = _load_ledger(p.store, self._ledger_key(p.abs_dir))
if not ledger:
return {"success": True, "restore": [], "skipped": [], "ledger_empty": True}
out: Dict[str, List[str]] = {"restore": [], "skipped": []}
@@ -832,6 +821,10 @@ class CheckpointManager:
targets.failed_deletes.append(rel)
return targets
+ def _ledger_key(self, path: str) -> str:
+ """Agent-write ledger key: hash of the marker-walked project dir, for writer and reader alike."""
+ return _project_hash(self.get_working_dir_for_path(path))
+
def get_working_dir_for_path(self, file_path: str) -> str:
"""Resolve a file path to its working directory (nearest project-marker ancestor)."""
path = _normalize_path(file_path)