Skip to content

fix(control): drop busy_gen from the task record when an incarnation is retired - #6733

Merged
kunchenguid merged 4 commits into
kunchenguid:mainfrom
tiago-peixoto:fm/upstream-4759
Oct 7, 2026
Merged

kunchenguid merged 4 commits into
kunchenguid:mainfrom
tiago-peixoto:fm/upstream-4759

Conversation

@tiago-peixoto

@tiago-peixoto tiago-peixoto commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4759.

Intent

What Changed

  • retire_busy_incarnation in bin/fm-control.sh now reads the current busy generation, retires that exact generation with fm-busy-event.sh retire --gen <gen> (previously --current-gen), and, only when the retire succeeds, removes the matching busy_gen= line from the task record (<id>.meta).
  • New helper clear_retired_meta_busy_gen does the removal under the task-record lock and publishes the rewritten record with fm_backlog_atomic_transition publish. It leaves the record untouched when busy_gen no longer names the retired generation, or when the record is missing or a symlink. It runs in fm-control.sh rather than inside the busy writer, because the busy writer is a child of teardown and spawn, which already hold that lock.
  • tests/fm-control.test.sh adds a test that exit on a codex agent removes the busy sidecar files and the busy_gen line while keeping the rest of the task record. Two existing tests now also assert that busy_gen stays in the task record after an unacknowledged interrupt and after a failed exit.

Risk Assessment

✅ Low: The change is one bounded task-record rewrite under the existing per-task lock and publish path, with a behavioral regression test; the only defects found are a macOS stderr and file-mode portability issue and one unrequired branch.

Testing

