@@ -194,6 +194,12 @@ export interface SessionPausedParams {
194194 executionState ?: SerializableExecutionState
195195}
196196
197+ /** What a completion was given that its cost-only fallback carries over. */
198+ type FallbackCompletion = Pick <
199+ SessionCompleteParams ,
200+ 'endedAt' | 'totalDurationMs' | 'traceSpans' | 'workflowInput' | 'executionState'
201+ >
202+
197203export interface LoggingSessionOptions {
198204 /**
199205 * Overrides the per-run fixed charge. Pass `0` for a run whose base charge is
@@ -1530,16 +1536,11 @@ export class LoggingSession {
15301536 requestId : this . requestId ,
15311537 error : errorMsg ,
15321538 } )
1533- await this . completeWithCostOnlyLog ( {
1534- traceSpans : params . traceSpans ,
1535- workflowInput : params . workflowInput ,
1536- endedAt : params . endedAt ,
1537- totalDurationMs : params . totalDurationMs ,
1539+ await this . completeWithCostOnlyLog ( params , {
15381540 errorMessage : `Failed to store trace spans: ${ errorMsg } ` ,
15391541 isError : false ,
15401542 finalizationPath : 'fallback_completed' ,
15411543 finalOutput : params . finalOutput || { } ,
1542- executionState : params . executionState ,
15431544 } )
15441545 }
15451546 }
@@ -1560,19 +1561,14 @@ export class LoggingSession {
15601561 `CompleteWithError failed for execution ${ this . executionId } , attempting fallback` ,
15611562 { requestId : this . requestId , error : errorMsg }
15621563 )
1563- await this . completeWithCostOnlyLog ( {
1564- traceSpans : params ?. traceSpans ,
1565- workflowInput : params ?. workflowInput ,
1566- endedAt : params ?. endedAt ,
1567- totalDurationMs : params ?. totalDurationMs ,
1564+ await this . completeWithCostOnlyLog ( params , {
15681565 errorMessage :
15691566 params ?. error ?. message || `Execution failed to store trace spans: ${ errorMsg } ` ,
15701567 isError : true ,
15711568 finalizationPath : 'force_failed' ,
15721569 finalOutput : {
15731570 error : params ?. error ?. message || `Execution failed to store trace spans: ${ errorMsg } ` ,
15741571 } ,
1575- executionState : params ?. executionState ,
15761572 status : 'failed' ,
15771573 } )
15781574 }
@@ -1595,16 +1591,11 @@ export class LoggingSession {
15951591 `CompleteWithCancellation failed for execution ${ this . executionId } , attempting fallback` ,
15961592 { requestId : this . requestId , error : errorMsg }
15971593 )
1598- await this . completeWithCostOnlyLog ( {
1599- traceSpans : params ?. traceSpans ,
1600- workflowInput : params ?. workflowInput ,
1601- endedAt : params ?. endedAt ,
1602- totalDurationMs : params ?. totalDurationMs ,
1594+ await this . completeWithCostOnlyLog ( params , {
16031595 errorMessage : 'Run was cancelled' ,
16041596 isError : false ,
16051597 finalizationPath : 'cancelled' ,
16061598 finalOutput : { cancelled : true } ,
1607- executionState : params ?. executionState ,
16081599 status : 'cancelled' ,
16091600 } )
16101601 }
@@ -1625,16 +1616,11 @@ export class LoggingSession {
16251616 `CompleteWithPause failed for execution ${ this . executionId } , attempting fallback` ,
16261617 { requestId : this . requestId , error : errorMsg }
16271618 )
1628- await this . completeWithCostOnlyLog ( {
1629- traceSpans : params ?. traceSpans ,
1630- workflowInput : params ?. workflowInput ,
1631- endedAt : params ?. endedAt ,
1632- totalDurationMs : params ?. totalDurationMs ,
1619+ await this . completeWithCostOnlyLog ( params , {
16331620 errorMessage : 'Run paused but failed to store full trace spans' ,
16341621 isError : false ,
16351622 finalizationPath : 'paused' ,
16361623 finalOutput : { paused : true } ,
1637- executionState : params ?. executionState ,
16381624 status : 'pending' ,
16391625 } )
16401626 }
@@ -1733,18 +1719,21 @@ export class LoggingSession {
17331719 }
17341720 }
17351721
1736- private async completeWithCostOnlyLog ( params : {
1737- traceSpans ?: TraceSpan [ ]
1738- workflowInput ?: unknown
1739- endedAt ?: string
1740- totalDurationMs ?: number
1741- errorMessage : string
1742- isError : boolean
1743- finalizationPath : ExecutionFinalizationPath
1744- finalOutput ?: Record < string , unknown >
1745- executionState ?: SerializableExecutionState
1746- status ?: 'completed' | 'failed' | 'cancelled' | 'pending'
1747- } ) : Promise < void > {
1722+ /**
1723+ * Completes the run with no trace spans, after the full completion failed: `completion` is
1724+ * what that completion was given (its timing, input and state carry over), `params` what the
1725+ * fallback records instead.
1726+ */
1727+ private async completeWithCostOnlyLog (
1728+ completion : FallbackCompletion | undefined ,
1729+ params : {
1730+ errorMessage : string
1731+ isError : boolean
1732+ finalizationPath : ExecutionFinalizationPath
1733+ finalOutput ?: Record < string , unknown >
1734+ status ?: 'completed' | 'failed' | 'cancelled' | 'pending'
1735+ }
1736+ ) : Promise < void > {
17481737 if ( this . completed || this . completing ) {
17491738 return
17501739 }
@@ -1761,18 +1750,18 @@ export class LoggingSession {
17611750 // from the in-memory trace spans when available (this fallback fires when
17621751 // persisting spans failed, not when computing them did), else just the
17631752 // base execution charge.
1764- const costSummary = calculateCostSummary ( params . traceSpans , this . costOptions )
1753+ const costSummary = calculateCostSummary ( completion ? .traceSpans , this . costOptions )
17651754
17661755 const finalOutput = params . finalOutput || { _fallback : true , error : params . errorMessage }
17671756
17681757 await this . completeExecutionWithFinalization ( {
1769- endedAt : params . endedAt || new Date ( ) . toISOString ( ) ,
1770- totalDurationMs : params . totalDurationMs || 0 ,
1758+ endedAt : completion ? .endedAt || new Date ( ) . toISOString ( ) ,
1759+ totalDurationMs : completion ? .totalDurationMs || 0 ,
17711760 costSummary,
17721761 finalOutput,
17731762 traceSpans : [ ] ,
1774- workflowInput : params . workflowInput ,
1775- executionState : params . executionState ,
1763+ workflowInput : completion ? .workflowInput ,
1764+ executionState : completion ? .executionState ,
17761765 finalizationPath : params . finalizationPath ,
17771766 completionFailure : params . errorMessage ,
17781767 level : params . isError ? 'error' : 'info' ,
0 commit comments