@@ -7,7 +7,12 @@ import {
77 createTriggerObject ,
88 loadWorkflowStateForExecution ,
99} from '@/lib/logs/execution/logging-factory'
10- import type { ExecutionEnvironment , ExecutionTrigger , WorkflowState } from '@/lib/logs/types'
10+ import type {
11+ ExecutionEnvironment ,
12+ ExecutionTrigger ,
13+ TraceSpan ,
14+ WorkflowState ,
15+ } from '@/lib/logs/types'
1116
1217const logger = createLogger ( 'LoggingSession' )
1318
@@ -25,6 +30,15 @@ export interface SessionCompleteParams {
2530 traceSpans ?: any [ ]
2631}
2732
33+ export interface SessionErrorCompleteParams {
34+ endedAt ?: string
35+ totalDurationMs ?: number
36+ error ?: {
37+ message ?: string
38+ stackTrace ?: string
39+ }
40+ }
41+
2842export class LoggingSession {
2943 private workflowId : string
3044 private executionId : string
@@ -115,8 +129,14 @@ export class LoggingSession {
115129 }
116130 }
117131
118- async completeWithError ( error ?: any ) : Promise < void > {
132+ async completeWithError ( params : SessionErrorCompleteParams = { } ) : Promise < void > {
119133 try {
134+ const { endedAt, totalDurationMs, error } = params
135+
136+ const endTime = endedAt ? new Date ( endedAt ) : new Date ( )
137+ const durationMs = typeof totalDurationMs === 'number' ? totalDurationMs : 0
138+ const startTime = new Date ( endTime . getTime ( ) - Math . max ( 1 , durationMs ) )
139+
120140 const costSummary = {
121141 totalCost : BASE_EXECUTION_CHARGE ,
122142 totalInputCost : 0 ,
@@ -129,13 +149,29 @@ export class LoggingSession {
129149 models : { } ,
130150 }
131151
152+ const message = error ?. message || 'Execution failed before starting blocks'
153+
154+ const syntheticErrorSpan : TraceSpan [ ] = [
155+ {
156+ id : 'pre-execution-validation' ,
157+ name : 'Workflow Error' ,
158+ type : 'validation' ,
159+ duration : Math . max ( 1 , durationMs ) ,
160+ startTime : startTime . toISOString ( ) ,
161+ endTime : endTime . toISOString ( ) ,
162+ status : 'error' ,
163+ children : [ ] ,
164+ output : { error : message } ,
165+ } ,
166+ ]
167+
132168 await executionLogger . completeWorkflowExecution ( {
133169 executionId : this . executionId ,
134- endedAt : new Date ( ) . toISOString ( ) ,
135- totalDurationMs : 0 ,
170+ endedAt : endTime . toISOString ( ) ,
171+ totalDurationMs : Math . max ( 1 , durationMs ) ,
136172 costSummary,
137- finalOutput : null ,
138- traceSpans : [ ] ,
173+ finalOutput : { error : message } ,
174+ traceSpans : syntheticErrorSpan ,
139175 } )
140176
141177 if ( this . requestId ) {
@@ -170,7 +206,7 @@ export class LoggingSession {
170206 }
171207 }
172208
173- async safeCompleteWithError ( error ?: any ) : Promise < void > {
209+ async safeCompleteWithError ( error ?: SessionErrorCompleteParams ) : Promise < void > {
174210 try {
175211 await this . completeWithError ( error )
176212 } catch ( enhancedError ) {
0 commit comments