Skip to content

feat(opencode-plugin): register embedded sessions for discovery - #1567

Merged
backnotprop merged 8 commits into
backnotprop:mainfrom
TheRealAlexV:feat/opencode-embedded-session-registry
Oct 28, 2026
Merged

backnotprop merged 8 commits into
backnotprop:mainfrom
TheRealAlexV:feat/opencode-embedded-session-registry

Conversation

@TheRealAlexV

Copy link
Copy Markdown
Contributor

Problem

Sessions started by the @plannotator/opencode plugin's embedded runtime are invisible to discovery. registerSession() is only called from the compiled CLI (apps/hook/server/index.ts), so plannotator sessions — and any external tooling that wants to list or route live sessions (for example a self-hosted multi-session hub for remote/headless use) — cannot see embedded plan/review/annotate sessions. This is the discovery half of the multi-session problem tracked in #899.

There is a second constraint: the embedded runtime hosts several concurrent servers inside the single OpenCode process, so a pid-keyed registration would be wrong even if it existed — every session would share one file, the last one would win, and one session's teardown would delete another's entry.

Change

  • packages/server/sessions.ts — add an optional SessionInfo.id used as the registry file key (falls back to the pid, so CLI behavior is unchanged), a sessionKey() helper, and let unregisterSession() take a string key. listSessions() is untouched: it already scans *.json and reclaims entries whose pid is gone.
  • apps/opencode-plugin/session-registry.ts (new) — registerEmbeddedSession() / unregisterEmbeddedSession() helpers that key sessions by <pid>-<port>.
  • apps/opencode-plugin/embedded.ts — register/unregister embedded plan reviews.
  • apps/opencode-plugin/commands.ts — register/unregister embedded review, annotate, and annotate-last sessions.
  • packages/server/sessions.test.ts (new) — id-vs-pid keying, two concurrent sessions in one process, targeted unregister, and stale-entry cleanup.

Why <pid>-<port>

Each live embedded server listens on its own port, so <pid>-<port> is unique per session, while the embedded pid still drives the existing liveness check (process.kill(pid, 0) in listSessions) for stale-entry cleanup. No format change for CLI sessions — they pass no id, so the key stays String(pid).

Testing

  • bun test packages/server/sessions.test.ts apps/opencode-plugin/commands.test.ts → 16 pass, 0 fail
  • bunx tsc --noEmit -p packages/server/tsconfig.json → clean
  • bun build apps/opencode-plugin/index.ts --target node --external @opencode-ai/plugin → bundles

No behavior change for CLI sessions; embedded sessions simply become discoverable.

Relates to #899.

Embedded plan/review/annotate sessions never reached the session registry: registerSession() is only called from the compiled CLI (apps/hook/server/index.ts), so `plannotator sessions` and any external discovery tool cannot see them.

The embedded runtime hosts several concurrent servers inside one OpenCode process, so a pid-only key would collide. Add an optional SessionInfo.id used as the registry file key (defaults to the pid, backward compatible) and key embedded sessions by <pid>-<port>.

- packages/server/sessions.ts: optional id + sessionKey(); unregisterSession accepts a string key.
- apps/opencode-plugin/session-registry.ts: register/unregister helpers.
- apps/opencode-plugin/{embedded,commands}.ts: register plan/review/annotate.
- packages/server/sessions.test.ts: coverage for id keying and concurrency.

Relates to backnotprop#899.
# Conflicts:
#	apps/opencode-plugin/commands.ts
#	packages/server/sessions.test.ts
#	packages/server/sessions.ts
…updates

The embedded entries sat in sessions/ beside the binary's, so auto-update
and the Inbox update dialog (and every released binary) would count a live
OpenCode process as "another Plannotator is open" and defer the update for
as long as OpenCode stays open.

- Move them to sessions/opencode/<pid>-<port>.json, tagged host: "opencode".
  listSessions/listLiveSessions read only the top folder's *.json, as every
  released binary does, so no version ever sees them.
- otherOpenSessions and the update dialog also skip any entry with host.
- decideListed (review, annotate, last) and submit_plan's finally remove the
  entry on decision, throw, abort and timeout; process exit removes the rest.
- listInProcessSessions drops and deletes an entry whose pid is dead, whose
  port no longer answers, or that cannot be read (signal deaths, missed
  removals). No signal listener is added to OpenCode's process.
- plannotator sessions lists them with the rest, marked (in OpenCode).
- Drop the id/sessionKey registry key: the subfolder makes it unneeded.
…process is gone

A host killed by a signal left its entry until someone ran plannotator
sessions; the next registration now deletes dead-pid entries (no port probe).
…uess for commands

- decideListed removes the entry before server.stop(), so a stop that throws
  can't leave it behind (as the plan review already did).
- listOpenCodeSession no longer runs project detection in OpenCode's cwd: a
  command passes the project it detected, or it is listed as unknown (a
  /plannotator-review <dir> was mislabeled). Only the plan review detects.
…ther user

kill(pid, 0) answers EPERM for a process that exists under another user;
the sweep now deletes only entries whose process does not exist. The test
also checks that live entries survive a registration.
@backnotprop
backnotprop merged commit 1097e52 into backnotprop:main Oct 28, 2026
29 checks passed
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