Skip to content

feat(content): plan each section's answer, format and facts in the brief behind CONTENT_BRIEF_RULES, with the cited pages numbered and the rules in effect (brief rules PR 4) - #1540

Merged
gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:feature/brief-section-plan
Oct 26, 2026

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Summary

Brief rules 2026, PR 4 of 10: the brief plans each section's answer, format and facts.

With CONTENT_BRIEF_RULES=true: Rebuild outline writes the brief's version 2 for each section, in the shape #1539's editor reads (the contract in web/src/lib/content/brief.ts).

With it off, the system prompt, the schema, the prompt and the saved brief are the same as before. Two tests check this:

One small change also applies with the flag off: an H2's fan-out now also matches when the model copies it with the quotes the prompt lists it in. A fan-out written exactly as listed still matches first.

What the model is asked

outlineSchemaV2 adds three fields to each section:

  • answer: what the section's first sentences must answer directly. It is a note to the writer, with no word count.
  • format: one of paragraphs, list, steps, table or faq.
  • facts: about one fact per section. Each fact has:
    • a claim, worded as what to find out (never a figure);
    • its kind of source: statistic, study, quote, example or official;
    • optionally the number of the cited page to check it against.

format and sourceType are enums in the JSON schema the model is given (.meta({ enum })), so a model that supports constrained output picks keys and doesn't write free words. A Turkish or German brief would otherwise translate them ("tablo"), which the web can't read.

Zod's own validation still accepts any string, so a model that writes "Table" doesn't fail the whole call, which would still be charged. sectionPlan then reads the key: trimmed and in lower case, or none.

SYSTEM_PROMPT_V2

It is version 1's rules plus these:

  • Answer first.
  • One fact per section with its kind of source:
    • a page's number only when it's the page to check that fact against (the model sees only its address);
    • never a figure of its own.
  • The numbers go in source only.
  • The formats, written as English keys whatever the brief's language.
  • Each H2 is about 8–15 words and under 90 characters.
  • No keyword stuffing, and no wording made to trigger a citation.
  • Follow the rules listed.

The prompt, with the flag on

  • Cited pages by number (S1: <url> (kind)):
    • Only the pages are numbered. A prompt, a fan-out or a query is a question, not a source.
    • The numbering uses numberSources, the same as the writer's.
    • The lines are built from that numbered list, so no number points at the wrong page.
  • "Its sections lean to: …" for the page type (PAGE_TYPE_SECTION_FORMATS), e.g. a table for a comparison or numbered steps for a guide.
  • The rules in effect:
  • rebuildSources is unchanged. The writer and Fix share it.

The answer as kept (sectionPlan)

  • Guarding and cutting:
  • Format: read trimmed and in lower case; one the web doesn't know reads as none.
  • Facts:
    • a fact with an unknown kind of source is dropped, and four at most are kept;
    • a fact's source is kept the way a suggestion keeps one (00248): { kind: 'cited_page', ref, label }, never by its number in this run. The number would point at another page once the Sources tab changes.
    • a number that isn't a listed page reads as no source.

fitBrief

A version 2 brief over 60,000 bytes first drops the facts' sources, then the facts. The column holds 64 KB, and the margin leaves room for jsonb's spacing. So a brief that was charged for always saves.

The eval (#1538)

  • promptVersion takes the flag into account.
  • The judge sees each section's Answer:, Format: and Fact (kind, check against <page>): lines.
  • The format criterion asks for H2 lengths only. The brief plans no H1: the page's title is the draft's.
  • The gate looks for added figures, names and addresses in the answers and claims too.
  • The script records with the flag's prompt and schema and notes briefRules. To record version 2's briefs (PR 10), run it with CONTENT_BRIEF_RULES=true.

.env.example

Adds CONTENT_BRIEF_RULES, off until the eval passes it.

Cost with the flag on

  • About +600 input tokens: the page numbers, the format line and the rules.
  • About +500 to +700 output tokens per brief.
  • That's about +$0.003 per brief at Gemini 3 Flash's list price.
  • With the flag off, nothing changes.

Related issue

Roadmap CE-EVAL-001 / brief rules 2026 (no GitHub 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. Run the tests:
    cd server && npx vitest run src/lib/content-brief.test.js src/lib/content-brief-eval.test.js src/routes/content-drafts.test.js
  2. With an AI key and CONTENT_BRIEF_RULES=true, click Rebuild outline on a draft that has a cited page in Sources. Check the saved content_drafts.brief:
    • it has version: 2;
    • each section has answer, format and facts;
    • a fact's source is null or one of the cited pages, by kind and ref.
  3. Compare version 2's briefs with the baseline:
    CONTENT_BRIEF_RULES=true node src/scripts/eval-brief.js 3

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no web changes
  • yarn typecheck passes (run from web/): no web changes
  • yarn format:check passes (run from web/): no web changes
  • Changes are focused — one concern per PR

Server checks, run locally on this branch:

  • npm run format: no changes beyond this PR's files.
  • npm run lint: clean.
  • npm test: 223 files, 2,778 passed, 4 skipped.

Tests

  • Flag off: the same rules, schema and prompt; no rules listed; no version 2 in the brief; the version 1 prompt hashes are pinned.
  • Flag on:
    • version 2's rules and schema;
    • only the cited pages numbered;
    • the page type's formats;
    • the rules.
  • A section's plan:
    • the guard on the answer and the claims (a rule's number kept);
    • the given page numbers taken out of the text fields, and a name with one ("(S23)") kept;
    • an unknown format and kind;
    • " Table " read as table;
    • a page source by kind and ref;
    • a fan-out's number and an unlisted number read as no source;
    • a lower-case s1;
    • four facts at most;
    • a fan-out copied with quotes, and one written as listed matched first (flag off too).
  • Enums: the JSON schema the model gets lists the keys, and an off-list answer ("Tablo", "Statistic") still parses.
  • Version 1 untouched: with the flag off, "(S1)" stays in a key point and a rule's number isn't known to the guard.
  • The 64 KB fit: both steps, sources first and then facts.
  • The cut to the web's limits.
  • briefRules and the route: rules passed through, cut to fit; more than 60 refused.
  • Eval: the judge sees the answers and facts; the gate finds an address or a figure in them.
  • Contract test:

Pre-push review

Two review passes ran before pushing.

First pass, on the first commit. Fixed:

  • free-text format and sourceType, which a Turkish brief would translate;
  • the eval didn't read the new fields;
  • the rules' numbers weren't known to the guard;
  • prompts, fan-outs and queries were numbered as sources;
  • S-numbers were left in the text;
  • a title-length line for a brief that plans no title;
  • numbering that could drift from the listed pages;
  • the rules sat in the shared rebuildSources;
  • Object.hasOwn for the page type's formats;
  • tests for the facts-dropped fit and the pinned version 1 hashes.

Second pass, on the fixes. It confirmed the flag-off hashes against #1538's head. Fixed:

  • strict enums would fail a charged call on an off-list value;
  • the S-number strip took model names;
  • quote-stripped fan-out keys could collide;
  • the judge asked for an H1;
  • test gaps: flag-off "(S1)" and rule numbers, a direct mapOutline test, a route test that actually cuts.

Left for later:

  • The eval's 30 cases carry no rules, so following the rules under version 2 isn't scored yet. PR 10, which records version 2, adds rules to the cases.
  • facts stays a required array in the schema, as keyPoints is.

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits October 26, 2026 04:31
…ave as a placeholder, so the writer can't take it as given

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…aceholder in half, and test the audience, names in headings and Turkish percentages (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s as a rubric, checks anyone can repeat with a gate on anything the brief wasn't given, a judge model's quoted scores, and a script that records them (CE-EVAL-001, brief rules PR 2)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e by line; record the model's own answers and check them again in the test; read quotes in any marks, shortened, three words at least, in one line the judge saw; version the prompt under Quality's phase and the schema; keep a partial run apart; compare addresses by host and path (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ief behind CONTENT_BRIEF_RULES, with the data numbered, the page type's formats and the rules in effect; keep the facts' sources by kind and ref, cut to the web's version 2 limits and fit under 64 KB (brief rules PR 4)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…umber only the cited pages as sources, take the rules beside the sources, and read version 2's answers and facts in the brief eval (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…N schema only, so an off-list value never fails a charged call; take out only the page numbers given; match a fan-out as listed before its unquoted form; ask the judge for H2 lengths only (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on-plan

# Conflicts:
#	server/.env.example
#	server/src/lib/content-brief-eval.js
#	server/src/lib/content-brief-eval.test.js
#	server/src/lib/content-brief.js
#	server/src/lib/content-brief.test.js
#	server/src/scripts/eval-brief.js
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged main forward (d887cb9) now that #1535 and #1538 are squash-merged. The conflicts were only in the files this PR changes on top of #1538's last head (content-brief*.js, eval-brief.js, .env.example). Main has those files exactly as #1538's head had them (your merge commit 0e030fa left them alone), so I kept this PR's side.

#1539 is now on the branch too, so the contract test that reads web/src/lib/content/brief.ts runs instead of skipping, and it passes.

Checks after the merge: server format and lint clean; 223 test files, 2,788 passed, 3 skipped. PR 5, stacked on this one, is coming next.

@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, with follow-ups.

What I checked:

  • With CONTENT_BRIEF_RULES off.

    • The system prompt and schema are version 1's. The prompt template gives the same text: the two new lines are empty strings, and the blank line before "Prompts the page must answer" stays. The pinned hashes hold this.
    • rules is parsed by the route but goes no further (planTo is []). The guard doesn't know the rules' numbers, as the version 1 test shows.
    • v2 is read from the answer's shape. Version 1's schema drops keys it doesn't know, so a version 1 answer never gets version: 2, and fitBrief returns it as it is.
    • stripSourceIds keeps its default, so the writer and Fix strip as before.
    • The only change: an H2's fan-out also matches when the model copies it in quotes. A fan-out written exactly as listed still matches first.
  • The route. rules is optional, at most 60, and cut to 60/200/500 characters. That's the writer's limits, and the web's MAX_AGENT_RULES (#1541) is 60, so the list the editor sends always parses. An API from before this PR drops the key.

  • The stored brief and the editor. Each new field is written as draftBriefSchema on main reads it, so nothing is dropped on read:

    • answer is at most 600 characters, or null;
    • format is a listed key, or null;
    • facts has at most 4 items, each with:
      • a non-empty claim of at most 300 characters;
      • a listed sourceType;
      • source: null, or { kind: 'cited_page', ref ≤ 2048, label ≤ 300 }, kept only with a label;
    • version is 2.

    Other readers:

    • The writer's briefText reads only version 1's fields, so a version 2 brief writes the same page.
    • Edit brief keeps the new fields (#1539).
    • A brief stored before reads as it did.
  • The model's answer.

    • An off-list format or kind is read by key or dropped.
    • A fact without a claim or with an unknown kind is dropped.
    • A number that isn't a listed page reads as no source.
    • A missing key or a wrong type fails the call, as in version 1: it's charged, nothing is saved, and the brief stays as it was. A stop saves nothing either.
  • The prompt.

    • The pages and the read come from the same numbered list (briefNumbered), so a number can't point at another page.
    • The route bounds the rules.
    • The page addresses are the request's, as before.
  • The AI budget.

    • Still one call, held with reserveAiBudget and charged by its usage, with release in finally.
    • With the flag on, the extra tokens are charged by what the call used. With it off, the tokens are the same.
  • The work. fanoutKey and stripSourceIds run on short strings, and the guard runs once per field, so no input makes them slow.

  • The fit. Sources go first, then facts, measured against 60,000 bytes, which leaves room for jsonb's spaces.

    • The last step isn't measured again, and version 1's fields aren't cut to fit. So a brief at every limit in a three-byte script could still go past 64 KB.
    • A real brief is a few KB, so I'd leave it.
  • Tests.

    • The plan, fit, enum and route tests fail without the change.
    • The enum test reads the JSON schema zod really writes, not a mock.
    • The contract test reads the web's own brief.ts.
  • Merge.

    • d887cb9 merges main. Six files conflict on a re-merge, each resolved to this PR's side. Main's copies of those files are byte for byte #1538's head (f267683), so nothing of main is lost. The rest is main's.
    • The head contains main (4efa150), so it merges cleanly. Server only, no migration.
    • Nothing changes on Vercel at merge. At the API deploy, with the flag off, the only change is the quoted fan-out match.

Follow-ups (before the version 2 record, PR 10):

  1. The judge can't quote the new Format lines (server/src/lib/content-brief-eval.js:113, :243, :252).
    • readJudged compares a quote with each field's text without its label, and asks for three words at least. A Format line's text is one word ("table").
    • So "Format: table", the quote the format criterion invites, can never pass. The criterion falls to 0, marked unquoted, exactly where version 2 adds the plan.
    • This is #1538's open item (a quote with its line's label) made worse. The record keeps only the scores as read, so a version 2 run would have to be paid for again.
    • Fix:
      • Compare quotes with each line as the judge sees it (norm(f.label + f.text)), which still contains the text alone.
      • Let a quote that is a whole line pass under three words.
      • Add a test: a "Format: table" quote keeps its score.
  2. The judge's rubric isn't versioned (content-brief-eval.js:68, :275).
    • This PR changes the format criterion. promptVersion hashes the brief's prompt only, so a baseline judged under #1538's wording isn't marked stale next to version 2 judged under this one.
    • Fix: record a hash of JUDGE_SYSTEM_PROMPT and BRIEF_CRITERIA beside promptVersion, and skip a record made under another. Or record the version 1 baseline only after this lands.
  3. A fact's source address isn't checked (server/src/lib/content-brief.js:482).
    • The ref is a cited page's address as the request sent it (rebuildSources.pages[].url is cut, not checked as an address). Nothing shows it yet.
    • Fix: keep a page as a fact's source only when its ref is http(s), as brief.ts does for links. At the latest, check it in PR 9 before the ref is ever used as a link.

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