Skip to content

Commit 48dd9b9

Browse files
committed
fix(executor): sync blockOutputs return selected child-workflow block outputs
A child-workflow selector was admitted on a synchronous run but the response projection only read parent block logs, so its key was silently missing from blockOutputs. Read the child block's output from the trace spans its workflow block carried back, matching by block ID or normalized name.
1 parent 461ae6b commit 48dd9b9

2 files changed

Lines changed: 107 additions & 5 deletions

File tree

‎apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* `selectedOutputs` on a deployed run of a workflow that calls a child workflow, against real
33
* PostgreSQL through the execute use case. A selector naming a parent block must reach the
44
* executor in its internal form, so the child-workflow block never mistakes `block.path` for a
5-
* `workflow.block` selector; a selector that targets the child but names nothing there must not
5+
* `workflow.block` selector; a selector that targets a child block returns that block's output
6+
* under the caller's selector; one that targets the child but names nothing there must not
67
* fail a run that otherwise succeeded; and a selector naming no parent block is refused before
78
* the run starts, so it is never executed or billed.
89
*/
@@ -206,13 +207,28 @@ describe('selectedOutputs on a run that calls a child workflow', () => {
206207
})
207208
})
208209

209-
it('completes when a selector targets the child workflow', async () => {
210-
const result = await run([`${childWorkflowId}.childdone.data`, 'done.data'])
210+
it('returns a child block output selected by child workflow, block name, and path', async () => {
211+
const childSelector = `${childWorkflowId}.childdone.data`
212+
const result = await run([childSelector, 'done.data'])
211213

212214
expect(result).toMatchObject({
213215
ok: true,
214216
status: 'completed',
215-
blockOutputs: { 'done.data': { answer: 'from child' } },
217+
blockOutputs: {
218+
[childSelector]: { answer: 'from child' },
219+
'done.data': { answer: 'from child' },
220+
},
221+
})
222+
})
223+
224+
it('returns a child block output selected by child block ID', async () => {
225+
const childSelector = `${childWorkflowId}.${blockIds.childDone}.data.answer`
226+
const result = await run([childSelector])
227+
228+
expect(result).toMatchObject({
229+
ok: true,
230+
status: 'completed',
231+
blockOutputs: { [childSelector]: 'from child' },
216232
})
217233
})
218234

‎apps/sim/lib/workflows/executor/execute-service.ts‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { containsLargeValueRef } from '@/lib/execution/payloads/large-value-ref'
1919
import { compactExecutionPayload } from '@/lib/execution/payloads/serializer'
2020
import { preprocessExecution } from '@/lib/execution/preprocessing'
2121
import { LoggingSession } from '@/lib/logs/execution/logging-session'
22+
import type { TraceSpan } from '@/lib/logs/types'
2223
import { MAX_MCP_WORKFLOW_RESPONSE_BYTES } from '@/lib/mcp/constants'
2324
import { hydrateUserFilesWithBase64 } from '@/lib/uploads/utils/user-file-base64.server'
2425
import { enqueueWorkflowExecution } from '@/lib/workflows/executor/enqueue-execution'
@@ -38,12 +39,17 @@ import {
3839
type NormalizedWorkflowData,
3940
} from '@/lib/workflows/persistence/utils'
4041
import { shouldEmitAgentStreamEvents } from '@/lib/workflows/streaming/agent-stream-protocol'
42+
import {
43+
type ParsedOutputSelector,
44+
parseInternalOutputSelector,
45+
} from '@/lib/workflows/streaming/output-selector'
4146
import { resolveOutputSelectors } from '@/lib/workflows/streaming/resolve-output-selectors'
4247
import {
4348
agentStreamProtocolResponseHeaders,
4449
createStreamingResponse,
4550
} from '@/lib/workflows/streaming/streaming'
4651
import { workflowHasResponseBlock } from '@/lib/workflows/utils'
52+
import { normalizeName } from '@/executor/constants'
4753
import { ExecutionSnapshot } from '@/executor/execution/snapshot'
4854
import type { ExecutionMetadata, SerializableExecutionState } from '@/executor/execution/types'
4955
import type { BlockLog, NormalizedBlockOutput } from '@/executor/types'
@@ -1011,6 +1017,74 @@ function resolveOutputPath(value: unknown, path: string[]): unknown {
10111017
return current
10121018
}
10131019

1020+
type ChildOutputSelector = Required<ParsedOutputSelector>
1021+
1022+
function parseChildSelector(resolvedId: string): ChildOutputSelector | null {
1023+
try {
1024+
const parsed = parseInternalOutputSelector(resolvedId)
1025+
return parsed.workflowId ? { ...parsed, workflowId: parsed.workflowId } : null
1026+
} catch {
1027+
return null
1028+
}
1029+
}
1030+
1031+
function asTraceSpans(value: unknown): TraceSpan[] {
1032+
return Array.isArray(value) ? value : []
1033+
}
1034+
1035+
/**
1036+
* Walks child-workflow trace spans for the last output of the selected child block. Spans of
1037+
* the selected child workflow are matched by block ID or normalized block name; spans of any
1038+
* other nested workflow are searched only for a deeper workflow with the selected ID.
1039+
*/
1040+
function findChildSpanOutput(
1041+
spans: TraceSpan[],
1042+
selector: ChildOutputSelector,
1043+
inSelectedWorkflow: boolean
1044+
): Record<string, unknown> | undefined {
1045+
let found: Record<string, unknown> | undefined
1046+
for (const span of spans) {
1047+
if (
1048+
inSelectedWorkflow &&
1049+
span.output !== undefined &&
1050+
(span.blockId === selector.blockId || normalizeName(span.name) === selector.blockId)
1051+
) {
1052+
found = span.output
1053+
}
1054+
const children = asTraceSpans(span.children)
1055+
if (children.length === 0) continue
1056+
const childScope = span.childWorkflowId
1057+
? span.childWorkflowId === selector.workflowId
1058+
: inSelectedWorkflow
1059+
const nested = findChildSpanOutput(children, selector, childScope)
1060+
if (nested !== undefined) found = nested
1061+
}
1062+
return found
1063+
}
1064+
1065+
/**
1066+
* Reads a child workflow block's output from the trace spans its workflow block carried back.
1067+
* Custom blocks carry no child workflow ID or spans into the parent log, so their children are
1068+
* never readable here.
1069+
*/
1070+
function findChildBlockOutput(
1071+
logs: BlockLog[],
1072+
selector: ChildOutputSelector
1073+
): Record<string, unknown> | undefined {
1074+
let found: Record<string, unknown> | undefined
1075+
for (const log of logs) {
1076+
const spans = asTraceSpans(log.childTraceSpans ?? log.output?.childTraceSpans)
1077+
if (spans.length === 0) continue
1078+
const nested = findChildSpanOutput(
1079+
spans,
1080+
selector,
1081+
log.output?.childWorkflowId === selector.workflowId
1082+
)
1083+
if (nested !== undefined) found = nested
1084+
}
1085+
return found
1086+
}
1087+
10141088
/**
10151089
* Projects `selectedOutputs` onto a finished run's block logs, so a sync run
10161090
* answers with the named blocks' outputs in the same response — no second call
@@ -1020,7 +1094,8 @@ function resolveOutputPath(value: unknown, path: string[]): unknown {
10201094
* ran, resolved to no block, or whose path is absent is omitted — the same
10211095
* missing-fields-are-omitted contract the streamed and finished-run selections
10221096
* follow. The last log per block wins, so a block inside a loop reports its
1023-
* final iteration's output.
1097+
* final iteration's output. A child-workflow selector reads the child block's
1098+
* output from the trace spans its workflow block returned, last invocation winning.
10241099
*/
10251100
export async function pickRunBlockOutputs(
10261101
selectedOutputs: string[] | undefined,
@@ -1041,6 +1116,17 @@ export async function pickRunBlockOutputs(
10411116
const resolvedId = resolved[i]
10421117
if (!selector || !resolvedId) continue
10431118

1119+
const childSelector = parseChildSelector(resolvedId)
1120+
if (childSelector) {
1121+
const childOutput = findChildBlockOutput(logs ?? [], childSelector)
1122+
const value =
1123+
childOutput === undefined
1124+
? undefined
1125+
: resolveOutputPath(childOutput, childSelector.path.split('.'))
1126+
if (value !== undefined) picked[selector] = value
1127+
continue
1128+
}
1129+
10441130
let blockId: string
10451131
let path: string[]
10461132
if (isValidUuid(resolvedId)) {

0 commit comments

Comments
 (0)