feat(opencode): restore the slash commands on OpenCode 2 - #1434
Merged
Merged
Conversation
OpenCode's V2 plugin API gained native command execution upstream
(anomalyco/opencode issue #2185, PR #44765): ctx.command.transform lets a
plugin add a command whose execute callback fully owns the invocation. That
shape currently ships on the beta and dev dist-tags of @opencode-ai/plugin
while next and latest still carry the older context, so the capability is
duck-typed at runtime and never imported. On a host that exposes it the V2
adapter registers /plannotator-review, /plannotator-annotate and
/plannotator-last and runs the same handleCliCommand machinery OpenCode 1
uses, passing the raw argument tail straight through to the CLI. On a host
without it nothing new is registered and behavior is byte-identical to before.
Also wires ctx.session.switchAgent (same API generation, same probe) so an
agent switch chosen in the review UI is applied instead of only warned about,
and accepts both agent.list() response shapes: the HTTP client types it as a
{ location, data } envelope while the in-process plugin domain answers with a
bare array, where reading .data threw and silently emptied the agent list.
The shared command stubs get model-mediated fallback bodies for OpenCode 2
hosts on the stale channels. They carry no shell interpolation on purpose:
OpenCode 1 evaluates a template's !`...` before the V1 plugin's
command.execute.before hook can clear the parts, so a bang template there
would launch a second Plannotator session on every OC1 invocation. A source
level test pins that.
AI-assisted (Claude) under maintainer direction.
… stubs
Review found the capability probe was wrong in the direction that matters.
ctx.command.transform exists on pre-#44765 hosts too: our own pinned
@opencode-ai/plugin@0.0.0-next-16775 declares CommandDraft as
{ list, get, update, remove } with no add. The probe therefore returned true on
next and latest, draft.add was undefined, and because transforms are stored and
replayed the TypeError landed in the batched reload flush and aborted it before
commit, plausibly taking every command registration on the host down with it.
Capability is now read from the draft handed to the callback, which is the only
witness, and the registration call is wrapped so no transform rejection can fail
plugin setup.
The stubs also shadowed the native definitions on new hosts. Command definitions
land in a name-keyed map where add is Map.set, transforms replay in registration
order, and OpenCode's own ConfigCommandPlugin activates in the post group after
package plugins while scanning the exact directory the installer writes the
three stubs to. A setup-time registration is therefore always overwritten on a
normal install. The plugin now re-registers the same transform once activation
settles, so its definitions are last in the replay order, and calls
ctx.command.reload() explicitly because a late registration only adds its reload
to the already-flushed boot batch. Ownership is read back from
ctx.command.list() by description, which is why the native descriptions and the
stub frontmatter are deliberately distinct. If the reclaim cannot run the stubs
keep the names and the commands still work through their fallback bodies.
Also: a failing switchAgent no longer costs the reviewer their feedback on the
command path, feedback is delivered as "queue" rather than replaying the
invocation's admission mode minutes later when a steer would land mid-turn, and
the agent-list comment no longer asserts a bare-array response that could not be
reproduced upstream (accepting both shapes is still right, since reading .data
blindly throws into a catch that degrades silently).
Tests: the real old-host draft shape registers nothing and throws nothing, the
shadowing contest is modelled against upstream's replay semantics, the OpenCode 1
parts-clearing invariant is pinned for all three commands in both plan-agent and
manual mode now that the stubs carry real instructions, and the V2 smoke asserts
the plugin did not activate as failed and that all three commands resolve. The
smoke now also installs the stubs into its sandbox config dir so the contest
actually happens there. scripts/opencode2-native-commands-smoke.sh runs the same
smoke against a dev-channel build with native commands required; CI cannot,
because it pins a next build.
AI-assisted (Claude) under maintainer direction.
…failing setup The reclaim ended the loop when the draft-probe flag read false, but that flag only flips when the transform replays, which under boot batching is the flush after every plugin has loaded. Plannotator loads before the post-group config plugins, so the first tick legitimately reads false and the loop exited for good: the reclaim was inert in exactly the shape production has. The tick is skipped now instead, with a test that flips the flag between ticks. The V1 entry called resolveBundledHtmlPath synchronously during plugin construction, outside the .catch that was there to absorb a missing asset, so an unbuilt checkout threw out of construction before any code path that needs the HTML. The Test workflow runs bun test with no build step, so the new OpenCode 1 interception tests failed there. Both preloads are guarded; the lazy getters still raise a clear error if something actually needs the file. The smoke's failed-plugin guard read entry.state.status, but Plugin.Info carries status and error at the top level, so a failed activation slipped through. Reads the top level first and keeps the nested one as a fallback. Comment corrections: State.batch clears its active flag before flushing, so a late transform registration materializes on its own; the explicit reload() is redundant-but-defensive rather than required. The reclaim schedule is a list of deltas the loop awaits in turn, so the ticks land near 0.3s, 1.5s, 5.5s and 15.5s, not at the raw numbers. AI-assisted (Claude) under maintainer direction.
This was referenced Aug 31, 2026
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Restores
/plannotator-review,/plannotator-annotateand/plannotator-laston OpenCode 2, where they are currently dead.Upstream lineage
OpenCode's V2 plugin API had no way for a plugin to own a slash command. That was raised as anomalyco/opencode issue #2185, drew a community PR (#44460), and was solved by maintainer PR #44765 (merged 2026-08-24 into their
v2branch): the command draft gainedadd({ name, description, execute }), whereexecutereceivesCommandInvocation { sessionID, prompt, delivery }and fully controls the invocation. It does not have to callctx.session.promptat all, which is exactly what a review command needs: the UI opens, the human decides, and only the result goes to the model.Capability detection
The new draft ships on the
betaanddevdist-tags of@opencode-ai/plugin;nextandlateststill carry the older one.ctx.command.transformis not the signal. It exists on both generations. This repo's own pinned@opencode-ai/plugin@0.0.0-next-16775declaresCommandDraftas{ list, get, update, remove }with noadd, so probing the context reports a false positive on every stale-channel host. Capability is read from the draft handed to the callback instead:This matters because transforms are stored and replayed, not run once at registration. A
TypeErrorfrom calling a missingaddsurfaces inside the batched reload flush and aborts it before commit, which can take the host's whole command registration down rather than just ours. The registration call is also wrapped intry/catchso no transform rejection can fail plugin setup: a slash command has a working fallback, the rest of the integration does not.Nothing imports the new API.
v2-client.tsdescribes the context with local duck-typed interfaces.Command precedence, and why a reclaim is needed
On a native-capable host the plugin's definitions would still lose to the markdown stubs on any normal install. Verified against
origin/v2:Mapanddraft.addisMap.set(packages/core/src/command.ts), so the last transform to add a name wins.packages/core/src/state.ts:transforms = [...transforms, transform], walked bymaterialize).prethen packages thenpost, and OpenCode's ownConfigCommandPluginis inpost(packages/core/src/plugin/internal.ts:265-269,packages/core/src/plugin/supervisor.ts). It scans~/.config/opencode/{command,commands}/**/*.md, which is exactly wherescripts/install.shand the package postinstall write the three stubs.So a setup-time registration always replays first and is overwritten. The fix is to register the same transform once more after activation settles, putting it last in the replay order; it stays last afterwards because config only ever calls
reload()and never re-registers. The explicitctx.command.reload()after re-registering is load-bearing: each plugin's effect runs insideState.batch(packages/core/src/plugin.ts:52), so a late registration only adds its reload to an already-flushed batch (if (batch) batch.add(reload)) and would never materialize on its own.Ownership is read back from
ctx.command.list(), a direct read of committed state, on a bounded four-tick schedule. The event bus is deliberately not used: upstream #44788 reports it as unreliable on some V2 nightlies. Provenance comes from the description, which is why the native descriptions and the stub frontmatter are deliberately distinct, pinned by a test.Failure mode if the reclaim cannot run (no
list/reload, a scope that closed, a future OpenCode that reorders activation): the stubs keep the names and the commands still work through the model-mediated fallback. Degraded, never broken.add(beta,dev)handleCliCommandmachinery OpenCode 1 usesctx.session.switchAgentadd(next,latest)The fallback story
The three shared stubs now have bodies: short, imperative instructions telling the agent to run the
plannotatorCLI in the foreground with$ARGUMENTSand relay its stdout. That is what an OpenCode 2 user on a stale channel gets, and it is also what a native-capable host falls back to if the reclaim never runs. It costs a model turn and depends on the agent following the instruction.Why the fallback is safe on OpenCode 1
On OpenCode 1 a command template's shell interpolation is evaluated before the V1 plugin's
command.execute.beforehook runs (prompt.ts:1397vs:1461upstream), so a bang template in these shared files would launch a second Plannotator session on every OC1 invocation, before the plugin could stop it. The bodies contain none, and a source-level test asserts they never will.The rest of the OC1 path is unchanged:
command.execute.beforeclearsoutput.partsin place before the model sees anything (index.ts:448-456), so the new body text never reaches the OpenCode 1 model and theresolvePromptPartsfile-attachment problem from #713 stays fixed. That invariant now has a test, for all three commands in bothplan-agentandmanualmode, since interception lives on the always-built plugin object whileshouldRegisterSubmitPlanonly gatesplugin.tool.Also fixed
switchAgentno longer costs the reviewer their feedback: it warns and delivers anyway, the same guarantee the approval path already gave."queue"rather than replaying the invocation's owndelivery. That value was chosen when the user pressed enter; a review comes back minutes later, when a"steer"would land in the middle of whatever turn is running. Upstream's default is"steer", so this is set explicitly.ctx.agent.list()is accepted as either the documented{ location, data }envelope or a bare array. Reading.dataunconditionally throws into a catch where the failure is invisible: an empty agent list silently disables subagent gating and agent-switch validation. (The earlier claim that new hosts return a bare array is not reproducible fromorigin/v2and has been dropped from the comment; accepting both is still the right shape.)Tests
bun test apps/opencode-plugin: 158 pass, 0 fail (118 on main).Every new test was checked red-before / green-after by reverting the specific fix:
{ list, get, update, remove }) registers nothing and throws nothing. Fails with aTypeErrorwithout the draft probe.Map.setadd): the config stubs take the names, the reclaim takes them back, a laterreload()keeps them, and the loop stops instead of piling on a transform per tick. All three fail without the reclaim.switchAgentstill delivers feedback; feedback carriesdelivery: "queue". Both fail without their fixes.output.partsin place for all three commands in both workflow modes. All six fail without the clearing.command.transformdoes not fail plugin setup.bun run typecheckpasses, plus a scopedtscoverapps/opencode-pluginwith no errors in any touched file.bun run build:opencodesucceeds (no UI changed, so the copied HTML is intentionally stale).Smoke and manual verification
fixtures/v2-installed-smoke.tsnow installs the three stubs into its sandbox config dir, so the contest between the config-loaded commands and the plugin's definitions actually happens there rather than in a shape no user has. It asserts the plannotator plugin entry is notstatus: "failed"(a failed plugin still lists, which is how a wrong probe could have passed the old smoke) and that all three commands resolve via/api/command, reporting which definition owns each name.CI pins
@opencode-ai/cli@0.0.0-next-16775, so that leg proves the stale-channel path only: the plugin registers nothing, the stubs win, and nothing fails. That is the expected outcome there.scripts/opencode2-native-commands-smoke.shis the dev-channel leg, run by a human: it installs@opencode-ai/cli@dev, packs the plugin, and runs the same fixture withPLANNOTATOR_SMOKE_EXPECT_NATIVE=1, which makes stub shadowing fatal. A dev-channel CI job is not proposed: those tags move daily and are not something to hang a required check on.Still needs a human before release: run that script, then open the TUI on the same build and confirm
/plannotator-reviewopens the UI without a model turn, arguments arrive intact, feedback returns to the session, and an agent switch from the review UI takes effect.AI-assisted (Claude) under maintainer direction.