Skip to content

feat(prompts): target prompts at multiple countries and US states - #783

Merged
gkhngyk merged 1 commit into
mainfrom
feature/691-multi-location
Aug 24, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/691-multi-location

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Part 2 of #691, and the part that makes part 1 usable. Until now nothing in the product could put more than one location on a prompt: prompts inherited the brand's country at creation and could never be retargeted, so the location-based quota metered a dimension users could not reach.

Locations are now per-prompt. A location code is a country (DE) or a US state (US-CA), edited from the prompt card. One shared parse decides what a code means — mirrored deliberately in web/src/lib/region.ts and server/src/lib/locations.js, the same split as the citation hostname helpers — so the picker can never offer targeting the worker cannot run. Codes with no mechanism behind them (DE-BY, US-ZZ) are rejected rather than silently degraded to their country.

Google engines collapse instead of double-spending. Google AI Overview and AI Mode have no sub-country mechanism (they use location/uule, which Cloro's AI endpoints don't accept). A prompt targeting California and Texas therefore submits one country-wide Google task, not two identical ones. That was the expensive failure mode this could have shipped with: double scraper spend, plus two result rows differing only by a state label neither run honoured. The task counter reflects the collapse, so the progress bar doesn't promise work that is never submitted, and the picker says so on screen when the situation applies.

State targeting moves off the brand. brands.state was applied to every US task the brand ran, which would have silently overridden per-prompt targeting — two sources of truth for one question. It is now only the default location offered to new prompts; the worker no longer reads it. Migration 00071 folds the one brand currently using it into its own prompts' locations (US → US-DE), which is quota-neutral: a prompt tracking US-DE costs the same one location as one tracking US.

API models get state targeting too, via user_location.region (the providers take a region name, so CA → "California"). Country-only locations produce byte-identical payloads to today.

Locations get their own value-returning action (updatePromptLocations) rather than a field on updatePrompt: they are the only prompt edit a plan can refuse, and a refusal must reach the user as a value, not a masked digest (#427). The picker bounds itself to the org's remaining capacity, so the cap is visible before a save rather than after it, and the last location cannot be removed.

Also closes the labelling gap noted in #781's review: the plan cards now read "N prompt locations tracked".

⚠️ Migration ordering

00071_prompt_state_locations.sql must be applied after the server deploy, not before. The running worker passes a prompt's region straight through as Cloro's country field, so a row reading US-DE would be submitted as country "US-DE" and rejected — that brand's tracking would fail until the deploy landed. The new worker parses the code first, so once it is live the order stops mattering. It is deliberately not applied yet. (Dry-run verified against production: 50 rows, all ['US'] → ['US-DE'].)

Related issue

Closes #691.

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. cd web && yarn test (134 pass) and cd server && yarn test (396 pass) — new suites cover code parsing, the Google collapse, user_location shapes, and the Delaware/Germany DE collision.
  2. Prompts page → edit a prompt → Locations: search and add a country and a US state; both appear as chips with flags. The label counts "N of M available".
  3. At the plan cap, the list disables further picks and says why; removing a location re-enables them immediately. The last remaining location has no remove control.
  4. Add a state while the prompt runs Google AI Overview / AI Mode: the note about country-wide answering appears.
  5. Run tracking for a multi-location prompt: one result row per prompt × engine × location, with region stamped as the full code (US-CA); Google rows are stamped with the country.
  6. Self-host (IS_CLOUD unset): the picker shows "N tracked" with no cap and never disables.

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

Part 2 of #691, and the part that makes part 1 usable: until now nothing
in the product could put more than one location on a prompt. Prompts
inherited the brand's country at creation and could never be retargeted,
so the location-based quota metered a dimension users could not reach.

Locations are now a per-prompt list of codes — a country (DE) or a US
state (US-CA) — edited from the prompt card. One shared parse decides
what a code means, mirrored deliberately in web/src/lib/region.ts and
server/src/lib/locations.js, so the picker can never offer targeting the
worker cannot run.

Google AI Overview and AI Mode have no sub-country mechanism, so their
state locations collapse to one country-wide task instead of running the
identical country query once per state. That was the expensive failure
mode: double spend, plus two result rows differing only by a state label
neither run honoured. The picker says so on screen when it applies.

State targeting moves off the brand: brands.state was applied to every US
task the brand ran, which would have silently overridden per-prompt
targeting. It is now only the default location offered to new prompts,
and 00071 folds the one brand using it into its prompts' own locations —
quota-neutral, since a prompt tracking US-DE costs the same one location
as one tracking US. That migration must be applied AFTER deploying the
server; the running worker would submit "US-DE" as a country.

Locations get their own value-returning action rather than a field on
updatePrompt: they are the only prompt edit a plan can refuse, and a
refusal must reach the user as a value, not a masked digest (#427). The
picker bounds itself to the org's remaining capacity, so the cap is
visible before a save rather than after it.
@gkhngyk gkhngyk added prompts Prompt management, suggestions, query fan-out needs-migration Adds or edits a migration — apply it to the hosted database labels Aug 24, 2026
@gkhngyk
gkhngyk merged commit 79da93b into main Aug 24, 2026
5 checks passed
gkhngyk added a commit that referenced this pull request Aug 25, 2026
Locations became editable per prompt in #783, one card at a time. A brand
with 310 prompts needed 310 edits to start tracking Germany, which is
the operation multi-location targeting exists for.

The brand's prompt page gets a Locations dialog beside Analyze Prompts:
pick add or remove, pick the locations, pick the prompts. It adds and
removes rather than assigning a list, because a selection can hold
prompts targeting different places and assigning one list to all of them
would erase that targeting without saying so. Adding a location a prompt
already has costs nothing, and a removal that would leave a prompt with
no location at all skips it and says how many stayed — a prompt with no
location cannot be tracked, and refusing the whole batch over one
single-location prompt would be worse than reporting it.

Cost is on screen before the click: how many prompts change, how many
locations that adds, and where it lands against the plan. The dialog
prices the change with planBulkLocationChange and the server action
enforces it with the same function, so the preview and the guard cannot
disagree.

The cap is measured once for the batch and the write goes out as a
single statement (apply_prompt_locations, 00074, applied). Looping the
single-prompt action would check the cap per prompt and could stop
half-way, leaving a partly-retargeted selection and a bill for whatever
landed first. Current locations are re-read server-side: quota
arithmetic must not rest on numbers the client sent.
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 prompts Prompt management, suggestions, query fan-out

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompts: allow multiple US states per prompt, and count quota by tracked location rather than by prompt row

1 participant