@@ -5,6 +5,7 @@ import type {
55 ThreadId ,
66 TurnItemId ,
77} from "@t3tools/contracts" ;
8+ import { usageLimitBlockedRun } from "@t3tools/shared/orchestrationV2ThreadError" ;
89
910/**
1011 * Every window stops at the byte budget. A window that holds a user turn also
@@ -524,10 +525,46 @@ function isRetainedDependency(
524525 ) ;
525526}
526527
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 (
532+ projection : Pick < OrchestrationV2ThreadProjection , "runs" | "turnItems" > ,
533+ ) : ReadonlySet < string > {
534+ const ids = new Set < string > ( ) ;
535+ const blocked = usageLimitBlockedRun ( projection . runs , projection . turnItems , null ) ;
536+ if ( blocked === null ) return ids ;
537+ const laterRoots = new Set < string > ( ) ;
538+ for ( const run of projection . runs ) {
539+ if ( run . ordinal > blocked . ordinal && run . rootNodeId !== null ) {
540+ laterRoots . add ( String ( run . rootNodeId ) ) ;
541+ }
542+ }
543+ if ( laterRoots . size === 0 ) return ids ;
544+ const firstInputs = new Map < string , OrchestrationV2TurnItem > ( ) ;
545+ for ( const item of projection . turnItems ) {
546+ if ( item . type === "error" && item . runId === blocked . id && item . nodeId === blocked . rootNodeId ) {
547+ ids . add ( String ( item . id ) ) ;
548+ } else if (
549+ ( item . type === "user_message" || item . type === "notification" ) &&
550+ item . nodeId !== null &&
551+ laterRoots . has ( String ( item . nodeId ) )
552+ ) {
553+ const first = firstInputs . get ( String ( item . nodeId ) ) ;
554+ if ( first === undefined || item . ordinal < first . ordinal ) {
555+ firstInputs . set ( String ( item . nodeId ) , item ) ;
556+ }
557+ }
558+ }
559+ for ( const item of firstInputs . values ( ) ) ids . add ( String ( item . id ) ) ;
560+ return ids ;
561+ }
562+
527563/** Retain small requests outside the visible window; later result pages still need them for attempt/run reducers. */
528564function retainedInterruptRequestTurnItems (
529565 projection : OrchestrationV2ThreadProjection ,
530566 visible : ReadonlyArray < OrchestrationV2ProjectedTurnItem > ,
567+ limitItemIds : ReadonlySet < string > ,
531568) : OrchestrationV2TurnItem [ ] {
532569 const visibleLocalIds = new Set < string > ( ) ;
533570 for ( const row of visible ) {
@@ -539,7 +576,7 @@ function retainedInterruptRequestTurnItems(
539576 const pendingRequestIds = pendingRequestIdsOf ( projection ) ;
540577 const retained : OrchestrationV2TurnItem [ ] = [ ] ;
541578 for ( const item of projection . turnItems ) {
542- if ( ! isRetainedDependency ( item , pendingRequestIds ) ) {
579+ if ( ! isRetainedDependency ( item , pendingRequestIds ) && ! limitItemIds . has ( String ( item . id ) ) ) {
543580 continue ;
544581 }
545582 if ( visibleLocalIds . has ( String ( item . id ) ) ) {
@@ -657,11 +694,12 @@ export function buildBoundedThreadProjection(input: {
657694 } ;
658695 const latestLocalTurnOrdinal = computeLatestLocalTurnOrdinal ( input . projection . turnItems ) ;
659696
697+ const limitItemIds = limitRecoveryItemIds ( controlProjection ) ;
660698 const dependencyReserve = ( ( ) => {
661699 let reserve = 0 ;
662700 const pendingRequestIds = pendingRequestIdsOf ( controlProjection ) ;
663701 for ( const item of controlProjection . turnItems ) {
664- if ( isRetainedDependency ( item , pendingRequestIds ) ) {
702+ if ( isRetainedDependency ( item , pendingRequestIds ) || limitItemIds . has ( String ( item . id ) ) ) {
665703 reserve += bytesOfJson ( item ) ;
666704 }
667705 }
@@ -692,6 +730,7 @@ export function buildBoundedThreadProjection(input: {
692730 const dependencyTurnItems = retainedInterruptRequestTurnItems (
693731 controlProjection ,
694732 visibleTurnItems ,
733+ limitItemIds ,
695734 ) ;
696735 const turnItemById = new Map < string , OrchestrationV2TurnItem > ( ) ;
697736 for ( const item of windowTurnItems ) {
0 commit comments