Skip to content

feat(opencode): add CLI-only v2 integration - #370

Merged
umputun merged 3 commits into
umputun:masterfrom
LerikP:feat/opencode-v2
Oct 6, 2026
Merged

umputun merged 3 commits into
umputun:masterfrom
LerikP:feat/opencode-v2

Conversation

@LerikP

@LerikP LerikP commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds OpenCode v2 support while retaining the existing v1 integration.

  • A CLI-only plugin provides /revdiff and automatic plan review using the
    shared launcher in the client's terminal environment.
  • Automatic review handles live session.execution.succeeded events for the
    displayed root session. Local atomic claims use the event ID, with markers
    older than one day removed when claiming.
  • Annotations return to the original session. Plan feedback includes the exact
    reviewed snapshot with line numbers, so it does not depend on a deleted
    temporary file. A clean exit sends no prompt and leaves plan mode unchanged.
  • /revdiff works from both home and an existing session.
  • The installer detects v1/v2 and handles migration of legacy registrations.
    V1 runtime files and shared launchers remain unchanged.
  • Adds regression tests, CI checks, and a Go PTY helper for runtime testing.

Refs #369 and #211.

Validation

  • TypeScript typecheck and 70 tests passed.
  • Runtime-tested with OpenCode 2.0.18, plus 2.0.0, 2.0.10, and 2.0.17.
  • Runtime coverage includes clean and annotated reviews from home, feedback
    session creation, automatic plan review and revision, and in-session commands.
  • Full Go suite with the race detector passed.
  • golangci-lint, ShellCheck, Prettier, and git diff --check passed.
  • The Go PTY helper was tested on macOS and cross-compiled for Linux amd64/arm64.

The runtime fixture runs the real OpenCode CLI in a PTY with deterministic
model, terminal-control, and revdiff fixtures.

Limitations

Review ownership is local-only and at-most-once: clients must share the same
state directory, and there is no takeover after a crash. The winning client
may be in a window the user is not currently looking at.

@LerikP
LerikP requested a review from umputun as a code owner October 4, 2026 09:02

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one blocking issue, the rest is small. The session rules from #369 are all in the code, except that v1 stays as it was (second item).

blocking

plugins/opencode/v2/launcher.ts:66-79, tui.ts:212-233, tui.ts:340 - when a review is cancelled (new execution or agent change in the reviewed session, session.deleted, plugin unload), the plugin kills the launcher's process group and removes the temp dir. On zellij, kitty, wezterm, cmux, ghostty, iTerm2, emacs and herdr revdiff is a child of the terminal, so the pane stays open with its --output dir gone: O shows "Flush failed", quit saves to revdiff history only, nothing reaches OpenCode, and with no notes flushed before the cancel there is no alert either.
Fix: keep the invalidation and the cancel of pending lookups, but a review still running at that point should either keep its reader until it ends and then show the notes in the "not delivered" alert, or be reported at cancel time as detached with the notes recoverable from revdiff history. README lines 68 and 70 should say whichever it is. Pls add a test where the fake revdiff runs outside the launcher's process group, the current one at launcher.test.ts:171-181 execs it inside the group and can't see this.

minor

