Skip to content

Support OpenCode v2 diff and plan review integration #369

Closed

Description

@LerikP

I'd like revdiff's OpenCode integration to support OpenCode v2. The current integration under plugins/opencode/ uses the v1 plugin and custom-tool APIs, which need to be ported for v2.

References:

Migration considerations

The integration currently includes a revdiff tool, a /revdiff command, and an automatic plan-review plugin.

The plan-review plugin exports an async function returning an event hook. On session.idle, it reads the last assistant message, checks whether it was produced in plan mode, launches revdiff, and sends annotations back to the session. V2 requires a new plugin entrypoint and explicit subscriptions, and the message-reading, plan-detection, and prompt-submission logic needs to be checked against the new server API and lifecycle contracts.

The custom tool also uses the v1 tool() helper. V2 documents tool registration through ctx.tool.transform, with JSON Schema inputs and structured results. Existing Markdown command definitions remain supported, so the /revdiff command may need little change once its underlying tool is available.

There is also a routing consideration beyond the API changes: v2 uses a shared background server by default, including for tool execution. The existing launchers rely on terminal-specific environment variables to open the overlay in the correct pane. The server's environment may not identify the client that requested the review.

A CLI plugin may be a better fit for launching the interactive review in the user's terminal. Preserving the agent-callable tool would also require defining how a server-side request reaches the appropriate CLI client and returns annotations to the originating session.

Expected behavior

  • Support manual diff review through /revdiff and the agent-callable revdiff tool, preserving the existing ref, staged, and only options.
  • Automatically review completed plans and return annotations to the originating session for revision in plan mode.
  • Open reviews in the correct terminal pane and project directory when multiple OpenCode clients share a server.
  • Avoid duplicate reviews and unintended launches for unrelated sessions or subagents.
  • Preserve exit-code handling: 0 for a clean exit, 10 for success with annotations, and other nonzero statuses as failures.
  • Handle cancellation and plugin cleanup without leaving review processes or temporary files behind.
  • Update installation instructions and tests for the supported v2 integration.
  • Installer checks OpenCode version and installs appropriate plugin + tool + command.
  • Existing v1 integration remains intact

Contribution

I'd be happy to try implementing this and submit a PR. Before starting, I'd appreciate feedback on the preferred approach

