Repository navigation
fix: clean up worker Claude Code temp directories during teardown - #60
Merged
Merged
Conversation
After the work is landed or discardable and the worker has stopped, teardown deletes only that worker's Claude Code session folder.
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
Clean up worker temp files in RAM-backed /tmp at cleanup time.
Found 2026-11-07: /tmp on this server is a RAM-backed tmpfs (about 7.9 GB). It hit 99% (7.7 of 7.9 GB). Claude Code session temp folders of finished workers, under /tmp/claude-/<slug of the worker's working directory>/, held about 5.5 GB; three folders of 1.3-1.6 GB came from one torn-down Opus re-check alone. A PurpleVoice CI suite was killed (exit 137) while starting Supabase. Firstmate removed those folders by hand. Fix idea: cleanup (fm-teardown) removes the finished worker's own session temp folder.
Part of the firstmate upkeep batch the captain approved (Q1931 A sweep; Q1994: "use grok and deepseek" - Grok builds).
What Changed
Risk Assessment
✅ Low: The removal is well guarded and runs at the right point. It only runs when teardown still owns the worktree, it fails open, and it never changes the teardown result. It removes only the exact slug folder, and only when that folder is a real directory owned by the current user and not a protected path. Non-secondmate tasks remove it after the reap and the landed-or-discardable check. Secondmates remove it only after the endpoint is killed and home removal succeeds. All three earlier fix-round items hold in the current code, and their tests assert behavior: the guard warnings, the completion marker, the record removal, and the folder state.
Testing
The base reproduced the leak; focused behavioral checks and isolated CLI scenarios passed with real Git, Treehouse, tmux, and permission failures. Transcripts capture results. Session payloads were planted; no Claude model session was launched. All labs were removed and the worktree is clean.
Evidence: Teardown evidence, including baseline reproduction
Source: Teardown evidence, including baseline reproduction
Evidence: Private tmux secondmate transcript
Source: Private tmux secondmate transcript
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 3 issues found → auto-fixed (2) ✅
bin/fm-teardown.sh:4605- The comment at 4349 says "The worker is stopped", but for kind=secondmate that is not yet true at this call. Line 4335 skips the process reap for secondmates. For tmux, zellij, rex and orca secondmates, the secondmate's Claude session is only stopped later: the endpoint kill around line 4787-4797 runs after this call, and remove_firstmate_home runs at line 4810. A secondmate record has worktree=<home> (bin/fm-spawn.sh:1632), so this call deletes /tmp/claude-<uid>/<home slug> while that secondmate's Claude Code session is still running in the home. Several later steps can still exit 1 and leave the secondmate running: endpoint_close_refusal, a failed t3code kill/archive, and a refused remove_firstmate_home or handoff_wake_retire_stage. In those cases the live secondmate has lost its session temp, including the output files of its running background tasks. Fix: for KIND=secondmate, move the call to after the endpoint kill and the successful remove_firstmate_home (around line 4824), or skip secondmates; non-secondmate kinds can keep the current position after the reap. Update the comment to match.bin/fm-teardown.sh:4370- Simplification: the branch for slugs longer than 200 characters goes beyond what the intent needs. It covers teardown_claude_to_int32, teardown_claude_java_hash, teardown_claude_base36, the long-slug path in teardown_claude_project_slug (about 70 lines plus header text), and test ct7. The intent is to remove finished workers' session temp on this server, where worktree paths are far shorter than 200 characters. The branch also copies an internal Claude Code hash that the repo cannot check: a native Bun build may use a different hash for long paths. If it does, the branch quietly matches nothing. Recommended: remove the branch and treat a slug over 200 characters like any other path that cannot be matched (skip it and print the existing warning).tests/fm-teardown.test.sh:7099- Test ct4 (relative worktree path) sets worktree=relative/wt but never checks rc or how far teardown got. It passes if teardown refuses early, before reaching teardown_remove_claude_session_temp, so it does not prove that the relative-path guard works. Fix: assert the expected exit code or a marker printed after the cleanup step, or drop the test if teardown always refuses relative worktrees before this point.🔧 Fix applied.
2 infos still open:
tests/fm-teardown.test.sh:7099- Test ct4 (relative worktree path) sets worktree=relative/wt but never checks rc or how far teardown got. It passes if teardown refuses early, before reaching teardown_remove_claude_session_temp, so it does not prove that the relative-path guard works. Fix: assert the expected exit code or a marker printed after the cleanup step, or drop the test if teardown always refuses relative worktrees before this point.tests/fm-teardown.test.sh:7006- Test ct1 plants session folders for $ROOT (the firstmate home or code root) and for the primary checkout, then asserts they survive. The teardown's worktree is still $case_dir/wt, so those folders would survive even if teardown_claude_temp_is_protected (bin/fm-teardown.sh) were deleted. This fixture runs from a linked worktree whose git common dir does not end in /.git, so the primary_dir assertion is doubly vacuous. As a result, the guard that refuses to remove the firstmate home, code root, or primary checkout slug has no behavioral coverage, even though the header test list (ct1, 'the firstmate home slug stays') implies it does. Fix: add a case where the recorded worktree resolves to the firstmate home or code root slug, with a protected path that exists, and assert the 'is the firstmate home or primary checkout' warning and that the folder is kept. If teardown always refuses such a record earlier, drop the home and primary assertions from ct1 so they do not claim coverage they lack.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
TMPDIR="$PWD/.phase-tmp" bash ~/.no-mistakes/evidence/01M79349T4PAQCW9F2AWM82KWT/focused-teardown.sh— eight selected behavioral checkspython3 ~/.no-mistakes/evidence/01M79349T4PAQCW9F2AWM82KWT/live-teardown.py— real teardown CLI and Treehouse scenariospython3 ~/.no-mistakes/evidence/01M79349T4PAQCW9F2AWM82KWT/endpoint-teardown.py— private tmux endpoint scenariospython3 ~/.no-mistakes/evidence/01M79349T4PAQCW9F2AWM82KWT/baseline-teardown.py— reproduced retained temp on the base commitRemoved disposable labs and verifiedgit status --shortwas empty.✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.