Skip to content

Invalidate RAG cache scope revision on table-fact and image edits (stacked on #3640) - #3650

Merged
BigSimmo merged 4 commits into
codex/full-deep-audit-remediation-20261018from
grok/rag-cache-evidence-revision-20261018
Oct 18, 2026
Merged

BigSimmo merged 4 commits into
codex/full-deep-audit-remediation-20261018from
grok/rag-cache-evidence-revision-20261018

Conversation

@BigSimmo

@BigSimmo BigSimmo commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

This PR extends rag_cache_scope_revision_v1 so that edits to document_table_facts and document_images change the cache scope revision and invalidate cached answers and searches. Before this, editing a fact threshold or action, or an image caption, relevance or searchability flag, could leave a stale cached answer in place.

Stacked on #3640. It must merge only after #3640, inside the owner-chosen production DB window. The base is #3640's branch because this migration replaces the function that #3640's 20261018082910_audit_durable_recovery_pairing_and_cache_fences.sql defines. Once #3640 merges, GitHub retargets this PR to main.

The owner approved this migration at 17:55 AWST on 18 Oct 2026, relayed by Grok Bot. Draft migration: supabase/migrations/20261018095804_rag_cache_scope_evidence_revision.sql, timestamped after main's newest and after #3640's.

Design and cost (gitar's concern)

  • The new table public.document_evidence_revisions holds one bigint counter per document. Statement-level triggers with transition tables, on insert, update and delete for both tables, bump it inside the writing transaction.
  • An UPDATE bumps the counter only when the row actually changes; created_at and search_tsv are excluded. A no-op rewrite does not invalidate the cache.
  • rag_cache_scope_revision_v1 keeps the same signature, digest shape and grants. Each document's row hash now also carries coalesce(r.revision, 0) through a keyed left join. The revision stays one pass over documents plus a PK join, with no per-request scan or hash of the fact or image tables.
  • RAG impact: deploying this changes every row hash once, so all cached answers and searches are invalidated once. After that, only documents whose evidence changes are invalidated.

Tests

  • tests/sql/rag-cache-scope-evidence-revision.sql runs in CI Migration replay on the replayed chain. It shows the revision changes on fact insert, threshold edit, fact delete, image insert, caption edit and relevance/searchable edit. It also shows that a no-op update does not change it, that another document's scoped revision is unaffected, and that deleting the document still cascades and removes the counter.
  • tests/rag-cache-governance-revocation.test.ts shows that an evidence-revision bump changes cacheIndexingVersion and rejects local and shared cached answers and searches.

Housekeeping

  • The migration is sealed with the append-only npm run migrations:seal. schema.sql has the projection appended. The drift manifest's schema_sha256 is synced; Docker isn't available on the box, so the full manifest snapshot will come from CI's regenerated drift-manifest-<run_id> artifact.
  • Auto-merge is deliberately off. AGENTS.md requires an owner-approved window for merging migration PRs, and this PR is stacked on fix: remediate full-deep audit findings and retain release holds #3640.

Not sealed or decided here (owner / clinical)

  • The cache revision now decides whether clinical evidence (table-fact thresholds and actions, image captions and relevance) can be served from cache. The owner should review this as a hazard control. No hazard control is sealed and no clinical sign-off is recorded.
  • The one-time global cache invalidation on deploy needs to fall in the production window.
  • fix: remediate full-deep audit findings and retain release holds #3640's TGA SaMD item and its other sign-offs are untouched.

Possible later follow-up

  • Duplicate-job close-out on retry.

Summary by cubic

Stops cached RAG answers and searches from going stale when a document's table facts or images are edited. Previously the cache scope revision only changed when the document row itself changed, so a fact threshold, image caption, relevance, or searchability edit could leave an outdated cached answer in place. Now those edits bump a per-document evidence counter that feeds into the cache scope revision, invalidating affected cached entries.

  • Adds document_evidence_revisions (one bigint counter per document) and statement-level triggers on document_table_facts and document_images that bump it in the writing transaction; created_at and search_tsv changes and no-op updates don't bump it.
  • rag_cache_scope_revision_v1 keeps its signature, digest shape, and grants; each document's row hash now includes the evidence counter via a keyed join (no per-request scan of the fact or image tables).
  • After deploy, every cached answer and search is invalidated once; afterwards only documents whose evidence changed are invalidated.

Migration

Written for commit 7b47c11. Summary will update on new commits.

View guided diff Turn on auto-fix

Adds draft migration 20261018095804_rag_cache_scope_evidence_revision.sql.
It is stacked on #3640 and must merge after it, because it replaces #3640's
rag_cache_scope_revision_v1.

- public.document_evidence_revisions holds a per-document counter. Statement-level
  triggers on document_table_facts and document_images bump it in the writing
  transaction when a fact or image row is inserted, deleted or changed. Every
  column except created_at/search_tsv counts; a no-op update does not bump it.
- rag_cache_scope_revision_v1 keeps its signature, digest shape and grants, and
  each document's row hash now also carries that counter. The revision stays one
  pass over documents plus a keyed join, with no per-request scan of facts or
  images (gitar's cost concern).
- tests/sql/rag-cache-scope-evidence-revision.sql runs on the replayed chain in
  CI. It shows that a fact threshold edit and image caption, relevance and
  searchability edits change the revision, that a no-op update and an edit on
  another document do not, and that deleting the document still cascades.
- tests/rag-cache-governance-revocation.test.ts shows that a counter bump
  rejects local and shared cached answers and searches.

Sealed with the append-only `npm run migrations:seal`. The owner approved this
migration at 17:55 AWST 2026-10-18, relayed by Grok Bot. The drift manifest's
schema_sha256 follows schema.sql; the full manifest comes from CI's
regenerated artifact. No SQL applied; no clinical or hazard sign-off recorded.
…n-20261018' into grok/rag-cache-evidence-revision-20261018

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigSimmo has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: BigSimmo/PsychSift/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 4384f518-ffba-468b-967d-fc524bc54f2f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T10:15:09.737917Z 998f7c8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 2s —— View job


I'll analyze this and get back to you.

@supabase

supabase Bot commented Oct 18, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy due to reaching the limit of concurrent preview branches.
Go to Project Integrations Settings ↗︎ if you wish to update this limit.


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

Comment thread supabase/drift-manifest.json Resolved
Comment on lines +1 to +3
-- DRAFT. Merge only inside the owner's approved database-change window, and only after
-- 20261018082910_audit_durable_recovery_pairing_and_cache_fences.sql (it replaces that
-- migration's rag_cache_scope_revision_v1). Owner approval: 17:55 AWST 2026-10-18, relayed by Grok Bot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Quality: Owner approval is recorded only as a bot relay, not the owner's own words

The migration header and the PR body record the owner's approval as "17:55 AWST 2026-10-18, relayed by Grok Bot". The repo's Supabase safety rules (docs/decisions/2026-10-15-database-changes-need-owner-permission.md) require the owner's explicit yes, in words, to this specific change before it reaches main. Approval relayed secondhand by an agent can't be checked against that rule. Before merging, get the owner to confirm directly on this PR (a comment or review) and point to it.

Was this helpful? React with 👍 / 👎

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged; not resolving. Grok Bot relayed the owner's approval of this migration at 17:55 AWST 2026-10-18. Automation can't record the owner's own words. The owner needs to confirm this specific change in their own words, and pick the production window, before this merges. Auto-merge is deliberately off.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. This finding stays open until the owner confirms this specific migration in their own words and a production window is chosen. Keeping auto-merge off is the right call until then. I'm not making any code changes for this.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 998f7c81fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/drift-manifest.json Resolved
…n-20261018' into grok/rag-cache-evidence-revision-20261018
The manifest is copied verbatim from artifact drift-manifest-39118948015, which CI Migration replay produced on 998f7c8 from a Docker replay of the chain. It adds document_evidence_revisions, both new functions and the six triggers, and refreshes the def_hash for rag_cache_scope_revision_v1.
@gitar-bot

gitar-bot Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

⚠️ Gitar auto-approved this PR but could not enable auto-merge: an unexpected error occurred.

@gitar-bot gitar-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gitar has auto-approved this PR (configure)

@gitar-bot

gitar-bot Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor
CI failed: One mobile-WebKit Playwright test failed: submitting a new Services query ('housing') never updated the URL from 'crisis', timing out. The analysis marks it unrelated to this RAG cache revision PR, so it looks like a pre-existing UI routing issue rather than something this change introduced.

Overview

Analyzed 8 logs across 1 unique error template. The only failure is a single mobile-WebKit Playwright test (159 passed, 10 skipped, 1 failed) in the Services shared-composer flow, where the q URL parameter stayed at crisis instead of changing to housing. The finding is marked not related to this PR.

Failures

Services shared-composer query not reflected in URL (confidence: high)

  • Type: test
  • Affected jobs: 117656456335
  • Related to change: no
  • Root cause: In tests/ui-tools.spec.ts (around lines 1577-1579), the test fills and submits a new query, but the expected q=housing URL update never occurs and the URL still shows q=crisis. The log does not establish why submission failed, and the PR's changes (RAG cache evidence-revision SQL, governance and manifests) have no evident link to Services UI query handling.
  • Suggested fix: Investigate the Services shared-composer submission flow and shortlist handling around tests/ui-tools.spec.ts:1538-1582. Verify that submitting a new query updates the URL to q=housing and removes the shortlist parameter. If the UI behavior is correct, update the test to match the intended routing.

Summary

  • Change-related failures: None identified. The single failure is marked unrelated to this PR.
  • Infrastructure/flaky failures: None classified. The Playwright browser install retry logic ran without a reported failure.
  • Recommended action: Triage the Services query-submission test separately as a pre-existing or unrelated failure. Confirm whether the test is flaky by checking prior runs on the base branch. This PR's RAG cache changes do not appear to be the cause.
Code Review 👍 Approved with suggestions 1 closed / 2 findings

🔴 High risk · The migration changes clinical RAG cache invalidation for table facts and images; missed revision bumps could leave stale evidence in cached answers.

Extends rag_cache_scope_revision_v1 with a per-document evidence counter so edits to document_table_facts and document_images invalidate cached RAG answers and searches. The drift manifest snapshot concern was resolved by syncing schema_sha256.

  • Open (minor): owner approval for the migration is recorded only as a bot relay in supabase/migrations/20261018095804_rag_cache_scope_evidence_revision.sql. Get the owner to confirm directly on this PR before merging.
💡 Quality: Owner approval is recorded only as a bot relay, not the owner's own words

📄 supabase/migrations/20261018095804_rag_cache_scope_evidence_revision.sql:1-3

The migration header and the PR body record the owner's approval as "17:55 AWST 2026-10-18, relayed by Grok Bot". The repo's Supabase safety rules (docs/decisions/2026-10-15-database-changes-need-owner-permission.md) require the owner's explicit yes, in words, to this specific change before it reaches main. Approval relayed secondhand by an agent can't be checked against that rule. Before merging, get the owner to confirm directly on this PR (a comment or review) and point to it.

✅ 1 closed
✅ Bug: Drift manifest snapshot is stale: only schema_sha256 was hand-synced

📄 supabase/drift-manifest.json:2-5
The PR updates only schema_sha256 in supabase/drift-manifest.json. The snapshot tree still has the old def_hash for public.rag_cache_scope_revision_v1(uuid,boolean,uuid[]) (around line 19728), and it has no entries for the new document_evidence_revisions table, the bump_document_evidence_revisions and document_evidence_rows_changed functions, or the six new triggers. scripts/check-drift-manifest-freshness.ts exists to catch exactly this: it diffs the full snapshot tree and rejects a manifest where schema_sha256 matches but the snapshot doesn't. So the required drift-manifest-freshness check in CI (ci.yml:1553-1557) will fail on this commit. If the PR were merged anyway, the post-merge live-drift comparison would be working from an inaccurate baseline. Fix: download the drift-manifest-<run_id> artifact that CI regenerates, commit it in place of the hand-edited file, and don't merge until the freshness check passes.

🤖 Prompt for agents
Code Review: Extends `rag_cache_scope_revision_v1` with a per-document evidence counter so edits to `document_table_facts` and `document_images` invalidate cached RAG answers and searches. The drift manifest snapshot concern was resolved by syncing `schema_sha256`.
  
  - Open (minor): owner approval for the migration is recorded only as a bot relay in `supabase/migrations/20261018095804_rag_cache_scope_evidence_revision.sql`. Get the owner to confirm directly on this PR before merging.

1. 💡 Quality: Owner approval is recorded only as a bot relay, not the owner's own words
   Files: supabase/migrations/20261018095804_rag_cache_scope_evidence_revision.sql:1-3

   The migration header and the PR body record the owner's approval as "17:55 AWST 2026-10-18, relayed by Grok Bot". The repo's Supabase safety rules (`docs/decisions/2026-10-15-database-changes-need-owner-permission.md`) require the owner's explicit yes, in words, to this specific change before it reaches `main`. Approval relayed secondhand by an agent can't be checked against that rule. Before merging, get the owner to confirm directly on this PR (a comment or review) and point to it.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Approved

For more verification details, comment gitar display:verbose.

Tip

Comment Gitar fix CI or enable auto-apply: gitar auto-apply:on

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Important

Your trial ends in 3 days — upgrade now to keep code review, CI analysis, auto-apply, custom automations, and more.

Was this helpful? React with 👍 / 👎 | Gitar

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Auto-fix isn't included in your plan ↗

Upgrade to Team

@BigSimmo
BigSimmo merged commit 2c0d741 into codex/full-deep-audit-remediation-20261018 Oct 18, 2026
54 of 61 checks passed
@BigSimmo
BigSimmo deleted the grok/rag-cache-evidence-revision-20261018 branch October 18, 2026 12:16
BigSimmo pushed a commit that referenced this pull request Oct 18, 2026
#3650 merged into the old #3640 branch after this rebuild was cut. This
applies exactly that branch's new changes, so the tree now equals the old
branch merged with main. Includes migration 20261018095804 and its seal
entry, which the owner approved by name.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UBtJDgQZJ3eBsxLWte9r4
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