Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(large-pages): lane X — searchTitles runs the manifest-grant probe inside its bounded transaction

garrytan4ede2c43c92a
diff --git a/src/core/engine-sql/titles.ts b/src/core/engine-sql/titles.ts
index ca50bf52be..a10cbd8adb 100644
--- a/src/core/engine-sql/titles.ts
+++ b/src/core/engine-sql/titles.ts
@@ -45,15 +45,23 @@ import { searchLimitCap } from '../search/eval-pool-depth.ts';
import { boundWebsearchQuery, buildOrFallbackWebsearchQuery } from '../search/sql-ranking.ts';
import { buildSearchTitlesStatement, type SearchTitlesDialect } from '../search/title-statement.ts';
import { rowToSearchResult } from '../utils.ts';
+import type { ScopedRead } from './brands.ts';
import type { ScopedReadRunner } from './cjk-search.ts';
-export type { SearchTitlesDialect } from '../search/title-statement.ts';
+/**
+ * `chunkManifest` is the chunkManifestReads probe, run inside each attempt's scoped read after its settings, so the
+ * statement timeout bounds it like the title statement (#6495: a probe outside the transaction waited on a lock with
+ * no bound) and it answers for the role that read runs as. Unset reads the manifest.
+ */
+export type SearchTitlesRunDialect = Omit<SearchTitlesDialect, 'chunkManifest'> & {
+ chunkManifest?: (exec: ScopedRead) => Promise<boolean>;
+};
export async function searchTitles(
scoped: ScopedReadRunner,
query: string,
opts: SearchOpts | undefined,
- dialect: SearchTitlesDialect,
+ dialect: SearchTitlesRunDialect,
): Promise<SearchResult[]> {
// language/symbolKind are chunk-grain code filters with no page-grain
// meaning; a code-scoped query gets no title candidates rather than rows
@@ -62,7 +70,8 @@ export async function searchTitles(
if (opts?.limit && opts.limit > searchLimitCap()) {
console.warn(`[gbrain] Warning: search limit clamped from ${opts.limit} to ${searchLimitCap()}`);
}
- const statement = buildSearchTitlesStatement(query, opts, dialect);
+ const statementFor = (chunkManifest: boolean) => buildSearchTitlesStatement(query, opts, { ...dialect, chunkManifest });
+ const statement = statementFor(true);
const run = (queryText: string, relaxed: boolean) => {
const preferIndex = relaxed && dialect.relaxedPrefersIndex && !statement.safeChunks;
const settings = {
@@ -71,7 +80,8 @@ export async function searchTitles(
};
const params = [...statement.params];
params[0] = queryText;
- return scoped(async exec => (await exec.unsafe(statement.sql, params)).rows, Object.keys(settings).length ? settings : undefined);
+ return scoped(async exec => (await exec.unsafe(statementFor(await dialect.chunkManifest?.(exec) ?? true).sql, params)).rows,
+ Object.keys(settings).length ? settings : undefined);
};
const strictQuery = statement.params[0] as string;
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index ae6e934b4a..1ac7c9e198 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -1194,7 +1194,7 @@ export class PostgresEngine implements BrainEngine {
(read, settings) => this.withScopedReadTransaction(opts?.sourceIds, opts?.sourceId, tx => read(scopedRead(this.engineSqlOn(tx))), { alwaysTransaction: true, jitOff: true, settings }),
query,
opts,
- { statementTimeout: await this.keywordStatementTimeoutSetting(), relaxedPrefersIndex: true, staleProbe: false, chunkManifest: await chunkManifestReads(this) },
+ { statementTimeout: await this.keywordStatementTimeoutSetting(), relaxedPrefersIndex: true, staleProbe: false, chunkManifest: exec => chunkManifestReads(this, exec) },
);
}
diff --git a/test/keyword-statement-timeout-knob-6495.test.ts b/test/keyword-statement-timeout-knob-6495.test.ts
index 36842f601c..f2921e371a 100644
--- a/test/keyword-statement-timeout-knob-6495.test.ts
+++ b/test/keyword-statement-timeout-knob-6495.test.ts
@@ -186,6 +186,16 @@ describe('search.keyword_statement_timeout_ms governs all six Postgres keyword s
});
}
+ test('every site issues no statement before its bound but the knob read (the manifest-grant probe runs inside the bounded transaction)', async () => {
+ for (const site of Object.keys(SITES)) {
+ const { engine, recorded } = await recordingEngine({ dbValue: '2500' });
+ await SITES[site](engine);
+ const boundAt = recorded.findIndex(({ sql, params }) => Connection.statementTimeoutChanges(sql, params).length > 0);
+ expect(boundAt, site).toBeGreaterThanOrEqual(0);
+ expect(recorded.slice(0, boundAt).filter(r => !r.sql.includes(KEYWORD_STATEMENT_TIMEOUT_KEY)).map(r => r.sql), site).toEqual([]);
+ }
+ });
+
test('countKeywordPages keeps a narrower caller timeoutMs under the knob', async () => {
expect(await timeoutFor('countKeywordPages', { dbValue: '5000' }, 1500)).toEqual(['1500ms']);
expect(await timeoutFor('countKeywordPages', {}, 2000)).toEqual(['2000ms']);