Skip to content

fix(citations): apply the source scope inside the URL cap, not after it - #791

Merged
gkhngyk merged 1 commit into
mainfrom
feature/citations-url-scope
Aug 27, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/citations-url-scope

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

The URL tab under-reported whenever a source scope was selected. Two changes that are each right on their own combined badly: #744 caps the URL list at the top 2,000 by citation count, and #742 then applied the scope on the client, over the rows that arrived. So the scope filtered within the global top 2,000.

Measured on the largest brand, a 30-day window:

scope shown actually exist
Brand 21 395
Competitors 125 2,794

And it was silent. The pager counts what it filtered, so the tab read "125 of 125" — a reader could not tell this brand has 125 competitor URLs from 125 of them ranked in the global top two thousand.

citations_urls now takes the domains a scope resolves to and cuts the set before the limit. total_urls counts the scoped set, so the tab reports the scope rather than the cap. The three scopes now partition the window exactly:

395 + 2,794 + 94,131 = 97,320

What a brand or competitor domain is stays in TypeScript. The caller already runs classifyDomain over every aggregated domain to build the Domains tab; the resolver reads that result. No second definition of the rule in SQL to drift from the first.

Two departures from the issue's sketch, both deliberate

Two parameters, not one. Resolving every scope to an include list would mean sending 17,532 domains for Third-party — the whole cited set minus fourteen. Naming the fourteen to exclude says the same thing in a request that fits in a packet. Every scope now resolves to a handful:

scope argument
All neither
Brand / Competitors p_domains
Third-party p_exclude_domains

The filter sits after the aggregate, and that placement is load-bearing. Pushed down into the citation scan, the planner abandons the hash join for a nested loop over the matching url ids — 4.57s against a 1.67s unscoped baseline, uncomfortably close to the 8s statement timeout on a warm cache. Applied to the aggregated rows it stays a hash semi-join costing 11ms:

unscoped (baseline) 1.67s
Brand 1.62s
Competitors 1.68s
Third-party 1.73s

The cost, stated plainly

Selecting a scope now refetches the overview, because the scope has to be known before the query runs — #742's instant toggle is gone for the three narrow scopes. In exchange the unscoped view that every page open starts on keeps its three queries in flight, so the default load is unchanged.

The client-side URL filter is kept rather than deleted: between picking a scope and the refetch landing, data still holds the previous scope's rows, and without it the table would briefly show the old scope under the new one's name. Once the fresh rows arrive it filters nothing.

The Domains tab is untouched — domains come back uncapped, so filtering them client-side is exact.

Related issue

Closes #745

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

Migration 00076 is already applied to the hosted database. It drops the old 8-argument signature before creating the 10-argument one — keeping both would make an 8-argument call ambiguous. PostgREST binds by name, so no in-flight request depends on argument position and the deploy window is safe (the same reason 00072 could do this).

  1. cd web && yarn test — 199 pass, including 8 new cases for the scope resolver: the empty include list that must mean "nothing" and the empty exclude list that must mean "everything", which are the two ways this reintroduces itself.
  2. Citations → URLs tab on a brand with competitors. Pick Competitors: the tab count is now the number that exist in the window, not the number that survived the cap, and the table pages through up to 2,000 of them.
  3. Pick Brand, then Third-party. The three counts sum to the All count.
  4. Switch back to All — unchanged, and the page load is still three parallel queries.
  5. Domains tab throughout: unchanged, still filters instantly with no refetch.
  6. Competitor Gaps: toggling the scope must not refetch it (gapFilters deliberately excludes the scope).

Verified against production, as a real org member:

all 97,320 · competitors 2,794 · own 395 · third-party 94,131

and as a member of a different organization: 0 rows — the security-definer guard from #780 is intact.

yarn typecheck, yarn lint (7 pre-existing warnings, 0 errors — the one on citations/page.tsx is on main too, confirmed by stashing), yarn format:check and yarn build all pass from web/.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • supabase/schema.sql regenerated via bash supabase/build-schema.sh
  • Changes are focused — one concern per PR

The URL tab under-reported whenever a source scope was selected. Two
changes that are each right on their own combined badly: #744 caps the
list at the top 2,000 by citation count, and #742 then applied the scope
on the client, over the rows that arrived. So the scope filtered within
the global top 2,000.

Measured on the largest brand, a 30-day window:

  scope          shown   actually exist
  Brand             21              395
  Competitors      125            2,794

And it was silent — the pager counts what it filtered, so the tab read
"125 of 125". A reader could not tell "this brand has 125 competitor
URLs" from "125 of them ranked in the global top two thousand".

citations_urls now takes the domains a scope resolves to and cuts the
set before the limit; total_urls counts the scoped set, so the tab
reports the scope rather than the cap. The three scopes now partition
the window exactly: 395 + 2,794 + 94,131 = 97,320.

What a brand or competitor domain *is* stays in TypeScript. The caller
already runs classifyDomain over every aggregated domain to build the
Domains tab, and the resolver reads that result, so the rule keeps one
definition instead of gaining a second one in SQL.

Two parameters rather than one, departing from the issue's sketch:
resolving every scope to an include list would send 17,532 domains for
Third-party — the whole cited set minus fourteen. Naming the fourteen to
exclude says the same thing in a request that fits in a packet.

The filter sits after the aggregate, and that placement is load-bearing.
Pushed into the citation scan the planner abandons the hash join for a
nested loop: 4.57s against a 1.67s unscoped baseline, close to the 8s
statement timeout on a warm cache. Applied to the aggregated rows it is
a hash semi-join costing 11ms, and every scope lands within noise of the
unscoped query.

Selecting a scope now refetches the overview, since the scope has to be
known before the query runs. The unscoped view every page open starts on
keeps its three queries in flight, so the default load is unchanged.

The Domains tab is untouched: domains come back uncapped, so filtering
them client-side is exact.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 26, 2026
@gkhngyk
gkhngyk merged commit fe673af into main Aug 27, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the feature/citations-url-scope branch August 27, 2026 16:07
gkhngyk added a commit that referenced this pull request Aug 28, 2026
…ion in prod

The Citations page and the per-URL detail page both die on load in
production with `Failed to load citation data`. Every server action in
citations.ts returns 500 before reaching its first query, all with the
same digest, while the same code works in dev.

The runtime log names the cause:

  ReferenceError: CitationsSourceScope is not defined
      at module evaluation (.next/server/chunks/ssr/_5c45c8f8._.js)

#791 added `export type { CitationsSourceScope }` to citations.ts for
convenience. That file is a 'use server' module, and Turbopack's
production transform lists every export clause of such a module as a
runtime value in ensureServerEntryExports:

  ensureServerEntryExports([k, m, n, o, CitationsSourceScope])

The type import is erased, so the bare identifier throws at module
evaluation — taking getCitationsOverview, getCitationGaps,
brandHasCitations and getCitationUrlDetail down with it. The dev
transform doesn't do this, which is why the bug only exists in deployed
builds. Declarations (`export type X = ...`, `export interface`) are
not affected — only re-export clauses.

Nothing ever imported the re-export (the type's home is
lib/citations/scope.ts and all consumers already read it from there),
so the fix is deleting the line, plus a comment so the pattern doesn't
come back. Verified by rebuilding: the identifier is gone from every
emitted chunk and each ensureServerEntryExports list holds only real
function bindings.
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.

Citations: scoping the URL tab filters inside the 2,000-row cap, so it under-reports

1 participant