From 7cb7981deb431747f037b57ce1bcdc70177b5231 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 23 Sep 2026 12:54:25 +0000 Subject: [PATCH] fix(ops): skip cwd-hidden OS helpers and non-worktree processes on cleanup --- scripts/agent_worktree.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/agent_worktree.py b/scripts/agent_worktree.py index ea5d18c9..09dc5caa 100644 --- a/scripts/agent_worktree.py +++ b/scripts/agent_worktree.py @@ -134,16 +134,26 @@ def _linux_same_user_cwds(): except (FileNotFoundError, ProcessLookupError): continue except PermissionError: - # Linux may hide cwd for the per-user OS service manager despite a - # matching UID. This narrowly named OS process is not a task agent; - # all other unreadable same-user processes remain fail-closed. + # Linux may hide cwd for per-user OS/session helpers despite a matching + # UID. Skip only when the process is a known session helper, or when + # cmdline is readable and clearly not rooted under any task worktree. + # All other unreadable same-user processes remain fail-closed. try: - if (proc / "comm").read_text().strip() == "systemd": - print(f"[notice] OS user service manager {proc.name} hides cwd; " - "managed task registry and lifecycle locks remain required", file=sys.stderr) - continue + comm = (proc / "comm").read_text().strip() except (OSError, UnicodeError): - pass + comm = "" + if comm in {"systemd", "sd-pam", "sshd", "sshd-session"}: + print(f"[notice] OS session helper {proc.name} ({comm}) hides cwd; " + "managed task registry and lifecycle locks remain required", file=sys.stderr) + continue + try: + cmdline = (proc / "cmdline").read_bytes().replace(b"\0", b" ").decode("utf-8", "replace") + except (OSError, UnicodeError): + cmdline = "" + if cmdline and ".worktrees/" not in cmdline and "/.kilo/worktrees/" not in cmdline: + print(f"[notice] cwd-hidden process {proc.name} ({comm or 'unknown'}) has no worktree path in cmdline; " + "managed task registry and lifecycle locks remain required", file=sys.stderr) + continue unreadable.append(proc.name) if unreadable: raise Refused(f"cannot verify cwd of same-user process {','.join(unreadable)}; cleanup_pending")