Skip to content

fix: retire stale worker busy state and reclaim orphan locks - #338

Merged
dnth merged 5 commits into
mainfrom
fm/fm-upstream-port-b-busy
Nov 27, 2026
Merged

dnth merged 5 commits into
mainfrom
fm/fm-upstream-port-b-busy

Conversation

@dnth

@dnth dnth commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Retire a stopped worker's busy generation with its incarnation, and let a fresh orphan busy lock reach the reclaim threshold before the writer refuses.

Adapted upstream 6733 (e7c3bdc1) and 8177 (4869cf58).

 exit <id> / relaunch <id>
-  retire sidecar + busy record only
+  fm_busy_retire_incarnation (state, id, spawn_gen)
+    take the record lock; refuse unless the record still names this spawn_gen
+    retire the armed generation -> sidecar and busy record go
+    drop busy_gen= from state/<id>.meta  (retry also clears a sidecar-less leftover)
   report stopped / launch the replacement

 fm-spawn.sh <id> --relaunch --harness devin
+  same retirement before the record is published, so an unarmed successor
+  cannot inherit `busy fm-spawn` from the retired worker

 fm-busy-event.sh lock_acquire
-  give up after 40 tries (~2s) although a lock is only reclaimable at 5s
+  poll until a lock present at wait start can reach FM_BUSY_LOCK_STALE_SECS,
+  then reclaim it; refuse at stale + 2s; a digit-only setting stays decimal
File Change
bin/fm-busy-lib.sh new fm_busy_retire_incarnation: one retirement owner beside the generation contract it reads
bin/fm-control.sh exit retires after every proven stop, so a retry repairs a half-finished retirement; relaunch retires before launching
bin/fm-spawn.sh a relaunch retires the previous generation before publishing; every relaunch takes the task's control lock
bin/fm-busy-event.sh lock acquisition outwaits the stale bound for an already-present lock and reclaims it
docs/ architecture.md owns the retirement contract; agent-control.md and configuration.md carry the verb and toolchain consequences

Where the fork diverges from upstream

  1. One retirement owner: upstream cleared the line inside bin/fm-control.sh through fm_backlog_atomic_transition publish after inline-sourcing the backlog and tasks-axi libraries. This fork puts retirement in bin/fm-busy-lib.sh beside the generation contract, takes the record lock, and republishes with mktemp + mv - the path this fork's own ms_meta_apply (fork PR 327) already uses for a control-plane record rewrite.
  2. Direct relaunch: upstream's fix covered only its control plane. This fork also documents bin/fm-spawn.sh <id> --relaunch --harness <name>, which may switch to a harness that arms no busy generation, so retirement also runs there before the record is published.
  3. Widened relaunch locking: every --relaunch now acquires state/.control-<id>.lock, where before only OMP model-switch coordination took it, so the early retirement cannot interleave with a control verb. fm-control relaunch still passes its own lock through the existing FM_CONTROL_RELAUNCH_TX / FM_CONTROL_RELAUNCH_LOCK_OWNER handshake, so it does not deadlock against itself.
  4. Idempotent repair: an exit retry clears a generation whose sidecar is already gone (upgrade leftovers, or a failed record rewrite). A failed rewrite fails the verb naming the retry and the relaunch recovery instead of passing silently.
  5. Lock deadline: upstream's rewrite dropped the 0.05 s poll interval and spun on mkdir/date/stat for the whole wait. This port keeps the interval and measures elapsed wait on a monotonic clock, so a backward wall-clock correction cannot extend it; lock age still reads filesystem mtimes.
  6. Toolchain consequence: that monotonic deadline makes Perl with Time::HiRes and CLOCK_MONOTONIC a requirement for a contended busy write, which docs/configuration.md now states.
  7. Test location: upstream's tests/fm-control.test.sh does not exist in this fork, so the AC1 regressions drive the real bin/fm-control.sh and bin/fm-spawn.sh through this fork's fake-backend harness, and the lock tests sit beside the existing lock tests in tests/fm-busy-state.test.sh.

Evidence

  • Before (ran): on base 078d185d, bash tests/fm-busy-state.test.sh fails with not ok - the first retire over a fresh orphan lock was refused: error: busy-state lock timeout for t1, and bash tests/fm-spawn-relaunch-dead-endpoint.test.sh fails with not ok - the retired incarnation's busy_gen survived in the task record.
  • After (ran): both suites pass. The new cases cover a refused replacement that still retires, an exit retry that finishes a retirement whose record rename was forced to fail, a real --relaunch --harness devin that leaves no generation and keeps idle herdr-native through a late retired-generation event, a fresh orphan reclaimed while a live holder is never broken, a lock that stays young refused inside the bound, and decimal 08 reclaimed at eight seconds.
  • After (ran), directly affected suites: tests/fm-busy-adapter-wiring.test.sh, tests/fm-control-switch-model.test.sh, tests/fm-omp-relaunch-guard.test.sh, and bin/fm-lint.sh on the changed files are green; the pipeline's live scenarios and CI run are below.

Merge Danger

Door: two-way for behavior - a revert restores the previous waiter and stops the new retirement - but the retirement deletes a worker's busy sidecar, record, and metadata line, which no revert can restore. Those are reconstructible from the next launch, so the loss is bounded.

Blast Radius: worker lifecycle and supervision.

exit, relaunch, spawn-time arming, and every harness hook that writes a busy event share this code. A defect here can misreport a worker idle or busy (which gates steers, idle alarms, and teardown), or block a relaunch outright. Safety that stays in place: retirement refuses when the recorded spawn_gen no longer matches, releases the record lock on every path, leaves the record untouched when it names another generation, and a failed retirement fails the verb loudly instead of claiming success.

Deferred review findings

The final review round raised these after the port's stop rule; each is recorded rather than fixed, and none changes an acceptance criterion.

Finding What it says Disposition
standalone-retirement-after-endpoint-creation a standalone relaunch retires the previous generation after creating the replacement endpoint, so a failed record rewrite can leave a new pane with the old record deferred: needs one failed record rewrite; the failure is loud and a relaunch reconciles the record
busy-lock-clock-runtime-regression the bounded wait uses Perl's monotonic clock, so a host without Perl refuses a contended lock deferred: Perl ships on the supported hosts and the requirement is now documented; a poll-count bound without Perl is the follow-up
unneeded-generic-exit-relaunch-advice the generic missing-endpoint exit message now also says how to relaunch deferred: wording only; the retirement-failure diagnostic carries the actionable advice
unneeded-omp-secondmate-retirement round 2 added retirement to the automatic OMP secondmate recovery too; secondmates never publish busy_gen, so that path is dead weight declined under the stop rule: the round-2 fix already removed the OMP_SECONDMATE_RELAUNCH condition and its prior-generation read, so the final code retires only on an explicit --relaunch

Accepted tradeoff: a writer that holds its lock past FM_BUSY_LOCK_STALE_SECS is treated as dead and its lock is reclaimed, now on the first attempt instead of a later retry. That is upstream 8177's contract and this fork's existing stale bound. Owner-aware locks and the rare concurrent-reclaimer race are follow-ups, not changes here.

Testing

The targeted busy-state, spawn, and control regressions passed, and all seven live local scenarios passed. The two transcripts capture the worker recovery and lock timing results, including both regressions reproduced on the base commit.

  • Live validation: ✅ go - 7 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Relaunch a stopped synthetic Claude worker as a real local Codex worker in tmux. Its old busy state disappears and a late event is rejected. ✅ pass live Live tmux worker retirement and recovery transcript
Retry exit in a real tmux pane after an injected metadata rename failure. The stopped worker’s old generation is cleared. ✅ pass live Live tmux worker retirement and recovery transcript
Recover after a real Codex exit closes its tmux pane and an injected metadata rename fails. Relaunch clears the old generation and requires a valid lease before replacing the worker. ✅ pass live Live tmux worker retirement and recovery transcript
Kill a real busy-event writer while it holds a fresh lock on synthetic state. The first retirement waits and reclaims the orphan. ✅ pass live Live busy-lock timing and persisted state transcript
Attempt retirement while a real writer still holds a young lock. Retirement waits without changing the record and succeeds after release. ✅ pass live Live busy-lock timing and persisted state transcript
Set the stale threshold to 08 and retire against a six-second-old synthetic orphan. Reclaim occurs at eight seconds. ✅ pass live Live busy-lock timing and persisted state transcript
Attempt retirement during a simulated 60-second wall-clock rollback. The real CLI times out without changing busy state. ✅ pass live Live busy-lock timing and persisted state transcript
Evidence: Live tmux worker retirement and recovery transcript
Target 663e5dba56cc08a5d58788a99d72216cbfd71288
Local real tmux and Codex, 120x36 grid, isolated home and no external network. Worker records and lease are synthetic.

