Skip to content

feat(bin): record timestamped task transitions in state/events.ndjson - #20

Merged
Hozzy02 merged 5 commits into
mainfrom
fm/firstmate-event-ledger
Sep 27, 2026
Merged

Hozzy02 merged 5 commits into
mainfrom
fm/firstmate-event-ledger

Conversation

@Hozzy02

@Hozzy02 Hozzy02 commented Sep 27, 2026 •

Copy link
Copy Markdown
Owner

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

  • Adds bin/fm-events.sh, which is always on and writes the append-only state/events.ndjson ledger. Each complete status line becomes a task.status record with ts taken from its [at=<epoch>] tag (time_source=status). When a line has no usable tag, ts falls back to the capture time (time_source=capture). The script also writes task.pr_ready and task.merged records for PR cycle timing. Record ids are derived from content, and a per-task byte cursor tracks progress; both are kept under state/.events.lock, so retries and watcher restarts don't create duplicate records.
  • Connects the writer to the existing flow:
    • The worker status command in fm-brief.sh records each line right after appending it.
    • fm-watch.sh runs a capture pass each cycle to pick up lines written through other paths.
    • fm-pr-check.sh records pr_ready, and fm-merge-outcome-lib.sh records merged.
    • fm-teardown.sh captures the final lines before retiring a task, and status_retire_presentation_task removes the task's events cursor together with its status log under the events lock.
  • Documents the record contract in the new docs/events.md and updates docs/architecture.md, docs/scripts.md, docs/fleet-ledger.md and docs/documentation-audiences.json. Adds tests/fm-events.test.sh and extends tests/fm-fleet-ledger.test.sh.

🤖 Generated with Claude Code

Risk Assessment

⚠️ Medium: The ledger is additive, fails soft, and meets the intent, but the per-task cursor it leaves at teardown can silently drop early events when a task id is reused on hosts without file birth times.

Testing

I ran the change's two test files and then drove the product scripts in a disposable fm-lab home, which I deleted afterwards. The worker status command came from a real fm-brief.sh scaffold. I also ran the real watcher checkpoint, fm-spawn, fm-pr-check with the real gh, the shared merge-outcome report, and fm-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.

  • Live validation: ✅ go - 8 of 8 scenarios driven live against the product
Scenario Result Live Evidence
A worker runs the status command from its generated brief: each transition goes into events.ndjson straight away with its source [at=] timestamp, and task duration and captain wait can be computed fro… ✅ pass live s1-worker-status-command.txt: ledger grew 1→4 with each append; time_source=status; jq gave task_duration_s=10 and captain_wait_s=4; the .status file format is unchanged
The watcher checkpoint recovers status lines written without the worker command (both stamped and legacy lines), holds back a partial line until its newline arrives, and repeated captures add no dupli… ✅ pass live s2-watcher-capture.txt: the real fm-watch-checkpoint.sh took the ledger from 4 to 6 records (blocked has time_source=status, the legacy line has time_source=capture); no partial record before the newl…
Registering a PR with fm-pr-check records one task.pr_ready with the full URL; a repeat run and the merge-time re-record add nothing ✅ pass live s3-s4-pr-cycle.txt: fm-pr-check rc=0 on all three runs, and there is exactly one task.pr_ready record (the task was spawned with a stand-in tmux and treehouse)
Reporting a merge outcome twice records one task.merged, and PR cycle time can be computed from pr_ready to merged ✅ pass live s3-s4-pr-cycle.txt: {"pr_cycle_s":10,"pr_ready_count":1,"merged_count":1}
Teardown records a final status line that was never projected before retiring the status log, and removes the task's events cursor ✅ pass live s5-teardown.txt: fm-teardown rc=0; the task's records went 7→8 including the final merged line; the status log and .evt-pr.events-cursor are both gone afterwards
Adversarial: a task id reused after teardown is captured from its first line (no stale cursor skips it) ✅ pass live s6-s9-adversarial.txt Scenario 6: the reused id's first line was recorded
Adversarial: when the ledger cannot be written, the worker's status append still succeeds, and the line is recovered once the ledger is back ✅ pass live s6-s9-adversarial.txt Scenario 7: status command rc=0 and the line landed; a later capture recovered it with time_source=status
Adversarial: the writer refuses to write through a symlinked ledger and rejects malformed invocations ✅ pass live s6-s9-adversarial.txt Scenarios 8-9: capture rc=1 and the decoy file was untouched; bad calls returned rc=2 with the ledger unchanged; final ledger 19 records, 19 unique ids, all fields valid
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

