Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/docs/openapi-v2-logs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,10 @@
"type": "string",
"description": "Workflow block associated with the span."
},
"childDeploymentVersionId": {
"type": "string",
"description": "Deployment version of the child workflow this call ran, on a workflow block span or a workflow Agent tool span. Match it to the child workflow's versions list. Absent for draft children, custom blocks, failed Agent tool calls, and runs recorded before this field existed."
},
"input": {
"description": "Input captured for the traced operation."
},
Expand Down
4 changes: 4 additions & 0 deletions apps/sim/executor/errors/child-workflow-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface ChildWorkflowErrorOptions {
childTraceSpans?: TraceSpan[]
executionResult?: ExecutionResult
childWorkflowSnapshotId?: string
/** The deployment version a deployed workflow-block child ran; unset for draft and custom-block children. */
childDeploymentVersionId?: string
childWorkflowInstanceId?: string
/** Child workflow names from this invocation down to the failing run, outermost first. */
workflowChain?: string[]
Expand Down Expand Up @@ -38,6 +40,7 @@ export class ChildWorkflowError extends Error {
readonly childWorkflowName: string
readonly executionResult?: ExecutionResult
readonly childWorkflowSnapshotId?: string
readonly childDeploymentVersionId?: string
/** Per-invocation unique ID used to correlate child block events with this workflow block. */
readonly childWorkflowInstanceId?: string
/**
Expand All @@ -59,6 +62,7 @@ export class ChildWorkflowError extends Error {
this.childTraceSpans = options.childTraceSpans ?? []
this.executionResult = options.executionResult
this.childWorkflowSnapshotId = options.childWorkflowSnapshotId
this.childDeploymentVersionId = options.childDeploymentVersionId
this.childWorkflowInstanceId = options.childWorkflowInstanceId
this.workflowChain = options.workflowChain ?? [options.childWorkflowName]
this.rootErrorMessage = options.rootErrorMessage ?? options.message
Expand Down
38 changes: 38 additions & 0 deletions apps/sim/executor/execution/block-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,44 @@ describe('BlockExecutor', () => {
)
})

it.each([
[BlockType.WORKFLOW_INPUT, 'dv-7'],
['custom_block_abc', undefined],
])(
'records the child deployment version on a failed %s block only when it is a workflow block',
async (blockType, expected) => {
const block = { ...createBlock(), metadata: { id: blockType, name: 'Child' } }
const state = new ExecutionState()
const workflow: SerializedWorkflow = {
version: '1',
blocks: [block],
connections: [],
loops: {},
parallels: {},
}
const handler: BlockHandler = {
canHandle: () => true,
execute: async () => {
throw new ChildWorkflowError({
message: '"Child" failed: boom',
childWorkflowName: 'Child',
childDeploymentVersionId: 'dv-7',
})
},
}
const executor = new BlockExecutor(
[handler],
new VariableResolver(workflow, {}, state),
{},
state
)

await executor.execute(createContext(state), createNode(block), block).catch(() => undefined)

expect(state.getBlockOutput(block.id)?.childDeploymentVersionId).toBe(expected)
}
)

it('does not soft-succeed non-agent blocks on user AbortError', async () => {
const block = createBlock()
const workflow: SerializedWorkflow = {
Expand Down
3 changes: 3 additions & 0 deletions apps/sim/executor/execution/block-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,9 @@ export class BlockExecutor {
if (error.childWorkflowSnapshotId) {
errorOutput.childWorkflowSnapshotId = error.childWorkflowSnapshotId
}
if (error.childDeploymentVersionId) {
errorOutput.childDeploymentVersionId = error.childDeploymentVersionId
}
}
// A custom block's consumer gets a machine-readable failure class and an
// opaque handle to the failed run — enough to branch on and to quote in a
Expand Down
28 changes: 28 additions & 0 deletions apps/sim/executor/handlers/agent/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3540,6 +3540,34 @@ describe('AgentBlockHandler', () => {
})
})

it('keeps a custom tool titled like the workflow tool from claiming a child version', async () => {
mockExecuteProviderRequest.mockResolvedValueOnce({
content: 'done',
model: 'mock-model',
tokens: { input: 10, output: 20, total: 30 },
toolCalls: [
{ name: 'custom_workflow_executor', result: { childDeploymentVersionId: 'forged' } },
{ name: 'workflow_executor', result: { childDeploymentVersionId: 'dv-7' } },
{ name: 'custom_lookup', result: { childDeploymentVersionId: 'user-data' } },
],
cost: 0.001,
timing: { total: 100 },
})

const result = await handler.execute(mockContext, mockBlock, {
model: 'gpt-4o',
userPrompt: 'Score it',
apiKey: 'test-api-key',
})

const [custom, workflow, otherCustom] = (
result as { toolCalls: { list: Array<{ result: Record<string, unknown> }> } }
).toolCalls.list
expect(custom.result).not.toHaveProperty('childDeploymentVersionId')
expect(workflow.result.childDeploymentVersionId).toBe('dv-7')
expect(otherCustom.result.childDeploymentVersionId).toBe('user-data')
})

it('uses a secret-backed skillId for lookup without carrying it into output provenance', async () => {
const skillId = 'x'
mockContext.workspaceId = 'workspace-1'
Expand Down
16 changes: 14 additions & 2 deletions apps/sim/executor/handlers/agent/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ import {
validateBlockType,
validateModelProvider,
} from '@/ee/access-control/utils/permission-check'
import { AGENT, BlockType, DEFAULTS, stripCustomToolPrefix } from '@/executor/constants'
import {
AGENT,
BlockType,
DEFAULTS,
isCustomTool,
stripCustomToolPrefix,
} from '@/executor/constants'
import {
getMemoryMessageAppendKey,
getMemoryMessageTurnId,
Expand Down Expand Up @@ -3227,6 +3233,7 @@ export class AgentBlockHandler implements BlockHandler {

private formatToolCall(tc: any) {
const toolName = stripCustomToolPrefix(tc.name)
const result = tc.result || tc.output

return {
...tc,
Expand All @@ -3235,7 +3242,12 @@ export class AgentBlockHandler implements BlockHandler {
endTime: tc.endTime,
duration: tc.duration,
arguments: tc.arguments || tc.input || {},
result: tc.result || tc.output,
// The span factory trusts this key only on a `workflow_executor` call. Stripping the prefix
// would let a custom tool titled `workflow_executor` claim a child version it never ran.
result:
isCustomTool(tc.name) && toolName === 'workflow_executor' && isRecordLike(result)
? omit(result, ['childDeploymentVersionId'])
: result,
}
}
}
80 changes: 80 additions & 0 deletions apps/sim/executor/handlers/workflow/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,86 @@ describe('WorkflowBlockHandler', () => {
})
})

describe('child deployment version', () => {
const inputs = { workflowId: 'child-workflow-id' }
const deployedCtx = () =>
({
...mockContext,
workspaceId: 'workspace-parent',
isDeployedContext: true,
}) as unknown as ExecutionContext

beforeEach(() => {
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
workspaceId: 'workspace-parent',
state: {
blocks: {},
edges: [],
loops: {},
parallels: {},
deploymentVersionId: 'dv-7',
},
})
mockResolveSnapshot.mockResolvedValue({ id: 'snapshot-1', workflowId: 'w', stateHash: 'h' })
})

it('records the version a deployed child ran', async () => {
mockExecutorExecute.mockResolvedValue({ success: true, output: { data: 'ok' } })

const output = await handler.execute(deployedCtx(), mockBlock, inputs)

expect(output).toMatchObject({ childDeploymentVersionId: 'dv-7' })
})

it('records the version on a deployed child that reports a failure', async () => {
mockExecutorExecute.mockResolvedValue({ success: false, error: 'boom', output: {} })

const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)

expect(failure).toMatchObject({ childDeploymentVersionId: 'dv-7' })
})

it('records the version on a deployed child that throws', async () => {
mockExecutorExecute.mockRejectedValue(new Error('engine blew up'))

const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)

expect(failure).toMatchObject({ childDeploymentVersionId: 'dv-7' })
})

it('records no version for a deployed child refused by the workspace check', async () => {
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
workspaceId: 'workspace-other',
state: { blocks: {}, edges: [], loops: {}, parallels: {}, deploymentVersionId: 'dv-7' },
})

const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)

expect(failure.message).toContain('belongs to a different workspace')
expect(failure).not.toHaveProperty('childDeploymentVersionId', 'dv-7')
})

it('records no version for a draft child', async () => {
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
workspaceId: 'workspace-parent',
state: { blocks: {}, edges: [], loops: {}, parallels: {} },
})
mockExecutorExecute.mockResolvedValue({ success: true, output: { data: 'ok' } })

const output = await handler.execute(
{ ...mockContext, workspaceId: 'workspace-parent' } as unknown as ExecutionContext,
mockBlock,
inputs
)

expect(output).toMatchObject({ childWorkflowId: 'child-workflow-id' })
expect(output).not.toHaveProperty('childDeploymentVersionId')
})
})

describe('workspace containment', () => {
const inputs = { workflowId: 'child-workflow-id' }

Expand Down
16 changes: 11 additions & 5 deletions apps/sim/executor/handlers/workflow/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLogger } from '@sim/logger'
import { findCause, getErrorMessage, toError } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { isRecordLike } from '@sim/utils/object'
import { filterUndefined, isRecordLike } from '@sim/utils/object'
import type { Variable, WorkflowState } from '@sim/workflow-types/workflow'
import { resolveBillingAttribution } from '@/lib/billing/core/attribution'
import { DelegatedWorkspaceAuthorizationError } from '@/lib/core/application'
Expand Down Expand Up @@ -350,6 +350,8 @@ export class WorkflowBlockHandler implements BlockHandler {
}

let childWorkflowSnapshotId: string | undefined
/** Unset for draft children, and for custom blocks so the consumer learns nothing of the source. */
let childDeploymentVersionId: string | undefined
/** Set for custom blocks only: the child's own execution id / log row. */
let childExecutionId: string | undefined
let childSession: LoggingSession | undefined
Expand Down Expand Up @@ -444,6 +446,9 @@ export class WorkflowBlockHandler implements BlockHandler {
// skipped or every custom-block invocation from another workspace throws.
if (!isCustomBlock) {
this.assertChildWorkflowInWorkspace(workflowId, childWorkflow.workspaceId, ctx.workspaceId)
// Recorded only once the child is known to be this workspace's, so a refusal never names
// another workspace's version.
childDeploymentVersionId = childWorkflow.deploymentVersionId
}

childWorkflowName = childWorkflow.name || 'Unknown Workflow'
Expand Down Expand Up @@ -999,7 +1004,7 @@ export class WorkflowBlockHandler implements BlockHandler {
childWorkflowName,
instanceId,
childTraceSpans,
childWorkflowSnapshotId,
{ childWorkflowSnapshotId, childDeploymentVersionId },
{ executionId: ctx.executionId, blockId: block.id }
)

Expand Down Expand Up @@ -1097,6 +1102,7 @@ export class WorkflowBlockHandler implements BlockHandler {
childTraceSpans,
executionResult,
childWorkflowSnapshotId,
childDeploymentVersionId,
childWorkflowInstanceId: instanceId,
cause: error instanceof Error ? error : undefined,
})
Expand Down Expand Up @@ -1587,7 +1593,7 @@ export class WorkflowBlockHandler implements BlockHandler {
childWorkflowName: string,
instanceId: string,
childTraceSpans?: WorkflowTraceSpan[],
childWorkflowSnapshotId?: string,
childRun: { childWorkflowSnapshotId?: string; childDeploymentVersionId?: string } = {},
parent?: { executionId?: string; blockId: string }
): BlockOutput {
const success = childResult.success !== false
Expand All @@ -1606,7 +1612,7 @@ export class WorkflowBlockHandler implements BlockHandler {
workflowChain: chain,
rootErrorMessage,
childTraceSpans: childTraceSpans || [],
childWorkflowSnapshotId,
...childRun,
childWorkflowInstanceId: instanceId,
})
/** The warning above is this failure's log line. */
Expand All @@ -1618,7 +1624,7 @@ export class WorkflowBlockHandler implements BlockHandler {
success: true,
childWorkflowName,
childWorkflowId,
...(childWorkflowSnapshotId ? { childWorkflowSnapshotId } : {}),
...filterUndefined(childRun),
result,
childTraceSpans: childTraceSpans || [],
_childWorkflowInstanceId: instanceId,
Expand Down
7 changes: 7 additions & 0 deletions apps/sim/lib/api/contracts/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export type LogTraceSpan = {
errorType?: string
errorMessage?: string
blockId?: string
childDeploymentVersionId?: string
input?: unknown
output?: unknown
tokens?: number | { total?: number; input?: number; output?: number }
Expand Down Expand Up @@ -244,6 +245,12 @@ export const traceSpanSchema: z.ZodType<LogTraceSpan> = z
errorType: z.string().describe('Recorded error type.').optional(),
errorMessage: z.string().describe('Recorded error message.').optional(),
blockId: z.string().describe('Workflow block associated with the span.').optional(),
childDeploymentVersionId: z
.string()
.describe(
"Deployment version of the child workflow this call ran, on a workflow block span or a workflow Agent tool span. Match it to the child workflow's versions list. Absent for draft children, custom blocks, failed Agent tool calls, and runs recorded before this field existed."
)
.optional(),
input: z.unknown().describe('Input captured for the traced operation.').optional(),
output: z.unknown().describe('Output captured for the traced operation.').optional(),
tokens: z
Expand Down
Loading
Loading