Repository navigation
feat(prompts): read each tracked prompt's real demand every night (PROMPT-014, part 1) - #1650
Merged
Merged
Conversation
…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]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
approved these changes
Oct 28, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
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.jsand its tests, the task helpers indataforseo.js, the route, the nightly hook inserver.js, 00370 withschema.sql, and the env docs. The branch starts from an older main. On b591947 onlyschema.sqlconflicts; rebuild it.server.jsandroutes/prompts.jsauto-merge, and the merged files keep main's catalog purge and the/from-topicsinput 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_SIGNALSgate, "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. anonhas no grant.authenticatedcan only select.- The three functions are security invoker and run by the service role only. Execute is revoked from
public,anonandauthenticated. - The migration is idempotent. The partial index fits
waitingRows, andgsc_query_stats (brand_id, date desc)serves the Search Console read. bash supabase/build-schema.shreproduces the committedschema.sql.
- RLS is on. Members of the brand's organization can select, using the same policy as
- 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_postrequests and threetasks_readycalls, at night and again an hour later.task_getreads 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,questionKeyand 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)
- 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_postrequest timed out after DataForSEO took it. Its row exists only from the claim, so it has nokeywordand noposted_at.collectDemandTasksreads the tasks back by tag, but the row it writes (prompt-demand.js:691) still has neither. ThenisDuesees a changed keyword (:419), so the same sweep sets the prompt's three tasks again. Writeposted_at(the task'sposted_at, or now) for rows found by tag, and treat a nullkeywordwith a recentposted_atas not due. Alternatively, pass the keyword, location and language toprompt_demand_claimso the row has them before any task is set. Add a test for "found by tag, then not set again tonight". - A failure while setting tasks skips the rest of the night.
postDemandTasksthrows when a write insideonChunkfails (anything but the foreign-key case). It isn't caught insweepPromptDemand(:911), so that night's rescoring is skipped, and so is the late collect chained after it inserver.js. Catch it, log it, and carry on. - One brand can take a whole night.
pickDuesorts 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 ofNIGHTLY_LIMIT), or go round the brands in turn. - 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. prompt_demand_claimtrusts the caller'sbrand_id. It already joinsprompts. Take the brand fromprompt_setsthere (00370 line 132), so a row can't be written under the wrong brand.- 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.
- As the description says: calibrate the level, add a tie-break for Search Console matches, and handle the
tasks_readycap 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.
Merged
7 of 12 tasks
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-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)Search Console: every night, free
prompt_demand_gsc_queries).classify.jsmatchPrompt), then summed per prompt.Trends, People Also Ask and YouTube: paid
tasks_ready,task_get).DEMAND_SIGNALS=true, documented inenv-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)
postTaskscatches 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.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.prompt_demand_brand_updates). Brands a sweep runs out of time for are counted (unfinished) and logged, and so is the due count.Other
GET /api/prompts/:promptId/demandanswers the card.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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
00370_prompt_demand.sql, or runbash supabase/build-schema.shand compareschema.sql.cd server && npx vitest run src/lib/dataforseo.test.js src/lib/prompt-demand.test.js src/routes/prompts-demand.test.jsunfinished, and done tasks read back by tag.DEMAND_SIGNALSunset, the nightly run only reads Search Console. No DataForSEO calls are made.DEMAND_SIGNALS=trueand DataForSEO credentials, run the sweep for one brand. Check that its tasks are posted, then read an hour later.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Checks were run at the top of the stack (part 3, 44c2887):
npm test: 249 files, 3,404 tests passed;vitest: 446 files, 4,362 tests passed;schema.sqlmatchesbuild-schema.sh.The new SQL was applied on the dev database.
Follow-ups before
DEMAND_SIGNALSis turned on:tasks_ready1,000 cap.🤖 Generated with Claude Code