Repository navigation
fix(citations): resolve URLs through the request body instead of the query string - #741
Merged
Merged
Conversation
…query string Third attempt at the same mistake, so this removes the class rather than guessing again at the limit. URL lookups used a PostgREST `.in()` filter, which is spelled out in the query string. First unbounded, then chunked at 100 — and it still failed, because URLs are percent-encoded on the way into a URI and a hundred of them at an average 92 characters (up to 1,333) overflows the request-line and header limits in front of PostgREST. The proxy answered `Bad Request` or reset the connection outright, and it happened on exactly the answers carrying the most citations: 182 of 174,466 after the chunked fix, each losing everything it had. A new function takes the list as a jsonb argument in the POST body, which has no such bound. It inserts what is unseen and returns an id for every URL asked about, so the lookup-then-insert-then-re-read dance is gone with it, along with the concurrent-insert race that dance existed to survive. The function is SECURITY DEFINER because the dictionary has no select policy of its own, and execute is revoked from everyone but the service role — the tracking worker and the backfill are the only callers, and nothing reachable from a browser should be writing to a cross-tenant table. Refs #732
4 of 9 tasks
This was referenced Aug 20, 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
This is the third fix for the same mistake, so it removes the class rather than guessing again at someone else's limit.
URL lookups used a PostgREST
.in()filter, which is spelled out in the query string. First unbounded (#738), then chunked at 100 (#740) — and it still fails. URLs are percent-encoded on the way into a URI, and a hundred of them at an average 92 characters, up to 1,333, overflows the request-line and header limits in front of PostgREST.The proxy answers
Bad Request, or resets the connection and Node reportsTypeError: fetch failed. Both appeared in the last production run:It failed on exactly the answers carrying the most citations — 22 to 51 each — and each one lost everything it had. 182 answers of 174,466 after the chunked fix.
What changed
citation_url_ids(p_urls jsonb)takes the list as a body argument, which has no such bound. One call inserts what is unseen and returns an id for every URL asked about.That also deletes the lookup-then-insert-then-re-read sequence, and with it the concurrent-insert race that sequence existed to survive: a writer that loses the race now gets the winner's id from the same statement.
There is nothing left to chunk, so there is no chunk size left to be wrong about.
Security
The function is
SECURITY DEFINERbecausecitation_urlshas no select policy of its own — it is a cross-tenant dictionary, and #737 deliberately left it unreadable except through a definer function.Execute is therefore revoked from
public,anonandauthenticated, and granted only toservice_role. The tracking worker and the backfill are the only callers, and nothing reachable from a browser should be writing to that table.Where this leaves the backfill
The last run wrote 3,994 rows and closed 27 answers, leaving 182:
After this, a re-run should close all of them.
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
00059_citation_url_ids.sql— this PR should pick upneeds-migration.citation_url_idsis not callable as an authenticated user.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn format:checkpasses (run fromserver/)Server suite passes: 308 tests. The chunking tests are replaced by ones that pin the new shape — a single call regardless of size, distinct URLs only, and a citation dropped rather than sent with an undefined id when the function returns no id for it.
supabase/schema.sqlregenerated; every migration parses under Postgres's own grammar.