Skip to content

feat(prompts): count plan quota by tracked location rather than prompt row - #781

Merged
gkhngyk merged 2 commits into
mainfrom
feature/691-location-quota
Aug 23, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/691-location-quota

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 1 of #691: the billable unit of maxPrompts becomes the tracked location (prompt × location), not the prompt row. A prompt tracked in N locations produces N times the scraper and model calls — the worker expands prompt × (models + scrapers) × regions — so it now counts N against the plan cap.

One counter, every surface. The location count is computed by a single aggregate RPC, org_prompt_location_usage (migration 00070, already applied to the hosted DB; security invoker, scoped by the existing org-membership RLS on prompts/prompt_sets/brands), wrapped once in web/src/lib/prompt-locations.ts and read by every place that shows or enforces the quota, so the number a user sees and the number that rejects a save cannot drift apart. Aggregating in Postgres avoids the un-paginated-select 1000-row cap (#427/#450/#464) that a row-fetching implementation would silently under-count against — caught in review — and keeps the parallel bulk-add guard checks from each downloading the org's whole roster:

  • savePromptSet — replace-style cap, counting locations on the org's other brands
  • addPromptToSet — covers the Add Prompt card, fan-out Track, and prompt suggestions (all funnel through it); addPromptToBrand delegates here
  • updatePrompt — gains a regions payload (targeting was not editable at all before) with a delta guard: growing a prompt from 1 to 3 locations costs exactly what adding two prompts would, and fails the same way
  • getPromptCapacity — the wizards' inline counter / disabled Continue
  • getTrackedPromptsKpi — the Insights KPI quota sub-line, relabeled "prompt locations"

Guard semantics (pinned by unit tests):

  • Only growth is metered — re-saving unchanged at the limit succeeds; removing locations frees capacity immediately, even for orgs already over the cap after a downgrade.
  • The cap is inclusive: landing exactly on maxPrompts is allowed.
  • The rejection message spells out the location math (used in place + added, how many to remove).
  • The -1 sentinel keeps self-host and enterprise unmetered — a test asserts an org at 10,000 used locations can still add 500.

Migration risk: measured, none. Queried production before writing code: no org has any multi-location prompt today (every prompt carries exactly one region), so this change alters no existing customer's usage or limit standing. No grandfathering needed.

Multi-state targeting (the US-CA shape, worker/scraper split, region editor UI) follows in part 2.

Related issue

Part 1 of #691 (do not close yet — multi-state targeting follows in a second PR).

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 — 128 tests pass, including the new prompt-locations.test.ts (sentinel, inclusive cap, delta semantics, message math).
  2. On a Starter org at 50 prompts (each single-location, as all are today): adding a 51st prompt from the Add Prompt card or fan-out Track is rejected with the location-math message; deleting one prompt immediately allows adding again.
  3. Onboarding / new-brand wizard: the review step's counter and disabled Continue behave exactly as before (wizard prompts are single-location, so one prompt still costs one).
  4. Insights → Tracked Prompts KPI: sub-line reads X / Y prompt locations and matches what enforcement counts.
  5. Self-host (IS_CLOUD unset): saves, edits and adds are never rejected; the wizard shows no cap.

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

…t row

A prompt tracked in N locations produces N times the scraper and model
calls (the worker expands prompt x (models + scrapers) x regions), but
plan limits counted prompt rows, so a multi-region prompt consumed
multiples of the scraper spend while counting as one. Part 1 of #691.

The tracked-location count now lives in one module
(web/src/lib/prompt-locations.ts) read by every surface: the save-time
caps in savePromptSet and addPromptToSet, the new delta guard in
updatePrompt (which also gains a regions payload so targeting becomes
editable), the wizards' capacity endpoint, and the Tracked Prompts KPI
sub-line — the number shown and the number that rejects a save cannot
drift apart.

Guards measure the delta, not the total: re-saving unchanged at the
limit succeeds, adding a location at the limit fails with a message
spelling out the location math, and removing locations frees capacity
immediately — including for orgs already over the cap after a plan
downgrade. The -1 sentinel keeps self-host and enterprise unmetered,
pinned by tests.

Measured before landing: no org currently has a multi-location prompt,
so this change alters no existing customer's usage or limit standing.
Review follow-ups on the location-quota change (#691 part 1):

- getOrgLocationUsage selected every prompt row's regions and summed in
  JavaScript. PostgREST silently caps an un-paginated select at 1000
  rows (the #427/#450/#464 trap), and a truncated sum under-counts, so
  the guard would wave an over-limit org through with no error anywhere
  — latent today (largest org: 552 rows) but part 2 multiplies rows
  into locations. The count now comes from a single aggregate RPC,
  org_prompt_location_usage (00070, applied to the hosted DB), which
  also stops the bulk-add path downloading the org's whole roster once
  per parallel guard check. Security invoker: prompts, prompt_sets and
  brands all carry org-membership select policies, so RLS scopes it.

- updatePrompt's category, platform/model and regions branches each
  fetched the prompt's row; one context fetch now serves all three.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 23, 2026
@gkhngyk
gkhngyk merged commit ea8f0a6 into main Aug 23, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the feature/691-location-quota branch August 23, 2026 21:18
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.

1 participant