Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
Expand Down
14 changes: 11 additions & 3 deletions src/v2/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import type { PTYSession } from '../plugin/pty/types.ts'
*/
export type V2SessionPrompt = Pick<Plugin.Context['session'], 'prompt'>

const exitMessageIds = new WeakMap<PTYSession, string>()

/**
* Delivers `<pty_exited>` notifications through opencode v2's plugin context.
*
* The notification is admitted as a user prompt with a deterministic message
* id (`pty_<id>_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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/opencode-v2-live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<pty_exited>')
expect(prompt?.text).toContain(`ID: ${spawned.id}`)
expect(prompt?.text).toContain('Exit Code: 3')
Expand Down
46 changes: 46 additions & 0 deletions test/v2-notifier.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})