Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(search): keep a keyword-mode count that lands while rows still load

garrytan8800c3192acb
diff --git a/src/core/search/keyword-paging.ts b/src/core/search/keyword-paging.ts
index a49ceeab4e..958b9b415e 100644
--- a/src/core/search/keyword-paging.ts
+++ b/src/core/search/keyword-paging.ts
@@ -179,10 +179,8 @@ export interface KeywordPage {
*/
export async function readKeywordPage(engine: BrainEngine, p: Record<string, unknown>, opts: SearchOpts, paging: SearchPaging, limit: number): Promise<KeywordPage> {
const queryText = String(p.query);
- const [probe, count] = await Promise.all([
- engine.searchKeywordPages(queryText, opts, { limit: limit + 1, ...(paging.cursor ? { after: paging.cursor } : { offset: paging.offset }) }),
- countKeywordMatches(engine, queryText, opts),
- ]);
+ const rowsRead = engine.searchKeywordPages(queryText, opts, { limit: limit + 1, ...(paging.cursor ? { after: paging.cursor } : { offset: paging.offset }) });
+ const [probe, count] = await Promise.all([rowsRead, countKeywordMatches(engine, queryText, opts, KEYWORD_COUNT_TIMEOUT_MS, rowsRead)]);
const rows = probe.slice(0, limit);
const start = paging.cursor ? paging.cursor.shown : paging.offset;
const truncated = probe.length > limit;
diff --git a/test/search-keyword-paging.test.ts b/test/search-keyword-paging.test.ts
index 0e850cd908..ce52476761 100644
--- a/test/search-keyword-paging.test.ts
+++ b/test/search-keyword-paging.test.ts
@@ -26,7 +26,7 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import type { BrainEngine } from '../src/core/engine.ts';
import { dispatchToolCall, type ToolResult } from '../src/mcp/dispatch.ts';
import { errorCodeRow } from '../src/core/error-docs.ts';
-import { countKeywordMatches, searchCountLine } from '../src/core/search/keyword-paging.ts';
+import { countKeywordMatches, readKeywordPage, searchCountLine } from '../src/core/search/keyword-paging.ts';
import { safeChunksFilter } from '../src/core/search/safe-chunks.ts';
import { withEnv } from './helpers/with-env.ts';
import {
@@ -330,6 +330,21 @@ describe('count unavailable: a degraded stage, never a number', () => {
expect('total' in count ? count.total : -1).toBeGreaterThan(0);
});
+ test('#8500: match: "keyword" keeps a count that lands past the floor while its rows are still loading', async () => {
+ const slow = new Proxy(engine, {
+ get(target, prop, receiver) {
+ if (prop === 'countKeywordPages') return async (...a: Parameters<BrainEngine['countKeywordPages']>) => { await after(2_200); return target.countKeywordPages(...a); };
+ if (prop === 'searchKeywordPages') return async (...a: Parameters<BrainEngine['searchKeywordPages']>) => { await after(2_500); return target.searchKeywordPages(...a); };
+ const v = Reflect.get(target, prop, receiver);
+ return typeof v === 'function' ? v.bind(target) : v;
+ },
+ });
+ const page = await readKeywordPage(slow, { query: TERM, match: 'keyword' }, {}, { match: 'keyword', offset: 0, cursor: null }, 5);
+ expect(page.degraded).toBeUndefined();
+ expect(page.meta.total).toBe(await engine.countKeywordPages(TERM, {}));
+ expect(page.rows).toHaveLength(5);
+ }, 15_000);
+
test('#6632: a count still running after the search and its grace is given up, not waited on', async () => {
const started = Date.now();
expect(await countKeywordMatches(stalled(engine, 'hang'), TERM, {}, 50, after(150))).toEqual({ unavailable: 'timeout' });