Files
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

287 lines
8.6 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import * as db from '../../src/db/queries'
const mockDb = {
prepare: vi.fn(),
exec: vi.fn(),
close: vi.fn(),
transaction: vi.fn()
} as any
vi.mock('bun:sqlite', () => ({
Database: vi.fn(() => mockDb)
}))
describe('Database Queries', () => {
beforeEach(() => {
vi.clearAllMocks()
mockDb.prepare.mockReset()
})
describe('createRepo', () => {
it('should insert new repo record', () => {
const clonedAt = Date.now()
const repo = {
repoUrl: 'https://github.com/test/repo',
localPath: 'repos/test-repo',
sourcePath: '/Users/test/repos/test-repo',
branch: 'main',
defaultBranch: 'main',
cloneStatus: 'ready' as const,
clonedAt,
isWorktree: false,
isLocal: true,
}
const existingCheckSourceStmt = {
get: vi.fn().mockReturnValue(undefined)
}
const existingCheckLocalStmt = {
get: vi.fn().mockReturnValue(undefined)
}
const insertStmt = {
run: vi.fn().mockReturnValue({ changes: 1, lastInsertRowid: 1 })
}
const selectStmt = {
get: vi.fn().mockReturnValue({
id: 1,
repo_url: repo.repoUrl,
local_path: repo.localPath,
source_path: repo.sourcePath,
branch: repo.branch,
default_branch: repo.defaultBranch,
clone_status: repo.cloneStatus,
cloned_at: repo.clonedAt,
last_accessed_at: clonedAt,
is_worktree: 0
})
}
mockDb.prepare
.mockReturnValueOnce(existingCheckSourceStmt)
.mockReturnValueOnce(existingCheckLocalStmt)
.mockReturnValueOnce(insertStmt)
.mockReturnValueOnce(selectStmt)
const result = db.createRepo(mockDb, repo)
expect(mockDb.prepare).toHaveBeenCalled()
expect(insertStmt.run).toHaveBeenCalledWith(
repo.repoUrl,
repo.localPath,
repo.sourcePath,
repo.branch || null,
repo.defaultBranch,
repo.cloneStatus,
repo.clonedAt,
clonedAt,
repo.isWorktree ? 1 : 0,
1
)
expect(result.id).toBe(1)
})
})
describe('getRepoById', () => {
it('should retrieve repo by ID', () => {
const clonedAt = Date.now()
const lastAccessedAt = Date.now()
const repoRow = {
id: 1,
repo_url: 'https://github.com/test/repo',
local_path: 'repos/test-repo',
source_path: '/Users/test/repos/test-repo',
branch: 'main',
default_branch: 'main',
clone_status: 'ready',
cloned_at: clonedAt,
last_pulled: null,
last_accessed_at: lastAccessedAt,
is_worktree: 0,
is_local: 0
}
const stmt = {
get: vi.fn().mockReturnValue(repoRow)
}
mockDb.prepare.mockReturnValue(stmt)
const result = db.getRepoById(mockDb, 1)
expect(result).toEqual({
id: 1,
repoUrl: 'https://github.com/test/repo',
localPath: 'repos/test-repo',
fullPath: '/Users/test/repos/test-repo',
sourcePath: '/Users/test/repos/test-repo',
branch: 'main',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: clonedAt,
lastPulled: null,
lastAccessedAt: lastAccessedAt,
isWorktree: undefined,
isLocal: undefined
})
})
it('should return null for non-existent repo', () => {
const stmt = {
get: vi.fn().mockReturnValue(undefined)
}
mockDb.prepare.mockReturnValue(stmt)
const result = db.getRepoById(mockDb, 999)
expect(result).toBeNull()
})
})
describe('listRepos', () => {
it('should return all repos', () => {
const repoRows = [
{
id: 1,
repo_url: 'https://github.com/test/repo1',
local_path: 'repos/test-repo1',
branch: 'main',
default_branch: 'main',
clone_status: 'ready',
cloned_at: Date.now(),
is_worktree: 0
},
{
id: 2,
repo_url: 'https://github.com/test/repo2',
local_path: 'repos/test-repo2',
branch: 'main',
default_branch: 'main',
clone_status: 'ready',
cloned_at: Date.now(),
is_worktree: 0
}
]
const stmt = {
all: vi.fn().mockReturnValue(repoRows)
}
mockDb.prepare.mockReturnValue(stmt)
const result = db.listRepos(mockDb)
expect(mockDb.prepare).toHaveBeenCalledWith(
expect.stringContaining('SELECT * FROM repos ORDER BY cloned_at DESC')
)
expect(stmt.all).toHaveBeenCalled()
expect(result).toHaveLength(2)
expect(result[0]?.repoUrl).toBe('https://github.com/test/repo1')
})
})
describe('updateRepoStatus', () => {
it('should update repo clone status', () => {
const stmt = {
run: vi.fn().mockReturnValue({ changes: 1 })
}
mockDb.prepare.mockReturnValue(stmt)
db.updateRepoStatus(mockDb, 1, 'ready')
expect(mockDb.prepare).toHaveBeenCalledWith(
'UPDATE repos SET clone_status = ? WHERE id = ?'
)
expect(stmt.run).toHaveBeenCalledWith('ready', 1)
})
})
describe('updateLastPulled', () => {
it('should update repo last pulled timestamp', () => {
const stmt = {
run: vi.fn().mockReturnValue({ changes: 1 })
}
mockDb.prepare.mockReturnValue(stmt)
db.updateLastPulled(mockDb, 1)
expect(mockDb.prepare).toHaveBeenCalledWith(
'UPDATE repos SET last_pulled = ? WHERE id = ?'
)
expect(stmt.run).toHaveBeenCalledWith(expect.any(Number), 1)
})
})
describe('updateLastAccessed', () => {
it('should update repo last accessed timestamp', () => {
const stmt = {
run: vi.fn().mockReturnValue({ changes: 1 })
}
mockDb.prepare.mockReturnValue(stmt)
db.updateLastAccessed(mockDb, 1)
expect(mockDb.prepare).toHaveBeenCalledWith(
'UPDATE repos SET last_accessed_at = ? WHERE id = ?'
)
expect(stmt.run).toHaveBeenCalledWith(expect.any(Number), 1)
})
it('should throw error when repo not found', () => {
const stmt = {
run: vi.fn().mockReturnValue({ changes: 0 })
}
mockDb.prepare.mockReturnValue(stmt)
expect(() => db.updateLastAccessed(mockDb, 999)).toThrow('Repository with id 999 not found')
})
})
describe('deleteRepo', () => {
it('should delete repo-scoped rows, including multi-runs, in one transaction', () => {
mockDb.transaction.mockImplementation((fn: () => void) => fn)
const deleteEntriesStmt = { run: vi.fn().mockReturnValue({ changes: 2 }) }
const deleteMultiRunsStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) }
const deleteRunsStmt = { run: vi.fn().mockReturnValue({ changes: 2 }) }
const deleteJobsStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) }
const deleteSettingsStmt = { run: vi.fn().mockReturnValue({ changes: 0 }) }
const deleteRepoStmt = { run: vi.fn().mockReturnValue({ changes: 1 }) }
mockDb.prepare
.mockReturnValueOnce(deleteEntriesStmt)
.mockReturnValueOnce(deleteMultiRunsStmt)
.mockReturnValueOnce(deleteJobsStmt)
.mockReturnValueOnce(deleteRunsStmt)
.mockReturnValueOnce(deleteSettingsStmt)
.mockReturnValueOnce(deleteRepoStmt)
db.deleteRepo(mockDb, 1)
expect(mockDb.transaction).toHaveBeenCalledTimes(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(1,
'DELETE FROM multi_run_entries WHERE multi_run_id IN (SELECT id FROM multi_runs WHERE repo_id = ?)'
)
expect(deleteEntriesStmt.run).toHaveBeenCalledWith(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(2, 'DELETE FROM multi_runs WHERE repo_id = ?')
expect(deleteMultiRunsStmt.run).toHaveBeenCalledWith(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(3, 'DELETE FROM schedule_jobs WHERE repo_id = ?')
expect(deleteJobsStmt.run).toHaveBeenCalledWith(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(4, 'DELETE FROM schedule_runs WHERE repo_id = ?')
expect(deleteRunsStmt.run).toHaveBeenCalledWith(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(5, 'DELETE FROM repo_settings WHERE repo_id = ?')
expect(deleteSettingsStmt.run).toHaveBeenCalledWith(1)
expect(mockDb.prepare).toHaveBeenNthCalledWith(6,
'DELETE FROM repos WHERE id = ?'
)
expect(deleteRepoStmt.run).toHaveBeenCalledWith(1)
})
it('should not delete the assistant repo', () => {
db.deleteRepo(mockDb, 0)
expect(mockDb.prepare).not.toHaveBeenCalled()
})
})
})