Skip to content

fix(platform): close the #3803 and #3808 review findings - #3931

Merged
yannickmonney merged 8 commits into
mainfrom
fix/rag-watchdog-error-code-scope-holder
Sep 29, 2026
Merged

yannickmonney merged 8 commits into
mainfrom
fix/rag-watchdog-error-code-scope-holder

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Follow-up to #3803 (TALE-102) and #3808 (TALE-103), from their reviews. I re-checked each item on current main before changing it.

The branch has one commit per item or pair of items, and one commit for the registers. The last commit settles the findings of an independent read-only review of the first seven. The PR squash-merges, so the list below describes the final state.

What changed

1. The watchdog keeps an error code with its text (file_metadata/watchdogs.ts, medium)

  • markRagQueued keeps a retried row's old rag_error and rag_error_code while it waits. The watchdog used to write rag_error on its own, so a lost retry could settle under the previous failure's code. The failed dialog then showed the Settings → Data residency link under the wrong sentence, and REST's indexing.errorCode disagreed with indexing.error.
  • A failure the watchdog writes itself now sets rag_error_code = NULL in the same statement. That covers two writes:
    • the interrupted text. This includes a lost retry whose corpus row recorded nothing, which is the brief's case: embedding_not_configured is not kept;
    • the corpus's copy of a sentence, on a row without a code.
  • A row that carries a code keeps its sentence and code together, whatever its corpus copy says:
    • An already-failed row is left alone. The text correction applies only to a row without a code, clears the code with the text, and is guarded by AND rag_error_code IS NULL.
    • A lost retry whose corpus row failed fails with its own pair, guarded by AND rag_error_code = <its code>.
  • Why the second case keeps the pair: the brief asked for NULL, but that turned out to be a regression. recordIndexingFailure upserts the corpus row with the same sentence, so this is the usual shape of a lost retry. Writing that copy without a code dropped the Settings link, and the embedding save's re-queue (scoped by the code) stopped picking the file up. The review caught this.
  • REST now documents a failed row without an errorCode: the platform settled it without a classified cause, usually because a run stopped; request a retry-indexing. This is in the indexing schema of scripts/openapi/spec.ts and public/openapi.json, and in a new row of the API reference's error-code table (EN/DE/FR). Only descriptions changed, so the contract fingerprint does not move.

2. Settled failures no longer take every slot of a watchdog batch (watchdogs.ts, migration 0138, low)

  • 0138_file_metadata_rag_reconciled.sql adds a nullable rag_reconciled_at_ms, with no index and no backfill. The previous image never reads it. I renumbered it from 0135 after main took 0135–0137; no open PR claims 0138.
  • Each tick stamps the failed candidates it read, in one statement: FOR NO KEY UPDATE SKIP LOCKED, AND rag_status = 'failed', best-effort. It moves no status_changed_at_ms (the clock of the 48 h window) and emits no hint.
  • The failed tier reads the row read longest ago first: coalesce(rag_reconciled_at_ms, rag_queued_at_ms, created_at_ms). A row not read yet counts as read when its run was queued, so a row is read again once every row read or queued before it has had its turn, however many fail meanwhile.
    • I did not use the brief's rag_reconciled_at_ms NULLS FIRST: the review showed that a burst of fresh failures would starve the older rows.
    • I did not use the no-migration fallback (freshest failures first) either: it lets fresher settled failures crowd out an older false failure.
  • A file parked by a bad search index (index_rebuilding, index_repair_failed) is left out of the sweep. Index health re-queues it under SKIP LOCKED and stops at a short batch, so a stamp holding the row's lock would leave the file parked. The two codes are now one list in rag_error_codes.ts, shared with index-health.ts.

3. The upload dialogs give the right reason for .md (lib/shared/file-types.ts, upload-file-row.tsx, low). I chose option (b).

  • hasIndexedExtension reads a name the way the indexer does. A name that ends in its dot (notes.) now has no extension either; that changes no caller's answer.
  • UploadFileRow shows upload.storedButNotIndexedNoExtension for a name that is only an extension: "Stored and downloadable, but not indexed for search: the file name is only an extension." (EN/DE/FR). It keeps the legacy-format hint for a format no extractor reads.
  • Uploads are unchanged. Option (a) would have refused .md with the allowlist's "Unsupported file type. Supported extensions: …, md, …", which is wrong for that name too.

