MCPcopy Index your code
hub / github.com/simstudioai/sim / runSandboxTask

Function runSandboxTask

apps/sim/lib/execution/sandbox/run-task.ts:46–138  ·  view source on GitHub ↗
(
  taskId: SandboxTaskId,
  input: TInput,
  options: RunSandboxTaskOptions = {}
)

Source from the content-addressed store, hash-verified

44 * so callers can propagate the error verbatim to UI.
45 */
46export 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 })

Callers 5

compileDocForWriteFunction · 0.90
resolveServableDocBytesFunction · 0.90
renderDocRecordResultMethod · 0.90
readFileContentMethod · 0.90
route.tsFile · 0.90

Calls 8

getSandboxTaskFunction · 0.90
generateShortIdFunction · 0.90
executeInIsolatedVMFunction · 0.90
handleMethod · 0.80
bindMethod · 0.80
errorMethod · 0.80
infoMethod · 0.80
toResultMethod · 0.80

Tested by

no test coverage detected