Repository navigation
Description
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_mentionsparticipates. The two obvious shapes are an aggregate RPC that joinsprompt_result_citationsagainstprompt_results.competitor_mentionsserver-side, or a narrow mention-rows table alongside the citation rows. - Move the co-occurrence counting (
domainMap,byCompMap, the1 / distinct sourcesweighting) into SQL, keeping the existing weighting and theGAP_MIN_COMPETITOR_ANSWERS/GAP_MAX_ROWS/GAP_LOW_VISIBILITY_RATIOthresholds 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, andscanFilteredResultsbecomes dead once this is its last callerweb/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
perf(citations): read Competitor Gaps from the citation rows (#778)