Skip to content

feat(content): rewrite the page to its rules from the Rules tab, and compare it - #1394

Merged
gkhngyk merged 19 commits into
ansvisor:mainfrom
cihan-gyk:feature/page-rewrite-web
Oct 23, 2026
Merged

gkhngyk merged 19 commits into
ansvisor:mainfrom
cihan-gyk:feature/page-rewrite-web

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Rewrite the page to these rules, in the editor: the Rules tab opens with the agent's two ways to meet its rules, recommends a rewrite when small edits can't get there, and a rewritten page comes back as one change to compare, accept, reject or undo.

Stacked on #1393 (the server half: POST /api/content-drafts/:id/rewrite, the page suggestion kind, 00247), which is stacked on #1387. Merge after both. The PR's own commits are 242ed53 and the review fixes on top.

Drive's Editor v1.1 spec (2026-10-23) replaces these two buttons with one split Run Agent ▾ (Fix open rules / Rewrite the whole page). That comes next, stacked on this PR.

Rules tab

  • At the top: Apply rules with Agent (small edits, up to 8 a run) and Rewrite the page to these rules (the whole page as one change), with a one-line hint.
  • Recommended: rewriteAdvice recommends the rewrite when the page is more than 30% off its Length ("the page is 19,203 characters for a Length of 8,000 (140% off)…") or when 4 rules or more that the agent can fix are open. It says nothing for an empty page, or one over 60,000 characters (the server's limit).
  • The recommended button is the primary one.
  • One agent run at a time: each button says why it waits (open suggestions, locked passages still loading, the agent already working).

Starting a rewrite (rewritePage)

  • It waits for the page to save, then sends:
    • the rules in effect, each with how the page reads now, as Apply rules does;
    • the person's own rules;
    • the passages still locked in the editor (never before they're read).
  • The page waits with the rewrite's own steps.
  • If a rewrite fails, Try again rewrites again. This browser remembers it was a rewrite (localStorage) after a reload too.

A rewritten page (_page-rewrite.tsx)

  • Its own bar over the page shows the characters before → after and the agent's reason, with Compare, Reject and Accept. The text gets a line first, so on a narrow page the buttons go under it.
  • Compare shows the page as the agent read it and the rewrite side by side. The rewrite is cleaned as a paste is (cleanHtml, DOMPurify); its links don't navigate.
  • Accept saves first, keeps the page in History (agent_before), records the decision with the editor held still, and puts the rewrite in place.
  • Undo puts the page back, two H1s and all.
  • A page whose words changed since the agent read it is not written over. Nor is a rewrite edited since it went in on Undo. The comparison is by words, so line breaks in stored HTML don't count as a change.
  • A page suggestion never shows as an inline card.

Data

  • DraftSuggestion.kind takes 'page', with beforeHtml, and getDraftSuggestions reads before_html.
  • startPageRewrite shares startAgentWriting's call.
  • AgentWriteError adds empty and too_long.
  • supabase.ts gains before_html.
  • agentWriteRequest takes the locked keys from the editor when it has them.

Related issue

Content Agent rule gaps, item C ("rewrite to length"), as the user specified it on 2026-10-23: one page, two buttons at the top of Rules, recommended at >30% off Length or ≥4 open rules. 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. With feat(content): rewrite a whole page to its rules as one suggestion #1393's server and 00247, open a draft whose text is far over its Length. The Rules tab opens with both buttons and "Recommended: …".
  2. Press Rewrite the page to these rules. The page waits ("Rewriting the page"), then opens with the bar: "Content Agent rewrote the page to its rules · 19,203 → 8,140 characters · …".
  3. Compare shows both pages. Accept puts the rewrite in place, and History has the page as it was. Undo brings the page back. Reject leaves it as it is.

I checked this in the browser on the dev DB with a temporary draft and page suggestion (deleted afterwards):

  • light and dark themes, at 1024 px;
  • the bar's layout, the advice line, and both buttons disabled while the suggestion was open;
  • Compare, Accept with its toast and the rules re-checked, Undo, and Reject.

Tests:

  • rules.test.ts: rewriteAdvice: Length 140% off, 30% exactly, 4 open rules, one the agent can't fix, an empty page, one over 60,000;
  • _rules-tab.test.tsx: buttons first, the advice line, the blocked reason;
  • _page-rewrite.test.ts: the page read alike with or without line breaks and lists, and changed when its words are; the rewrite mark;
  • page.test.tsx ("Rewrite the page to these rules"):
    • the request (rules with how the page reads, locked passages) and the waiting view;
    • Compare / Accept / Undo;
    • stored line breaks are no change;
    • Undo refused after an edit;
    • Try again after a failed rewrite;
    • a page changed since it was read;
  • content-drafts.test.ts: a page row read with its before_html.

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 and yarn build.

🤖 Generated with Claude Code

cihan-gyk and others added 9 commits October 23, 2026 02:24
The workspace's one AI budget (Improve, the Content Editor's AI and the
Action Center's task writer) had no meter: the first sign of it was an
"AI usage is used up" error. Migration 00242 records what each AI call
was for (prompt_improve_usage.feature, backfilled) and sums the period's
spend by feature (ai_usage_by_feature). GET /api/billing/ai-usage
returns the shares of the plan's budget, and the Billing page shows the
share used, when it resets and what used it, with Agent Chat noted as
running on the workspace's own key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The workspace's AI budget was checked by reading the period's spend; the
call's cost was written once the model answered. Calls running at once
(two tabs, a teammate, an Action Center sweep) all read the same spend
and all ran past the budget. 00243's reserve_ai_usage takes a
per-organization lock, reads the spend with the calls in flight, and
holds the call's estimated cost; reserveAiBudget turns the hold into the
real cost or releases it. Improve, the brief rebuild, Apply rules with
Agent, Start Agent and the Action Center task writer use it in place of
enforceImproveQuota, which goes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes:
- Apply rules with Agent and Start Agent charge an answer the provider
  billed but that missed the schema (NoObjectGeneratedError), as Improve,
  the brief and the task writer do; before, the hold was let go and the
  call ran free.
- A call holds the org's median cost over its last 20 calls this period,
  never less than the model's typical cost, so calls in flight pass the
  budget by far less.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Apply rules with Agent makes at most 8 small edits a run, which can't
bring a long page to its Length (about 2% a run on prod). The Content
Agent can now rewrite the whole page in one background call, left as ONE
suggestion (kind 'page', migration 00247) to compare and accept or reject:
- POST /api/content-drafts/:draftId/rewrite, through Start Agent's claim,
  budget hold and background run;
- passages AI quotes today, tables, images, quotes and HTML blocks go to
  the model as [[KEEP n]] lines and come back as they were; a rewrite that
  drops one fails the run;
- one H1, only the page's own links and the ones offered, the editor's
  tags; the page as it was is kept on the suggestion (before_html).

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

Review fixes:
- Kept blocks are described under the page (a quoted passage's words, a
  table's rows, an image's alt) and their text counts toward Length, so a
  rewrite aims at the whole page's length; with Length off it keeps the
  page's own. A kept H1 stays first, and the page's language is kept.
- The answer is taken out of a code fence, text left at its root goes in
  paragraphs, and an answer in Markdown fails the run.
- The page's own links stay exactly as written (query and anchor too).
- Kept blocks go back untouched (an empty table cell stays), only the
  page's first H1 counts as its title, a page that writes "[[KEEP" gets
  markers of its own, and a page that is only kept blocks isn't sent.

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

The Rules tab opens with the agent's two ways to meet its rules: Apply
rules with Agent (small edits, up to 8 a run) and Rewrite the page to
these rules (the whole page as one change). The rewrite is recommended,
with a one-line reason, when the page is more than 30% off its Length or
4 rules or more are open.

A rewritten page comes back as one suggestion with its own bar over the
page: the characters before and after and the agent's reason, Compare
(the page and the rewrite side by side), Accept (History keeps the page
first), Reject, and Undo. A page that changed since the agent read it
isn't written over. The page waits for the rewrite with its own steps,
and a failed rewrite's Try again rewrites again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes:
- A page is compared with the one the agent read by its words, with a
  space between blocks, so stored line breaks and lists don't read as a
  change (Accept and Undo were refused on imported pages).
- One agent run at a time from the page; a rewrite never starts before
  the locked passages are read, and sends the editor's own locked keys.
- Accept holds the page still from the start and checks it again before
  putting the rewrite in; Undo puts the page back as it was (two H1s too),
  and the bar waits while the HTML view is open.
- No rewrite advice for an empty page or one too long to send whole; the
  rewrite mark survives a reload; the waiting view says the rewrite waits
  in the editor; Compare's links don't navigate.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is the web half of "Rewrite the page to these rules". It is stacked on #1393, which is stacked on #1387. It adds the two buttons at the top of Rules with a recommendation line, and a bar to compare, accept, reject or undo the rewritten page; Accept keeps the page in History first. I checked it in the browser with a temporary draft, in light and dark themes at 1024 px. The user's Drive spec for Editor v1.1 (today) replaces the two buttons with one split Run Agent ▾ button. That and the rest of v1.1 (agent card, Instructions tab, the data the agent uses) come next as PRs stacked on this one.

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

A few fixes before merge:

  1. A failed rewrite is known only to the browser that started it. The mark lives in localStorage (_page-rewrite.tsx:55-77), and the waiting view (page.tsx:95) and Try again (_draft-editor.tsx:199, :1177) read it from there. On another device, for another member, or after site data is cleared, the banner shows the usual copy, and Try again calls startAgent() → /write. On a create draft that writes a new page from the brief over the current one (History keeps it), when the person asked for a rewrite to compare. Read which run it was from the draft instead (the column suggested on #1393, selected in getContentDraft), and drop the localStorage mark.

  2. (low) getDraftSuggestions (web/src/lib/actions/content-drafts.ts:1377-1386) now selects before_html and after_text for the last 200 rows of every batch, then keeps only the latest batch. Each past rewrite can add up to two full pages to that read. It runs on every editor open, and every 5 s while Fix open rules waits for its batch. Read the latest batch_id first (one row, no text columns), then select only that batch.

  3. (low) Compare's links are pointer-events-none (_page-rewrite.tsx:86), but they can still take focus, and Enter leaves the editor. Drop href from Compare's copy, or give the links tabindex="-1".

Merge note: Vercel deploys on merge, and getDraftSuggestions selects before_html. Apply 00247 before merging this PR. Otherwise every editor's suggestions read fails, and Fix open rules never sees its batch.

Checked and fine:

  • Accept saves first and keeps History.
  • The page is held still while the decision saves, and it is checked again before the rewrite goes in.
  • Undo is refused once the rewrite was edited.
  • The bar waits while the HTML view is open.
  • A page suggestion never becomes a card.
  • Compare is cleaned with cleanHtml.
  • A rewrite waits for the locked passages and sends the editor's own.
  • The three Start/Fix/Rewrite buttons block each other.
  • The i18n keys are all in en.json.
  • The supabase.ts columns match 00247.
  • The merge commit 3747440 carries no changes of its own.

cihan-gyk and others added 6 commits October 23, 2026 14:45
# Conflicts:
#	server/src/lib/content-writer.js
#	server/src/lib/content-writer.test.js
#	supabase/schema.sql
…le it writes

content_drafts.agent_run ('write' or 'rewrite', in 00247 and its guard) is
set when Start or a rewrite claims the draft, so every device knows a failed
rewrite from a failed Start. Fix open rules and the MCP suggest call answer
409 'writing' while a run holds the draft, so its answer is never dropped
after it was charged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le it writes

content_drafts.agent_run ('write' or 'rewrite', in 00247 and its guard) is
set when Start or a rewrite claims the draft, so every device knows a failed
rewrite from a failed Start. Fix open rules and the MCP suggest call answer
409 'writing' while a run holds the draft, so its answer is never dropped
after it was charged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…st batch's rows

The editor reads content_drafts.agent_run, so a failed rewrite is tried again
as one on every device (the localStorage mark is gone). Fix open rules' new
409 'writing' says the agent is already writing. getDraftSuggestions finds
the latest batch first and reads only its rows, and Compare's links can't
take focus.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Fixed in 565449c (after merging #1393, which now carries main):

  1. Which run failed is read from the draft. getContentDraft selects agent_run (feat(content): rewrite a whole page to its rules as one suggestion #1393's column) as draft.agentRun. The waiting view, the failed banner's copy and Try again follow it, so a failed rewrite is tried again as a rewrite on any device. The localStorage mark and its helpers are gone; onAgentStarted passes the run ('write' / 'rewrite'). Fix's new 409 writing has its own message.
  2. (low) getDraftSuggestions reads the latest batch_id first (one row, no text columns), then only that batch's rows.
  3. (low) Compare drops href from its links, so they can't take focus.

Web 2,916 tests + build, lint, typecheck, format pass. Merge note as before: apply 00247 (now with agent_run and the recreated guard) before merging.

cihan-gyk and others added 3 commits October 23, 2026 15:03
A merge brought in a line formatted by another Prettier version; this puts
back main's formatting, so format:check passes.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk I merged main (#1404–#1410) forward through the content stack, bottom-up: #1393 → #1394 → … → #1403 → #1411 → #1412. Each PR is now mergeable again.

@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. Status of the earlier items:

  1. Done. getContentDraft reads agent_run (web/src/lib/actions/content-drafts.ts:220, :245). The waiting view (page.tsx:94), the failed banner and Try again (_draft-editor.tsx:193) follow it, so a failed rewrite is tried again as a rewrite on any device. The localStorage mark is gone, and onAgentStarted passes the run, so the waiting view is right before the next read.
  2. Done. getDraftSuggestions (content-drafts.ts:1376) reads the latest batch_id first, a one-row read that content_draft_suggestions_draft_idx serves. Then it reads only that batch's rows.
  3. Done. Compare's links lose href (_page-rewrite.tsx:66), so they can't take focus.

Also checked:

  • Fix's new 409 writing has its message under suggestions.fixErrors.
  • supabase.ts has agent_run.
  • The merges cefdf36, e8c4d76 and 4d07025 carry no changes of their own.
  • 5f1a4b5 only formats a type the way #1393 has it.
  • It merges cleanly on current main.

Merge note: apply 00247 before merging, since Vercel deploys on merge. getContentDraft now selects agent_run and getDraftSuggestions selects before_html. Without 00247, no draft opens in the editor.

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

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