Skip to content

Competitor Gaps still scans raw answers and silently truncates at 50,000 #777

Closed

Description

@gkhngyk

Problem

getCitationGaps is the last reader on the pre-#732 path. It pages raw prompt_results out to the app tier through scanFilteredResults, selecting id, citations, competitor_mentions, mention_count — both jsonb columns — and does all the domain classification and co-occurrence counting in JavaScript.

On the largest brand that window carries 77 MB of citations plus 52 MB of competitor_mentions across 62,285 rows, fetched 1,000 at a time in sequential requests.

The bigger problem is the same one #776 fixed for the URL detail page. scanFilteredResults stops at CITATIONS_SCAN_MAX_ROWS = 50_000 and orders created_at ascending, so on any brand past that ceiling the sweep never reaches its newest answers — the most recent data is exactly what gets dropped, and nothing on screen says the result is partial. One brand is over the ceiling today; a second is on the same trajectory.

This has stayed invisible because the tab is lazy-loaded (sourceTab !== 'gaps' returns early), so the cost is only paid when someone actually opens Competitor Gaps.

Why it wasn't fixed in #776

That PR moved the URL detail page onto prompt_result_citations, which has everything a citation-only reader needs. Gaps needs more: it correlates each answer's cited domains with its competitor_mentions, and that column is not represented in the citation index at all. So this is not a matter of pointing the existing helper at the new RPCs — it needs a decision about where the competitor side of the join lives.

Sketch of the work

  • Decide how competitor_mentions participates. The two obvious shapes are an aggregate RPC that joins prompt_result_citations against prompt_results.competitor_mentions server-side, or a narrow mention-rows table alongside the citation rows.
  • Move the co-occurrence counting (domainMap, byCompMap, the 1 / distinct sources weighting) into SQL, keeping the existing weighting and the GAP_MIN_COMPETITOR_ANSWERS / GAP_MAX_ROWS / GAP_LOW_VISIBILITY_RATIO thresholds intact.
  • Remove the row ceiling for this path so wide windows are complete rather than quietly clipped.
  • Diff old against new on live data before cutover, the way 00067 was verified: same brand, same filters, zero differences.

Where

  • web/src/lib/actions/citations.ts — getCitationGaps, and scanFilteredResults becomes dead once this is its last caller
  • web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx — the lazy load that calls it

Notes

Watch the chatgpt-shopping exclusion (#155) and the domain classification context (brand domains + competitor domains) — both have to keep behaving exactly as they do now, or the gap list changes meaning rather than just getting faster.

Activity

  1. added
    bug
    on Aug 23, 2026
  2. self-assigned this
    on Aug 23, 2026
  3. added
    citations
    on Aug 23, 2026
  4. added a commit that references this issue on Aug 23, 2026
    3c8ad0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcitations

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions