Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(opencode): dead hook exports + missing unix-socket transport killed all statuses

enesd6f6af4ae87a
diff --git a/src/core/agents/hooks/opencode.test.ts b/src/core/agents/hooks/opencode.test.ts
index ff327ce28..a5ed7fbdb 100644
--- a/src/core/agents/hooks/opencode.test.ts
+++ b/src/core/agents/hooks/opencode.test.ts
@@ -54,6 +54,193 @@ describe('opencode plugin install', () => {
})
})
+// Execute the generated plugin body against SDK-shaped inputs. opencode delivers bus
+// events (session.created/idle/error, message.updated, permission.*) ONLY through the
+// `event` catch-all hook — a hook keyed by the event name itself is never called (the
+// original bug: every handler was dead, so no status ever reached the hook server).
+describe('generated plugin behavior (executed)', () => {
+ let posts: Array<{ url: string; payload: Record<string, unknown>; nodeId: string }>
+
+ async function loadHooks(): Promise<Record<string, (input: unknown) => Promise<void>>> {
+ posts = []
+ vi.stubGlobal('fetch', (url: string, init: { body: string }) => {
+ const params = new URLSearchParams(init.body)
+ posts.push({
+ url,
+ nodeId: params.get('nodeId') ?? '',
+ payload: JSON.parse(params.get('payload') ?? '{}')
+ })
+ return Promise.resolve(new Response())
+ })
+ vi.stubEnv('NODETERM_NODE_ID', 'node-1')
+ vi.stubEnv('NODETERM_HOOK_PORT', '43210')
+ vi.stubEnv('NODETERM_HOOK_TOKEN', 'tok')
+ vi.stubEnv('NODETERM_HOOK_ENDPOINT', '')
+ const file = path.join(tmp, `plugin-under-test-${Math.random().toString(36).slice(2)}.mjs`)
+ fs.writeFileSync(file, buildOpencodePlugin())
+ const mod = await import(/* @vite-ignore */ `file://${file}`)
+ return mod.NodetermStatus()
+ }
+
+ afterEach(() => {
+ vi.unstubAllEnvs()
+ vi.unstubAllGlobals()
+ })
+
+ it('forwards bus events through the `event` catch-all with the wire-contract names', async () => {
+ const hooks = await loadHooks()
+ expect(typeof hooks.event).toBe('function')
+
+ await hooks.event({ event: { type: 'session.created', properties: { info: { id: 'ses_1' } } } })
+ await hooks.event({ event: { type: 'session.idle', properties: { sessionID: 'ses_1' } } })
+ await hooks.event({ event: { type: 'session.error', properties: { sessionID: 'ses_1' } } })
+ await hooks.event({ event: { type: 'permission.updated', properties: { id: 'perm1', sessionID: 'ses_1' } } })
+ await hooks.event({
+ event: { type: 'permission.replied', properties: { sessionID: 'ses_1', permissionID: 'perm1', response: 'once' } }
+ })
+
+ expect(posts.map((p) => p.payload)).toEqual([
+ { event: 'session.created', sessionID: 'ses_1' },
+ { event: 'session.idle', sessionID: 'ses_1' },
+ { event: 'session.error', sessionID: 'ses_1' },
+ { event: 'permission.asked', sessionID: 'ses_1' },
+ { event: 'permission.replied', sessionID: 'ses_1' }
+ ])
+ expect(posts[0].url).toBe('http://127.0.0.1:43210/hook/opencode')
+ expect(posts[0].nodeId).toBe('node-1')
+ })
+
+ it('forwards message.updated only for user messages (turn start)', async () => {
+ const hooks = await loadHooks()
+ await hooks.event({
+ event: { type: 'message.updated', properties: { info: { id: 'm1', sessionID: 'ses_1', role: 'user' } } }
+ })
+ await hooks.event({
+ event: { type: 'message.updated', properties: { info: { id: 'm2', sessionID: 'ses_1', role: 'assistant' } } }
+ })
+ expect(posts.map((p) => p.payload)).toEqual([{ event: 'message.updated', sessionID: 'ses_1', role: 'user' }])
+ })
+
+ it('ignores unrelated bus events (token-stream deltas never reach the hook server)', async () => {
+ const hooks = await loadHooks()
+ await hooks.event({ event: { type: 'message.part.delta', properties: {} } })
+ await hooks.event({ event: { type: 'session.updated', properties: { info: { id: 'ses_1' } } } })
+ expect(posts).toEqual([])
+ })
+
+ it('posts tool.execute.before from the real plugin hook of that name', async () => {
+ const hooks = await loadHooks()
+ await hooks['tool.execute.before']({ tool: 'bash', sessionID: 'ses_1', callID: 'c1' })
+ expect(posts.map((p) => p.payload)).toEqual([{ event: 'tool.execute.before', sessionID: 'ses_1' }])
+ })
+})
+
+// SSH hosts advertise a UNIX SOCKET (NODETERM_HOOK_SOCK), not a TCP port — the endpoint
+// file on a remote host has no PORT line at all. The POSIX managed script posts with
+// `curl --unix-socket`; the plugin must speak the socket too or every opencode status
+// on an SSH project silently vanishes (fetch can't do unix sockets in Node — node:http
+// socketPath is the fallback; under Bun (opencode's runtime) fetch takes `unix`).
+describe('generated plugin unix-socket transport', () => {
+ let sockDir: string
+ beforeEach(() => {
+ sockDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-oc-sock-'))
+ })
+ afterEach(() => {
+ vi.unstubAllEnvs()
+ vi.unstubAllGlobals()
+ fs.rmSync(sockDir, { recursive: true, force: true })
+ })
+
+ async function importPlugin(): Promise<Record<string, (input: unknown) => Promise<void>>> {
+ const file = path.join(tmp, `plugin-sock-${Math.random().toString(36).slice(2)}.mjs`)
+ fs.writeFileSync(file, buildOpencodePlugin())
+ const mod = await import(/* @vite-ignore */ `file://${file}`)
+ return mod.NodetermStatus()
+ }
+
+ it('posts over the unix socket via node:http when NODETERM_HOOK_SOCK is set (no port)', async () => {
+ const { createServer } = await import('node:http')
+ const sock = path.join(sockDir, 'hook.sock')
+ const received: Array<{ url: string; token: string; body: string }> = []
+ const server = createServer((req, res) => {
+ let body = ''
+ req.on('data', (c: Buffer) => (body += c))
+ req.on('end', () => {
+ received.push({ url: req.url ?? '', token: String(req.headers['x-nodeterm-hook-token']), body })
+ res.end('ok')
+ })
+ })
+ await new Promise<void>((r) => server.listen(sock, r))
+ try {
+ vi.stubEnv('NODETERM_NODE_ID', 'node-ssh')
+ vi.stubEnv('NODETERM_HOOK_SOCK', sock)
+ vi.stubEnv('NODETERM_HOOK_TOKEN', 'socktok')
+ vi.stubEnv('NODETERM_HOOK_PORT', '')
+ vi.stubEnv('NODETERM_HOOK_ENDPOINT', '')
+ const hooks = await importPlugin()
+ await hooks.event({ event: { type: 'session.idle', properties: { sessionID: 'ses_ssh' } } })
+ await vi.waitFor(() => expect(received.length).toBe(1))
+ expect(received[0].url).toBe('/hook/opencode')
+ expect(received[0].token).toBe('socktok')
+ const params = new URLSearchParams(received[0].body)
+ expect(params.get('nodeId')).toBe('node-ssh')
+ expect(JSON.parse(params.get('payload') ?? '{}')).toEqual({ event: 'session.idle', sessionID: 'ses_ssh' })
+ } finally {
+ server.close()
+ }
+ })
+
+ it('reads NODETERM_HOOK_SOCK from the live endpoint file (restart handoff) and prefers it over a TCP port', async () => {
+ const { createServer } = await import('node:http')
+ const sock = path.join(sockDir, 'hook2.sock')
+ const received: string[] = []
+ const server = createServer((req, res) => {
+ req.resume()
+ req.on('end', () => {
+ received.push(req.url ?? '')
+ res.end('ok')
+ })
+ })
+ await new Promise<void>((r) => server.listen(sock, r))
+ const tcpFetch = vi.fn(() => Promise.resolve(new Response()))
+ vi.stubGlobal('fetch', tcpFetch)
+ try {
+ const envFile = path.join(sockDir, 'hook-endpoint.env')
+ fs.writeFileSync(envFile, `NODETERM_HOOK_SOCK=${sock}\nNODETERM_HOOK_TOKEN=filetok\nNODETERM_HOOK_VERSION=1\n`)
+ vi.stubEnv('NODETERM_NODE_ID', 'node-ssh')
+ vi.stubEnv('NODETERM_HOOK_ENDPOINT', envFile)
+ vi.stubEnv('NODETERM_HOOK_PORT', '59999') // stale env port — socket from the file must win
+ vi.stubEnv('NODETERM_HOOK_TOKEN', 'stale')
+ vi.stubEnv('NODETERM_HOOK_SOCK', '')
+ const hooks = await importPlugin()
+ await hooks.event({ event: { type: 'session.idle', properties: { sessionID: 'ses_ssh' } } })
+ await vi.waitFor(() => expect(received.length).toBe(1))
+ expect(tcpFetch).not.toHaveBeenCalled()
+ } finally {
+ server.close()
+ }
+ })
+
+ it('uses Bun fetch with the `unix` option when running under Bun', async () => {
+ const calls: Array<{ url: string; init: Record<string, unknown> }> = []
+ vi.stubGlobal('Bun', {})
+ vi.stubGlobal('fetch', (url: string, init: Record<string, unknown>) => {
+ calls.push({ url, init })
+ return Promise.resolve(new Response())
+ })
+ vi.stubEnv('NODETERM_NODE_ID', 'node-ssh')
+ vi.stubEnv('NODETERM_HOOK_SOCK', '/tmp/some.sock')
+ vi.stubEnv('NODETERM_HOOK_TOKEN', 'tok')
+ vi.stubEnv('NODETERM_HOOK_PORT', '')
+ vi.stubEnv('NODETERM_HOOK_ENDPOINT', '')
+ const hooks = await importPlugin()
+ await hooks.event({ event: { type: 'session.idle', properties: { sessionID: 'ses_ssh' } } })
+ expect(calls.length).toBe(1)
+ expect(calls[0].url).toBe('http://localhost/hook/opencode')
+ expect(calls[0].init.unix).toBe('/tmp/some.sock')
+ })
+})
+
describe('opencodeConfigDir honors XDG_CONFIG_HOME', () => {
it('lands the plugin under $XDG_CONFIG_HOME/opencode when the (absolute) env var is set', () => {
const xdg = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-xdg-'))
diff --git a/src/core/agents/hooks/opencode.ts b/src/core/agents/hooks/opencode.ts
index 63a1f82ee..3d256632a 100644
--- a/src/core/agents/hooks/opencode.ts
+++ b/src/core/agents/hooks/opencode.ts
@@ -31,14 +31,23 @@ export function pluginPath(): string {
* restart (the restart handoff); fall back to the env vars;
* - POST application/x-www-form-urlencoded `nodeId` + `version` + `payload` (JSON) with
* the x-nodeterm-hook-token header to http://127.0.0.1:<port>/hook/opencode.
- * Payload fields are extracted defensively — the event NAME is the contract with
- * normalizeOpencode; sessionID/role are best-effort. message.updated forwards ONLY user
- * messages (turn start) so assistant token streaming never floods the hook server.
- * (NODETERM_HOOK_SOCK unix-socket transport is not implemented here — desktop buildPtyEnv
- * advertises the TCP port; if only a socket is available the plugin no-ops, fail-open.) */
+ * Bus events (session.created/idle/error, message.updated, permission.updated/replied)
+ * reach a plugin ONLY through the `event` catch-all hook as { event: { type, properties } }
+ * — opencode never calls a hook keyed by the event name itself, so per-event-name exports
+ * are dead code (the bug that made every status silently missing). `tool.execute.before`
+ * is the exception: it IS a real named plugin hook. The event NAME posted is the contract
+ * with normalizeOpencode (permission.updated is posted as `permission.asked`);
+ * sessionID/role are extracted defensively per the SDK payload shapes. message.updated
+ * forwards ONLY user messages (turn start) so assistant token streaming never floods the
+ * hook server.
+ * Transport: an SSH host advertises a UNIX SOCKET (NODETERM_HOOK_SOCK, no PORT line in the
+ * endpoint file) — the socket wins over TCP, like the POSIX script's `curl --unix-socket`
+ * branch. opencode runs on Bun, whose fetch takes a `unix` option; the node:http
+ * socketPath fallback covers any non-Bun runtime (and is what the tests exercise). */
export function buildOpencodePlugin(): string {
return `${PLUGIN_MARKER}
import fs from 'node:fs'
+import http from 'node:http'
export const NodetermStatus = async () => {
const nodeId = process.env.NODETERM_NODE_ID
@@ -46,6 +55,7 @@ export const NodetermStatus = async () => {
const live = () => {
const conf = {
port: process.env.NODETERM_HOOK_PORT,
+ sock: process.env.NODETERM_HOOK_SOCK,
token: process.env.NODETERM_HOOK_TOKEN,
version: process.env.NODETERM_HOOK_VERSION
}
@@ -53,7 +63,7 @@ export const NodetermStatus = async () => {
const file = process.env.NODETERM_HOOK_ENDPOINT
if (file) {
for (const line of fs.readFileSync(file, 'utf8').split('\\n')) {
- const m = line.match(/^NODETERM_HOOK_(PORT|TOKEN|VERSION)=(.*)$/)
+ const m = line.match(/^NODETERM_HOOK_(PORT|SOCK|TOKEN|VERSION)=(.*)$/)
if (m) conf[m[1].toLowerCase()] = m[2]
}
}
@@ -62,35 +72,55 @@ export const NodetermStatus = async () => {
}
const post = (event, extra) => {
try {
- const { port, token, version } = live()
- if (!port || !token) return
+ const { port, sock, token, version } = live()
+ if (!token || (!sock && !port)) return
const payload = JSON.stringify({ event, ...extra })
- fetch('http://127.0.0.1:' + port + '/hook/opencode', {
- method: 'POST',
- headers: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'x-nodeterm-hook-token': token
- },
- body:
- 'nodeId=' + encodeURIComponent(nodeId) +
- '&version=' + encodeURIComponent(version || '') +
- '&payload=' + encodeURIComponent(payload)
- }).catch(() => {})
+ const headers = {
+ 'content-type': 'application/x-www-form-urlencoded',
+ 'x-nodeterm-hook-token': token
+ }
+ const body =
+ 'nodeId=' + encodeURIComponent(nodeId) +
+ '&version=' + encodeURIComponent(version || '') +
+ '&payload=' + encodeURIComponent(payload)
+ if (sock && typeof Bun !== 'undefined') {
+ fetch('http://localhost/hook/opencode', { method: 'POST', unix: sock, headers, body }).catch(() => {})
+ } else if (sock) {
+ const req = http.request(
+ { socketPath: sock, path: '/hook/opencode', method: 'POST', headers },
+ (res) => res.resume()
+ )
+ req.on('error', () => {})
+ req.end(body)
+ } else {
+ fetch('http://127.0.0.1:' + port + '/hook/opencode', { method: 'POST', headers, body }).catch(() => {})
+ }
} catch {}
}
- const sid = (x) =>
- (x && (x.sessionID || x.session_id || (x.session && x.session.id) || (x.info && x.info.sessionID))) || undefined
return {
- 'session.created': async (input) => post('session.created', { sessionID: sid(input) }),
- 'session.idle': async (input) => post('session.idle', { sessionID: sid(input) }),
- 'session.error': async (input) => post('session.error', { sessionID: sid(input) }),
- 'permission.asked': async (input) => post('permission.asked', { sessionID: sid(input) }),
- 'permission.replied': async (input) => post('permission.replied', { sessionID: sid(input) }),
- 'tool.execute.before': async (input) => post('tool.execute.before', { sessionID: sid(input) }),
- 'message.updated': async (input) => {
- const role = input && ((input.info && input.info.role) || input.role)
- if (role === 'user') post('message.updated', { sessionID: sid(input), role: 'user' })
- }
+ event: async (input) => {
+ const ev = input && input.event
+ if (!ev || !ev.type) return
+ const p = ev.properties || {}
+ const info = p.info || {}
+ switch (ev.type) {
+ case 'session.created':
+ return post('session.created', { sessionID: info.id || p.sessionID })
+ case 'session.idle':
+ case 'session.error':
+ return post(ev.type, { sessionID: p.sessionID })
+ case 'permission.updated':
+ return post('permission.asked', { sessionID: p.sessionID })
+ case 'permission.replied':
+ return post('permission.replied', { sessionID: p.sessionID })
+ case 'message.updated':
+ if ((info.role || p.role) === 'user')
+ post('message.updated', { sessionID: info.sessionID || p.sessionID, role: 'user' })
+ return
+ }
+ },
+ 'tool.execute.before': async (input) =>
+ post('tool.execute.before', { sessionID: input && input.sessionID })
}
}
`