The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(kanban): release worker cwd before worktree cleanup
KoNit-K76901e9abbdf
diff --git a/hermes_cli/kanban_db_workspace.py b/hermes_cli/kanban_db_workspace.pyindex aa27c810a09..3bb5b543faa 100644--- a/hermes_cli/kanban_db_workspace.py+++ b/hermes_cli/kanban_db_workspace.py@@ -11,6 +11,7 @@ import osimport shutilimport sqlite3import subprocess+import timefrom pathlib import Pathfrom typing import Optionalfrom typing import TYPE_CHECKING@@ -199,9 +200,25 @@ def _cleanup_worktree_workspace(task_id, wp,)return+ # Windows cannot delete a directory while this process has its current+ # directory inside it. Completed workers normally run from their own+ # linked worktree, so move this process back to the main checkout+ # before asking Git to remove the worktree.+ worktree_path = wp.resolve(strict=False)+ try:+ cwd = Path.cwd().resolve(strict=False)+ if cwd == worktree_path or cwd.is_relative_to(worktree_path):+ os.chdir(repo_root)+ except OSError:+ return # Cannot leave the worktree safely — preserve it.# No --force: git's own dirty guard re-verifies at removal time, so if# the tree became dirty since our check (TOCTOU) removal fails safe.result = _git(repo_root, "worktree", "remove", str(wp), timeout=60)+ if result.returncode != 0:+ # Windows can retain a directory handle briefly after cwd changes.+ # Retry once without --force; Git still enforces its dirty guard.+ time.sleep(0.1)+ result = _git(repo_root, "worktree", "remove", str(wp), timeout=60)if result.returncode != 0:_kb._log.warning("git worktree remove failed for task %s at %s: %s",diff --git a/tests/hermes_cli/test_kanban_worktree_teardown.py b/tests/hermes_cli/test_kanban_worktree_teardown.pyindex 9c5100c880d..464b40f54c1 100644--- a/tests/hermes_cli/test_kanban_worktree_teardown.py+++ b/tests/hermes_cli/test_kanban_worktree_teardown.py@@ -92,6 +92,54 @@ def test_clean_pushed_worktree_removed(repo: Path) -> None:assert (repo / "README.md").exists()+def test_cleanup_leaves_a_worktree_cwd_before_removal(+ repo: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Windows cannot remove a worktree that is the process's current directory."""+ wt = _make_worktree(repo, "t_cwd112425")+ real_git = kbw._git++ def windows_git(repo_root: Path, *args: str, timeout: int) -> subprocess.CompletedProcess:+ if args[:2] == ("worktree", "remove") and Path.cwd().is_relative_to(wt):+ return subprocess.CompletedProcess(+ ["git", *args], 1, stderr="Permission denied: current directory"+ )+ return real_git(repo_root, *args, timeout=timeout)++ monkeypatch.setattr(kbw, "_git", windows_git)+ monkeypatch.chdir(wt)+ kbw._cleanup_worktree_workspace("t_cwd112425", str(wt))++ assert Path.cwd() == repo+ assert not wt.exists()+++def test_cleanup_retries_worktree_removal_once(+ repo: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A brief Windows directory-handle delay gets one safe retry."""+ wt = _make_worktree(repo, "t_retry112425")+ real_git = kbw._git+ attempts = 0++ def delayed_remove(repo_root: Path, *args: str, timeout: int) -> subprocess.CompletedProcess:+ nonlocal attempts+ if args[:2] == ("worktree", "remove"):+ attempts += 1+ if attempts == 1:+ return subprocess.CompletedProcess(+ ["git", *args], 1, stderr="Permission denied: handle pending"+ )+ return real_git(repo_root, *args, timeout=timeout)++ monkeypatch.setattr(kbw, "_git", delayed_remove)+ monkeypatch.setattr(kbw.time, "sleep", lambda _delay: None)+ kbw._cleanup_worktree_workspace("t_retry112425", str(wt))++ assert attempts == 2+ assert not wt.exists()++def test_dirty_worktree_preserved(repo: Path) -> None:wt = _make_worktree(repo, "t_bbbb2222")(wt / "wip.txt").write_text("uncommitted\n", encoding="utf-8")