Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions docs/rollouts/2026-12-03-collector-sweep-followups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Two ways the receipt sweep could get it wrong

**Date:** 2026-12-03 **Seat:** ZC **Session:** `sess_6ab61beb-b36e-4e27-b44f-5bf2948f67d3`
**Files:** `scripts/lib/collector-sentry-bridge.mjs`, `scripts/prune-collector-sentry-outboxes.mjs`,
`scripts/minimax-runtime-usage-collector.mjs`, `scripts/test-collector-sentry-outboxes.mjs`
**Rows:** UM-1684 (follow-up)
**Found by:** Jet (peer review of #2124) and by running the sweep on the host

## What was wrong

The receipt sweep finds a producer's checkpoint by the producer's name, and it checks
that the checkpoint file records that same producer.  That is the right rule, because a
watermark authorizes the deletion of receipts and must belong to the collector whose
receipts it would allow to go.

One collector does not use the same name for both.  The minimax collector checkpoints
under its state key, `minimax-runtime`, while its outbox belongs to the producer
`minimax-code`, so the two names never match.  The sweep reported that outbox as
`no-checkpoint` and left it alone, which is the safe answer and the wrong one: the
receipts in that outbox were never pruned, and it is the largest of the six on this
host.  Its checkpoint was written moments before the sweep ran, so the watermark was
there the whole time.

Every other collector uses one name for both, which is why the sweep found four of the
six outboxes and skipped only this one and the botfleet outbox, which has no checkpoint
because no botfleet collector runs here.

## The fix

The collector is the only side that knows both names, so the collector records the key it
checkpoints under, in the identity row the outbox already carries.  The sweep reads that
key and looks the checkpoint up by it, checking that the file records the key it is named
after.  Without the recorded key the sweep still falls back to the producer's own name,
and an outbox whose checkpoint cannot be found is still skipped.

The fallback matters: a checkpoint from another collector must never authorize the
deletion of these receipts, so the match stays exact.

## Verification

`scripts/test-collector-sentry-outboxes.mjs` passes 25 assertions.  The two new ones
cover the outbox whose checkpoint is filed under the collector's state key, which is now
pruned, and the same outbox without the recorded key, which is still skipped and keeps
its receipts.  The first of the two fails against the code before this change.

The key is recorded on both paths the collector takes.  The pass that finds nothing to
send knows the key because it writes the checkpoint, and the pass that finds events to
send now carries it as well, because that pass is the one that writes the outbox's
identity row.  An identity recorded without a key keeps the key that was already
recorded, so neither pass can reset the other.

## The second way: a collector could write into the outbox as it was removed

Jet raised this on the first review of #2124, and it is right: the sweep counted an
outbox's receipts, and if the count was zero it removed the directory.  A collector
writing into that outbox at that moment would lose the row it was writing, because the
count and the removal were not one step.  The count closes the database handle before the
removal, and the enqueue path shares no guard with the sweep.

The fix is the lock the collector's own writes already take.  The reclaim opens the
outbox, takes the write lock, counts under it, and removes the directory while it still
holds it.  A collector that was already writing either holds the lock itself - so the
removal waits for it and then sees the row - or waits for the lock, and its write fails
loudly rather than disappearing into a directory that is being removed.  A collector
that writes after the removal recreates the directory, which is the directory it wanted.

A regression test reproduces the window Jet named: the sweep has counted the outbox
empty, and a collector writes into it at that moment.  Without the lock the outbox is
removed and the row goes with it; with it the outbox is left in place and the sweep
reports why.

## What the review found on top of that

The review of this pull request left four findings open, and all four were real.

The checkpoint key was carried only by the pass that finds nothing to send, so a
collector whose passes always find events never recorded it at all, and the sweep kept
reporting its outbox as `no-checkpoint`.  The key now travels with the identity on the
path that writes the outbox, and an identity recorded without a key keeps the one that
was already recorded.

The checkpoint file was read without being validated, even though a watermark
authorizes the deletion of receipts and the file is untrusted input.  It is now checked
against a schema before any of its fields are read.

The reclaim took the write lock with a bare `BEGIN IMMEDIATE`, whose wait is capped by
the connection's short busy timeout, so a collector write that ran longer than that
failed the outbox rather than being waited for.  The reclaim now retries a busy
database, and the outbox's schema is checked with reads before it is changed, so a
maintenance job no longer needs the write lock merely to open an outbox.  The busy check
lives in the bridge, where the collector and the maintenance job can both reach it.

## What the review found on the reclaim as well

The review of the merged pull request found a third way the reclaim could get it wrong,
and it is the same window Jet named, one step further on.

A collector that had already opened the outbox when the reclaim removed it is waiting for
the lock, and two things can then happen to its write. It can fail with an I/O error,
because the write-ahead log it needs went with the directory, or it can succeed, because
a connection that already holds that log open writes into it happily. In the second case
the receipt lands in a file no path points at, and the collector is told it stored a
receipt that is gone.

The write now compares the file the path points at before and after the write, by device
and inode, and writes the receipt again when the path no longer holds what was written
into it. A write that failed with the I/O error is retried once, because the outbox is
simply gone and a new one is the answer.

The test file passes 30 assertions.  The four new ones cover an identity that keeps
its recorded key, a checkpoint file of the wrong shape, the reclaim waiting for a
collector's write instead of failing the outbox, and a receipt written into an outbox
that was reclaimed being written again.
Loading
Loading