(
taskId: SandboxTaskId,
input: TInput,
options: RunSandboxTaskOptions = {}
)
| 44 | * so callers can propagate the error verbatim to UI. |
| 45 | */ |
| 46 | export async function runSandboxTask<TInput extends SandboxTaskInput>( |
| 47 | taskId: SandboxTaskId, |
| 48 | input: TInput, |
| 49 | options: RunSandboxTaskOptions = {} |
| 50 | ): Promise<Buffer> { |
| 51 | const task = getSandboxTask(taskId) |
| 52 | const requestId = generateShortId(12) |
| 53 | |
| 54 | const brokerContext: SandboxBrokerContext = { |
| 55 | workspaceId: input.workspaceId, |
| 56 | requestId, |
| 57 | } |
| 58 | const brokers: Record<string, IsolatedVMBrokerHandler> = {} |
| 59 | for (const broker of task.brokers) { |
| 60 | brokers[broker.name] = (args) => broker.handle(brokerContext, args) |
| 61 | } |
| 62 | |
| 63 | const request: IsolatedVMExecutionRequest = { |
| 64 | code: input.code, |
| 65 | params: {}, |
| 66 | envVars: {}, |
| 67 | contextVariables: {}, |
| 68 | timeoutMs: task.timeoutMs, |
| 69 | requestId, |
| 70 | ownerKey: options.ownerKey, |
| 71 | ownerWeight: 1, |
| 72 | task: { |
| 73 | id: task.id, |
| 74 | bundles: [...task.bundles], |
| 75 | bootstrap: task.bootstrap, |
| 76 | brokers: task.brokers.map((b) => b.name), |
| 77 | finalize: task.finalize, |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | const start = Date.now() |
| 82 | const result = await executeInIsolatedVM(request, { brokers, signal: options.signal }) |
| 83 | const elapsedMs = Date.now() - start |
| 84 | |
| 85 | // Phase timings come from the worker (see executeTask). `queue` is the |
| 86 | // gap between client call and worker-side start — useful for diagnosing |
| 87 | // pool saturation vs. isolate-internal slowness. |
| 88 | const queueMs = result.timings ? Math.max(0, elapsedMs - result.timings.total) : undefined |
| 89 | |
| 90 | if (result.error) { |
| 91 | const isSystemError = result.error.isSystemError === true |
| 92 | const logFn = isSystemError ? logger.error.bind(logger) : logger.warn.bind(logger) |
| 93 | logFn('Sandbox task failed', { |
| 94 | taskId, |
| 95 | requestId, |
| 96 | workspaceId: input.workspaceId, |
| 97 | elapsedMs, |
| 98 | queueMs, |
| 99 | timings: result.timings, |
| 100 | error: result.error.message, |
| 101 | errorName: result.error.name, |
| 102 | isSystemError, |
| 103 | }) |
no test coverage detected