From e9d68f9c30de221cdcabf783d2913283cf3b607b Mon Sep 17 00:00:00 2001 From: dhaern <7317522+dhaern@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:29:59 +0000 Subject: [PATCH] fix(v2): use valid message IDs for exit notifications --- bun.lock | 22 +++-------------- package.json | 2 ++ src/v2/notifier.ts | 14 ++++++++--- test/opencode-v2-live.test.ts | 4 +-- test/v2-notifier.test.ts | 46 +++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 test/v2-notifier.test.ts diff --git a/bun.lock b/bun.lock index b792edff..a85d6da1 100644 --- a/bun.lock +++ b/bun.lock @@ -8,6 +8,7 @@ "@opencode-ai/plugin": "^1.18", "@opencode-ai/sdk": "^1.18", "@opencode/plugin": "^2.0.10", + "@opencode/schema": "^2.0.10", "bun-pty": "^0.4.10", "open": "^11.0.0", }, @@ -25,6 +26,7 @@ "@xterm/addon-fit": "^0.11.0", "@xterm/addon-serialize": "^0.14.0", "@xterm/xterm": "^6.0.0", + "effect": "4.0.0-rc.112", "happy-dom": "^20.5.0", "jsdom": "^27.4.0", "playwright-core": "1.57.0", @@ -638,7 +640,7 @@ "ecdsa-sig-formatter": ["ecdsa-sig-formatter@1.0.11", "", { "dependencies": { "safe-buffer": "^5.0.1" } }, "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ=="], - "effect": ["effect@4.0.0-beta.83", "", { "dependencies": { "@standard-schema/spec": "^1.1.0", "fast-check": "^4.8.0", "find-my-way-ts": "^0.1.6", "ini": "^7.0.0", "kubernetes-types": "^1.30.0", "msgpackr": "^2.0.1", "multipasta": "^0.2.7", "toml": "^4.1.1", "uuid": "^14.0.0", "yaml": "^2.9.0" } }, "sha512-0wsak8RtgGAr9UWSbVDgJHZcUqMSvicHcvaZv1MbMM7MCGgW4Rn/137J1MHQbwYPcwYGxT/IqehFd+UbYuj78w=="], + "effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], "electron-to-chromium": ["electron-to-chromium@1.5.278", "", {}, "sha512-dQ0tM1svDRQOwxnXxm+twlGTjr9Upvt8UFWAgmLsxEzFQxhbti4VwxmMjsDxVC51Zo84swW7FVCXEV+VAkhuPw=="], @@ -1080,12 +1082,6 @@ "@babel/plugin-transform-typescript/@babel/helper-plugin-utils": ["@babel/helper-plugin-utils@7.29.7", "", {}, "sha512-G7sHYigPY17oO5SYWnfD/0MTBwVR781S/JI643e/JhUYgVgWE/61SoW3NH9KWUKyKq5LVh3npif99Wkt6j86Jw=="], - "@effect/opentelemetry/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@effect/platform-node/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@effect/platform-node-shared/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - "@effect/platform-node-shared/ws": ["ws@8.21.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw=="], "@isaacs/cliui/string-width": ["string-width@5.1.2", "", { "dependencies": { "eastasianwidth": "^0.2.0", "emoji-regex": "^9.2.2", "strip-ansi": "^7.0.1" } }, "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA=="], @@ -1118,17 +1114,7 @@ "@npmcli/promise-spawn/which": ["which@6.0.1", "", { "dependencies": { "isexe": "^4.0.0" }, "bin": { "node-which": "bin/which.js" } }, "sha512-oGLe46MIrCRqX7ytPUf66EAYvdeMIZYn3WaocqqKZAxrBpkqHfL/qvTyJ/bTk5+AqHCjXmrv3CEWgy368zhRUg=="], - "@opencode/ai/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@opencode/client/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@opencode/plugin/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@opencode/protocol/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@opencode/schema/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], - - "@opencode/util/effect": ["effect@4.0.0-rc.112", "", { "dependencies": { "fast-check": "^4.9.0", "msgpackr": "^2.0.5" } }, "sha512-wXxwuh1Ywnv4cPRM3Wfa0vDwuOHnZ1TsTgHJkG9XgzND6inhBH9n1vBxhg3iIXOia/OrpmvVmd3lrD4vq6bF3A=="], + "@opencode-ai/plugin/effect": ["effect@4.0.0-beta.83", "", { "dependencies": { "@standard-schema/spec": "^1.1.0", "fast-check": "^4.8.0", "find-my-way-ts": "^0.1.6", "ini": "^7.0.0", "kubernetes-types": "^1.30.0", "msgpackr": "^2.0.1", "multipasta": "^0.2.7", "toml": "^4.1.1", "uuid": "^14.0.0", "yaml": "^2.9.0" } }, "sha512-0wsak8RtgGAr9UWSbVDgJHZcUqMSvicHcvaZv1MbMM7MCGgW4Rn/137J1MHQbwYPcwYGxT/IqehFd+UbYuj78w=="], "@opencode/util/glob": ["glob@13.0.5", "", { "dependencies": { "minimatch": "^10.2.1", "minipass": "^7.1.2", "path-scurry": "^2.0.0" } }, "sha512-BzXxZg24Ibra1pbQ/zE7Kys4Ua1ks7Bn6pKLkVPZ9FZe4JQS6/Q7ef3LG1H+k7lUf5l4T3PLSyYyYJVYUvfgTw=="], diff --git a/package.json b/package.json index c2fae0cc..550ebaa4 100644 --- a/package.json +++ b/package.json @@ -95,6 +95,7 @@ "@xterm/addon-fit": "^0.11.0", "@xterm/addon-serialize": "^0.14.0", "@xterm/xterm": "^6.0.0", + "effect": "4.0.0-rc.112", "happy-dom": "^20.5.0", "jsdom": "^27.4.0", "playwright-core": "1.57.0", @@ -124,6 +125,7 @@ "@opencode-ai/plugin": "^1.18", "@opencode-ai/sdk": "^1.18", "@opencode/plugin": "^2.0.10", + "@opencode/schema": "^2.0.10", "bun-pty": "^0.4.10", "open": "^11.0.0" } diff --git a/src/v2/notifier.ts b/src/v2/notifier.ts index 40b18ae1..be744694 100644 --- a/src/v2/notifier.ts +++ b/src/v2/notifier.ts @@ -13,11 +13,13 @@ import type { PTYSession } from '../plugin/pty/types.ts' */ export type V2SessionPrompt = Pick +const exitMessageIds = new WeakMap() + /** * Delivers `` notifications through opencode v2's plugin context. * - * The notification is admitted as a user prompt with a deterministic message - * id (`pty__exited`), so `admission.reconcile` makes repeated deliveries + * The notification is admitted as a user prompt with a stable message id, + * so `admission.reconcile` makes repeated deliveries * idempotent (e.g. when a kill races the process exit). Default delivery * (`steer`) plus `execution.wake` starts a new agent turn, and opencode * resolves the run with the session's *current* model — so the user's model @@ -39,9 +41,15 @@ export class V2SessionNotifier implements SessionNotifier { const text = buildExitNotification(session, exitCode) try { + const { ascending } = await import('@opencode/schema/identifier') + let id = exitMessageIds.get(session) + if (!id) { + id = `msg_${ascending()}` + exitMessageIds.set(session, id) + } await this.session.prompt({ sessionID: session.parentSessionId, - id: `pty_${session.id}_exited`, + id, text, }) } catch (error) { diff --git a/test/opencode-v2-live.test.ts b/test/opencode-v2-live.test.ts index e937a1dc..7951a2d4 100644 --- a/test/opencode-v2-live.test.ts +++ b/test/opencode-v2-live.test.ts @@ -153,9 +153,9 @@ describe('OpenCode V2 Live Integration', () => { const prompt = prompts[0] expect(prompt).toBeDefined() - // Delivered to the spawning session with a deterministic, idempotent id. + // Delivered to the spawning session with a valid, idempotent id. expect(prompt?.sessionID).toBe('ses_parent') - expect(prompt?.id).toBe(`pty_${spawned.id}_exited`) + expect(prompt?.id).toStartWith('msg_') expect(prompt?.text).toContain('') expect(prompt?.text).toContain(`ID: ${spawned.id}`) expect(prompt?.text).toContain('Exit Code: 3') diff --git a/test/v2-notifier.test.ts b/test/v2-notifier.test.ts new file mode 100644 index 00000000..7a6568f6 --- /dev/null +++ b/test/v2-notifier.test.ts @@ -0,0 +1,46 @@ +import { expect, it } from 'bun:test' +import { createRequire } from 'node:module' +import { SessionMessage } from '@opencode/schema/session-message' +import { RingBuffer } from '../src/plugin/pty/buffer.ts' +import type { PTYSession } from '../src/plugin/pty/types.ts' +import { V2SessionNotifier } from '../src/v2/notifier.ts' + +it('reuses a valid message id for repeated notifications of one PTY session', async () => { + const requireFromSchema = createRequire(import.meta.resolve('@opencode/schema/session-message')) + const { Schema } = (await import(requireFromSchema.resolve('effect'))) as typeof import('effect') + const generated = SessionMessage.ID.create() + expect(Schema.decodeUnknownSync(SessionMessage.ID)(generated)).toBe(generated) + + const ids: unknown[] = [] + const notifier = new V2SessionNotifier({ + prompt: async ({ id }) => { + ids.push(id) + Schema.decodeUnknownSync(SessionMessage.ID)(id) + return {} as never + }, + }) + const session: PTYSession = { + id: 'pty_test', + title: 'Test session', + command: 'echo', + args: [], + workdir: '.', + status: 'exited', + pid: 1, + createdAt: new Date(), + parentSessionId: 'ses_parent', + notifyOnExit: true, + timedOut: false, + buffer: new RingBuffer(), + process: null, + } + + await Promise.all([ + notifier.sendExitNotification(session, 0), + notifier.sendExitNotification(session, 0), + ]) + + expect(ids).toHaveLength(2) + const id = Schema.decodeUnknownSync(SessionMessage.ID)(ids[0]) + expect(ids[1]).toBe(id) +})