worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

OpenCode plugin: activity markers never fire on OpenCode 2.3.x (tracked events dropped; replacements carry no location)

Open ava-terra opened this issue 2 weeks ago • 3 comments

Summary

With OpenCode 2.3.x, the worktrunk OpenCode plugin's activity markers (🤖 working / 💬 waiting in wt list) never appear. Three issues in the plugin's event handling contribute; all verified against a live OpenCode 2.3.2 event stream. Related: #4014 (where the OpenCode 2 activity plugin landed).

Environment

  • worktrunk 0.82.0 (plugin as shipped by wt config plugins opencode install)
  • OpenCode 2.3.0 and 2.3.2 (both affected), Linux
  • Plugin is loaded and otherwise active: the worktree strategy works (worktrees created through OpenCode report strategy: "worktrunk")

Finding 1: the tracked events no longer exist

The plugin's OpenCode-2 watcher switches on session.status (reading data.status.type) and session.idle. OpenCode 2.3 emits neither. A capture of the authenticated /api/event stream across a session turn shows these session event types and no others:

session.execution.started / succeeded
session.step.started / streamed / first-token / ended
session.tool.called / input.started / input.ended / progress / success
session.text.started / delta / ended
session.reasoning.started / delta / ended
session.usage.updated, session.inbox.*, session.viewed, ...

session.status and session.idle are absent entirely.

Finding 2: the replacement events carry no location

session.execution.started / succeeded arrive with only a sessionID:

{"type":"session.execution.started",
 "data":{"sessionID":"ses_..."},
 "durable":{"aggregateID":"ses_...","seq":43042,"version":1}}

Every other session event carries location.directory (verified equal to the session's current worktree). The plugin filters with

if (event.location?.directory !== directory) continue;

so even a handler for session.execution.* would be skipped — the events cannot be attributed to a worktree by location alone.

Workaround verified working: resolve the directory from the plugin client, ctx.session.get({ sessionID }) → location.directory (present on Session.Info), then wt config state marker set with that directory as cwd. With that patch, markers appear and update at each turn boundary (verified live on 2.3.2).

Finding 3: the plugin is instantiated twice per location

setup runs twice for the same ctx.location.directory (two loaders). Both instances subscribe and both call wt config state marker set on the same branch; the concurrent git config writes race and one loses with exit code 255 (git config worktrunk.state.<branch>.marker ... failed). A per-directory refcount (share one watcher, teardown at zero) removes the race.

Suggested fix

The patch below implements all three (verified on 2.3.2):

  1. Handle session.execution.started → 🤖 and succeeded / failed → 💬 before the location filter, resolving the worktree via ctx.session.get({ sessionID }).location.directory.
  2. Keep the session.status / session.idle cases for hosts that still emit them.
  3. Refcount the watcher per directory.
Verified patch (against the 0.82.0-shipped plugin)
--- a/worktrunk.ts (as shipped by wt 0.82.0)
+++ b/worktrunk.ts (patched, verified on OpenCode 2.3.2)
@@ -32,6 +32,9 @@
 const WORKING = "🤖";
 const WAITING = "💬";
 
+// Active watcher count per directory — see setup().
+const watchers = new Map<string, number>();
+
 // The worktrunk binary, chosen as `plugins/worktrunk/hooks/wt.sh` chooses it.
@@ -81,7 +84,7 @@
 type SessionEvent = {
   type: string;
   location?: { directory: string };
-  data?: { status?: { type?: string } };
+  data?: { status?: { type?: string }; sessionID?: string };
 };
@@ -101,6 +104,9 @@
 type SetupContext = {
   location?: { directory: string };
+  session?: {
+    get(input: { sessionID: string }): Promise<{ location?: { directory?: string } }>;
+  };
   event?: { subscribe(options?: { signal?: AbortSignal }): AsyncIterable<SessionEvent> };
@@ -189,6 +195,23 @@
   },
 };
 
