Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Load the OpenCode plugin under OpenCode 2 (#4018)

Worktrunk Bot8e420cb8c43c
diff --git a/dev/opencode-plugin.ts b/dev/opencode-plugin.ts
index bc198c008..4711464db 100644
--- a/dev/opencode-plugin.ts
+++ b/dev/opencode-plugin.ts
@@ -6,30 +6,125 @@
//
// Installed globally via: wt config plugins opencode install
// Or manually: copy to ~/.config/opencode/plugins/worktrunk.ts
+//
+// One file, two plugin runtimes. OpenCode 2 decodes the default export as
+// `{ id, setup }` and loads nothing else β€” the bare default-exported function
+// this file used to be is rejected without a message. OpenCode 1 reads
+// `{ id, server }` and never looks at `setup`; 1.16 is the floor that matters,
+// where the host began filtering events per plugin instance (the object shape
+// itself loads as far back as 1.14.19). Both ignore the other's key, so
+// a single installed file works either side of the version boundary.
+//
+// The types below are declared locally rather than imported from
+// `@opencode-ai/plugin`: its `Plugin` type means different things in the two
+// versions, and a lone file in the config directory has no package to resolve.
+
+import { execFile } from "node:child_process";
+import { promisify } from "node:util";
+
+const execute = promisify(execFile);
+
+const WORKING = "πŸ€–";
+const WAITING = "πŸ’¬";
+
+/**
+ * Run `wt config state marker …` in this plugin instance's worktree.
+ *
+ * `cwd` is load-bearing: the marker belongs to the worktree the session is in,
+ * not to whatever directory the host process happens to sit in.
+ */
+async function marker(directory: string, args: string[]): Promise<void> {
+ try {
+ await execute("wt", ["config", "state", "marker", ...args], { cwd: directory });
+ } catch {
+ // `wt` may be off PATH, or the directory may no longer be a worktree. An
+ // activity marker is not worth raising an error into the session over.
+ }
+}
+
+// --- OpenCode 2 -------------------------------------------------------------
-import type { Plugin } from "@opencode-ai/plugin";
+type SessionEvent = {
+ type: string;
+ location?: { directory: string };
+ data?: { status?: { type?: string } };
+};
-export default (async ({ $, directory }) => {
+type SetupContext = {
+ location?: { directory: string };
+ event?: { subscribe(options?: { signal?: AbortSignal }): AsyncIterable<SessionEvent> };
+};
+
+function setup(context: SetupContext) {
+ const directory = context.location?.directory;
+ const events = context.event;
+ // OpenCode 1.18 also calls `setup`, from a second loader that passes neither
+ // of these. Its `server` hook below drives the marker there, so bail quietly
+ // rather than throwing out of a plugin the host did load.
+ if (!directory || !events) return;
+
+ const controller = new AbortController();
+
+ const watcher = (async () => {
+ for await (const event of events.subscribe({ signal: controller.signal })) {
+ // The event envelope makes `location` optional; the session events below
+ // carry one, and skipping the rest keeps each instance to its own worktree.
+ if (event.location?.directory !== directory) continue;
+
+ switch (event.type) {
+ case "session.status":
+ // Status is `idle | busy | retry`. OpenCode 2 marks `session.idle`
+ // deprecated in favor of this, so read the status, not the event name.
+ await marker(directory, ["set", event.data?.status?.type === "idle" ? WAITING : WORKING]);
+ break;
+ case "session.idle":
+ await marker(directory, ["set", WAITING]);
+ break;
+ case "session.deleted":
+ await marker(directory, ["clear"]);
+ break;
+ }
+ }
+ })().catch((error: unknown) => {
+ if (!controller.signal.aborted) {
+ console.error("Worktrunk activity tracking stopped", error);
+ }
+ });
+
+ // Awaiting the watcher before clearing keeps an in-flight `set` from landing
+ // after the `clear` and leaving a marker behind.
+ return async () => {
+ controller.abort();
+ await watcher;
+ await marker(directory, ["clear"]);
+ };
+}
+
+// --- OpenCode 1 (1.16+) -----------------------------------------------------
+
+type ServerInput = { directory: string };
+type HookInput = { event: { type: string } };
+
+function server({ directory }: ServerInput) {
return {
- event: async ({ event }) => {
- // `$` is the host's process-global Bun shell, shared by every plugin in
- // the process. The promise-level `.cwd(directory)` on each command below
- // is load-bearing: it pins the command to the directory this plugin
- // instance was created for. Without it, the command runs in whatever the
- // process-wide cwd happens to be at spawn time. Do not "simplify" to the
- // instance-level `$.cwd(...)` β€” that mutates the shared default for every
- // plugin in the host process.
+ // OpenCode 1.16+ filters events to this plugin's directory before calling
+ // the hook, so there is nothing to match on here. 1.15.x does not β€” it fans
+ // every bus event to every plugin instance β€” so the marker there follows the
+ // worktree the instance was created for, whichever session is active.
+ event: async ({ event }: HookInput) => {
switch (event.type) {
case "session.status":
- await $`wt config state marker set ${'πŸ€–'} || true`.cwd(directory).quiet();
+ await marker(directory, ["set", WORKING]);
break;
case "session.idle":
- await $`wt config state marker set ${'πŸ’¬'} || true`.cwd(directory).quiet();
+ await marker(directory, ["set", WAITING]);
break;
case "session.deleted":
- await $`wt config state marker clear || true`.cwd(directory).quiet();
+ await marker(directory, ["clear"]);
break;
}
},
};
-}) satisfies Plugin;
+}
+
+export default { id: "worktrunk", setup, server };
diff --git a/docs/src/content/docs/claude-code.md b/docs/src/content/docs/claude-code.md
index ef8ed4e9e..fee79eb58 100644
--- a/docs/src/content/docs/claude-code.md
+++ b/docs/src/content/docs/claude-code.md
@@ -50,7 +50,7 @@ To remove the marketplace entry, run `wt config plugins codex uninstall`. Alread
wt config plugins opencode install
```
-This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it.
+This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it. The one file covers both plugin runtimes β€” OpenCode 2 loads its `setup` export, OpenCode 1.16 and later its `server` export.
### Gemini CLI
diff --git a/plugins/worktrunk/skills/worktrunk/reference/claude-code.md b/plugins/worktrunk/skills/worktrunk/reference/claude-code.md
index 15b6ce5d0..eeaa80fb6 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/claude-code.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/claude-code.md
@@ -46,7 +46,7 @@ To remove the marketplace entry, run `wt config plugins codex uninstall`. Alread
wt config plugins opencode install
```
-This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it.
+This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it. The one file covers both plugin runtimes β€” OpenCode 2 loads its `setup` export, OpenCode 1.16 and later its `server` export.
### Gemini CLI
diff --git a/skills/worktrunk/reference/claude-code.md b/skills/worktrunk/reference/claude-code.md
index 15b6ce5d0..eeaa80fb6 100644
--- a/skills/worktrunk/reference/claude-code.md
+++ b/skills/worktrunk/reference/claude-code.md
@@ -46,7 +46,7 @@ To remove the marketplace entry, run `wt config plugins codex uninstall`. Alread
wt config plugins opencode install
```
-This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it.
+This writes the activity-tracking plugin to OpenCode's global plugins directory, `~/.config/opencode/plugins/worktrunk.ts` (honoring `$OPENCODE_CONFIG_DIR` and `$XDG_CONFIG_HOME`). `wt config plugins opencode uninstall` removes it. The one file covers both plugin runtimes β€” OpenCode 2 loads its `setup` export, OpenCode 1.16 and later its `server` export.
### Gemini CLI
diff --git a/src/commands/config/opencode.rs b/src/commands/config/opencode.rs
index 9cf47b5e4..158145cc5 100644
--- a/src/commands/config/opencode.rs
+++ b/src/commands/config/opencode.rs
@@ -4,6 +4,8 @@
//! The plugin source (`dev/opencode-plugin.ts`) is embedded in the binary via
//! `include_str!()` and written under the OpenCode global-config directory (see
//! `opencode_plugins_dir` for the precedence rules) at `…/plugins/worktrunk.ts`.
+//! That one file exports both plugin shapes OpenCode has shipped, so the same
+//! install works on OpenCode 1 and OpenCode 2.
use std::path::PathBuf;