## Scenario 1: worker status command (from generated brief) records timestamped events immediately
rc=0 after 'working'; ledger lines:        1
rc=0 after 'needs-decision'; ledger lines:        2
rc=0 after 'resolved'; ledger lines:        3
rc=0 after 'done'; ledger lines:        4
--- state/evt-demo.status (unchanged format):
working [at=1790508618]: implementation started
needs-decision [at=1790508622]: [key=api] choose REST or gRPC
resolved [at=1790508626]: [key=api] captain chose REST
done [at=1790508628]: ready in branch
--- state/events.ndjson:
{"v":1,"id":"0033d66f4ae2e0ca930d80f27e3c205a91a3f6e22ef0a6e2e6dec764627a324b","event":"task.status","task":"evt-demo","ts":1790508618,"time_source":"status","state":"working","key":null,"line":"working [at=1790508618]: implementation started"}
{"v":1,"id":"c0056969d9e4ce04ef0ec8eb23fc24e3aeca10424678ac237cd7c1023004c12e","event":"task.status","task":"evt-demo","ts":1790508622,"time_source":"status","state":"needs-decision","key":"api","line":"needs-decision [at=1790508622]: [key=api] choose REST or gRPC"}
{"v":1,"id":"e2118b4f4d10e598404cb08dd85b966bcc4e8777fb0f9ee06774abfc49e8f7a3","event":"task.status","task":"evt-demo","ts":1790508626,"time_source":"status","state":"resolved","key":"api","line":"resolved [at=1790508626]: [key=api] captain chose REST"}
{"v":1,"id":"0eaab76ba7cf1c39493fca54de4c6e3cd9ab9e780c526133c69bf53e6ecc9766","event":"task.status","task":"evt-demo","ts":1790508628,"time_source":"status","state":"done","key":null,"line":"done [at=1790508628]: ready in branch"}
--- derived metrics (read-only jq consumer):
{"task_duration_s":10,"captain_wait_s":4,"all_status_sourced":true}
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

## Scenario 2: watcher recovers status lines written outside the worker command; partial line waits
ledger lines before watcher:        4
--- watcher checkpoint output:
signal: /var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T//fm-lab.wRfRYg/state/evt-demo.status
ledger lines after watcher:        6
{"state":"blocked","ts":1790508641,"time_source":"status","line":"blocked [at=1790508641]: written by another path (no fm-events call)"}
{"state":null,"ts":1790508641,"time_source":"capture","line":"legacy line with no at tag"}
partial-line records before newline: 0
after newline + two repeated captures:        7 lines; unique ids: 7
{"state":"paused","ts":1790508641,"time_source":"status","line":"paused [at=1790508641]: partial, no newline yet - now complete"}
Evidence: PR ready and merged recorded once each; PR cycle time derived

Source: PR ready and merged recorded once each; PR cycle time derived

