Skip to content

fix(pi): retire dead and duplicate replacement-handoff wakes - #26

Merged
Boxyboxy merged 3 commits into
feat/omp-adaptorfrom
fm/fm-pi-watch-same-dead-wake-defect
Sep 27, 2026
Merged

Boxyboxy merged 3 commits into
feat/omp-adaptorfrom
fm/fm-pi-watch-same-dead-wake-defect

Conversation

@Boxyboxy

Copy link
Copy Markdown
Owner

Closes the dead-task notification replay defect in the Pi watcher extension, the same one fixed for the omp fork in #24.

The defect

.pi/extensions/fm-primary-pi-watch.ts replayed every stored actionable wake at session_start and re-persisted whatever was still pending at session_shutdown, with no rule for retiring a record.
A record only cleared once it was handled against a live task, and a task that no longer exists can never clear one.
Because each arm close minted a fresh token for the same unread reason and the queue deduplicated on token alone, one undelivered wake grew a copy per close and survived every restart.

Measurement

The captain's preserved live store, 78 records, was transplanted unaltered into an isolated temporary home and replayed through both the pre-fix and post-fix extension on the identical harness.

records in store after load follow-ups delivered naming the torn-down task
before 78 78 retained 78 75
after 78 removed 0 0

The fix

Three rules bound the store, applied wherever it is read or written and to the in-memory queue as well:

  • Liveness. A signal: reason names <state>/<id>.status and <state>/<id>.turn-ended files, and state/<id>.meta is that task's record, published by fm-spawn.sh before the worker can write any signal file and removed by fm-teardown.sh with the backlog transition.
    A record whose every named task has lost its meta is dropped rather than replayed; one naming a live task among torn-down ones stays, and a reason naming no task at all is never dropped.
  • Identity. Undelivered records collapse by message, keeping the earliest, so one unread reason is one queued turn however often it re-fires.
    A delivered record is never collapsed into.
  • Bound. At most FM_PI_HANDOFF_MAX_RECORDS records (32), none older than FM_PI_HANDOFF_MAX_AGE_MS (24 hours), oldest first.

A load that drops records rewrites the store immediately, so a session that never reaches shutdown cannot hand them on.
An arm close whose wake is retired no longer waits for a delivery that will never run: it starts its own successor, or defers that re-arm when a restoration is already delivering, so retiring a record never leaves supervision dark.

Divergence from the omp port

The two extensions are not identical, and the differences were established before porting rather than assumed.

  • Pi carries the supervision-branch model that omp does not.
    Retention is orthogonal to it: retention runs at enqueue and at every store read and write, strictly before delivery chooses between the branch and main, so a collapsed duplicate is one branch offer rather than none, and a wake naming only torn-down tasks is offered to neither.
    A branch-handled wake already clears immediately, so nothing lingers there.
  • lib/fm-branch-dispatch.ts already reads state/<id>.meta as the task record, so the liveness rule rests on the same fact Pi's own branch routing does.
  • Pi's node:fs import lacked existsSync, which the liveness check needs.
  • Pi's validatePendingActionable gates on actionableLine, while omp additionally accepts supervision-host: messages.

The omp header's note that handoff retention diverges from the Pi port became false with this change and is removed; that is the only edit to the omp extension and it is comment-only.

Tests

tests/fm-pi-watch-extension.test.sh gains six cases covering the three retention rules, the delivery continuity the fix must not break, and the arm-close continuity cases:

  • A handoff record whose task has no state/<id>.meta is dropped at load and never delivered.
  • Identical undelivered wakes collapse to one, and the record bound and age cut-off drop the oldest first.
  • A live task's undelivered wake still survives a session replacement and is replayed.
  • An arm close whose wake is a duplicate or names a torn-down task still starts a successor arm.
  • A duplicate close re-drives a queued wake whose earlier delivery failed.
  • A duplicate close while a restoration delivers still leaves a successor arm.

Four of the six fail against the pre-fix extension.
The restoration case was separately confirmed to pin the new deferred re-arm: removing that branch from the fixed code leaves the watcher dark.
The continuity case passes before and after by design, as the guard that retention never drops a live undelivered record.

Validation

  • tests/fm-pi-watch-extension.test.sh: 57 passed, 0 failed.
  • bin/fm-lint.sh over the full canonical set: pass (ShellCheck 0.11.0, actionlint 1.7.12).
  • bin/fm-doc-audience-check.sh: ok, 112 surfaces, 622 local links.
  • no-mistakes pipeline: review, test, document, lint and push all passed.

No CI checks are expected on this base.

The Pi watch extension replayed every stored actionable wake at
session_start and re-persisted whatever was still pending at
session_shutdown, with no rule for retiring a record. Because each arm
close minted a fresh token for the same unread reason and the queue
deduplicated on token alone, one undelivered wake grew a copy per close
and survived every restart. Replaying the captain's preserved 78-record
store through this extension delivered 78 follow-ups, 75 of them for a
task torn down nine days earlier, costing the session one model turn and
one arm restart each.

This is the same defect fixed for the omp fork in #24, and it is bounded
the same way, by three rules applied wherever the store is read or
written and to the in-memory queue as well:

- Liveness. A "signal:" reason names <state>/<id>.status and
  <state>/<id>.turn-ended files, and state/<id>.meta is that task's
  record, published by fm-spawn.sh before the worker can write any
  signal file and removed by fm-teardown.sh with the backlog
  transition. A record whose every named task has lost its meta is
  dropped rather than replayed; one naming a live task among torn-down
  ones stays, and a reason naming no task at all is never dropped.
- Identity. Undelivered records collapse by message, keeping the
  earliest, so one unread reason is one queued turn however often it
  re-fires. A delivered record is never collapsed into.
- Bound. At most 32 records, none older than 24 hours, oldest first.

A load that drops records rewrites the store immediately, so a session
that never reaches shutdown cannot hand them on. An arm close whose wake
is retired no longer waits for a delivery that will never run: it starts
its own successor, or defers that re-arm when a restoration is already
delivering, so retiring a record never leaves supervision dark.

Retention is orthogonal to Pi's supervision branch. It runs at enqueue
and at every store read and write, strictly before delivery chooses
between the branch and main, so a collapsed duplicate is one branch
offer rather than none, and a wake naming only torn-down tasks is
offered to neither. lib/fm-branch-dispatch.ts already reads
state/<id>.meta as the task record, so the liveness rule rests on the
same fact the branch routing does.

Delivery continuity is unchanged: a live task's undelivered wake is
still loaded, delivered, carried across a session replacement, and
replayed, which tests/fm-pi-watch-extension.test.sh now pins alongside
the three retention rules and the arm-close continuity cases.
@Boxyboxy
Boxyboxy merged commit baad56f into feat/omp-adaptor Sep 27, 2026
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