Skip to content

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

Open
TheRealAlexV wants to merge 1 commit into
backnotprop:mainfrom
TheRealAlexV:feat/opencode-embedded-session-registry
Open

TheRealAlexV wants to merge 1 commit into
backnotprop:mainfrom
TheRealAlexV:feat/opencode-embedded-session-registry

Conversation

@TheRealAlexV

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
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.

1 participant