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(engine): a step test in a Failure branch resolves the parent's error message (#16216)

Othman Abu Ajamieh41aa5cb86734
diff --git a/brain/knowledge/flows-execution/flows.md b/brain/knowledge/flows-execution/flows.md
index 42f41642aba..c89dec66180 100644
--- a/brain/knowledge/flows-execution/flows.md
+++ b/brain/knowledge/flows-execution/flows.md
@@ -23,6 +23,7 @@ Flows are the core automation primitive: a versioned directed graph of trigger +
- **Step settings autosave from inside the form resolver, on every validating `setValue`.** `step-settings/index.tsx` runs `applyOperation(UPDATE_ACTION/UPDATE_TRIGGER)` in its `resolver` whenever the new values differ from the last saved snapshot — it is *not* gated on `isDirty` or on a submit. Any transient value a component writes with `shouldValidate: true` is therefore persisted immediately, including one it intends to overwrite a moment later from an async response.
- **A CODE step's compiled size is its `packageJson`, not its code — bundling *inlines* `node_modules`, it does not exclude it.** This gets assumed backwards a lot: there is no `node_modules` at runtime precisely *because* esbuild inlines every dependency into the step's `index.js`. Measured on cloud, Aug 2026: a step with **1,870 characters** of user source and `{"pdfkit":"0.14.0","aws-sdk":"2.1531.0","uuid":"9.0.1"}` compiles to **24.13 MB**, of which 24.13 MB is `node_modules` — **21.16 MB of it `aws-sdk` alone**. v2 of that SDK resolves its ~200 service clients by dynamic `require`, so esbuild cannot tree-shake it and inlines all of them; `@aws-sdk/client-*` (v3) would be a few hundred KB. Fleet-wide there were 13,918 compiled steps totalling 1.4 GB, **61 of them over 10 MB**, and per *flow* the totals reach **190.7 MB across 40 code steps**. That per-flow number is the one that matters operationally, because `flowBundleStore.publish` holds a flow's entire compiled output in memory at once (three copies — see the OOM gotcha on [[workers]]). To find the offenders: esbuild leaves `// node_modules/<pkg>/…` markers in the output, so you can attribute bytes per package by summing the lines between markers.
- **Step output nesting (schema v21+)**: every step output is wrapped as `{ output, error? }`; expressions must use the `['output']` accessor. The v20→v21 migration rewrites existing expressions via `expression-rewriter`.
+- **A single-step test never runs the steps before it — `testExecutionContext.stateFromFlowVersion` seeds them from sample data, and seeds them SUCCEEDED.** The one exception is a continue-on-failure step whose `onFailure` branch holds the tested step: it is seeded FAILED with `---runtime error message---` inside the `formatPieceError` envelope, the same placeholder the Data Selector shows, so `{{parent['error']['message']}}` stops resolving to `""` in a test (GIT-1924). A plain sibling after a continue-on-failure step has no Failure branch to read, so its parent stays SUCCEEDED and the reference is still `""`. `executeProps` passes no tested step, so dropdowns never see the seeded failure. The seeded FAILED step is in the test run's log, so MCP `ap_test_step` lists it with ❌ even though the run succeeded.
- **Migrations run on every single-version read, including the worker's own `getFlowVersion`** (`findOne` → `flowVersionMigrationService.migrate`). So no run can execute an unmigrated version, and restoring an old version migrates it forward too. The listing path is the exception: `flowVersionService.list()` paginates the repository directly and hands back whatever schema version the row holds, so a caller reading the version history can see unmigrated rows. The result is persisted, then short-circuits at `LATEST_FLOW_SCHEMA_VERSION`. The old version is saved to `backupFiles[oldSchemaVersion]` first — **that** is what a restore reads, so there's no need to keep an old code path alive "just in case". Nothing restores on its own: `flowVersionBackupService.get()` has no callers, so recovering a version is a deliberate manual step. A migration that throws pages on-call (`FLOW_MIGRATION_FAILED`).
- **Template migrations run on a flow version that is not stored.** `migrateFlowVersionTemplate` (the `IMPORT_FLOW` pre-validation and the template controllers) builds a stand-in version with `flowId: ''` and passes no `MigrationContext`, so there is no project or platform either. A migration that looks anything up by flow id must treat "not found" as "nothing stored", never throw: the v23 piece-upgrade audit event used `findOneByOrFail`, which turned every import of an old template holding a register-listed piece into a 500 (the event-destinations "Generate handler flow" hit it with webhook `0.1.33`).
- **Continue on Failure**: CODE/PIECE steps with `continueOnFailure.value: true` carry `onSuccess`/`onFailure` sub-trees under `settings.errorHandlingOptions.continueOnFailureBranches`.
diff --git a/packages/server/engine/src/lib/handler/context/test-execution-context.ts b/packages/server/engine/src/lib/handler/context/test-execution-context.ts
index 9da372fc3fa..72fd1742f6b 100644
--- a/packages/server/engine/src/lib/handler/context/test-execution-context.ts
+++ b/packages/server/engine/src/lib/handler/context/test-execution-context.ts
@@ -1,6 +1,6 @@
-import { isNil, spreadIfDefined } from '@activepieces/core-utils'
+import { formatPieceError, isNil, spreadIfDefined } from '@activepieces/core-utils'
import { LATEST_CONTEXT_VERSION } from '@activepieces/pieces-framework'
-import { AiRouterStepOutput, FlowActionType, flowStructureUtil, FlowTriggerType, FlowVersion, GenericStepOutput, LoopStepOutput, RouterStepOutput, StepOutputStatus } from '@activepieces/shared'
+import { AiRouterStepOutput, FlowActionType, flowCanvasUtils, flowStructureUtil, FlowTriggerType, FlowVersion, GenericStepOutput, LoopStepOutput, RouterStepOutput, Step, StepOutputStatus } from '@activepieces/shared'
import { createPropsResolver } from '../../variables/props-resolver'
import { EngineConstants } from './engine-constants'
import { FlowExecutorContext } from './flow-execution-context'
@@ -81,20 +81,30 @@ export const testExecutionContext = {
case FlowActionType.PIECE:
case FlowActionType.CODE:
case FlowTriggerType.EMPTY:
- case FlowTriggerType.PIECE:
- flowExecutionContext = await flowExecutionContext.upsertStep(step.name, GenericStepOutput.create({
+ case FlowTriggerType.PIECE: {
+ const stepOutput = GenericStepOutput.create({
input: {},
type: stepType,
status: StepOutputStatus.SUCCEEDED,
...spreadIfDefined('output', sampleData?.[step.name]),
- }))
+ })
+ flowExecutionContext = await flowExecutionContext.upsertStep(step.name, isOnFailureBranchAncestor({ step, excludedStepName })
+ ? stepOutput.setStatus(StepOutputStatus.FAILED).setErrorMessage(PLACEHOLDER_ERROR_MESSAGE)
+ : stepOutput)
break
+ }
}
}
return flowExecutionContext
},
}
+const PLACEHOLDER_ERROR_MESSAGE = JSON.stringify(formatPieceError('---runtime error message---'))
+
+function isOnFailureBranchAncestor({ step, excludedStepName }: { step: Step, excludedStepName: string | undefined }): boolean {
+ return !isNil(excludedStepName) && flowCanvasUtils.getStepBranchRelativeTo(step, excludedStepName) === 'on-failure'
+}
+
type TestExecutionParams = {
engineConstants: EngineConstants
diff --git a/packages/server/engine/test/handler/context/test-execution-context.test.ts b/packages/server/engine/test/handler/context/test-execution-context.test.ts
new file mode 100644
index 00000000000..710ffd0a1f2
--- /dev/null
+++ b/packages/server/engine/test/handler/context/test-execution-context.test.ts
@@ -0,0 +1,175 @@
+import { tryParseFriendlyPieceError } from '@activepieces/core-utils'
+import { LATEST_CONTEXT_VERSION } from '@activepieces/pieces-framework'
+import { CodeAction, FlowAction, FlowActionType, FlowTriggerType, FlowVersion, FlowVersionState, StepOutputStatus } from '@activepieces/shared'
+import { FlowExecutorContext } from '../../../src/lib/handler/context/flow-execution-context'
+import { testExecutionContext } from '../../../src/lib/handler/context/test-execution-context'
+import { createPropsResolver } from '../../../src/lib/variables/props-resolver'
+import { generateMockEngineConstants } from '../test-helper'
+
+describe('testExecutionContext.stateFromFlowVersion', () => {
+ it('seeds the parent as failed when the tested step is in its failure branch', async () => {
+ const flowVersion = buildFlowVersion({
+ firstAction: buildCodeStep({
+ name: 'parent',
+ continueOnFailure: true,
+ onFailure: buildCodeStep({ name: 'failure_child' }),
+ }),
+ })
+
+ const state = await buildState({ flowVersion, excludedStepName: 'failure_child', sampleData: { parent: { id: 7 } } })
+ const resolved = await resolveInput({
+ state,
+ input: { message: '{{parent[\'error\'][\'message\']}}', id: '{{parent[\'output\'][\'id\']}}' },
+ })
+
+ expect(state.getStepOutput('parent')?.status).toBe(StepOutputStatus.FAILED)
+ expect(tryParseFriendlyPieceError(resolved.message)?.message).toBe('---runtime error message---')
+ expect(resolved.id).toBe(7)
+ })
+
+ it('keeps the parent succeeded when the tested step is in its success branch', async () => {
+ const flowVersion = buildFlowVersion({
+ firstAction: buildCodeStep({
+ name: 'parent',
+ continueOnFailure: true,
+ onSuccess: buildCodeStep({ name: 'success_child' }),
+ onFailure: buildCodeStep({ name: 'failure_child' }),
+ }),
+ })
+
+ const state = await buildState({ flowVersion, excludedStepName: 'success_child' })
+ const resolved = await resolveInput({ state, input: { message: '{{parent[\'error\'][\'message\']}}' } })
+
+ expect(state.getStepOutput('parent')?.status).toBe(StepOutputStatus.SUCCEEDED)
+ expect(resolved.message).toBe('')
+ })
+
+ it('fails only the ancestor whose failure branch holds the tested step', async () => {
+ const flowVersion = buildFlowVersion({
+ firstAction: buildCodeStep({
+ name: 'outer',
+ continueOnFailure: true,
+ onSuccess: buildCodeStep({
+ name: 'inner',
+ continueOnFailure: true,
+ onFailure: buildCodeStep({ name: 'nested_child' }),
+ }),
+ }),
+ })
+
+ const state = await buildState({ flowVersion, excludedStepName: 'nested_child' })
+
+ expect(state.getStepOutput('inner')?.status).toBe(StepOutputStatus.FAILED)
+ expect(state.getStepOutput('outer')?.status).toBe(StepOutputStatus.SUCCEEDED)
+ })
+
+ it('keeps the parent succeeded when the tested step is a plain sibling after it', async () => {
+ const flowVersion = buildFlowVersion({
+ firstAction: buildCodeStep({
+ name: 'parent',
+ continueOnFailure: true,
+ nextAction: buildCodeStep({ name: 'sibling' }),
+ }),
+ })
+
+ const state = await buildState({ flowVersion, excludedStepName: 'sibling' })
+
+ expect(state.getStepOutput('parent')?.status).toBe(StepOutputStatus.SUCCEEDED)
+ })
+
+ it('keeps every step succeeded when no step is under test', async () => {
+ const flowVersion = buildFlowVersion({
+ firstAction: buildCodeStep({
+ name: 'parent',
+ continueOnFailure: true,
+ onFailure: buildCodeStep({ name: 'failure_child' }),
+ }),
+ })
+
+ const state = await buildState({ flowVersion })
+
+ expect(state.getStepOutput('parent')?.status).toBe(StepOutputStatus.SUCCEEDED)
+ expect(state.getStepOutput('failure_child')?.status).toBe(StepOutputStatus.SUCCEEDED)
+ })
+})
+
+function buildState({ flowVersion, excludedStepName, sampleData }: {
+ flowVersion: FlowVersion
+ excludedStepName?: string
+ sampleData?: Record<string, unknown>
+}): Promise<FlowExecutorContext> {
+ return testExecutionContext.stateFromFlowVersion({
+ flowVersion,
+ excludedStepName,
+ sampleData,
+ projectId: 'projectId',
+ apiUrl: 'http://127.0.0.1:3000/',
+ engineToken: 'engineToken',
+ engineConstants: generateMockEngineConstants(),
+ })
+}
+
+async function resolveInput<T>({ state, input }: { state: FlowExecutorContext, input: T }): Promise<T> {
+ const { resolvedInput } = await createPropsResolver({
+ projectId: 'projectId',
+ engineToken: 'engineToken',
+ apiUrl: 'http://127.0.0.1:3000/',
+ contextVersion: LATEST_CONTEXT_VERSION,
+ stepNames: STEP_NAMES,
+ }).resolve<T>({ unresolvedInput: input, executionState: state })
+ return resolvedInput
+}
+
+function buildCodeStep({ name, continueOnFailure, onSuccess, onFailure, nextAction }: {
+ name: string
+ continueOnFailure?: boolean
+ onSuccess?: FlowAction
+ onFailure?: FlowAction
+ nextAction?: FlowAction
+}): CodeAction {
+ return {
+ name,
+ displayName: name,
+ type: FlowActionType.CODE,
+ skip: false,
+ valid: true,
+ lastUpdatedDate: LAST_UPDATED_DATE,
+ settings: {
+ input: {},
+ sourceCode: { packageJson: '', code: '' },
+ errorHandlingOptions: { continueOnFailure: { value: continueOnFailure ?? false } },
+ },
+ continueOnFailureBranches: { onSuccess, onFailure },
+ nextAction,
+ }
+}
+
+function buildFlowVersion({ firstAction }: { firstAction: FlowAction }): FlowVersion {
+ return {
+ id: 'flowVersionId',
+ created: LAST_UPDATED_DATE,
+ updated: LAST_UPDATED_DATE,
+ flowId: 'flowId',
+ displayName: 'Test Flow',
+ trigger: {
+ name: 'trigger',
+ valid: true,
+ displayName: 'Trigger',
+ type: FlowTriggerType.EMPTY,
+ settings: {},
+ lastUpdatedDate: LAST_UPDATED_DATE,
+ nextAction: firstAction,
+ },
+ updatedBy: null,
+ valid: true,
+ schemaVersion: null,
+ agentIds: [],
+ state: FlowVersionState.DRAFT,
+ connectionIds: [],
+ backupFiles: null,
+ notes: [],
+ }
+}
+
+const STEP_NAMES = ['trigger', 'parent', 'failure_child', 'success_child', 'outer', 'inner', 'nested_child', 'sibling']
+const LAST_UPDATED_DATE = '2026-01-01T00:00:00Z'