Repository navigation
Port the OpenCode plugin to the opencode v2 plugin API - #27
Conversation
opencode v2 rejects the v1 plugin shape ("Plugin must export a default
definition with an id and an effect or setup function"), so the plugin
failed to load on any opencode v2 install. v1 plugin implementations do
not run in v2, so this is a port, not a rename:
plugin/opencode/ is now a v2 plugin directory:
- index.ts (server part) — Plugin.define + ctx.event.subscribe instead of
a returned event hook. session.created (root sessions) still runs
`--on-start` to record the session (idempotent, so the TUI part's own
run for the banner is harmless). Exports now trigger on
session.execution.succeeded/failed/interrupted: v2 does not emit the
v1-era session.idle (kept in the subscription in case a release restores
it; the cursor makes a double export a no-op). The server runs one
plugin instance per location and every instance sees every session's
events, so exports are gated on the session's directory matching the
instance's location — otherwise N instances race on the cursor file and
the same turn is traced N times (observed live while porting). Messages
come from ctx.session.context (v2 flat shape) and the version from
ctx.app. The cursor file is unchanged, so upgrading mid-session keeps
its position.
- tui.ts (TUI part) — v2 has no server-side tui.showToast; the
ENABLED/PAUSED banner toast now lives here (context.ui.toast.show on
session.created, subagents skipped as before). Headless runs show no
toast but still record the session via the server part.
src/opencode.rs: normalize opencode v2's flat messages ({type, text /
content, model.id, tokens}; tool results inside each tool item's state)
into the same Claude-format rows as the v1 shape, with tool results
emitted as a user tool_result row so turns::build_turns attaches them.
The v1 {info, parts} path is untouched; both shapes can be mixed.
src/setup.rs: embed and install both parts as
~/.config/opencode/plugins/code-trace/ and remove the legacy single-file
plugin on install — opencode loads it alongside the new directory and its
load error masks the ported plugin.
Verified end to end against opencode v2.2.1: plugin loads (server+tui
active), sessions are recorded, and a headless `opencode run` session
exports exactly one turn to the binary.
PR Reviewer Guide 🔍(Review updated until commit 3a4b92a)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
docker compose's bake integration refuses to read /dev/null without an explicit fs.read allowance, so CI has failed at `docker compose up` with "additional privileges requested: pass --allow=fs.read=/dev/null" on every PR since November (first seen on the dependabot rustls bump — nothing to do with the PRs themselves). Default the secret to a committed empty file instead: equivalent for npm, no privilege needed.
Claude Code fires Stop hooks concurrently with writing the session transcript: the file can be missing when the hook starts and appear a few hundred milliseconds later. Observed with claude-code 2.1.198 in the Track 1 harness, where config-file-mode Stop hooks lost the race every time (scenario b) and env-mode hooks lost it intermittently — the hook bailed at the transcript existence check and the turn was never traced. Poll for up to 2s before giving up, before the state lock so concurrent invocations are not queued behind the wait. All 5 harness scenarios pass again locally (4/4 config-mode runs emit after the fix).
|
Persistent review updated to latest commit 3a4b92a |
|
Two extra commits after the first CI run (the
Both verified locally in the container harness before pushing; |
User description
Why
opencode v2 rejects the v1 plugin shape — the code-trace plugin fails to load with:
v1 plugin implementations do not run in v2, so this is a port, not a rename. The Rust side also needed the new message shape: without it the plugin loads but the binary silently emits degraded traces (no user text, no tool spans, no token usage).
What changed
plugin/opencode/is now a v2 plugin directory:index.ts(server part) —Plugin.define+ctx.event.subscribeinstead of a returnedeventhook.session.created(root sessions) still runs--on-startso sessions are recorded (idempotent — the TUI part's own--on-startfor the banner is harmless). Exports trigger onsession.execution.succeeded/failed/interrupted; opencode v2 does not emit the v1-erasession.idle(verified against v2.2.1 — it's kept in the subscription in case a release restores it; the cursor makes a double export a no-op). Because the server runs one plugin instance per location and every instance sees every session's events, exports are gated on the session's directory matching the instance's location — without the gate, N instances race on the cursor file and the same turn is traced N times (observed live while porting).tui.ts(TUI part) — v2 has no server-sidetui.showToast; the ENABLED/PAUSED banner toast now lives here. Headless runs show no toast but still record the session.src/opencode.rs— normalizes opencode v2's flat messages ({type, text/content, model.id, tokens}, tool results inside each tool item'sstate) into the same Claude-format rows as before, emitting tool results as a usertool_resultrow. The v1{info, parts}path is untouched; both shapes can be mixed. 10 new unit tests.src/setup.rs— embeds and installs both parts as~/.config/opencode/plugins/code-trace/and removes the legacy single-file plugin on install (opencode loads it alongside the new directory and its load error masks the ported plugin). New install tests including legacy-removal.README / AGENTS.md — install instructions and architecture notes updated.
Verification (against opencode v2.2.1, live)
code-trace local {server: true, tui: true} active(the v1 file produced the load error above).opencode runsession: recorded in the registry at creation, and exactly oneProcessed 1 turnsper run incode_trace.logafter the location gate.cargo test(232 tests) and clippy pass.The banner toast needs a quick manual check in a fresh TUI session (can't drive the TUI from a script).
Notes for reviewers
~/.local/share/code-trace/opencode_cursor.json) is unchanged, so upgrading mid-session keeps positions.get_rolefalls back to the top-leveltype), so nothing got worse while this PR waits — but the normalizer fix is needed for correct traces.code-trace setup --install-opencodeon each machine removes the legacy file and installs the directory (already deployed by hand on doug's machine).PR Type
Enhancement, Bug fix
Description
Port OpenCode plugin to v2 API (server + TUI parts)
Normalize opencode v2 flat messages in
src/opencode.rsInstall plugin directory and remove legacy v1 file
Wait briefly for Claude Code transcript at Stop hook
Diagram Walkthrough
File Walkthrough
1 files
Wait briefly for transcript at Stop hook4 files
Normalize opencode v2 flat messages into rowsInstall plugin directory and remove legacy v1 fileAdd v2 server plugin partAdd v2 TUI plugin part for banner toast1 files
Update install tests for plugin directory1 files
Remove legacy v1 single-file plugin3 files
Document opencode v2 plugin directoryUpdate OpenCode install and behavior docsClarify default npmrc empty file1 files
Use committed empty npmrc default1 files