|
| 1 | +/** |
| 2 | + * The persistent workbench's input history is what lets a scratch file reach the model. These |
| 3 | + * run the real code boundary against the real history recorder, so a mount the caller could not |
| 4 | + * classify has to leave the machine uncertified. |
| 5 | + */ |
| 6 | +import { redisConfigMockFns } from '@sim/testing/mocks/redis-config.mock' |
| 7 | +import { |
| 8 | + remoteSandboxProviderMock, |
| 9 | + remoteSandboxProviderMockFns, |
| 10 | +} from '@sim/testing/mocks/remote-sandbox-provider.mock' |
| 11 | +import { generateShortId } from '@sim/utils/id' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | +import { CodeLanguage } from '@/lib/execution/languages' |
| 14 | +import type { SandboxHandle, SandboxProvider } from '@/lib/execution/remote-sandbox/types' |
| 15 | + |
| 16 | +const { mockFindSessionSandbox } = vi.hoisted(() => ({ mockFindSessionSandbox: vi.fn() })) |
| 17 | + |
| 18 | +vi.mock('@/lib/execution/remote-sandbox/provider', () => remoteSandboxProviderMock) |
| 19 | +vi.mock('@/lib/execution/remote-sandbox/resolve', () => ({ |
| 20 | + resolveWorkspaceSandbox: vi.fn().mockResolvedValue(null), |
| 21 | + provisionRuntimeDependencies: vi.fn(), |
| 22 | + repairMissingSandboxImage: vi.fn().mockResolvedValue(null), |
| 23 | + RUNTIME_INSTALL_TIMEOUT_MS: 60_000, |
| 24 | +})) |
| 25 | +vi.mock('@/lib/core/execution-limits/metrics', () => ({ |
| 26 | + recordSandboxTeardownFailure: vi.fn(), |
| 27 | + recordSandboxProviderLimit: vi.fn(), |
| 28 | +})) |
| 29 | + |
| 30 | +import { |
| 31 | + executeInSandbox, |
| 32 | + executeShellInSandbox, |
| 33 | + SIM_RESULT_PREFIX, |
| 34 | +} from '@/lib/execution/remote-sandbox' |
| 35 | +import { observeSandboxSessionInputs } from '@/lib/execution/remote-sandbox/execution-observer' |
| 36 | +import { |
| 37 | + initializeSessionFileProvenance, |
| 38 | + isSessionFileProvenanceClean, |
| 39 | +} from '@/lib/execution/remote-sandbox/session-file-provenance' |
| 40 | + |
| 41 | +/** Same one-way semantics as the Lua history script. */ |
| 42 | +const records = new Map<string, string>() |
| 43 | +redisConfigMockFns.mockGetRedisClient.mockImplementation(() => ({ |
| 44 | + set: async (key: string, value: string) => { |
| 45 | + if (!records.has(key)) records.set(key, value) |
| 46 | + return 'OK' |
| 47 | + }, |
| 48 | + get: async (key: string) => records.get(key) ?? null, |
| 49 | + eval: async (_script: string, _count: number, key: string, input: string) => { |
| 50 | + records.set(key, records.get(key) === 'clean' && input === 'clean' ? 'clean' : 'unknown') |
| 51 | + return records.get(key) |
| 52 | + }, |
| 53 | +})) |
| 54 | +remoteSandboxProviderMockFns.mockResolveProvider.mockImplementation( |
| 55 | + (): SandboxProvider => ({ |
| 56 | + id: 'e2b', |
| 57 | + dependencyStrategy: 'prebuilt', |
| 58 | + resolveLifetimeMs: (ms: number) => ms, |
| 59 | + create: vi.fn(), |
| 60 | + findSessionSandbox: mockFindSessionSandbox, |
| 61 | + }) |
| 62 | +) |
| 63 | + |
| 64 | +function machine(sandboxId: string): SandboxHandle { |
| 65 | + return { |
| 66 | + sandboxId, |
| 67 | + runCode: async () => ({ text: `${SIM_RESULT_PREFIX}{"ok":true}`, stdout: '', stderr: '' }), |
| 68 | + runCommand: async () => ({ stdout: '', stderr: '', exitCode: 0 }), |
| 69 | + extendLifetime: async () => {}, |
| 70 | + getFileSize: async () => 0, |
| 71 | + readFile: async () => '', |
| 72 | + readFileWithLimit: async () => ({ content: '', byteLength: 0 }), |
| 73 | + writeFile: async () => {}, |
| 74 | + removeFile: async () => {}, |
| 75 | + listFiles: async () => [], |
| 76 | + kill: async () => {}, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +beforeEach(() => { |
| 81 | + records.clear() |
| 82 | +}) |
| 83 | + |
| 84 | +describe('workbench certification at the code boundary', () => { |
| 85 | + it.each([ |
| 86 | + ['code', false], |
| 87 | + ['code', true], |
| 88 | + ['shell', false], |
| 89 | + ['shell', true], |
| 90 | + ] as const)('%s with unprovenanced mounts %s', async (kind, unprovenanced) => { |
| 91 | + const sandboxId = `machine-${generateShortId(8)}` |
| 92 | + const key = `chat-${generateShortId(8)}` |
| 93 | + const identity = { providerId: 'e2b', sandboxId } as const |
| 94 | + mockFindSessionSandbox.mockResolvedValue(machine(sandboxId)) |
| 95 | + await initializeSessionFileProvenance(key, identity) |
| 96 | + const session = { key, ...(unprovenanced ? { unprovenancedInputs: true } : {}) } |
| 97 | + const request = { |
| 98 | + code: 'print(1)', |
| 99 | + language: CodeLanguage.Python, |
| 100 | + timeoutMs: 30_000, |
| 101 | + session, |
| 102 | + } |
| 103 | + await observeSandboxSessionInputs( |
| 104 | + () => true, |
| 105 | + () => |
| 106 | + kind === 'code' |
| 107 | + ? executeInSandbox(request) |
| 108 | + : executeShellInSandbox({ ...request, envs: {} }) |
| 109 | + ) |
| 110 | + expect(await isSessionFileProvenanceClean(key, identity)).toBe(!unprovenanced) |
| 111 | + }) |
| 112 | +}) |
0 commit comments