Files
opencode-manager/backend/test/services/sandbox/resources.test.ts
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

65 lines
2.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import os from 'os'
import { ENV } from '@opencode-manager/shared/config/env'
import { parseMemoryMib } from '../../../src/services/sandbox/command'
import {
envSandboxResources,
resolveSandboxResources,
sandboxResourceLimitError,
} from '../../../src/services/sandbox/resources'
describe('sandbox resources', () => {
const hostCpus = os.availableParallelism()
const hostMemoryMib = Math.floor(os.totalmem() / 1048576)
it('reads the environment sandbox resources', () => {
expect(envSandboxResources()).toEqual({
cpus: ENV.SANDBOX.CPUS,
memoryMib: parseMemoryMib(ENV.SANDBOX.MEMORY),
})
})
it('falls back to the environment when no preferences or override are set', () => {
expect(resolveSandboxResources(undefined, null)).toEqual(envSandboxResources())
})
it('prefers preference values over the environment', () => {
expect(resolveSandboxResources({ enabled: false, cpus: 6, memoryMib: 2048 }, null)).toEqual({
cpus: 6,
memoryMib: 2048,
})
})
it('prefers the per-session override over preferences and the environment', () => {
expect(
resolveSandboxResources({ enabled: false, cpus: 6, memoryMib: 2048 }, { cpus: 2, memoryMib: 1024 }),
).toEqual({ cpus: 2, memoryMib: 1024 })
})
it('fills each field independently from preferences and the environment', () => {
const env = envSandboxResources()
expect(resolveSandboxResources({ enabled: false, cpus: 6 }, null)).toEqual({ cpus: 6, memoryMib: env.memoryMib })
expect(resolveSandboxResources({ enabled: false, memoryMib: 2048 }, null)).toEqual({ cpus: env.cpus, memoryMib: 2048 })
})
it('accepts requests within the host limits', () => {
expect(sandboxResourceLimitError({})).toBeNull()
expect(sandboxResourceLimitError({ cpus: hostCpus })).toBeNull()
expect(sandboxResourceLimitError({ memoryMib: hostMemoryMib })).toBeNull()
})
it('rejects a cpu request above the host limit', () => {
const error = sandboxResourceLimitError({ cpus: hostCpus + 1 })
expect(error).toContain(String(hostCpus + 1))
expect(error).toContain(String(hostCpus))
})
it('rejects a memory request above the host limit', () => {
const error = sandboxResourceLimitError({ memoryMib: hostMemoryMib + 1 })
expect(error).toContain(String(hostMemoryMib + 1))
expect(error).toContain(String(hostMemoryMib))
})
})