plugins/opencode/setup.sh:25-26 - the v1 install now refuses when only opencode.jsonc exists. The old script created opencode.json next to it and installed, and that works on OpenCode 1.18: the global loader merges both files and picks up plugins/*.ts on its own. Fix: drop the refusal and change the case at setup.test.ts:486-507 to "jsonc untouched, install succeeds".

plugins/opencode/v2/tui.test.ts:287-342, :355 - nothing asserts that the client which lost the claim stays silent, and first.session.agent is a field of the fake that the plugin can't change, so that check can't fail. Fix: assert empty alerts and prompts on both clients after the competing emits and after the replay.

plugins/opencode/v2/tui.ts:264 - the command returns without a message when it runs from a subagent session view. Fix: throw there, so the existing toast path reports it, plus one test case.

plugins/opencode/v2/runtimepty/main.go:81 - golangci-lint 2.14 flags errors.As here (modernize, errorsastype). CI pins 2.12.2 and won't show it. Fix: errors.AsType[*exec.ExitError](waitErr), same as app/diff/diff.go:737.

plugins/opencode/v2/tui.ts:124 - the comment says the idle marker is absent from the SDK union, but SessionMessageIdle is in SessionMessageInfo at 2.0.18. Fix: drop the comment and the local IdleMarker type.

site/llms.txt:12, :63-65 - still describes the tool call and the plan-mode exit trigger, which is v1 only. Fix: same v1/v2 split as in README.

.github/workflows/ci.yml:55-74 - setup.test.ts needs jq and the new job has no install step. Fix: copy the install jq step from the build job.

Go helper

all Go source in this repo is under app/, and plugins/opencode/v2/runtimepty is a package main outside of it. Pls move it under app/ and update the go build path in runtime-check.ts. The runtime check itself is worth keeping.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last round's items check out. What is left is small.

installer

plugins/opencode/setup.sh:59-61 - a v2 install stops with "Invalid opencode.json." and copies nothing when the file has a comment or a trailing comma. OpenCode v2 reads opencode.json as JSONC, so that config is valid for it. Fix: when jq can't parse the file, print the same manual-cleanup notice as the opencode.jsonc branch, naming opencode.json, and go on to the copy.

plugins/opencode/setup.sh:20 - the v1 install asks for jq before anything else, but with no opencode.json it only runs printf. The old script installed there without jq. Fix: move the check into the if [[ -f "$config_file" ]] block at line 23, and README line 7 to match.

plugins/opencode/setup.sh:54, plugins/opencode/README.md:34, :45 - "experimental server plugin" and "early experimental v2 package" describe something that was never released. Fix: say what the check found, e.g. "$target contains index.* or server.*, which OpenCode would load as a server plugin".

tests

plugins/opencode/v2/tui.test.ts:728-746, :204-220, :748-781 - these can't fail. assert.fail in the mocked review is thrown inside run(), the catch there turns it into a toast, and nothing asserts on alerts. With the parentID check in planFor removed, or the throw at tui.ts:264, all of them still pass, and :216 matches the word "subagent" from the mock's own message. Fix: count launches in the mock and assert 0, assert empty alerts in the :728 cases, match only /root session/i at :216, and drop line 772 so the abort is the only thing that stops the launch.

plugins/opencode/v2/setup.test.ts:67 - the v2 install case checks tui.ts and the two scripts only, while the file list at setup.sh:78 is hardcoded. A new module imported from tui.ts and missing from that list breaks the installed plugin with green tests. Fix: assert launcher.ts and claims.ts too.

app/opencode-runtime-pty/main_test.go:21-28 - both tests re-exec the test binary and main() runs in the child only, so the package reports 0% coverage and lowers the coveralls total. Fix: call terminalBridge.run in-process and cancel the context instead of sending SIGTERM.

other

plugins/opencode/v2/package-lock.json:2250 - http-cache-semantics 4.2.0 has GHSA-ch52-4w7c-c8xp, fixed in 4.3.0. Fix: npm update http-cache-semantics.

app/opencode-runtime-pty - every other directory under app/ is a single lowercase word. Pls rename to match and update runtime-check.ts:211 and plugins/opencode/README.md:92.

@LerikP

LerikP commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, addressed in f9edaa8.

Installer

  • V2 now preserves opencode.json containing comments or trailing commas,
    prints the manual-cleanup notice naming that file, and continues installation.
  • V1 checks for jq only when opencode.json already exists; a fresh install
    works without it.
  • Replaced the “experimental” wording with a description of the detected
    index.* / server.* files.

Tests

  • Replaced swallowed assert.fail calls with launch counters and explicit
    zero-launch assertions. The ignored-session cases also require empty alerts.
  • Tightened the manual subagent error assertion to /root session/i.
  • Removed the agent-field mutation from the initial-lookup cancellation test.
    Temporarily disabling the guards/cancellation now makes these tests fail;
    the production code was restored afterward.
  • Installation tests explicitly check launcher.ts and claims.ts.

Go helper and dependency

  • Renamed the helper to app/ptybridge and updated the runtime build path
    and documentation.
  • The Go tests now call terminalBridge.run in-process and cancel its context.
    Package coverage is 64.9%.
  • Updated http-cache-semantics to 4.3.0.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, last round's items check out.

one thing left, not blocking: plugins/opencode/setup.sh:22-25 - the v1 install stops with "Invalid opencode.json or plugin array." and copies nothing when opencode.json has a comment or a trailing comma. OpenCode 1.18 reads that file as JSONC, and the old script updated the files there and left the config alone. Fix: same as the v2 branch at line 61. When jq empty fails, copy the files, skip the registration block at lines 37-46 and print a notice to make sure ./plugins/revdiff-plan-review.ts is in plugin.

up to you whether it goes in this PR, I can do it after the merge.

@umputun
umputun merged commit ea00f92 into umputun:master Oct 6, 2026
3 checks passed
umputun added a commit that referenced this pull request Oct 6, 2026
the v1 install stopped with "Invalid opencode.json or plugin array." and copied nothing when opencode.json had a comment or a trailing comma. OpenCode reads that file as JSONC, so the config is valid. The installer now copies the files, leaves the config byte-identical and prints a notice to check the registration by hand. A parseable config of the wrong shape still fails before any copy.

also documents app/ptybridge and the OpenCode plugin in CLAUDE.md and docs/ARCHITECTURE.md.

Related to #370
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.

2 participants