From 461ae6b98825b40b0961a5fae1104335003663cc Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 21:35:32 -0800 Subject: [PATCH 1/2] fix(executor): selected outputs no longer fail runs that call a child workflow A sync run handed the caller's public `block.path` selectors to the executor instead of the internal form it had already resolved, so the child-workflow block parsed `done.data` as workflow `done`, block `data` and failed the run after it executed and billed. The v2 execute service and the v1 sync path now pass resolved selectors (v1 drops ones naming no block, as it always ignored them), and the child-workflow block drops a selector it cannot apply with a warning rather than failing the block. --- .../app/api/workflows/[id]/execute/route.ts | 21 +- .../sim/executor/handlers/workflow/handler.ts | 22 +- ...e-workflow-selected-outputs.integration.ts | 237 ++++++++++++++++++ .../lib/workflows/executor/execute-service.ts | 2 +- .../streaming/output-selector.test.ts | 15 ++ .../workflows/streaming/output-selector.ts | 35 ++- .../streaming/resolve-output-selectors.ts | 55 +++- 7 files changed, 364 insertions(+), 23 deletions(-) create mode 100644 apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts diff --git a/apps/sim/app/api/workflows/[id]/execute/route.ts b/apps/sim/app/api/workflows/[id]/execute/route.ts index 195fc27607d..23e541c29df 100644 --- a/apps/sim/app/api/workflows/[id]/execute/route.ts +++ b/apps/sim/app/api/workflows/[id]/execute/route.ts @@ -151,7 +151,10 @@ import { forwardAgentStreamToExecutionEvents, shouldForwardAnswerTextFromSink, } from '@/lib/workflows/streaming/forward-agent-stream-events' -import { resolveOutputSelectors } from '@/lib/workflows/streaming/resolve-output-selectors' +import { + resolveOutputSelectors, + resolveOutputSelectorsLeniently, +} from '@/lib/workflows/streaming/resolve-output-selectors' import { agentStreamProtocolResponseHeaders, createStreamingResponse, @@ -1460,6 +1463,20 @@ async function handleExecutePost( } const executionVariables = cachedWorkflowData?.variables ?? workflow.variables ?? {} + /** + * The executor reads only the internal selector form; a public `block.path` reaching + * a child-workflow block would read as `workflow.block`. The sync response has never + * refused a stale selector, so one that names no block is dropped, not answered with 400. + */ + const syncSelection = resolveOutputSelectorsLeniently({ + selectedOutputs, + currentBlocks: effectiveWorkflowStateOverride?.blocks ?? {}, + }) + if (syncSelection.rejected.length > 0) { + reqLogger.warn('Ignoring selected outputs that name no block', { + rejected: syncSelection.rejected, + }) + } const timeoutController = createTimeoutAbortController(getEffectiveSyncTimeoutMs()) const didExecutionTimeOut = (error?: unknown) => @@ -1484,7 +1501,7 @@ async function handleExecutePost( workflow, processedInput, executionVariables, - selectedOutputs + syncSelection.selectedOutputs ) const result = await executeWorkflowCore({ diff --git a/apps/sim/executor/handlers/workflow/handler.ts b/apps/sim/executor/handlers/workflow/handler.ts index bb357961de4..770e5fedc5d 100644 --- a/apps/sim/executor/handlers/workflow/handler.ts +++ b/apps/sim/executor/handlers/workflow/handler.ts @@ -506,15 +506,23 @@ export class WorkflowBlockHandler implements BlockHandler { childWorkflow.rawBlocks || {}, ctx.selectedOutputs ) - if (isCustomBlock && childOutputSelection.targetsChildWorkflow) { - throw new Error('Custom block child outputs cannot be selected for streaming') + if (childOutputSelection.ignored.length > 0) { + logger.warn('Ignoring output selectors the child workflow cannot serve', { + childWorkflowId: workflowId, + ignored: childOutputSelection.ignored, + }) } - if (!withinSseChildDepth && childOutputSelection.targetsChildWorkflow) { - throw new Error( - `Selected stream output exceeds the maximum child workflow depth of ${DEFAULTS.MAX_SSE_CHILD_DEPTH}` - ) + if (childOutputSelection.targetsChildWorkflow && (isCustomBlock || !withinSseChildDepth)) { + logger.warn('Ignoring child output selection the stream cannot carry', { + childWorkflowId: workflowId, + reason: isCustomBlock + ? 'custom block child outputs cannot be selected for streaming' + : 'selected output exceeds the maximum child workflow depth', + maxDepth: DEFAULTS.MAX_SSE_CHILD_DEPTH, + }) } - const childSelectedOutputs = isCustomBlock ? [] : childOutputSelection.selectedOutputs + const childSelectedOutputs = + isCustomBlock || !withinSseChildDepth ? [] : childOutputSelection.selectedOutputs const shouldStreamChild = shouldPropagateCallbacks && Boolean(ctx.stream) && childSelectedOutputs.length > 0 diff --git a/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts b/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts new file mode 100644 index 00000000000..df22965a32c --- /dev/null +++ b/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts @@ -0,0 +1,237 @@ +/** + * `selectedOutputs` on a deployed run of a workflow that calls a child workflow, against real + * PostgreSQL through the execute use case. A selector naming a parent block must reach the + * executor in its internal form, so the child-workflow block never mistakes `block.path` for a + * `workflow.block` selector; a selector that targets the child but names nothing there must not + * fail a run that otherwise succeeded; and a selector naming no parent block is refused before + * the run starts, so it is never executed or billed. + */ +import { db } from '@sim/db' +import { + permissions, + user, + workflow, + workflowDeploymentVersion, + workflowExecutionLogs, + workflowExecutionSnapshots, + workspace, +} from '@sim/db/schema' +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' +import { createSessionPrincipal } from '@sim/testing/factories/principal.factory' +import { generateId } from '@sim/utils/id' +import { eq, inArray } from 'drizzle-orm' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' +import { executeWorkflowOperation } from '@/lib/workflows/application/execute-workflow' + +const ownerId = generateId() +const workspaceId = generateId() +const parentWorkflowId = generateId() +const childWorkflowId = generateId() +const principal = createSessionPrincipal({ userId: ownerId, sessionId: generateId() }) +/** Deployed block IDs are UUIDs; `blockOutputs` projects only blocks with one. */ +const blockIds = { + start: generateId(), + ask: generateId(), + done: generateId(), + childDone: generateId(), +} + +const startBlock = { + id: blockIds.start, + type: 'start_trigger', + name: 'Start', + enabled: true, + position: { x: 0, y: 0 }, + outputs: {}, + subBlocks: {}, +} + +function responseBlock(id: string, name: string, data: string) { + return { + id, + type: 'response', + name, + enabled: true, + position: { x: 600, y: 0 }, + outputs: {}, + subBlocks: { + dataMode: { id: 'dataMode', type: 'dropdown', value: 'json' }, + data: { id: 'data', type: 'code', value: data }, + status: { id: 'status', type: 'short-input', value: '200' }, + }, + } +} + +const childState = { + blocks: { + [blockIds.start]: startBlock, + [blockIds.childDone]: responseBlock( + blockIds.childDone, + 'Child Done', + '{"answer": "from child"}' + ), + }, + edges: [{ id: generateId(), source: blockIds.start, target: blockIds.childDone }], + loops: {}, + parallels: {}, + variables: {}, +} + +const parentState = { + blocks: { + [blockIds.start]: startBlock, + [blockIds.ask]: { + id: blockIds.ask, + type: 'workflow_input', + name: 'Ask', + enabled: true, + position: { x: 300, y: 0 }, + outputs: {}, + subBlocks: { + workflowId: { id: 'workflowId', type: 'workflow-selector', value: childWorkflowId }, + inputMapping: { id: 'inputMapping', type: 'input-mapping', value: {} }, + }, + }, + [blockIds.done]: responseBlock(blockIds.done, 'Done', '{"answer": ""}'), + }, + edges: [ + { id: generateId(), source: blockIds.start, target: blockIds.ask }, + { id: generateId(), source: blockIds.ask, target: blockIds.done }, + ], + loops: {}, + parallels: {}, + variables: {}, +} + +function run(selectedOutputs: string[]) { + return executeWorkflowOperation.execute({ + principal, + input: { + workflowId: parentWorkflowId, + requestId: generateId(), + input: {}, + mode: 'sync', + selectedOutputs, + requestHeaders: new Headers(), + }, + }) +} + +async function parentLogCount(): Promise { + const rows = await db + .select({ id: workflowExecutionLogs.id }) + .from(workflowExecutionLogs) + .where(eq(workflowExecutionLogs.workflowId, parentWorkflowId)) + return rows.length +} + +beforeAll(async () => { + const now = new Date() + await db.insert(user).values({ + id: ownerId, + name: 'Selected outputs fixture', + email: `${ownerId}@selected-outputs.test`, + emailVerified: true, + createdAt: now, + updatedAt: now, + }) + await insertWorkspaceFixture(db, { + id: workspaceId, + name: 'Selected outputs fixture', + ownerId, + billedAccountUserId: ownerId, + }) + await db.insert(permissions).values({ + id: generateId(), + userId: ownerId, + entityType: 'workspace', + entityId: workspaceId, + permissionType: 'admin', + }) + await db.insert(workflow).values( + [ + { id: parentWorkflowId, name: 'Parent' }, + { id: childWorkflowId, name: 'Child' }, + ].map(({ id, name }) => ({ + id, + userId: ownerId, + workspaceId, + name, + isDeployed: true, + deployedAt: now, + lastSynced: now, + createdAt: now, + updatedAt: now, + })) + ) + await db.insert(workflowDeploymentVersion).values([ + { + id: generateId(), + workflowId: parentWorkflowId, + version: 1, + state: parentState, + isActive: true, + }, + { + id: generateId(), + workflowId: childWorkflowId, + version: 1, + state: childState, + isActive: true, + }, + ]) +}) + +afterAll(async () => { + const workflowIds = [parentWorkflowId, childWorkflowId] + await db + .delete(workflowExecutionLogs) + .where(inArray(workflowExecutionLogs.workflowId, workflowIds)) + await db + .delete(workflowExecutionSnapshots) + .where(inArray(workflowExecutionSnapshots.workflowId, workflowIds)) + await deleteWorkspaceFixture(db, eq(workspace.id, workspaceId)) + await db.delete(user).where(eq(user.id, ownerId)) + await db.$client.end() +}) + +describe('selectedOutputs on a run that calls a child workflow', () => { + it('completes and returns a parent block output selected by name and path', async () => { + const result = await run(['done.data']) + + expect(result).toMatchObject({ + ok: true, + status: 'completed', + blockOutputs: { 'done.data': { answer: 'from child' } }, + }) + }) + + it('completes when a selector targets the child workflow', async () => { + const result = await run([`${childWorkflowId}.childdone.data`, 'done.data']) + + expect(result).toMatchObject({ + ok: true, + status: 'completed', + blockOutputs: { 'done.data': { answer: 'from child' } }, + }) + }) + + it('completes when a child-targeted selector names no block in the child', async () => { + const result = await run([`${childWorkflowId}.missing.data`, 'done.data']) + + expect(result).toMatchObject({ + ok: true, + status: 'completed', + blockOutputs: { 'done.data': { answer: 'from child' } }, + }) + }) + + it('refuses a selector naming no parent block before the run starts, leaving no run to bill', async () => { + const before = await parentLogCount() + + const result = await run(['nosuchblock.data']) + + expect(result).toMatchObject({ ok: false, failure: { kind: 'input', statusCode: 400 } }) + expect(await parentLogCount()).toBe(before) + }) +}) diff --git a/apps/sim/lib/workflows/executor/execute-service.ts b/apps/sim/lib/workflows/executor/execute-service.ts index 92c394a4ac3..fb462e6ac71 100644 --- a/apps/sim/lib/workflows/executor/execute-service.ts +++ b/apps/sim/lib/workflows/executor/execute-service.ts @@ -743,7 +743,7 @@ export async function executeWorkflowService( workflow, processedInput, workflowVariables, - selectedOutputs + resolvedSelectedOutputs ) const result = await executeWorkflowCore({ diff --git a/apps/sim/lib/workflows/streaming/output-selector.test.ts b/apps/sim/lib/workflows/streaming/output-selector.test.ts index b4e07d4916f..8de3930729e 100644 --- a/apps/sim/lib/workflows/streaming/output-selector.test.ts +++ b/apps/sim/lib/workflows/streaming/output-selector.test.ts @@ -104,6 +104,21 @@ describe('output selector scoping', () => { expect(selection.targetsChildWorkflow).toBe(true) }) + it('drops a selector it cannot apply instead of failing the child block', () => { + const selection = selectChildOutputSelectors( + CHILD_WORKFLOW_ID, + { [CHILD_BLOCK_ID]: block(CHILD_BLOCK_ID, 'Writer') }, + ['done.data', formatInternalOutputSelector('missing', 'content', CHILD_WORKFLOW_ID)] + ) + + expect(selection.selectedOutputs).toEqual([]) + expect(selection.targetsChildWorkflow).toBe(false) + expect(selection.ignored.map((entry) => entry.selector)).toEqual([ + 'done.data', + formatInternalOutputSelector('missing', 'content', CHILD_WORKFLOW_ID), + ]) + }) + it.each([ '', ' workflow.agent_content', diff --git a/apps/sim/lib/workflows/streaming/output-selector.ts b/apps/sim/lib/workflows/streaming/output-selector.ts index ac74dd9bf9b..e7741f3e30c 100644 --- a/apps/sim/lib/workflows/streaming/output-selector.ts +++ b/apps/sim/lib/workflows/streaming/output-selector.ts @@ -1,3 +1,4 @@ +import { getErrorMessage } from '@sim/utils/errors' import { isValidUuid } from '@sim/utils/id' import type { BlockState } from '@sim/workflow-types/workflow' import { normalizeName } from '@/executor/constants' @@ -26,6 +27,12 @@ export interface ChildOutputSelection { /** Actual child block ID to the caller-supplied ref used in the scoped selector. */ selectedBlockRefs: ReadonlyMap targetsChildWorkflow: boolean + /** + * Selectors this child neither serves nor forwards, with why. A selection only shapes what + * the run reports, so a selector that cannot apply here is dropped, never a reason to fail + * the child block. + */ + ignored: ReadonlyArray<{ selector: string; reason: string }> } function assertValidSelectorPart(value: string, label: string): void { @@ -235,7 +242,14 @@ export function resolveOutputBlockRef( return matches[0].id } -/** Routes workflow-scoped selections into a child executor. */ +/** + * Routes workflow-scoped selections into a child executor. Selectors arrive in the internal + * form; one that names a block of the current workflow is skipped, one scoped to a deeper + * workflow is forwarded unchanged, and one scoped to this child is rewritten to the child's + * block ID. A selector that does not parse, or names no block of this child, is reported in + * `ignored` rather than thrown: the run has already been admitted and selection is + * presentation. + */ export function selectChildOutputSelectors( childWorkflowId: string, childBlocks: Record, @@ -244,21 +258,34 @@ export function selectChildOutputSelectors( assertValidSelectorPart(childWorkflowId, 'workflow ID') const childSelectors: string[] = [] const selectedBlockRefs = new Map() + const ignored: { selector: string; reason: string }[] = [] let targetsChildWorkflow = false for (const selector of selectedOutputs ?? []) { - const parsed = parseInternalOutputSelector(selector) + let parsed: ParsedOutputSelector + try { + parsed = parseInternalOutputSelector(selector) + } catch (error) { + ignored.push({ selector, reason: getErrorMessage(error) }) + continue + } if (!parsed.workflowId) continue if (parsed.workflowId !== childWorkflowId) { childSelectors.push(selector) continue } + let childBlockId: string + try { + childBlockId = resolveOutputBlockRef(parsed.blockId, childBlocks) + } catch (error) { + ignored.push({ selector, reason: getErrorMessage(error) }) + continue + } targetsChildWorkflow = true - const childBlockId = resolveOutputBlockRef(parsed.blockId, childBlocks) selectedBlockRefs.set(childBlockId, parsed.blockId) childSelectors.push(formatInternalOutputSelector(childBlockId, parsed.path)) } - return { selectedOutputs: childSelectors, selectedBlockRefs, targetsChildWorkflow } + return { selectedOutputs: childSelectors, selectedBlockRefs, targetsChildWorkflow, ignored } } diff --git a/apps/sim/lib/workflows/streaming/resolve-output-selectors.ts b/apps/sim/lib/workflows/streaming/resolve-output-selectors.ts index a59d6cad7b6..6da0077d7e3 100644 --- a/apps/sim/lib/workflows/streaming/resolve-output-selectors.ts +++ b/apps/sim/lib/workflows/streaming/resolve-output-selectors.ts @@ -1,3 +1,4 @@ +import { getErrorMessage } from '@sim/utils/errors' import type { BlockState } from '@sim/workflow-types/workflow' import { getWorkflowInvocationTarget } from '@/lib/workflows/streaming/nested-output-options' import { @@ -42,13 +43,9 @@ function resolveCurrentBlockId( } } -/** Resolves current-workflow names and leaves child names for its authorized loader. */ -export function resolveOutputSelectors({ - selectedOutputs, - currentBlocks, -}: ResolveOutputSelectorsOptions): string[] | undefined { - if (!selectedOutputs || selectedOutputs.length === 0) return selectedOutputs?.slice() - +function createSelectorResolver( + currentBlocks: Record +): (selector: string) => string { const currentBlockRefs = new Set() const childWorkflowIds = new Set() for (const block of Object.values(currentBlocks)) { @@ -58,11 +55,51 @@ export function resolveOutputSelectors({ if (childWorkflowId) childWorkflowIds.add(childWorkflowId) } - return selectedOutputs.map((selector) => { + return (selector) => { const parsed = parseStoredOutputSelector(selector, { currentBlockRefs, childWorkflowIds }) const blockId = parsed.workflowId ? parsed.blockId : resolveCurrentBlockId(parsed.blockId, selector, currentBlocks) return formatInternalOutputSelector(blockId, parsed.path, parsed.workflowId) - }) + } +} + +/** + * Resolves current-workflow names and leaves child names for its authorized loader. Throws on + * the first selector that names no block, so a caller can refuse the request before running. + */ +export function resolveOutputSelectors({ + selectedOutputs, + currentBlocks, +}: ResolveOutputSelectorsOptions): string[] | undefined { + if (!selectedOutputs || selectedOutputs.length === 0) return selectedOutputs?.slice() + return selectedOutputs.map(createSelectorResolver(currentBlocks)) +} + +export interface LenientOutputSelectorResolution { + /** Resolved internal selectors, in caller order, without the rejected ones. */ + selectedOutputs: string[] + rejected: Array<{ selector: string; reason: string }> +} + +/** + * Resolves like {@link resolveOutputSelectors} but drops a selector that names no block + * instead of throwing. For surfaces whose contract has always tolerated a stale selector, + * where refusing it now would break callers that worked before. + */ +export function resolveOutputSelectorsLeniently({ + selectedOutputs, + currentBlocks, +}: ResolveOutputSelectorsOptions): LenientOutputSelectorResolution { + const resolution: LenientOutputSelectorResolution = { selectedOutputs: [], rejected: [] } + if (!selectedOutputs || selectedOutputs.length === 0) return resolution + const resolve = createSelectorResolver(currentBlocks) + for (const selector of selectedOutputs) { + try { + resolution.selectedOutputs.push(resolve(selector)) + } catch (error) { + resolution.rejected.push({ selector, reason: getErrorMessage(error) }) + } + } + return resolution } From 48dd9b92185cfbce6f9e1c5afbc28cdbf868cb2d Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:31:14 -0800 Subject: [PATCH 2/2] 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. --- ...e-workflow-selected-outputs.integration.ts | 24 ++++- .../lib/workflows/executor/execute-service.ts | 88 ++++++++++++++++++- 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts b/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts index df22965a32c..602bb458739 100644 --- a/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts +++ b/apps/sim/lib/workflows/application/execute-workflow-selected-outputs.integration.ts @@ -2,7 +2,8 @@ * `selectedOutputs` on a deployed run of a workflow that calls a child workflow, against real * PostgreSQL through the execute use case. A selector naming a parent block must reach the * executor in its internal form, so the child-workflow block never mistakes `block.path` for a - * `workflow.block` selector; a selector that targets the child but names nothing there must not + * `workflow.block` selector; a selector that targets a child block returns that block's output + * under the caller's selector; one that targets the child but names nothing there must not * fail a run that otherwise succeeded; and a selector naming no parent block is refused before * the run starts, so it is never executed or billed. */ @@ -206,13 +207,28 @@ describe('selectedOutputs on a run that calls a child workflow', () => { }) }) - it('completes when a selector targets the child workflow', async () => { - const result = await run([`${childWorkflowId}.childdone.data`, 'done.data']) + it('returns a child block output selected by child workflow, block name, and path', async () => { + const childSelector = `${childWorkflowId}.childdone.data` + const result = await run([childSelector, 'done.data']) expect(result).toMatchObject({ ok: true, status: 'completed', - blockOutputs: { 'done.data': { answer: 'from child' } }, + blockOutputs: { + [childSelector]: { answer: 'from child' }, + 'done.data': { answer: 'from child' }, + }, + }) + }) + + it('returns a child block output selected by child block ID', async () => { + const childSelector = `${childWorkflowId}.${blockIds.childDone}.data.answer` + const result = await run([childSelector]) + + expect(result).toMatchObject({ + ok: true, + status: 'completed', + blockOutputs: { [childSelector]: 'from child' }, }) }) diff --git a/apps/sim/lib/workflows/executor/execute-service.ts b/apps/sim/lib/workflows/executor/execute-service.ts index fb462e6ac71..3f6e9ebc697 100644 --- a/apps/sim/lib/workflows/executor/execute-service.ts +++ b/apps/sim/lib/workflows/executor/execute-service.ts @@ -19,6 +19,7 @@ import { containsLargeValueRef } from '@/lib/execution/payloads/large-value-ref' import { compactExecutionPayload } from '@/lib/execution/payloads/serializer' import { preprocessExecution } from '@/lib/execution/preprocessing' import { LoggingSession } from '@/lib/logs/execution/logging-session' +import type { TraceSpan } from '@/lib/logs/types' import { MAX_MCP_WORKFLOW_RESPONSE_BYTES } from '@/lib/mcp/constants' import { hydrateUserFilesWithBase64 } from '@/lib/uploads/utils/user-file-base64.server' import { enqueueWorkflowExecution } from '@/lib/workflows/executor/enqueue-execution' @@ -38,12 +39,17 @@ import { type NormalizedWorkflowData, } from '@/lib/workflows/persistence/utils' import { shouldEmitAgentStreamEvents } from '@/lib/workflows/streaming/agent-stream-protocol' +import { + type ParsedOutputSelector, + parseInternalOutputSelector, +} from '@/lib/workflows/streaming/output-selector' import { resolveOutputSelectors } from '@/lib/workflows/streaming/resolve-output-selectors' import { agentStreamProtocolResponseHeaders, createStreamingResponse, } from '@/lib/workflows/streaming/streaming' import { workflowHasResponseBlock } from '@/lib/workflows/utils' +import { normalizeName } from '@/executor/constants' import { ExecutionSnapshot } from '@/executor/execution/snapshot' import type { ExecutionMetadata, SerializableExecutionState } from '@/executor/execution/types' import type { BlockLog, NormalizedBlockOutput } from '@/executor/types' @@ -1011,6 +1017,74 @@ function resolveOutputPath(value: unknown, path: string[]): unknown { return current } +type ChildOutputSelector = Required + +function parseChildSelector(resolvedId: string): ChildOutputSelector | null { + try { + const parsed = parseInternalOutputSelector(resolvedId) + return parsed.workflowId ? { ...parsed, workflowId: parsed.workflowId } : null + } catch { + return null + } +} + +function asTraceSpans(value: unknown): TraceSpan[] { + return Array.isArray(value) ? value : [] +} + +/** + * Walks child-workflow trace spans for the last output of the selected child block. Spans of + * the selected child workflow are matched by block ID or normalized block name; spans of any + * other nested workflow are searched only for a deeper workflow with the selected ID. + */ +function findChildSpanOutput( + spans: TraceSpan[], + selector: ChildOutputSelector, + inSelectedWorkflow: boolean +): Record | undefined { + let found: Record | undefined + for (const span of spans) { + if ( + inSelectedWorkflow && + span.output !== undefined && + (span.blockId === selector.blockId || normalizeName(span.name) === selector.blockId) + ) { + found = span.output + } + const children = asTraceSpans(span.children) + if (children.length === 0) continue + const childScope = span.childWorkflowId + ? span.childWorkflowId === selector.workflowId + : inSelectedWorkflow + const nested = findChildSpanOutput(children, selector, childScope) + if (nested !== undefined) found = nested + } + return found +} + +/** + * Reads a child workflow block's output from the trace spans its workflow block carried back. + * Custom blocks carry no child workflow ID or spans into the parent log, so their children are + * never readable here. + */ +function findChildBlockOutput( + logs: BlockLog[], + selector: ChildOutputSelector +): Record | undefined { + let found: Record | undefined + for (const log of logs) { + const spans = asTraceSpans(log.childTraceSpans ?? log.output?.childTraceSpans) + if (spans.length === 0) continue + const nested = findChildSpanOutput( + spans, + selector, + log.output?.childWorkflowId === selector.workflowId + ) + if (nested !== undefined) found = nested + } + return found +} + /** * Projects `selectedOutputs` onto a finished run's block logs, so a sync run * answers with the named blocks' outputs in the same response — no second call @@ -1020,7 +1094,8 @@ function resolveOutputPath(value: unknown, path: string[]): unknown { * ran, resolved to no block, or whose path is absent is omitted — the same * missing-fields-are-omitted contract the streamed and finished-run selections * follow. The last log per block wins, so a block inside a loop reports its - * final iteration's output. + * final iteration's output. A child-workflow selector reads the child block's + * output from the trace spans its workflow block returned, last invocation winning. */ export async function pickRunBlockOutputs( selectedOutputs: string[] | undefined, @@ -1041,6 +1116,17 @@ export async function pickRunBlockOutputs( const resolvedId = resolved[i] if (!selector || !resolvedId) continue + const childSelector = parseChildSelector(resolvedId) + if (childSelector) { + const childOutput = findChildBlockOutput(logs ?? [], childSelector) + const value = + childOutput === undefined + ? undefined + : resolveOutputPath(childOutput, childSelector.path.split('.')) + if (value !== undefined) picked[selector] = value + continue + } + let blockId: string let path: string[] if (isValidUuid(resolvedId)) {