Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(search): an all-sources scope walks with the base overfetch despite float4 frequencies (#6728)

time-attack2fb2ec62e2ad
diff --git a/src/core/search/vector-statement.ts b/src/core/search/vector-statement.ts
index 95f9ad79fa..c8f0047055 100644
--- a/src/core/search/vector-statement.ts
+++ b/src/core/search/vector-statement.ts
@@ -196,7 +196,11 @@ export interface ScopeChunkCount {
sample_chunks: number;
}
-/** Share of pages the source scope holds, or undefined without a scope or statistics. */
+/**
+ * Share of pages the source scope holds, or undefined without a scope or statistics. The float4
+ * `most_common_freqs` of a scope covering every source sum to 0.99999997, which widened the walk by
+ * half (#6728), so the sum is rounded to SCOPE_SHARE_PRECISION and capped at 1.
+ */
export function sourceScopeShare(stats: PageSourceStats | undefined, opts?: SearchOpts): number | undefined {
const scope = scopeSourceIds(opts);
if (!scope || !stats) return undefined;
@@ -204,9 +208,12 @@ export function sourceScopeShare(stats: PageSourceStats | undefined, opts?: Sear
const freqs = (stats.freqs ?? []).map(Number);
const distinct = Number(stats.n_distinct) < 0 ? -Number(stats.n_distinct) * Number(stats.reltuples) : Number(stats.n_distinct);
const unlisted = Math.max(0, 1 - Number(stats.null_frac) - freqs.reduce((sum, freq) => sum + freq, 0)) / Math.max(1, distinct - sources.length);
- return scope.reduce((sum, id) => sum + (sources.includes(id) ? freqs[sources.indexOf(id)]! : unlisted), 0);
+ const share = scope.reduce((sum, id) => sum + (sources.includes(id) ? freqs[sources.indexOf(id)]! : unlisted), 0);
+ return Math.min(1, Math.round(share * SCOPE_SHARE_PRECISION) / SCOPE_SHARE_PRECISION);
}
+const SCOPE_SHARE_PRECISION = 1e6;
+
function scopeSourceIds(opts?: SearchOpts): string[] | undefined {
const scope = opts?.sourceIds?.length ? opts.sourceIds : opts?.sourceId ? [opts.sourceId] : undefined;
return scope && [...new Set(scope)];
diff --git a/test/search/vector-statement.test.ts b/test/search/vector-statement.test.ts
index d3f90c684f..3bf0ae20b7 100644
--- a/test/search/vector-statement.test.ts
+++ b/test/search/vector-statement.test.ts
@@ -170,6 +170,15 @@ describe('source scope strategy: walk overfetch, walk skip and scope scan', () =
expect(indexWalkOverfetch(INDEX_WALK_MIN_SCOPE_SHARE / 10)).toBe(50);
});
+ test('#6728 a scope covering every source walks with the base overfetch despite float4 frequency rounding', () => {
+ const float4: PageSourceStats = { sources: ['default', 'second'], freqs: [0.81954885, 0.18045112], n_distinct: 2, null_frac: 0, reltuples: 663 };
+ expect(sourceScopeShare(float4, { sourceIds: ['default', 'second'] })).toBe(1);
+ expect(indexWalkOverfetch(sourceScopeShare(float4, { sourceIds: ['default', 'second'] }))).toBe(2);
+ expect(sourceScopeShare(float4, { sourceIds: ['second'] })).toBeCloseTo(0.18045112, 6);
+ const over: PageSourceStats = { sources: ['a', 'b'], freqs: [0.6, 0.40000004], n_distinct: 2, null_frac: 0, reltuples: 100 };
+ expect(sourceScopeShare(over, { sourceIds: ['a', 'b'] })).toBe(1);
+ });
+
test('an unscoped or whole-brain scope keeps the walk byte-identical; a partial share only changes its LIMIT factor', () => {
for (const dialect of ['postgres', 'pglite'] as const) {
const unscoped = build({ sourceId: 'notes', excludePrivate: true }, dialect);