Skip to content

The agent that grows with you

fix(update): a user's file at a path the update adds is kept aside, never deleted (review F19)

Teknium5238113db286
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index ad81c702a32..58652991599 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -378,13 +378,15 @@ def _hash_worktree(git, paths: list[str]) -> dict[str, str]:
return blobs
-def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str], list[str], set[str]] | None:
- """Paths the killed git already touched on the way to ``target``: (restore from HEAD, delete as added,
- directories git may have created for its added files).
+def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str], list[str], set[str], set[str]] | None:
+ """Paths the killed git already touched on the way to ``target``: (restore from HEAD, remove as added,
+ directories git may have created for its added files, the added ones to keep aside, not delete).
Git rewrites a file as unlink, create, write, so a kill leaves it missing, empty or cut short:
all of those count as git's, like the full new blob. Content that matches neither side and is not
- the start of a new blob is the user's own edit (e.g. a re-applied stash) and is left alone.
+ the start of a new blob is the user's own edit (e.g. a re-applied stash) and is left alone. An
+ added path holding less than a whole new blob may equally be the user's own file created there
+ after the kill (a ``touch``, a first line): it leaves the tree, but renamed aside, never deleted.
``None``: git no longer knows ``target``.
"""
if git("rev-parse", "-q", "--verify", f"{target}^{{commit}}").returncode != 0:
@@ -403,7 +405,7 @@ def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str],
entry = entries.setdefault(path, (old_mode, None if status == "A" else old_blob, []))
entry[2].append((new_mode, None if status == "D" else new_blob))
worktree_blob = _hash_worktree(git, [path for path in entries if (root / path).is_file()])
- restore, added = [], []
+ restore, added, kept = [], [], set()
for path, (old_mode, old_blob, new) in entries.items():
file, blobs = root / path, {blob for _mode, blob in new if blob}
if path not in worktree_blob:
@@ -423,11 +425,13 @@ def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str],
written = written or shown.stdout.startswith(content)
if written:
(added if old_blob is None else restore).append(path)
+ if old_blob is None and worktree_blob.get(path) not in blobs: # a whole blob is in git's objects
+ kept.add(path)
new_dirs = {str(parent) for path, (_m, old_blob, _n) in entries.items() if old_blob is None
for parent in PurePosixPath(path).parents if parent.parts}
if new_dirs:
new_dirs -= set(git("ls-tree", "-r", "-d", "--name-only", "-z", pre).stdout.split("\0"))
- return restore, added, new_dirs
+ return restore, added, new_dirs, kept
# Launches that start together after a killed update (a restarting gateway or Desktop backend next to
@@ -1036,7 +1040,7 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
marker.unlink()
print(f"⚠ Ignoring a stale interrupted-update marker: commit {target[:10]} is gone.", file=sys.stderr)
return False
- restore, added, new_dirs = written
+ restore, added, new_dirs, kept = written
if (restore or added) and foreign_lock:
print("⚠ Another git holds the index, so the files the update already wrote cannot be put back "
"now; the next launch restores them.", file=sys.stderr)
@@ -1045,7 +1049,7 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
print(("⚠ git stopped partway through writing the new code — " if after_failure else
"⚠ A previous `hermes update` was killed while git was writing the new code — ")
+ f"restoring the checkout to {pre[:10]}...", file=sys.stderr)
- failed = _put_back_paths(git, root, restore, added)
+ failed = _put_back_paths(git, root, restore, added, kept)
if failed:
# No manual recipe: a reset would also wipe the edits this restore keeps, and the
# marker stays so the next launch retries.
@@ -1126,8 +1130,9 @@ def _rollback_verified(git, git_dir: Path, pre: str, owned: set[str] | None = No
return status.returncode == 0 and not (changed if owned is None else changed & owned)
-def _put_back_paths(git, root: Path, restore: list[str], added: list[str]) -> subprocess.CompletedProcess | None:
- """Return ``restore`` to HEAD and drop ``added``; the failed git run, or None."""
+def _put_back_paths(git, root: Path, restore: list[str], added: list[str],
+ kept: set[str]) -> subprocess.CompletedProcess | None:
+ """Return ``restore`` to HEAD and drop ``added``, renaming those in ``kept`` aside; the failed git run, or None."""
if restore:
run = git("restore", "--source=HEAD", "--staged", "--worktree", "--pathspec-from-file=-",
"--pathspec-file-nul", stdin="\0".join(restore))
@@ -1137,12 +1142,24 @@ def _put_back_paths(git, root: Path, restore: list[str], added: list[str]) -> su
run = git("rm", "-q", "--cached", "--ignore-unmatch", "--pathspec-from-file=-",
"--pathspec-file-nul", stdin="\0".join(added))
for rel in added:
- (root / rel).unlink(missing_ok=True)
+ if rel in kept:
+ print(f" Kept {rel} as {_keep_aside(root / rel).name}: it may be your own file.", file=sys.stderr)
+ else:
+ (root / rel).unlink(missing_ok=True)
if run.returncode:
return run
return None
+def _keep_aside(file: Path) -> Path:
+ """Rename ``file`` to a free ``<name>.hermes-update-kept[-N]`` beside it (never importable, never clobbered)."""
+ n = 1
+ while os.path.lexists(aside := file.with_name(f"{file.name}.hermes-update-kept" + (f"-{n}" if n > 1 else ""))):
+ n += 1
+ os.rename(file, aside)
+ return aside
+
+
def relaunch_after_restore() -> None:
"""Re-run this command from the restored tree; never returns.
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index 1a524a58f15..f9dd55b87e9 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -734,3 +734,41 @@ def test_a_rollback_never_rewinds_a_branch_checked_out_at_its_target_since(tmp_p
assert _git(root, "rev-parse", "feature") == target
assert er.interrupted_pull_marker(root).exists()
+
+def _killed_move(tmp_path: Path, change) -> tuple[Path, str, str]:
+ """A checkout back on ``pre`` (one module) with a dead updater's marker for ``target`` = ``change(pre)``."""
+ root = tmp_path / "install"
+ root.mkdir()
+ _git(root, "init", "-q", "-b", "main")
+ _git(root, "config", "user.email", "[email protected]")
+ _git(root, "config", "user.name", "t")
+ (root / "core.py").write_text("OLD = 1\n", encoding="utf-8", newline="")
+ _git(root, "add", "-A")
+ _git(root, "commit", "-qm", "pre")
+ pre = _git(root, "rev-parse", "HEAD")
+ change(root)
+ _git(root, "add", "-A")
+ _git(root, "commit", "-qm", "target")
+ target = _git(root, "rev-parse", "HEAD")
+ _git(root, "reset", "-q", "--hard", pre)
+ er.interrupted_pull_marker(root).write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\n", encoding="utf-8")
+ return root, pre, target
+
+
+def test_a_users_file_at_a_path_the_update_adds_is_kept_aside_never_deleted(tmp_path):
+ """The update adds files; after the kill the user creates their own at two of those paths (a first
+ line that starts git's blob, a ``touch``). Git's whole file goes; theirs leave the tree, bytes intact."""
+ def adds(root: Path) -> None:
+ for name in ("full.py", "started.py", "touched.py"):
+ (root / name).write_text(f"{name} = 'new content'\n", encoding="utf-8", newline="")
+
+ root, pre, _target = _killed_move(tmp_path, adds)
+ (root / "full.py").write_text("full.py = 'new content'\n", encoding="utf-8", newline="") # git's own write
+ (root / "started.py").write_bytes(b"started")
+ (root / "touched.py").write_bytes(b"")
+ assert er.restore_interrupted_pull(root) is True
+ assert _git(root, "rev-parse", "HEAD") == pre and not er.interrupted_pull_marker(root).exists()
+ assert not (root / "full.py").exists()
+ assert (root / "started.py.hermes-update-kept").read_bytes() == b"started"
+ assert (root / "touched.py.hermes-update-kept").read_bytes() == b""
+ assert not (root / "started.py").exists() and not (root / "touched.py").exists()