Skip to content

Commit 6b87cf8

Browse files
authored
feat(logs): record which deployment version of a child workflow ran (#10383)
* feat(logs): record which deployment version of a child workflow ran A deployed run calls each child workflow's active version, and the handler already loads that version id to authorize the call, but the parent trace only kept the child's snapshot id. Snapshot ids are not a stable graph identity: orphaned-snapshot cleanup and block migrations can mint a new id with no redeploy. Spans now carry an optional childDeploymentVersionId: - on a workflow block's span, on success and failure, for deployed children - on a workflow_executor Agent tool span after a successful call It is taken only from workflow blocks and that tool, so another block or tool returning the same key records nothing. Draft children and custom blocks never get one. The v2 log contract documents the field. * chore(sim-cli): regenerate v2 API types for childDeploymentVersionId * fix(logs): keep child versions honest across tools, workspaces and the CLI - A custom tool titled workflow_executor loses its custom_ prefix before the span factory sees it, so its result could claim a child version. The Agent drops the key from custom tool results where the prefix is stripped. - The version is recorded only after the child passes the same-workspace check, so that refusal never names another workspace's version. - `sim logs get --trace` text output shows the child version. * fix(agent): only drop the child version from a custom tool named like the workflow tool
1 parent b660380 commit 6b87cf8

15 files changed

Lines changed: 399 additions & 17 deletions

File tree

‎apps/docs/openapi-v2-logs.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,10 @@
13951395
"type": "string",
13961396
"description": "Workflow block associated with the span."
13971397
},
1398+
"childDeploymentVersionId": {
1399+
"type": "string",
1400+
"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."
1401+
},
13981402
"input": {
13991403
"description": "Input captured for the traced operation."
14001404
},

