Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): a Postgres statement timeout in a lexical arm is stamped reason timeout (#6495)
garrytanf77f1418caed
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 24b5bad876..e0cb605465 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,6 +25,7 @@ identifiers and attribution are available in the pre-removal Git revision- **`fetch` without an id names both routes.** The refusal says to re-run `search` once `fetch` is callable, or to pass the row's slug (accepted when unambiguous in the caller's read scope).- **`degraded_recall` shows on every affected call on stdio and the CLI.** Transient degradations (`rerank_failed`, `embed_timeout`, a failed keyword or title arm, every other per-call stage) are no longer deduped per session, so a failure after a different earlier failure, or the same failure after a recovery, always carries its notice. A notice made only of configuration-persistent stages (a keyless brain's `embed_unavailable`, about 600 characters on every search otherwise) carries an unrendered `dedupe_key` of its sorted unique stage/reason pairs, which the notice ledger dedupes on per session and the wire never shows. HTTP stays per call. `docs/protocol/AGENT_OPERATOR_v1.md` now states one rule for stdio, CLI and HTTP.- **`entity names[]` is 5 to 17 times faster on a large brain, with byte-identical output.** On a 55,838-page PGLite brain built from the Cat 40 Hard world M generator (seed 20261021, no embeddings), a 20-name batch's median went from 2,250 ms to 129 ms with every name found, from 1,743 ms to 155 ms with half found, and from 1,112 ms to 203 ms with none found. Profiling showed the name lookup itself dominated, not the cards: each found name ran two sequential scans of the source's pages, the alias resolver's "no other live page has this exact title" check and the card's exact-title / exact-slug / slug-suffix arm, about 50 ms each. Migration v240 adds `idx_pages_source_title_lower` (`source_id, lower(title)`) and a trigram index on `slug` (`idx_pages_slug_trgm`), built online on Postgres; the queries are unchanged and now run as index scans in about 1 ms. Single-name `entity`, context_pack cards and search's alias fan-out use the same two reads. The batch also stops assembling a full card per name it then threw away: candidate selection is now one exported function, `selectEntityCandidates`, that `buildEntityCard` and `lookupEntityNames` both call; the lead reuses the candidate row's body; siblings are read without the identity excerpt (`readSiblingIdentity`); and the private-page setting, mention policy and source pack are read once per batch (`entityLookupScope`). Parity tests compare the batch with the old full-card resolver over adversarial names (colliding aliases and titles, namespaced slugs, the same slug in two sources, private, deleted and quarantined candidates, identity siblings, stale aliases, LIKE wildcards) on PGLite and Postgres, and plan tests pin both reads to the new indexes.+- **A Postgres statement timeout in the keyword or title arm is reported as a timeout (#6495).** `isTimeoutError` (`src/core/search/hybrid/degraded.ts`) now recognizes SQLSTATE 57014 through `isStatementTimeoutError`, so the arms' 8 s `SET LOCAL statement_timeout` on a large brain stamps `keyword_arm_failed` / `title_arm_failed` with reason `timeout` instead of `provider_error`. Probe in `test/hybrid-arm-rethrow.test.ts` (fails before).## [0.60.180.0] - 2026-10-12diff --git a/src/core/search/hybrid/degraded.ts b/src/core/search/hybrid/degraded.tsindex 075fda5476..bc3ff2ea88 100644--- a/src/core/search/hybrid/degraded.ts+++ b/src/core/search/hybrid/degraded.ts@@ -1,17 +1,20 @@/** Degradation-stamp helpers shared by hybridSearch, its stages and hybridSearchCached. */import type { DegradedStage, DegradedStageEntry, DegradedReason } from '../../types.ts';import type { TokenBudgetMeta } from '../token-budget.ts';+import { isStatementTimeoutError } from '../../retry-matcher.ts';/*** WP2/T3 — classify an embed/vector failure as a timeout vs a provider* error for the enumerated degraded[] reason codes (D6). Matches both the* embedQueryBounded deadline rejection and AbortSignal.timeout's- * TimeoutError/AbortError. The raw error text never rides the wire — it+ * TimeoutError/AbortError, and a Postgres statement timeout (SQLSTATE 57014,+ * the keyword and title arms' `SET LOCAL statement_timeout`). The raw error text never rides the wire — it* goes to stderr via warnOncePerProcess only.*/export function isTimeoutError(err: unknown): boolean {if (!(err instanceof Error)) return false;if (/deadline \d+ms exceeded/.test(err.message)) return true;+ if (isStatementTimeoutError(err)) return true;return err.name === 'TimeoutError' || err.name === 'AbortError';}diff --git a/test/hybrid-arm-rethrow.test.ts b/test/hybrid-arm-rethrow.test.tsindex 72707b1703..7d7c597b24 100644--- a/test/hybrid-arm-rethrow.test.ts+++ b/test/hybrid-arm-rethrow.test.ts@@ -115,6 +115,19 @@ describe('a failed lexical arm is stamped in meta.degraded (#5324)', () => {expect(titles.map(d => d.stage)).not.toContain('keyword_arm_failed');});+ test('a Postgres statement timeout (SQLSTATE 57014) is stamped reason timeout, not provider_error (#6495)', async () => {+ const statementTimeout = () => Object.assign(new Error('canceling statement due to statement timeout'), { name: 'PostgresError', code: '57014' });+ const run = async (arms: Parameters<typeof fakeEngine>[0]) => {+ let degraded: Array<{ stage: string; reason?: string }> = [];+ await hybridSearch(fakeEngine(arms), QUERY, { limit: 5, onMeta: (m) => { degraded = (m.degraded ?? []) as typeof degraded; } });+ return degraded;+ };+ expect(await run({ searchKeyword: () => Promise.reject(statementTimeout()), searchTitles: async () => [] }))+ .toContainEqual({ stage: 'keyword_arm_failed', reason: 'timeout' });+ expect(await run({ searchKeyword: async () => [], searchTitles: () => Promise.reject(statementTimeout()) }))+ .toContainEqual({ stage: 'title_arm_failed', reason: 'timeout' });+ });+test('healthy arms stamp neither stage', async () => {let degraded: Array<{ stage: string }> = [];await hybridSearch(fakeEngine({ searchKeyword: async () => [], searchTitles: async () => [] }), QUERY, {