Repository navigation
feat(prompts): count plan quota by tracked location rather than prompt row - #781
Merged
Merged
Conversation
…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.
This was referenced Aug 24, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part 1 of #691: the billable unit of
maxPromptsbecomes 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 expandsprompt × (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(migration00070, already applied to the hosted DB; security invoker, scoped by the existing org-membership RLS on prompts/prompt_sets/brands), wrapped once inweb/src/lib/prompt-locations.tsand 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 brandsaddPromptToSet— covers the Add Prompt card, fan-out Track, and prompt suggestions (all funnel through it);addPromptToBranddelegates hereupdatePrompt— gains aregionspayload (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 waygetPromptCapacity— the wizards' inline counter / disabled ContinuegetTrackedPromptsKpi— the Insights KPI quota sub-line, relabeled "prompt locations"Guard semantics (pinned by unit tests):
maxPromptsis allowed.used in place + added, how many to remove).-1sentinel 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-CAshape, 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
cd web && yarn test— 128 tests pass, including the newprompt-locations.test.ts(sentinel, inclusive cap, delta semantics, message math).X / Y prompt locationsand matches what enforcement counts.IS_CLOUDunset): saves, edits and adds are never rejected; the wizard shows no cap.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)