Files
opencode-manager/backend/src/services/session-goal-audit.ts
T
Chris Scott 96e02e9646 feat(sessions): add permission modes, session goals, ocm session tool and multi-run
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.
2026-10-03 21:16:05 -04:00

136 lines
3.3 KiB
TypeScript

import { SessionGoalVerdictSchema, type SessionGoalVerdict } from '@opencode-manager/shared/schemas'
import { truncateSessionReply } from './session-reply'
export const GOAL_AUDIT_REASON_MAX_CHARS = 500
export const GOAL_AUDIT_UNPARSED_REASON = 'Auditor response was not understood'
export interface GoalAuditPromptInput {
objective: string
reply: string | null
}
export interface GoalContinuationPromptInput {
objective: string
reason: string
}
export interface GoalVerdict {
verdict: SessionGoalVerdict
reason: string
}
export function buildGoalAuditPrompt({ objective, reply }: GoalAuditPromptInput): string {
return [
'You audit whether an autonomous coding goal has been achieved.',
'You can only see the goal objective and the agent\'s latest reply. You cannot run tools or read files.',
'',
'Goal objective:',
objective,
'',
'Agent\'s latest reply:',
truncateReply(reply),
'',
'Decide the goal state:',
'- "done": the objective is verifiably achieved based on the reply.',
'- "blocked": the agent needs a user decision or access it cannot obtain.',
'- "continue": anything else, including when the reply is not enough to confirm completion.',
'',
'Respond with only this JSON and nothing else:',
'{"verdict":"continue"|"done"|"blocked","reason":"<one sentence>"}',
].join('\n')
}
export function buildGoalContinuationPrompt({ objective, reason }: GoalContinuationPromptInput): string {
return [
`Continue working toward this goal: ${objective}`,
'',
`Progress check: ${reason}`,
'',
'Keep working until the goal is achieved. If you need a user decision or access you cannot obtain, stop and state exactly what you need.',
].join('\n')
}
export function parseGoalVerdict(text: string): GoalVerdict | null {
const block = extractFirstJsonObject(text)
if (!block) {
return null
}
let parsed: unknown
try {
parsed = JSON.parse(block)
} catch {
return null
}
if (typeof parsed !== 'object' || parsed === null) {
return null
}
const candidate = parsed as { verdict?: unknown; reason?: unknown }
const verdict = SessionGoalVerdictSchema.safeParse(candidate.verdict)
if (!verdict.success) {
return null
}
if (typeof candidate.reason !== 'string') {
return null
}
const reason = candidate.reason.trim()
if (!reason) {
return null
}
return { verdict: verdict.data, reason: reason.slice(0, GOAL_AUDIT_REASON_MAX_CHARS) }
}
function truncateReply(reply: string | null): string {
if (!reply) {
return '(no reply yet)'
}
return truncateSessionReply(reply)
}
function extractFirstJsonObject(text: string): string | null {
const start = text.indexOf('{')
if (start === -1) {
return null
}
let depth = 0
let inString = false
let escaped = false
for (let index = start; index < text.length; index += 1) {
const char = text[index]
if (inString) {
if (escaped) {
escaped = false
} else if (char === '\\') {
escaped = true
} else if (char === '"') {
inString = false
}
continue
}
if (char === '"') {
inString = true
continue
}
if (char === '{') {
depth += 1
} else if (char === '}') {
depth -= 1
if (depth === 0) {
return text.slice(start, index + 1)
}
}
}
return null
}