## Scenario 3: PR registration (real fm-pr-check; real gh read of an unreadable PR) records task.pr_ready once; merge-time re-record adds nothing
fm-pr-check (first) rc=0: armed: state/evt-pr.check.sh 
fm-pr-check (repeat) rc=0: armed: state/evt-pr.check.sh 
fm-pr-check (merge-time) rc=0: armed: state/evt-pr.check.sh 
{"event":"task.status","task":"evt-pr","ts":1790508736,"time_source":"status","state":"done","pr":null}
{"event":"task.pr_ready","task":"evt-pr","ts":1790508737,"time_source":"recorded","state":null,"pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
## Scenario 4: merge outcome (fm_merge_outcome_report, twice) records task.merged once; PR cycle time derivable
report1 rc=0
report2 rc=0
{"event":"task.pr_ready","task":"evt-pr","ts":1790508737,"time_source":"recorded","pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"event":"task.merged","task":"evt-pr","ts":1790508747,"time_source":"recorded","pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"pr_cycle_s":10,"pr_ready_count":1,"merged_count":1}
Evidence: Teardown records the final status line and removes the events cursor

Source: Teardown records the final status line and removes the events cursor

## Scenario 5: teardown captures the final status line before retiring the log, and removes the task's events cursor
cursor before teardown: .evt-pr.events-cursor
ledger records for evt-pr before teardown: 7
fm-teardown rc=0
WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.
/var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T/fm-lab.wRfRYg/projects/sample: already current
teardown evt-pr complete (window firstmate:fm-evt-pr, worktree /var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T//fm-lab.wRfRYg/wt-evt-pr)
Backlog: evt-pr just finished (this home keeps no markdown backlog at /private/var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T/fm-lab.wRfRYg/data/backlog.md). Update /private/var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T/fm-lab.wRfRYg/data/backlog.md - move evt-pr to Done, keep Done to the 10 most recent, then re-scan Queued and dispatch only work whose blockers are gone and date is due.
ledger records for evt-pr after teardown: 8
{"event":"task.merged","state":null,"ts":1790508747,"time_source":"recorded","line":null,"pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"event":"task.status","state":"merged","ts":1790508756,"time_source":"status","line":"merged [at=1790508756]: final line appended directly, never projected","pr":null}
status log present after teardown: no; events cursor present: no
Evidence: Adversarial cases: task-id reuse, unwritable ledger, symlinked ledger, malformed calls

Source: Adversarial cases: task-id reuse, unwritable ledger, symlinked ledger, malformed calls

## Scenario 6 (adversarial): reused task id after teardown starts capture from its first line
capture rc=0
{"task":"evt-pr","state":"working","ts":1790508774,"time_source":"status","line":"working [at=1790508774]: reused id first line"}

## Scenario 7 (adversarial): ledger unwritable (events.ndjson is a directory) - worker status command still appends and exits 0
worker status command rc=0
status log: blocked [at=1790508775]: need a token
after ledger restored, watcher-style capture rc=0 recovers: {"state":"blocked","ts":1790508775,"time_source":"status"}

## Scenario 8 (adversarial): events.ndjson replaced by a symlink - writer refuses to follow it
capture rc=1 (nonzero expected)
decoy contents: untouched

## Scenario 9 (adversarial): malformed invocations are rejected without writing
appended relative path rc=2
pr_ready bad task id rc=2
merged empty URL rc=2
ledger lines unchanged:       19 ->       19

## Final ledger integrity
{"records":19,"unique_ids":19,"all_valid":true}
Evidence: Final lab events.ndjson ledger (19 records)

Source: Final lab events.ndjson ledger (19 records)

{"v":1,"id":"0033d66f4ae2e0ca930d80f27e3c205a91a3f6e22ef0a6e2e6dec764627a324b","event":"task.status","task":"evt-demo","ts":1790508618,"time_source":"status","state":"working","key":null,"line":"working [at=1790508618]: implementation started"}
{"v":1,"id":"c0056969d9e4ce04ef0ec8eb23fc24e3aeca10424678ac237cd7c1023004c12e","event":"task.status","task":"evt-demo","ts":1790508622,"time_source":"status","state":"needs-decision","key":"api","line":"needs-decision [at=1790508622]: [key=api] choose REST or gRPC"}
{"v":1,"id":"e2118b4f4d10e598404cb08dd85b966bcc4e8777fb0f9ee06774abfc49e8f7a3","event":"task.status","task":"evt-demo","ts":1790508626,"time_source":"status","state":"resolved","key":"api","line":"resolved [at=1790508626]: [key=api] captain chose REST"}
{"v":1,"id":"0eaab76ba7cf1c39493fca54de4c6e3cd9ab9e780c526133c69bf53e6ecc9766","event":"task.status","task":"evt-demo","ts":1790508628,"time_source":"status","state":"done","key":null,"line":"done [at=1790508628]: ready in branch"}
{"v":1,"id":"30257402bac24d9d0c5a240d0bbe1dc442557516c76ab4226e46aa7ff7f87a0c","event":"task.status","task":"evt-demo","ts":1790508641,"time_source":"status","state":"blocked","key":null,"line":"blocked [at=1790508641]: written by another path (no fm-events call)"}
{"v":1,"id":"91b3ab488ba9e6db9712697503c165412dd3acdd4cc9aace7a88b2ff9a4a2c94","event":"task.status","task":"evt-demo","ts":1790508641,"time_source":"capture","state":null,"key":null,"line":"legacy line with no at tag"}
{"v":1,"id":"59776c02c17781b85f38382c290c9aa669ba1d75d6cc7c24c490f6cc791a5ea1","event":"task.status","task":"evt-demo","ts":1790508641,"time_source":"status","state":"paused","key":null,"line":"paused [at=1790508641]: partial, no newline yet - now complete"}
{"v":1,"id":"72879efc2b4a7d9d6e53911d13d90f4bc345d05528284893a759f423e7f6865a","event":"task.status","task":"evt-demo","ts":1790508657,"time_source":"status","state":"done","key":null,"line":"done [at=1790508657]: PR https://github.com/Hozzy02/firstmate/pull/19"}
{"v":1,"id":"576efe44747aabcdadc0e4c91bafa7e457479f66beedc5e8b175031900cefbb1","event":"task.merged","task":"evt-demo","ts":1790508661,"time_source":"recorded","pr":"https://github.com/Hozzy02/firstmate/pull/19"}
{"v":1,"id":"db1e58264780d299033889f5cbe571b30af5c60e7f66a5b53790bd1237d3a5ac","event":"task.status","task":"evt-pr","ts":1790508690,"time_source":"status","state":"done","key":null,"line":"done [at=1790508690]: PR https://github.com/Hozzy02/firstmate/pull/19"}
{"v":1,"id":"496f1691eedde4d69fc8d95c6754be9c4069f173b3c5a999eba0bef8bde54450","event":"task.merged","task":"evt-pr","ts":1790508697,"time_source":"recorded","pr":"https://github.com/Hozzy02/firstmate/pull/19"}
{"v":1,"id":"e194fe356bff15472c1faa424b86ce95767f31a106b33515a1643f2528434c47","event":"task.status","task":"evt-pr","ts":1790508710,"time_source":"status","state":"done","key":null,"line":"done [at=1790508710]: PR https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/7"}
{"v":1,"id":"322400e1ddd36440cf32f023228f6dd90b2fa0367ecdfea87c38497db94d4047","event":"task.merged","task":"evt-pr","ts":1790508718,"time_source":"recorded","pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/7"}
{"v":1,"id":"7c6eddea09a6451c02aa0d3fb9183697fad74ac724d4b2d4be0e94293378b934","event":"task.status","task":"evt-pr","ts":1790508736,"time_source":"status","state":"done","key":null,"line":"done [at=1790508736]: PR https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"v":1,"id":"76c0f4befe2390cd6955b16806ec46de6b5b31cd3908a0a1b6599f2eb7c55ad5","event":"task.pr_ready","task":"evt-pr","ts":1790508737,"time_source":"recorded","pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"v":1,"id":"f5830f5b4134fc1577a8684ca4c8c2c6e60cfadd53eb571352959acfeca65928","event":"task.merged","task":"evt-pr","ts":1790508747,"time_source":"recorded","pr":"https://github.com/Hozzy02/fm-lab-nonexistent-repo/pull/8"}
{"v":1,"id":"37421c0ab5651697c341e338b38513941c6d33f2d27c61f23d64d906e0ecfb2c","event":"task.status","task":"evt-pr","ts":1790508756,"time_source":"status","state":"merged","key":null,"line":"merged [at=1790508756]: final line appended directly, never projected"}
{"v":1,"id":"d74b3703d29c577fed3b11d7968e595e2bc75182c6fb42f4bc0e7be4807e927c","event":"task.status","task":"evt-pr","ts":1790508774,"time_source":"status","state":"working","key":null,"line":"working [at=1790508774]: reused id first line"}
{"v":1,"id":"754a151d8057692f850bed0a0fe43d2f38cee95753640c5f1cf5d493c642bb5c","event":"task.status","task":"evt-fail","ts":1790508775,"time_source":"status","state":"blocked","key":null,"line":"blocked [at=1790508775]: need a token"}

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ⚠️ bin/fm-teardown.sh:3733 - Teardown never deletes the per-task cursor state/.&lt;task&gt;.events-cursor. Teardown runs fm-events.sh appended as its final capture, then removes other per-task markers (fm-teardown.sh:3738-3746). Compare fm-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 where stat %W returns 0, _fm_open_decisions_file_ident falls back to dev:inode only. Failure sequence: task foo is torn down, then a new task reuses id foo, 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 early working/needs-decision timestamps are lost with no error. The same missing cleanup applies to the remote second-mate teardown path (fm-teardown.sh:1046). Fix: after the final appended capture 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 a cleaned_up-style mode in fm-events.sh that captures and then deletes the cursor under the lock.
  • ℹ️ bin/fm-events.sh:59 - record_line and record_pr run grep -F over 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.lock without 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_line and record_pr run grep -F over 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.lock without 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-cursor to the rm list in status_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 its appended capture and releases .events.lock. The watcher's capture (fm-watch.sh:2624) then takes the lock and enters capture_task for the task, since the status file still exists. Retire then deletes the status file and the cursor. The watcher's mv -f &#34;$tmp&#34; &#34;$cursor&#34; (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 have capture_task re-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_line and record_pr run grep -F over 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.lock without 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.

  • Live validation: ✅ go - 8 of 8 scenarios driven live against the product
Scenario Result Live Evidence
A worker runs the status command from its generated brief: each transition goes into events.ndjson straight away with its source [at=] timestamp, and task duration and captain wait can be computed fro… ✅ pass live s1-worker-status-command.txt: ledger grew 1→4 with each append; time_source=status; jq gave task_duration_s=10 and captain_wait_s=4; the .status file format is unchanged
The watcher checkpoint recovers status lines written without the worker command (both stamped and legacy lines), holds back a partial line until its newline arrives, and repeated captures add no dupli… ✅ pass live s2-watcher-capture.txt: the real fm-watch-checkpoint.sh took the ledger from 4 to 6 records (blocked has time_source=status, the legacy line has time_source=capture); no partial record before the newl…
Registering a PR with fm-pr-check records one task.pr_ready with the full URL; a repeat run and the merge-time re-record add nothing ✅ pass live s3-s4-pr-cycle.txt: fm-pr-check rc=0 on all three runs, and there is exactly one task.pr_ready record (the task was spawned with a stand-in tmux and treehouse)
Reporting a merge outcome twice records one task.merged, and PR cycle time can be computed from pr_ready to merged ✅ pass live s3-s4-pr-cycle.txt: {"pr_cycle_s":10,"pr_ready_count":1,"merged_count":1}
Teardown records a final status line that was never projected before retiring the status log, and removes the task's events cursor ✅ pass live s5-teardown.txt: fm-teardown rc=0; the task's records went 7→8 including the final merged line; the status log and .evt-pr.events-cursor are both gone afterwards
Adversarial: a task id reused after teardown is captured from its first line (no stale cursor skips it) ✅ pass live s6-s9-adversarial.txt Scenario 6: the reused id's first line was recorded
Adversarial: when the ledger cannot be written, the worker's status append still succeeds, and the line is recovered once the ledger is back ✅ pass live s6-s9-adversarial.txt Scenario 7: status command rc=0 and the line landed; a later capture recovered it with time_source=status
Adversarial: the writer refuses to write through a symlinked ledger and rejects malformed invocations ✅ pass live s6-s9-adversarial.txt Scenarios 8-9: capture rc=1 and the decoy file was untouched; bad calls returned rc=2 with the ledger unchanged; final ledger 19 records, 19 unique ids, all fields valid
  • 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 it
  • bin/fm-brief.sh evt-demo sample --mode local-only, then ran the brief's generated status command four times in an env -i worker shell (working, needs-decision, resolved, done)
  • Real watcher bin/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 capture repeated after the partial line was completed, to check deduplication
  • bin/fm-spawn.sh evt-pr ... --mode direct-PR --yolo off in the lab; tmux and treehouse were stand-ins so nothing was written outside the lab
  • bin/fm-pr-check.sh evt-pr &lt;url&gt; run twice, plus once with FM_PR_CHECK_MERGE=1, using the real gh
  • fm_merge_outcome_report from bin/fm-merge-outcome-lib.sh run twice (self, then poll)
  • bin/fm-teardown.sh evt-pr after a final status line was appended directly
  • Reused the task id after teardown and ran bin/fm-events.sh capture
  • Made events.ndjson a directory, then ran the worker status command
  • Replaced events.ndjson with a symlink to a decoy file, then ran capture
  • Malformed fm-events.sh calls: a relative appended path, a bad task id, an empty PR URL
  • jq checks over the final ledger: unique ids, v/ts/task/time_source on every record, and the derived duration, captain wait, and PR cycle
✅ **Document** - passed

✅ No issues found.

🔧 **Lint** - 1 issue found → no changes applied (2) ✅
  • ⚠️ linter found issues (exit code 1)

🔧 No changes applied.
1 warning still open:

  • ⚠️ linter found issues (exit code 1)

🔧 No changes applied.
✅ Re-checked - no issues remain.

✅ **Push** - passed

✅ No issues found.

… 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
@Hozzy02
Hozzy02 merged commit 8eba773 into main Sep 27, 2026
19 checks passed
@Hozzy02
Hozzy02 deleted the fm/firstmate-event-ledger branch September 27, 2026 12:51
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