Skip to content

Commit 6b8b1cf

Browse files
committed
fix: resolve serverUrl lazily to get the actual server port
The serverUrl getter on the plugin input returns a fallback (http://localhost:4096) until the HTTP server has bound to a port. Destructuring it at plugin init time captures the stale fallback, causing `opencode attach` to connect to the wrong port. Read serverUrl lazily inside the session.created handler so the getter runs after the server is up. Also check OPENCODE_SERVER_URL env var first as a future-proof source (anomalyco/opencode#9099).
1 parent 9448094 commit 6b8b1cf

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

‎src/index.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,33 @@ import {
1212
type SplitDirection,
1313
} from "./cmux.js"
1414

15-
const plugin: Plugin = async ({ client, $, serverUrl }) => {
15+
const plugin: Plugin = async (input) => {
16+
const { client, $ } = input
1617
const pendingPermissions = new Set<string>()
1718
const pendingQuestions = new Set<string>()
1819

1920
const originalSurfaceId = process.env.CMUX_SURFACE_ID
2021

21-
let resolvedServerUrl = ""
22-
let splitsEnabled = false
23-
try {
24-
const raw = serverUrl?.toString() ?? ""
25-
const parsed = new URL(raw)
26-
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
27-
if (port !== "0") {
22+
// Resolve the server URL lazily (on each call) instead of at init time.
23+
// `serverUrl` is a getter on the plugin input that returns a fallback
24+
// (http://localhost:4096) until the HTTP server has actually bound to a
25+
// port. Reading it later — when a subagent split is needed — gives the
26+
// getter time to return the real URL.
27+
// See: https://github.com/anomalyco/opencode/issues/9099
28+
function resolveServerUrl(): string | null {
29+
try {
30+
const raw =
31+
process.env.OPENCODE_SERVER_URL ?? input.serverUrl?.toString() ?? ""
32+
const parsed = new URL(raw)
33+
const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80")
34+
if (port === "0") return null
2835
if (parsed.hostname === "0.0.0.0" || parsed.hostname === "[::]") {
2936
parsed.hostname = "localhost"
3037
}
31-
resolvedServerUrl = parsed.toString().replace(/\/$/, "")
32-
splitsEnabled = true
38+
return parsed.toString().replace(/\/$/, "")
39+
} catch {
40+
return null
3341
}
34-
} catch {
35-
// swallow errors silently
3642
}
3743

3844
const activeSplits = new Map<string, string>()
@@ -109,7 +115,8 @@ const plugin: Plugin = async ({ client, $, serverUrl }) => {
109115

110116
if (e.type === "session.created") {
111117
const info = e.properties.info
112-
if (info?.parentID && splitsEnabled) {
118+
const url = info?.parentID ? resolveServerUrl() : null
119+
if (info?.parentID && url) {
113120
await enqueueSplitOp(async () => {
114121
if (activeSplits.has(info.id)) return
115122

@@ -145,7 +152,7 @@ const plugin: Plugin = async ({ client, $, serverUrl }) => {
145152
activeSplits.set(info.id, surfaceId)
146153
agentCount++
147154

148-
const attachCmd = `opencode attach ${resolvedServerUrl} --session ${info.id}`
155+
const attachCmd = `opencode attach ${url} --session ${info.id}`
149156
await sendToSurface($, surfaceId, attachCmd)
150157
await sendKeyToSurface($, surfaceId, "enter")
151158

0 commit comments

Comments
 (0)