Repository navigation
fix: prevent OpenCode watch-arm churn during wake bursts - #7316
amanmishra2 wants to merge 9 commits into
Conversation
Wake bursts deliver overlapping wakes within seconds, and the OpenCode host can hold more than one load of the watch-arm plugin in one process. Each load kept its own module-level arm state, and every spawn launched `bin/fm-watch-arm.sh --restart`, so overlapping deliveries started colliding successor arms: the second `--restart` signalled the healthy cycle the first had just started, which then read as a signal-exit failure and drove the bounded retry loop until it exhausted. The live 2026-10-14 12:19-12:32 PDT burst shows plugin-spawned arms arriving in pairs and cycles TERMed 2-20s after start in state/.watch-cycle-exits.log, alongside the plugin's "unready successor arm did not exit" and "could not restore watcher continuity after 5 retries" failures. Burst reproduction (replayed arm spawns): a scratch fixture drives two independent plugin loads with overlapping session.idle deliveries and scripted wake closes. Before the fix that replay showed 13 arm starts, 13 --restart invocations, and 10 healthy-cycle stops with 13 distinct cycle pids. After the fix the same replay shows one successor per delivery, zero restarts, zero stops, and no predecessor arm alive behind its successor. Fixed behavior: - One arming state per home, anchored on globalThis, is shared by every load of the plugin in the host process, so overlapping deliveries deduplicate to exactly one successor arm instead of one per load. - Successors launch as plain arms: they attach to a verified healthy cycle and start their own only when none is live, so a redundant successor never stops a live cycle. --restart remains the explicit replacement mode for recovery paths. - Predecessor arms exit before their successor is established, because the successor is spawned from the predecessor's close handler, and the shared single-child state leaves no attached arm following the chain. The supervision-host path keeps its documented `park --restart` first-cycle behavior; docs/supervision-host.md records changing that path as a separate follow-up. Regression coverage: tests/fm-pi-watch-extension.test.sh gains the overlapping-burst and attach-without-stopping tests driven through two plugin loads, and tests/fm-watch-arm.test.sh gains a real-watcher concurrent-attach test proving the live cycle keeps its lock while both attached arms resolve its wake.
|
Speaking as Kun's firstmate: thank you @amanmishra2 — first look at tip Review: OpenCode watch-arm shares one per-home coordinator across plugin loads, dedupes burst successors, routes wakes to the current session-lock owner, and switches OpenCode successor launch from Contract-class: new-default (my judgment). Dedup alone would be closer to restoring one-live-cycle continuity, but replacing the specified OpenCode VISION.md: one-interface / authority / scripts / restart / spine / vendor / scope all align for the churn fix; captain merge-or-leave applies because of the default successor-path change. Blocker this pass: live validation is 3 of 5 — owner-routing and opted-in host takeover are still untested live (arm refused by the lab gate). When you can, please live-test those two against a real OpenCode primary (or re-raise through no-mistakes with the evidence) so we can take a full look. No need to rush; whenever it fits your schedule is fine. Once live is complete and CI is green, this goes to Kun as merge-or-leave (new-default), not auto-merge. Nothing else needed from you for CI — we will watch the approved runs. |
… which caused a spurious initial wake before the intended decision signal. `bash tests/fm-pi-watch-extension.test.sh` and `git diff --check` passed
|
Thanks for the review. The CI fix for the coverage guard is up as a new tip (b060d5f); its workflow runs are waiting on first-time fork approval again whenever you have a moment. Working on the owner-routing and opted-in host takeover live evidence; will follow up here. |
|
Speaking as Kun's firstmate: thanks @amanmishra2, and thanks for picking up the two remaining live scenarios. I checked the delta from Nothing has changed since my last note. The class is still new-default, because the OpenCode successor path moves from |
Intent
Fix the supervision churn: under wake bursts, the OpenCode watch-arm spawns colliding successor arms that restart healthy cycles, so supervision oscillates, downtime episodes repeat, and the plugin exhausts its five retries. The goal: exactly one live supervision cycle under burst load - successors are deduplicated per delivery, a healthy cycle is never stopped by a redundant successor, and predecessors are retired deterministically instead of lingering as attached arms.
What Changed
Risk Assessment
Testing
The existing live TUI transcript shows one wake for a status burst, a stable watcher across extra idle turns, and successor-cycle handoff. Four focused fixtures passed for burst deduplication, healthy-cycle attachment, owner routing, and host takeover. A new live host attempt reached the isolated OpenCode server but was stopped by the gate guard before plugin delivery; I did not bypass it. No screenshot was captured because this changes supervision lifecycle behavior rather than visual layout. A
fm-lock.sh --helpsetup probe acquired a worktree-local lock; I removed that test-created file and confirmed the worktree was clean.test_opencode_overlapping_deliveries_keep_one_live_cyclealso passed.fm-labtmux session with an isolated home and local provider. The plugin-spawned arm was refused bybin/fm-watch-arm.shbecause the plugin sets `FM_RO…Evidence: OpenCode live TUI and watcher-state transcript
Source: OpenCode live TUI and watcher-state transcript
Evidence: Focused OpenCode fixture fallback results
Source: Focused OpenCode fixture fallback results
ok - OpenCode overlapping deliveries keep one live cycle and retire predecessors ok - OpenCode successors attach to a healthy cycle without stopping it ok - OpenCode wake delivery follows the current owner and logs unresolved fallback ok - OpenCode host takes over a healthy detached cycle without a downtime resurfacePipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
.opencode/plugins/fm-primary-watch-arm.js:570- If session A idles first and starts the shared home arm, a later session B load returns that existing child without updating its delivery target. The close handler retains A’ssessionIDand client (.opencode/plugins/fm-primary-watch-arm.js:519-524) and sends the wake to A (.opencode/plugins/fm-primary-watch-arm.js:349-353), so B may not receive it. The code and intent do not specify whether the first or latest session should receive a deduplicated home wake; clarify that routing choice before relying on cross-session deduplication.🔧 Fix applied.
2 issues (1 error, 1 warning) still open:
.opencode/plugins/fm-primary-watch-arm.js:570- If session A idles first and starts the shared home arm, a later session B load returns that existing child without updating its delivery target. The close handler retains A’ssessionIDand client (.opencode/plugins/fm-primary-watch-arm.js:519-524) and sends the wake to A (.opencode/plugins/fm-primary-watch-arm.js:349-353), so B may not receive it. The code and intent do not specify whether the first or latest session should receive a deduplicated home wake; clarify that routing choice before relying on cross-session deduplication..opencode/plugins/fm-primary-watch-arm.js:488- Required criterion: “a healthy cycle is never stopped by a redundant successor.” The changed launch hunk still selectsfm-supervision-host.sh park --restartin host mode at line 488. On a main-only/pass-through wake, the host first detaches a healthy successor for main (bin/fm-supervision-host.sh:710-715), then this launch restarts that cycle (bin/fm-supervision-host.sh:1061-1069). The ordinary-arm change addressed churn, but left this host-mode path; the owner-routing fix round did not change it. The changed docs also describe an unconditional plain, non-restarting arm (docs/supervision-protocols/opencode.md:7,docs/watcher-continuity.md:143). Launchparkwithout--restarthere so the next host can take over the recorded cycle, and document the host-mode behavior.🔧 Fix applied.
4 issues (2 errors, 2 warnings) still open:
.opencode/plugins/fm-primary-watch-arm.js:570- If session A idles first and starts the shared home arm, a later session B load returns that existing child without updating its delivery target. The close handler retains A’ssessionIDand client (.opencode/plugins/fm-primary-watch-arm.js:519-524) and sends the wake to A (.opencode/plugins/fm-primary-watch-arm.js:349-353), so B may not receive it. The code and intent do not specify whether the first or latest session should receive a deduplicated home wake; clarify that routing choice before relying on cross-session deduplication..opencode/plugins/fm-primary-watch-arm.js:488- Required criterion: “a healthy cycle is never stopped by a redundant successor.” The changed launch hunk still selectsfm-supervision-host.sh park --restartin host mode at line 488. On a main-only/pass-through wake, the host first detaches a healthy successor for main (bin/fm-supervision-host.sh:710-715), then this launch restarts that cycle (bin/fm-supervision-host.sh:1061-1069). The ordinary-arm change addressed churn, but left this host-mode path; the owner-routing fix round did not change it. The changed docs also describe an unconditional plain, non-restarting arm (docs/supervision-protocols/opencode.md:7,docs/watcher-continuity.md:143). Launchparkwithout--restarthere so the next host can take over the recorded cycle, and document the host-mode behavior..opencode/plugins/fm-primary-watch-arm.js:322- Round 1 (537df8a) snapshotsstate.deliveryTargetat line 322, then validates it beforesendPromptawaits the encoder subprocess (:304-305). If session A starts delivery and session B registers as the current owner while encoding runs, line 328 still prompts A with the captured target, so B can miss the wake. This violates the accepted rule to route to the current lock owner and update the target on ownership changes. Select and recheck the latest target immediately beforepromptAsync, retaining the logged fallback if ownership is unresolved; keep the lock stable across its asynchronous ancestry check too. The same routing path serves confirmation-failure wakes (:374), confirmed wakes (:378), and surfaced failures (:386)..opencode/plugins/fm-primary-watch-arm.js:482- Round 2 changed the host launch to plainpark, but.opencode/plugins/fm-primary-watch-arm.js:9still describespark --restart, anddocs/supervision-host.md:114still says OpenCode and omp use--restart. These stale claims contradict the new command and misdescribe OpenCode's host handoff; update both references while preserving the omp behavior.🔧 No changes applied.
5 issues (2 errors, 3 warnings) still open:
.opencode/plugins/fm-primary-watch-arm.js:570- If session A idles first and starts the shared home arm, a later session B load returns that existing child without updating its delivery target. The close handler retains A’ssessionIDand client (.opencode/plugins/fm-primary-watch-arm.js:519-524) and sends the wake to A (.opencode/plugins/fm-primary-watch-arm.js:349-353), so B may not receive it. The code and intent do not specify whether the first or latest session should receive a deduplicated home wake; clarify that routing choice before relying on cross-session deduplication..opencode/plugins/fm-primary-watch-arm.js:488- Required criterion: “a healthy cycle is never stopped by a redundant successor.” The changed launch hunk still selectsfm-supervision-host.sh park --restartin host mode at line 488. On a main-only/pass-through wake, the host first detaches a healthy successor for main (bin/fm-supervision-host.sh:710-715), then this launch restarts that cycle (bin/fm-supervision-host.sh:1061-1069). The ordinary-arm change addressed churn, but left this host-mode path; the owner-routing fix round did not change it. The changed docs also describe an unconditional plain, non-restarting arm (docs/supervision-protocols/opencode.md:7,docs/watcher-continuity.md:143). Launchparkwithout--restarthere so the next host can take over the recorded cycle, and document the host-mode behavior..opencode/plugins/fm-primary-watch-arm.js:322- Round 1 (537df8a) snapshotsstate.deliveryTargetat line 322, then validates it beforesendPromptawaits the encoder subprocess (:304-305). If session A starts delivery and session B registers as the current owner while encoding runs, line 328 still prompts A with the captured target, so B can miss the wake. This violates the accepted rule to route to the current lock owner and update the target on ownership changes. Select and recheck the latest target immediately beforepromptAsync, retaining the logged fallback if ownership is unresolved; keep the lock stable across its asynchronous ancestry check too. The same routing path serves confirmation-failure wakes (:374), confirmed wakes (:378), and surfaced failures (:386)..opencode/plugins/fm-primary-watch-arm.js:482- Round 2 changed the host launch to plainpark, but.opencode/plugins/fm-primary-watch-arm.js:9still describespark --restart, anddocs/supervision-host.md:114still says OpenCode and omp use--restart. These stale claims contradict the new command and misdescribe OpenCode's host handoff; update both references while preserving the omp behavior..opencode/plugins/fm-primary-watch-arm.js:620- Round 2’s owner-routing fix introduced duplicate admission checks:registerDeliveryTargetrunsisPrimaryRootandsessionOwnsLock, thenensureArmimmediately repeats both inbeginArm, even when a child is already live. Each check spawns subprocesses; during an idle burst these run concurrently before launch single-flight can deduplicate them. Both changed entrypoints reach this path (:628coordinator callback and:635idle handler). Avoid repeating the checks while preserving owner validation.🔧 Fix applied.
6 issues (3 errors, 3 warnings) still open:
.opencode/plugins/fm-primary-watch-arm.js:570- If session A idles first and starts the shared home arm, a later session B load returns that existing child without updating its delivery target. The close handler retains A’ssessionIDand client (.opencode/plugins/fm-primary-watch-arm.js:519-524) and sends the wake to A (.opencode/plugins/fm-primary-watch-arm.js:349-353), so B may not receive it. The code and intent do not specify whether the first or latest session should receive a deduplicated home wake; clarify that routing choice before relying on cross-session deduplication..opencode/plugins/fm-primary-watch-arm.js:488- Required criterion: “a healthy cycle is never stopped by a redundant successor.” The changed launch hunk still selectsfm-supervision-host.sh park --restartin host mode at line 488. On a main-only/pass-through wake, the host first detaches a healthy successor for main (bin/fm-supervision-host.sh:710-715), then this launch restarts that cycle (bin/fm-supervision-host.sh:1061-1069). The ordinary-arm change addressed churn, but left this host-mode path; the owner-routing fix round did not change it. The changed docs also describe an unconditional plain, non-restarting arm (docs/supervision-protocols/opencode.md:7,docs/watcher-continuity.md:143). Launchparkwithout--restarthere so the next host can take over the recorded cycle, and document the host-mode behavior..opencode/plugins/fm-primary-watch-arm.js:322- Round 1 (537df8a) snapshotsstate.deliveryTargetat line 322, then validates it beforesendPromptawaits the encoder subprocess (:304-305). If session A starts delivery and session B registers as the current owner while encoding runs, line 328 still prompts A with the captured target, so B can miss the wake. This violates the accepted rule to route to the current lock owner and update the target on ownership changes. Select and recheck the latest target immediately beforepromptAsync, retaining the logged fallback if ownership is unresolved; keep the lock stable across its asynchronous ancestry check too. The same routing path serves confirmation-failure wakes (:374), confirmed wakes (:378), and surfaced failures (:386)..opencode/plugins/fm-primary-watch-arm.js:482- Round 2 changed the host launch to plainpark, but.opencode/plugins/fm-primary-watch-arm.js:9still describespark --restart, anddocs/supervision-host.md:114still says OpenCode and omp use--restart. These stale claims contradict the new command and misdescribe OpenCode's host handoff; update both references while preserving the omp behavior..opencode/plugins/fm-primary-watch-arm.js:620- Round 2’s owner-routing fix introduced duplicate admission checks:registerDeliveryTargetrunsisPrimaryRootandsessionOwnsLock, thenensureArmimmediately repeats both inbeginArm, even when a child is already live. Each check spawns subprocesses; during an idle burst these run concurrently before launch single-flight can deduplicate them. Both changed entrypoints reach this path (:628coordinator callback and:635idle handler). Avoid repeating the checks while preserving owner validation..opencode/plugins/fm-primary-watch-arm.js:316- Round 3 added an unresolved-owner fallback that conflicts with Round 4’s instruction to “use the OWNING sessions id/client rather than the retained arming sessions.” If A arms the home and the lock switches to B before B registers its client, A’s close fails restoration as read-only, then this branch sends the wake and failure to A; B receives no prompt. The shared helper also serves confirmation failures and surfaced failures (:366,:378; normal delivery at:370). The added test codifies fallback to A attests/fm-pi-watch-extension.test.sh:5122-5128, and changed docs describe it atdocs/watcher-continuity.md:146anddocs/supervision-protocols/opencode.md:8. Remove the arming-session fallback to meet the owner-routing instruction; if unresolved-owner failures must still be delivered, that behavior needs an explicit decision.🔧 **Test** - 1 issue found → auto-fixed ✅
pass-through attended no supervision engine; its cycle ended withsuccessor=none, s…bash tests/fm-pi-watch-extension.test.shbash tests/fm-watch-arm.test.shRan OpenCode 1.18.29 in a privatefm-labtmux session against a disposable local model endpoint; submitted multiple turns and captured the live TUI pane.Changed three disposable.statusfiles during a live watcher cycle; inspected the wake prompt, durable queue,.watch.lock/pid, and.watch-cycle-exits.log.Repeated the burst withconfig/supervision-hostenabled; inspected.supervision-host.log, the host record, current watcher PID, and retired process IDs.Checkedcommand -v claude; saved the TUI and lifecycle transcript to the evidence directory, then removed the disposable labs and nested project fixture.🔧 Fix applied.
✅ Re-checked - no issues remain.
test_opencode_overlapping_deliveries_keep_one_live_cyclealso passed.fm-labtmux session with an isolated home and local provider. The plugin-spawned arm was refused bybin/fm-watch-arm.shbecause the plugin sets `FM_RO…opencode --help,opencode serve --help, andopencode run --helpStarted OpenCode in a privatefm-labtmux session with a temporary home and localhost provider; the real watcher arm refused before a prompt was sent.bash tests/.fm-opencode-focus.test.shrunning the four focused OpenCode fixture scenariosReviewed the existing live artifactopencode-watch-arm-live.txtRemoved the probe-created worktreestate/.lockand temporary fixture files; finalgit status --shortwas clean🔧 **Document** - 1 issue found → auto-fixed ✅
docs/supervision-host.md:114- This still says OpenCode launches the host with--restart, while the target plugin uses plainpark. I left it unchanged because the latest recorded decision declined this specific documentation fix.🔧 Fix applied.
✅ Re-checked - no issues remain.
🔧 Fix applied.
1 warning still open:
✅ **Push** - passed
✅ No issues found.