Skip to content

feat(tracking): offer and run only the countries each engine answers from, read daily from the provider (PROMPT-015, part 5) - #1659

Merged
gkhngyk merged 46 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-cloro-countries
Oct 28, 2026
Merged

gkhngyk merged 46 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-cloro-countries

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PROMPT-015, part 5 of 6: offer and run only the countries each engine answers from, read daily from the provider.

Stacked on #1658. This PR's own change is its commits after part 4's.

The tracking provider supports a different set of countries for each engine, and asking an engine in a country it doesn't support fails. Its GET /v1/countries?model= is the only source of truth; its docs publish no list.

  • The daily run reads every engine's list before tracking (refreshCloroCountries) into cloro_countries (migration 00364, service role only).
    • A list that can't be read keeps the last one.
    • Without a provider key (self-hosted, local), nothing is read and nothing is restricted.
  • The tracking run skips an engine in a country it doesn't support, and logs how many pairs it skipped. No task fails and no credit is spent.
  • The location picker offers the countries the engines answer from, named with the runtime's own region names, with "not on " where one doesn't. Cities are offered only where Google's engines answer. Before the first read, the picker keeps its own list.

Pre-push review fixes (ce9c3a1):

  • The response is read in any of these shapes: a bare array, { countries: [...] }, and entries with code or country. Codes are normalised to two capital letters.
  • A response in any other shape is logged as a warning and keeps the last list.
  • A list under half the size of the last one is logged and not applied. So a cut-short answer can't stop tracking in most countries at once.
  • A test keeps the provider's engines the same on the web and the API (94bb06e).

@gkhngyk: the real lists need the provider key, as asked on #1625. Please run the first read, or paste one response, to confirm its shape. The dev database has a temporary sample list until then.

Where people see it: the location picker ("not on ChatGPT" next to a country), and the Locations view (part 6: "No engine here").

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00364. Without CLORO_API_KEY, the daily run logs that it skipped the read, and nothing is restricted.
  2. With the key: after the run, select model, count(*) from cloro_countries group by 1. The picker then shows "not on …" where an engine is missing.
  3. cd server && npx vitest run src/lib/cloro-countries.test.js

Related issue

PROMPT-015 (location matrix and custom columns), spec and mockup in Drive › Prompt. No GitHub issue.

Checklist

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

Full checks at the top of the stack (part 6, 29f649d):

  • server format, lint and npm test: 248 files, 3,364 tests passed;
  • web format, lint (0 errors; 7 warnings already on main), typecheck, vitest (453 files, 4,403 tests) and build: all pass;
  • schema.sql matches build-schema.sh.

A pre-push review found 9 issues; all are fixed in this stack (the fix(...) commits). Migrations 00363 and 00364 are applied on the dev database.

Follow-ups, not in this stack:

🤖 Generated with Claude Code

cihan-gyk and others added 30 commits October 28, 2026 03:46
…columns, and share them with the brand (PROMPT-015, part 1)

PROMPT-015a. A Columns panel beside the table's search:
- show or hide every column but Prompt, and reorder them by dragging or with
  the arrows;
- saved per person and brand (00363 prompt_table_views, RLS);
- an admin or manager can share their columns with everyone on the brand,
  who see them until they change their own;
- Reset goes back to the brand's view, or the default.

Nobody who never opens Columns sees a change: the default is today's
columns in today's order. The table builds its header and cells by column
id, unchanged inside.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Locations view (PROMPT-015, part 2)

PROMPT-015b. A Table | Locations switch above Tracked Prompts:
- Locations shows each prompt in each location the brand tracks, side by
  side: Visibility, Position, Mentions or Citations; an average row; a Gap
  column (best minus worst location); and three cards: the weakest and
  strongest location and the prompt that differs most between them.
- Cells use Brand Visibility's Topics vs competitors shades (HEAT_SHADES,
  moved beside heatShade). A cell opens the prompt's Positions panel on that
  location; a location the prompt isn't tracked in offers + Track (its Edit
  dialog for now).
- The Columns panel can add "<metric> · <location>" columns to the table.
- getPromptLocationMatrix reads each location with the table's own reads
  (visibility summaries, position sums), side by side, so a location's
  numbers equal the table filtered to it. No migration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… brand (PROMPT-015, part 1)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…aving, with more countries (PROMPT-015, part 3)

