Skip to content

fix(citations): page the already-stored lookup past PostgREST's 1000-row cap - #740

Merged
gkhngyk merged 1 commit into
mainfrom
fix/backfill-postgrest-cap
Aug 19, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/backfill-postgrest-cap

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

The already-stored skip added in #739 was wrong, and its own reporting hid it.

alreadyStored reads one row per citation, not per answer. In production, 200 answers match 1,591 rows — and PostgREST silently caps an un-paginated select at 1,000. Everything past that first page looked unstored, was reprocessed, and had its writes discarded as duplicates by the upsert.

The run then reported thousands of citations written while the table did not move at all:

2600 answers · 2320 already stored · 2549 citations · 0 failed

rows_written in the database: unchanged, before and after.

This is the same trap as #714 and #716. The comment on scanFilteredResults in the web app already spells it out; this file now does too.

Two fixes

The lookup is paginated. It reads in exactly PostgREST's own page size, so a short page is an unambiguous end-of-results signal.

The counter no longer lies. persistCitationRows counted rows it attempted, so ignored duplicates counted as writes. The upsert now returns what it actually inserted, and the function distinguishes three outcomes that used to be one number:

Return Meaning
n rows inserted
0 nothing new — every row was already there
null nothing stored, retry

Both conflations matter in opposite directions. Treating 0 as failure would make --force report every answer it revisits as broken. Treating failure as 0 is what let the original 228 lost answers read as a healthy run in the first place.

How this was caught

Only by the row count not moving. The log said it was working, the failure counter said zero, and both were sincere. That is the second time on this table that a silent truncation produced confident wrong numbers, which is why the fix includes the reporting and not just the query.

Related issue

Refs #732 — phase 1 tooling; 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. Stop the running backfill — it is idempotent, and right now it is doing nothing useful:
    docker exec ansvisor-server-1 pkill -f backfill-citations
  2. Deploy, then run it again:
    docker exec -d ansvisor-server-1 sh -c 'node src/scripts/backfill-citations.js > /tmp/backfill.log 2>&1'
  3. Expect already stored to track answers closely, and citations to climb only by what is genuinely new — roughly 10,380 across the whole run, not thousands per page.
  4. Confirm this returns zero afterwards, which it has not yet done:
    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);
  5. Confirm the row count reaches ~2,170,187, matching the jsonb total.

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: 307 tests, 5 of them new — pinning each of the three return values, including that a failed URL lookup and a failed insert both yield null. No migration, no web/ changes.

…row cap

The skip added in #739 read one row per *citation*, not per answer: 200
answers matched 1,591 rows in production, and PostgREST silently caps an
un-paginated select at 1,000. Everything past that first page looked unstored,
was reprocessed, and had its writes discarded as duplicates — so a repair run
reported thousands of citations written while the table did not move at all.
The same trap as #714 and #716, and this file now says so.

The lookup is paginated. A short page is the end-of-results signal, which is
why it reads in exactly PostgREST's own page size.

The reporting that hid it is fixed too. persistCitationRows counted the rows it
attempted rather than the rows that landed, so duplicates counted as writes.
The upsert now returns what it inserted, and the function separates three
outcomes that were previously one number:

  n     rows inserted
  0     nothing new — every row was already there, which is what --force
        produces on every answer it revisits
  null  nothing stored, retry

Conflating 0 with failure would have made --force report every answer as
broken; conflating failure with 0 is what let the original 228 lost answers
look like a healthy run.

Refs #732
@gkhngyk
gkhngyk merged commit 9da82f7 into main Aug 19, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the fix/backfill-postgrest-cap branch August 19, 2026 11:47
gkhngyk added a commit that referenced this pull request Aug 20, 2026
…ing past 50 prompts (#747)

The Prompts page sent every unanalyzed prompt inline in one request, and the
route refused more than 50 of them. A brand with 100 active prompts was shown
an "Analyze 100 prompts" button that could only ever return 400, and the page
turned the reason into "Volume analysis failed" — so the one system that knew
what was wrong never said it.

Raising the cap alone would not have worked. Each prompt costs an LLM call plus
a DataForSEO call and they run in order, measured at a 6.4s median across 1,215
consecutive rows: about eleven minutes for 100 prompts, far past what a request
can be held open for.

So the brand, not a list of prompts, is now the unit of work. The route starts
the run and returns 202; the page follows a status endpoint and shows real
progress ("Analyzing 32/100..."). Each prompt is saved as it lands, so a closed
tab costs nothing already done. Quota still counts one run per click.

Along the way, the prompt reads no longer assemble a list of ids to filter on
and no longer trust an un-paginated select — the two shapes that lost data in
#714, #740 and #741, and that a 1000-prompt brand sits exactly on the edge of.
Inactive prompts are now skipped rather than spending an LLM call and a
DataForSEO call on a row no surface reads.

Failures reach the user as wording chosen for a person. The server's text names
batch sizes, upstream APIs and Postgres columns; none of it is shown. The
actions return a small set of codes instead of throwing, which also fixes the
quota message never appearing — it was matched on text that Next.js redacts in
production.
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