$ bin/fm-spawn.sh direct --relaunch --harness codex
Before: pane=%1 command=bash grid=120x36, busy fm-spawn, old generation g1795778492.683095.1742
spawned direct harness=codex kind=ship mode=local-only yolo=off window=lab:fm-direct worktree={run-worktree}/.phase-test/workers-verified/pool/direct/project
pane=%1 command=codex grid=120x36
harness=codex
spawn_gen=s1795778493.683156.1831
Retired old busy sidecar, record and metadata field.
Replacement verdict: unknown codex-unverified
$ bin/fm-busy-event.sh apply <isolated-state> direct busy --gen g1795778492.683095.1742 --source claude-hook --event user-prompt-submit
error: no armed busy-state gen for direct [exit 1]
Replacement verdict after the late event: unknown codex-unverified
Retired old busy sidecar, record and metadata field.

Retry with the stopped shell still present
pane=%2 command=bash grid=120x36
$ bin/fm-control.sh stopped exit
Injected failed rename of the retired metadata.
error: could not retire task stopped's busy generation; retry 'fm-control.sh stopped exit' if the endpoint remains, or use 'fm-control.sh stopped relaunch' if it is gone (ships and scouts require --note or --note-file)
[exit 1]
busy_gen=g1795778495.685475.16489
$ bin/fm-control.sh stopped exit
already-stopped stopped harness=claude backend=tmux endpoint=lab:fm-stopped worktree={run-worktree}/.phase-test/workers-verified/pool/stopped/project
[exit 0]
Retired old busy sidecar, record and metadata field.

Recover a verified exit that closed its tmux pane
pane=%3 command=codex grid=120x36
bash-5.1# exec codex --no-alt-screen

  >_ OpenAI Codex (v0.170.0)
     {run-worktree}/.phase-test/…/pool/closed/project

  Tip: Maximize usage with GPT-6.1 Sol. Try it on complex work for near-Astra performance at a lower cost.


› Ask Codex to do anything

  gpt-5.4 default · {run-worktree}/.phase-test/workers-verif…
  ← for agents · ? for shortcuts
$ bin/fm-control.sh closed exit
Injected failed rename of the retired metadata.
error: could not retire task closed's busy generation; retry 'fm-control.sh closed exit' if the endpoint remains, or use 'fm-control.sh closed relaunch' if it is gone (ships and scouts require --note or --note-file)
[exit 1]
busy_gen=g1795778495.686128.10022
tmux confirms the recorded pane closed.
$ bin/fm-control.sh closed exit
error: task closed's recorded endpoint is gone, so there is no agent to stop; use 'fm-control.sh closed relaunch' to reconcile it (ships and scouts require --note or --note-file)
[exit 1]
busy_gen=g1795778495.686128.10022
$ bin/fm-control.sh closed relaunch --note Restore\ the\ disposable\ local\ worker
error: task closed's recorded worktree '{run-worktree}/.phase-test/workers-verified/pool/closed/project' has no durable Treehouse lease held by fm-closed; refusing to relaunch into a worktree it cannot prove it still owns
error: the replacement agent for closed could not be launched on codex
error: closed's agent was stopped but the replacement did not launch; no agent is running, and its work plus the recorded progress note are preserved at {run-worktree}/.phase-test/workers-verified/pool/closed/project
[exit 1]
Retired old busy sidecar, record and metadata field.
$ bin/fm-control.sh closed relaunch --note Restore\ the\ disposable\ local\ worker
relaunched closed harness=codex from=codex model=default effort=default backend=tmux endpoint=lab:fm-closed worktree={run-worktree}/.phase-test/workers-verified/pool/closed/project
[exit 0]
pane=%4 command=codex grid=120x36
Retired old busy sidecar, record and metadata field.
harness=codex
spawn_gen=s1795778508.695870.15493

