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.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(prompts): show Improve the brand's earlier Improve rounds on the prompt (learning B5, part 2) (#1693)
Cihan53671d4b9ffb
diff --git a/docs/guides/prompts.mdx b/docs/guides/prompts.mdxindex 7aafe9787..68fbb4e21 100644--- a/docs/guides/prompts.mdx+++ b/docs/guides/prompts.mdx@@ -186,6 +186,8 @@ If a draft that isn't published yet already covers the prompt — one started fr**Optimize content** opens Start on the page of yours AI engines cite most for the prompt. When none is cited, Start lists your pages that answer the prompt so you can pick one. When a draft already covers the prompt, the step offers **Open draft**.+The tab's suggestions per platform are written from the answers' evidence and from this brand's last two Improve actions on the prompt for the same region: which of their suggestions were done or skipped, and how each platform moved since. They build on what moved, and don't suggest again what was done on a platform that didn't, or what your team skipped. An action sent less than a week ago is too early to tell either way. Only this brand's own actions are read.+## Best practices- Aim for **20–80 active prompts** per brand. Below 20 you miss coverage; above 80 noise drowns insights.diff --git a/server/src/lib/action-center/from-prompt-improve.js b/server/src/lib/action-center/from-prompt-improve.jsindex a44fa6c12..e97c3799d 100644--- a/server/src/lib/action-center/from-prompt-improve.js+++ b/server/src/lib/action-center/from-prompt-improve.js@@ -13,7 +13,12 @@import supabaseAdmin from '../../config/supabase.js';import { IMPROVE_PLATFORM_LABELS } from '../prompt-improve.js';-import { compareOutcome, outcomeWindow, readPlatformStates } from './improve-outcome.js';+import {+ OUTCOME_WINDOW_DAYS,+ compareOutcome,+ outcomeWindow,+ readPlatformStates,+} from './improve-outcome.js';import { priorityOnCreate } from './priority-data.js';export const IMPROVE_ACTION_KIND = 'prompt_improve';@@ -105,6 +110,114 @@ export function improveBaseline(snapshots, capturedAt, window) {return { capturedAt, window, platforms: snapshots };}+/** Earlier Improve rounds on a prompt the next generation is shown (learning B5). */+export const IMPROVE_PAST_ROUNDS = 2;++/** Suggestions of one earlier round the next generation is shown, in task order. */+export const IMPROVE_ROUND_TASKS = 12;++const DAY_MS = 86_400_000;++/** How the next generation reads a task's status: done, skipped by the team, or neither. */+function suggestionStatus(status) {+ if (status === 'completed') return 'done';+ if (status === 'skipped') return 'skipped';+ return 'not_done';+}++/**+ * One earlier Improve round on a prompt, as the next generation reads it+ * (learning B5): when it was sent and how many days before `now`, whether+ * it's still open, how each of its platforms moved since (final a month+ * after it was completed), and the suggestions it carried, each with whether+ * it was done or skipped.+ *+ * Until its read is OUTCOME_WINDOW_DAYS past the send, the read's days still+ * overlap the baseline's (on the day it's sent, every platform is+ * "unchanged"), so its platforms are `too_early` rather than a verdict.+ *+ * @param {{ created_at?: string, status: string, position_outcome?: object | null }} action+ * @param {Array<{ title?: string | null, status: string, details?: { platform?: string } | null }>} tasks+ * @param {Date} [now]+ */+export function improveRound(action, tasks, now = new Date()) {+ const outcome = action.position_outcome ?? {};+ const sentMs = typeof action.created_at === 'string' ? Date.parse(action.created_at) : NaN;+ const sent = Number.isFinite(sentMs);+ const readMs = Date.parse(outcome.measuredAt);+ const tooEarly =+ sent && (!Number.isFinite(readMs) || readMs - sentMs < OUTCOME_WINDOW_DAYS * DAY_MS);+ return {+ sent: sent ? action.created_at.slice(0, 10) : null,+ daysAgo: sent ? Math.max(0, Math.floor((now.getTime() - sentMs) / DAY_MS)) : null,+ open: action.status !== 'completed',+ final: outcome.final === true,+ platforms: (Array.isArray(outcome.platforms) ? outcome.platforms : [])+ .filter((p) => typeof p?.platform === 'string')+ .map((p) => ({+ id: p.platform,+ verdict: tooEarly ? 'too_early' : (p.verdict ?? 'not_enough_data'),+ })),+ suggestions: tasks+ .filter((task) => typeof task.title === 'string' && task.title.trim())+ .slice(0, IMPROVE_ROUND_TASKS)+ .map((task) => ({+ platform: task.details?.platform ?? null,+ // One line in the prompt, however the title was edited.+ title: task.title.replace(/\s+/g, ' ').trim(),+ status: suggestionStatus(task.status),+ })),+ };+}++/**+ * The brand's last Improve rounds on a prompt, newest first: sent and not+ * dismissed, nor deleted. The brand's own actions only, sent from a set of+ * the same region (an Improve set is kept per region): '' or null is all+ * regions, which is how an action sent without a region is measured.+ *+ * @returns {Promise<ReturnType<typeof improveRound>[]>}+ */+export async function loadImproveRounds({+ promptId,+ brandId,+ region = null,+ limit = IMPROVE_PAST_ROUNDS,+ now = new Date(),+}) {+ let q = supabaseAdmin+ .from('actions')+ .select('id, status, created_at, position_outcome')+ .eq('brand_id', brandId)+ .eq('kind', IMPROVE_ACTION_KIND)+ .eq('payload->>promptId', promptId);+ q = region ? q.eq('payload->>region', region) : q.is('payload->>region', null);+ const { data: actions, error } = await q+ .neq('status', 'dismissed')+ .is('deleted_at', null)+ .order('created_at', { ascending: false })+ .limit(limit);+ if (error) throw new Error(`earlier Improve rounds read failed: ${error.message}`);+ if (!actions?.length) return [];+ const { data: tasks, error: tasksError } = await supabaseAdmin+ .from('action_tasks')+ .select('action_id, position, title, status, details')+ .in(+ 'action_id',+ actions.map((action) => action.id),+ )+ .order('position');+ if (tasksError)+ throw new Error(`earlier Improve rounds' tasks read failed: ${tasksError.message}`);+ return actions.map((action) =>+ improveRound(+ action,+ (tasks ?? []).filter((task) => task.action_id === action.id),+ now,+ ),+ );+}+/** One task row per selected suggestion, in the order they were shown. */export function improveTaskRows(actionId, suggestions) {return suggestions.map((s, i) => ({diff --git a/server/src/lib/action-center/from-prompt-improve.test.js b/server/src/lib/action-center/from-prompt-improve.test.jsindex f172411d5..a5e8f6598 100644--- a/server/src/lib/action-center/from-prompt-improve.test.js+++ b/server/src/lib/action-center/from-prompt-improve.test.js@@ -1,11 +1,41 @@-import { describe, expect, it, vi } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';// The module imports the supabase admin client, whose config hard-exits when// SUPABASE_* env is absent (as in CI) — stub it before the import chain.-vi.mock('../../config/supabase.js', () => ({ default: {} }));+const { tables, errors, reads } = vi.hoisted(() => ({ tables: {}, errors: {}, reads: [] }));+vi.mock('../../config/supabase.js', () => ({+ default: {+ // A read of one table, its filters, order and limit recorded, its rows+ // as given, or its error.+ from: (table) => {+ const filters = [];+ reads.push({ table, filters });+ const chain = {+ select: () => chain,+ eq: (column, value) => (filters.push(['eq', column, value]), chain),+ neq: (column, value) => (filters.push(['neq', column, value]), chain),+ in: (column, value) => (filters.push(['in', column, value]), chain),+ is: (column, value) => (filters.push(['is', column, value]), chain),+ order: (column, options) => (filters.push(['order', column, options]), chain),+ limit: (count) => (filters.push(['limit', count]), chain),+ then: (resolve) =>+ resolve(+ errors[table]+ ? { data: null, error: errors[table] }+ : { data: tables[table] ?? [], error: null },+ ),+ };+ return chain;+ },+ },+}));const {+ IMPROVE_PAST_ROUNDS,+ IMPROVE_ROUND_TASKS,improveBaseline,+ improveRound,+ loadImproveRounds,improveGoal,improveImpact,improveKpiKeys,@@ -154,3 +184,215 @@ describe('improveKpiKeys', () => {expect(improveKpiKeys([])).toEqual(['ai_visibility', 'mentions']);});});++/**+ * Earlier Improve rounds on a prompt (learning B5): what the next+ * generation is told each one did, and how its platforms moved.+ */+describe('improveRound', () => {+ const now = new Date('2026-10-28T09:00:00Z');++ it('reads how its platforms moved and which suggestions were done', () => {+ expect(+ improveRound(+ {+ created_at: '2026-09-10T08:00:00Z',+ status: 'completed',+ position_outcome: {+ measuredAt: '2026-10-20T03:00:00Z',+ final: true,+ platforms: [+ { platform: 'chatgpt-web', verdict: 'improved' },+ { platform: 'gemini-web', verdict: 'unchanged' },+ ],+ },+ },+ [+ {+ title: 'Add a table to /pricing',+ status: 'completed',+ details: { platform: 'chatgpt-web' },+ },+ { title: 'Pitch seriouseats.com', status: 'todo', details: { platform: 'gemini-web' } },+ { title: 'Add an FAQ', status: 'skipped', details: { platform: 'gemini-web' } },+ { title: 'Run it again', status: 'failed', details: { platform: 'gemini-web' } },+ { title: ' ', status: 'todo', details: null },+ ],+ now,+ ),+ ).toEqual({+ sent: '2026-09-10',+ daysAgo: 48,+ open: false,+ final: true,+ platforms: [+ { id: 'chatgpt-web', verdict: 'improved' },+ { id: 'gemini-web', verdict: 'unchanged' },+ ],+ suggestions: [+ { platform: 'chatgpt-web', title: 'Add a table to /pricing', status: 'done' },+ { platform: 'gemini-web', title: 'Pitch seriouseats.com', status: 'not_done' },+ // Skipped is the team's call, told apart; a failed run is not done.+ { platform: 'gemini-web', title: 'Add an FAQ', status: 'skipped' },+ { platform: 'gemini-web', title: 'Run it again', status: 'not_done' },+ ],+ });+ });++ it('reads an open round with no read yet', () => {+ expect(improveRound({ status: 'in_progress', position_outcome: null }, [], now)).toEqual({+ sent: null,+ daysAgo: null,+ open: true,+ final: false,+ platforms: [],+ suggestions: [],+ });+ });++ /**+ * Its first read is the baseline itself, and for 7 days the read's days+ * overlap the baseline's: every platform reads "unchanged" whatever the+ * suggestions do, so the round is too early to read, not a failure.+ */+ it('reads a round whose read is still within 7 days of the send as too early', () => {+ const sentToday = {+ created_at: '2026-10-28T08:00:00Z',+ status: 'new',+ position_outcome: {+ measuredAt: '2026-10-28T08:00:00Z',+ final: false,+ platforms: [{ platform: 'gemini-web', verdict: 'unchanged' }],+ },+ };+ expect(improveRound(sentToday, [], now)).toMatchObject({+ sent: '2026-10-28',+ daysAgo: 0,+ platforms: [{ id: 'gemini-web', verdict: 'too_early' }],+ });+ const sixDaysIn = {+ ...sentToday,+ created_at: '2026-10-21T08:00:00Z',+ position_outcome: { ...sentToday.position_outcome, measuredAt: '2026-10-27T20:00:00Z' },+ };+ expect(improveRound(sixDaysIn, [], now)).toMatchObject({+ daysAgo: 7,+ platforms: [{ id: 'gemini-web', verdict: 'too_early' }],+ });+ // A full window after the send, the read is the platform's own.+ const weekIn = {+ ...sixDaysIn,+ position_outcome: { ...sixDaysIn.position_outcome, measuredAt: '2026-10-28T08:00:00Z' },+ };+ expect(improveRound(weekIn, [], now).platforms).toEqual([+ { id: 'gemini-web', verdict: 'unchanged' },+ ]);+ });+});++describe('improveRound suggestions', () => {+ it('keeps the first 12 suggestions, in task order, each title on one line', () => {+ const tasks = Array.from({ length: 15 }, (_, i) => ({+ title: i === 0 ? 'Add a table\n to /pricing\r\n' : `Task ${i + 1}`,+ status: 'todo',+ details: null,+ }));+ const { suggestions } = improveRound({ status: 'new' }, tasks);+ expect(IMPROVE_ROUND_TASKS).toBe(12);+ expect(suggestions).toHaveLength(12);+ expect(suggestions[0].title).toBe('Add a table to /pricing');+ expect(suggestions.at(-1).title).toBe('Task 12');+ });+});++describe('loadImproveRounds', () => {+ beforeEach(() => {+ reads.length = 0;+ for (const table of Object.keys(errors)) delete errors[table];+ });++ it("reads the brand's own last two rounds on the prompt, newest first, not dismissed, with their suggestions", async () => {+ // As the read returns them: newest first.+ tables.actions = [+ { id: 'a2', status: 'in_progress', created_at: '2026-10-01T08:00:00Z' },+ { id: 'a1', status: 'completed', created_at: '2026-09-10T08:00:00Z' },+ ];+ tables.action_tasks = [+ { action_id: 'a1', title: 'Add a table to /pricing', status: 'completed', details: {} },+ { action_id: 'a2', title: 'Pitch seriouseats.com', status: 'todo', details: {} },+ { action_id: 'other', title: 'Not this round', status: 'completed', details: {} },+ ];++ const rounds = await loadImproveRounds({ promptId: 'p1', brandId: 'b1' });++ expect(IMPROVE_PAST_ROUNDS).toBe(2);+ expect(reads[0]).toEqual({+ table: 'actions',+ filters: [+ ['eq', 'brand_id', 'b1'],+ ['eq', 'kind', 'prompt_improve'],+ ['eq', 'payload->>promptId', 'p1'],+ ['is', 'payload->>region', null],+ ['neq', 'status', 'dismissed'],+ ['is', 'deleted_at', null],+ ['order', 'created_at', { ascending: false }],+ ['limit', 2],+ ],+ });+ // The tasks of those rounds only, in their order on the action.+ expect(reads[1]).toEqual({+ table: 'action_tasks',+ filters: [+ ['in', 'action_id', ['a2', 'a1']],+ ['order', 'position', undefined],+ ],+ });+ expect(rounds.map((round) => [round.sent, round.suggestions.map((s) => s.title)])).toEqual([+ ['2026-10-01', ['Pitch seriouseats.com']],+ ['2026-09-10', ['Add a table to /pricing']],+ ]);+ });++ it('fails with what could not be read, for the route to write without it', async () => {+ errors.actions = { message: 'timeout' };+ await expect(loadImproveRounds({ promptId: 'p1', brandId: 'b1' })).rejects.toThrow(+ 'earlier Improve rounds read failed: timeout',+ );++ delete errors.actions;+ tables.actions = [{ id: 'a1', status: 'completed', created_at: '2026-09-10T08:00:00Z' }];+ errors.action_tasks = { message: 'permission denied' };+ await expect(loadImproveRounds({ promptId: 'p1', brandId: 'b1' })).rejects.toThrow(+ "earlier Improve rounds' tasks read failed: permission denied",+ );+ });++ /**+ * An Improve set is kept per region, and the action keeps the region of+ * the set it was sent from: a US set reads the US rounds, the all-regions+ * set the rounds sent without one ('' and null alike).+ */+ it("reads only the rounds sent for the set's region", async () => {+ tables.actions = [];+ reads.length = 0;+ await loadImproveRounds({ promptId: 'p1', brandId: 'b1', region: 'US' });+ expect(reads[0].filters).toContainEqual(['eq', 'payload->>region', 'US']);+ expect(reads[0].filters).not.toContainEqual(['is', 'payload->>region', null]);++ for (const region of ['', null, undefined]) {+ reads.length = 0;+ await loadImproveRounds({ promptId: 'p1', brandId: 'b1', region });+ expect(reads[0].filters).toContainEqual(['is', 'payload->>region', null]);+ expect(+ reads[0].filters.some(([op, column]) => op === 'eq' && column === 'payload->>region'),+ ).toBe(false);+ }+ });++ it('reads no tasks when there is no earlier round', async () => {+ reads.length = 0;+ tables.actions = [];+ expect(await loadImproveRounds({ promptId: 'p1', brandId: 'b1' })).toEqual([]);+ expect(reads.map((read) => read.table)).toEqual(['actions']);+ });+});diff --git a/server/src/lib/prompt-improve.js b/server/src/lib/prompt-improve.jsindex 719e14cae..7e1b9dbb5 100644--- a/server/src/lib/prompt-improve.js+++ b/server/src/lib/prompt-improve.js@@ -327,11 +327,72 @@ function evidenceBlock(evidence) {return lines.map((l) => ` ${l}`).join('\n');}+const VERDICT_WORDS = {+ improved: 'improved',+ declined: 'declined',+ unchanged: 'unchanged',+ not_enough_data: 'not enough data',+ too_early: 'too early to read',+};++const SUGGESTION_STATUS_WORDS = {+ done: 'done',+ skipped: 'skipped by the team',+ not_done: 'not done',+};++/** "today", "1 day ago", "48 days ago". */+const ago = (days) => (days === 0 ? 'today' : `${days} day${days === 1 ? '' : 's'} ago`);++/**+ * Earlier Improve rounds on the prompt (learning B5), newest first: how its+ * platforms moved since and which of its suggestions were done or skipped.+ * Empty with none.+ */+function historyBlock(history) {+ if (!history?.length) return '';+ const name = (id) => IMPROVE_PLATFORM_LABELS[id] ?? id;+ const rounds = history.map((round) => {+ const moved = round.platforms.length+ ? round.platforms+ .map((p) => `${name(p.id)} ${VERDICT_WORDS[p.verdict] ?? p.verdict}`)+ .join(', ')+ : 'not read yet';+ const when = round.open+ ? 'still open'+ : round.final+ ? 'completed, final read'+ : 'completed, read so far';+ const suggestions = round.suggestions+ .map(+ (s) =>+ `[${s.platform ? name(s.platform) : 'any'}] "${s.title.replace(/"/g, "'")}" (${SUGGESTION_STATUS_WORDS[s.status] ?? 'not done'})`,+ )+ .join('; ');+ const sent = round.sent+ ? `${round.sent}${typeof round.daysAgo === 'number' ? `, ${ago(round.daysAgo)}` : ''}`+ : 'earlier';+ return `- Sent ${sent} (${when}): ${moved}.${suggestions ? `\n Suggestions: ${suggestions}` : ''}`;+ });+ const early = history.some((round) => round.platforms.some((p) => p.verdict === 'too_early'));+ const skipped = history.some((round) => round.suggestions.some((s) => s.status === 'skipped'));+ return `++Earlier Improve rounds on this prompt, newest first:+${rounds.join('\n')}+Build on suggestions that came before an improvement on their platform. Do not suggest again what was done on a platform that did not move, unless the evidence now shows something new.${+ early+ ? ' A platform "too early to read" was sent too recently to tell whether it moved: do not count it as not moving.'+ : ''+ }${skipped ? ' Do not suggest again what the team skipped.' : ''}`;+}+/*** System and user prompt for one generation. `platforms` are the actionable* ones: [{ id, state, mentionPosition, mentionTotal, citationRank, evidence }].+ * `history`: the brand's earlier Improve rounds on the prompt (loadImproveRounds).*/-export function buildImprovePrompt({ promptText, brand, platforms }) {+export function buildImprovePrompt({ promptText, brand, platforms, history = [] }) {const system = `You are an AI search (answer engine) optimization strategist. For one tracked prompt, you tell a brand what to do on each AI platform where it is missing or ranked late.Rules:@@ -360,7 +421,7 @@ ${evidenceBlock(p.evidence)}`;const prompt = `Brand: ${brand.name}${brand.domains?.length ? ` (${brand.domains.join(', ')})` : ''}${brand.description ? `About the brand: ${brand.description}\n` : ''}Tracked prompt: "${promptText}"-${blocks}+${blocks}${historyBlock(history)}Write the suggestions. Use each platform id exactly as given.`;diff --git a/server/src/lib/prompt-improve.test.js b/server/src/lib/prompt-improve.test.jsindex de2ec865d..fc141d4cc 100644--- a/server/src/lib/prompt-improve.test.js+++ b/server/src/lib/prompt-improve.test.js@@ -276,6 +276,176 @@ describe('buildImprovePrompt', () => {expect(prompt).toContain('"q1" ×2');});+ /** The brand's earlier rounds on the prompt (learning B5), and none without. */+ it('says what earlier Improve rounds on the prompt did, and how their platforms moved', () => {+ const platforms = [+ {+ id: 'gemini-web',+ state: 'not_mentioned',+ evidence: {+ fanouts: [],+ citedDomains: [],+ competitors: [],+ competitorUrls: [],+ brandPages: [],+ excerpt: '',+ },+ },+ ];+ const brand = { name: 'Acme', domains: [] };+ const { prompt } = buildImprovePrompt({+ promptText: 'best coffee subscription',+ brand,+ platforms,+ history: [+ {+ sent: '2026-09-10',+ daysAgo: 48,+ open: false,+ final: true,+ platforms: [+ { id: 'chatgpt-web', verdict: 'improved' },+ { id: 'gemini-web', verdict: 'unchanged' },+ ],+ suggestions: [{ platform: 'gemini-web', title: 'Pitch seriouseats.com', status: 'done' }],+ },+ ],+ });+ expect(prompt).toContain(+ '- Sent 2026-09-10, 48 days ago (completed, final read): ChatGPT improved, Gemini unchanged.\n Suggestions: [Gemini] "Pitch seriouseats.com" (done)',+ );+ expect(prompt).toContain('Do not suggest again what was done on a platform that did not move');+ expect(+ buildImprovePrompt({ promptText: 'best coffee subscription', brand, platforms }).prompt,+ ).not.toContain('Earlier Improve rounds');+ expect(prompt).not.toContain('too early to read');+ expect(prompt).not.toContain('skipped');+ });++ /** What the team skipped is its call, not a suggestion that waits to be done. */+ it('says which earlier suggestions the team skipped, and not to suggest them again', () => {+ const { prompt } = buildImprovePrompt({+ promptText: 'best coffee subscription',+ brand: { name: 'Acme', domains: [] },+ platforms: [+ {+ id: 'gemini-web',+ state: 'not_mentioned',+ evidence: {+ fanouts: [],+ citedDomains: [],+ competitors: [],+ competitorUrls: [],+ brandPages: [],+ excerpt: '',+ },+ },+ ],+ history: [+ {+ sent: '2026-09-10',+ daysAgo: 48,+ open: false,+ final: true,+ platforms: [{ id: 'gemini-web', verdict: 'unchanged' }],+ suggestions: [+ { platform: 'gemini-web', title: 'Pitch seriouseats.com', status: 'skipped' },+ { platform: 'gemini-web', title: 'Add an FAQ', status: 'not_done' },+ ],+ },+ ],+ });+ expect(prompt).toContain(+ 'Suggestions: [Gemini] "Pitch seriouseats.com" (skipped by the team); [Gemini] "Add an FAQ" (not done)',+ );+ expect(prompt).toContain('Do not suggest again what the team skipped.');+ });++ it("keeps an earlier suggestion's own double quotes from closing its quoted title", () => {+ const { prompt } = buildImprovePrompt({+ promptText: 'best coffee subscription',+ brand: { name: 'Acme', domains: [] },+ platforms: [+ {+ id: 'gemini-web',+ state: 'not_mentioned',+ evidence: {+ fanouts: [],+ citedDomains: [],+ competitors: [],+ competitorUrls: [],+ brandPages: [],+ excerpt: '',+ },+ },+ ],+ history: [+ {+ sent: '2026-09-10',+ daysAgo: 48,+ open: false,+ final: true,+ platforms: [{ id: 'gemini-web', verdict: 'improved' }],+ suggestions: [+ {+ platform: 'gemini-web',+ title: 'Answer "is it worth it" on /pricing',+ status: 'done',+ },+ ],+ },+ ],+ });+ expect(prompt).toContain(`[Gemini] "Answer 'is it worth it' on /pricing" (done)`);+ });++ /** A round sent under a week ago has no read of its own yet (learning B5). */+ it('says a round sent days ago is too early to read, not that it did not move', () => {+ const { prompt } = buildImprovePrompt({+ promptText: 'best coffee subscription',+ brand: { name: 'Acme', domains: [] },+ platforms: [+ {+ id: 'gemini-web',+ state: 'not_mentioned',+ evidence: {+ fanouts: [],+ citedDomains: [],+ competitors: [],+ competitorUrls: [],+ brandPages: [],+ excerpt: '',+ },+ },+ ],+ history: [+ {+ sent: '2026-10-26',+ daysAgo: 2,+ open: true,+ final: false,+ platforms: [{ id: 'gemini-web', verdict: 'too_early' }],+ suggestions: [{ platform: 'gemini-web', title: 'Pitch seriouseats.com', status: 'done' }],+ },+ {+ sent: '2026-10-28',+ daysAgo: 0,+ open: true,+ final: false,+ platforms: [{ id: 'chatgpt-web', verdict: 'too_early' }],+ suggestions: [],+ },+ ],+ });+ expect(prompt).toContain(+ '- Sent 2026-10-26, 2 days ago (still open): Gemini too early to read.',+ );+ expect(prompt).toContain('- Sent 2026-10-28, today (still open): ChatGPT too early to read.');+ expect(prompt).toContain(+ 'A platform "too early to read" was sent too recently to tell whether it moved: do not count it as not moving.',+ );+ });+it('says how rarely a not-mentioned platform names the brand', () => {const { prompt } = buildImprovePrompt({promptText: 'best coffee subscription',diff --git a/server/src/routes/prompts-improve-generate.test.js b/server/src/routes/prompts-improve-generate.test.jsnew file mode 100644index 000000000..e5a29cd42--- /dev/null+++ b/server/src/routes/prompts-improve-generate.test.js@@ -0,0 +1,172 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import express from 'express';++/**+ * POST /api/prompts/:promptId/improve builds the Improve set. The brand's+ * earlier Improve rounds on the prompt (learning B5) are read for the set's+ * region and go into the model's prompt; when they can't be read, the set is+ * still written, as before, and the request's log says why.+ */++const generateObject = vi.fn();+const loadImproveRounds = vi.fn();+const getImproveQuotaStatus = vi.fn();+const release = vi.fn();++// One platform where the brand isn't named: actionable, so the model is called.+const answers = [+ {+ id: 'r1',+ platform: 'chatgpt-web',+ created_at: '2026-10-12T08:00:00.000Z',+ mention_count: 0,+ mention_position: null,+ mentioned_entity_count: 3,+ citation_count: 0,+ citations: [],+ search_queries: [],+ competitor_mentions: [],+ },+];++function table(name) {+ const result = () => {+ if (name === 'prompts') {+ return {+ data: {+ id: 'p1',+ text: 'best crm',+ platforms: ['chatgpt-web'],+ prompt_sets: { brand_id: 'b1' },+ },+ error: null,+ };+ }+ if (name === 'brands') {+ return { data: { id: 'b1', name: 'Acme', organization_id: 'o1' }, error: null };+ }+ if (name === 'profiles') return { data: { organization_id: 'o1' }, error: null };+ if (name === 'prompt_results') return { data: answers, error: null };+ if (name === 'prompt_improve_suggestions') {+ return {+ data: {+ range_key: '7d',+ region: '',+ platform_states: [],+ suggestions: [],+ latest_result_at: answers[0].created_at,+ generated_at: '2026-10-13T08:00:00.000Z',+ },+ error: null,+ };+ }+ return { data: [], error: null };+ };+ const chain = new Proxy(+ {},+ {+ get: (_t, prop) => {+ if (prop === 'then') {+ return (resolve, reject) => Promise.resolve(result()).then(resolve, reject);+ }+ if (prop === 'single' || prop === 'maybeSingle') return async () => result();+ return () => chain;+ },+ },+ );+ return chain;+}++vi.mock('ai', async (importOriginal) => ({ ...(await importOriginal()), generateObject }));+vi.mock('../config/supabase.js', () => ({ default: { from: (name) => table(name) } }));+vi.mock('../lib/ai-provider.js', () => ({ resolveModel: vi.fn() }));+vi.mock('../lib/plan-guard.js', () => ({+ PlanLimitError: class extends Error {},+ enforceFeature: vi.fn(),+ reserveAiBudget: async () => ({ charge: vi.fn(), release }),+ getImproveQuotaStatus,+ requireFeature: () => (req, res, next) => next(),+ reserveRelevanceAllowance: vi.fn(),+ settleRelevanceUsage: vi.fn(),+}));+vi.mock('../lib/action-center/from-prompt-improve.js', () => ({+ liveImproveSends: async () => ({ latest: null, sentSuggestionIds: [] }),+ loadImproveRounds,+ sendImproveToActionCenter: vi.fn(),+}));++const { default: router } = await import('./prompts.js');++let server;+let baseUrl;+let log;++const build = (region = '') =>+ fetch(`${baseUrl}/p1/improve`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ preset: '7d', from: '2026-10-06T00:00:00.000Z', region }),+ });++beforeEach(async () => {+ generateObject.mockReset();+ generateObject.mockResolvedValue({ object: { suggestions: [] }, usage: {} });+ loadImproveRounds.mockReset();+ release.mockReset();+ getImproveQuotaStatus.mockResolvedValue({ unlimited: true });+ log = { error: vi.fn(), warn: vi.fn(), info: vi.fn() };+ const app = express();+ app.use(express.json());+ app.use((req, _res, next) => {+ req.user = { id: 'u1' };+ req.log = log;+ next();+ });+ app.use('/api/prompts', router);+ await new Promise((resolve) => {+ server = app.listen(0, resolve);+ });+ baseUrl = `http://127.0.0.1:${server.address().port}/api/prompts`;+});++afterEach(async () => {+ await new Promise((resolve) => server.close(resolve));+});++describe('POST /api/prompts/:promptId/improve', () => {+ it("tells the model the set's region's earlier rounds", async () => {+ loadImproveRounds.mockResolvedValue([+ {+ sent: '2026-09-10',+ daysAgo: 48,+ open: false,+ final: true,+ platforms: [{ id: 'chatgpt-web', verdict: 'unchanged' }],+ suggestions: [{ platform: 'chatgpt-web', title: 'Add a table', status: 'done' }],+ },+ ]);+ const res = await build('US');+ expect(res.status).toBe(200);+ expect(loadImproveRounds).toHaveBeenCalledWith({ promptId: 'p1', brandId: 'b1', region: 'US' });+ expect(generateObject).toHaveBeenCalledTimes(1);+ expect(generateObject.mock.calls[0][0].prompt).toContain(+ 'Earlier Improve rounds on this prompt, newest first:',+ );+ });++ it('still writes the suggestions when the earlier rounds cannot be read', async () => {+ const err = new Error('earlier Improve rounds read failed: timeout');+ loadImproveRounds.mockRejectedValue(err);+ const res = await build();+ expect(res.status).toBe(200);+ expect((await res.json()).improve.rangeKey).toBe('7d');+ expect(generateObject).toHaveBeenCalledTimes(1);+ expect(generateObject.mock.calls[0][0].prompt).not.toContain('Earlier Improve rounds');+ expect(log.warn).toHaveBeenCalledWith(+ { err, promptId: 'p1' },+ 'prompt improve earlier rounds unavailable',+ );+ expect(log.error).not.toHaveBeenCalled();+ expect(release).toHaveBeenCalledTimes(1);+ });+});diff --git a/server/src/routes/prompts.js b/server/src/routes/prompts.jsindex 54c97fb55..0781e6840 100644--- a/server/src/routes/prompts.js+++ b/server/src/routes/prompts.js@@ -35,6 +35,7 @@ import {} from '../lib/prompt-improve.js';import {liveImproveSends,+ loadImproveRounds,sendImproveToActionCenter,} from '../lib/action-center/from-prompt-improve.js';@@ -1157,10 +1158,19 @@ router.post('/:promptId/improve', requireFeature('prompt_improve'), async (req,promptId,});try {+ // The brand's earlier Improve rounds on this prompt (learning B5).+ // Without them the suggestions are still written, as before.+ let history = [];+ try {+ history = await loadImproveRounds({ promptId, brandId, region: range.region });+ } catch (err) {+ req.log.warn({ err, promptId }, 'prompt improve earlier rounds unavailable');+ }const { system, prompt: userPrompt } = buildImprovePrompt({promptText: prompt.text,brand: { name: brand.name, description: brand.description, domains: brandDomains },platforms: withEvidence,+ history,});// Every attempt is charged at its real token cost, so the budget holds// for any model, including attempts withRetry repeats after a schema miss.