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): keep the Content Agent from inventing people and quotes (CE-044) (#1421)

Cihan19cb8cb81bdd
diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.js
index 7e7688891..0366682c1 100644
--- a/server/src/lib/content-agent.js
+++ b/server/src/lib/content-agent.js
@@ -7,6 +7,7 @@ import { resolveModel } from './ai-provider.js';
import { contentBriefModelId, rebuildSources } from './content-brief.js';
import { linkAllowList, linkKey, linkTargetsPrompt, readLinkTargets } from './content-links.js';
import { pageTypeFormat } from './content-page-types.js';
+import { authorWords, guardPeopleInHtml, knownPeople } from './content-people.js';
import {
RunStoppedError,
advanceRun,
@@ -23,6 +24,7 @@ import {
marksField,
numberSources,
sourceSuggestion,
+ sourceWords,
sourcesField,
sourcesPrompt,
stripSourceIds,
@@ -369,19 +371,27 @@ export function splitReplace(after, tag, links = null) {
* Links stay only to the URLs in `links` (linkAllowList): a replace's go in
* after_links, by offset into its words, an insert's in its HTML. Each keeps
* the sources it names and the words each supports (withSources); one that
- * adds a number with no source is not kept. `sourcing` is the numbered
- * sources and the numbers known already (the page's, the brand's): without
- * it, the page's.
+ * adds a number with no source is not kept. A person's name or a quote
+ * that what it was given doesn't have becomes a placeholder, read in its
+ * words as written, blockquotes too, before they are cleaned (CE-044).
+ * `sourcing` is the numbered sources, the numbers known already
+ * (the page's, the brand's) and the words names and quotes are looked up in
+ * (knownPeople): without it, the page's.
*/
export function keepSuggestions(raw, passages, lockedKeys, ruleKeys, links = null, sourcing = {}) {
const numbered = sourcing.numbered ?? [];
const known = sourcing.known ?? knownNumbers(...passages.map((p) => p.text));
+ const people = sourcing.people ?? knownPeople(...passages.map((p) => p.text));
const kept = [];
const seen = new Set();
for (const answer of raw) {
if (kept.length >= MAX_AGENT_SUGGESTIONS) break;
- // Source numbers belong in "sources", never in the words.
- const s = { ...answer, after: stripSourceIds(answer.after) };
+ // Source numbers belong in "sources", never in the words; no invented
+ // person or quote (CE-044).
+ const s = {
+ ...answer,
+ after: guardPeopleInHtml(stripSourceIds(answer.after), people).html,
+ };
const passage = passages[s.passage - 1];
// The card shows it: a source number the model wrote in ("(S2)") goes.
const reason = stripSourceIds(s.reason).trim().slice(0, 300);
@@ -676,6 +686,18 @@ async function fixWithRun({ draft, brand, brandId, draftId, input, passages, mor
...input.textRules,
todayText(),
);
+ // The people and quotes it may name: the same words, its data's and the authors' (CE-044).
+ const people = knownPeople(
+ htmlText(draft.body_html),
+ draft.title,
+ brand.name,
+ brand.industry,
+ brand.description,
+ ...input.rules.flatMap((r) => [r.name, r.detail]),
+ ...input.textRules,
+ ...numbered.map(sourceWords),
+ ...(await authorWords(supabaseAdmin, brand.organization_id)),
+ );
// Where it may link: the brand's pages and the draft's sources (A).
const targets = await readLinkTargets({
brandId,
@@ -729,7 +751,7 @@ async function fixWithRun({ draft, brand, brandId, draftId, input, passages, mor
lockedKeys,
ruleKeys,
linkAllowList(targets),
- { numbered, known },
+ { numbered, known, people },
);
// Stopped meanwhile: nothing it wrote is kept.
await stopIfStopped(run);
diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.js
index 03d40a396..729a20019 100644
--- a/server/src/lib/content-agent.test.js
+++ b/server/src/lib/content-agent.test.js
@@ -76,6 +76,7 @@ const {
} = await import('./content-agent.js');
const { linkKey, NO_LINK_TARGETS } = await import('./content-links.js');
const { knownNumbers, numberSources } = await import('./content-sources.js');
+const { NAME_PLACEHOLDER, QUOTE_PLACEHOLDER, knownPeople } = await import('./content-people.js');
const { PlanLimitError } = await import('./plan-guard.js');
// The real hold: self-hosted here, so it reserves nothing and writes each charge.
const realGuard = await vi.importActual('./plan-guard.js');
@@ -336,6 +337,71 @@ describe('keepSuggestions', () => {
* line ("What is LLM SEO? LLM SEO optimizes…", "Frequently Asked
* QuestionsWhat is an AEO tracker?…"): it now goes after the heading.
*/
+describe('no invented people or quotes (CE-044)', () => {
+ const passages = draftPassages(BODY);
+ const ruleKeys = new Set(['dated', 'faq']);
+ const people = knownPeople(
+ ...passages.map((p) => p.text),
+ 'Ada Lovelace',
+ '“Brew it slow and taste it twice,” Ada Lovelace says.',
+ );
+
+ it('keeps a person and a quote it was given, and puts others as placeholders', () => {
+ const kept = keepSuggestions(
+ [
+ suggestion({
+ before: 'good coffee and hot water',
+ after: 'good coffee and hot water, as Ada Lovelace says',
+ sources: [],
+ marks: [],
+ }),
+ suggestion({
+ kind: 'insert',
+ passage: 3,
+ before: '',
+ after:
+ '<p>“We doubled our sales in a single quarter with it,” said Marco Rossi.</p>' +
+ '<p>“Brew it slow and taste it twice,” Ada Lovelace says.</p>',
+ sources: [],
+ marks: [],
+ }),
+ ],
+ passages,
+ new Set(),
+ ruleKeys,
+ null,
+ { ...sourcing, people },
+ );
+ expect(kept.map((row) => row.after_text)).toEqual([
+ 'good coffee and hot water, as Ada Lovelace says',
+ `<p>${QUOTE_PLACEHOLDER} said ${NAME_PLACEHOLDER}.</p>` +
+ '<p>“Brew it slow and taste it twice,” Ada Lovelace says.</p>',
+ ]);
+ });
+
+ it('takes an invented testimonial out before its blockquote is cleaned away', () => {
+ const [kept] = keepSuggestions(
+ [
+ suggestion({
+ kind: 'insert',
+ passage: 3,
+ before: '',
+ after:
+ '<blockquote><p>We doubled our pipeline in a month.</p><cite>Jane Doe</cite></blockquote>',
+ sources: [],
+ marks: [],
+ }),
+ ],
+ passages,
+ new Set(),
+ ruleKeys,
+ null,
+ { ...sourcing, people },
+ );
+ expect(kept.after_text).toBe(`<p>${QUOTE_PLACEHOLDER}</p>`);
+ });
+});
+
describe('a replace on a heading', () => {
const page = draftPassages(
'<h2>What is LLM SEO</h2><p>It is new.</p><h2>FAQ</h2><p>Ask us.</p><p>Use good coffee.</p>',
@@ -580,6 +646,19 @@ describe('links to the pages and sources given', () => {
});
});
+ it('puts a name it was not given as a placeholder, its link moved with the words (CE-044)', () => {
+ const [kept] = keep({
+ after:
+ 'Marco Rossi said <a href="https://www.acme.example/pricing/">our plans</a> fit teams.',
+ });
+ const words = `${NAME_PLACEHOLDER} said our plans fit teams.`;
+ expect(kept.after_text).toBe(words);
+ const start = words.indexOf('our plans');
+ expect(kept.after_links).toEqual([
+ { start, end: start + 'our plans'.length, href: 'https://acme.example/pricing' },
+ ]);
+ });
+
it('unwraps a link to anything not listed, and drops what then changes nothing', () => {
expect(
keep({ after: 'See <a href="https://made-up.example/x">our plans</a> for teams.' }),
@@ -773,6 +852,35 @@ describe('fixDraftRules', () => {
]);
});
+ it("knows the workspace's authors, and puts any other person as a placeholder (CE-044)", async () => {
+ rows.content_authors = [{ name: 'Ada Lovelace', title: 'Head of Content', bio: null }];
+ generateObject.mockResolvedValue({
+ object: {
+ suggestions: [
+ suggestion({
+ after: 'good coffee and hot water, says Ada Lovelace',
+ sources: [],
+ marks: [],
+ }),
+ suggestion({
+ passage: 3,
+ before: 'A dripper and a filter',
+ after: 'A dripper and a filter, as Marco Rossi said',
+ sources: [],
+ marks: [],
+ }),
+ ],
+ },
+ usage: { inputTokens: 900, outputTokens: 200 },
+ });
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input({ sources: SOURCES }) });
+ const batch = writes.find((w) => w.table === 'content_draft_suggestions');
+ expect(batch.value.map((row) => row.after_text)).toEqual([
+ 'good coffee and hot water, says Ada Lovelace',
+ `A dripper and a filter, as ${NAME_PLACEHOLDER} said`,
+ ]);
+ });
+
it('numbers the brand’s prompts among those kept, first', async () => {
rows.prompts = [{ id: 'p1', text: 'What ratio for pour-over?' }];
generateObject.mockResolvedValue({ object: { suggestions: [] }, usage: {} });
diff --git a/server/src/lib/content-people.js b/server/src/lib/content-people.js
new file mode 100644
index 000000000..de68a6a36
--- /dev/null
+++ b/server/src/lib/content-people.js
@@ -0,0 +1,484 @@
+import * as cheerio from 'cheerio';
+import { logger } from './logger.js';
+
+/**
+ * No invented people or quotes (Content Editor Quality spec §9, CE-044).
+ * Every person's name and quoted sentence the Content Agent writes must be
+ * in what it was given: the page, the brand, the brief, the rules, the
+ * person's data (Sources) or Settings › Authors. One that isn't is taken
+ * out and a placeholder stands in its place, for the person to fill (a
+ * to-do in Quality). As the number guard (content-sources.js), it reads the
+ * words, never the model's say-so.
+ *
+ * A name is read where a person is plainly meant: after Dr./Prof./Mr./Ms.,
+ * next to a role ("CEO Jane Doe", "Jane Doe, head of sales", "Jane Doe
+ * (CEO)"), or with a verb only people do ("Jane Doe said", "told Jane Doe",
+ * "Ayşe Yılmaz'a göre"); a product or a company recommends, adds or
+ * describes too ("Google Ads recommends"). A name or a quote is known when
+ * what the agent was given has it as whole words ("Chen" isn't in
+ * "kitchen"). A quoted sentence is two words or more in
+ * quotation marks next to speech, four or more anywhere, or a blockquote or
+ * <q>; a term in quotes ("AI visibility") is no quote. Headings are read
+ * for quotes and speech only: their capitals aren't names.
+ */
+
+export const NAME_PLACEHOLDER = '[name of a real person]';
+export const QUOTE_PLACEHOLDER = '[quote from a real person, with its source]';
+
+/** A quote this short is a term in quotes, unless someone is said to say it. */
+const MIN_QUOTE_WORDS = 4;
+
+/**
+ * Words as compared, whole: lowercased, with what isn't a letter, a mark or
+ * a digit (quotes, apostrophes, punctuation, spaces) as one space ("O'Neil"
+ * → "o neil", the same on both sides).
+ */
+const wordsOf = (text) =>
+ String(text ?? '')
+ .toLowerCase()
+ .replace(/[^\p{L}\p{M}\p{N}]+/gu, ' ')
+ .trim();
+
+/** What the agent was given, read as one text names and quotes are looked up in. */
+export function knownPeople(...texts) {
+ return { words: ` ${wordsOf(texts.filter(Boolean).join('\n'))} ` };
+}
+
+/** Whether what the agent was given has these words, whole: "Chen" isn't in "kitchen". */
+const isKnown = (words, known) => {
+ const needle = wordsOf(words);
+ return needle.length > 0 && known.words.includes(` ${needle} `);
+};
+
+const wordCount = (text) => (String(text).match(/\p{L}[\p{L}\p{N}'’-]*/gu) ?? []).length;
+
+// A capitalised word of a name ("Ada", "McDonald", "O'Neil", "Jean-Luc"): one
+// way to read it only (the lookahead checks it has a small letter), so a
+// long word can't make the reading slow; an apostrophe only before a
+// capital ("O'Neil", not "Yılmaz'a" or "Doe's"). Initials ("J. K.
+// Rowling"), and particles between the words ("Ursula von der Leyen").
+const WORD = String.raw`\p{Lu}(?=[\p{L}'’]*\p{Ll})(?:\p{L}|['’](?=\p{Lu}))+(?:-\p{L}+)?`;
+const PARTICLE = String.raw`(?:von|van|der|den|de|del|della|di|da|du|la|le|al|bin|ibn|ten|ter)`;
+const NAME = String.raw`(?<!\p{L})(?:(?:\p{Lu}\.\s?){1,3}\s?${WORD}|${WORD}(?:\s+(?:${PARTICLE}\s+){0,3}(?:\p{Lu}\.\s+)?${WORD}){1,2})(?!\p{L})`;
+
+const ROLE_WORDS = [
+ 'ceo',
+ 'cto',
+ 'cfo',
+ 'coo',
+ 'cmo',
+ 'vp',
+ 'founder',
+ 'co-founder',
+ 'cofounder',
+ 'president',
+ 'chairman',
+ 'chairwoman',
+ 'director',
+ 'professor',
+ 'analyst',
+ 'researcher',
+ 'editor',
+ 'economist',
+ 'scientist',
+ 'consultant',
+ 'journalist',
+ 'spokesperson',
+ 'spokesman',
+ 'spokeswoman',
+ 'expert',
+ 'specialist',
+ 'strategist',
+ 'marketer',
+ 'engineer',
+ 'author',
+ 'advisor',
+ 'adviser',
+ 'coach',
+ 'designer',
+ 'developer',
+ 'writer',
+ 'owner',
+ 'officer',
+ 'executive',
+ 'manager',
+ 'lead',
+ 'chief',
+ 'head of',
+ 'vice president',
+ 'doctor',
+ 'physician',
+ 'lawyer',
+ 'attorney',
+ 'therapist',
+ 'nutritionist',
+ 'dietitian',
+ 'trainer',
+ 'chef',
+ 'barista',
+ 'gründer',
+ 'gründerin',
+ 'geschäftsführer',
+ 'geschäftsführerin',
+ 'kurucu',
+ 'uzman',
+ 'müdür',
+ 'direktör',
+];
+const ABBREVIATIONS = new Set(['ceo', 'cto', 'cfo', 'coo', 'cmo', 'vp']);
+const spaced = (role) => role.replace(/ /g, String.raw`\s+`);
+/** A role as written in a sentence: lowercase, or an abbreviation ("CEO"). */
+const ROLE_LOWER = `(?:${ROLE_WORDS.map((r) => (ABBREVIATIONS.has(r) ? r.toUpperCase() : spaced(r))).join('|')})`;
+/** A role in either case ("Head of Sales", "head of sales"). */
+const ROLE_ANY = `(?:${ROLE_WORDS.map((r) =>
+ ABBREVIATIONS.has(r) ? r.toUpperCase() : `[${r[0].toUpperCase()}${r[0]}]${spaced(r.slice(1))}`,
+).join('|')})`;
+/** Role words, to tell them from the name after them ("Founder Jane Doe"). */
+const ROLE_SET = new Set(ROLE_WORDS.filter((r) => !r.includes(' ')));
+
+/** Speech, read with a quote: whoever or whatever says it, it is a quote. */
+const SPEECH = String.raw`(?:says|said|told|tells|explains|explained|notes|noted|writes|wrote|recalls|recalled|adds|added|argues|argued|believes|warns|warned|states|stated|puts it|put it|emphasi[sz]es|emphasi[sz]ed|suggests|suggested|recommends|recommended|advises|advised|points out|pointed out|shares|shared|observes|observed|insists|insisted|admits|admitted|comments|commented|agrees|agreed|claims|claimed|predicts|predicted|describes|described|sagt|sagte|meint|erklärt|dit|explique|dice|explica|afirma|söyledi|diyor|belirtti|açıkladı)`;
+/**
+ * Verbs said of people only, read with a name that has no honorific or role
+ * ("Marco Rossi said"): the rest of SPEECH a product or a company does too
+ * ("Google Ads recommends", "Microsoft Teams adds", "Product Hunt
+ * describes"), so those read a name only with an honorific or a role.
+ */
+const PERSON_SPEECH = String.raw`(?:said|told|explained|recalled|admitted|insisted|argued|sagte|söyledi)`;
+
+/**
+ * Where a name plainly means a person: the name is the group "name", with
+ * how many words it needs; `role` cues aren't read in headings.
+ */
+const NAME_CUES = [
+ // "Dr. Jane Doe", "Dr. Doe": the honorific goes with the name.
+ {
+ re: new RegExp(
+ String.raw`(?<!\p{L})(?:Dr|Prof|Mr|Mrs|Ms|Mx)\.\s*(?<name>${WORD}(?:\s+${WORD}){0,2})(?!\p{L})`,
+ 'gu',
+ ),
+ words: 1,
+ honorific: true,
+ },
+ {
+ re: new RegExp(String.raw`(?<!\p{L})(?:Dr|Prof|Mr|Mrs|Ms|Mx)\s+(?<name>${NAME})`, 'gu'),
+ words: 2,
+ honorific: true,
+ },
+ // "CEO Jane Doe", "SEO expert Sarah Chen".
+ {
+ re: new RegExp(String.raw`(?<!\p{L})${ROLE_LOWER}\s+(?<name>${NAME})`, 'gu'),
+ words: 2,
+ role: true,
+ },
+ // "Jane Doe, head of sales", "Sarah Chen, a marketing strategist at Acme".
+ {
+ re: new RegExp(String.raw`(?<name>${NAME}),\s+(?:[\p{L}-]+\s+){0,3}${ROLE_ANY}(?!\p{L})`, 'gu'),
+ words: 2,
+ role: true,
+ },
+ // "Jane Doe (CEO of Acme)".
+ {
+ re: new RegExp(
+ String.raw`(?<name>${NAME})\s*\(\s*(?:[\p{L}-]+\s+){0,2}${ROLE_ANY}(?!\p{L})`,
+ 'gu',
+ ),
+ words: 2,
+ role: true,
+ },
+ // "Sarah, our head of growth", at a sentence's start: a single name.
+ {
+ re: new RegExp(
+ String.raw`(?<=^|[.!?]\s+|[“"]\s*)(?<name>${WORD}),\s+(?:our|the|a|an|their|its)\s+(?:[\p{L}-]+\s+){0,3}${ROLE_ANY}(?!\p{L})`,
+ 'gu',
+ ),
+ words: 1,
+ role: true,
+ },
+ // "Jane Doe said", "Anna Schmidt sagte".
+ { re: new RegExp(String.raw`(?<name>${NAME})\s+${PERSON_SPEECH}(?!\p{L})`, 'gu'), words: 2 },
+ // "said Jane Doe.", "told by Jane Doe," — a name the sentence stops at.
+ // "According to" isn't read: it goes with a tool as well ("according to
+ // Google Analytics"), and with a role a name is read all the same.
+ {
+ re: new RegExp(
+ String.raw`(?<!\p{L})${PERSON_SPEECH}(?:\s+by)?\s+(?<name>${NAME})(?=\s*(?:[.,;:!?)]|$))`,
+ 'gu',
+ ),
+ words: 2,
+ },
+ // "Ayşe Yılmaz'a göre".
+ {
+ re: new RegExp(String.raw`(?<name>${NAME})['’](?:a|e|ya|ye)\s+göre(?!\p{L})`, 'gu'),
+ words: 2,
+ },
+];
+
+/** Capitalised words that start a sentence, not a name ("The team said", "As Ada says"). */
+const NOT_NAMES = new Set(
+ (
+ 'the this that these those our your their his her its a an every each some most many one we they it ' +
+ 'as when if but and so then while because after before like unlike for in on at by with from to of ' +
+ 'also yet still now here there what why how who where which'
+ ).split(' '),
+);
+/** A name after these is a place ("In Silicon Valley, the founder…"). */
+const PLACE_BEFORE = new Set(
+ 'in at on from across near around throughout within inside outside into'.split(' '),
+);
+/** A name ending so is a publication or a body, not a person ("Search Engine Land"). */
+const NOT_PEOPLE = new Set(
+ (
+ 'research review journal land institute university times post news magazine report group inc ' +
+ 'labs media press daily weekly today central console insights foundation association council ' +
+ 'agency bureau center centre school academy college team blog'
+ ).split(' '),
+);
+
+/**
+ * A name as matched, without the sentence words, adverbs and roles before
+ * it: its start in the match and its words, or null when what is left
+ * isn't a person's name, or all or a part of it is known.
+ */
+function personIn(name, minWords, known) {
+ const words = name.split(/\s+/);
+ let start = 0;
+ while (words.length) {
+ const lower = words[0].toLowerCase();
+ if (PLACE_BEFORE.has(lower)) return null;
+ if (!NOT_NAMES.has(lower) && !ROLE_SET.has(lower) && !/^\p{L}+ly$/u.test(lower)) break;
+ start = name.indexOf(words[1] ?? '', start + words[0].length);
+ words.shift();
+ }
+ if (words.length < minWords) return null;
+ if (NOT_PEOPLE.has(words.at(-1).toLowerCase())) return null;
+ // The person's own name may be all or the end of it ("Recently Jane Doe").
+ for (let i = 0; words.length - i >= minWords; i++) {
+ if (isKnown(words.slice(i).join(' '), known)) return null;
+ }
+ return { start, name: name.slice(start) };
+}
+
+/**
+ * A quoted sentence: in curly, low-high, angle or straight quotation marks
+ * (a straight one not after a number: 27" is inches), or in curly single
+ * ones around words (not an apostrophe's).
+ */
+const QUOTES = [
+ /“([^“”]{1,800})”/g,
+ /„([^„“”]{1,800})[“”]/g,
+ /«([^«»]{1,800})»/g,
+ /(?<![\p{L}\p{N}])"([^"\n]{1,800})"(?![\p{L}\p{N}])/gu,
+ /(?<!\p{L})‘([^‘]{1,800}?)’(?!\p{L})/gu,
+];
+/** Speech right after a quote ("…,” said Jane Doe") or right before it ("Jane Doe said: “…"). */
+const SPOKEN_AFTER = new RegExp(
+ String.raw`^[,.!?]?\s*(?:${SPEECH}(?!\p{L})|${NAME}\s+${SPEECH}(?!\p{L}))`,
+ 'u',
+);
+const SPOKEN_BEFORE = new RegExp(String.raw`${SPEECH}[,:]?\s*$`, 'u');
+
+/** A placeholder the agent left, or one put in: never read again. */
+const BRACKETS = /\[[^\]]*\]/g;
+
+/**
+ * The names and quoted sentences in a text that what the agent was given
+ * doesn't have, as edits { start, end, text } (the placeholder), in order
+ * and apart. A heading's role cues aren't read: its capitals aren't names.
+ */
+export function peopleEdits(text, known, { heading = false } = {}) {
+ const s = String(text ?? '');
+ // Placeholders aren't read: blanked out, with their places kept.
+ const read = s.replace(BRACKETS, (m) => ' '.repeat(m.length));
+ const edits = [];
+ const free = (start, end) => edits.every((e) => end <= e.start || start >= e.end);
+
+ for (const pattern of QUOTES) {
+ for (const m of read.matchAll(pattern)) {
+ const end = m.index + m[0].length;
+ // The words as written, a placeholder in them too.
+ const inner = s.slice(m.index + 1, end - 1);
+ const words = wordCount(inner);
+ if (words < 2) continue;
+ const spoken =
+ SPOKEN_AFTER.test(read.slice(end, end + 80)) ||
+ SPOKEN_BEFORE.test(read.slice(Math.max(0, m.index - 40), m.index));
+ if ((words < MIN_QUOTE_WORDS && !spoken) || isKnown(inner, known)) continue;
+ if (free(m.index, end)) edits.push({ start: m.index, end, text: QUOTE_PLACEHOLDER });
+ }
+ }
+ for (const cue of NAME_CUES) {
+ if (heading && cue.role) continue;
+ for (const m of read.matchAll(cue.re)) {
+ const at = m.index + m[0].indexOf(m.groups.name);
+ const person = personIn(m.groups.name, cue.words, known);
+ if (!person) continue;
+ const start = cue.honorific ? m.index : at + person.start;
+ const end = at + person.start + person.name.length;
+ if (free(start, end)) edits.push({ start, end, text: NAME_PLACEHOLDER });
+ }
+ }
+ return edits.sort((a, b) => a.start - b.start);
+}
+
+/** A text with each unknown name and quote put as a placeholder, and the edits made. */
+export function guardPeople(text, known, options) {
+ const s = String(text ?? '');
+ const edits = peopleEdits(s, known, options);
+ let out = '';
+ let at = 0;
+ for (const e of edits) {
+ out += s.slice(at, e.start) + e.text;
+ at = e.end;
+ }
+ return { text: out + s.slice(at), edits };
+}
+
+/** Tags whose text a page's placeholders never touch: code and what isn't words. */
+const KEPT_AS_IS = new Set([
+ 'script',
+ 'style',
+ 'noscript',
+ 'template',
+ 'svg',
+ 'textarea',
+ 'code',
+ 'pre',
+]);
+/** Tags that hold a line of their own: the text in one is read together. */
+const BLOCKS = new Set(
+ (
+ 'p li ul ol dl dd dt h1 h2 h3 h4 h5 h6 blockquote figure figcaption table thead tbody tfoot tr ' +
+ 'td th caption div section article aside header footer main details summary'
+ ).split(' '),
+);
+const HEADINGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
+
+/** The text nodes under a node, in order: its words as htmlText reads them. */
+function textNodes($, el) {
+ const out = [];
+ const walk = (nodes) => {
+ for (const node of nodes) {
+ if (node.type === 'text') out.push(node);
+ else if (node.type === 'tag' && !KEPT_AS_IS.has(node.name)) walk(node.children ?? []);
+ }
+ };
+ walk($(el).contents().get());
+ return out;
+}
+
+/**
+ * An HTML page or insert with each unknown name and quote put as a
+ * placeholder, in its words only. A blockquote or <q> is a quote whatever
+ * its marks: one whose words aren't known becomes one placeholder, and an
+ * unknown name in its cite one too. Each line's text is read together,
+ * across the bold and the links in it. Returns the HTML (as it was when
+ * nothing is put) and how many were put in.
+ */
+export function guardPeopleInHtml(html, known) {
+ const $ = cheerio.load(html || '', null, false);
+ let count = 0;
+ $('blockquote, q').each((_, el) => {
+ // Its words, without the cite that says whose they are.
+ const words = textNodes($, el)
+ .filter((n) => !$(n).closest('cite, footer').length)
+ .map((n) => n.data)
+ .join(' ');
+ if (wordCount(words) >= 2 && !isKnown(words, known)) {
+ if (el.name === 'q') $(el).replaceWith(QUOTE_PLACEHOLDER);
+ else $(el).html(`<p>${QUOTE_PLACEHOLDER}</p>`);
+ count += 1;
+ return;
+ }
+ $(el)
+ .find('cite, footer')
+ .each((__, by) => {
+ const name = textNodes($, by)
+ .map((n) => n.data)
+ .join(' ');
+ const words = wordCount(name);
+ if (words && words <= 6 && !isKnown(name, known)) {
+ $(by).text(NAME_PLACEHOLDER);
+ count += 1;
+ }
+ });
+ });
+
+ // A line: its own text nodes and its inline tags', in order, read as one.
+ const line = (nodes, heading) => {
+ if (!nodes.length) return;
+ const joined = nodes.map((n) => n.data).join('');
+ const edits = peopleEdits(joined, known, { heading });
+ if (!edits.length) return;
+ count += edits.length;
+ const starts = [];
+ let at = 0;
+ for (const n of nodes) {
+ starts.push(at);
+ at += n.data.length;
+ }
+ // From the end, so the places before stay put. The placeholder goes in
+ // the node the edit starts in; the rest of its words are cut.
+ for (const e of [...edits].reverse()) {
+ nodes.forEach((n, i) => {
+ const from = starts[i];
+ const to = from + n.data.length;
+ if (to <= e.start || from >= e.end) return;
+ const head = n.data.slice(0, Math.max(0, e.start - from));
+ const tail = n.data.slice(Math.min(n.data.length, e.end - from));
+ n.data = head + (e.start >= from ? e.text : '') + tail;
+ });
+ }
+ };
+ // An inline tag's text, with any line inside it read on its own.
+ const inlineText = (el) => {
+ const out = [];
+ for (const node of $(el).contents().get()) {
+ if (node.type === 'text') out.push(node);
+ else if (node.type !== 'tag' || KEPT_AS_IS.has(node.name)) continue;
+ else if (BLOCKS.has(node.name)) walk(node, HEADINGS.has(node.name));
+ else out.push(...inlineText(node));
+ }
+ return out;
+ };
+ const walk = (el, heading) => {
+ let run = [];
+ for (const node of $(el).contents().get()) {
+ if (node.type === 'text') run.push(node);
+ else if (node.type !== 'tag' || KEPT_AS_IS.has(node.name)) continue;
+ else if (BLOCKS.has(node.name)) {
+ line(run, heading);
+ run = [];
+ walk(node, HEADINGS.has(node.name));
+ } else run.push(...inlineText(node));
+ }
+ line(run, heading);
+ };
+ walk($.root(), false);
+ if (!count) return { html, count };
+ // A link or a bold whose words all went with a name or a quote goes too.
+ $('a, strong, em, b, i, u, s, span, mark, small, sup, sub').each((_, el) => {
+ if (!$(el).text() && !$(el).find('img, br').length) $(el).remove();
+ });
+ return { html: $.html(), count };
+}
+
+/**
+ * The words of the workspace's authors (Settings › Authors): names, titles
+ * and bios a page may name them by. One that can't be read (content_authors
+ * not there yet, a failed read) is logged and reads as none: the run goes
+ * on, and an author's name it writes becomes a placeholder like any other.
+ */
+export async function authorWords(db, organizationId, log = logger) {
+ try {
+ const { data, error } = await db
+ .from('content_authors')
+ .select('name, title, bio')
+ .eq('organization_id', organizationId);
+ if (error) throw error;
+ return (data ?? []).flatMap((a) => [a.name, a.title, a.bio]).filter(Boolean);
+ } catch (err) {
+ log.warn({ err, organizationId }, '[content-people] authors not read');
+ return [];
+ }
+}
diff --git a/server/src/lib/content-people.test.js b/server/src/lib/content-people.test.js
new file mode 100644
index 000000000..54883696e
--- /dev/null
+++ b/server/src/lib/content-people.test.js
@@ -0,0 +1,309 @@
+import { describe, expect, it, vi } from 'vitest';
+import {
+ NAME_PLACEHOLDER as NAME,
+ QUOTE_PLACEHOLDER as QUOTE,
+ authorWords,
+ guardPeople,
+ guardPeopleInHtml,
+ knownPeople,
+} from './content-people.js';
+
+/**
+ * No invented people or quotes (Quality spec §9, CE-044): a name or a quoted
+ * sentence the agent was given stays; any other becomes a placeholder.
+ */
+
+const known = knownPeople(
+ 'Ada Lovelace writes about forecasting.',
+ '“Forecasts are only as good as the deals in them,” Ada Lovelace says.',
+ 'Acme Coffee',
+);
+const guard = (text) => guardPeople(text, known).text;
+
+describe('guardPeople', () => {
+ it('puts a person it was not given as a placeholder, wherever a person is plainly meant', () => {
+ expect(guard('Dr. Jane Doe tested twelve grinders.')).toBe(`${NAME} tested twelve grinders.`);
+ expect(guard('CEO Tim Cook agreed; founder Jean-Luc Picard nodded.')).toBe(
+ `CEO ${NAME} agreed; founder ${NAME} nodded.`,
+ );
+ expect(guard('Jane McDonald, our senior product manager, agrees.')).toBe(
+ `${NAME}, our senior product manager, agrees.`,
+ );
+ expect(guard('Marco Rossi said the beans matter.')).toBe(`${NAME} said the beans matter.`);
+ expect(guard('The beans matter, according to CEO Marco Rossi.')).toBe(
+ `The beans matter, according to CEO ${NAME}.`,
+ );
+ expect(guard('Çağrı Işık said hello.')).toBe(`${NAME} said hello.`);
+ });
+
+ it('keeps a person it was given, and names that are no person', () => {
+ expect(guard('According to Ada Lovelace, teams forecast weekly.')).toBe(
+ 'According to Ada Lovelace, teams forecast weekly.',
+ );
+ expect(guard('Dr. Ada Lovelace says so.')).toBe('Dr. Ada Lovelace says so.');
+ expect(guard('The team said it works.')).toBe('The team said it works.');
+ expect(guard('Clicks rose, according to Google Search Console data.')).toBe(
+ 'Clicks rose, according to Google Search Console data.',
+ );
+ expect(guard('Acme Coffee roasts on Mondays.')).toBe('Acme Coffee roasts on Mondays.');
+ });
+
+ it('puts a quoted sentence it was not given as a placeholder, and keeps one it was', () => {
+ expect(guard('“Pour-over is the best way to taste a single origin,” said Marco Rossi.')).toBe(
+ `${QUOTE} said ${NAME}.`,
+ );
+ expect(guard('She wrote "this changed how we brew every morning" last year.')).toBe(
+ `She wrote ${QUOTE} last year.`,
+ );
+ expect(guard('As Ada Lovelace says, "forecasts are only as good as the deals in them."')).toBe(
+ 'As Ada Lovelace says, "forecasts are only as good as the deals in them."',
+ );
+ // A term in quotes is no quote.
+ expect(guard('Use the “AI visibility” term with care.')).toBe(
+ 'Use the “AI visibility” term with care.',
+ );
+ });
+
+ it('leaves placeholders alone, and says where it put its own', () => {
+ expect(guard(`Read the ${QUOTE} here, from ${NAME}.`)).toBe(
+ `Read the ${QUOTE} here, from ${NAME}.`,
+ );
+ const { edits } = guardPeople('Ask Dr. Jane Doe.', known);
+ expect(edits).toEqual([{ start: 4, end: 16, text: NAME }]);
+ });
+});
+
+describe('guardPeople: what the review found', () => {
+ const people = knownPeople('Jane Doe writes about pipelines.', "Bob O'Neil");
+ const fix = (text, options) => guardPeople(text, people, options).text;
+
+ it('catches the ways a person is usually named', () => {
+ expect(fix('Sarah Chen, a marketing strategist at Acme, notes that it works.')).toBe(
+ `${NAME}, a marketing strategist at Acme, notes that it works.`,
+ );
+ expect(fix('SEO expert Sarah Chen agrees.')).toBe(`SEO expert ${NAME} agrees.`);
+ expect(fix('Marco Rossi (CEO of Acme) recommends it.')).toBe(
+ `${NAME} (CEO of Acme) recommends it.`,
+ );
+ expect(fix('Marco Rossi, Senior Product Manager at Acme, explains why.')).toBe(
+ `${NAME}, Senior Product Manager at Acme, explains why.`,
+ );
+ expect(fix('Dr. Marco Rossi emphasizes speed.')).toBe(`${NAME} emphasizes speed.`);
+ expect(fix('Sarah, our head of growth, says yes.')).toBe(
+ `${NAME}, our head of growth, says yes.`,
+ );
+ expect(fix('This was said by Ursula von der Leyen.')).toBe(`This was said by ${NAME}.`);
+ expect(fix('As J. K. Rowling said, write daily.')).toBe(`As ${NAME} said, write daily.`);
+ expect(fix("Ayşe Yılmaz'a göre bu doğru.")).toBe(`${NAME}'a göre bu doğru.`);
+ expect(fix('Geschäftsführerin Anna Schmidt sagte, es funktioniert.')).toBe(
+ `Geschäftsführerin ${NAME} sagte, es funktioniert.`,
+ );
+ expect(fix('Ayşe Yılmaz söyledi.')).toBe(`${NAME} söyledi.`);
+ expect(fix("Mary O'Hara said so.")).toBe(`${NAME} said so.`);
+ });
+
+ it('takes a short quote someone is said to say, and one in single curly quotes', () => {
+ expect(fix('“Absolutely game-changing,” said Marco Rossi.')).toBe(`${QUOTE} said ${NAME}.`);
+ expect(fix('‘We tested it for weeks and it held up,’ she said.')).toBe(`${QUOTE} she said.`);
+ // An apostrophe isn't a quote's end.
+ expect(fix('She said it’s fine.')).toBe('She said it’s fine.');
+ });
+
+ it("keeps a known author's name with the words before it", () => {
+ expect(fix('Recently Jane Doe said it.')).toBe('Recently Jane Doe said it.');
+ expect(fix('Acme Founder Jane Doe said it best.')).toBe('Acme Founder Jane Doe said it best.');
+ expect(fix("Bob O'Neil said so.")).toBe("Bob O'Neil said so.");
+ });
+
+ it('leaves headings, places, publications and inches alone', () => {
+ for (const heading of [
+ 'Content Editor Best Practices',
+ 'Lead Scoring Best Practices for Small Teams',
+ 'How Chief Marketing Officers Use AEO',
+ 'Head of Sales Playbook Template',
+ ]) {
+ expect(fix(heading, { heading: true })).toBe(heading);
+ }
+ expect(fix('In Silicon Valley, the founder of Acme spoke.')).toBe(
+ 'In Silicon Valley, the founder of Acme spoke.',
+ );
+ expect(fix('Traffic fell, according to Search Engine Land.')).toBe(
+ 'Traffic fell, according to Search Engine Land.',
+ );
+ expect(fix('Our 27" monitor is the best of the "pro" line.')).toBe(
+ 'Our 27" monitor is the best of the "pro" line.',
+ );
+ });
+
+ it('knows a name only as whole words of what it was given, not inside another word', () => {
+ const page = knownPeople(
+ 'Our kitchen opens at six. Sleep well.',
+ 'Our fleet does next-day delivery.',
+ 'See the annual report.',
+ );
+ const check = (text) => guardPeople(text, page).text;
+ expect(check('Dr. Sarah Chen runs the clinic.')).toBe(`${NAME} runs the clinic.`);
+ expect(check('Mr. Lee runs the shop.')).toBe(`${NAME} runs the shop.`);
+ expect(check('Dr. Mark Lee runs the shop.')).toBe(`${NAME} runs the shop.`);
+ expect(check('Mr. Doe agreed.')).toBe(`${NAME} agreed.`);
+ expect(check('Ann, our head of growth, set it.')).toBe(`${NAME}, our head of growth, set it.`);
+ // A name it was given stays, whatever the punctuation around it.
+ const given = knownPeople('Interview: Dr. Chen (Sarah Chen), and Mr. Lee.');
+ expect(guardPeople('Dr. Sarah Chen runs the clinic.', given).text).toBe(
+ 'Dr. Sarah Chen runs the clinic.',
+ );
+ expect(guardPeople('Mr. Lee runs the shop.', given).text).toBe('Mr. Lee runs the shop.');
+ });
+
+ it('reads a name with a bare verb only when the verb is said of people', () => {
+ // Products and companies recommend, add and describe too.
+ for (const line of [
+ 'Google Ads recommends a daily budget for new campaigns.',
+ 'Microsoft Teams adds a tab for each channel.',
+ 'Product Hunt describes it as a launch site.',
+ 'Clicks doubled, according to Google Analytics.',
+ 'Marco Rossi emphasizes speed.',
+ ]) {
+ expect(fix(line)).toBe(line);
+ }
+ // With an honorific or a role, any of them reads the name.
+ expect(fix('Dr. Sarah Chen recommends a daily budget.')).toBe(
+ `${NAME} recommends a daily budget.`,
+ );
+ expect(fix('CEO Marco Rossi adds a tab.')).toBe(`CEO ${NAME} adds a tab.`);
+ for (const verb of [
+ 'said',
+ 'told',
+ 'explained',
+ 'recalled',
+ 'admitted',
+ 'insisted',
+ 'argued',
+ ]) {
+ expect(fix(`Marco Rossi ${verb} it twice.`)).toBe(`${NAME} ${verb} it twice.`);
+ }
+ expect(fix('It was explained by Marco Rossi.')).toBe(`It was explained by ${NAME}.`);
+ });
+
+ it('reads long text quickly, whatever its words', () => {
+ const word = `A${'a'.repeat(400)}`;
+ const long = `${word} ${word} ${word} said. `.repeat(10);
+ const start = Date.now();
+ guardPeople(long, people);
+ expect(Date.now() - start).toBeLessThan(200);
+ });
+});
+
+describe('guardPeopleInHtml', () => {
+ it('reads the words only, and makes an unknown blockquote one placeholder', () => {
+ const { html, count } = guardPeopleInHtml(
+ '<blockquote><p>We doubled our sales in a <strong>single</strong> quarter.</p></blockquote>' +
+ '<p>Marco Rossi said <a href="https://acme.com/">it</a> works.</p>' +
+ '<pre>Jane Doe said "keep code as it is written here"</pre>',
+ known,
+ );
+ expect(count).toBe(2);
+ expect(html).toBe(
+ `<blockquote><p>${QUOTE}</p></blockquote>` +
+ `<p>${NAME} said <a href="https://acme.com/">it</a> works.</p>` +
+ '<pre>Jane Doe said "keep code as it is written here"</pre>',
+ );
+ });
+
+ it("keeps the page's own blockquote, and reads its cite for the name", () => {
+ const page = knownPeople(
+ 'We doubled our pipeline in a month. Then we tripled it again.',
+ 'Ada Lovelace',
+ );
+ const own =
+ '<blockquote><p>We doubled our pipeline in a month.</p><p>Then we tripled it again.</p>' +
+ '<cite>Ada Lovelace</cite></blockquote>';
+ expect(guardPeopleInHtml(own, page)).toEqual({ html: own, count: 0 });
+ expect(
+ guardPeopleInHtml(
+ '<blockquote><p>We doubled our pipeline in a month.</p><cite>Mark Twain</cite></blockquote>',
+ page,
+ ).html,
+ ).toBe(
+ `<blockquote><p>We doubled our pipeline in a month.</p><cite>${NAME}</cite></blockquote>`,
+ );
+ expect(guardPeopleInHtml('<p>As she put it, <q>we never looked back</q>.</p>', page).html).toBe(
+ `<p>As she put it, ${QUOTE}.</p>`,
+ );
+ });
+
+ it('reads a quote across the bold and the links in it, and drops what it emptied', () => {
+ expect(
+ guardPeopleInHtml(
+ '<p>“We <strong>doubled</strong> our pipeline in a month,” said Marco Rossi.</p>' +
+ '<p>“Our <a href="https://x.example/">AEO tracker</a> doubled our pipeline,” she said.</p>',
+ known,
+ ).html,
+ ).toBe(`<p>${QUOTE} said ${NAME}.</p><p>${QUOTE} she said.</p>`);
+ });
+
+ it('reads a heading for quotes and speech only', () => {
+ expect(
+ guardPeopleInHtml('<h2>Lead Scoring Best Practices</h2><p>CEO Tim Cook agrees.</p>', known)
+ .html,
+ ).toBe(`<h2>Lead Scoring Best Practices</h2><p>CEO ${NAME} agrees.</p>`);
+ });
+
+ it('returns the HTML as it was when there is nothing to put', () => {
+ const html = '<p>Ada Lovelace&nbsp;writes.</p>';
+ expect(guardPeopleInHtml(html, known)).toEqual({ html, count: 0 });
+ });
+});
+
+describe('authorWords', () => {
+ it("reads the workspace's authors: names, titles and bios", async () => {
+ const calls = [];
+ const db = {
+ from: (table) => ({
+ select: (cols) => ({
+ eq: async (col, value) => {
+ calls.push([table, cols, col, value]);
+ return {
+ data: [
+ { name: 'Ada Lovelace', title: 'Head of Content', bio: null },
+ { name: 'Bob Stone', title: null, bio: 'Roasts coffee.' },
+ ],
+ error: null,
+ };
+ },
+ }),
+ }),
+ };
+ expect(await authorWords(db, 'o1')).toEqual([
+ 'Ada Lovelace',
+ 'Head of Content',
+ 'Bob Stone',
+ 'Roasts coffee.',
+ ]);
+ expect(calls).toEqual([['content_authors', 'name, title, bio', 'organization_id', 'o1']]);
+ });
+
+ it('reads none, and fails no run, when the authors can’t be read', async () => {
+ const log = { warn: vi.fn() };
+ // content_authors not there yet (before its migration), or a failed read.
+ const missing = {
+ from: () => ({
+ select: () => ({
+ eq: async () => ({
+ data: null,
+ error: { code: '42P01', message: 'relation "public.content_authors" does not exist' },
+ }),
+ }),
+ }),
+ };
+ await expect(authorWords(missing, 'o1', log)).resolves.toEqual([]);
+ const down = {
+ from: () => {
+ throw new Error('fetch failed');
+ },
+ };
+ await expect(authorWords(down, 'o1', log)).resolves.toEqual([]);
+ expect(log.warn).toHaveBeenCalledTimes(2);
+ expect(log.warn.mock.calls[0][0]).toMatchObject({ organizationId: 'o1' });
+ });
+});
diff --git a/server/src/lib/content-writer.js b/server/src/lib/content-writer.js
index da3e29d82..ac19f398c 100644
--- a/server/src/lib/content-writer.js
+++ b/server/src/lib/content-writer.js
@@ -20,6 +20,7 @@ import {
import { contentBriefModelId, rebuildSources } from './content-brief.js';
import { linkAllowList, linkTargetsPrompt, readLinkTargets } from './content-links.js';
import { pageTypeFormat } from './content-page-types.js';
+import { authorWords, guardPeople, guardPeopleInHtml, knownPeople } from './content-people.js';
import {
SOURCES_RULE,
htmlText,
@@ -181,21 +182,27 @@ export function renderPage(page) {
const list = (items) => (items.length ? items.map((item) => `- ${item}`).join('\n') : '- none');
-/** A written page with each number its sources don't have put as a placeholder (CE-033). */
-export function sourcedPage(page, known) {
- const fix = (text) => placeholderNumbers(stripSourceIds(text), known);
+/**
+ * A written page with each number its sources don't have (CE-033), and each
+ * person's name or quote what it was given doesn't have (CE-044), put as a
+ * placeholder.
+ */
+export function sourcedPage(page, known, people = knownPeople()) {
+ const fix = (text, heading = false) =>
+ guardPeople(placeholderNumbers(stripSourceIds(text), known), people, { heading }).text;
+ const heading = (text) => fix(text, true);
return {
...page,
- title: fix(page.title),
- intro: page.intro.map(fix),
+ title: heading(page.title),
+ intro: page.intro.map((text) => fix(text)),
sections: page.sections.map((section) => ({
...section,
- heading: fix(section.heading),
- paragraphs: section.paragraphs.map(fix),
- bullets: section.bullets.map(fix),
+ heading: heading(section.heading),
+ paragraphs: section.paragraphs.map((text) => fix(text)),
+ bullets: section.bullets.map((text) => fix(text)),
})),
- faqHeading: fix(page.faqHeading),
- faq: page.faq.map((item) => ({ question: fix(item.question), answer: fix(item.answer) })),
+ faqHeading: heading(page.faqHeading),
+ faq: page.faq.map((item) => ({ question: heading(item.question), answer: fix(item.answer) })),
};
}
@@ -486,7 +493,18 @@ async function letGo(draft) {
* is put back as it was (content-rewrite.js); a rewrite that drops one, or
* leaves no page, fails the run. The draft's status doesn't change.
*/
-async function rewritePage({ draft, brand, model, input, budget, numbered, known, brief, run }) {
+async function rewritePage({
+ draft,
+ brand,
+ model,
+ input,
+ budget,
+ numbered,
+ known,
+ people,
+ brief,
+ run,
+}) {
const page = keepBlocks(draft.body_html, new Set(input.locked.map(passageKey)));
const ruleKeys = new Set(input.rules.map((r) => r.key));
// The Length rule's target; with Length off, the page keeps its length.
@@ -520,8 +538,11 @@ async function rewritePage({ draft, brand, model, input, budget, numbered, known
await advanceRun(run, 3);
if (!isPageHtml(object.html)) throw new Error('The agent answered with no page in HTML');
+ // No invented person or quote (CE-044): read in its words as written,
+ // blockquotes too, before what it kept goes back, so the page's own
+ // passages are never touched.
const cleaned = cleanPageHtml(
- object.html,
+ guardPeopleInHtml(object.html, people).html,
rewriteLinks(draft.body_html, linkAllowList(targets)),
{ h1Kept: page.h1Token !== null, originalH1: page.h1 },
);
@@ -688,6 +709,19 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
todayText(),
);
const knownInPage = new Set([...known, ...knownNumbers(...numbered.map(sourceWords))]);
+ // The people and quotes it may name: the same words, its data's and the authors' (CE-044).
+ const people = knownPeople(
+ htmlText(draft.body_html),
+ draft.title,
+ brand.name,
+ brand.industry,
+ brand.description,
+ brief,
+ ...input.rules.flatMap((r) => [r.name, r.detail]),
+ ...input.textRules,
+ ...numbered.map(sourceWords),
+ ...(await authorWords(supabaseAdmin, brand.organization_id)),
+ );
if (rewrite) {
return await rewritePage({
@@ -698,6 +732,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
budget,
numbered,
known: knownInPage,
+ people,
brief,
run,
});
@@ -717,7 +752,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
await budget.charge(usage);
await advanceRun(run, 3);
// No number without a source: a placeholder where one had none.
- const written = sourcedPage(object, knownInPage);
+ const written = sourcedPage(object, knownInPage, people);
const title = clean(written.title).slice(0, 300) || draft.title;
// Stopped meanwhile: the page stays as it was, and History with it.
await stopIfStopped(run);
@@ -784,7 +819,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
lockedKeys,
ruleKeys,
linkAllowList(targets),
- { numbered, known },
+ { numbered, known, people },
);
// Stopped meanwhile: nothing it wrote is kept.
await stopIfStopped(run);
diff --git a/server/src/lib/content-writer.test.js b/server/src/lib/content-writer.test.js
index a7d03a7be..3cd6a1bec 100644
--- a/server/src/lib/content-writer.test.js
+++ b/server/src/lib/content-writer.test.js
@@ -88,8 +88,9 @@ vi.mock('./content-agent.js', async (importOriginal) => ({
hasOpenSuggestions: (...args) => hasOpenSuggestions(...args),
}));
-const { briefText, renderPage, runDraftWriting, startDraftWriting } =
+const { briefText, renderPage, runDraftWriting, sourcedPage, startDraftWriting } =
await import('./content-writer.js');
+const { NAME_PLACEHOLDER, QUOTE_PLACEHOLDER, knownPeople } = await import('./content-people.js');
const { textLength } = await import('./content-rewrite.js');
const { passageKey } = await import('./content-agent.js');
const { PlanLimitError } = await import('./plan-guard.js');
@@ -178,6 +179,27 @@ describe('renderPage', () => {
});
});
+describe('sourcedPage', () => {
+ it('puts a person or a quote the agent was not given as a placeholder (CE-044)', () => {
+ const written = sourcedPage(
+ {
+ ...page,
+ intro: ['Dr. Jane Doe brews with Ada Lovelace every morning.'],
+ faq: [
+ {
+ question: 'Who says so?',
+ answer: '“Slow pours taste sweeter and cleaner,” says Ada Lovelace.',
+ },
+ ],
+ },
+ new Set(),
+ knownPeople('Ada Lovelace'),
+ );
+ expect(written.intro).toEqual([`${NAME_PLACEHOLDER} brews with Ada Lovelace every morning.`]);
+ expect(written.faq[0].answer).toBe(`${QUOTE_PLACEHOLDER} says Ada Lovelace.`);
+ });
+});
+
describe('briefText', () => {
it('reads the draft’s brief, else its opportunity’s', () => {
const own = briefText(
@@ -684,6 +706,24 @@ describe('Rewrite the page to these rules', () => {
expect(generateObject.mock.calls[0][0].system).toContain('Put the numbers (S1, S2…)');
});
+ it('puts a person or a quote it was not given as a placeholder, and knows the authors (CE-044)', async () => {
+ rows.content_authors = [{ name: 'Ada Lovelace', title: null, bio: null }];
+ answer(
+ '<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p>' +
+ '<p>“Pour slowly and you taste every note,” said Marco Rossi.</p>' +
+ '<p>Ada Lovelace said the bloom matters.</p>',
+ );
+ await expect(run()).resolves.toEqual({ rewritten: true });
+ const row = writes.find((w) => w.table === 'content_draft_suggestions').value;
+ expect(row.after_text).toContain(`<p>${QUOTE_PLACEHOLDER} said ${NAME_PLACEHOLDER}.</p>`);
+ expect(row.after_text).toContain('<p>Ada Lovelace said the bloom matters.</p>');
+ // What it kept is the page's own, as it was.
+ expect(row.after_text).toContain(`<p>${QUOTED}</p>`);
+ expect(row.after_text).toContain(
+ '<table><tbody><tr><td>Ratio</td><td>1:16</td></tr></tbody></table>',
+ );
+ });
+
it('keeps the page’s length when the Length rule is off', async () => {
answer('<h1>Pour-over</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Shorter.</p>');
await run({ rules: [{ key: 'faq', name: 'FAQ', detail: '' }] });