Repository navigation
fix(citations): page the already-stored lookup past PostgREST's 1000-row cap - #740
Merged
Merged
Conversation
…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
This was referenced Aug 19, 2026
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.
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
The already-stored skip added in #739 was wrong, and its own reporting hid it.
alreadyStoredreads 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:
rows_writtenin the database: unchanged, before and after.This is the same trap as #714 and #716. The comment on
scanFilteredResultsin 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.
persistCitationRowscounted 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:n0nullBoth conflations matter in opposite directions. Treating
0as failure would make--forcereport every answer it revisits as broken. Treating failure as0is 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
docker exec ansvisor-server-1 pkill -f backfill-citationsalready storedto trackanswersclosely, andcitationsto climb only by what is genuinely new — roughly 10,380 across the whole run, not thousands per page.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn format:checkpasses (run fromserver/)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, noweb/changes.