Add authorization-filtered PostgreSQL lexical search (I03, #19) #63

Merged
amiryahaya merged 10 commits from feat/19-lexical-search into main 2026-10-26 19:25:14 +01:00
Owner

Refs #19 (I03). Implementation plan: docs/superpowers/plans/2026-10-27-lexical-search.md; guide: docs/operations/lexical-search.md.

What

  • New crate rag-retrieval: LexicalSearch, LexicalQuery (NFC, trimmed, ≤512 chars, no NUL), ResultLimit (1..=50, refused not clamped), LexicalHit (chunk id, version id, score only), payload-free SearchError.
  • Migration 0013_lexical_index.sql: generated tsv (to_tsvector('simple', text)), GIN index, RLS select policy and SELECT (id, version_id, organization, tsv) for rag_app (never text). The startup guard now allows exactly those four columns and rejects anything else.
  • PostgresStore implements LexicalSearch: one parameterized statement applies the whole scope (organization, status active, policy version, classification, group, compartments, need-to-know) mirroring policy.rs; scope expiry is checked first; 2 s statement timeout; errors carry no database detail.
  • Fixtures, a behavior table checked by a test, and a measured baseline (recall@5 = 0.833 on 17 passages / 12 queries; floor 0.80). The two misses are the no-stemming limit. It is a tiny synthetic baseline and does not resolve OD-09.
  • scripts/test-postgres.sh and scripts/coverage.sh did not list the three new suites; they do now, so CI runs them.

Review

Per-task reviews (spec + quality) approved all four tasks. Whole-branch dual pass: ponytail Must-fix list empty; Opus correctness review found no Critical issues and two Important documentation points (below), fixed in 4fbc4f2. Kept despite a ponytail suggestion: the score clamp, because rank fusion (#23) will rely on non-negative scores.

Coverage

Workspace line coverage 94.56% (TEST_DATABASE_URL=… bash scripts/coverage.sh rust, gate 80%); lexical.rs 100%. fmt, clippy -D warnings and gitleaks clean.

Read before merging

  • Accepted risk for #21. The RLS policy on chunks checks only organization and active; group, classification, compartments and need-to-know are applied in the application SQL. rag.organization_id is set by the session itself, so a bug or a compromised rag_app credential could read the near-content lexeme stream (tsv) of every active chunk of any marking in any organization. Nothing is active yet, so there is no exposure today. #21 must clear this with the security authority before it activates versions; the alternative is a later SECURITY DEFINER function returning ids and scores only.
  • Migration 0013 rewrites document_chunks under ACCESS EXCLUSIVE. Measured on a scratch database: 200k chunks (243 MB) took 8.6 s and the table grew 43%. A real corpus needs a maintenance window with the ingest worker drained and ~1.4× the table size in disk headroom.
  • READY means document_versions.status = 'active'. #21 must require the ingestion job state succeeded before activating (the #18 deviation); rag_app cannot read jobs, so it is not checked here.
  • The startup guard rejects extra chunk privileges but does not notice a missing grant: searches would fail as Unavailable at runtime.

Not in this PR

HTTP route, rank fusion (#23), phase timing for NFR-PERF-002, a shared contract suite (add with a second implementation), accent folding and stemming.

🤖 Generated with Claude Code

Refs #19 (I03). Implementation plan: `docs/superpowers/plans/2026-10-27-lexical-search.md`; guide: `docs/operations/lexical-search.md`. ## What - New crate `rag-retrieval`: `LexicalSearch`, `LexicalQuery` (NFC, trimmed, ≤512 chars, no NUL), `ResultLimit` (1..=50, refused not clamped), `LexicalHit` (chunk id, version id, score only), payload-free `SearchError`. - Migration `0013_lexical_index.sql`: generated `tsv` (`to_tsvector('simple', text)`), GIN index, RLS select policy and `SELECT (id, version_id, organization, tsv)` for `rag_app` (never `text`). The startup guard now allows exactly those four columns and rejects anything else. - `PostgresStore` implements `LexicalSearch`: one parameterized statement applies the whole scope (organization, status `active`, policy version, classification, group, compartments, need-to-know) mirroring `policy.rs`; scope expiry is checked first; 2 s statement timeout; errors carry no database detail. - Fixtures, a behavior table checked by a test, and a measured baseline (recall@5 = 0.833 on 17 passages / 12 queries; floor 0.80). The two misses are the no-stemming limit. It is a tiny synthetic baseline and does not resolve OD-09. - `scripts/test-postgres.sh` and `scripts/coverage.sh` did not list the three new suites; they do now, so CI runs them. ## Review Per-task reviews (spec + quality) approved all four tasks. Whole-branch dual pass: ponytail Must-fix list empty; Opus correctness review found no Critical issues and two Important documentation points (below), fixed in `4fbc4f2`. Kept despite a ponytail suggestion: the score clamp, because rank fusion (#23) will rely on non-negative scores. ## Coverage Workspace line coverage 94.56% (`TEST_DATABASE_URL=… bash scripts/coverage.sh rust`, gate 80%); `lexical.rs` 100%. `fmt`, `clippy -D warnings` and `gitleaks` clean. ## Read before merging - **Accepted risk for #21.** The RLS policy on chunks checks only organization and `active`; group, classification, compartments and need-to-know are applied in the application SQL. `rag.organization_id` is set by the session itself, so a bug or a compromised `rag_app` credential could read the near-content lexeme stream (`tsv`) of every active chunk of any marking in any organization. Nothing is `active` yet, so there is no exposure today. #21 must clear this with the security authority before it activates versions; the alternative is a later `SECURITY DEFINER` function returning ids and scores only. - **Migration 0013 rewrites `document_chunks` under ACCESS EXCLUSIVE.** Measured on a scratch database: 200k chunks (243 MB) took 8.6 s and the table grew 43%. A real corpus needs a maintenance window with the ingest worker drained and ~1.4× the table size in disk headroom. - **READY** means `document_versions.status = 'active'`. #21 must require the ingestion job state `succeeded` before activating (the #18 deviation); `rag_app` cannot read jobs, so it is not checked here. - The startup guard rejects extra chunk privileges but does not notice a missing grant: searches would fail as `Unavailable` at runtime. ## Not in this PR HTTP route, rank fusion (#23), phase timing for NFR-PERF-002, a shared contract suite (add with a second implementation), accent folding and stemming. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Migration 0013 adds a generated tsvector column with a GIN index on
document_chunks, an RLS select policy for active versions of the caller's
organization, and SELECT on exactly id, version_id, organization and tsv for
rag_app. The startup guard now rejects any other chunk privilege.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
PostgresStore implements LexicalSearch. Every scope field (organization,
group, classification, compartments, need-to-know, policy version, active
status) is enforced in one parameterized statement under a 2s local
statement_timeout, as the column-limited rag_app role. Scores that are not
finite and non-negative are mapped to 0.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Synthetic English/Malay/mixed fixtures, a behavior table recording what the
simple configuration does (including no stemming and no accent folding), a
recall@5 baseline with a regression floor, and docs/operations/lexical-search.md.
Shared lexical test helpers move to tests/common/lexical.rs; the coverage and
Postgres test scripts now run the lexical suites.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix(storage)+docs: drop dead empty-scope guard, share test helpers, correct tsv exposure notes (#19)
Some checks failed
CI / Web checks (pull_request) Successful in 1m50s
CI / Rust checks (pull_request) Failing after 15m8s
4fbc4f2ca8
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Merge remote-tracking branch 'origin/main' into feat/19-lexical-search
All checks were successful
CI / Web checks (pull_request) Successful in 1m1s
CI / Rust checks (pull_request) Successful in 19m54s
22e865e46c
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.