Repository navigation
fix(platform): close the #3785 mail stamp pass review findings - #3808
Merged
yannickmonney merged 9 commits intoSep 29, 2026
Merged
Conversation
The corpus reconcile's re-queue of the bytes it could not delete and the mail lanes' release each chunked their refs into knowledge.release_refs jobs with a loop and a 500-ref constant of their own. Both now go through queueRefRelease and RELEASE_REFS_PER_JOB in knowledge/release-queue.ts, a module of its own so the mail lanes reach it without the release seam's imports. queueMessageRefRelease keeps its name for the mail lanes and delegates: an enqueue that fails still fails the caller's transaction. The reconcile hands in onChunkError, so a failed job is logged as before and the next one is still queued.
The reconcile logged every failed release as "reconcile release failed for <ref> (<stage>): <msg>" and only then re-queued the blob-stage refs to knowledge.release_refs. A re-queue that went through logged nothing, so an operator read a byte delete the job would retry as final. requeueBlobFailures now logs the blob-stage lines itself, after the enqueue. Each ends "re-queued to knowledge.release_refs", or "not re-queued" when its job could not be queued, followed as before by the job's "could not re-queue ... their bytes stay" line with the error. warnReleaseFailures keeps the corpus-stage lines.
A WebDAV COPY leaves two documents on one blob ref, and the corpus has one row for it. The indexer indexed an unbound file holding such a ref as its lowest-id active holder (activeDocumentHoldingRef), but the nightly scope pass and syncRagDocumentScopes wrote every active document: within a page Postgres applied either twin to the row, a later page's twin overwrote an earlier one's, and the row was drift again every night. On real Postgres the old pass corrected the same row twice on every run. Both statements now read each ref's holder alone, the active document no lower-id active document shares its ref with, so they write the scope the indexer indexes the ref as. The indexer reads the holder first for a file filed into a document too (the file row of a COPY stays with its source, whose id can be the higher one), and falls back to the bound document when no active document holds the ref. The new checkScopeRefHolder lane proves it on real Postgres: a trashed lowest id, the holder and its twin on pages of their own, and a batch sync of the twin alone.
A clear took the stamps off up to 100 corpus rows, then read their backing again on an app transaction, and the rows stayed locked until that read answered. Its only bound was the app transaction's server-side statement_timeout. A half-open app connection (peer gone, no reset) never delivers that timeout, so postgres.js waited until TCP gave up, minutes later, while indexers and releases on those refs waited behind the locks. The recheck now races a 15 s client-side bound, above the server's 10 s. When it lapses the corpus transaction rolls back with its stamps still on, and the next night retries the clear. The clear is what its corpus transaction did: the read-only app transaction around it is waited for only as long again, so the pass goes on past a dead connection instead of queueing behind its ROLLBACK. A commit whose app transaction does not end cleanly still counts and is logged as before. On Postgres 16, with the recheck's backend stopped (SIGSTOP), the first clear's rows were lockable again after 15.4 s and the pass went on to the next clear at 30 s. Before, they stayed locked as long as the backend was stopped.
…isters The emailed-attachment rows of automation.md and MIGRATION.md now say that a ref several documents share is indexed and scoped as its holder, that a re-queued byte delete goes out in jobs of at most 500 refs and says so in its log line, and that the clear's recheck has a 15 s bound of its own which the pass does not wait out. The scope stamps row of automation.md names the holder rule and the checkScopeRefHolder lane, and the attachment row lists release-queue.test.ts.
The scope pass, the batch scope sync and the indexer now agree on a ref's holder, but the two other scope writers still wrote whichever document was edited. syncRagDocumentScope wrote an edited twin's scope over the holder's, and syncRagFolderSubtree wrote every document under a renamed folder, two twins under it in one statement. The next night the scope pass wrote the holder's scope back and reported "the per-edit sync had failed for these" after every edit of a non-holder twin (a WebDAV COPY, or one upload bound as two documents). syncRagDocumentScope now writes the scope of its ref's holder (activeDocumentHoldingRef), and the edited document's own only when no active document holds the ref, so an edit of a trashed document still reaches its row. subtreeDocumentFolderPaths reads each ref's holder alone, as the reconcile does. checkScopeRefHolder covers both on real Postgres. Without this change, the twin's edit writes its own team and the twin's folder rename writes its folder's path. The indexer's holder test now also pins the order it reads the holder in.
releaseUnlistedTaskBlobRefs handed every ref a hard delete orphaned to one knowledge.release_refs job, and a project's whole task tree can orphan thousands. It now queues them through queueRefRelease, at most RELEASE_REFS_PER_JOB to a job, as the mail lanes and the reconcile do, still in the retire's own transaction. The docstrings that said every release lane bounds its jobs are now true, and say which lanes they mean.
The fake-timer test proved the clear rolls back when its recheck never answers and that the pass then goes on, but a pass that waited for the dead app transaction's end for a millisecond or for the whole 15 s bound passed alike. It now checks the pass is still waiting a millisecond before that second bound lapses, and has gone on to the next clear once it has.
The registers and the stamp clear's docstring said the pass goes on past a dead connection. The bound covers a connection that stops answering during the clear, its recheck and its app transaction's end. One already gone at the clear's BEGIN holds no corpus row, and holds the pass as any other read of it would. Both now say that. The emailed-attachment rows also name every scope writer as following the holder, and one upload bound as two documents as a second way to share a ref. They name a task retire among the lanes that bound their release jobs.
yannickmonney
deleted the
fix/mail-stamp-release-helper-holder-recheck
branch
September 29, 2026 06:04
yannickmonney
added a commit
that referenced
this pull request
Sep 29, 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.
Follow-up to #3785, from its review. Each item was re-checked on main before the fix. There is one commit per item, a commit for the registers, and four commits from an adversarial review of the first five. Each commit passes the touched suites and the type check on its own.
What changed
One release-enqueue helper (
domains/knowledge/release-queue.ts, new). The reconcile's re-queue of the bytes it could not delete (requeueBlobFailures,release.ts) and the mail lanes' release (queueMessageRefRelease,conversations/message-corpus.ts) each had their own 500-ref constant and their own chunking loop. Both now go throughqueueRefRelease(tx, organizationId, refs, { onChunkError? })andRELEASE_REFS_PER_JOB.queueMessageRefReleasekeeps its name for its three callers (conversation delete, retention, spam verdict) and delegates to the helper. A failed enqueue still throws into the caller's transaction.onChunkError: a failed job is logged, and the next one is still queued, as before.releaseUnlistedTaskBlobRefs(tasks/retire.ts). It queued all the refs a task hard delete orphaned as one job, and a project's task tree can orphan thousands. It now goes through the same helper, still in the retire's own transaction.The log says when a failed byte delete was re-queued (
release.ts).requeueBlobFailuresnow logs the blob-stage failures itself, after the enqueue:... (blob): <msg> — re-queued to knowledge.release_refswhen the job went through;... (blob): <msg> — not re-queuedwhen it did not, followed as before by the job'scould not re-queue ... their bytes stay:line with the error.warnReleaseFailureskeeps the corpus-stage lines. The pinned wordings inrelease.reconcile.test.tsare updated. A new test covers one failed job among two: 500 lines say "not re-queued", 1 says "re-queued", and one job line follows.One holder for a shared ref (
knowledge/service.ts,folders/paths.ts). Several documents can hold one blob ref: a WebDAV COPY, or one upload bound as two documents. The corpus has one row for it. The indexer indexed an unbound file holding such a ref as the ref's lowest-id active holder (activeDocumentHoldingRef), but the other scope writers wrote whichever document they read. In the nightly scope pass:Every scope writer now writes the holder's scope:
syncRagDocumentScopesread each ref's holder alone:AND NOT EXISTS (… o.id < d.id …)over active documents, the brief's clause.syncRagDocumentScope(from the review) writes the scope of its document's ref holder. The document's own scope is written only when it is the holder, or when no active document holds the ref (a trashed document keeps reaching its row, as before). Before, an edit of a non-holder twin wrote its own scope, and the next night the pass reported "the per-edit sync had failed for these".subtreeDocumentFolderPaths(from the review), which feedssyncRagFolderSubtreeon a folder rename or move, reads each ref's holder alone.The stamp clear's recheck has a client-side bound (
knowledge/service.ts,clearMailStampRows).statement_timeout(10 s), and a half-open app connection never delivers it: postgres.js then waits until TCP gives up.STAMP_RECHECK_ANSWER_MS). When the bound lapses, the corpus transaction rolls back with its stamps still on,clearMailStampslogs "a clear did not finish", and the next night retries it.Promise.withResolvers). The read-only app transaction around it is waited for at most 15 s more. Without that second wait, the pass would queue behind the dead connection's ROLLBACK, because postgres.jsbeginawaits it..cancel(). I left out the optional.cancel()on purpose. It sends the cancel request over a new connection to the same server, which is unreachable in exactly the case this bound exists for. When the connection is sound, the server's own timeout answers first.Registers. The emailed-attachment rows of
automation.mdandMIGRATION.mdnow describe:The scope-stamp row of
automation.mdnames the holder rule and the new lane.Verification
release-queue.test.ts(new): chunking atRELEASE_REFS_PER_JOBand its boundary, throw vsonChunkError.release.reconcile.test.ts: the new wordings and the mixed-outcome case.scope-reconcile.test.ts: twins across a page boundary with pages of 1, 2 and 3; the batch sync of the twin alone writes nothing; the single-document sync of either twin writes the holder's scope, and a trashed document with no active holder still writes its own. The fakes answer a holders-only read only when the statement carries the clause.service.mail-stamp.test.ts:ORDER BY d.id LIMIT 1over active documents.retire.test.ts: 501 orphaned refs go out as jobs of 500 and 1.vitest relatedon every changed source file (release-queue.ts,release.ts,message-corpus.ts,knowledge/service.ts,folders/paths.ts,tasks/retire.ts; server and client projects): 257 files, 3,387 tests, all passed.oxlint --type-aware --type-checkon every changed TypeScript file: clean.oxfmtandbun run lint:manual: green.check-guideonautomation.md: the same output as main.knip --workspace services/platform: only the known cross-workspace false positive (oidcOrganizationClaims).backend:integration. CI does not run this suite.checkScopeRefHolderlane, on an organization of its own, plants a trashed lowest id, the holder and a twin on one ref, and runs:corrected 1/scanned 1, then 0, the holder's scope;first=corrected 2/scanned 2 → ["team-twin"], second=corrected 2: the row is drift again on every run). Without the review's per-edit fix, the twin's edit writes["team-twin"]and its folder rename writes the twin's folder.checkFiles, checkProviderCredentials, checkProviderCredentialConfiguration, checkKnowledge, checkCorpusScopeReconcile, checkScopeRefHolder, checkEmailedAttachments, checkRetention: 66/68 checks. This includes all 12 emailed-attachment checks (the dead-conversation race with a concurrent clone during the clear among them), the knowledge checks that a folder stamp follows document moves and folder renames, and retention.pg_search), and moto'sblobGone=false. A filtered run on main (without the provider-credential lanes) showed the same environmental failures, pluscheckKnowledge's own, which needs a seeded embedding credential.SIGSTOPs the app backend that ran the clear'sSET LOCAL statement_timeout: the socket stays open and the kernel still acknowledges writes, but nothing comes back, not even the server's timeout.cleared=50and the "did not finish ... the recheck did not answer within 15000 ms" line. No unhandled rejection afterSIGCONT.SIGCONT.Left open
No schema change, no migration, no user-visible strings.
Refs #3785