Skip to content

feat(content): cluster prompts by intent within each topic - #925

Merged
gkhngyk merged 1 commit into
mainfrom
feature/prompt-clusters
Oct 4, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/prompt-clusters

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Summary

First of three PRs for #857 (Phase 1 of the updated plan). It builds and maintains prompt clusters and their fan-out baskets. Opportunity generation does not read them yet; that is the next PR. Nothing changes in the UI.

Clusters

  • A scope is a topic, or a brand's prompts with no topic. Each scope is clustered with one model call into groups of prompts that one piece of content could answer.
  • Every prompt lands in exactly one cluster. A prompt the model leaves out gets a cluster of its own instead of disappearing.
  • A scope is re-clustered only when its active prompts change (ids or text), tracked by a fingerprint in prompt_cluster_scopes.
  • A rebuilt cluster keeps the old cluster's id when their prompts overlap by at least half (Jaccard). Opportunities linked to a cluster in the next PR survive a re-run.
  • replace_prompt_clusters swaps a scope's clusters in one statement, so readers never see a scope half-rebuilt.

Fan-out basket

  • brand_prompt_fanout_queries returns a brand's fan-out queries per prompt in one jsonb value: queries searched in at least two answers in the window. On the largest brand this is about 5,700 rows a month and takes about 750 ms.
  • Each cluster keeps its 100 most-searched queries. Each query is judged once per cluster as included or excluded, with a short reason, against the cluster's need and the brand. Later runs only refresh the count. Queries that leave the window are removed.

Running it

  • The tracking worker refreshes clusters right before generating opportunities, and only for plans with content optimization. A failed refresh is logged and generation still runs.
  • PROMPT_CLUSTER_MODEL selects the model and falls back to DEFAULT_SUGGESTION_MODEL.

Tried on production data

  • Dry run with openai/gpt-6-luna: a 9-prompt topic became 2 clusters, and an 11-prompt topic became 4.
    • The first version excluded competitor-product queries, so the judge now treats them as relevant: comparison content has to answer them.
    • The first version split the 11-prompt topic into 6 clusters, so the cluster prompt now prefers fewer clusters when one article covers them.
  • Real run on a 35-prompt brand: 7 topics became 14 clusters and 90 queries were judged, in 36 s. A second run made no model calls and took 1.5 s.

Known limitation: queries that differ only in word order count as separate queries. The next PR merges them where basket counts are shown.

Related issue

Part of #857

Type of change

  • feat — New feature
  • test — Adding or updating tests

How to test

  1. The migration is already applied to the hosted database.
  2. Run refreshPromptClusters('<brand id>') from server/src/lib/prompt-clusters.js for a small brand, then check prompt_clusters, prompt_cluster_members and prompt_cluster_queries.
  3. Run it again. The log should show reclustered: 0, judged: 0.
  4. Edit a prompt in one topic and run it again. Only that topic is re-clustered, and clusters that kept most of their prompts keep their ids.

prompt-clusters.test.js covers the fingerprint, cluster normalization, id reuse and basket ranking.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint and yarn format:check pass (run from server/)
  • yarn test passes (run from server/)
  • supabase/schema.sql rebuilt with build-schema.sh
  • Changes are focused — one concern per PR

🤖 Generated with Claude Code

Content opportunities are generated per prompt, so one need asked several
ways yields near-identical suggestions and most prompts get none (#857).
This adds the clusters they will be generated for. Generation does not use
them yet.

- Migration 00101 adds prompt_clusters, prompt_cluster_members (one cluster
  per prompt), prompt_cluster_scopes and prompt_cluster_queries, plus
  replace_prompt_clusters, which swaps a scope's clusters in one statement,
  and brand_prompt_fanout_queries.
- lib/prompt-clusters.js clusters each topic, and the prompts with no topic,
  with one model call. A scope is re-clustered only when its active prompts
  change, and a rebuilt cluster keeps its id when most of its prompts stay.
- Each cluster gets a fan-out basket: queries searched in at least two
  answers in the last 30 days, the 100 most searched per cluster, each
  judged once as included or excluded with a short reason.
- The tracking worker refreshes clusters before generating opportunities.
  A failed refresh does not block generation.
- PROMPT_CLUSTER_MODEL picks the model; it defaults to
  DEFAULT_SUGGESTION_MODEL.
@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 88d5115 into main Oct 4, 2026
6 checks passed
gkhngyk added a commit that referenced this pull request Oct 4, 2026
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.
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