fix(backend): update session API endpoints for OpenCode v1.16+

This commit is contained in:
Chris Scott committed 2026-07-10 09:33:15 -04:00
1 parent 195182e3ae
commit d474dd652d
7 files changed
+1261 -457

No files matched your search

@@ -116,23 +116,21 @@ describe('OpenCodeRestartCoordinator', () => {
// Assert interleaved ordering: aborts → restart → resumes
expect(events).toEqual([
'forward:/session/s1/abort',
'forward:/session/s2/abort',
'forward:/api/session/s1/interrupt',
'forward:/api/session/s2/interrupt',
'restart',
'forward:/session/s1/prompt_async',
'forward:/session/s2/prompt_async',
'forward:/api/session/s1/prompt',
'forward:/api/session/s2/prompt',
])
// Aborts called first for both sessions
expect(forward).toHaveBeenNthCalledWith(1, {
method: 'POST',
path: '/session/s1/abort',
directory: '/a',
path: '/api/session/s1/interrupt',
})
expect(forward).toHaveBeenNthCalledWith(2, {
method: 'POST',
path: '/session/s2/abort',
directory: '/b',
path: '/api/session/s2/interrupt',
})
expect(restart).toHaveBeenCalledOnce()
@@ -141,19 +139,21 @@ describe('OpenCodeRestartCoordinator', () => {
expect(forward).toHaveBeenCalledTimes(4)
expect(forward).toHaveBeenNthCalledWith(3, {
method: 'POST',
path: '/session/s1/prompt_async',
directory: '/a',
path: '/api/session/s1/prompt',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }),
body: JSON.stringify({ prompt: { text: 'continue' } }),
})
expect(forward).toHaveBeenNthCalledWith(4, {
method: 'POST',
path: '/session/s2/prompt_async',
directory: '/b',
path: '/api/session/s2/prompt',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }),
body: JSON.stringify({ prompt: { text: 'continue' } }),
})
// Verify resume body deserializes correctly
const resumeBody = JSON.parse(forward.mock.calls[2]![0].body as string)
expect(resumeBody).toEqual({ prompt: { text: 'continue' } })
expect(result).toEqual({ healthy: true, resumedSessionIDs: ['s1', 's2'] })
})
@@ -172,16 +172,15 @@ describe('OpenCodeRestartCoordinator', () => {
// Aborts still happen
expect(forward).toHaveBeenNthCalledWith(1, {
method: 'POST',
path: '/session/s1/abort',
directory: '/a',
path: '/api/session/s1/interrupt',
})
expect(restart).toHaveBeenCalledOnce()
// No prompt_async calls
const promptAsyncCalls = forward.mock.calls.filter(
(call: unknown[]) => (call[0] as { path: string }).path.includes('prompt_async'),
// No prompt calls
const promptCalls = forward.mock.calls.filter(
(call: unknown[]) => (call[0] as { path: string }).path.includes('/prompt'),
)
expect(promptAsyncCalls).toHaveLength(0)
expect(promptCalls).toHaveLength(0)
expect(result).toEqual({ healthy: false, resumedSessionIDs: [] })
})
@@ -219,15 +218,13 @@ describe('OpenCodeRestartCoordinator', () => {
expect(forward).toHaveBeenCalledTimes(2)
expect(forward).toHaveBeenNthCalledWith(1, {
method: 'POST',
path: '/session/manual1/abort',
directory: '/a',
path: '/api/session/manual1/interrupt',
})
expect(forward).toHaveBeenNthCalledWith(2, {
method: 'POST',
path: '/session/manual1/prompt_async',
directory: '/a',
path: '/api/session/manual1/prompt',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }),
body: JSON.stringify({ prompt: { text: 'continue' } }),
})
expect(result).toEqual({ healthy: true, resumedSessionIDs: ['manual1'] })
@@ -274,13 +271,11 @@ describe('OpenCodeRestartCoordinator', () => {
expect(forward).toHaveBeenCalledTimes(2)
expect(forward).toHaveBeenNthCalledWith(1, {
method: 'POST',
path: '/session/s1/abort',
directory: '/a',
path: '/api/session/s1/interrupt',
})
expect(forward).toHaveBeenNthCalledWith(2, {
method: 'POST',
path: '/session/s2/abort',
directory: '/b',
path: '/api/session/s2/interrupt',
})
})
})
@@ -45,8 +45,7 @@ export class OpenCodeRestartCoordinator {
try {
await this.client.forward({
method: 'POST',
path: `/session/${s.sessionID}/abort`,
directory: s.directory,
path: `/api/session/${s.sessionID}/interrupt`,
})
} catch (error) {
logger.warn(`Failed to abort session ${s.sessionID}: ${error}`)
@@ -61,10 +60,9 @@ export class OpenCodeRestartCoordinator {
try {
const response = await this.client.forward({
method: 'POST',
path: `/session/${s.sessionID}/prompt_async`,
directory: s.directory,
path: `/api/session/${s.sessionID}/prompt`,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }),
body: JSON.stringify({ prompt: { text: 'continue' } }),
})
return response.ok ? s.sessionID : null
} catch (error) {
+221 -208
View File
@@ -7,7 +7,6 @@ import {
type ScheduleRunTriggerSource,
type UpdateScheduleJobRequest,
} from '@opencode-manager/shared/types'
import { buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
import { getRepoById } from '../db/queries'
import type { ScheduleJobWithRepo } from '../db/schedules'
import {
@@ -40,6 +39,7 @@ import {
buildUpdatedSchedulePersistenceInput,
computeNextRunAtForJob,
} from './schedule-config'
import { buildSchedulePermissionRuleset, type SchedulePermissionRuleset, evaluateSchedulePermission } from '@opencode-manager/shared/schemas'
import { resolveOpenCodeModel } from './opencode-models'
import type { OpenCodeClient } from './opencode/client'
import type { ScheduleWorktreeManager } from './schedule-worktree'
@@ -60,45 +60,27 @@ class ScheduleServiceError extends Error {
}
interface SessionResponse {
id: string
data: {
id: string
}
}
interface PromptResponse {
parts?: Array<{
type?: string
text?: string
}>
}
interface SessionMessagePart {
type?: string
interface SessionMessageContent {
type: string
text?: string
}
interface SessionMessage {
info?: {
id?: string
sessionID?: string
role?: string
time?: {
created?: number
completed?: number
}
error?: {
name?: string
data?: {
message?: string
}
}
}
parts?: SessionMessagePart[]
type: string
id: string
content?: SessionMessageContent[]
finish?: string
error?: { name?: string; data?: { message?: string } }
time?: { created?: number; completed?: number }
}
interface SessionStatus {
type: 'idle' | 'retry' | 'busy'
attempt?: number
message?: string
next?: number
interface SessionActiveEntry {
type: string
}
const RUN_POLL_INTERVAL_MS = 2_000
@@ -106,18 +88,9 @@ const RUN_POLL_TIMEOUT_MS = 5 * 60_000
interface SessionMonitor {
getErrorText(): string | null
isIdle(): boolean
dispose(): void
}
function extractResponseText(response: PromptResponse): string {
return (response.parts ?? [])
.filter((part) => part.type === 'text' && typeof part.text === 'string')
.map((part) => part.text?.replace(/<think>[\s\S]*?<\/think>\s*/g, '').trim() ?? '')
.filter(Boolean)
.join('\n\n')
}
function buildSessionTitle(job: ScheduleJob): string {
return `Scheduled: ${job.name}`
}
@@ -253,46 +226,56 @@ function buildRunStartedLog(input: {
].join('\n')
}
function parsePromptResponse(responseText: string): PromptResponse | null {
if (!responseText.trim()) {
return null
}
try {
return JSON.parse(responseText) as PromptResponse
} catch {
return null
}
}
function extractAssistantMessageText(parts: SessionMessagePart[] | undefined): string {
return (parts ?? [])
function extractAssistantMessageText(content: SessionMessageContent[] | undefined): string {
return (content ?? [])
.filter((part) => part.type === 'text' && typeof part.text === 'string')
.map((part) => part.text?.replace(/<think>[\s\S]*?<\/think>\s*/g, '').trim() ?? '')
.filter(Boolean)
.join('\n\n')
}
function getAssistantMessageState(messages: SessionMessage[]): {
responseText: string | null
errorText: string | null
completed: boolean
} | null {
const assistantMessage = [...messages]
.reverse()
.find((message) => message.info?.role === 'assistant')
function getAssistantMessageState(messages: SessionMessage[]): AssistantState | null {
const assistantMessage = messages.findLast((message) => message.type === 'assistant')
if (!assistantMessage) {
return null
}
return {
responseText: extractAssistantMessageText(assistantMessage.parts) || null,
errorText: assistantMessage.info?.error?.data?.message ?? assistantMessage.info?.error?.name ?? null,
completed: Boolean(assistantMessage.info?.time?.completed),
responseText: extractAssistantMessageText(assistantMessage.content) || null,
errorText: assistantMessage.error?.data?.message ?? assistantMessage.error?.name ?? null,
completed: Boolean(assistantMessage.time?.completed),
}
}
type AssistantState = {
responseText: string | null
errorText: string | null
completed: boolean
}
type RunTerminalState = {
terminal: true
type: 'completed' | 'failed'
responseText: string | null
errorText: string | null
} | {
terminal: false
type: null
responseText: null
errorText: null
}
function isRunTerminal(assistantState: AssistantState | null): RunTerminalState {
if (assistantState?.errorText) {
return { terminal: true, type: 'failed', responseText: assistantState.responseText, errorText: assistantState.errorText }
}
if (assistantState?.completed) {
return { terminal: true, type: 'completed', responseText: assistantState.responseText, errorText: null }
}
return { terminal: false, type: null, responseText: null, errorText: null }
}
function getSessionEventId(event: SSEEvent): string | null {
const properties = event.properties as {
sessionID?: string
@@ -315,19 +298,8 @@ function getSessionErrorText(event: SSEEvent): string | null {
return properties.error?.data?.message ?? properties.error?.name ?? null
}
function getSessionStatusType(event: SSEEvent): string | null {
const properties = event.properties as {
status?: {
type?: string
}
}
return properties.status?.type ?? null
}
function createSessionMonitor(directory: string, sessionId: string): SessionMonitor {
let errorText: string | null = null
let idle = false
const unsubscribe = sseAggregator.onEvent((eventDirectory, event) => {
if (eventDirectory !== directory) {
@@ -342,20 +314,10 @@ function createSessionMonitor(directory: string, sessionId: string): SessionMoni
errorText = getSessionErrorText(event) ?? 'The session reported an unknown error.'
return
}
if (event.type === 'session.idle') {
idle = true
return
}
if (event.type === 'session.status' && getSessionStatusType(event) === 'idle') {
idle = true
}
})
return {
getErrorText: () => errorText,
isIdle: () => idle,
dispose: unsubscribe,
}
}
@@ -596,12 +558,11 @@ export class ScheduleService {
const sessionTitle = buildSessionTitle(job)
const sessionResponse = await this.openCodeClient.forward({
method: 'POST',
path: '/session',
directory: runDirectory,
path: '/api/session',
body: JSON.stringify({
title: sessionTitle,
agent: job.agentSlug ?? undefined,
permission: buildSchedulePermissionRuleset(job.permissionConfig),
model: model ? { providerID: model.providerID, id: model.modelID } : undefined,
location: { directory: runDirectory },
}),
headers: { 'Content-Type': 'application/json' },
})
@@ -610,7 +571,7 @@ export class ScheduleService {
throw new ScheduleServiceError('Failed to create OpenCode session', 502)
}
const session = await sessionResponse.json() as SessionResponse
const session = (await sessionResponse.json() as SessionResponse).data
const runWithSession = updateScheduleRunMetadata(this.db, repoId, jobId, run.id, {
sessionId: session.id,
sessionTitle,
@@ -626,7 +587,21 @@ export class ScheduleService {
throw new ScheduleServiceError('Failed to attach session to run', 500)
}
try {
const titleResponse = await this.openCodeClient.forward({
method: 'PATCH',
path: `/session/${session.id}`,
directory: runDirectory,
body: JSON.stringify({ title: sessionTitle }),
headers: { 'Content-Type': 'application/json' },
})
if (!titleResponse.ok) logger.warn(`Failed to set session title for ${session.id} (${titleResponse.status})`)
} catch (error) {
logger.warn(`Failed to set session title for ${session.id}:`, error)
}
const sessionMonitor = createSessionMonitor(runDirectory, session.id)
const ruleset = buildSchedulePermissionRuleset(job.permissionConfig)
void this.submitPromptAndMonitor({
repoId,
@@ -635,8 +610,8 @@ export class ScheduleService {
sessionId: session.id,
sessionTitle,
triggerSource,
model,
sessionMonitor,
ruleset,
directory: runDirectory,
})
@@ -694,17 +669,16 @@ export class ScheduleService {
throw new ScheduleServiceError('Only running schedule runs can be cancelled', 409)
}
const runDirectory = run.worktreePath ?? repo.fullPath
if (run.sessionId) {
const messages = await this.listSessionMessages(runDirectory, run.sessionId)
const messages = await this.listSessionMessages(run.sessionId)
const assistantState = getAssistantMessageState(messages)
const terminal = isRunTerminal(assistantState)
if (assistantState?.completed || assistantState?.errorText) {
if (terminal.terminal) {
await this.finalizeRecoveredRun(job, run, {
status: assistantState.errorText ? 'failed' : 'completed',
responseText: assistantState.responseText,
errorText: assistantState.errorText,
status: terminal.type,
responseText: terminal.responseText,
errorText: terminal.errorText,
}, repo)
return this.getRun(repoId, jobId, runId)
@@ -712,8 +686,7 @@ export class ScheduleService {
const abortResponse = await this.openCodeClient.forward({
method: 'POST',
path: `/session/${run.sessionId}/abort`,
directory: runDirectory,
path: `/api/session/${run.sessionId}/interrupt`,
})
if (!abortResponse.ok) {
@@ -764,8 +737,8 @@ export class ScheduleService {
sessionId: string
sessionTitle: string
triggerSource: ScheduleRunTriggerSource
model: { providerID: string; modelID: string }
sessionMonitor: SessionMonitor
ruleset: SchedulePermissionRuleset
directory: string
}): Promise<void> {
const repo = this.assertRepo(input.repoId)
@@ -773,11 +746,9 @@ export class ScheduleService {
try {
const promptResponse = await this.openCodeClient.forward({
method: 'POST',
path: `/session/${input.sessionId}/message`,
directory: input.directory,
path: `/api/session/${input.sessionId}/prompt`,
body: JSON.stringify({
parts: [{ type: 'text', text: await buildPromptWithSkills(input.job.prompt, input.job.skillMetadata, input.directory, this.openCodeClient) }],
model: input.model,
prompt: { text: await buildPromptWithSkills(input.job.prompt, input.job.skillMetadata, input.directory, this.openCodeClient) },
}),
headers: { 'Content-Type': 'application/json' },
})
@@ -787,40 +758,7 @@ export class ScheduleService {
throw new ScheduleServiceError(errorText || 'Failed to run scheduled prompt', 502)
}
const promptBody = await promptResponse.text()
const promptResult = parsePromptResponse(promptBody)
if (promptResult) {
const currentRun = getScheduleRunById(this.db, input.repoId, input.job.id, input.runId)
if (!currentRun || currentRun.status !== 'running') {
return
}
const finishedAt = Date.now()
const responseText = extractResponseText(promptResult)
updateScheduleRun(this.db, input.repoId, input.job.id, input.runId, {
status: 'completed',
finishedAt,
sessionId: input.sessionId,
sessionTitle: input.sessionTitle,
responseText,
logText: buildRunLog({
job: input.job,
triggerSource: input.triggerSource,
sessionId: input.sessionId,
sessionTitle: input.sessionTitle,
responseText,
finishedAt,
}),
})
updateScheduleJobRunState(this.db, input.repoId, input.job.id, {
lastRunAt: finishedAt,
nextRunAt: input.triggerSource === 'manual' ? input.job.nextRunAt : computeNextRunAtForJob(input.job, finishedAt),
})
return
}
await this.respondToPendingRequests(input.sessionId, input.ruleset)
await this.monitorRunCompletion({
sessionMonitor: input.sessionMonitor,
@@ -830,6 +768,7 @@ export class ScheduleService {
sessionId: input.sessionId,
sessionTitle: input.sessionTitle,
triggerSource: input.triggerSource,
ruleset: input.ruleset,
directory: input.directory,
})
return
@@ -878,36 +817,15 @@ export class ScheduleService {
sessionId: string
sessionTitle: string
triggerSource: ScheduleRunTriggerSource
ruleset: SchedulePermissionRuleset
directory: string
initialSessionStatus?: SessionStatus
}): Promise<void> {
try {
const sessionStatus = input.initialSessionStatus
if (sessionStatus && sessionStatus.type === 'idle') {
const repo = this.assertRepo(input.repoId)
const messages = await this.listSessionMessages(input.directory, input.sessionId)
const assistantState = getAssistantMessageState(messages)
if (assistantState?.completed || assistantState?.errorText) {
await this.finalizeRecoveredRun(input.job, {
id: input.runId,
repoId: input.repoId,
jobId: input.job.id,
sessionId: input.sessionId,
sessionTitle: input.sessionTitle,
triggerSource: input.triggerSource,
} as ScheduleRun, {
status: assistantState.errorText ? 'failed' : 'completed',
responseText: assistantState.responseText,
errorText: assistantState.errorText,
}, repo)
return
}
}
const repo = this.assertRepo(input.repoId)
const currentMessages = await this.listSessionMessages(input.directory, input.sessionId)
const currentMessages = await this.listSessionMessages(input.sessionId)
const currentAssistantState = getAssistantMessageState(currentMessages)
if (currentAssistantState?.completed || currentAssistantState?.errorText) {
const terminal = isRunTerminal(currentAssistantState)
if (terminal.terminal) {
await this.finalizeRecoveredRun(input.job, {
id: input.runId,
repoId: input.repoId,
@@ -916,14 +834,14 @@ export class ScheduleService {
sessionTitle: input.sessionTitle,
triggerSource: input.triggerSource,
} as ScheduleRun, {
status: currentAssistantState.errorText ? 'failed' : 'completed',
responseText: currentAssistantState.responseText,
errorText: currentAssistantState.errorText,
status: terminal.type,
responseText: terminal.responseText,
errorText: terminal.errorText,
}, repo)
return
}
const response = await this.waitForAssistantMessage(input.job, input.sessionId, input.sessionMonitor, input.directory)
const response = await this.waitForAssistantMessage(input.job, input.sessionId, input.sessionMonitor, input.ruleset)
const currentRun = getScheduleRunById(this.db, input.repoId, input.job.id, input.runId)
if (!currentRun || currentRun.status !== 'running') {
return
@@ -1008,6 +926,70 @@ export class ScheduleService {
}
}
private async respondToPendingRequests(sessionId: string, ruleset: SchedulePermissionRuleset): Promise<void> {
try {
const permResponse = await this.openCodeClient.forward({
method: 'GET',
path: `/api/session/${sessionId}/permission`,
})
if (permResponse.ok) {
const permBody = await permResponse.json() as { data: Array<{ id: string; action: string; resources: string[] }> }
for (const req of permBody.data) {
try {
const verdict = evaluateSchedulePermission(ruleset, req.action, req.resources ?? [])
const replyBody = verdict === 'deny'
? { reply: 'reject', message: 'Denied by schedule permission config' }
: { reply: 'once' }
const replyResponse = await this.openCodeClient.forward({
method: 'POST',
path: `/api/session/${sessionId}/permission/${req.id}/reply`,
body: JSON.stringify(replyBody),
headers: { 'Content-Type': 'application/json' },
})
if (!replyResponse.ok && replyResponse.status !== 404) {
logger.warn(`Unexpected status ${replyResponse.status} replying to permission ${req.id} in session ${sessionId}`)
}
} catch (innerError) {
logger.warn(`Failed to reply to permission ${req.id} in session ${sessionId}:`, innerError)
}
}
} else if (permResponse.status !== 404) {
logger.warn(`Unexpected status ${permResponse.status} fetching permissions for session ${sessionId}`)
}
} catch (error) {
logger.warn(`Failed to fetch permissions for session ${sessionId}:`, error)
}
try {
const questionResponse = await this.openCodeClient.forward({
method: 'GET',
path: `/api/session/${sessionId}/question`,
})
if (questionResponse.ok) {
const questionBody = await questionResponse.json() as { data: Array<{ id: string }> }
for (const q of questionBody.data) {
try {
const rejectResponse = await this.openCodeClient.forward({
method: 'POST',
path: `/api/session/${sessionId}/question/${q.id}/reject`,
})
if (!rejectResponse.ok && rejectResponse.status !== 404) {
logger.warn(`Unexpected status ${rejectResponse.status} rejecting question ${q.id} in session ${sessionId}`)
}
} catch (innerError) {
logger.warn(`Failed to reject question ${q.id} in session ${sessionId}:`, innerError)
}
}
} else if (questionResponse.status !== 404) {
logger.warn(`Unexpected status ${questionResponse.status} fetching questions for session ${sessionId}`)
}
} catch (error) {
logger.warn(`Failed to fetch questions for session ${sessionId}:`, error)
}
}
private async recoverRunningRun(job: ScheduleJob, run: ScheduleRun): Promise<void> {
try {
const repo = this.assertRepo(job.repoId)
@@ -1021,23 +1003,25 @@ export class ScheduleService {
return
}
const messages = await this.listSessionMessages(runDirectory, run.sessionId)
const messages = await this.listSessionMessages(run.sessionId)
const assistantState = getAssistantMessageState(messages)
if (assistantState?.completed || assistantState?.errorText) {
const activeSessions = await this.getActiveSessions()
const sessionActive = run.sessionId ? run.sessionId in activeSessions : false
const terminal = isRunTerminal(assistantState)
if (terminal.terminal) {
await this.finalizeRecoveredRun(job, run, {
status: assistantState.errorText ? 'failed' : 'completed',
responseText: assistantState.responseText,
errorText: assistantState.errorText,
status: terminal.type,
responseText: terminal.responseText,
errorText: terminal.errorText,
}, repo)
return
}
const sessionStatuses = await this.getSessionStatuses(runDirectory)
const sessionStatus = run.sessionId ? sessionStatuses[run.sessionId] : undefined
if (sessionStatus && sessionStatus.type !== 'idle') {
if (sessionActive) {
const sessionMonitor = createSessionMonitor(runDirectory, run.sessionId)
const ruleset = buildSchedulePermissionRuleset(job.permissionConfig)
void this.monitorRunCompletion({
sessionMonitor,
repoId: run.repoId,
@@ -1046,7 +1030,7 @@ export class ScheduleService {
sessionId: run.sessionId,
sessionTitle: run.sessionTitle ?? buildSessionTitle(job),
triggerSource: run.triggerSource,
initialSessionStatus: sessionStatus,
ruleset,
directory: runDirectory,
})
return
@@ -1135,15 +1119,16 @@ export class ScheduleService {
job: ScheduleJob,
sessionId: string,
sessionMonitor: SessionMonitor,
directory: string,
ruleset: SchedulePermissionRuleset,
): Promise<{ responseText: string | null; errorText: string | null }> {
const startedAt = Date.now()
let timeoutStartedAt: number | null = null
while (Date.now() - startedAt < RUN_POLL_TIMEOUT_MS) {
const messages = await this.listSessionMessages(directory, sessionId)
while (true) {
await this.respondToPendingRequests(sessionId, ruleset)
const messages = await this.listSessionMessages(sessionId)
const assistantState = getAssistantMessageState(messages)
if (assistantState && (assistantState.completed || assistantState.errorText)) {
if (assistantState?.errorText) {
return {
responseText: assistantState.responseText,
errorText: assistantState.errorText,
@@ -1158,50 +1143,78 @@ export class ScheduleService {
}
}
if (sessionMonitor.isIdle()) {
const activeSessions = await this.getActiveSessions()
const sessionActive = sessionId in activeSessions
if (assistantState?.completed) {
return {
responseText: assistantState.responseText,
errorText: null,
}
}
if (sessionActive) {
timeoutStartedAt = null
await Bun.sleep(RUN_POLL_INTERVAL_MS)
continue
}
if (timeoutStartedAt === null) {
timeoutStartedAt = Date.now()
} else if (Date.now() - timeoutStartedAt >= RUN_POLL_TIMEOUT_MS) {
return {
responseText: null,
errorText: 'The session became idle without producing an assistant response. Open the linked session to inspect any pending questions, permissions, or provider issues.',
errorText: 'Timed out waiting for the assistant response. Open the linked session to inspect any pending questions, permissions, or provider issues.',
}
}
await Bun.sleep(RUN_POLL_INTERVAL_MS)
}
return {
responseText: null,
errorText: 'Timed out waiting for the assistant response. Open the linked session to inspect any pending questions, permissions, or provider issues.',
}
}
private async listSessionMessages(directory: string, sessionId: string): Promise<SessionMessage[]> {
const messagesResponse = await this.openCodeClient.forward({
method: 'GET',
path: `/session/${sessionId}/message`,
directory,
})
private async listSessionMessages(sessionId: string): Promise<SessionMessage[]> {
const allMessages: SessionMessage[] = []
let cursor: string | null = null
let pages = 0
const MAX_PAGES = 10
if (!messagesResponse.ok) {
const errorText = await messagesResponse.text()
throw new ScheduleServiceError(errorText || 'Failed to fetch session messages', 502)
}
do {
const query = cursor
? `/api/session/${sessionId}/message?limit=200&cursor=${encodeURIComponent(cursor)}`
: `/api/session/${sessionId}/message?limit=200`
return await messagesResponse.json() as SessionMessage[]
const messagesResponse = await this.openCodeClient.forward({
method: 'GET',
path: query,
})
if (!messagesResponse.ok) {
const errorText = await messagesResponse.text()
throw new ScheduleServiceError(errorText || 'Failed to fetch session messages', 502)
}
const body = await messagesResponse.json() as { data: SessionMessage[]; cursor?: { next?: string } }
allMessages.push(...body.data)
cursor = body.cursor?.next ?? null
pages++
} while (cursor && pages < MAX_PAGES)
return allMessages
}
private async getSessionStatuses(directory: string): Promise<Record<string, SessionStatus>> {
private async getActiveSessions(): Promise<Record<string, SessionActiveEntry>> {
const response = await this.openCodeClient.forward({
method: 'GET',
path: '/session/status',
directory,
path: '/api/session/active',
})
if (!response.ok) {
const errorText = await response.text()
throw new ScheduleServiceError(errorText || 'Failed to fetch session statuses', 502)
throw new ScheduleServiceError(errorText || 'Failed to fetch active sessions', 502)
}
return await response.json() as Record<string, SessionStatus>
const body = await response.json() as { data: Record<string, SessionActiveEntry> }
return body.data
}
private assertRepo(repoId: number) {
@@ -1,5 +1,9 @@
import { describe, expect, it } from 'vitest'
import { DEFAULT_DESTRUCTIVE_BASH_PATTERNS, buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
import {
DEFAULT_DESTRUCTIVE_BASH_PATTERNS,
buildSchedulePermissionRuleset,
evaluateSchedulePermission,
} from '@opencode-manager/shared/schemas'
describe('buildSchedulePermissionRuleset', () => {
it('returns the allow-all baseline with default deny rules when given null', () => {
@@ -28,3 +32,34 @@ describe('buildSchedulePermissionRuleset', () => {
])
})
})
describe('evaluateSchedulePermission', () => {
it('denies bash sudo commands with the default ruleset', () => {
const ruleset = buildSchedulePermissionRuleset(null)
expect(evaluateSchedulePermission(ruleset, 'bash', ['sudo rm -rf /'])).toBe('deny')
})
it('allows benign bash commands with the default ruleset', () => {
const ruleset = buildSchedulePermissionRuleset(null)
expect(evaluateSchedulePermission(ruleset, 'bash', ['git status'])).toBe('allow')
})
it('denies external_directory access with the default ruleset', () => {
const ruleset = buildSchedulePermissionRuleset(null)
expect(evaluateSchedulePermission(ruleset, 'external_directory', ['/etc'])).toBe('deny')
})
it('allows external_directory when allowExternalDirectory is true', () => {
const ruleset = buildSchedulePermissionRuleset({ allowExternalDirectory: true, bashDenyPatterns: [] })
expect(evaluateSchedulePermission(ruleset, 'external_directory', ['/some/path'])).toBe('allow')
})
it('denies when at least one resource matches a deny pattern', () => {
const ruleset = buildSchedulePermissionRuleset(null)
expect(evaluateSchedulePermission(ruleset, 'bash', ['git status', 'sudo rm -rf /'])).toBe('deny')
})
it('asks for an unmatched action with an empty ruleset', () => {
expect(evaluateSchedulePermission([], 'bash', ['git status'])).toBe('ask')
})
})
@@ -1,6 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { ScheduleJob, ScheduleRun } from '@opencode-manager/shared/types'
import { buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
const mocks = vi.hoisted(() => ({
getRepoById: vi.fn(),
@@ -30,7 +29,7 @@ const mocks = vi.hoisted(() => ({
resolveOpenCodeModel: vi.fn(),
forward: vi.fn(),
onEvent: vi.fn(),
loggerError: vi.fn(),
loggerWarn: vi.fn(),
updateScheduleRunWorktree: vi.fn(),
stubWorktreeManager: {
prepare: vi.fn().mockResolvedValue(null),
@@ -84,9 +83,9 @@ vi.mock('../../src/services/sse-aggregator', () => ({
vi.mock('../../src/utils/logger', () => ({
logger: {
error: mocks.loggerError,
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
warn: mocks.loggerWarn,
},
}))
@@ -110,6 +109,29 @@ function textResponse(body: string, status: number = 200): Response {
return new Response(body, { status })
}
function promptReceipt(): Response {
return jsonResponse({
data: {
admittedSeq: 1,
id: 'msg-1',
sessionID: 'ses-test',
delivery: 'steer',
timeCreated: Math.floor(Date.now() / 1000),
},
})
}
function v2Messages(messages: Array<{
type: string
id?: string
content?: Array<{ type: string; text?: string }>
time?: { created?: number; completed?: number }
finish?: string
error?: { name?: string; data?: { message?: string } }
}>): Response {
return jsonResponse({ data: messages.map(m => ({ ...m, id: m.id ?? 'msg-1' })), cursor: {} })
}
function createOpenCodeClientStub(): OpenCodeClient {
return {
forward: mocks.forward,
@@ -176,7 +198,7 @@ const baseRun: ScheduleRun = {
workspaceId: null,
}
describe('ScheduleService permission ruleset in session creation', () => {
describe('ScheduleService permission auto-responder', () => {
beforeEach(() => {
vi.clearAllMocks()
Reflect.get(ScheduleService, 'activeRuns').clear()
@@ -189,32 +211,46 @@ describe('ScheduleService permission ruleset in session creation', () => {
mocks.onEvent.mockReturnValue(vi.fn())
mocks.getScheduleRunById.mockReturnValue({
...baseRun,
sessionId: 'ses-perm-test',
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
})
})
it('sends default permission ruleset when job.permissionConfig is null', async () => {
it('denies dangerous bash command with default permission config', async () => {
mocks.getScheduleJobById.mockReturnValue(baseJob)
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-1',
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/session' && method === 'POST') {
return jsonResponse({ id: 'ses-perm-1' })
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'POST') {
return textResponse('')
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [{ id: 'perm-1', action: 'bash', resources: ['sudo rm -rf /'] }] })
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
@@ -222,39 +258,61 @@ describe('ScheduleService permission ruleset in session creation', () => {
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
const sessionCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
)
expect(sessionCall).toBeDefined()
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
expect(body.title).toBe('Scheduled: Weekly engineering summary')
expect(body.agent).toBeUndefined()
await vi.waitFor(() => {
const replyCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === `/api/session/ses-perm-auto/permission/perm-1/reply`
&& (req as ForwardRequest).method === 'POST',
)
expect(replyCall).toBeDefined()
const body = JSON.parse((replyCall![0] as ForwardRequest).body!)
expect(body).toEqual({ reply: 'reject', message: 'Denied by schedule permission config' })
})
const expectedRules = buildSchedulePermissionRuleset(null)
expect(body.permission).toEqual(expectedRules)
await vi.waitFor(() => {
expect(mocks.updateScheduleRun).toHaveBeenCalledWith(
expect.anything(),
42,
7,
5,
expect.objectContaining({ status: 'completed' }),
)
})
})
it('sends custom permission ruleset when job has custom permissionConfig', async () => {
const customConfig = { allowExternalDirectory: true, bashDenyPatterns: [] }
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, permissionConfig: customConfig })
it('auto-approves benign bash command with reply: once', async () => {
mocks.getScheduleJobById.mockReturnValue(baseJob)
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-2',
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/session' && method === 'POST') {
return jsonResponse({ id: 'ses-perm-2' })
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'POST') {
return textResponse('')
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [{ id: 'perm-2', action: 'bash', resources: ['git status'] }] })
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
@@ -262,38 +320,51 @@ describe('ScheduleService permission ruleset in session creation', () => {
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
const sessionCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
)
expect(sessionCall).toBeDefined()
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
expect(body.title).toBe('Scheduled: Weekly engineering summary')
expect(body.agent).toBeUndefined()
const expectedRules = buildSchedulePermissionRuleset(customConfig)
expect(body.permission).toEqual(expectedRules)
await vi.waitFor(() => {
const replyCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === `/api/session/ses-perm-auto/permission/perm-2/reply`
&& (req as ForwardRequest).method === 'POST',
)
expect(replyCall).toBeDefined()
const body = JSON.parse((replyCall![0] as ForwardRequest).body!)
expect(body).toEqual({ reply: 'once' })
})
})
it('preserves existing fields (title, agent) when permission is added', async () => {
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, agentSlug: 'my-agent' })
it('rejects external_directory with allowExternalDirectory: false', async () => {
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, permissionConfig: { allowExternalDirectory: false, bashDenyPatterns: [] } })
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-3',
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/session' && method === 'POST') {
return jsonResponse({ id: 'ses-perm-3' })
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'POST') {
return textResponse('')
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [{ id: 'perm-3', action: 'external_directory', resources: ['/etc'] }] })
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
@@ -301,14 +372,223 @@ describe('ScheduleService permission ruleset in session creation', () => {
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
const sessionCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
)
expect(sessionCall).toBeDefined()
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
expect(body.title).toBe('Scheduled: Weekly engineering summary')
expect(body.agent).toBe('my-agent')
expect(body.permission).toBeDefined()
expect(body.permission[0]).toEqual({ permission: '*', pattern: '*', action: 'allow' })
await vi.waitFor(() => {
const replyCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === `/api/session/ses-perm-auto/permission/perm-3/reply`
&& (req as ForwardRequest).method === 'POST',
)
expect(replyCall).toBeDefined()
const body = JSON.parse((replyCall![0] as ForwardRequest).body!)
expect(body).toEqual({ reply: 'reject', message: 'Denied by schedule permission config' })
})
})
it('approves external_directory with allowExternalDirectory: true', async () => {
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, permissionConfig: { allowExternalDirectory: true, bashDenyPatterns: [] } })
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [{ id: 'perm-4', action: 'external_directory', resources: ['/some/path'] }] })
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
await vi.waitFor(() => {
const replyCall = mocks.forward.mock.calls.find(
([req]) => (req as ForwardRequest).path === `/api/session/ses-perm-auto/permission/perm-4/reply`
&& (req as ForwardRequest).method === 'POST',
)
expect(replyCall).toBeDefined()
const body = JSON.parse((replyCall![0] as ForwardRequest).body!)
expect(body).toEqual({ reply: 'once' })
})
})
it('rejects pending questions', async () => {
mocks.getScheduleJobById.mockReturnValue(baseJob)
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [{ id: 'q-1' }] })
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
await vi.waitFor(() => {
expect(mocks.forward).toHaveBeenCalledWith(
expect.objectContaining({
method: 'POST',
path: `/api/session/ses-perm-auto/question/q-1/reject`,
}),
)
})
})
it('completes the run despite permission endpoint returning 500', async () => {
mocks.getScheduleJobById.mockReturnValue(baseJob)
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return textResponse('Internal Server Error', 500)
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return textResponse('Internal Server Error', 500)
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
await vi.waitFor(() => {
expect(mocks.updateScheduleRun).toHaveBeenCalledWith(
expect.anything(),
42,
7,
5,
expect.objectContaining({ status: 'completed' }),
)
})
})
it('handles individual permission reply failure without affecting other replies', async () => {
mocks.getScheduleJobById.mockReturnValue(baseJob)
const runWithSession: ScheduleRun = {
...baseRun,
sessionId: 'ses-perm-auto',
sessionTitle: 'Scheduled: Weekly engineering summary',
logText: 'Run started.',
}
let replyCount = 0
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
routeForward(({ path, method }) => {
if (path === '/api/session' && method === 'POST') {
return jsonResponse({ data: { id: 'ses-perm-auto' } })
}
if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') {
return promptReceipt()
}
if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') {
return jsonResponse({ data: [
{ id: 'perm-ok', action: 'bash', resources: ['git status'] },
{ id: 'perm-fail', action: 'bash', resources: ['sudo rm -rf /'] },
]})
}
if (path === `/api/session/ses-perm-auto/question` && method === 'GET') {
return jsonResponse({ data: [] })
}
if (path.match(/^\/api\/session\/ses-perm-auto\/permission\/[\w-]+\/reply$/) && method === 'POST') {
replyCount++
return jsonResponse({})
}
if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') {
return v2Messages([
{ type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' },
])
}
if (path === '/api/session/active' && method === 'GET') {
return jsonResponse({ data: {} })
}
if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') {
return jsonResponse({})
}
throw new Error(`Unexpected forward request: ${method} ${path}`)
})
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
await service.runJob(42, 7, 'manual')
await vi.waitFor(() => {
expect(replyCount).toBe(2)
})
})
})
File diff suppressed because it is too large. Load diff
+31 -7
View File
@@ -52,14 +52,11 @@ export interface SchedulePermissionRule {
export type SchedulePermissionRuleset = SchedulePermissionRule[]
/**
* Builds the OpenCode session permission ruleset for an unattended scheduled run.
* Builds the permission ruleset enforced by unattended scheduled runs.
*
* OpenCode's `POST /session` `permission` field expects an ordered array of
* `{ permission, pattern, action }` rules (`PermissionV1.Ruleset`), evaluated
* with last-match-wins semantics (see https://opencode.ai/docs/permissions).
* A leading `*`/`*` allow rule sets the allow-all baseline; the trailing
* `external_directory` and `bash` deny rules then override it for external
* directory access and matching destructive command patterns.
* Rules use OpenCode's last-match-wins semantics. The leading allow rule sets
* the baseline and trailing rules deny external directory access and matching
* destructive commands.
*/
export function buildSchedulePermissionRuleset(
config: SchedulePermissionConfig | null | undefined,
@@ -75,6 +72,33 @@ export function buildSchedulePermissionRuleset(
return ruleset
}
function matchesPermissionPattern(pattern: string, value: string): boolean {
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')
return new RegExp(`^${escaped}$`).test(value)
}
export function evaluateSchedulePermission(
ruleset: SchedulePermissionRuleset,
action: string,
resources: string[],
): SchedulePermissionAction {
const targets = resources.length > 0 ? resources : ['*']
const verdicts = targets.map((resource) => {
let verdict: SchedulePermissionAction = 'ask'
for (const rule of ruleset) {
if (matchesPermissionPattern(rule.permission, action) && matchesPermissionPattern(rule.pattern, resource)) {
verdict = rule.action
}
}
return verdict
})
if (verdicts.some((v) => v === 'deny')) return 'deny'
if (verdicts.some((v) => v === 'ask')) return 'ask'
return 'allow'
}
export const ScheduleJobSchema = z.object({
id: z.number(),
repoId: z.number(),