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.
This commit is contained in:
Chris Scott committed 2026-10-03 21:16:05 -04:00
1 parent 2af2577af8
commit 96e02e9646
91 files changed
+2140 -688

No files matched your search

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { act, render, screen, fireEvent } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { SessionAutomationSettings } from './SessionAutomationSettings'
import { useSettings } from '@/hooks/useSettings'
@@ -43,7 +43,7 @@ describe('SessionAutomationSettings', () => {
expect(screen.getByRole('combobox', { name: 'Default permission mode for new sessions' })).toHaveTextContent('Ask every time')
})
it('persists the chosen default permission mode', async () => {
it('persists the chosen default permission mode immediately', async () => {
const user = userEvent.setup()
const updateSettings = vi.fn()
mockUseSettings({
@@ -58,7 +58,7 @@ describe('SessionAutomationSettings', () => {
expect(updateSettings).toHaveBeenCalledWith({ sessionDefaults: { permissionMode: 'auto' } })
})
it('persists the goal auditor model', () => {
it('saves the goal auditor model once on blur with the final value', () => {
const updateSettings = vi.fn()
mockUseSettings({
preferences: { ...basePreferences, sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20 } },
@@ -66,10 +66,11 @@ describe('SessionAutomationSettings', () => {
})
render(<SessionAutomationSettings />)
fireEvent.change(screen.getByLabelText('Goal auditor model'), {
target: { value: 'anthropic/claude-sonnet-4' },
})
const input = screen.getByLabelText('Goal auditor model')
fireEvent.change(input, { target: { value: 'anthropic/claude-sonnet-4' } })
fireEvent.blur(input)
expect(updateSettings).toHaveBeenCalledTimes(1)
expect(updateSettings).toHaveBeenCalledWith({
sessionDefaults: {
permissionMode: 'ask',
@@ -79,7 +80,37 @@ describe('SessionAutomationSettings', () => {
})
})
it('persists the max automatic continuations', () => {
it('saves the goal auditor model after the debounce without blurring', () => {
vi.useFakeTimers()
try {
const updateSettings = vi.fn()
mockUseSettings({
preferences: { ...basePreferences, sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20 } },
updateSettings,
})
render(<SessionAutomationSettings />)
fireEvent.change(screen.getByLabelText('Goal auditor model'), {
target: { value: 'anthropic/claude-sonnet-4' },
})
act(() => {
vi.advanceTimersByTime(800)
})
expect(updateSettings).toHaveBeenCalledTimes(1)
expect(updateSettings).toHaveBeenCalledWith({
sessionDefaults: {
permissionMode: 'ask',
goalMaxContinuations: 20,
goalAuditorModel: 'anthropic/claude-sonnet-4',
},
})
} finally {
vi.useRealTimers()
}
})
it('saves an in-range max automatic continuations on blur', () => {
const updateSettings = vi.fn()
mockUseSettings({
preferences: { ...basePreferences, sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20 } },
@@ -87,16 +118,16 @@ describe('SessionAutomationSettings', () => {
})
render(<SessionAutomationSettings />)
fireEvent.change(screen.getByLabelText('Max automatic continuations'), {
target: { value: '50' },
})
const input = screen.getByLabelText('Max automatic continuations')
fireEvent.change(input, { target: { value: '50' } })
fireEvent.blur(input)
expect(updateSettings).toHaveBeenCalledWith({
sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 50 },
})
})
it('persists the token budget per goal', () => {
it('does not save out-of-range max automatic continuations', () => {
const updateSettings = vi.fn()
mockUseSettings({
preferences: { ...basePreferences, sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20 } },
@@ -104,9 +135,24 @@ describe('SessionAutomationSettings', () => {
})
render(<SessionAutomationSettings />)
fireEvent.change(screen.getByLabelText('Token budget per goal'), {
target: { value: '5000' },
const input = screen.getByLabelText('Max automatic continuations')
fireEvent.change(input, { target: { value: '500' } })
fireEvent.blur(input)
expect(updateSettings).not.toHaveBeenCalled()
})
it('saves the token budget per goal on blur', () => {
const updateSettings = vi.fn()
mockUseSettings({
preferences: { ...basePreferences, sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20 } },
updateSettings,
})
render(<SessionAutomationSettings />)
const input = screen.getByLabelText('Token budget per goal')
fireEvent.change(input, { target: { value: '5000' } })
fireEvent.blur(input)
expect(updateSettings).toHaveBeenCalledWith({
sessionDefaults: { permissionMode: 'ask', goalMaxContinuations: 20, goalTokenBudget: 5000 },
@@ -1,26 +1,97 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { useSettings } from '@/hooks/useSettings'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import type { SessionDefaults, SessionPermissionMode } from '@opencode-manager/shared/schemas'
import {
DEFAULT_SESSION_DEFAULTS,
GOAL_MAX_CONTINUATIONS_MAX,
GOAL_MAX_CONTINUATIONS_MIN,
type SessionDefaults,
type SessionPermissionMode,
} from '@opencode-manager/shared/schemas'
const AUTOSAVE_DELAY_MS = 800
export function SessionAutomationSettings() {
const { preferences, updateSettings } = useSettings()
const sessionDefaults = preferences?.sessionDefaults
const permissionMode = sessionDefaults?.permissionMode ?? 'ask'
const goalMaxContinuations = sessionDefaults?.goalMaxContinuations ?? 20
const goalTokenBudget = sessionDefaults?.goalTokenBudget
const goalAuditorModel = sessionDefaults?.goalAuditorModel ?? ''
const permissionMode = sessionDefaults?.permissionMode ?? DEFAULT_SESSION_DEFAULTS.permissionMode
const updateSessionDefaults = (patch: Partial<SessionDefaults>) => {
updateSettings({
sessionDefaults: {
...sessionDefaults,
permissionMode,
...patch,
},
})
}
const [goalAuditorModel, setGoalAuditorModel] = useState(sessionDefaults?.goalAuditorModel ?? '')
const [goalMaxContinuations, setGoalMaxContinuations] = useState(
String(sessionDefaults?.goalMaxContinuations ?? DEFAULT_SESSION_DEFAULTS.goalMaxContinuations),
)
const [goalTokenBudget, setGoalTokenBudget] = useState(
sessionDefaults?.goalTokenBudget !== undefined ? String(sessionDefaults.goalTokenBudget) : '',
)
const committed = useRef({
goalAuditorModel: sessionDefaults?.goalAuditorModel,
goalMaxContinuations: sessionDefaults?.goalMaxContinuations ?? DEFAULT_SESSION_DEFAULTS.goalMaxContinuations,
goalTokenBudget: sessionDefaults?.goalTokenBudget,
})
useEffect(() => {
setGoalAuditorModel(sessionDefaults?.goalAuditorModel ?? '')
setGoalMaxContinuations(
String(sessionDefaults?.goalMaxContinuations ?? DEFAULT_SESSION_DEFAULTS.goalMaxContinuations),
)
setGoalTokenBudget(
sessionDefaults?.goalTokenBudget !== undefined ? String(sessionDefaults.goalTokenBudget) : '',
)
committed.current = {
goalAuditorModel: sessionDefaults?.goalAuditorModel,
goalMaxContinuations: sessionDefaults?.goalMaxContinuations ?? DEFAULT_SESSION_DEFAULTS.goalMaxContinuations,
goalTokenBudget: sessionDefaults?.goalTokenBudget,
}
}, [sessionDefaults])
const updateSessionDefaults = useCallback(
(patch: Partial<SessionDefaults>) => {
updateSettings({
sessionDefaults: {
...sessionDefaults,
permissionMode,
...patch,
},
})
},
[sessionDefaults, permissionMode, updateSettings],
)
const commitGoalAuditorModel = useCallback(() => {
const next = goalAuditorModel.trim() || undefined
if (next === committed.current.goalAuditorModel) return
committed.current.goalAuditorModel = next
updateSessionDefaults({ goalAuditorModel: next })
}, [goalAuditorModel, updateSessionDefaults])
const commitGoalMaxContinuations = useCallback(() => {
const value = Number(goalMaxContinuations)
if (!Number.isInteger(value) || value < GOAL_MAX_CONTINUATIONS_MIN || value > GOAL_MAX_CONTINUATIONS_MAX) return
if (value === committed.current.goalMaxContinuations) return
committed.current.goalMaxContinuations = value
updateSessionDefaults({ goalMaxContinuations: value })
}, [goalMaxContinuations, updateSessionDefaults])
const commitGoalTokenBudget = useCallback(() => {
const next = goalTokenBudget.trim() === '' ? undefined : Number(goalTokenBudget)
if (next !== undefined && (!Number.isInteger(next) || next <= 0)) return
if (next === committed.current.goalTokenBudget) return
committed.current.goalTokenBudget = next
updateSessionDefaults({ goalTokenBudget: next })
}, [goalTokenBudget, updateSessionDefaults])
useEffect(() => {
const timer = setTimeout(() => {
commitGoalAuditorModel()
commitGoalMaxContinuations()
commitGoalTokenBudget()
}, AUTOSAVE_DELAY_MS)
return () => clearTimeout(timer)
}, [commitGoalAuditorModel, commitGoalMaxContinuations, commitGoalTokenBudget])
return (
<div className="space-y-6">
@@ -59,7 +130,8 @@ export function SessionAutomationSettings() {
value={goalAuditorModel}
placeholder="provider/model"
className="w-full shrink-0 sm:w-64"
onChange={(event) => updateSessionDefaults({ goalAuditorModel: event.target.value.trim() || undefined })}
onChange={(event) => setGoalAuditorModel(event.target.value)}
onBlur={commitGoalAuditorModel}
/>
</div>
@@ -73,16 +145,12 @@ export function SessionAutomationSettings() {
<Input
id="goalMaxContinuations"
type="number"
min={1}
max={200}
min={GOAL_MAX_CONTINUATIONS_MIN}
max={GOAL_MAX_CONTINUATIONS_MAX}
value={goalMaxContinuations}
className="w-full shrink-0 sm:w-40"
onChange={(event) => {
const value = Number(event.target.value)
if (Number.isInteger(value) && value >= 1 && value <= 200) {
updateSessionDefaults({ goalMaxContinuations: value })
}
}}
onChange={(event) => setGoalMaxContinuations(event.target.value)}
onBlur={commitGoalMaxContinuations}
/>
</div>
@@ -97,20 +165,11 @@ export function SessionAutomationSettings() {
id="goalTokenBudget"
type="number"
min={1}
value={goalTokenBudget ?? ''}
value={goalTokenBudget}
placeholder="No limit"
className="w-full shrink-0 sm:w-40"
onChange={(event) => {
const raw = event.target.value
if (raw === '') {
updateSessionDefaults({ goalTokenBudget: undefined })
return
}
const value = Number(raw)
if (Number.isInteger(value) && value > 0) {
updateSessionDefaults({ goalTokenBudget: value })
}
}}
onChange={(event) => setGoalTokenBudget(event.target.value)}
onBlur={commitGoalTokenBudget}
/>
</div>
</div>