Repository navigation
feat(content): rewrite the page to its rules from the Rules tab, and compare it - #1394
Conversation
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]>
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]>
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]>
|
@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
left a comment
There was a problem hiding this comment.
A few fixes before merge:
-
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 callsstartAgent()→/write. On acreatedraft 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 ingetContentDraft), and drop the localStorage mark. -
(low)
getDraftSuggestions(web/src/lib/actions/content-drafts.ts:1377-1386) now selectsbefore_htmlandafter_textfor 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 latestbatch_idfirst (one row, no text columns), then select only that batch. -
(low) Compare's links are
pointer-events-none(_page-rewrite.tsx:86), but they can still take focus, and Enter leaves the editor. Drophreffrom Compare's copy, or give the linkstabindex="-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.tscolumns match 00247. - The merge commit 3747440 carries no changes of its own.
# 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]>
|
@gkhngyk Fixed in 565449c (after merging #1393, which now carries main):
Web 2,916 tests + build, lint, typecheck, format pass. Merge note as before: apply 00247 (now with |
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]>
# Conflicts: # supabase/schema.sql
|
@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
left a comment
There was a problem hiding this comment.
Looks good. Status of the earlier items:
- Done.
getContentDraftreadsagent_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, andonAgentStartedpasses the run, so the waiting view is right before the next read. - Done.
getDraftSuggestions(content-drafts.ts:1376) reads the latestbatch_idfirst, a one-row read thatcontent_draft_suggestions_draft_idxserves. Then it reads only that batch's rows. - Done. Compare's links lose
href(_page-rewrite.tsx:66), so they can't take focus.
Also checked:
- Fix's new 409
writinghas its message undersuggestions.fixErrors. supabase.tshasagent_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
left a comment
There was a problem hiding this comment.
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).
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.
Rules tab
rewriteAdvicerecommends 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).Starting a rewrite (
rewritePage)A rewritten page (
_page-rewrite.tsx)cleanHtml, DOMPurify); its links don't navigate.agent_before), records the decision with the editor held still, and puts the rewrite in place.Data
DraftSuggestion.kindtakes'page', withbeforeHtml, andgetDraftSuggestionsreadsbefore_html.startPageRewritesharesstartAgentWriting's call.AgentWriteErroraddsemptyandtoo_long.supabase.tsgainsbefore_html.agentWriteRequesttakes 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
I checked this in the browser on the dev DB with a temporary draft and page suggestion (deleted afterwards):
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"):content-drafts.test.ts: a page row read with itsbefore_html.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) (0 errors; the 6 warnings are in untouched files, as on main)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run: web
yarn testandyarn build.🤖 Generated with Claude Code