Skip to content

fix(bin): abort parked runs and reap leaked processes before teardown - #1710

Merged
kunchenguid merged 9 commits into
mainfrom
fm/fm-teardown-abort-run-before-reap
Aug 5, 2026
Merged

kunchenguid merged 9 commits into
mainfrom
fm/fm-teardown-abort-run-before-reap

Conversation

@kunchenguid

@kunchenguid kunchenguid commented Aug 4, 2026 •

Copy link
Copy Markdown
Owner

Intent

Harden bin/fm-teardown.sh's pre-teardown cleanup sequence with two intertwined fixes, delivered as one coordinated PR because both restructure what teardown does before it removes the worker.

Fix 1 - abort/conclude an active no-mistakes run before removing the worker: tearing down a task worker while its no-mistakes pipeline run is still PARKED at a gate (status stays running / parked awaiting_agent with no responsive agent) leaves an ORPHANED active run holding a fleet slot indefinitely. Observed twice on 2026-08-03 (a run parked 7h39m, and a run parked at a CI approval gate after the worker was cleaned post-CI-green), each requiring a manual no-mistakes axi abort --run to reap. Fix: teardown (and the worker-cleanup path) must conclude or abort any active/parked no-mistakes run owned by this task before removing the worker, using the supported no-mistakes abort interface (no hand-manipulation of run state), only aborting a run this task owns and never touching another task's run. Preserve the existing landed-work/unlanded-work teardown safety exactly as-is (never force, never discard unlanded work) - this fix adds a cleanup step, it does not relax that gate. A run with an autonomous step still under way (running/fixing/ci, not parked at a gate) is intentionally left alone since no-mistakes drives those against its own gate-repo clone, not the crew's worktree, so they are not orphaned by removing the worktree - only genuinely gate-parked runs (awaiting_approval/fix_review/an awaiting_agent field/a gate field) with no terminal outcome are aborted. A bootstrap/heartbeat sweep that detects and reports orphaned runs fleet-wide was explicitly scoped OUT as a follow-up rather than included here, to keep this PR to the smaller teardown-scoped change.

Fix 2 - reap leaked descendant processes at teardown: a task's descendant processes can survive teardown and be orphaned to init (ppid=1), pinning CPU cores for hours/days. Observed 2026-08-03: two Go test binaries (branchsync.test 18.5h CPU, steps.test 92.6h CPU) with -test.timeout=10m blown past by ~100x, reparented to launchd, referenced by no live task meta. Fix: at teardown, reap the task's descendant/worktree-rooted processes rather than only clearing the agent endpoint, scoped precisely to this task's own worktree and per-task tasktmp root (both unique per task, never shared) via a process cwd scan (lsof -a -d cwd, not the slow recursive +D file-tree walk) so it can never kill another task's or the primary's processes. A bootstrap orphan-reaper sweep for pre-existing orphaned ppid=1 processes was explicitly scoped OUT as a follow-up, to keep this PR to the smaller teardown-scoped change.

Ordering and interaction: the run is concluded/aborted BEFORE the process reap and BEFORE the worker/worktree is removed, since a still-active run may own live processes. The whole pre-teardown sequence is idempotent and safe to re-run (a partial teardown that is retried converges: an already-aborted run reads back terminal and is skipped, and a process already reaped is simply absent from the recheck). Every existing teardown refusal (uncommitted/unlanded work) is kept exactly as-is.

Implementation: bin/fm-teardown.sh gained conclude_task_no_mistakes_run (Fix 1) and reap_task_worktree_processes (Fix 2), both called unconditionally (including under --force) right after every landed/discard-work safety refusal passes and before any worktree return, branch delete, or backend kill; skipped for kind=secondmate since that lifecycle is owned by separate machinery. The branch+code-identity run-attribution rule (used to prove a run belongs to this exact task) was factored out of bin/fm-crew-state.sh into a new shared bin/fm-nm-run-lib.sh so both scripts share one ownership contract instead of two copies that could drift and misattribute a run. Colocated regression tests in tests/fm-teardown.test.sh exercise the real teardown path (never matching source text): a task's own simulated parked run is aborted and a parked run on another branch is left untouched; a leaked descendant process under the worktree and under tasktmp are both reaped; and a combined test proves the causal ordering (abort and reap both complete before the destructive worktree return, verified via real process liveness and a call-log, not line-number correlation).

PR-body requirement: state plainly that this PR carries TWO independent-but-coupled teardown-hardening fixes (abort-active-run-before-reap and leaked-descendant-process reaping), so a later reverter knows reverting it undoes both.

What Changed

  • This PR carries two independent but coupled teardown fixes: abort a task-owned gate-parked no-mistakes run, then reap processes rooted under its worktree or tasktmp before removing the worker.
  • Share branch and code-identity attribution between crew-state reporting and teardown, while leaving autonomous and other tasks’ runs untouched and refusing teardown when cleanup cannot be confirmed.
  • Add executable teardown regressions covering run ownership, abort confirmation, process identity and convergence, and abort-before-reap-before-removal ordering.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed
✅ **Rebase** - passed
✅ **Review** - completed
✅ **Test** - passed
✅ **Document** - passed
🔧 **Lint** - 1 issue found → auto-fixed ✅
✅ **Push** - passed

Accepted residual risk

Reviewed and consciously accepted rather than hardened further (stop-and-ship best-effort posture):

  • Abort is run-id-scoped, not fully atomic on live state. no-mistakes axi abort --run <id> targets the exact verified run, but the CLI has no compare-and-cancel primitive conditioned on the run still being parked at the instant of the call. A run that autonomously resumes in the narrow window between our verification read and the abort call could still be cancelled. Closing this fully needs an upstream no-mistakes capability that does not exist today.
  • The initial run lookup stays fail-open. A no-mistakes communication failure (timeout, daemon unreachable) reads the same as "no active run" rather than refusing teardown. Making a successful query a hard precondition for every ship-task teardown would expand this fix's blast radius fleet-wide - blocking teardown for tasks that never had an active run at all - well beyond its intended best-effort scope.
  • Forced secondmate child cleanup isn't covered yet. cleanup_firstmate_home_children still removes a torn-down secondmate's own ship/scout children directly, without routing them through conclude_task_no_mistakes_run/reap_task_worktree_processes. Filed as a follow-up: fm-teardown-secondmate-child-abort-reap-r1.

Both code paths carry inline comments documenting the same rationale at the point of the tradeoff.

…t teardown

Teardown could remove a task's worker while its no-mistakes pipeline run was
still parked at a gate, leaving an orphaned run holding a fleet slot
indefinitely (observed 2026-08-03: runs parked 7h39m and parked at a
post-CI approval gate). It could also leave backgrounded/disowned
descendant processes rooted under the worktree or tasktmp surviving
reparented to init (observed: two `go test` binaries pinning CPU for
hours with no live task meta to attribute them to).

Add two coupled pre-teardown steps, both scoped to this task's exact
branch/head or worktree/tasktmp so they can never touch another task's
run or processes:
- conclude_task_no_mistakes_run aborts a run parked at a gate via
  `no-mistakes axi abort`, cd'd into the exact worktree so the daemon
  resolves the run itself rather than teardown naming a --run id.
- reap_task_worktree_processes sweeps for processes whose cwd is under
  the worktree or tasktmp (via `lsof -a -d cwd`) and TERM/KILLs them.

Both run before any worktree return, branch delete, or backend kill,
and are idempotent on a retried teardown. The branch+head attribution
logic is factored out of bin/fm-crew-state.sh into the new shared
bin/fm-nm-run-lib.sh so both scripts use the same ownership contract.
@kunchenguid
kunchenguid merged commit 7ef26c4 into main Aug 5, 2026
14 checks passed
@kunchenguid
kunchenguid deleted the fm/fm-teardown-abort-run-before-reap branch August 5, 2026 00:16
karotkriss added a commit to karotkriss/firstmate that referenced this pull request Aug 5, 2026
…beyond the worktree

A live no-mistakes run advances its branch tip (rebase + fix commits) in the
daemon's own repo, so the run head named by `no-mistakes runs`/`axi status` no
longer resolves in the crew's worktree. fm-crew-state.sh treated that
unresolvable head as "not this crew", skipped the live row, and fell through to
an older superseded row on the pre-run worktree sha - mislabelling an
actively-fixing run "failed" and firing a no-timer false-stale cascade.

Fix: a non-empty but unresolvable run head is the live pipeline's advanced tip,
so attribute it. On the `axi status` path this is trusted directly because the
daemon vouched the run is the branch's active-or-most-recent. In the runs-list
walk it is attributed only for a live running/fixing row; a terminal
unresolvable row is skipped as ambiguous daemon-only history from a reused
branch. Resolvable heads keep the strict exact/ancestor rule (strict-ancestor
and diverged still skip); an empty or non-hex/truncated sha column is rejected;
and `fixing` maps to working in the coarse status map.

This is deliberately split across two call sites of the shared code-identity
rule, and the split is the design - do not re-unify it. bin/fm-crew-state.sh
only READS and reports a state, so a mis-attributed unresolvable head costs a
wrong label. bin/fm-teardown.sh (added in kunchenguid#1710) feeds the same shared predicate
fm_nm_head_matches_worktree into a decision to ABORT a parked validation run,
where a wrong match destroys another task's work. Leniency that is right for a
status read is dangerous on a destructive path, so the shared owner in
bin/fm-nm-run-lib.sh stays strict and fail-closed exactly as kunchenguid#1710 left it, and
the lenient read-only interpretation lives only in fm-crew-state.sh. A comment
at both call sites records this so a future refactor does not helpfully fold the
leniency into the shared owner and silently arm teardown.

Rebased onto current main: re-homes this fix (and its review-round refinements)
over kunchenguid#1710's extraction of the head-attribution rule into bin/fm-nm-run-lib.sh.
Regression tests cover the advanced-tip live run (running and fixing) attributed
working, a terminal unresolvable row and a strict-ancestor/diverged run skipped,
a truncated headless row rejected, and a genuinely cancelled current run
reported failed.
withally added a commit to withally/firstmate that referenced this pull request Aug 5, 2026