Skip to content

Commit 1051c20

Browse files
committed
improvement(knowledge): give a filter-first probe the budget its index-driven read can use
1 parent bcf0673 commit 1051c20

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎apps/sim/lib/knowledge/search/queries.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,9 @@ describe('filters on a resolved scope', () => {
21772177
expect(probes).toHaveLength(1)
21782178
/** Filter first, over the date index: never the reach count that reports a broad reader saturated. */
21792179
expect(probes[0].sql).not.toContain('WITH reach')
2180+
/** An index-driven probe earns its own budget: a window at the document limit fits inside it. */
2181+
const deadlines = statements().filter((query) => query.sql.includes('statement_timeout'))
2182+
expect(deadlines.at(-1)?.params[0]).toBe('1500')
21802183
expect(JSON.stringify(probes[0])).toContain('"type":"gte"')
21812184
})
21822185

‎apps/sim/lib/knowledge/search/queries.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ const VECTOR_PROBE_BUDGET_MS = 600
114114
* comparable cache pressure: the access predicate, evaluated once per document.
115115
*/
116116
const VECTOR_PROBE_MICROSECONDS_PER_DOCUMENT = 6
117+
/**
118+
* What a filter-first probe may spend: it reads the filtered documents off their own index and
119+
* tests each one's access, bounded by the same document limit, and measures around 2 µs per
120+
* document to enumerate plus the access test — a window at the limit fits with room. Its result
121+
* is ranked exactly, at a cost that is predictable where a walk through a mostly-excluded
122+
* neighbourhood is not.
123+
*/
124+
const FILTERED_PROBE_BUDGET_MS = 1500
117125
/**
118126
* Documents the probe enumerates before it concludes the permitted set is too large to rank
119127
* exactly. Derived so that reaching it is what spends the probe's budget, rather than a separate
@@ -1001,7 +1009,9 @@ async function probeVisibleDocuments(
10011009
stage: 'vector.probe' | 'permitted_documents',
10021010
shape: 'reach-first' | 'direct' = 'reach-first'
10031011
): Promise<ProbeOutcome> {
1004-
const probeBudget = budget?.capped(VECTOR_PROBE_BUDGET_MS)
1012+
const probeBudget = budget?.capped(
1013+
shape === 'direct' ? FILTERED_PROBE_BUDGET_MS : VECTOR_PROBE_BUDGET_MS
1014+
)
10051015
try {
10061016
const probed = await runSearchQuery(probeBudget, stage, (executor) =>
10071017
executor.execute<PermittedDocument & { saturated: boolean }>(

0 commit comments

Comments
 (0)