The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a move killed after git wrote a symlink change is restored, not marked done (review N07)
Teknium3960791c22cb
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.pyindex 58652991599..81e9488c8ba 100644--- a/hermes_cli/_early_recovery.py+++ b/hermes_cli/_early_recovery.py@@ -217,6 +217,8 @@ _INTERRUPTED_PULL_MAX_AGE_SECONDS = 10 * 60# The user (or a killed updater) is mid-operation: its own state files own the tree._GIT_OPERATION_IN_PROGRESS = ("MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "rebase-merge", "rebase-apply")_REGULAR_FILE_MODES = ("100644", "100755")+# What git writes into the tree itself: files and symlinks (a gitlink, 160000, is a submodule's own checkout).+_WORKTREE_BLOB_MODES = (*_REGULAR_FILE_MODES, "120000")def git_operation_in_progress(root: Path) -> str | None:@@ -400,21 +402,28 @@ def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str],parts = diff.stdout.split("\0")for meta, path in zip(parts[::2], parts[1::2]):old_mode, new_mode, old_blob, new_blob, status = meta.lstrip(":").split()- if (old_mode if status == "D" else new_mode) not in _REGULAR_FILE_MODES:+ if (old_mode if status == "D" else new_mode) not in _WORKTREE_BLOB_MODES:continueentry = 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()])+ # A symlink's blob is its target text, read without following it; core.symlinks=false checks a+ # link out as a plain file holding that text, which hash-object already matches.+ links = {path for path in entries if os.path.islink(root / path)}+ worktree_blob = _hash_worktree(git, [path for path in entries if path not in links and (root / path).is_file()])+ worktree_blob.update({path: blob_id(os.fsencode(os.readlink(root / path)), pre) for path in links})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:written = old_blob is not None # unlinked (or deleted), not yet recreatedelif worktree_blob[path] == old_blob: # only a mode change tells whether git got here- written = (sys.platform != "win32" and any(b == old_blob and m != old_mode for m, b in new)+ written = (sys.platform != "win32" and path not in links+ and any(b == old_blob and m != old_mode for m, b in new)and bool(file.stat().st_mode & 0o100) != (old_mode == "100755"))elif worktree_blob[path] in blobs or path in unknown:written = True+ elif path in links: # git creates a symlink whole: any other target is the user's+ written = Falseelse: # git's own file cut short starts one of the new blobscontent = file.read_bytes()written = Falsediff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.pyindex f9dd55b87e9..afd19b5ace2 100644--- a/tests/hermes_cli/test_update_interrupted_pull.py+++ b/tests/hermes_cli/test_update_interrupted_pull.py@@ -735,14 +735,15 @@ def test_a_rollback_never_rewinds_a_branch_checked_out_at_its_target_since(tmp_passert _git(root, "rev-parse", "feature") == targetassert 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)``."""+def _killed_move(tmp_path: Path, change, base=lambda root: None) -> tuple[Path, str, str]:+ """A checkout back on ``pre`` (a module + ``base``) 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="")+ base(root)_git(root, "add", "-A")_git(root, "commit", "-qm", "pre")pre = _git(root, "rev-parse", "HEAD")@@ -772,3 +773,36 @@ def test_a_users_file_at_a_path_the_update_adds_is_kept_aside_never_deleted(tmp_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()+++def _regular(root: Path) -> None:+ (root / "L").unlink(missing_ok=True)+ (root / "L").write_text("L = 'regular'\n", encoding="utf-8", newline="")+++def _link(root: Path) -> None:+ (root / "L").unlink(missing_ok=True)+ os.symlink("core.py", root / "L")+++def _what_is_at(path: Path):+ """A link's target, a file's bytes, or None: never following the link."""+ if os.path.islink(path):+ return ("link", os.readlink(path))+ return ("file", path.read_bytes()) if path.exists() else None++[email protected]("posix") # creating symlinks needs Developer Mode/admin on Windows[email protected](("base", "change"), [+ (_regular, _link), (lambda root: None, _link), (_link, lambda root: (root / "L").unlink()), (_link, _regular),+], ids=["regular-to-symlink", "added-symlink", "deleted-symlink", "symlink-to-regular"])+def test_a_move_killed_after_git_wrote_a_symlink_change_is_restored(tmp_path, base, change):+ """Symlinks are blobs git moves like files: a kill after git wrote the link change (added, deleted,+ or a type change either way) is put back to ``pre``, never left with the marker spent."""+ root, pre, _target = _killed_move(tmp_path, change, base)+ before = _what_is_at(root / "L")+ change(root) # git wrote this path, then the kill came before HEAD moved+ assert er.restore_interrupted_pull(root) is True+ assert _git(root, "rev-parse", "HEAD") == pre and not er.interrupted_pull_marker(root).exists()+ assert _git(root, "status", "--porcelain", "--untracked-files=all") == ""+ assert _what_is_at(root / "L") == before