brain/knowledge/execution-runtime/index.md
Where and how a flow job runs. The Worker is the Sandbox: it polls a job, resolves it, and forks the engine in-process. Destination model is concurrency 1 + horizontal replicas; a transitional mode still honors AP_WORKER_CONCURRENCY=N. Glossary below; the why lives in the Decision records nested under this page.
The deployment unit and the execution unit, now one. Polls jobs, acts as Resolver, runs each job in an in-process Sandbox, reports the result. Sole holder of the apiClient. Destination: concurrency 1 (one job per container), scaled horizontally (N replicas, each capped 0.5 CPU / 1 GB, so an OOM kills one worker โ blast radius one job).
AP_WORKER_CONCURRENCY=N by running N poll loops over N in-process boxes in one container. Default 5 (main's historical value), so the default deployment is this mode. See the decision Transitional multi-box concurrency.The single execution box the worker runs in-process. Given fully-resolved inputs it materializes them to disk, runs one engine operation in a child process, returns the result. Holds no app connection โ its only outbound traffic is pulling the blobs named in its params (S3 signed URLs, npm/file-store for pieces).
Turns a job into materialized box inputs: resolves flowVersion + piece metadata, produces a ready (compiled) Flow Bundle โ cache hit = existing S3 ref; miss = compile, build, publish to S3, then hand back the ref. Disables the flow on a missing piece. Always the worker (owns the only apiClient). Runs before execute, so the box only sees healthy, complete, compiled inputs.
The Sandbox's single entry point: { operationType, operation, timeoutInSeconds, settings, provision } โ { engineResponse, logs }. provision groups resolved deps { flowBundle?, pieces?, archiveRefs? }. Run/dispose are internal (acquire box โ run โ release, or invalidate on throw).
Whether a run reuses an already-booted engine process (warm โ steady state with AP_REUSE_SANDBOX) or forks a fresh one (cold โ the edge: first run after deploy/restart/scale-up, or reuse off). A property of dedicated execution, identical on self-host and Cloud โ not a Cloud-vs-self-host thing.
The four calls a run emits to the app during execution: updateRunProgress, updateStepProgress, sendFlowResponse, uploadRunLog. The engine posts all four directly over HTTP (internalApiUrl + engineToken), not back through the worker. uploadRunLog is dual-sourced: the worker also calls it to record a terminal status the engine couldn't (crash, OOM). See the decision Engine posts run-time callbacks directly to the app.
AP_WORKER_GROUP_ID) that realizes a Reservation by polling its own dedicated queue. The physical partition; the Reservation is the guarantee..tgz for one name@version, addressed as a link, resolved lazily in source order: own S3 bucket โ Activepieces CDN (official pieces only, behind AP_USE_CDN_FOR_BUNDLES) โ npm, with file-store serving ARCHIVE pieces directly. See the decision Pieces are distributed as links, resolved lazily.agent-worker-tools.ts โ RPC executePieceTool โ piece-tool-runner.ts โ flow-run-utils.ts โ actionRunService submits a separate action run that resolves its own piece from pieceName@pieceVersion. The flow bundle only ever needs @activepieces/piece-ai. flow-provisioning.ts used to scan step.settings.input['agentTools'] and union the result into resolvePieces (extractAgentToolPieceRefs, deleted 2026-08); it was installing packages into a sandbox nothing loaded them from. The lesson it was written for still holds wherever a validate-then-provision pair exists: provisioning must not be stricter than the engine. It strict-safeParsed each entry against AgentPieceTool and silently return []ed on failure, while the engine tolerated the legacy flat predefinedInput shape โ so pieces went un-provisioned and runs died INTERNAL_ERROR with an empty failedStep.parseManifest only invalidates on schemaVersion !== LATEST_FLOW_SCHEMA_VERSION. A bundle published by buggy/older worker code stays "valid", keeps being served for that locked flow version, and short-circuits resolvePieces โ so fixing the resolver code does not heal affected flows. Recovery is deleting the FLOW_BUNDLE file row (its id is the flowVersionId) + S3 object, or republishing the flow. Worth a bundle-format/generation field in the manifest.CDN_PIECES_URL (piece-bundle.ts) points at https://cdn.activepieces.com/pieces/bundled/ โ a 2026-08-13 seeding of the repackaged, self-contained tarballs, anonymously readable (200). It replaces pieces/retro/, whose ~1735 objects all answered 403 AccessDenied on both cdn.activepieces.com and the Spaces origin (object ACL, not the CDN); since cdnBundleExists counts only 2xx as present, that tier silently bought nothing but a wasted HEAD per resolve. AP_USE_CDN_FOR_BUNDLES defaults to false โ opt in per deployment. Two sharp edges survive the move: release-pieces.yml does not mirror to the bucket, so any version published after a seeding permanently misses; and safeHttp.axios sets no timeout, so an egress policy that blackholes the CDN hangs the existence check for the OS TCP connect timeout on the piece-install path instead of failing fast. Auditing a prefix means an anonymous curl against the exact URL the server builds โ an authenticated ls proves only that the bytes exist. Verified end-to-end on staging 2026-08-13 with the flag on: 1745 objects / 746 pieces, anonymously listable and readable, and the tarball a worker caches at cache/v14/common/pieces/<name>-<version>/bundle.tgz is byte-identical (md5 == CDN ETag) to the public object and carries src/bundle.cjs. The seeding holds one version per minor line as of that date, so latest versions 404 and fall back to npm โ the "published after a seeding permanently misses" edge is the common case, not the rare one.AP_USE_CDN_FOR_BUNDLES on is a one-way door for every piece version resolved during the rollout. The flag is per-app-container, and a rolling deploy runs flagged and unflagged containers side by side. An unflagged container that resolves a piece writes the npm tarball into pieces/v2/, and because resolve() checks S3 before the CDN that version is pinned to the unbundled copy permanently โ it never re-resolves, so finishing the rollout does not heal it. Measured on staging with only two app containers (Aug 2026): text-helper 0.5.1 came back as the 18 KB npm tarball (md5 68334b5cโฆ) instead of the 396 KB CDN bundle (fcdc62c9โฆ), while pieces resolved by the flagged container correctly logged source:"cdn". Cloud prod is 35 app containers across 5 hosts, so the window is far wider and lands on the hottest piece versions first. Deploying canary first surfaces it but does not avoid it; the only clean fixes are pre-seeding pieces/v2/ from the CDN before flipping, or deleting the poisoned keys afterwards.S3_PIECES_PREFIX, not purging it. resolve() (piece-bundle.ts) checks S3 before the CDN, so whatever BUNDLE_PIECE wrote wins for every later request. Until Aug 2026 that job cached the npm tarball, which for versions published before piece repackaging still declares its build-time deps โ measured cost: 12 resident @activepieces/shared versions holding 388 MB of a 554 MB engine heap on cloud. The job now prefers the CDN artifact, but fixing the writer does not fix the objects already written, and purging them cannot work: a rolling deploy leaves old app instances writing npm tarballs back into the prefix for the rest of the rollout, and the purge has no way to know when the last one is gone. So the prefix is versioned (pieces/ โ pieces/v2/) โ old code can only write the old prefix, so the new one is reachable only by a CDN-preferring writer. Same reflex as LATEST_CACHE_VERSION on the worker: when the meaning of a cached value changes, move the key; the abandoned prefix is dead storage to be swept later, never a correctness dependency.extractConnectionIds misses agent-tool connections. It only reads step/trigger settings.input.auth, never agentTools[].pieceMetadata.predefinedInput.auth, so flowVersion.connectionIds under-reports and "which flows use this connection" lies.functions entry must be a standalone declaration, never an object-method shorthand. The v8 isolate re-injects each entry as source via const ${key} = ${value.toString()} (v8-isolate-code-sandbox.ts). A standalone function flattenNestedKeys(...) {...} (as exported from script-evaluator.ts) stringifies to a valid RHS and keeps recursion working by its inner name; an inline object-method shorthand stringifies to flattenNestedKeys(...) {...}, a syntax error as a const RHS. Keep it a standalone function export, never a method. For the same reason do not relocate a sandbox-injected function behind a separately-built package boundary (e.g. @activepieces/core-utils): its serialized .toString() would then depend on that package's build/minify config staying isolate-friendly. The trap: no-op-code-sandbox.ts passes the function by reference and tolerates either form, so a test run that skips the isolated-vm suite ships the bug green. Related: the functions key is also the global name users type in flow inputs ({{flattenNestedKeys(...)}}), so it is a public contract string, not an implementation detail. Keep it a hardcoded literal (matched by FLATTEN_NESTED_KEYS_PATTERN in props-resolver.ts); never derive it from the function's .name, which mangles under minification and would wrongly couple the token to the JS identifier.core/piece/piece-context-builder.ts): project.externalId is a function the piece calls, not a value โ resolving it while building the context fires a /v1/worker/project request on every step; the backward-compatibility wrapper (backwardCompatabilityContextUtils.makeActionContextBackwardCompatible) must wrap the finished context or pieces on older context versions die with ctx.run.pause is not a function; and the legacy pause shim calls createWaitpoint() without awaiting it, so whoever owns the context has to drain in-flight hook work before the process ends or the waitpoint POST never lands and the run hangs until timeout.formatPieceError (friendly-piece-error.ts) reads error.response.{status,body}, error.status, and falls back to error.constructor.name for errorName โ but on HttpError (pieces-common) response is a prototype getter and name is plain 'Error'. Structured clone, {...e}, and JSON.stringify all copy own enumerable props only, so a child-process runner that ships an error back verbatim silently drops status, apiMessage, and the error name, and the step renders as an opaque JSON blob. Serialize errors explicitly: read the getter keys by name (response, request, status, headers, body, error) plus own props, and carry constructor.name as name. Same trap applies to the run result: it must be JSON round-tripped, or an unresolved promise/function anywhere in the returned object throws could not be cloned from process.send and fails the step.resolve(), never shared or hoisted. getPropsResolver(...).resolve(...) builds a fresh PropsResolver per call, creates the script session via scriptEvaluator.initSession(), and disposes it in resolve's finally, so an instance is single-use. Freshness is load-bearing: setGlobal is no-overwrite (v8-isolate-code-sandbox.ts) and injects each referenced step view once per resolve, so a session reused across resolves serves stale step views as flow state advances, and a reused instance would run on an already-disposed session. When refactoring props-resolver, capture getStepView and scriptSession inside resolve (they depend on the per-call executionState), not at instance scope, and never behind a shared mutable variable.๐ Decisions nested under this page: Worker is the Sandbox ยท Transitional multi-box concurrency ยท Engine posts run-time callbacks directly ยท Sandbox pool is a pure execute() (superseded) ยท Freeze piece versions in the Flow Bundle manifest.
kamal app exec leaking a permanent worker, serial per-queue dispatch as the real throughput cap, the silent mid-poll-loop wedge, and why polling starves first