From 05ab8766e71d9c932fa3e9253528c1203121ecb7 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 3 Oct 2026 20:15:39 +0530 Subject: [PATCH] refactor(worktree): load the reclaim merge cache only when it is used reclaim_worktrees loaded the git-cherry merge cache on every call, dry runs included, though a dry run never revalidates. It also never saved the verdicts revalidation added, so the next audit re-ran `git cherry` for any tree whose head moved after the audit (gate r2 efficiency, Low). Load it only for a real run and save it once after the loop when it grew, as audit_worktrees already does. Verdicts are unchanged: the cache is only a memo. --- hermes_cli/worktree_gc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hermes_cli/worktree_gc.py b/hermes_cli/worktree_gc.py index 387e885812..df31b873ef 100644 --- a/hermes_cli/worktree_gc.py +++ b/hermes_cli/worktree_gc.py @@ -290,7 +290,9 @@ def reclaim_worktrees( if records is None: records = audit_worktrees(repo_root, with_sizes=False) - merge_cache = _ops._load_worktree_merge_cache() + # The cache only memoizes revalidation's `git cherry`; dry runs never revalidate. + merge_cache = {} if dry_run else _ops._load_worktree_merge_cache() + cache_size_before = len(merge_cache) remote_heads = ( _ops._fetch_remote_branch_heads(repo_root) if not dry_run and any(r.verdict == "reap-keep-branch" for r in records) else None) @@ -358,6 +360,8 @@ def reclaim_worktrees( if not dry_run: with contextlib.suppress(Exception): _git(["worktree", "prune"], cwd=repo_root, timeout=15) + if len(merge_cache) != cache_size_before: + _ops._save_worktree_merge_cache(merge_cache) return actions