/** * Query rewrite/expansion stage (FEAT-550, ADR-005 seam pattern — mirrors * src/search/decompose.ts and src/search/rerank.ts). * * Before retrieval, an optional cheap local LLM call rewrites/expands the * caller's query into alias/synonym/decomposition variants (entity-alias * forms, synonyms, sub-questions — e.g. "when did X move?" also produces "X * new apartment date"). Retrieval then runs over the union of the original * query and every variant; hits are merged by atom id keeping the MAX score * across all variant hit sets (see `mergeVariantHits` below and the merge * step in search.ts) — no new ranking model, no re-ordering within a single * variant's own result list, no damping (unlike WS6's supplemental-add * merge in decompose.ts, which this stage deliberately does NOT reuse — see * that module's dilution-bound doc comment for why a damped merge exists * there and not here). * * OFF by default (config.search.queryRewrite.mode: 'off') — * `resolveQueryRewriteOpts` returns `undefined` in that case and search() * takes its pre-FEAT-550 code path with zero extra branches, byte-identical * to before this file existed (AC-1). Even when `mode: 'enabled'`, search() * only actually invokes this stage when the caller ALSO passes `expand: * true` (`SearchOpts.expand`) — a synchronous local-model call on the hot * recall path is a real latency risk until measured (this FEAT's own "No * default-on for interactive recall until latency measured" scope fence), * so enabling the config only makes the capability AVAILABLE, never * default-on for a plain search() call. * * Fail-open contract (AC-2/AC-5): a rewrite call that throws, times out, or * exceeds its hard `timeoutMs` cap — OR that succeeds but produces zero * usable variants (empty/degenerate output, e.g. every variant echoes the * original query) — degrades exactly to pre-FEAT-550 behavior: retrieval * runs over the ORIGINAL QUERY ONLY. Both cases return `[]` from * `getQueryVariants`, never throw; the caller (search.ts) treats an empty * variant list identically regardless of which case produced it — this is * the same fallback posture `getSubQueries` (decompose.ts) and * `applyRerank` (rerank.ts) already carry. */ import type { Config } from '../config/config.js'; import type { LLMProvider } from '../contracts/llm.js'; import type { FusedHit } from './fuse.js'; export interface QueryRewriter { readonly name: 'mock' | 'llm'; /** * Return 0+ rewritten query variants. An empty array means "no useful * rewrite" (or the rewriter couldn't produce one) — callers treat that * exactly like the stage being disabled for this call. * * `signal` fires when `getQueryVariants`'s timeout budget elapses (mirrors * `QueryDecomposer.decompose`'s contract) — a real (LLM-backed) * implementation must forward it into its provider call so a timeout * actually cancels the in-flight request instead of abandoning it. * `MockQueryRewriter` ignores it — it never does I/O. */ rewrite(query: string, signal?: AbortSignal): Promise; } /** * Deterministic MOCK rewriter: NOT a real rewriter — it carries no semantic * understanding, just a couple of cheap heuristic transforms (stripping a * leading interrogative word, and appending a generic broadening suffix) so * a unit test / `provider: 'mock'` can prove the stage is correctly wired * end to end without a model. `LlmQueryRewriter` (below) plugs a real LLM in * behind the same `QueryRewriter` interface without touching search.ts. */ export declare class MockQueryRewriter implements QueryRewriter { readonly name: "mock"; rewrite(query: string): Promise; } /** * Real LLM-backed rewriter (FEAT-550). Asks the model for a JSON array of up * to `maxVariants` alternative standalone search queries — alias forms, * synonyms, simple decompositions — that would retrieve the same * information as the original query. A query with no useful rewrite is * expected to yield `[]`; the caller's `getQueryVariants` treats fewer than * 1 usable variant as "nothing to add" either way. * * Parsing follows `LlmQueryDecomposer`'s exact tolerant-extraction posture * (issue #261 precedent): a clean JSON array is the expected shape; a JSON * object is accepted as a fallback (its keys); anything else yields `[]`, * never an error. Genuine provider failures (network error, thrown * exception) still propagate so `getQueryVariants`'s timeout/error fallback * logs them. */ export declare class LlmQueryRewriter implements QueryRewriter { private readonly provider; private readonly maxVariants; readonly name: "llm"; constructor(provider: LLMProvider, maxVariants: number); rewrite(query: string, signal?: AbortSignal): Promise; } /** Resolved query-rewrite options as consumed by `getQueryVariants` / `SearchOpts.queryRewrite`. */ export interface QueryRewriteOpts { rewriter: QueryRewriter; maxVariants: number; timeoutMs: number; /** * Cap on the merged (original query + every variant) fused candidate set * size — bounds the cost of the downstream meta/memory-row join, not a * relevance judgment. See `mergeVariantHits` below. */ maxCandidates: number; } /** * Build a `QueryRewriter` from config. `provider: 'llm'` reuses the existing * extraction LLM provider machinery (src/providers/index.ts) — * `config.search.queryRewrite.model` optionally overrides the model, * defaulting to `config.llm.extraction.model` on * `config.llm.extraction.provider` (no new provider-selection surface * invented for this one stage — same posture as `buildQueryDecomposer`). */ export declare function buildQueryRewriter(config: Config, buildLLMProvider: (providerName: Config['llm']['extraction']['provider'], model: string, config: Config) => LLMProvider): QueryRewriter; /** * Resolve `SearchOpts.queryRewrite` from config: `undefined` when * `mode !== 'enabled'` (so search() takes zero extra branches — same * contract as `resolveDecomposeOpts`/`resolveRerankOpts`), or a fully-built * `QueryRewriteOpts` (rewriter instantiated once, reused per call) when * enabled. Note this only makes the stage AVAILABLE — see this module's doc * comment for the additional per-call `expand: true` gate search() applies. */ export declare function resolveQueryRewriteOpts(config: Config, buildLLMProvider: (providerName: Config['llm']['extraction']['provider'], model: string, config: Config) => LLMProvider): QueryRewriteOpts | undefined; /** * Run the rewrite stage for one query: the rewriter call under a hard * timeout, then de-dup/cap. Returns `[]` (never throws) on a timeout, a * provider error, or fewer than 1 usable variant (echo of the original * query, blank strings) — every one of those cases means "search.ts should * treat this call exactly like the stage never ran," which is the fallback * contract this stage promises (degrades quality, never availability — same * posture as decompose.ts/rerank.ts, and satisfies AC-2/AC-5 together: a * hard failure and a "succeeded but useless" result are indistinguishable * to the caller, both fail open). */ export declare function getQueryVariants(query: string, opts: QueryRewriteOpts): Promise; /** * AC-3 fusion: merge the primary (original-query) fused hits with N * variants' own separately-fused hit lists by atom id, keeping the MAX * score across all lists — no damping, no re-ordering within any one list's * own result set, no new ranking model. The primary list's hits are ALWAYS * part of the returned union (seeded first, below) even when every variant * returns zero hits. * * The merged union is then capped to `maxCandidates` (ranked by score) — a * cost bound on the downstream meta/memory-row join, not a relevance * judgment: a low-scoring primary hit CAN be displaced by the cap when * enough higher-scoring variant hits exist. This is deliberately simpler * than WS6's damped-supplemental merge (decompose.ts) — FEAT-550 is * explicitly scoped to "no new ranking model, no re-ordering," so a * straightforward max-by-id union is the right instrument here, not a * dilution-bounded blend. */ export declare function mergeVariantHits(primary: FusedHit[], variants: FusedHit[][], maxCandidates: number): FusedHit[];