Repository navigation
feat(bin): record timestamped task transitions in state/events.ndjson - #20
Merged
Merged
Conversation
… 8" was tests/fm-watch-arm.test.sh: "watcher pid … outlived its deleted state directory". Cause (introduced by this PR): the watcher now runs `fm-events.sh capture` on every cycle (bin/fm-watch.sh:2624), which waits for state/.events.lock using fm_lock_acquire_wait. That wait retries forever. If the state directory is deleted after the watcher checks it exists but before the lock is taken, the lock can never be created, so the capture loops forever and the watcher never reaches its state-gone exit. I reproduced this: a wait on a lock inside a missing directory was still running after 3 seconds. What must always hold: waiting for a lock must stop once the directory holding the lock is gone. The fix is in the shared wait, so both fm-events.sh modes (the watcher's capture and the worker's appended) and all 124 other callers get it. bin/fm-wake-lib.sh: after each failed attempt, fm_lock_acquire_wait returns 1 if the lock's directory no longer exists. The normal wait while another process holds the lock is unchanged, and the bounded variant, which uses this function, benefits too. Added a regression test in tests/fm-wake-queue.test.sh: a lock wait in a removed directory must return 1 within 5 seconds and must not recreate the directory. It fails without the fix and passes with it. These suites pass locally: fm-watch-arm (including the previously failing test), fm-wake-queue, fm-events, fm-fleet-ledger and fm-send-inbox. Shellcheck reported nothing in the changed lines. It still reports one warning (SC2034 in fm-wake-lib.sh, line 2147) and some info notes in lines this fix did not touch. Changes are not committed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix the missing timestamps at the source: add a small firstmate change that writes a timestamped state/events.ndjson ledger, so task durations, time waiting on the captain and PR cycle times can be measured after the fact (platform design decision D8, option A). Background: data/fleet-telemetry-design/report.md sections 5.2-G and 11 found that state/.status lines carry no timestamps.
What Changed
bin/fm-events.sh, which is always on and writes the append-onlystate/events.ndjsonledger. Each complete status line becomes atask.statusrecord withtstaken from its[at=<epoch>]tag (time_source=status). When a line has no usable tag,tsfalls back to the capture time (time_source=capture). The script also writestask.pr_readyandtask.mergedrecords for PR cycle timing. Record ids are derived from content, and a per-task byte cursor tracks progress; both are kept understate/.events.lock, so retries and watcher restarts don't create duplicate records.fm-brief.shrecords each line right after appending it.fm-watch.shruns acapturepass each cycle to pick up lines written through other paths.fm-pr-check.shrecordspr_ready, andfm-merge-outcome-lib.shrecordsmerged.fm-teardown.shcaptures the final lines before retiring a task, andstatus_retire_presentation_taskremoves the task's events cursor together with its status log under the events lock.docs/events.mdand updatesdocs/architecture.md,docs/scripts.md,docs/fleet-ledger.mdanddocs/documentation-audiences.json. Addstests/fm-events.test.shand extendstests/fm-fleet-ledger.test.sh.🤖 Generated with Claude Code
Risk Assessment
Testing
I ran the change's two test files and then drove the product scripts in a disposable
fm-labhome, which I deleted afterwards. The worker status command came from a realfm-brief.shscaffold. I also ran the real watcher checkpoint,fm-spawn,fm-pr-checkwith the real gh, the shared merge-outcome report, andfm-teardown, plus adversarial cases: reused task id, unwritable ledger, symlinked ledger, and malformed calls. Every scenario passed. The ledger records source timestamps from which task duration (10s), captain wait (4s), and PR cycle (10s) were computed in this run, with no duplicate records and no regression in the status append. The change has no UI, so the evidence is CLI transcripts plus the resulting ledger file. Spawning the PR/teardown task used a stand-in tmux and treehouse, because real treehouse writes worktrees outside the sandbox; everything on the ledger path ran for real. No real Claude primary or worker was launched, because the transitions only need the worker's generated status command, which I ran directly.Evidence: Worker status command writes timestamped events; derived task duration and captain wait
Source: Worker status command writes timestamped events; derived task duration and captain wait
Evidence: Watcher recovers directly-appended and legacy lines; partial line waits for its newline; no duplicates on replay
Source: Watcher recovers directly-appended and legacy lines; partial line waits for its newline; no duplicates on replay
Evidence: PR ready and merged recorded once each; PR cycle time derived
Source: PR ready and merged recorded once each; PR cycle time derived
Evidence: Teardown records the final status line and removes the events cursor
Source: Teardown records the final status line and removes the events cursor
Evidence: Adversarial cases: task-id reuse, unwritable ledger, symlinked ledger, malformed calls
Source: Adversarial cases: task-id reuse, unwritable ledger, symlinked ledger, malformed calls
Evidence: Final lab events.ndjson ledger (19 records)
Source: Final lab events.ndjson ledger (19 records)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-teardown.sh:3733- Teardown never deletes the per-task cursorstate/.<task>.events-cursor. Teardown runsfm-events.sh appendedas its final capture, then removes other per-task markers (fm-teardown.sh:3738-3746). Comparefm-fleet-ledger.sh cleaned_up, which removes its offset at fm-fleet-ledger.sh:230. The stale cursor causes more than clutter.capture_task(bin/fm-events.sh:101-106) keeps the saved offset whenever the saved identity matches the new file and size >= offset. On Linux filesystems wherestat %Wreturns 0,_fm_open_decisions_file_identfalls back todev:inodeonly. Failure sequence: taskfoois torn down, then a new task reuses idfoo, and its new status file gets the recycled inode. Once that file grows past the old offset, its first lines are silently skipped and never reach events.ndjson, so earlyworking/needs-decisiontimestamps are lost with no error. The same missing cleanup applies to the remote second-mate teardown path (fm-teardown.sh:1046). Fix: after the finalappendedcapture succeeds, remove$STATE/.$ID.events-cursor, either by adding it to the rm list at fm-teardown.sh:3738 and in remote_secondmate_teardown, or with acleaned_up-style mode in fm-events.sh that captures and then deletes the cursor under the lock.bin/fm-events.sh:59-record_lineandrecord_prrungrep -Fover the whole ledger for every new line to deduplicate. The ledger is documented as never rotated, and the worker's status command waits on.events.lockwithout a timeout while the watcher captures. So each status append slowly gets more expensive as the ledger grows. The cost is acceptable for now; a note in case the ledger reaches multi-MB sizes.🔧 Fix applied.
2 infos still open:
bin/fm-events.sh:59-record_lineandrecord_prrungrep -Fover the whole ledger for every new line to deduplicate. The ledger is documented as never rotated, and the worker's status command waits on.events.lockwithout a timeout while the watcher captures. So each status append slowly gets more expensive as the ledger grows. The cost is acceptable for now; a note in case the ledger reaches multi-MB sizes.bin/fm-classify-lib.sh:1720- This is a gap left in the round-1 fix, which added$state/.$task.events-cursorto the rm list instatus_retire_presentation_task. That fix is correct at the shared boundary and covers all three teardown call sites (fm-teardown.sh:1048, 3279, 3736). The removal runs under.status-presentation-lock, but fm-events.sh writes the cursor under.events.lock, so the two are not serialized. Race: teardown finishes itsappendedcapture and releases.events.lock. The watcher'scapture(fm-watch.sh:2624) then takes the lock and enterscapture_taskfor the task, since the status file still exists. Retire then deletes the status file and the cursor. The watcher'smv -f "$tmp" "$cursor"(fm-events.sh:128) then writes a stale cursor back. A second gap: the early-return no-op check (fm-classify-lib.sh:1666-1673) does not test for the events cursor, so a later retire never removes a cursor left behind like this. The effect is the one round 1 targeted: a reused task id whose new status file gets a recycled inode, on a filesystem without birth time, silently skips its first bytes. The window is milliseconds and needs inode reuse, so this is low priority. Fix: remove the events cursor while holding$state/.events.lock(take it briefly in retire just for that rm), or havecapture_taskre-check that the status file still has the same identity before replacing the cursor. Also add the events cursor to the early-return existence checks.🔧 Fix applied.
1 info still open:
bin/fm-events.sh:59-record_lineandrecord_prrungrep -Fover the whole ledger for every new line to deduplicate. The ledger is documented as never rotated, and the worker's status command waits on.events.lockwithout a timeout while the watcher captures. So each status append slowly gets more expensive as the ledger grows. The cost is acceptable for now; a note in case the ledger reaches multi-MB sizes.✅ **Test** - passed
✅ No issues found.
bash tests/fm-events.test.sh(the change's own test file)bash tests/fm-fleet-ledger.test.sh(the change's updated test file)bin/fm-lab-home.sh create $LAB: a disposable marked lab home, with a lab git project and a bare origin inside itbin/fm-brief.sh evt-demo sample --mode local-only, then ran the brief's generated status command four times in anenv -iworker shell (working, needs-decision, resolved, done)Real watcherbin/fm-watch-checkpoint.sh --seconds 4, run inside a lab tmux session (tmux -L fm-lab) after lines were appended directly to the status file (a stamped line, a legacy line, a partial line)bin/fm-events.sh capturerepeated after the partial line was completed, to check deduplicationbin/fm-spawn.sh evt-pr ... --mode direct-PR --yolo offin the lab; tmux and treehouse were stand-ins so nothing was written outside the labbin/fm-pr-check.sh evt-pr <url>run twice, plus once withFM_PR_CHECK_MERGE=1, using the real ghfm_merge_outcome_reportfrombin/fm-merge-outcome-lib.shrun twice (self, then poll)bin/fm-teardown.sh evt-prafter a final status line was appended directlyReused the task id after teardown and ranbin/fm-events.sh captureMadeevents.ndjsona directory, then ran the worker status commandReplacedevents.ndjsonwith a symlink to a decoy file, then ran captureMalformedfm-events.shcalls: a relativeappendedpath, a bad task id, an empty PR URLjqchecks over the final ledger: unique ids,v/ts/task/time_sourceon every record, and the derived duration, captain wait, and PR cycle✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → no changes applied (2) ✅
🔧 No changes applied.
1 warning still open:
🔧 No changes applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.