Skip to content

fix(citations): resolve URLs through the request body instead of the query string - #741

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citation-urls-rpc
Aug 19, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citation-urls-rpc

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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 reports TypeError: fetch failed. Both appeared in the last production run:

citation url lookup: Bad Request
citation url lookup: TypeError: fetch failed

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 DEFINER because citation_urls has 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, anon and authenticated, and granted only to service_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:

Rows written 2,163,796
Answers still short 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 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. Apply 00059_citation_url_ids.sql — this PR should pick up needs-migration.
  2. Deploy, then re-run the backfill:
    docker exec -d ansvisor-server-1 sh -c 'node src/scripts/backfill-citations.js > /tmp/backfill.log 2>&1'
  3. Expect it to finish reporting 0 failed and exit zero.
  4. Confirm this returns zero, which it never yet has:
    select count(*) from prompt_results pr
    where exists (select 1 from jsonb_array_elements(coalesce(pr.citations,'[]'::jsonb)) c
                  where c.value->>'url' ~ '^https?://')
      and not exists (select 1 from prompt_result_citations x where x.prompt_result_id = pr.id);
  5. Run a tracking cycle and confirm an answer with 40+ citations stores all of them, through the webhook path as well as the synchronous one.
  6. Confirm citation_url_ids is not callable as an authenticated user.

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: 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.sql regenerated; every migration parses under Postgres's own grammar.

…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
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 19, 2026
@gkhngyk
gkhngyk merged commit 8ccf5a2 into main Aug 19, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the fix/citation-urls-rpc branch August 20, 2026 11:36
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

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant