@@ -1193,6 +1193,89 @@ describe('authorized live retrieval', () => {
11931193 } )
11941194 ) . rejects . toBe ( 'user_stop:test' )
11951195 } )
1196+ describe ( 'read window' , ( ) => {
1197+ const content = 'x' . repeat ( 30_000 )
1198+ async function readWindow ( extra : Record < string , unknown > , text = content ) {
1199+ const [ result ] = ( await searchLiveKnowledge . execute ( { principal, input } ) ) . results
1200+ if ( ! result ) throw new Error ( 'Expected the search fixture to return a result' )
1201+ mocks . read . mockResolvedValue ( { ...document , content : text } )
1202+ const read = await readLiveDocument . execute ( {
1203+ principal,
1204+ input : {
1205+ workspaceId : 'workspace' ,
1206+ documentId : result . documentId ,
1207+ limit : 3 ,
1208+ resultSecretRegistry : new ResolvedSecretTraceRegistry ( [ ] ) ,
1209+ ...extra ,
1210+ } ,
1211+ } )
1212+ const [ chunk ] = read . chunks
1213+ if ( ! chunk ) throw new Error ( 'Expected the read to return a window' )
1214+ return {
1215+ length : chunk . content . length ,
1216+ encoded : JSON . stringify ( chunk . content ) . length - 2 ,
1217+ next : read . next ,
1218+ }
1219+ }
1220+
1221+ const scaled = ( documentsInCall : number ) => ( { scaledWindow : { documentsInCall } } )
1222+
1223+ it ( 'returns a proportionally longer window when a scaling caller asks for more chunks' , async ( ) => {
1224+ const { length, next } = await readWindow ( { limit : 8 , ...scaled ( 1 ) } )
1225+ expect ( length ) . toBe ( 21_333 )
1226+ expect ( next ) . toEqual ( { startChunkIndex : 0 , startOffset : 21_333 } )
1227+ } )
1228+
1229+ it ( 'continues a longer window from where the previous one ended' , async ( ) => {
1230+ const { length, next } = await readWindow ( { limit : 8 , ...scaled ( 1 ) , startOffset : 21_333 } )
1231+ expect ( length ) . toBe ( 30_000 - 21_333 )
1232+ expect ( next ) . toBeNull ( )
1233+ } )
1234+
1235+ it . each ( [
1236+ [ 5 , 1 , 13_333 ] ,
1237+ [ 8 , 2 , 20_000 ] ,
1238+ [ 8 , 3 , 13_333 ] ,
1239+ [ 8 , 4 , 10_000 ] ,
1240+ ] ) (
1241+ 'shares one call budget: limit %i across %i documents reads %i characters each' ,
1242+ async ( limit , documentsInCall , expected ) => {
1243+ expect ( ( await readWindow ( { limit, ...scaled ( documentsInCall ) } ) ) . length ) . toBe ( expected )
1244+ }
1245+ )
1246+
1247+ it . each ( [
1248+ [ 1 , 21_333 ] ,
1249+ [ 2 , 20_000 ] ,
1250+ ] ) (
1251+ 'keeps a quote-heavy window within its share once JSON-encoded, across %i documents' ,
1252+ async ( documentsInCall , share ) => {
1253+ const quotes = '"' . repeat ( 30_000 )
1254+ const { length, encoded, next } = await readWindow (
1255+ { limit : 8 , ...scaled ( documentsInCall ) } ,
1256+ quotes
1257+ )
1258+ expect ( encoded ) . toBeLessThanOrEqual ( share )
1259+ expect ( length ) . toBeGreaterThanOrEqual ( 8_000 )
1260+ expect ( next ) . toEqual ( { startChunkIndex : 0 , startOffset : length } )
1261+ }
1262+ )
1263+
1264+ it ( 'keeps the standard window for quote-heavy text at the default limit' , async ( ) => {
1265+ const { length } = await readWindow ( { limit : 3 , ...scaled ( 1 ) } , '"' . repeat ( 30_000 ) )
1266+ expect ( length ) . toBe ( 8_000 )
1267+ } )
1268+
1269+ it . each ( [
1270+ [ 'at the default limit' , { limit : 3 , ...scaled ( 1 ) } ] ,
1271+ [ 'below the default limit' , { limit : 1 , ...scaled ( 1 ) } ] ,
1272+ [ 'when the call budget is spread over five documents' , { limit : 8 , ...scaled ( 5 ) } ] ,
1273+ [ 'when the call budget is spread over ten documents' , { limit : 8 , ...scaled ( 10 ) } ] ,
1274+ [ 'for a caller that does not scale' , { limit : 8 } ] ,
1275+ ] ) ( 'keeps the standard window %s' , async ( _ , extra ) => {
1276+ expect ( ( await readWindow ( extra ) ) . length ) . toBe ( 8_000 )
1277+ } )
1278+ } )
11961279 it ( 'refuses a chunk index other than zero before reading the document from its provider' , async ( ) => {
11971280 const [ result ] = ( await searchLiveKnowledge . execute ( { principal, input } ) ) . results
11981281 if ( ! result ) throw new Error ( 'Expected the search fixture to return a result' )
0 commit comments