Skip to content

fix(opencode): migrate Firstmate plugins to the V2 API so they load on OpenCode 2.x - #5108

Closed
BajajGirik wants to merge 14 commits into
kunchenguid:mainfrom
BajajGirik:fix/opencode-v2-plugins
Closed

BajajGirik wants to merge 14 commits into
kunchenguid:mainfrom
BajajGirik:fix/opencode-v2-plugins

Conversation

@BajajGirik

Copy link
Copy Markdown
Contributor

What broke

OpenCode 2.x refuses every Firstmate plugin:

Plugin must export a default definition with an id and an effect or setup function

All five plugins were written against the V1 API, which exported a named factory
that received { client, directory, worktree } and returned a hooks object. V2
expects a default export with an id and an effect function that receives a
context object. No plugin loaded, so supervision, the turn-end guard, the
directory-change check, the pretool check, and the session-start nudge were all
silently inactive on OpenCode 2.x.

What this changes

Each plugin now exports a V2 default definition. A shared helper in
.opencode/plugins/lib/fm-v2-plugin.js holds the parts every plugin repeats,
so the five plugins keep one shape.

The event names moved too. V1 session.idle and session.status do not exist
in V2. Supervision now keys on session.execution.succeeded,
session.execution.failed, and session.execution.interrupted. Those three fire
once per turn, not once per step, which is what watcher arming needs.

bin/fm-spawn.sh no longer passes --model to OpenCode. V2 reads the model from
its per-launch configuration, so opencode_config_content() writes it there
instead. The other twelve harnesses keep the --model flag path unchanged.

tests/fm-opencode-primary-live-e2e.test.sh read the V1 sqlite tables, which V2
froze in place. Its six queries now read session_v2 and session_message. The
old tables still exist and still answer, so the test passed while never reaching
a migrated plugin.

A real bug found on the way

fm-primary-watch-arm.js read the git root identity with an async child process
inside the turn-end path. A SIGTERM arriving during that read left the promise
unsettled and the watcher never armed. revParse() now uses spawnSync, so the
read completes before a signal can interrupt it. runProcess() also reports
code: 128 on a signal kill instead of the null that Node passes through.

Verification

All five affected suites pass, 239 assertions total:

suite assertions
fm-pi-watch-extension 54
fm-turnend-guard 90
fm-busy-adapter-wiring 12
fm-sessionstart-nudge 23
fm-spawn-dispatch-profile 60

opencode plugin list loads all 8 plugins on 2.0.11.

The session_v2 migration was confirmed against the real database by comparing
max(time_created) between the legacy and V2 tables. The legacy tables stopped
receiving rows 28 days ago.

Every change is scoped to OpenCode. The only non-OpenCode file touched is
bin/fm-spawn.sh, where the change is behind [ "$HARNESS" = opencode ].

Ran through the no-mistakes pipeline: intent, rebase, review, test, document, and
lint all passed.

🤖 Generated with Claude Code

OpenCode 2.x renamed the transcript tables. The live end-to-end test still
read `session`, `message`, and `part`, which are now frozen V1 leftovers that
a 2.x run never writes, so the test could not reach the migrated plugins.

Read `session_v2` and `session_message` instead. The V2 message row carries
its role in a `type` column and holds assistant text in a `content` JSON
array, so the assistant queries expand that array rather than joining `part`.
…y so turn-end SIGTERM cannot block watcher arming
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: whole thread read (first stamp; no prior comments/reviews; no linked closing issues; tip vs main bd65e4aef05376bb50dc371aadb1258da0694241).

HEAD: f1ca15037c215e381b16401a06f2a4315dc6bb06
Attestation: n/a — body has no no-mistakes ## Pipeline / attestation bind (body claims the pipeline ran, but Require no-mistakes cannot verify it). Require no-mistakes FAILURE (35566221479): PR not raised through no-mistakes.
mergeable: MERGEABLE / UNSTABLE
workflow-zero: yes (no .github/**).
CI/NM: CI 35566221472 approved this pass → SUCCESS (all shards + timing aggregate green). NM red as above (blocking). Author BajajGirik not blocked.

Contract-class: restore (own verdict; do not trust title/body self-claims)

  • Main OpenCode plugins export the V1 named factory (FmPrimary* / hooks object). OpenCode 2.x refuses them with "Plugin must export a default definition with an id and an effect or setup function", so supervision, turn-end guard, cd/pretool checks, and session-start nudge are silently inactive.
  • Tip migrates the five .opencode/plugins/fm-primary-*.js plus spawn-written busy-state to V2 default id+setup, shared fm-v2-plugin.js, and session.execution.{started,succeeded,failed,interrupted} (V1 session.idle/session.status gone). bin/fm-spawn.sh opencode-only: --standalone + model via OPENCODE_CONFIG_CONTENT (other harnesses unchanged). Live e2e reads session_v2/session_message.
  • Unconfigured path inspected: this is the existing default OpenCode adapter path, re-bound to vendor V2 semantics so the already-specified guards load again — not a new default-on surface.

VISION (per-rule, tip vs main inspected):

  1. One captain/interface — aligns (restores silent-dead OpenCode supervision so crew reports land again).
  2. Authority explicit — aligns (no new autonomy grant; same seatbelts/watcher, V2 wiring).
  3. Scripts/mechanics — aligns (plugins still call owner scripts; mechanics stay deterministic).
  4. Restart non-event — aligns (no durability contract change).
  5. Delegation spine — aligns (watcher arm + turn-end guard resume their existing jobs).
  6. Fleet outlives vendor — aligns (exactly: adapter rebinds to OpenCode 2.x session.execution semantics the vendor exposes).
  7. Scope — aligns (OpenCode harness adapter + docs/tests; scoped [ \"$HARNESS\" = opencode ] spawn change). Note: docs admit interactive follow-up delivery is still V1-only evidence — honesty, not a VISION break.

Security skim: no .github/**; plugins invoke existing bin/fm-*-*.sh seatbelts and ctx.session.prompt; spawn still grants broad OpenCode permissions (same intent as prior permission:* allow, V2 array form). No hidden workflow RCE / fake pin. Not a security withhold.

Closes: none linked.

Outcome: waiting-author. Please raise/rebind via git push no-mistakes so the PR body attestation binds exact HEAD f1ca15037c215e381b16401a06f2a4315dc6bb06, then keep CI+NM green. Diff itself looks restore-shaped and CI is green — the NM attestation is the hold. Not otherwise ready — no Firstmate flag, no merge.

Workflow approvals: CI 35566221472 + NM 35566221479 approved this pass (first-time fork action_required → then NM failed; CI succeeded).

@BajajGirik

BajajGirik commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing this PR in favor of a replacement that preserves the validated implementation and exact-head evidence.

@BajajGirik

BajajGirik commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Replacement PR: #5143. It uses the same fork branch and retains the completed no-mistakes evidence for exact head f7ed0f7.

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.

3 participants