feat(sandbox): cache the guest image at startup instead of on first command

Pull the configured guest image in the background when the Manager boots and
when sandbox enforcement is switched on, so a sandboxed command no longer
pays the multi-gigabyte download. msb pull is a no-op once cached, startup is
never blocked by it, and a command racing the pull joins the same in-flight
boot.
This commit is contained in:
Chris Scott committed 2026-08-28 13:05:56 -04:00
1 parent 5beb3432d4
commit 2eba81d524
8 files changed
+82 -12

No files matched your search

@@ -414,6 +414,11 @@ class OpenCodeServerManager {
this.failNonRecoverable(`Failed to stop the workspace sandbox while disabling enforcement: ${error instanceof Error ? error.message : String(error)}`)
}
}
if (sandboxEnforced && !this.sandboxEnforced) {
void new SandboxRuntimeService(this.db).prepareWorkspaceSandboxOnBoot().catch((error) => {
logger.warn('Sandbox enforcement enabled but preparing the guest image failed:', error)
})
}
logger.info(`OpenCode sandbox enforcement: ${sandboxEnforced ? 'enabled' : 'disabled'}`)
}
+4
View File
@@ -120,6 +120,10 @@ export function buildSandboxVersionArgs(): string[] {
return ['--version']
}
export function buildSandboxPullArgs(): string[] {
return ['pull', ENV.SANDBOX.IMAGE]
}
export function sandboxMountRoots(): string[] {
return [getReposPath(), getScheduleWorktreesPath()]
}
+11 -1
View File
@@ -16,6 +16,7 @@ import {
buildSandboxInspectArgs,
buildSandboxListArgs,
buildSandboxProvisionArgs,
buildSandboxPullArgs,
buildSandboxRemoveArgs,
buildSandboxStartArgs,
buildSandboxStopManagedArgs,
@@ -542,6 +543,14 @@ async function attestWorkspaceSandbox(running: boolean): Promise<SandboxAttestat
return await attestWorkspaceSandboxConfig(outcome.record.config)
}
async function cacheSandboxImage(): Promise<void> {
logger.info(`Caching the sandbox guest image ${ENV.SANDBOX.IMAGE}`)
await executeCommand([sandboxExecutablePath(), ...buildSandboxPullArgs()], {
timeout: ENV.SANDBOX.START_TIMEOUT_MS,
})
logger.info(`Sandbox guest image ${ENV.SANDBOX.IMAGE} is cached`)
}
async function provisionSandboxExecUserPasswd(): Promise<void> {
const provisionArgs = buildSandboxProvisionArgs()
if (provisionArgs.length === 0) {
@@ -649,11 +658,12 @@ async function startWorkspaceSandbox(): Promise<void> {
export class SandboxRuntimeService {
constructor(private readonly db: Database) {}
async provisionWorkspaceSandboxOnBoot(): Promise<void> {
async prepareWorkspaceSandboxOnBoot(): Promise<void> {
if (!this.isSandboxEnabled()) return
const capability = detectSandboxCapability()
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