Skip to content

fix(pi): settle watcher delivery on Pi accepting the follow-up - #3513

Merged
kunchenguid merged 2 commits into
mainfrom
fm/fm-supervision-continuity-this-session-s1
Sep 2, 2026
Merged

kunchenguid merged 2 commits into
mainfrom
fm/fm-supervision-continuity-this-session-s1

Conversation

@kunchenguid

@kunchenguid kunchenguid commented Sep 2, 2026 •

Copy link
Copy Markdown
Owner

Problem

Since #3498, the Pi watcher extension delivered an actionable wake to main and then waited for Pi's before_agent_start carrying that exact text before it would clear its single-flight pipeline. Pi raises before_agent_start only for a prompt that starts an idle agent. A follow-up queued while main is mid-turn is drained by the agent loop into the running run and never raises it, so the first wake that landed during a busy turn pinned the pipeline for the rest of the session: every later watcher close was enqueued and dropped, no successor started, nothing was offered to the supervision branch, and the turn-end guard woke main to re-arm by hand after every close. Task-local events then fell through to main, which narrated routine progress into the captain's chat.

Proven against the installed Pi 0.84.3 binary on 2026-09-02: a follow-up sent while streaming raises input and a new turn_start, then a user message_start with the exact text once the run reaches it, but no before_agent_start; the same follow-up sent while idle raises before_agent_start with the exact text.

Change

  • A main follow-up counts as delivered once Pi accepts it (sendUserMessage resolves). The successor pipeline never waits for consumption.
  • Consumption tracking is kept only for what fix(pi): preserve watcher continuity across session replacement #3498 added it for: deciding what a replacement session (/new, /resume, /fork, reload) replays. It is now observed at before_agent_start for an idle main and at the user message_start for a streaming main; either finishes the pending record, and a still-unconsumed record rides the replacement handoff.
  • A delivery continuation that runs after the session was replaced reports a main-queued wake undelivered so the replacement replays it, and still finishes a branch-handled one.
  • An exhausted restoration delivers its typed failure without launching one more arm past the retry bound. fix(pi): preserve watcher continuity across session replacement #3498's pipeline finally had added that launch; the stall hid it in the existing hung-successor test, and a hung extra child would make the repair call report unchanged.
  • The replacement-coordinator map is typed, so the strict no-emit typecheck passes again (it failed on main with an implicit any).

Tests

  • Test doubles no longer raise before_agent_start for a streaming send; the live-guard doubles model the idle path Pi actually takes.
  • New portable regression test_pi_streaming_time_delivery_keeps_the_successor_chain: two actionable closes while main streams must each be followed by a successor launch and a delivered wake with no before_agent_start; after one is consumed at its user message_start, a replacement replays exactly the other, and the idle replay is consumed at before_agent_start.
  • New real-SDK probe in tests/fm-pi-branch-live-e2e.test.sh: the tracked extension loaded through Pi's own resource loader into a real AgentSession with an in-process fake provider held open mid-stream. It pins the event contract for both the streaming and the idle follow-up and proves the successor chain against the real runtime, with no credential read and no request leaving the machine.

Verification

bin/fm-test-run.sh tests/fm-pi-watch-extension.test.sh
FM_PI_PACKAGE_DIR=<pi-0.84.4> bin/fm-test-run.sh tests/fm-pi-branch-extension.test.sh
FM_PI_PACKAGE_DIR=<pi-0.84.4> npm exec --yes [email protected] -- bash tests/fm-pi-primary-types.test.sh
FM_PI_BRANCH_LIVE_E2E=1 FM_PI_PACKAGE_DIR=<pi-0.84.4> bin/fm-test-run.sh tests/fm-pi-branch-live-e2e.test.sh
bin/fm-lint.sh
bin/fm-doc-audience-check.sh
ok - Pi hung successor falls back to one typed actionable wake
ok - Pi streaming-time wake delivery keeps the successor chain and replays only unconsumed wakes
FM_TEST_SUMMARY total=1 failed=0 skipped_gate=0 duration_ms=52642   (fm-pi-watch-extension, 28 Pi cases + OpenCode cases)
FM_TEST_SUMMARY total=1 failed=0 skipped_gate=0 duration_ms=42212   (fm-pi-branch-extension)
ok - tracked Pi extensions pass strict no-emit typecheck against Pi 0.84.4
ok - real Pi SDK 0.84.4 queues a streaming-time watcher wake without before_agent_start, keeps the successor chain, and surfaces consumption of both follow-ups
FM_TEST_SUMMARY total=1 failed=0 skipped_gate=0 duration_ms=5515    (fm-pi-branch-live-e2e, 6 probes)
fm-doc-audience-check: ok surfaces=89 local_links=301

