mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(multi-run): add a runs sheet with inline fusion
Replace the centered Multi-run dialog and the stacked Fusion dialog with one right-side runs sheet, turn fusion into an inline selection mode, and pre-approve each selected source workspace for the fusion session. - Move MultiRunDialog into a SideDrawer with Runs / New run tabs; add MultiRunCard, FusionComposer and an optimistic starting fusion. - Add buildFusionSourcePermissionRuleset (external_directory allow, edit deny) and thread permissions through SessionLauncher.launch. - Apply the default permission mode to fusion sessions after launch. - Let ModelCombobox and BranchCombobox take a listClassName; make SideDrawer ignore an Escape already handled by a nested layer.
This commit is contained in:
1 parent
b0361cd98b
commit
f2e06abb62
23 files changed
+2122
-1736
No files matched your search
@@ -271,7 +271,7 @@ const sessionGoalService = new SessionGoalService(db, openCodeClient, sessionSet
|
||||
})
|
||||
sessionGoalService.loadOpenGoals()
|
||||
|
||||
const multiRunService = new MultiRunService(db, openCodeClient, repoWorkspaces)
|
||||
const multiRunService = new MultiRunService(db, openCodeClient, repoWorkspaces, sessionPermissionModeService)
|
||||
const changeWalkthroughService = new ChangeWalkthroughService(db, openCodeClient)
|
||||
|
||||
sseAggregator.onEvent((directory, event) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resolve } from 'node:path'
|
||||
import {
|
||||
isSessionNotFoundError,
|
||||
type FileDiffInfo,
|
||||
@@ -7,6 +8,7 @@ import {
|
||||
FUSION_PROMPT_MAX_LENGTH,
|
||||
type FusionUnavailableSource,
|
||||
type MultiRunFusionSource,
|
||||
type SchedulePermissionRuleset,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import type { MultiRunEntryRecord } from '../db/multi-runs'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
@@ -42,6 +44,33 @@ const FUSION_REPLY_BUDGET_RATIO = 0.4
|
||||
|
||||
const FUSION_BLOCK_SEPARATOR = '\n\n'
|
||||
|
||||
/**
|
||||
* Builds the OpenCode session permission ruleset that lets a fusion session read
|
||||
* its selected source workspaces while denying any edit to them.
|
||||
*
|
||||
* Each unique directory is normalized with `resolve` before a pair of rules is
|
||||
* emitted: an `external_directory` allow so the session may read outside its own
|
||||
* workspace, followed by an `edit` deny so the read-only reference cannot be
|
||||
* mutated. Deduplication happens after normalization.
|
||||
*/
|
||||
export function buildFusionSourcePermissionRuleset(directories: string[]): SchedulePermissionRuleset {
|
||||
const seen = new Set<string>()
|
||||
const ruleset: SchedulePermissionRuleset = []
|
||||
|
||||
for (const directory of directories) {
|
||||
const normalized = resolve(directory).replaceAll('\\', '/')
|
||||
if (seen.has(normalized)) {
|
||||
continue
|
||||
}
|
||||
|
||||
seen.add(normalized)
|
||||
ruleset.push({ action: 'external_directory', resource: `${normalized}/*`, effect: 'allow' })
|
||||
ruleset.push({ action: 'edit', resource: `${normalized}/*`, effect: 'deny' })
|
||||
}
|
||||
|
||||
return ruleset
|
||||
}
|
||||
|
||||
export class FusionContextLimitError extends ServiceError {
|
||||
constructor(requiredPerSource: number, availablePerSource: number) {
|
||||
super(
|
||||
|
||||
@@ -19,11 +19,12 @@ import { getRepoById } from '../db/queries'
|
||||
import type { Repo } from '../types/repo'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
import { ServiceError } from '../utils/service-error'
|
||||
import { buildFusionPrompt, collectFusionSources } from './multi-run-fusion'
|
||||
import { buildFusionPrompt, buildFusionSourcePermissionRuleset, collectFusionSources } from './multi-run-fusion'
|
||||
import type { OpenCodeClient } from './opencode/client'
|
||||
import { RepoWorkspaceError } from './repo'
|
||||
import type { RepoWorkspaceService } from './repo-workspace'
|
||||
import { requireReadyRepo, SessionLauncher, SessionLaunchError, type LaunchedSession } from './session-launcher'
|
||||
import type { SessionPermissionModeService } from './session-permission-modes'
|
||||
|
||||
const MULTI_RUN_LIST_LIMIT = 20
|
||||
|
||||
@@ -75,6 +76,7 @@ export class MultiRunService {
|
||||
private readonly db: Database,
|
||||
private readonly openCodeClient: OpenCodeClient,
|
||||
private readonly repoWorkspaces: RepoWorkspaceService,
|
||||
private readonly permissionModes: SessionPermissionModeService,
|
||||
) {
|
||||
this.sessionLauncher = new SessionLauncher(db, openCodeClient, repoWorkspaces)
|
||||
}
|
||||
@@ -194,6 +196,12 @@ export class MultiRunService {
|
||||
}
|
||||
}
|
||||
|
||||
const sourcePermissions = buildFusionSourcePermissionRuleset(
|
||||
selectedEntries
|
||||
.map((entry) => entry.directory)
|
||||
.filter((directory): directory is string => directory !== null),
|
||||
)
|
||||
|
||||
try {
|
||||
await this.sessionLauncher.resolveModel(repo, request.model)
|
||||
} catch (error) {
|
||||
@@ -241,6 +249,7 @@ export class MultiRunService {
|
||||
model: request.model,
|
||||
title: `${record.name} · fusion`,
|
||||
...(request.agent ? { agent: request.agent } : {}),
|
||||
...(sourcePermissions.length > 0 ? { permissions: sourcePermissions } : {}),
|
||||
...(request.isolate
|
||||
? {
|
||||
workspace: {
|
||||
@@ -256,6 +265,8 @@ export class MultiRunService {
|
||||
sessionId: launched.sessionId,
|
||||
directory: launched.directory,
|
||||
})
|
||||
|
||||
await this.permissionModes.applyDefaultMode(launched.sessionId, launched.directory)
|
||||
} catch (error) {
|
||||
const launchError = error instanceof SessionLaunchError ? error : null
|
||||
const workspaceDirectory = launchError?.workspaceDirectory ?? null
|
||||
|
||||
@@ -8,6 +8,8 @@ import type { OpenCodeClient } from './opencode/client'
|
||||
import { resolveOpenCodeModel, type ResolvedOpenCodeModel } from './opencode-models'
|
||||
import type { RepoWorkspaceService } from './repo-workspace'
|
||||
|
||||
type SessionCreateInput = NonNullable<Parameters<OpenCodeClient['api']['session']['create']>[0]>
|
||||
|
||||
interface LaunchSessionInput {
|
||||
repoId: number
|
||||
prompt: string
|
||||
@@ -15,6 +17,7 @@ interface LaunchSessionInput {
|
||||
model?: string
|
||||
agent?: string
|
||||
workspace?: { name?: string; ref?: string }
|
||||
permissions?: SessionCreateInput['permissions']
|
||||
}
|
||||
|
||||
export interface LaunchedSession {
|
||||
@@ -109,6 +112,7 @@ export class SessionLauncher {
|
||||
session = await this.openCodeClient.api.session.create({
|
||||
...(input.title ? { title: input.title } : {}),
|
||||
...(input.agent ? { agent: input.agent } : {}),
|
||||
...(input.permissions ? { permissions: input.permissions } : {}),
|
||||
model: {
|
||||
providerID: model.providerID,
|
||||
id: model.id,
|
||||
|
||||
@@ -126,6 +126,21 @@ export class SessionPermissionModeService {
|
||||
return this.settingsService.getSettings().preferences.sessionDefaults?.permissionMode ?? 'ask'
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the default permission mode for a session after it has been launched
|
||||
* and accepts any permission requests it raised before the mode was stored.
|
||||
*
|
||||
* Does nothing when the default mode is `ask`, since that needs no stored row.
|
||||
*/
|
||||
async applyDefaultMode(sessionId: string, directory: string): Promise<void> {
|
||||
if (this.defaultMode() !== 'auto') {
|
||||
return
|
||||
}
|
||||
|
||||
insertSessionPermissionModeIfAbsent(this.db, sessionId, 'auto')
|
||||
await this.acceptPendingRequestsInDirectory(directory, sessionId)
|
||||
}
|
||||
|
||||
async acceptPendingRequestsForActiveSessions(): Promise<void> {
|
||||
try {
|
||||
const active = await this.openCodeClient.api.session.active()
|
||||
|
||||
@@ -4,10 +4,13 @@ import { createRepo, deleteRepo } from '../../src/db/queries'
|
||||
import { migrate } from '../../src/db/migration-runner'
|
||||
import { allMigrations } from '../../src/db/migrations'
|
||||
import { createMultiRunWithEntries } from '../../src/db/multi-runs'
|
||||
import { getSessionPermissionMode } from '../../src/db/session-permission-modes'
|
||||
import { FusionContextLimitError } from '../../src/services/multi-run-fusion'
|
||||
import { MultiRunError, MultiRunService } from '../../src/services/multi-runs'
|
||||
import { RepoWorkspaceError } from '../../src/services/repo'
|
||||
import type { RepoWorkspaceService } from '../../src/services/repo-workspace'
|
||||
import { SessionPermissionModeService } from '../../src/services/session-permission-modes'
|
||||
import { SettingsService } from '../../src/services/settings'
|
||||
import type { Repo } from '../../src/types/repo'
|
||||
import type { OpenCodeClient } from '../../src/services/opencode/client'
|
||||
import { assistantMessage } from '../helpers/stub-schedule-api'
|
||||
@@ -109,6 +112,9 @@ function createClient(sessions: Record<string, FakeSessionConfig> = {}): FakeMul
|
||||
|
||||
const sessionDiff = vi.fn(async () => [])
|
||||
|
||||
const permissionRequestList = vi.fn(async () => ({ data: [] }))
|
||||
const permissionReply = vi.fn(async () => ({}))
|
||||
|
||||
const client = {
|
||||
api: {
|
||||
session: {
|
||||
@@ -119,6 +125,10 @@ function createClient(sessions: Record<string, FakeSessionConfig> = {}): FakeMul
|
||||
diff: sessionDiff,
|
||||
},
|
||||
message: { list: messageList },
|
||||
permission: {
|
||||
request: { list: permissionRequestList },
|
||||
reply: permissionReply,
|
||||
},
|
||||
},
|
||||
} as unknown as OpenCodeClient
|
||||
|
||||
@@ -198,8 +208,16 @@ describe('MultiRunService', () => {
|
||||
return repo.id
|
||||
}
|
||||
|
||||
function createService(client: OpenCodeClient, repoWorkspaces: RepoWorkspaceService): MultiRunService {
|
||||
return new MultiRunService(db, client, repoWorkspaces)
|
||||
function createService(
|
||||
client: OpenCodeClient,
|
||||
repoWorkspaces: RepoWorkspaceService,
|
||||
permissionModes: SessionPermissionModeService = new SessionPermissionModeService(
|
||||
db,
|
||||
client,
|
||||
new SettingsService(db),
|
||||
),
|
||||
): MultiRunService {
|
||||
return new MultiRunService(db, client, repoWorkspaces, permissionModes)
|
||||
}
|
||||
|
||||
it('launches one session per model, each in its own workspace named from the group', async () => {
|
||||
@@ -641,6 +659,98 @@ describe('MultiRunService', () => {
|
||||
).toEqual(before)
|
||||
})
|
||||
|
||||
it('grants read access to the selected source workspaces and denies edits when fusing', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, sessionCreate } = createClient()
|
||||
const repoWorkspaces = createRepoWorkspaces()
|
||||
const service = createService(client, repoWorkspaces.service)
|
||||
|
||||
const run = await service.launch({
|
||||
repoId,
|
||||
name: 'Sweep',
|
||||
prompt: 'go',
|
||||
models: ['openai/a', 'openai/b', 'openai/c'],
|
||||
isolate: true,
|
||||
})
|
||||
const entryIds = run.entries.map((entry) => entry.id)
|
||||
sessionCreate.mockClear()
|
||||
|
||||
await service.fuse(run.id, {
|
||||
requestId: FUSE_REQUEST_ID,
|
||||
entryIds: [entryIds[0]!, entryIds[2]!],
|
||||
model: 'openai/d',
|
||||
isolate: true,
|
||||
})
|
||||
|
||||
expect(sessionCreate).toHaveBeenCalledTimes(1)
|
||||
expect(sessionCreate.mock.calls[0]![0].permissions).toEqual([
|
||||
{ action: 'external_directory', resource: '/worktrees/Sweep-1/*', effect: 'allow' },
|
||||
{ action: 'edit', resource: '/worktrees/Sweep-1/*', effect: 'deny' },
|
||||
{ action: 'external_directory', resource: '/worktrees/Sweep-3/*', effect: 'allow' },
|
||||
{ action: 'edit', resource: '/worktrees/Sweep-3/*', effect: 'deny' },
|
||||
])
|
||||
const serializedPermissions = JSON.stringify(sessionCreate.mock.calls[0]![0].permissions)
|
||||
expect(serializedPermissions).not.toContain('Sweep-2')
|
||||
expect(serializedPermissions).not.toContain('Sweep-fusion-1')
|
||||
})
|
||||
|
||||
it('stores the default auto mode on the fusion session', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, sessionCreate } = createClient()
|
||||
const repoWorkspaces = createRepoWorkspaces()
|
||||
const settingsService = new SettingsService(db)
|
||||
settingsService.updateSettings({ sessionDefaults: { permissionMode: 'auto' } })
|
||||
const permissionModes = new SessionPermissionModeService(db, client, settingsService)
|
||||
const service = createService(client, repoWorkspaces.service, permissionModes)
|
||||
|
||||
const run = await service.launch({
|
||||
repoId,
|
||||
name: 'Sweep',
|
||||
prompt: 'go',
|
||||
models: ['openai/a', 'openai/b'],
|
||||
isolate: true,
|
||||
})
|
||||
const entryIds = run.entries.map((entry) => entry.id)
|
||||
sessionCreate.mockClear()
|
||||
|
||||
const { run: fused } = await service.fuse(run.id, {
|
||||
requestId: FUSE_REQUEST_ID,
|
||||
entryIds,
|
||||
model: 'openai/c',
|
||||
isolate: false,
|
||||
})
|
||||
|
||||
const fusionSessionId = fused.fusions[0]!.sessionId!
|
||||
expect(getSessionPermissionMode(db, fusionSessionId)).toBe('auto')
|
||||
})
|
||||
|
||||
it('stores no mode on the fusion session when the default is ask', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, sessionCreate } = createClient()
|
||||
const repoWorkspaces = createRepoWorkspaces()
|
||||
const service = createService(client, repoWorkspaces.service)
|
||||
|
||||
const run = await service.launch({
|
||||
repoId,
|
||||
name: 'Sweep',
|
||||
prompt: 'go',
|
||||
models: ['openai/a', 'openai/b'],
|
||||
isolate: true,
|
||||
})
|
||||
const entryIds = run.entries.map((entry) => entry.id)
|
||||
sessionCreate.mockClear()
|
||||
|
||||
const { run: fused } = await service.fuse(run.id, {
|
||||
requestId: FUSE_REQUEST_ID,
|
||||
entryIds,
|
||||
model: 'openai/c',
|
||||
isolate: false,
|
||||
})
|
||||
|
||||
const fusionSessionId = fused.fusions[0]!.sessionId!
|
||||
expect(getSessionPermissionMode(db, fusionSessionId)).toBeNull()
|
||||
})
|
||||
|
||||
it('returns the same fusion for a repeated requestId and launches once', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, sessionCreate } = createClient()
|
||||
|
||||
@@ -122,6 +122,32 @@ describe('SessionLauncher', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('forwards a permission ruleset to session creation when provided', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, create } = createClient()
|
||||
const { service } = createRepoWorkspaces()
|
||||
const launcher = new SessionLauncher(db, client, service)
|
||||
const permissions = [
|
||||
{ action: 'external_directory', resource: '/repos/source/*', effect: 'allow' as const },
|
||||
{ action: 'edit', resource: '/repos/source/*', effect: 'deny' as const },
|
||||
]
|
||||
|
||||
await launcher.launch({ repoId, prompt: 'hello', permissions })
|
||||
|
||||
expect(create).toHaveBeenCalledWith(expect.objectContaining({ permissions }))
|
||||
})
|
||||
|
||||
it('omits the permissions key from session creation when none are given', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, create } = createClient()
|
||||
const { service } = createRepoWorkspaces()
|
||||
const launcher = new SessionLauncher(db, client, service)
|
||||
|
||||
await launcher.launch({ repoId, prompt: 'hello' })
|
||||
|
||||
expect(create.mock.calls[0]![0]).not.toHaveProperty('permissions')
|
||||
})
|
||||
|
||||
it('launches in a new workspace and prompts there', async () => {
|
||||
const repoId = readyRepo()
|
||||
const { client, create } = createClient()
|
||||
|
||||
@@ -177,6 +177,54 @@ describe('SessionPermissionModeService', () => {
|
||||
expect(service.defaultMode()).toBe('auto')
|
||||
})
|
||||
|
||||
it('stores the default auto mode for a launched session', async () => {
|
||||
const settingsService = new SettingsService(db)
|
||||
settingsService.updateSettings({ sessionDefaults: { permissionMode: 'auto' } })
|
||||
const service = new SessionPermissionModeService(db, createFakeSessionPermissionClient(), settingsService)
|
||||
|
||||
await service.applyDefaultMode('ses_launched', DIRECTORY)
|
||||
|
||||
expect(getSessionPermissionMode(db, 'ses_launched')).toBe('auto')
|
||||
})
|
||||
|
||||
it('stores no mode for a launched session when the default is ask', async () => {
|
||||
const service = new SessionPermissionModeService(db, createFakeSessionPermissionClient(), new SettingsService(db))
|
||||
|
||||
await service.applyDefaultMode('ses_launched', DIRECTORY)
|
||||
|
||||
expect(getSessionPermissionMode(db, 'ses_launched')).toBeNull()
|
||||
})
|
||||
|
||||
it('does not override an existing stored ask when applying the default auto mode', async () => {
|
||||
const settingsService = new SettingsService(db)
|
||||
settingsService.updateSettings({ sessionDefaults: { permissionMode: 'auto' } })
|
||||
setSessionPermissionMode(db, 'ses_launched', 'ask')
|
||||
const service = new SessionPermissionModeService(db, createFakeSessionPermissionClient(), settingsService)
|
||||
|
||||
await service.applyDefaultMode('ses_launched', DIRECTORY)
|
||||
|
||||
expect(getSessionPermissionMode(db, 'ses_launched')).toBe('ask')
|
||||
})
|
||||
|
||||
it('accepts a pending request raised before the launched session mode was recorded', async () => {
|
||||
const settingsService = new SettingsService(db)
|
||||
settingsService.updateSettings({ sessionDefaults: { permissionMode: 'auto' } })
|
||||
const client = createFakeSessionPermissionClient({
|
||||
parents: { ses_launched: null },
|
||||
pendingRequests: { [DIRECTORY]: [{ id: 'perm-launched', sessionID: 'ses_launched' }] },
|
||||
})
|
||||
const service = new SessionPermissionModeService(db, client, settingsService)
|
||||
|
||||
await service.applyDefaultMode('ses_launched', DIRECTORY)
|
||||
|
||||
expect(client.replyPermission).toHaveBeenCalledTimes(1)
|
||||
expect(client.replyPermission).toHaveBeenCalledWith({
|
||||
sessionID: 'ses_launched',
|
||||
requestID: 'perm-launched',
|
||||
decision: 'once',
|
||||
})
|
||||
})
|
||||
|
||||
it('auto-accepts a permission request for an auto root session', async () => {
|
||||
setSessionPermissionMode(db, 'ses_root', 'auto')
|
||||
const client = createFakeSessionPermissionClient({ parents: { ses_root: null } })
|
||||
|
||||
Reference in new issue
Block a user