@@ -525,26 +525,28 @@ function isRetainedDependency(
525525 ) ;
526526}
527527
528- // Clients find a limited run that later notification wakes hide (usageLimitBlockedRun) from its
529- // root error and from how each later run started. A long chain of wakes pushes those out of the
530- // newest turns, which would leave the thread unresumable.
531- function limitRecoveryItemIds (
528+ // usageLimitBlockedRun needs the limited run's root error and the notification that started each
529+ // later run, and a long chain of wakes pushes both out of the window. Starts are kept as stubs
530+ // without their unbounded detail, and dropped past a quarter of the budget (clients then show
531+ // the plain latest run).
532+ function limitRecoveryItems (
532533 projection : Pick < OrchestrationV2ThreadProjection , "runs" | "turnItems" > ,
533- ) : ReadonlySet < string > {
534- const ids = new Set < string > ( ) ;
534+ maxEncodedBytes : number ,
535+ ) : ReadonlyMap < string , OrchestrationV2TurnItem > {
536+ const items = new Map < string , OrchestrationV2TurnItem > ( ) ;
535537 const blocked = usageLimitBlockedRun ( projection . runs , projection . turnItems , null ) ;
536- if ( blocked === null ) return ids ;
538+ if ( blocked === null ) return items ;
537539 const laterRoots = new Set < string > ( ) ;
538540 for ( const run of projection . runs ) {
539541 if ( run . ordinal > blocked . ordinal && run . rootNodeId !== null ) {
540542 laterRoots . add ( String ( run . rootNodeId ) ) ;
541543 }
542544 }
543- if ( laterRoots . size === 0 ) return ids ;
545+ if ( laterRoots . size === 0 ) return items ;
544546 const firstInputs = new Map < string , OrchestrationV2TurnItem > ( ) ;
545547 for ( const item of projection . turnItems ) {
546548 if ( item . type === "error" && item . runId === blocked . id && item . nodeId === blocked . rootNodeId ) {
547- ids . add ( String ( item . id ) ) ;
549+ items . set ( String ( item . id ) , item ) ;
548550 } else if (
549551 ( item . type === "user_message" || item . type === "notification" ) &&
550552 item . nodeId !== null &&
@@ -556,15 +558,26 @@ function limitRecoveryItemIds(
556558 }
557559 }
558560 }
559- for ( const item of firstInputs . values ( ) ) ids . add ( String ( item . id ) ) ;
560- return ids ;
561+ let startBytes = 0 ;
562+ const starts : Array < OrchestrationV2TurnItem > = [ ] ;
563+ for ( const item of firstInputs . values ( ) ) {
564+ if ( item . type !== "notification" ) continue ;
565+ const { detail : _detail , ...rest } = item ;
566+ const stub = { ...rest , title : null , summary : item . summary . slice ( 0 , 120 ) . trimEnd ( ) } ;
567+ startBytes += bytesOfJson ( stub ) ;
568+ starts . push ( stub ) ;
569+ }
570+ if ( startBytes <= maxEncodedBytes / 4 ) {
571+ for ( const stub of starts ) items . set ( String ( stub . id ) , stub ) ;
572+ }
573+ return items ;
561574}
562575
563576/** Retain small requests outside the visible window; later result pages still need them for attempt/run reducers. */
564577function retainedInterruptRequestTurnItems (
565578 projection : OrchestrationV2ThreadProjection ,
566579 visible : ReadonlyArray < OrchestrationV2ProjectedTurnItem > ,
567- limitItemIds : ReadonlySet < string > ,
580+ limitItems : ReadonlyMap < string , OrchestrationV2TurnItem > ,
568581) : OrchestrationV2TurnItem [ ] {
569582 const visibleLocalIds = new Set < string > ( ) ;
570583 for ( const row of visible ) {
@@ -576,14 +589,17 @@ function retainedInterruptRequestTurnItems(
576589 const pendingRequestIds = pendingRequestIdsOf ( projection ) ;
577590 const retained : OrchestrationV2TurnItem [ ] = [ ] ;
578591 for ( const item of projection . turnItems ) {
579- if ( ! isRetainedDependency ( item , pendingRequestIds ) && ! limitItemIds . has ( String ( item . id ) ) ) {
592+ if ( ! isRetainedDependency ( item , pendingRequestIds ) ) {
580593 continue ;
581594 }
582595 if ( visibleLocalIds . has ( String ( item . id ) ) ) {
583596 continue ;
584597 }
585598 retained . push ( item ) ;
586599 }
600+ for ( const [ id , item ] of limitItems ) {
601+ if ( ! visibleLocalIds . has ( id ) ) retained . push ( item ) ;
602+ }
587603 return retained ;
588604}
589605
@@ -694,15 +710,16 @@ export function buildBoundedThreadProjection(input: {
694710 } ;
695711 const latestLocalTurnOrdinal = computeLatestLocalTurnOrdinal ( input . projection . turnItems ) ;
696712
697- const limitItemIds = limitRecoveryItemIds ( controlProjection ) ;
713+ const limitItems = limitRecoveryItems ( controlProjection , policy . maxEncodedBytes ) ;
698714 const dependencyReserve = ( ( ) => {
699715 let reserve = 0 ;
700716 const pendingRequestIds = pendingRequestIdsOf ( controlProjection ) ;
701717 for ( const item of controlProjection . turnItems ) {
702- if ( isRetainedDependency ( item , pendingRequestIds ) || limitItemIds . has ( String ( item . id ) ) ) {
718+ if ( isRetainedDependency ( item , pendingRequestIds ) ) {
703719 reserve += bytesOfJson ( item ) ;
704720 }
705721 }
722+ for ( const item of limitItems . values ( ) ) reserve += bytesOfJson ( item ) ;
706723 return reserve ;
707724 } ) ( ) ;
708725 const controlBytes = bytesOfJson ( {
@@ -730,7 +747,7 @@ export function buildBoundedThreadProjection(input: {
730747 const dependencyTurnItems = retainedInterruptRequestTurnItems (
731748 controlProjection ,
732749 visibleTurnItems ,
733- limitItemIds ,
750+ limitItems ,
734751 ) ;
735752 const turnItemById = new Map < string , OrchestrationV2TurnItem > ( ) ;
736753 for ( const item of windowTurnItems ) {
0 commit comments