Skip to content

feat(prompts): read each tracked prompt's real demand every night (PROMPT-014, part 1) - #1650

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompt-demand-card
Oct 28, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompt-demand-card

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PROMPT-014, part 1 of 3: read each tracked prompt's real demand every night. A prompt's AI search volume is an estimate. This reads signals that show whether people really ask it. The Demand card on the prompt's page (part 2) shows them, and the Priority Score (part 3) uses them.

prompt_demand (migration 00370)

  • One row per prompt with Search Console, Google Trends, People Also Ask and YouTube, each with the day it was read.
  • Also on the row: the level they add up to (high / medium / low), and how many of the four were read.
  • Members read their brands' rows; only the server writes them.

Search Console: every night, free

  • The brand's queries over 28 days (prompt_demand_gsc_queries).
  • Each query is matched to the closest prompt with the Action Center's rule (classify.js matchPrompt), then summed per prompt.

Trends, People Also Ask and YouTube: paid

  • DataForSEO Standard tasks, read for the prompt's top volume keyword with its location and language.
  • About once a week per prompt, at most 500 prompts a night.
  • Set at night, read an hour later or the next night (tasks_ready, task_get).
  • Off unless DEMAND_SIGNALS=true, documented in env-variables.mdx. About $0.004 per prompt a week.

Level (demandLevel): the share of points the read signals give. High is 60% with two signals or more, Medium 30%, Low below that.

Sweep safety (pre-push review fixes, a108b6a)

  • postTasks catches each 100-task request's failure on its own. The sweep writes every request's task ids as soon as they're in, so a later failure loses none that were paid for.
  • If a prompt was deleted in the meantime, the write is redone without it instead of dropping the whole chunk.
  • Done tasks whose ids were never written are read back by their tag (the prompt id).
  • The sweep claims prompts first (prompt_demand_claim, claimed_at) and sets only those. Two sweeps at once never pay twice for the same prompt. A claim older than two hours may be taken again.
  • Brand order: brands never written come first, then the longest since their last write (prompt_demand_brand_updates). Brands a sweep runs out of time for are counted (unfinished) and logged, and so is the due count.

Other

Where people see it: the prompt's page (part 2) and "Why this priority?" in the Action Center (part 3). This part has no UI.

Related issue

PROMPT-014 of the roadmap (Drive › ROADMAP-plan.md). No GitHub issue.

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. Apply 00370_prompt_demand.sql, or run bash supabase/build-schema.sh and compare schema.sql.
  2. cd server && npx vitest run src/lib/dataforseo.test.js src/lib/prompt-demand.test.js src/routes/prompts-demand.test.js
    • Covers a chunk failing halfway (the ids already in are written), a deleted prompt, two sweeps at once (the second sends nothing), stale and fresh claims, brand order and unfinished, and done tasks read back by tag.
  3. With DEMAND_SIGNALS unset, the nightly run only reads Search Console. No DataForSEO calls are made.
  4. Needs a key (maintainer): with DEMAND_SIGNALS=true and DataForSEO credentials, run the sweep for one brand. Check that its tasks are posted, then read an hour later.

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

Checks were run at the top of the stack (part 3, 44c2887):

  • server format, lint and npm test: 249 files, 3,404 tests passed;
  • web format, lint (0 errors; 7 warnings already on main), typecheck and vitest: 446 files, 4,362 tests passed;
  • web build: OK;
  • schema.sql matches build-schema.sh.

The new SQL was applied on the dev database.

Follow-ups before DEMAND_SIGNALS is turned on:

  • level calibration (People Also Ask ≤1 point, YouTube by median and recency, High needs Search Console or a strong Trends);
  • a Search Console tie-break, and skipping one-word and brand queries;
  • the tasks_ready 1,000 cap.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 28, 2026 04:19
…OMPT-014, part 1)

A prompt's AI search volume is an estimate. This reads what shows people
really ask it, for the Demand card on the prompt's page (part 2):

- prompt_demand (00370): per prompt, Search Console, Google Trends, People
  Also Ask and YouTube, each with the day it was read, the level they come
  to (high / medium / low) and how many of the four were read. Members
  read their brands' rows; the server writes them.
- Search Console, every night and free: the brand's queries over 28 days
  (prompt_demand_gsc_queries), each matched to the closest prompt with
  the Action Center's rule (classify.js matchPrompt), summed per prompt.
- Trends, PAA and YouTube: DataForSEO Standard tasks for the prompt's
  top volume keyword and its location and language, once a week, at most
  500 prompts a night. Set at night, read an hour later or the next night
  (tasks_ready, task_get). Off unless DEMAND_SIGNALS=true.
- The level is the share of points the read signals give (demandLevel):
  high at 60% with two signals or more, medium at 30%, low below.
- Workspaces whose plan has ended are skipped. Runs after the nightly
  Search Console sync.
- GET /api/prompts/:promptId/demand answers the card.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tasks (PROMPT-014, part 1)

Review fixes for the nightly demand sweep:

- postTasks catches each 100-task request's failure on its own and hands
  every request's ids to the caller as soon as they're in; the sweep
  writes them at once, so a later failure loses none that were paid for.
