Repository navigation
fix(platform): close the #3739 mail stamp pass review findings - #3785
Merged
Merged
Conversation
The stamp pass's kept-ref test still passed with releaseUnbacked wired back to releaseCorpusRefs, which answers the same outcome for a ref something keeps. Fold it into the byte-release test: one call with an orphaned ref and a document-held one, where only the orphan's corpus rows, bytes and trashed file row go.
A reconcile release deletes a dead ref's corpus rows before its bytes. When only the byte delete failed, nothing listed the ref again: the stamp pass's second walk reads stamped corpus rows, the blob walk corpus refs, and no sweep lists bucket objects. Both walks now queue knowledge.release_refs for those refs, at most 500 per job, and the job's retries redo the blob stage only. A corpus-stage failure keeps its row for the next night and is not queued; a failed enqueue is logged and the organization's other passes still run. The release job's retry comment no longer claims the reconcile catches a job that ran out of retries on the blob stage.
The post-clear backing recheck's releases were added to unbackedReleased and unbackedFailures, documented as refs no attachment backs, and the stamps it put back were counted nowhere. The stamp pass stats gain restamped, recheckReleased and recheckFailures, and the stale-stamps log line reports each of them, even when only the recheck acted, with wording that fits every row it counts.
A clear row-locked up to a page of 1000 corpus rows while it read the app database with no timeout, and an indexer claiming one of those refs waited behind it. A clear now takes at most 100 rows per transaction. Its recheck reads in an app transaction opened before any corpus row is locked, under a ten-second statement timeout, so the lock never waits for the app pool. A clear that cannot finish rolls back with its stamps on, is logged, and the next night retries it; one whose corpus side committed stands even if the read-only app transaction fails to end. No idle-in-transaction timeout backstops the corpus side: with postgres.js, a session the server ends mid-transaction lets the callback's later statements run outside the transaction.
An emailed attachment's unbound file row whose ref an active document also holds was re-indexed with its conversation stamp and no scope, which the upsert wrote over the document's corpus row. The nightly clear took the stamp off again and the scope pass reported the scope as failed-sync drift, on every re-index. An unbound file whose ref an active document holds now indexes as that document, under its scope and no conversation: the rule both stamp walks read. The comments that named the old rule say so.
The knowledge row of MIGRATION.md still described the stamp pass's second walk as it was before #3739. It now carries the automation register's wording, and both rows name this branch's changes: the re-queued byte deletes, the bounded clear, the recheck's own counts and the indexer's rule for a ref an active document holds.
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 #3739 (itself a follow-up to #3668), from its review. Each item was re-checked on main e3943c5 before the fix. There is one commit per item, and each commit passes the touched suites and the type check on its own.
What changed
Failed byte deletes are re-queued (
domains/knowledge/release.ts). A reconcile release first deletes a dead ref's corpus rows, then its bytes. If only the byte delete failed, nothing would ever list the ref again: the stamp pass's second walk reads stamped corpus rows, the blob walk lists corpus refs, and no sweep lists bucket objects.requeueBlobFailuresqueuesknowledge.release_refsfor those refs, at most 500 per job. It is wired into the stamp pass'sreleaseUnbackedand into the blob walk (reconcileRange).jobs/tasks.tscomment no longer claims the reconcile catches a job that ran out of retries on the blob stage.True counts (
service.ts,release.ts).MailAttachmentStampStatsgains three counts:restamped: stamps the post-clear backing recheck put back.recheckReleased/recheckFailures: attachments the recheck found on a deleted or spam conversation, and whose release failed.These used to be folded into
clearedandunbackedReleased/unbackedFailures, or not counted at all. The field docs now match what each count holds. The stale-stamps line reports every count, and its guard includes the new ones. Its wording now fits every row it counts: "rows no emailed attachment backed when the walk read them, not a failed sync".The clear holds its corpus row locks only briefly (
service.ts).STAMP_CLEAR_ROWS) in a transaction of its own.SET LOCAL statement_timeout = '10s'. The lock therefore never waits for the app pool (10 connections by default), which a timeout inside the corpus transaction could not have bounded.idle_in_transaction_session_timeoutbackstop. A probe on real Postgres 16 with postgres.js 3.4.7 showed the problem: when the server ends the session mid-transaction,beginrejects withCONNECTION_CLOSEDbut the callback keeps running. Its next statement then either crashes the process (socket.writeon a null socket) or runs on the reconnected connection outside any transaction; the probe row read'late'.The indexer follows the walks' rule (
service.ts). An unbound file whose ref an active document holds now indexes as that document: the document's teams, project and folder, and no conversation stamp.emailedAttachmentConversationtakes aheldByActiveDocumentflag, whichactiveDocumentHoldingRefreads with the same active-document condition asreadMailAttachments. Before this, each re-index of such an attachment wrote a conversation stamp and a blank scope over the document's corpus row. The nightly clear then removed the stamp again, and the scope pass reported the scope as failed-sync drift.This applies to every unbound file, thread/chat files included. For those, the corpus row now gets straight away the document scope that the nightly scope pass already wrote back each night (
reconcileDocumentScopeStampswalks every active document with afile_ref). The comments infile_metadata/mail-attachments.tsandconversations/message-corpus.tsthat named the old rule are updated.Docs. The knowledge row of
MIGRATION.mdnow carries theautomation.mdwording for the stamp pass, and both registers name the changes above.Tests. The kept-ref test in
release.reconcile.test.tsis folded into the byte-release test. One call now releases an orphaned ref and keeps a document-held one, and exactly onedeleteOrgObjectand oneapp.file_metadatadelete follow. That test fails ifreleaseUnbackedis wired back toreleaseCorpusRefs.Verification
release.reconcile.test.ts(18 tests) andservice.mail-stamp.test.ts(29 tests). Their new and changed tests fail against the old sources. The fakes give a transaction its own handle, so a test can see which connection the recheck and the timeout ran on.releaseUnbackedwired toreleaseCorpusRefs→ the folded test and two requeue tests fail.SET LOCALon the pool → 3 tests fail.vitest relatedonrelease.tsandservice.ts(server and client projects): 246 files, 3,265 tests, all passed.release.message-refs,message-corpusandmail-attachments: 70/70 after the rebase onto main.oxlint --type-aware --type-checkon every changed TypeScript file: clean.oxfmt,bun run lint:manual: green.check-guideonautomation.mdadds one finding,knowledge.release_refsread as an i18n key. It is the same false positive as therag.index_fileandrag.index_messageentries already there.backend:integrationwithITEST_LANES=checkFiles,checkEmailedAttachments, on a local Postgres 16 with pgvector and a moto S3. CI does not run this suite.restamped=1), the dead-conversation race with a concurrent ordinary indexer during the clear (clone during clear=true restamped=1 recheckFailures=1, no mail context donated), and the orphan released with its bytes and its trashed file row.pg_searchhere) andfiles upload/serve/delete(blobGone=false, which is how the moto double answers the presigned GET after delete).Left open
tasks.tscomment says so.mail-attachmentand wrap it as untrusted. That is conservative, and it is how things already were after the nightly clear.No schema change, no migration, no user-visible strings.
Refs #3739
Refs #3668