Repository navigation
fix(platform): reconcile and report corpus scope drift - #3216
Merged
Merged
Conversation
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.
This was referenced Sep 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
syncRagDocumentScopeandsyncRagFolderSubtreecatch a corpus failure so an unreachable corpus cannotfail 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 surfacehas 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
reconcileDocumentScopeStampscompares each live, file-backed document'sintended stamp against what the corpus holds and corrects the difference. The
daily
knowledge.reconcile_corpuscron runs it per org beside the ref-releasepass, 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 FROMon all four stamped columns, so an in-sync corpus writes nothing and therow 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:
[]instead of NULL for a hub rowstamps a hub document with NULL rather than an empty arrayfolder_pathcopyscannedas the drift countreports the corpus row count as the drift countprefers the tag array over the deprecated single columnOne of those tests found a real trap while I wrote it: without normalizing the
path, the WebDAV lane's
/A/Bspelling and the folder tree'sA/Bdifferforever, so the reconcile would rewrite the same row on every run and report
permanent drift.
I verified the
jsonb_to_recordsetbinding and theIS DISTINCT FROMguardagainst 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.
checkCorpusScopeReconcilemakes that repeatable in the harness: write awrong stamp straight into the corpus, assert the reconcile repairs it, and
assert a second run corrects nothing.
Gate
typecheck0 errors,oxlint --type-awareclean,oxfmt --checkclean,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.