@@ -146,6 +146,8 @@ const DEFAULT_THREAD_LIST_LIMIT = 10;
146146const DEFAULT_THREAD_READ_LIMIT = 10 ;
147147const DEFAULT_THREAD_RUN_LIMIT = 10 ;
148148const DEFAULT_THREAD_ITEM_MAX_CHARS = 500 ;
149+ /** Claude Code rejects MCP results over 25k tokens; CJK text runs near one token per char. */
150+ const THREAD_READ_RESULT_MAX_BYTES = 30_000 ;
149151
150152interface ResolvedTarget {
151153 readonly modelSelection : ModelSelection ;
@@ -631,14 +633,12 @@ function taskResultTransfer(
631633
632634function pageIncludesTerminalTaskResult ( input : {
633635 readonly parentThreadId : ThreadId ;
634- readonly page : ReadonlyArray < OrchestrationV2ThreadProjection [ "visibleTurnItems" ] [ number ] > ;
636+ readonly items : ReadonlyArray < OrchestratorMcpThreadTimelineItem > ;
635637 readonly task : OrchestrationV2Subagent ;
636638 readonly target : Pick <
637639 OrchestrationV2ThreadProjection ,
638640 "thread" | "runs" | "contextTransfers" | "messages" | "turnItems" | "providerTurns"
639641 > ;
640- readonly maxChars : number ;
641- readonly textOffset : number ;
642642} ) : boolean {
643643 const transfer = taskResultTransfer ( input . parentThreadId , input . target , input . task ) ;
644644 if ( transfer === undefined ) return false ;
@@ -651,21 +651,14 @@ function pageIncludesTerminalTaskResult(input: {
651651 const result = subagentResultForRun ( input . target , run ) ;
652652 if ( result . messageId === null && result . turnItemId === null ) return false ;
653653
654- return input . page . some ( ( row ) => {
655- if ( row . sourceThreadId !== input . target . thread . id ) return false ;
654+ return input . items . some ( ( item ) => {
655+ if ( item . sourceThreadId !== input . target . thread . id ) return false ;
656656 const matchesResult =
657- ( result . turnItemId !== null && row . sourceItemId === result . turnItemId ) ||
657+ ( result . turnItemId !== null && item . itemId === result . turnItemId ) ||
658658 ( result . messageId !== null &&
659- row . item . type === "assistant_message" &&
660- row . item . messageId === result . messageId ) ;
661- if ( ! matchesResult ) return false ;
662-
663- const text = turnItemText ( row . item ) ;
664- return (
665- text !== null &&
666- ( input . textOffset === 0 || input . textOffset < text . length ) &&
667- text . length <= textPageEnd ( text , input . textOffset , input . maxChars )
668- ) ;
659+ item . type === "assistant_message" &&
660+ item . messageId === result . messageId ) ;
661+ return matchesResult && item . text !== null && ! item . textTruncated ;
669662 } ) ;
670663}
671664
@@ -1098,6 +1091,72 @@ function timelineItem(input: {
10981091 } ;
10991092}
11001093
1094+ const jsonBytes = ( value : unknown ) => Buffer . byteLength ( JSON . stringify ( value ) , "utf8" ) ;
1095+
1096+ /** The longest prefix length in [low, high] that `fits`, or `low - 1`. */
1097+ function longestFitting ( low : number , high : number , fits : ( length : number ) => boolean ) : number {
1098+ let best = low - 1 ;
1099+ while ( low <= high ) {
1100+ const mid = Math . floor ( ( low + high ) / 2 ) ;
1101+ if ( fits ( mid ) ) {
1102+ best = mid ;
1103+ low = mid + 1 ;
1104+ } else {
1105+ high = mid - 1 ;
1106+ }
1107+ }
1108+ return best ;
1109+ }
1110+
1111+ /** The item with its text cut, and an oversized title too, until its JSON fits `maxBytes`. */
1112+ function timelineItemWithin (
1113+ input : Parameters < typeof timelineItem > [ 0 ] ,
1114+ maxBytes : number ,
1115+ ) : OrchestratorMcpThreadTimelineItem {
1116+ const longestText = ( title : string | null ) => {
1117+ const withText = ( maxChars : number ) => ( { ...timelineItem ( { ...input , maxChars } ) , title } ) ;
1118+ const chars = longestFitting ( 2 , input . maxChars - 1 , ( n ) => jsonBytes ( withText ( n ) ) <= maxBytes ) ;
1119+ return withText ( Math . max ( chars , 1 ) ) ;
1120+ } ;
1121+ const item = longestText ( input . row . item . title ) ;
1122+ const title = item . title ;
1123+ if ( title === null || jsonBytes ( item ) <= maxBytes ) return item ;
1124+ // Half the budget for the title keeps `textOffset` pages of the text useful.
1125+ const cutTitle = ( chars : number ) =>
1126+ title . slice ( 0 , chars === 0 ? 0 : textPageEnd ( title , 0 , chars ) ) ;
1127+ const titleChars = longestFitting (
1128+ 1 ,
1129+ title . length - 1 ,
1130+ ( n ) => jsonBytes ( { ...item , title : cutTitle ( n ) } ) <= maxBytes / 2 ,
1131+ ) ;
1132+ return longestText ( cutTitle ( Math . max ( titleChars , 0 ) ) ) ;
1133+ }
1134+
1135+ /** Leading items whose result fits THREAD_READ_RESULT_MAX_BYTES; never empty for a non-empty page. */
1136+ function timelineItemsWithinBudget (
1137+ envelope : Omit < OrchestratorMcpThreadReadResult , "items" > ,
1138+ inputs : ReadonlyArray < Parameters < typeof timelineItem > [ 0 ] > ,
1139+ ) : Array < OrchestratorMcpThreadTimelineItem > {
1140+ // An oversized envelope (say, a huge thread title) must not starve items to one char per read.
1141+ const budget = Math . max (
1142+ THREAD_READ_RESULT_MAX_BYTES - jsonBytes ( { ...envelope , items : [ ] } ) ,
1143+ THREAD_READ_RESULT_MAX_BYTES / 2 ,
1144+ ) ;
1145+ const items : Array < OrchestratorMcpThreadTimelineItem > = [ ] ;
1146+ let bytes = 0 ;
1147+ for ( const input of inputs ) {
1148+ const item = timelineItem ( input ) ;
1149+ bytes += jsonBytes ( item ) + ( items . length === 0 ? 0 : 1 ) ;
1150+ if ( bytes <= budget ) {
1151+ items . push ( item ) ;
1152+ continue ;
1153+ }
1154+ if ( items . length === 0 ) items . push ( timelineItemWithin ( input , budget ) ) ;
1155+ break ;
1156+ }
1157+ return items ;
1158+ }
1159+
11011160const make = Effect . gen ( function * ( ) {
11021161 const random = yield * SecureRandom . SecureRandom ;
11031162 const threadManagement = yield * ThreadManagementService . ThreadManagementService ;
@@ -3093,6 +3152,27 @@ const make = Effect.gen(function* () {
30933152 const returnedRunIds = new Set (
30943153 page . filter ( ( row ) => row . sourceThreadId === target . thread . id ) . map ( ( row ) => row . item . runId ) ,
30953154 ) ;
3155+ const thread = threadDetail (
3156+ scope ,
3157+ target ,
3158+ timeline . totalItems ,
3159+ shell ,
3160+ yield * Clock . currentTimeMillis ,
3161+ input . afterPosition === undefined && input . itemId === undefined ,
3162+ ) ;
3163+ const recentRuns = target . runs
3164+ . toSorted ( ( left , right ) => right . ordinal - left . ordinal )
3165+ . slice ( 0 , input . runLimit ?? DEFAULT_THREAD_RUN_LIMIT )
3166+ . map ( ( run ) => threadRun ( target , run ) ) ;
3167+ const items = timelineItemsWithinBudget (
3168+ { thread, recentRuns, nextPosition : page . at ( - 1 ) ?. position ?? null , hasMore : false } ,
3169+ page . map ( ( row ) => ( {
3170+ row,
3171+ maxChars,
3172+ messagesByThreadId,
3173+ ...( input . itemId === undefined ? { } : { textOffset : input . textOffset ?? 0 } ) ,
3174+ } ) ) ,
3175+ ) ;
30963176 const tasks =
30973177 parent === undefined
30983178 ? [ ]
@@ -3107,7 +3187,9 @@ const make = Effect.gen(function* () {
31073187 target ,
31083188 returnedRunIds ,
31093189 ) ;
3110- if ( parent !== undefined && scope . thread !== undefined && tasks . length > 0 ) {
3190+ // A read past the end of an item's text shows the agent nothing to acknowledge.
3191+ const readPastEnd = ( input . textOffset ?? 0 ) > 0 && items . every ( ( item ) => ! item . text ) ;
3192+ if ( parent !== undefined && scope . thread !== undefined && tasks . length > 0 && ! readPastEnd ) {
31113193 for ( const task of tasks ) {
31123194 const transfer = taskResultTransfer ( parent . thread . id , target , task ) ;
31133195 const resultRunId = transfer ?. sourcePoint . runId ?? delegatedTaskRun ( target , task ) ?. id ;
@@ -3123,11 +3205,9 @@ const make = Effect.gen(function* () {
31233205 if (
31243206 pageIncludesTerminalTaskResult ( {
31253207 parentThreadId : parent . thread . id ,
3126- page ,
3208+ items ,
31273209 task,
31283210 target : { ...target , ...resultRecords } ,
3129- maxChars,
3130- textOffset : input . itemId === undefined ? 0 : ( input . textOffset ?? 0 ) ,
31313211 } )
31323212 ) {
31333213 yield * readTask (
@@ -3141,28 +3221,11 @@ const make = Effect.gen(function* () {
31413221 }
31423222 }
31433223 return {
3144- thread : threadDetail (
3145- scope ,
3146- target ,
3147- timeline . totalItems ,
3148- shell ,
3149- yield * Clock . currentTimeMillis ,
3150- input . afterPosition === undefined && input . itemId === undefined ,
3151- ) ,
3152- recentRuns : target . runs
3153- . toSorted ( ( left , right ) => right . ordinal - left . ordinal )
3154- . slice ( 0 , input . runLimit ?? DEFAULT_THREAD_RUN_LIMIT )
3155- . map ( ( run ) => threadRun ( target , run ) ) ,
3156- items : page . map ( ( row ) =>
3157- timelineItem ( {
3158- row,
3159- maxChars,
3160- messagesByThreadId,
3161- ...( input . itemId === undefined ? { } : { textOffset : input . textOffset ?? 0 } ) ,
3162- } ) ,
3163- ) ,
3164- nextPosition : page . at ( - 1 ) ?. position ?? null ,
3165- hasMore : timeline . hasMore ,
3224+ thread,
3225+ recentRuns,
3226+ items,
3227+ nextPosition : items . at ( - 1 ) ?. position ?? null ,
3228+ hasMore : timeline . hasMore || items . length < page . length ,
31663229 } satisfies OrchestratorMcpThreadReadResult ;
31673230 } ) ,
31683231 sendToThread : Effect . fn ( "OrchestratorMcpService.sendToThread" ) ( function * ( scope , input ) {
0 commit comments