Repository navigation
fix(citations): chunk URL lookups so answers with many citations are not dropped - #738
Merged
Merged
Conversation
…not dropped A PostgREST `.in()` filter is spelled out in the query string, so the whole list travels in the request line — which has a length limit an insert body does not. resolveUrlIds sent every URL of an answer in one call, which held for a typical answer and failed for the ones that mattered most. The first production backfill lost 228 of 174,466 answers this way, 10,380 citations in total. They were not malformed: the answers that failed averaged 45 citations and 16 KB of URL text, against 12 citations and 1.2 KB for the ones that succeeded. persistCitationRows is best-effort, so each failure was logged and the answer skipped, and the totals looked healthy — 99.5% written reads as success until the shortfall is traced to a shape rather than to chance. Lookups now go 100 URLs at a time. The insert between them carries its payload in the body and needs no chunking; only the reads around it do. The backfill also now separates "this answer had no storable citations" from "this answer had citations and stored none", reports the second per batch, and exits non-zero when any occurred. The first run reported neither, which is why the loss had to be found by comparing row counts against the jsonb afterwards. Re-running the backfill picks up the 228: everything already stored is skipped by the existing upsert. Refs #732
This was referenced Aug 19, 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.
Summary
A bug in the phase 1 write path (#737), found by comparing the backfilled rows against the jsonb they came from.
A PostgREST
.in()filter is spelled out in the query string, so the whole list travels in the request line — which has a length limit that an insert body does not.resolveUrlIdssent every URL of an answer in a single call. That holds for a typical answer and fails for exactly the ones that matter most.What it cost
The first production backfill stored 2,159,802 of 2,170,187 citations. The shortfall was not spread thin — it was 228 whole answers out of 174,466, each losing every citation it had:
The URLs were not malformed — 10,133 of the 10,380 lost citations are ordinary
https://links. The only thing the failures had in common was size.persistCitationRowsis best-effort by design, so each failure was logged and the answer skipped. 99.5% written reads as success right up until the missing half-percent turns out to be a shape rather than chance.The fix
Lookups now go 100 URLs at a time. At the 2,048-character ceiling this module already enforces, that is roughly 200 KB per request in the worst case, well inside the limit, and the largest answer observed carries 191 citations — two batches.
The insert between the two lookups carries its payload in the body, so it needs no chunking. Only the
.in()reads around it do.Making the next one loud
The backfill reported one number for "answers that stored nothing", which conflated had no storable citations with had citations and stored none. Those are a normal outcome and a fault, and merging them is why this had to be found afterwards by counting rows against jsonb.
They are now separate. Failures are counted per batch, listed at the end, and the script exits non-zero if any occurred.
Recovering the lost 228
Re-run the backfill. Everything already stored is skipped by the existing upsert on
(prompt_result_id, position), so it walks the history and writes only the answers that failed.Afterwards, this should return zero:
Related issue
Refs #732 — phase 1 correction; the page still reads jsonb until phase 2.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
0 failed.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn format:checkpasses (run fromserver/)Server suite passes: 302 tests, 3 of them new — one asserting the batch sizes directly, one storing a 191-citation answer end to end, one confirming no second round trip when every URL is already known. No migration, no
web/changes.