Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* The `query` op's post-retrieval arms, applied after hybrid search and the
* declared-name fan-out, before the CRAG grade: entity anchoring
* (`search.entity_anchoring`, default off, search/entity-anchor.ts), fact
* source pages (`search.fact_source_pages`, default off) and then the facts
* arm (`search.query_facts_arm`, default on, search/facts-arm.ts), or in its
* place, for a query whose caller passed a token budget, the facts block
* (`search.query_facts_block`, default on; the no-budget evidence plan's
* implicit budget does not count, #8411) or, with a temporal cue, the
* temporal fact reserve (`search.temporal_fact_reserve`, default off). With
* all off the rows pass through unchanged; the facts block is checked first, so
* turning it off restores the temporal reserve or the facts arm.
*/
import type { BrainEngine } from '../engine.ts';
import type { PageReadScope, SearchResult } from '../types.ts';
import { anchorOpResults } from './entity-anchor.ts';
import { applyFactsArm, applyFactsBlock, applyFactSourcePages, applyTemporalFactReserve, factSourcePagesEnabled, hasTemporalCue, queryFactsArmEnabled, queryFactsBlockEnabled, temporalFactReserveEnabled } from './facts-arm.ts';
import { type StageTimer, UNTIMED } from './stage-timings.ts';
export async function applyQueryArms(engine: BrainEngine, p: Record<string, unknown>, query: string, results: SearchResult[],
scope: PageReadScope & { filtered: boolean; evidencePlan: boolean; evidenceBudget?: number; remote: boolean; queryEmbedding: Float32Array | null; rowCap: () => Promise<number>; timer?: StageTimer }): Promise<SearchResult[]> {
const timer = scope.timer ?? UNTIMED;
const anchored = await timer.stage('anchoring', () => anchorOpResults(engine, p, query, results, scope));
return timer.stage('facts_arm', () => applyFactsStage(engine, p, query, anchored, scope));
}
async function applyFactsStage(engine: BrainEngine, p: Record<string, unknown>, query: string, anchored: SearchResult[],
scope: Parameters<typeof applyQueryArms>[4]): Promise<SearchResult[]> {
if (p.offset || scope.filtered || p.since || p.until || p.lang || p.symbol_kind || p.near_symbol) return anchored;
const tokenBudget = !scope.evidencePlan && typeof p.token_budget === 'number' ? p.token_budget : undefined;
const opts = { sourceId: scope.sourceId, sourceIds: scope.sourceIds, remote: scope.remote, minTrust: scope.minTrust, suppressFlagged: scope.suppressFlagged, queryEmbedding: scope.queryEmbedding, rowCap: scope.rowCap, readScope: scope, tokenBudget };
const pages = await factSourcePagesEnabled(engine) ? await applyFactSourcePages(engine, query, anchored, opts) : anchored;
const budget = scope.evidencePlan ? scope.evidenceBudget : tokenBudget;
const callerBudget = typeof p.token_budget === 'number' && p.token_budget > 0;
if (budget && callerBudget && await queryFactsBlockEnabled(engine)) return applyFactsBlock(engine, query, pages, { ...opts, budget });
if (budget && hasTemporalCue(query) && await temporalFactReserveEnabled(engine)) return applyTemporalFactReserve(engine, query, pages, { ...opts, budget });
if (!await queryFactsArmEnabled(engine)) return pages;
return applyFactsArm(engine, query, pages, opts);
}