Repository navigation
fix(wake): surface every unseen status event in a coalesced drain - #9
Merged
Merged
Conversation
A watcher wake can coalesce several appends to the same state/<id>.status. The drain annotated only that file's newest line, so an earlier event in the same window - including a needs-decision escalation - was invisible to the supervisor reading the drain output. fm-wake-lib.sh now keeps a home-private per-status-file read cursor (state/.drain-cursor-<id>) recording the byte size at the last drain that emitted that file's annotation. The next drain prints every event appended since that cursor, oldest first and clearly labeled as the unseen backlog, ahead of the unchanged latest-line annotation. The cursor is advisory only. An absent, corrupt, or past-EOF cursor degrades to the existing latest-line-only annotation and re-initializes at EOF, so a first run never dumps existing history and a damaged cursor never crashes or floods the drain. The backlog reuses the existing bounded read: one perl read per file, starting at the cursor when it lies inside the tail window, with explicit markers when older events fell outside that window or past the 20-line per-file cap. A file's block is emitted whole or not at all, and its cursor advances only once the block is emitted, so a capped-out backlog is repeated rather than silently dropped. Teardown removes a task's cursor with its other volatile state.
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.
Problem
A watcher wake can coalesce several appends to the same
state/<id>.status.The drain annotated only that file's newest line, so an earlier event in the same window was invisible to the supervisor reading the drain output.
Reproduced 2026-07-27: a secondmate appended a
needs-decision:escalation immediately followed by an unrelatedresolved:line.The supervising session's next drain annotated only the
resolved:line, and the decision sat unnoticed for hours until the captain asked directly.The annotation is correctly documented as "latest wake-EVENT observed at drain, not current state" - the defect is that nothing in the drain output surfaced the other unseen lines from the same window.
Fix
bin/fm-wake-lib.shkeeps a home-private per-status-file read cursor atstate/.drain-cursor-<id>, recording the byte size of the status file at the last drain that actually emitted that file's annotation.The next drain prints every event appended since that cursor, oldest first and clearly labeled as the unseen backlog, ahead of the unchanged latest-line annotation.
Added, not reshaped: the existing
wake annotation: latest wake-EVENT observed at drain, not current state: <key>: <line>record, its historical variant, its truncation marker, and both cap markers are byte-identical, so single-append behavior and every existing consumer are unchanged.The new lines use distinct prefixes:
Safety properties:
perlread per status file, now starting at the cursor when it lies inside the tail window and at the window otherwise. Status-file volume still cannot turn a drain into an unbounded context read.bin/fm-teardown.shremoves a task's cursor with its other volatile state, on both the ordinary and secondmate-child paths.Second layer - deliberately skipped, filed as its own follow-up
The optional watcher-side layer (a keyed
needs-decision:/blocked:with no matchingresolved:re-surfacing as a fresh wake after a bounded interval) is not in this PR.It needs its own durable per-key open/resolved tracking, a re-surface interval, and de-duplication against the existing stale/heartbeat paths in
bin/fm-watch.sh- a separate state machine, disproportionate to this diff, and independently testable.The cursor fix alone satisfies the acceptance criteria: a
needs-decisionfollowed by aresolvedin the same window is now impossible to miss from drain output alone.The captain has confirmed this backstop is being filed as its own follow-up work item.
Docs
Per the one-owner rule, the annotation contract is owned by the code (
fm_wake_print_annotationsand thefm-wake-drain.shheader);grepfound no prose restatement of it.Updated the drain script header and added the cursor to
AGENTS.md's state-layout listing.docs/scripts.md's one-line summary ("bounded best-effort status-event annotations") remains accurate and was left alone rather than restated.Harness compatibility
Drain output is read by all supported primary harnesses.
Reviewed every protocol in
docs/supervision-protocols/(claude, codex, grok, opencode, pi, unknown): each instructs "drain first withbin/fm-wake-drain.sh" and reads the output as text.None parses the annotation lines, and no existing annotation line changed, so the added lines are backward-compatible across all of them.
Validation
This branch was validated through the full no-mistakes pipeline (intent, rebase, review, test, document, lint, push).
Five pipeline commits sit on top of the original change; the PR head is
52697fc.The review rounds found three real durability defects in the original cursor implementation, all now fixed:
A later round replaced bounded partial progress with atomic whole-block deferral, which reintroduced the possibility of an oversized block deferring forever.
The final commit resolves that by construction: each block is sized against the actual remaining emission budget, so an oversized backlog now drains across successive drains in fitting chunks with an explicit remainder marker, and starvation is structurally impossible rather than merely unlikely.
Local verification
GitHub Actions is billing-disabled fleet-wide, so this PR shows no CI checks and local verification is the gate.
The pipeline's own lint step could not run ShellCheck in its environment (exit 127), so the lint result below was produced locally against the exact PR head with the pinned version.
Verified at
52697fc:18/18 pass.
The last three are new coverage added by the review rounds for the durability defects above; the twelve pre-existing cases pass unchanged, which is the backward-compatibility evidence.
Neighbouring suites and the documentation audience check were verified clean on the original commit and are unaffected by the pipeline fixes, which touch only
bin/fm-wake-lib.sh,bin/fm-wake-drain.sh,bin/fm-teardown.sh,AGENTS.md, andtests/fm-wake-queue.test.sh:Two failures were observed and confirmed pre-existing on
origin/mainby re-running each suite with this branch's changes stashed:tests/fm-session-start.test.sh:not ok - concurrent session-lock acquisition produced 40 winners(matches open issue fm-lock.sh prints "lock acquired" and exits 0 when the lock file was never written kunchenguid/firstmate#1131).tests/fm-pi-watch-extension.test.sh:not ok - OpenCode watch plugin must arm only when this session owns the fleet lock: expected exit 0, got 1.Neither touches the drain or annotation path.