Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(content): stop opportunities piling up on the same prompts (#869)

Gökhan Geyik1b02632bf9d9
diff --git a/server/src/lib/opportunity-generator.js b/server/src/lib/opportunity-generator.js
index 81595391d..4f935591a 100644
--- a/server/src/lib/opportunity-generator.js
+++ b/server/src/lib/opportunity-generator.js
@@ -10,7 +10,13 @@ import { getLanguageName } from './languages.js';
import supabaseAdmin from '../config/supabase.js';
import { selectInChunks } from './chunked-in.js';
import { logger } from './logger.js';
-import { OPPORTUNITIES_PER_RUN, OPPORTUNITY_COUNT_RULE } from './opportunity-limits.js';
+import {
+ OPPORTUNITIES_PER_RUN,
+ OPPORTUNITY_COUNT_RULE,
+ belowOpenCap,
+ openCountsByPrompt,
+ relatedCandidate,
+} from './opportunity-limits.js';
const opportunitySchema = z.object({
opportunities: z
@@ -42,6 +48,7 @@ Rules:
- Focus on content that will improve the brand's visibility in AI-generated answers.
- Categorize as "owned" or "earned".
${OPPORTUNITY_COUNT_RULE}
+- Some prompts list opportunities "Already suggested" for them. Do not repeat or reword those: suggest a genuinely different piece of content for that prompt, or choose another prompt.
- The bracketed [N] indexes in the prompt data exist ONLY for the relatedPromptIndex field. NEVER mention an index like "[0]" or "Prompt 3" in titles or descriptions — refer to the prompt by quoting or paraphrasing its actual text/topic instead.`;
function computeScore(volume, visibility, competitorGap, intent) {
@@ -125,7 +132,31 @@ export async function generateContentOpportunities(brandId) {
const testedPrompts = prompts.filter((p) => (resMap[p.id] || []).length > 0);
if (!testedPrompts.length) return;
- const scored = testedPrompts
+ // Open opportunities, read before choosing candidates: prompts already at
+ // the cap are skipped, and the rest show the model what they already hold.
+ // Paged — a large brand holds more than PostgREST's 1,000-row default.
+ const existing = [];
+ for (let from = 0; ; from += 1000) {
+ const { data, error } = await supabaseAdmin
+ .from('content_opportunities')
+ .select('prompt_id, title')
+ .eq('brand_id', brandId)
+ .eq('status', 'new')
+ .order('id')
+ .range(from, from + 999);
+ if (error) throw new Error(error.message);
+ existing.push(...(data || []));
+ if (!data || data.length < 1000) break;
+ }
+ const openCounts = openCountsByPrompt(existing);
+ const openTitles = new Map();
+ for (const o of existing) {
+ if (!o.prompt_id || !o.title) continue;
+ if (!openTitles.has(o.prompt_id)) openTitles.set(o.prompt_id, []);
+ openTitles.get(o.prompt_id).push(o.title);
+ }
+
+ const ranked = testedPrompts
.map((p) => {
const vol = volMap[p.id];
const res = resMap[p.id] || [];
@@ -161,10 +192,26 @@ export async function generateContentOpportunities(brandId) {
score: computeScore(vol?.est_ai_volume || 0, avgVis, cg, vol?.intent || 'other'),
};
})
- .sort((a, b) => b.score - a.score)
- .slice(0, 25);
+ .sort((a, b) => b.score - a.score);
+ // Capped prompts drop out before the cut, so their slots go to the next
+ // prompts down instead of to another rewording of the same idea.
+ const scored = belowOpenCap(ranked, openCounts).slice(0, 25);
+
+ if (!scored.length) {
+ logger.info(
+ { brandId, openOpportunities: existing.length },
+ '[opportunities] every candidate prompt is at its open cap — nothing generated',
+ );
+ return;
+ }
- if (!scored.length) return;
+ const promptLine = (p, i) => {
+ const already = openTitles.get(p.promptId) || [];
+ const suffix = already.length
+ ? ` | Already suggested: ${already.map((t) => `"${t}"`).join('; ')}`
+ : '';
+ return `[${i}] "${p.text}" | Intent: ${p.intent} | AI Vol: ${p.estAiVolume}/mo | Vis: ${p.avgVisibility}% | Gap: ${p.competitorGap}%${suffix}`;
+ };
const userPrompt = `Brand: ${brand.name}
Industry: ${brand.industry || 'Not specified'}
@@ -172,7 +219,7 @@ Domain: ${(domains || []).map((d) => d.domain).join(', ') || 'N/A'}
Competitors: ${(compRes.data || []).map((c) => c.name).join(', ') || 'None'}
Prompt Data:
-${scored.map((p, i) => `[${i}] "${p.text}" | Intent: ${p.intent} | AI Vol: ${p.estAiVolume}/mo | Vis: ${p.avgVisibility}% | Gap: ${p.competitorGap}%`).join('\n')}
+${scored.map(promptLine).join('\n')}
Generate actionable content opportunities.
@@ -185,25 +232,19 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;
prompt: userPrompt,
});
- const { data: existing } = await supabaseAdmin
- .from('content_opportunities')
- .select('prompt_id, title')
- .eq('brand_id', brandId)
- .eq('status', 'new');
-
+ // Exact repeats are still caught here, as the last guard behind the cap and
+ // the already-suggested list.
const seen = new Set(
- (existing || []).map(
+ existing.map(
(opportunity) =>
`${opportunity.prompt_id}::${(opportunity.title || '').toLowerCase().trim()}`,
),
);
const rows = object.opportunities
- .map((opp) => {
- const rel = scored[opp.relatedPromptIndex] || scored[0];
- return { rel, opp };
- })
+ .map((opp) => ({ rel: relatedCandidate(scored, opp.relatedPromptIndex), opp }))
.filter(({ rel, opp }) => {
+ if (!rel) return false;
const key = `${rel.promptId}::${opp.title.toLowerCase().trim()}`;
if (seen.has(key)) return false;
seen.add(key);
@@ -231,7 +272,7 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;
if (rows.length > 0) await supabaseAdmin.from('content_opportunities').insert(rows);
logger.info(
- { brandId, generated: rows.length, alreadyPending: (existing || []).length },
+ { brandId, generated: rows.length, alreadyPending: existing.length },
'[opportunities] generated new opportunities',
);
}
diff --git a/server/src/lib/opportunity-limits.js b/server/src/lib/opportunity-limits.js
index e9bbb9a0c..af26a6aec 100644
--- a/server/src/lib/opportunity-limits.js
+++ b/server/src/lib/opportunity-limits.js
@@ -24,3 +24,37 @@ export const OPPORTUNITIES_PER_RUN = 3;
* ordered by score, which is what "provided" refers to.
*/
export const OPPORTUNITY_COUNT_RULE = `- Generate exactly ${OPPORTUNITIES_PER_RUN} opportunities: the ${OPPORTUNITIES_PER_RUN} highest-impact ones supported by the data provided, not the first ${OPPORTUNITIES_PER_RUN} that come to mind. Weigh volume, the visibility gap and the competitor gap together when choosing which ${OPPORTUNITIES_PER_RUN} to keep, and drop anything you would have ranked below them.`;
+
+/**
+ * Open (status 'new') opportunities a prompt may hold before the nightly run
+ * skips it (#837). Prompts are ranked by a deterministic score, so without a
+ * cap the same top prompts win every night and pile up rewordings of one idea
+ * — one prompt had collected 38 open opportunities over 32 nights.
+ */
+export const MAX_OPEN_PER_PROMPT = 3;
+
+/** Open opportunity count per prompt id, from rows carrying `prompt_id`. */
+export function openCountsByPrompt(rows) {
+ const counts = new Map();
+ for (const r of rows || []) {
+ if (r.prompt_id) counts.set(r.prompt_id, (counts.get(r.prompt_id) || 0) + 1);
+ }
+ return counts;
+}
+
+/** Candidates whose prompt still has room for another open opportunity. */
+export function belowOpenCap(candidates, counts) {
+ return candidates.filter((c) => (counts.get(c.promptId) || 0) < MAX_OPEN_PER_PROMPT);
+}
+
+/**
+ * The candidate an opportunity's relatedPromptIndex points at, or null when
+ * the model returned an index outside the list. Such an opportunity is
+ * dropped: falling back to the first candidate attached it to the prompt that
+ * was already the most crowded.
+ */
+export function relatedCandidate(candidates, index) {
+ return Number.isInteger(index) && index >= 0 && index < candidates.length
+ ? candidates[index]
+ : null;
+}
diff --git a/server/src/lib/opportunity-limits.test.js b/server/src/lib/opportunity-limits.test.js
index 65c03c73b..33fcec398 100644
--- a/server/src/lib/opportunity-limits.test.js
+++ b/server/src/lib/opportunity-limits.test.js
@@ -1,7 +1,14 @@
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
-import { OPPORTUNITIES_PER_RUN, OPPORTUNITY_COUNT_RULE } from './opportunity-limits.js';
+import {
+ MAX_OPEN_PER_PROMPT,
+ OPPORTUNITIES_PER_RUN,
+ OPPORTUNITY_COUNT_RULE,
+ belowOpenCap,
+ openCountsByPrompt,
+ relatedCandidate,
+} from './opportunity-limits.js';
/**
* The count rule is shared because it was duplicated (#730).
@@ -45,5 +52,53 @@ describe('opportunity count', () => {
expect(code).toContain('${OPPORTUNITY_COUNT_RULE}');
expect(code).not.toMatch(/Generate (between )?\d+/);
});
+
+ it(`the ${label} generator drops an out-of-range prompt index`, () => {
+ const code = source(path);
+ expect(code).toContain('relatedCandidate(');
+ // The old fallback attached the opportunity to the top candidate.
+ expect(code).not.toMatch(/relatedPromptIndex\]\s*\|\|/);
+ });
}
});
+
+describe('open-opportunity cap (#837)', () => {
+ it('counts open opportunities per prompt', () => {
+ const counts = openCountsByPrompt([
+ { prompt_id: 'a' },
+ { prompt_id: 'a' },
+ { prompt_id: 'b' },
+ { prompt_id: null },
+ ]);
+ expect(counts.get('a')).toBe(2);
+ expect(counts.get('b')).toBe(1);
+ expect(counts.size).toBe(2);
+ });
+
+ it('keeps prompts under the cap and drops prompts at it', () => {
+ const counts = new Map([
+ ['full', MAX_OPEN_PER_PROMPT],
+ ['partly', MAX_OPEN_PER_PROMPT - 1],
+ ]);
+ const kept = belowOpenCap(
+ [{ promptId: 'full' }, { promptId: 'partly' }, { promptId: 'fresh' }],
+ counts,
+ );
+ expect(kept.map((c) => c.promptId)).toEqual(['partly', 'fresh']);
+ });
+
+ it('resolves an in-range index and drops anything else', () => {
+ const candidates = [{ promptId: 'a' }, { promptId: 'b' }];
+ expect(relatedCandidate(candidates, 1)).toEqual({ promptId: 'b' });
+ expect(relatedCandidate(candidates, 2)).toBeNull();
+ expect(relatedCandidate(candidates, -1)).toBeNull();
+ expect(relatedCandidate(candidates, 0.5)).toBeNull();
+ expect(relatedCandidate(candidates, undefined)).toBeNull();
+ });
+
+ it('the nightly generator applies the cap and shows the model what is already open', () => {
+ const code = source('./opportunity-generator.js');
+ expect(code).toContain('belowOpenCap(');
+ expect(code).toContain('Already suggested');
+ });
+});
diff --git a/server/src/workers/content-worker.js b/server/src/workers/content-worker.js
index 96740652c..664df3e1c 100644
--- a/server/src/workers/content-worker.js
+++ b/server/src/workers/content-worker.js
@@ -10,7 +10,11 @@ import { getLanguageName } from '../lib/languages.js';
import supabaseAdmin from '../config/supabase.js';
import { selectInChunks } from '../lib/chunked-in.js';
import logger from '../lib/logger.js';
-import { OPPORTUNITIES_PER_RUN, OPPORTUNITY_COUNT_RULE } from '../lib/opportunity-limits.js';
+import {
+ OPPORTUNITIES_PER_RUN,
+ OPPORTUNITY_COUNT_RULE,
+ relatedCandidate,
+} from '../lib/opportunity-limits.js';
const opportunitySchema = z.object({
opportunities: z
@@ -251,8 +255,9 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;
.eq('brand_id', brandId)
.in('status', ['new']);
- const rows = object.opportunities.map((opp) => {
- const relatedPrompt = scoredPrompts[opp.relatedPromptIndex] || scoredPrompts[0];
+ const rows = object.opportunities.flatMap((opp) => {
+ const relatedPrompt = relatedCandidate(scoredPrompts, opp.relatedPromptIndex);
+ if (!relatedPrompt) return [];
const score = computeOpportunityScore(
relatedPrompt.estAiVolume,
relatedPrompt.avgVisibility,
@@ -260,28 +265,33 @@ IMPORTANT: Write every opportunity title and description in ${langName}.`;
relatedPrompt.intent,
);
- return {
- brand_id: brandId,
- prompt_id: relatedPrompt.promptId,
- title: opp.title,
- description: opp.description,
- type: opp.type,
- impact: opp.impact,
- opportunity_score: score,
- status: 'new',
- source_data: {
- promptText: relatedPrompt.text,
- estAiVolume: relatedPrompt.estAiVolume,
- visibilityScore: relatedPrompt.avgVisibility,
- competitorGap: relatedPrompt.competitorGap,
- intent: relatedPrompt.intent,
- keywords: relatedPrompt.keywords,
- competitorsCited: relatedPrompt.competitorsCited,
+ return [
+ {
+ brand_id: brandId,
+ prompt_id: relatedPrompt.promptId,
+ title: opp.title,
+ description: opp.description,
+ type: opp.type,
+ impact: opp.impact,
+ opportunity_score: score,
+ status: 'new',
+ source_data: {
+ promptText: relatedPrompt.text,
+ estAiVolume: relatedPrompt.estAiVolume,
+ visibilityScore: relatedPrompt.avgVisibility,
+ competitorGap: relatedPrompt.competitorGap,
+ intent: relatedPrompt.intent,
+ keywords: relatedPrompt.keywords,
+ competitorsCited: relatedPrompt.competitorsCited,
+ },
},
- };
+ ];
});
- const { error: insertErr } = await supabaseAdmin.from('content_opportunities').insert(rows);
+ const { error: insertErr } =
+ rows.length > 0
+ ? await supabaseAdmin.from('content_opportunities').insert(rows)
+ : { error: null };
if (insertErr) {
throw new Error(insertErr.message);