Skip to content

rag.ts decomposition — part 1: quote verification, source block, numeric verification, context selection - #341

Merged
BigSimmo merged 8 commits into
mainfrom
claude/rag-decomp
Jul 7, 2026
Merged

rag.ts decomposition — part 1: quote verification, source block, numeric verification, context selection#341
BigSimmo merged 8 commits into
mainfrom
claude/rag-decomp

Conversation

@BigSimmo

@BigSimmo BigSimmo commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Summary

First tranche of the src/lib/rag.ts decomposition (7,874 → ~7,450 lines), applying the same verbatim move-only playbook used for the ClinicalDashboard decomposition. Four leaf families extracted, all cycle-free, one commit per move with per-move gates (typecheck + focused vitest + lint + prettier):

  1. Quote-verification family → src/lib/rag-quote-verification.tsnormalizeQuoteVerificationText, tableFactQuoteText, sourceTextForQuoteVerification, isExactSourceQuote, sanitizeQuoteCards, sanitizeConflictsOrGaps, enrichGroundedReviewCitations.
  2. Source-block family → src/lib/rag-source-block.tstruncateForModel, compactContextText, richTableSourceContextEnabled, tableSnippetForFact, formatTableFactForSourceBlock, buildRagSourceBlock.
  3. Numeric-verification application → src/lib/answer-verification.tsapplyNumericVerification, unboldUnverifiedNumbers, hasActionableNumericContext now live beside the verifyAnswerNumbers/extractNumericTokens primitives they wrap.
  4. Model-context selection → src/lib/rag-context-selection.tscapPerDocumentCrowding, selectModelContextResults (+ their consts).

Supporting re-homes (all one-way, no new cycles): allowedChunkMapcitations.ts; safeRecord + metadataTextrag-answer-text.ts; appendRoutingReasonrag-routing.ts; and rag.ts's resultCitation was an exact duplicate of citations.citationFromResult, so it now imports that under the old alias (a dedup win). rag.ts re-exports every moved name that tests or routes import, so no consumer changed.

The debt log (docs/process-hardening.md) gained a dated part-1 entry including the measured coupling for the remaining regions: the extractive-answer (L) and answer-quality (M) families are mutually entangled on main and must move together in a dedicated pass; retrieval variants (H) additionally depend on owner-scope helpers; context packing (J) belongs with the cache region (F). The pre-drift region map should not be trusted for part 2.

Verification

  • npm run verify:cheap — pass (runtime, sitemap, lint [5 pre-existing warnings, 0 errors], typecheck, 1,181 unit tests)
  • npm run verify:ui109/109 passed
  • npm run build — pass (production build)
  • npm run verify:release before release or handoff confidence claims
  • npm run format:check — pass (prettier run per move)
  • npm run eval:retrieval:quality (must stay 23/23) — could not run here (needs live keys). These moves are answer-side and verbatim (no retrieval selection/ordering/scoring logic changed), but per the standing gate please run it locally before merge and paste the summary.
  • npm run eval:rag / eval:quality — no answer-generation logic changed (verbatim moves); same caveat as above if you want belt-and-braces
  • npm run check:production-readiness — N/A: no env/privacy/deployment behavior change
  • npm run check:deployment-readiness — N/A

Per-move gates that ran green on every commit: tsc --noEmit, focused vitest (rag-trust, rag-content-accuracy, answer-ranking, answer-verification, rag-context-budget — 43–59 tests per move), eslint, prettier. Every moved block was diff-verified byte-identical against its rag.ts source before deletion.

Clinical Governance Preflight

Code-motion only in the answer pipeline; no behavior, prompt, ranking, or data-access change.

  • Source-backed claims still require linked source verification before clinical use
  • No patient-identifiable document workflow was introduced or expanded without explicit governance approval
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy)
  • Service-role keys and private document access remain server-only
  • Demo/synthetic content remains clearly separated from real clinical sources
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative
  • Deployment classification/TGA SaMD impact was checked when clinical decision-support behavior changed — no clinical decision-support behavior changed

Notes

  • Part 2 (extractive answers + quality gates together, then retrieval variants and the cache region) should start from the coupling notes in the debt-log entry, not the original region map.
  • Moves 7–8 in the approved sequence are retrieval-touching; when they land, the golden retrieval eval is a hard pre-merge gate.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY


Generated by Claude Code

claude added 5 commits July 6, 2026 18:24
…ation.ts (move-only)

Move 1 of the rag.ts decomposition. The 7-function family
(normalizeQuoteVerificationText, tableFactQuoteText,
sourceTextForQuoteVerification, isExactSourceQuote, sanitizeQuoteCards,
sanitizeConflictsOrGaps, enrichGroundedReviewCitations) moved verbatim.
Four tiny shared helpers re-homed to their domain siblings so the new
module stays cycle-free: allowedChunkMap -> citations.ts, safeRecord ->
rag-answer-text.ts, appendRoutingReason -> rag-routing.ts, and rag.ts's
resultCitation was an exact duplicate of citations.citationFromResult so
it now imports that under the old alias. rag.ts 7,874 -> 7,740 lines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY
…ve-only)

Move 2 of the rag.ts decomposition. truncateForModel, compactContextText,
RagSourceBlockOptions, richTableSourceContextEnabled, tableSnippetForFact,
formatTableFactForSourceBlock, and buildRagSourceBlock moved verbatim.
metadataText re-homed to rag-answer-text.ts beside safeRecord. rag.ts
re-exports buildRagSourceBlock + truncateForModel so existing consumers
(tests/rag-trust, tests/rag-content-accuracy) are unchanged. rag.ts
7,740 -> 7,589 lines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY
…fication.ts (move-only)

Move 3a of the rag.ts decomposition. actionableNumericAnswerPattern,
hasActionableNumericContext, applyNumericVerification, and
unboldUnverifiedNumbers moved verbatim next to the verifyAnswerNumbers /
extractNumericTokens primitives they wrap. rag.ts re-exports
applyNumericVerification + unboldUnverifiedNumbers for existing test
consumers. rag.ts 7,589 -> 7,484 lines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY
…on.ts (move-only)

Move 4 of the rag.ts decomposition. capPerDocumentCrowding,
selectModelContextResults, maxContextChunksPerDocument, and
fastRoutineModelContextLimit moved verbatim; rag.ts re-exports the two
functions for existing consumers (tests/rag-context-budget). The adjacent
context-packing family (packedContextCacheKey/packAdjacentSourceContext)
stays for the cache-region move — it depends on stableHash and
committed-generation helpers that belong there. rag.ts 7,484 -> 7,446 lines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY
…for remaining moves

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XPVNBVo4cg9PEYtNhJZBQY
@supabase

supabase Bot commented Jul 6, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
@BigSimmo
BigSimmo marked this pull request as ready for review July 7, 2026 05:18
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@BigSimmo
BigSimmo enabled auto-merge July 7, 2026 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants