Skip to content

fix(content): tell a Fix run or hand-off how far past its Length the page is when it isn't asked to shorten it - #1552

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/agent-fix-over-length
Oct 26, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/agent-fix-over-length

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The problem (live test, 2026-10-26). On a live Optimize draft, the page was 17,683 characters against a Length of 13,998:

The change. When the page is over its Length and a run doesn't send the Length rule, the run says so, and the agent keeps what it adds short. That covers:

  • Fix open rules when the Length is held, or when the run is asked for some rules only;
  • Quality's hand-off, whose checks add text too (an FAQ, a specs table, headings).
  • Web:
    • overLengthFor (web/src/lib/content/rules.ts) gives the page's characters and the most its Length allows (the band's top, +20%). It's undefined when Length is off, within its band, or sent.
    • Fix open rules and Hand off send it as overLength (fixDraftRules, handOffDraft).
  • Server:
    • fixRulesInput takes an optional overLength: { count, max }, and handOffDraft passes it on.
    • buildFixPrompt adds one line under the rules: "The page is already 17,683 characters, past the most its Length allows (16,798). The Length isn't among the rules here, so don't cut the page for it. Fix each rule with as few words as it needs: an answer in one or two sentences, no section, list, table or example a rule doesn't ask for, and a replace no longer than the words it replaces unless the rule needs words added (a keyword, a figure, a name)."

There's no change when the Length rule is among the rules sent, since it already says how far off the page is. Rewrite page already sends the Length with its reading.

Deploy order:

Related issue

Live editor test, 2026-10-26 (no issue). It goes with the PR that moves Next past the third step once the agent has taken it.

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-agent.test.js -t "over its Length"
    cd web && npx vitest run src/lib/content/rules.test.ts src/lib/actions/content-drafts.test.ts -t "over its Length|fixDraftRules"
  2. With an AI key, on a draft well over its Length whose Length shows "yours to decide", run Fix open rules. In the API's request log, the body has overLength. Its suggestions add short answers, with no new sections beyond what a rule asks.

Checklist

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

Also run: server format, lint and tests (225 files, 2,890 passed, 3 skipped); web yarn test (427 files, 4,057 tests) and yarn build.

Tests:

  • Server:
    • the prompt line, under the rules, with the numbers in English;
    • nothing said within the Length or with none sent;
    • the input parsed, and a negative count or a zero max refused;
    • a hand-off passes it on.
  • Web:
    • overLengthFor gives { count, max } when the Length is held, when only other rules are asked for, and for a hand-off (none sent);
    • it gives nothing when the Length is sent as a rule, within its band, short, or off;
    • fixDraftRules and handOffDraft send it in the body.
  • The editor's call (fixRules) passes the open rules it sends. A page test would need a held Length, which depends on a stored last rewrite, so it's covered at the helper.

Pre-push review

