/** * Selective post-rerank evidence bundling (Wave-6, P2 retrieval-overhaul * "evidence-sufficiency" lever — mirrors src/search/rerank.ts's/decompose.ts's * ADR-005 seam pattern). * * The oracle-factorial investigation found ~87% of the LoCoMo gap lives in * the answerer/evidence layer, not ranking: the gold-turns ceiling (~0.526) * shows the answerer often receives top-k atoms that are each individually * well-ranked but COLLECTIVELY insufficient to answer a multi-hop question — * the missing fact simply never made it into the window search()/rerank * already finalized. This stage runs AFTER that top-k is finalized (post- * rerank when rerank is enabled, post-fusion-truncation otherwise) and, only * when the evidence looks thin by a cheap heuristic, pulls a small number of * ADDITIONAL scoped atoms (same-entity / same-session as the finalized hits) * into the bundle. Ranking of the original top-k is never touched — this is * pure addition, same "never evict, only add" posture WS6's supplemental * merge already established. * * OFF by default (config.evidenceBundling.enabled: false) — * `resolveEvidenceBundlingOpts` returns `undefined` in that case and * search() takes its pre-Wave-6 code path with zero extra branches, * byte-identical to before this file existed. * * Deliberately HEURISTIC, not LLM-based: the trigger (`isEvidenceThin`) and * the scoped pull below are both plain DB queries against data the ingest * pipeline already wrote (`memories.session_id`, `memory_entities`) — no new * LLM call on this hot, latency-sensitive path. A future LLM-backed * sufficiency check (asking a model "is this evidence enough to answer X?") * is plausible follow-on work, but belongs behind its OWN sub-flag with its * own cost/timeout budget (FEAT-482's lesson: an ungated LLM call on a * per-recall path can wedge a metered endpoint) — not folded into this * default heuristic path. */ import type { DB } from '../storage/db.js'; import type { Config } from '../config/config.js'; import type { SearchHit } from './search.js'; /** Resolved evidence-bundling options as consumed by `applyEvidenceBundling` / * `SearchOpts.evidenceBundling`. */ export interface EvidenceBundlingOpts { /** Hard cap on how many extra atoms one enrichment pass may ever add, * regardless of how many scoped candidates were found. */ maxExtra: number; /** "Evidence looks thin" trigger: enrichment only fires when the * finalized top-k's distinct `memories.session_id` count is BELOW this. */ minDistinctSessions: number; /** Pull atoms sharing an entity (via `memory_entities`) with any finalized hit. */ sameEntity: boolean; /** Pull atoms sharing a `session_id` with any finalized hit. */ sameSession: boolean; } /** * Resolve `SearchOpts.evidenceBundling` from config: `undefined` when * disabled (so search() takes zero extra branches — same contract as * `resolveRerankOpts`/`resolveDecomposeOpts`), or a fully-resolved * `EvidenceBundlingOpts` when enabled. No provider/model to build (unlike * rerank/decompose) — this stage is pure heuristic + DB queries. */ export declare function resolveEvidenceBundlingOpts(config: Config): EvidenceBundlingOpts | undefined; /** * Pure heuristic trigger: given each finalized hit's `session_id` (`null` * when unset), true when the DISTINCT non-null count is below * `minDistinctSessions` — the finalized top-k is drawn from too few distinct * sessions for comfort on a question that may need cross-session context. * Exported standalone so the trigger is unit-testable without a DB. */ export declare function isEvidenceThin(sessionIds: Array, minDistinctSessions: number): boolean; /** * The exact SQL fragments search() already built for the base result set * (post-fusion column/range filters + bitemporal validity window) — reused * VERBATIM here rather than re-derived from `SearchFilters`, so an enrichment * pull can never surface an atom that project/scope/type/agent/since * filtering or the as_of/validity window would have excluded (no drift * between the two, by construction — there is only one filter-building call * site, in search.ts). NOT reapplied here: `filters.entity` — that narrowing * is a PRE-fusion candidate-set intersection in search() (applyEntityFilter, * before gatherCandidates' output ever reaches fusion), not part of this * post-fusion `whereFilter`/`validityClause` pair, so an entity-filtered * search's enrichment pull can surface an atom outside that entity scope. * `filters.entity` is a relevance narrowing, not a tenant/security boundary, * so this is an accuracy caveat, not a scoping leak. */ export interface EvidenceBundleSqlContext { /** ` AND ...`-prefixed fragment, or `''` when no post-fusion filter is active. */ whereFilter: string; filterParams: (string | number)[]; validityClause: string; validityParams: number[]; } /** * Apply the evidence-bundling stage to an already-finalized hit list (post- * rerank when rerank ran, post-fusion-truncation otherwise). Pure addition — * `hits` themselves are never reordered or dropped; enrichment atoms are * appended after them, each marked `bundled: true` with a `bundle_reason`. * * No-ops (returns `hits` unchanged) when: `hits` is empty, `maxExtra <= 0`, * the thinness trigger doesn't fire (evidence already spans enough distinct * sessions), or every scoped candidate is filtered out by `sql`'s clauses. */ export declare function applyEvidenceBundling(db: DB, hits: SearchHit[], sql: EvidenceBundleSqlContext, opts: EvidenceBundlingOpts): Promise;