Skip to content

fix(content): stop recommending a page rewrite for a Length it can't reach, and offer one it can - #1514

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/rewrite-length-reach
Oct 26, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/rewrite-length-reach

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

In the live editor test (2026-10-25), an Optimize "best of" page with a Length of 4,000 kept 14 cited passages and 4 tables, about 3,125 characters (78% of the Length). The Rules tab and Next step kept recommending Rewrite page. Three rewrites came to 5,468, 6,843 and 7,335 characters, and the third made the page longer.

Two things caused it:

  • lengthOutOfReach only fired once kept blocks reached 85% of the Length.
  • rewriteAdvice ignored that the last rewrite had already missed.

This PR reads a Length's reach from two things, either one enough (web/src/lib/content/rules.ts, RewriteReach).

1. The least a rewrite comes to (rewriteFloor, _cited-passages.ts). It's the sum of:

  • what the rewrite keeps as it is;
  • each heading;
  • the least answer under each heading;
  • the least summary under the H1 (when the rules ask for one).

Notes:

  • No least answer is added where a kept passage already stands, in a list or a quote too.
  • A heading that only groups deeper ones (an H2 over H3s) needs no answer.
  • It reuses LEAST_SUMMARY and a new LEAST_ANSWER (100). It stays low on purpose, so "out of reach" means it.

2. What the page's last rewrite came to.

  • Migration 00285 adds three nullable columns to content_draft_suggestions, written by content-writer.js on a page rewrite:

    • length_target: the Length it was written to, null with Length off;
    • kept_chars: what it kept word for word;
    • after_chars: what it came to.
  • The editor reads these numbers only (getLastPageRewrite), on opening and once a new rewrite shows. The rewritten page itself isn't read.

  • The last rewrite counts only when all of these hold:

    • it was written to a Length;
    • the page keeps what it kept then: nothing unlocked or locked since, with a little slack for the two counts;
    • it was asked for this Length, or a longer one, or it missed its own and this Length is within that one's band.

    So a rewrite for a Length of 10,000 says nothing of 3,000, met or missed, and one written with Length off says nothing at all.

  • A last rewrite that got within the band means the Length is in reach, whatever the estimate says.

If either one is past the top of the Length's band (+20%), the page is out of reach:

  • Rules tab and Next step: a rewrite isn't recommended for the Length, so the Next step no longer offers the length rewrite.
  • Rules tab note: it says why, in one of three ways:
    • "The last rewrite came to N, so another won't get there";
    • "a rewrite can't go much under N", with what it keeps;
    • with nothing kept, the headings and their answers.
  • Raise Length to N button in the same note: the more of the two, rounded up to 500, never past the page as it is. So one click is enough.
  • Agent card: a rewrite that missed the Length names what it kept only when that alone puts the Length out of reach (keptOutOfReach); the rewrite's own result is no reason. The old 85% gate is gone (KEPT_LENGTH_SHARE).

Page rewrites made before 00285 have no numbers, so for them only the estimate counts.

The other parts of this round go in separate PRs from main:

  • the server's second cut pass and the "longer than before" flag;
  • the agent's open count leaving out rules it can't move (Next step);
  • the card flow.

Related issue

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

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

Deploy order

00285 goes first: the API writes the new columns. Before it's applied, the API's insert of a page rewrite would fail. The web reads the columns and logs a failed read, falling back to the estimate. Applied on the dev database.

How to test

  1. Open an Optimize draft whose page has many cited passages, with a Length well under the page (e.g. 4,000 for a 7,000-character page).
  2. Run Rewrite page once, then accept or reject it. If it misses the Length:
    • the Rules tab says "The last rewrite came to N, so another won't get there";
    • Next step and Run Agent no longer recommend another rewrite for the Length.
  3. Click Raise Length to N. The Length rule takes N, and the page reads within it.
  4. Lower the Length below what a rewrite once met. A rewrite is recommended again, because that one says nothing of a shorter Length.
  5. On a page whose kept passages, headings and answers alone are past the Length, the note says so before any rewrite runs.

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. bash supabase/build-schema.sh was run, and schema.sql is in this PR. Two pre-push reviews; their findings are the second and third commits. The browser check of the Rules tab note is in the next batched local test session.

🤖 Generated with Claude Code

cihan-gyk and others added 3 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]>

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

What I checked:

  • 00285.

    • It adds three nullable integer columns to content_draft_suggestions, with no default and a check on each. Nothing is rewritten. The checks take one pass over the table while it's locked. add column if not exists makes it safe to run again.
    • Members read the columns through the table's existing select policy, which covers drafts of their own organization. authenticated has select only, so only the API writes them.
    • schema.sql has the migration as the build script writes it. supabase.ts has the three columns in Row, Insert and Update.
    • getLastPageRewrite filters on draft_id and kind, takes the newest row and reads one. content_draft_suggestions_draft_idx (draft_id, created_at desc) covers that.
  • What the API writes (content-writer.js:599-603).

    • length_target is the Length the rewrite was sent, or null with Length off. rebuildSources keeps it between 100 and 50,000, so the > 0 check always holds.
    • kept_chars is keepBlocks' own count.
    • after_chars is counted on the final page, after the kept blocks go back and unsourced numbers become placeholders.
    • The server test checks all three, and the null with Length off.
  • The floor (rewriteFloor). It walks the top-level blocks the same way keptCharacters does. It counts:

    • what a rewrite keeps;
    • each heading;
    • 100 characters under each heading that has no kept quoted passage under it;
    • 100 under the H1, only when Summary is on.

    A heading that only groups deeper ones adds no answer. The tests cover a quoted passage that answers a heading (in a quote too), a heading that groups, and an unlocked passage.

  • The last rewrite (lastRewriteSays).

    • It's ignored with Length off, and when the kept count has moved by more than the slack.
    • For a Length shorter than the one it was written to, it counts only when it missed its own and the new Length is inside that one's band.
    • A rewrite that got within the band wins over the estimate.
    • suggested is the larger of the two numbers, rounded up to 500. It never goes past the page or RULE_LENGTH_MAX, so one Raise is enough.
    • Each case has a test.
  • Where it shows.

    • The Rules tab note has three wordings, and each gets all of its placeholders.
    • Raise Length sits inside the fieldset that stays disabled until the rules are ready.
    • Next step and Run Agent's recommendation stop offering the Length rewrite. Rewrite page itself is still under ▾.
    • The agent card names the kept blocks only when they alone put the Length out of reach.
  • The read. It runs on opening, and again when a new page suggestion shows. It goes through RLS. If it fails, it's logged and the estimate stands.

  • Before the API is deployed. This assumes 00285 is applied first.

    • The web reads the new columns. They're null for every rewrite until the API writes them, so only the estimate counts.
    • The new note wordings and Raise Length show.
    • On a page where the floor still fits the band, as on the test page, a rewrite is still recommended after one misses. That stays until the API goes out.
    • Nothing that works today breaks.
    • The API with this change needs 00285 on the database first, because its page-rewrite insert names the new columns.
  • Merge. It's based on current main and merges cleanly.

Low, follow-up:

  1. _cited-passages.ts:173: only its own tests use keptCharacters now, and rewriteFloor repeats its walk. Drop it and its tests, or have rewriteFloor take its kept test from one shared helper.

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

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants