Skip to content
This repository was archived by the owner on Aug 17, 2026. It is now read-only.

Commit f6ca7a7

Browse files
authored
fix(grunt/tui): attach explicitly subscribes to /global/event SSE (#2)
Without this, externally-triggered session activity (a wake fired via POST /session/<id>/prompt_async from another peer) never renders in attach client TUIs. The wake processes server-side (LLM runs, message written to db), but the attach client has no way to learn about it. ## Root cause attach.ts called tui() without passing `events`. The SDKProvider had a fallback path (startSSE() in context/sdk.tsx onMount) that should have subscribed to /global/event when props.events was undefined. In practice that fallback is silent on failure — its async IIFE catches every error. This patch explicitly creates an EventSource in attach.ts that subscribes to /global/event via the SDK and forwards events to the SDKProvider, mirroring how the local-worker case in thread.ts wires `events: createEventSource(client)`. ## Verification Local typecheck passes (`bun typecheck` from packages/opencode). Manual end-to-end test plan (requires rebuilt binary): 1. Start `opencode serve --port 4096` in one terminal 2. `opencode attach http://127.0.0.1:4096 --dir <project>` in another 3. From a third terminal: `curl -X POST 'http://127.0.0.1:4096/session/<id>/prompt_async' -H 'Content-Type: application/json' -d '{"parts":[{"type":"text","text":"wake test"}]}'` 4. The attach client TUI should render the new user message + assistant response Refs hivemind anomalyco#221 (wake-up display bug verified in production) and anomalyco#222 Phase 2 patch 2c.
1 parent 4153dfe commit f6ca7a7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/attach.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,46 @@ import { TuiConfig } from "@/cli/cmd/tui/config/tui"
55
import { errorMessage } from "@/util/error"
66
import { validateSession } from "./validate-session"
77
import { ServerAuth } from "@/server/auth"
8+
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
9+
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
10+
import type { EventSource } from "./context/sdk"
11+
12+
// Subscribe to the server's /global/event SSE stream so externally-triggered
13+
// session activity (e.g. wakes via POST /session/<id>/prompt_async from another
14+
// peer) renders in the attach client's TUI.
15+
//
16+
// Without this, attach passes `events: undefined` to the SDKProvider, which is
17+
// supposed to fall back to startSSE() internally. In practice the fallback is
18+
// silent on failure (the IIFE catches everything) — explicit wiring here makes
19+
// the subscription observable and lifts errors to the user.
20+
function createAttachEventSource(opts: {
21+
url: string
22+
directory?: string
23+
headers?: RequestInit["headers"]
24+
}): EventSource {
25+
const sdk = createOpencodeClient({
26+
baseUrl: opts.url,
27+
directory: opts.directory,
28+
headers: opts.headers,
29+
})
30+
return {
31+
subscribe: async (handler) => {
32+
const ctrl = new AbortController()
33+
;(async () => {
34+
// Single-pass — caller's onCleanup aborts; SDKProvider does retry
35+
// internally if it owns the subscription, but here we deliberately
36+
// keep it simple and one-shot. Reconnect on transient failures would
37+
// be a follow-up.
38+
const events = await sdk.global.event({ signal: ctrl.signal, sseMaxRetryAttempts: 0 })
39+
for await (const event of events.stream as AsyncIterable<GlobalEvent>) {
40+
if (ctrl.signal.aborted) break
41+
handler(event)
42+
}
43+
})().catch(() => {})
44+
return () => ctrl.abort()
45+
},
46+
}
47+
}
848

949
export const AttachCommand = cmd({
1050
command: "attach <url>",
@@ -92,6 +132,7 @@ export const AttachCommand = cmd({
92132
},
93133
directory,
94134
headers,
135+
events: createAttachEventSource({ url: args.url, directory, headers }),
95136
})
96137
} finally {
97138
unguard?.()

0 commit comments

Comments
 (0)