@@ -280,31 +280,6 @@ export const layer: Layer.Layer<
280280 cause : `Run ${ runId } is missing its execution projection state.` ,
281281 } ) ;
282282 }
283- const deliveredAttemptIds = new Set (
284- projection . providerTurns . map ( ( turn ) => turn . runAttemptId ) ,
285- ) ;
286- const readHandoffHistory = Effect . fn ( function * ( runIds ?: ReadonlyArray < RunId > ) {
287- const items = yield * projectionStore . getTurnStartHistory ( input . threadId , runIds ) ;
288- const maintenanceMessageIds = new Set (
289- items . flatMap ( ( item ) =>
290- item . type === "user_message" &&
291- item . attachments . length === 0 &&
292- [ "/compact" , "/logout" ] . includes ( item . text . trim ( ) . toLowerCase ( ) )
293- ? [ item . messageId ]
294- : [ ] ,
295- ) ,
296- ) ;
297- const excludedRunIds = new Set (
298- projection . runs
299- . filter (
300- ( source ) =>
301- maintenanceMessageIds . has ( source . userMessageId ) &&
302- ! deliveredAttemptIds . has ( source . activeAttemptId ) ,
303- )
304- . map ( ( source ) => source . id ) ,
305- ) ;
306- return items . filter ( ( item ) => item . runId === null || ! excludedRunIds . has ( item . runId ) ) ;
307- } ) ;
308283 // Settles a run that never reached the provider: one signal turn item plus
309284 // terminal run, attempt and root node, written only while the run is still
310285 // the current starting attempt.
@@ -313,6 +288,8 @@ export const layer: Layer.Layer<
313288 readonly signal : string ;
314289 readonly status : "completed" | "failed" ;
315290 readonly now : DateTime . Utc ;
291+ /** Omitted when the run never started, so `startedAt` stays as projected. */
292+ readonly startedAt ?: DateTime . Utc ;
316293 readonly providerInstanceId : OrchestrationV2Run [ "providerInstanceId" ] ;
317294 readonly itemProviderThreadId : OrchestrationV2ProviderThread [ "id" ] ;
318295 readonly item :
@@ -328,6 +305,7 @@ export const layer: Layer.Layer<
328305 readonly providerThreadUpdate ?: OrchestrationV2ProviderThread ;
329306 } ) {
330307 const { now, status } = input ;
308+ const started = input . startedAt === undefined ? { } : { startedAt : input . startedAt } ;
331309 const item : OrchestrationV2TurnItem = {
332310 id : idAllocator . derive . runSignalTurnItem ( { runId, signal : input . signal } ) ,
333311 threadId : projection . thread . id ,
@@ -352,14 +330,14 @@ export const layer: Layer.Layer<
352330 } ;
353331 const eventPayloads = [
354332 { type : "turn-item.updated" , payload : item } ,
355- { type : "run.updated" , payload : { ...run , status, completedAt : now } } ,
333+ { type : "run.updated" , payload : { ...run , status, ... started , completedAt : now } } ,
356334 {
357335 type : "run-attempt.updated" ,
358- payload : { ...attempt , status, completedAt : now } ,
336+ payload : { ...attempt , status, ... started , completedAt : now } ,
359337 } ,
360338 {
361339 type : "node.updated" ,
362- payload : { ...rootNode , status, completedAt : now } ,
340+ payload : { ...rootNode , status, ... started , completedAt : now } ,
363341 } ,
364342 ...( input . providerThreadUpdate === undefined
365343 ? [ ]
@@ -407,7 +385,6 @@ export const layer: Layer.Layer<
407385 const previousNativeRun = projection . runs . reduce < OrchestrationV2Run | undefined > (
408386 ( previous , candidate ) =>
409387 candidate . ordinal < run . ordinal &&
410- candidate . startedAt !== null &&
411388 candidate . providerThreadId !== null &&
412389 nativeThreads . has ( candidate . providerThreadId ) &&
413390 ( previous === undefined || candidate . ordinal > previous . ordinal )
@@ -451,6 +428,7 @@ export const layer: Layer.Layer<
451428 signal : isEmptyCompaction ? "empty-compaction" : "provider-sign-out" ,
452429 status,
453430 now,
431+ startedAt : now ,
454432 providerInstanceId : authInstanceId ,
455433 itemProviderThreadId : nativeThread ?. id ?? providerThread . id ,
456434 item :
@@ -747,7 +725,7 @@ export const layer: Layer.Layer<
747725 coveredRunOrdinals : { from : 1 , to : Math . max ( 1 , run . ordinal - 1 ) } ,
748726 strategy : "full_thread_summary" ,
749727 runs : projection . runs ,
750- items : ( yield * readHandoffHistory ( ) ) . filter (
728+ items : ( yield * projectionStore . getTurnStartHistory ( input . threadId ) ) . filter (
751729 ( item ) =>
752730 item . runId === null ||
753731 projection . runs . some (
@@ -859,36 +837,6 @@ export const layer: Layer.Layer<
859837 createdAt : providerThread . createdAt ,
860838 updatedAt : now ,
861839 } ;
862- const nativeThreadId = runningProviderThread . nativeThreadRef ?. nativeId ;
863- if (
864- message . attachments . length === 0 &&
865- message . text . trim ( ) . toLowerCase ( ) === "/compact" &&
866- effectiveHandoffs . some (
867- ( handoff ) =>
868- nativeThreadId === undefined ||
869- handoff . delivery ?. nativeThreadId !== nativeThreadId ||
870- handoff . delivery . status === "pending" ,
871- )
872- ) {
873- yield * settleRunBeforeStart ( {
874- signal : "handoff-replaces-compaction" ,
875- status : "failed" ,
876- now,
877- providerInstanceId : run . providerInstanceId ,
878- itemProviderThreadId : providerThread . id ,
879- item : {
880- type : "error" ,
881- title : "Context handoff replaces compaction" ,
882- failure : makeProviderFailure ( {
883- class : "validation_error" ,
884- message :
885- "Context handoff replaces compaction. Send a message to the selected provider, or switch back to compact the current conversation." ,
886- } ) ,
887- } ,
888- providerThreadUpdate : { ...runningProviderThread , status : "idle" } ,
889- } ) ;
890- return ;
891- }
892840 const runningRun : OrchestrationV2Run = {
893841 ...run ,
894842 status : "running" ,
@@ -1046,6 +994,9 @@ export const layer: Layer.Layer<
1046994 ...deliveredItemIds ,
1047995 ...settledHandoffs . flatMap ( ( handoff ) => handoff . delivery ?. omittedItemIds ?? [ ] ) ,
1048996 ] ) ;
997+ const deliveredAttemptIds = new Set (
998+ projection . providerTurns . map ( ( turn ) => turn . runAttemptId ) ,
999+ ) ;
10491000 const acceptedAttempts = projection . attempts . filter (
10501001 ( source ) =>
10511002 source . providerThreadId === providerThread . id && deliveredAttemptIds . has ( source . id ) ,
@@ -1120,36 +1071,29 @@ export const layer: Layer.Layer<
11201071 ...runningProviderThread ,
11211072 contextUsage : handoffUsage ,
11221073 } ;
1123- const undeliveredRuns = projection . runs . filter (
1074+ const missedRuns = projection . runs . filter (
11241075 ( source ) =>
11251076 source . ordinal < run . ordinal &&
11261077 source . providerThreadId === providerThread . id &&
11271078 ( source . status === "failed" || source . status === "interrupted" ) &&
11281079 ! deliveredAttemptIds . has ( source . activeAttemptId ) ,
11291080 ) ;
1081+ const missedRunIds = new Set ( missedRuns . map ( ( source ) => source . id ) ) ;
11301082 const missedItems =
1131- undeliveredRuns . length === 0
1083+ missedRunIds . size === 0
11321084 ? [ ]
1133- : ( yield * readHandoffHistory ( undeliveredRuns . map ( ( source ) => source . id ) ) ) . filter (
1134- ( item ) => ! coveredItemIds . has ( item . id ) && historicalMessage ( item ) !== null ,
1085+ : ( yield * projectionStore . getTurnStartHistory ( input . threadId , [ ...missedRunIds ] ) ) . filter (
1086+ ( item ) =>
1087+ item . runId !== null &&
1088+ missedRunIds . has ( item . runId ) &&
1089+ ! coveredItemIds . has ( item . id ) &&
1090+ historicalMessage ( item ) !== null ,
11351091 ) ;
1136- const missedRunIds = new Set ( missedItems . map ( ( item ) => item . runId ) ) ;
1137- const missedRuns = undeliveredRuns . filter ( ( source ) => missedRunIds . has ( source . id ) ) ;
11381092 const startWithHandoffs = (
11391093 turnInput : Parameters < typeof session . startTurn > [ 0 ] ,
11401094 compact = false ,
11411095 ) =>
11421096 Effect . gen ( function * ( ) {
1143- if ( compact && missedItems . length > 0 ) {
1144- return yield * new ProviderAdapter . ProviderAdapterTurnStartError ( {
1145- driver : session . driver ,
1146- threadId : projection . thread . id ,
1147- providerThreadId : providerThread . id ,
1148- runId : run . id ,
1149- cause :
1150- "Context handoff replaces compaction. Send a message to the selected provider, or switch back to compact the current conversation." ,
1151- } ) ;
1152- }
11531097 // A failed turn/start can leave the requested turn absent from
11541098 // native history even when its preceding handoff was injected.
11551099 const retryHandoff =
0 commit comments