Final persisted state
direct: harness=codex spawn_gen=s1795778493.683156.1831
  busy_gen field, sidecar and record absent
stopped: harness=claude spawn_gen=prior-stopped
  busy_gen field, sidecar and record absent
closed: harness=codex spawn_gen=s1795778508.695870.15493
  busy_gen field, sidecar and record absent

Base 078d185d803514669063f11c00239a167f42a675, standalone relaunch comparison

$ .phase-test/base/bin/fm-spawn.sh direct --relaunch --harness codex
Before: pane=%1 command=bash grid=120x36, busy fm-spawn, old generation g1795778451.641843.30764
spawned direct harness=codex kind=ship mode=local-only yolo=off window=lab:fm-direct worktree={run-worktree}/.phase-test/workers-base-direct/pool/direct/project
pane=%1 command=codex grid=120x36
harness=codex
spawn_gen=s1795778452.641971.26192
Prior busy sidecar after replacement: g1795778451.641843.30764
$ .phase-test/base/bin/fm-busy-event.sh apply <isolated-state> direct busy --gen g1795778451.641843.30764 --source claude-hook --event user-prompt-submit
Old writer accepted on base; persisted record: v1 gen=g1795778451.641843.30764 seq=2 state=busy source=claude-hook event=user-prompt-submit ts=1795778454
Evidence: Live busy-lock timing and persisted state transcript
Real busy-event CLI. Base 078d185d803514669063f11c00239a167f42a675 and target 663e5dba56cc08a5d58788a99d72216cbfd71288.
Synthetic isolated records. Process controls paused the actual writer at its rename boundary. A local date command simulated wall-clock rollback.

Fresh orphan from a killed product writer
Killed real apply writer after lock acquisition: writer exit=-9; lock present=True
$ .phase-test/base/bin/fm-busy-event.sh retire <base-orphan> worker --gen g1795778329.530025.23778
exit=1 elapsed=2.02s error: busy-state lock timeout for worker
Generation sidecar, busy record and lock removed: False
Killed real apply writer after lock acquisition: writer exit=-9; lock present=True
$ bin/fm-busy-event.sh retire <target-orphan> worker --gen g1795778331.530436.8917
exit=0 elapsed=4.51s 
Generation sidecar, busy record and lock removed: True

Active writer holds a young lock and then releases it
At 1.0s, real apply and retire are both pending. Lock and busy record are unchanged.
After release: writer exit=0, retire exit=0, elapsed=1.01s
Generation sidecar, busy record and lock removed.

Decimal reclaim setting
$ bin/fm-busy-event.sh retire <decimal-08> worker --gen g1795778337.533427.4772
exit=0 elapsed=1.95s 
Lock backdated six seconds. Age at successful retirement: 8s.

