Skip to content

fix(prompts): let volume analysis cover a whole brand instead of failing past 50 prompts - #747

Merged
gkhngyk merged 1 commit into
mainfrom
feature/volume-analysis-whole-brand
Aug 20, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/volume-analysis-whole-brand

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Volume analysis could not be run on a brand with more than 50 prompts, and the page did not say why.

The Prompts page sent every unanalyzed prompt inline in one request, and POST /api/volumes/analyze-batch refused more than 50 of them. A brand with 100 active prompts was therefore shown an "Analyze 100 prompts" button whose only possible outcome was 400, and the page turned the server's reason into Volume analysis failed — so the one component that knew what was wrong never said it. No run larger than 50 prompts exists anywhere in the usage history; the ceiling was never reachable.

Raising the cap alone would not have fixed it. Each prompt costs an LLM call plus a DataForSEO call and they run in order, which measures at a 6.4s median across 1,215 consecutive rows in production — roughly eleven minutes for 100 prompts, far past what a request can be held open for.

So the brand, not a list of prompts, is now the unit of work:

  • The route validates, claims the brand and returns 202 immediately; the run continues in the background. This mirrors the existing POST /api/internal/analyze-volumes used by the Stripe success flow.
  • A new GET /api/volumes/status/:brandId reports { running, total, analyzed }, and the page polls it to show real movement — Analyzing 32/100... — instead of an indefinite spinner.
  • Every prompt is saved on its own row as it lands, so a closed tab costs nothing already done. The page only claims the run "finished" when a status says so; giving up on the watch says so honestly instead.
  • A brand can only have one run in flight. The claim is taken before the first await, because a check followed by an await leaves a gap two simultaneous clicks both pass through — which would analyze every prompt twice and bill the quota twice.

Errors are now written for a person

Server text names batch sizes, upstream APIs and Postgres columns; none of it reaches the UI. The actions return a small closed set of codes (quota_exceeded, plan_limit, failed) instead of throwing, and the page chooses the wording.

This also fixes a second, quieter bug: the quota message never appeared. It was selected by matching on text that Next.js redacts in production, so an exhausted quota showed the same generic failure as everything else.

Two latent data bugs closed along the way

The prompt reads no longer assemble a list of ids to filter on, and no longer trust an un-paginated select — the two shapes that silently lost data in #714, #740 and #741. A brand is allowed up to 1000 prompts today, which sits exactly on the 1000-row cap. Scoping through the prompt_sets embed keeps the filter one id wide and pages the rest.

Inactive prompts are now skipped rather than spending an LLM call and a DataForSEO call on a row no surface reads.

Related issue

None — found while investigating a failing toast on the Prompts page.

Type of change

  • fix — Bug fix

How to test

Requires a brand with more than 50 active prompts and no volume data.

  1. Open Prompts → Volume Analysis. The amber banner offers Analyze N prompts.
  2. Click it. Expected: a toast saying the run has started, and the button counting Analyzing 0/N... upward roughly every 10s. Previously this returned Volume analysis failed immediately for any N above 50.
  3. Wait for completion. Expected: Volume analysis finished, and the table filled in.
  4. Click the button twice in quick succession. Expected: the second click reports the run is already going; it does not start a second pass.
  5. Switch to another brand mid-run and come back. Expected: the page stops narrating but the run continues; reopening shows the progress it reached.
  6. On a brand whose active prompts all have volumes, click Analyze. Expected: Every active prompt already has volume data.

The server changes require a deploy — the page polls an endpoint that does not exist on the current build.

Verification already done

  • 337 server tests pass (was 308). 12 new unit tests around analyzeBrandVolumes / getVolumeProgress, 17 new route-level tests driving the real router over HTTP.
  • The tests were mutation-checked, not just run: reverting the is_active filter, the pagination and the claim ordering each fails the test written for it.
  • The three PostgREST query shapes were run against the hosted database, read-only, on a brand with 3 active prompts (all analyzed) and 9 inactive (8 analyzed). getVolumeProgress returned { total: 3, analyzed: 3 } — an embed filter that did not work would have returned 12/11.
  • yarn lint, yarn typecheck, yarn format:check and yarn build pass from web/; yarn lint, yarn format:check and yarn test pass from server/.

Not covered: the browser polling loop itself, and no real analysis was triggered — that spends DataForSEO and LLM credit.

Out of scope

POST /api/volumes/refresh (the Refresh Volumes button) is still synchronous and has the same shape, though it is faster since it skips the LLM. Worth the same treatment separately.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • 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

…ing past 50 prompts

The Prompts page sent every unanalyzed prompt inline in one request, and the
route refused more than 50 of them. A brand with 100 active prompts was shown
an "Analyze 100 prompts" button that could only ever return 400, and the page
turned the reason into "Volume analysis failed" — so the one system that knew
what was wrong never said it.

Raising the cap alone would not have worked. Each prompt costs an LLM call plus
a DataForSEO call and they run in order, measured at a 6.4s median across 1,215
consecutive rows: about eleven minutes for 100 prompts, far past what a request
can be held open for.

So the brand, not a list of prompts, is now the unit of work. The route starts
the run and returns 202; the page follows a status endpoint and shows real
progress ("Analyzing 32/100..."). Each prompt is saved as it lands, so a closed
tab costs nothing already done. Quota still counts one run per click.

Along the way, the prompt reads no longer assemble a list of ids to filter on
and no longer trust an un-paginated select — the two shapes that lost data in
#714, #740 and #741, and that a 1000-prompt brand sits exactly on the edge of.
Inactive prompts are now skipped rather than spending an LLM call and a
DataForSEO call on a row no surface reads.

Failures reach the user as wording chosen for a person. The server's text names
batch sizes, upstream APIs and Postgres columns; none of it is shown. The
actions return a small set of codes instead of throwing, which also fixes the
quota message never appearing — it was matched on text that Next.js redacts in
production.
@gkhngyk
gkhngyk merged commit 3ca5282 into main Aug 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant