/** * Filter-descriptor registry (FEAT-429). * * Problem this collapses: before this file, adding one recall/search filter * signal (project, agent, entity, ...) meant hand-editing the same pattern in * lockstep across the Zod contract (src/contracts/recall-filters.ts), the * GET /search query-string parser, the POST /recall body parser, the MCP * `search_memory`/`recall_memory` tool schemas, and the post-fusion SQL * WHERE-clause builder in src/search/search.ts. Missing any one silently * degrades a surface — see FEAT-424 (cross-surface drift) and the FEAT-429 * arch-review finding that motivated this file. * * A `FilterDescriptor` is the single declaration of one filter signal: its * wire name, its Zod value shape, how a raw GET query-string value parses * into that shape, how a structured (already-parsed) value is recognized as * "present" (mirrors the exact truthy/`!== undefined` checks the hand-rolled * code used per field, preserved here for byte-identical behavior), and how * it composes into the SQL WHERE clause (a plain equals/IN column, or * `'custom'` for filters like `entity` that resolve through a side table * before fusion rather than a plain `WHERE col = ?`). * * Every function below is generic over an explicit `descriptors` array * (defaulting to the shared `RECALL_FILTER_REGISTRY`), so a descriptor-only * addition — including one defined only inside a test — drives parsing + * SQL end-to-end without touching any of these functions. * * To add a new hard-filter SQL-column signal: add one * `stringOrListColumnFilter({...})` entry to `RECALL_FILTER_REGISTRY` below * (plus a migration if it needs a new column). Every surface that calls * `assignQueryFilterValues` / `assignStructuredFilterValues` / * `applySqlColumnFilters` / `buildFilterFieldsSchema` picks it up for free. */ import { z } from 'zod'; /** How a filter's wire value is shaped. */ export type FilterValueShape = 'stringOrList' | 'scalarString' | 'scalarNumber'; /** How a filter composes into the post-fusion SQL WHERE clause. */ export type FilterApply = /** Plain `col = ?` (single value) or `col IN (...)` (list, OR-semantics). */ { kind: 'sqlColumn'; column: string; } /** * Numeric comparison column (FEAT-429 SLICE-B) — `col >= ?`. Narrowly * typed to the one operator `since` actually needs rather than a generic * operator-string field; a second distinct comparison shape showing up * later is the trigger to reconsider this, not a reason to generalize now. */ | { kind: 'sqlRangeColumn'; column: string; op: 'gte'; } /** Resolved through custom logic (e.g. entity -> EntityRepo candidate-set * intersection) rather than a plain SQL column — the registry still * enumerates it for schema/parsing derivation, but SQL-builder helpers * below skip it (the surface applies its own resolution). */ | { kind: 'custom'; }; export interface FilterDescriptor { /** Wire field name — shared verbatim across query params, JSON body, and MCP tool args. */ readonly name: string; readonly valueShape: FilterValueShape; /** Zod schema for this field, reused by every surface's input schema. */ readonly schema: z.ZodTypeAny; readonly apply: FilterApply; /** All current filters AND-compose with every other active filter. */ readonly compose: 'AND'; readonly description: string; /** * Presence predicate for a value that has already been parsed into its * wire shape (JSON body / MCP args). Mirrors the exact per-field check the * hand-rolled parsing used before this registry existed — `project`/`agent` * used a truthy check (so an explicit `''` is treated as absent), `entity` * used `!== undefined` (so an explicit `''` is a filter that resolves to * zero results, not "absent"). Preserved here, not "fixed", to stay * byte-identical (FEAT-429 AC: behavior-preserving). */ presentInStructured(value: unknown): boolean; /** * Parse a raw GET query-string value into this filter's wire shape. * Only invoked when the raw string is non-empty (callers gate on * `if (raw) ...`), matching the pre-registry per-field truthy check. * Returns `undefined` to mean "no constraint" (e.g. an all-empty CSV list). */ parseQueryValue(raw: string): unknown; } /** * The production recall-filter registry (FEAT-429). Migrated from FEAT-423/ * FEAT-424's hand-rolled project/agent/entity wiring — see * src/contracts/recall-filters.ts for the canonical-contract mirror these * schemas are drawn from. */ export declare const RECALL_FILTER_REGISTRY: readonly FilterDescriptor[]; /** Look up a single descriptor's schema by wire name (throws if unknown — programmer error, not a wire-input error). */ export declare function getFilterSchema(name: string, descriptors?: readonly FilterDescriptor[]): z.ZodTypeAny; /** * Build a `z.object({...})` shape from every descriptor's schema — the * registry-driven replacement for hand-assembling `{ project, agent, entity }` * object schemas at each call site (route body schema, MCP tool inputSchema). */ export declare function buildFilterFieldsSchema(descriptors?: D): z.ZodObject>; /** * Assign every present, already-structured filter value (JSON body / MCP * args, already validated by each descriptor's `schema`) from `raw` onto * `target`, using each descriptor's `presentInStructured` predicate. Mutates * `target` in place (matches the pre-registry `if (x) filters.x = ...` style * so call sites can keep building a single `filters` object incrementally). */ export declare function assignStructuredFilterValues(raw: Record | undefined, target: Record, descriptors?: readonly FilterDescriptor[]): void; /** * Assign every present GET query-string filter value from `params` onto * `target`. A field is only parsed when its raw string is non-empty (the * pre-registry `if (params.x) ...` truthy gate), then `parseQueryValue` * converts it into the filter's wire shape. */ export declare function assignQueryFilterValues(params: Record, target: Record, descriptors?: readonly FilterDescriptor[]): void; /** * Derive the post-fusion SQL WHERE-clause fragments + bound params for every * `'sqlColumn'`-kind descriptor present in `filters` — the registry-driven * replacement for src/search/search.ts's hand-rolled `addEqOrIn` helper. * `'custom'`-kind descriptors (e.g. `entity`) are skipped here; their * surface applies its own resolution (candidate-set intersection, etc.) * before or after this call as appropriate. * * A single value produces `col = ?`; a non-empty array produces * `col IN (...)` (OR-semantics); an empty array is treated as "no * constraint" (skipped), never "match nothing" — mirrors FEAT-423's * `type[]` / `project`/`agent` semantics exactly. */ /** * Fold a legacy scalar `repo` value into `project`'s OR-list shape (atom@2 * repo/project consolidation, plan 20260709) — `repo` is accepted as a * UX-alias input on every surface (GET /search, POST /recall, MCP * search_memory/recall_memory) but is never applied as its own SQL column * (see the `repo` descriptor's `apply: { kind: 'custom' }` above). Callers * apply this once, at the single point every surface funnels through * (src/search/search.ts), before building `project`'s SQL clause: * - both absent -> `undefined` (no constraint) * - `repo` only -> that scalar (exact match on `project`) * - `project` only -> unchanged * - both present -> deduped union as a string[] (OR-semantics) — never an * impossible AND of two structurally-different columns, which is what * the pre-atom@2 registry did when both were sent. */ export declare function mergeRepoAlias(repo: string | undefined, project: string | string[] | undefined): string | string[] | undefined; export declare function applySqlColumnFilters(filters: Record, descriptors?: readonly FilterDescriptor[]): { clauses: string[]; params: (string | number)[]; }; /** * Derive the post-fusion SQL WHERE-clause fragments + bound params for every * `'sqlRangeColumn'`-kind descriptor present in `filters` (FEAT-429 SLICE-B) * — parallel to `applySqlColumnFilters` above but for numeric comparison * filters (e.g. `since` -> `created_at >= ?`) rather than equality/IN-list * filters. A separate function rather than widening `applySqlColumnFilters` * itself, since the equal/IN-only callers of that function should not need * to reason about a comparison operator. */ export declare function applySqlRangeFilters(filters: Record, descriptors?: readonly FilterDescriptor[]): { clauses: string[]; params: (string | number)[]; };