Skip to content

feat(content): generate one opportunity per prompt cluster - #930

Merged
gkhngyk merged 1 commit into
mainfrom
feature/cluster-opportunities
Oct 4, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/cluster-opportunities

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Second of three PRs for #857. Content opportunities are now generated per prompt cluster (#925) instead of per prompt. The score now shows what it is made of.

Generation

  • One generator. The nightly run and the Generate button carried two copies of the same logic. Both now call generateContentOpportunities, and the button refreshes clusters first.
  • Which clusters. Each run takes the 3 highest-scoring clusters that have answers in the window and no opportunity yet. The model writes one opportunity per cluster, for content that answers all of its prompts. It sees the cluster's prompts, the searches AI engines ran for them and the metrics.
  • One opportunity per cluster. A cluster that already has an opportunity is not suggested again, whatever its status, so a dismissed idea does not come back in other words. Re-evaluating completed work is Phase 4 of the plan.
  • Merging across topics. The model also sees existing opportunities from other topics. When a cluster needs the same content as one of them, the cluster is merged into it:
    • its id is added to related_cluster_ids;
    • the score and source data are recomputed from all its clusters' prompts;
    • no near-copy is created.

Score

  • Weights are unchanged: demand 40, visibility gap 30, competitor gap 20, intent 10. The components are stored in source_data.scoreComponents.
  • Window. Metrics cover the last 30 days and come from the Insights rollups through brand_prompt_opportunity_metrics. Computing the same figures from raw answers took about 50 s on the largest brand and timed out; the rollups take about 0.6 s.
    • The previous code read raw answers with no window, so on large brands PostgREST's 1,000-row cap cut each read short.
  • Competitor gap is now the strongest competitor's visibility minus the brand's. The previous figure added up competitor scores over all answers, so it grew with the number of answers.
  • Demand is on a log scale. Cluster volumes are sums, and on the old linear scale with a 50k ceiling every cluster scored 100. Now 1,000 scores 50, 10,000 scores 67 and 150,000 scores 86, with a 1M ceiling.

Brief

  • The brief now lists the cluster's prompts as the questions the content must answer, and the cluster's fan-out queries as related searches.

Detail page

  • An Opportunity Basket card shows each cluster's label, topic, need and prompts, plus the included searches with their counts. Excluded searches and the reason for each are behind a toggle. The data is read live through GET /api/content/:id/basket.
  • A Why This Score card shows the four components.
  • A Strongest Competitor stat is added.
  • Per-prompt opportunities from before this change render as they did.

Compatibility

  • Migration 00102 adds cluster_id and related_cluster_ids to content_opportunities, plus the metrics function. It is already applied to the hosted database.
  • prompt_id is still set, to the cluster's highest-volume prompt, so the prompt filter, webhooks and MCP keep working. PR 3 moves the filters to clusters and maps the old per-prompt backlog.

Tried on production data

On a 35-prompt brand with 14 clusters, one run produced 3 opportunities in about 8 s.

  • The first run surfaced two problems: demand scored 100 for every cluster, and one description said "cluster".
  • Both were fixed: demand moved to the log scale, and the prompt now forbids internal terms. The 3 test rows were deleted and regenerated.
  • After the fix the demand components are 91.6, 86.3 and 86.4.

Related issue

Part of #857

Type of change

  • feat — New feature
  • refactor — The Generate button and the nightly run share one generator
  • test — Adding or updating tests

How to test

  1. Content → Generate on a brand that has clusters. Up to 3 opportunities appear, each for a group of prompts.
  2. Open one:
    • The Opportunity Basket lists the prompts and searches, and "Show N excluded searches" expands the excluded ones with reasons.
    • Why This Score shows four bars.
  3. Generate a brief. It should cover the cluster's prompts as questions.
  4. Press Generate again. Clusters that already have an opportunity are skipped.

opportunity-limits.test.js covers the cluster metrics, the score components and the log scale, and which clusters are covered and picked.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint, yarn typecheck, yarn format:check, yarn test (400) and yarn build pass (run from web/)
  • yarn lint, yarn format:check and yarn test (944) pass (run from server/)
  • supabase/schema.sql rebuilt with build-schema.sh

🤖 Generated with Claude Code

Opportunities were generated per prompt, so the same need asked several
ways piled up near-identical suggestions (#857). They are now generated per
prompt cluster (#925), with a score that explains itself.

- One generator for the nightly run and the Generate button, which carried
  two copies of the same logic. The button refreshes clusters first.
- Each run takes the three highest-scoring clusters that have answers and
  no opportunity yet, and writes one opportunity per cluster. A cluster
  that already has one, in any status, is not suggested again.
- A cluster that needs the same content as an opportunity in another topic
  is merged into it (related_cluster_ids) instead of getting a near-copy.
- Cluster metrics come from the Insights rollups over the last 30 days:
  the same figures from raw answers take about 50 s on the largest brand.
  The competitor gap is the strongest competitor's visibility minus the
  brand's; the old sum grew with the number of answers.
- Score components are stored with the opportunity. Demand moves to a log
  scale, since summed cluster volumes put most clusters at the old cap.
- The brief lists the cluster's prompts as the questions to answer and its
  fan-out queries as related searches.
- The detail page shows the basket (prompts, included and excluded
  searches with reasons) and the score breakdown.
- Migration 00102 adds cluster_id and related_cluster_ids to
  content_opportunities and the brand_prompt_opportunity_metrics function.
  prompt_id stays, set to the cluster's highest-volume prompt, so filters,
  webhooks and MCP keep working.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 4, 2026
@gkhngyk
gkhngyk merged commit 41382a8 into main Oct 4, 2026
6 checks passed
gkhngyk added a commit that referenced this pull request Oct 5, 2026
Cluster opportunities (#930) were listed among thousands of untouched
per-prompt ones, and the prompt filter only matched an opportunity's own
prompt_id (#857).

- Migration 00103 archives the per-prompt backlog: status 'new', no
  cluster, no brief. Nothing is deleted; Restore on the detail page sets one
  back to 'new'. The list, its summary and the MCP list hide archived rows
  unless the Archived status is chosen.
- The prompt filter matches the opportunities of the prompt's cluster, own
  or merged, and still matches older ones by prompt_id. Its option counts
  are computed the same way.
- A topic filter matches opportunities whose own or merged clusters are in
  the topic, with options from GET /brand/:id/topics.
- content_opportunity_aggregates takes the topic filter, matches prompts
  through clusters and returns the brand's signal count: the prompts and
  fan-out queries its clusters were built from. The first KPI card shows it
  as "from N signals analyzed".
- A run picks at most one cluster per topic, so its few suggestions spread
  across the brand's topics.
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.

1 participant