Skip to content

fix(wake): surface every unseen status event in a coalesced drain - #9

Merged
knowttl merged 6 commits into
mainfrom
fm/fm-fix-wake-drain-full-tail
Jul 28, 2026
Merged

knowttl merged 6 commits into
mainfrom
fm/fm-fix-wake-drain-full-tail

Conversation

@knowttl

@knowttl knowttl commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

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 unrelated resolved: 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.sh keeps a home-private per-status-file read cursor at state/.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:

wake annotation: earlier unseen wake-EVENT since the last drain, not current state: task.status: needs-decision: pick a serializer [key=d6-stage2]
wake annotation: latest wake-EVENT observed at drain, not current state: task.status: resolved: unrelated blocker cleared [key=other]

Safety properties:

  • The cursor is advisory only. Absent, corrupt, or past-EOF (file truncated or replaced) all degrade to the existing latest-line-only annotation and re-initialize 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 - still one perl read 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.
  • Explicit markers when older events fell outside the read window, or past the 20-line per-file cap.
  • A file's block is emitted whole or not at all under the unchanged 8192-byte global cap, and its cursor advances only once the block is emitted, so a capped-out backlog is repeated on the next drain rather than silently dropped. That matches the drain's existing at-least-once boundary.
  • Cursor writes stay outside the raw-commit boundary and cannot fail, duplicate, or hide a consumed row.
  • bin/fm-teardown.sh removes 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 matching resolved: 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-decision followed by a resolved in 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_annotations and the fm-wake-drain.sh header); grep found 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 with bin/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:

  1. The cursor advanced while annotations were still buffered, so an interruption before the write consumed the wake and advanced the cursor without emitting anything.
  2. A capped-out backlog was discarded with no durable retry, and a block could permanently exceed the global cap and never be emitted at all.
  3. A transient status-read failure cleared a pre-existing retry marker, discarding an obligation that had not yet been shown.

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:

$ bin/fm-lint.sh
fm-lint.sh: ShellCheck 0.11.0 (pinned 0.11.0)
LINT EXIT=0

$ bash tests/fm-wake-queue.test.sh
ok - concurrent append plus drain preserves queue records
ok - signal written while no watcher runs is caught on next run
ok - stale wake is queued before suppressor state is advanced
ok - a not-provably-working stale wake is queued before its suppressor is advanced
ok - registered custom check output is queued before cadence suppression
ok - two atomic drains cannot consume the same records twice
ok - drain collapses obvious duplicate heartbeat and signal records
ok - drain asserts watcher liveness: warns on a lapse, stays silent right after a fire
ok - structural signal enrichment is separate, deduped, home-local, and tier-zero for other wakes
ok - bounded reads and per-item/global caps fail open with explicit truncation and omission markers
ok - slow annotation releases the append lock and a deleted status file fails open
ok - interruptions restore before commitment and never replay after raw commitment
ok - a coalesced multi-append window surfaces every unseen wake-EVENT
ok - a first drain initializes the cursor at EOF instead of dumping history
ok - a corrupt or past-EOF cursor degrades to the latest-line annotation and re-initializes
ok - an oversized unseen backlog is chunked with an explicit remainder marker
ok - failed annotation output preserves the cursor and retries durably
ok - a 21 by 2048-byte unseen backlog fully drains across successive drains
SUITE EXIT=0

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, and tests/fm-wake-queue.test.sh:

$ bash tests/fm-watch-triage.test.sh            # no failures
$ bash tests/fm-watcher-lock.test.sh            # no failures
$ bash tests/fm-watch-checkpoint.test.sh        # no failures
$ bash tests/fm-afk-return.test.sh              # no failures
$ bash tests/fm-teardown.test.sh                # no failures
$ bash tests/fm-wake-daemon-lifecycle-e2e.test.sh  # no failures
$ bin/fm-doc-audience-check.sh
fm-doc-audience-check: ok surfaces=56 local_links=151

Two failures were observed and confirmed pre-existing on origin/main by re-running each suite with this branch's changes stashed:

Neither touches the drain or annotation path.

knowttl added 6 commits July 27, 2026 21:57
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.
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