Bounded lock refusal while wall clock jumps backward
$ bin/fm-busy-event.sh retire <clock-rollback> worker --gen g1795778339.534670.3787
exit=1 elapsed=4.02s error: busy-state lock timeout for worker
Wall time stepped back 60s on second read. Generation, record and lock unchanged: True
$ bin/fm-busy-event.sh retire <clock-rollback> worker --gen g1795778339.534670.3787
exit=0 elapsed=0.01s 

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 10 issues (2 errors, 8 warnings)
  • 🚨 bin/fm-control.sh:660 - AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalone fm-spawn.sh --relaunch --harness devin path can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815, :5327, :5374). If the old record says busy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190, :319, :361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths at bin/fm-control.sh:696, :724, and :1565 must use compatible retirement semantics.
  • 🚨 bin/fm-busy-event.sh:134 - AC2 requires coverage of “a live holder (still refused).” The changed if [ &#34;$age&#34; -ge &#34;$stale&#34; ] branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention with FM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151, :166, :189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203, :249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.
  • 🚨 tests/fm-spawn-relaunch-dead-endpoint.test.sh:1471 - AC1 requires “A focused test shows a replaced incarnation's stale busy_gen no longer marks the new incarnation busy.” This test deliberately refuses the replacement at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1460, then manually appends the generation it checks at :1472–1475. A replacement publisher that leaves stale metadata can pass this test on every CI run. Keep the failed-launch cleanup assertions at :1463–1467. Remove the simulated replacement and use a successful relaunch through the public CLI, then assert that the replacement’s busy verdict is unchanged when the old worker emits a late event.
  • ⚠️ bin/fm-control.sh:669 - Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the old busy_gen behind because bin/fm-control.sh:662 skips cleanup. Retrying exit also skips cleanup at :681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.
  • ⚠️ tests/fm-busy-state.test.sh:223 - The 08 regression case releases its lock after one second, so a writer that silently falls back to five seconds still passes on every CI run. The intended oracle is an eight-second reclaim threshold. Give the lock an age between five and eight seconds and verify that it remains intact until eight. This catches a fallback that prematurely reclaims a lock for operators using the existing setting.

🔧 Fix applied.
7 issues (2 errors, 5 warnings) still open:

  • 🚨 bin/fm-control.sh:660 - AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalone fm-spawn.sh --relaunch --harness devin path can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815, :5327, :5374). If the old record says busy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190, :319, :361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths at bin/fm-control.sh:696, :724, and :1565 must use compatible retirement semantics.
  • 🚨 bin/fm-busy-event.sh:134 - AC2 requires coverage of “a live holder (still refused).” The changed if [ &#34;$age&#34; -ge &#34;$stale&#34; ] branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention with FM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151, :166, :189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203, :249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.
  • ⚠️ bin/fm-control.sh:669 - Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the old busy_gen behind because bin/fm-control.sh:662 skips cleanup. Retrying exit also skips cleanup at :681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.
  • ⚠️ bin/fm-busy-event.sh:143 - The wait deadline uses wall-clock time. If an operator corrects a host clock backward by a minute while a waiter is polling a fresh orphan, both now - start and the lock age become negative, so the writer can wait about a minute despite the advertised seven-second default bound. This is uncommon and affects launch, hook, and retirement calls through bin/fm-busy-event.sh:166 and :189. Bound elapsed waiting independently of the wall clock used to interpret filesystem timestamps. The refusal test at tests/fm-busy-state.test.sh:254 exercises a lock that stays young under a stable clock.
  • ⚠️ bin/fm-busy-event.sh:135 - Follow-up for a rare concurrent reclaim. If a late hook and a retire command are already waiting on the same fresh orphan, both can observe it crossing the age threshold in the same poll. One waiter can remove the orphan and acquire the lock before the other executes its pending removal. The second waiter then deletes a newly acquired lock whose holder is still inside the writer. This can break serialization for arm at bin/fm-busy-event.sh:166 and apply or retire at :189, with release at :151 also operating on the reused path. The race is distinct from reclaiming a single holder that has itself exceeded the age threshold. Address reclaim ownership at the shared writer boundary in a follow-up.
  • ⚠️ bin/fm-control.sh:1614 - Round 1's retirement-retry fix left out exits that close the pane. The original invocation accepts a missing endpoint as proof of a delivered exit at bin/fm-control.sh:681. If retirement then fails, retrying exit rejects that same missing endpoint at :649 before reaching retirement at :1614. This is uncommon and requires one retirement failure after a verified stop. The regression test at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1480 and :1497 keeps an idle shell in the pane, so it misses this case. Keep the missing-endpoint refusal and identify the supported recovery path in the retirement diagnostic. Relaunch already reconciles the missing-endpoint case at bin/fm-control.sh:1517 and :1527.
  • ⚠️ bin/fm-spawn.sh:4793 - Round 1's standalone relaunch fix also added retirement to automatic OMP secondmate recovery. AC1 requires retirement of a worker's recorded busy generation, and bin/fm-spawn.sh:4816 excludes secondmates from busy-generation publication. Every stopped or missing OMP secondmate recovered through fm-spawn --secondmate now reaches this extra metadata-lock and retirement path. Remove the OMP secondmate condition at bin/fm-spawn.sh:4793 and its separate prior-generation read and fallback at :2585 and :4794. The explicit relaunch branch covers the standalone worker replacement that prompted the fix.

🔧 Fix applied.
10 issues (2 errors, 8 warnings) still open:

  • 🚨 bin/fm-control.sh:660 - AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalone fm-spawn.sh --relaunch --harness devin path can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815, :5327, :5374). If the old record says busy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190, :319, :361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths at bin/fm-control.sh:696, :724, and :1565 must use compatible retirement semantics.
  • 🚨 bin/fm-busy-event.sh:134 - AC2 requires coverage of “a live holder (still refused).” The changed if [ &#34;$age&#34; -ge &#34;$stale&#34; ] branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention with FM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151, :166, :189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203, :249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.
  • ⚠️ bin/fm-control.sh:669 - Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the old busy_gen behind because bin/fm-control.sh:662 skips cleanup. Retrying exit also skips cleanup at :681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.
  • ⚠️ bin/fm-busy-event.sh:143 - The wait deadline uses wall-clock time. If an operator corrects a host clock backward by a minute while a waiter is polling a fresh orphan, both now - start and the lock age become negative, so the writer can wait about a minute despite the advertised seven-second default bound. This is uncommon and affects launch, hook, and retirement calls through bin/fm-busy-event.sh:166 and :189. Bound elapsed waiting independently of the wall clock used to interpret filesystem timestamps. The refusal test at tests/fm-busy-state.test.sh:254 exercises a lock that stays young under a stable clock.
  • ⚠️ bin/fm-busy-event.sh:135 - Follow-up for a rare concurrent reclaim. If a late hook and a retire command are already waiting on the same fresh orphan, both can observe it crossing the age threshold in the same poll. One waiter can remove the orphan and acquire the lock before the other executes its pending removal. The second waiter then deletes a newly acquired lock whose holder is still inside the writer. This can break serialization for arm at bin/fm-busy-event.sh:166 and apply or retire at :189, with release at :151 also operating on the reused path. The race is distinct from reclaiming a single holder that has itself exceeded the age threshold. Address reclaim ownership at the shared writer boundary in a follow-up.
  • ⚠️ bin/fm-control.sh:1614 - Round 1's retirement-retry fix left out exits that close the pane. The original invocation accepts a missing endpoint as proof of a delivered exit at bin/fm-control.sh:681. If retirement then fails, retrying exit rejects that same missing endpoint at :649 before reaching retirement at :1614. This is uncommon and requires one retirement failure after a verified stop. The regression test at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1480 and :1497 keeps an idle shell in the pane, so it misses this case. Keep the missing-endpoint refusal and identify the supported recovery path in the retirement diagnostic. Relaunch already reconciles the missing-endpoint case at bin/fm-control.sh:1517 and :1527.
  • ⚠️ bin/fm-spawn.sh:4793 - Round 1's standalone relaunch fix also added retirement to automatic OMP secondmate recovery. AC1 requires retirement of a worker's recorded busy generation, and bin/fm-spawn.sh:4816 excludes secondmates from busy-generation publication. Every stopped or missing OMP secondmate recovered through fm-spawn --secondmate now reaches this extra metadata-lock and retirement path. Remove the OMP secondmate condition at bin/fm-spawn.sh:4793 and its separate prior-generation read and fallback at :2585 and :4794. The explicit relaunch branch covers the standalone worker replacement that prompted the fix.
  • ⚠️ bin/fm-spawn.sh:4793 - Round 1 placed retirement after endpoint creation in standalone relaunch. For example, if an operator replaces a stopped Claude worker with Devin after its Herdr pane has closed, spawn creates a new pane at bin/fm-spawn.sh:4142 before retiring the old busy generation. If the metadata rewrite fails, the command exits with the new pane open and the task record still pointing at the old pane. A retry can create another pane. This is uncommon and requires one retirement failure. The same ordering applies to tmux at bin/fm-spawn.sh:3917, Zellij at bin/fm-spawn.sh:4179, and cmux at bin/fm-spawn.sh:4210. Retire the generation after the shared missing-endpoint and lease checks at bin/fm-spawn.sh:3896, before any backend creates a replacement. Keep retirement after the idle-shell proof when reusing an existing endpoint.
  • ⚠️ bin/fm-busy-event.sh:126 - Round 2 made Perl Time::HiRes mandatory whenever the busy lock is contested. The documented toolchain permits a tmux host without Perl. If teardown kills a worker while its hook owns the lock on such a host, the next writer refuses before inspecting the lock's age, even if the orphan is already reclaimable. This occurs on every contention or orphan recovery on affected hosts, while uncontended writes conceal the dependency. Arm at bin/fm-busy-event.sh:166 and apply or retire at bin/fm-busy-event.sh:189 share this path. Provide a bounded, wall-clock-independent fallback at lock acquisition for hosts without the Perl clock.
  • ⚠️ bin/fm-control.sh:652 - Round 2 also changed the generic missing-endpoint exit message to recommend relaunch. Every operator who repeats an exit after a pane closes now gets advice to start a replacement, even when no busy retirement failed and the intended outcome was to leave the worker stopped. The accepted recovery requirement only needs that advice in the retirement-failure diagnostic at bin/fm-control.sh:635, which the test covers at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1512. Restore the generic missing-endpoint message.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 7 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Relaunch a stopped synthetic Claude worker as a real local Codex worker in tmux. Its old busy state disappears and a late event is rejected. ✅ pass live Live tmux worker retirement and recovery transcript
Retry exit in a real tmux pane after an injected metadata rename failure. The stopped worker’s old generation is cleared. ✅ pass live Live tmux worker retirement and recovery transcript
Recover after a real Codex exit closes its tmux pane and an injected metadata rename fails. Relaunch clears the old generation and requires a valid lease before replacing the worker. ✅ pass live Live tmux worker retirement and recovery transcript
Kill a real busy-event writer while it holds a fresh lock on synthetic state. The first retirement waits and reclaims the orphan. ✅ pass live Live busy-lock timing and persisted state transcript
Attempt retirement while a real writer still holds a young lock. Retirement waits without changing the record and succeeds after release. ✅ pass live Live busy-lock timing and persisted state transcript
Set the stale threshold to 08 and retire against a six-second-old synthetic orphan. Reclaim occurs at eight seconds. ✅ pass live Live busy-lock timing and persisted state transcript
Attempt retirement during a simulated 60-second wall-clock rollback. The real CLI times out without changing busy state. ✅ pass live Live busy-lock timing and persisted state transcript
  • bash tests/fm-busy-state.test.sh
  • bash tests/fm-spawn-relaunch-dead-endpoint.test.sh
  • bash tests/fm-control-switch-model.test.sh
  • uv run --no-project --offline --no-python-downloads python -u .phase-test/locks.py drove the real busy-event CLI with a killed writer, active contention, a decimal threshold, and wall-clock rollback.
  • unshare -Urmn --propagation private bash .phase-test/workers.sh drove standalone relaunch, exit retry, and closed-pane recovery in isolated tmux with real Codex.
  • unshare -Urmn --propagation private bash .phase-test/workers-base.sh reproduced acceptance of the late event on the base commit.
  • Read the final persisted worker records, saved both CLI transcripts, and verified the disposable tmux servers had stopped.
✅ **Document** - passed

✅ No issues found.

🔧 **Lint** - 1 issue found → auto-fixed ✅
  • ⚠️ linter found issues (exit code 1)

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Push** - passed

✅ No issues found.

dnth added 5 commits November 27, 2026 18:47
…an busy locks

Adapt upstream PR 6733 and PR 8177.

retire_busy_incarnation (bin/fm-control.sh) retired the busy sidecar but
left busy_gen= in the task record, so the record and the sidecar disagreed
about the generation a later steer resolves. It now retires the exact armed
generation and drops the matching record line under the task-record lock,
leaving the record untouched when it already names another generation.

lock_acquire (bin/fm-busy-event.sh) gave up after 40 tries (about 2s) while a
lock only becomes reclaimable at FM_BUSY_LOCK_STALE_SECS (default 5s), so a
fresh orphan left by a killed writer failed the first writer behind it. It
now polls until a lock that existed at wait start can cross that bound, then
reclaims it, and still refuses within stale + 2s when a lock never ages.
A digit-only stale setting keeps its decimal value.
@dnth
dnth merged commit 1a10f96 into main Nov 27, 2026
22 checks passed
@dnth
dnth deleted the fm/fm-upstream-port-b-busy branch November 27, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant