Skip to content

feat(content): aim an optimized page's Length rule at its own length - #1383

Merged
gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/optimize-length
Oct 23, 2026
Merged

gkhngyk merged 14 commits into
ansvisor:mainfrom
cihan-gyk:feature/optimize-length

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A page brought in to optimize now aims its Length rule at its own length, not at a new page's target.

Stacked on #1374 (B2, on #1372 → #1371 → #1369 / #1370). This PR's own change is bf20814. Merge after #1374.

On prod, an 18,000-character page imported to optimize had a Length rule of 2,000 (My rules) or 3,000 (the blog post default).

  • Effect: the rule stayed open, and "Apply rules with Agent" kept trying to cut 85% of the page a few passages at a time.

What changed

  • What's stored: a draft started with method optimize and a page's text stores that text's length as rules.importLength. The length is clamped to the Length rule's 100–50,000 range.
    • This covers a page pasted, imported from its URL or a file, the live page Start optimizes, and the page an Action Center task opens.
    • It goes through startingRules() in lib/content/rules.ts, which the server actions (createContentDraft, startDraftFromAction) and New content's "Who does it: Agent" request all use.
  • The Length rule: it starts at that value (ruleSet(type, { length })), and is met within ±20% as before.
    • The person can still change it.
    • Reset and picking a set keep it.
  • My rules: they still apply to such a draft, except their Length value, which is for a new page (overridesForImport).
    • Save as my rules keeps the workspace's own Length value (setToSave), so saving from an imported page doesn't erase it.
  • The Content Agent: Start Agent and Apply rules get the same aim (agentWriteRequest).
  • The length count: pageTextLength() counts the text as the editor does, plus one space between blocks. On a 5.5k-character page, the editor counted 5,515 and the stored value was 5,527.
  • Rule hint: the Length rule's hint says a page brought in starts at its own length.

There's no migration, since importLength is an optional field in the existing rules jsonb (00213 only checks it is an object of at most 32 KB). Drafts made before this keep the type's length.

Follow-up, not in this PR: an Optimize draft made by MCP create_content_draft doesn't get importLength. There the agent writes the body, so it has no imported page to measure yet.

Related issue

Content Agent rule gaps, item F (folded into C), 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. New content → Optimize content → Paste text: paste a long page (e.g. 5,000+ characters) and click Open editor.
    • The Rules tab's Length shows the page's length (5,527 in my run), not 3,000.
    • The header counter reads e.g. "5,515 / 5,527 characters", and the rule is met.
  2. Pick My rules for the type from the Rule set menu, with a Length value saved there: Length keeps the page's length.
  3. Save as my rules on that draft, then open a new Create draft. My rules' Length is still the workspace's value.
  4. Pick Who does it: Agent for a pasted page. The agent's request carries the page's length.

Tests:

  • rules.test.ts: effectiveRules, overridesForImport, setToSave, pageTextLength, and startingRules, including the clamp;
  • content-drafts-start.test.ts: paste/import, Start from an opportunity, an Action Center task, and no rules without the page's text;
  • new-content-dialog.test.tsx: the Agent request.

Checked in the browser at 1024 px, in light and dark themes (Rules tab, Length 5527). The temporary draft was deleted.

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: 2,865 of 2,866 passed in the full run. The Library export test ("reads a long list's pages fifty at a time") hit the 5 s timeout under memory pressure. It's untouched here and passes alone (21/21).
  • yarn build.

🤖 Generated with Claude Code

cihan-gyk and others added 13 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]>
An Optimize draft pasted, read from its URL or a file started with no
prompts, so it had no fan-outs unless AI answers cited that very page, and
the fan-out rules read "No fan-outs". It now starts with up to ten of the
brand's active prompts its title and headings speak to (60% of a prompt's
words and at least two, the most in common first), which the person can
leave out in Sources. A draft with no prompts shows "Add prompts" on those
rules, opening the Sources tab.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A page brought in to optimize (pasted, imported from its URL or a file,
read by Start or by an Action Center task) stores its text length as
rules.importLength. Its Length rule aims there (±20%), not at a new
page's target for the type, and the Content Agent gets the same aim.
My rules still apply to such a draft, but not their Length value; Save
as my rules keeps the workspace's value, and the person's own value
wins.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Content Agent rule gaps C1 (item F): an Optimize draft's Length rule now aims at the imported page's own length (rules.importLength, no migration). Stacked on #1374. Please merge after #1374.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 22, 2026
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. One low follow-up:

  1. importLength accepts far more than the Length rule takes (web/src/lib/content/rules.ts:317).
    • The schema allows .min(1).max(10_000_000), while startingRules clamps to 100–50,000. A value saved through saveDraftRules outside that range goes straight into ruleSet's Length value, and from there into the Rules tab.
    • ruleLength still clamps what the agent and the counter use.
    • Fix: bound the schema to 100–50,000. RULE_LENGTH_MIN/MAX are declared below the schema, so either move them up or use the literals.

Checked and fine:

  • Where it's set.
    • createContentDraft and startDraftFromAction measure the sanitized body. They write rules only when there is an import length, so other drafts keep the column default.
    • The insert in startDraftFromAction goes through the admin client, but the rules object is built on the server.
  • Kept through every path.
    • edited spreads effective, so the import length survives every rule change.
    • pickSet and reset carry it over explicitly.
    • effectiveRules and pickSet drop only My rules' Length value and keep its on/off.
    • setToSave puts the workspace's own value back. A value the person typed is saved as theirs, which the tests pin.
  • Interaction with the Length rule on main (#1357/#1366).
    • The counter colour comes from lengthWarns, the Length status, and the counter's target from ruleLength. Both read the same ruleSet value, so they agree.
    • activeRules doesn't mark the import length as changed, and Reset returns to it.
  • The count. pageTextLength adds one space per block over TipTap's count, which is well inside the ±20% band.
  • Other readers. Nothing else builds the rule set from a draft's rules. MCP and the server don't parse it, so an optional field is safe.
  • Cost, i18n and tests.
    • There is no LLM change, and the Length hint is updated in en.json.
    • Tests cover effectiveRules, overridesForImport, setToSave, pageTextLength, the clamp, both server starts and the dialog's request.

@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 into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).

@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
@gkhngyk
gkhngyk merged commit 942c492 into ansvisor:main Oct 23, 2026
4 of 5 checks passed
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