Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/plugin/pty/notification-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class NotificationManager {
path: { id: session.parentSessionId },
body: {
parts: [{ type: 'text', text: message }],
...(session.parentAgent ? { agent: session.parentAgent } : {}),
},
})
} catch {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/pty/session-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class SessionLifecycleManager {
pid: 0, // will be set after spawn
createdAt: moment(),
parentSessionId: opts.parentSessionId,
parentAgent: opts.parentAgent,
notifyOnExit: opts.notifyOnExit ?? false,
buffer,
process: null, // will be set
Expand Down
1 change: 1 addition & 0 deletions src/plugin/pty/tools/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ptySpawn = tool({
title: args.title,
description: args.description,
parentSessionId: sessionId,
parentAgent: ctx.agent,
notifyOnExit: args.notifyOnExit,
})

Expand Down
2 changes: 2 additions & 0 deletions src/plugin/pty/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface PTYSession {
pid: number
createdAt: moment.Moment
parentSessionId: string
parentAgent?: string
notifyOnExit: boolean
buffer: RingBuffer
process: IPty | null
Expand Down Expand Up @@ -46,6 +47,7 @@ export interface SpawnOptions {
title?: string
description?: string
parentSessionId: string
parentAgent?: string
notifyOnExit?: boolean
}

Expand Down
77 changes: 77 additions & 0 deletions test/notification-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it, mock } from 'bun:test'
import type { OpencodeClient } from '@opencode-ai/sdk'
import moment from 'moment'
import { RingBuffer } from '../src/plugin/pty/buffer.ts'
import { NotificationManager } from '../src/plugin/pty/notification-manager.ts'
import type { PTYSession } from '../src/plugin/pty/types.ts'

type PromptPayload = {
path: { id: string }
body: {
parts: Array<{ type: string; text: string }>
agent?: string
}
}

function createSession(overrides: Partial<PTYSession> = {}): PTYSession {
const buffer = new RingBuffer()
buffer.append('line 1\nline 2\n')

return {
id: 'pty_test',
title: 'Test Session',
description: 'Test session description',
command: 'echo',
args: ['hello'],
workdir: '/tmp',
status: 'running',
pid: 12345,
createdAt: moment(),
parentSessionId: 'parent-session-id',
parentAgent: 'agent-two',
notifyOnExit: true,
buffer,
process: null,
...overrides,
}
}

describe('NotificationManager', () => {
it('includes body.agent when originating agent is present', async () => {
const promptAsync = mock(async (_payload: PromptPayload) => {})
const manager = new NotificationManager()

manager.init({ session: { promptAsync } } as unknown as OpencodeClient)

await manager.sendExitNotification(createSession({ parentAgent: 'agent-two' }), 0)

expect(promptAsync).toHaveBeenCalledTimes(1)
const payload = promptAsync.mock.calls[0]![0]

expect(payload.path).toEqual({ id: 'parent-session-id' })
expect(payload.body.agent).toBe('agent-two')
expect(payload.body.parts).toHaveLength(1)
expect(payload.body.parts[0]?.text).toContain('<pty_exited>')
expect(payload.body.parts[0]?.text).toContain('Use pty_read to check the full output.')
})

it('omits body.agent when originating agent is missing', async () => {
const promptAsync = mock(async (_payload: PromptPayload) => {})
const manager = new NotificationManager()

manager.init({ session: { promptAsync } } as unknown as OpencodeClient)

await manager.sendExitNotification(createSession({ parentAgent: undefined }), 1)

expect(promptAsync).toHaveBeenCalledTimes(1)
const payload = promptAsync.mock.calls[0]![0]

expect(payload.path).toEqual({ id: 'parent-session-id' })
expect(Object.hasOwn(payload.body, 'agent')).toBe(false)
expect(payload.body.parts).toHaveLength(1)
expect(payload.body.parts[0]?.text).toContain('<pty_exited>')
expect(payload.body.parts[0]?.text).toContain(
'Process failed. Use pty_read with the pattern parameter to search for errors in the output.'
)
})
})
2 changes: 2 additions & 0 deletions test/pty-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('PTY Tools', () => {
args: ['hello'],
description: 'Test session',
parentSessionId: 'parent-session-id',
parentAgent: 'test-agent',
workdir: undefined,
env: undefined,
title: undefined,
Expand Down Expand Up @@ -92,6 +93,7 @@ describe('PTY Tools', () => {
title: 'My Node Session',
description: 'Running Node.js script',
parentSessionId: 'parent-session-id',
parentAgent: 'test-agent',
notifyOnExit: true,
})

Expand Down