+// `session.execution.started/succeeded` replaced `session.status` in OpenCode
+// 2.3 and arrive without a location, so the session's own record supplies the
+// directory to mark.
+async function markExecution(context: SetupContext, event: SessionEvent): Promise<void> {
+  const sessionID = event.data?.sessionID;
+  if (!sessionID) return;
+  try {
+    const session = await context.session?.get({ sessionID });
+    const dir = session?.location?.directory;
+    if (!dir) return;
+    await marker(dir, ["set", event.type === "session.execution.started" ? WORKING : WAITING]);
+  } catch {
+    // Resolving the session is best-effort: a missing session or a host
+    // without the client API just leaves the marker untouched.
+  }
+}
+
 async function setup(context: SetupContext) {
@@ -201,15 +224,39 @@
   if (!directory || !events) return;
 
+  // The host loads this file twice per location (two plugin loaders). Share
+  // one watcher per directory so instances don't race on the same marker.
+  watchers.set(directory, (watchers.get(directory) ?? 0) + 1);
+  if (watchers.get(directory)! > 1) {
+    return async () => {
+      watchers.set(directory, (watchers.get(directory) ?? 1) - 1);
+    };
+  }
   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.
+      // `session.execution.*` events carry only a sessionID — no location —
+      // so resolve the session's directory and mark that worktree. Every
+      // other event carries its location; skipping non-matching ones keeps
+      // each instance to its own worktree.
+      if (event.type.startsWith("session.execution.")) {
+        await markExecution(context, event);
+        continue;
+      }
       if (event.location?.directory !== directory) continue;
 
       switch (event.type) {
+        // OpenCode 2.3 dropped `session.status`/`session.idle` for
+        // `session.execution.*` (verified against the live event stream on
+        // 2.3.2); the old cases stay for hosts that still emit them.
+        case "session.execution.started":
+          await marker(directory, ["set", WORKING]);
+          break;
+        case "session.execution.succeeded":
+        case "session.execution.failed":
+          await marker(directory, ["set", WAITING]);
+          break;
         case "session.status":
@@ -232,6 +279,10 @@
   return async () => {
+    const remaining = (watchers.get(directory) ?? 1) - 1;
+    watchers.set(directory, remaining);
+    if (remaining > 0) return;
+    watchers.delete(directory);
     controller.abort();
     await watcher;
     await marker(directory, ["clear"]);
   };

ava-terra avatar Nov 19 '26 17:11 ava-terra

Thanks for the detailed capture and the patch. Findings 1 and 2 hold against OpenCode v2.3.2's source. #4850 fixes them, based on your approach. CI is green at its head. It is waiting on review, and a maintainer decides whether it merges.

How #4850 differs from your patch:

  • Marks only its own worktree. After session.get, it marks only when the session's location.directory is this plugin instance's directory. Your version marks whatever directory the session reports, so with several worktrees open, every instance would write every session's marker.
  • Skips subagent sessions. Sessions with a parentID are ignored, so a subagent finishing doesn't flip a working parent to 💬.
  • Handles one more event. session.execution.interrupted also sets 💬, alongside succeeded and failed.

Finding 3, the duplicate instance per directory, is left out. Both instances write the same value, and the plugin already swallows the losing write's error, so the marker still ends up correct. I also couldn't confirm that a module-level map is shared between OpenCode's two loaders. If you see a wrong marker that traces back to the duplicate instances, rather than just the logged 255, that would justify a follow-up.

I tested it by running the plugin under Node with a fake OpenCode context. It hasn't been tested inside a live OpenCode 2.3 session. If you can install the plugin from the fix/issue-4849 branch and confirm the markers change at each turn boundary, that would close that gap.

worktrunk-bot avatar Nov 19 '26 17:11 worktrunk-bot

Confirmed #4850 in a live session: OpenCode 2.3.0 on macOS, with dev/opencode-plugin.ts from main (5e2106c70) installed globally. I ran a session in one worktree. The marker was 🤖 for the whole turn, including an 8-second shell call, and switched to 💬 when the turn ended. No other worktree got a marker, even though plugin instances were loaded in two other directories at the time. With the 0.81.0 plugin, the same session set no marker at all.

cbovis-phantom avatar Nov 20 '26 16:11 cbovis-phantom

Thanks for testing it live. That closes the gap I flagged: with the fix on main, the marker shows 🤖 for the whole turn, switches to 💬 when the turn ends, and stays on its own worktree. #4850 is merged, so I'm closing this as fixed. It will ship in the next release.

If the duplicate plugin instance (Finding 3) ever leaves a wrong marker behind, as opposed to just logging the 255 error, please open a new issue.

worktrunk-bot avatar Nov 20 '26 16:11 worktrunk-bot