Files
opencode-manager/backend/test/db/session-sandbox-modes.test.ts
T
Chris Scott b21fdf5b94 feat(sandbox): per-session sandbox VMs with resource settings and provider arbitration
- Sandbox new root sessions by default with a per-session composer toggle;
  child sessions inherit their root session's mode (OM-82)
- Run each sandboxed root session in its own microVM that mounts only the
  session's repository, with per-session CPU/memory overrides, Settings
  defaults, idle stop, and a cap on running VMs (OM-89)
- Arbitrate shell sandboxing with other plugins (opencode-forge) through the
  shared opencode.sandbox-arbiter.v1 registry so exactly one provider
  sandboxes each call regardless of load order; fail closed on claim errors
- Throw when another plugin rewrites a pinned sandbox shell setting
- Tag sandboxed tool results with the owning provider and show it in the badge
- Hint the agent to start Docker inside the VM only when OCM owns the session
2026-10-15 16:53:49 -04:00

125 lines
3.9 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { Database } from 'bun:sqlite'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
import {
deleteSessionSandboxMode,
getSessionSandboxMode,
insertSessionSandboxModeIfAbsent,
setSessionSandboxResources,
setSessionSandboxed,
} from '../../src/db/session-sandbox-modes'
function createTestDb(): Database {
const db = new Database(':memory:')
migrate(db, allMigrations)
return db
}
describe('session sandbox modes db', () => {
let db: Database
beforeEach(() => {
db = createTestDb()
})
afterEach(() => {
db.close()
})
it('returns null for a session with no stored row', () => {
expect(getSessionSandboxMode(db, 'ses_root')).toBeNull()
})
it('stores and reads back the sandbox flag with a default source', () => {
insertSessionSandboxModeIfAbsent(db, 'ses_root', { sandboxed: true, source: 'default', resources: null })
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({
sandboxed: true,
source: 'default',
resources: null,
})
})
it('records the user source when the sandbox flag is set explicitly', () => {
setSessionSandboxed(db, 'ses_root', false)
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({
sandboxed: false,
source: 'user',
resources: null,
})
})
it('inserts only when no row exists', () => {
insertSessionSandboxModeIfAbsent(db, 'ses_root', { sandboxed: true, source: 'default', resources: null })
insertSessionSandboxModeIfAbsent(db, 'ses_root', { sandboxed: false, source: 'user', resources: null })
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({ sandboxed: true, source: 'default' })
})
it('round-trips resources and keeps toggled_at stable across resource updates', () => {
setSessionSandboxed(db, 'ses_root', true)
const toggledAt = getSessionSandboxMode(db, 'ses_root')?.toggledAt
setSessionSandboxResources(db, 'ses_root', { cpus: 4, memoryMib: 8192 }, true)
const row = getSessionSandboxMode(db, 'ses_root')
expect(row).toMatchObject({
sandboxed: true,
source: 'user',
resources: { cpus: 4, memoryMib: 8192 },
})
expect(row?.toggledAt).toBe(toggledAt)
setSessionSandboxResources(db, 'ses_root', { cpus: 8, memoryMib: 16384 }, true)
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({
resources: { cpus: 8, memoryMib: 16384 },
toggledAt,
})
})
it('inserts a default-sourced row when resources are set before the flag', () => {
setSessionSandboxResources(db, 'ses_root', { cpus: 4, memoryMib: 8192 }, true)
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({
sandboxed: true,
source: 'default',
resources: { cpus: 4, memoryMib: 8192 },
})
})
it('clears resources without changing the sandbox flag or source', () => {
setSessionSandboxed(db, 'ses_root', false)
setSessionSandboxResources(db, 'ses_root', { cpus: 4, memoryMib: 8192 }, false)
const toggledAt = getSessionSandboxMode(db, 'ses_root')?.toggledAt
setSessionSandboxResources(db, 'ses_root', null, false)
expect(getSessionSandboxMode(db, 'ses_root')).toMatchObject({
sandboxed: false,
source: 'user',
resources: null,
toggledAt,
})
})
it('updates toggled_at when the sandbox flag changes', () => {
setSessionSandboxed(db, 'ses_root', true)
const first = getSessionSandboxMode(db, 'ses_root')?.toggledAt ?? 0
setSessionSandboxed(db, 'ses_root', false)
const second = getSessionSandboxMode(db, 'ses_root')?.toggledAt ?? 0
expect(second).toBeGreaterThanOrEqual(first)
})
it('deletes a stored row', () => {
setSessionSandboxed(db, 'ses_root', true)
deleteSessionSandboxMode(db, 'ses_root')
expect(getSessionSandboxMode(db, 'ses_root')).toBeNull()
})
})