@@ -66,6 +66,26 @@ function makeRow(
6666 } ;
6767}
6868
69+ function makeTurnStart (
70+ index : number ,
71+ createdBy : "user" | "agent" ,
72+ ) : OrchestrationV2ProjectedTurnItem {
73+ const row = makeRow ( index ) ;
74+ return {
75+ ...row ,
76+ item : {
77+ ...row . item ,
78+ type : "user_message" ,
79+ createdBy,
80+ creationSource : "provider" ,
81+ inputIntent : "turn_start" ,
82+ messageId : MessageId . make ( `prompt-${ index } ` ) ,
83+ text : `Prompt ${ index } ` ,
84+ attachments : [ ] ,
85+ } ,
86+ } as OrchestrationV2ProjectedTurnItem ;
87+ }
88+
6989function interruptItem (
7090 id : string ,
7191 type : "run_interrupt_request" | "run_interrupt_result" ,
@@ -450,6 +470,65 @@ describe("threadHistoryPaging", () => {
450470 }
451471 } ) ;
452472
473+ it . each ( [
474+ {
475+ name : "item ceiling" ,
476+ items : Array . from ( { length : 5 } , ( _ , index ) => makeRow ( index ) ) ,
477+ policy : { maxItems : 3 , maxEncodedBytes : 1_000 } ,
478+ admitted : 3 ,
479+ measured : 3 ,
480+ } ,
481+ {
482+ name : "user turn ceiling" ,
483+ items : [ makeRow ( 0 ) , makeTurnStart ( 1 , "user" ) , makeRow ( 2 ) ] ,
484+ policy : { maxUserTurns : 1 , maxItems : 100 , maxEncodedBytes : 1_000 } ,
485+ admitted : 2 ,
486+ measured : 2 ,
487+ } ,
488+ {
489+ name : "raw turn ceiling" ,
490+ items : [
491+ makeTurnStart ( 0 , "user" ) ,
492+ ...Array . from ( { length : 151 } , ( _ , index ) => makeTurnStart ( index + 1 , "agent" ) ) ,
493+ ] ,
494+ policy : { maxUserTurns : 10 , maxItems : 1_000 , maxEncodedBytes : 1_000_000 } ,
495+ admitted : 150 ,
496+ measured : 150 ,
497+ } ,
498+ {
499+ name : "byte budget, which still admits an exact fit" ,
500+ items : Array . from ( { length : 5 } , ( _ , index ) => makeRow ( index ) ) ,
501+ policy : { maxItems : 100 , maxEncodedBytes : 20 } ,
502+ admitted : 2 ,
503+ measured : 3 ,
504+ } ,
505+ {
506+ name : "byte budget, which still admits an oversized first row" ,
507+ items : Array . from ( { length : 5 } , ( _ , index ) => makeRow ( index ) ) ,
508+ policy : { maxItems : 100 , maxEncodedBytes : 5 } ,
509+ admitted : 1 ,
510+ measured : 2 ,
511+ } ,
512+ ] ) ( "measures only the rows needed to stop at the $name" , ( testCase ) => {
513+ const measured : OrchestrationV2ProjectedTurnItem [ ] = [ ] ;
514+ const page = selectRecentTimelineWindow ( {
515+ items : testCase . items ,
516+ snapshotSequence : 1 ,
517+ policy : testCase . policy ,
518+ rowEncodedBytes : ( row ) => {
519+ measured . push ( row ) ;
520+ return 10 ;
521+ } ,
522+ } ) ;
523+
524+ expect ( page . items . map ( ( row ) => row . sourceItemId ) ) . toEqual (
525+ testCase . items . slice ( - testCase . admitted ) . map ( ( row ) => row . sourceItemId ) ,
526+ ) ;
527+ expect ( measured ) . toHaveLength ( testCase . measured ) ;
528+ expect ( page . hasMoreHistory ) . toBe ( true ) ;
529+ expect ( decodeThreadHistoryCursor ( page . nextCursor ! ) . si ) . toBe ( page . items [ 0 ] ?. sourceItemId ) ;
530+ } ) ;
531+
453532 it ( "recovers identity-miss cursors when position is past a shrunken timeline" , ( ) => {
454533 const items = Array . from ( { length : 5 } , ( _ , index ) => makeRow ( index ) ) ;
455534 const cursor = encodeThreadHistoryCursor ( {
0 commit comments