Skip to content

Commit 33e6921

Browse files
improvement(execution): multiple response blocks (#3918)
* improvement(execution): multiple response blocks * address comments
1 parent adfcb67 commit 33e6921

File tree

5 files changed

+324
-11
lines changed

5 files changed

+324
-11
lines changed

apps/docs/content/docs/en/blocks/response.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The Response block formats and sends structured HTTP responses back to API calle
2020
</div>
2121

2222
<Callout type="info">
23-
Response blocks are terminal blocks - they end workflow execution and cannot connect to other blocks.
23+
Response blocks are exit points — when a Response block executes, it ends the workflow and sends the HTTP response immediately. Multiple Response blocks can be placed on different branches (e.g. after a Router or Condition), but only the first one to execute determines the API response.
2424
</Callout>
2525

2626
## Configuration Options
@@ -77,7 +77,11 @@ Condition (Error Detected) → Router → Response (400/500, Error Details)
7777

7878
## Outputs
7979

80-
Response blocks are terminal — no downstream blocks execute after them. However, the block does define outputs (`data`, `status`, `headers`) which are used to construct the HTTP response sent back to the API caller.
80+
Response blocks are exit points — when one executes, no further blocks run. The block defines outputs (`data`, `status`, `headers`) which are used to construct the HTTP response sent back to the API caller.
81+
82+
<Callout type="warning">
83+
If a Response block is placed on a parallel branch, there are no guarantees about whether other parallel blocks will run or not. Execution order across parallel branches is non-deterministic, so a parallel block may execute before or after the Response block on any given run. Avoid placing Response blocks in parallel with blocks that have important side effects.
84+
</Callout>
8185

8286
## Variable References
8387

@@ -110,10 +114,10 @@ Use the `<variable.name>` syntax to dynamically insert workflow variables into y
110114
- **Validate variable references**: Ensure all referenced variables exist and contain the expected data types before the Response block executes
111115

112116
<FAQ items={[
113-
{ question: "Can I have multiple Response blocks in a workflow?", answer: "No. The Response block is a single-instance block — only one is allowed per workflow. If you need different responses for different conditions, use a Condition or Router block upstream to determine what data reaches the single Response block." },
117+
{ question: "Can I have multiple Response blocks in a workflow?", answer: "Yes. You can place multiple Response blocks on different branches (e.g. after a Router or Condition block). The first Response block to execute determines the API response and ends the workflow. This is useful for returning different responses based on conditions — for example, a 200 on the success branch and a 500 on the error branch." },
114118
{ question: "What triggers require a Response block?", answer: "The Response block is designed for use with the API Trigger. When your workflow is invoked via the API, the Response block sends the structured HTTP response back to the caller. Other trigger types (like webhooks or schedules) do not require a Response block." },
115119
{ question: "What is the difference between Builder and Editor mode?", answer: "Builder mode provides a visual interface for constructing your response structure with fields and types. Editor mode gives you a raw JSON code editor where you can write the response body directly. Builder mode is recommended for most use cases." },
116120
{ question: "What is the default status code?", answer: "If you do not specify a status code, the Response block defaults to 200 (OK). You can set any valid HTTP status code including error codes like 400, 404, or 500." },
117-
{ question: "Can the Response block connect to downstream blocks?", answer: "No. Response blocks are terminal — they end workflow execution and send the HTTP response. No further blocks can be connected after a Response block." },
121+
{ question: "Can the Response block connect to downstream blocks?", answer: "No. Response blocks are exit points — they end workflow execution and send the HTTP response. No further blocks can execute after a Response block." },
118122
]} />
119123

apps/docs/content/docs/en/execution/basics.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ Understanding these core principles will help you build better workflows:
9696
2. **Automatic Parallelization**: Independent blocks run concurrently without configuration
9797
3. **Smart Data Flow**: Outputs flow automatically to connected blocks
9898
4. **Error Handling**: Failed blocks stop their execution path but don't affect independent paths
99-
5. **State Persistence**: All block outputs and execution details are preserved for debugging
100-
6. **Cycle Protection**: Workflows that call other workflows (via Workflow blocks, MCP tools, or API blocks) are tracked with a call chain. If the chain exceeds 25 hops, execution is stopped to prevent infinite loops
99+
5. **Response Blocks as Exit Points**: When a Response block executes, the entire workflow stops and the API response is sent immediately. Multiple Response blocks can exist on different branches — the first one to execute wins
100+
6. **State Persistence**: All block outputs and execution details are preserved for debugging
101+
7. **Cycle Protection**: Workflows that call other workflows (via Workflow blocks, MCP tools, or API blocks) are tracked with a call chain. If the chain exceeds 25 hops, execution is stopped to prevent infinite loops
101102

102103
## Next Steps
103104

apps/sim/blocks/blocks/response.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ export const ResponseBlock: BlockConfig<ResponseBlockOutput> = {
1212
bestPractices: `
1313
- Only use this if the trigger block is the API Trigger.
1414
- Prefer the builder mode over the editor mode.
15-
- This is usually used as the last block in the workflow.
15+
- The Response block is an exit point. When it executes, the workflow stops and the API response is sent immediately.
16+
- Multiple Response blocks can be placed on different branches (e.g. after a Router or Condition). The first one to execute determines the API response and ends the workflow.
17+
- If a Response block is on a parallel branch, there are no guarantees about whether other parallel blocks will run. Avoid placing Response blocks in parallel with blocks that have important side effects.
1618
`,
1719
category: 'blocks',
1820
bgColor: '#2F55FF',
1921
icon: ResponseIcon,
20-
singleInstance: true,
2122
subBlocks: [
2223
{
2324
id: 'dataMode',

apps/sim/executor/execution/engine.test.ts

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,297 @@ describe('ExecutionEngine', () => {
957957
})
958958
})
959959

960+
describe('Response block exit-point behavior', () => {
961+
it('should lock finalOutput and stop execution when a terminal Response block fires', async () => {
962+
const startNode = createMockNode('start', 'starter')
963+
const responseNode = createMockNode('response', 'response')
964+
965+
startNode.outgoingEdges.set('edge1', { target: 'response' })
966+
967+
const dag = createMockDAG([startNode, responseNode])
968+
const context = createMockContext()
969+
const edgeManager = createMockEdgeManager((node) => {
970+
if (node.id === 'start') return ['response']
971+
return []
972+
})
973+
974+
const nodeOrchestrator = {
975+
executionCount: 0,
976+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
977+
nodeOrchestrator.executionCount++
978+
if (nodeId === 'response') {
979+
return {
980+
nodeId,
981+
output: { data: { message: 'ok' }, status: 200, headers: {} },
982+
isFinalOutput: true,
983+
}
984+
}
985+
return { nodeId, output: {}, isFinalOutput: false }
986+
}),
987+
handleNodeCompletion: vi.fn(),
988+
} as unknown as MockNodeOrchestrator
989+
990+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
991+
const result = await engine.run('start')
992+
993+
expect(result.success).toBe(true)
994+
expect(result.output).toEqual({ data: { message: 'ok' }, status: 200, headers: {} })
995+
expect(nodeOrchestrator.executionCount).toBe(2)
996+
})
997+
998+
it('should stop execution after Response block on a branch (Router)', async () => {
999+
const startNode = createMockNode('start', 'starter')
1000+
const routerNode = createMockNode('router', 'router')
1001+
const successResponse = createMockNode('success-response', 'response')
1002+
const errorResponse = createMockNode('error-response', 'response')
1003+
1004+
startNode.outgoingEdges.set('edge1', { target: 'router' })
1005+
routerNode.outgoingEdges.set('success', { target: 'success-response' })
1006+
routerNode.outgoingEdges.set('error', { target: 'error-response' })
1007+
1008+
const dag = createMockDAG([startNode, routerNode, successResponse, errorResponse])
1009+
const context = createMockContext()
1010+
const edgeManager = createMockEdgeManager((node) => {
1011+
if (node.id === 'start') return ['router']
1012+
if (node.id === 'router') return ['success-response']
1013+
return []
1014+
})
1015+
1016+
const executedNodes: string[] = []
1017+
const nodeOrchestrator = {
1018+
executionCount: 0,
1019+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1020+
executedNodes.push(nodeId)
1021+
nodeOrchestrator.executionCount++
1022+
if (nodeId === 'success-response') {
1023+
return {
1024+
nodeId,
1025+
output: { data: { result: 'success' }, status: 200, headers: {} },
1026+
isFinalOutput: true,
1027+
}
1028+
}
1029+
return { nodeId, output: {}, isFinalOutput: false }
1030+
}),
1031+
handleNodeCompletion: vi.fn(),
1032+
} as unknown as MockNodeOrchestrator
1033+
1034+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1035+
const result = await engine.run('start')
1036+
1037+
expect(result.success).toBe(true)
1038+
expect(result.output).toEqual({ data: { result: 'success' }, status: 200, headers: {} })
1039+
expect(executedNodes).not.toContain('error-response')
1040+
})
1041+
1042+
it('should stop all branches when a parallel Response block fires first', async () => {
1043+
const startNode = createMockNode('start', 'starter')
1044+
const responseNode = createMockNode('fast-response', 'response')
1045+
const slowNode = createMockNode('slow-work', 'function')
1046+
const afterSlowNode = createMockNode('after-slow', 'function')
1047+
1048+
startNode.outgoingEdges.set('edge1', { target: 'fast-response' })
1049+
startNode.outgoingEdges.set('edge2', { target: 'slow-work' })
1050+
slowNode.outgoingEdges.set('edge3', { target: 'after-slow' })
1051+
1052+
const dag = createMockDAG([startNode, responseNode, slowNode, afterSlowNode])
1053+
const context = createMockContext()
1054+
const edgeManager = createMockEdgeManager((node) => {
1055+
if (node.id === 'start') return ['fast-response', 'slow-work']
1056+
if (node.id === 'slow-work') return ['after-slow']
1057+
return []
1058+
})
1059+
1060+
const executedNodes: string[] = []
1061+
const nodeOrchestrator = {
1062+
executionCount: 0,
1063+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1064+
executedNodes.push(nodeId)
1065+
nodeOrchestrator.executionCount++
1066+
if (nodeId === 'fast-response') {
1067+
return {
1068+
nodeId,
1069+
output: { data: { fast: true }, status: 200, headers: {} },
1070+
isFinalOutput: true,
1071+
}
1072+
}
1073+
if (nodeId === 'slow-work') {
1074+
await new Promise((resolve) => setTimeout(resolve, 1))
1075+
return { nodeId, output: { slow: true }, isFinalOutput: false }
1076+
}
1077+
return { nodeId, output: {}, isFinalOutput: true }
1078+
}),
1079+
handleNodeCompletion: vi.fn(),
1080+
} as unknown as MockNodeOrchestrator
1081+
1082+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1083+
const result = await engine.run('start')
1084+
1085+
expect(result.success).toBe(true)
1086+
expect(result.output).toEqual({ data: { fast: true }, status: 200, headers: {} })
1087+
expect(executedNodes).not.toContain('after-slow')
1088+
})
1089+
1090+
it('should use standard finalOutput logic when no Response block exists', async () => {
1091+
const startNode = createMockNode('start', 'starter')
1092+
const endNode = createMockNode('end', 'function')
1093+
startNode.outgoingEdges.set('edge1', { target: 'end' })
1094+
1095+
const dag = createMockDAG([startNode, endNode])
1096+
const context = createMockContext()
1097+
const edgeManager = createMockEdgeManager((node) => {
1098+
if (node.id === 'start') return ['end']
1099+
return []
1100+
})
1101+
1102+
const nodeOrchestrator = {
1103+
executionCount: 0,
1104+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1105+
nodeOrchestrator.executionCount++
1106+
if (nodeId === 'end') {
1107+
return { nodeId, output: { result: 'done' }, isFinalOutput: true }
1108+
}
1109+
return { nodeId, output: {}, isFinalOutput: false }
1110+
}),
1111+
handleNodeCompletion: vi.fn(),
1112+
} as unknown as MockNodeOrchestrator
1113+
1114+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1115+
const result = await engine.run('start')
1116+
1117+
expect(result.success).toBe(true)
1118+
expect(result.output).toEqual({ result: 'done' })
1119+
})
1120+
1121+
it('should not let a second Response block overwrite the first', async () => {
1122+
const startNode = createMockNode('start', 'starter')
1123+
const response1 = createMockNode('response1', 'response')
1124+
const response2 = createMockNode('response2', 'response')
1125+
1126+
startNode.outgoingEdges.set('edge1', { target: 'response1' })
1127+
startNode.outgoingEdges.set('edge2', { target: 'response2' })
1128+
1129+
const dag = createMockDAG([startNode, response1, response2])
1130+
const context = createMockContext()
1131+
const edgeManager = createMockEdgeManager((node) => {
1132+
if (node.id === 'start') return ['response1', 'response2']
1133+
return []
1134+
})
1135+
1136+
const nodeOrchestrator = {
1137+
executionCount: 0,
1138+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1139+
nodeOrchestrator.executionCount++
1140+
if (nodeId === 'response1') {
1141+
return {
1142+
nodeId,
1143+
output: { data: { first: true }, status: 200, headers: {} },
1144+
isFinalOutput: true,
1145+
}
1146+
}
1147+
if (nodeId === 'response2') {
1148+
return {
1149+
nodeId,
1150+
output: { data: { second: true }, status: 201, headers: {} },
1151+
isFinalOutput: true,
1152+
}
1153+
}
1154+
return { nodeId, output: {}, isFinalOutput: false }
1155+
}),
1156+
handleNodeCompletion: vi.fn(),
1157+
} as unknown as MockNodeOrchestrator
1158+
1159+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1160+
const result = await engine.run('start')
1161+
1162+
expect(result.success).toBe(true)
1163+
expect(result.output).toEqual({ data: { first: true }, status: 200, headers: {} })
1164+
})
1165+
1166+
it('should not let non-Response terminals overwrite a Response block output', async () => {
1167+
const startNode = createMockNode('start', 'starter')
1168+
const responseNode = createMockNode('response', 'response')
1169+
const otherTerminal = createMockNode('other', 'function')
1170+
1171+
startNode.outgoingEdges.set('edge1', { target: 'response' })
1172+
startNode.outgoingEdges.set('edge2', { target: 'other' })
1173+
1174+
const dag = createMockDAG([startNode, responseNode, otherTerminal])
1175+
const context = createMockContext()
1176+
const edgeManager = createMockEdgeManager((node) => {
1177+
if (node.id === 'start') return ['response', 'other']
1178+
return []
1179+
})
1180+
1181+
const nodeOrchestrator = {
1182+
executionCount: 0,
1183+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1184+
nodeOrchestrator.executionCount++
1185+
if (nodeId === 'response') {
1186+
return {
1187+
nodeId,
1188+
output: { data: { response: true }, status: 200, headers: {} },
1189+
isFinalOutput: true,
1190+
}
1191+
}
1192+
if (nodeId === 'other') {
1193+
await new Promise((resolve) => setTimeout(resolve, 1))
1194+
return { nodeId, output: { other: true }, isFinalOutput: true }
1195+
}
1196+
return { nodeId, output: {}, isFinalOutput: false }
1197+
}),
1198+
handleNodeCompletion: vi.fn(),
1199+
} as unknown as MockNodeOrchestrator
1200+
1201+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1202+
const result = await engine.run('start')
1203+
1204+
expect(result.success).toBe(true)
1205+
expect(result.output).toEqual({ data: { response: true }, status: 200, headers: {} })
1206+
})
1207+
1208+
it('should honor locked Response output even when a parallel node throws an error', async () => {
1209+
const startNode = createMockNode('start', 'starter')
1210+
const responseNode = createMockNode('response', 'response')
1211+
const errorNode = createMockNode('error-node', 'function')
1212+
1213+
startNode.outgoingEdges.set('edge1', { target: 'response' })
1214+
startNode.outgoingEdges.set('edge2', { target: 'error-node' })
1215+
1216+
const dag = createMockDAG([startNode, responseNode, errorNode])
1217+
const context = createMockContext()
1218+
const edgeManager = createMockEdgeManager((node) => {
1219+
if (node.id === 'start') return ['response', 'error-node']
1220+
return []
1221+
})
1222+
1223+
const nodeOrchestrator = {
1224+
executionCount: 0,
1225+
executeNode: vi.fn().mockImplementation(async (_ctx: ExecutionContext, nodeId: string) => {
1226+
nodeOrchestrator.executionCount++
1227+
if (nodeId === 'response') {
1228+
return {
1229+
nodeId,
1230+
output: { data: { ok: true }, status: 200, headers: {} },
1231+
isFinalOutput: true,
1232+
}
1233+
}
1234+
if (nodeId === 'error-node') {
1235+
await new Promise((resolve) => setTimeout(resolve, 1))
1236+
throw new Error('Parallel branch failed')
1237+
}
1238+
return { nodeId, output: {}, isFinalOutput: false }
1239+
}),
1240+
handleNodeCompletion: vi.fn(),
1241+
} as unknown as MockNodeOrchestrator
1242+
1243+
const engine = new ExecutionEngine(context, dag, edgeManager, nodeOrchestrator)
1244+
const result = await engine.run('start')
1245+
1246+
expect(result.success).toBe(true)
1247+
expect(result.output).toEqual({ data: { ok: true }, status: 200, headers: {} })
1248+
})
1249+
})
1250+
9601251
describe('Cancellation flag behavior', () => {
9611252
it('should set cancelledFlag when abort signal fires', async () => {
9621253
const abortController = new AbortController()

0 commit comments

Comments
 (0)