11import { createLogger } from '@sim/logger'
2+ import { snapshotService } from '@/lib/logs/execution/snapshot/service'
23import { buildTraceSpans } from '@/lib/logs/execution/trace-spans/trace-spans'
34import type { TraceSpan } from '@/lib/logs/types'
45import type { BlockOutput } from '@/blocks/types'
@@ -57,6 +58,7 @@ export class WorkflowBlockHandler implements BlockHandler {
5758 const workflowMetadata = workflows [ workflowId ]
5859 let childWorkflowName = workflowMetadata ?. name || workflowId
5960
61+ let childWorkflowSnapshotId : string | undefined
6062 try {
6163 const currentDepth = ( ctx . workflowId ?. split ( '_sub_' ) . length || 1 ) - 1
6264 if ( currentDepth >= DEFAULTS . MAX_WORKFLOW_DEPTH ) {
@@ -107,6 +109,12 @@ export class WorkflowBlockHandler implements BlockHandler {
107109 childWorkflowInput = inputs . input
108110 }
109111
112+ const childSnapshotResult = await snapshotService . createSnapshotWithDeduplication (
113+ workflowId ,
114+ childWorkflow . workflowState
115+ )
116+ childWorkflowSnapshotId = childSnapshotResult . snapshot . id
117+
110118 const subExecutor = new Executor ( {
111119 workflow : childWorkflow . serializedState ,
112120 workflowInput : childWorkflowInput ,
@@ -139,7 +147,8 @@ export class WorkflowBlockHandler implements BlockHandler {
139147 workflowId ,
140148 childWorkflowName ,
141149 duration ,
142- childTraceSpans
150+ childTraceSpans ,
151+ childWorkflowSnapshotId
143152 )
144153
145154 return mappedResult
@@ -172,6 +181,7 @@ export class WorkflowBlockHandler implements BlockHandler {
172181 childWorkflowName,
173182 childTraceSpans,
174183 executionResult,
184+ childWorkflowSnapshotId,
175185 cause : error instanceof Error ? error : undefined ,
176186 } )
177187 }
@@ -279,6 +289,10 @@ export class WorkflowBlockHandler implements BlockHandler {
279289 )
280290
281291 const workflowVariables = ( workflowData . variables as Record < string , any > ) || { }
292+ const workflowStateWithVariables = {
293+ ...workflowState ,
294+ variables : workflowVariables ,
295+ }
282296
283297 if ( Object . keys ( workflowVariables ) . length > 0 ) {
284298 logger . info (
@@ -290,6 +304,7 @@ export class WorkflowBlockHandler implements BlockHandler {
290304 name : workflowData . name ,
291305 serializedState : serializedWorkflow ,
292306 variables : workflowVariables ,
307+ workflowState : workflowStateWithVariables ,
293308 rawBlocks : workflowState . blocks ,
294309 }
295310 }
@@ -358,11 +373,16 @@ export class WorkflowBlockHandler implements BlockHandler {
358373 )
359374
360375 const workflowVariables = ( wfData ?. variables as Record < string , any > ) || { }
376+ const workflowStateWithVariables = {
377+ ...deployedState ,
378+ variables : workflowVariables ,
379+ }
361380
362381 return {
363382 name : wfData ?. name || DEFAULTS . WORKFLOW_NAME ,
364383 serializedState : serializedWorkflow ,
365384 variables : workflowVariables ,
385+ workflowState : workflowStateWithVariables ,
366386 rawBlocks : deployedState . blocks ,
367387 }
368388 }
@@ -504,7 +524,8 @@ export class WorkflowBlockHandler implements BlockHandler {
504524 childWorkflowId : string ,
505525 childWorkflowName : string ,
506526 duration : number ,
507- childTraceSpans ?: WorkflowTraceSpan [ ]
527+ childTraceSpans ?: WorkflowTraceSpan [ ] ,
528+ childWorkflowSnapshotId ?: string
508529 ) : BlockOutput {
509530 const success = childResult . success !== false
510531 const result = childResult . output || { }
@@ -521,6 +542,8 @@ export class WorkflowBlockHandler implements BlockHandler {
521542 return {
522543 success : true ,
523544 childWorkflowName,
545+ childWorkflowId,
546+ childWorkflowSnapshotId,
524547 result,
525548 childTraceSpans : childTraceSpans || [ ] ,
526549 } as Record < string , any >
0 commit comments