aft icon indicating copy to clipboard operation
aft copied to clipboard

OpenCode 2: the plugin event hook opens a route to its own location root for a session of another location when the session lookup has no result

Open iceteaSA opened this issue 3 weeks ago • 2 comments

Summary

On OpenCode 2 the plugin runs one time for each open location. The event hook of each plugin instance does not filter events to the sessions of its location. On session.idle it looks up the directory of the session. If the lookup has no result, each instance uses its own location directory. Thus one session gets a route to the project root of each open location.

Code (v0.64.0)

packages/opencode-plugin/src/index.ts:894-906:

const sessionDir =
  (await getSessionDirectory(input.client, sessionID, input.directory)) ?? input.directory;
if (!isProjectEnabled(sessionDir)) return;
await handleIdleBgCompletions({ ctx, directory: sessionDir, sessionID, client: input.client });

getSessionDirectory (shared/session-directory.ts:104-118) gives null when the lookup is not available or has no result. The fallback input.directory is the directory of the plugin instance, not of the session. handleIdleBgCompletions then opens a route and a background subscription for (sessionDir, sessionID).

What I saw

  • A host plugin makes hidden child sessions and removes each one in less than a second. For these sessions the lookup has no result.
  • With about 20 open locations, one such session got about 100 routes to about 20 project roots in one minute. The module log shows subc attach: route … bound to root <dir> for each location directory, and a bg subscription for that session on each root.
  • One open location had /tmp as its directory. Thus a session that did not belong to /tmp made AFT set /tmp as a project root, with the cost in #1428.

When the lookup succeeds, each instance opens a route to the correct root of the session. These are duplicates, but they go to the correct root.

Requests

  1. When getSessionDirectory gives null in the event hook, do not open a route. An alternative is to use the fallback only in the location that owns the session.
  2. Filter session.idle (and the other session events) to the sessions of the location of the plugin instance.

Related: #1428.

iceteaSA avatar Nov 13 '26 09:11 iceteaSA

Thanks, your diagnosis is right. When the session lookup has no answer, the event hook falls back to the plugin instance's own folder, so every instance that receives the event opens a route to its own location, which matches the ~20 roots you saw.

The fix: an instance acts only on sessions of its own location, and a session whose lookup has no answer opens no route at all. With the #1428 fix, a location at /tmp also no longer gets indexed.

aft-alfonso[bot] avatar Nov 13 '26 09:11 aft-alfonso[bot]

Fixed in v0.65.0, released today: OpenCode 2 no longer opens AFT routes in unrelated folders for another location's sessions. Thanks for the report. If you still see it after updating, reply here and we'll reopen.

aft-alfonso[bot] avatar Nov 14 '26 10:11 aft-alfonso[bot]