feat(v2): add an OpenCode v2 adapter for micode

Closed
PJ
2d ago
+3030-157

micode 0.11.0 is a v1 plugin: it exports a named OpenCodeConfigPlugin, returns a v1 hooks object, and mutates the host config directly. v2 requires a default Plugin.define({id, setup}) and exposes no config mutation, so no released version loads on v2 — upstream main is still pinned to the v1 plugin package.

v2-adapter/ is a shim that runs the published plugin unchanged and translates its v1 API surface into v2: agents via ctx.agent.transform, commands and tools via their editors, MCP via ctx.mcp.transform, permissions via a ctx.permission evaluate hook, and the v1 hooks onto ctx.session.hook / ctx.tool.hook / ctx.event.subscribe.

Two v2 gaps shape the design:

  • SessionDomain is a Pick that omits compact and remove, so a plugin cannot request a compaction. micode's Auto-Compact is disarmed by withholding the token counts it triggers on — session.usage.updated carries cumulative session usage, which is not what its per-request ratio expects — and summarize() rejects rather than parking the hook in a two-minute wait. OpenCode's own compaction.auto does the compacting.

  • ctx.session.prompt admits and returns a receipt in v2, where v1 returned the reply synchronously. The client shim waits and re-reads context so spawn_agent and octto's probe agents still receive a reply.

Verified on 2.0.21: no load errors, 26 agents, 4 commands, 41 tools, the context7 MCP server and 6 bridged hooks all present in the live registries, and subagent spawning works end to end with a live model.

npm test runs the real upstream plugin against a stand-in host in both directions: 11 assertions, no server required.

What

Brief description of the change.

Why

What problem does this solve? Link to related issue if applicable.

Checklist

  • Tests added or updated
  • bun run check passes
  • Documentation updated (if public API changed)
  • CHANGELOG.md updated (if user-facing change)

Summary by cubic

Adds v2-adapter/, a shim that runs the published micode 0.11.0 plugin on OpenCode v2 despite its v1 plugin API, and fixes the bugs that fix exposed.

v2-adapter

  • Translates the v1 API surface onto v2 transforms and hooks: agents via ctx.agent.transform, commands and tools via their editors, MCP via ctx.mcp.transform, permissions via a ctx.permission hook, and v1 hooks onto ctx.session.hook / ctx.tool.hook / ctx.event.subscribe.
  • Works around two v2 gaps: no session.compact/remove (Auto-Compact disarmed by withholding the token counts it triggers on), and ctx.session.prompt returning a receipt instead of a reply (client shim waits and re-reads context).
  • Addresses review findings: re-throws micode's ConstraintViolationError, resubscribes the event bridge with backoff, seeds agent permissions from config.permission plus subagent (per-agent denies layer over the base), bounds session.wait with a 5-minute race, parses features with jsonc-parser, queues log writes on a timer, and reads session.deleted's id from both the 2.0.21 and fallback shapes.
  • Loads the repo's local dist/ build over the published bundle, so prompt fixes actually reach running agents; local-build.test.js asserts the resolution order and that the loaded bundle teaches the array-form call. npm test runs the real upstream plugin against a stand-in host (16 assertions, no server).

Related fixes

  • Aligns executor, planner, and project-initializer prompts with spawn_agent's single { agents: [...] } array schema; batched spawns previously failed validation and spawned nothing. The contract test pins prompts to the schema, verifies one call overlaps all agents concurrently, and asserts every element is complete.
  • Fixes mindmodel lookup: a manifest-less .mindmodel/ no longer loads as null, and matching is now symmetric over a file's path, description, and headings instead of only the description.
  • Excludes v2-adapter from the root biome/eslint configs (making bun run check green) and syncs the biome schema URL with the installed CLI.