PROMPT-015c. Track in more locations, from the Locations view's
+ Add location or a cell's + Track:
- pick countries or US states (LocationPicker), and the prompts to ask
  there: the one clicked, the selected, a topic, or all shown;
- Plan usage shows used → after of the plan's prompt locations before
  anything is saved, with the same arithmetic the save enforces
  (planBulkLocationChange, bulkUpdatePromptLocations);
- past the limit the dialog says what the choice needs and what is left,
  with the plan's way out (Starter: upgrade or a custom plan; Growth: a
  custom plan; Enterprise: raise the limit), and Save stays off.

Prompts can be tracked in about 50 countries (TRACKING_COUNTRIES), not 18;
a brand's own region keeps the shorter list that volumes and audits read.
PromptLimitActions moves to its own file (a page can't export it).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n answer from one (PROMPT-015, part 4)

PROMPT-015d. A city is the location code `<country>~<CITY>` (`TR~ISTANBUL`),
51 major cities in both lists (server/src/lib/cities.js,
web/src/config/cities.ts; same ids):
- Google AI Overview and AI Mode ask from the city: the provider gets the
  city's Google canonical name as `location` beside the country.
- The API models (OpenAI, Claude) get `user_location` with the city and its
  region.
- ChatGPT, Perplexity, Copilot, Gemini and Grok can't target a city through
  the provider, so locationsForScraper skips city codes for them: no second
  country-wide run, no double cost. A prompt tracked only in cities runs
  nothing on them.
- A city counts one location on the plan, like a country (decision 6), on
  every plan.

Web: the location picker lists cities ("City · Turkey") with a note on which
engines ask there; the Locations view marks a city column "AI Mode · AIO ·
APIs"; Track in more locations turns a city off, and says why, when none of
the chosen prompts runs one of those engines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ound of reads (PROMPT-015, part 2)

The user's bar: data within 1–2 s of a choice. Measured locally (dev, hosted
dev DB) the matrix read took 1.4–1.8 s.
- getPromptLocationMatrix starts every read at once: the plan's engines no
  longer wait in front of the locations' reads.
- A matrix read stays a minute in the browser (loadLocationMatrix), so going
  Table ↔ Locations, or back to a location column, reads nothing again;
  switching the metric was already in the browser.
- The Locations button reads ahead on hover or focus, so the view opens on
  data already there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d-locations

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx
…from, read daily from the provider (PROMPT-015, part 5)

PROMPT-015e. The tracking provider supports a different set of countries
per engine, and asking an engine in a country it doesn't support fails;
its GET /v1/countries?model= is the only source of truth (its docs publish
no list). So:
- The daily run reads every engine's list before tracking
  (refreshCloroCountries) into cloro_countries (00364). A list that can't be
  read keeps the last one; an empty or malformed answer never empties it.
  No provider key (self-hosted, local): nothing read, nothing restricted.
- The tracking run skips an engine in a country it doesn't support
  (supportedLocations), and logs how many pairs it skipped: no failed task,
  no spent credit.
- The location picker offers the countries the engines answer from, named
  by the runtime's own region names, with "not on <engine>" where one
  doesn't; cities only where Google's engines answer. Before the first read
  it keeps its own list. Any country code is now valid on save.

Real lists need the provider key: gkhngyk was asked on ansvisor#1625 to paste them,
to check the first read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…their own view with the brand (PROMPT-015, part 1)

Someone looking at the brand's shared view without one of their own
changed it with every click: a member got "Only admins and managers can
share" errors, and a manager hiding one column changed it for everyone.
Changes now update the brand's view only after an admin or manager
shared their own, which sharing now also saves as theirs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…able (PROMPT-015, part 1)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ations-view

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/_columns-panel.test.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/_columns-panel.tsx
… and each location once a minute (PROMPT-015, part 2)

Up to twelve locations ran two heavy aggregates each, all at once, on
every open, every read-ahead on hover and every new location column.
The server now runs at most three of them at a time, the page sends one
read at a time, read-ahead included, and each location's figures are
kept for a minute on their own, so a new location column reads only its
location.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed, resumed, deleted or edited (PROMPT-015, part 2)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d-locations

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx
…o longer shown, not to every prompt (PROMPT-015, part 3)

Track in more locations opened from a Locations view cell fell back to
every prompt shown when the cell's prompt had left the rows. It now
targets none, says to choose the prompts, and keeps the save button off
until a scope is picked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y-tracking

# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/_add-locations-dialog.tsx
…ntry-wide under its name (PROMPT-015, part 4)

An unknown city code read as its country: Google's engines ran without a
location and the API models without a city, and both answers were saved
with the city's code. The worker now skips such a code for every engine
and logs it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…o, and ask Google from its cities as Turkiye (PROMPT-015, part 4)

The location lists, labels and docs say Türkiye. The picker finds it,
and its cities, typed as Turkey or Turkiye. The cities' Google location
names spell the country Turkiye, from one constant, to be confirmed
with a real request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…iye name (PROMPT-015, part 4)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e same on the web and the API (PROMPT-015, part 4)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ro-countries

# Conflicts:
#	server/src/workers/tracking-worker.js
#	supabase/schema.sql
#	web/src/components/dashboard/location-picker.tsx
#	web/src/lib/region.test.ts
…try lists (PROMPT-015, part 5)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ends, and keep the last list when a new one shrinks by more than half (PROMPT-015, part 5)

The daily read took only a bare array of codes. It now also reads
`{ countries: [...] }` and entries with a `code` or `country`, logs an
answer in any other shape and keeps the last list. A list under half the
last one is logged and not applied, so an answer cut short can't stop
tracking in most countries at once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mintlify

mintlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 2fdde6b.

Approve preview

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 28, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged main forward (8356ba3) through the stack; supabase/schema.sql rebuilt. Typecheck and lint pass on the chain tip.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with follow-ups

Reviewed this PR's own change (part 4's head 773461a → be050b1). Adds migration 00364_cloro_countries (new table; the number is free on main).

What I checked

  • Migration 00364. New table only, so no locks or rewrites on existing tables and no backfill. create table if not exists, drop policy if exists and the grants make it safe to re-run. RLS is on; anon has nothing; authenticated can only select (it's provider metadata, not org data, so a using (true) read is fine); only the service role writes. No functions, so no security definer or search_path concerns. The primary key (model, country) covers both the upsert and the per-model prune. schema.sql and types/supabase.ts match the migration.
  • The daily read (refreshCloroCountries). One request per provider model, all in parallel, each capped at 15 s, awaited once before the daily run. A failed read, an answer with no codes, or a list under half the last one keeps the last list. The prune only removes rows this read didn't stamp. Without a provider key nothing is read and nothing is restricted.
  • Cost. The run skips an engine in a country its list doesn't include, so those tasks are never submitted (and logged as a count). The API models aren't affected. With no lists, or an engine the provider doesn't list, nothing is skipped, so the worst case is today's behaviour. The lists are read once per tracking job (a couple of pages), not per prompt.
  • Plan limits. The new server action is read-only. Saving still goes through the existing location actions, which check the plan on the server. isValidLocation now accepts any ISO country the runtime knows (Node and browsers both use Intl.DisplayNames), so that is also what the server action accepts. A country no engine lists can be saved and counts on the plan, and only the API models run there. That's consistent with what the picker offers, and the cell says "No engine here" (part 6).
  • Deploy order. In either order nothing breaks: before 00364 is applied, the web's read fails and the picker keeps its own list, and the worker logs and skips nothing.
  • Tests on this tree: server cloro-countries, cities, locations, cloro-scraper, tracking-worker, catalog/* (225 passed); web region, tracking-countries, actions/tracking-countries, location-picker, _add-locations-dialog (25 passed). Lint and prettier clean on the touched files.

Merging on current main

Besides supabase/schema.sql, server/src/lib/catalog/__fixtures__/fake-db.js conflicts: main added a content_shopping_checks key, a not_is filter and a null-safe lt in the same spots. Keep main's lines, add this PR's cloro_countries key, and drop this PR's own lt (main's already covers it).

Follow-ups

  1. grok-web isn't in CLORO_COUNTRY_MODELS, so Grok is asked in every country. If the provider has a Grok country list, add it on both sides.
  2. Cities are offered only where Google's engines answer, but the API models also ask from a city. A prompt that runs only API models can't pick a city in a country Google doesn't cover. That may be intended; if so, the picker's city note could say it.
  3. The PR asks for a real response to confirm the shape. countryCodesIn already reads the likely shapes, and the warning log names the keys of any other shape.

@gkhngyk
gkhngyk merged commit 858f11d into ansvisor:main Oct 28, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants