Skip to content

The agent that grows with you

fix(worktree): preserve commits without remote refs

KoNit-K1039c15a0361
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index 72efdd1174d..26d1d6d0b47 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -370,14 +370,15 @@ def _setup_worktree(repo_root: str = None, sync_base: bool = True,
def _worktree_has_unpushed_commits(worktree_path: str, timeout: int = 10) -> bool:
"""Whether a worktree has commits unreachable from any remote branch. Fails SAFE toward True.
- No remote-tracking refs = no baseline -> False. A shallow boundary can disconnect an older
- HEAD from origin/* so public commits look unpushed; ``_deepen_shallow_repo`` first if affordable.
+ No remote-tracking refs = no baseline -> True: without a reachable upstream, cleanup cannot
+ prove that local commits are redundant. A shallow boundary can disconnect an older HEAD from
+ origin/* so public commits look unpushed; ``_deepen_shallow_repo`` first if affordable.
"""
try:
remote_refs = _git_out(["for-each-ref", "--format=%(refname)", "refs/remotes"], worktree_path,
timeout=timeout)
if not remote_refs:
- return remote_refs is None # no remote-tracking refs: nothing to be unpushed against
+ return True # no remote-tracking refs: no baseline can prove local work was pushed
unpushed = _git_out(["log", "--oneline", "HEAD", "--not", "--remotes"], worktree_path,
timeout=timeout)
return unpushed is None or bool(unpushed)
diff --git a/tests/hermes_cli/test_worktree_gc.py b/tests/hermes_cli/test_worktree_gc.py
index 0952525e450..1844fbfd72f 100644
--- a/tests/hermes_cli/test_worktree_gc.py
+++ b/tests/hermes_cli/test_worktree_gc.py
@@ -106,6 +106,30 @@ class TestAuditVerdicts:
assert record.verdict == "keep"
assert "unpushed" in record.reason
+ def test_unique_commits_without_a_remote_keep_tree_and_branch(self, tmp_path, monkeypatch):
+ """No remote means there is no baseline that can prove local work is merged."""
+ monkeypatch.setenv("HOME", str(tmp_path / "home"))
+ (tmp_path / "home").mkdir()
+ local_repo = tmp_path / "local-repo"
+ _git(["init", "-b", "main", str(local_repo)], tmp_path)
+ (local_repo / "README.md").write_text("hello\n")
+ _git(["add", "."], local_repo)
+ _git(["commit", "-m", "init"], local_repo)
+ (local_repo / ".worktrees").mkdir()
+
+ tree, branch = _add_worktree(local_repo, "hermes-local-work")
+ (tree / "new.py").write_text("x = 1\n")
+ _git(["add", "."], tree)
+ _git(["commit", "-m", "unique local work"], tree)
+
+ records = worktree_gc.audit_worktrees(str(local_repo), with_sizes=False)
+ record = _verdict(records, "hermes-local-work")
+ assert record.verdict == "keep"
+ assert "unpushed" in record.reason
+ assert worktree_gc.reclaim_worktrees(str(local_repo), records=records) == []
+ assert tree.exists()
+ assert _git(["rev-parse", "--verify", branch], local_repo)
+
def test_patch_equivalent_commits_reap(self, repo):
"""The squash/rebase-merge leak: local commit unreachable from any
remote ref but patch-equivalent to an upstream commit → merged work."""