Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Key files: Core Search (keyword arm: CJK fallback, OR retry, identifier probe, slug handles)
Subsystem index · Core Search (2/2). Read only the entries relevant to your change. Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG.
src/core/search/cjk-keyword-sql.ts+src/core/engine-sql/cjk-search.ts+src/core/search/cjk-keyword-deadline.ts— the CJK keyword fallback shared by both engines.buildCJKKeywordSql(query, ctx, variant)keeps today's strict-AND SQL byte-identical by default;match: 'any'(the OR fallback, #6043) qualifies a chunk on any term and orders by matched-term count, then the TF score (the count enters the score as 1000 x matched outside the source factor, so a boosted partial match never outranks an all-term chunk);candidateIdsscores only a candidate set by joiningunnest($n::bigint[])tocontent_chunks(an= ANYfilter was a sequential scan).buildCJKCandidateSqlis the unordered capped LIKE scan for ids (all-term, or any-term excluding ids already taken).searchKeywordCJK(scoped, query, ctx, engine)runs strict AND and, withopts.orFallbackand 2+ terms, the OR pass only when strict AND returns fewer thanlimitrows; withopts.cjkKeyword(hybrid only, deadline fromsearch.cjk_keyword_deadline_ms, integer ms 500..30000, default 3000) it bounds the arm: Postgres runs full scoring in its own scoped read transaction within 2/3 of the deadline (counted from before the pool wait; every statement of an attempt getsstatement_timeout= what is left of that attempt, minus a 40 ms rollback margin), and on SQLSTATE 57014 a capped retry (CJK_CANDIDATE_CAP= 2000; all-term ids first, partial ids only when the all-term rows fall short) in a fresh scoped transaction with the remaining third (a fresh transaction, not a savepoint, because the driver rethrows any failed statement at the end of its transaction scope); PGLite runs the capped stage wheneverpg_class.reltuplesexceedsPGLITE_CJK_CAPPED_CHUNKS= 50,000.onMetagets{ incomplete, reason (timeout | candidate_budget), capped, arm_ms }(test seams:candidateCap,pgliteCappedChunks,fullBudgetMs, andonStatementBudget, which reports each Postgres statement's timeout and the arm ... (line truncated to 2000 chars)src/core/search/keyword-or-rarity.ts— rare words first in both engines' keyword OR retry. For at mostRARITY_MAX_KEYWORD_TERMS(12) words (the title arm keepsRARITY_MAX_TERMS= 4: it runs one statement per word),rareTermPagesSqlreads the pages of each word's firstRARE_TERM_CHUNK_CAP+ 1 visible chunks (brain-wide, weights only; stopwords and, on Postgres, wordscommonOrTokensin keyword-or-narrow.ts estimates past the cap are skipped); a fitting word weighs 1 / pages.rankOrFallbackByRarityre-runs the retry with the rarest words' OR and sorts by summed weight.src/core/search/keyword-identifier-probe.ts— #7491:keywordArmRowsbuilds both engines'searchKeywordrows from the strict rows. WithorFallback, offset 0 and no quote or negation, a query naming an identifier (identifierTokens: a hash number#412, or a tokenidentifier-query.tsrecognizes: a 6+ digit run, hex id, code, email, URL or phone) and other words runs the arm's own statement once more with the AND of the identifier tokens as the match query and the any-word query as the rank query: same scope, visibility, chunk predicate, filters and candidate cap as strict. Its rows are untagged (they vote like strict rows): after the strict rows when strict matched, ahead of the relaxed rows (keyword_relaxed) when it did not; one row per page, cut to the limit. A failed probe adds nothing.holdsIdentifierslets the title arm's OR retry (engine-sql/titles.ts) lead with, untagged, the titles holding every identifier. Pinned bytest/search/identifier-probe.serial.test.ts(+ Postgres E2E) andtest/search/keyword-identifier-probe.test.ts.src/core/search/keyword-handles.ts— GBRA-108:slugHandlesfinds the query's slug handles (a hyphenated slug-leaf token with a letter, bare or quoted on its own, at mostMAX_SLUG_HANDLES= 16; each alternative of a bareORis judged on its own: an alternative with a negation or a longer quoted phrase resolves none, and the strict match (slugHandles(query, true)) resolves an alternative's handles only when every term of it is a handle). A failed handle or identifier-probe read adds no rows and callsSearchOpts.onKeywordIncomplete(keyword_candidates_incomplete, reasontimeoutorprovider_error). They resolve by exact slug basename (handleMatchSql, served byidx_pages_slug_basenamelikewhois) as an arm of the keyword statement (search/keyword-statement.ts) under its filters, scope and visibility, so--types, granted sources, private pages and quarantine apply.match: "keyword"lists and counts the named pages first; insearchKeyword(orFallback, offset 0)keywordArmRowsputs them ahead of every other row, untagged. A query without a handle builds the statements it built before. Pinned bytest/search/slug-handles.serial.test.ts(+ Postgres E2E) andtest/search/keyword-handles.test.ts.