worktree-runtime-cleanup
Stop a worktree's Compose stacks and confirmed dev servers, watchers, and tool daemons, then clean up temporary files created by those runs.
SKILL.mdskills/local/worktree-runtime-cleanup/SKILL.md
GitHubWorktree Runtime Cleanup
Dev servers, watchers, tool daemons, and Compose dependencies can outlive their worktree or launcher. Tie cleanup to confirmed runtime ownership and retain verification evidence.
Worktree cleanup skills (github-worktree-cleanup,
azure-devops-worktree-cleanup, beads-merge-cleanup) read this file and run
it for the worktree they are about to remove. Review skills that run Codex
follow only Brokers Started by a Codex Review,
so a broker found at cleanup is a leak to report.
A confirmation a cleanup skill obtained from its Caller preview covers only the
runtime resources listed there with the same identifiers: processes by PID,
start time, and cwd, Compose projects with their working_dir and volumes, and
temporary file paths. Get a separate confirmation for any other resource.
Run this before git worktree remove: the live worktree helps establish runtime
ownership, and Compose stacks can be matched by path rather than the weaker
--stale reasons.
Dev Servers, Watchers, and Tool Daemons
Tools can launch detached processes in a worktree that survive the session
that started them: Codex plugin brokers (app-server-broker.mjs --cwd <worktree>) with their codex app-server, MCP servers such as Serena, and
language servers such as Expert. No launch information for them exists in the
cleanup session, so discover them from process state.
- Resolve the target worktree and list candidates with
scripts/list-processes.shin this skill's directory. The script only reads process state. A non-zero exit means the listing is unavailable, never empty: report the processes as unverified. A row can contain invalid UTF-8. Run the script under the caller's locale, check its exit status, and only then applyLC_ALL=Cto the tool that reads its saved output, as inlist="$(mktemp)" && list-processes.sh <path> >"$list" && LC_ALL=C cut -f1 "$list".- A worktree path given:
list-processes.sh <path>. - No argument:
list-processes.shtargets the current worktree root. --stalegiven:list-processes.sh --stalelists processes whose cwd or--cwdargument no longer exists, such as runtimes left by a worktree already removed. Add dev servers/watchers known from existing launch information, tool sessions, or explicit user-provided targets. A cwd or--cwdunder the target establishes ownership; a port or process name alone is insufficient. A row with reasoncwd ambiguousor--cwd ambiguousnever establishes ownership, because its cwd or--cwdmay name another directory: report it as uncertain. Interactive shells, terminal panes, and agent sessions with a tty (such aszsh,claude, orcodexTUIs) are not runtimes: report them and leave them running. Report other uncertain candidates and leave them running too.
- A worktree path given:
- Group the candidates into trees by
ppidand stop each tree from its topmost listed process, so the owner shuts down its children. Codex plugin brokers handle SIGTERM by closing theircodex app-serverand removing their socket and PID file. Preview the exact processes and owned children to stop. Obtain any required approval, honoring authorization already given for those targets. Immediately before stopping, recheck PID, start time, and launch context to detect PID reuse. If they no longer match, leave the process untouched and report it as unverified. - Prefer the documented shutdown command or owning tool session; otherwise use graceful termination. Include children only when their ownership is established, and signal a process group only when it is dedicated to this runtime. If shutdown fails, report what remains; obtain any additional approval needed for a forceful stop.
- Verify that the owned processes and their listeners have exited by re-running
the same
list-processes.shcommand. Sending a signal or ending the launcher alone does not establish cleanup. Report remaining processes and unverified ownership explicitly.
Brokers Started by a Codex Review
change-review, change-adversarial-review, and change-security-review
follow this section around each Codex companion run for a target worktree. The
run owns the brokers it starts, so stopping them needs no confirmation; the stop
covers only the processes recorded below and sends only SIGTERM. Runs started
together for one worktree as a set count as one run: record the first list
before any starts and the second after all end.
- Before the run, record
scripts/list-processes.sh <worktree>in this skill's directory. Take both lists under the same locale, becausestartedfollows it. Save each full list outside the worktree, check its exit status, and only then read it withLC_ALL=Ctools. - After the run, whether it succeeded or failed, list again. A process running
app-server-broker.mjsis a broker this run started only when it is absent from the first list, by PID and start time, and itscwdcolumn in the second list equals exactly the target worktree root. The companion starts the broker with its cwd set to the--cwdpath, so the untruncatedcwdcolumn carries that path. Thecommandcolumn is cut to 200 bytes and never establishes ownership. A missing, unreadable, or(deleted)cwd is not an exact match, nor is acwdcolumn containing a backslash: the script escapes control bytes and backslashes in the path, and acwd ambiguousrow always carries one. The list also matches paths below the target, such as a nested worktree; prefix matching never establishes ownership here. Report any other listed broker, whether pre-existing or under the target without equaling it, and leave it running. If the run started none, report that and stop here. - Record each started broker's tree from the second list: the broker and every
listed process descending from it by
ppid, with PID and start time. - Immediately before signaling, recheck each broker's PID and start time; if they no longer match, leave it and report it as unverified. Send SIGTERM to each broker.
- Re-list for up to about 10 seconds until the recorded processes exit. Send SIGTERM once to each recorded process still running whose PID and start time match, even if it was reparented. Never signal a process outside the recorded tree, and never use SIGKILL.
- Report the stopped brokers, any broker left running, and recorded processes still running. Leftovers do not change the review result; cleanup under Dev Servers, Watchers, and Tool Daemons handles them before the worktree is removed.
When list-processes.sh cannot run or exits non-zero, such as without bash,
/proc, or a working lsof, its listing is unavailable, never empty: report
the teardown as unverified and leave it to cleanup.
Compose Stacks
docker compose run --rm leaves depends_on services up, and a wrapper killed
by a tool timeout may never reach its down trap.
- Resolve the target and list its stacks with
scripts/list-stacks.shin this skill's directory. The script only reads Docker state. If Docker is unavailable, report Compose cleanup as unverified and continue with the remaining checks.- A worktree path given:
list-stacks.sh <path>. - No argument:
list-stacks.shtargets the current worktree root. --stalegiven:list-stacks.sh --stalelists stacks whoseworking_diris missing or is not a registered worktree, across every repository. It also lists projects left with only volumes (working_dirshown as-), whose owner cannot be determined from Docker state. This discovers Compose stacks only; native processes still require known launch context.
- A worktree path given:
- If nothing is listed, report that and continue with temporary-file cleanup.
- Show the listed stacks (project, running/total,
working_dir, reason) and get explicit confirmation for each project to stop.- When the target is a primary checkout (the one that owns
.git) or the list came from--stale, the stacks may belong to other sessions: list them and let the user choose, with no pre-selected default. - A stack is worktree-owned when the target is a linked worktree, the list
came from a path, and the reason is exactly
under target. For these, also preview the volumes fromdocker volume ls --filter label=com.docker.compose.project=<project>; the confirmation covers deleting them, because they are unreachable once the worktree is gone.
- When the target is a primary checkout (the one that owns
- Run
docker compose -p <project> down --remove-orphansfor each confirmed project, adding--volumesfor worktree-owned stacks. For other stacks, volumes stay; add--volumesonly when the user asks to delete data. Volumes markedexternalsurvive--volumeseither way.- A volume-only project has nothing to bring down. Delete its volumes only
when the user chooses that project: preview the names from
docker volume ls --filter label=com.docker.compose.project=<project>, then rundocker volume rmon exactly those names.
- A volume-only project has nothing to bring down. Delete its volumes only
when the user chooses that project: preview the names from
- Re-run the same
list-stacks.shcommand and report what stopped and what remains.
Temporary Runtime Files
- Identify exact sockets, PID files, temporary override configuration, or other temporary files created by those runs using existing launch information. Include the paths and ownership evidence in the cleanup preview and follow applicable deletion approvals. Resolve symlinks before deletion to establish the real target.
- Remove only confirmed run-owned temporary files after the associated runtime has stopped. Leave files in place when runtime state or ownership is uncertain. Preserve shared or user-owned files, persistent data, and verification evidence such as logs, screenshots, and test output, even when stored in a temporary directory.
- Verify removal and report stopped, remaining, and unverified runtimes, removed temporary paths, and retained evidence locations.
If a confirmed runtime remains running, report a cleanup blocker and leave its temporary files and worktree intact.