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.

feat(content): tell the Content Agent what the live page earns before it changes it (learning B5, part 3) (#1699)

Cihan5732d492a050
diff --git a/docs/guides/content-editor.mdx b/docs/guides/content-editor.mdx
index f2df874ec..a490f3488 100644
--- a/docs/guides/content-editor.mdx
+++ b/docs/guides/content-editor.mdx
@@ -146,7 +146,7 @@ Rebuilding the outline and running the Content Agent use your workspace's monthl
### Apply rules with Agent
-**Apply rules with Agent**, in the Rules tab, asks the Content Agent to suggest changes for the rules the page doesn't meet yet. Its suggestions appear in the page, each with **Accept** and **Reject**, under a bar with **Accept all** and **Reject all**. What you accept and reject teaches it: for a rule whose suggestions this brand has mostly rejected over the last 90 days (at least five decided, on two pages or more, seven in ten rejected), it keeps its changes as small as the rule allows, and while it works its card says which rules. Only its small changes count: a rejected **Rewrite the whole page** doesn't. Quality's **Hand off** isn't affected.
+**Apply rules with Agent**, in the Rules tab, asks the Content Agent to suggest changes for the rules the page doesn't meet yet. Its suggestions appear in the page, each with **Accept** and **Reject**, under a bar with **Accept all** and **Reject all**. What you accept and reject teaches it: for a rule whose suggestions this brand has mostly rejected over the last 90 days (at least five decided, on two pages or more, seven in ten rejected), it keeps its changes as small as the rule allows, and while it works its card says which rules. Only its small changes count: a rejected **Rewrite the whole page** doesn't. On a draft that optimizes a live page, it is also told how that page does now, as the **Live page** tab shows it over 30 days: how often AI answers cite it and the prompts it lost. It keeps the answers and facts AI engines cite, answers a lost prompt's question where a rule's change can, and its card says what it kept in mind. Quality's **Hand off** isn't affected.
- A decided suggestion can be taken back with **Undo** or **Restore**; **Undo all** brings every one back to review.
- One run makes at most 8 changes. Run it again for the rules still open.
diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.js
index 818e98365..6d857bbce 100644
--- a/server/src/lib/content-agent.js
+++ b/server/src/lib/content-agent.js
@@ -50,6 +50,7 @@ import { editSpans } from './edit-spans.js';
import { getLanguageName } from './languages.js';
import { PlanLimitError, reserveAiBudget } from './plan-guard.js';
import { carefulLine, carefulRules } from './content-suggestion-stats.js';
+import { livePageLine, readLivePage } from './content-live-page.js';
import { logger } from './logger.js';
/**
@@ -1127,6 +1128,7 @@ export function buildFixPrompt({
sources = '',
handoff = false,
careful = '',
+ livePage = '',
}) {
const lines = passages.map((p, i) => {
const locked = lockedKeys.has(passageKey(p.text)) ? ', LOCKED' : '';
@@ -1146,7 +1148,7 @@ Page type: ${pageTypeFormat(draft.page_type)}
Rules the page doesn't meet yet (key: rule — what it asks and how the page reads now):
${rules.join('\n')}
-${over ? `${over}\n` : ''}${careful ? `${careful}\n` : ''}${handoff ? `${KEEP_PLACEHOLDERS}\n` : ''}${own.length ? `\nThe writer's own rules, to follow in every change:\n${own.join('\n')}\n` : ''}${sources ? `\n${sources}\n` : ''}${linkTargets ? `\n${linkTargets}\n` : ''}
+${over ? `${over}\n` : ''}${careful ? `${careful}\n` : ''}${livePage ? `${livePage}\n` : ''}${handoff ? `${KEEP_PLACEHOLDERS}\n` : ''}${own.length ? `\nThe writer's own rules, to follow in every change:\n${own.join('\n')}\n` : ''}${sources ? `\n${sources}\n` : ''}${linkTargets ? `\n${linkTargets}\n` : ''}
The page, passage by passage:
${lines.join('\n')}
@@ -1219,7 +1221,7 @@ export async function fixDraftRules({ draftId, brandId, input, handoff = null })
const { data: draft, error: draftError } = await supabaseAdmin
.from('content_drafts')
.select(
- `id, brand_id, title, body_html, page_type, rules, action_id, status, source_url, agent_state, agent_state_at, shopping_path${handoff ? ', quality_handoff' : ''}`,
+ `id, brand_id, title, body_html, page_type, rules, action_id, status, source_url, published_url, agent_state, agent_state_at, shopping_path${handoff ? ', quality_handoff' : ''}`,
)
.eq('id', draftId)
.maybeSingle();
@@ -1381,12 +1383,40 @@ async function fixWithRun({
// The data it writes from, numbered, Brief › Your input last (CE-048);
// the numbers it may write unmarked.
await advanceRun(run, 1);
- const [prompts, inputRow, shoppingPath] = await Promise.all([
+ const [prompts, inputRow, shoppingPath, livePage] = await Promise.all([
readPrompts(brandId, input.sources?.promptIds ?? []),
readDraftInputs(draftId),
// Its shopping path, checked: none when it isn't one (shopping path, part 4).
shoppingPathFor(draft),
+ // How the page it optimizes does now (learning B5), as the Live page
+ // tab reads it: at the address it was published at, else the one it
+ // optimizes. Apply rules with Agent only, as the careful rules. A read
+ // that fails leaves the run as it was.
+ handoff
+ ? null
+ : readLivePage(brandId, draft.published_url ?? draft.source_url).catch((err) => {
+ logger.warn(
+ { err: { message: err?.message }, draftId },
+ '[content-agent] live page not read',
+ );
+ return null;
+ }),
]);
+ // Named on the run's sources step, which the editor's agent card shows:
+ // only a page AI answers cited, as the card speaks of its citations. One
+ // with AI visits alone is still told to the agent.
+ const cited = livePage && (livePage.citations.cur > 0 || livePage.citations.prev > 0);
+ if (cited && run?.steps?.[1]) {
+ run.steps[1] = {
+ ...run.steps[1],
+ live: {
+ days: livePage.days,
+ citations: livePage.citations.cur,
+ before: livePage.citations.prev,
+ lost: livePage.prompts.lost,
+ },
+ };
+ }
const numbered = numberSources({
prompts,
fanouts: input.sources?.fanouts,
@@ -1479,6 +1509,7 @@ async function fixWithRun({
sources: sourcesPrompt(numbered),
handoff,
careful: carefulLine(careful),
+ livePage: livePageLine(livePage),
}),
abortSignal,
}),
diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.js
index a66a921eb..5070f5f31 100644
--- a/server/src/lib/content-agent.test.js
+++ b/server/src/lib/content-agent.test.js
@@ -13,11 +13,12 @@ let rows;
let writes;
const generateObject = vi.fn();
const reserveAiBudget = vi.fn();
+/** The functions called, with what they were sent. */
+const rpc = vi.fn();
vi.mock('../config/supabase.js', () => ({
default: {
- // A function's rows, by its name (content_suggestion_decisions, 00367).
- rpc: async (name) => ({ data: rows[name] ?? [], error: null }),
+ rpc: (...args) => rpc(...args),
from: (table) => {
const result = () => ({ data: rows[table] ?? null, error: null });
const chain = {
@@ -103,6 +104,7 @@ const { knownNumbers, numberSources } = await import('./content-sources.js');
const { NAME_PLACEHOLDER, QUOTE_PLACEHOLDER, guardPeople, knownPeople } =
await import('./content-people.js');
const { PlanLimitError } = await import('./plan-guard.js');
+const { logger } = await import('./logger.js');
// The real hold: self-hosted here, so it reserves nothing and writes each charge.
const realGuard = await vi.importActual('./plan-guard.js');
@@ -147,6 +149,8 @@ const sourcing = { numbered: numberSources(SOURCES) };
beforeEach(() => {
stopIfStopped.mockReset().mockResolvedValue(undefined);
writes = [];
+ // A function's rows, by its name (content_suggestion_decisions, 00367).
+ rpc.mockReset().mockImplementation(async (name) => ({ data: rows[name] ?? [], error: null }));
rows = {
content_drafts: {
id: DRAFT,
@@ -1757,6 +1761,155 @@ describe('fixDraftRules', () => {
expect(run.steps[0]).toMatchObject({ key: 'read_rules', careful: ['faq'] });
});
+ it('tells the agent how the live page does now, and names it on the run (learning B5)', async () => {
+ rows.content_drafts = { ...rows.content_drafts, source_url: 'https://acme.example/guide' };
+ rows.content_page_impact = {
+ citations: { cur: 12, prev: 20 },
+ aiVisits: { cur: 35, prev: 40 },
+ promptCounts: { cur: 4, prev: 6, lost: 1 },
+ prompts: [{ text: 'pour over vs french press', cur: 0, prev: 4 }],
+ };
+ const run = {
+ id: 'r1',
+ steps: [
+ { key: 'read_rules', count: 2 },
+ { key: 'gather_sources', count: 0 },
+ { key: 'write_changes', count: null },
+ ],
+ signal: new AbortController().signal,
+ };
+ runs.startRun = async () => run;
+ generateObject.mockResolvedValue({
+ object: { suggestions: [] },
+ usage: { inputTokens: 900, outputTokens: 20 },
+ });
+ try {
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() });
+ } finally {
+ runs.startRun = null;
+ delete rows.content_page_impact;
+ }
+ expect(generateObject.mock.calls[0][0].prompt).toContain(
+ 'How the live page does now (last 30 days, against the 30 before): tracked AI answers cited it 12 times (20 before)',
+ );
+ expect(run.steps[1]).toMatchObject({
+ key: 'gather_sources',
+ live: { days: 30, citations: 12, before: 20, lost: 1 },
+ });
+ // The brand's own page, at the address it optimizes, over the tab's 30 days.
+ expect(rpc).toHaveBeenCalledWith('content_page_impact', {
+ p_brand_id: BRAND,
+ p_page: 'https://acme.example/guide',
+ p_days: 30,
+ });
+ });
+
+ it('reads the live page where it was published, as the Live page tab does', async () => {
+ // Published at another address, then back to Draft: the tab reads that one.
+ rows.content_drafts = {
+ ...rows.content_drafts,
+ source_url: 'https://acme.example/guide',
+ published_url: 'https://acme.example/brew-guide',
+ };
+ generateObject.mockResolvedValue({
+ object: { suggestions: [] },
+ usage: { inputTokens: 900, outputTokens: 20 },
+ });
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() });
+ expect(rpc).toHaveBeenCalledWith(
+ 'content_page_impact',
+ expect.objectContaining({ p_page: 'https://acme.example/brew-guide' }),
+ );
+ expect(rpc).not.toHaveBeenCalledWith(
+ 'content_page_impact',
+ expect.objectContaining({ p_page: 'https://acme.example/guide' }),
+ );
+ });
+
+ it('names a live page on the run only when AI answers cited it, and still tells the agent of its visits', async () => {
+ rows.content_drafts = { ...rows.content_drafts, source_url: 'https://acme.example/guide' };
+ rows.content_page_impact = {
+ citations: { cur: 0, prev: 0 },
+ aiVisits: { cur: 35, prev: 40 },
+ promptCounts: { cur: 0, prev: 0, lost: 0 },
+ prompts: [],
+ };
+ const run = {
+ id: 'r1',
+ steps: [
+ { key: 'read_rules', count: 2 },
+ { key: 'gather_sources', count: 0 },
+ { key: 'write_changes', count: null },
+ ],
+ signal: new AbortController().signal,
+ };
+ runs.startRun = async () => run;
+ generateObject.mockResolvedValue({
+ object: { suggestions: [] },
+ usage: { inputTokens: 900, outputTokens: 20 },
+ });
+ try {
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() });
+ } finally {
+ runs.startRun = null;
+ delete rows.content_page_impact;
+ }
+ expect(generateObject.mock.calls[0][0].prompt).toContain(
+ 'tracked AI answers cited it 0 times (0 before), for 0 prompts, and it had 35 AI visits (40 before).',
+ );
+ expect(run.steps[1]).not.toHaveProperty('live');
+ });
+
+ it('tells nothing of a live page for a new page', async () => {
+ generateObject.mockResolvedValue({
+ object: { suggestions: [] },
+ usage: { inputTokens: 900, outputTokens: 20 },
+ });
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() });
+ expect(generateObject.mock.calls[0][0].prompt).not.toContain('How the live page does now');
+ });
+
+ it('runs on when the live page cannot be read, telling nothing of it', async () => {
+ rows.content_drafts = { ...rows.content_drafts, source_url: 'https://acme.example/guide' };
+ rpc.mockImplementation(async (name) =>
+ name === 'content_page_impact'
+ ? { data: null, error: { message: 'canceling statement due to statement timeout' } }
+ : { data: rows[name] ?? [], error: null },
+ );
+ const run = {
+ id: 'r1',
+ steps: [
+ { key: 'read_rules', count: 2 },
+ { key: 'gather_sources', count: 0 },
+ { key: 'write_changes', count: null },
+ ],
+ signal: new AbortController().signal,
+ };
+ runs.startRun = async () => run;
+ generateObject.mockResolvedValue({
+ object: { suggestions: [suggestion()] },
+ usage: { inputTokens: 900, outputTokens: 200 },
+ });
+ vi.mocked(logger.warn).mockClear();
+ try {
+ await fixDraftRules({
+ draftId: DRAFT,
+ brandId: BRAND,
+ input: input({ sources: SOURCES }),
+ });
+ } finally {
+ runs.startRun = null;
+ }
+ expect(generateObject).toHaveBeenCalledTimes(1);
+ expect(generateObject.mock.calls[0][0].prompt).not.toContain('How the live page does now');
+ expect(run.steps[1]).not.toHaveProperty('live');
+ expect(writes.find((w) => w.table === 'content_draft_suggestions')).toBeDefined();
+ expect(logger.warn).toHaveBeenCalledWith(
+ expect.objectContaining({ draftId: DRAFT }),
+ '[content-agent] live page not read',
+ );
+ });
+
it("knows the shopping path's prices and names, a product's number only beside its name, and links its pages", async () => {
rows.content_drafts = {
...rows.content_drafts,
@@ -2302,6 +2455,52 @@ describe('Quality’s hand-off (CE-051)', () => {
expect(fixed.steps[0]).toMatchObject({ careful: ['trust:claimsSourced'] });
});
+ it('runs as it was on a live page: Apply rules with Agent only is told how it does (learning B5)', async () => {
+ rows.content_drafts = { ...rows.content_drafts, source_url: 'https://acme.example/guide' };
+ rows.content_page_impact = {
+ citations: { cur: 12, prev: 20 },
+ aiVisits: { cur: 35, prev: 40 },
+ promptCounts: { cur: 4, prev: 6, lost: 1 },
+ prompts: [{ text: 'pour over vs french press', cur: 0, prev: 4 }],
+ };
+ const run = () => ({
+ id: 'r1',
+ steps: [
+ { key: 'read_rules', count: 1 },
+ { key: 'gather_sources', count: 0 },
+ { key: 'write_changes', count: null },
+ ],
+ signal: new AbortController().signal,
+ });
+ const handedOff = run();
+ const fixed = run();
+ generateObject.mockResolvedValue({
+ object: { suggestions: [] },
+ usage: { inputTokens: 900, outputTokens: 20 },
+ });
+ try {
+ runs.startRun = async () => handedOff;
+ await fixDraftRules({
+ draftId: DRAFT,
+ brandId: BRAND,
+ input: handoff(),
+ handoff: { origin: 'person' },
+ });
+ // The hand-off doesn't even read it.
+ expect(rpc).not.toHaveBeenCalledWith('content_page_impact', expect.anything());
+ // The same page in Apply rules with Agent is told how it does.
+ runs.startRun = async () => fixed;
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: handoff() });
+ } finally {
+ runs.startRun = null;
+ delete rows.content_page_impact;
+ }
+ expect(generateObject.mock.calls[0][0].prompt).not.toContain('How the live page does now');
+ expect(handedOff.steps[1]).not.toHaveProperty('live');
+ expect(generateObject.mock.calls[1][0].prompt).toContain('How the live page does now');
+ expect(fixed.steps[1]).toMatchObject({ live: { citations: 12, before: 20, lost: 1 } });
+ });
+
it('runs an automatic one only on a draft its brand hands off', async () => {
await expect(
fixDraftRules({
diff --git a/server/src/lib/content-live-page.js b/server/src/lib/content-live-page.js
new file mode 100644
index 000000000..e27237b34
--- /dev/null
+++ b/server/src/lib/content-live-page.js
@@ -0,0 +1,81 @@
+/**
+ * How a draft's live page does now, for the Content Agent (learning B5): the
+ * editor's Live page tab numbers (content_page_impact, 00209) over the last
+ * 30 days against the 30 before — how often tracked AI answers cite it, for
+ * how many prompts and the ones it lost, and its AI visits. The page earns
+ * these as it reads now, so the agent keeps the answers and facts AI engines
+ * cite from it, and answers a lost prompt's question where a rule's change
+ * can. The brand's own page only.
+ */
+
+import supabaseAdmin from '../config/supabase.js';
+
+/** The window the agent is told about, as the Live page tab's 30d. */
+export const LIVE_PAGE_DAYS = 30;
+
+/** Lost prompts it is told, by name. */
+const LOST_SHOWN = 3;
+/**
+ * A lost prompt's text at most, as it is quoted: a prompt has no length
+ * limit where it is kept, and one long one mustn't make the line as long as
+ * the page (the CSV import stops at 500).
+ */
+export const LOST_PROMPT_CHARS = 300;
+
+const n = (value) => (Number.isFinite(Number(value)) ? Number(value) : 0);
+const pair = (value) => ({ cur: n(value?.cur), prev: n(value?.prev) });
+/** "1 time", "12 times". */
+const counted = (count, word) => `${count} ${word}${count === 1 ? '' : 's'}`;
+/** A prompt's text on one line, cut to LOST_PROMPT_CHARS. */
+const promptText = (text) => {
+ const line = text.replace(/\s+/g, ' ').trim();
+ return line.length > LOST_PROMPT_CHARS ? `${line.slice(0, LOST_PROMPT_CHARS).trimEnd()}…` : line;
+};
+
+/**
+ * The numbers the agent is told, from content_page_impact's answer; null
+ * when the page was neither cited nor visited from AI in either window.
+ */
+export function livePageSummary(impact, days = LIVE_PAGE_DAYS) {
+ if (!impact || typeof impact !== 'object') return null;
+ const citations = pair(impact.citations);
+ const aiVisits = pair(impact.aiVisits);
+ if (!citations.cur && !citations.prev && !aiVisits.cur && !aiVisits.prev) return null;
+ const lostPrompts = (Array.isArray(impact.prompts) ? impact.prompts : [])
+ .filter((p) => n(p?.cur) === 0 && n(p?.prev) > 0 && typeof p.text === 'string' && p.text.trim())
+ .sort((a, b) => n(b.prev) - n(a.prev))
+ .slice(0, LOST_SHOWN)
+ .map((p) => promptText(p.text));
+ return {
+ days,
+ citations,
+ prompts: { cur: n(impact.promptCounts?.cur), lost: n(impact.promptCounts?.lost) },
+ lostPrompts,
+ aiVisits,
+ };
+}
+
+/** The live page's summary, read with the service role; null with nothing to tell. */
+export async function readLivePage(brandId, page, days = LIVE_PAGE_DAYS) {
+ if (!page) return null;
+ const { data, error } = await supabaseAdmin.rpc('content_page_impact', {
+ p_brand_id: brandId,
+ p_page: page,
+ p_days: days,
+ });
+ if (error) throw new Error(`live page read failed: ${error.message}`);
+ return livePageSummary(data, days);
+}
+
+/** What the agent is told about the live page; empty with nothing to tell. */
+export function livePageLine(summary) {
+ if (!summary) return '';
+ const { days, citations, prompts, lostPrompts, aiVisits } = summary;
+ const lost = prompts.lost
+ ? ` It lost ${counted(prompts.lost, 'prompt')} it was cited for${
+ lostPrompts.length ? `: ${lostPrompts.map((text) => `"${text}"`).join(', ')}` : ''
+ }.`
+ : '';
+ return `How the live page does now (last ${days} days, against the ${days} before): tracked AI answers cited it ${counted(citations.cur, 'time')} (${citations.prev} before), for ${counted(prompts.cur, 'prompt')}, and it had ${counted(aiVisits.cur, 'AI visit')} (${aiVisits.prev} before).${lost}
+It earns these as it reads now: keep the answers, facts and names AI engines may cite from it, and change them only where a rule needs it.${prompts.lost ? " Where a rule's change can, answer a lost prompt's question." : ''}`;
+}
diff --git a/server/src/lib/content-live-page.test.js b/server/src/lib/content-live-page.test.js
new file mode 100644
index 000000000..6e3887603
--- /dev/null
+++ b/server/src/lib/content-live-page.test.js
@@ -0,0 +1,159 @@
+import { describe, expect, it, vi } from 'vitest';
+
+const rpc = vi.fn();
+vi.mock('../config/supabase.js', () => ({ default: { rpc: (...args) => rpc(...args) } }));
+
+const { LOST_PROMPT_CHARS, livePageLine, livePageSummary, readLivePage } =
+ await import('./content-live-page.js');
+
+/**
+ * How the live page does now, as the Content Agent is told (learning B5):
+ * what it earns as it reads, and the prompts it lost.
+ */
+const impact = {
+ citations: { cur: 12, prev: 20 },
+ aiVisits: { cur: 35, prev: 40 },
+ promptCounts: { cur: 4, prev: 6, lost: 2 },
+ prompts: [
+ { text: 'best pour over kettle', cur: 5, prev: 6 },
+ { text: 'how long to brew pour over', cur: 0, prev: 2 },
+ { text: 'pour over vs french press', cur: 0, prev: 4 },
+ ],
+};
+
+describe('livePageSummary', () => {
+ it('reads the numbers and the lost prompts, the most cited before first', () => {
+ expect(livePageSummary(impact)).toEqual({
+ days: 30,
+ citations: { cur: 12, prev: 20 },
+ prompts: { cur: 4, lost: 2 },
+ lostPrompts: ['pour over vs french press', 'how long to brew pour over'],
+ aiVisits: { cur: 35, prev: 40 },
+ });
+ });
+
+ it('quotes a lost prompt on one line, cut when it is long', () => {
+ const long = `pour over\n\n kettle ${'x'.repeat(5000)}`;
+ const { lostPrompts } = livePageSummary({
+ ...impact,
+ prompts: [{ text: long, cur: 0, prev: 3 }],
+ });
+ expect(LOST_PROMPT_CHARS).toBe(300);
+ expect(lostPrompts).toHaveLength(1);
+ expect(lostPrompts[0].startsWith('pour over kettle x')).toBe(true);
+ expect(lostPrompts[0]).toHaveLength(LOST_PROMPT_CHARS + 1);
+ expect(lostPrompts[0].endsWith('…')).toBe(true);
+ expect(
+ livePageLine(livePageSummary({ ...impact, prompts: [{ text: long, cur: 0, prev: 3 }] }))
+ .length,
+ ).toBeLessThan(1000);
+ });
+
+ it('tells nothing of a page neither cited nor visited from AI', () => {
+ expect(
+ livePageSummary({ citations: { cur: 0, prev: 0 }, aiVisits: { cur: 0, prev: 0 } }),
+ ).toBeNull();
+ expect(livePageSummary(null)).toBeNull();
+ });
+});
+
+describe('readLivePage', () => {
+ const BRAND = '33333333-3333-3333-3333-333333333333';
+ const PAGE = 'https://acme.example/guide';
+
+ it("reads the brand's page over the tab's 30 days, or the window it is given", async () => {
+ rpc.mockReset().mockResolvedValue({ data: impact, error: null });
+ expect(await readLivePage(BRAND, PAGE)).toMatchObject({
+ days: 30,
+ citations: { cur: 12, prev: 20 },
+ });
+ expect(rpc).toHaveBeenCalledWith('content_page_impact', {
+ p_brand_id: BRAND,
+ p_page: PAGE,
+ p_days: 30,
+ });
+ expect(await readLivePage(BRAND, PAGE, 7)).toMatchObject({ days: 7 });
+ expect(rpc).toHaveBeenLastCalledWith('content_page_impact', {
+ p_brand_id: BRAND,
+ p_page: PAGE,
+ p_days: 7,
+ });
+ });
+
+ it('names three lost prompts, the most cited before first', async () => {
+ rpc.mockReset().mockResolvedValue({
+ data: {
+ ...impact,
+ promptCounts: { cur: 1, prev: 5, lost: 4 },
+ prompts: [
+ { text: 'kept', cur: 3, prev: 1 },
+ { text: 'lost 2', cur: 0, prev: 2 },
+ { text: 'lost 9', cur: 0, prev: 9 },
+ { text: 'lost 1', cur: 0, prev: 1 },
+ { text: 'lost 5', cur: 0, prev: 5 },
+ { text: 'new', cur: 2, prev: 0 },
+ ],
+ },
+ error: null,
+ });
+ const summary = await readLivePage(BRAND, PAGE);
+ expect(summary.lostPrompts).toEqual(['lost 9', 'lost 5', 'lost 2']);
+ // The count is all it lost, past the three named.
+ expect(summary.prompts.lost).toBe(4);
+ });
+
+ it('tells nothing without a page, or of a page neither cited nor visited', async () => {
+ rpc.mockReset();
+ expect(await readLivePage(BRAND, null)).toBeNull();
+ expect(await readLivePage(BRAND, '')).toBeNull();
+ expect(rpc).not.toHaveBeenCalled();
+ rpc.mockResolvedValue({
+ data: { citations: { cur: 0, prev: 0 }, aiVisits: { cur: 0, prev: 0 }, prompts: [] },
+ error: null,
+ });
+ expect(await readLivePage(BRAND, PAGE)).toBeNull();
+ rpc.mockResolvedValue({ data: null, error: null });
+ expect(await readLivePage(BRAND, PAGE)).toBeNull();
+ });
+
+ it('throws when the read fails, for its caller to go on without it', async () => {
+ rpc.mockReset().mockResolvedValue({ data: null, error: { message: 'statement timeout' } });
+ await expect(readLivePage(BRAND, PAGE)).rejects.toThrow(
+ 'live page read failed: statement timeout',
+ );
+ });
+});
+
+describe('livePageLine', () => {
+ it('says what the page earns, and asks to keep it and answer what it lost', () => {
+ const line = livePageLine(livePageSummary(impact));
+ expect(line).toContain(
+ 'tracked AI answers cited it 12 times (20 before), for 4 prompts, and it had 35 AI visits (40 before). It lost 2 prompts it was cited for: "pour over vs french press", "how long to brew pour over".',
+ );
+ expect(line).toContain('keep the answers, facts and names AI engines may cite from it');
+ expect(line).toContain("answer a lost prompt's question");
+ });
+
+ it('says one as one', () => {
+ const line = livePageLine(
+ livePageSummary({
+ citations: { cur: 1, prev: 3 },
+ aiVisits: { cur: 1, prev: 0 },
+ promptCounts: { cur: 1, lost: 1 },
+ prompts: [{ text: 'pour over vs french press', cur: 0, prev: 2 }],
+ }),
+ );
+ expect(line).toContain(
+ 'tracked AI answers cited it 1 time (3 before), for 1 prompt, and it had 1 AI visit (0 before). It lost 1 prompt it was cited for: "pour over vs french press".',
+ );
+ });
+
+ it('asks nothing about lost prompts when none was lost', () => {
+ const line = livePageLine(
+ livePageSummary({ ...impact, promptCounts: { cur: 1, lost: 0 }, prompts: [] }),
+ );
+ expect(line).toContain('for 1 prompt,');
+ expect(line).not.toContain('lost');
+ expect(livePageLine(null)).toBe('');
+ });
+});
diff --git a/web/messages/en.json b/web/messages/en.json
index e7e3c6dee..08617532f 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -4066,6 +4066,7 @@
},
"agentCard": {
"careful": "Keeping its changes small for rules people here mostly reject: {rules}.",
+ "live": "Keeping what the live page earns: cited {citations, plural, one {# time} other {# times}} in {days} days ({before, number} before){lost, plural, =0 {} one {, # prompt lost} other {, # prompts lost}}.",
"breaks": "would break {names}",
"runFailed": "Content Agent couldn't finish",
"runFailedLine": "Nothing it wrote was kept. Try again; if it fails again, fix what's left yourself.",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx
index 143c97e60..3c039ba01 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx
@@ -473,6 +473,26 @@ describe('AgentCard', () => {
expect(container.textContent).toContain(
'Keeping its changes small for rules people here mostly reject: Price, GTIN and stock match the feed and FAQ block.',
);
+
+ // What the live page earns, which it keeps (learning B5).
+ run.steps[1] = {
+ ...run.steps[1],
+ key: 'gather_sources',
+ live: { days: 30, citations: 12, before: 20, lost: 2 },
+ };
+ await show(false);
+ expect(container.textContent).toContain(
+ 'Keeping what the live page earns: cited 12 times in 30 days (20 before), 2 prompts lost.',
+ );
+ // Thousands as the citations are, before as now.
+ run.steps[1] = {
+ ...run.steps[1],
+ live: { days: 30, citations: 1500, before: 1200, lost: 1 },
+ };
+ await show(false);
+ expect(container.textContent).toContain(
+ 'Keeping what the live page earns: cited 1,500 times in 30 days (1,200 before), 1 prompt lost.',
+ );
});
it('stops all of its motion with reduced motion, in every state', async () => {
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx
index 0644462d1..1d55ee9b3 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx
@@ -199,6 +199,8 @@ export function RunSteps({
const careful = (run.steps[0]?.careful ?? [])
.filter((key) => tRules.has(key))
.map((key) => tRules(key));
+ // What the live page earns, kept in mind on its sources step (learning B5).
+ const live = run.steps.find((step) => step.key === 'gather_sources')?.live;
return (
<>
<ol className="mt-1 flex flex-wrap items-center gap-1 text-[12px]">
@@ -236,6 +238,8 @@ export function RunSteps({
{tCard('careful', { rules: format.list(careful, { type: 'conjunction' }) })}
</p>
)}
+ {/* The live page it kept in mind (learning B5). */}
+ {live && <p className="mt-1 text-[11px] text-muted-foreground">{tCard('live', live)}</p>}
</>
);
}
diff --git a/web/src/lib/content/agent-runs.test.ts b/web/src/lib/content/agent-runs.test.ts
index e79d75d53..484d5cf2e 100644
--- a/web/src/lib/content/agent-runs.test.ts
+++ b/web/src/lib/content/agent-runs.test.ts
@@ -45,6 +45,17 @@ describe('agent runs', () => {
expect(agentRunFrom(row())!.steps[0]).not.toHaveProperty('careful');
});
+ it('reads the live page its sources step kept in mind, whole numbers only (learning B5)', () => {
+ const live = { days: 30, citations: 12, before: 20, lost: 1 };
+ const steps = [
+ { key: 'read_rules', count: 2 },
+ { key: 'gather_sources', count: 0, live },
+ ];
+ expect(agentRunFrom(row({ steps }))!.steps[1].live).toEqual(live);
+ const broken = [{ key: 'gather_sources', count: 0, live: { ...live, lost: -1 } }];
+ expect(agentRunFrom(row({ steps: broken }))!.steps[0]).not.toHaveProperty('live');
+ });
+
it('is under way until it ends, or until a stopped server’s run goes stale', () => {
const run = agentRunFrom(row())!;
const started = Date.parse('2026-10-23T10:00:00Z');
diff --git a/web/src/lib/content/agent-runs.ts b/web/src/lib/content/agent-runs.ts
index 5e5a5cec1..8d30d5dad 100644
--- a/web/src/lib/content/agent-runs.ts
+++ b/web/src/lib/content/agent-runs.ts
@@ -30,6 +30,11 @@ export interface RunStep {
* kept small (learning B6), as rule keys.
*/
careful?: string[];
+ /**
+ * Its sources step's live page (learning B5): how often tracked AI answers
+ * cited it in its last `days` and the days before, and the prompts it lost.
+ */
+ live?: { days: number; citations: number; before: number; lost: number };
}
export type AgentRunKind = 'fix' | 'rewrite' | 'write' | 'brief' | 'handoff';
@@ -53,6 +58,25 @@ export interface AgentRun {
*/
export const RUN_STALE_MS = 10 * 60_000;
+/** A step's live page as stored, when every number is one. */
+function liveOf(value: unknown): { live?: RunStep['live'] } {
+ if (!value || typeof value !== 'object') return {};
+ const { days, citations, before, lost } = value as Record<string, unknown>;
+ const whole = [days, citations, before, lost].every(
+ (n) => Number.isInteger(n) && (n as number) >= 0,
+ );
+ return whole
+ ? {
+ live: {
+ days: days as number,
+ citations: citations as number,
+ before: before as number,
+ lost: lost as number,
+ },
+ }
+ : {};
+}
+
/** A run as stored, or null when it isn't one the editor can show. */
export function agentRunFrom(row: Record<string, unknown> | null): AgentRun | null {
if (!row || typeof row.id !== 'string' || !Array.isArray(row.steps)) return null;
@@ -69,6 +93,7 @@ export function agentRunFrom(row: Record<string, unknown> | null): AgentRun | nu
...(Array.isArray(s.careful) && s.careful.some((k) => typeof k === 'string' && k)
? { careful: s.careful.filter((k) => typeof k === 'string' && k).slice(0, 5) }
: {}),
+ ...liveOf(s.live),
},
]
: [],