Repository navigation
fix(bin): let teardown finish treehouse's own slot return instead of reaping it - #6067
SilvioLima94 wants to merge 5 commits into
Conversation
…eaping it A crew slot is held by a `treehouse get` process outside it. Once teardown's leaked-process reap ends the worker's subshell, that holder resets the slot through git steps whose working directory is the slot (checkout --detach, status, read-tree --reset -u <default>, clean -fd, then its HEAD move). The reap killed those steps as leaks, so it refused after three passes with changing pids, and a cut after read-tree left the index and tracked files at the default branch with HEAD still on the merged task commit, which refused every later plain retry as uncommitted changes until the captain ran --force. The reap now never signals a git process started directly by a treehouse holder outside the task roots, or anything below it: it waits for the holder to exit, moves on when a live holder stays idle, and refuses rather than cut steps still running at a bounded deadline. The landed-work safety check also recognizes a copy an earlier cut already left in that state - staged-only entries, no worktree edit, no untracked file even when status hides them, and an index tree equal to a commit in the default branch's first-parent history - so the pool return finishes it; HEAD's commits still face the landed-work checks.
|
| else | ||
| [ $((SECONDS - last_step)) -lt "$POOL_RETURN_QUIET_SECS" ] || return 0 |
There was a problem hiding this comment.
Quiet holder may still return If the Treehouse holder pauses for two seconds between return steps, this check treats it as idle even though it is still active. Teardown can then start its own forced return while the holder is mid-reset, risking competing resets of the slot. The regression holder runs its steps consecutively, so that boundary remains uncovered.
…e requested comment for ci-4. ci-1 and ci-2 were left alone as decided: they're fork workflows waiting on maintainer approval. **ci-3 (staged work could be discarded).** Rule it broke: a copy is treated as an interrupted pool reset only if the matched default-branch commit already contains the task's work. The one place this rule applies is `worktree_is_interrupted_pool_reset` in `bin/fm-teardown.sh`. Both callers go through `validate_worktree_teardown_safety`, so that function is the single choke point. It now also requires: - HEAD is detached (treehouse's return always detaches first). - For the default-branch commit whose tree matches the index, `git merge-tree --write-tree <commit> HEAD` gives back that same tree, the same test `content_in_default` uses. So a staged rollback to a default-branch tree from before the task landed no longer matches. The function comment is updated to match. I also added a comment at `wait_for_pool_return` for ci-4: treehouse runs its return steps back to back and a slow step keeps a live process under the slot, so the quiet window only has to cover the gaps between steps. **ci-5 (reset test never reset).** In `tests/fm-teardown.test.sh`, the fake `treehouse` used by the interrupted-reset tests now does what `treehouse return --force` does to the slot: it detaches HEAD, runs `reset --hard refs/remotes/origin/main` and `clean -fd`. The success test (where the default branch moved on after the cut) now also checks that after teardown the slot's HEAD equals `refs/remotes/origin/main` and `git status --porcelain` is empty. **New regression test.** A `historical-rollback` variant in the refusal test stages the default-branch tree from before the landing (`origin/main~2`) with HEAD detached. It must be refused with "uncommitted changes present" and must not be reported as an interrupted reset. Against the unfixed script this variant tears the copy down (exit 0); with the fix it refuses. **One changed expectation.** The existing `unlanded-head` variant is now refused by the new check itself, so its message changed from "not on any remote and not landed" to "uncommitted changes present". It still refuses, and it asserts the same things as the other variants. **Verification, and what couldn't run.** - `tests/fm-teardown.test.sh` stops early on this machine: tasks-axi here is 0.2.5, and the backlog tests need 0.2.6. So I ran the file without the 16 tests that set up a real backlog. The other 91 passed, including all of the pool-holder and interrupted-reset tests. The 16 skipped tests weren't run here. - `tests/fm-teardown-endpoint-safety.test.sh` passes. - `bin/fm-lint.sh` passes
| merged=$(git -C "$WT" merge-tree --write-tree "$commit" HEAD 2>/dev/null) || continue | ||
| if [ "$(printf '%s\n' "$merged" | head -1)" = "$tree" ]; then |
There was a problem hiding this comment.
Genuine resets can be refused If a merged task’s changes are later undone or altered on the default branch, Treehouse can leave a genuine interrupted reset with that branch’s tree staged beneath the detached task HEAD. The new merge check rejects that tree because merging HEAD would restore or conflict with the task’s changes. Plain teardown then keeps refusing “uncommitted changes present,” leaving the slot recoverable only with
--force.
…the captain's decision, this is accepted as a deliberate limitation. No behavior or test changes were made; it is documented only. Invariant involved: plain teardown never discards a staged rollback to a historical default-branch tree. The price is that a copy counts as an interrupted pool reset only when the matched default-branch commit already contains HEAD's work. Change: in bin/fm-teardown.sh, the comment on worktree_is_interrupted_pool_reset now says: if the default branch reverts or alters the merged task's changes before teardown, a genuine interrupted reset fails the contains-HEAD's-work check and plain teardown refuses. That refusal is safe and needs the captain's --force. It is the accepted price for never discarding a staged rollback to a historical default-branch tree. This is the only place the check exists; both callers go through validate_worktree_teardown_safety, so there is no other site to update. PR description: not edited here. The PR body is generated by the pipeline's PR phase, which this CI phase must not run. Please have the PR phase add this line under the fix's safety notes: "Known limitation (deliberate): if the default branch reverts or alters a merged task's changes before teardown, a genuine interrupted pool reset fails the contains-HEAD's-work check and plain teardown refuses with 'uncommitted changes present'. That refusal is safe and needs the captain's --force. It is the accepted price for never discarding a staged rollback to a historical default-branch tree." ci-1 and ci-2 (fork workflows awaiting maintainer approval) and ci-3 (approved as decided): unchanged, as instructed. Verification: bash -n bin/fm-teardown.sh passes and bin/fm-lint.sh passes. The teardown suites were not rerun, because the change is a comment only
|
Speaking as Kun's firstmate: first-time fork CI was reviewed (no workflow / security risk) and approved; waiting on CI and no-mistakes to finish on HEAD |
Intent
Task: Diagnose and fix a recurring bin/fm-teardown.sh failure that forced a manual --force cleanup after almost every merged PR. Shared tracked firstmate material; firstmate-coding-guidelines apply.
Observed behaviour (2026-09-26..28): after a task's PR is squash-merged, plain
bin/fm-teardown.sh <id>first refuses with "leaked worktree processes for remain after 3 reap attempts; preserving the worktree/tasktmp ..." (the reaped PID changes each attempt). An immediate plain retry then refuses with "uncommitted changes present": the pooled copy has thousands of staged changes,git diff --cached --quiet origin/mainandgit diff --quietboth succeed, and HEAD is still the merged branch tip (the pool's reset was cut off mid-checkout). It never clears on its own; only --force completes it, and the auto-mode classifier refuses to let firstmate run --force, so the captain had to. Seen on at least nine teardowns; some in the same period succeeded first time. treehouse went v2.3.0 -> v3.1.0 on 2026-09-26; the pattern predates that.Required: (1) diagnose before fixing with a throwaway repository and pool (never real project pools or live tasks), separating trigger, masking condition, and symptom, verifying the leading hypothesis (the leaked-process reaper kills the pool's own
treehouse returnreset) with the smallest counterfactual and recording disconfirming evidence; (2) fix the cause so a merged task's teardown completes without --force while every safety guarantee holds: never discard uncommitted or unlanded work, never kill processes outside the task's worktree, keep refusing genuinely dirty or unlanded copies; if the fix recognises and finishes the interrupted-reset state (index and worktree equal to the default branch, HEAD at a landed branch tip, nothing unique), prove the detection cannot match real unlanded work; (3) turn the reproduction into a regression test in the existing teardown suite; (4) record the finding where the repo keeps such knowledge. Validation: the new regression test fails on current main and passes with the fix; the full existing suites and CI lint pass. Scope: only this worktree and throwaway fixtures; do not change treehouse itself (report a treehouse-side cause, fix firstmate's side only). Never merge.Diagnosis (reproduced with a throwaway repo, real treehouse v3.1.0 pool, real tmux, and the real fm-teardown.sh):
treehouse get, a holder process whose cwd is the project (outside the slot) and whose child subshell runs the worker in the slot. Teardown's Fix-2 reaper kills that subshell; the holder then returns the slot itself through git steps whose working directory is the slot (logged sequence: checkout --detach, status --porcelain --untracked-files=all, rev-parse..., read-tree --reset -u <origin/main>, clean -fd, then it writes HEAD). The reaper's next passes see those git processes as new leaked processes under the worktree and kill them - hence a different PID each pass and "remain after 3 reap attempts". The holder itself is never reaped (outside the roots), so it keeps spawning steps.clean -fdstep leaves HEAD detached at the branch tip with index and tracked files equal to origin/main, and treehouse marks the slot dirty; every later plain teardown refuses "uncommitted changes present". Reproduced exactly.gitprocesses) refuse on main. Also, when the copy has untracked files the holder stops at its "Clean worktree and return to pool? [Y/n]" prompt and runs no steps, so teardown's own return does the reset and succeeds.treehouse return --force.Fix (bin/fm-teardown.sh; header Fix 2 owns the rationale):
git diff --quietpasses,git ls-files --others --exclude-standardshows no untracked file beyond the existing firstmate allowlist (so status.showUntrackedFiles=no cannot hide one), and the index tree (write-tree) equals a commit in the first-parent history (up to 1000) of refs/remotes/origin/ (plus local only for local-only mode). Then the tracked content is on the default branch by construction; HEAD's own commits still face the existing landed-work checks, and the pool return finishes the reset. The untracked allowlist is shared through one variable. Deliberate: write-tree may write tree objects to the object store (harmless, content-addressed).Validation done locally: both teardown suites pass (106 and 26), CI portable-parallel-1 and -2 lanes pass (331, 908 ok), bin/fm-lint.sh and fm-doc-audience-check pass. The portable-serial lane had 17 failing scripts on this machine (live codex, herdr hidden per the task's Herdr gate, local tool versions); all 17 fail identically with main's teardown, so they are environmental and unrelated. A newer tasks-axi (0.2.6) was installed only in a scratch prefix for the suite; the machine's global 0.2.5 is too old for it.
What Changed
bin/fm-teardown.sh's leaked-process reaper no longer kills agitprocess started directly by atreehouseholder outside the task roots, or anything that process starts. These are the holder's own steps for resetting the slot. Instead, the reaper waits for the holder to exit. It moves on once a live holder has been idle forFM_TEARDOWN_POOL_RETURN_QUIET_SECS(default 2). If steps are still running atFM_TEARDOWN_POOL_RETURN_WAIT_SECS(default 60), it refuses rather than cut them off. Nothing outside the task roots is ever signalled, and the reap log now lists only the PIDs it actually signals.validate_worktree_teardown_safetynow recognizes a copy where an earlier cut interrupted the pool reset and lets the pool return finish it. It accepts the copy only when all of these hold:git diff --quietpasses;ls-files --othersshows no untracked files beyond the firstmate allowlist, which is now shared throughTEARDOWN_IGNORABLE_UNTRACKED;write-tree) matches a commit in the last 1000 first-parent commits oforigin/<default>, or of the local default branch in local-only mode.HEAD's own commits still go through the existing landed-work checks.
The header's Fix 2 section records the diagnosis.
docs/architecture.mdpoints to the pool-return exemption anddocs/configuration.mdlists the two new settings.tests/fm-teardown.test.shadds a holder emulation and four tests:showUntrackedFiles=no), worktree edits, staged content beyond the default branch, and an unlanded HEAD.🤖 Generated with Claude Code
Risk Assessment
git diff, untracked files are checked withls-fileseven when status hides them, the index must match a default-branch tree, HEAD still faces the landed-work checks, and the project lock is held throughout. Only a timing mismatch and a date correction remain, but the area is safety-critical.Known limitation (deliberate)
If the default branch reverts or alters a merged task's changes before teardown, a genuine interrupted pool reset fails the contains-HEAD's-work check and plain teardown refuses with "uncommitted changes present".
That refusal is safe and needs the captain's
--force.It is the accepted price for never discarding a staged rollback to a historical default-branch tree.
Testing
I ran the 4 new teardown regression tests against the fix (all pass) and against base (holder-return and interrupted-reset-finished fail as intended). I then ran five live labs, each using a throwaway repo, a real treehouse v3.1.0 pool rooted in a disposable lab home, a real
treehouse getholder on the lab's private tmux socket, real git slowed only for the holder's own return steps (the masking condition), and the real fm-teardown.sh; gh, gh-axi and no-mistakes were stubbed because the origin is a local path. Base reproduced both reported symptoms. The fix completed a merged task on the first plain teardown, finished a cut-off slot through the real treehouse return, and handled a holder idling at its prompt. It also refused hidden untracked files, tracked edits, and a task commit that never landed, leaving that work intact. Every lab, tmux server and temp tree was removed; the worktree is clean. Overall result: pass. This is a CLI change with no UI, so the evidence is CLI transcripts rather than screenshots.Evidence: New regression tests passing with the fix
Source: New regression tests passing with the fix
Evidence: Same new tests against base a256cb5 teardown (two targeted tests fail)
Source: Same new tests against base a256cb5 teardown (two targeted tests fail)
Evidence: Live: fixed teardown lets the real treehouse holder's return finish, first plain run exits 0
Source: Live: fixed teardown lets the real treehouse holder's return finish, first plain run exits 0
Evidence: Live counterfactual: base teardown refuses 'remain after 3 reap attempts' with a changing PID
Source: Live counterfactual: base teardown refuses 'remain after 3 reap attempts' with a changing PID
Evidence: Live: cut-off reset state; base refuses on every retry; fix refuses hidden-untracked and tracked-edit variants, then finishes the clean state
Source: Live: cut-off reset state; base refuses on every retry; fix refuses hidden-untracked and tracked-edit variants, then finishes the clean state
Evidence: Live: cut-off reset state over a task commit that never landed; fix refuses and keeps the commit
Source: Live: cut-off reset state over a task commit that never landed; fix refuses and keeps the commit
Evidence: Live: holder idle (stopped) at its clean-and-return prompt; teardown moves on without signalling it
Source: Live: holder idle (stopped) at its clean-and-return prompt; teardown moves on without signalling it
Evidence: Live idle attempt where .claude/ was globally git-ignored so the holder never prompted (still exit 0)
Source: Live idle attempt where .claude/ was globally git-ignored so the holder never prompted (still exit 0)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-teardown.sh:2303- The idle wait in wait_for_pool_return counts rounds instead of measuring time. The docs and header say it moves on once a holder has run no step for FM_TEARDOWN_POOL_RETURN_QUIET_SECS seconds (default 2). The code instead waits for POOL_RETURN_QUIET_SECS*5+1 quiet rounds and assumes each round lasts 0.2s. In fact each round runs a system-widelsof -a -d cwdonce per root (the worktree and tasktmp, so twice), plus task_pool_classify's ancestry walk, which forkspstwice for each ancestor level. On macOS a round therefore takes about 0.5–2s, so the 'default 2s' idle wait really lasts about 6–25s. This hits the common case where the holder waits at its 'Clean worktree and return to pool? [Y/n]' prompt (the idle-holder path). The quiet branch never checks the time, so nothing caps it. Fix: record when the quiet stretch began (for examplequiet_since=$SECONDS, reset whenever TASK_POOL_STEPS is non-empty) and return 0 onceSECONDS - quiet_since >= POOL_RETURN_QUIET_SECS, the way the step deadline already works. Optionally, replace the per-pidpsancestry walk with oneps -A -o pid=,ppid=,comm=snapshot per round. Both parts must hold together: docs/configuration.md:2365 and the header at bin/fm-teardown.sh:294-296 describe the knob in seconds.bin/fm-teardown.sh:289- The Fix 2 header records the failure as 'observed 2026-09-22 to 09-28', but the task intent gives the observed range as 2026-09-26..28. Since this header is where the finding is recorded, change the dates to match the observed evidence.🔧 Fix applied.
1 info still open:
bin/fm-teardown.sh:289- The Fix 2 header records the failure as 'observed 2026-09-22 to 09-28', but the task intent gives the observed range as 2026-09-26..28. Since this header is where the finding is recorded, change the dates to match the observed evidence.bin/fm-teardown.sh:1952- When the cut-off-reset shape sits over a task commit that never landed, teardown first prints 'holds a pool reset cut off before it moved HEAD ... letting the pool return finish it' and then refuses 'work not on any remote and not landed'. The refusal is correct and no work is lost, but the first message promises a return that does not happen, which could confuse an operator reading the output.bash tests/focused.test.sh(the 4 new tests in tests/fm-teardown.test.sh, from agit archive HEADtree): all okThe same 4 tests one at a time against the base a256cb5 bin/fm-teardown.sh: holder-return and interrupted-reset-finished fail; the idle-holder test passes on base too; the unlanded-head safety variant fails on base because base refuses earlier as uncommitted changeslive-lab.sh <fix-tree> fix: real treehouse get holder + slowed real git return steps; one plain fm-teardown.sh task-x1 runlive-lab.sh <base-tree> base: same lab against base teardown (counterfactual)live-lab-cut.sh <fix> <base> cut: SIGKILL only the real holder'sgit clean -fd, then base teardown twice, fixed teardown with a hidden untracked file, then with a tracked edit, then on the clean interrupted stateLAND_FEATURE=0 live-lab-cut.sh ... cut-unlanded: same cut-off state with a task commit that never landed; fixed teardown run twiceWORKER_EXTRA='&& touch .fm-grok-turnend' live-lab-cut.sh ... idle: holder stops at its 'Clean worktree and return to pool? [Y/n]' prompt during teardown; pane and holder steps recordedScratch treehouse probe confirming the prompt appears when an untracked file exists at subshell exit✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.