@@ -1160,9 +1160,11 @@ const indexDocumentCounts = new LRUCache<string, number>({
11601160async function estimateFilteredDocuments (
11611161 knowledgeBaseIds : string [ ] ,
11621162 filters : WorkspaceSearchFilters ,
1163- plan : SearchAccessPlan
1163+ plan : SearchAccessPlan ,
1164+ budget : SearchBudget | undefined
11641165) : Promise < number > {
1165- const [ row ] = await db . execute < { 'QUERY PLAN' : Array < { Plan : { 'Plan Rows' : number } } > } > ( sql `
1166+ const [ row ] = await runSearchQuery ( budget , 'permitted_documents' , ( executor ) =>
1167+ executor . execute < { 'QUERY PLAN' : Array < { Plan : { 'Plan Rows' : number } } > } > ( sql `
11661168 EXPLAIN (FORMAT JSON) SELECT 1 FROM ${ document }
11671169 WHERE ${ and (
11681170 inArray ( document . knowledgeBaseId , knowledgeBaseIds ) ,
@@ -1172,6 +1174,7 @@ async function estimateFilteredDocuments(
11721174 : undefined ,
11731175 filters . source ? planSourceCondition ( plan ) : undefined
11741176 ) } `)
1177+ )
11751178 return Number ( row ?. [ 'QUERY PLAN' ] ?. [ 0 ] ?. Plan ?. [ 'Plan Rows' ] ?? 0 )
11761179}
11771180
@@ -2421,11 +2424,21 @@ export async function retrieveKnowledgeSearch(
24212424 * source confined on the row, the date tested through the document — since a set that large
24222425 * holds most of the query's neighbours anyway. The planner's estimate decides which.
24232426 */
2427+ /** Planning only, so a short cap of its own: running past it answers as the wide window it may be. */
2428+ const estimateBudget = budgets . vector . capped ( VECTOR_PROBE_BUDGET_MS )
24242429 const enumerateFiltered =
24252430 accessPlan && ( params . filters ?. modifiedAfter || params . filters ?. source )
2426- ? ( await measureSearchStage ( 'permitted_documents' , ( ) =>
2427- estimateFilteredDocuments ( knowledgeBaseIds , params . filters ! , accessPlan )
2428- ) ) <= VECTOR_PROBE_DOCUMENT_LIMIT
2431+ ? await estimateFilteredDocuments (
2432+ knowledgeBaseIds ,
2433+ params . filters ,
2434+ accessPlan ,
2435+ estimateBudget
2436+ )
2437+ . then ( ( estimate ) => estimate <= VECTOR_PROBE_DOCUMENT_LIMIT )
2438+ . catch ( ( error ) => {
2439+ if ( ! estimateBudget . isTimeout ( error ) ) throw error
2440+ return false
2441+ } )
24292442 : false
24302443 const permitted =
24312444 access . kind === 'user' && params . accessProvider && ! params . filters ?. documentIds ?. length
0 commit comments