Skip to content

fix(content): keep one open opportunity per question, merging new ones into it - #1923

Merged
gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:fix/opportunity-same-question
Nov 3, 2026
Merged

gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:fix/opportunity-same-question

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The same question could have several open opportunities, each with its own card and score. On dev, "What are the best coffee subscription services in 2026?" has two open opportunities.

  • The duplicate check (CONTENT-004) only caught generated clusters whose prompts and AI searches overlapped by 0.6 or more; these two overlap by 0.15–0.50.
  • "+ New" from a page, a prompt's Suggest content, and Action Center's Send to Content were never checked at all.

Now one open opportunity stands for each question. A new request for the same question is added to that opportunity instead of opening another one. Customer priorities plan, P0 item 2 (list hygiene).

What counts as "the same question" (opportunity-same-question.js, pure):

  • the same words, with years, case and punctuation set aside;
  • the same prompt;
  • a shared cluster.

Owner decision (2026-11-01): keep it this narrow. A cluster's other prompts don't count. Counting them would have merged four different opportunities on dev (shipping, offices, subscription) into one.

Which one is kept, and what joins it:

  • Kept: the one furthest along, then the one with work on it, then the higher score, then the older.
  • Joined: prompts, AI searches, keywords and cited competitors.
  • Signal and score: the stronger measured signal stays with its own score, so the score and its breakdown agree. The higher impact stays.
  • The merge is recorded on the opportunity (merged, the latest 20).

Where it applies:

  • The generator checks every path except Another angle before any model call or quota slot: nightly, + New, from a prompt, from a page. Owner decision: Another angle stays exempt, because the person asks for a second view on purpose.
  • Send to Content from an Action Center task about one prompt joins the open opportunity. History gets a line, and the toast says so (merged).
  • A done opportunity isn't reopened while another open one answers its question.

On screen:

  • the opportunity shows "N more requests for the same question…";
  • an archived duplicate links to the one it joined;
  • a task joining an opportunity gets its own toast.

One-off clean-up: server/src/scripts/merge-same-question-opportunities.js <brandId|--all> [--apply].

  • It's a dry run unless --apply. It keeps one per question and archives the rest with mergedInto. Drafts stay with their own opportunity.
  • On dev, the plan (computed from dev data, not applied) merges only the coffee-subscription pair and keeps the one with a draft.

Help: content-opportunities.md, section "No duplicates" (approved-articles.json refreshed).

Next: "Show an opportunity's volume as its question's" (owner decision 2026-11-01) is stacked on this PR. With it, the merged pair no longer shows the frozen 145,000/mo.

For @gkhngyk:

Related issue

None. Customer priorities plan, P0 item 2.

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. On a brand with an open opportunity for a question, generate again or use "+ New" from its page. No second card opens; the open one shows "1 more request for the same question".
  2. From an Action Center task about that prompt, use Send to Content: the toast says it joined the open opportunity, and History has a line.
  3. Another angle still opens a new one.
  4. Run the script without --apply and read its plan.
  5. Unit tests:
    • the question key and match;
    • which one is kept;
    • the merge, including score and breakdown;
    • each path: generator, page, prompt, task;
    • a done opportunity isn't reopened;
    • the script's plan;
    • action-task-send with merged false and true.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

Full checks on this branch (c62f9ce, from bf707cb; it merges cleanly with current main): web 797 files / 8,293 tests pass, and yarn build passes. Server format:check and lint are clean, and 353 files / 4,999 tests pass (3 skipped).

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 31, 2026 23:27
…s into it

The basket check (CONTENT-004) only caught clusters whose prompts and AI
searches overlapped an existing opportunity by 0.6 or more, and only for
generated ones. The dev database has two open opportunities for "What are
the best coffee subscription services in 2026?" (basket overlap 0.15-0.50),
and "+ New" with a page, a prompt's Suggest content and Action Center tasks
were never checked at all.

An opportunity now also stands for its question (source_data.promptText).
When a new one would answer the question of an open one - the same words
with years, case and punctuation aside, the same prompt, or a shared
cluster - it is added to that one instead of opened:

