fix(process): safely clean up lingering worktree processes#17
Merged
Conversation
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.
Summary
treehousesession.getandreturnreflect the new process-cleanup behavior.Risk Assessment
Testing
Pipeline
Updates from git push no-mistakes
✅ **Rebase** - passed
Round 1 - passed ✅
Round 1 - found 2 issues (1 error, 1 warning)
cmd/return_cmd.go:58-treehouse returnnow callskillLingeringProcessesbefore cleanup, so invoking it from inside the target worktree will match the currenttreehouseprocess and the caller's shell by cwd and send them SIGTERM/SIGKILL. That can kill the command beforepool.Releaseruns and abruptly terminate the user's session.cmd/return_cmd.go:58- Returning a worktree by path now unconditionally terminates every process with that worktree as cwd, even without--forceor any in-use confirmation. This is a significant behavior change from a cleanup command and can silently kill another active agent/editor session unless that UX is explicitly intended.Round 2 (auto-fix) - found 1 warning
internal/process/terminate.go:45- IfparentPIDlookup fails while protecting the current process chain, the loop stops and leaves ancestor processes unprotected. In that fallback casetreehouse returncan still terminate the invoking shell or wrapper process from inside the worktree, which is the exact failure mode this patch is trying to prevent. Make the failure path conservative by skipping termination entirely, or by treating unknown ancestors as protected instead of continuing with a partial filter.Round 3 (auto-fix) - found 2 warnings
internal/process/terminate.go:45- If walking the current process's parent chain fails,filterProtectedProcessesreturnsnil, and callers treat that as 'no lingering processes' and proceed to reset/reuse the worktree. A transient gopsutil failure here can leave background processes running against a pooled worktree with no warning.internal/process/terminate_windows.go:11- The Windows termination path firesTerminateProcessand returns immediately, butreturn/getreset the worktree right after this call. Because process teardown and handle release are asynchronous on Windows, this can race into flaky checkout/clean failures when a just-killed process still has files open.✅ **Test** - passed
Round 1 - found 0 issues
🔧 **Document** - 2 issues found → auto-fixed (2)
Round 1 - found 2 warnings
README.md:37- The Quick Start and lifecycle docs do not mention that leaving a worktree viatreehouse/treehouse getnow terminates lingering processes whose cwd is inside that worktree before returning it to the pool. This is a new user-visible behavior and should be documented where the exit flow is described.README.md:139- The CLI reference fortreehouse return [path]is now incomplete:returnalso kills lingering worktree-scoped processes before cleaning and pooling the worktree. The command description or flag/usage docs should mention this side effect so users know detached tools will be stopped.Round 2 (auto-fix) - found 2 issues (1 warning, 1 info)
cmd/return_cmd.go:20- The Cobra short help still saysReturn a worktree to the pool, but this change now terminates lingering worktree processes before release. Update the command help text sotreehouse help returnmatches the new behavior documented in the README.AGENTS.md:14-internal/process/is documented as only providing in-use detection via cwd scanning, but this change adds cross-platform termination of lingering worktree processes as well. Update the project structure note so internal docs reflect the package’s expanded responsibility.Round 3 (auto-fix) - found 0 issues
✅ **Lint** - passed
Round 1 - found 0 issues
✅ **Push** - passed
Round 1 - passed ✅