Skip to content

fix(citations): resolve the ambiguous url reference in citation_url_ids - #743

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citation-url-ids-ambiguous
Aug 20, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citation-url-ids-ambiguous

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

citation_url_ids from #741 raised on every call:

column reference "url" is ambiguous
DETAIL: It could refer to either a PL/pgSQL variable or a table column.

returns table (id bigint, url text) declares those names as PL/pgSQL variables, so on conflict (url) inside the body could mean the variable or the column, and PL/pgSQL refuses to guess.

That is worse than the bug it replaced. The old one lost the answers carrying the most citations; this one lost all of them.

#variable_conflict use_column resolves an ambiguous name to the column, which is what every reference in this body means. Renaming the output columns would also work and would change the shape the caller reads, for no benefit.

Why it got through

#741 was checked two ways, and neither could see this:

  • Parsed with Postgres's own grammar (pglast). Parsing proves a function compiles, not that it runs — the ambiguity is raised at execution.
  • Unit tested with the Supabase client mocked, which is the right shape for testing the caller and no test at all of the SQL.

The gap is that nothing executed the function against a database before it shipped. That is now the thing to change, not the specific mistake.

Applied ahead of merge

The hosted database has this already. The deployed write path was calling a function that raised for every answer, so leaving it until a merge would have cost another night of the nightly run writing no citation rows at all.

The repo was briefly behind the database, which is the drift #728 exists to prevent — this PR closes it, and the migration is create or replace, so applying it again is a no-op.

Verified against real data

Not mocked this time. Run against one of the answers that had been failing since the start:

Citations in the answer 37
URL text 21,892 bytes
Ids returned 26 (26 distinct URLs)

That payload is precisely what overflowed the query string in the .in() versions. It passes in the body.

Also confirmed: a URL already in the dictionary comes back with its existing id rather than a new row, and a URL cited twice in one answer collapses to one entry.

What this means for the backfill

No new deploy is needed. The server already runs the code that calls this function, and the function now works — the backfill can be re-run as it stands.

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. Re-run the backfill and confirm it finishes reporting 0 failed and exits zero.
  2. 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);
  3. Confirm the row count reaches roughly 2,170,000, matching the jsonb total less the citations whose URL yields no host.
  4. Run a tracking cycle and confirm an answer with 30+ citations stores all of them.

Checklist

Migration only; no application code changes, so server/ and web/ are untouched. supabase/schema.sql regenerated.

`returns table (id bigint, url text)` declares those names as PL/pgSQL
variables, so `on conflict (url)` could mean the variable or the column and
PL/pgSQL refuses to guess. The function raised on every call, which is worse
than the bug it replaced: that one lost the answers with the most citations,
this one lost all of them.

00059 was parsed with Postgres's grammar but never executed against a
database, and the unit tests around this path mock the client, so neither
could catch it. Parsing proves a function compiles, not that it runs.

`#variable_conflict use_column` resolves an ambiguous name to the column,
which is what every reference in this body means.

Applied to the hosted database ahead of this commit rather than after it: the
deployed write path was calling a function that raised for every answer, and
leaving that in place until a merge would have cost another night of the
nightly run. Verified against a real answer that had been failing since the
start — 37 citations, 21,892 bytes of URL text, 26 ids returned.

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 2462578 into main Aug 20, 2026
6 checks passed
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