-
Notifications
You must be signed in to change notification settings - Fork 2
feat(opencode-plugin): give each OpenCode session its own wire identity #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // opencode plugin: per-session wire identities. | ||
| // Install: copy (or symlink) this file into ~/.config/opencode/plugin/ — | ||
| // opencode auto-loads every *.js there; no opencode.json entry needed. | ||
| // opencode forwards no session-id env var to spawned MCP servers | ||
| // (SlanchaAi/wire#92). opencode fires session.created ~0.5s BEFORE booting | ||
| // local MCP servers, so the event hook learns the session id in time to | ||
| // stamp WIRE_SESSION_ID=opencode-<sessionID> — birth identity == resume | ||
| // identity for every session. | ||
| // Priority: exported WIRE_SESSION_ID > first top-level session.created > | ||
| // `-s <id>` / `-c` argv resolution > fresh uuid. The lookup only ever | ||
| // yields the correct or a fresh key, never a foreign session's identity. | ||
| import { randomUUID } from "node:crypto" | ||
| import { execFileSync } from "node:child_process" | ||
| import { existsSync, realpathSync } from "node:fs" | ||
| import os from "node:os" | ||
| import path from "node:path" | ||
|
|
||
| function explicitSessionId(argv) { | ||
| for (let i = 0; i < argv.length; i++) { | ||
| const a = argv[i] | ||
| if ((a === "-s" || a === "--session") && argv[i + 1] && !argv[i + 1].startsWith("-")) { | ||
| return argv[i + 1] | ||
| } | ||
| if (a.startsWith("--session=")) return a.slice("--session=".length) | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| function tryRealpath(p) { | ||
| try { | ||
| return realpathSync(p) | ||
| } catch { | ||
| return p | ||
| } | ||
| } | ||
|
|
||
| function lastSessionId(cwd) { | ||
| try { | ||
| const db = path.join( | ||
| process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"), | ||
| "opencode", | ||
| "opencode.db", | ||
| ) | ||
| if (!existsSync(db)) return null | ||
| const esc = (s) => s.replace(/'/g, "''") | ||
| const dirs = [...new Set([cwd, tryRealpath(cwd)])].map(esc).join("','") | ||
| const out = execFileSync( | ||
| "sqlite3", | ||
| [ | ||
| "-readonly", | ||
| db, | ||
| `select id from session where directory in ('${dirs}') and parent_id is null order by time_updated desc limit 1;`, | ||
| ], | ||
| { encoding: "utf8", timeout: 2000 }, | ||
| ) | ||
| const id = out.trim() | ||
| return /^ses_/.test(id) ? id : null | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export default async () => { | ||
| let wireCfg = null | ||
| let stampedByEvent = false | ||
|
|
||
| return { | ||
| config: (cfg) => { | ||
| const wire = cfg.mcp?.wire | ||
| if (!wire || wire.type !== "local") return | ||
| wireCfg = wire | ||
| if (process.env.WIRE_SESSION_ID) return | ||
| const argv = process.argv | ||
| let key = explicitSessionId(argv) | ||
| if (!key && argv.some((a) => a === "-c" || a === "--continue")) { | ||
| if (!argv.includes("--fork")) key = lastSessionId(process.cwd()) | ||
| } | ||
| wire.environment = { | ||
| ...wire.environment, | ||
| WIRE_SESSION_ID: key ? `opencode-${key}` : `opencode-${randomUUID()}`, | ||
| } | ||
| }, | ||
| event: ({ event }) => { | ||
| if (stampedByEvent || !wireCfg || process.env.WIRE_SESSION_ID) return | ||
| if (event?.type !== "session.created" && event?.type !== "session.updated") return | ||
| const info = event?.properties?.info | ||
| if (!info?.id || !/^ses_/.test(info.id)) return | ||
| if (info.parentID) return | ||
| // For resume flows trust only the id argv already named, if any. | ||
| const argvKey = explicitSessionId(process.argv) | ||
| if (argvKey && info.id !== argvKey) return | ||
| wireCfg.environment = { | ||
| ...wireCfg.environment, | ||
| WIRE_SESSION_ID: `opencode-${info.id}`, | ||
| } | ||
| stampedByEvent = true | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: SlanchaAI/wire
Length of output: 16309
🏁 Script executed:
Repository: SlanchaAI/wire
Length of output: 2102
Isolate Wire identities for
opencode run --attach.When one process serves multiple top-level sessions,
stampedByEventkeeps the first session’sWIRE_SESSION_IDand ignores later session events. Later sessions can therefore use the first session’s Wire identity, mailbox, and pairing state. Scope the MCP server per session, or disable this plugin for--attachuntil OpenCode supplies per-session context.🤖 Prompt for AI Agents