mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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:
1 parent
5beb3432d4
commit
2eba81d524
8 files changed
+82
-12
No files matched your search
@@ -296,11 +296,9 @@ try {
|
||||
|
||||
opencodeServerManager.setDatabase(db)
|
||||
detectSandboxCapability()
|
||||
try {
|
||||
await new SandboxRuntimeService(db).provisionWorkspaceSandboxOnBoot()
|
||||
} catch (error) {
|
||||
logger.warn('Workspace sandbox provisioning skipped:', error)
|
||||
}
|
||||
void new SandboxRuntimeService(db).prepareWorkspaceSandboxOnBoot().catch((error) => {
|
||||
logger.warn('Workspace sandbox preparation failed:', error)
|
||||
})
|
||||
const openCodeStatus = await openCodeSupervisor.start()
|
||||
if (openCodeStatus.healthy) {
|
||||
logger.info(`OpenCode server running on port ${openCodeStatus.port}`)
|
||||
|
||||
@@ -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'}`)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()]
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user