‎apps/sim/executor/errors/child-workflow-error.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ interface ChildWorkflowErrorOptions {
88
childTraceSpans?: TraceSpan[]
99
executionResult?: ExecutionResult
1010
childWorkflowSnapshotId?: string
11+
/** The deployment version a deployed workflow-block child ran; unset for draft and custom-block children. */
12+
childDeploymentVersionId?: string
1113
childWorkflowInstanceId?: string
1214
/** Child workflow names from this invocation down to the failing run, outermost first. */
1315
workflowChain?: string[]
@@ -38,6 +40,7 @@ export class ChildWorkflowError extends Error {
3840
readonly childWorkflowName: string
3941
readonly executionResult?: ExecutionResult
4042
readonly childWorkflowSnapshotId?: string
43+
readonly childDeploymentVersionId?: string
4144
/** Per-invocation unique ID used to correlate child block events with this workflow block. */
4245
readonly childWorkflowInstanceId?: string
4346
/**
@@ -59,6 +62,7 @@ export class ChildWorkflowError extends Error {
5962
this.childTraceSpans = options.childTraceSpans ?? []
6063
this.executionResult = options.executionResult
6164
this.childWorkflowSnapshotId = options.childWorkflowSnapshotId
65+
this.childDeploymentVersionId = options.childDeploymentVersionId
6266
this.childWorkflowInstanceId = options.childWorkflowInstanceId
6367
this.workflowChain = options.workflowChain ?? [options.childWorkflowName]
6468
this.rootErrorMessage = options.rootErrorMessage ?? options.message

‎apps/sim/executor/execution/block-executor.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,44 @@ describe('BlockExecutor', () => {
736736
)
737737
})
738738

739+
it.each([
740+
[BlockType.WORKFLOW_INPUT, 'dv-7'],
741+
['custom_block_abc', undefined],
742+
])(
743+
'records the child deployment version on a failed %s block only when it is a workflow block',
744+
async (blockType, expected) => {
745+
const block = { ...createBlock(), metadata: { id: blockType, name: 'Child' } }
746+
const state = new ExecutionState()
747+
const workflow: SerializedWorkflow = {
748+
version: '1',
749+
blocks: [block],
750+
connections: [],
751+
loops: {},
752+
parallels: {},
753+
}
754+
const handler: BlockHandler = {
755+
canHandle: () => true,
756+
execute: async () => {
757+
throw new ChildWorkflowError({
758+
message: '"Child" failed: boom',
759+
childWorkflowName: 'Child',
760+
childDeploymentVersionId: 'dv-7',
761+
})
762+
},
763+
}
764+
const executor = new BlockExecutor(
765+
[handler],
766+
new VariableResolver(workflow, {}, state),
767+
{},
768+
state
769+
)
770+
771+
await executor.execute(createContext(state), createNode(block), block).catch(() => undefined)
772+
773+
expect(state.getBlockOutput(block.id)?.childDeploymentVersionId).toBe(expected)
774+
}
775+
)
776+
739777
it('does not soft-succeed non-agent blocks on user AbortError', async () => {
740778
const block = createBlock()
741779
const workflow: SerializedWorkflow = {

‎apps/sim/executor/execution/block-executor.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,9 @@ export class BlockExecutor {
760760
if (error.childWorkflowSnapshotId) {
761761
errorOutput.childWorkflowSnapshotId = error.childWorkflowSnapshotId
762762
}
763+
if (error.childDeploymentVersionId) {
764+
errorOutput.childDeploymentVersionId = error.childDeploymentVersionId
765+
}
763766
}
764767
// A custom block's consumer gets a machine-readable failure class and an
765768
// opaque handle to the failed run — enough to branch on and to quote in a

‎apps/sim/executor/handlers/agent/handler.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,6 +3540,34 @@ describe('AgentBlockHandler', () => {
35403540
})
35413541
})
35423542

3543+
it('keeps a custom tool titled like the workflow tool from claiming a child version', async () => {
3544+
mockExecuteProviderRequest.mockResolvedValueOnce({
3545+
content: 'done',
3546+
model: 'mock-model',
3547+
tokens: { input: 10, output: 20, total: 30 },
3548+
toolCalls: [
3549+
{ name: 'custom_workflow_executor', result: { childDeploymentVersionId: 'forged' } },
3550+
{ name: 'workflow_executor', result: { childDeploymentVersionId: 'dv-7' } },
3551+
{ name: 'custom_lookup', result: { childDeploymentVersionId: 'user-data' } },
3552+
],
3553+
cost: 0.001,
3554+
timing: { total: 100 },
3555+
})
3556+
3557+
const result = await handler.execute(mockContext, mockBlock, {
3558+
model: 'gpt-4o',
3559+
userPrompt: 'Score it',
3560+
apiKey: 'test-api-key',
3561+
})
3562+
3563+
const [custom, workflow, otherCustom] = (
3564+
result as { toolCalls: { list: Array<{ result: Record<string, unknown> }> } }
3565+
).toolCalls.list
3566+
expect(custom.result).not.toHaveProperty('childDeploymentVersionId')
3567+
expect(workflow.result.childDeploymentVersionId).toBe('dv-7')
3568+
expect(otherCustom.result.childDeploymentVersionId).toBe('user-data')
3569+
})
3570+
35433571
it('uses a secret-backed skillId for lookup without carrying it into output provenance', async () => {
35443572
const skillId = 'x'
35453573
mockContext.workspaceId = 'workspace-1'

‎apps/sim/executor/handlers/agent/handler.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ import {
6363
validateBlockType,
6464
validateModelProvider,
6565
} from '@/ee/access-control/utils/permission-check'
66-
import { AGENT, BlockType, DEFAULTS, stripCustomToolPrefix } from '@/executor/constants'
66+
import {
67+
AGENT,
68+
BlockType,
69+
DEFAULTS,
70+
isCustomTool,
71+
stripCustomToolPrefix,
72+
} from '@/executor/constants'
6773
import {
6874
getMemoryMessageAppendKey,
6975
getMemoryMessageTurnId,
@@ -3227,6 +3233,7 @@ export class AgentBlockHandler implements BlockHandler {
32273233

32283234
private formatToolCall(tc: any) {
32293235
const toolName = stripCustomToolPrefix(tc.name)
3236+
const result = tc.result || tc.output
32303237

32313238
return {
32323239
...tc,
@@ -3235,7 +3242,12 @@ export class AgentBlockHandler implements BlockHandler {
32353242
endTime: tc.endTime,
32363243
duration: tc.duration,
32373244
arguments: tc.arguments || tc.input || {},
3238-
result: tc.result || tc.output,
3245+
// The span factory trusts this key only on a `workflow_executor` call. Stripping the prefix
3246+
// would let a custom tool titled `workflow_executor` claim a child version it never ran.
3247+
result:
3248+
isCustomTool(tc.name) && toolName === 'workflow_executor' && isRecordLike(result)
3249+
? omit(result, ['childDeploymentVersionId'])
3250+
: result,
32393251
}
32403252
}
32413253
}

‎apps/sim/executor/handlers/workflow/handler.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,86 @@ describe('WorkflowBlockHandler', () => {
358358
})
359359
})
360360

361+
describe('child deployment version', () => {
362+
const inputs = { workflowId: 'child-workflow-id' }
363+
const deployedCtx = () =>
364+
({
365+
...mockContext,
366+
workspaceId: 'workspace-parent',
367+
isDeployedContext: true,
368+
}) as unknown as ExecutionContext
369+
370+
beforeEach(() => {
371+
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
372+
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
373+
workspaceId: 'workspace-parent',
374+
state: {
375+
blocks: {},
376+
edges: [],
377+
loops: {},
378+
parallels: {},
379+
deploymentVersionId: 'dv-7',
380+
},
381+
})
382+
mockResolveSnapshot.mockResolvedValue({ id: 'snapshot-1', workflowId: 'w', stateHash: 'h' })
383+
})
384+
385+
it('records the version a deployed child ran', async () => {
386+
mockExecutorExecute.mockResolvedValue({ success: true, output: { data: 'ok' } })
387+
388+
const output = await handler.execute(deployedCtx(), mockBlock, inputs)
389+
390+
expect(output).toMatchObject({ childDeploymentVersionId: 'dv-7' })
391+
})
392+
393+
it('records the version on a deployed child that reports a failure', async () => {
394+
mockExecutorExecute.mockResolvedValue({ success: false, error: 'boom', output: {} })
395+
396+
const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)
397+
398+
expect(failure).toMatchObject({ childDeploymentVersionId: 'dv-7' })
399+
})
400+
401+
it('records the version on a deployed child that throws', async () => {
402+
mockExecutorExecute.mockRejectedValue(new Error('engine blew up'))
403+
404+
const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)
405+
406+
expect(failure).toMatchObject({ childDeploymentVersionId: 'dv-7' })
407+
})
408+
409+
it('records no version for a deployed child refused by the workspace check', async () => {
410+
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
411+
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
412+
workspaceId: 'workspace-other',
413+
state: { blocks: {}, edges: [], loops: {}, parallels: {}, deploymentVersionId: 'dv-7' },
414+
})
415+
416+
const failure = await handler.execute(deployedCtx(), mockBlock, inputs).catch((e) => e)
417+
418+
expect(failure.message).toContain('belongs to a different workspace')
419+
expect(failure).not.toHaveProperty('childDeploymentVersionId', 'dv-7')
420+
})
421+
422+
it('records no version for a draft child', async () => {
423+
mockReadWorkflowDefinitionAsExecutor.mockResolvedValue({
424+
workflow: { id: 'child-workflow-id', name: 'Score', variables: {} },
425+
workspaceId: 'workspace-parent',
426+
state: { blocks: {}, edges: [], loops: {}, parallels: {} },
427+
})
428+
mockExecutorExecute.mockResolvedValue({ success: true, output: { data: 'ok' } })
429+
430+
const output = await handler.execute(
431+
{ ...mockContext, workspaceId: 'workspace-parent' } as unknown as ExecutionContext,
432+
mockBlock,
433+
inputs
434+
)
435+
436+
expect(output).toMatchObject({ childWorkflowId: 'child-workflow-id' })
437+
expect(output).not.toHaveProperty('childDeploymentVersionId')
438+
})
439+
})
440+
361441
describe('workspace containment', () => {
362442
const inputs = { workflowId: 'child-workflow-id' }
363443

‎apps/sim/executor/handlers/workflow/handler.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { findCause, getErrorMessage, toError } from '@sim/utils/errors'
33
import { generateId } from '@sim/utils/id'
4-
import { isRecordLike } from '@sim/utils/object'
4+
import { filterUndefined, isRecordLike } from '@sim/utils/object'
55
import type { Variable, WorkflowState } from '@sim/workflow-types/workflow'
66
import { resolveBillingAttribution } from '@/lib/billing/core/attribution'
77
import { DelegatedWorkspaceAuthorizationError } from '@/lib/core/application'
@@ -350,6 +350,8 @@ export class WorkflowBlockHandler implements BlockHandler {
350350
}
351351

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

449454
childWorkflowName = childWorkflow.name || 'Unknown Workflow'
@@ -999,7 +1004,7 @@ export class WorkflowBlockHandler implements BlockHandler {
9991004
childWorkflowName,
10001005
instanceId,
10011006
childTraceSpans,
1002-
childWorkflowSnapshotId,
1007+
{ childWorkflowSnapshotId, childDeploymentVersionId },
10031008
{ executionId: ctx.executionId, blockId: block.id }
10041009
)
10051010

@@ -1097,6 +1102,7 @@ export class WorkflowBlockHandler implements BlockHandler {
10971102
childTraceSpans,
10981103
executionResult,
10991104
childWorkflowSnapshotId,
1105+
childDeploymentVersionId,
11001106
childWorkflowInstanceId: instanceId,
11011107
cause: error instanceof Error ? error : undefined,
11021108
})
@@ -1587,7 +1593,7 @@ export class WorkflowBlockHandler implements BlockHandler {
15871593
childWorkflowName: string,
15881594
instanceId: string,
15891595
childTraceSpans?: WorkflowTraceSpan[],
1590-
childWorkflowSnapshotId?: string,
1596+
childRun: { childWorkflowSnapshotId?: string; childDeploymentVersionId?: string } = {},
15911597
parent?: { executionId?: string; blockId: string }
15921598
): BlockOutput {
15931599
const success = childResult.success !== false
@@ -1606,7 +1612,7 @@ export class WorkflowBlockHandler implements BlockHandler {
16061612
workflowChain: chain,
16071613
rootErrorMessage,
16081614
childTraceSpans: childTraceSpans || [],
1609-
childWorkflowSnapshotId,
1615+
...childRun,
16101616
childWorkflowInstanceId: instanceId,
16111617
})
16121618
/** The warning above is this failure's log line. */
@@ -1618,7 +1624,7 @@ export class WorkflowBlockHandler implements BlockHandler {
16181624
success: true,
16191625
childWorkflowName,
16201626
childWorkflowId,
1621-
...(childWorkflowSnapshotId ? { childWorkflowSnapshotId } : {}),
1627+
...filterUndefined(childRun),
16221628
result,
16231629
childTraceSpans: childTraceSpans || [],
16241630
_childWorkflowInstanceId: instanceId,

‎apps/sim/lib/api/contracts/logs.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export type LogTraceSpan = {
213213
errorType?: string
214214
errorMessage?: string
215215
blockId?: string
216+
childDeploymentVersionId?: string
216217
input?: unknown
217218
output?: unknown
218219
tokens?: number | { total?: number; input?: number; output?: number }
@@ -244,6 +245,12 @@ export const traceSpanSchema: z.ZodType<LogTraceSpan> = z
244245
errorType: z.string().describe('Recorded error type.').optional(),
245246
errorMessage: z.string().describe('Recorded error message.').optional(),
246247
blockId: z.string().describe('Workflow block associated with the span.').optional(),
248+
childDeploymentVersionId: z
249+
.string()
250+
.describe(
251+
"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."
252+
)
253+
.optional(),
247254
input: z.unknown().describe('Input captured for the traced operation.').optional(),
248255
output: z.unknown().describe('Output captured for the traced operation.').optional(),
249256
tokens: z

0 commit comments

Comments
 (0)