- opportunity-same-question.js (pure): the question key, the match, which
  open one is kept (furthest along, then with work, then score, then age)
  and the merge: prompts, AI searches, keywords and cited competitors join,
  the stronger measured signal and the higher impact stay, and the merge is
  recorded on it (`merged`, the latest 20).
- The generator checks every request but Another angle before any model
  call or quota slot; a prompt's or a page's request points at the open
  one. Re-opening a done opportunity is skipped while another open one
  answers its question.
- Send to Content from a task about one prompt joins the open opportunity
  on that question, records the task there once, and says so in the
  action's History; a task's own opportunity now carries its question.
- The opportunity page notes what was added ("N more requests for the same
  question") and, on an archived duplicate, links the one it went into.
- A one-off merge for the duplicates made before this:
  scripts/merge-same-question-opportunities.js <brandId|--all> [--apply],
  a dry run by default; it keeps one per question and archives the others
  pointing at it (mergedInto). On dev it would merge exactly the pair above.

Help: "No duplicates" in Find content opportunities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reakdown shows

A merge kept the higher of the two scores but only a measured signal's
breakdown, so an Action Center task with a higher priority raised the
opportunity's score while its Score breakdown still showed the old
components. The score now follows the signal that stays: the incoming
one's when it is higher and measured, or when neither is; the higher
impact is still kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ning an open opportunity

sendActionTask now says whether the task joined the open opportunity on its
question (`merged`), so the content send's expected result names it, and a
second case covers a task that joined one.

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

gkhngyk commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

One issue before merge: taskQuestion() in server/src/lib/action-center/task-send.js passes entity_key straight into the uuid filters (prompts.eq("id", …), prompt_cluster_members.eq("prompt_id", …)). A topic_opportunity action keys its prompts entity by the suggestion text, so Send to Content on one of its content tasks gets 22P02 and returns 500. Please return null (skip the prompt lookups) unless entity_key matches the uuid pattern — validate.js already has one. #1929 can follow once this lands.

cihan-gyk and others added 2 commits November 1, 2026 23:29
…eyed by text, not an id

A topic_opportunity action keys its prompts entity by the suggestion's text, so
taskQuestion() handed that text to the prompts.id and
prompt_cluster_members.prompt_id uuid filters and Send to Content failed with
22P02 (500). It now returns null unless the entity_key is a uuid (the pattern
validate.js already uses), and such a task opens its own opportunity without a
question, as a task about several prompts does.

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

Copy link
Copy Markdown
Collaborator Author

Thanks, good catch. Fixed in 9b8a44e (after merging main, 97a1c55): taskQuestion() now returns null unless entity_key matches the uuid pattern from validate.js (now exported), so the prompts / prompt_cluster_members lookups are skipped. A topic_opportunity content task keyed by suggestion text now sends to Content without a question and opens its own opportunity, the same as a task about several prompts.

The test mock now returns 22P02 when a uuid column gets text, as Postgres does. New tests cover the text-keyed topic_opportunity send (no 500, no question) and the uuid-keyed lookup, which works as before; with the fix reverted, the new test fails with 22P02.

Checks: format and lint are clean; the action-center and audits-opportunities tests pass (37 files / 791 tests), and the routes that load validate.js (action-center routes, opportunity same-question and reopen, impact ledger, demo) pass (27 files / 287 tests). Web is unchanged. #1929 can follow.

@gkhngyk

gkhngyk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

The uuid fix looks good. One more issue: mergeSameQuestion can merge a cluster into a target whose cluster_id is null (e.g. one made by Send to Content, which now sets prompt_id but no cluster_id). covered comes from loadClusterOpportunities, which skips rows with a null cluster_id, so the cluster is never seen as covered and gets merged into the same target every night — mergeQueries then adds its timesSearched again each time (N, 2N, 3N…). Setting cluster_id on a target that has none, counting related_cluster_ids of the basket rows as covered, or skipping a merge when merged[] already holds that clusterId would fix it.

cihan-gyk and others added 2 commits November 2, 2026 14:19
…its own only once

An opportunity made without a cluster (Send to Content, a task) never makes
the clusters merged into it covered, since loadClusterOpportunities reads only
rows with a cluster_id. The same cluster came back every night and was merged
into the same opportunity again, adding its times searched again (N, 2N, 3N).

A cluster an open opportunity already holds (its cluster_id or one of its
related_cluster_ids) now counts as covered by it and points at it, without
merging again. The test runs two nights in a row and checks that times
searched don't grow on the second.

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

Copy link
Copy Markdown
Collaborator Author

Thanks, good catch. In 15131c5, mergeSameQuestion first checks whether an open opportunity already holds the cluster (its cluster_id or related_cluster_ids, which every merge adds to); if so, the cluster counts as covered and points at that opportunity, with no second merge and no write. I left the target's cluster_id unset, so a Send to Content or task row isn't reopened or rewritten by the cluster paths that read loadClusterOpportunities. The new opportunity-same-question-nightly.test.js runs the merge two nights in a row against a target with a null cluster_id and checks that timesSearched and merged[] don't grow on the second night (it fails without the fix).

Separately, the one-off opportunity-same-question-merge.js would add the counts again if it were stopped halfway and run a second time. Happy to make it idempotent in a follow-up if you'd like.

@gkhngyk

gkhngyk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Two issues left, both in the one-off merge script (planSameQuestionMerges), which the PR asks to run with --apply on prod:

  • It groups an Another-angle opportunity with its original, since both share cluster_id/prompt_id, and archives one of them, although Another angle is meant to stay exempt. Please leave rows with source_data.alternativeOf out of grouping (and out of findSameQuestion/answersSameQuestion too).
  • mergeQueries adds up timesSearched when both rows carry the same cluster's basket, so the kept row gets 2N searches, written permanently. 15131c5 fixed this for the nightly path only; the script needs the same "a cluster already held counts once" guard.

…count a shared cluster's searches once

The one-off merge (planSameQuestionMerges) grouped an "Another angle"
opportunity with its original, since both carry the same cluster and
prompt, and archived one of them. An opportunity with
source_data.alternativeOf is now left out of grouping, and
answersSameQuestion (so findSameQuestion too) never matches one: it is
asked for on purpose beside its original.

mergeOpportunity added up times searched even when both rows held the
same cluster, so both baskets were that cluster's and the kept row got
2N searches, written for good by the script. When the incoming row
shares a cluster with the target, a query now counts once, at the higher
of the two, the same "a cluster already held counts once" rule the
nightly path follows.

@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.

LGTM

@gkhngyk
gkhngyk merged commit e1510f0 into ansvisor:main Nov 3, 2026
5 checks passed
@gkhngyk gkhngyk mentioned this pull request Nov 3, 2026
3 tasks done
gkhngyk added a commit that referenced this pull request Nov 3, 2026
The squash merge of #2005 (0b47e04) carried an old tree of the PR that
predated the main it was merged into, so it reverted about 17,000 lines
that #1923–#2004 had added: migrations 00470, 00472 and 00473, the
report card, brand integrations, take-down, the model menu and more.
#2008 and #2011 then merged on top.

This commit carries the tree of 9cc1acc (main before #2005, what
production runs) with #2005, #2008 and #2011 merged onto it again
against the versions they were built on. Compared with 9cc1acc it
deletes nothing, and every changed file is one of those three PRs' own
or a generated snapshot. Web tsc and the chat/help/mcp suites (2,844)
and the server suite (5,289) pass.
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
… elsewhere

- A done opportunity the same run reopened was judged by its old
  created_at, so its need could get an angle in that run: two open
  opportunities on one need and a slot spent. An open opportunity now has
  its chance from the later of created_at and its last reopening
  (source_data.reopened.at, and reopenStrengthened marks the run's rows).
- Angles skipped one open opportunity per question (ansvisor#1923): a need held by
  another open opportunity (a Send to Content or task one, through its
  related clusters) or whose question another open one answers no longer
  gets an angle.
- New's dialog rounds a need's visibility as the generator does, so 19.96%
  counts no angle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMenyNyxsr2Doq4CaQ3
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.

2 participants