A contested byline must not kill the feed that carried it - #146
Merged
Conversation
Two Substack feeds have been failing every crawl for hours on `UNIQUE constraint failed: authors.slug`, and the cost is out of all proportion to the cause: because the author insert rides in the crawl's own write transaction, losing a slug race took the *whole crawl* with it. The feed was recorded as uncrawlable and walked up the backoff ladder -- ten consecutive failures marks a feed dead -- over a byline. The race is real and older than the queue. `claimAuthorSlug` reads the slugs already taken and this insert runs later, so two crawls naming the same author can both pick `jane-doe` before either commits. The `on conflict (identity_key)` upsert does not catch it, because the colliding rows are two *different* people who happen to share a display name. SQLite has allowed multiple upsert clauses since 3.35 and libSQL honours them, so the fix is one line: `on conflict (slug) do nothing`. Doing nothing is the right answer and not merely the safe one. The loser of the race is deferred rather than lost -- by the next crawl the slug is taken, so `claimAuthorSlug` picks the next free one and the credit lands then. A byline missing for one cycle is a far smaller thing than a feed marked dead. `upsertAuthor` takes the other path into this table from the enrichment pass and had the same hole; it is closed the same way. The tests pin the whole shape, including that the guard does not swallow the ordinary path: a matching identity_key must still update, and only a different identity colliding on slug is a no-op. Verified non-vacuous -- removing either clause fails them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
The harm
Two Substack feeds have been failing every crawl for hours:
The cost is wildly out of proportion to the cause. Because the author insert rides in the crawl's own write transaction, losing a slug race takes the whole crawl with it — the feed is recorded uncrawlable and walks up the backoff ladder. Ten consecutive failures marks a feed
dead. Over a byline.The race
claimAuthorSlugreads the slugs already taken, and this insert runs later. Two crawls naming the same author can both pickjane-doebefore either commits.The existing
on conflict (identity_key)upsert does not catch it, because the colliding rows are two different people who happen to share a display name.It predates the write queue — it was failing crawls hours before Redis was switched on.
The fix
SQLite has allowed multiple upsert clauses since 3.35 and libSQL honours them (verified against a real database before relying on it), so this is one line:
Doing nothing is the right answer, not merely the safe one. The loser of the race is deferred, not lost: by the next crawl the slug is taken, so
claimAuthorSlugpicks the next free one and the credit lands then. A byline missing for one cycle is a far smaller thing than a feed marked dead.upsertAuthortakes the other path into this table from the enrichment pass and had the same hole; closed the same way.Tests
The tests pin the whole shape, including that the guard does not swallow the ordinary path — a matching
identity_keymust still update, and only a different identity colliding on slug is a no-op. Verified non-vacuous: removing the clause fails them.Full suite: 1,157 tests, 0 failures.
Note
Feeds already pushed up the backoff ladder by this will recover on their own once the fix deploys, but their
error_countstays elevated until a successful crawl resets it. Say the word if you want those reset explicitly, as I did for the 429s earlier.