Repository navigation
feat(prompts): keep per-brand usage counters for the tracked prompts cards - #1053
Merged
Merged
Conversation
Paused prompts don't run, so they no longer take room on the plan (org_prompt_location_usage now filters on is_active, 00128). Resuming a paused prompt brings its locations back, so it is metered like adding them: setPromptActive refuses with a plan_limit value when there is no room. That closes the pause, add new prompts, resume the old ones way past the limit. Location edits on paused prompts cost nothing until the prompt is resumed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sh safe Second review pass on the active-only prompt quota: - the edit dialog shows a plain message for non-plan failures instead of the raw database error; - setPromptActive returns a failed limit check as a value (ansvisor#427) instead of throwing; - the brand Prompts page keeps its last quota when the refresh after a toggle fails, so the location pickers don't become uncapped; - the prompt-usage doc no longer says paused prompts count; - tests for a missing prompt, a failed limit check, and location edits on paused vs active prompts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolves the conflicts with the bulk import (ansvisor#1013): prompt.ts keeps both setPromptActive and importPrompts, and the Prompts page imports both. The import RPC (00122) counts locations through org_prompt_location_usage, which 00128 limits to active prompts, so imports follow the same count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cards PROMPT-012 part 1: the data behind the usage cards, read without counting the prompts table on a page view. - Migration 00139 adds prompt_usage_counters, one row per brand (active, paused, active prompt locations). Statement-level triggers on prompts and prompt_sets rebuild the affected brands in the same transaction, under a per-brand advisory lock. prompt_usage_actual() is the one place the counts are computed; it backs the rebuild, the backfill and the nightly check. - prompt_usage_summary(brand) returns the brand's counts and its account's totals from the counter rows only (security invoker). - Indexes on prompts.prompt_set_id and prompt_sets.brand_id, which the rebuild needs and neither had. - GET /api/prompts/usage?brandId= returns the summary with the plan's location limit and call to action. - MCP tool get_prompt_usage and its REST mirror /api/mcp/prompt-usage. - The daily run checks the counters against prompts, rebuilds any that drifted and logs them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
This was referenced Oct 15, 2026
…e-counters # Conflicts: # supabase/schema.sql
7 of 10 tasks
…e-counters # Conflicts: # supabase/schema.sql # web/src/types/supabase.ts
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
…e-counters # Conflicts: # supabase/schema.sql
gkhngyk
approved these changes
Oct 16, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Thanks, approving. On the tree merged with main, schema.sql matches build-schema.sh, server tests pass (1249), web tests pass (789) and CI is green.
Checked against production:
- The counter formula equals
org_prompt_location_usagefor all 206 brands (7,326 locations). - A rebuild of the largest brand takes 3.9 ms, even before the two new indexes. Writes to
promptsare rare enough that the statement-level triggers cost little. - Neither index exists yet under another name, and the existing triggers on
promptsandprompt_setsdon't overlap.
gkhngyk
pushed a commit
that referenced
this pull request
Oct 16, 2026
* docs(skills): cover all MCP tools and the AI Visibility Score in the AEO coach skills Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(skills): teach the AEO coach skills about get_prompt_usage - List the get_prompt_usage MCP tool and its REST mirror GET /api/mcp/prompt-usage (#1053): the brand's active, paused and total prompts and the account's prompt locations against the plan limit, as on the Tracked Prompts usage cards. - Explain the unit: prompt locations, account-wide; paused prompts use no slots, and resuming one needs free slots like adding one. - In the prompt coverage audit, check the slots left before suggesting new prompts; a null limit means unlimited. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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
PROMPT-012 replaces the one-line "781 / 1000 prompt locations" meter above the Tracked Prompts table with usage cards:
This is part 1, the data. Part 2, the cards themselves, follows in its own PR.
The spec requires that a page view never counts the prompts table, with p95 < 100 ms and a payload under 1 KB. So the numbers come from counters kept on every write.
Counters (migration 00139)
prompt_usage_counters: one row per brand, holding active prompts, paused prompts and active prompt locations. Locations followorg_prompt_location_usagefrom feat(prompts): count only active prompt locations toward the plan limit #1034: an active prompt countsgreatest(array_length(regions), 1); a paused one counts none.promptsandprompt_setsrebuild the affected brands in the same transaction as the write. That covers a prompt added, paused, resumed, edited, moved or deleted, and a set deleted (its prompts cascading) or moved to another brand.prompt_usage_actual(): the one place the counts are computed fromprompts. The rebuild, the backfill and the nightly check all use it.prompt_usage_summary(brand): the brand's counts plus its account's active prompts, brands and locations, read from the counter rows only. It is security invoker, so a session only gets its own organization's brands. Database time on the demo brand is 0.6 ms, and the plan never touchesprompts.prompts.prompt_set_idandprompt_sets.brand_id. The rebuild looks prompts up through them, and neither had an index.CREATE TRIGGERs already hold off writes to these tables until the migration commits.Nightly check
runDailyTrackingnow callscheckPromptUsageCounters(), which runsrepair_prompt_usage_counters(). That function:prompt_usage_actual(), including a brand moved to another organization;[prompt-usage] counters drifted…).It runs non-blocking, like the pulse and signal catch-ups.
Endpoint
GET /api/prompts/usage?brandId=…returns the shape from the spec:{ "plan": { "key": "enterprise", "name": "Enterprise plan", "locationLimit": 1000, "cta": "contact" }, "brand": { "active": 702, "paused": 7, "total": 709 }, "account": { "activePrompts": 1248, "brands": 3, "locationsUsed": 781 }, "updatedAt": "2026-10-15T09:12:00.000Z" }planFromOrgRow, taken out ofgetOrgPlanso the MCP server can use it), Enterprise overrides included.locationLimitisnullwhen unlimited: self-hosted, or an Enterprise account whose limit isn't set yet.ctaisupgrade_growth(Starter),upgrade(Growth),contact(Enterprise), ornull(self-hosted).MCP
The new
get_prompt_usagetool, with its REST mirrorGET /api/mcp/prompt-usage?brand_id=, returns the same summary, so the Agent Chat can answer "how many prompt slots do we have left?".docs/guides/mcp-server.mdxlists it, and the tool count is now 30.Product decisions (from the spec's open questions)
locationLimit: null. The cards will say "Unlimited".Review
I ran a code review before opening this. It found 10 things; 6 are fixed:
Left as is:
import_prompts(00122) inserts row by row, so a bulk import rebuilds its brand once per row. For 500 rows into a 1,000-prompt brand, that is well under a second.getLocationQuota(the old count) on every view. Part 2 replaces that meter with these cards.Related issue
None. Product spec: PROMPT-012 (Tracked Prompts usage cards). Depends on #1034.
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
supabase/migrations/00139_prompt_usage_counters.sql. Thenselect count(*) from repair_prompt_usage_counters();should return 0./api/prompts/usage?brandId=<your brand>. The brand numbers match the prompts on its Tracked Prompts page;locationsUsedmatches the current meter.GET /api/mcp/prompt-usage?brand_id=<brand>, or ask the Agent Chat how many prompt slots are left.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): 0 errors; the 7 warnings are in code this PR doesn't touchyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
Web,
yarn test: 553/553. New:prompt-usage-summary.test.ts: plan names, limits and CTAs; the row shape; the RPC read;api/prompts/usage/route.test.ts: 200 against the org's plan, the Enterprise limit, self-hosted unlimited, 400 / 401 / 404, and 500 on a database error.Web,
yarn build: passed.Server,
npm run lintandnpm test: 1178/1178, including the newprompt-usage-counters.test.js.supabase/build-schema.sh:schema.sqlregenerated.On the dev database, rolled back: the migration plus these checks, each giving the expected counts:
The counters also equal
org_prompt_location_usage(74 = 74 on the demo brand). After that, I applied the migration to the dev database.In the browser: the route returns the demo brand's numbers (57 active, 4 paused, 74 locations), and 400 / 404 for a bad or foreign id. End to end it took ~200–300 ms locally, almost all of it round trips to the remote dev database; the RPC itself took 0.6 ms.
🤖 Generated with Claude Code