5. A CI-run test pins the holder clause of the folder subtree re-stamp (knowledge/scope-reconcile.test.ts, low)

  • The test drives syncRagFolderSubtree over a fake whose subtree read returns the twin only when the statement lacks NOT EXISTS … o.id < d.id. The real folderTreePaths runs inside paths.ts against the same fake.
  • Two twins under the renamed folder write the holder's path alone. A twin whose holder sits outside the folder writes nothing. A rename of the holder's own folder writes its path.

6. A write that moves a shared ref's holder re-stamps its corpus row (knowledge/service.ts, low)

  • syncRagRefHolderScopes(sql, organizationId, refs) reads each ref's holder with the clause every scope writer uses, and writes that scope. A ref that no active document holds is left to the release seam, or to its trashed document. The call is best-effort and runs after commit.
  • The batch writers share one corpus statement now (writeScopeStamps).
  • Called after the commit of:
    • WebDAV: COPY of a document or a folder; DELETE of either, including a folder cascade and a destination that a MOVE or COPY overwrote; and PUT over an existing document (handlers.ts collects the refs through the transaction, and re-stamps nothing for a rolled-back one).
    • The governance Trash restore.
    • purgeDocument, the funnel of every hard delete.
    • Two lanes beyond the brief: a cloud sync update that replaced a document's bytes, and a knowledge-entry delete.

7. The drift log stops blaming a failed sync for every row (knowledge/release.ts, low)

  • The warning now ends "— a per-edit scope sync failed or did not cover the change, or a shared ref's holder changed without a re-stamp".
  • MIGRATION.md's knowledge row records the one-off burst on the first night after the release that ships fix(platform): close the #3785 mail stamp pass review findings #3808 (not in v0.5.63). The log line would repeat it every night, so it stays out of the line.

8. One settle step for a reconcile release (release.ts, low)

  • settleReleaseOutcome replaces warnReleaseFailures and requeueBlobFailures: the same lines, in the same order.
  • Every call site uses it. For releaseCorpusRefs the blob branch queues nothing.

9. The shared-ref search gap is on record (tests/manual/reference/not-a-finding.md, low)

  • BL-7 says which member misses a shared file: one outside the holder's team finds it in no search, even with access to the copy, and a search scoped to the copy's folder or team misses it. It also records that a COPY into a team folder can narrow the original to that team, and that it costs one drift count on a lane that does not re-stamp.
  • Its pay-off trigger names a first step: a holder order a copy cannot win.
  • automation.md links it. I did not file a tracking issue.

