Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
5.1 KB
Raw Blame

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); candidateIds scores only a candidate set by joining unnest($n::bigint[]) to content_chunks (an = ANY filter was a sequential scan). buildCJKCandidateSql is 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, with opts.orFallback and 2+ terms, the OR pass only when strict AND returns fewer than limit rows; with opts.cjkKeyword (hybrid only, deadline from search.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 gets statement_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 whenever pg_class.reltuples exceeds PGLITE_CJK_CAPPED_CHUNKS = 50,000. onMeta gets { incomplete, reason (timeout | candidate_budget), capped, arm_ms } (test seams: candidateCap, pgliteCappedChunks, fullBudgetMs, and onStatementBudget, 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 most RARITY_MAX_KEYWORD_TERMS (12) words (the title arm keeps RARITY_MAX_TERMS = 4: it runs one statement per word), rareTermPagesSql reads the pages of each word's first RARE_TERM_CHUNK_CAP + 1 visible chunks (brain-wide, weights only; stopwords and, on Postgres, words commonOrTokens in keyword-or-narrow.ts estimates past the cap are skipped); a fitting word weighs 1 / pages. rankOrFallbackByRarity re-runs the retry with the rarest words' OR and sorts by summed weight.
  • src/core/search/keyword-identifier-probe.ts — #7491: keywordArmRows builds both engines' searchKeyword rows from the strict rows. With orFallback, offset 0 and no quote or negation, a query naming an identifier (identifierTokens: a hash number #412, or a token identifier-query.ts recognizes: 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. holdsIdentifiers lets the title arm's OR retry (engine-sql/titles.ts) lead with, untagged, the titles holding every identifier. Pinned by test/search/identifier-probe.serial.test.ts (+ Postgres E2E) and test/search/keyword-identifier-probe.test.ts.
  • src/core/search/keyword-handles.ts — GBRA-108: slugHandles finds the query's slug handles (a hyphenated slug-leaf token with a letter, bare or quoted on its own, at most MAX_SLUG_HANDLES = 16; each alternative of a bare OR is 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 calls SearchOpts.onKeywordIncomplete (keyword_candidates_incomplete, reason timeout or provider_error). They resolve by exact slug basename (handleMatchSql, served by idx_pages_slug_basename like whois) 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; in searchKeyword (orFallback, offset 0) keywordArmRows puts them ahead of every other row, untagged. A query without a handle builds the statements it built before. Pinned by test/search/slug-handles.serial.test.ts (+ Postgres E2E) and test/search/keyword-handles.test.ts.