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
fix(content): check the agent's changes and a page rewrite against the rules before they're accepted (#1519)
Cihan38ca76bd1b73
diff --git a/web/messages/en.json b/web/messages/en.jsonindex 551ea0804..7a3b9cbe5 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3803,6 +3803,7 @@}},"agentCard": {+ "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.","placeholders": "{count, plural, one {# placeholder} other {# placeholders}} to fill",@@ -3913,6 +3914,7 @@"fixed": "{count, plural, one {Content Agent suggested # change} other {Content Agent suggested # changes}} · review them in the page","fixedMax": "Content Agent suggested {count} changes, the most one run makes · review them, then run it again for the rules still open","nothingToFix": "Content Agent found nothing it could change without new facts.",+ "breaks": "Would break {names}","nothingToFixLocked": "Content Agent's changes were all in passages AI answers quote, which it keeps. Unlock the ones you're fine to change (Edit anyway), or change them yourself.","notForAgent": "Content Agent doesn't change heading levels: make the extra H1s H2s in the toolbar.","notForAgentFeed": "Content Agent doesn't change a price, GTIN or stock: check which is right, the page or your product feed, and fix that one.",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.tsxindex a175043e3..6d774db12 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@@ -316,6 +316,34 @@ describe('AgentCard', () => {expect(container.textContent).not.toContain('All suggestions reviewed');});+ it('reads the gain from the page with the changes in, and names a rule they would break', async () => {+ await render([suggestion()], false, {+ rules: { ...rules, after: { now: 9, met: 8, breaks: 'Internal links' } },+ });+ expect(container.textContent).toContain('Rules 9 → 8 of 14 · would break Internal links');+ // Counted from the page as it is now, not a read a beat behind it.+ await render([suggestion()], false, {+ rules: { ...rules, met: 7, after: { now: 9, met: 10, breaks: null } },+ });+ expect(container.textContent).toContain('Rules 9 → 10 of 14');+ // A whole page rewritten: its count beside its characters.+ await render(+ [+ suggestion({+ kind: 'page',+ beforeHtml: '<h1>Old</h1><p>Long.</p>',+ afterText: '<h1>New</h1><p>Short.</p>',+ reason: 'answered each heading first',+ }),+ ],+ false,+ { rules: { ...rules, after: { now: 9, met: 8, breaks: 'FAQ' } } },+ );+ expect(container.textContent).toContain(+ 'answered each heading first · Rules 9 → 8 of 14 · would break FAQ',+ );+ });+it('says it is working, with nothing to press', async () => {await render([], true);const card = container.querySelector('[role="region"]')!;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.tsxindex 7546fe275..564c04be0 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@@ -40,6 +40,13 @@ export interface CardRules {total: number;/** The keys of the rules on that the page doesn't meet now. */open: Set<string>;+ /**+ * What accepting the open result would do, checked on a copy of the page+ * (editor test 2026-10-25): the rules met then, and those met now it would+ * break, named. Without it, the gain is read from what each change says+ * it fixes (rulesAfter).+ */+ after?: { now: number; met: number; breaks: string | null };}/**@@ -299,7 +306,16 @@ export function AgentCard({const { open, accepted } = suggestionCounts(suggestions);const openOnes = suggestions.filter((s) => s.status === 'open');const action = suggestions.find((s) => s.actionId && s.actionNo !== null);- const after = rulesAfter(suggestions, rules);+ // From the page as it is now when checked so: never a count a beat behind it.+ const from = rules.after?.now ?? rules.met;+ const after = rules.after?.met ?? rulesAfter(suggestions, rules);+ // Rules met now that accepting it would break: said, never left to find after.+ const breaks = rules.after?.breaks ? (+ <span className="text-amber-700 dark:text-amber-400">+ {' · '}+ {t('breaks', { names: rules.after.breaks })}+ </span>+ ) : null;const stillOpen = Math.max(0, rules.total - rules.met);// Run Agent shows once nothing is left to decide; why it can't run is// written in the card, never only on hover (as the Rules card does, CE-028).@@ -412,6 +428,22 @@ export function AgentCard({page.reason)}{pageLeft > 0 && ` · ${t('placeholders', { count: pageLeft })}`}+ {rules.after && rules.after.met !== from && (+ <>+ {' · '}+ <span+ className={cn(+ 'font-medium',+ rules.after.met > from+ ? 'text-emerald-700 dark:text-emerald-400'+ : 'text-amber-700 dark:text-amber-400',+ )}+ >+ {t('gain', { met: from, after: rules.after.met, total: rules.total })}+ </span>+ </>+ )}+ {breaks}{builtFrom}</>) : page.status === 'accepted' && stillOpen > 0 ? (@@ -435,14 +467,23 @@ export function AgentCard({{sentence(openOnes[0]?.reason ?? '', locale)}{openOnes.length > 1 && ` · ${t('more', { count: openOnes.length - 1 })}`}{batchLeft > 0 && ` · ${t('placeholders', { count: batchLeft })}`}- {after > rules.met && (+ {after > from && (<>{' · '}<span className="font-medium text-emerald-700 dark:text-emerald-400">- {t('gain', { met: rules.met, after, total: rules.total })}+ {t('gain', { met: from, after, total: rules.total })}+ </span>+ </>+ )}+ {after < from && (+ <>+ {' · '}+ <span className="font-medium text-amber-700 dark:text-amber-400">+ {t('gain', { met: from, after, total: rules.total })}</span></>)}+ {breaks}{builtFrom}{action && (<>diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsxindex 121641f05..33ea5e960 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx@@ -3,6 +3,7 @@import { useCallback, useEffect, useMemo, useRef, useState } from 'react';import { useFormatter, useTranslations } from 'next-intl';import { toast } from 'sonner';+import { getHTMLFromFragment } from '@tiptap/core';import { EditorContent, useEditor, type Editor } from '@tiptap/react';import type { Node as PmNode } from '@tiptap/pm/model';import { TextSelection, type Transaction } from '@tiptap/pm/state';@@ -175,6 +176,8 @@ import {keptOutOfReach,lengthOutOfReach,readPage,+ pageTextLength,+ rulesMet,type ActiveRule,type LastRewrite,type RewriteReach,@@ -1663,6 +1666,7 @@ export function DraftEditor({const suggestionLabels = useMemo<SuggestionLabels>(() => ({agent: t('suggestions.agent'),+ breaks: (names: string) => t('suggestions.breaks', { names }),accept: t('suggestions.accept'),reject: t('suggestions.reject'),accepted: t('suggestions.accepted'),@@ -1702,16 +1706,85 @@ export function DraftEditor({.catch((err) => console.error('Failed to read the agent suggestions:', err));}, [editor, draft.id]);+ // What the agent's open result would do to the rules (editor test+ // 2026-10-25: a fix run turned 11 of 15 met into 10, a rewrite 4 open into+ // 5): each change, all of them (as Accept all takes them, one after the+ // other), or a whole page rewritten, checked as the page is, against the+ // page as it is now (read here, not the rules' deferred read, so a change+ // just accepted isn't blamed on the rest). A rule met now that it would+ // break is said.+ const ruleEffects = useMemo(() => {+ if (!editor || editor.isDestroyed) return null;+ const open = suggestions.filter((s) => s.status === 'open' && s.kind !== 'page');+ const page = suggestions.find((s) => s.kind === 'page' && s.status === 'open') ?? null;+ if (!open.length && !page) return null;+ const check = (html: string, characters: number) => rules.checkHtml(html, characters);+ const ofDoc = (doc: PmNode) =>+ check(+ getHTMLFromFragment(doc.content, editor.schema),+ (editor.storage.characterCount.characters({ node: doc }) as number | undefined) ?? 0,+ );+ const now = ofDoc(editor.state.doc);+ const metNow = new Set(+ now+ .filter(({ rule, status }) => rule.on && status.state === 'ok')+ .map(({ rule }) => rule.key),+ );+ const effect = (after: ReturnType<typeof check>, leaveOut: string[] = []) => ({+ now: rulesMet(now).met,+ met: rulesMet(after).met,+ broken: after+ .filter(+ ({ rule, status }) =>+ metNow.has(rule.key) && status.state === 'bad' && !leaveOut.includes(rule.key),+ )+ .map(({ rule }) => rule.key),+ });+ // A whole page rewritten: its Length is its own line on the card (read+ // the same way, pageTextLength), so it isn't named again as broken.+ if (page) {+ return {+ each: new Map<string, string[]>(),+ all: effect(check(page.afterText, pageTextLength(page.afterText)), ['length']),+ };+ }+ const each = new Map<string, string[]>();+ for (const s of open) {+ const one = editor.state.tr;+ if (acceptInto(one, editor.state, s, suggestions))+ each.set(s.id, effect(ofDoc(one.doc)).broken);+ }+ const all = redoDecision(+ editor.state,+ suggestions,+ open.map((s) => s.id),+ 'accept',+ ).tr;+ return { each, all: effect(ofDoc(all.doc)) };+ // pageHtml: the page as it changes (the editor's state is read through it).+ }, [editor, suggestions, rules.checkHtml, pageHtml]); // eslint-disable-line react-hooks/exhaustive-deps+ const ruleNames = (keys: string[]) =>+ format.list(+ keys.map((key) => tRules(`names.${key}`)),+ { type: 'conjunction' },+ );+// The page shows them as they are now.useEffect(() => {suggestionsNow.current = suggestions;if (!editor || editor.isDestroyed) return;- // A whole page (Rewrite the whole page) has its own bar, not a card.- const cards = suggestions.filter((s) => s.kind !== 'page');+ // A whole page (Rewrite the whole page) has its own bar, not a card; each+ // card says which rules met now it would break.+ const cards = suggestions+ .filter((s) => s.kind !== 'page')+ .map((s) => {+ const broken = ruleEffects?.each.get(s.id);+ return broken?.length ? { ...s, breaks: ruleNames(broken) } : s;+ });editor.view.dispatch(editor.state.tr.setMeta(suggestionsKey, { suggestions: cards, labels: suggestionLabels }),);- }, [editor, suggestions, suggestionLabels]);+ }, [editor, suggestions, suggestionLabels, ruleEffects]); // eslint-disable-line react-hooks/exhaustive-deps// While Fix open rules runs, a dashed card in the page where its change// will most likely land, from which of the kept data (CE-027/035).@@ -1896,6 +1969,7 @@ export function DraftEditor({[heldKey],);// The rules as the agent card counts its gain: met now, and the ones still open.+ const effect = ruleEffects?.all ?? null;const cardRules = useMemo(() => ({met: rules.met,@@ -1905,8 +1979,17 @@ export function DraftEditor({.filter(({ rule, status }) => rule.on && status.state === 'bad').map(({ rule }) => rule.key),),+ ...(effect+ ? {+ after: {+ now: effect.now,+ met: effect.met,+ breaks: effect.broken.length ? ruleNames(effect.broken) : null,+ },+ }+ : {}),}),- [rules.met, rules.total, rules.rules],+ [rules.met, rules.total, rules.rules, effect], // eslint-disable-line react-hooks/exhaustive-deps);/**diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsxindex 0c7cb8713..4944075f4 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx@@ -52,6 +52,7 @@ function state(...rulesMet(checked),length: 3000,lengthConflict: null,+ checkHtml: () => checked,change,pickSet,addTextRules,diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_suggestions.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_suggestions.tsindex 2dd004cfc..1d09873eb 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_suggestions.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_suggestions.ts@@ -36,6 +36,8 @@ export type SuggestionDecision = 'accept' | 'reject' | 'undo' | 'restore';/** The words on the cards, from the editor's messages. */export interface SuggestionLabels {agent: string;+ /** "Would break …": the rules met now it would break, named (editor test 2026-10-25). */+ breaks?: (names: string) => string;accept: string;reject: string;accepted: string;@@ -516,10 +518,20 @@ function button(return node;}-/** The card's top line: the agent and why, then its buttons or what was decided. */-function head(s: DraftSuggestion, labels: SuggestionLabels, controls: HTMLElement[]): HTMLElement {+/**+ * The card's top line: the agent and why, then its buttons or what was+ * decided. An open one that would break a rule met now says which.+ */+function head(+ s: DraftSuggestion & { breaks?: string },+ labels: SuggestionLabels,+ controls: HTMLElement[],+): HTMLElement {const line = el('div', 'flex items-center gap-2 text-xs text-muted-foreground');line.append(el('span', 'min-w-0 truncate', `${labels.agent} · ${s.reason}`));+ if (s.status === 'open' && s.breaks && labels.breaks) {+ line.append(el('span', 'shrink-0 text-amber-700 dark:text-amber-400', labels.breaks(s.breaks)));+ }const right = el('span', 'ml-auto flex shrink-0 items-center gap-1.5');right.append(...controls);line.append(right);@@ -659,7 +671,7 @@ export const AgentSuggestions = Extension.create<{const widget = (pos: number) =>Decoration.widget(pos, () => card(s, labels, blocked, decide, open), {// Drawn again once the sources are read, for their hover details.- key: `${s.id}:${s.status}:${blocked ?? ''}:${s.reason}:${labels.sourceTip ? 1 : 0}`,+ key: `${s.id}:${s.status}:${blocked ?? ''}:${s.reason}:${(s as { breaks?: string }).breaks ?? ''}:${labels.sourceTip ? 1 : 0}`,side: -1,ignoreSelection: true,stopEvent: () => true,diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.tsindex eef87057c..b4ac92d68 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts@@ -1,6 +1,6 @@'use client';-import { useDeferredValue, useEffect, useMemo, useRef, useState } from 'react';+import { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState } from 'react';import { useTranslations } from 'next-intl';import { toast } from 'sonner';import { getMyRuleSets, saveDraftRules, saveMyRuleSet } from '@/lib/actions/content-drafts';@@ -51,6 +51,11 @@ export interface DraftRulesState {length: number;/** The sections and questions the rules ask for take more than Length allows. */lengthConflict: { need: number; max: number } | null;+ /**+ * The rules checked against another page (an agent's change applied to a+ * copy): what it would meet and break (editor test 2026-10-25).+ */+ checkHtml: (html: string, characters: number) => { rule: ActiveRule; status: RuleStatus }[];/** Turns a rule on or off, or gives it a value, for this draft. */change: (key: string, change: { on?: boolean; value?: string }) => void;/** Loads a set into the draft: a type's Ansvisor default, or the workspace's My rules for it. */@@ -185,6 +190,13 @@ export function useDraftRules({[rules, page, ctx],);const { met, total } = rulesMet(checked);+ const checkHtml = useCallback(+ (other: string, count: number) => {+ const read = readPage(other, count);+ return rules.map((rule) => ({ rule, status: checkRule(rule, read, ctx) }));+ },+ [rules, ctx],+ );const conflict = useMemo(() => lengthConflict(checked, ctx.fanouts.length),[checked, ctx.fanouts.length],@@ -246,6 +258,7 @@ export function useDraftRules({total,length: ruleLength(rules, setType),lengthConflict: conflict,+ checkHtml,change: (key, change) =>edited({overrides: { ...effective.overrides, [key]: { ...effective.overrides[key], ...change } },diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsxindex 15304379b..effc92f5d 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx@@ -5846,6 +5846,22 @@ describe('the side panel', () => {(b) => b.textContent === label,)!;+ it('says when a change would break a rule the page meets now, on it and on the card', async () => {+ getContentDraft.mockResolvedValue(traced);+ // Its new words make the page's one sentence run past 150 characters.+ getDraftSuggestions.mockResolvedValue([+ { ...agentSuggestion, afterText: `look ${'very '.repeat(35)}closely at` },+ ]);+ await renderPage();+ await act(async () => {});+ expect(container.querySelector('.ProseMirror')!.textContent).toContain(+ 'Would break Max sentence length',+ );+ expect(+ container.querySelector('[role="region"][aria-label="Agent suggestions"]')!.textContent,+ ).toContain('would break Max sentence length');+ });+it('says the month’s AI usage is used up before anything else, and runs nothing', async () => {aiUsage.usage = usageAt(100);getContentDraft.mockResolvedValue(traced);