A review agent read the change before the push. It confirmed the condition, the only path, the numbers and that an older API ignores the field (Zod strips it). Fixed from it:

  • the first wording, "make the words you replace no longer", clashed with rules fixed by adding words (keywords, a dated figure, a name, who it's for). It now allows those;
  • Quality's hand-off had the same gap, and now sends it;
  • comments and test numbers (16,798), with separate refusals for each bound.

Left for a separate PR: Start on an existing page gives its writer only the Length target, not the page's current count. Its prompt invites a missing section or an FAQ.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 26, 2026 18:02
…page is when it isn't asked to shorten it, so what it adds stays short

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

@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:

  • Before the API deploy.

    • The fix and hand-off routes parse with fixRulesInput.extend({ brandId }) and handOffInput.extend({ brandId }) (server/src/routes/content-drafts.js:208, :246, the same on main).
    • Both are plain z.objects (zod 4.3), which strip unknown keys. None of the three files has had .strict() or strictObject since the routes came in (#1304, #1481).
    • So an older API drops overLength and runs as it does today: no 400, and the same prompt. I parsed both bodies with the field using zod 4.3.6, and each passed without it.
    • What users see between merge and the API deploy: no change, and no error. Until then, Fix on a held Length keeps adding full sections, as in your live test.
    • A new API with an older web gets no field and adds no line.
  • overLengthFor (web/src/lib/content/rules.ts:825-835).

    • The count is the Length status's page.characters. useDraftRules reads it from the toolbar's characterCount.characters(), so it's #1521's one count.
    • The max is lengthBand(n).max, the band's top as the rules read it: 1,000 gives 1,200, and 13,998 gives 16,798.
    • When it's undefined: with Length off (rule.on), within its band or short (code !== 'long'), and when Length is among the rules sent.
    • Units and types. Length has one unit, characters; words are only shown beside it. Every page type has a Length (lengthTarget, 3,000 by default), so no type goes without one.
    • What each run sends.
      • Fix open rules passes open, what's left after held and NOT_FOR_AGENT. So the field goes with a held Length, and with Remove AI traces' own Fix, the only caller with only.
      • A hand-off passes [], so the field goes whenever the page is past its Length.
    • A locked passage counts in the page, as the toolbar counts it. That's right, since the line asks the agent to add little, not to cut.
    • One edge is item 1.
  • The server.

    • overLength is validated as two integers: a count of 0–10,000,000 and a max of 1–10,000,000. They reach the prompt only as toLocaleString('en-US') of those integers, so no text from the sender gets in.
    • The line is said only when count > max. With no field (an older web), overLengthLine returns '' and the prompt is main's, byte for byte.
    • The hand-off takes it through handOffInput (fixRulesInput without rules), with the same bounds. handOffDraft passes it on to the same buildFixPrompt.
    • Prompt only. There's no new check, so nothing is dropped for length.
      • "no section, list, table or example a rule doesn't ask for" leaves out what a rule asks for: an FAQ, a fan-out H2, a specs table, a summary.
      • "unless the rule needs words added" covers keywords, figures and names, and also a disclosure or a balance paragraph.
      • The worst case is shorter answers, which the person reviews. One or two sentences still clears the 40-character least answer under a heading.
  • AI budget. No new call. The line adds about 100 tokens to the one prompt, held and charged by the run's one reserveAiBudget.

  • Speed. No new regex, and no loop on the page's text. overLengthFor reads the rules list once (about 25 rules), and the server formats two numbers.

  • Tests. Each new test fails without the change:

    • overLengthFor is new;
    • the prompt line, the parse and the two refusals need the schema and overLengthLine;
    • the hand-off test needs both the schema and handOffDraft's pass-through;
    • the two action tests need the field in the body.

    The editor's two calls aren't covered (item 2).

  • Merge.

    • One commit on main (c58edb8). git merge-tree with main gives the PR's own tree. No migration.
    • It conflicts with #1550 in fixRules (_draft-editor.tsx:2343-2347), where #1550 adds arguments to the same runAgentFix call. Whichever merges second keeps both:
      const overLength = overLengthFor(rules.rules, open);
      await runAgentFix(
        () => fixDraftRules(draft.id, draft.brandId, overLength ? { ...request, overLength } : request),
        undefined,
        false,
        beginThirdTry(),
      );
      The hand-off's change merges on its own. It merges clean with #1544, #1549 and #1551.

Follow-ups:

  1. A Length of 0 makes the API refuse the run (web/src/lib/content/rules.ts:832-834; server/src/lib/content-agent.js:136).
    • The Rules tab saves "0" (_rules-tab.tsx:721 takes ^\d{1,6}$). lengthBand(0) is 0–0, so every page reads as long, with max: 0.
    • Such a Length isn't held, because lengthOutOfReach needs a target above 0 (rules.ts:959). So Fix open rules sends it as a rule, as today.
    • But a hand-off, the automatic one included, and Remove AI traces' own Fix send overLength: { count, max: 0 }. The schema refuses max: 0 (min(1)), so once the API is deployed those runs answer 400 invalid and the editor says the run failed.
    • Fix: the hint shouldn't cost a run.
      • On the server, make the field .optional().catch(undefined), so a value out of range is dropped and the run goes on. I checked this with zod 4.3.6.
      • Also skip the line when a length rule is among the rules, since it says the Length isn't.
      • On the web, return undefined when status.max < 1.
      • In the server test, the two toThrow checks become "parses without overLength".
    • It's small, and it can go in with this PR, before the API deploy.
  2. The editor's two calls aren't tested (_draft-editor.tsx:2344, :2528).
    • The PR says a page test would need a held Length. That's true for Fix open rules, but a hand-off sends the field on any page past its Length.
    • Fix: in page.test.tsx, use a draft with Length set to 100 (overrides: { length: { value: '100' } }) and a longer page:
      • Hand off sends overLength: { count: expect.any(Number), max: 120 };
      • with the Length within its band, Hand off sends no overLength.

@gkhngyk
gkhngyk merged commit 307670b into ansvisor:main Oct 26, 2026
2 of 3 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 26, 2026
…e run, and test the editor's Fix and hand-off calls (#1552 review) (#1553)

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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