The branch-extension suite needs FM_PI_PACKAGE_DIR at a 0.84.x install; against the globally installed 0.80.10 its first rendering-consumer case fails before any delivery case, as docs/verification/runtime-backends.md already records.

Review follow-up

Greptile's P1 (a verified successor closing while branch settlement or sendUserMessage is still pending left the generation with no watcher and no retry) was reproduced with a test that holds a branch settlement open while the verified successor exits with a failure: the generation stayed at two arms after the settlement released. The close handler now records such a failure when the child had reported readiness and was not retired by the restoration itself, and the pipeline runs the ordinary bounded, lock-checked retry for it once the delivery settles. An exhausted restoration still delivers its typed failure without a further arm. Regression: test_pi_successor_failure_during_delivery_is_retried_after_delivery.

Docs

docs/watcher-continuity.md, docs/pi-supervision-branch.md, and the two verification records now state delivery as "Pi accepted the follow-up" and point at the extension header, which owns the delivery-versus-consumption contract.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QJjTsUvKkWAwLGNoncaZ3a

A follow-up queued while main is streaming joins the running run without
ever raising before_agent_start, so waiting on that event before clearing
the successor pipeline (#3498) stalled every later actionable close: no
successor started, no wake was delivered or offered to the branch, and the
turn-end guard woke main to re-arm by hand after every close.

The pipeline now settles once Pi accepts the follow-up. Consumption is
observed at before_agent_start for an idle main and at the user
message_start for a streaming main, and decides only what a replacement
session (/new, /resume, /fork, reload) replays. An exhausted restoration
delivers its typed failure without launching an arm past the retry bound,
which the stall had hidden. The replacement-coordinator map is typed so the
strict no-emit typecheck passes again.

Tests: the doubles no longer raise before_agent_start for a streaming send,
a portable regression drives two actionable closes while main streams and
proves the successor chain plus consumption-scoped replay, and a
credential-free real-SDK probe pins Pi's event contract for both the
streaming and the idle follow-up.

Claude-Session: https://claude.ai/code/session_01QJjTsUvKkWAwLGNoncaZ3a
@greptile-apps

greptile-apps Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported successor-close gap is addressed by preserving the verified close during delivery and scheduling its bounded retry after settlement.

Reviews (2): Last reviewed commit: "fix(pi): retry a verified successor that..." | Re-trigger Greptile

Comment thread .pi/extensions/fm-primary-pi-watch.ts Resolved
A verified successor can exit while the wake it was started for is still
being delivered, most plausibly during a branch turn that holds the
settlement for minutes. Its failure close arrived while the pipeline's
single-flight guard was set, so the close handler skipped the retry, and
the pipeline's end no longer launched an arm, which left the live
generation with no watcher and no retry timer.

The close handler now records that failure when the child had reported
readiness and was not retired by the restoration itself, and the pipeline
runs the ordinary bounded, lock-checked retry for it once the delivery
settles. A restoration started for a later pending supersedes it, and an
exhausted restoration still hands repair to main without a further arm.

The regression holds a branch settlement open while the verified
successor exits with a failure and proves one retry watcher starts after
the settlement releases, none while it is held.

Claude-Session: https://claude.ai/code/session_01QJjTsUvKkWAwLGNoncaZ3a
@kunchenguid
kunchenguid merged commit 77ee3c8 into main Sep 2, 2026
14 of 16 checks passed
@kunchenguid
kunchenguid deleted the fm/fm-supervision-continuity-this-session-s1 branch September 2, 2026 14:59
npayette84 added a commit to npayette84/firstmate that referenced this pull request Sep 2, 2026
…contract

Upstream main now settles a main delivery once Pi accepts the follow-up and
observes consumption at before_agent_start for an idle main and at the user
message_start for a streaming one (kunchenguid#3513), replacing the acknowledgement
contract this change was built against.

Presentation coalescing moves on top of that contract unchanged in behavior:
sendWake keeps its WakePresentation argument next to the base's pending-record
argument, sets the ordinary latch before the delivery await, rolls it back in
the catch that already drops the unconsumed-wake record, and a coalesced wake
never enters that map, so its pending record is finished rather than left
waiting for a consumption that will never come.

The base's streaming-time delivery regression drove two actionable closes
through one streaming run and expected a docked row for each. Coalescing
presents the second through the row already docked, so it now proves the
successor chain advances without a second row and takes its unconsumed handoff
wake from a later close raised after that row is consumed at message_start and
its run settles.

The verification record names the new base, and the counterfactual matrix was
re-run there: unmodified upstream fails only the burst test, each single
clearing edge fails only its own re-arm test, call-site-only urgency fails both
restoration-failure tests, removing urgency fails all four failure-bypass
tests, and a latch shared across generations fails only the session-replacement
test.
Marcos-Leyva added a commit to Marcos-Leyva/firstmate that referenced this pull request Sep 3, 2026