Skip to content

fix(platform): end an indexing run whose document was released - #3439

Merged
larryro merged 3 commits into
mainfrom
fix/knowledge-index-release-race
Sep 20, 2026
Merged

larryro merged 3 commits into
mainfrom
fix/knowledge-index-release-race

Conversation

@larryro

@larryro larryro commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

What

A document rewritten, replaced or deleted while its rag.index_file job is queued or running never comes back into the corpus, and the job ends quietly instead of failing.

Why

Seen on the dev stack (09-20 17:07 UTC, org test): an automation run wrote setup-state.json and rewrote it two seconds later. The rewrite rotated the document's ref, releasePreviousBlob trashed the old file row and knowledge.release_refs deleted the old ref's corpus row — while the job indexing the old ref was still embedding. Its chunk insert failed with

PostgresError: insert or update on table "chunks" violates foreign key constraint "chunks_document_id_org_fkey"

the job burned a retry, and in the shapes where the old file row survives (a replacement keeps it bound, a late first attempt) the retry re-indexed the dead ref until the daily knowledge.reconcile_corpus sweep.

indexDocument claimed its corpus row in one short transaction, embedded, then wrote chunks in another, with nothing in between checking the ref was still alive; a continuation slice whose row vanished re-claimed and re-embedded from chunk 0; indexUploadedFile never asked whether anything still referenced the file.

How

One concept, three guards — a released document ends the run as released (a new skipped outcome: no failed status, no rethrow, no retry):

  • indexUploadedFile asks the release seam's own corpus-liveness predicate (isCorpusRefLive, moved with assessRefLiveness into domains/knowledge/liveness.ts so the service can import it without a cycle) before it downloads anything, and hands the same question to the indexer as stillWanted.
  • indexDocument asks stillWanted after claiming the corpus row, on every slice, and drops the claim on no. After the claim, never before: a release always follows the commit that killed the ref, so a release that ran before the claim is visible to a check made after it, and one that runs later finds the claimed row and takes it.
  • writeChunks / cloneChunks hold the document row FOR KEY SHARE inside the write transaction and stop when it is gone (a concurrent release waits for the commit and cascades — never an FK violation); a continuation slice that no longer finds the row the first slice claimed (resumeDocumentId) stops instead of claiming a fresh one.

Proof

  • Unit: core/knowledge/indexing.test.ts (write-time guard, post-claim question and its ordering, continuation guard, clone path, proceed-when-wanted), domains/knowledge/service.released.test.ts (early gate, released handling, the wired question); the three sibling fakes answer the liveness query.
  • Real Postgres + MinIO, backend:integration, new lane checkIndexingReleaseRace — the fake embedding server performs the rotation and runs the release inline from inside the embedding request, on the real pg-boss worker:
    • release during the embedding of a single-slice document → job completed, retry_count 0, no corpus row for the old ref, old file row gone;
    • release on a later slice of a multi-slice document → same;
    • a job for an already-superseded ref (replacement finalized through the real door) indexes nothing and writes no status.
  • Gates: tsc, oxlint --type-aware, oxfmt --check, lint:manual, knowledge vitest (149) all green. Full backend:integration run: 706/708 checks across 151 lanes; the two red lanes are task-agent turn drive (reused host: exec→harvest→settle) (latest agent comment is the deliverables notice, not the report text) and sandbox teardown revokes the session gateway key (gateway admin answered 503 on expiryHandBack) — both outside knowledge indexing, no [knowledge] line near either, not re-run on main (the harness has no lane filter).

Not in scope

The [automations] trigger scan: N due schedule(s) have no deployed version to run lines from the same log are by design: provisioning binds every builtin pack's schedule enabled while the pack starts Not deployed, and the dev database has four organizations with nothing deployed.

tests/manual/reference/automation.md carries the new row.

Also here: the CLI workflow's Windows red on main

The last main run of the CLI workflow (35525098046, after #3438) failed its Windows build in config-source.test.ts: a git-fixture test took 5.7 s and bun test's 5 s default killed it. 1d1c532 had raised the budget per file with setDefaultTimeout(30_000), but twelve fixture-building files never got it. eb3b97769 makes it one rule: the CLI package's test script runs bun test --timeout 30000 and the workflow's unit and smoke steps go through the script. Touching tools/cli and the workflow file makes the CLI workflow run on this PR, so the fix is proven here on all three platforms.

… module

`assessRefLiveness` — the one definition of corpus-liveness and
blob-liveness for a blob ref — moves from `domains/knowledge/release.ts`
to `domains/knowledge/liveness.ts`, unchanged. The release seam imports
the knowledge service for the scope reconcile, so the service could not
import the predicate from it without a cycle; the indexer is about to ask
the same question.
The corpus is keyed by blob ref, and a ref dies the moment its document
is rewritten, replaced or deleted: the release seam deletes the corpus
row, often seconds after the `rag.index_file` job for the same ref was
queued. An agent that wrote `setup-state.json` and rewrote it two seconds
later made the first job fail its chunk insert with 23503
`chunks_document_id_org_fkey` (the row had been released while the slice
was embedding), burn a retry, and — wherever the old file row survived —
a retry or a later slice re-indexed the dead ref until the daily
reconcile.

Three guards, one concept — a released document ends the run quietly:

- `indexUploadedFile` asks the release seam's own corpus-liveness
  predicate (`isCorpusRefLive`) before it downloads anything, so a job
  that runs after the rotation indexes nothing, and hands the same
  question to the indexer as `stillWanted`.
- `indexDocument` asks `stillWanted` AFTER claiming its corpus row, on
  every slice, and drops the claim on no. After the claim, never before:
  a release always follows the commit that killed the ref, so a release
  that ran before the claim is visible to a check made after it, and one
  that runs later finds the claimed row and takes it.
- The write path holds the document row `FOR KEY SHARE` inside the write
  transaction and stops as `released` when it is gone (a concurrent
  release waits for the commit and cascades — never an FK violation);
  a continuation slice that no longer finds the row the first slice
  claimed stops instead of claiming a fresh one for the dead ref.

`released` is a new `skipped` outcome: no `failed` status, no rethrow,
no retry. Unit tests cover the write-time guard, the post-claim question
and its ordering, the continuation guard, the clone path and the service
handling; `checkIndexingReleaseRace` proves it on the real worker — the
release landing during the embedding of a single-slice and of a later
slice completes the job on its first attempt with no corpus row left for
the old ref, and a job for an already-superseded ref indexes nothing.
The CLI workflow on main (run 35525098046, after #3438) failed its
Windows build in config-source.test.ts: "a self-consistent changed
archive still cannot claim the original Git pack tree" took 5.7 s and
bun test's 5 s default killed it. The test builds the git release
fixture; Windows runners stall on git for seconds at a time, and Linux
and macOS finish the same test in well under a second.

1d1c532 raised the budget per file with `setDefaultTimeout(30_000)`,
but twelve test files that build the same fixture never got it, and
three of the four tests in this file carried a per-test 30 s while the
fourth did not. One rule instead of a per-file convention: the package
`test` script runs `bun test --timeout 30000`, and the workflow's unit
and smoke steps go through the script, so no test file has to remember
the budget and the local run matches CI.

Linux, tools/cli: 1218 pass, 19 skip, 1 fail — provision.test.ts binding
port 3005 against a running dev stack, the documented environmental red.
@larryro
larryro merged commit 3b31d62 into main Sep 20, 2026
56 checks passed
@larryro
larryro deleted the fix/knowledge-index-release-race branch September 20, 2026 18:15
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