@@ -70,6 +70,33 @@ export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStart
7070
7171const isProviderTurnStartError = Schema . is ( ProviderTurnStartError ) ;
7272
73+ export const nativeInputRunIdsForThread = ( input : {
74+ readonly acceptedAttempts : ReadonlyArray <
75+ Pick < OrchestrationV2RunAttempt , "runId" | "providerThreadId" | "nativeThreadId" >
76+ > ;
77+ readonly runs : ReadonlyArray < Pick < OrchestrationV2Run , "id" | "status" > > ;
78+ readonly providerThreadId : OrchestrationV2ProviderThread [ "id" ] ;
79+ readonly nativeThreadId : string | undefined ;
80+ } ) => {
81+ const rolledBackRunIds = new Set (
82+ input . runs . filter ( ( run ) => run . status === "rolled_back" ) . map ( ( run ) => run . id ) ,
83+ ) ;
84+ return new Set (
85+ input . acceptedAttempts
86+ . filter (
87+ ( attempt ) =>
88+ attempt . providerThreadId === input . providerThreadId &&
89+ attempt . nativeThreadId !== undefined &&
90+ attempt . nativeThreadId === input . nativeThreadId &&
91+ ! rolledBackRunIds . has ( attempt . runId ) ,
92+ )
93+ . map ( ( attempt ) => attempt . runId ) ,
94+ ) ;
95+ } ;
96+
97+ export const isNativeTranscriptHistoryKind = ( kind : string ) =>
98+ kind === "user_message" || kind === "assistant_message" || kind === "command_execution" ;
99+
73100/**
74101 * Claude refuses these before it reads the prompt: replacing a process running background work,
75102 * or launch arguments whose appended system prompt cannot be read.
@@ -1080,15 +1107,12 @@ export const layer: Layer.Layer<
10801107 ( source ) =>
10811108 source . providerThreadId === providerThread . id && deliveredAttemptIds . has ( source . id ) ,
10821109 ) ;
1083- const nativeInputRunIds = new Set (
1084- acceptedAttempts
1085- . filter (
1086- ( source ) =>
1087- source . nativeThreadId !== undefined &&
1088- source . nativeThreadId === runningProviderThread . nativeThreadRef ?. nativeId ,
1089- )
1090- . map ( ( source ) => source . runId ) ,
1091- ) ;
1110+ const nativeInputRunIds = nativeInputRunIdsForThread ( {
1111+ acceptedAttempts,
1112+ runs : projection . runs ,
1113+ providerThreadId : providerThread . id ,
1114+ nativeThreadId : runningProviderThread . nativeThreadRef ?. nativeId ,
1115+ } ) ;
10921116 const legacyInputRunIds = new Set (
10931117 acceptedAttempts
10941118 . filter ( ( source ) => source . nativeThreadId === undefined )
@@ -1161,13 +1185,16 @@ export const layer: Layer.Layer<
11611185 // A turn in this native thread is not enough: another provider's handoff
11621186 // may contain work the native transcript has never seen.
11631187 const itemIsInNativeThread = ( item : {
1164- readonly threadId : string ;
1165- readonly providerThreadId : string | null ;
1166- readonly runId : string | null ;
1188+ readonly threadId : ThreadId ;
1189+ readonly providerThreadId : OrchestrationV2TurnItem [ "providerThreadId" ] ;
1190+ readonly runId : OrchestrationV2TurnItem [ "runId" ] ;
1191+ readonly type ?: string ;
1192+ readonly kind ?: string ;
11671193 } ) =>
11681194 item . threadId === projection . thread . id &&
11691195 item . providerThreadId === providerThread . id &&
11701196 item . runId !== null &&
1197+ isNativeTranscriptHistoryKind ( item . kind ?? item . type ?? "" ) &&
11711198 nativeInputRunIds . has ( item . runId ) ;
11721199 const handoffIsAlreadyInNativeThread = (
11731200 handoff : OrchestrationV2ContextHandoff ,
@@ -1235,10 +1262,28 @@ export const layer: Layer.Layer<
12351262 nativeThreadHasTurns &&
12361263 missedItems . length === 0 &&
12371264 handoffsToDeliver . length > 0 ;
1265+ const omittedSourceRunIds = new Set (
1266+ handoffsToDeliver . flatMap ( ( handoff ) =>
1267+ ( handoff . history ?. omittedItems ?? 0 ) === 0
1268+ ? [ ]
1269+ : projection . runs
1270+ . filter (
1271+ ( source ) =>
1272+ source . providerThreadId !== null &&
1273+ handoff . fromProviderThreadIds . includes ( source . providerThreadId ) &&
1274+ source . ordinal >= handoff . coveredRunOrdinals . from &&
1275+ source . ordinal <= handoff . coveredRunOrdinals . to ,
1276+ )
1277+ . map ( ( source ) => source . id ) ,
1278+ ) ,
1279+ ) ;
12381280 const sourceItemsById =
1239- canDeferHandoff &&
1240- handoffsToDeliver . some ( ( handoff ) => ( handoff . history ?. omittedItems ?? 0 ) > 0 )
1241- ? new Map ( ( yield * loadTurnStartHistory ) . map ( ( item ) => [ item . id , item ] as const ) )
1281+ canDeferHandoff && omittedSourceRunIds . size > 0
1282+ ? new Map (
1283+ ( yield * projectionStore . getTurnStartHistory ( input . threadId , [
1284+ ...omittedSourceRunIds ,
1285+ ] ) ) . map ( ( item ) => [ item . id , item ] as const ) ,
1286+ )
12421287 : new Map < string , OrchestrationV2TurnItem > ( ) ;
12431288 const delivery = yield * deliverContextHandoffs ( {
12441289 handoffs : handoffsToDeliver ,
0 commit comments