mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(sandbox): boot the shared microVM at startup when sandboxing is enabled
Startup and the enforcement toggle now run the same ensureWorkspaceSandbox path a command triggers, so the image pull, microVM boot, attestation and exec-user provisioning are done before the first command instead of on it. One microVM serves every repo and schedule worktree.
This commit is contained in:
1 parent
2eba81d524
commit
297f6a5237
3 files changed
+63
-35
No files matched your search
@@ -664,11 +664,9 @@ export class SandboxRuntimeService {
|
||||
if (!capability.available) return
|
||||
if (getProcessIdentityAttestationError() !== null) return
|
||||
await cacheSandboxImage()
|
||||
if (buildSandboxProvisionArgs().length === 0) return
|
||||
const entry = findWorkspaceSandboxEntry(await listSandboxes())
|
||||
if (entry === null || !entry.running) return
|
||||
await ensureWorkspaceSandbox()
|
||||
await provisionSandboxExecUserPasswd()
|
||||
logger.info('Provisioned the workspace sandbox exec user passwd entry')
|
||||
logger.info('Workspace sandbox is running and its exec user is provisioned')
|
||||
}
|
||||
|
||||
isEnabled(): boolean {
|
||||
|
||||
@@ -338,9 +338,10 @@ describe('SandboxRuntimeService', () => {
|
||||
expect(provisionArgs.join(' ')).toContain('getent passwd')
|
||||
})
|
||||
|
||||
it('provisions a running workspace sandbox once at boot when enforcement is on', async () => {
|
||||
it('leaves an already running workspace sandbox in place at boot and provisions its exec user', async () => {
|
||||
enableEnforcement()
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('inspect')) return inspectedRunningSandbox()
|
||||
if (args.includes('ls')) {
|
||||
return {
|
||||
exitCode: 0,
|
||||
@@ -353,13 +354,62 @@ describe('SandboxRuntimeService', () => {
|
||||
|
||||
await service.prepareWorkspaceSandboxOnBoot()
|
||||
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('run'))).toHaveLength(0)
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('start'))).toHaveLength(0)
|
||||
const provisionCall = mockExecuteCommand.mock.calls.find((call) => call[0].includes('exec'))
|
||||
expect(provisionCall).toBeDefined()
|
||||
const provisionArgs = provisionCall![0]
|
||||
expect(provisionArgs[provisionArgs.indexOf('-u') + 1]).toBe('0:0')
|
||||
})
|
||||
|
||||
it('skips boot provisioning when the sandbox preference is off', async () => {
|
||||
it('creates the shared microVM at boot when none exists yet', async () => {
|
||||
enableEnforcement()
|
||||
let inspectCalls = 0
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('inspect')) {
|
||||
inspectCalls += 1
|
||||
if (inspectCalls === 1) {
|
||||
return { exitCode: 1, stdout: '', stderr: 'no such sandbox' }
|
||||
}
|
||||
return inspectedRunningSandbox()
|
||||
}
|
||||
if (args.includes('ls')) {
|
||||
return { exitCode: 0, stdout: '[]', stderr: '' }
|
||||
}
|
||||
return { exitCode: 0, stdout: '', stderr: '' }
|
||||
})
|
||||
|
||||
await service.prepareWorkspaceSandboxOnBoot()
|
||||
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('run'))).toHaveLength(1)
|
||||
expect(mockExecuteCommand.mock.calls.find((call) => call[0].includes('exec'))).toBeDefined()
|
||||
})
|
||||
|
||||
it('starts a stopped microVM at boot instead of waiting for the first command', async () => {
|
||||
enableEnforcement()
|
||||
let inspectCalls = 0
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('ls')) {
|
||||
return { exitCode: 0, stdout: stoppedListingOutput(), stderr: '' }
|
||||
}
|
||||
if (args.includes('inspect')) {
|
||||
inspectCalls += 1
|
||||
if (inspectCalls === 1) {
|
||||
return { exitCode: 0, stdout: trustedInspectOutput(), stderr: '' }
|
||||
}
|
||||
return inspectedRunningSandbox()
|
||||
}
|
||||
return { exitCode: 0, stdout: '', stderr: '' }
|
||||
})
|
||||
|
||||
await service.prepareWorkspaceSandboxOnBoot()
|
||||
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('start'))).toHaveLength(1)
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('run'))).toHaveLength(0)
|
||||
expect(mockExecuteCommand.mock.calls.find((call) => call[0].includes('exec'))).toBeDefined()
|
||||
})
|
||||
|
||||
it('never touches msb at boot when the sandbox preference is off', async () => {
|
||||
mockExecuteCommand.mockImplementation(async () => {
|
||||
throw new Error('msb must not be invoked when sandboxing is disabled')
|
||||
})
|
||||
@@ -369,23 +419,10 @@ describe('SandboxRuntimeService', () => {
|
||||
expect(mockExecuteCommand).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips boot provisioning when the workspace sandbox is not running', async () => {
|
||||
enableEnforcement()
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('ls')) {
|
||||
return { exitCode: 0, stdout: stoppedListingOutput(), stderr: '' }
|
||||
}
|
||||
return { exitCode: 0, stdout: '', stderr: '' }
|
||||
})
|
||||
|
||||
await service.prepareWorkspaceSandboxOnBoot()
|
||||
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('exec'))).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('caches the guest image at boot before touching the microVM', async () => {
|
||||
enableEnforcement()
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('inspect')) return inspectedRunningSandbox()
|
||||
if (args.includes('ls')) {
|
||||
return { exitCode: 0, stdout: stoppedListingOutput(), stderr: '' }
|
||||
}
|
||||
@@ -399,20 +436,12 @@ describe('SandboxRuntimeService', () => {
|
||||
expect.objectContaining({ timeout: ENV.SANDBOX.START_TIMEOUT_MS }),
|
||||
)
|
||||
const pullIndex = mockExecuteCommand.mock.calls.findIndex((call) => call[0].includes('pull'))
|
||||
const listIndex = mockExecuteCommand.mock.calls.findIndex((call) => call[0].includes('ls'))
|
||||
expect(pullIndex).toBeGreaterThanOrEqual(0)
|
||||
expect(pullIndex).toBeLessThan(listIndex)
|
||||
const inspectIndex = mockExecuteCommand.mock.calls.findIndex((call) => call[0].includes('inspect'))
|
||||
expect(pullIndex).toBe(0)
|
||||
expect(pullIndex).toBeLessThan(inspectIndex)
|
||||
})
|
||||
|
||||
it('never pulls the guest image at boot when the sandbox preference is off', async () => {
|
||||
mockExecuteCommand.mockImplementation(async () => ({ exitCode: 0, stdout: '', stderr: '' }))
|
||||
|
||||
await service.prepareWorkspaceSandboxOnBoot()
|
||||
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('pull'))).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('surfaces a failed boot image pull to the caller', async () => {
|
||||
it('surfaces a failed boot image pull to the caller and never boots the microVM', async () => {
|
||||
enableEnforcement()
|
||||
mockExecuteCommand.mockImplementation(async (args: string[]) => {
|
||||
if (args.includes('pull')) {
|
||||
@@ -422,7 +451,8 @@ describe('SandboxRuntimeService', () => {
|
||||
})
|
||||
|
||||
await expect(service.prepareWorkspaceSandboxOnBoot()).rejects.toThrow('registry unreachable')
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('ls'))).toHaveLength(0)
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('inspect'))).toHaveLength(0)
|
||||
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('run'))).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('returns blocked with the start stderr when a stopped sandbox fails to start', async () => {
|
||||
|
||||
@@ -191,7 +191,7 @@ The guest runs every command as a numeric host uid that has no `/etc/passwd` ent
|
||||
|
||||
Chromium launches headless as the non-root exec user without extra flags. If your host kernel restricts user namespaces so Chromium's own sandbox fails, pass `--no-sandbox` — the microVM is already the isolation boundary.
|
||||
|
||||
The image is over 3 GB against a 1.6 GB `node:24` baseline, almost entirely Chromium and its dependencies. The Manager caches it ahead of first use: at startup, and whenever sandbox enforcement is switched on, it runs `msb pull` in the background (bounded by `SANDBOX_START_TIMEOUT_MS`; raise it on slow links). The pull is a no-op once the image is cached, and `docker-compose.sandbox.yml` persists the microsandbox store in the `microsandbox-data` volume so the download survives container replacement. Because it runs in the background, server startup is never blocked by it, and a sandboxed command issued while the pull is still running joins the same in-flight boot instead of starting a second download.
|
||||
The image is over 3 GB against a 1.6 GB `node:24` baseline, almost entirely Chromium and its dependencies. When sandboxing is enabled, the Manager gets everything ready before the first command instead of on it: at startup, and whenever enforcement is switched on, it pulls the image and then boots the shared microVM in the background (`msb pull`, then the same create/start/attest/provision path a command would trigger, bounded by `SANDBOX_START_TIMEOUT_MS`; raise it on slow links). One microVM serves every repo and schedule worktree, so a single warm-up covers the whole workspace. The pull is a no-op once cached, `docker-compose.sandbox.yml` persists the microsandbox store in the `microsandbox-data` volume so the download survives container replacement, and the shutdown handler stops the microVM again. Because the warm-up runs in the background, server startup never waits for it, and a command issued while it is still running joins the same in-flight boot rather than starting a second one.
|
||||
|
||||
### Using your own image
|
||||
|
||||
@@ -210,7 +210,7 @@ Override the Playwright version at build time with `--build-arg PLAYWRIGHT_VERSI
|
||||
|
||||
## Caveats
|
||||
|
||||
- The first command pays microVM boot latency, bounded by `SANDBOX_START_TIMEOUT_MS`. The guest image itself is pulled in the background at startup, so it only pays a download too if it runs before that pull finishes.
|
||||
- With sandboxing enabled the image pull and microVM boot happen at Manager startup, so commands normally pay neither. A command that runs before the warm-up finishes waits on that same boot, bounded by `SANDBOX_START_TIMEOUT_MS`.
|
||||
- `SANDBOX_IMAGE` must contain every tool the agent expects to run.
|
||||
- `SANDBOX_EXEC_USER` must match the workspace owner so commands can write mounted files.
|
||||
- A `shell` the user configured does not apply to slash-command shell templates while enforcement is on, and is bypassed for `!command` shell mode because that surface is routed into the microVM.
|
||||
|
||||
Reference in new issue
Block a user