Skip to content

fix(citations): chunk URL lookups so answers with many citations are not dropped - #738

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citation-url-chunking
Aug 19, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citation-url-chunking

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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. resolveUrlIds sent 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:

Answers that failed Answers that succeeded
Count 228 174,238
Average citations 45.5 12.4
Average URL text per answer 16,568 bytes 1,182 bytes
Largest 191 citations 156 citations

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.

persistCitationRows is 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.

docker exec -d ansvisor-server-1 sh -c 'node src/scripts/backfill-citations.js > /tmp/backfill.log 2>&1'

Afterwards, this should return zero:

select count(*) from prompt_results pr
where jsonb_array_length(coalesce(pr.citations,'[]'::jsonb)) > 0
  and not exists (select 1 from prompt_result_citations c where c.prompt_result_id = pr.id);

Related issue

Refs #732 — phase 1 correction; the page still reads jsonb until phase 2.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Deploy, then re-run the backfill and confirm it finishes reporting 0 failed.
  2. Confirm the query above returns zero.
  3. Confirm the row count now matches the jsonb total, allowing only for citations whose URL yields no host at all.
  4. Run a tracking cycle and confirm an answer with an unusually long citation list stores every one of them.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from server/)
  • yarn format:check passes (run from server/)
  • Changes are focused — one concern per PR

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.

…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
@gkhngyk
gkhngyk merged commit 59b33a0 into main Aug 19, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the fix/citation-url-chunking branch August 19, 2026 10:55
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