Skip to content

fix(platform): reconcile and report corpus scope drift - #3216

Merged
Israeltheminer merged 1 commit into
mainfrom
fix/reconcile-corpus-scope-stamps
Sep 4, 2026
Merged

Israeltheminer merged 1 commit into
mainfrom
fix/reconcile-corpus-scope-stamps

Conversation

@Israeltheminer

Copy link
Copy Markdown
Contributor

Closes #2990.

A document's corpus scope stamp could disagree with the document
indefinitely, and nothing said so.

Why there was no backstop

The per-edit syncs are best-effort by contract: syncRagDocumentScope and
syncRagFolderSubtree catch a corpus failure so an unreachable corpus cannot
fail the edit that already committed. That part is right.

Their note names the next re-index as the backstop. But a scope or folder
change never re-embeds — that is the whole point of a scope-only sync — so for
exactly this class of edit there is no next re-index. A corpus briefly
unreachable for one edit leaves that row mis-stamped forever.

The issue was filed against 0.4's syncRagDocumentScopes. On 0.5 the surface
has grown: two best-effort syncs now, covering team/project scope and
folder path, both catch { console.warn }.

What is and isn't at stake

Not a leak. Retrieval re-checks every hit against live truth
(decideRetrievable), so a wrongly-scoped row is rejected after the fact.

A silent cost instead. Every scope column NULL reads as an org-wide hub row
in the SQL pre-filter, so the row is handed to every caller and thrown away
afterwards — the pre-filter stops pre-filtering, and nothing reports that it
has.

The fix

reconcileDocumentScopeStamps compares each live, file-backed document's
intended stamp against what the corpus holds and corrects the difference. The
daily knowledge.reconcile_corpus cron runs it per org beside the ref-release
pass, and logs a warning naming the count whenever it corrects anything.

This is the shape the issue suggested — "a periodic reconcile comparing
document scope against corpus stamps… the RAG watchdog already does an
analogous corpus reconcile and could carry it."
That cron now exists, so it
carries this too.

One statement per org, bounded at 1000 documents. The guard is IS DISTINCT FROM on all four stamped columns, so an in-sync corpus writes nothing and the
row count is the drift count.

Verification

The derivation is what a unit test can hold, and getting it wrong would
rewrite every row on every run and report the whole corpus as drifted:

Mutation Went red
stamp [] instead of NULL for a hub row stamps a hub document with NULL rather than an empty array
trust the row's stale folder_path copy all three folder-path assertions
report scanned as the drift count reports the corpus row count as the drift count
prefer the deprecated single team column prefers the tag array over the deprecated single column

One of those tests found a real trap while I wrote it: without normalizing the
path, the WebDAV lane's /A/B spelling and the folder tree's A/B differ
forever, so the reconcile would rewrite the same row on every run and report
permanent drift.

I verified the jsonb_to_recordset binding and the IS DISTINCT FROM guard
against a real Postgres by hand before writing the code — a three-row fixture
where the in-sync row was skipped and the update reported 2, not 3.
checkCorpusScopeReconcile makes that repeatable in the harness: write a
wrong stamp straight into the corpus, assert the reconcile repairs it, and
assert a second run corrects nothing.

Gate

typecheck 0 errors, oxlint --type-aware clean, oxfmt --check clean,
knowledge suites 61 passed (7 files).

I could not run the harness lane itself — the suite aborts early on the
knowledge/embedding step, which needs an S3 endpoint this machine has no MinIO
for.

A document's corpus scope stamp could disagree with the document
indefinitely, and nothing said so.

The per-edit syncs are best-effort BY CONTRACT: `syncRagDocumentScope` and
`syncRagFolderSubtree` catch a corpus failure so an unreachable corpus
cannot fail the edit that already committed. Their note names the next
re-index as the backstop — but a scope or folder change never re-embeds, so
for exactly this class of edit there is no next re-index. A corpus that was
briefly unreachable for one edit leaves that row mis-stamped forever.

A stale stamp is not a leak. Retrieval re-checks every hit against live
truth (`decideRetrievable`), so a wrongly-scoped row is rejected after the
fact. It is a silent cost instead: every scope column NULL reads as an
ORG-WIDE hub row in the SQL pre-filter, so the row is handed to every
caller and thrown away afterwards. The pre-filter stops pre-filtering, and
nothing reports that it has.

`reconcileDocumentScopeStamps` compares each live, file-backed document's
intended stamp against what the corpus holds and corrects the difference.
The daily `knowledge.reconcile_corpus` cron now runs it per org beside the
ref-release pass, and logs a warning naming the count whenever it corrects
anything — every corrected row is an edit whose corpus write had failed
silently.

One statement per org, bounded at 1000 documents. The guard is
`IS DISTINCT FROM` on all four stamped columns, so an in-sync corpus writes
nothing and the row count IS the drift count, not the row count.

## Tests

The derivation is what a unit test can hold, and getting it wrong would
rewrite every row on every run and report the whole corpus as drifted:

| Mutation | Went red |
| --- | --- |
| stamp `[]` instead of NULL for a hub row | `stamps a hub document with NULL rather than an empty array` |
| trust the row's stale `folder_path` copy | the three folder-path assertions |
| report `scanned` as the drift count | `reports the corpus row count as the drift count` |
| prefer the deprecated single team column | `prefers the tag array over the deprecated single column` |

The `IS DISTINCT FROM` guard is SQL, so `checkCorpusScopeReconcile` drives
it in the harness: write a wrong stamp straight into the corpus — the state
a failed sync leaves — then assert the reconcile repairs it AND that a
second run corrects nothing. The second half is the load-bearing one.

Refs #2990.
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.

Bug: syncRagDocumentScopes can fail silently, leaving corpus scope stamps stale

1 participant