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): send the rules in effect with Rebuild outline, so the brief plans to them with the brief rules (2026) on (brief rules PR 7) (#1541)

Cihan062792ea129b
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
index 1d07fa9d6..061bf513b 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
@@ -4,6 +4,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { useTranslations } from 'next-intl';
import { toast } from 'sonner';
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
+import { rulesForBrief, useRuleDescriber } from '@/lib/content/agent-write';
import { isPageType, type ContentDraft } from '@/lib/content/drafts';
import type { SiteType } from '@/lib/content/page-types';
import { buildSourceGroups, includedCounts, type DraftSourceKind } from '@/lib/content/sources';
@@ -237,10 +238,12 @@ export function EditorSidePanel({
const [tab, setTab] = useState<PanelTab>('instructions');
const [segment, setSegment] = useState<InstructionSegment>('rules');
const [opened, setOpened] = useState<Set<PanelTarget>>(() => new Set(['instructions', 'rules']));
- const page = useRef<PageNow>({ title, html: bodyHtml, length: rules.length });
+ const describeRule = useRuleDescriber();
+ // The rules as checked; described only when a rebuild asks for them, not on every keystroke.
+ const page = useRef({ title, html: bodyHtml, length: rules.length, checks: rules.rules });
const sourcesNow = useRef(reads.sources);
useEffect(() => {
- page.current = { title, html: bodyHtml, length: rules.length };
+ page.current = { title, html: bodyHtml, length: rules.length, checks: rules.rules };
sourcesNow.current = reads.sources;
});
@@ -308,7 +311,10 @@ export function EditorSidePanel({
const { rebuilding, rebuild } = useBriefRebuild({
draft,
- pageNow: () => page.current,
+ pageNow: (): PageNow => {
+ const { checks, ...now } = page.current;
+ return { ...now, rules: rulesForBrief(checks, describeRule) };
+ },
sourcesNow: () => sourcesNow.current,
onSourcesRead: (read) => reads.setSources(read),
onRebuilt: (next) => {
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.test.tsx
index 8591fce19..31cc59b38 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.test.tsx
@@ -45,12 +45,13 @@ const draft = { id: 'd1', brandId: 'b1', sourceUrl: null } as unknown as Content
const onSourcesRead = vi.fn();
const onRebuilt = vi.fn();
let inHand: SourcesRead | null;
+let rulesNow: { key: string; name: string; detail: string }[] | undefined;
/** The hook behind buttons, as the side panel drives it. */
function Harness() {
const { rebuild } = useBriefRebuild({
draft,
- pageNow: () => ({ title: 'T', html: '<h1>T</h1>', length: 3000 }),
+ pageNow: () => ({ title: 'T', html: '<h1>T</h1>', length: 3000, rules: rulesNow }),
sourcesNow: () => inHand,
onSourcesRead,
onRebuilt,
@@ -78,6 +79,7 @@ beforeEach(async () => {
getDraftSources.mockResolvedValue(SOURCES);
rebuildDraftBrief.mockResolvedValue({ brief: { builtFrom: null } });
inHand = { ...SOURCES, failed: false };
+ rulesNow = undefined;
container = document.createElement('div');
root = createRoot(container);
await act(async () =>
@@ -99,6 +101,15 @@ describe('useBriefRebuild', () => {
expect(onRebuilt).toHaveBeenCalled();
});
+ it('sends the rules in effect beside the sources, so the brief plans to them', async () => {
+ rulesNow = [{ key: 'h2Answer', name: 'Answer under each H2', detail: 'aim: 60 words' }];
+ await press('rebuild');
+ const [, , sources, rules] = rebuildDraftBrief.mock.calls[0];
+ expect(sources).toMatchObject({ length: 3000 });
+ expect(sources).not.toHaveProperty('rules');
+ expect(rules).toEqual(rulesNow);
+ });
+
it('reads the sources once after adding prompts, and hands that read to the editor', async () => {
await press('lost');
expect(addDraftPrompts).toHaveBeenCalledWith('d1', ['p1']);
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.ts
index 70bcb3181..90ca38b7b 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.ts
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-brief-rebuild.ts
@@ -5,6 +5,7 @@ import { useTranslations } from 'next-intl';
import { toast } from 'sonner';
import { addDraftPrompts, getDraftSources, rebuildDraftBrief } from '@/lib/actions/content-drafts';
import { builtFromCount, rebuildSources, type DraftBrief } from '@/lib/content/brief';
+import type { AgentRule } from '@/lib/content/agent-write';
import type { ContentDraft } from '@/lib/content/drafts';
import type { DraftSourceChoice, DraftSourcesData } from '@/lib/content/sources';
import type { SourcesRead } from './_use-draft-reads';
@@ -15,6 +16,8 @@ export interface PageNow {
html: string;
/** The length the rules ask for. */
length: number;
+ /** The rules in effect, as the agent reads them: the brief plans to them. */
+ rules?: AgentRule[];
}
/**
@@ -79,6 +82,7 @@ export function useBriefRebuild({
html: page.html,
length: page.length,
}),
+ page.rules,
);
if ('error' in result) {
// Stopped from the agent card (CE-027): no failure, nothing changed.
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.tsx
index 541d57eaf..7026e299c 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
@@ -3049,14 +3049,26 @@ describe('the side panel', () => {
await pickTab('Sources');
await act(async () => button('Rebuild with selection')!.click());
- expect(rebuildDraftBrief).toHaveBeenCalledWith('d1', 'b1', {
- promptIds: [],
- fanouts: [{ query: 'pipeline forecasting', platforms: ['copilot-web'] }],
- pages: [],
- queries: [],
- headings: [],
- length: 3000,
- });
+ expect(rebuildDraftBrief).toHaveBeenCalledWith(
+ 'd1',
+ 'b1',
+ {
+ promptIds: [],
+ fanouts: [{ query: 'pipeline forecasting', platforms: ['copilot-web'] }],
+ pages: [],
+ queries: [],
+ headings: [],
+ length: 3000,
+ },
+ // The rules in effect, as the agent reads them.
+ expect.any(Array),
+ );
+ const sent = (rebuildDraftBrief.mock.calls[0][3] as { key: string; name: string }[]).map(
+ (rule) => rule.key,
+ );
+ expect(sent).toContain('length');
+ // Not the editor's own (one H1), nor the brief's own keywords: a rebuild writes them anew.
+ for (const key of ['oneH1', 'feedMatch', 'keywords']) expect(sent).not.toContain(key);
expect(toast.success).toHaveBeenCalledWith('Brief rebuilt from 1 source');
expect(selectedTab()).toBe('Instructions');
expect(selectedSegment()).toBe('Brief');
diff --git a/web/src/lib/actions/content-drafts-brief.test.ts b/web/src/lib/actions/content-drafts-brief.test.ts
index a6aac5325..6db2d7dca 100644
--- a/web/src/lib/actions/content-drafts-brief.test.ts
+++ b/web/src/lib/actions/content-drafts-brief.test.ts
@@ -186,6 +186,15 @@ describe('rebuildDraftBrief', () => {
expect(JSON.parse(init.body)).toEqual({ brandId: BRAND, sources });
});
+ it('sends the rules in effect beside the sources, and none when there are none', async () => {
+ fetchMock.mockResolvedValue({ ok: true, json: async () => ({ brief }) });
+ const rules = [{ key: 'h2Answer', name: 'Answer under each H2', detail: 'aim: 60 words' }];
+ await rebuildDraftBrief(DRAFT, BRAND, sources, rules);
+ expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ brandId: BRAND, sources, rules });
+ await rebuildDraftBrief(DRAFT, BRAND, sources, []);
+ expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ brandId: BRAND, sources });
+ });
+
it('names why it didn’t, and anything unexpected as failed', async () => {
for (const [body, ok, error] of [
[{ code: 'ai_budget' }, false, 'ai_budget'],
diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.ts
index bf101966e..ec98a0925 100644
--- a/web/src/lib/actions/content-drafts.ts
+++ b/web/src/lib/actions/content-drafts.ts
@@ -93,7 +93,7 @@ import {
} from '@/lib/content/history';
import { RUN_STALE_MS } from '@/lib/content/agent-runs';
import { HANDOFF_KEYS } from '@/lib/content/hand-off';
-import type { AgentWriteRequest } from '@/lib/content/agent-write';
+import type { AgentRule, AgentWriteRequest } from '@/lib/content/agent-write';
import {
actionPages,
completeDraftTasks,
@@ -2238,6 +2238,8 @@ export async function rebuildDraftBrief(
id: string,
brandId: string,
sources: RebuildSources,
+ /** The rules in effect, as the agent reads them: the brief plans to them (brief rules, 2026). */
+ rules?: AgentRule[],
): Promise<{ brief: DraftBrief } | { error: BriefRebuildError }> {
if (!draftId.safeParse(id).success || !draftId.safeParse(brandId).success) {
return { error: 'failed' };
@@ -2254,7 +2256,7 @@ export async function rebuildDraftBrief(
'Content-Type': 'application/json',
Authorization: `Bearer ${session.access_token}`,
},
- body: JSON.stringify({ brandId, sources }),
+ body: JSON.stringify({ brandId, sources, ...(rules?.length ? { rules } : {}) }),
});
const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;
if (!res.ok) {
diff --git a/web/src/lib/content/agent-write.test.ts b/web/src/lib/content/agent-write.test.ts
index 155586a4d..94b2ea8f2 100644
--- a/web/src/lib/content/agent-write.test.ts
+++ b/web/src/lib/content/agent-write.test.ts
@@ -17,7 +17,7 @@ vi.mock('@/lib/actions/content-drafts', () => ({
getCitedPassages: (...args: unknown[]) => getCitedPassages(...args),
}));
-const { agentWriteRequest } = await import('./agent-write');
+const { agentWriteRequest, rulesForAgent, rulesForBrief } = await import('./agent-write');
const { fanoutsToUse } = await import('./rules');
const data: DraftSourcesData = {
@@ -158,3 +158,55 @@ describe('agentWriteRequest', () => {
expect(request.rules.length).toBeGreaterThan(0);
});
});
+
+describe('rulesForAgent', () => {
+ it('gives the rules in effect as the agent reads them: on, the agent’s, a fan-out rule with its fan-outs', () => {
+ const rules = activeRules(ruleSet('product_page'), NO_DRAFT_RULES);
+ const summary = rules.find((rule) => rule.key === 'summary')!;
+ const feed = rules.find((rule) => rule.key === 'feedMatch')!;
+ const off = { ...rules.find((rule) => rule.key === 'faq')!, on: false };
+ const fanout = rules.find((rule) => rule.key === 'fanoutH2')!;
+ const status = { state: 'bad', code: 'missing', fanouts: ['espresso under 500'] } as never;
+ const seen: unknown[] = [];
+ const read = rulesForAgent(
+ [
+ { rule: summary, status: { state: 'ok' } as never },
+ { rule: feed },
+ { rule: off },
+ { rule: fanout, status },
+ ],
+ (rule, given) => {
+ seen.push([rule.key, given]);
+ return { name: `name:${rule.key}`, detail: rule.current };
+ },
+ );
+ expect(read.map((rule) => rule.key)).toEqual(['summary', 'fanoutH2']);
+ // Only a fan-out rule is described with its status (the fan-outs it still needs).
+ expect(seen).toEqual([
+ ['summary', undefined],
+ ['fanoutH2', status],
+ ]);
+ });
+});
+
+describe('rulesForBrief', () => {
+ it('leaves out the Keywords rule while it holds the brief’s own keywords, not one the person set', () => {
+ const describe = (rule: ActiveRule) => ({ name: rule.key, detail: rule.current });
+ const fromBrief = activeRules(ruleSet('guide', { keywords: ['old', 'words'] }), NO_DRAFT_RULES);
+ expect(
+ rulesForBrief(
+ fromBrief.map((rule) => ({ rule })),
+ describe,
+ ).map((r) => r.key),
+ ).not.toContain('keywords');
+ const set = activeRules(ruleSet('guide', { keywords: ['old', 'words'] }), {
+ overrides: { keywords: { value: 'espresso, grinder' } },
+ });
+ expect(
+ rulesForBrief(
+ set.map((rule) => ({ rule })),
+ describe,
+ ).find((r) => r.key === 'keywords'),
+ ).toEqual({ key: 'keywords', name: 'keywords', detail: 'espresso, grinder' });
+ });
+});
diff --git a/web/src/lib/content/agent-write.ts b/web/src/lib/content/agent-write.ts
index 6c8445980..5ba3d6386 100644
--- a/web/src/lib/content/agent-write.ts
+++ b/web/src/lib/content/agent-write.ts
@@ -18,6 +18,13 @@ import {
type RuleStatus,
} from './rules';
+/** A rule as the agent reads it: its key, its name and what it asks. */
+export interface AgentRule {
+ key: string;
+ name: string;
+ detail: string;
+}
+
/**
* What the Content Agent is given when Start hands it a draft (CE-015), as
* the API's content-writer.js reads it: the sources the Sources tab keeps
@@ -28,7 +35,7 @@ import {
*/
export interface AgentWriteRequest {
sources: RebuildSources;
- rules: { key: string; name: string; detail: string }[];
+ rules: AgentRule[];
textRules: string[];
locked: string[];
/** How many rules the editor counts ("met of total"): the run's "Reading N rules" says the same. */
@@ -72,6 +79,40 @@ export function useRuleDescriber(): RuleDescriber {
/** The rules whose status names the fan-outs to use. */
const FANOUT_RULES = new Set(['fanoutH2', 'fanoutFaq']);
+/**
+ * The rules in effect as the editor checks them, as the agent reads them: on,
+ * the agent's to follow, described, a fan-out rule with the fan-outs it still
+ * needs. Rebuild outline sends them, so the brief plans to them (brief rules,
+ * 2026; the API reads them with CONTENT_BRIEF_RULES on).
+ */
+export function rulesForAgent(
+ checked: { rule: ActiveRule; status?: RuleStatus }[],
+ describe: RuleDescriber,
+): AgentRule[] {
+ return checked
+ .filter(({ rule }) => rule.on && rule.check !== 'feed' && !NOT_FOR_AGENT.has(rule.key))
+ .slice(0, MAX_AGENT_RULES)
+ .map(({ rule, status }) => ({
+ key: rule.key,
+ ...describe(rule, FANOUT_RULES.has(rule.key) ? status : undefined),
+ }));
+}
+
+/**
+ * The rules a rebuilt brief plans to: the agent's (rulesForAgent), without
+ * the Keywords rule while it holds the brief's own keywords — a rebuild
+ * writes them anew from the sources — unless the person set it.
+ */
+export function rulesForBrief(
+ checked: { rule: ActiveRule; status?: RuleStatus }[],
+ describe: RuleDescriber,
+): AgentRule[] {
+ return rulesForAgent(
+ checked.filter(({ rule }) => rule.key !== 'keywords' || rule.changed),
+ describe,
+ );
+}
+
/** The page's headings and paragraphs, as the editor's passages. */
function pagePassages(html: string): string[] {
const doc = new DOMParser().parseFromString(html, 'text/html');