Skip to content

feat(privacy): gate rag_queries.answer persistence behind RAG_PERSIST_ANSWER_TEXT (PIA-3)#531

Merged
BigSimmo merged 1 commit into
mainfrom
claude/gate-answer-persistence-flag-4b55da
Jul 12, 2026
Merged

feat(privacy): gate rag_queries.answer persistence behind RAG_PERSIST_ANSWER_TEXT (PIA-3)#531
BigSimmo merged 1 commit into
mainfrom
claude/gate-answer-persistence-flag-4b55da

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

Summary

  • Closes PIA-3: rag_queries.answer was persisting the full generated answer un-redacted — PHI-derived content at rest even though the query itself is hashed, owner-scoped, and 30-day purged.
  • Gates answer-text persistence behind a new RAG_PERSIST_ANSWER_TEXT flag (default off), mirroring the existing RAG_PERSIST_RAW_QUERY_TEXT precedent. With the flag off, answer is written as null and each row records metadata.answer_retained = false.
  • Applied at a single central chokepoint (insertRagQuery) so every logRagQuery caller is covered, plus the promoted-eval-case write, so one switch governs generated-answer persistence everywhere.

Changes

  • src/lib/env.ts — new RAG_PERSIST_ANSWER_TEXT (default false).
  • src/lib/query-privacy.ts — new answerTextForStorage() + answerPrivacyMetadata() helpers.
  • src/lib/rag.tsinsertRagQuery nulls answer and folds answer_retained into metadata by default (covers all four logRagQuery call sites at once).
  • src/app/api/eval-cases/route.ts — its metadata.answer gate moved from the raw-query flag to the dedicated answer flag (free-text note stays on the raw-query flag).
  • scripts/production-readiness.ts — blocks RAG_PERSIST_ANSWER_TEXT=true in a production-like environment (mirrors the raw-query guard).
  • .env.example + docs/privacy-impact-assessment.md — documented; PIA-3 marked Resolved.

Why default-off is safe (the task's "confirm the eval pipeline's real need")

Traced before shipping: no eval path depends on the persisted answer. All eval scripts call the RAG functions with logQuery: false and read the in-memory answer.answer; scripts/promote-query-misses.ts selects specific columns, never metadata.answer. So persistence-off does not affect eval. rag_response_cache.payload (ephemeral ~5-min cache) is intentionally out of scope — nulling it would break caching; PIA-3 was only about the 30-day log column.

Verification

  • typecheck — clean
  • eslint (changed files) — clean
  • prettier --check (changed files) — clean
  • vitest targeted: tests/privacy.test.ts, tests/eval-cases-route.test.ts, tests/rag-answer-fallback.test.ts — all green
  • npm run check:production-readiness — ran; new answer-flag guard is correctly quiet (default off, non-prod). Remaining FAILs are the expected missing-secret / no-.env.local ones for this demo-mode worktree, unrelated to this change.
  • npm run eval:rag / eval:qualitynot run: this is answer persistence (logging), not synthesis or answer post-processing; generated answer content is byte-identical. These gates also require live OpenAI + Supabase keys (provider-confirmation boundary), so left to a maintainer if desired.

Tests added

  • answerTextForStorage / answerPrivacyMetadata unit tests (default → null; on → pass-through).
  • Real-chokepoint proof: a generated answer through answerQuestionWithScope is written to rag_queries.answer as null with answer_retained: false.
  • eval-cases decoupling test: raw-query-on / answer-off keeps the query but still drops the answer.

Clinical Governance Preflight

  • Source-backed claims still require linked source verification before clinical use — unchanged (logging-only change)
  • No patient-identifiable document workflow introduced or expanded — this reduces PHI-derived data at rest
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy) — unchanged
  • Service-role keys and private document access remain server-only — env.ts / query-privacy.ts are server-only; no client exposure
  • Demo/synthetic content remains clearly separated from real clinical sources — unchanged
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative — unchanged
  • Deployment classification/TGA SaMD impact — n/a: no change to clinical decision-support output; only whether the answer text is logged at rest

Notes

  • Generated answer text is byte-identical to before — the only change is whether it is persisted to the rag_queries.answer log column.
  • The answer_retained metadata flag mirrors the existing raw_query_retained, so each logged row self-documents its retention posture.

🤖 Generated with Claude Code

…_ANSWER_TEXT (PIA-3)

rag_queries.answer stored the full generated answer text un-redacted — PHI-derived
content at rest even though the query itself is hashed. Gate answer-text persistence
behind a new RAG_PERSIST_ANSWER_TEXT flag (default off), mirroring the existing
RAG_PERSIST_RAW_QUERY_TEXT precedent.

- env: new RAG_PERSIST_ANSWER_TEXT (default false)
- query-privacy: answerTextForStorage() + answerPrivacyMetadata() helpers
- rag.ts insertRagQuery: central chokepoint nulls the answer and records
  metadata.answer_retained; covers every logRagQuery caller at once
- eval-cases route: answer persistence moved onto the dedicated flag so one
  switch governs answer text everywhere (free-text note stays on raw-query flag)
- production-readiness: block RAG_PERSIST_ANSWER_TEXT=true in production
- docs: .env.example + privacy-impact-assessment (PIA-3 -> Resolved)

The offline eval pipeline reads the in-memory answer (logQuery:false) and never
reads the persisted column, so default-off does not affect eval.

Tests: answerTextForStorage/answerPrivacyMetadata unit tests; rag_queries insert
nulls the answer by default at the real chokepoint; eval-cases decoupling test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@supabase

supabase Bot commented Jul 12, 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 ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@BigSimmo, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 26 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 99ef03db-87ec-4b38-b7a1-cf5b19b22498

📥 Commits

Reviewing files that changed from the base of the PR and between 837e310 and 782de2d.

📒 Files selected for processing (10)
  • .env.example
  • docs/privacy-impact-assessment.md
  • scripts/production-readiness.ts
  • src/app/api/eval-cases/route.ts
  • src/lib/env.ts
  • src/lib/query-privacy.ts
  • src/lib/rag.ts
  • tests/eval-cases-route.test.ts
  • tests/privacy.test.ts
  • tests/rag-answer-fallback.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/gate-answer-persistence-flag-4b55da
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/gate-answer-persistence-flag-4b55da

Comment @coderabbitai help to get the list of available commands.

@BigSimmo
BigSimmo merged commit db3948d into main Jul 12, 2026
15 checks passed
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.

1 participant