@@ -280,6 +280,31 @@ 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+ } ) ;
283308 // Settles a run that never reached the provider: one signal turn item plus
284309 // terminal run, attempt and root node, written only while the run is still
285310 // the current starting attempt.
@@ -722,7 +747,7 @@ export const layer: Layer.Layer<
722747 coveredRunOrdinals : { from : 1 , to : Math . max ( 1 , run . ordinal - 1 ) } ,
723748 strategy : "full_thread_summary" ,
724749 runs : projection . runs ,
725- items : ( yield * projectionStore . getTurnStartHistory ( input . threadId ) ) . filter (
750+ items : ( yield * readHandoffHistory ( ) ) . filter (
726751 ( item ) =>
727752 item . runId === null ||
728753 projection . runs . some (
@@ -1021,9 +1046,6 @@ export const layer: Layer.Layer<
10211046 ...deliveredItemIds ,
10221047 ...settledHandoffs . flatMap ( ( handoff ) => handoff . delivery ?. omittedItemIds ?? [ ] ) ,
10231048 ] ) ;
1024- const deliveredAttemptIds = new Set (
1025- projection . providerTurns . map ( ( turn ) => turn . runAttemptId ) ,
1026- ) ;
10271049 const acceptedAttempts = projection . attempts . filter (
10281050 ( source ) =>
10291051 source . providerThreadId === providerThread . id && deliveredAttemptIds . has ( source . id ) ,
@@ -1105,33 +1127,14 @@ export const layer: Layer.Layer<
11051127 ( source . status === "failed" || source . status === "interrupted" ) &&
11061128 ! deliveredAttemptIds . has ( source . activeAttemptId ) ,
11071129 ) ;
1108- const undeliveredItems =
1130+ const missedItems =
11091131 undeliveredRuns . length === 0
11101132 ? [ ]
1111- : yield * projectionStore . getTurnStartHistory (
1112- input . threadId ,
1113- undeliveredRuns . map ( ( source ) => source . id ) ,
1133+ : ( yield * readHandoffHistory ( undeliveredRuns . map ( ( source ) => source . id ) ) ) . filter (
1134+ ( item ) => ! coveredItemIds . has ( item . id ) && historicalMessage ( item ) !== null ,
11141135 ) ;
1115- const maintenanceMessageIds = new Set (
1116- undeliveredItems . flatMap ( ( item ) =>
1117- item . type === "user_message" &&
1118- item . attachments . length === 0 &&
1119- [ "/compact" , "/logout" ] . includes ( item . text . trim ( ) . toLowerCase ( ) )
1120- ? [ item . messageId ]
1121- : [ ] ,
1122- ) ,
1123- ) ;
1124- const missedRuns = undeliveredRuns . filter (
1125- ( source ) => ! maintenanceMessageIds . has ( source . userMessageId ) ,
1126- ) ;
1127- const missedRunIds = new Set ( missedRuns . map ( ( source ) => source . id ) ) ;
1128- const missedItems = undeliveredItems . filter (
1129- ( item ) =>
1130- item . runId !== null &&
1131- missedRunIds . has ( item . runId ) &&
1132- ! coveredItemIds . has ( item . id ) &&
1133- historicalMessage ( item ) !== null ,
1134- ) ;
1136+ const missedRunIds = new Set ( missedItems . map ( ( item ) => item . runId ) ) ;
1137+ const missedRuns = undeliveredRuns . filter ( ( source ) => missedRunIds . has ( source . id ) ) ;
11351138 const startWithHandoffs = (
11361139 turnInput : Parameters < typeof session . startTurn > [ 0 ] ,
11371140 compact = false ,
0 commit comments