Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(content): cut a page rewrite to its Length with one more pass, and say when one came out longer (#1515)
Cihan59b146c540f3
diff --git a/server/src/lib/content-writer.js b/server/src/lib/content-writer.jsindex 432e3e3c6..db96600d6 100644--- a/server/src/lib/content-writer.js+++ b/server/src/lib/content-writer.js@@ -79,6 +79,14 @@ import { reserveAiBudget } from './plan-guard.js';/** How long the model may take: nobody waits on the request. */const CALL_TIMEOUT_MS = 180_000;+/** How far a Length's band reaches either side of it (the editor's LENGTH_TOLERANCE). */+const LENGTH_TOLERANCE = 0.2;+/**+ * How far past the top of its Length's band a rewrite may come before one+ * more pass cuts it: 15% of the Length (editor test 2026-10-25, where+ * rewrites for 4,000 came to 5,468, 6,843 and 7,335 characters).+ */+const CUT_PASS_OVER = 0.15;// A 'writing' older than WRITING_STALE_MS (content-agent.js, which Fix open// rules checks too) was left by a server that stopped: it can start again.@@ -563,14 +571,18 @@ async function rewritePage({}),);await budget.charge(usage);+ if (!isPageHtml(object.html)) throw new Error('The agent answered with no page in HTML');+ // Well past its Length: one more pass that only cuts.+ const answer = ruleKeys.has('length')+ ? await cutToLength({ answer: object, page, length, brand, draft, model, run })+ : object;await advanceRun(run, 3);- if (!isPageHtml(object.html)) throw new Error('The agent answered with no page in HTML');// No invented person or quote (CE-044): read in its words as written,// blockquotes too, before what it kept goes back, so the page's own// passages are never touched.const cleaned = cleanPageHtml(- guardPeopleInHtml(object.html, people).html,+ guardPeopleInHtml(answer.html, people).html,rewriteLinks(draft.body_html, linkAllowList(targets)),{ h1Kept: page.h1Token !== null, originalH1: page.h1 },);@@ -595,7 +607,7 @@ async function rewritePage({await stopIfStopped(run);// The data it says it wrote from (CE-033).const { sources } = sourceSuggestion({- named: object.sources,+ named: answer.sources,marks: [],pieces: [],numbered,@@ -614,7 +626,7 @@ async function rewritePage({before_text: '',before_html: draft.body_html,after_text: html,- reason: clean(stripSourceIds(object.reason)).slice(0, 300) || 'Rewrote the page to the rules',+ reason: clean(stripSourceIds(answer.reason)).slice(0, 300) || 'Rewrote the page to the rules',rule_keys: [...ruleKeys],// What it was asked for and came to (00285): a Length it missed isn't// offered again. With Length off, it was asked for none.@@ -636,6 +648,85 @@ async function rewritePage({return { rewritten: true };}+/**+ * One more pass when a rewrite came well past its Length (CUT_PASS_OVER past+ * its band's top): the model cuts its own page to the Length, keeping every+ * heading with its answer, each kept line, the links and the facts. On its+ * own hold on the budget, so a budget the first pass used up skips it. The+ * cut is taken only when it is shorter and keeps every kept line; else, or+ * when it fails, the first rewrite stands (logged). A stop goes through.+ */+async function cutToLength({ answer, page, length, brand, draft, model, run }) {+ const total = ownText({ ...page, html: answer.html }) + page.keptChars;+ const max = Math.round(length * (1 + LENGTH_TOLERANCE));+ if (total <= max + length * CUT_PASS_OVER) return answer;+ const budget = await reserveAiBudget(brand.organization_id, {+ feature: 'content',+ modelId: contentBriefModelId(),+ draftId: draft.id,+ }).catch((err) => {+ logger.info(+ { draftId: draft.id, code: err?.code },+ '[content-writer] no budget to cut further',+ );+ return null;+ });+ if (!budget) return answer;+ try {+ const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>+ generateObject({+ model,+ schema: rewriteSchema,+ system: REWRITE_SYSTEM_PROMPT,+ prompt: cutPrompt({ html: answer.html, page, total, length, max }),+ abortSignal,+ }),+ );+ await budget.charge(usage);+ const cut = isPageHtml(object.html)+ ? ownText({ ...page, html: object.html }) + page.keptChars+ : Infinity;+ // Each kept line on its own, as restoreKept puts them back: one written+ // into a paragraph would fail the whole run.+ const keepsAll =+ cut < Infinity &&+ restoreKept(object.html, page.kept, { word: page.word, h1Token: page.h1Token }).missing+ .length === 0;+ if (cut >= total || !keepsAll) {+ logger.info(+ { draftId: draft.id, total, cut },+ '[content-writer] the cut pass kept nothing shorter',+ );+ return answer;+ }+ // The first pass's data and reason: the cut had no data to name, only the page.+ return { ...answer, html: object.html };+ } catch (err) {+ if (isStop(err, run)) throw err;+ if (NoObjectGeneratedError.isInstance(err) && err.usage) await budget.charge(err.usage);+ else await budget.release();+ logger.warn(+ { err: { name: err?.name, message: err?.message }, draftId: draft.id },+ '[content-writer] the cut pass failed; the first rewrite stands',+ );+ return answer;+ } finally {+ // A stop before the call went out leaves the hold unused: it goes (a no-op once charged or kept).+ await budget.release();+ }+}++/** What the cut pass is told: its own page, how long it is and the Length to cut it to. */+function cutPrompt({ html, page, total, length, max }) {+ return `Your rewrite below is about ${total} characters of text, ${page.keptChars} of them in kept blocks, which count toward Length. Its Length is ${length} characters, up to ${max}. Cut it to about ${length}.++Cut repetition, filler, long examples and the least useful detail first. Keep the H1 and every heading, a short answer under each heading, every [[${page.word} n]] line exactly as it is and on its own, the links on their words, the facts and any placeholders in square brackets. Add nothing new. Return the whole page.++${html}++Write in the page's own language.`;+}+/*** The run itself: the page written, or the suggestions left, and the draft* In review; 'failed' when the model fails or leaves nothing to review. Thediff --git a/server/src/lib/content-writer.test.js b/server/src/lib/content-writer.test.jsindex 796c3aa5c..b18a446ce 100644--- a/server/src/lib/content-writer.test.js+++ b/server/src/lib/content-writer.test.js@@ -842,6 +842,120 @@ describe('Rewrite the page to these rules', () => {await expect(run()).resolves.toEqual({ skipped: true });expect(writes.find((w) => w.table === 'content_draft_suggestions')).toBeUndefined();});++ describe('a rewrite well past its Length: one more pass cuts it', () => {+ const cutBudget = {+ charge: vi.fn(async () => {}),+ release: vi.fn(async () => {}),+ keep: vi.fn(async () => {}),+ };+ // A Length of 100 (up to 120): a cut pass past 135.+ const toLength = (length = 100) => ({+ rules: [{ key: 'length', name: 'Length', detail: `aim: ${length} characters` }],+ sources: { ...input().sources, length },+ });+ const long = `<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>${'Long words. '.repeat(10)}</p>`;+ const short = '<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Short.</p>';+ const answers = (...htmls) => {+ for (const [i, html] of htmls.entries()) {+ generateObject.mockResolvedValueOnce({+ object: { html, reason: i ? 'cut it' : 'answered each heading first', sources: [] },+ usage: { inputTokens: 900 + i, outputTokens: 700 + i },+ });+ }+ };+ const row = () => writes.find((w) => w.table === 'content_draft_suggestions').value;+ beforeEach(() => {+ cutBudget.charge.mockClear();+ cutBudget.release.mockClear();+ cutBudget.keep.mockClear();+ reserveAiBudget.mockReset().mockResolvedValue(cutBudget);+ });++ it('cuts its own page to the Length on its own hold, keeping every kept line', async () => {+ answers(long, short);+ await expect(run(toLength())).resolves.toEqual({ rewritten: true });+ expect(generateObject).toHaveBeenCalledTimes(2);+ const { prompt } = generateObject.mock.calls[1][0];+ expect(prompt).toContain('Its Length is 100 characters, up to 120. Cut it to about 100.');+ expect(prompt).toContain('every [[KEEP n]] line exactly as it is');+ expect(prompt).toContain('Long words.');+ expect(reserveAiBudget).toHaveBeenCalledWith('org-1', {+ feature: 'content',+ modelId: expect.any(String),+ draftId: DRAFT,+ });+ expect(cutBudget.charge).toHaveBeenCalledWith({ inputTokens: 901, outputTokens: 701 });+ // The cut page, what it kept put back, the first pass's reason.+ expect(row().after_text).toContain('Short.');+ expect(row().after_text).not.toContain('Long words.');+ expect(row().after_text).toContain(QUOTED);+ expect(row().reason).toBe('answered each heading first');+ expect(row().after_chars).toBe(textLength(row().after_text));+ });++ it('keeps the first rewrite when the cut is no shorter, drops a kept line, fails or has no budget', async () => {+ answers(long, long);+ await run(toLength());+ expect(row().after_text).toContain('Long words.');++ writes = [];+ answers(long, '<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>Short.</p>');+ await run(toLength());+ expect(row().after_text).toContain('Long words.');++ // Kept lines written into a paragraph: restoreKept couldn't put them back.+ writes = [];+ answers(long, '<h1>Pour-over coffee</h1><p>[[KEEP 1]] [[KEEP 2]] Short.</p>');+ await expect(run(toLength())).resolves.toEqual({ rewritten: true });+ expect(row().after_text).toContain('Long words.');++ writes = [];+ generateObject.mockResolvedValueOnce({+ object: { html: long, reason: 'answered each heading first', sources: [] },+ usage: { inputTokens: 900, outputTokens: 700 },+ });+ generateObject.mockRejectedValueOnce(new Error('provider down'));+ cutBudget.charge.mockClear();+ cutBudget.release.mockClear();+ await expect(run(toLength())).resolves.toEqual({ rewritten: true });+ expect(row().after_text).toContain('Long words.');+ // Nothing billed for a failed call: its hold goes.+ expect(cutBudget.charge).not.toHaveBeenCalled();+ expect(cutBudget.release).toHaveBeenCalled();++ writes = [];+ generateObject.mockReset();+ reserveAiBudget.mockRejectedValueOnce(+ Object.assign(new Error('used up'), { code: 'plan_limit' }),+ );+ answers(long);+ await run(toLength());+ expect(generateObject).toHaveBeenCalledTimes(1);+ expect(row().after_text).toContain('Long words.');+ });++ it('keeps the hold of a cut stopped while it runs, and writes nothing', async () => {+ const { RunStoppedError } = await import('./content-runs.js');+ answers(long);+ generateObject.mockRejectedValueOnce(new RunStoppedError());+ cutBudget.keep.mockClear();+ await run(toLength());+ expect(cutBudget.keep).toHaveBeenCalledTimes(1);+ expect(+ writes.find((w) => w.table === 'content_draft_suggestions' && w.kind === 'insert'),+ ).toBeUndefined();+ });++ it('cuts nothing within reach of the Length, or with Length off', async () => {+ answers(long);+ await run(toLength(3000));+ answers(long);+ await run();+ expect(generateObject).toHaveBeenCalledTimes(2);+ expect(reserveAiBudget).not.toHaveBeenCalled();+ });+ });});describe('the write prompt', () => {diff --git a/web/messages/en.json b/web/messages/en.jsonindex 6bdfbf1b9..42b18dde0 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3853,6 +3853,7 @@"ready": "Content Agent rewrote the page to its rules","characters": "{before, number} → {after, number} characters","lengthMissed": "Length {target, number} not reached",+ "longer": "longer than the page before","lengthKept": "({kept, number} characters are passages AI answers quote, tables and other blocks a rewrite keeps as they are)","accepted": "Rewrite accepted","rejected": "Rewrite rejected",@@ -5269,6 +5270,8 @@"lengthOutOfReachRun": "The page is {count, number} characters for a Length of {target, number}. The last rewrite came to {least, number}, so another won't get there. Raise the Length to about {suggested, number}, or unlock passages you're fine to have reworded.","lengthOutOfReachHeadings": "The page is {count, number} characters for a Length of {target, number}, but its headings and an answer under each come to about {least, number}. Raise the Length to about {suggested, number}, or use fewer sections.","raiseLength": "Raise Length to {suggested, number}",+ "rewriteCitedToo": "Rewrite, cited passages too",+ "rewriteCitedTooHint": "That rewrites the passages AI answers quote as well, so they may stop being quoted. You compare it with the page before anything changes.","lengthConflict": "The fan-out H2s, FAQ questions and summary these rules still ask for take about {need, number} characters, more than Length allows (up to {max, number}). Raise Length, or ask for fewer.","fixTrace": "Fix","fixTraceHint": "Content Agent rewrites only these phrases, for you to review",diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsxindex a9f5ed8bf..94aadee92 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.test.tsx@@ -256,6 +256,24 @@ describe('AgentCard', () => {);});+ it('says when a rewrite meant to shorten the page came out longer', async () => {+ const page = suggestion({+ kind: 'page',+ beforeHtml: '<h1>Old</h1><p>Short.</p>',+ afterText: '<h1>New</h1><p>A much longer page than before.</p>',+ reason: 'answered each heading first',+ });+ // Before 10, over the Length's top (5): asked to shorten it, it came out longer.+ await render([page], false, { length: { target: 4, min: 3, max: 5, kept: 0 } });+ expect(container.textContent).toContain(+ '10 → 35 characters · Length 4 not reached · longer than the page before',+ );+ // A page under its Length, asked to grow: longer is what it was asked for.+ await render([page], false, { length: { target: 20, min: 16, max: 24, kept: 0 } });+ expect(container.textContent).toContain('10 → 35 characters · Length 20 not reached');+ expect(container.textContent).not.toContain('longer than the page before');+ });+it('says it is working, with nothing to press', async () => {await render([], true);const card = container.querySelector('[role="region"]')!;diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsxindex 1de361c22..d4354eb9a 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-card.tsx@@ -384,6 +384,11 @@ export function AgentCard({missed.outOfReach &&missed.kept > 0 &&` ${tRewrite('lengthKept', { kept: missed.kept })}`}+ {/* Asked to shorten it, it came out longer: said, not offered silently. */}+ {afterChars > missed.max &&+ before > missed.max &&+ afterChars > before &&+ ` · ${tRewrite('longer')}`}</span>) : (page.reasondiff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.tsindex 648738aad..8c6ee4101 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.ts@@ -8,6 +8,7 @@ import {citedPassagesKey,keptCharacters,lockedPassageCount,+ lockedPassagesLeft,rewriteFloor,type HeldEdit,} from './_cited-passages';@@ -156,9 +157,18 @@ describe('rewriteFloor', () => {});describe('locked passages', () => {- it('marks the quoted passage and counts it', () => {+ it('marks the quoted passage and counts it, and how many a rewrite keeps', () => {expect(lockedPassageCount(editor.state)).toBe(1);+ expect(lockedPassagesLeft(editor.state)).toEqual({ count: 1, chars: QUOTED.length });expect(editor.view.dom.querySelectorAll('.cited-passage')).toHaveLength(1);+ // Unlocked by the person: a rewrite keeps none.+ editor.view.dispatch(+ editor.state.tr.setMeta(citedPassagesKey, {+ labels: new Map([[passageKey(QUOTED), 'Cited by ChatGPT 3×']]),+ unlocked: new Set([passageKey(QUOTED)]),+ }),+ );+ expect(lockedPassagesLeft(editor.state)).toEqual({ count: 0, chars: 0 });});it('reads only the paragraph a keystroke changes, not the whole page', () => {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.tsindex 103715c60..186bfe58f 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.ts@@ -247,6 +247,21 @@ export function rewriteFloor(return { kept, floor: kept + floor };}+/** The quoted passages a rewrite keeps now (locked, not unlocked): how many, and their characters. */+export function lockedPassagesLeft(state: EditorState): { count: number; chars: number } {+ const locks = citedLocks(state);+ let count = 0;+ let chars = 0;+ state.doc.descendants((node) => {+ if (node.isTextblock && isLocked(blockKey(node), locks)) {+ count += 1;+ chars += node.textContent.length;+ }+ return !node.isTextblock;+ });+ return { count, chars };+}+/** How many quoted passages the page has now (the marks the plugin keeps). */export function lockedPassageCount(state: EditorState): number {return citedPassagesKey.getState(state)?.marks.find().length ?? 0;diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsxindex 4fdc78112..48682e270 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx@@ -196,6 +196,7 @@ import {citedLocks,citedPassagesKey,lockedPassageCount,+ lockedPassagesLeft,rewriteFloor,type HeldEdit,} from './_cited-passages';@@ -2638,7 +2639,12 @@ export function DraftEditor({* rewrites the whole page in the background; the page waits for it, then* opens with the rewrite to compare.*/- const rewritePage = async () => {+ /**+ * Rewrite the page to these rules. `citedToo`: the passages AI answers+ * quote are rewritten too (the Rules tab's choice when what they keep puts+ * the Length out of reach); the person compares it before anything changes.+ */+ const rewritePage = async ({ citedToo = false }: { citedToo?: boolean } = {}) => {// Never before the locked passages are read: the rewrite would be told none are.if (!editor || agentBusy || !citedRead) return;setRewriting(true);@@ -2663,7 +2669,7 @@ export function DraftEditor({textRules: rules.textRules.filter(({ rule }) => rule.on).map(({ rule }) => rule.text),},// The passages still locked here, the ones the person didn't unlock.- [...locks.labels.keys()].filter((key) => !locks.unlocked.has(key)),+ citedToo ? [] : [...locks.labels.keys()].filter((key) => !locks.unlocked.has(key)),);// Each rule says how the page reads now, as Fix open rules' do.const statusOf = new Map(inEffect.map(({ rule, status }) => [rule.key, status]));@@ -2688,9 +2694,10 @@ export function DraftEditor({};const pageRewrite: PageRewrite | undefined = canMarkReady? {- run: () => void rewritePage(),+ run: (options) => void rewritePage(options),running: rewriting,reach,+ cited: editor && citedRead ? lockedPassagesLeft(editor.state) : undefined,blocked:aiUsedUp && !rewriting? aiUsedUpdiff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsxindex 369f8c892..ade93bb45 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.test.tsx@@ -186,6 +186,39 @@ describe('the Rules tab', () => {.click(),);expect(change).toHaveBeenCalledWith('length', { value: '4000' });+ // No quoted passage kept: no rewrite of them to offer.+ expect(buttons().some((b) => b.textContent === 'Rewrite, cited passages too')).toBe(false);+ });++ it('offers to rewrite the cited passages too, when they hold the Length out of reach', async () => {+ const pageRewrite = {+ ...fix(),+ // Without the 600 characters of cited passages, the rest fits under 3,600.+ reach: { kept: 2_900, floor: 3_900, last: null },+ cited: { count: 3, chars: 600 },+ };+ await show({ agentFix: fix(), pageRewrite });+ expect(container.textContent).toContain(+ 'That rewrites the passages AI answers quote as well, so they may stop being quoted.',+ );+ await act(async () =>+ buttons()+ .find((b) => b.textContent === 'Rewrite, cited passages too')!+ .click(),+ );+ expect(pageRewrite.run).toHaveBeenCalledWith({ citedToo: true });+ });++ it('doesn’t offer it when the cited passages aren’t what holds the Length', async () => {+ await show({+ agentFix: fix(),+ pageRewrite: {+ ...fix(),+ reach: { kept: 2_900, floor: 3_900, last: null },+ cited: { count: 1, chars: 100 },+ },+ });+ expect(buttons().some((b) => b.textContent === 'Rewrite, cited passages too')).toBe(false);});it('doesn’t offer a rewrite again for a Length the last one missed', async () => {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsxindex c373872c2..b52481393 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx@@ -35,6 +35,7 @@ import {RULE_GROUPS,notForAgentMessage,openRulesForAgent,+ lengthBand,lengthOutOfReach,rewriteAdvice,splitRules,@@ -85,12 +86,15 @@ export interface AgentFix {/** Rewrite the whole page to these rules, as the editor runs it: one suggestion. */export interface PageRewrite {- run: () => void;+ /** `citedToo`: the passages AI answers quote are rewritten too. */+ run: (options?: { citedToo?: boolean }) => void;running: boolean;/** Why it can't run now (suggestions still open), or null. */blocked: string | null;/** What it keeps, the least it comes to and what the last one came to: a Length under them is out of reach. */reach?: RewriteReach;+ /** The passages AI answers quote that it keeps now (locked, not unlocked): how many, their characters. */+ cited?: { count: number; chars: number };}/** Run Agent's two ways: the open rules fixed a few passages at a time, or the whole page. */@@ -255,6 +259,14 @@ export function RulesTab({const outOfReach = pageRewrite? lengthOutOfReach(state.rules, pageRewrite.reach ?? NO_REACH): null;+ // Rewrite the cited passages too: offered when they are what holds the+ // Length out of reach, the rest fitting without them.+ const cited = pageRewrite?.cited;+ const citedHold =+ !!outOfReach &&+ !!cited &&+ cited.count > 0 &&+ (pageRewrite?.reach ?? NO_REACH).floor - cited.chars <= lengthBand(outOfReach.target).max;// The rules the page doesn't meet now, in the tab's order, and those the agent fixes.const open = state.rules.filter(({ rule, status }) => rule.on && status.state === 'bad');const { forAgent, notForAgent } = openRulesForAgent(state.rules);@@ -359,14 +371,29 @@ export function RulesTab({},)}</p>- <Button- size="sm"- variant="outline"- className="h-7 text-xs"- onClick={() => state.change('length', { value: String(outOfReach.suggested) })}- >- {t('raiseLength', { suggested: outOfReach.suggested })}- </Button>+ <div className="flex flex-wrap gap-1.5">+ <Button+ size="sm"+ variant="outline"+ className="h-7 text-xs"+ onClick={() => state.change('length', { value: String(outOfReach.suggested) })}+ >+ {t('raiseLength', { suggested: outOfReach.suggested })}+ </Button>+ {/* The quoted passages kept are what holds it: rewritten too, compared first. */}+ {citedHold && (+ <Button+ size="sm"+ variant="outline"+ className="h-7 text-xs"+ disabled={pageRewrite.running || pageRewrite.blocked !== null}+ onClick={() => pageRewrite.run({ citedToo: true })}+ >+ {t('rewriteCitedToo')}+ </Button>+ )}+ </div>+ {citedHold && <p className="text-muted-foreground">{t('rewriteCitedTooHint')}</p>}</div></div>)}diff --git a/web/src/lib/content/rules.test.ts b/web/src/lib/content/rules.test.tsindex eb98e642f..d4a13690c 100644--- a/web/src/lib/content/rules.test.ts+++ b/web/src/lib/content/rules.test.ts@@ -879,6 +879,17 @@ describe('rewriteAdvice', () => {});// Never a Length past the page as it is.expect(lengthOutOfReach(statuses(4_900, [], '4000'), run)?.suggested).toBe(5_000);+ // Rewritten with its cited passages too (it kept less): still missing says keeping them misses too…+ const citedToo = { ...run, last: { chars: 5_200, target: 4_000, kept: 0 } };+ expect(lengthOutOfReach(page, citedToo)).toMatchObject({ why: 'reached', least: 5_200 });+ // …but coming within the band doesn't say keeping them does.+ expect(+ lengthOutOfReach(page, {+ ...citedToo,+ floor: 5_000,+ last: { ...citedToo.last, chars: 4_500 },+ }),+ ).toMatchObject({ why: 'floor', least: 5_000 });});it('names what a rewrite keeps as why it missed only when that alone puts the Length out of reach', () => {diff --git a/web/src/lib/content/rules.ts b/web/src/lib/content/rules.tsindex 105a933a5..f9611f09c 100644--- a/web/src/lib/content/rules.ts+++ b/web/src/lib/content/rules.ts@@ -861,18 +861,28 @@ export interface LengthOutOfReach {/*** What the last rewrite says of this Length: the characters it came to, when- * it was written to a Length (not with Length off), the page keeps what it- * kept then (nothing unlocked or locked since; the counts differ by about a- * space a block, so a little slack), and it was asked for this Length or a- * shorter one, or missed its own and this one is within that one's band.- * Null when it says nothing (a rewrite to 10,000 says nothing of 3,000).+ * it was written to a Length (not with Length off) and it was asked for+ * this Length or a longer one, or missed its own and this one is within that+ * one's band. Null when it says nothing (a rewrite to 10,000 says nothing of+ * 3,000), or the page keeps less now than it kept then (a passage unlocked+ * since: a rewrite may go shorter). The counts differ by about a space a+ * block, so a little slack. `inReach`: whether coming within the band would+ * say this Length is in reach — not when the page keeps more now than then+ * (one rewritten with its cited passages too, say): keeping less and still+ * missing says keeping more misses too, but not the other way round.*/-function lastRewriteSays(last: LastRewrite | null, target: number, kept: number): number | null {+function lastRewriteSays(+ last: LastRewrite | null,+ target: number,+ kept: number,+): { chars: number; inReach: boolean } | null {if (!last || last.target === null) return null;- if (Math.abs(kept - last.kept) > last.kept * 0.02 + 20) return null;+ const slack = last.kept * 0.02 + 20;+ if (last.kept > kept + slack) return null;const band = lengthBand(last.target);const missedItsOwn = last.chars > band.max && target >= band.min;- return target >= last.target || missedItsOwn ? last.chars : null;+ if (target < last.target && !missedItsOwn) return null;+ return { chars: last.chars, inReach: last.kept >= kept - slack };}/**@@ -910,8 +920,9 @@ export function lengthOutOfReach<T extends { rule: ActiveRule; status: RuleStatureturn null;}const { max } = lengthBand(target);- const reached = lastRewriteSays(reach.last, target, reach.kept);- if (reached !== null && reached <= max) return null;+ const says = lastRewriteSays(reach.last, target, reach.kept);+ if (says?.inReach && says.chars <= max) return null;+ const reached = says && says.chars > max ? says.chars : null;if (reached === null && reach.floor <= max) return null;// The more of the two, so the Length suggested is one a rewrite can reach.const least = Math.max(reach.floor, reached ?? 0);