- A write that hits a prompt deleted meanwhile (foreign key) is written
  again without it, instead of dropping the whole chunk.
- Done tasks whose ids were never written are read back by their tag
  (the prompt's id) from tasks_ready.
- The sweep claims the prompts it is about to set (prompt_demand_claim,
  claimed_at) and sets only those, so two sweeps at once never set the
  same prompt twice; a claim older than two hours may be taken again.
- Brands go never-written first, then longest since their last write
  (prompt_demand_brand_updates); the ones a sweep runs out of time for
  are counted (unfinished) and logged. The due count is logged too.
- 00370: drop the unused prompt_demand_posted_idx, fix the header.
- DEMAND_SIGNALS documented in env-variables.mdx (paid, off by default).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mintlify

mintlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit fc73b73.

Approve preview

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with follow-ups.

Nothing blocks the merge. The paid reads are off unless DEMAND_SIGNALS=true, and items 1–3 should be fixed before anyone turns them on.

What I checked

  • Own diff. 3f6a4d4b..a108b6a0: prompt-demand.js and its tests, the task helpers in dataforseo.js, the route, the nightly hook in server.js, 00370 with schema.sql, and the env docs. The branch starts from an older main. On b591947 only schema.sql conflicts; rebuild it. server.js and routes/prompts.js auto-merge, and the merged files keep main's catalog purge and the /from-topics input schema.
  • Tests. The PR's server tests pass (53). On the merged stack, the whole server suite passes except two known timing tests. I took each guard out in turn, and a test failed every time: the DEMAND_SIGNALS gate, "not due while tasks wait", the 500-a-night cap, the claim, the retry without deleted prompts, the ended-plan skip in the sweep and in the collect, "read only what's listed until it expires", and the 20-minute deadline.
  • 00370.
    • RLS is on. Members of the brand's organization can select, using the same policy as content_shopping_checks.
    • anon has no grant. authenticated can only select.
    • The three functions are security invoker and run by the service role only. Execute is revoked from public, anon and authenticated.
    • The migration is idempotent. The partial index fits waitingRows, and gsc_query_stats (brand_id, date desc) serves the Search Console read.
    • bash supabase/build-schema.sh reproduces the committed schema.sql.
  • Cost.
    • Search Console is free: one RPC per brand per night.
    • The paid part is three DataForSEO Standard tasks per prompt, about once a week, and at most 500 prompts a night across all brands. That's up to 15 task_post requests and three tasks_ready calls, at night and again an hour later. task_get reads are free.
    • Workspaces whose plan has ended are skipped, both when tasks are set and when they're read (#1608's filters).
    • A failed request is logged and counts only its own 100 tasks. A paid task whose id was never written is found again by its tag.
  • Speed. trendsKeyword, questionKey and the other new regexes are linear: a few ms on 100 KB of -, spaces, punctuation or accents.

Follow-ups (before DEMAND_SIGNALS is turned on)

  1. A recovered task is bought again the same night. Take the case where none of a prompt's task ids were written, for example when the task_post request timed out after DataForSEO took it. Its row exists only from the claim, so it has no keyword and no posted_at. collectDemandTasks reads the tasks back by tag, but the row it writes (prompt-demand.js:691) still has neither. Then isDue sees a changed keyword (:419), so the same sweep sets the prompt's three tasks again. Write posted_at (the task's posted_at, or now) for rows found by tag, and treat a null keyword with a recent posted_at as not due. Alternatively, pass the keyword, location and language to prompt_demand_claim so the row has them before any task is set. Add a test for "found by tag, then not set again tonight".
  2. A failure while setting tasks skips the rest of the night. postDemandTasks throws when a write inside onChunk fails (anything but the foreign-key case). It isn't caught in sweepPromptDemand (:911), so that night's rescoring is skipped, and so is the late collect chained after it in server.js. Catch it, log it, and carry on.
  3. One brand can take a whole night. pickDue sorts all brands' prompts together, so a brand with 500 prompts that were never read takes the night's whole cap, and the others wait. Cap the prompts one brand can take in a night (e.g. a share of NIGHTLY_LIMIT), or go round the brands in turn.
  4. Matching blocks the API process. gscDemand (:176) compares every query with every prompt, synchronously. For 5,000 queries and 2,000 prompts, that's about 2 s with nothing else running in the process. Build a word → prompts index once, or yield between batches of queries.
  5. prompt_demand_claim trusts the caller's brand_id. It already joins prompts. Take the brand from prompt_sets there (00370 line 132), so a row can't be written under the wrong brand.
  6. The late collect has no deadline. It reads up to 1,500 tasks, 5 at a time, with a 60 s timeout each. Stop starting new reads after a few minutes; the next night picks up the rest.
  7. As the description says: calibrate the level, add a tie-break for Search Console matches, and handle the tasks_ready cap of 1,000.

Rollout

Apply 00370 before the API with this PR is deployed. Without the table, every brand's nightly read fails (each is logged), and the route answers 500.

@gkhngyk
gkhngyk merged commit 554a313 into ansvisor:main Oct 28, 2026
5 of 6 checks passed
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants