Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(insights): compare changes on the same prompts by default (#1036)
Cihan2249d2a6c60b
diff --git a/supabase/migrations/00130_insights_same_prompts.sql b/supabase/migrations/00130_insights_same_prompts.sqlnew file mode 100644index 000000000..e9d60f14f--- /dev/null+++ b/supabase/migrations/00130_insights_same_prompts.sql@@ -0,0 +1,454 @@+-- Insights "Compare: Same prompts" (VIS-002).+--+-- Adding prompts inflated every change on the Insights page: 509 new prompts+-- in a period read as +203% mentions, and the breakdown counted each new+-- prompt as a gain from 0. With "Same prompts" on, a change compares only the+-- prompts answered in both periods, and the new ones are counted separately.+--+-- That needs the Visibility components per prompt, for the brand and for+-- every competitor. Computing them from prompt_results on each page load is+-- what the daily rollups exist to avoid (unpacking competitor jsonb over 30+-- days took 16s on the largest brand, 00098), so the two prompt-grain rollup+-- tables gain them:+--+-- insights_prompt_daily the brand's components per prompt/day+-- insights_competitor_prompt_daily each competitor's, per sighting+--+-- The new columns are nullable on purpose: existing rows read NULL until+-- refresh_insights_daily rewrites their day. The read functions report such a+-- window as incomplete and the app falls back to comparing all prompts, so a+-- partly backfilled history never shows a wrong change. After deploying, run+-- the existing backfill once to fill the history:+--+-- cd server && node src/scripts/backfill-insights-daily.js+--+-- Counting rules are the ones insights_brand_daily and+-- insights_competitor_daily already use, so the per-prompt sums add up to+-- what the card reads.++-- ─── Prompt-grain components ────────────────────────────────────────────────++alter table public.insights_prompt_daily+ add column if not exists mention_answers integer,+ add column if not exists citation_answers integer,+ add column if not exists mentioning_answers integer,+ add column if not exists positive_count integer,+ add column if not exists total_mentions bigint,+ add column if not exists total_citations bigint,+ add column if not exists sum_inv_position numeric,+ add column if not exists position_count integer;++comment on column public.insights_prompt_daily.mention_answers is+ 'Answers naming the brand (00130). NULL until refresh_insights_daily rewrites the day.';++alter table public.insights_competitor_prompt_daily+ add column if not exists mention_answers integer,+ add column if not exists citation_answers integer,+ add column if not exists sum_inv_position numeric,+ add column if not exists position_count integer;++comment on column public.insights_competitor_prompt_daily.mention_answers is+ 'Answers naming the competitor in this sighting (00130). NULL until refresh_insights_daily rewrites the day.';++-- ─── Refresh: 00098 plus the new columns ────────────────────────────────────++create or replace function public.refresh_insights_daily(+ p_brand_id uuid,+ p_day_from date,+ p_day_to date+) returns void+language plpgsql+set search_path to 'public'+as $$+begin+ delete from public.insights_brand_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_competitor_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_prompt_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_competitor_prompt_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;++ insert into public.insights_brand_daily (+ brand_id, day, model_used, platform, region,+ answer_count, mention_answers, citation_answers, mentioning_answers,+ sum_visibility, sum_visibility_visible, total_mentions, total_citations,+ positive_count, sum_inv_position, position_count, max_created_at)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region,+ count(*),+ count(*) filter (where pr.mention_count > 0),+ count(*) filter (where pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0 or pr.citation_count > 0),+ coalesce(sum(pr.visibility_score), 0),+ coalesce(sum(pr.visibility_score)+ filter (where pr.mention_count > 0 or pr.citation_count > 0), 0),+ coalesce(sum(pr.mention_count), 0),+ coalesce(sum(pr.citation_count), 0),+ count(*) filter (where pr.sentiment = 'positive'),+ sum(1.0 / pr.mention_position) filter (where pr.mention_position is not null),+ count(*) filter (where pr.mention_position is not null),+ max(pr.created_at)+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping' -- #155 — isolate from Insights+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ group by 2, pr.model_used, pr.platform, pr.region;++ insert into public.insights_prompt_daily (+ brand_id, day, prompt_id, model_used, platform, region,+ answer_count, has_mention, has_citation,+ mention_answers, citation_answers, mentioning_answers, positive_count,+ total_mentions, total_citations, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.prompt_id, pr.model_used, pr.platform, pr.region,+ count(*),+ bool_or(pr.mention_count > 0),+ bool_or(pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0),+ count(*) filter (where pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0 or pr.citation_count > 0),+ count(*) filter (where pr.sentiment = 'positive'),+ coalesce(sum(pr.mention_count), 0),+ coalesce(sum(pr.citation_count), 0),+ sum(1.0 / pr.mention_position) filter (where pr.mention_position is not null),+ count(*) filter (where pr.mention_position is not null)+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ group by 2, pr.prompt_id, pr.model_used, pr.platform, pr.region;++ insert into public.insights_competitor_daily (+ brand_id, day, competitor_id, model_used, platform, region,+ answer_count, sum_visibility, total_mentions, total_citations,+ mention_answers, citation_answers, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ cm.value->>'competitor_id',+ pr.model_used, pr.platform, pr.region,+ count(*),+ sum((cm.value->>'visibility_score')::numeric),+ coalesce(sum(coalesce((cm.value->>'mention_count')::int, 0)), 0),+ coalesce(sum(coalesce((cm.value->>'citation_count')::int, 0)), 0),+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0),+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0),+ sum(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null),+ count(*) filter (where (cm.value->>'mention_position') is not null)+ from public.prompt_results pr,+ lateral jsonb_array_elements(coalesce(pr.competitor_mentions, '[]'::jsonb)) cm+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ and cm.value ? 'competitor_id'+ group by 2, cm.value->>'competitor_id', pr.model_used, pr.platform, pr.region;++ insert into public.insights_competitor_prompt_daily (+ brand_id, day, competitor_id, prompt_id, model_used, platform, region, mention_count,+ mention_answers, citation_answers, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ cm.value->>'competitor_id',+ pr.prompt_id, pr.model_used, pr.platform, pr.region,+ sum(coalesce((cm.value->>'mention_count')::int, 0)),+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0),+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0),+ sum(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null),+ count(*) filter (where (cm.value->>'mention_position') is not null)+ from public.prompt_results pr,+ lateral jsonb_array_elements(coalesce(pr.competitor_mentions, '[]'::jsonb)) cm+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ and cm.value ? 'competitor_id'+ and (coalesce((cm.value->>'mention_count')::int, 0) > 0+ or coalesce((cm.value->>'citation_count')::int, 0) > 0+ or coalesce((cm.value->>'visibility_score')::numeric, 0) > 0)+ group by 2, cm.value->>'competitor_id', pr.prompt_id, pr.model_used, pr.platform, pr.region;+end;+$$;++revoke execute on function public.refresh_insights_daily(uuid, date, date)+ from public, anon, authenticated;++-- ─── Same-prompt comparison over the rollups (whole-day windows) ───────────+--+-- The two windows' totals over the prompts answered in both, in the+-- ai_visibility_aggregates shape (so the app folds them the same way), plus+-- how many prompts were only answered in one of them. `complete` is false+-- while any row it read has not been rewritten since 00130.++create or replace function public.insights_same_prompt_compare_daily(+ p_brand_id uuid,+ p_cur_from date,+ p_cur_to date,+ p_prev_from date,+ p_prev_to date,+ p_models text[] default null,+ p_region text default null+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with prompt_rows as (+ select d.*,+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win+ from public.insights_prompt_daily d+ where d.brand_id = p_brand_id+ and (p_models is null or d.model_used = any (p_models))+ and (p_region is null or d.region = p_region)+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur' and answer_count > 0) as in_cur,+ bool_or(win = 'prev' and answer_count > 0) as in_prev+ from prompt_rows+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ brand_agg as (+ select r.win,+ sum(r.answer_count) as answers,+ sum(r.mention_answers) as mention_answers,+ sum(r.citation_answers) as citation_answers,+ sum(r.sum_inv_position) / nullif(sum(r.position_count), 0) as position_factor,+ sum(r.total_mentions) as total_mentions,+ sum(r.total_citations) as total_citations,+ sum(r.positive_count) as positive_count,+ sum(r.mentioning_answers) as mentioning_answers+ from prompt_rows r+ join same_prompts s using (prompt_id)+ group by r.win+ ),+ comp_rows as (+ select cd.*,+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win+ from public.insights_competitor_prompt_daily cd+ join same_prompts s using (prompt_id)+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ -- Competitors tracked in each window, from the dense day-grain table: the+ -- prompt-grain one only records sightings, so it can't tell a competitor+ -- never named from one not tracked yet. A competitor added during the+ -- period then has no previous value, as on all prompts, instead of a gain+ -- from 0.+ tracked as (+ select distinct+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id+ from public.insights_competitor_daily cd+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ comp_agg as (+ select t.win,+ t.competitor_id,+ max(c.name) as name,+ coalesce(sum(cr.mention_answers), 0) as mention_answers,+ coalesce(sum(cr.citation_answers), 0) as citation_answers,+ sum(cr.sum_inv_position) / nullif(sum(cr.position_count), 0) as position_factor+ from tracked t+ join public.competitors c+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id+ left join comp_rows cr+ on cr.win = t.win and cr.competitor_id = t.competitor_id+ group by t.win, t.competitor_id+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', not exists (select 1 from prompt_rows where mention_answers is null)+ and not exists (select 1 from comp_rows where mention_answers is null),+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ );+$$;++-- ─── Same-prompt comparison over the raw rows (24h and topic views) ────────+--+-- Same output from prompt_results, for the windows the card also reads raw.+-- Each window is selected on its own, inclusive at both ends like+-- ai_visibility_aggregates, so an answer exactly on the boundary counts in+-- both, as it does on the card. Always complete.++create or replace function public.insights_same_prompt_compare(+ p_brand_id uuid,+ p_cur_from timestamptz,+ p_cur_to timestamptz,+ p_prev_from timestamptz,+ p_prev_to timestamptz,+ p_models text[] default null,+ p_region text default null,+ p_topic_id uuid default null+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with filtered as (+ select pr.prompt_id, pr.created_at, pr.mention_count, pr.citation_count,+ pr.mention_position, pr.sentiment, pr.competitor_mentions+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (p_models is null or pr.model_used = any (p_models))+ and (p_region is null or pr.region = p_region)+ and pr.created_at >= p_prev_from+ and pr.created_at <= p_cur_to+ and (p_topic_id is null or exists (+ select 1 from public.prompts p+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))+ ),+ windowed as (+ select 'cur' as win, f.* from filtered f+ where f.created_at >= p_cur_from and f.created_at <= p_cur_to+ union all+ select 'prev' as win, f.* from filtered f+ where f.created_at >= p_prev_from and f.created_at <= p_prev_to+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur') as in_cur,+ bool_or(win = 'prev') as in_prev+ from windowed+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ same_rows as (+ select w.* from windowed w join same_prompts s using (prompt_id)+ ),+ brand_agg as (+ select win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ avg(1.0 / mention_position) filter (where mention_position is not null)+ as position_factor,+ coalesce(sum(mention_count), 0) as total_mentions,+ coalesce(sum(citation_count), 0) as total_citations,+ count(*) filter (where sentiment = 'positive') as positive_count,+ count(*) filter (where mention_count > 0 or citation_count > 0)+ as mentioning_answers+ from same_rows+ group by win+ ),+ comp_agg as (+ select r.win,+ cm.value->>'competitor_id' as competitor_id,+ max(c.name) as name,+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0)+ as mention_answers,+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0)+ as citation_answers,+ avg(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null)+ as position_factor+ from same_rows r+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm+ join public.competitors c+ on c.id::text = cm.value->>'competitor_id' and c.brand_id = p_brand_id+ where cm.value ? 'competitor_id'+ group by r.win, cm.value->>'competitor_id'+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', true,+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ );+$$;++revoke all on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) from public;+grant execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) to authenticated, service_role;++revoke all on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) from public;+grant execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) to authenticated, service_role;++comment on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) is+ 'Insights "Same prompts" (VIS-002, 00130): both windows'' Visibility, mention, citation and sentiment totals over the prompts answered in both, from the prompt-grain rollups, plus new/dropped prompt counts. complete=false until the rows are rewritten by refresh_insights_daily.';++comment on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) is+ 'Insights "Same prompts" (VIS-002, 00130) for timestamp windows (24h, topic views), from prompt_results. Same output as insights_same_prompt_compare_daily.';diff --git a/supabase/migrations/00147_insights_same_prompts_definer.sql b/supabase/migrations/00147_insights_same_prompts_definer.sqlnew file mode 100644index 000000000..8854f0d42--- /dev/null+++ b/supabase/migrations/00147_insights_same_prompts_definer.sql@@ -0,0 +1,326 @@+-- "Compare: Same prompts" timed out on the largest brand (#1036 review).+--+-- insights_same_prompt_compare_daily (00130) took 4.5 s as the table owner+-- and over 30 s for a signed-in member on the largest brand (~219k answers,+-- 30 days against the 30 before). The authenticated role's statement_timeout+-- is 8 s, so every Insights open fell back to comparing all prompts. Two+-- causes:+--+-- * A SQL-language function is planned without its argument values+-- (PostgreSQL 17 and older), so the brand and both day ranges were+-- guesses: a range between unknown bounds is estimated at 0.5% of the+-- rows. The plan, and the RLS membership subquery planned into it, were+-- built for a few hundred rows and got ~200k. plan_cache_mode does not+-- reach SQL functions, which is why force_custom_plan did not help.+-- * The per-day rollup rows were joined to the shared prompts again for+-- the totals, after being grouped once for the presence check.+--+-- Both functions become plpgsql with force_custom_plan, so every call is+-- planned with its real arguments, and security definer with the+-- organization check made explicit, as citations_urls_daily does (00119), so+-- RLS no longer runs inside the scan. The service role still reads: the+-- cached 24h view (cachedAnchoredInsights) calls the raw variant with it,+-- after checking the caller's access to the brand. The rollup rows are now+-- folded to one row per prompt and window first, and the raw variant keeps+-- one copy of its rows instead of three. Output and counting rules are+-- 00130's.++-- ─── Over the rollups (whole-day windows) ──────────────────────────────────++create or replace function public.insights_same_prompt_compare_daily(+ p_brand_id uuid,+ p_cur_from date,+ p_cur_to date,+ p_prev_from date,+ p_prev_to date,+ p_models text[] default null,+ p_region text default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ with per_prompt as (+ -- One row per prompt and window: nothing below reads the per-day rows.+ select d.prompt_id,+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ sum(d.answer_count) as answers,+ sum(d.mention_answers) as mention_answers,+ sum(d.citation_answers) as citation_answers,+ sum(d.sum_inv_position) as sum_inv_position,+ sum(d.position_count) as position_count,+ sum(d.total_mentions) as total_mentions,+ sum(d.total_citations) as total_citations,+ sum(d.positive_count) as positive_count,+ sum(d.mentioning_answers) as mentioning_answers,+ bool_or(d.mention_answers is null) as stale+ from public.insights_prompt_daily d+ where d.brand_id = p_brand_id+ and (p_models is null or d.model_used = any (p_models))+ and (p_region is null or d.region = p_region)+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)+ group by 1, 2+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur' and answers > 0) as in_cur,+ bool_or(win = 'prev' and answers > 0) as in_prev+ from per_prompt+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ brand_agg as (+ select p.win,+ sum(p.answers) as answers,+ sum(p.mention_answers) as mention_answers,+ sum(p.citation_answers) as citation_answers,+ sum(p.sum_inv_position) / nullif(sum(p.position_count), 0) as position_factor,+ sum(p.total_mentions) as total_mentions,+ sum(p.total_citations) as total_citations,+ sum(p.positive_count) as positive_count,+ sum(p.mentioning_answers) as mentioning_answers+ from per_prompt p+ join same_prompts s using (prompt_id)+ group by p.win+ ),+ comp_sums as (+ select case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id,+ sum(cd.mention_answers) as mention_answers,+ sum(cd.citation_answers) as citation_answers,+ sum(cd.sum_inv_position) / nullif(sum(cd.position_count), 0) as position_factor,+ bool_or(cd.mention_answers is null) as stale+ from public.insights_competitor_prompt_daily cd+ where cd.brand_id = p_brand_id+ and cd.prompt_id in (select prompt_id from same_prompts)+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ group by 1, 2+ ),+ -- Competitors tracked in each window, from the dense day-grain table (see+ -- 00130): one added during the period then has no previous value.+ tracked as (+ select distinct+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id+ from public.insights_competitor_daily cd+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ comp_agg as (+ select t.win,+ t.competitor_id,+ c.name,+ coalesce(cs.mention_answers, 0) as mention_answers,+ coalesce(cs.citation_answers, 0) as citation_answers,+ cs.position_factor+ from tracked t+ join public.competitors c+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id+ left join comp_sums cs+ on cs.win = t.win and cs.competitor_id = t.competitor_id+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', not exists (select 1 from per_prompt where stale)+ and not exists (select 1 from comp_sums where stale),+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ )+ into v_result;++ return v_result;+end;+$$;++-- ─── Over the raw rows (24h and topic views) ───────────────────────────────++create or replace function public.insights_same_prompt_compare(+ p_brand_id uuid,+ p_cur_from timestamptz,+ p_cur_to timestamptz,+ p_prev_from timestamptz,+ p_prev_to timestamptz,+ p_models text[] default null,+ p_region text default null,+ p_topic_id uuid default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ with filtered as (+ select pr.prompt_id, pr.mention_count, pr.citation_count,+ pr.mention_position, pr.sentiment, pr.competitor_mentions,+ pr.created_at >= p_cur_from and pr.created_at <= p_cur_to as in_cur,+ pr.created_at >= p_prev_from and pr.created_at <= p_prev_to as in_prev+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (p_models is null or pr.model_used = any (p_models))+ and (p_region is null or pr.region = p_region)+ and pr.created_at >= p_prev_from+ and pr.created_at <= p_cur_to+ and (p_topic_id is null or exists (+ select 1 from public.prompts p+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))+ ),+ presence as (+ select prompt_id,+ bool_or(in_cur) as in_cur,+ bool_or(in_prev) as in_prev+ from filtered+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ -- Each window selected on its own, inclusive at both ends like+ -- ai_visibility_aggregates: an answer exactly on the boundary counts in+ -- both, as it does on the card.+ same_rows as not materialized (+ select w.win, f.*+ from filtered f+ join same_prompts s using (prompt_id)+ cross join lateral (values ('cur', f.in_cur), ('prev', f.in_prev)) w(win, inside)+ where w.inside+ ),+ brand_agg as (+ select win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ avg(1.0 / mention_position) filter (where mention_position is not null)+ as position_factor,+ coalesce(sum(mention_count), 0) as total_mentions,+ coalesce(sum(citation_count), 0) as total_citations,+ count(*) filter (where sentiment = 'positive') as positive_count,+ count(*) filter (where mention_count > 0 or citation_count > 0)+ as mentioning_answers+ from same_rows+ group by win+ ),+ comp_agg as (+ select r.win,+ cm.value->>'competitor_id' as competitor_id,+ max(c.name) as name,+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0)+ as mention_answers,+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0)+ as citation_answers,+ avg(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null)+ as position_factor+ from same_rows r+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm+ join public.competitors c+ on c.id::text = cm.value->>'competitor_id' and c.brand_id = p_brand_id+ where cm.value ? 'competitor_id'+ group by r.win, cm.value->>'competitor_id'+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', true,+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ )+ into v_result;++ return v_result;+end;+$$;++revoke execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) from public, anon;+grant execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) to authenticated, service_role;++revoke execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) from public, anon;+grant execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) to authenticated, service_role;diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 9474a6eef..5095ac277 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -14958,3 +14958,791 @@ comment on function public.insights_breakdown_groups() is'Insights breakdown panel inputs (VIS-001, 00129): Visibility-score components and mention totals per (prompt, platform) for one window, plus the prompts behind them. Whole UTC days or a timestamp range. Security invoker.';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00130_insights_same_prompts.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Insights "Compare: Same prompts" (VIS-002).+--+-- Adding prompts inflated every change on the Insights page: 509 new prompts+-- in a period read as +203% mentions, and the breakdown counted each new+-- prompt as a gain from 0. With "Same prompts" on, a change compares only the+-- prompts answered in both periods, and the new ones are counted separately.+--+-- That needs the Visibility components per prompt, for the brand and for+-- every competitor. Computing them from prompt_results on each page load is+-- what the daily rollups exist to avoid (unpacking competitor jsonb over 30+-- days took 16s on the largest brand, 00098), so the two prompt-grain rollup+-- tables gain them:+--+-- insights_prompt_daily the brand's components per prompt/day+-- insights_competitor_prompt_daily each competitor's, per sighting+--+-- The new columns are nullable on purpose: existing rows read NULL until+-- refresh_insights_daily rewrites their day. The read functions report such a+-- window as incomplete and the app falls back to comparing all prompts, so a+-- partly backfilled history never shows a wrong change. After deploying, run+-- the existing backfill once to fill the history:+--+-- cd server && node src/scripts/backfill-insights-daily.js+--+-- Counting rules are the ones insights_brand_daily and+-- insights_competitor_daily already use, so the per-prompt sums add up to+-- what the card reads.++-- ─── Prompt-grain components ────────────────────────────────────────────────++alter table public.insights_prompt_daily+ add column if not exists mention_answers integer,+ add column if not exists citation_answers integer,+ add column if not exists mentioning_answers integer,+ add column if not exists positive_count integer,+ add column if not exists total_mentions bigint,+ add column if not exists total_citations bigint,+ add column if not exists sum_inv_position numeric,+ add column if not exists position_count integer;++comment on column public.insights_prompt_daily.mention_answers is+ 'Answers naming the brand (00130). NULL until refresh_insights_daily rewrites the day.';++alter table public.insights_competitor_prompt_daily+ add column if not exists mention_answers integer,+ add column if not exists citation_answers integer,+ add column if not exists sum_inv_position numeric,+ add column if not exists position_count integer;++comment on column public.insights_competitor_prompt_daily.mention_answers is+ 'Answers naming the competitor in this sighting (00130). NULL until refresh_insights_daily rewrites the day.';++-- ─── Refresh: 00098 plus the new columns ────────────────────────────────────++create or replace function public.refresh_insights_daily(+ p_brand_id uuid,+ p_day_from date,+ p_day_to date+) returns void+language plpgsql+set search_path to 'public'+as $$+begin+ delete from public.insights_brand_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_competitor_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_prompt_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;+ delete from public.insights_competitor_prompt_daily+ where brand_id = p_brand_id and day between p_day_from and p_day_to;++ insert into public.insights_brand_daily (+ brand_id, day, model_used, platform, region,+ answer_count, mention_answers, citation_answers, mentioning_answers,+ sum_visibility, sum_visibility_visible, total_mentions, total_citations,+ positive_count, sum_inv_position, position_count, max_created_at)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.model_used, pr.platform, pr.region,+ count(*),+ count(*) filter (where pr.mention_count > 0),+ count(*) filter (where pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0 or pr.citation_count > 0),+ coalesce(sum(pr.visibility_score), 0),+ coalesce(sum(pr.visibility_score)+ filter (where pr.mention_count > 0 or pr.citation_count > 0), 0),+ coalesce(sum(pr.mention_count), 0),+ coalesce(sum(pr.citation_count), 0),+ count(*) filter (where pr.sentiment = 'positive'),+ sum(1.0 / pr.mention_position) filter (where pr.mention_position is not null),+ count(*) filter (where pr.mention_position is not null),+ max(pr.created_at)+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping' -- #155 — isolate from Insights+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ group by 2, pr.model_used, pr.platform, pr.region;++ insert into public.insights_prompt_daily (+ brand_id, day, prompt_id, model_used, platform, region,+ answer_count, has_mention, has_citation,+ mention_answers, citation_answers, mentioning_answers, positive_count,+ total_mentions, total_citations, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ pr.prompt_id, pr.model_used, pr.platform, pr.region,+ count(*),+ bool_or(pr.mention_count > 0),+ bool_or(pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0),+ count(*) filter (where pr.citation_count > 0),+ count(*) filter (where pr.mention_count > 0 or pr.citation_count > 0),+ count(*) filter (where pr.sentiment = 'positive'),+ coalesce(sum(pr.mention_count), 0),+ coalesce(sum(pr.citation_count), 0),+ sum(1.0 / pr.mention_position) filter (where pr.mention_position is not null),+ count(*) filter (where pr.mention_position is not null)+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ group by 2, pr.prompt_id, pr.model_used, pr.platform, pr.region;++ insert into public.insights_competitor_daily (+ brand_id, day, competitor_id, model_used, platform, region,+ answer_count, sum_visibility, total_mentions, total_citations,+ mention_answers, citation_answers, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ cm.value->>'competitor_id',+ pr.model_used, pr.platform, pr.region,+ count(*),+ sum((cm.value->>'visibility_score')::numeric),+ coalesce(sum(coalesce((cm.value->>'mention_count')::int, 0)), 0),+ coalesce(sum(coalesce((cm.value->>'citation_count')::int, 0)), 0),+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0),+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0),+ sum(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null),+ count(*) filter (where (cm.value->>'mention_position') is not null)+ from public.prompt_results pr,+ lateral jsonb_array_elements(coalesce(pr.competitor_mentions, '[]'::jsonb)) cm+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ and cm.value ? 'competitor_id'+ group by 2, cm.value->>'competitor_id', pr.model_used, pr.platform, pr.region;++ insert into public.insights_competitor_prompt_daily (+ brand_id, day, competitor_id, prompt_id, model_used, platform, region, mention_count,+ mention_answers, citation_answers, sum_inv_position, position_count)+ select+ p_brand_id,+ (pr.created_at at time zone 'utc')::date,+ cm.value->>'competitor_id',+ pr.prompt_id, pr.model_used, pr.platform, pr.region,+ sum(coalesce((cm.value->>'mention_count')::int, 0)),+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0),+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0),+ sum(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null),+ count(*) filter (where (cm.value->>'mention_position') is not null)+ from public.prompt_results pr,+ lateral jsonb_array_elements(coalesce(pr.competitor_mentions, '[]'::jsonb)) cm+ where pr.brand_id = p_brand_id+ and pr.prompt_id is not null+ and pr.platform <> 'chatgpt-shopping'+ and (pr.created_at at time zone 'utc')::date between p_day_from and p_day_to+ and cm.value ? 'competitor_id'+ and (coalesce((cm.value->>'mention_count')::int, 0) > 0+ or coalesce((cm.value->>'citation_count')::int, 0) > 0+ or coalesce((cm.value->>'visibility_score')::numeric, 0) > 0)+ group by 2, cm.value->>'competitor_id', pr.prompt_id, pr.model_used, pr.platform, pr.region;+end;+$$;++revoke execute on function public.refresh_insights_daily(uuid, date, date)+ from public, anon, authenticated;++-- ─── Same-prompt comparison over the rollups (whole-day windows) ───────────+--+-- The two windows' totals over the prompts answered in both, in the+-- ai_visibility_aggregates shape (so the app folds them the same way), plus+-- how many prompts were only answered in one of them. `complete` is false+-- while any row it read has not been rewritten since 00130.++create or replace function public.insights_same_prompt_compare_daily(+ p_brand_id uuid,+ p_cur_from date,+ p_cur_to date,+ p_prev_from date,+ p_prev_to date,+ p_models text[] default null,+ p_region text default null+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with prompt_rows as (+ select d.*,+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win+ from public.insights_prompt_daily d+ where d.brand_id = p_brand_id+ and (p_models is null or d.model_used = any (p_models))+ and (p_region is null or d.region = p_region)+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur' and answer_count > 0) as in_cur,+ bool_or(win = 'prev' and answer_count > 0) as in_prev+ from prompt_rows+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ brand_agg as (+ select r.win,+ sum(r.answer_count) as answers,+ sum(r.mention_answers) as mention_answers,+ sum(r.citation_answers) as citation_answers,+ sum(r.sum_inv_position) / nullif(sum(r.position_count), 0) as position_factor,+ sum(r.total_mentions) as total_mentions,+ sum(r.total_citations) as total_citations,+ sum(r.positive_count) as positive_count,+ sum(r.mentioning_answers) as mentioning_answers+ from prompt_rows r+ join same_prompts s using (prompt_id)+ group by r.win+ ),+ comp_rows as (+ select cd.*,+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win+ from public.insights_competitor_prompt_daily cd+ join same_prompts s using (prompt_id)+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ -- Competitors tracked in each window, from the dense day-grain table: the+ -- prompt-grain one only records sightings, so it can't tell a competitor+ -- never named from one not tracked yet. A competitor added during the+ -- period then has no previous value, as on all prompts, instead of a gain+ -- from 0.+ tracked as (+ select distinct+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id+ from public.insights_competitor_daily cd+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ comp_agg as (+ select t.win,+ t.competitor_id,+ max(c.name) as name,+ coalesce(sum(cr.mention_answers), 0) as mention_answers,+ coalesce(sum(cr.citation_answers), 0) as citation_answers,+ sum(cr.sum_inv_position) / nullif(sum(cr.position_count), 0) as position_factor+ from tracked t+ join public.competitors c+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id+ left join comp_rows cr+ on cr.win = t.win and cr.competitor_id = t.competitor_id+ group by t.win, t.competitor_id+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', not exists (select 1 from prompt_rows where mention_answers is null)+ and not exists (select 1 from comp_rows where mention_answers is null),+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ );+$$;++-- ─── Same-prompt comparison over the raw rows (24h and topic views) ────────+--+-- Same output from prompt_results, for the windows the card also reads raw.+-- Each window is selected on its own, inclusive at both ends like+-- ai_visibility_aggregates, so an answer exactly on the boundary counts in+-- both, as it does on the card. Always complete.++create or replace function public.insights_same_prompt_compare(+ p_brand_id uuid,+ p_cur_from timestamptz,+ p_cur_to timestamptz,+ p_prev_from timestamptz,+ p_prev_to timestamptz,+ p_models text[] default null,+ p_region text default null,+ p_topic_id uuid default null+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with filtered as (+ select pr.prompt_id, pr.created_at, pr.mention_count, pr.citation_count,+ pr.mention_position, pr.sentiment, pr.competitor_mentions+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (p_models is null or pr.model_used = any (p_models))+ and (p_region is null or pr.region = p_region)+ and pr.created_at >= p_prev_from+ and pr.created_at <= p_cur_to+ and (p_topic_id is null or exists (+ select 1 from public.prompts p+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))+ ),+ windowed as (+ select 'cur' as win, f.* from filtered f+ where f.created_at >= p_cur_from and f.created_at <= p_cur_to+ union all+ select 'prev' as win, f.* from filtered f+ where f.created_at >= p_prev_from and f.created_at <= p_prev_to+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur') as in_cur,+ bool_or(win = 'prev') as in_prev+ from windowed+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ same_rows as (+ select w.* from windowed w join same_prompts s using (prompt_id)+ ),+ brand_agg as (+ select win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ avg(1.0 / mention_position) filter (where mention_position is not null)+ as position_factor,+ coalesce(sum(mention_count), 0) as total_mentions,+ coalesce(sum(citation_count), 0) as total_citations,+ count(*) filter (where sentiment = 'positive') as positive_count,+ count(*) filter (where mention_count > 0 or citation_count > 0)+ as mentioning_answers+ from same_rows+ group by win+ ),+ comp_agg as (+ select r.win,+ cm.value->>'competitor_id' as competitor_id,+ max(c.name) as name,+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0)+ as mention_answers,+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0)+ as citation_answers,+ avg(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null)+ as position_factor+ from same_rows r+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm+ join public.competitors c+ on c.id::text = cm.value->>'competitor_id' and c.brand_id = p_brand_id+ where cm.value ? 'competitor_id'+ group by r.win, cm.value->>'competitor_id'+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', true,+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ );+$$;++revoke all on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) from public;+grant execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) to authenticated, service_role;++revoke all on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) from public;+grant execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) to authenticated, service_role;++comment on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) is+ 'Insights "Same prompts" (VIS-002, 00130): both windows'' Visibility, mention, citation and sentiment totals over the prompts answered in both, from the prompt-grain rollups, plus new/dropped prompt counts. complete=false until the rows are rewritten by refresh_insights_daily.';++comment on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) is+ 'Insights "Same prompts" (VIS-002, 00130) for timestamp windows (24h, topic views), from prompt_results. Same output as insights_same_prompt_compare_daily.';++-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00147_insights_same_prompts_definer.sql+-- ─────────────────────────────────────────────────────────────────────────+-- "Compare: Same prompts" timed out on the largest brand (#1036 review).+--+-- insights_same_prompt_compare_daily (00130) took 4.5 s as the table owner+-- and over 30 s for a signed-in member on the largest brand (~219k answers,+-- 30 days against the 30 before). The authenticated role's statement_timeout+-- is 8 s, so every Insights open fell back to comparing all prompts. Two+-- causes:+--+-- * A SQL-language function is planned without its argument values+-- (PostgreSQL 17 and older), so the brand and both day ranges were+-- guesses: a range between unknown bounds is estimated at 0.5% of the+-- rows. The plan, and the RLS membership subquery planned into it, were+-- built for a few hundred rows and got ~200k. plan_cache_mode does not+-- reach SQL functions, which is why force_custom_plan did not help.+-- * The per-day rollup rows were joined to the shared prompts again for+-- the totals, after being grouped once for the presence check.+--+-- Both functions become plpgsql with force_custom_plan, so every call is+-- planned with its real arguments, and security definer with the+-- organization check made explicit, as citations_urls_daily does (00119), so+-- RLS no longer runs inside the scan. The service role still reads: the+-- cached 24h view (cachedAnchoredInsights) calls the raw variant with it,+-- after checking the caller's access to the brand. The rollup rows are now+-- folded to one row per prompt and window first, and the raw variant keeps+-- one copy of its rows instead of three. Output and counting rules are+-- 00130's.++-- ─── Over the rollups (whole-day windows) ──────────────────────────────────++create or replace function public.insights_same_prompt_compare_daily(+ p_brand_id uuid,+ p_cur_from date,+ p_cur_to date,+ p_prev_from date,+ p_prev_to date,+ p_models text[] default null,+ p_region text default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ with per_prompt as (+ -- One row per prompt and window: nothing below reads the per-day rows.+ select d.prompt_id,+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ sum(d.answer_count) as answers,+ sum(d.mention_answers) as mention_answers,+ sum(d.citation_answers) as citation_answers,+ sum(d.sum_inv_position) as sum_inv_position,+ sum(d.position_count) as position_count,+ sum(d.total_mentions) as total_mentions,+ sum(d.total_citations) as total_citations,+ sum(d.positive_count) as positive_count,+ sum(d.mentioning_answers) as mentioning_answers,+ bool_or(d.mention_answers is null) as stale+ from public.insights_prompt_daily d+ where d.brand_id = p_brand_id+ and (p_models is null or d.model_used = any (p_models))+ and (p_region is null or d.region = p_region)+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)+ group by 1, 2+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur' and answers > 0) as in_cur,+ bool_or(win = 'prev' and answers > 0) as in_prev+ from per_prompt+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ brand_agg as (+ select p.win,+ sum(p.answers) as answers,+ sum(p.mention_answers) as mention_answers,+ sum(p.citation_answers) as citation_answers,+ sum(p.sum_inv_position) / nullif(sum(p.position_count), 0) as position_factor,+ sum(p.total_mentions) as total_mentions,+ sum(p.total_citations) as total_citations,+ sum(p.positive_count) as positive_count,+ sum(p.mentioning_answers) as mentioning_answers+ from per_prompt p+ join same_prompts s using (prompt_id)+ group by p.win+ ),+ comp_sums as (+ select case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id,+ sum(cd.mention_answers) as mention_answers,+ sum(cd.citation_answers) as citation_answers,+ sum(cd.sum_inv_position) / nullif(sum(cd.position_count), 0) as position_factor,+ bool_or(cd.mention_answers is null) as stale+ from public.insights_competitor_prompt_daily cd+ where cd.brand_id = p_brand_id+ and cd.prompt_id in (select prompt_id from same_prompts)+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ group by 1, 2+ ),+ -- Competitors tracked in each window, from the dense day-grain table (see+ -- 00130): one added during the period then has no previous value.+ tracked as (+ select distinct+ case when cd.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win,+ cd.competitor_id+ from public.insights_competitor_daily cd+ where cd.brand_id = p_brand_id+ and (p_models is null or cd.model_used = any (p_models))+ and (p_region is null or cd.region = p_region)+ and (cd.day between p_cur_from and p_cur_to or cd.day between p_prev_from and p_prev_to)+ ),+ comp_agg as (+ select t.win,+ t.competitor_id,+ c.name,+ coalesce(cs.mention_answers, 0) as mention_answers,+ coalesce(cs.citation_answers, 0) as citation_answers,+ cs.position_factor+ from tracked t+ join public.competitors c+ on c.id::text = t.competitor_id and c.brand_id = p_brand_id+ left join comp_sums cs+ on cs.win = t.win and cs.competitor_id = t.competitor_id+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', not exists (select 1 from per_prompt where stale)+ and not exists (select 1 from comp_sums where stale),+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ )+ into v_result;++ return v_result;+end;+$$;++-- ─── Over the raw rows (24h and topic views) ───────────────────────────────++create or replace function public.insights_same_prompt_compare(+ p_brand_id uuid,+ p_cur_from timestamptz,+ p_cur_to timestamptz,+ p_prev_from timestamptz,+ p_prev_to timestamptz,+ p_models text[] default null,+ p_region text default null,+ p_topic_id uuid default null+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set work_mem to '64MB'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_result jsonb;+begin+ if auth.role() is distinct from 'service_role' and not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) then+ return null;+ end if;++ with filtered as (+ select pr.prompt_id, pr.mention_count, pr.citation_count,+ pr.mention_position, pr.sentiment, pr.competitor_mentions,+ pr.created_at >= p_cur_from and pr.created_at <= p_cur_to as in_cur,+ pr.created_at >= p_prev_from and pr.created_at <= p_prev_to as in_prev+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (p_models is null or pr.model_used = any (p_models))+ and (p_region is null or pr.region = p_region)+ and pr.created_at >= p_prev_from+ and pr.created_at <= p_cur_to+ and (p_topic_id is null or exists (+ select 1 from public.prompts p+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))+ ),+ presence as (+ select prompt_id,+ bool_or(in_cur) as in_cur,+ bool_or(in_prev) as in_prev+ from filtered+ group by prompt_id+ ),+ same_prompts as (+ select prompt_id from presence where in_cur and in_prev+ ),+ -- Each window selected on its own, inclusive at both ends like+ -- ai_visibility_aggregates: an answer exactly on the boundary counts in+ -- both, as it does on the card.+ same_rows as not materialized (+ select w.win, f.*+ from filtered f+ join same_prompts s using (prompt_id)+ cross join lateral (values ('cur', f.in_cur), ('prev', f.in_prev)) w(win, inside)+ where w.inside+ ),+ brand_agg as (+ select win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ avg(1.0 / mention_position) filter (where mention_position is not null)+ as position_factor,+ coalesce(sum(mention_count), 0) as total_mentions,+ coalesce(sum(citation_count), 0) as total_citations,+ count(*) filter (where sentiment = 'positive') as positive_count,+ count(*) filter (where mention_count > 0 or citation_count > 0)+ as mentioning_answers+ from same_rows+ group by win+ ),+ comp_agg as (+ select r.win,+ cm.value->>'competitor_id' as competitor_id,+ max(c.name) as name,+ count(*) filter (where coalesce((cm.value->>'mention_count')::int, 0) > 0)+ as mention_answers,+ count(*) filter (where coalesce((cm.value->>'citation_count')::int, 0) > 0)+ as citation_answers,+ avg(1.0 / (cm.value->>'mention_position')::numeric)+ filter (where (cm.value->>'mention_position') is not null)+ as position_factor+ from same_rows r+ cross join lateral jsonb_array_elements(coalesce(r.competitor_mentions, '[]'::jsonb)) cm+ join public.competitors c+ on c.id::text = cm.value->>'competitor_id' and c.brand_id = p_brand_id+ where cm.value ? 'competitor_id'+ group by r.win, cm.value->>'competitor_id'+ ),+ windows(win) as (values ('cur'), ('prev'))+ select jsonb_build_object(+ 'complete', true,+ 'same_prompts', (select count(*) from same_prompts),+ 'new_prompts', (select count(*) from presence where in_cur and not in_prev),+ 'dropped_prompts', (select count(*) from presence where in_prev and not in_cur)+ ) || (+ select jsonb_object_agg(w.win, jsonb_build_object(+ 'answers', coalesce(b.answers, 0),+ 'mention_answers', coalesce(b.mention_answers, 0),+ 'citation_answers', coalesce(b.citation_answers, 0),+ 'position_factor', b.position_factor,+ 'total_mentions', coalesce(b.total_mentions, 0),+ 'total_citations', coalesce(b.total_citations, 0),+ 'positive_count', coalesce(b.positive_count, 0),+ 'mentioning_answers', coalesce(b.mentioning_answers, 0),+ 'by_competitor', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'competitor_id', ca.competitor_id,+ 'name', ca.name,+ 'mention_answers', ca.mention_answers,+ 'citation_answers', ca.citation_answers,+ 'position_factor', ca.position_factor)+ order by ca.mention_answers desc, ca.competitor_id)+ from comp_agg ca where ca.win = w.win),+ '[]'::jsonb)))+ from windows w+ left join brand_agg b on b.win = w.win+ )+ into v_result;++ return v_result;+end;+$$;++revoke execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) from public, anon;+grant execute on function public.insights_same_prompt_compare_daily(+ uuid, date, date, date, date, text[], text+) to authenticated, service_role;++revoke execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) from public, anon;+grant execute on function public.insights_same_prompt_compare(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid+) to authenticated, service_role;+diff --git a/web/messages/en.json b/web/messages/en.jsonindex b83c4821b..ee856c76e 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -1678,6 +1678,13 @@"brandCitations": "Brand Citations","totalCitations": "Total Citations","checkedAt": "Checked: {date}"+ },+ "compare": {+ "label": "Compare",+ "samePrompts": "Same prompts",+ "hint": "Changes compare only prompts tracked in both periods",+ "newExcluded": "{count, plural, one {# new prompt excluded} other {# new prompts excluded}}",+ "newIncluded": "{count, plural, one {includes # new prompt} other {includes # new prompts}}"}},"topics": {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/insights/_metric-breakdown-sheet.tsx b/web/src/app/[locale]/(dashboard)/dashboard/insights/_metric-breakdown-sheet.tsxindex 6cf2df8d5..1538cf1c1 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/insights/_metric-breakdown-sheet.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/insights/_metric-breakdown-sheet.tsx@@ -131,6 +131,8 @@ function BreakdownSkeleton() {function BreakdownBody({ data, metric }: { data: InsightsBreakdown; metric: BreakdownMetric }) {const [tab, setTab] = useState<'prompts' | 'platforms' | 'topics'>('prompts');+ const samePrompts = data.comparison?.basis === 'same';+ const newCount = data.comparison?.newPrompts ?? 0;const pickRows = () =>tab === 'prompts' ? data.byPrompt : tab === 'platforms' ? data.byPlatform : data.byTopic;@@ -152,14 +154,30 @@ function BreakdownBody({ data, metric }: { data: InsightsBreakdown; metric: Brea</div>{data.windowDays !== null && (<div className="text-xs text-muted-foreground">- vs previous{' '}- {data.prevTotal === null ? (- 'no data'+ {samePrompts && data.comparison!.samePrompts === 0 ? (+ 'No prompt was answered in both periods'+ ) : samePrompts ? (+ <>+ Same {data.comparison!.samePrompts}{' '}+ {data.comparison!.samePrompts === 1 ? 'prompt' : 'prompts'}:{' '}+ <span className="tabular-nums">+ {data.prevTotal === null ? '—' : formatValue(data.prevTotal, metric)} →{' '}+ {formatValue(data.comparedCur, metric)}+ {METRIC_UNIT[metric]}+ </span>+ </>) : (- <span className="tabular-nums">- {formatValue(data.prevTotal, metric)}- {METRIC_UNIT[metric]}- </span>+ <>+ vs previous{' '}+ {data.prevTotal === null ? (+ 'no data'+ ) : (+ <span className="tabular-nums">+ {formatValue(data.prevTotal, metric)}+ {METRIC_UNIT[metric]}+ </span>+ )}+ </>)}</div>)}@@ -169,6 +187,13 @@ function BreakdownBody({ data, metric }: { data: InsightsBreakdown; metric: Brea)}</div><RootCauseSummary data={data} metric={metric} />+ {newCount > 0 && (+ <div className="mt-2 border-t pt-2 text-xs text-muted-foreground">+ {samePrompts+ ? `Not counted in the change: ${newCount} new ${newCount === 1 ? 'prompt' : 'prompts'} this period.`+ : `The change includes ${newCount} new ${newCount === 1 ? 'prompt' : 'prompts'} added this period.`}+ </div>+ )}</div>{/* Tabs */}diff --git a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsxindex b0de0b972..41c1849ed 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx@@ -113,6 +113,7 @@ import { usePlanContext } from '@/components/providers/plan-provider';import { formatCompactNumber } from '@/lib/format';import { toast } from 'sonner';import { readUrlChoice, readUrlParam, writeUrlParams } from '@/lib/url-state';+import type { PromptComparison } from '@/lib/insights-same-prompts';// ─── Filter Types ─────────────────────────────────────────────────────────────@@ -127,6 +128,8 @@ interface InsightsFilters {region: string;model: string;topic: string;+ /** "Compare: Same prompts" (VIS-002): changes compare only prompts answered in both periods. */+ samePrompts: boolean;}const DEFAULT_FILTERS: InsightsFilters = {@@ -136,12 +139,14 @@ const DEFAULT_FILTERS: InsightsFilters = {region: '',model: '',topic: '',+ samePrompts: true,};/*** The filters as kept in the URL (#917): ?range=, plus ?from=/&to= for a- * custom range, and ?region=, ?model=, ?topic=. Defaults are left out, and an- * unknown range falls back to the default.+ * custom range, ?region=, ?model=, ?topic=, and ?compare=all when "Same+ * prompts" is off. Defaults are left out, and an unknown range falls back to+ * the default.*/function filtersFromUrl(): InsightsFilters {const datePreset = readUrlChoice<DatePreset>(@@ -156,6 +161,7 @@ function filtersFromUrl(): InsightsFilters {region: readUrlParam('region'),model: readUrlParam('model'),topic: readUrlParam('topic'),+ samePrompts: readUrlParam('compare') !== 'all',};}@@ -169,6 +175,7 @@ function writeFiltersToUrl(f: InsightsFilters) {region: f.region,model: f.model,topic: f.topic,+ compare: f.samePrompts ? null : 'all',},{ range: DEFAULT_FILTERS.datePreset },);@@ -542,6 +549,7 @@ function FilterBar({availableTopics: Topic[];}) {const t = useTranslations('common');+ const tInsights = useTranslations('insights');const set = (patch: Partial<InsightsFilters>) => onChange({ ...filters, ...patch });return (@@ -652,6 +660,35 @@ function FilterBar({</SelectContent></Select></div>+ {/* Compare: Same prompts (VIS-002) */}+ <div>+ <label className="mb-1.5 block text-xs font-medium text-muted-foreground">+ {tInsights('compare.label')}+ </label>+ <button+ type="button"+ role="switch"+ aria-checked={filters.samePrompts}+ title={tInsights('compare.hint')}+ onClick={() => set({ samePrompts: !filters.samePrompts })}+ className="inline-flex h-8 items-center gap-2 rounded-md border bg-background px-2.5 text-xs transition-colors hover:bg-muted/50"+ >+ <span+ className={cn(+ 'relative inline-flex h-4 w-7 shrink-0 items-center rounded-full transition-colors',+ filters.samePrompts ? 'bg-primary' : 'bg-muted-foreground/30',+ )}+ >+ <span+ className={cn(+ 'block h-3 w-3 rounded-full bg-background shadow transition-transform',+ filters.samePrompts ? 'translate-x-3.5' : 'translate-x-0.5',+ )}+ />+ </span>+ {tInsights('compare.samePrompts')}+ </button>+ </div></div>);}@@ -1034,6 +1071,7 @@ export default function InsightsPage() {const [rateTrend, setRateTrend] = useState<VisibilityRateTrendData | null>(null);const [sovData, setSovData] = useState<ShareOfVoiceData | null>(null);const [recommendations, setRecommendations] = useState<InsightsRecommendations | null>(null);+ const [comparison, setComparison] = useState<PromptComparison | null>(null);const [breakdownMetric, setBreakdownMetric] = useState<BreakdownMetric | null>(null);// The window and filters the cards were last loaded with (the 24h view// anchored to its tracking run): the breakdown panel explains those exact@@ -1083,6 +1121,7 @@ export default function InsightsPage() {// rollups (00066). Absent for 24h, whose anchored window above is// timestamp-shaped and stays on the raw path.days,+ samePrompts: f.samePrompts,};const hasFilters = Boolean(f.datePreset !== 'all' || f.model || f.region || f.topic);@@ -1119,6 +1158,7 @@ export default function InsightsPage() {setCompetitorData(insights.competitors.brands.length > 1 ? insights.competitors : null);setSovData(insights.sov.byPlatform.length > 0 ? insights.sov : null);setRecommendations(insights.recommendations);+ setComparison(insights.comparison);setHasAnyData(insights.hasAnyData);// Group raw model slugs by their resolved display name so different@@ -1713,6 +1753,18 @@ export default function InsightsPage() {{formatWindowBound(rateTrend.summary.prevTo)}</span>)}+ {/* Shown even without a change: when every prompt is new,+ this is why there is nothing to compare. */}+ {comparison && comparison.newPrompts > 0 && (+ <span className="text-muted-foreground" title={t('compare.hint')}>+ {rateTrend.summary.change !== null &&+ rateTrend.summary.prevFrom &&+ '· '}+ {comparison.basis === 'same'+ ? t('compare.newExcluded', { count: comparison.newPrompts })+ : t('compare.newIncluded', { count: comparison.newPrompts })}+ </span>+ )}</div></div>) : (diff --git a/web/src/lib/actions/tracking-insights-cache.test.ts b/web/src/lib/actions/tracking-insights-cache.test.tsnew file mode 100644index 000000000..a76c72031--- /dev/null+++ b/web/src/lib/actions/tracking-insights-cache.test.ts@@ -0,0 +1,157 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * The anchored 24h view is computed in strict mode and cached until the next+ * tracking run (cachedAnchoredInsights). A section that fails there must+ * fail the cached read, so the page falls back to a live read and nothing+ * degraded is stored. The same-prompt comparison (VIS-002) used to swallow+ * its own errors: one timeout then cached the all-prompt changes, with no+ * new-prompts note, until the next run.+ */++let sameError: { message: string } | null = null;++const visibility = (answers: number, mentions: number) => ({+ answers,+ mention_answers: mentions,+ citation_answers: 50,+ position_factor: 0.5,+ by_competitor: [],+});+const sameTotals = (answers: number, mentions: number) => ({+ ...visibility(answers, mentions),+ total_mentions: mentions,+ total_citations: 50,+ positive_count: 20,+ mentioning_answers: mentions,+});+const SAME = {+ complete: true,+ same_prompts: 200,+ new_prompts: 509,+ dropped_prompts: 3,+ cur: sameTotals(800, 220),+ prev: sameTotals(780, 200),+};++function rpc(name: string) {+ let data: unknown = null;+ if (name === 'insights_same_prompt_compare') {+ if (sameError) return Promise.resolve({ data: null, error: sameError });+ data = SAME;+ } else if (name === 'ai_visibility_aggregates') data = visibility(1800, 540);+ else if (name === 'insights_aggregates') {+ data = {+ total_results: 1800,+ sum_visibility: 0,+ total_mentions: 710,+ total_citations: 180,+ positive_count: 0,+ mentioning_results: 0,+ last_checked_at: null,+ by_model: [],+ };+ } else if (name === 'competitor_aggregates') {+ data = {+ brand_row_count: 1800,+ brand_sum_visibility: 0,+ brand_total_mentions: 710,+ brand_total_citations: 180,+ brand_prompt_count: 2,+ brand_visible_prompts: 2,+ by_competitor: [],+ by_brand_provider: [],+ by_competitor_provider: [],+ };+ } else if (name === 'share_of_voice_aggregates') {+ data = { total_brand_mentions: 0, total_competitor_mentions: 0, by_platform: [] };+ } else if (name === 'visibility_rate_trend') data = [];+ else if (name === 'insights_filter_options') data = [{ regions: [], models: [] }];+ else if (name === 'tracked_prompt_count') data = 2;+ return Promise.resolve({ data, error: null });+}++// The last two completed tracking runs anchor the 24h window.+const RUNS = [+ { completed_at: '2026-10-14T06:00:00.000Z' },+ { completed_at: '2026-10-13T06:00:00.000Z' },+];++function table(name: string) {+ let activeRunQuery = false;+ const builder: Record<string, unknown> = {+ is: () => {+ activeRunQuery = true;+ return builder;+ },+ maybeSingle: async () => ({ data: { id: 'b1' }, error: null }),+ single: async () => ({ data: { organization_id: 'org-1' }, error: null }),+ then: (resolve: (v: { data: unknown[]; error: null }) => unknown) =>+ resolve({ data: name === 'tracking_runs' && !activeRunQuery ? RUNS : [], error: null }),+ };+ for (const method of ['select', 'eq', 'neq', 'not', 'gte', 'order', 'limit']) {+ builder[method] = () => builder;+ }+ return builder;+}++const client = { from: table, rpc };++vi.mock('@/lib/supabase/server', () => ({ createClient: async () => client }));+vi.mock('@/lib/supabase/admin', () => ({ supabaseAdmin: client }));+vi.mock('@/lib/actions/topic', () => ({ getTopicById: vi.fn() }));+vi.mock('@/lib/guards/plan-guard', () => ({+ getOrgPlan: async () => ({ limits: { maxPrompts: 100 } }),+}));+vi.mock('@/lib/prompt-locations', () => ({ getOrgLocationUsage: async () => ({ total: 2 }) }));+vi.mock('@/lib/actions/volumes', () => ({ getPromptVolumes: async () => ({ volumes: [] }) }));+vi.mock('@/lib/actions/prompt-suggestions', () => ({+ getPromptSuggestions: async () => ({ suggestions: [] }),+}));++// unstable_cache as Next runs it: a result is stored once its function+// resolves, and a thrown error stores nothing.+const cache = new Map<string, unknown>();+vi.mock('next/cache', () => ({+ unstable_cache: (fn: () => Promise<unknown>, keyParts: string[]) => async () => {+ const key = keyParts.join('|');+ if (!cache.has(key)) cache.set(key, await fn());+ return cache.get(key);+ },+}));++const { getInsightsData } = await import('./tracking');++describe('the cached 24h Insights view (VIS-002)', () => {+ beforeEach(() => {+ cache.clear();+ sameError = null;+ });++ it('keeps a failed same-prompt comparison out of the cache', async () => {+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});+ sameError = { message: 'canceling statement due to statement timeout' };++ // The live fallback still shows the page, comparing all prompts.+ const degraded = await getInsightsData('b1', { anchor24h: true });+ expect(degraded.comparison).toBeNull();+ expect(cache.size).toBe(0);+ expect(errorLog).toHaveBeenCalled();+ errorLog.mockRestore();++ // The next open reads it again instead of serving the degraded result.+ sameError = null;+ const fresh = await getInsightsData('b1', { anchor24h: true });+ expect(fresh.comparison).toMatchObject({ basis: 'same', newPrompts: 509 });+ expect(cache.size).toBe(1);+ });++ it('caches the view once the comparison reads', async () => {+ const first = await getInsightsData('b1', { anchor24h: true });+ sameError = { message: 'canceling statement due to statement timeout' };+ const second = await getInsightsData('b1', { anchor24h: true });++ expect(second.comparison).toEqual(first.comparison);+ expect(second.comparison?.samePrompts).toBe(200);+ });+});diff --git a/web/src/lib/actions/tracking-visibility-parity.test.ts b/web/src/lib/actions/tracking-visibility-parity.test.tsindex 09fd99796..e6d843fba 100644--- a/web/src/lib/actions/tracking-visibility-parity.test.ts+++ b/web/src/lib/actions/tracking-visibility-parity.test.ts@@ -36,6 +36,35 @@ const summaryPayload = (results: number, mentions: number) => ({const SUMMARY_CUR = summaryPayload(1800, 710);let SUMMARY_PREV = summaryPayload(1600, 500);+// insights_same_prompt_compare[_daily] (VIS-002): the prompts answered in both+// windows score lower than the full current window, which the new prompts lift.+const sameTotals = (answers: number, mentions: number, totalMentions: number) => ({+ ...visibilityPayload(answers, mentions, 50),+ by_competitor: [+ {+ competitor_id: 'c1',+ name: 'Peec AI',+ mention_answers: 60,+ citation_answers: 10,+ position_factor: 0.4,+ },+ ],+ total_mentions: totalMentions,+ total_citations: 50,+ positive_count: 20,+ mentioning_answers: mentions,+});+const SAME_PAYLOAD = {+ complete: true,+ same_prompts: 200,+ new_prompts: 509,+ dropped_prompts: 3,+ cur: sameTotals(800, 220, 300),+ prev: sameTotals(780, 200, 260),+};+let SAME: unknown = null;+let SAME_ERROR: { message: string } | null = null;+const groups = {groups: [{@@ -89,6 +118,10 @@ function rpc(name: string, args: Record<string, unknown>) {else if (name.startsWith('visibility_rate_trend')) data = [];else if (name.startsWith('competitor_aggregates')) data = competitorAgg;else if (name.startsWith('insights_aggregates')) data = isPrev(args) ? SUMMARY_PREV : SUMMARY_CUR;+ else if (name.startsWith('insights_same_prompt_compare')) {+ if (SAME_ERROR) return Promise.resolve({ data: null, error: SAME_ERROR });+ data = SAME;+ }return Promise.resolve({ data, error: null });}@@ -114,6 +147,8 @@ const {getCompetitorComparison,getInsightsSummary,} = await import('./tracking');+const { parseSamePromptComparison, withPromptComparison } =+ await import('@/lib/insights-same-prompts');const thirtyDays = {dateFrom: '2026-09-15T00:00:00.000Z',@@ -125,6 +160,7 @@ describe('Visibility parity (VIS-001)', () => {calls = [];PREV = visibilityPayload(1600, 400, 176);SUMMARY_PREV = summaryPayload(1600, 500);+ SAME = null;});it('shows the same number and change on the card, the panel and the leaderboard', async () => {@@ -214,3 +250,97 @@ describe('Visibility parity (VIS-001)', () => {expect(panel).toMatchObject({ curTotal: 710, prevTotal: null, delta: null, deltaPct: null });});});++describe('Same prompts parity (VIS-002)', () => {+ beforeEach(() => {+ calls = [];+ PREV = visibilityPayload(1600, 400, 176);+ SUMMARY_PREV = summaryPayload(1600, 500);+ SAME = SAME_PAYLOAD;+ SAME_ERROR = null;+ });++ /** What the page gets: the cards, re-measured on the shared prompts. */+ async function cards(samePrompts: boolean) {+ const [trend, summary, competitors] = await Promise.all([+ getVisibilityRateTrend('b1', thirtyDays),+ getInsightsSummary('b1', thirtyDays),+ getCompetitorComparison('b1', thirtyDays),+ ]);+ return withPromptComparison(+ { trend, summary, competitors },+ parseSamePromptComparison(SAME),+ samePrompts,+ );+ }++ it('shows the same change on the card, the panel and the leaderboard', async () => {+ const card = await cards(true);+ const panel = await getInsightsBreakdown('b1', 'visibility', thirtyDays);+ const mentions = await getInsightsBreakdown('b1', 'mentions', thirtyDays);+ const own = card.competitors.brands.find((e) => e.isOwnBrand)!;++ expect(panel.comparison).toEqual({+ basis: 'same',+ samePrompts: 200,+ newPrompts: 509,+ droppedPrompts: 3,+ });+ expect(panel.delta).not.toBeNull();+ expect(panel.delta).toBe(card.trend!.summary.change);+ expect(own.scoreChange).toBe(panel.delta);+ expect(mentions.deltaPct).toBe(card.summary.mentionsChange);+ expect(mentions).toMatchObject({ comparedCur: 300, prevTotal: 260, delta: 40 });+ // The numbers themselves still cover every prompt.+ expect(panel.curTotal).toBe(card.trend!.summary.rate);+ expect(mentions.curTotal).toBe(710);+ });++ it('compares all prompts when the switch is off, and still counts the new ones', async () => {+ const card = await cards(false);+ const panel = await getInsightsBreakdown('b1', 'visibility', {+ ...thirtyDays,+ samePrompts: false,+ });++ expect(panel.comparison?.basis).toBe('all');+ expect(panel.comparison?.newPrompts).toBe(509);+ expect(panel.delta).toBe(card.trend!.summary.change);+ expect(panel.comparedCur).toBe(panel.curTotal);+ });++ it('compares all prompts until the rollups are backfilled', async () => {+ SAME = { ...SAME_PAYLOAD, complete: false };+ const card = await cards(true);+ const panel = await getInsightsBreakdown('b1', 'visibility', thirtyDays);++ expect(card.comparison).toBeNull();+ expect(panel.comparison).toBeNull();+ expect(panel.delta).toBe(card.trend!.summary.change);+ });++ it('compares all prompts in the panel when the shared-prompt read fails', async () => {+ SAME_ERROR = { message: 'canceling statement due to statement timeout' };+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});+ const card = (await getVisibilityRateTrend('b1', thirtyDays)).summary;+ const panel = await getInsightsBreakdown('b1', 'visibility', thirtyDays);++ expect(panel.comparison).toBeNull();+ expect(panel.delta).toBe(card.change);+ expect(panel.comparedCur).toBe(panel.curTotal);+ expect(errorLog).toHaveBeenCalled();+ errorLog.mockRestore();+ });++ it('reads the shared prompts over the card\u2019s whole days', async () => {+ await getInsightsBreakdown('b1', 'visibility', thirtyDays);++ const call = calls.find((c) => c.name === 'insights_same_prompt_compare_daily')!;+ expect(call.args).toMatchObject({+ p_cur_from: '2026-09-15',+ p_cur_to: '2026-10-14',+ p_prev_from: '2026-08-16',+ p_prev_to: '2026-09-14',+ });+ });+});diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.tsindex c764e51de..079629ed8 100644--- a/web/src/lib/actions/tracking.ts+++ b/web/src/lib/actions/tracking.ts@@ -9,9 +9,17 @@ import {deltaDayWindows,visibilityChange,windowBounds,+ type ComparisonWindows,type DayWindow,type MetricWindow,} from '@/lib/insights-windows';+import {+ parseSamePromptComparison,+ samePromptChanges,+ withPromptComparison,+ type PromptComparison,+ type SamePromptComparison,+} from '@/lib/insights-same-prompts';import { computeAiVisibilityScore } from '@/lib/visibility-score';import type {PromptResult,@@ -673,6 +681,12 @@ export interface InsightsData {hasAnyData: boolean;/** The visibility-rate chart for the same window; null when its read failed. */trend: VisibilityRateTrendData | null;+ /**+ * Which prompts the changes on the page compare (VIS-002), with how many+ * were added or dropped between the periods. Null without a previous+ * window (All time) or before the rollups behind it are backfilled.+ */+ comparison: PromptComparison | null;/** The run ledger, when the 24h window was anchored to it (`anchor24h`). */trackingWindow: TrackingWindow | null;}@@ -759,7 +773,7 @@ export async function getTrackingWindow(brandId: string): Promise<TrackingWindow/** The part of the Insights page that depends only on the brand and the window. */type WindowedInsights = Pick<InsightsData,- 'summary' | 'competitors' | 'sov' | 'visibilityRate' | 'filterOptions' | 'trend'+ 'summary' | 'competitors' | 'sov' | 'visibilityRate' | 'filterOptions' | 'trend' | 'comparison'>;type InsightsFilterArgs = {@@ -769,6 +783,8 @@ type InsightsFilterArgs = {dateFrom?: string;dateTo?: string;days?: DayWindow;+ /** "Compare: Same prompts" (VIS-002); on unless explicitly false. */+ samePrompts?: boolean;};const EMPTY_COMPETITORS: CompetitorComparisonData = { brands: [], providerRows: [] };@@ -799,24 +815,76 @@ async function loadWindowedInsights(return fallback;});- const [summary, competitors, sov, visibilityRate, filterOptions, trend] = await Promise.all([- getInsightsSummary(brandId, filterOpts),- lenient(- getCompetitorComparison(brandId, filterOpts),- EMPTY_COMPETITORS,- 'competitor comparison',- ),- lenient(getShareOfVoiceData(brandId, filterOpts), EMPTY_SOV, 'share of voice'),- getVisibilityRateKpi(brandId, filterOpts),- getInsightsFilterOptions(brandId),- // The chart follows the same window as every other number on the page.- lenient<VisibilityRateTrendData | null>(- getVisibilityRateTrend(brandId, filterOpts),- null,- 'visibility trend',- ),- ]);- return { summary, competitors, sov, visibilityRate, filterOptions, trend };+ const [summary, competitors, sov, visibilityRate, filterOptions, trend, same] = await Promise.all(+ [+ getInsightsSummary(brandId, filterOpts),+ lenient(+ getCompetitorComparison(brandId, filterOpts),+ EMPTY_COMPETITORS,+ 'competitor comparison',+ ),+ lenient(getShareOfVoiceData(brandId, filterOpts), EMPTY_SOV, 'share of voice'),+ getVisibilityRateKpi(brandId, filterOpts),+ getInsightsFilterOptions(brandId),+ // The chart follows the same window as every other number on the page.+ lenient<VisibilityRateTrendData | null>(+ getVisibilityRateTrend(brandId, filterOpts),+ null,+ 'visibility trend',+ ),+ lenient(getSamePromptComparison(brandId, filterOpts), null, 'same-prompt comparison'),+ ],+ );+ const windowed = { summary, competitors, sov, visibilityRate, filterOptions, trend };+ return withPromptComparison(windowed, same, filterOpts.samePrompts !== false);+}++/**+ * The comparison windows' totals over the prompts answered in both+ * (VIS-002): the daily rollups for whole days, the raw rows for the 24h and+ * topic views. Null without a previous window, or while the rollups behind it+ * are not yet backfilled (00130) — the page then compares all prompts, as it+ * always did. A failed read throws, so strict mode keeps it out of the cache.+ */+async function getSamePromptComparison(+ brandId: string,+ opts?: InsightsFilterArgs,+ windows: ComparisonWindows = comparisonWindows(opts),+): Promise<SamePromptComparison | null> {+ if (!windows.prev) return null;+ const supabase = await dbClient();+ const base = {+ p_brand_id: brandId,+ p_models: modelFilterArray(opts?.model),+ p_region: opts?.region ?? undefined,+ };++ let res;+ if (windows.cur.kind === 'days') {+ // Explicit bounds for both windows: the same days the card compares.+ const { cur, prev } = deltaDayWindows(windows.cur.days);+ res = await supabase.rpc('insights_same_prompt_compare_daily', {+ ...base,+ p_cur_from: cur.dayFrom,+ p_cur_to: cur.dayTo,+ p_prev_from: prev.dayFrom,+ p_prev_to: prev.dayTo,+ });+ } else {+ const cur = windowBounds(windows.cur);+ const prev = windowBounds(windows.prev);+ res = await supabase.rpc('insights_same_prompt_compare', {+ ...base,+ p_cur_from: cur.from,+ p_cur_to: cur.to,+ p_prev_from: prev.from,+ p_prev_to: prev.to,+ p_topic_id: opts?.topicId ?? undefined,+ });+ }+ const { data, error } = res;+ if (error) throw new Error(error.message);+ return parseSamePromptComparison(data);}/**@@ -858,6 +926,7 @@ async function cachedAnchoredInsights(filterOpts.model ?? '',filterOpts.region ?? '',filterOpts.topicId ?? '',+ filterOpts.samePrompts === false ? 'all-prompts' : 'same-prompts',],{ revalidate: 2 * 24 * 60 * 60, tags: [`insights:${brandId}`] },);@@ -873,6 +942,8 @@ export async function getInsightsData(dateFrom?: string;dateTo?: string;days?: DayWindow;+ /** "Compare: Same prompts" (VIS-002); on unless explicitly false. */+ samePrompts?: boolean;/** When the current view is filtered, check unfiltered data existence too. */checkUnfiltered?: boolean;/**@@ -893,6 +964,7 @@ export async function getInsightsData(dateFrom: anchored?.dateFrom ?? opts.dateFrom,dateTo: anchored?.dateTo ?? opts.dateTo,days: opts.days,+ samePrompts: opts.samePrompts,};// An anchored window is served from the cache; a failure there (including a@@ -2481,7 +2553,12 @@ export interface VisibilityRateTrendSummary {rate: number;/** Rate over the equal-length window immediately before; null without data. */prevRate: number | null;- /** rate − prevRate in points, one decimal; null when prevRate is null. */+ /**+ * rate − prevRate in points, one decimal; null when prevRate is null. With+ * Same prompts on (VIS-002) the Insights page replaces it with the change+ * on the prompts answered in both windows, so it no longer equals+ * rate − prevRate there.+ */change: number | null;/*** Inclusive bounds of the previous window, for the "vs <range>" label:@@ -2913,35 +2990,61 @@ export async function getInsightsBreakdown(return agg && agg.total_results > 0 ? agg.total_mentions : null;};- const [curGroupsRes, prevGroupsRes, curScore, prevScore] = await Promise.all([+ const [curGroupsRes, prevGroupsRes, curScore, prevScore, same] = await Promise.all([groupsFor(windows.cur),windows.prev ? groupsFor(windows.prev) : Promise.resolve(null),totalFor(windows.cur),windows.prev ? totalFor(windows.prev) : Promise.resolve(null),+ // Without it the panel compares all prompts, as the cards then do.+ getSamePromptComparison(brandId, opts, windows).catch((err) => {+ console.error('[insights] same-prompt comparison failed', err);+ return null;+ }),]);if (curGroupsRes.error) throw new Error(curGroupsRes.error.message);if (prevGroupsRes?.error) throw new Error(prevGroupsRes.error.message);+ // "Same prompts" (VIS-002): the change and every row compare only the+ // prompts answered in both windows, exactly as the cards do.+ const useSame = opts?.samePrompts !== false && same !== null;+ const comparison: PromptComparison | null = same && {+ basis: useSame ? 'same' : 'all',+ samePrompts: same.samePrompts,+ newPrompts: same.newPrompts,+ droppedPrompts: same.droppedPrompts,+ };+const { byPrompt, byPlatform, byTopic } = buildBreakdownRows(metric,parseBreakdownGroups(curGroupsRes.data),prevGroupsRes ? parseBreakdownGroups(prevGroupsRes.data) : null,formatPlatformLabel,+ { samePrompts: useSame },);// Totals and their change, worked out the way each card does.let curTotal: number;+ let comparedCur: number;let prevTotal: number | null;let totalDelta: number | null;let totalDeltaPct: number | null;+ const changes = useSame && same ? samePromptChanges(same) : null;if (metric === 'visibility') {const v = visibilityChange(curScore, prevScore);curTotal = v.value;- prevTotal = v.prev;- totalDelta = v.change;+ comparedCur = changes ? (changes.visibility.cur ?? 0) : v.value;+ prevTotal = changes ? changes.visibility.prev : v.prev;+ totalDelta = changes ? changes.visibility.change : v.change;totalDeltaPct = null;+ } else if (changes) {+ curTotal = curScore ?? 0;+ comparedCur = changes.mentions.cur;+ prevTotal = changes.mentions.prev;+ totalDeltaPct = changes.mentionsChange;+ totalDelta = totalDeltaPct === null ? null : comparedCur - prevTotal;} else {curTotal = curScore ?? 0;+ comparedCur = curTotal;prevTotal = prevScore;({ delta: totalDelta, deltaPct: totalDeltaPct } =curScore === null@@ -2958,6 +3061,12 @@ export async function getInsightsBreakdown(let rootCause: string;if (!windows.prev) {rootCause = `${metricLabel} over all time; there is no earlier period of the same length to compare.`;+ } else if (useSame && same?.samePrompts === 0) {+ rootCause = `No prompt was answered in both ${windowDays}-day windows, so there is no like-for-like change yet.`;+ } else if (totalDelta === null && useSame) {+ // Shared prompts were answered in both windows, so the change is missing+ // only because the previous count was 0 (no percentage from 0).+ rootCause = `No ${metricLabel.toLowerCase()} on the shared prompts in the previous ${windowDays}-day window, so there is no percentage change.`;} else if (totalDelta === null) {rootCause = `No answers in one of the two ${windowDays}-day windows, so there is no change to explain yet.`;} else if (totalDelta === 0) {@@ -2978,7 +3087,9 @@ export async function getInsightsBreakdown(return {metric,curTotal,+ comparedCur,prevTotal,+ comparison,delta: totalDelta,deltaPct: totalDeltaPct,windowDays,diff --git a/web/src/lib/insights-breakdown.test.ts b/web/src/lib/insights-breakdown.test.tsindex 2be68d29c..86ea93a92 100644--- a/web/src/lib/insights-breakdown.test.ts+++ b/web/src/lib/insights-breakdown.test.ts@@ -171,3 +171,39 @@ describe('parseBreakdownGroups', () => {expect(parseBreakdownGroups(null)).toEqual({ groups: [], prompts: [] });});});++describe('buildBreakdownRows with Same prompts', () => {+ it('measures every row on the prompts answered in both windows', () => {+ const rows = buildBreakdownRows('visibility', cur, prev, label, { samePrompts: true });++ // p1 is the only prompt answered in both windows.+ expect(rows.byPrompt.map((r) => r.id)).toEqual(['p1']);+ const gemini = rows.byPlatform.find((r) => r.id === 'gemini')!;+ expect(gemini.cur).toBe(+ pooled(cur.groups.filter((g) => g.prompt_id === 'p1' && g.platform === 'gemini')),+ );+ });++ it('never reads a prompt added this period as a gain from 0', () => {+ const rows = buildBreakdownRows('visibility', cur, prev, label, { samePrompts: true });++ // p2 and p3 were only answered now: no row includes them.+ expect(rows.byPrompt.map((r) => r.id)).not.toContain('p3');+ expect(rows.byTopic.map((r) => r.id)).not.toContain('__uncategorized__');+ expect(+ [...rows.byPrompt, ...rows.byPlatform, ...rows.byTopic].map((r) => r.delta),+ ).not.toContain(pooled(cur.groups.filter((g) => g.prompt_id === 'p3')));+ });++ it('keeps the rows of earlier prompts unchanged when prompts are added', () => {+ const more: BreakdownGroupsPayload = {+ ...cur,+ groups: [...cur.groups, group('p9', 'gemini', 50, 50, 50)],+ };++ const before = buildBreakdownRows('visibility', cur, prev, label, { samePrompts: true });+ const after = buildBreakdownRows('visibility', more, prev, label, { samePrompts: true });+ expect(after.byPlatform).toEqual(before.byPlatform);+ expect(after.byPrompt).toEqual(before.byPrompt);+ });+});diff --git a/web/src/lib/insights-breakdown.ts b/web/src/lib/insights-breakdown.tsindex dd8ede7aa..581d74356 100644--- a/web/src/lib/insights-breakdown.ts+++ b/web/src/lib/insights-breakdown.ts@@ -1,6 +1,7 @@import { computeAiVisibilityScore } from '@/lib/visibility-score';import { percentageChange } from '@/lib/metrics';import type { DayWindow } from '@/lib/insights-windows';+import type { PromptComparison } from '@/lib/insights-same-prompts';/*** The Insights "View breakdown" panel (VIS-001): which prompts, platforms and@@ -20,6 +21,8 @@ export interface BreakdownFilters {dateFrom?: string;dateTo?: string;days?: DayWindow;+ /** "Compare: Same prompts" (VIS-002); on unless explicitly false. */+ samePrompts?: boolean;}export interface BreakdownRow {@@ -45,9 +48,17 @@ export interface BreakdownRow {export interface InsightsBreakdown {metric: BreakdownMetric;+ /** The card's number: every prompt in the current window. */curTotal: number;- /** Null when there is no previous window (All time). */+ /**+ * The values the change is measured between: every prompt in each window,+ * or with Same prompts only the prompts answered in both (VIS-002).+ * prevTotal is null when there is no previous window (All time).+ */+ comparedCur: number;prevTotal: number | null;+ /** Which prompts the change compares, with the counts; null when unknown. */+ comparison: PromptComparison | null;delta: number | null;deltaPct: number | null;/** Days in each comparison window (24h is 1); null for All time. */@@ -174,20 +185,42 @@ function sumBy(groups: BreakdownGroup[], keyOf: (g: BreakdownGroup) => string):const UNCATEGORIZED = '__uncategorized__';+/** Prompts with at least one answer in the window. */+const answeredPrompts = (p: BreakdownGroupsPayload) =>+ new Set(p.groups.filter((g) => g.answers > 0).map((g) => g.prompt_id));+/*** Per-prompt, per-platform and per-topic rows for one comparison. `prev` is* null when there is no previous window (All time): rows then carry no* previous value and no change. A slice missing from one window counts as 0* there. Rows come sorted by change, biggest drop first.+ *+ * With `samePrompts` (VIS-002), every row is measured on the prompts+ * answered in both windows, so a prompt added this period is never read as a+ * gain from 0.*/export function buildBreakdownRows(metric: BreakdownMetric,- cur: BreakdownGroupsPayload,- prev: BreakdownGroupsPayload | null,+ allCur: BreakdownGroupsPayload,+ allPrev: BreakdownGroupsPayload | null,platformLabel: (slug: string) => string,+ { samePrompts = false }: { samePrompts?: boolean } = {},): { byPrompt: BreakdownRow[]; byPlatform: BreakdownRow[]; byTopic: BreakdownRow[] } {+ let cur = allCur;+ let prev = allPrev;+ if (samePrompts && allPrev) {+ const before = answeredPrompts(allPrev);+ const shared = new Set([...answeredPrompts(allCur)].filter((id) => before.has(id)));+ const only = (p: BreakdownGroupsPayload, keep: (id: string) => boolean) => ({+ ...p,+ groups: p.groups.filter((g) => keep(g.prompt_id)),+ });+ cur = only(allCur, (id) => shared.has(id));+ prev = only(allPrev, (id) => shared.has(id));+ }+const prompts = new Map<string, BreakdownPrompt>();- for (const p of [...(prev?.prompts ?? []), ...cur.prompts]) prompts.set(p.id, p);+ for (const p of [...(allPrev?.prompts ?? []), ...allCur.prompts]) prompts.set(p.id, p);const topicOf = (g: BreakdownGroup) => prompts.get(g.prompt_id)?.topic_id ?? UNCATEGORIZED;const topicNames = new Map<string, string>();for (const p of prompts.values()) {diff --git a/web/src/lib/insights-same-prompts.test.ts b/web/src/lib/insights-same-prompts.test.tsnew file mode 100644index 000000000..552564c88--- /dev/null+++ b/web/src/lib/insights-same-prompts.test.ts@@ -0,0 +1,151 @@+import { describe, expect, it } from 'vitest';+import { computeAiVisibilityScore } from './visibility-score';+import {+ parseSamePromptComparison,+ samePromptChanges,+ withPromptComparison,+ type SamePromptComparison,+ type SamePromptTotals,+} from './insights-same-prompts';++const totals = (over: Partial<SamePromptTotals> = {}): SamePromptTotals => ({+ answers: 400,+ mention_answers: 100,+ citation_answers: 40,+ position_factor: 0.5,+ total_mentions: 150,+ total_citations: 60,+ positive_count: 30,+ mentioning_answers: 110,+ by_competitor: [],+ ...over,+});++const same = (over: Partial<SamePromptComparison> = {}): SamePromptComparison => ({+ samePrompts: 200,+ newPrompts: 509,+ droppedPrompts: 3,+ cur: totals({ mention_answers: 120, total_mentions: 180 }),+ prev: totals(),+ ...over,+});++describe('parseSamePromptComparison', () => {+ it('reads the RPC payload', () => {+ const parsed = parseSamePromptComparison({+ complete: true,+ same_prompts: 200,+ new_prompts: '509',+ dropped_prompts: 0,+ cur: { answers: 10, position_factor: null, by_competitor: [{ competitor_id: 'c1' }] },+ prev: { answers: 8 },+ });++ expect(parsed).toMatchObject({ samePrompts: 200, newPrompts: 509, droppedPrompts: 0 });+ expect(parsed!.cur).toMatchObject({ answers: 10, mention_answers: 0, position_factor: null });+ expect(parsed!.cur.by_competitor[0]).toMatchObject({ competitor_id: 'c1', mention_answers: 0 });+ });++ it('refuses a comparison built on rollups not yet backfilled', () => {+ expect(parseSamePromptComparison({ complete: false, same_prompts: 200 })).toBeNull();+ expect(parseSamePromptComparison(null)).toBeNull();+ });+});++describe('samePromptChanges', () => {+ it('measures Visibility on the shared prompts with the card formula', () => {+ const s = same();+ const score = (t: SamePromptTotals) =>+ computeAiVisibilityScore({+ answers: t.answers,+ mentionAnswers: t.mention_answers,+ citationAnswers: t.citation_answers,+ positionFactor: t.position_factor,+ })!;++ const changes = samePromptChanges(s);+ expect(changes.visibility.cur).toBe(score(s.cur));+ expect(changes.visibility.change).toBe(Math.round((score(s.cur) - score(s.prev)) * 10) / 10);+ expect(changes.mentionsChange).toBe(20);+ });++ it('scores competitors tracked in both windows, and leaves newly tracked ones without a change', () => {+ const named = (id: string, mentions: number) => ({+ competitor_id: id,+ name: id,+ mention_answers: mentions,+ citation_answers: 0,+ position_factor: mentions > 0 ? 1 : null,+ });+ const changes = samePromptChanges(+ same({+ cur: totals({ by_competitor: [named('c1', 40), named('c2', 0), named('new', 30)] }),+ prev: totals({ by_competitor: [named('c1', 0), named('c2', 0)] }),+ }),+ );++ expect(changes.competitorChange('c1')).toBeGreaterThan(0);+ // Tracked in both windows but never named: no change.+ expect(changes.competitorChange('c2')).toBe(0);+ // Added during the period: like a new prompt, no gain from 0.+ expect(changes.competitorChange('new')).toBeNull();+ });++ it('has no percentage changes without answers in both windows', () => {+ const changes = samePromptChanges(+ same({ samePrompts: 0, cur: totals({ answers: 0 }), prev: totals({ answers: 0 }) }),+ );++ expect(changes.visibility.change).toBeNull();+ expect(changes.mentionsChange).toBeNull();+ expect(changes.sentimentChange).toBeNull();+ });+});++describe('withPromptComparison', () => {+ const insights = () => ({+ summary: { totalResults: 709, mentionsChange: 203, citationsChange: 223, sentimentChange: 4 },+ trend: { summary: { change: 7.9 } },+ competitors: {+ brands: [+ { isOwnBrand: true, scoreChange: 7.9 },+ { isOwnBrand: false, competitorId: 'c1', scoreChange: 2 },+ ],+ },+ untouched: 'kept',+ });++ it('re-measures every change on the shared prompts and keeps the rest', () => {+ const s = same();+ const out = withPromptComparison(insights(), s, true);+ const changes = samePromptChanges(s);++ expect(out.comparison).toEqual({+ basis: 'same',+ samePrompts: 200,+ newPrompts: 509,+ droppedPrompts: 3,+ });+ expect(out.summary.mentionsChange).toBe(changes.mentionsChange);+ expect(out.trend!.summary.change).toBe(changes.visibility.change);+ // The leaderboard's own row moves with the card.+ expect(out.competitors.brands[0].scoreChange).toBe(changes.visibility.change);+ expect(out.competitors.brands[1].scoreChange).toBe(changes.competitorChange('c1'));+ expect(out.untouched).toBe('kept');+ });++ it('only reports the counts when Same prompts is off', () => {+ const out = withPromptComparison(insights(), same(), false);++ expect(out.comparison?.basis).toBe('all');+ expect(out.summary.mentionsChange).toBe(203);+ expect(out.trend!.summary.change).toBe(7.9);+ });++ it('changes nothing without a usable comparison', () => {+ const out = withPromptComparison(insights(), null, true);++ expect(out.comparison).toBeNull();+ expect(out.summary.mentionsChange).toBe(203);+ });+});diff --git a/web/src/lib/insights-same-prompts.ts b/web/src/lib/insights-same-prompts.tsnew file mode 100644index 000000000..bca0b7b3a--- /dev/null+++ b/web/src/lib/insights-same-prompts.ts@@ -0,0 +1,226 @@+import { computeAiVisibilityScore } from '@/lib/visibility-score';+import { percentageChange } from '@/lib/metrics';++/**+ * Insights "Compare: Same prompts" (VIS-002). Adding prompts used to inflate+ * every change on the page: 509 new prompts read as +203% mentions. With the+ * switch on, a change compares only the prompts answered in both periods+ * (insights_same_prompt_compare[_daily], 00130); the numbers themselves still+ * cover every prompt.+ */++export interface SamePromptCompetitor {+ competitor_id: string;+ name: string | null;+ mention_answers: number;+ citation_answers: number;+ position_factor: number | null;+}++/** One window's totals over the shared prompts (ai_visibility_aggregates shape plus summary fields). */+export interface SamePromptTotals {+ answers: number;+ mention_answers: number;+ citation_answers: number;+ position_factor: number | null;+ total_mentions: number;+ total_citations: number;+ positive_count: number;+ mentioning_answers: number;+ by_competitor: SamePromptCompetitor[];+}++export interface SamePromptComparison {+ /** Prompts answered in both windows: the ones every change is measured on. */+ samePrompts: number;+ /** Prompts answered only in the current window. */+ newPrompts: number;+ /** Prompts answered only in the previous window. */+ droppedPrompts: number;+ cur: SamePromptTotals;+ prev: SamePromptTotals;+}++/** What the page says about the prompts behind its changes. */+export interface PromptComparison {+ /** 'same': changes compare only prompts answered in both periods; 'all': every prompt. */+ basis: 'same' | 'all';+ samePrompts: number;+ newPrompts: number;+ droppedPrompts: number;+}++const num = (v: unknown): number => Number(v ?? 0);+const numOrNull = (v: unknown): number | null => (v === null || v === undefined ? null : Number(v));++function parseTotals(raw: unknown): SamePromptTotals {+ const t = (raw ?? {}) as Record<string, unknown>;+ return {+ answers: num(t.answers),+ mention_answers: num(t.mention_answers),+ citation_answers: num(t.citation_answers),+ position_factor: numOrNull(t.position_factor),+ total_mentions: num(t.total_mentions),+ total_citations: num(t.total_citations),+ positive_count: num(t.positive_count),+ mentioning_answers: num(t.mentioning_answers),+ by_competitor: ((t.by_competitor as Record<string, unknown>[] | undefined) ?? []).map((c) => ({+ competitor_id: String(c.competitor_id),+ name: (c.name as string | null) ?? null,+ mention_answers: num(c.mention_answers),+ citation_answers: num(c.citation_answers),+ position_factor: numOrNull(c.position_factor),+ })),+ };+}++/**+ * The RPC payload, or null when it can't be used: missing, or read from+ * rollup rows not yet rewritten since 00130 (`complete: false`). Callers then+ * compare all prompts, as before, rather than show a change built on gaps.+ */+export function parseSamePromptComparison(raw: unknown): SamePromptComparison | null {+ const r = raw as Record<string, unknown> | null;+ if (!r || r.complete !== true) return null;+ return {+ samePrompts: num(r.same_prompts),+ newPrompts: num(r.new_prompts),+ droppedPrompts: num(r.dropped_prompts),+ cur: parseTotals(r.cur),+ prev: parseTotals(r.prev),+ };+}++const round1 = (n: number) => Math.round(n * 10) / 10;++const brandScore = (t: SamePromptTotals) =>+ computeAiVisibilityScore({+ answers: t.answers,+ mentionAnswers: t.mention_answers,+ citationAnswers: t.citation_answers,+ positionFactor: t.position_factor,+ });++/**+ * A competitor's score over the brand's answers. The list holds every+ * competitor tracked in the window (named or not), so one missing from it+ * wasn't tracked yet and has no score, as on all prompts.+ */+function competitorScore(t: SamePromptTotals, id: string): number | null {+ const c = t.by_competitor.find((x) => x.competitor_id === id);+ if (!c) return null;+ return computeAiVisibilityScore({+ answers: t.answers,+ mentionAnswers: c.mention_answers,+ citationAnswers: c.citation_answers,+ positionFactor: c.position_factor,+ });+}++const sentimentPct = (t: SamePromptTotals) =>+ t.mentioning_answers > 0 ? Math.round((t.positive_count / t.mentioning_answers) * 100) : 0;++export interface SamePromptChanges {+ /** Visibility on the shared prompts in each window, and the change in points. */+ visibility: { cur: number | null; prev: number | null; change: number | null };+ /** A competitor's score change on the shared prompts, in points. */+ competitorChange: (competitorId: string) => number | null;+ mentions: { cur: number; prev: number };+ /** Percentage changes, with the cards' rule: null unless both windows have answers. */+ mentionsChange: number | null;+ citationsChange: number | null;+ /** Positive-sentiment share change in points. */+ sentimentChange: number | null;+}++/** Every change the Insights page shows, measured on the shared prompts. */+export function samePromptChanges(same: SamePromptComparison): SamePromptChanges {+ const { cur, prev } = same;+ const comparable = cur.answers > 0 && prev.answers > 0;+ const visCur = brandScore(cur);+ const visPrev = brandScore(prev);+ return {+ visibility: {+ cur: visCur,+ prev: visPrev,+ change: visCur === null || visPrev === null ? null : round1(visCur - visPrev),+ },+ competitorChange: (id) => {+ const c = competitorScore(cur, id);+ const p = competitorScore(prev, id);+ return c === null || p === null ? null : round1(c - p);+ },+ mentions: { cur: cur.total_mentions, prev: prev.total_mentions },+ mentionsChange: comparable ? percentageChange(cur.total_mentions, prev.total_mentions) : null,+ citationsChange: comparable+ ? percentageChange(cur.total_citations, prev.total_citations)+ : null,+ sentimentChange: comparable ? sentimentPct(cur) - sentimentPct(prev) : null,+ };+}++/** The parts of the Insights payload whose changes Same prompts re-measures. */+export interface ComparedInsights {+ summary: {+ totalResults: number;+ mentionsChange: number | null;+ citationsChange: number | null;+ sentimentChange: number | null;+ };+ trend: { summary: { change: number | null } } | null;+ competitors: {+ brands: { isOwnBrand: boolean; competitorId?: string; scoreChange: number | null }[];+ };+}++/**+ * Re-measure the page's changes on the prompts answered in both periods+ * when Same prompts is on. The numbers themselves still cover every prompt;+ * only the changes move: the Visibility change on the trend card, every+ * leaderboard row's change, and the Mentions, Citations and Sentiment+ * changes. Without a usable comparison the changes stay as they are,+ * measured on all prompts. With Same prompts off, the counts still come+ * back so the page can say how many new prompts the change includes.+ */+export function withPromptComparison<T extends ComparedInsights>(+ insights: T,+ same: SamePromptComparison | null,+ samePrompts: boolean,+): T & { comparison: PromptComparison | null } {+ if (!same) return { ...insights, comparison: null };+ const comparison: PromptComparison = {+ basis: samePrompts ? 'same' : 'all',+ samePrompts: same.samePrompts,+ newPrompts: same.newPrompts,+ droppedPrompts: same.droppedPrompts,+ };+ if (!samePrompts) return { ...insights, comparison };++ const changes = samePromptChanges(same);+ const { summary, trend, competitors } = insights;+ return {+ ...insights,+ comparison,+ summary:+ summary.totalResults > 0+ ? {+ ...summary,+ mentionsChange: changes.mentionsChange,+ citationsChange: changes.citationsChange,+ sentimentChange: changes.sentimentChange,+ }+ : summary,+ trend: trend && { ...trend, summary: { ...trend.summary, change: changes.visibility.change } },+ competitors: {+ ...competitors,+ brands: competitors.brands.map((entry) => ({+ ...entry,+ scoreChange: entry.isOwnBrand+ ? changes.visibility.change+ : entry.competitorId+ ? changes.competitorChange(entry.competitorId)+ : null,+ })),+ },+ };+}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex ab8418c2e..de36ed8fd 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3097,6 +3097,31 @@ export type Database = {regions: string[]}[]}+ insights_same_prompt_compare: {+ Args: {+ p_brand_id: string+ p_cur_from: string+ p_cur_to: string+ p_models?: string[]+ p_prev_from: string+ p_prev_to: string+ p_region?: string+ p_topic_id?: string+ }+ Returns: Json+ }+ insights_same_prompt_compare_daily: {+ Args: {+ p_brand_id: string+ p_cur_from: string+ p_cur_to: string+ p_models?: string[]+ p_prev_from: string+ p_prev_to: string+ p_region?: string+ }+ Returns: Json+ }normalize_page_path: { Args: { p_url: string }; Returns: string }import_prompts: {Args: {