Activity

  1. umputun commented on Sep 28, 2026

    @umputun
    Owner

    the premise is right: v2 does not run v1 plugins, so on v2 the revdiff tool and the plan-review plugin never load, and /revdiff points the model at a tool that does not exist. A PR is welcome. My answers on the approach:

    v1 stays. npm opencode-ai@latest is still 1.18.x, so the files in plugins/opencode/ stay byte-identical and v2 goes in its own files.

    CLI plugin for both /revdiff and plan review. The launchers pick the pane from the terminal env (TMUX, KITTY_WINDOW_ID, AGTERM_SESSION_ID and the rest). The shared server keeps the env of whichever client started it, and a tool call carries no client identity, so a server-side launch opens the review in the wrong pane. A CLI plugin runs in the user's terminal, so the env is right and launch-revdiff.sh / launch-plan-review.sh need no changes.

    plan review can be in the same PR, with an explicit session rule. The CLI plugin sees every server event, so the PR has to define:

    • only the session this client is showing counts, subagent sessions never do
    • one review per completed plan, even when two clients show the same session
    • annotations go back to the session captured at launch, even if the user switches sessions during the review
    • the Plan mode never transitions to Build mode after revdiff closes without annotations #211 behavior carries over: a clean exit leaves the session in plan mode, annotations go back for revision

    the agent-callable tool is not required for v2. If you want it, pls describe in the issue how a server-side call reaches the right client and gets the annotations back before writing code. Plugin RPC might carry it, but that is a small protocol with its own cancellation and no-client cases.

    two things to settle here before writing code:

    • how the CLI plugin gets installed: cli.json edits, npm resolution of @opencode/plugin, or a plain file copy. This decides what setup.sh does.
    • what the installer does with v1 files already in ~/.config/opencode/. OpenCode v2 scans plugins/*.ts, fails to load the v1 plan-review file (packages/core/src/plugin/module.ts:111 in v2.0.18), and skips the file path setup.sh writes into opencode.json (config/plugin/source.ts:152). Both only log a warning, so the user ends up with no integration and no visible error. Upgrade and rollback should not leave broken v1 registrations behind or touch unrelated config.

    pls pin the @opencode/plugin version you tested against, and describe in the PR what you ran against a live v2 and which version. Tests here can't prove real loading or event order.

  2. LerikP commented on Sep 29, 2026

    @LerikP
    ContributorAuthor

    the agent-callable tool is not required for v2. If you want it, pls describe in the issue how a server-side call reaches the right client and gets the annotations back before writing code. Plugin RPC might carry it, but that is a small protocol with its own cancellation and no-client cases.

    Looks like context contains SessionID of the TUI, so agent can send event from server-side to open revdiff and proper client will filter it out and use it, but need to try it out

    how the CLI plugin gets installed:
    cli.json edits

    So far, it's not necessary, autodiscovery works just fine

    npm resolution of @opencode/plugin

    Looks like there is no need to use npm to resolve packages Import @opencode/plugin/tui directly. OpenCode resolves this import at runtime, so local CLI plugins do not need an absolute path to an OpenCode checkout.

    or a plain file copy.

    Will try to stick to this route

    OpenCode v2 scans plugins/*.ts

    It can be in plugins/plan-review/tui.js, so it would not be discovered by OpenCode v1, and old plugin will just be ignored as not-supported in v2, but I can include some setup parameter (e.g. --clean to remove both plugin variants and opencode.json(c) entry before executing installation logic, so extra files are removed from plugins directory)

  3. umputun commented on Sep 29, 2026

    @umputun
    Owner

    file copy with autodiscovery and a plugin directory are fine. OpenCode 1.18 only globs top-level {plugin,plugins}/*.{ts,js} (config/plugin.ts) and v2 only picks up directories under plugins/ (discovery.ts), so v1 never sees it. A few adjustments:

    • directory name and files. One CLI plugin carries both /revdiff and plan review, so pls name it for the whole thing, e.g. plugins/revdiff/, and keep it .ts like the rest of the repo. Don't put a server.* or index.* file in it, the host resolves those before tui (host.ts) and would try to load it as a server plugin.
    • config dir. OpenCode v2 reads OPENCODE_CONFIG_DIR, then $XDG_CONFIG_HOME/opencode, so the v2 install should target that rather than the hardcoded ~/.config/opencode.
    • cleanup is the default, not --clean. I was wrong earlier that a leftover v1 plugin only logs a warning. OpenCode v2 marks it failed and shows "1 plugin failed" in the home footer (footer.tsx). So the v2 install removes plugins/revdiff-plan-review.ts and the exact ./plugins/revdiff-plan-review.ts entry setup.sh wrote to opencode.json, nothing else. For opencode.jsonc print a notice and leave it alone, jq can't round-trip comments. A v2 directory left behind after rolling back to v1 is inert, no need to handle it.
    • commands/revdiff.md. A CLI slash command runs before server commands (prompt/index.tsx), but autocomplete lists both, and the md one asks for a tool that doesn't exist on v2. The v2 install should remove it.
    • agent tool. sessionID identifies a session, not a client. Two clients showing the same session both pass a sessionID filter and both open a review, and nothing defines what the tool call does when no CLI client is attached. Pls leave the tool out of this PR. If you want it later, write up the claim, return path, cancellation and no-client cases here first.

    one thing from my first comment is still open: one review per completed plan when two clients show the same session. It applies to automatic plan review regardless of the tool, so pls describe how the plugin decides which client owns the review before writing that part.

    the links above are to the v2.0.19 source, not a live run, so the tested OpenCode version and what you ran against it still go in the PR.

  4. LerikP commented on Oct 2, 2026

    @LerikP
    ContributorAuthor

    one thing from my first comment is still open: one review per completed plan when two clients show the same session. It applies to automatic plan review regardless of the tool, so pls describe how the plugin decides which client owns the review before writing that part.

    For automatic plan review, I'd use a local-only, at-most-once ownership rule:

    • Only a CLI currently displaying the completed plan's root session is eligible; subagent and background sessions are ignored.
    • Before opening the overlay, each eligible client attempts to create a marker using exclusive file creation (open(..., "wx")). The marker key is a hash of [sessionID, assistantMessageID].
    • The filesystem operation is atomic: the first client succeeds and owns the review; the others receive EEXIST and skip it.
    • The winning client launches revdiff using its own terminal environment. Annotations go back to the session captured at launch, even if the user switches views.

    Markers live in a shared local state directory and contain no plan text. They remain across client restarts to prevent the same completed plan from reopening.

    The trade-off is at most one review attempt, rather than guaranteed completion: if the winning client crashes or the launch fails after claiming, another client does not automatically take over. A newly generated plan has a new message ID and can be reviewed normally.

    This coordinates clients on the same machine using the same state directory. It does not provide cross-machine coordination for clients attached to a remote shared server.

    Would this local-only scope and at-most-once behavior be acceptable for the initial PR?

  5. umputun commented on Oct 2, 2026

    @umputun
    Owner

    yes, local-only and at-most-once are fine for the first PR, no takeover after a crash included. A few changes to the rule:

    • trigger. session.idle is deprecated in v2 (session-status-event.ts), the turn-completion event is session.execution.succeeded. Review live completions only, no sweep of older plans on load or session switch.
    • claim key. Use the id of that event, not a hash of session and message id. Every subscriber gets the same event frame, while the assistant message id comes from each client's own cache and can lag behind the event. The message id is still what picks the plan text, after a fresh message.sync.
    • markers don't need to survive restarts. The event feed has no replay: a client that connects later gets server.connected and then live events only (event.ts). So a marker only has to outlive the race between two clients. Pls put them in a named directory, e.g. ${XDG_STATE_HOME:-$HOME/.local/state}/revdiff/opencode-plan-review, and remove the ones older than a day when claiming. EEXIST means another client owns the review, any other error is shown and does not launch.
    • subagent check. data.session.root(id) returns the id itself when the session info is not loaded (data.ts), so root(id) === id alone lets an unknown subagent through. Require loaded session info with no parentID.
    • order. Check that the launcher is executable and revdiff is on PATH before claiming, otherwise a client that can't open a review wins the marker and nobody else tries. Recheck ui.router.current() after any await that comes before the claim.
    • failure. A launcher exit other than 0 or 10 shows a toast, since the claim stays and that review is gone.
    • plan file. A unique temp file removed in finally. The v1 /tmp/revdiff-plan-<sessionID>.md path shouldn't carry over.

    the winner can be a client in a window the user is not looking at. That is fine as a documented limitation.

    the links are to the v2.0.19 source, not a live run.

  6. LerikP commented on Oct 8, 2026

    @LerikP
    ContributorAuthor

    Can we close this as completed?

  7. umputun commented on Oct 8, 2026

    @umputun
    Owner

    yes, closing. OpenCode v2 support is in master via #370: /revdiff and automatic plan review as a CLI plugin, with the installer picking v1 or v2.

    the agent-callable tool is the one item from the original list that is not there. If you want it, pls open a separate issue with the write-up first: how a client claims the call, how annotations get back, cancellation and the no-client case.

    thx for the work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions