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

@@ -104,11 +104,17 @@ const sandboxRuntimeServiceMock = vi.hoisted(() => ({
SandboxRuntimeService: vi.fn<() => {
isEnabled: () => boolean
stopWorkspaceSandboxForToggle?: () => Promise<void>
prepareWorkspaceSandboxOnBoot?: () => Promise<void>
}>(() => ({ isEnabled: () => false })),
}))
vi.mock('../../src/services/sandbox/runtime', () => ({
SandboxRuntimeService: sandboxRuntimeServiceMock.SandboxRuntimeService,
SandboxRuntimeService: function SandboxRuntimeServiceStub() {
return {
prepareWorkspaceSandboxOnBoot: async () => undefined,
...sandboxRuntimeServiceMock.SandboxRuntimeService(),
}
},
}))
import { promises as fs, accessSync, readdirSync } from 'fs'
@@ -13,6 +13,7 @@ import {
buildSandboxInspectArgs,
buildSandboxListArgs,
buildSandboxProvisionArgs,
buildSandboxPullArgs,
buildSandboxRemoveArgs,
buildSandboxStartArgs,
buildSandboxStopManagedArgs,
@@ -36,6 +37,10 @@ describe('sandbox command builders', () => {
expect(buildSandboxVersionArgs()).toEqual(['--version'])
})
it('builds pull args for the configured guest image', () => {
expect(buildSandboxPullArgs()).toEqual(['pull', ENV.SANDBOX.IMAGE])
})
it('builds inspect args targeting the shared workspace sandbox with JSON output', () => {
expect(buildSandboxInspectArgs()).toEqual(['inspect', WORKSPACE_SANDBOX_NAME, '--format', 'json'])
})
+45 -3
View File
@@ -351,7 +351,7 @@ describe('SandboxRuntimeService', () => {
return { exitCode: 0, stdout: '', stderr: '' }
})
await service.provisionWorkspaceSandboxOnBoot()
await service.prepareWorkspaceSandboxOnBoot()
const provisionCall = mockExecuteCommand.mock.calls.find((call) => call[0].includes('exec'))
expect(provisionCall).toBeDefined()
@@ -364,7 +364,7 @@ describe('SandboxRuntimeService', () => {
throw new Error('msb must not be invoked when sandboxing is disabled')
})
await service.provisionWorkspaceSandboxOnBoot()
await service.prepareWorkspaceSandboxOnBoot()
expect(mockExecuteCommand).not.toHaveBeenCalled()
})
@@ -378,11 +378,53 @@ describe('SandboxRuntimeService', () => {
return { exitCode: 0, stdout: '', stderr: '' }
})
await service.provisionWorkspaceSandboxOnBoot()
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('ls')) {
return { exitCode: 0, stdout: stoppedListingOutput(), stderr: '' }
}
return { exitCode: 0, stdout: '', stderr: '' }
})
await service.prepareWorkspaceSandboxOnBoot()
expect(mockExecuteCommand).toHaveBeenCalledWith(
[sandboxExecutablePath(), 'pull', ENV.SANDBOX.IMAGE],
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)
})
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 () => {
enableEnforcement()
mockExecuteCommand.mockImplementation(async (args: string[]) => {
if (args.includes('pull')) {
throw new Error('msb pull failed: registry unreachable')
}
return { exitCode: 0, stdout: '', stderr: '' }
})
await expect(service.prepareWorkspaceSandboxOnBoot()).rejects.toThrow('registry unreachable')
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('ls'))).toHaveLength(0)
})
it('returns blocked with the start stderr when a stopped sandbox fails to start', async () => {
enableEnforcement()
mockExecuteCommand.mockImplementation(async (args: string[]) => {