@@ -331,21 +331,88 @@ export class LoggingSession {
331331 try {
332332 await this . complete ( params )
333333 } catch ( error ) {
334- // Error already logged in complete(), log a summary here
335334 logger . warn (
336- `[${ this . requestId || 'unknown' } ] Logging completion failed for execution ${ this . executionId } - execution data not persisted `
335+ `[${ this . requestId || 'unknown' } ] Logging completion failed for execution ${ this . executionId } - attempting cost-only fallback `
337336 )
337+
338+ try {
339+ const costSummary = calculateCostSummary ( params . traceSpans || [ ] )
340+ const endTime = params . endedAt || new Date ( ) . toISOString ( )
341+ const duration = params . totalDurationMs || 0
342+
343+ await executionLogger . completeWorkflowExecution ( {
344+ executionId : this . executionId ,
345+ endedAt : endTime ,
346+ totalDurationMs : duration ,
347+ costSummary,
348+ finalOutput : { _fallback : true , error : 'Trace spans too large to store' } ,
349+ traceSpans : [ ] ,
350+ isResume : this . isResume ,
351+ } )
352+
353+ logger . info (
354+ `[${ this . requestId || 'unknown' } ] Cost-only fallback succeeded for execution ${ this . executionId } `
355+ )
356+ } catch ( fallbackError ) {
357+ logger . error (
358+ `[${ this . requestId || 'unknown' } ] Cost-only fallback also failed for execution ${ this . executionId } :` ,
359+ {
360+ error : fallbackError instanceof Error ? fallbackError . message : String ( fallbackError ) ,
361+ }
362+ )
363+ }
338364 }
339365 }
340366
341- async safeCompleteWithError ( error ?: SessionErrorCompleteParams ) : Promise < void > {
367+ async safeCompleteWithError ( params ?: SessionErrorCompleteParams ) : Promise < void > {
342368 try {
343- await this . completeWithError ( error )
344- } catch ( enhancedError ) {
345- // Error already logged in completeWithError(), log a summary here
369+ await this . completeWithError ( params )
370+ } catch ( error ) {
346371 logger . warn (
347- `[${ this . requestId || 'unknown' } ] Error logging completion failed for execution ${ this . executionId } - execution data not persisted `
372+ `[${ this . requestId || 'unknown' } ] Error logging completion failed for execution ${ this . executionId } - attempting cost-only fallback `
348373 )
374+
375+ try {
376+ const costSummary = params ?. traceSpans
377+ ? calculateCostSummary ( params . traceSpans )
378+ : {
379+ totalCost : BASE_EXECUTION_CHARGE ,
380+ totalInputCost : 0 ,
381+ totalOutputCost : 0 ,
382+ totalTokens : 0 ,
383+ totalPromptTokens : 0 ,
384+ totalCompletionTokens : 0 ,
385+ baseExecutionCharge : BASE_EXECUTION_CHARGE ,
386+ modelCost : 0 ,
387+ models : { } ,
388+ }
389+
390+ const endTime = params ?. endedAt || new Date ( ) . toISOString ( )
391+ const duration = params ?. totalDurationMs || 0
392+
393+ await executionLogger . completeWorkflowExecution ( {
394+ executionId : this . executionId ,
395+ endedAt : endTime ,
396+ totalDurationMs : duration ,
397+ costSummary,
398+ finalOutput : {
399+ _fallback : true ,
400+ error : params ?. error ?. message || 'Execution failed, trace spans too large to store' ,
401+ } ,
402+ traceSpans : [ ] ,
403+ } )
404+
405+ logger . info (
406+ `[${ this . requestId || 'unknown' } ] Cost-only fallback succeeded for execution ${ this . executionId } `
407+ )
408+ } catch ( fallbackError ) {
409+ logger . error (
410+ `[${ this . requestId || 'unknown' } ] Cost-only fallback also failed for execution ${ this . executionId } :` ,
411+ {
412+ error : fallbackError instanceof Error ? fallbackError . message : String ( fallbackError ) ,
413+ }
414+ )
415+ }
349416 }
350417 }
351418}
0 commit comments