mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Per-session permission modes: each root session runs in ask or auto. Auto answers each permission request once from the backend, child sessions inherit their root's mode, forks copy the source's mode, and scheduled runs and agent-created sessions stay on ask. A lock reason reports why a session's mode cannot change. Session goals: arm a goal in the composer and a backend audit loop keeps the session working until an auditor model returns done or blocked. The loop is bounded by continuation and token limits, survives restarts, and pushes an outcome notification. Goals cannot start on scheduled-run or child sessions. ocm session tool: allow-listed internal routes to list, create, follow up on, read replies from, and fork sessions. Created and forked sessions are pinned to ask, listing covers a repo's workspaces, and replies wait for the session to settle with a capped response. Multi-run: launch one prompt on up to five models, optionally each in its own OpenCode workspace, with per-entry status and discard. Also add the shared owners this group needed (session launcher, session reply, workspace create/remove, multi-run storage) and update the feature docs.
216 lines
7.9 KiB
TypeScript
216 lines
7.9 KiB
TypeScript
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
|
import type { SessionMessageInfo } from '@opencode-manager/shared/opencode'
|
|
import type { OpenCodeClient } from '../../src/services/opencode/client'
|
|
import {
|
|
SESSION_REPLY_MAX_LENGTH,
|
|
getLatestAssistantReplyState,
|
|
isSessionBusy,
|
|
readLatestAssistantReply,
|
|
sessionSettleSignal,
|
|
truncateSessionReply,
|
|
waitForSessionSettled,
|
|
} from '../../src/services/session-reply'
|
|
import { assistantMessage } from '../helpers/stub-schedule-api'
|
|
|
|
const mockOnEvent = vi.fn()
|
|
vi.mock('../../src/services/sse-aggregator', () => ({
|
|
sseAggregator: { onEvent: (...args: unknown[]) => mockOnEvent(...args) },
|
|
}))
|
|
|
|
beforeEach(() => {
|
|
mockOnEvent.mockReset()
|
|
})
|
|
|
|
function createFakeClient(messages: SessionMessageInfo[], active: Record<string, unknown> = {}): OpenCodeClient {
|
|
return {
|
|
api: {
|
|
message: {
|
|
list: vi.fn(async () => ({ data: messages, cursor: {} })),
|
|
},
|
|
session: {
|
|
active: vi.fn(async () => active),
|
|
},
|
|
},
|
|
forwardRaw: vi.fn(),
|
|
} as unknown as OpenCodeClient
|
|
}
|
|
|
|
describe('getLatestAssistantReplyState', () => {
|
|
it('returns the newest assistant text with think blocks stripped', () => {
|
|
const state = getLatestAssistantReplyState([
|
|
assistantMessage('', {
|
|
completed: true,
|
|
content: [
|
|
{ type: 'text', text: '\u003cthink\u003eprivate reasoning\u003c/think\u003e\nFinal answer' },
|
|
{ type: 'text', text: 'Second paragraph.' },
|
|
],
|
|
}),
|
|
])
|
|
|
|
expect(state).toEqual({
|
|
responseText: 'Final answer\n\nSecond paragraph.',
|
|
errorText: null,
|
|
completed: true,
|
|
})
|
|
})
|
|
|
|
it('surfaces the assistant error message', () => {
|
|
const state = getLatestAssistantReplyState([
|
|
assistantMessage('', { error: 'Provider exploded' }),
|
|
])
|
|
|
|
expect(state).toEqual({
|
|
responseText: null,
|
|
errorText: 'Provider exploded',
|
|
completed: false,
|
|
})
|
|
})
|
|
|
|
it('reports an incomplete assistant message as not completed', () => {
|
|
const state = getLatestAssistantReplyState([assistantMessage('Partial output')])
|
|
|
|
expect(state).toEqual({
|
|
responseText: 'Partial output',
|
|
errorText: null,
|
|
completed: false,
|
|
})
|
|
})
|
|
|
|
it('returns null when no assistant message is present', () => {
|
|
expect(getLatestAssistantReplyState([])).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('readLatestAssistantReply', () => {
|
|
it('reads the newest assistant reply through the client', async () => {
|
|
const client = createFakeClient([assistantMessage('From the client.', { completed: true })])
|
|
|
|
await expect(readLatestAssistantReply(client, 'ses-1')).resolves.toEqual({
|
|
responseText: 'From the client.',
|
|
errorText: null,
|
|
completed: true,
|
|
})
|
|
expect(client.api.message.list).toHaveBeenCalledWith({
|
|
sessionID: 'ses-1',
|
|
order: 'desc',
|
|
limit: 20,
|
|
})
|
|
})
|
|
|
|
it('returns null when the session has no assistant message', async () => {
|
|
await expect(readLatestAssistantReply(createFakeClient([]), 'ses-1')).resolves.toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('isSessionBusy', () => {
|
|
it('is true when the session appears in the active set', async () => {
|
|
const client = createFakeClient([], { 'ses-1': { type: 'running' } })
|
|
|
|
await expect(isSessionBusy(client, 'ses-1')).resolves.toBe(true)
|
|
})
|
|
|
|
it('is false when the session is absent from the active set', async () => {
|
|
await expect(isSessionBusy(createFakeClient([], {}), 'ses-1')).resolves.toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('truncateSessionReply', () => {
|
|
it('returns short replies unchanged', () => {
|
|
expect(truncateSessionReply('short reply')).toBe('short reply')
|
|
})
|
|
|
|
it('caps long replies and appends a truncation marker', () => {
|
|
const reply = 'x'.repeat(SESSION_REPLY_MAX_LENGTH + 25)
|
|
const truncated = truncateSessionReply(reply)
|
|
|
|
expect(truncated.startsWith('x'.repeat(SESSION_REPLY_MAX_LENGTH))).toBe(true)
|
|
expect(truncated.endsWith('[reply truncated]')).toBe(true)
|
|
expect(truncated).not.toContain('x'.repeat(SESSION_REPLY_MAX_LENGTH + 1))
|
|
})
|
|
})
|
|
|
|
describe('sessionSettleSignal', () => {
|
|
it('signals a clean settle for idle, succeeded, and status idle events', () => {
|
|
expect(sessionSettleSignal({ type: 'session.idle', data: { sessionID: 's1' } } as never, 's1')).toEqual({ errorText: null })
|
|
expect(sessionSettleSignal({ type: 'session.execution.succeeded', data: { sessionID: 's1' } } as never, 's1')).toEqual({ errorText: null })
|
|
expect(sessionSettleSignal({ type: 'session.status', data: { sessionID: 's1', status: { type: 'idle' } } } as never, 's1')).toEqual({ errorText: null })
|
|
})
|
|
|
|
it('signals a failure with the reported error message', () => {
|
|
expect(sessionSettleSignal({ type: 'session.execution.failed', data: { sessionID: 's1', error: { message: 'boom' } } } as never, 's1')).toEqual({ errorText: 'boom' })
|
|
expect(sessionSettleSignal({ type: 'session.execution.failed', data: { sessionID: 's1', error: { message: '' } } } as never, 's1')).toEqual({ errorText: 'The session reported an unknown error.' })
|
|
})
|
|
|
|
it('signals an interruption', () => {
|
|
expect(sessionSettleSignal({ type: 'session.execution.interrupted', data: { sessionID: 's1' } } as never, 's1')).toEqual({ errorText: 'The session execution was interrupted.' })
|
|
})
|
|
|
|
it('ignores events for other sessions and non-settling events', () => {
|
|
expect(sessionSettleSignal({ type: 'session.idle', data: { sessionID: 's2' } } as never, 's1')).toBeNull()
|
|
expect(sessionSettleSignal({ type: 'session.status', data: { sessionID: 's1', status: { type: 'busy' } } } as never, 's1')).toBeNull()
|
|
expect(sessionSettleSignal({ type: 'session.execution.started', data: { sessionID: 's1' } } as never, 's1')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('waitForSessionSettled', () => {
|
|
function captureListener() {
|
|
let listener: ((directory: string, event: unknown) => void) | undefined
|
|
const unsubscribe = vi.fn()
|
|
mockOnEvent.mockImplementation((callback: (directory: string, event: unknown) => void) => {
|
|
listener = callback
|
|
return unsubscribe
|
|
})
|
|
return {
|
|
emit: (event: unknown) => listener?.('', event),
|
|
unsubscribe,
|
|
}
|
|
}
|
|
|
|
it('does not subscribe when the timeout is zero', async () => {
|
|
const client = createFakeClient([], { 'ses-1': {} })
|
|
|
|
await expect(waitForSessionSettled(client, 'ses-1', 0)).resolves.toBeUndefined()
|
|
expect(mockOnEvent).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('resolves once the session settles and unsubscribes', async () => {
|
|
const client = createFakeClient([], { 'ses-1': {} })
|
|
const { emit, unsubscribe } = captureListener()
|
|
|
|
const settled = waitForSessionSettled(client, 'ses-1', 1000)
|
|
emit({ type: 'session.idle', data: { sessionID: 'ses-1' } })
|
|
|
|
await expect(settled).resolves.toBeUndefined()
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('ignores settle events for other sessions', async () => {
|
|
const client = createFakeClient([], { 'ses-1': {} })
|
|
const { emit, unsubscribe } = captureListener()
|
|
|
|
const settled = waitForSessionSettled(client, 'ses-1', 1000)
|
|
emit({ type: 'session.idle', data: { sessionID: 'ses-2' } })
|
|
expect(unsubscribe).not.toHaveBeenCalled()
|
|
|
|
emit({ type: 'session.idle', data: { sessionID: 'ses-1' } })
|
|
await expect(settled).resolves.toBeUndefined()
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('resolves immediately when the session is no longer busy after subscribing', async () => {
|
|
const client = createFakeClient([], {})
|
|
const { unsubscribe } = captureListener()
|
|
|
|
await expect(waitForSessionSettled(client, 'ses-1', 1000)).resolves.toBeUndefined()
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('resolves on timeout and unsubscribes', async () => {
|
|
const client = createFakeClient([], { 'ses-1': {} })
|
|
const { unsubscribe } = captureListener()
|
|
|
|
await expect(waitForSessionSettled(client, 'ses-1', 20)).resolves.toBeUndefined()
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|