Skip to content

fix(platform): close the RAG watchdog review round after #3782 - #3803

Merged
yannickmonney merged 4 commits into
mainfrom
fix/rag-watchdog-settled-failures
Sep 29, 2026
Merged

yannickmonney merged 4 commits into
mainfrom
fix/rag-watchdog-settled-failures

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Follow-up to #3782 (TALE-92), from its review. I re-checked every item on current main before changing it. The Convex original of the watchdog (convex/file_metadata/rag_watchdog.ts at 94135ac) had both the same-error skip and a stuck-rows-first read. The Postgres port (fd5c009) dropped both.

What changed

1. Settled failures no longer starve stuck rows (file_metadata/watchdogs.ts, medium)

  • Before this change, when the corpus reported failed, the corpus-failure branch rewrote the row every tick. Each rewrite moved status_changed_at_ms, which is also the clock of the 48 h failed window, so a job-failed row never left the window, and its document lists refetched every five minutes. The candidate read ranked those rows with the stuck ones by queue time alone under LIMIT 200.
  • The candidate read now selects rag_error. A failed row that already reads its corpus row's error returns null: no write, no hint, not counted.
  • When the corpus error differs, a text-only UPDATE … SET rag_error fixes it, guarded by AND rag_status = 'failed' (a retry may have re-queued the row since the read). It leaves status_changed_at_ms alone. It is still hinted and not counted.
  • One case goes past the brief. A secret-scan refusal records failed with a NULL error on the corpus row, while the file row carries the refusal's sentence. Main (and the brief's status.error ?? RAG_INTERRUPTED_MESSAGE) would overwrite that sentence with the generic "interrupted, use Retry indexing" text. That breaks the module's own rule that an already-failed row keeps its real error. Such a row now keeps its own error. Only a failed row with no error at all gets the generic text.
  • The candidate read is now ORDER BY (rag_status = 'failed'), coalesce(rag_queued_at_ms, created_at_ms), so stuck rows come first, as 0.4 read them.
  • The brief also asked to exclude settled rows from the read itself. Plain SQL can't do that: the corpus lives in the per-org knowledge database, and a marker column would be a write, which the done-when rules out. Instead, settled rows no longer renew their window, so each drops out within 48 h, and they sort after every stuck row.

2. A held row lock no longer stalls the tick (file_metadata/watchdogs.ts, low)

  • Each settle write (adopt, corpus failure, text correction, revival, interrupted run) now goes through settleWrite: its own sql.begin with SET LOCAL lock_timeout = '2s'. This follows the rest/project-scope-check.ts precedent and leaves createSql untouched.
  • A lock held past 2 s now throws lock_not_available into fix(platform): re-queue renamed synced files and isolate watchdog faults #3782's per-row isolation, so only that row is deferred.

3. The sync scheduling test feeds only what the scheduler reads (onedrive/service.rag-scheduling.test.ts, low)

  • fakeSql takes the stored file directly and answers the document read with [{ fileRef }] only.
  • One case pins the columns each read selects, so the fake cannot drift again.
  • The retitled and reverse cases become cases on the stored name. An extension-less stored name (README) is added and goes to the terminal lane.

4. One rule for a name without an extension: option (a), name-only (lib/shared/file-types.ts, low)

  • isRagIndexableFile and shouldRagIndexOnUpload now read the extension the way the indexer's router does (Node's extname). There is no MIME fallback, and a dotfile such as .md has no extension either: extractExtension('.md') is md, but extname('.md') is ''.
  • I chose (a) because the indexer, markRagUnsupportedIfNoExtractor, the rag_fetch miss and data migrations 0128/0129 already decide by name. The fallback's justification (ensureExtension) no longer exists. The Documents upload allowlist (isAllowedDocumentUpload) already refuses an extension-less name.
  • Outcomes do not change: such a file ended unsupported_type before and still does. It just gets there without a job, and without the dialogs or composer promising an index first.
  • isRagIndexableFile loses its unused content-type parameter; the call sites are the two dialogs, replacement.ts and the sync scheduler. The sync scheduler no longer reads content_type.
  • backend/core/knowledge/rag_unsupported.test.ts runs both gates against unsupportedByName: a table of (name, type) pairs that includes the four named in the brief, plus every extension either list knows. That last check also enforces the "MUST stay in sync" comment on RAG_INDEXABLE_EXTENSIONS.

Other changes: tests/manual/reference/automation.md rows 155–157 are updated. The new lane backend/domains/file_metadata/watchdogs.integration.ts (checkRagWatchdogBatch) is registered in backend/integration-check.ts.

