Skip to content

Commit 46e88eb

Browse files
committed
fix(knowledge): give a broad reader's wider walk half of what the leg has left
The rerank and hydration of whatever is found — the first walk's candidates at least — keep the rest, so a wider walk that runs out of its share can no longer take the leg's results with it.
1 parent da7afa8 commit 46e88eb

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,15 @@ describe('permitted-document planner', () => {
13721372
},
13731373
})
13741374
expect(rows.map((row) => row.id)).toEqual(['short-hit'])
1375+
/** The wider walk ran under a share of the leg's budget, not all of it. */
1376+
const timeouts = statements()
1377+
.filter((query) => query.sql.includes("'statement_timeout'"))
1378+
.map((query) =>
1379+
Number(query.params.find((param) => typeof param === 'string' && /^\d+$/.test(param)))
1380+
)
1381+
.filter((value) => Number.isFinite(value))
1382+
expect(Math.min(...timeouts)).toBeLessThanOrEqual(5000)
1383+
expect(Math.max(...timeouts)).toBeGreaterThan(5000)
13751384
})
13761385

13771386
it('walks an indexed source a bounded caller is a member of instead of ranking it exactly', async () => {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,11 @@ async function selectVectorResults(params: SearchParams): Promise<SearchResult[]
16661666
* budget the first walk's candidates stand.
16671667
*/
16681668
const walked = selected
1669+
/**
1670+
* The wider walk gets half of what the leg has left, so the rerank and hydration of
1671+
* whatever is found — the first walk's candidates at least — still have the rest.
1672+
*/
1673+
const wideBudget = params.budget?.capped(Math.floor(params.budget.remaining() / 2))
16691674
try {
16701675
const wider = await withVectorScanSettings(
16711676
(executor) =>
@@ -1680,14 +1685,16 @@ async function selectVectorResults(params: SearchParams): Promise<SearchResult[]
16801685
)}
16811686
ORDER BY ${candidateDistance} LIMIT ${candidateLimit}
16821687
`),
1683-
params.budget,
1688+
wideBudget,
16841689
'vector.candidate_search',
16851690
WIDE_WALK_SCAN_TUPLES
16861691
)
16871692
const seen = new Set(walked.map(({ id }) => id))
16881693
selected = [...walked, ...wider.filter(({ id }) => !seen.has(id))]
16891694
} catch (error) {
1690-
if (!params.budget?.isTimeout(error)) throw error
1695+
if (!wideBudget?.isTimeout(error)) throw error
1696+
/** Only the wider walk's share was spent; the leg's own deadline still governs. */
1697+
params.budget?.remaining()
16911698
selected = walked
16921699
}
16931700
}

0 commit comments

Comments
 (0)