Skip to content

The agent that grows with you

fix(curator): carry nested excluded subtrees across rollback

kshitijk4poor30be83ab72a7
diff --git a/agent/curator_backup.py b/agent/curator_backup.py
index bd98ebb4514..c82930a4dae 100644
--- a/agent/curator_backup.py
+++ b/agent/curator_backup.py
@@ -42,6 +42,7 @@ from __future__ import annotations
import json
import logging
+import os
import re
import shutil
import tarfile
@@ -555,6 +556,27 @@ def _restore_cron_skill_links(snapshot_dir: Path) -> Dict[str, Any]:
+def _restore_excluded_subtrees(staged: Path, skills: Path) -> None:
+ """Move excluded subtrees (nested ``.git``/``.hub``/...) from *staged*
+ back under *skills* after a successful extract. Best-effort: a subtree
+ is only moved when its parent skill dir was restored and the target
+ does not already exist."""
+ for dirpath, dirnames, _files in os.walk(staged):
+ keep = []
+ for name in dirnames:
+ if name not in _EXCLUDE_TOP_LEVEL:
+ keep.append(name)
+ continue
+ src = Path(dirpath) / name
+ dest = skills / src.relative_to(staged)
+ if dest.parent.is_dir() and not dest.exists():
+ try:
+ shutil.move(str(src), str(dest))
+ except OSError as e:
+ logger.debug("Could not restore excluded subtree %s: %s", src, e)
+ dirnames[:] = keep
+
+
def _unstage(moved: List[Tuple[Path, Path]]) -> List[str]:
"""Move staged entries back to their original paths.
@@ -712,8 +734,13 @@ def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]
pass
return (False, f"snapshot extract failed (state restored): {e}", None)
- # Extract succeeded — the staging dir has served its purpose. The
- # user's undo handle is the safety snapshot tarball we took earlier.
+ # Extract succeeded. Snapshots never contain excluded subtrees (nested
+ # ``.git``, ``.hub``, ...), so the extract cannot restore them — carry
+ # them over from the staged copy of the live tree instead, the same way
+ # a top-level ``.git`` is preserved by never being staged at all. Then
+ # the staging dir has served its purpose; the user's undo handle is the
+ # safety snapshot tarball we took earlier.
+ _restore_excluded_subtrees(staged, skills)
try:
shutil.rmtree(staged, ignore_errors=True)
except OSError:
diff --git a/tests/agent/test_curator_backup.py b/tests/agent/test_curator_backup.py
index 215679504e6..8ab7c8846e9 100644
--- a/tests/agent/test_curator_backup.py
+++ b/tests/agent/test_curator_backup.py
@@ -548,3 +548,29 @@ def test_rollback_preserves_top_level_git(backup_env):
assert (git_dir / "HEAD").exists()
assert (git_dir / "HEAD").read_text(encoding="utf-8") == "ref: refs/heads/feature\n"
+
+
+def test_rollback_preserves_nested_git_inside_skill(backup_env):
+ """A skill that is itself a git checkout keeps its .git across rollback:
+ the snapshot excludes it, so rollback must carry it over from the live tree."""
+ cb = backup_env["cb"]
+ skills = backup_env["skills"]
+
+ _write_skill(skills, "alpha", body="v1")
+ nested_git = skills / "alpha" / ".git"
+ nested_git.mkdir()
+ (nested_git / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
+
+ snap_dir = cb.snapshot_skills(reason="snap-v1")
+ assert snap_dir is not None
+
+ _write_skill(skills, "alpha", body="v2")
+ (nested_git / "HEAD").write_text("ref: refs/heads/feature\n", encoding="utf-8")
+
+ ok, msg, _ = cb.rollback(backup_id=snap_dir.name)
+ assert ok, f"rollback failed: {msg}"
+ assert "v1" in (skills / "alpha" / "SKILL.md").read_text(encoding="utf-8")
+ # Live .git state (post-snapshot) is what survives — it was never archived.
+ assert (nested_git / "HEAD").read_text(encoding="utf-8") == "ref: refs/heads/feature\n"
+ staging = list((skills / ".curator_backups").glob(".rollback-staging-*"))
+ assert staging == [], f"staging dir left behind: {staging}"