@@ -1288,21 +1288,22 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
12881288 ...Array . from ( { length : 6 } , ( _ , i ) => doc ( t , `second_${ i } ` , `${ QUERY } w${ i + 5 } x` ) ) ,
12891289 ...wordsOf ( 52 , 11 ) . map ( ( text , i ) => doc ( t , `rest_${ String ( i ) . padStart ( 2 , '0' ) } ` , `w1x ${ text } ` ) ) ,
12901290 ] ;
1291- const over = await built ( t , tiers , { vectorIndexOverfetch : 10 } ) ;
1292- // Every passage but the two best in scope: 18 or more of the 2 × 10 candidates are left, 8 × topK
1291+ const over = await built ( t , tiers , { vectorIndexOverfetch : 20 } ) ;
1292+ // Every passage but the two best in scope: 38 or more of the 2 × 20 candidates are left, 8 × topK
12931293 // and more, so the candidates answer, without the exact scan.
12941294 from = over . sql . length ;
12951295 const allButBest = tiers . flatMap ( ( d ) => d . sourceRecordId ?? [ ] ) . filter ( ( id ) => ! id . startsWith ( 'best_' ) ) ;
12961296 const wide = await over . adapter . search ( QUERY , { source : t , topK : 2 , scope : [ { kind : 'record' , ids : allButBest } ] } ) ;
12971297 expect ( wide ) . toHaveLength ( 2 ) ;
12981298 for ( const hit of wide ) expect ( hit . sourceRecordId ) . toMatch ( / ^ s e c o n d _ / ) ;
12991299 expect ( vectorLegs ( over . sql , from ) ) . toEqual ( { approximate : 1 , exact : 0 } ) ;
1300- // Two passages in scope: at most 2 of the 20 candidates, fewer than 8 × topK, so the exact scan answers.
1300+ // Two passages in scope: at most 2 of the 40 candidates, fewer than 8 × topK, so the exact scan
1301+ // answers; a scope of records alone that narrow goes to it without the one-bit pass.
13011302 from = over . sql . length ;
13021303 const scoped = [ 'second_1' , 'second_4' ] ;
13031304 const narrow = await over . adapter . search ( QUERY , { source : t , topK : 2 , scope : [ { kind : 'record' , ids : scoped } ] } ) ;
13041305 expect ( narrow . map ( ( h ) => h . sourceRecordId ) . sort ( ) ) . toEqual ( scoped ) ;
1305- expect ( vectorLegs ( over . sql , from ) ) . toEqual ( { approximate : 1 , exact : 1 } ) ;
1306+ expect ( vectorLegs ( over . sql , from ) ) . toEqual ( { approximate : 0 , exact : 1 } ) ;
13061307 } ) ;
13071308
13081309 it ( 'a leg under 8 × topK in scope tries 10 × the candidates once before the exact scan, unless its pool is too thin or its scope is of records alone' , async ( ) => {
@@ -1353,16 +1354,59 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
13531354 expect ( vectorLegs ( sql , from ) ) . toEqual ( { approximate : 1 , exact : 1 } ) ;
13541355
13551356 // The 'wide' passages as a scope of records alone: the record index serves the exact scan from the
1356- // rows in scope, so there is no second pass, whatever the first pool.
1357+ // rows in scope, so there is no second pass, and at 8 candidates a hit no first one either: no share
1358+ // of the passages lets the 16 candidates be expected to keep 1.25 × the floor of 16.
13571359 const records = corpus ( s )
13581360 . filter ( ( d ) => ( d . metadata ?. tags as string [ ] | undefined ) ?. includes ( 'wide' ) )
13591361 . flatMap ( ( d ) => d . sourceRecordId ?? [ ] ) ;
13601362 ( { hits, from } = await search ( [ { kind : 'record' , ids : records } ] ) ) ;
13611363 expect ( ids ( hits ) ) . toEqual ( [ 'best_1' , 'best_2' ] ) ;
1362- expect ( vectorLegs ( sql , from ) ) . toEqual ( { approximate : 1 , exact : 1 } ) ;
1364+ expect ( vectorLegs ( sql , from ) ) . toEqual ( { approximate : 0 , exact : 1 } ) ;
13631365 // Two builds of 400 passages on each store kind: past vitest's 5 s default on a loaded CI box.
13641366 } , 30_000 ) ;
13651367
1368+ it ( 'a scope of record terms alone that keeps under 25% of the passages goes straight to the exact scan; a wider one, or one with a parent term, takes the approximate pass' , async ( ) => {
1369+ const QUERY = 'w1x w2x w3x' ;
1370+ // 400 passages: records 000…039 hold five passages each, 040…239 one each.
1371+ const corpus = ( s : KnowledgeSource ) =>
1372+ wordsOf ( 240 , 13 ) . map ( ( text , i ) => {
1373+ const n = String ( i ) . padStart ( 3 , '0' ) ;
1374+ const d = doc ( s , n , text ) ;
1375+ if ( i >= 40 ) return d ;
1376+ return { ...d , chunks : [ 0 , 1 , 2 , 3 , 4 ] . map ( ( index ) => ( { id : `${ d . id } #${ index } ` , documentId : d . id , index, content : `${ text } wz${ index } x` } ) ) } ;
1377+ } ) ;
1378+ const s = source ( 'ann_record_cut' ) ;
1379+ // topK 2 and the default 40 candidates a hit: 80 candidates, a floor of 16. The first pass is taken
1380+ // when it is expected to keep 1.25 × that floor, 20 of the 80: 25% of the passages, 100 of 400.
1381+ const { adapter, sql, args } = await built ( s , corpus ( s ) ) ;
1382+ const t = source ( 'ann_record_cut_exact' ) ;
1383+ const exact = await built ( t , corpus ( t ) , { vectorIndexThreshold : Infinity } ) ;
1384+ const records = ( from : number , to : number ) => Array . from ( { length : to - from } , ( _ , i ) => String ( from + i ) . padStart ( 3 , '0' ) ) ;
1385+ const search = async ( scope : NonNullable < Parameters < TursoKnowledgeAdapter [ 'search' ] > [ 1 ] [ 'scope' ] > ) => {
1386+ const from = sql . length ;
1387+ const hits = await adapter . search ( QUERY , { source : s , topK : 2 , scope } ) ;
1388+ const want = await exact . adapter . search ( QUERY , { source : t , topK : 2 , scope } ) ;
1389+ expect ( hits . map ( ( h ) => h . chunkId ) ) . toEqual ( want . map ( ( h ) => h . chunkId ) ) ;
1390+ return vectorLegs ( sql , from ) ;
1391+ } ;
1392+
1393+ // 99 passages in scope (records 141…239), under the cut: the exact scan alone, no one-bit pass.
1394+ expect ( await search ( [ { kind : 'record' , ids : records ( 141 , 240 ) } ] ) ) . toEqual ( { approximate : 0 , exact : 1 } ) ;
1395+ // 100 (records 140…239): the first pass, which keeps about 20 of its 80 candidates, and answers.
1396+ expect ( await search ( [ { kind : 'record' , ids : records ( 140 , 240 ) } ] ) ) . toEqual ( { approximate : 1 , exact : 0 } ) ;
1397+ // The passages in scope are counted, not the ids: 20 records holding 100 passages take the first pass.
1398+ expect ( await search ( [ { kind : 'record' , ids : records ( 0 , 20 ) } ] ) ) . toEqual ( { approximate : 1 , exact : 0 } ) ;
1399+ // Two terms of 40 records each, one id in both: 80 passages, under the cut.
1400+ expect ( await search ( [ { kind : 'record' , ids : records ( 100 , 140 ) } , { kind : 'record' , ids : records ( 139 , 180 ) } ] ) ) . toEqual ( { approximate : 0 , exact : 1 } ) ;
1401+ // The narrow records beside a parent (metadata) term: the record index does not serve that scan, so
1402+ // the approximate passes run as before (a thin first pool, then the wider pass, which answers).
1403+ expect ( await search ( [ { kind : 'record' , ids : records ( 190 , 240 ) } , { kind : 'metadata' , field : 'status' , ids : [ 'none' ] } ] ) ) . toEqual ( { approximate : 2 , exact : 0 } ) ;
1404+ // The count of the passages in scope binds the scope's ids alone, each once.
1405+ const counts = sql . flatMap ( ( q , i ) => ( ! q . includes ( 'vec AS (' ) && q . includes ( 'source_record_id IN (SELECT value FROM json_each(?1))' ) ? [ args [ i ] ] : [ ] ) ) ;
1406+ expect ( counts . map ( ( bound ) => bound . length ) ) . toEqual ( [ 1 , 1 , 1 , 1 ] ) ;
1407+ expect ( JSON . parse ( String ( counts [ 3 ] [ 0 ] ) ) ) . toEqual ( records ( 100 , 180 ) ) ;
1408+ } , 30_000 ) ;
1409+
13661410 it ( 'keeps the index in step with every write and removal; a generation swap and dropSource drop it whole' , async ( ) => {
13671411 const s = source ( 'ann_life' ) ;
13681412 const { adapter, client, sql } = await built ( s , corpus ( s , 60 ) ) ;
@@ -1492,13 +1536,15 @@ for (const kind of ['memory', 'file'] as StoreKind[]) {
14921536 await adapter . completeBuild ( ctx ( s ) ) ;
14931537 expect ( keywordTerms ( '?! …' ) ) . toEqual ( [ ] ) ;
14941538 const from = watched . sql . length ;
1495- // Answered through the index; then a scope of its two hits, under the floor, runs both statements.
1539+ // Answered through the index; then a scope of its two records beside a parent term, under the
1540+ // floor, runs both approximate passes and the exact scan, and the two records alone the exact scan.
14961541 const hits = await adapter . search ( '?! …' , { source : s , topK : 2 } ) ;
14971542 expect ( hits ) . toHaveLength ( 2 ) ;
14981543 const ids = hits . flatMap ( ( h ) => h . sourceRecordId ?? [ ] ) ;
1544+ expect ( await adapter . search ( '?! …' , { source : s , topK : 2 , scope : [ { kind : 'record' , ids } , { kind : 'metadata' , field : 'status' , ids : [ 'none' ] } ] } ) ) . toHaveLength ( 2 ) ;
14991545 expect ( await adapter . search ( '?! …' , { source : s , topK : 2 , scope : [ { kind : 'record' , ids } ] } ) ) . toHaveLength ( 2 ) ;
15001546 const statements = vectorStatements ( watched , from ) ;
1501- expect ( statements . map ( ( st ) => st . approximate ) ) . toEqual ( [ true , true , false ] ) ;
1547+ expect ( statements . map ( ( st ) => st . approximate ) ) . toEqual ( [ true , true , true , false , false ] ) ;
15021548 for ( const st of statements ) expect ( st ) . toMatchObject ( { reads : st . bound } ) ;
15031549 } ) ;
15041550
0 commit comments