How I verified it

  • Unit tests (CI runs these). They fail against the old sources.

    • watchdogs.test.ts has 32 cases. They cover code pairing (interrupted text, corpus copy, kept pair with the same or an older sentence, codeless copy, the correction guard), rotation (stamp, ordering, parked exclusion) and the existing lock and fault cases.
    • scope-reconcile.test.ts: the subtree holder clause and syncRagRefHolderScopes (the fake reads its NOT EXISTS literally).
    • The new webdav/handlers.holder-scope.test.ts (COPY, DELETE, MOVE and PUT, each after commit and never after a rollback) and retention/purge-document.test.ts.
    • Restore in governance/routes.test.ts, which proves the call runs after commit.
    • onedrive/service.import-deps.test.ts, knowledge_entries/service.delete.test.ts and release.reconcile.test.ts (settle step, drift line).
    • file-types.test.ts and upload-file-row.test.tsx (.md).
  • Mutation checks: 26 mutations, all killed, each by the test named for it. They cover:

    • each rag_error_code clause, the kept-pair branch, the correction guard, the parked exclusion, the rotation order, the stamp (removed, or stamping stuck rows);
    • both holder clauses of the helper, and the subtree clause;
    • each WebDAV collector, and the purge re-stamp removed or moved before the delete;
    • the restore re-stamp removed or moved into the transaction, the sync update and entry delete calls;
    • the settle step's blob branch, hasIndexedExtension, and the row's key.
  • Related suites (server and client, --maxWorkers=1), all passed:

    • before the review fixes, onedrive, knowledge_entries, retention, governance, webdav, knowledge, file_metadata, documents, erasure, backend/jobs, core/knowledge, lib/shared, REST v1-core/v1-projects and both document dialogs: 163 files, 2,372 tests;
    • after the review fixes, file_metadata, knowledge, governance routes, scripts/openapi and backend/db: 34 files, 677 tests;
    • after each rebase, the suites main touched: governance, OpenAPI, i18n and backend/db, 7 files and 325 tests.
    • One "Failed to start forks worker" timeout, on untouched subaddress.test.ts, came from four agents sharing the host; that file passes on its own.
    • services/docs locale and structure tests: 8 files and 41 tests passed. bun run lint:manual is green.
  • Real Postgres 16 with pgvector (no ParadeDB), ITEST_LANES=checkRagWatchdogBatch,checkScopeRefHolder, on this branch rebased on main. CI does not run this suite.

    • 30/31 pass. The only failure is the BM25 boot check, which needs pg_search.
    • Boot migrations ran twice concurrently and applied 0138; the column is nullable bigint.
    • New watchdog checks:
      • a false failure queued after 201 settled failures is adopted in tick 2;
      • the stamps move no status clock and emit no hint;
      • a parked file is never stamped;
      • a lost retry with no corpus row settles as interrupted with no code;
      • a codeless running row takes its corpus error with no code;
      • a lost retry whose corpus row holds the same sentence, or an older one, fails with its own embedding_not_configured pair;
      • a classified failed row stays as it is.
    • New holder checks:
      • trash, restore, a copy sorting first, and the real purgeDocument each re-stamp from the new holder;
      • a ref with no active holder keeps its row;
      • the real WebDAV COPY handler stamps the copy's root path, and DELETE of the copy hands the ref back.
    • The same lanes against main's watchdogs.ts, handlers.ts and retention/service.ts: 24/31. The six new checks fail:
      • the false failure is not adopted in three ticks, and no row is stamped;
      • the lost retry keeps embedding_not_configured under the interrupted text;
      • the older corpus copy overwrites the kept sentence;
      • the purge and the COPY re-stamp nothing.
  • Review. An independent read-only review of the first seven commits confirmed:

    • the stamp cannot block or deadlock;
    • the holder helper matches activeDocumentHoldingRef;
    • every WebDAV sync runs after commit;
    • the settle step is equivalent to the old pair;
    • no other writer writes rag_error without its code.

    Its findings are settled in the last commit: the kept pair, the fair order, the parked exclusion, wording, BL-7 and two test fakes. The migration renumber was done in the rebase.

  • Lint, types, format. oxlint --type-aware --type-check on every changed TypeScript file: clean. oxfmt applied. public/openapi.json was regenerated; spec.test.ts passes.

Left open

  • Item 4: backend:integration in CI. I did not add the job: CI workflows are infrastructure, outside what this task may change. It also needs decisions I cannot make here:

    • a Postgres image with pgvector and ParadeDB pg_search: on plain postgres:16 the knowledge schema fails, and without pg_search the boot BM25 check fails;
    • an S3 service for the S3-gated lanes;
    • a lane selection and a time budget. The full suite has never run in CI.

    Until then, the real-Postgres proofs of this PR run only locally: checkRagWatchdogBatch (the rotation's effect on a real batch, the codes on real rows) and checkScopeRefHolder (holder moves, WebDAV COPY and DELETE). The CI-run unit tests carry each item, but for item 2 they pin the statement rather than its effect.

  • Lanes that move a holder but still leave the row to the nightly pass. The drift line names this cause:

    • a controlled-record replacement (documents/replacement.ts);
    • a knowledge-entry version (knowledge_entries attachEntryDocument);
    • an agent rewrite, and a twin created through the workflow document.create door (documents/agent-write.ts);
    • a second bind of one upload (createDocumentFromUpload). Its queued index run heals it, unless skipRagIndexing is set;
    • a retention expiry flip (sweepDocuments) and a project delete cascade (deleteProject).
  • Pre-existing, found in review, not changed here:

    • A WebDAV folder MOVE into a team folder re-stamps the documents' team_tags, but only their folder path in the corpus (syncRagFolderSubtree), until the nightly pass. The drift line and MIGRATION.md name it.
    • The watchdog's stuck-to-failed writes are not guarded against a Retry pressed between its read and its write. The job's first write heals it.
    • Rows deferred on every tick (an organization whose corpus lookup keeps failing) keep their place at the head of the stuck tier.
    • While a retried row waits, REST shows its previous failure's error and errorCode (indexingStateFrom).
    • A codeless stuck row whose corpus row still holds an earlier attempt's failure is settled with that sentence. Comparing the corpus row's updated_at with rag_queued_at_ms could settle it as interrupted instead. I did not, because it compares the clocks of two databases.
  • BL-7: no tracking issue filed. Its pay-off trigger names the cheapest first step: a holder order that a copy cannot win.

Refs #3803, #3808

The RAG watchdog wrote rag_error without rag_error_code. A retried row
keeps its previous failure's sentence and code while queued (markRagQueued
leaves both until the job's first write), so a lost retry of a file that
had failed for want of an embedding model settled as "interrupted" under
the code embedding_not_configured: the failed dialog showed the Settings
deep link beneath the interrupted sentence, and REST's indexing.errorCode
disagreed with indexing.error.

- Every failure the sweep writes (the corpus's error or the interrupted
  text) clears rag_error_code in the same statement. The sweep classifies
  nothing, and indexer_error would promise a retry no job will run.
- A failed row that carries a code is the app's classified pair and is
  left alone whatever the corpus copy says; that copy is best-effort and
  can be an earlier attempt's sentence. The text correction of a codeless
  row also clears the code and is guarded by rag_error_code IS NULL.
- The failed tier is read in rotation. Migration 0138 adds
  rag_reconciled_at_ms; each tick stamps the failed candidates it read
  (FOR NO KEY UPDATE SKIP LOCKED, best-effort, no status clock, no hint)
  and reads never-stamped rows first, then the stalest stamp. Ranked by
  queue time, the same oldest settled failures filled every batch until
  they left the 48 h window, and a false failure queued after them was
  never reached.
- The REST contract and the API reference (EN/DE/FR) say what a failed
  row without an errorCode means: a run the platform settled after its
  job stopped, which nothing retries by itself. Descriptions only; the
  contract fingerprint does not move.

Refs #3803
A file named `.md` or `.pdf` passes the Documents upload allowlist, which
reads the dotfile's name as its extension, but the indexer reads no
extension from it (Node's extname), so the upload and replace dialogs
showed "not indexed for search (legacy format)" — true that it is not
indexed, false that the format is legacy.

- file-types.ts exports hasIndexedExtension, the indexer's own reading
  of a name; a name ending in its dot (`notes.`) has none either now,
  which changes no caller's answer.
- UploadFileRow picks upload.storedButNotIndexedNoExtension ("the file
  name is only an extension", EN/DE/FR) for such a name and keeps the
  legacy-format hint for a format no extractor reads.

The upload itself is unchanged: the file is stored and downloadable, as
before, and the allowlist still admits it.

Refs #3803
subtreeDocumentFolderPaths feeds syncRagFolderSubtree on a folder rename
or move, and reads each shared ref's holder alone (NOT EXISTS over a
lower-id active document), as the other scope writers do. No CI-run test
covered that clause: removing it kept every unit test green, and only the
checkScopeRefHolder lane, which no workflow runs, noticed.

scope-reconcile.test.ts now drives syncRagFolderSubtree over a fake whose
subtree read answers the twin only when the statement lacks the clause:
two twins under the renamed folder write the holder's path alone, a twin
whose holder sits outside it writes nothing, and a rename of the holder's
own folder writes its path. The real folderTreePaths runs inside paths.ts
against the same fake.

Refs #3808
A shared ref's corpus row carries its holder's scope: the lowest-id
active document holding it. A write that moves the holder without editing
any scope re-stamped nothing, so the row kept the old holder's scope until
the nightly scope pass rewrote it (and counted it as a failed sync), and a
scope-filtered search from the new holder's team or folder missed it.

- knowledge/service.ts: syncRagRefHolderScopes(sql, orgId, refs) reads
  each ref's holder with the clause every scope writer uses and writes its
  scope; a ref no active document holds is left to the release seam, or
  to its trashed document. Best-effort, after commit, like the per-edit
  syncs. The batch writers (the reconcile page, syncRagDocumentScopes and
  the new helper) now share one corpus write, writeScopeStamps.
- Called after the commit of every write that moves a holder here:
  - the WebDAV COPY of a document or a folder, a DELETE of either (a
    folder cascade, and a destination a MOVE or COPY overwrote, too), and
    a PUT that moved a document off its old bytes (handlers.ts collects
    the refs through the transaction);
  - a restore from the governance Trash (syncRagDocumentScope of the
    restored document);
  - purgeDocument, the funnel of every hard delete (user and REST delete,
    folder cascade, retention sweep, erasure, sync prune);
  - a cloud sync update that replaced a document's bytes, and a
    knowledge-entry delete: their old ref, a twin may still hold it.
- scope-holder.integration.ts proves it on real Postgres: the helper after
  each holder move, the real purgeDocument, and the real WebDAV COPY and
  DELETE handlers.

Left to the nightly pass (named in the PR): a controlled-record
replacement, a knowledge-entry version and an agent rewrite moving off a
shared ref, a twin bound through the upload or workflow doors, a
retention expiry flip and a project delete cascade.

Refs #3808
warnReleaseFailures logged a release's corpus-stage failures and skipped
its blob-stage ones, which requeueBlobFailures logged and re-queued to
knowledge.release_refs. Every caller had to pipe the outcome through both:
all three did, but one that used the first alone would have dropped a
failed byte delete without a log line or a re-queue.

settleReleaseOutcome(sql, organizationId, outcome) does both, in the same
order and with the same lines, and every release of the reconcile goes
through it: the blob walk, the stamp pass's corpus-only first walk (whose
blob branch queues nothing) and its second walk. No behaviour change.

Refs #3808
The nightly scope pass warned "the per-edit sync had failed for these"
for every row it corrected. Two causes correct rows with no failed sync:
a shared ref's holder that changed on a lane that does not re-stamp it,
and, once after the upgrade that makes every scope writer write the
holder (#3808), the rows older releases stamped from a copy of the ref.
The first night after that upgrade reported a burst of failures that
never happened.

The line now names both causes and the one-off burst, and the comment
beside it says why.

Refs #3808
- not-a-finding.md: BL-7, the known search gap of a file two documents
  share. It has one index row, stamped with its holder's scope, so a
  search scoped to a team or folder where only the other copy sits does
  not find it; the trigger to pay it off, and the one drift count a COPY
  that becomes the holder costs on a lane that does not re-stamp.
- automation.md: the RAG watchdog row names the failed-row rotation and
  the error codes it writes; the knowledge row names a code-less watchdog
  failure, the holder re-stamps after a write that moves the holder, and
  links BL-7.
- MIGRATION.md: the watchdog row and the knowledge row say the same,
  including the one-off drift burst the first night after the release
  that ships #3808.

Refs #3803, #3808
An adversarial review of this branch found:

- A lost retry usually meets a failed corpus row: recordIndexingFailure
  upserts it with the same sentence. Writing that copy with no code
  dropped the Settings link and the embedding save's re-queue, which is
  scoped by the code. A row that carries a code now fails with its own
  pair, guarded by that code; a row without one takes the corpus copy with
  none, guarded by rag_error_code IS NULL. A lost retry whose corpus row
  recorded nothing still settles as interrupted, without the code.
- Never-read rows first let a burst of fresh failures starve the rows
  read before it. The failed tier now reads the row read longest ago
  first, a row not read yet counting as read when its run was queued:
  coalesce(rag_reconciled_at_ms, rag_queued_at_ms, created_at_ms).
- The stamp could hold the lock of a file parked by a bad search index
  while index health re-queues it under SKIP LOCKED and stops at a short
  batch, leaving it parked. Parked files (INDEX_PARKED_RAG_ERROR_CODES,
  now shared with index-health.ts) are left out of the sweep.
- A failed row without an errorCode is described as a failure settled
  without a classified cause, usually a run that stopped (spec,
  openapi.json, API reference EN/DE/FR).
- The drift warning names an edit whose sync writes less than the whole
  scope (a WebDAV folder MOVE into a team folder re-stamps paths alone)
  and no longer repeats the one-off upgrade note every night; MIGRATION.md
  keeps it.
- BL-7 says who misses a shared file (a member outside the holder's team,
  in every search) and names a first pay-down step; automation.md and
  MIGRATION.md follow the rules above and cite the upload hint's test.

The unit tests cover the kept pair on a same-sentence and an older corpus
copy, the codeless copy, the parked exclusion and the ordering; the
restore test proves the re-stamp runs after commit; the holder fake reads
the NOT EXISTS literally; the lanes gain the realistic fixture, a codeless
row and a parked row.

Refs #3803, #3808
@yannickmonney
yannickmonney merged commit 0c488c4 into main Sep 29, 2026
57 checks passed
@yannickmonney
yannickmonney deleted the fix/rag-watchdog-error-code-scope-holder branch September 29, 2026 15:56
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