Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(flows): a code step's error reads the same on every instance (#15607)

Bartosz Majewskiab8cd5c9069e
diff --git a/brain/knowledge/execution-runtime/workers.md b/brain/knowledge/execution-runtime/workers.md
index ba2ededb071..d31d32b7247 100644
--- a/brain/knowledge/execution-runtime/workers.md
+++ b/brain/knowledge/execution-runtime/workers.md
@@ -39,6 +39,7 @@ The deep `Resolver`/`Runtime` concurrency and bundle-caching model lives on the
- **Clear an interval in `finally`, not at the end of the `try`.** `worker.ts`'s per-job `lockExtensionInterval` clears at the end of its `try` block. It is safe *only* because both awaits before it are wrapped in `tryCatch`, which catches everything and never rejects; put one unwrapped `await` between them and every job leaks a repeating 30s timer whose closure retains the `job` (token included) and keeps calling `extendLock` forever. `execute-agent-run.ts` is the pattern to copy — both its intervals clear in a `finally`.
- **Bumping `LATEST_CACHE_VERSION` only works because the runtime calls `deleteStaleCache` — keep that call wired, and keep it on a timer.** The bump changes which directory is used (`cache/v14`), it does not remove the old one; `deleteStaleCache` reclaims every non-current version, and until Aug 2026 it had **zero callers**, so a bump would have stranded the whole previous cache on disk indefinitely. The trap is that reclaiming it eagerly is *worse* than not reclaiming it: the cache volume is shared by every worker container on the host (~28 of them) and a rolling deploy runs both versions at once, so a fresh v14 worker deleting `v13` on startup rips the tree out from under still-running v13 workers mid-run. So the function refuses to delete anything until the **current** version directory's own mtime is older than `STALE_CACHE_GRACE_MS` (2h) — "this host has been on v14 long enough that the rollout is over" — and it runs from the periodic `sweepActionRunCache` timer in `worker.ts`, not from `prewarm`, because at boot the grace period has by definition not passed. Failing the guard only costs disk, so it is the safe direction; do not "fix" it by deleting on startup, and if you refactor the sweeper move the call rather than dropping it, since the failure is silent and only shows up as disks filling with `v12`/`v13` leftovers.
- **`@activepieces/shared` and `pieces-framework` can never be resident twice — that is enforced by the bundler, not by luck.** `bundle-piece-utils.ts` always inlines `@activepieces/*` (the esbuild plugin only ever externalizes third-party packages) and then *filters* `@activepieces/*` out of the declared `external` list before writing the manifest. Verified at runtime on staging Aug 2026 by `Runtime.evaluate` against the live `sandbox-<id>` engine and a heap snapshot: **0 `@activepieces/shared` modules and 0 `pieces-framework` modules** in `require.cache`, on both the worker process (671 modules / 5.22 MB) and the engine (1017 modules / 28.12 MB) — and each piece resolves to exactly one module, its `bundle.cjs`. Module keys carry the proof that the *bundled* copy is what loaded: they read `…/pieces/@activepieces/piece-slack-0.9.5/bundle.tgz/…`, reached via `package.json main → src/index.mjs → import './bundle.cjs'`. When auditing this, ignore string-match hits inside a probe's own source text — grepping heap strings for the package name finds your own instrumentation first.
+- **A code step's error text must be built from a structured payload, never from a stringified error — the three sandboxes disagree the moment anyone stringifies early.** `AP_EXECUTION_MODE` + the step's `useDeno` pick one of three runners, and each used to hand the host a *different* rendering of the same `throw`: the node child sent `util.inspect(e)`, the deno child sent `error.stack`, and isolated-vm marshals only `message`/`stack`. `formatPieceError`'s `stripStack` then removes `\n at …` lines but leaves the `Error: ` prefix and, for inspect, a mangled own-props tail — so `{{step['error'].message}}` read `"Error: Card declined"` on the default `UNSANDBOXED` mode and `"Card declined"` on `SANDBOX_CODE_ONLY`, and `errorName` was populated only on the isolate (GIT-1898, Pylon 6022). Cloud and self-hosted could differ on the same flow. Fixed at the serializing boundary, not in the parser: both child runners now emit `{ message, name, stack }` (`sandboxError.payloadSource`, injected into both generated programs so the two copies cannot drift) and the host rebuilds a real `Error` via `sandboxError.build`. Do **not** "fix" a future variant of this in `formatPieceError` — it also serves piece and HTTP errors, where a provider legitimately returns text starting `Error: `, and widening `stripStack` puts a lossy heuristic on every error source in the product. Two corollaries worth keeping: captured stdout/stderr goes on the `stack` (→ the UI's *Technical Details*), never on `message`, because which sandbox captures output is itself mode-specific; and a non-`Error` throw still diverges by design — isolated-vm refuses to marshal it at all. `code-sandbox-error-contract.test.ts` drives all three real sandboxes through `formatPieceError` and is what stops a fourth shape appearing.
- **A piece that trips the 5 MB size fallback ships as a "bundle" that still installs its full npm tree, and it dominates engine memory.** `bundlePiece` inlines everything by default, but if the result exceeds `FAIL_BYTES` (5 MB) it rebuilds with `inlineAll: false`, externalizing **all** third-party deps (`bundle-piece-utils.ts`) — deliberate, so every piece keeps building and the tarball stays small. The runtime bill lands elsewhere: `google-sheets` has done this since 0.8.4 (10 externals, growing to 17 by 0.16.1), so its bundle declares `[email protected]`, which bun installs into the shared workspace store and the engine loads — **23.52 MB of a 28.12 MB `require.cache`, 83%, for one piece**, while fully-inlined `[email protected]` contributes 1.58 MB despite being the larger piece. Two `googleapis-common` majors (7.0.1 and 7.2.0) sit resident together, which is exactly the duplication bundling was meant to end. Sampling 60 CDN bundles: 60/60 carry `bundle.cjs`, 55/60 declare **zero** dependencies, and the 5 that don't externalize only native/wasm-ish packages (`pino`, `jimp`, `pdf-lib`, `undici`, `bufferutil`, `tiktoken`). So "bundled" is not a uniform guarantee — check `package.json.dependencies` inside the tarball before assuming a piece is self-contained.
- **Self-contained piece bundles remove cross-piece dedup, so the per-run spike now scales with how many *fat* pieces one flow touches.** A CDN bundle inlines everything: its `package.json` declares **no dependencies** and every `require()` in `src/bundle.cjs` is a Node built-in. Good for install size and it is what finally kills the multi-copy `@activepieces/shared` problem — but two pieces that both use `axios`/`googleapis` no longer share one hoisted copy, they each carry their own. Measured on staging Aug 2026 (`AP_USE_CDN_FOR_BUNDLES=true`, isolate mode): 9 fat bundled pieces in one flow = ~15 MB of uncompressed JS, engine at **926 MB RSS / 788 MB heapUsed** against a V8 `heap_size_limit` of **1216 MB** (`--max-old-space-size=1024` from the default `SANDBOX_MEMORY_LIMIT`) inside a **1 GiB** container already holding a 247 MB worker + 74 MB pm2 → `MEMORY_LIMIT_EXCEEDED`, reproducibly, in ~8 s. The identical flow succeeds with the container raised to 3 GiB. This is the deterministic reproducer for the standing over-commit above: a per-run spike, not accumulation. Sizing rule: budget the engine ceiling against *container minus worker minus pm2*, and treat "many fat pieces in one flow" as the spike driver now that bundles don't share.
- **The cost of a resident `@activepieces/shared` copy is zod schema construction, not data — ~40 MB per copy, and reused engines collect one per piece version.** Heap-snapshotted a dedicated-worker engine at 596 MB RSS / 402 MB heap (0.88.1 beta, pre-CDN-bundles, `AP_REUSE_SANDBOX`), Aug 2026: after a double forced GC, 393 MB survived, and the histogram was **2.05 M anonymous closures (109 MB) + 143 MB of `(object properties)` arrays + 359 k `system/Context` scopes (18 MB)** — instantiated module graphs, not retained run data (only 16 k distinct functions back those 2 M closures). Every sampled retainer path ended `require.cache → @activepieces+shared@<version>/…/<dto>.js → exports → <ZodSchema>._def → get shape → refine/pipe/optional/brand closures`: `shared` eagerly builds ~500 top-level zod DTO trees at import, and zod v4 attaches per-instance accessor/method closures (120 k `get`, 67 k `set`, 31 k `validate` in that one heap). The engine held **9 shared versions at once** (0.37 → 0.96.2) because each piece bundle pins its own, and reuse + `import()` pins them forever — including **3 versions of `piece-hubspot` and 2 of `piece-slack` simultaneously**, one per flow-pinned piece version, so republish/upgrade churn multiplies copies of the *same* piece. Budget ≈ 60–70 MB engine baseline + ~40 MB per distinct resident shared copy. Two independent fixes attack it: CDN self-contained bundles (no shared inside pieces at all, see above) and require()-based piece LRU eviction; snapshot mechanics for redoing this measurement are in the `profile-worker-memory` skill. Follow-up exact measurement (same process at 542 MB heap, 9 shared copies, graph-cut retained sizes): one cleanly-severable copy weighs **41.3 MB / 445 k nodes**, but the other 8 each show <1 MB *exclusive* retained because copies are **co-retained as clusters, and the pin is the ESM module map, not `require.cache`**: deleting every `require.cache` entry under both `.bun` store roots frees only ~65 MB of 553 MB, while blocking the ESM `ModuleWrap`/`SyntheticModule` entries too frees **406 MB** (engine-only floor: 147 MB, which includes main.js's own bundled shared). Retainer chain: `Global handles → SyntheticModule (piece import()) → Piece object → action run closures → context:pieces_framework_1 / shared_1 (whole exports)` — every action closure captures its module scope, so a piece and its framework+shared copies live and die together. Consequence: any eviction scheme must make the *piece entry itself* collectable (hence require()-based loading in the LRU fix — an import()ed entry can never be dropped); purging shared's cache entries alone reclaims ~nothing.
diff --git a/packages/core/utils/package.json b/packages/core/utils/package.json
index b33518695a3..2f51b0a80ef 100644
--- a/packages/core/utils/package.json
+++ b/packages/core/utils/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/core-utils",
- "version": "0.7.0",
+ "version": "0.8.0",
"type": "commonjs",
"main": "./dist/src/index.js",
"scripts": {
diff --git a/packages/core/utils/src/index.ts b/packages/core/utils/src/index.ts
index 961246e206e..5a335eaca07 100644
--- a/packages/core/utils/src/index.ts
+++ b/packages/core/utils/src/index.ts
@@ -23,3 +23,4 @@ export * from './lib/form-errors'
export * from './lib/byte-lru-cache'
export * from './lib/ai-provider-health'
export * from './lib/deno'
+export * from './lib/sandbox-error'
diff --git a/packages/core/utils/src/lib/deno.ts b/packages/core/utils/src/lib/deno.ts
index 0a7a73e2d49..a020c90a5a8 100644
--- a/packages/core/utils/src/lib/deno.ts
+++ b/packages/core/utils/src/lib/deno.ts
@@ -1,5 +1,6 @@
import type { ChildProcessWithoutNullStreams } from 'node:child_process'
import { nanoid } from 'nanoid'
+import { sandboxError, SandboxErrorPayload } from './sandbox-error'
export const deno = {
/**
@@ -40,7 +41,7 @@ export const deno = {
}
if (resultJson === null) {
- reject(buildError({ message: `Deno process exited with code ${code} and signal ${signal} without returning a result`, stdout: userOutput, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: `Deno process exited with code ${code} and signal ${signal} without returning a result`, stdout: userOutput, stderr: capturedStderr }))
return
}
@@ -49,17 +50,17 @@ export const deno = {
message = JSON.parse(resultJson)
}
catch {
- reject(buildError({ message: 'Deno process returned a malformed result', stdout: userOutput, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: 'Deno process returned a malformed result', stdout: userOutput, stderr: capturedStderr }))
return
}
if (!message.success) {
- reject(buildError({ message: message.error, stdout: userOutput, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: message.error, stdout: userOutput, stderr: capturedStderr }))
}
else if (code !== 0) {
// e.g. an unhandled rejection fired after the result was printed — deno exits
// non-zero, so the run must fail even though a success marker exists.
- reject(buildError({ message: `Deno process exited with code ${code} and signal ${signal} after producing a result`, stdout: userOutput, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: `Deno process exited with code ${code} and signal ${signal} after producing a result`, stdout: userOutput, stderr: capturedStderr }))
}
else {
resolve(message.result)
@@ -72,7 +73,7 @@ export const deno = {
return
}
settled = true
- reject(buildError({ message: `Failed to spawn deno (${denoPath}): ${error.message}`, stdout: capturedStdout, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: `Failed to spawn deno (${denoPath}): ${error.message}`, stdout: capturedStdout, stderr: capturedStderr }))
})
})
},
@@ -166,12 +167,25 @@ function toPermissionFlags({ permissions, tmpDir }: { permissions: DenoPermissio
function buildRunProgram({ body, marker }: { body: string, marker: string }): string {
return `
+${sandboxError.payloadSource}
+let settled = false;
+const emit = (payload) => {
+ if (settled) return;
+ settled = true;
+ console.log(${JSON.stringify(marker)} + JSON.stringify(payload));
+};
+globalThis.addEventListener('unhandledrejection', (event) => {
+ event.preventDefault();
+ emit({ success: false, error: toErrorPayload(event.reason) });
+ Deno.exit(1);
+});
try {
${body}
- console.log(${JSON.stringify(marker)} + JSON.stringify({ success: true, result: result ?? null }));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ emit({ success: true, result: result ?? null });
}
catch (error) {
- console.log(${JSON.stringify(marker)} + JSON.stringify({ success: false, error: (error && error.stack) || String(error) }));
+ emit({ success: false, error: toErrorPayload(error) });
Deno.exit(1);
}
`
@@ -189,17 +203,6 @@ function extractResult(stdout: string, marker: string): { userOutput: string, re
return { userOutput: stdout.slice(0, idx) + trailing, resultJson }
}
-function buildError({ message, stdout, stderr }: BuildErrorParams): Error {
- const parts: string[] = [message ?? 'Code execution failed']
- if (stdout.trim()) {
- parts.push(`\n--- stdout ---\n${stdout.trim()}`)
- }
- if (stderr.trim()) {
- parts.push(`\n--- stderr ---\n${stderr.trim()}`)
- }
- return new Error(parts.join(''))
-}
-
const DEFAULT_MEMORY_LIMIT_MB = 128
export enum DenoPermission {
@@ -240,17 +243,11 @@ type NodeApis = {
fs: typeof import('node:fs/promises')
}
-type BuildErrorParams = {
- message: string
- stdout: string
- stderr: string
-}
-
type DenoResultMessage = {
success: true
result: unknown
} | {
success: false
- error: string
+ error: SandboxErrorPayload
}
diff --git a/packages/core/utils/src/lib/sandbox-error.ts b/packages/core/utils/src/lib/sandbox-error.ts
new file mode 100644
index 00000000000..f48a37e5d18
--- /dev/null
+++ b/packages/core/utils/src/lib/sandbox-error.ts
@@ -0,0 +1,51 @@
+import { isNil, isString } from './utils'
+
+export const sandboxError = {
+ build({ error, stdout, stderr }: BuildSandboxErrorParams): Error {
+ const payload = isString(error) ? { message: error } : error
+ const builtError = new Error(payload?.message ?? 'Code execution failed')
+ if (!isNil(payload?.name)) {
+ builtError.name = payload.name
+ }
+ builtError.stack = `${payload?.stack ?? builtError.stack}${formatCapturedOutput({ stdout, stderr })}`
+ return builtError
+ },
+
+ payloadSource: `const toErrorPayload = (value) => {
+ try {
+ if (value instanceof Error) {
+ return { message: value.message, name: value.name, stack: value.stack }
+ }
+ if (typeof value === 'string') {
+ return { message: value }
+ }
+ return { message: JSON.stringify(value) ?? String(value) }
+ }
+ catch {
+ return { message: 'Code execution failed' }
+ }
+}`,
+}
+
+function formatCapturedOutput({ stdout, stderr }: { stdout: string, stderr: string }): string {
+ const parts: string[] = []
+ if (stdout.trim()) {
+ parts.push(`\n--- stdout ---\n${stdout.trim()}`)
+ }
+ if (stderr.trim()) {
+ parts.push(`\n--- stderr ---\n${stderr.trim()}`)
+ }
+ return parts.join('')
+}
+
+export type SandboxErrorPayload = {
+ message: string
+ name?: string
+ stack?: string
+}
+
+type BuildSandboxErrorParams = {
+ error: SandboxErrorPayload | string | undefined
+ stdout: string
+ stderr: string
+}
diff --git a/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts b/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
index 1a159cd8ee9..4f61e172670 100644
--- a/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
+++ b/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
@@ -1,22 +1,23 @@
import { spawn } from 'node:child_process'
+import { sandboxError, SandboxErrorPayload } from '@activepieces/core-utils'
import { CodeSandbox } from '../../core/code/code-sandbox-common'
const CODE_RUNNER_SCRIPT = `
process.once('message', async function(msg) {
let settled = false
- const inspect = require('util').inspect
+ ${sandboxError.payloadSource}
process.on('unhandledRejection', (reason) => {
if (settled) return
settled = true
- process.send({ success: false, error: inspect(reason) }, () => process.exit(1))
+ process.send({ success: false, error: toErrorPayload(reason) }, () => process.exit(1))
})
process.on('uncaughtException', (err) => {
if (settled) return
settled = true
- process.send({ success: false, error: inspect(err) }, () => process.exit(1))
+ process.send({ success: false, error: toErrorPayload(err) }, () => process.exit(1))
})
try {
@@ -32,7 +33,7 @@ process.once('message', async function(msg) {
} catch(e) {
if (settled) return
settled = true
- process.send({ success: false, error: inspect(e) }, () => process.exit(0))
+ process.send({ success: false, error: toErrorPayload(e) }, () => process.exit(0))
}
})
`
@@ -60,27 +61,27 @@ async function runInChildProcess({ codeFilePath, inputs }: { codeFilePath: strin
let settled = false
- child.on('message', (msg: { success: boolean, result?: unknown, error?: string }) => {
+ child.on('message', (msg: { success: boolean, result?: unknown, error?: SandboxErrorPayload }) => {
if (settled) return
settled = true
if (msg.success) {
resolve(msg.result)
}
else {
- reject(buildError({ message: msg.error, stdout: capturedStdout, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: msg.error, stdout: capturedStdout, stderr: capturedStderr }))
}
})
child.on('close', (code, signal) => {
if (settled) return
settled = true
- reject(buildError({ message: `Code process exited with code ${code} and signal ${signal}`, stdout: capturedStdout, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: `Code process exited with code ${code} and signal ${signal}`, stdout: capturedStdout, stderr: capturedStderr }))
})
child.on('error', (error) => {
if (settled) return
settled = true
- reject(buildError({ message: error.message, stdout: capturedStdout, stderr: capturedStderr }))
+ reject(sandboxError.build({ error: error.message, stdout: capturedStdout, stderr: capturedStderr }))
})
if (typeof child.send !== 'function') {
@@ -91,17 +92,6 @@ async function runInChildProcess({ codeFilePath, inputs }: { codeFilePath: strin
})
}
-function buildError({ message, stdout, stderr }: { message: string | undefined, stdout: string, stderr: string }): Error {
- const parts: string[] = [message ?? 'Code execution failed']
- if (stdout.trim()) {
- parts.push(`\n--- stdout ---\n${stdout.trim()}`)
- }
- if (stderr.trim()) {
- parts.push(`\n--- stderr ---\n${stderr.trim()}`)
- }
- return new Error(parts.join(''))
-}
-
export const noOpCodeSandbox: CodeSandbox = {
async runCodeModule({ codeFilePath, inputs }) {
return runInChildProcess({ codeFilePath, inputs })
diff --git a/packages/server/engine/test/core/code/code-sandbox-error-contract.test.ts b/packages/server/engine/test/core/code/code-sandbox-error-contract.test.ts
new file mode 100644
index 00000000000..040e5844a3d
--- /dev/null
+++ b/packages/server/engine/test/core/code/code-sandbox-error-contract.test.ts
@@ -0,0 +1,96 @@
+import { unlink, writeFile } from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import { inspect } from 'node:util'
+import { apId, formatPieceError, FriendlyPieceError } from '@activepieces/core-utils'
+import { CodeSandbox } from '../../../src/lib/core/code/code-sandbox-common'
+import { denoCodeSandbox, DenoPermission } from '../../../src/lib/core/code/deno-code-sandbox'
+import { noOpCodeSandbox } from '../../../src/lib/core/code/no-op-code-sandbox'
+import { v8IsolateCodeSandbox } from '../../../src/lib/core/code/v8-isolate-code-sandbox'
+
+const SANDBOXES: SandboxUnderTest[] = [
+ { name: 'noOp', sandbox: noOpCodeSandbox, wrap: (body) => `exports.code = async () => { ${body} }`, extension: 'js' },
+ { name: 'v8Isolate', sandbox: v8IsolateCodeSandbox, wrap: (body) => `exports.code = async () => { ${body} }`, extension: 'js' },
+ { name: 'deno', sandbox: denoCodeSandbox([DenoPermission.NET]), wrap: (body) => `export const code = async () => { ${body} }`, extension: 'ts' },
+]
+
+const CHILD_PROCESS_SANDBOXES = SANDBOXES.filter((entry) => entry.name !== 'v8Isolate')
+
+const THROWING_BODIES: ThrowingBody[] = [
+ { label: 'a plain Error', body: 'throw new Error("Card declined")', message: 'Card declined' },
+ { label: 'a subclassed Error', body: 'null.x', message: "Cannot read properties of null (reading 'x')", errorName: 'TypeError' },
+ { label: 'an Error carrying own properties', body: 'const e = new Error("Card declined"); e.code = "X"; throw e', message: 'Card declined' },
+ { label: 'an Error whose message is JSON', body: 'throw new Error(JSON.stringify({ code: 42 }))', message: '{"code":42}' },
+ { label: 'a thrown string', body: 'throw "boom"', message: 'boom' },
+ { label: 'a user-defined Error subclass', body: 'class PaymentError extends Error { constructor(m) { super(m); this.name = "PaymentError" } } throw new PaymentError("Card declined")', message: 'Card declined', errorName: 'PaymentError' },
+ { label: 'a rejected promise', body: 'await Promise.reject(new Error("Card declined"))', message: 'Card declined' },
+ { label: 'a promise left unhandled after the step returns', body: 'Promise.reject(new Error("late boom")); return 1', message: 'late boom' },
+ { label: 'a thrown number', body: 'throw 42', message: '42' },
+]
+
+async function resolveStepError({ sandbox, wrap, extension, body }: ResolveStepErrorParams): Promise<FriendlyPieceError> {
+ const codeFilePath = path.join(os.tmpdir(), `ap-error-contract-${apId()}.${extension}`)
+ await writeFile(codeFilePath, wrap(body), 'utf8')
+ try {
+ await sandbox.runCodeModule({ codeFilePath, inputs: {} })
+ throw new Error('expected the code step to fail')
+ }
+ catch (error) {
+ return formatPieceError(error, { raw: inspect(error) })
+ }
+ finally {
+ await unlink(codeFilePath).catch(() => undefined)
+ }
+}
+
+describe('code sandbox error contract', () => {
+ describe.each(THROWING_BODIES)('$label', ({ body, message, errorName }) => {
+ it.each(SANDBOXES)('reads the same on $name', async ({ sandbox, wrap, extension }) => {
+ const stepError = await resolveStepError({ sandbox, wrap, extension, body })
+
+ expect(stepError.message).toBe(message)
+ expect(stepError.errorName).toBe(errorName)
+ expect(stepError.raw).toContain(message)
+ })
+ })
+
+ it.each(CHILD_PROCESS_SANDBOXES)('keeps captured output out of the message on $name', async ({ sandbox, wrap, extension }) => {
+ const stepError = await resolveStepError({ sandbox, wrap, extension, body: 'console.log("noisy"); throw new Error("Card declined")' })
+
+ expect(stepError.message).toBe('Card declined')
+ expect(stepError.raw).toContain('noisy')
+ })
+
+ it.each(CHILD_PROCESS_SANDBOXES)('keeps the fields of a thrown non-Error object on $name', async ({ sandbox, wrap, extension }) => {
+ const stepError = await resolveStepError({ sandbox, wrap, extension, body: 'throw { code: "CARD_DECLINED", amount: 42 }' })
+
+ expect(stepError.message).toBe('{"code":"CARD_DECLINED","amount":42}')
+ })
+
+ it.each(CHILD_PROCESS_SANDBOXES)('falls back instead of losing the failure when reading the thrown value throws on $name', async ({ sandbox, wrap, extension }) => {
+ const stepError = await resolveStepError({ sandbox, wrap, extension, body: 'const e = new Error("x"); Object.defineProperty(e, "message", { get() { throw new Error("nested") } }); throw e' })
+
+ expect(stepError.message).toBe('Code execution failed')
+ })
+})
+
+type SandboxUnderTest = {
+ name: string
+ sandbox: CodeSandbox
+ wrap: (body: string) => string
+ extension: string
+}
+
+type ThrowingBody = {
+ label: string
+ body: string
+ message: string
+ errorName?: string
+}
+
+type ResolveStepErrorParams = {
+ sandbox: CodeSandbox
+ wrap: (body: string) => string
+ extension: string
+ body: string
+}
diff --git a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
index 44887b8bf48..df6c0c0aa0c 100644
--- a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
+++ b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
@@ -36,21 +36,24 @@ async function runModule(source: string, inputs: Record<string, unknown> = {}):
const PERMISSION_DENIED = /NotCapable|PermissionDenied/
+async function expectRejection(run: Promise<unknown>, pattern: RegExp): Promise<void> {
+ const thrown = await run.then(() => null, (error: Error) => error)
+ expect(thrown).toBeInstanceOf(Error)
+ expect(String(thrown)).toMatch(pattern)
+}
+
describe('denoCodeSandbox permission boundary', () => {
describe('blocks unpermitted operations', () => {
it('rejects outbound network access', async () => {
- await expect(runModule(`export const code = async () => (await fetch('https://example.com')).status`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => (await fetch('https://example.com')).status`), PERMISSION_DENIED)
})
it('rejects reading a file outside the step directory', async () => {
- await expect(runModule(`export const code = async () => Deno.readTextFile('/etc/hosts')`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => Deno.readTextFile('/etc/hosts')`), PERMISSION_DENIED)
})
it('rejects reading environment variables', async () => {
- await expect(runModule(`export const code = async () => Deno.env.toObject()`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => Deno.env.toObject()`), PERMISSION_DENIED)
})
it('gives each run its own DENO_DIR and removes it after the run', async () => {
@@ -123,32 +126,29 @@ describe('denoCodeSandbox permission boundary', () => {
})
it('rejects the symlink escape (link inside dir -> outside, read through it)', async () => {
- await expect(runModule(`export const code = async () => {
+ await expectRejection(runModule(`export const code = async () => {
await Deno.symlink('/etc/passwd', './escape')
return Deno.readTextFile('./escape')
- }`)).rejects.toThrow(PERMISSION_DENIED)
+ }`), PERMISSION_DENIED)
})
it('rejects spawning a subprocess', async () => {
- await expect(runModule(`export const code = async () => {
+ await expectRejection(runModule(`export const code = async () => {
const out = await new Deno.Command('sh', { args: ['-c', 'id'] }).output()
return new TextDecoder().decode(out.stdout)
- }`)).rejects.toThrow(PERMISSION_DENIED)
+ }`), PERMISSION_DENIED)
})
it('rejects writing outside the step directory', async () => {
- await expect(runModule(`export const code = async () => Deno.writeTextFile('/tmp/ap-pwned.txt', 'hi')`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => Deno.writeTextFile('/tmp/ap-pwned.txt', 'hi')`), PERMISSION_DENIED)
})
it('rejects writing inside the step directory (locked profile grants no write)', async () => {
- await expect(runModule(`export const code = async () => Deno.writeTextFile('./data.json', 'x')`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => Deno.writeTextFile('./data.json', 'x')`), PERMISSION_DENIED)
})
it('rejects path traversal out of the step directory', async () => {
- await expect(runModule(`export const code = async () => Deno.readTextFile('../../../../etc/hosts')`))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(runModule(`export const code = async () => Deno.readTextFile('../../../../etc/hosts')`), PERMISSION_DENIED)
})
})
@@ -165,8 +165,7 @@ describe('denoCodeSandbox permission boundary', () => {
})
it('surfaces user thrown errors', async () => {
- await expect(runModule(`export const code = async () => { throw new Error('boom') }`))
- .rejects.toThrow(/boom/)
+ await expectRejection(runModule(`export const code = async () => { throw new Error('boom') }`), /boom/)
})
})
@@ -181,13 +180,11 @@ describe('denoCodeSandbox permission boundary', () => {
})
it('surfaces a TS syntax error as a catchable user failure', async () => {
- await expect(runModule(`export const code = async () => {`))
- .rejects.toThrow(/SyntaxError|Unexpected|expected/i)
+ await expectRejection(runModule(`export const code = async () => {`), /SyntaxError|Unexpected|expected/i)
})
it('rejects a module that does not export a code function', async () => {
- await expect(runModule(`export const notCode = 1`))
- .rejects.toThrow(/must export a "code" function/)
+ await expectRejection(runModule(`export const notCode = 1`), /must export a "code" function/)
})
})
@@ -221,8 +218,7 @@ describe('denoCodeSandbox permission boundary', () => {
})
it('rejects remote imports', async () => {
- await expect(runModule(`export const code = async () => (await import('https://example.com/mod.ts')).default`))
- .rejects.toThrow(/Requires import access|--no-remote/i)
+ await expectRejection(runModule(`export const code = async () => (await import('https://example.com/mod.ts')).default`), /Requires import access|--no-remote/i)
})
})
@@ -243,8 +239,7 @@ describe('denoCodeSandbox permission boundary', () => {
})
it('runs without any permissions (network blocked)', async () => {
- await expect(denoCodeSandbox.runScript({ script: `fetch('https://example.com')`, scriptContext: {}, functions: {} }))
- .rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(denoCodeSandbox.runScript({ script: `fetch('https://example.com')`, scriptContext: {}, functions: {} }), PERMISSION_DENIED)
})
})
@@ -264,10 +259,10 @@ describe('denoCodeSandbox permission boundary', () => {
await session.setGlobal('base', 100)
expect(await session.run('base')).toBe(40)
- await expect(session.run('missingVar.foo')).rejects.toThrow(/missingVar/)
+ await expectRejection(session.run('missingVar.foo'), /missingVar/)
expect(await session.run('Promise.resolve(base + step_1.out)')).toBe(45)
- await expect(session.run(`fetch('https://example.com')`)).rejects.toThrow(PERMISSION_DENIED)
+ await expectRejection(session.run(`fetch('https://example.com')`), PERMISSION_DENIED)
}
finally {
session.dispose()