Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(search): the response-meta reads run concurrently (#6721)

garrytan856be7d19194
diff --git a/src/core/ops/search.ts b/src/core/ops/search.ts
index 77e12bc696..3f08e55d5a 100644
--- a/src/core/ops/search.ts
+++ b/src/core/ops/search.ts
@@ -461,22 +461,21 @@ async function buildRetrievalResponseMeta(
: undefined;
const excludePrivate = await resolveExcludePrivatePages(ctx.engine, ctx.remote);
const excludeSlugPrefixes = resolveHardExcludes();
- const safeIndexPending = ctx.remote !== false
- && await hasUnsealedPagesInScope(ctx, scope, excludePrivate, { types: opts.types, excludeSlugPrefixes });
- const readiness = await probeProjectionReadiness(ctx.engine, {
- ...scope,
- excludePrivate,
- types: opts.types,
- excludeSlugPrefixes,
- });
const aliases = (opts.declarations ?? new DeclarationMemo()).scan(results as DeclarationRow[], queryText);
- // Facts saved about the entity the query names come first (newest first, one hop through entities they name).
- const entityFacts = opts.entity && ctx.emitResponseMeta ? await entitySavedFacts(ctx.engine, opts.entity, { remote: ctx.remote !== false, excludePrivate }) : [];
+ // #6721: these reads depend on nothing but the inputs above, so they run at once: on a pooled Postgres
+ // the wall time is the slowest read's round trips, not their sum. The answer-feedback write still runs last.
+ const [safeIndexPending, readiness, entityFacts, matchedFacts, heldFiles] = await Promise.all([
+ ctx.remote !== false && hasUnsealedPagesInScope(ctx, scope, excludePrivate, { types: opts.types, excludeSlugPrefixes }),
+ probeProjectionReadiness(ctx.engine, { ...scope, excludePrivate, types: opts.types, excludeSlugPrefixes }),
+ // Facts saved about the entity the query names come first (newest first, one hop through entities they name).
+ opts.entity && ctx.emitResponseMeta ? entitySavedFacts(ctx.engine, opts.entity, { remote: ctx.remote !== false, excludePrivate }) : [],
+ matchingSavedFacts(ctx, scope, queryText, aliases),
+ stampHeldHits(ctx.engine, results as SearchResult[], scope, ctx).catch(() => []),
+ ]);
// A fact the facts arm already returned as a row is not repeated beside the blocks.
const factRowIds = new Set((results as SearchResult[]).flatMap(r => r.fact_row ? [r.fact_row.id] : []));
- const savedFacts = [...entityFacts, ...(await matchingSavedFacts(ctx, scope, queryText, aliases)).filter(m => !entityFacts.some(e => e.id === m.id))]
+ const savedFacts = [...entityFacts, ...matchedFacts.filter(m => !entityFacts.some(e => e.id === m.id))]
.filter(f => !factRowIds.has(f.id));
- const heldFiles = await stampHeldHits(ctx.engine, results as SearchResult[], scope, ctx).catch(() => []);
const heldNotice = heldFilesNotice(heldFiles, ctx.remote !== false);
if (heldNotice) ctx.emitNotice?.(heldNotice);
const degraded = [...(m?.degraded ?? [])];
diff --git a/test/search-response-meta-concurrency.test.ts b/test/search-response-meta-concurrency.test.ts
new file mode 100644
index 0000000000..22264f255a
--- /dev/null
+++ b/test/search-response-meta-concurrency.test.ts
@@ -0,0 +1,74 @@
+/**
+ * #6721: the independent reads behind a search's response meta run at once.
+ *
+ * 1. Protects: on a pooled Postgres the safe-index check, projection
+ * readiness, saved-fact match and held-file coverage cost one round trip
+ * of wall time, not one each.
+ * 2. Fails when: buildRetrievalResponseMeta awaits those reads one after
+ * another again (the saved-fact probe would only start once the
+ * safe-index statement has answered).
+ * 3. Existing coverage: test/dispatch-response-meta.serial.test.ts and
+ * test/search-other-names.test.ts pin what the meta says, not when its
+ * reads are issued.
+ * 4. Seam: none. Statement start and finish are observed on the PGLite
+ * handle the engine uses; a remote search through dispatch drives them.
+ * Contributed by @troyhoffman-oss (#6734).
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { importFromContent } from '../src/core/import-file.ts';
+import { serializeMarkdown } from '../src/core/markdown.ts';
+import { dispatchToolCall } from '../src/mcp/dispatch.ts';
+
+let engine: PGLiteEngine;
+const events: Array<{ at: 'start' | 'end'; sql: string }> = [];
+let failSafeIndex = false;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ await importFromContent(engine, 'notes/widget-renewal', serializeMarkdown({}, 'Widget renewal terms are net 30.', '', { type: 'note', title: 'Widget renewal', tags: [] }), { noEmbed: true, forceRechunk: true });
+ await engine.executeRaw(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source) VALUES
+ ('default', 'widget-co', 'Widget-co renewal terms moved to net 45.', 'fact', 'world', 'user update')`);
+ const db = engine.db as unknown as { query: (sql: string, ...rest: unknown[]) => Promise<unknown> };
+ const query = db.query.bind(db);
+ db.query = (sql: string, ...rest: unknown[]) => {
+ const text = sql.replace(/\s+/g, ' ').trim();
+ events.push({ at: 'start', sql: text });
+ if (failSafeIndex && text.includes('AND p.chunker_version <')) return Promise.reject(new Error('safe-index sentinel'));
+ return query(sql, ...rest).finally(() => events.push({ at: 'end', sql: text }));
+ };
+}, 120_000);
+
+afterAll(async () => {
+ await engine?.disconnect();
+});
+
+describe('search response meta', () => {
+ test('the saved-fact match is issued before the safe-index check has answered', async () => {
+ events.length = 0;
+ const r = await dispatchToolCall(engine, 'search', { query: 'widget renewal terms' }, { remote: true, transport: 'http', sourceId: 'default' });
+ expect(r.isError).toBeFalsy();
+ const safeIndexEnd = events.findIndex(e => e.at === 'end' && e.sql.includes('AND p.chunker_version <'));
+ // The saved-fact match's first statement (its existence probe, or the match itself once the probe is gone).
+ const factProbeStart = events.findIndex(e => e.at === 'start' && /^SELECT .*\bFROM facts f WHERE f\.source_id = ANY\(\$1::text\[\]\)/.test(e.sql));
+ expect(safeIndexEnd).toBeGreaterThan(-1);
+ expect(factProbeStart).toBeGreaterThan(-1);
+ expect(factProbeStart).toBeLessThan(safeIndexEnd);
+ // Same answer as before: the saved fact is still reported beside the results.
+ expect(r.content.slice(1).map(c => c.text ?? '').join('\n')).toContain('Widget-co renewal terms moved to net 45.');
+ });
+
+ test('a failing read keeps to its own catch: the others still answer', async () => {
+ failSafeIndex = true;
+ try {
+ const r = await dispatchToolCall(engine, 'search', { query: 'widget renewal terms' }, { remote: true, transport: 'http', sourceId: 'default' });
+ expect(r.isError).toBeFalsy();
+ expect(events.some(e => e.sql.includes('AND p.chunker_version <'))).toBe(true);
+ expect(r.content.slice(1).map(c => c.text ?? '').join('\n')).toContain('Widget-co renewal terms moved to net 45.');
+ } finally {
+ failSafeIndex = false;
+ }
+ });
+});