How I verified it

  • Unit: watchdogs.test.ts has 22 cases. The 6 new ones fail on main's watchdogs.ts and pass here. The fake gained begin (statements carry their transaction) and now answers only the columns the candidate read selects.
    • A mutation run killed all 11 watchdog mutations: dropping the skip, bumping the clock in the text fix, dropping the guard, the generic text over the row's own error, the old ORDER BY, no SET LOCAL, SET on the pool, not selecting rag_error, counting the text fix, and adopt or revive bypassing settleWrite.
    • The parity test killed all 3 gate mutations: the MIME fallback, extractExtension's dotfile rule, and a leading-dot slip. A scheduler mutation re-adding title, mime_type to the document read is killed.
  • Related suites: --project server over documents/replacement*, files/, knowledge/, onedrive/, google_drive/, file_metadata/, core/knowledge/, the extraction router, lib/shared/ and db/data-migrations: 119 files, 1729 tests, all passed. --project client over both dialogs, shared/files/ and the chat hooks: 15 files, 166 tests, all passed.
  • Real Postgres (backend:integration, a filtered run on a local PG 16 + pgvector + pg_search cluster with moto; not a CI job):
    • Lanes checkFiles,checkDocuments,checkOneDriveSync,checkRecoverySweeps,checkRagStatusHintScope,checkRagWatchdogBatch: 81/82. The one failure is files upload/serve/delete … blobGone=false: moto keeps the deleted blob. That check fails on every local moto run, and nothing here touches blob deletion.
    • The four new checks all passed:
      • 201 settled failures queued ahead of a stale running row: the row is failed in one tick;
      • 201/201 settled rows unchanged after two ticks, with 0 hints;
      • the corrected row keeps its clock and gets 1 hint;
      • a FOR UPDATE held on the middle of three rows: the tick returns after 2015 ms, first and last rows failed, the held row still running.
    • Both binding-race checks still pass under the 2 s timeout.
    • The same lane against main's watchdogs.ts failed 4/4:
      • the stuck row stayed running after a tick;
      • 0/201 rows unchanged, with 2 hints;
      • the clock moved, with 2 hints;
      • the tick blocked the full 15 s, and the held row was failed after release.
  • Type check: a scoped tsc over every changed file and integration-check.ts passed. oxlint --type-aware on each changed file and oxfmt --check are clean. The workspace-wide gates are left to CI.

Left open

  • A file named only by a dotfile extension (.md) that the upload allowlist admits now shows the dialog's "stored but not indexed (legacy format)" hint. "Not indexed" is correct, since the indexer refuses it. "(legacy format)" is not a fitting reason, but that is an odd edge and I left the message alone.
  • If the product wants extension-less files indexed (option (b): an effective name from the MIME type in the indexer, the unsupported writer and the router), that is a separate feature.

The RAG watchdog rewrote a failed row whose corpus row held the same
error on every tick. Each rewrite moved status_changed_at_ms, the clock
of the failed window, so the row never left the window, and its document
lists refetched every five minutes. The candidate read ranked those rows
with the stuck ones by queue time alone, so two hundred of them filled
the batch and a row that got stuck after them was never reached.

A failed row that already reads its corpus row's error is now left as it
is, with no write and no hint. Another corpus error is written in place
without moving the status clock, and a corpus row that failed without an
error leaves the row its own. Stuck rows are read ahead of every failed
row, as the 0.4 sweep read them.
The app pool sets no lock timeout, so a settle write whose row another
transaction held waited as long as that transaction ran. The per-row
isolation only ever saw writes that threw, so the whole tick stalled
behind the lock, and every organization after it went unswept until
pg-boss expired the job at 600 s.

Each settle write (adopt, corpus failure, text correction, revival,
interrupted run) now runs in a transaction of its own under SET LOCAL
lock_timeout = '2s'. A row held past that throws lock_not_available,
which defers that row alone to the next tick. The rest of the pool keeps
its waits.
isRagIndexableFile and shouldRagIndexOnUpload fell back to the content
type's extension when a file name had none, a relic of an upload helper
that appended one before the old RAG service saw the file. The indexer
decides by the name alone, so an extension-less README stored as
text/plain was queued by the register door, a replacement, the sync
scheduler and the chat backstop, and promised by the dialogs and the
composer, and its run then ended unsupported_type.

Both gates now read the extension the way the indexer's router does,
Node's extname rule: no content-type fallback, and a dotfile such as .md
has none. isRagIndexableFile loses its unused content-type parameter,
and the sync scheduler no longer reads content_type. A parity test runs
names and types through both gates and unsupportedByName, and every
extension either side lists.
The scheduler reads a synced document's file_ref and nothing else, and
decides on the stored file's name. The test still answered the document
read with a title and a mime type, and took the stored file's name and
type from them by default, so most cases set the stored file only
through a document title the code never reads.

The fake now takes the stored file directly and answers the document
read with the blob reference alone. One case pins the columns each read
selects, so the fake cannot drift from the reads again. The retitled and
reverse cases, which could no longer inject a title, become cases on the
stored name, and an extension-less stored name joins the terminal lane.
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