Skip to content

feat(content): tell the Content Agent which fan-outs to use - #1373

Merged
gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-fanouts
Oct 23, 2026
Merged

gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-fanouts

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The fan-out rules now tell the Content Agent which fan-outs to use, so it can meet them.

Its base (#1369–#1372) has merged; main is merged in, so this PR now carries only its own change.

On prod, "Use top fan-outs as H2s" and "Use fan-outs in FAQ questions" stayed open after every agent run.

  • Cause: the agent was told only "now: 3 of 4", not which fan-outs were missing. It wrote H2s and FAQ questions of its own, and the check (carries, 75% of a fan-out's words) didn't count them.
  • A misleading label: it also called changes "fan-out answers" when it had no fan-outs at all. The Start Agent prompt's example reason invited that.
  • A question added into a question (found on prod, 2026-10-23): for fanoutFaq, the agent appended the fan-out's question to an existing FAQ question heading ("What is AEO? What is an AEO tracker?"). The editor's heading guard then refused it ("2 suggestions couldn't be applied").

Rules (web/src/lib/content/rules.ts)

  • The status names what is missing:
    • fanoutH2 gives the top-N fan-outs that no H2 carries;
    • fanoutFaq gives the most searched fan-outs that no FAQ question uses, as many as are still needed. On a page with no FAQ yet (needsFaq), it gives the top N.
  • fanoutFaq counts the different fan-outs its questions use, as fanoutH2 counts its H2s, and asks for no more than there are (of = min(N, fan-outs)). Before, with fewer fan-outs than N it could stay open with nothing left to name. Two questions using the same fan-out now count once. lengthConflict counts the same.
  • fanoutsToUse(status) quotes them for the agent: most searched first, within 300 characters. A fan-out too long for the room left is skipped, so a shorter one after it still fits.

Where the agent gets them

  • Apply rules with Agent: the rule's detail gains fan-outs to use, in their words: "…"; "…". That fits the server's 500-character detail.
  • Start Agent: agentWriteRequest checks fanoutH2 and fanoutFaq against the page and passes their status to the rule describer, which names the fan-outs the same way.
    • On an Optimize draft, the page is the existing one.
    • On a page the agent writes anew (Create, Try again), it starts from none, so every top fan-out is named.

Prompts (server). FANOUT_RULE is shared by the fix-rules and Start Agent (optimize) prompts:

  • for each named fan-out, write in its words an H2 (fanoutH2) or a question ending in "?" under the FAQ heading (fanoutFaq), not questions of its own;
  • a new FAQ question goes in as its own question: an insert after an answer's last paragraph, a heading at the FAQ questions' level and a paragraph answering it. Reword an existing question only when its answer still answers it;
  • one question per heading;
  • call a change a fan-out answer only when it uses one listed.

WRITE_SYSTEM_PROMPT (a new page, Create and Try again) gets the same instruction, shaped for its schema: a section heading for fanoutH2, an FAQ question for fanoutFaq.

One question per heading, enforced. asksMoreThanOne drops a heading replace that makes the heading ask more than one question. It applies on the server (keepSuggestions) and over the API (keepAgentSuggestions, dropped reason heading_two_questions, listed in the MCP tool description). A heading that already asked two can still change. splitReplace no longer moves a second question under the heading as if it were the answer, with or without its own answer after it. An answer that ends on a question is still moved.

No migration. The MCP path doesn't send rule statuses; it gets the one-question guard only.

Related issue

Content Agent rule gaps, item B, found by testing on prod on 2026-10-22. No issue.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Open a draft whose Sources list fan-outs, with the fan-out rules open, and run Apply rules with Agent.
  2. The request's fanoutH2 / fanoutFaq details name the missing fan-outs. The agent's H2s and FAQ questions use their words, and the rules move.
  3. Start Agent on a new page: its fan-out rules name the top fan-outs.

There are no AI keys locally, so this was checked through the request the editor builds and the prompts:

  • rules.test.ts: the missing lists, needsFaq, fanoutsToUse's budget, fewer fan-outs than questions, two questions with one fan-out, one question with two, and lengthConflict with two fan-outs;
  • agent-write.test.ts: new page vs existing page;
  • the editor's page.test.tsx: the detail sent;
  • content-agent.test.js / content-writer.test.js: FANOUT_RULE in both suggestion prompts, the fan-out line in the new-page prompt, a second question dropped (short, past the limit, answered), and a question reworded kept;
  • agent-suggestions.test.ts / suggestions.test.ts: the same over the API, and asksMoreThanOne.

Known, not changed here: an insert can still carry a heading with two questions. growsPastLimit doesn't look at inserts either.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • web yarn test and yarn build;
  • server npm run format, npm run lint and npm test.

🤖 Generated with Claude Code

cihan-gyk and others added 12 commits October 22, 2026 22:42
A replace on a heading that carried its answer ("What is X? X is …") or a
whole FAQ was flattened into the heading's line. Content Agent and the
editor API now split such a replace: the heading keeps its words and the
answer or blocks become an insert after it, kept together or not at all.
An agent change may not grow an H1 past 70 characters or another heading
past 90 (a long imported heading may still be shortened); both agent
prompts and the MCP description say a heading stays one line, and the
editor won't accept an older suggestion that would glue text into one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A page's H1 is its title. The editor now makes a second H1 an H2 however it
comes (typed, pasted, the HTML view, Add HTML or an agent suggestion),
keeping the page's own; Heading 1 is off while the page has one, and says
why on hover. A version restored from History and older drafts keep theirs,
and a new always-on "One H1 per page" rule flags them. The Content Agent is
never asked to fix it (it doesn't change heading levels) and its inserted
H1s become H2s. URL import keeps the H1 nearest the article's text, not a
logo's or a site name's, and makes the rest H2s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
…nly a real answer

Review fixes for ansvisor#1369 (gkhngyk):
- the question/answer split runs whenever the passage is a heading, also
  when blocks follow, and the answer goes first under it (inline answer
  plus a list, or <h2>Q? A.</h2><p>…);
- a heading that fits keeps a subtitle: only an answer that ends a
  sentence (. ! 。 !) is split off; past the limit the rest always goes;
- "?" ends a question only before a space; a full-width "?" with or
  without one;
- the regex has no catastrophic case on long text, and the garbled doc
  comment in agent-suggestions.ts reads again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent, count HTML blocks'

Review fixes for ansvisor#1370 (gkhngyk):
- URL import picks the title's H1 once, before the frame and the root go:
  the last H1 with words before the text's first paragraph (a byline,
  date or breadcrumb line doesn't count as one), else the first; the
  title comes from it, and the root's other H1s become H2s;
- New content keeps one H1 in a pasted page, a file or an imported page
  (oneH1Html in bring(): the first H1 with words stays, others are H2s,
  empty ones unwrapped);
- "One H1 per page" counts H1s with words inside HTML blocks too;
- an empty H1 line never counts as the page's H1 in the editor, so it
  never stays over one with words, and Heading 1 stays on beside it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e draft's sources

The agent could never meet the Internal links, Outbound links or Cite
sources inline rules: a replace's new words were plain text, inserts took
no <a>, and it had nowhere to link to. When one of those rules is asked
for, it now gets the brand's own pages (site_pages, the most cited and
visited first) and the draft's sources (cited pages, not the brand's or a
competitor's, not left out), and may link only to those URLs: any other
link is unwrapped, tracking parameters come off. A replace keeps its links
beside its words (after_links, 00240); an insert keeps them in its HTML.
The editor puts them on as link marks when a suggestion is accepted, takes
them off on undo, and shows on the card where each one goes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/lib/content-agent.js
… the draft's sources

The editor API's suggest_content_changes took no links, so an agent working
through MCP could not meet the link rules either. get_content_draft now
lists link_targets (the brand's own pages, and the draft's sources without
the brand's or a competitor's), and suggest_content_changes keeps a link
only to one of those URLs, as the Content Agent does on the server: a
replace's links go in after_links, an insert's stay in its HTML, any other
link is unwrapped and a link with no words dropped. The targets are read
only when a suggestion links.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fan-out rules stayed open after a run: the agent was told only "now: 3
of 4", wrote H2s and FAQ questions of its own, and the check didn't count
them. A fan-out rule's status now names what's missing (fanoutH2: the top
fan-outs no H2 carries; fanoutFaq: the most searched ones no FAQ question
uses, or the top ones while there's no FAQ), and both Apply rules with
Agent and Start Agent send them in the rule's detail, quoted, within the
room the server allows. Both prompts share FANOUT_RULE: an H2 or a FAQ
question per named fan-out, in its words, and a change is called a fan-out
answer only when it uses one listed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 22, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Content Agent rule gaps B (stacked on #1372): the fan-out rules now name the missing fan-outs, so the agent writes those, in their words, instead of questions of its own. This covers both Apply rules with Agent and Start Agent. A review pass before pushing caught that a page with no FAQ yet got no names at all, and that Start Agent read a page it was about to rewrite; both are fixed.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One fix before merge:

  1. A new page's prompt doesn't get the fan-out rule (server/src/lib/content-writer.js:94-103).

    • FANOUT_RULE is added to SUGGEST_SYSTEM_PROMPT (line 113) and the fix-rules prompt. It is not in WRITE_SYSTEM_PROMPT, which runDraftWriting uses for method === 'create'.
    • That is the case where agentWriteRequest names every top fan-out ("a page the agent writes anew starts from none"). So on Create and Try again, the model sees the names in the rule detail but not the instruction to use their words in an H2 or an FAQ question, rather than questions of its own.
    • Fix: add a line shaped for the page schema to WRITE_SYSTEM_PROMPT. For example: "For each fan-out a rule names, use its words: a section heading for fanoutH2, an FAQ question ending in "?" for fanoutFaq." Assert it in the create-path test in content-writer.test.js.
  2. Low: fanoutFaq can stay open with nothing to name (web/src/lib/content/rules.ts:768-773).

    • missing lists the fan-outs no question uses yet. With fewer fan-outs than the rule asks for (2 fan-outs, the default 3 questions), both get used, missing becomes [], and the rule stays at "2 of 3".
    • The agent then gets no names. Any question it writes on its own doesn't count.
    • Fix: when missing is empty but used < n, name the top fan-outs again (a question may reuse one). Or cap of at the fan-outs there are, as fanoutH2 does.

Checked and fine:

  • Budget. fanoutsToUse keeps the joined list within 300 characters, separators included. With aim and now in front, the Apply rules detail stays well under the server's detail.max(500).
  • Status shapes. fanoutH2 gives the same count and total as before, plus missing. needsFaq only gains an optional field. fanoutsToUse returns null for every other status. Nothing reads statuses by deep equality.
  • Start Agent request. It reads the same included fan-out rows as the editor's rule context (buildSourceGroups, included only). It uses the existing page only for optimize, and only the two fan-out rules get a status.
  • Cost. There are no new calls. The two system prompts grow by about 90 tokens, and each fan-out rule's detail by at most 300 characters.
  • i18n and tests. agent.fanouts is in en.json. The tests cover the lists, the budget, new vs existing page, the detail sent, and both server prompts.

cihan-gyk and others added 2 commits October 23, 2026 09:57
The Content Agent appended a fan-out's question to an existing FAQ
question heading ("What is AEO? What is an AEO tracker?"), which the
editor's heading guard then refused. The fan-out rule now says to insert
a new question with its answer after an existing answer, or reword a
question only when its answer still answers it. A heading replace that
makes a heading ask more than one question is dropped, on the server and
over the API (heading_two_questions), and a second question is no longer
moved under the heading as its answer.

Review fixes: the new-page prompt names the fan-out rule too, and
fanoutFaq counts the different fan-outs its questions use, asking for no
more than there are, so it never stays open with nothing to name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Thanks. Both fixes are in a7c015b, on top of a merge of main (b2ec760; #1369–#1372 are squashed in, so the diff is this PR's own again).

  1. New-page prompt: WRITE_SYSTEM_PROMPT now has "For each fan-out a rule names, use its words, not questions of your own: a section heading for fanoutH2, an FAQ question ending in "?" for fanoutFaq." The create-path test in content-writer.test.js asserts it.
  2. fanoutFaq with nothing to name: it now counts the different fan-outs its questions use, as fanoutH2 counts its H2s, and asks for of = min(N, fan-outs). So missing is never empty while the rule is open. Two questions using the same fan-out count once, and one question using two counts both. lengthConflict counts the same.

Also in this commit, from the user's test on prod (draft 3b94abb0, "2 suggestions couldn't be applied"): fanoutFaq suggestions appended the fan-out's question into an existing FAQ question heading.

  • FANOUT_RULE now says to insert a new question with its answer after an answer's last paragraph, at the FAQ questions' level, and to reword an existing question only when its answer still answers it. One question per heading.
  • asksMoreThanOne drops a heading replace that makes the heading ask more than one question. It applies in keepSuggestions and over the API, as dropped reason heading_two_questions, which is listed in the MCP tool description. A heading that already asked two can still change.
  • splitReplace no longer moves a second question under the heading as if it answered it. That holds whether or not its own answer follows, on both sides.

Known and left as it was: an insert can still carry a heading with two questions (growsPastLimit doesn't look at inserts either).

Checks: server 2,096 and web 2,899 tests, lint, typecheck, format and build all pass. The PR body is updated.

gkhngyk
gkhngyk previously approved these changes Oct 23, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Both earlier items are fixed, and the one-question guard is right. One optional follow-up:

  1. Low, optional: fanoutFaq still says "questions" (web/src/lib/content/rules.ts:86).
    • The rule now counts the different fan-outs its questions use (rules.ts:845-852). Two questions on one fan-out count once, and one question on two counts both.
    • Its unit is still questions, so the Rules tab can show "1 of 3" next to "3 questions" on a page with three fan-out questions.
    • Fix: give it unit: 'fanouts', as fanoutH2 has.

Earlier items:

  • New-page prompt: done. WRITE_SYSTEM_PROMPT names the fan-out rule for its own schema (server/src/lib/content-writer.js:101). The create-path test asserts it.
  • fanoutFaq open with nothing to name: done. of is min(N, fan-outs) and used counts different fan-outs, so missing is never empty while the rule is open. lengthConflict caps it the same way. The tests cover fewer fan-outs than N, two questions on one fan-out, and one question on two.

Checked and fine in a7c015b:

  • asksMoreThanOne (content-agent.js:269, suggestions.ts). It only drops a heading replace that adds a question, so a heading that already asked two can still change. It runs after splitReplace, so it reads the words that would go in. The server (keepSuggestions) and the API (keepAgentSuggestions, heading_two_questions) behave the same. The MCP tool description lists the new reason, and nothing else lists the dropped reasons.
  • splitReplace (content-agent.js:321, agent-suggestions.ts:201). A second question is no longer moved under the heading as its answer. That suggestion is now dropped as a whole, so no paragraph ends up under the wrong question. An answer that ends on a question is still split off. The server and web copies match.
  • FANOUT_RULE (content-agent.js:50). The insert it asks for (an H3 and a paragraph after the last answer paragraph) is what faqQuestions counts. The tags it uses are allowed in inserts.
  • Merge of main (b2ec760): no hidden changes. The PR's diff against main is the same as a667fc6's own diff. The only difference is context in page.test.tsx, where #1384's "a full batch is the most one run makes" test and this PR's fan-out test both stay.
  • It merges cleanly on main (c456881). There's no migration.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after merging main (#1387) into the branch. server/src/lib/content-writer.test.js conflicted in the new-page test: I kept both sides, #1387's budget charge/release assertions and this PR's fan-out prompt assertion. The PR's diff against main is otherwise unchanged. The merged tree passes the web and server tests, typecheck, lint and format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants