Skip to content

feat(content): cut a page rewrite to its Length with one more pass, and say when one came out longer - #1515

Merged
gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:fix/rewrite-length-second-pass
Oct 26, 2026
Merged

gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:fix/rewrite-length-second-pass

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #1514. The PR's own changes are the two commits after the merge of #1514's branch.

In the live editor test (2026-10-25), page rewrites for a Length of 4,000 came to 5,468, 6,843 and 7,335 characters. The third made the page longer, and nothing said so. #1514 stops recommending a rewrite for a Length it can't reach. This PR makes a rewrite reach a Length it can, and says when it didn't.

Server: one more pass that only cuts (content-writer.js, cutToLength).

  • When it runs: Length is on, and the rewrite came more than 15% of the Length past the top of its band (Length + 20%). For 4,000, that's past 5,400.
  • What it does: the model cuts its own page to the Length. It keeps the H1 and every heading, a short answer under each, every [[KEEP n]] line as it is, the links, the facts and the placeholders, and adds nothing new.
  • Its own hold on the AI budget. A budget the first pass used up skips it.
  • When the first rewrite stands: the cut is taken only if it's shorter and restoreKept can put every kept line back. A line written into a paragraph would otherwise fail the whole run. If the cut isn't taken, or the call fails, the first rewrite stands (logged).
  • Its hold: charged as billed, kept on a stop during the call, and released otherwise, also on a stop before the call goes out. A Stop goes through as before.
  • The people and number guards, the kept blocks' restore and fix(content): stop recommending a page rewrite for a Length it can't reach, and offer one it can #1514's after_chars all run on the page that's kept. The first pass's reason and data stay; the cut pass gets the page only, so it has no data to name.

Web.

  • Agent card: a rewrite meant to shorten the page (the page was over its Length) that missed and came out longer than before says "· longer than the page before".
  • Rules tab: when the Length is out of a rewrite's reach and the quoted passages it keeps are what holds it, the note offers Rewrite, cited passages too. "What holds it" means the least a rewrite comes to, less their characters, fits the Length. Its line says those passages may stop being quoted, and that the person compares the page before anything changes. The rewrite is sent with no locked passages (rewritePage({ citedToo })).
  • fix(content): stop recommending a page rewrite for a Length it can't reach, and offer one it can #1514's evidence (lastRewriteSays):

Related issue

Editor test findings 2026-10-25 (group B, part 2).

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. On a long page with few cited passages and a Length well under it, run Rewrite page. The server log shows a second call when the first came well past the Length. The result reads closer to the Length.
  2. On a page whose cited passages hold the Length out of reach, the Rules tab note offers Rewrite, cited passages too. The rewrite it starts may reword those passages, and Compare shows it.
  3. A shortening rewrite that comes out longer says "longer than the page before" on the card.

Server tests cover:

  • the cut pass taken;
  • the first rewrite standing when the cut is no shorter, drops a kept line or writes it into a paragraph, fails, or has no budget;
  • a stop during the cut;
  • no cut within reach of the Length or with Length off.

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 files this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Server: format, lint, npm test. Web: yarn test and yarn build. A pre-push review ran; its findings are the last commit.

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits October 25, 2026 21:35
…adings' answers and what the last rewrite came to, and offer a Length it can reach

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… for, keep its numbers on the row (00285), and read the floor's sections closer (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and count the last rewrite only within its Length's band and the same kept text (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er to rewrite the cited passages too when they hold the Length out of reach

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e back, release an unused hold, keep a cited-too miss as evidence, and offer it only when cited passages hold the Length (pre-push review)

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

What I checked:

  • When the cut runs. All three must hold:

    • the Length rule was sent;
    • the first answer is a page;
    • its text (its own words plus the kept blocks) is past the band's top by more than 15% of the Length. For 4,000, that's past 5,400.

    With Length off, or under that line, there's no second call and no hold. The tests cover both.

  • The AI budget.

    • The hold. The cut takes its own hold, with the same feature, model and draft as Start's. It takes it after the first call is charged. On a nearly spent budget the hold is refused, so the cut is skipped and the first rewrite stands.
    • An answer is charged at its tokens, and the finally release does nothing after that.
    • A stop during the call. runCall keeps the hold as the call's cost, and the stop ends the run as before.
    • A stop before the call goes out. runCall throws before sending, and the finally releases the hold.
    • A failure. A schema miss that reports usage is charged. Any other failure releases the hold. The first rewrite stands either way.
    • No double charge. The cut's errors never reach runDraftWriting's catch, which charges Start's hold.
  • When the cut is taken.

    • It must be shorter than the first rewrite.
    • restoreKept must find every kept line on a line of its own in the cut.
    • The people, link and number guards, the restore and after_chars all run on the page that's kept.
    • The first pass's reason and sources stay.
  • Time. Two calls of up to three minutes each fit inside the ten-minute stale window for both the claim and the run.

  • Rewrite, cited passages too.

    • It sends locked: []. The API already takes that, since the editor could always unlock passages.
    • It's disabled in the same cases as Run Agent: AI usage spent, suggestions still open, cited passages still loading, or the agent busy.
    • The hint says the passages may stop being quoted, and that the person compares before anything changes.
    • lastRewriteSays now keeps a miss by a rewrite that kept less than the page keeps now. It doesn't take that rewrite reaching the band as proof the Length is in reach. Both cases have tests.
  • "longer than the page before". It shows only when all of these hold:

    • the page was over the band's top;
    • the rewrite still is;
    • the rewrite is longer than the page.
  • Merges. Merge commit 854d28a changes nothing beyond merging its parents. The PR contains #1514, and it merges cleanly on main and with #1525.

  • Before the API is deployed.

    • The card's "longer than the page before" works.
    • The cited-passages button works on the old API, which takes an empty locked list. It's one call, with no cut.
    • Nothing that works today breaks.
    • Once the API goes out, a page rewrite far past its Length can make a second, charged call.

Follow-ups:

  1. _rules-tab.tsx:264-269: a cited-passages rewrite that misses gets offered again.

    • After it misses, the note says "The last rewrite came to N, so another won't get there". It still offers Rewrite, cited passages too, the same rewrite that just missed.
    • Each click is charged, and costs two calls once the API is deployed. That brings back the loop #1514 closes, through this button.
    • Two texts are also wrong for that rewrite, since it kept none of the quoted passages:
      • the note's "or unlock passages you're fine to have reworded" (en.json:5248);
      • the card's "(N characters are passages AI answers quote…)" (_agent-card.tsx:383-386), which shows when the floor is past the band.

    The row already tells such a rewrite apart: its kept is below the page's by more than the slack, the same test as inReach (rules.ts:885). In that case, after a miss:

    • hide the button;
    • drop the unlock clause;
    • skip lengthKept.

    Add a test with last: { chars: 5_200, target: 4_000, kept: 0 }.

  2. content-writer.js:639-642: a first answer that leaves a kept line out entirely still gets a cut.

    • The run fails at restoreKept anyway, and the cut can't bring the line back, because it never sees it.
    • Yet the cut runs first and is charged, and the person waits up to three more minutes before seeing "failed".
    • Fix: return answer before reserving when one of page.kept's numbers isn't anywhere in answer.html as [[${page.word} n]].
    • Leave a line written into a paragraph to the cut. It may put the line back on its own, which saves the run.

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