@@ -413,11 +413,9 @@ export async function summarizeInStages(params: {
413413
414414 const partialSummaries : string [ ] = [ ] ;
415415 let consecutiveGenericFallbacks = 0 ;
416- // Chunk 0 carries the oldest messages, which is where a caller puts context it needs
417- // redistilled into the new summary. Only its degradation means that context never
418- // reached the merge, so this — not "any chunk degraded" — is what the result kind
419- // reports; otherwise a caller restoring lost context duplicates what the merge has.
420- let firstChunkDegraded = false ;
416+ // Caller-owned leading context lives in the oldest split. Only losing that
417+ // split requires restoration; later fallback placeholders remain in the merge.
418+ let oldestChunkDegraded = false ;
421419 for ( const [ index , chunk ] of plan . chunks . entries ( ) ) {
422420 const result = await summarizeWithFallbackResult ( {
423421 ...params ,
@@ -427,7 +425,7 @@ export async function summarizeInStages(params: {
427425 consecutiveGenericFallbacks =
428426 result . kind === "generic-fallback" ? consecutiveGenericFallbacks + 1 : 0 ;
429427 if ( index === 0 ) {
430- firstChunkDegraded = result . kind === "generic-fallback" ;
428+ oldestChunkDegraded = result . kind === "generic-fallback" ;
431429 }
432430
433431 // Keep one placeholder to mark the missing split, but stop before repeated
@@ -451,7 +449,7 @@ export async function summarizeInStages(params: {
451449 throw new Error ( "Compaction summary plan produced no summary" ) ;
452450 }
453451 return {
454- kind : firstChunkDegraded ? "generic-fallback" : "summary" ,
452+ kind : oldestChunkDegraded ? "generic-fallback" : "summary" ,
455453 text : summary ,
456454 } ;
457455 }
@@ -492,7 +490,7 @@ export async function summarizeInStages(params: {
492490 messages : summaryMessages ,
493491 customInstructions : mergeInstructions ,
494492 } ) ;
495- return firstChunkDegraded && mergedResult . kind === "summary"
493+ return oldestChunkDegraded && mergedResult . kind === "summary"
496494 ? { kind : "generic-fallback" , text : mergedResult . text }
497495 : mergedResult ;
498496}
0 commit comments