I stood up a disposable lab (bin/fm-live-lab.sh up --harness claude --worker) whose home is a checkout of target commit 47c74cf, with a real Claude worker spawned by bin/fm-spawn.sh on a private tmux server. Against that worker I ran the real fm-control.sh verbs interrupt, exit, a second exit, and relaunch, and snapshotted the task record, the busy sidecar files, and the pane's foreground command after each. exit removed both the sidecar files and the busy_gen line and kept every other line; interrupt left both in place; the second exit reported already-stopped and changed nothing; relaunch wrote a fresh matching busy_gen. I then ran the base commit's fm-control.sh exit on the relaunched worker and it left busy_gen behind with no sidecar, which is the defect this change fixes. The hermetic file tests/fm-control.test.sh also passed (45 ok lines, exit code 0). The scenario where the agent does not obey the exit was not driven live: a real Claude always obeys /exit, so only the hermetic test with a stubbed tmux covers it, and it is marked untested. I tore the lab down with bin/fm-live-lab.sh down and the worktree is clean. No screenshot applies: the surface is a CLI and state files, so the evidence is CLI transcripts. The lab helper's own readiness run ended "not ready" for two reasons unrelated to the change: I passed --expect-host no while a supervision host still started, and I stopped the worker while the helper was still polling for it to be parked.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Operator runs fm-control.sh &lt;id&gt; exit on a live armed Claude worker: the agent stops, the busy sidecar files disappear, and the task record no longer has a busy_gen line while every other line stays ✅ pass live exit-target.log: before has busy_gen=g1791326255.1730600.25712 and both sidecar files with pane command claude; after exit prints stopped, sidecars (none), no busy_gen line, the other 13 lines uncha…
Guard: fm-control.sh &lt;id&gt; interrupt on the same live worker leaves busy_gen and the sidecar files in place, because the incarnation is still running ✅ pass live exit-target.log: interrupt-delivered ... verified=agent-alive, then the task record still has the same busy_gen and both sidecar files exist, pane command claude
A second exit on the already stopped worker reports already-stopped and leaves the cleaned task record unchanged ✅ pass live exit-target.log: second exit prints already-stopped, rc=0, task record identical to the one after the first exit
fm-control.sh &lt;id&gt; relaunch after the exit starts a new Claude incarnation whose task record carries a fresh busy_gen equal to the new sidecar token ✅ pass live relaunch-and-baseline.log: relaunched ..., busy-gen file and task record both read g1791326894.2258028.18728, pane command claude
Baseline reproduction: the base commit's fm-control.sh exit on the same live worker removes the sidecar but leaves busy_gen in the task record ✅ pass live relaunch-and-baseline.log: base e06a46f exit prints stopped, sidecars (none), task record still has busy_gen=g1791326894.2258028.18728
Guard: an exit that the agent does not obey fails closed and keeps busy_gen in the task record ⏸️ untested no The prior payload did not establish a live result for this scenario. It was driven only through the hermetic test bash tests/fm-control.test.sh with a stubbed tmux (`ok - fm-control exit: a stubborn…
Evidence: Live exit on target commit: before, after interrupt, after exit, after second exit

Source: Live exit on target commit: before, after interrupt, after exit, after second exit

47c74cfb727870431bb1d1e2263edca265ebd6fd
--- before (armed worker spawned by fm-spawn.sh)
sidecars:
lab742c683c3184-worker.busy-gen
lab742c683c3184-worker.busy-state
busy-gen file: g1791326255.1730600.25712
task record (664):
window=firstmate:fm-lab742c683c3184-worker
endpoint_task_id=lab742c683c3184-worker
worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
project=/tmp/fmlab.AyDAnO/home/projects/notes
harness=claude
kind=ship
mode=local-only
yolo=on
branch=fm/lab742c683c3184-worker
tasktmp=/tmp/fm-lab742c683c3184-worker
model=sonnet
effort=low
busy_gen=g1791326255.1730600.25712
spawn_gen=s1791326255.1727964.26581
pane command: claude

$ bin/fm-control.sh lab742c683c3184-worker interrupt
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
interrupt-delivered lab742c683c3184-worker harness=claude backend=tmux verified=agent-alive cancel=unconfirmed
 [rc=0]
--- after interrupt
sidecars:
lab742c683c3184-worker.busy-gen
lab742c683c3184-worker.busy-state
busy-gen file: g1791326255.1730600.25712
task record (664):
window=firstmate:fm-lab742c683c3184-worker
endpoint_task_id=lab742c683c3184-worker
worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
project=/tmp/fmlab.AyDAnO/home/projects/notes
harness=claude
kind=ship
mode=local-only
yolo=on
branch=fm/lab742c683c3184-worker
tasktmp=/tmp/fm-lab742c683c3184-worker
model=sonnet
effort=low
busy_gen=g1791326255.1730600.25712
spawn_gen=s1791326255.1727964.26581
pane command: claude

$ bin/fm-control.sh lab742c683c3184-worker exit
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
stopped lab742c683c3184-worker harness=claude backend=tmux endpoint=firstmate:fm-lab742c683c3184-worker worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
 [rc=0]
--- after exit
sidecars:
(none)
task record (600):
window=firstmate:fm-lab742c683c3184-worker
endpoint_task_id=lab742c683c3184-worker
worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
project=/tmp/fmlab.AyDAnO/home/projects/notes
harness=claude
kind=ship
mode=local-only
yolo=on
branch=fm/lab742c683c3184-worker
tasktmp=/tmp/fm-lab742c683c3184-worker
model=sonnet
effort=low
spawn_gen=s1791326255.1727964.26581
pane command: bash

$ bin/fm-control.sh lab742c683c3184-worker exit   (second time)
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
already-stopped lab742c683c3184-worker harness=claude backend=tmux endpoint=firstmate:fm-lab742c683c3184-worker worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
 [rc=0]
--- after second exit
sidecars:
(none)
task record (600):
window=firstmate:fm-lab742c683c3184-worker
endpoint_task_id=lab742c683c3184-worker
worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
project=/tmp/fmlab.AyDAnO/home/projects/notes
harness=claude
kind=ship
mode=local-only
yolo=on
branch=fm/lab742c683c3184-worker
tasktmp=/tmp/fm-lab742c683c3184-worker
model=sonnet
effort=low
spawn_gen=s1791326255.1727964.26581
pane command: bash
Evidence: Live relaunch on target commit, then baseline exit with base commit code (bug reproduced)

Source: Live relaunch on target commit, then baseline exit with base commit code (bug reproduced)

$ bin/fm-control.sh lab742c683c3184-worker relaunch --note ...   (target commit 47c74cfb)
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
relaunched lab742c683c3184-worker harness=claude from=claude model=sonnet effort=low backend=tmux endpoint=firstmate:fm-lab742c683c3184-worker worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
 [rc=0]
--- after relaunch (new incarnation)
sidecars:
lab742c683c3184-worker.busy-gen
lab742c683c3184-worker.busy-state
busy-gen file: g1791326894.2258028.18728
task record busy/spawn lines:
window=firstmate:fm-lab742c683c3184-worker
harness=claude
busy_gen=g1791326894.2258028.18728
spawn_gen=s1791326894.2256330.21143
pane command: claude

$ <base e06a46fe>/bin/fm-control.sh lab742c683c3184-worker exit   (baseline, before the fix)
fm-gate-refuse: gate agent lifecycle permitted only against lab home /tmp/fmlab.AyDAnO/home
stopped lab742c683c3184-worker harness=claude backend=tmux endpoint=firstmate:fm-lab742c683c3184-worker worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
 [rc=0]
--- after exit with BASE code: sidecar gone, busy_gen left behind (the bug)
sidecars:
(none)
task record busy/spawn lines:
window=firstmate:fm-lab742c683c3184-worker
harness=claude
busy_gen=g1791326894.2258028.18728
spawn_gen=s1791326894.2256330.21143
pane command: bash
Evidence: Lab stand-up transcript

Source: Lab stand-up transcript

lab: /tmp/fmlab.AyDAnO (tear down with: bin/fm-live-lab.sh down /tmp/fmlab.AyDAnO)
tree: 47c74cfb727870431bb1d1e2263edca265ebd6fd from ~/.no-mistakes/worktrees/5284051b2355/01M49N6YTMN76GNKZE9YPDVFPC
spawned lab742c683c3184-worker harness=claude kind=ship mode=local-only yolo=on window=firstmate:fm-lab742c683c3184-worker worktree=/tmp/fmlab.AyDAnO/treehouse/.treehouse/notes-863bbf/1/notes
gate: /tmp/fmlab.AyDAnO/home/data/lab742c683c3184-worker/gate (touch, then message the worker to resume)
primary: claude --setting-sources project,local --model sonnet --effort medium --permission-mode auto
ok primary: claude pid 1738299 in /tmp/fmlab.AyDAnO/home
ok probe: the primary answered LABREADY-742c683c3184
ok trust: /tmp/fmlab.AyDAnO/home is trusted in the Claude store
fail host: supervision host pid 1754547 runs (expected none)
ok watcher: live watcher with a fresh beacon
fail worker: the worker is not currently parked on /tmp/fmlab.AyDAnO/home/data/lab742c683c3184-worker/gate (state: unknown · source: pane · harness state unavailable (unknown missing))
ok treehouse: ~/.treehouse unchanged
not ready after 600s; the lab is left up for inspection: bin/fm-live-lab.sh pane /tmp/fmlab.AyDAnO, then bin/fm-live-lab.sh down /tmp/fmlab.AyDAnO
Evidence: tests/fm-control.test.sh output

Source: tests/fm-control.test.sh output

ok - fm-control exit: every verified harness gets its own verified exit command
ok - fm-control interrupt: every verified harness gets its own verified key and repeat count
ok - fm-control Devin interrupt: second press only after the armed hint, then busy invalidated without fabricating idle
ok - fm-control Devin interrupt: an idle agent gets one Escape and reports not-running
ok - fm-control Devin exit: a busy record whose turn already ended never opens the revert picker
ok - fm-control Devin interrupt: a revert picker a press opened is closed with Escape, never Enter
ok - fm-control Devin: an open revert picker refuses every typed command
ok - fm-control interrupt: opencode needs a double Escape, claude a single one
ok - fm-control: a harness with no verified control mechanics is refused, not guessed at
ok - fm-control-lib: a recorded harness resolves to its verified adapter without guessing
ok - fm-control-lib: only a runtime's own recorded session has a relaunch resume form
ok - fm-control: prefixed recorded harnesses reach interrupt and exit mechanics
ok - fm-control-lib: the backend key matrix matches each adapter's real send-key surface
ok - fm-control-lib: adapter capability is per task kind, not per adapter alone
ok - fm-control interrupt: a backend that cannot deliver the harness's key refuses instead of sending another
ok - fm-control: a backend that cannot prove an agent stopped refuses exit and relaunch
ok - fm-control-lib: stop-proving verbs are gated on the backends that really classify agent state
ok - fm-control: a legacy window label is refused and the exact task id is named
ok - fm-control: an explicit backend endpoint is never a control target
ok - fm-control: an unrecorded task id is refused
ok - fm-control: a record whose endpoint identity names another task is refused
ok - fm-control: a remotely placed secondmate is refused by placement, not by a metadata complaint
ok - fm-control: interrupt and exit lock before task-state resolution
ok - fm-control: the verb list is closed - no raw keys, arbitrary text, or clear verb
ok - fm-control: resume is refused with the determinism reason and the alternative
ok - fm-control: profile and note flags belong to relaunch only
ok - fm-control exit: an already-stopped agent is idempotent success with no bytes sent
ok - fm-control exit: an unprovable tmux endpoint refuses instead of claiming the agent stopped
ok - fm-control interrupt: refuses when no agent is running rather than keying a shell
ok - fm-control exit: an endpoint whose process cannot be attributed refuses
ok - fm-control exit: a busy agent receives interrupt delivery before the exit command
ok - fm-control exit: an idle agent goes straight to its exit command
ok - fm-control exit: retiring a codex incarnation drops busy_gen with the sidecar
ok - fm-control exit: an already-open background-task picker is not typed into
ok - fm-control exit: the Enter that opens the background-task picker is not followed by a confirming Enter
ok - fm-control exit: a picker that renders after the submit returned is named when the stop wait times out
ok - fm-control interrupt: unconfirmed delivery preserves observed busy state
ok - fm-control interrupt: muse confirms cancellation from its session log
ok - fm-control interrupt: postconditions are revalidated after acknowledgement polling
ok - fm-control exit: an interrupt-stopped agent satisfies the gone-state postcondition
ok - fm-control exit: a stubborn agent reports delivered input and an unconfirmed exit
ok - fm-control interrupt: grok reports delivery without claiming cancellation
ok - fm-control interrupt: grok's idle footer does not confirm cancellation
ok - fm-control: a lifecycle command to a secondmate is unmarked and opens no reply expectation
ok - fm-control's arrival leaves fm-send's from-firstmate marking untouched
Evidence: Before and after the fix, same live worker
target 47c74cfb exit -> stopped | sidecars: (none) | task record: no busy_gen line, spawn_gen and 12 other lines kept
base e06a46fe exit -> stopped | sidecars: (none) | task record: busy_gen=g1791326894.2258028.18728 still present
- Outcome: ⚠️ 1 info across 1 run (14m4s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ⚠️ bin/fm-control.sh:610 - chmod --reference=&#34;$meta&#34; &#34;$tmp&#34; is a GNU-only option, and its stderr is not silenced. On macOS (which this repo supports, see the Darwin branches in bin/fm-busy-event.sh and commit 5838f10) BSD chmod rejects the option, so every successful exit or relaunch of an armed task prints a chmod usage error to stderr before the chmod 0600 fallback runs. bin/fm-secondmate-restart.sh:175 captures that stderr with 2&gt;&amp;1. The rewritten task record also ends at mode 0600 on macOS and at the original mode on Linux, so the two platforms differ. The sibling rewriters in bin/fm-x-lib.sh:916-941 publish the mktemp file (already 0600) with no chmod at all. Smallest fix: remove the chmod block to match those siblings, or add 2&gt;/dev/null to the first chmod. This is the only use of --reference in bin/.
  • ⚠️ bin/fm-control.sh:631 - The new else branch in retire_busy_incarnation (lines 631-634) clears busy_gen from the task record when the busy sidecar is already absent. The stated purpose of the commit is the sequence where a deliberate exit removes the sidecar and leaves busy_gen behind; that sequence always takes the if branch. The else branch runs only when an agent is alive, the task record names a busy_gen, and no sidecar exists, which fm-spawn never produces because it writes busy_gen only after arming. A record left stale by an earlier exit does not reach it either: do_exit returns already-stopped at bin/fm-control.sh:644 before calling retire_busy_incarnation. No test in tests/fm-control.test.sh exercises this branch. No stated requirement needs it, so the recommended remedy is to remove it.
  • ℹ️ bin/fm-control.sh:629 - If the sidecar retire succeeds and clear_retired_meta_busy_gen then fails (mktemp or publish failure), || true discards the failure and the two records disagree again. Nothing retries: the next exit reads the agent as dead and returns already-stopped before reaching retire_busy_incarnation. The traced consequence is small: bin/fm-teardown.sh:1415 retires with the stale gen and bin/fm-busy-event.sh treats a missing sidecar as already retired, and bin/fm-send.sh:305 returns early when the sidecar is absent. Noted as an accepted tradeoff, no action needed.

🔧 Fix applied.
4 issues (2 warnings, 2 infos) still open:

  • ⚠️ bin/fm-control.sh:610 - chmod --reference=&#34;$meta&#34; &#34;$tmp&#34; is a GNU-only option, and its stderr is not silenced. On macOS (which this repo supports, see the Darwin branches in bin/fm-busy-event.sh and commit 5838f10) BSD chmod rejects the option, so every successful exit or relaunch of an armed task prints a chmod usage error to stderr before the chmod 0600 fallback runs. bin/fm-secondmate-restart.sh:175 captures that stderr with 2&gt;&amp;1. The rewritten task record also ends at mode 0600 on macOS and at the original mode on Linux, so the two platforms differ. The sibling rewriters in bin/fm-x-lib.sh:916-941 publish the mktemp file (already 0600) with no chmod at all. Smallest fix: remove the chmod block to match those siblings, or add 2&gt;/dev/null to the first chmod. This is the only use of --reference in bin/.
  • ⚠️ bin/fm-control.sh:631 - The new else branch in retire_busy_incarnation (lines 631-634) clears busy_gen from the task record when the busy sidecar is already absent. The stated purpose of the commit is the sequence where a deliberate exit removes the sidecar and leaves busy_gen behind; that sequence always takes the if branch. The else branch runs only when an agent is alive, the task record names a busy_gen, and no sidecar exists, which fm-spawn never produces because it writes busy_gen only after arming. A record left stale by an earlier exit does not reach it either: do_exit returns already-stopped at bin/fm-control.sh:644 before calling retire_busy_incarnation. No test in tests/fm-control.test.sh exercises this branch. No stated requirement needs it, so the recommended remedy is to remove it.
  • ℹ️ bin/fm-control.sh:629 - If the sidecar retire succeeds and clear_retired_meta_busy_gen then fails (mktemp or publish failure), || true discards the failure and the two records disagree again. Nothing retries: the next exit reads the agent as dead and returns already-stopped before reaching retire_busy_incarnation. The traced consequence is small: bin/fm-teardown.sh:1415 retires with the stale gen and bin/fm-busy-event.sh treats a missing sidecar as already retired, and bin/fm-send.sh:305 returns early when the sidecar is absent. Noted as an accepted tradeoff, no action needed.
  • ℹ️ bin/fm-control.sh:571 - The comment above clear_retired_meta_busy_gen says a second acquire of the task-record lock inside the busy writer "by this process is treated as an abandoned hold". The busy writer is bin/fm-busy-event.sh, which runs as a child process with its own PID. fm_lock_try_acquire (bin/fm-wake-lib.sh:1176) reclaims a hold only when the recorded PID equals the caller's own PID; for a live foreign holder it returns 1 (bin/fm-wake-lib.sh:1192), so fm_lock_acquire_wait would wait forever while teardown or spawn waits on the child. The design conclusion (do not clear the line inside the busy writer) still holds, but the stated consequence is wrong: it is a deadlock, not a reclaimed hold. Reword the last clause to name the deadlock. Comment-only change, no behavior affected.

🔧 Fix applied.
1 info still open:

  • ℹ️ bin/fm-control.sh:629 - If the sidecar retire succeeds and clear_retired_meta_busy_gen then fails (mktemp or publish failure), || true discards the failure and the two records disagree again. Nothing retries: the next exit reads the agent as dead and returns already-stopped before reaching retire_busy_incarnation. The traced consequence is small: bin/fm-teardown.sh:1415 retires with the stale gen and bin/fm-busy-event.sh treats a missing sidecar as already retired, and bin/fm-send.sh:305 returns early when the sidecar is absent. Noted as an accepted tradeoff, no action needed.
⚠️ **Test** - 1 info
  • ℹ️ bin/fm-control.sh:611 - After the live exit the task record's file mode changed from 664 to 600, because the rewrite publishes a mktemp file. This matches the recorded review decision to follow the sibling record rewriters, so no action is needed; it is noted only because it is visible in exit-target.log.
  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Operator runs fm-control.sh &lt;id&gt; exit on a live armed Claude worker: the agent stops, the busy sidecar files disappear, and the task record no longer has a busy_gen line while every other line stays ✅ pass live exit-target.log: before has busy_gen=g1791326255.1730600.25712 and both sidecar files with pane command claude; after exit prints stopped, sidecars (none), no busy_gen line, the other 13 lines uncha…
Guard: fm-control.sh &lt;id&gt; interrupt on the same live worker leaves busy_gen and the sidecar files in place, because the incarnation is still running ✅ pass live exit-target.log: interrupt-delivered ... verified=agent-alive, then the task record still has the same busy_gen and both sidecar files exist, pane command claude
A second exit on the already stopped worker reports already-stopped and leaves the cleaned task record unchanged ✅ pass live exit-target.log: second exit prints already-stopped, rc=0, task record identical to the one after the first exit
fm-control.sh &lt;id&gt; relaunch after the exit starts a new Claude incarnation whose task record carries a fresh busy_gen equal to the new sidecar token ✅ pass live relaunch-and-baseline.log: relaunched ..., busy-gen file and task record both read g1791326894.2258028.18728, pane command claude
Baseline reproduction: the base commit's fm-control.sh exit on the same live worker removes the sidecar but leaves busy_gen in the task record ✅ pass live relaunch-and-baseline.log: base e06a46f exit prints stopped, sidecars (none), task record still has busy_gen=g1791326894.2258028.18728
Guard: an exit that the agent does not obey fails closed and keeps busy_gen in the task record ⏸️ untested no The prior payload did not establish a live result for this scenario. It was driven only through the hermetic test bash tests/fm-control.test.sh with a stubbed tmux (`ok - fm-control exit: a stubborn…
  • bin/fm-live-lab.sh up --harness claude --worker --supervision-host none --expect-host no (lab home at 47c74cfb, real Claude worker spawned by fm-spawn.sh on a private tmux server)
  • FM_HOME=&lt;lab&gt;/home bin/fm-control.sh &lt;worker-id&gt; interrupt then snapshot of state/<id>.meta, state/<id>.busy-gen, state/<id>.busy-state
  • FM_HOME=&lt;lab&gt;/home bin/fm-control.sh &lt;worker-id&gt; exit then the same snapshot plus tmux #{pane_current_command}
  • FM_HOME=&lt;lab&gt;/home bin/fm-control.sh &lt;worker-id&gt; exit a second time (idempotence)
  • FM_HOME=&lt;lab&gt;/home bin/fm-control.sh &lt;worker-id&gt; relaunch --note ... then snapshot
  • Baseline: git archive e06a46fe to a temp dir, then that copy's bin/fm-control.sh &lt;worker-id&gt; exit against the same lab worker
  • bash tests/fm-control.test.sh
  • bin/fm-live-lab.sh down &lt;lab&gt; and git status --short (clean)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

A deliberate exit removed the busy sidecar and left busy_gen in the task record, so the two records disagreed about whether that incarnation was still observable.
@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[High risk] Modifies task record cleanup during agent shutdown.

The PR appears safe to merge; the earlier test gap is fixed.

Reviews (2) · Last reviewed commit: "no-mistakes(ci): The test `test_exit_dro..."

Comment thread tests/fm-control.test.sh Resolved
…ecar` in tests/fm-control.test.sh now compares the whole task record (the `state/<id>.meta` file), so the Greptile finding is fixed. Invariant: after `exit` retires an incarnation, the task record must equal the record from before `exit` with only the `busy_gen` line removed. This test is the only place in the change that asserts the record survives the rewrite, so it is the only site to fix. The other `busy_gen` tests assert that the line stays, and they do not go through the rewrite. What changed: before `exit`, the test writes the record without its `busy_gen` line to `expected.meta`. After `exit`, the test runs `diff` between that expected copy and the real record, and fails with the diff output if they differ. This one comparison replaces the two earlier checks (no `busy_gen` line left, and the `window` line present), because it covers both. I did not change bin/fm-control.sh or any other file. How I know it works: - I ran `bash tests/fm-control.test.sh`: exit code 0, 45 lines starting with `ok`, no other lines. - I temporarily changed the rewrite in bin/fm-control.sh to also drop the `harness` line. The test then failed with `not ok - exit should drop only busy_gen from the task record:` and the diff `< harness=codex`. The earlier `window`-only check would have passed that rewrite. I restored bin/fm-control.sh afterwards; `git status` shows only tests/fm-control.test.sh modified. - `bash -n` and `shellcheck` on the test file report no new warnings from the edit. The change is not committed; the working tree holds it
@kunchenguid

kunchenguid commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: reviewed head 9e6d3e8e against main 2e8cd9e9, and this is ready to land. Thanks, @tiago-peixoto.

What I checked:

  • Closes Retiring a Codex incarnation leaves busy_gen in task metadata, so native observation still applies #4759: verified. On main, fm-control.sh <id> exit retires the busy sidecar and record through retire_busy_incarnation, but it leaves the spawn-written busy_gen= in state/<id>.meta. So the stopped incarnation still claims native observation. This PR now retires the exact current generation (--gen <gen> instead of --current-gen). Only after that retire succeeds does it drop the matching busy_gen= line, under the task-record lock, through fm_backlog_atomic_transition publish. It leaves the record alone when busy_gen already names a different generation, so a concurrent relaunch's fresh generation is safe. It also leaves the record alone when the record is missing or is a symlink. The other retire callers do not leave a stale meta: fm-spawn relaunch writes a fresh busy_gen, and teardown removes the record.
  • Lock safety. retire_busy_incarnation is called only from do_exit (the exit verb, and relaunch, which snapshots the record to meta-prior first). No caller holds the task-record lock when invoking fm-control.sh, so the new lock acquire can't deadlock. That is why the comment keeps the rewrite out of the busy writer. Teardown already handles an empty busy_gen by falling back to the sidecar.
  • Greptile's P2 (the test checked only window) is fixed. The test now diffs the whole record minus busy_gen. Greptile reports 5/5 with no open finding.
  • The diff touches only bin/fm-control.sh and tests/fm-control.test.sh. No workflow edits.
  • The attestation matches this head (MATCH). CI and no-mistakes are green on this head. The PR is MERGEABLE/CLEAN. Main moved two commits past this PR's base, and neither touches these files. A local merge onto main applies cleanly, and tests/fm-control.test.sh passes there (45 ok).

Contract-class: restore. exit is specified to retire the incarnation's busy wiring so that nothing stale survives the agent. The task-record busy_gen was the piece left behind, and this makes the existing exit path do what it already promises. It adds no new default surface.

VISION (compact, per rule): One captain / one interface: aligns (a stopped worker no longer reports an unverified busy state forever). Authority explicit: aligns (no new autonomy). Scripts own mechanics: aligns (exact generation-matched rewrite). Restart non-event: aligns (durable records now agree after a retire). Delegation with a spine: aligns. Fleet outlives vendor: aligns (generic across harnesses). Scope: aligns.

Squash-merging. Closes #4759.

@kunchenguid
kunchenguid merged commit e7c3bdc into kunchenguid:main Oct 7, 2026
20 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @tiago-peixoto — really appreciate you taking the time on this.

kirkins added a commit to kirkins/firstmate that referenced this pull request Oct 7, 2026
…y continuity (#2)

* fix: refuse a confirming Enter on the Claude background-task exit picker (kunchenguid#6666)

* fix: refuse a confirming Enter on the Claude exit picker

The background-task picker still classifies as pending, so a retried Enter confirms "Exit and stop tasks".
Stop after the Enter that opened it, and raise the existing stale wake with the dialog name.

A non-paused secondmate still skips that wake, so a secondmate parked on the picker still looks idle.
Model-downgrade confirmation, MCP approval, and any other Claude exit confirmation are not covered, because there is no recorded screen for them.

* no-mistakes(review): fix: anchor exit picker match and wake second mates

* fix: refuse a typed submit while the Claude exit picker is open

A pane that already shows the picker must not receive the message or a confirming Enter.
The match requires the recorded heading and footer lines, and it is skipped when no dialog name is being recorded.

* no-mistakes(review): restore watcher to main behaviour, drop dialog wake

* no-mistakes(test): test: align Herdr picker fixtures with the preflight read

* no-mistakes(document): document exit refusal on a recognised dialog

* fix: remove the dialog file when exit runs in a subshell

do_exit is invoked from a command substitution, so the parent cleanup never saw the path it is supposed to delete.

* no-mistakes(review): fix: set the dialog file path after the control lock

* no-mistakes(document): document why the dialog file path follows the lock

* no-mistakes(ci): The exit cleanup in bin/fm-control.sh now deletes the dialog file first and releases the control lock second. The changes are in the worktree and are not committed. Invariant: only the process that holds the control lock for a task may write or delete that task's dialog file ($STATE/<id>.composer-dialog, the file that carries the picker name from the composer read to the Enter checks). The old cleanup released the lock and then deleted the file, so the delete ran outside the lock. Places where this invariant must hold: control_cleanup is the only place that deletes the file, and the single assignment after CONTROL_LOCK_HELD=1 is the only place that names it. do_exit only truncates the file, and it runs while the parent holds the lock. A process that loses the lock never sets the path, so it deletes nothing (earlier fix, unchanged). No sibling site needed a change. Fix: in control_cleanup, the rm of the dialog file moved above the fm_lock_release call, with a two-line comment that gives the reason. No dialog recognition or supervision code changed. Test: added test_exit_removes_the_dialog_file_before_releasing_the_lock in tests/fm-control-relaunch.test.sh. It runs one `fm-control exit` with a recording rm on PATH. The lock release removes paths at or under the control lock with rm, so the recording rm writes whether the dialog file exists at that moment. The test asserts the last record is "absent". It starts no second command. Verification, all run locally: - Before the fix, the new test failed with "the dialog file must be gone when the control lock is released, got: present". - After the fix, tests/fm-control-relaunch.test.sh exits 0 with 75 ok lines and no "not ok" line, including the new test and the existing test that the file is gone after exit and after relaunch. - tests/fm-control.test.sh exits 0 with 44 ok lines. - shellcheck -x on both changed files exits 0 with no output. One thing I noticed and did not change: tests/fm-control-relaunch.test.sh prints 615 "rm: cannot remove ... Permission denied" lines during its temp-directory teardown. The count is the same with my change reverted, so this change does not cause it, and the suite still exits 0. I could not read the Greptile check log (the check run was not found), so I worked from the finding text and the user instructions

* fix: restore timeout watchdog compatibility with macOS Bash 3.2 (kunchenguid#6028)

* fix: support stock macOS Bash in timeout watchdog

* no-mistakes(ci): Fixed ci-1 in tests/fm-timeout-lib.test.sh: both startup-owner failure paths now kill the bounded command group before killing the watchdog. Fault injection reproduced both leaks before the fix and confirmed cleanup afterward. Bash 3.2 suite, syntax check, ShellCheck, and diff checks passed; GNU timeout coverage skipped because the binary is unavailable. Production code unchanged

* fix(project-management): use subshell form for Initialize command (kunchenguid#6699)

Wrap the cd command in a subshell to comply with the cd-guard policy
that blocks persistent top-level directory changes in the primary
firstmate checkout. The subshell form (cd projects/<name> && ...) is
accepted by the policy as documented in issue kunchenguid#6502.

Fixes kunchenguid#6502

* feat(bin): add armable daily startup growth check (kunchenguid#6725)

* Add daily startup growth check

* no-mistakes(review): watch printed startup memory, retain growth baselines, drop knobs

* no-mistakes(review): delegate budget verdict, report before publish, pin shim home

* no-mistakes(review): baseline first sightings silently, drop mtime, spare secondmates

* no-mistakes(review): cap the wake line, validate budget verdict fields

* no-mistakes(review): guard record schema, check appends, tighten assertions

* no-mistakes(review): drop arbitrary tracked library, make re-arm idempotent

* no-mistakes(review): correct tracked-set wording, restore shim guards, register artifacts

* no-mistakes(review): exit on signal instead of publishing partial record

* no-mistakes(document): correct startup-growth record removal cost in state registry

* no-mistakes(test): sweep orphaned empty startup-growth temp records on due evaluation

* no-mistakes(document): note watcher need for armed startup growth check

* no-mistakes(ci): Diagnosed "Behavior portable serial 5": the shard failed on tests/fm-contributions.test.sh in test_arm_plumbs_a_configured_budget_into_the_check_shim (inherited mode) with "generated check did not attempt a read" and a missing forge/calls file. Root cause: bin/fm-contributions.sh:379-382 computes DEADLINE=$(date +%s)+BUDGET and then gates each read on DEADLINE-$(date +%s) >= OBSERVATION_RESERVE (= min(BUDGET,15)). At the one-second budget this test uses, that gate demands zero elapsed whole seconds, so a wall-clock second boundary crossing between the two date calls makes the poll loop break before any forge read. The test calls wrap_forge (which installs a fake date reading $FORGE/clock only when that file exists) but never seeded forge/clock, so it ran against the real clock. The repo already documents this exact hazard at tests/fm-contributions.test.sh:675-677 and freezes the clock in every other budget-constrained test: test_budget_exhaustion_keeps_prior_record (both modes), test_genuine_failure_near_deadline_is_unavailable, test_reservation_defers_later_url_when_fifteen_seconds_do_not_remain, test_slow_read_deadline_kill_is_budget_refusal, test_budget_is_cut_down_to_the_watcher_check_bound. test_arm_plumbs was the only omission; its single loop body covers both the configured and inherited modes. The remaining wrap_forge users run the default 20-second budget (5 seconds of slack above the reserve) and are not reachable by this race. Fix (smallest, matching the sibling idiom): added the clock freeze `/bin/date +%s > "$home/forge/clock"` plus a two-line comment inside that test's loop, before the poll. Three added lines in tests/fm-contributions.test.sh; no production code and no other file touched. Verification: reproduced the exact CI failure message by running a scratch copy whose fake date advances one second per read (worst case of the unfrozen clock) - missing forge/calls, "not ok - generated check did not attempt a read". With the fix, the single test passes 10/10 standalone and the full tests/fm-contributions.test.sh passes all 46 assertions with exit 0. tests/fm-startup-growth-check.test.sh still exits 0. bash -n and shellcheck -S warning are clean on the edited file; scratch repro files were removed, leaving only the intended three-line change. Note for the author: this flake is not caused by this branch - tests/fm-contributions.test.sh is untouched by the PR and the race is pre-existing, surfacing only on a loaded runner (the shard's neighbouring assertions show 80+ second gaps). It was fixed because it is genuine nondeterminism with an established in-repo remedy and the check cannot otherwise go green. No work was done on the unselected greptile findings (ci-2..ci-5) or the cancelled "Behavior portable serial 2" check (ci-6)

* no-mistakes(ci): Diagnosed "Behavior portable serial 6": the omitted log section (fetched with gh api) shows the shard failed on tests/fm-calm-pi-extension.test.sh in test_hidden_block_geometry_e2e with "Pi Calm hidden-block geometry E2E did not complete the /reload viewport transition" (exit=1, duration_ms=23437; the family line pure-contract-unit count=3 duration_ms=51193 failed=1 matches devin-harness 3203 + calm-pi 23437 + task-delivery 24553). Root cause: tests/fm-calm-pi-extension.test.sh:2831 wait_for_geometry_transition detected the /reload by SAMPLING a single intermediate frame - it polled tmux capture-pane for Pi's transient "Reloading keybindings, extensions, skills, prompts, themes, and context files..." box and only accepted the rebuilt transcript afterwards (elif gated on saw_transient). Pi shows that box only while the reload runs and replaces it via dismissReloadBox when done, so on a loaded runner the box can live entirely between two polls; saw_transient then stays 0 forever and the 600-attempt wait times out although the reload fully succeeded. Reproduced locally under CPU oversubscription with CI's Pi 1.0.4: 1 failure in 8 runs, diagnostics proving the mechanism ("DIAG: transition timeout saw_transient=0 attempt=600") while the captured viewport already showed the completed reload status row and the intact collapsed transcript. Not a Pi-version regression: 1.0.4's handleReloadCommand and both banner strings are identical to the installed 0.85.1, and shard 6 of the previous pipeline run (job 112490351105) passed this same script on Pi 1.0.4. Invariant violated: a TUI E2E assertion must key off a durable state the UI retains, never off one intermediate frame polling may miss. Sibling enumeration: the helper was defined once and called once; grep for transient/saw_/two-stage waits across tests/, bin/ and .pi/ found no other one-shot-frame wait - every other wait in the file keys off durable text presence/absence or off continuously repeating animation frames (the working-ship motion checks) where sampling eventually connects; no doc or other test referenced the helper. Fix (smallest, one helper): renamed it wait_for_geometry_reload and made it wait for the durable status row Pi appends once the reload completed and the chat was rebuilt ("Reloaded keybindings, extensions, skills, prompts, themes, and context files", a persistent chat child via showStatus, present in both 0.85.1 and 1.0.4) together with CALM_GEOMETRY_FINAL. That marker is absent before the reload and never appears if the reload throws, so the check is strictly stronger than before (a failed reload now fails the test instead of passing on transient-then-final). One file changed: tests/fm-calm-pi-extension.test.sh, 2 hunks, no production code. Verification: deterministic before/after - with polls spaced 0.4s apart (the loaded-runner worst case) the pre-fix helper fails with exactly the CI message and the post-fix helper passes; 12/12 consecutive passes of the isolated test under load with Pi 1.0.4; full file 15/15 ok exit 0 under LANG=C.utf8 (matching the runner locale); bash -n, shellcheck -S warning and bin/fm-lint.sh clean; scratch repro files and the temp Pi 1.0.4 install removed, git status shows only the one intended modification. Notes: (1) the flake is not caused by this branch - that test file is untouched by the PR and the race is pre-existing; fixed because it is genuine nondeterminism and the check cannot otherwise go green. (2) Under far harsher load than CI applies a separate unrelated flake appeared in the same test: Pi's "Warning: tmux extended-keys is off..." row occasionally lands between the collapsed [skill] ahoy row and the final response, making assert_geometry_gap see 5 rows instead of 2. Different cause, never seen in CI, and its plausible remedies would change key delivery for the other TUI tests in the file, so it was left alone rather than growing this fix

---------

Co-authored-by: Quartermaster <[email protected]>

* fix(bin): reopen the remote-reply continuity decision on a later break (kunchenguid#6708)

* fix: reopen a remote-reply continuity break after repair

A later break for the same route and reason was swallowed after the
operator resolved the first one, because the status line matched for
the life of the log. The continuity ingest now appends again when the
cursor has moved or retirement has reset that episode, and an unchanged
re-read still appends nothing.

status_event_recorded is unchanged. Its other callers are the
pending-reply escalation, which already decides its own episode, the
parent-channel note append, and the remote document transfer note.

* no-mistakes(review): seed continuity episode for already recorded break line

* no-mistakes(document): document when a remote-reply continuity break reopens

* no-mistakes(ci): The adapter now stores the continuity episode record before it appends the `blocked` line, so a failed store appends nothing. The changes are uncommitted in the worktree, in `bin/fm-procevent-remote-reply.sh` and `tests/fm-remote-reply.test.sh`. **Invariant:** a continuity `blocked` line is on the parent status log only when the episode record for that break is already stored. Only the `continuity-broken` branch of `cmd_ingest` appends that line, so the fix is at that one place. **What changed in `cmd_ingest`:** - It decides first whether the break needs a line, then stores the episode record, then appends. - If storing the record fails, it stops with "cannot record continuity episode" and appends nothing. - If the line is already on the log and no episode record exists, it stores the record and does not append. **One addition you did not ask for:** if the append fails after the record is stored, the adapter puts the earlier record back (or deletes the new one when none existed). Without that, the retry would read as the unchanged repeat and append nothing, which is the issue 6701 failure again. **Deviation from your test instruction:** the test does not use `chmod` on the cursor directory. `write_continuity_episode` runs `chmod 700` on that directory before every write, so a read-only directory is made writable again. The test instead makes `mktemp` fail for the episode's temporary file in that directory, the same way the existing receipt-failure test does. **Tests added to `tests/fm-remote-reply.test.sh`:** - Store failure: the second break exits 1, appends no `blocked` line and opens no decision. One retry after storage recovers exits 3 and appends a single line. - Append failure: with the status log read-only, the third break exits 1 and appends nothing. One retry after the log is writable exits 3 and appends a single line. **Verification:** `bash tests/fm-remote-reply.test.sh` ends with "ALL TESTS PASSED" with the fix. Against the script at commit b750c828 the same test file fails at "a continuity break appended its line before its episode was stored". `shellcheck -S warning` reports only an unused loop variable at line 667 of the test file, which this change does not touch. I ran no other test files. The Greptile Review check log could not be retrieved, so I worked from the finding text alone

* fix: record a continuity break's reader position on its status line

A later break at another cursor is then a different line, so the existing
duplicate check appends it and reopens the decision. An unchanged re-read
builds the same line and appends nothing.

* fix: reopen a continuity break after an identical restore

A retirement that puts the same bytes back used to rebuild the recorded line, so the later break stayed closed. The retirement count on that line makes the later break distinct.

* no-mistakes(review): remove continuity match for full-prefix line without retirement count

* no-mistakes(document): clarify what a continuity break status line records

* fix: remove the reply cursor before recording retirement

A stop between those steps must leave the count unchanged, so an unchanged continuity break still builds the same line.

* fix(control): drop busy_gen from the task record when an incarnation is retired (kunchenguid#6733)

* fix(control): drop busy_gen when an incarnation is retired

A deliberate exit removed the busy sidecar and left busy_gen in the task record, so the two records disagreed about whether that incarnation was still observable.

* no-mistakes(review): drop GNU-only chmod and unreached sidecar-absent branch

* no-mistakes(review): correct lock comment to name the deadlock

* no-mistakes(ci): The test `test_exit_drops_meta_busy_gen_with_the_sidecar` in tests/fm-control.test.sh now compares the whole task record (the `state/<id>.meta` file), so the Greptile finding is fixed. Invariant: after `exit` retires an incarnation, the task record must equal the record from before `exit` with only the `busy_gen` line removed. This test is the only place in the change that asserts the record survives the rewrite, so it is the only site to fix. The other `busy_gen` tests assert that the line stays, and they do not go through the rewrite. What changed: before `exit`, the test writes the record without its `busy_gen` line to `expected.meta`. After `exit`, the test runs `diff` between that expected copy and the real record, and fails with the diff output if they differ. This one comparison replaces the two earlier checks (no `busy_gen` line left, and the `window` line present), because it covers both. I did not change bin/fm-control.sh or any other file. How I know it works: - I ran `bash tests/fm-control.test.sh`: exit code 0, 45 lines starting with `ok`, no other lines. - I temporarily changed the rewrite in bin/fm-control.sh to also drop the `harness` line. The test then failed with `not ok - exit should drop only busy_gen from the task record:` and the diff `< harness=codex`. The earlier `window`-only check would have passed that rewrite. I restored bin/fm-control.sh afterwards; `git status` shows only tests/fm-control.test.sh modified. - `bash -n` and `shellcheck` on the test file report no new warnings from the edit. The change is not committed; the working tree holds it

* test: cover PID collisions in harness ancestry detection (kunchenguid#6484)

* test(secondmate-harness): scope fake ps -codex label for pid 5252 to the liveness probe

Closes kunchenguid#6456

* no-mistakes(ci): Updated the collision test to log and assert that PID 5252 was queried before selecting 4242. Full fm-secondmate harness suite passes

---------

Co-authored-by: YifuGu <[email protected]>

---------

Co-authored-by: Tiago <[email protected]>
Co-authored-by: M00NLIG7 <[email protected]>
Co-authored-by: Symphony <[email protected]>
Co-authored-by: Freudator86 <[email protected]>
Co-authored-by: Quartermaster <[email protected]>
Co-authored-by: YifuGu <[email protected]>
Co-authored-by: YifuGu <[email protected]>
VasanthAmana pushed a commit to VasanthAmana/firstmate that referenced this pull request Oct 7, 2026
…is retired (kunchenguid#6733)

* fix(control): drop busy_gen when an incarnation is retired

A deliberate exit removed the busy sidecar and left busy_gen in the task record, so the two records disagreed about whether that incarnation was still observable.

* no-mistakes(review): drop GNU-only chmod and unreached sidecar-absent branch

* no-mistakes(review): correct lock comment to name the deadlock

* no-mistakes(ci): The test `test_exit_drops_meta_busy_gen_with_the_sidecar` in tests/fm-control.test.sh now compares the whole task record (the `state/<id>.meta` file), so the Greptile finding is fixed. Invariant: after `exit` retires an incarnation, the task record must equal the record from before `exit` with only the `busy_gen` line removed. This test is the only place in the change that asserts the record survives the rewrite, so it is the only site to fix. The other `busy_gen` tests assert that the line stays, and they do not go through the rewrite. What changed: before `exit`, the test writes the record without its `busy_gen` line to `expected.meta`. After `exit`, the test runs `diff` between that expected copy and the real record, and fails with the diff output if they differ. This one comparison replaces the two earlier checks (no `busy_gen` line left, and the `window` line present), because it covers both. I did not change bin/fm-control.sh or any other file. How I know it works: - I ran `bash tests/fm-control.test.sh`: exit code 0, 45 lines starting with `ok`, no other lines. - I temporarily changed the rewrite in bin/fm-control.sh to also drop the `harness` line. The test then failed with `not ok - exit should drop only busy_gen from the task record:` and the diff `< harness=codex`. The earlier `window`-only check would have passed that rewrite. I restored bin/fm-control.sh afterwards; `git status` shows only tests/fm-control.test.sh modified. - `bash -n` and `shellcheck` on the test file report no new warnings from the edit. The change is not committed; the working tree holds it
yelenplays added a commit to yelenplays/firstmate that referenced this pull request Oct 8, 2026