Skip to content

fix(platform): close the #3739 mail stamp pass review findings - #3785

Merged
yannickmonney merged 6 commits into
mainfrom
fix/mail-stamp-requeue-bound-counts
Sep 29, 2026
Merged

yannickmonney merged 6 commits into
mainfrom
fix/mail-stamp-requeue-bound-counts

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

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.

  • The new requeueBlobFailures queues knowledge.release_refs for those refs, at most 500 per job. It is wired into the stamp pass's releaseUnbacked and into the blob walk (reconcileRange).
  • pg-boss retries only the blob stage: the job re-checks liveness and treats absent corpus rows as already released.
  • A corpus-stage failure is not queued, because its row still lists the ref for the next night.
  • An enqueue that fails is logged, not thrown, so the organization's scope pass and stamp pass still run.
  • The jobs/tasks.ts comment no longer claims the reconcile catches a job that ran out of retries on the blob stage.

True counts (service.ts, release.ts). MailAttachmentStampStats gains 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 cleared and unbackedReleased/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).

  • Each clear takes at most 100 rows (STAMP_CLEAR_ROWS) in a transaction of its own.
  • The recheck reads in an app transaction opened before any corpus row is locked, under 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.
  • A clear that fails before its corpus commit rolls back with its stamps still on. It is logged, the next clear goes on, and the next night retries it.
  • A clear whose corpus side committed stands and counts, even if the read-only app transaction then fails to end.
  • I deliberately left out the optional idle_in_transaction_session_timeout backstop. A probe on real Postgres 16 with postgres.js 3.4.7 showed the problem: when the server ends the session mid-transaction, begin rejects with CONNECTION_CLOSED but the callback keeps running. Its next statement then either crashes the process (socket.write on 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. emailedAttachmentConversation takes a heldByActiveDocument flag, which activeDocumentHoldingRef reads with the same active-document condition as readMailAttachments. 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 (reconcileDocumentScopeStamps walks every active document with a file_ref). The comments in file_metadata/mail-attachments.ts and conversations/message-corpus.ts that named the old rule are updated.

Docs. The knowledge row of MIGRATION.md now carries the automation.md wording for the stamp pass, and both registers name the changes above.

Tests. The kept-ref test in release.reconcile.test.ts is folded into the byte-release test. One call now releases an orphaned ref and keeps a document-held one, and exactly one deleteOrgObject and one app.file_metadata delete follow. That test fails if releaseUnbacked is wired back to releaseCorpusRefs.

Verification

  • release.reconcile.test.ts (18 tests) and service.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.
  • Mutation checks:
    • releaseUnbacked wired to releaseCorpusRefs → the folded test and two requeue tests fail.
    • Requeueing corpus failures too → the two negative guards fail.
    • No requeue in the blob walk → the blob-walk test fails.
    • Recheck read on the pool instead of its transaction → 4 tests fail.
    • SET LOCAL on the pool → 3 tests fail.
    • Committed-clear branch removed → its test fails.
  • vitest related on release.ts and service.ts (server and client projects): 246 files, 3,265 tests, all passed.
  • The touched suites plus release.message-refs, message-corpus and mail-attachments: 70/70 after the rebase onto main.
  • oxlint --type-aware --type-check on every changed TypeScript file: clean.
  • oxfmt, bun run lint:manual: green.
  • check-guide on automation.md adds one finding, knowledge.release_refs read as an i18n key. It is the same false positive as the rag.index_file and rag.index_message entries already there.
  • backend:integration with ITEST_LANES=checkFiles,checkEmailedAttachments, on a local Postgres 16 with pgvector and a moto S3. CI does not run this suite.
    • All 12 emailed-attachment checks pass, including: the recheck race (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.
    • The lane's hook now wraps only the recheck's own transaction, so a recheck on the pool would fail the lane.
    • The same two checks fail with and without this branch (33/35 on both), because of the environment: the boot BM25 check (no ParadeDB pg_search here) and files upload/serve/delete (blobGone=false, which is how the moto double answers the presigned GET after delete).
  • An independent read-only review found no high-severity issue. Its medium test-gap finding and two low findings are fixed here (the handle-level fake, the logged enqueue failure, the committed-clear accounting), and so is its comment drift.

Left open

  • A re-queued job that exhausts its retry ladder (8 retries from 10 s with backoff, roughly an hour) still strands the bytes: no sweep lists bucket objects. The tasks.ts comment says so.
  • A connection lost in the middle of a postgres.js transaction is a general hazard (see the probe above). The app transaction is now also open during the corpus clear, a window of milliseconds; the corpus transaction had the same window before.
  • The chat tools still label a hit on a ref an active document also holds as mail-attachment and 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

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.
@yannickmonney
yannickmonney merged commit 48b02f8 into main Sep 29, 2026
50 checks passed
@yannickmonney
yannickmonney deleted the fix/mail-stamp-requeue-bound-counts branch September 29, 2026 01:51
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