Repository navigation
fix(bin): only reap leaked worktree processes that belong to the task - #79
Merged
Merged
Conversation
… own Teardown's leaked-process reaper matched purely by current working directory: every process on the machine whose cwd resolved under the task's worktree or tasktmp was TERMed and then KILLed, with nothing checking that it belonged to the task at all. On 2026-10-16 that reaped a shared virtual-machine helper whose cwd had wandered into a task worktree, in the same second the task was cleaned up. The cwd scan stays as the candidate source, because nothing else answers "which processes are standing in this directory". Before any signal, each candidate must now also be proved to be the task's own: it is the endpoint's leader pid, or it is in that leader's process group or session, or the leader is in its parent chain. Anything else - including a lineage that cannot be read - is reported as preserved and left running, and it can no longer hold the cleanup back either. The session is what carries the case the reaper was built for. A process the agent started sits in the agent's own process group rather than the pane shell's, so once the agent exits and it is reparented to init, the session it inherited at birth is the only identifier left. That session is read from /proc where the process table exposes it and from the controlling terminal otherwise, since macOS never exposes a session id. The leader is resolved through the backend, not by pattern: fm_backend_leader_pid reports tmux's pane_pid and herdr's pane shell pid. zellij, cmux and orca expose no per-pane pid, so they resolve no leader and preserve every candidate rather than guessing - the same answer a retried cleanup gets once its pane is already gone.
…e-return process scan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
orbstack yeah fix it like proposed.
Context: on 2026-10-16 around 01:37 UTC OrbStack's helper was killed (TERM then KILL) in the same second the lead firstmate cleaned up a task. The investigation found that teardown's leaked-process reaper (reap_task_worktree_processes in bin/fm-teardown.sh, fed by pids_with_cwd_under) kills any process on the machine whose current working directory is under the task's worktree or task temp directory, with no check that the process belongs to the task; a reproduction killed an unrelated dummy process the same way.
The proposed fix: before the reaper kills a process matched by cwd, check that it descends from the task's recorded worker/backend leader, and never touch anything else. Regression coverage: an unrelated process with its cwd under the worktree survives, and the original coverage for the task's own disowned leaked processes still passes.
What Changed
reap_task_worktree_processesinbin/fm-teardown.shnow treats a process whose cwd is under the task's worktree or tasktmp only as a candidate. Before signaling one, it checks withtask_pid_belongs_to_taskthat the process is the task's endpoint leader, shares the leader's process group or session, or has the leader in its parent chain. Group or session ids that teardown's own process shares are not used for this check. Any process that fails the check, or whose lineage can't be read, is reported as preserved and never signaled. If the leader can't be resolved, every candidate is preserved. The final "remain after N passes" refusal now counts only the task's own survivors.fm_backend_leader_pidinbin/fm-backend.shreturns the leader pid: tmuxpane_pid, or the herdr pane shell pid from the newfm_backend_herdr_pane_shell_pidinbin/backends/herdr.sh. It fails for zellij, cmux, and orca, which don't expose a pane pid, and it never returns pid 0 or 1.treehouse return --forcekills every process with its cwd in the worktree, so teardown now runsrefuse_unowned_worktree_processesfirst. It refuses before the return, listing each unowned process with its command and cwd, so they don't get killed that way instead. On backends that can't name a leader (all except tmux, herdr, and orca), the endpoint is now closed before the return. Teardown then waits up to 5 seconds for the pane's processes to exit before the pre-return scan.tests/fm-teardown.test.shadds a regression test: an unrelated process with its cwd under the worktree survives teardown. The existing tests for the task's own disowned leaked processes are kept.🤖 Generated with Claude Code
Risk Assessment
Testing
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-teardown.sh:2398- The reaper now leaves an unrelated cwd-matched process alone and, on purpose, lets teardown continue past it (2386-2398: only the task's own survivors can cause a refusal). For a pooled worktree, the next destructive step isteardown_treehouse_return→treehouse return --force "$WT"(fm-teardown.sh:1707, called around :3740). The installed treehouse (v3.1.3) describesreturnas 'return a worktree to the pool, after terminating lingering processes and verifying no foreign process remains'. The v1.8.0 source in the module cache (internal/process/terminate.go TerminateWorktreeProcesses) TERMs and then KILLs every process whose cwd is inside the worktree, skipping only its own ancestors. So in the same cleanup, a few lines later, the OrbStack-style helper this change preserves is either still killed by treehouse (the 2026-10-16 incident can still happen on the standard ship/scout pool path), or v3 refuses the return because a foreign process remains, which aborts teardown with 'treehouse return failed' and contradicts the new 'it can never hold teardown back' promise. I could not read v3.1.3's source, so which of the two happens is unconfirmed. Either way, the reaper fix alone does not make 'never touch anything else' hold end to end. Choosing a policy (pass ownership to treehouse, refuse before the return while an unowned process stands in the slot, or accept treehouse's behavior) is outside the intent's reaper-only scope. That is why this needs your decision rather than an auto-fix.bin/fm-backend.sh:941- fm_backend_leader_pid returns failure for zellij, cmux and orca, and in that case reap_task_worktree_processes (fm-teardown.sh:2292-2296) keeps every candidate and returns 0. On those backends the task's own disowned leaked processes are no longer reaped, and they no longer block teardown either, where before they caused a refusal. This matches the intent's 'never touch anything else' and the commit message acknowledges it, so it is noted only as an accepted trade-off.🔧 Fix applied.
3 issues (1 error, 1 warning, 1 info) still open:
bin/fm-teardown.sh:2398- The reaper now leaves an unrelated cwd-matched process alone and, on purpose, lets teardown continue past it (2386-2398: only the task's own survivors can cause a refusal). For a pooled worktree, the next destructive step isteardown_treehouse_return→treehouse return --force "$WT"(fm-teardown.sh:1707, called around :3740). The installed treehouse (v3.1.3) describesreturnas 'return a worktree to the pool, after terminating lingering processes and verifying no foreign process remains'. The v1.8.0 source in the module cache (internal/process/terminate.go TerminateWorktreeProcesses) TERMs and then KILLs every process whose cwd is inside the worktree, skipping only its own ancestors. So in the same cleanup, a few lines later, the OrbStack-style helper this change preserves is either still killed by treehouse (the 2026-10-16 incident can still happen on the standard ship/scout pool path), or v3 refuses the return because a foreign process remains, which aborts teardown with 'treehouse return failed' and contradicts the new 'it can never hold teardown back' promise. I could not read v3.1.3's source, so which of the two happens is unconfirmed. Either way, the reaper fix alone does not make 'never touch anything else' hold end to end. Choosing a policy (pass ownership to treehouse, refuse before the return while an unowned process stands in the slot, or accept treehouse's behavior) is outside the intent's reaper-only scope. That is why this needs your decision rather than an auto-fix.bin/fm-backend.sh:941- fm_backend_leader_pid returns failure for zellij, cmux and orca, and in that case reap_task_worktree_processes (fm-teardown.sh:2292-2296) keeps every candidate and returns 0. On those backends the task's own disowned leaked processes are no longer reaped, and they no longer block teardown either, where before they caused a refusal. This matches the intent's 'never touch anything else' and the commit message acknowledges it, so it is noted only as an accepted trade-off.bin/fm-teardown.sh:3705- This defect is in code the round-1 fix added (refuse_unowned_worktree_processes, fm-teardown.sh:2414-2437, called at :3705). On zellij and cmux, fm_backend_leader_pid always fails (fm-backend.sh:941,*) return 1). That leaves TASK_LEADER_PID empty, so task_pid_belongs_to_task returns 1 for every pid (:2175). On those backends the endpoint is only closed later, after the treehouse return (fm_backend_kill at ~:3852). So when teardown runs, the task's own live worker (the pane shell, the agent, and its children, all with cwd in $WT) is still standing in the worktree. Concrete sequence: a zellij or cmux ship/scout task finishes, and the lead runs teardown while the pane is still open (the normal case). The reaper preserves every candidate and returns 0. The refusal then lists the task's own agent as 'not provably task-x1's own' and exits 1. Every rerun refuses the same way until the operator kills the agent by hand. Before this round, those backends completed teardown. The same thing happens on tmux/herdr when the endpoint can no longer name its leader, but there the endpoint is usually gone, so that case is less severe. The user's 'refuse before the return' instruction, read literally, produces this. But it changes teardown on zellij/cmux from 'completes' to 'always refuses while the worker is alive', which the user may not have meant. Possible remedies, each a policy choice: close the endpoint before the scan on backends without a leader pid; skip the refusal when ownership cannot be established at all; or accept the manual-stop requirement. No test covers a zellij or cmux teardown with a live process in the worktree.🔧 Fix applied.
4 issues (1 error, 2 warnings, 1 info) still open:
bin/fm-teardown.sh:2398- The reaper now leaves an unrelated cwd-matched process alone and, on purpose, lets teardown continue past it (2386-2398: only the task's own survivors can cause a refusal). For a pooled worktree, the next destructive step isteardown_treehouse_return→treehouse return --force "$WT"(fm-teardown.sh:1707, called around :3740). The installed treehouse (v3.1.3) describesreturnas 'return a worktree to the pool, after terminating lingering processes and verifying no foreign process remains'. The v1.8.0 source in the module cache (internal/process/terminate.go TerminateWorktreeProcesses) TERMs and then KILLs every process whose cwd is inside the worktree, skipping only its own ancestors. So in the same cleanup, a few lines later, the OrbStack-style helper this change preserves is either still killed by treehouse (the 2026-10-16 incident can still happen on the standard ship/scout pool path), or v3 refuses the return because a foreign process remains, which aborts teardown with 'treehouse return failed' and contradicts the new 'it can never hold teardown back' promise. I could not read v3.1.3's source, so which of the two happens is unconfirmed. Either way, the reaper fix alone does not make 'never touch anything else' hold end to end. Choosing a policy (pass ownership to treehouse, refuse before the return while an unowned process stands in the slot, or accept treehouse's behavior) is outside the intent's reaper-only scope. That is why this needs your decision rather than an auto-fix.bin/fm-backend.sh:941- fm_backend_leader_pid returns failure for zellij, cmux and orca, and in that case reap_task_worktree_processes (fm-teardown.sh:2292-2296) keeps every candidate and returns 0. On those backends the task's own disowned leaked processes are no longer reaped, and they no longer block teardown either, where before they caused a refusal. This matches the intent's 'never touch anything else' and the commit message acknowledges it, so it is noted only as an accepted trade-off.bin/fm-teardown.sh:3705- This defect is in code the round-1 fix added (refuse_unowned_worktree_processes, fm-teardown.sh:2414-2437, called at :3705). On zellij and cmux, fm_backend_leader_pid always fails (fm-backend.sh:941,*) return 1). That leaves TASK_LEADER_PID empty, so task_pid_belongs_to_task returns 1 for every pid (:2175). On those backends the endpoint is only closed later, after the treehouse return (fm_backend_kill at ~:3852). So when teardown runs, the task's own live worker (the pane shell, the agent, and its children, all with cwd in $WT) is still standing in the worktree. Concrete sequence: a zellij or cmux ship/scout task finishes, and the lead runs teardown while the pane is still open (the normal case). The reaper preserves every candidate and returns 0. The refusal then lists the task's own agent as 'not provably task-x1's own' and exits 1. Every rerun refuses the same way until the operator kills the agent by hand. Before this round, those backends completed teardown. The same thing happens on tmux/herdr when the endpoint can no longer name its leader, but there the endpoint is usually gone, so that case is less severe. The user's 'refuse before the return' instruction, read literally, produces this. But it changes teardown on zellij/cmux from 'completes' to 'always refuses while the worker is alive', which the user may not have meant. Possible remedies, each a policy choice: close the endpoint before the scan on backends without a leader pid; skip the refusal when ownership cannot be established at all; or accept the manual-stop requirement. No test covers a zellij or cmux teardown with a live process in the worktree.bin/fm-teardown.sh:3720- Round 2 introduced this. It closes the zellij/cmux endpoint early (fm-teardown.sh:3711-3719) and then runs refuse_unowned_worktree_processes (:3720, defined at :2414) right away, with no wait. Both close commands only send a request and return immediately. fm_backend_zellij_kill firesaction close-tab-by-id ... || true(zellij.sh:618), and fm_backend_cmux_kill firesclose-workspace ... || true(cmux.sh:632). Neither waits for the pane's processes to exit. Once the pane closes, the shell and the agent (for example claude/node, which runs its own SIGHUP cleanup) still take a moment to exit, and lsof can still see their cwd in $WT during that time. Concrete sequence: a zellij ship task finishes and the lead runs teardown with the tab open. close-tab-by-id returns, pids_with_cwd_under still lists the agent pid a few milliseconds later, and on a leaderless backend task_pid_belongs_to_task returns 1 for every pid. Teardown then exits 1 with 'REFUSED: process(es) that are not provably task-x1's own ... Stop each one', naming the task's own agent. That is the regression the user asked round 2 to remove ('still completes for the task's own processes'). A rerun usually passes, so the refusal is flaky rather than permanent. The new test cannot catch this: its fake zellijclose-tab-by-idrunskill -KILLon the pane pid synchronously before returning (tests/fm-teardown.test.sh, add_zellij_task_pane), so the pane is always gone before the scan, and the test has no separate agent child process. Smallest fix inside round 2's own scope: after the early close, poll pids_with_cwd_under "$WT" for a few seconds, the same way the reaper already uses sleep 1 between passes, until the set stops shrinking or is empty. Only then run refuse_unowned_worktree_processes. In the test, make the fake close end the pane process asynchronously (for example( sleep 0.5; kill -TERM $2 ) &) so the test proves the wait.🔧 Fix applied.
1 info still open:
bin/fm-backend.sh:941- fm_backend_leader_pid returns failure for zellij, cmux and orca, and in that case reap_task_worktree_processes (fm-teardown.sh:2292-2296) keeps every candidate and returns 0. On those backends the task's own disowned leaked processes are no longer reaped, and they no longer block teardown either, where before they caused a refusal. This matches the intent's 'never touch anything else' and the commit message acknowledges it, so it is noted only as an accepted trade-off.⏭️ **Test** - skipped
Step was skipped.
✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.