From b5b05fa7c380d6b3d1ce449e6e728ecb99499652 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 17:15:52 +0800 Subject: [PATCH 1/2] Measure every brand on the same answers for the Overall rank Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011zdoJUrqnkRrxhSVaRvd1X --- .../workflows/replacement-rerun-db-tests.yml | 9 ++- lib/metric-definitions.ts | 1 + .../20261204_restate_overall_rank.sql | 35 +++++++++ ...0261204220000_overall_rank_all_answers.sql | 74 +++++++++++++++++++ supabase/tests/overall_rank_behaviour.sql | 68 +++++++++++++++++ 5 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 supabase/maintenance/20261204_restate_overall_rank.sql create mode 100644 supabase/migrations/20261204220000_overall_rank_all_answers.sql create mode 100644 supabase/tests/overall_rank_behaviour.sql diff --git a/.github/workflows/replacement-rerun-db-tests.yml b/.github/workflows/replacement-rerun-db-tests.yml index 5fadb318..042ebe35 100644 --- a/.github/workflows/replacement-rerun-db-tests.yml +++ b/.github/workflows/replacement-rerun-db-tests.yml @@ -67,7 +67,8 @@ jobs: supabase/migrations/20261118100000_harden_metric_finalization_search_path.sql \ supabase/migrations/20261204120000_multilingual_brand_matching.sql \ supabase/migrations/20261204160000_recompute_keeps_history.sql \ - supabase/migrations/20261204200000_fewer_false_mentions.sql + supabase/migrations/20261204200000_fewer_false_mentions.sql \ + supabase/migrations/20261204220000_overall_rank_all_answers.sql do echo "Applying $migration" psql "$DB_URL" -v ON_ERROR_STOP=1 -f "$migration" @@ -103,6 +104,12 @@ jobs: -v ON_ERROR_STOP=1 \ -f supabase/tests/recompute_keeps_history_behaviour.sql + - name: Run Overall rank behavioural test + run: | + psql "postgresql://postgres:postgres@127.0.0.1:54322/postgres" \ + -v ON_ERROR_STOP=1 \ + -f supabase/tests/overall_rank_behaviour.sql + - name: Run synthetic candidate validation behavioural tests run: | psql "postgresql://postgres:postgres@127.0.0.1:54322/postgres" \ diff --git a/lib/metric-definitions.ts b/lib/metric-definitions.ts index ee26643c..e6cf18e5 100644 --- a/lib/metric-definitions.ts +++ b/lib/metric-definitions.ts @@ -28,6 +28,7 @@ export const METRIC_DEFINITIONS: Record 1 then + raise exception 'expected one rank step in %', target.fn; + end if; + execute replace(d, anchor, target.call || E'\n\n ' || anchor); + end loop; +end $patch$; diff --git a/supabase/tests/overall_rank_behaviour.sql b/supabase/tests/overall_rank_behaviour.sql new file mode 100644 index 00000000..113401b9 --- /dev/null +++ b/supabase/tests/overall_rank_behaviour.sql @@ -0,0 +1,68 @@ +-- Overall rank measures every brand on the same answers. +-- Runs only in the disposable local Supabase database in GitHub Actions. + +begin; +set local role postgres; +select set_config('request.jwt.claim.role', 'service_role', true); + +do $overall_rank$ +declare + v_project uuid; v_tracked uuid; v_beta uuid; v_gamma uuid; v_skin uuid; v_hair uuid; + v_today date := (now() at time zone 'Asia/Kuala_Lumpur')::date; + ts timestamptz; + v_row record; +begin + ts := (v_today::text || ' 12:00:00+08')::timestamptz; + insert into public.projects (brand_name, brand_terms, brand_domains, categories, tracking_market, tracking_locations) + values ('Synthetic Overall Brand', array['Acme Clinic'], array['acme.example'], array['Skin', 'Hair'], 'MY', array['MY']) + returning id into v_project; + insert into public.brand_registry (project_id, canonical_name, normalized_key, aliases, primary_website, is_tracked_brand, is_active) + values (v_project, 'Acme Clinic', 'acme-clinic', array['Acme'], 'acme.example', true, true) returning id into v_tracked; + insert into public.brand_registry (project_id, canonical_name, normalized_key, aliases, primary_website, is_tracked_brand, is_active) + values (v_project, 'Beta Clinic', 'beta-clinic', array['Beta'], 'beta.example', false, true) returning id into v_beta; + insert into public.brand_registry (project_id, canonical_name, normalized_key, aliases, primary_website, is_tracked_brand, is_active) + values (v_project, 'Gamma Clinic', 'gamma-clinic', array['Gamma'], 'gamma.example', false, true) returning id into v_gamma; + insert into public.category_brand_assignments (project_id, category, brand_id, role, aliases, websites, include_in_metrics) + values (v_project, 'Skin', v_beta, 'competitor', array['Beta'], array['beta.example'], true), + (v_project, 'Hair', v_gamma, 'competitor', array['Gamma'], array['gamma.example'], true); + + insert into public.tracking_runs (project_id, provider, status, total_prompts, completed_prompts, failed_prompts, started_at, completed_at, tracking_location, category, run_kind) + values (v_project, 'chatgpt', 'completed', 1, 1, 0, ts, ts, 'MY', 'Skin', 'category') returning id into v_skin; + insert into public.tracking_runs (project_id, provider, status, total_prompts, completed_prompts, failed_prompts, started_at, completed_at, tracking_location, category, run_kind) + values (v_project, 'chatgpt', 'completed', 3, 3, 0, ts, ts, 'MY', 'Hair', 'category') returning id into v_hair; + insert into public.ai_responses (project_id, llm_provider, captured_at, prompt_snapshot, raw_response, response_text, response_status, run_id, tracking_location) + values (v_project, 'chatgpt', ts, 'skin one', '{}'::jsonb, 'Beta Clinic is great.', 'completed', v_skin, 'MY'), + (v_project, 'chatgpt', ts, 'hair one', '{}'::jsonb, 'Acme Clinic and Gamma Clinic.', 'completed', v_hair, 'MY'), + (v_project, 'chatgpt', ts, 'hair two', '{}'::jsonb, 'Acme Clinic and Beta Clinic.', 'completed', v_hair, 'MY'), + (v_project, 'chatgpt', ts, 'hair three', '{}'::jsonb, 'Acme Clinic. See https://gamma.example/page', 'completed', v_hair, 'MY'); + insert into public.visibility_daily (project_id, metric_date, category, llm_provider, tracking_location, run_id, visibility_score, citation_count, presence_count, response_count, rank_population_status) + values (v_project, v_today, 'Skin', 'chatgpt', 'MY', v_skin, 0, 0, 0, 1, 'unverified'), + (v_project, v_today, 'Hair', 'chatgpt', 'MY', v_hair, 100, 0, 3, 3, 'unverified'); + update public.tracking_runs set published_at = ts where id in (v_skin, v_hair); + + perform public.recompute_competitor_metrics(v_project); + + select * into v_row from public.visibility_daily + where project_id = v_project and category = 'Overall' and llm_provider = 'chatgpt'; + if v_row.run_id is null then raise exception 'no Overall row was published'; end if; + if v_row.presence_count <> 3 or v_row.response_count <> 4 or v_row.visibility_rank <> 1 or v_row.rank_population_count <> 3 then + raise exception 'unexpected Overall tracked row: %', to_jsonb(v_row); + end if; + + -- Beta: its own category answer plus one from the other category. Gamma: a link alone does not count. + if not exists (select 1 from public.category_brand_observations where run_id = v_row.run_id and category = 'Overall' + and brand_id = v_beta and presence_count = 2 and response_count = 4) then + raise exception 'Beta Overall presence is not measured across all answers'; + end if; + if not exists (select 1 from public.category_brand_observations where run_id = v_row.run_id and category = 'Overall' + and brand_id = v_gamma and presence_count = 1 and response_count = 4) then + raise exception 'Gamma Overall presence is not measured across all answers'; + end if; + -- Category results are unchanged. + if not exists (select 1 from public.category_brand_observations where run_id = v_skin and brand_id = v_beta + and presence_count = 1 and response_count = 1) then + raise exception 'category observation changed'; + end if; +end $overall_rank$; + +rollback; From 6e1069dc27f5480869866730075ed44f7d996b5c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 17:19:28 +0800 Subject: [PATCH 2/2] Store the mentions the test run publishes Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011zdoJUrqnkRrxhSVaRvd1X --- supabase/tests/overall_rank_behaviour.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/supabase/tests/overall_rank_behaviour.sql b/supabase/tests/overall_rank_behaviour.sql index 113401b9..afe827f5 100644 --- a/supabase/tests/overall_rank_behaviour.sql +++ b/supabase/tests/overall_rank_behaviour.sql @@ -38,6 +38,9 @@ begin insert into public.visibility_daily (project_id, metric_date, category, llm_provider, tracking_location, run_id, visibility_score, citation_count, presence_count, response_count, rank_population_status) values (v_project, v_today, 'Skin', 'chatgpt', 'MY', v_skin, 0, 0, 0, 1, 'unverified'), (v_project, v_today, 'Hair', 'chatgpt', 'MY', v_hair, 100, 0, 3, 3, 'unverified'); + insert into public.response_brand_evidence (project_id, response_id, brand_id, category, match_source, matched_value, evidence_text, first_position, occurrence_count, confidence) + select v_project, a.id, v_tracked, 'Hair', 'response_text', 'Acme Clinic', 'Published mention', 0, 1, 'verified' + from public.ai_responses a where a.run_id = v_hair; update public.tracking_runs set published_at = ts where id in (v_skin, v_hair); perform public.recompute_competitor_metrics(v_project);