@@ -138,16 +138,23 @@ async function readSnapshotStates(
138138}
139139
140140/**
141- * Read here, after authorization, rather than with the context: a caller the
142- * use case refuses never fetches the run's execution data.
141+ * The run's execution data with the fields a completion write changes alongside it,
142+ * read in one statement after authorization: a caller the use case refuses never
143+ * fetches the payload, and a run finishing mid-request cannot pair new data with
144+ * stale timing or cost.
143145 */
144- async function readExecutionData ( executionId : string ) : Promise < unknown > {
146+ async function readRunCompletion ( executionId : string ) {
145147 const [ row ] = await db
146- . select ( { executionData : workflowExecutionLogs . executionData } )
148+ . select ( {
149+ executionData : workflowExecutionLogs . executionData ,
150+ endedAt : workflowExecutionLogs . endedAt ,
151+ totalDurationMs : workflowExecutionLogs . totalDurationMs ,
152+ costTotal : workflowExecutionLogs . costTotal ,
153+ } )
147154 . from ( workflowExecutionLogs )
148155 . where ( eq ( workflowExecutionLogs . executionId , executionId ) )
149156 . limit ( 1 )
150- return row ?. executionData ?? null
157+ return row
151158}
152159
153160function collectChildSnapshotIds ( traceSpans : TraceSpan [ ] ) : string [ ] {
@@ -215,8 +222,10 @@ const authorizedReadExecutionSnapshotUseCase = defineAuthorizedWorkspaceUseCase(
215222 }
216223
217224 input . signal ?. throwIfAborted ( )
225+ const completion = await readRunCompletion ( record . executionId )
226+ if ( ! completion ) throw new OrchestrationError ( 'not_found' , 'Workflow execution not found' )
218227 const executionData = ( await materializeExecutionData (
219- ( await readExecutionData ( record . executionId ) ) as Record < string , unknown > | null ,
228+ completion . executionData as Record < string , unknown > | null ,
220229 {
221230 workspaceId : context . workspaceId ,
222231 workflowId : record . workflowId ,
@@ -246,9 +255,9 @@ const authorizedReadExecutionSnapshotUseCase = defineAuthorizedWorkspaceUseCase(
246255 executionMetadata : {
247256 trigger : record . trigger ,
248257 startedAt : record . startedAt . toISOString ( ) ,
249- endedAt : record . endedAt ?. toISOString ( ) ,
250- totalDurationMs : record . totalDurationMs ,
251- cost : projectCostTotal ( record . costTotal , projection ) ,
258+ endedAt : completion . endedAt ?. toISOString ( ) ,
259+ totalDurationMs : completion . totalDurationMs ,
260+ cost : projectCostTotal ( completion . costTotal , projection ) ,
252261 } ,
253262 }
254263 } ,
0 commit comments