Skip to content

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.

fix(content): read what the last rewrite came to by the editor's count once it's decided too (#1526)

Cihana31a71cfcd6f
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.tsx
index 66aa495dc..723e76bf9 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
@@ -1938,9 +1938,9 @@ export function DraftEditor({
const openSuggestions = suggestions.filter((s) => s.status === 'open').length;
const pageSuggestion = suggestions.find((s) => s.kind === 'page') ?? null;
// The page's last rewrite, from any run (00285), read on opening and again
- // once a new one is shown: a Length it missed isn't a rewrite's to reach
+ // whenever the rewrite shown changes: a Length it missed isn't a rewrite's to reach
// (rules.ts RewriteReach). A later Fix run's batch doesn't hide it.
- const [lastRewrite, setLastRewrite] = useState<LastRewrite | null>(null);
+ const [lastRewrite, setLastRewrite] = useState<(LastRewrite & { html: string }) | null>(null);
const shownRewriteId = pageSuggestion?.id ?? null;
useEffect(() => {
let cancelled = false;
@@ -1965,12 +1965,19 @@ export function DraftEditor({
// eslint-disable-next-line react-hooks/exhaustive-deps -- the rewrite's two pages, once
[pageSuggestion?.id, pageSuggestion?.afterText, pageSuggestion?.beforeHtml, editor],
);
- // The rewrite shown, counted as the toolbar counts it, not as the API did:
- // its Length band is the editor's.
- const lastCounted =
- lastRewrite && pageChars && lastRewrite.id === pageSuggestion?.id
- ? { ...lastRewrite, chars: pageChars.after }
- : lastRewrite;
+ // The last rewrite counted as the toolbar counts it, not as the API did,
+ // shown or decided since: its Length band is the editor's, and what it came
+ // to reads the same before and after (editor test 2026-10-25).
+ const lastCounted = useMemo(
+ () =>
+ lastRewrite && {
+ target: lastRewrite.target,
+ kept: lastRewrite.kept,
+ chars: lastRewrite.html ? htmlChars(lastRewrite.html) : lastRewrite.chars,
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- its page, once
+ [lastRewrite, editor],
+ );
const reach: RewriteReach = { kept: keptChars, floor: floorChars, last: lastCounted };
// The rules' count stands once My rules and the fan-outs (the fan-out
// rules' own) are read: before, rules come and go from it.
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
index e380ae701..83c1ee0b3 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
@@ -7161,6 +7161,33 @@ describe('the side panel', () => {
container.querySelector('[role="region"][aria-label="Rewritten page"]') as HTMLElement | null;
const page = () => container.querySelector('.ProseMirror')!.textContent;
+ it('reads what the last rewrite came to as the toolbar counts it, shown or decided since', async () => {
+ getContentDraft.mockResolvedValue(
+ draft({
+ pageType: 'blog_post',
+ bodyHtml: `<h1>Pipeline forecasting</h1><p>${'Teams forecast from the pipeline. '.repeat(12).trim()}</p>`,
+ rules: {
+ set: null,
+ source: null,
+ overrides: { length: { value: '100' } },
+ textRules: [],
+ },
+ }),
+ );
+ // Decided since (a later Fix batch shows): the API counted it a little
+ // over; its page reads 1,200 as the editor counts it (4 + 1,196).
+ getLastPageRewrite.mockResolvedValue({
+ chars: 1250,
+ target: 100,
+ kept: 0,
+ html: `<h1>Plan</h1><p>${'x'.repeat(1196)}</p>`,
+ });
+ await openRules();
+ const text = panel()!.textContent;
+ expect(text).toContain('The last rewrite came to 1,200');
+ expect(text).not.toContain('1,250');
+ });
+
it('sends the rules in effect, how the page reads against each, and the page waits', async () => {
getContentDraft.mockResolvedValue(long);
await openRules();
diff --git a/web/src/lib/actions/content-drafts-rewrite.test.ts b/web/src/lib/actions/content-drafts-rewrite.test.ts
index 2de313442..c64362b4f 100644
--- a/web/src/lib/actions/content-drafts-rewrite.test.ts
+++ b/web/src/lib/actions/content-drafts-rewrite.test.ts
@@ -36,15 +36,20 @@ beforeEach(() => {
});
describe('getLastPageRewrite', () => {
- it('reads the numbers of the latest page rewrite, not the page', async () => {
- row = { id: 'p1', after_chars: 5_468, length_target: 4_000, kept_chars: 3_125 };
+ it('reads the latest page rewrite: its numbers and its page, for the editor to count', async () => {
+ row = {
+ after_text: '<h1>Plan</h1><p>Teams forecast.</p>',
+ after_chars: 5_468,
+ length_target: 4_000,
+ kept_chars: 3_125,
+ };
await expect(getLastPageRewrite(DRAFT)).resolves.toEqual({
- id: 'p1',
chars: 5_468,
target: 4_000,
kept: 3_125,
+ html: '<h1>Plan</h1><p>Teams forecast.</p>',
});
- expect(selected).toBe('id, after_chars, length_target, kept_chars');
+ expect(selected).toBe('after_text, after_chars, length_target, kept_chars');
expect(filters).toEqual([
['draft_id', DRAFT],
['kind', 'page'],
@@ -53,14 +58,14 @@ describe('getLastPageRewrite', () => {
it('says nothing for none, or for one made before 00285; Length off keeps its null', async () => {
await expect(getLastPageRewrite(DRAFT)).resolves.toBeNull();
- row = { after_chars: null, length_target: null, kept_chars: null };
+ row = { after_text: '<p>Old.</p>', after_chars: null, length_target: null, kept_chars: null };
await expect(getLastPageRewrite(DRAFT)).resolves.toBeNull();
- row = { id: 'p2', after_chars: 6_000, length_target: null, kept_chars: 900 };
+ row = { after_text: '<p>New.</p>', after_chars: 6_000, length_target: null, kept_chars: 900 };
await expect(getLastPageRewrite(DRAFT)).resolves.toEqual({
- id: 'p2',
chars: 6_000,
target: null,
kept: 900,
+ html: '<p>New.</p>',
});
await expect(getLastPageRewrite('nope')).rejects.toThrow('invalid');
});
diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.ts
index 72add5045..6a27124d7 100644
--- a/web/src/lib/actions/content-drafts.ts
+++ b/web/src/lib/actions/content-drafts.ts
@@ -1859,16 +1859,20 @@ export async function keepAgentBeforeVersion(id: string): Promise<void> {
/**
* The draft's last page rewrite, whatever became of it, as the API kept it
* (00285): the characters it came to, the Length it was written to and what
- * it kept. A Length it missed isn't offered again (rules.ts RewriteReach).
- * Numbers only: the rewritten page isn't read for them. Null without one,
- * or for one made before 00285.
+ * it kept, and the page it wrote, for the editor to count as its toolbar
+ * does (the API's count runs a little over). A Length it missed isn't
+ * offered again (rules.ts RewriteReach). One row, read on opening and
+ * whenever the rewrite shown changes. Null without one, or for one made
+ * before 00285 (after_chars marks those).
*/
-export async function getLastPageRewrite(id: string): Promise<LastRewrite | null> {
+export async function getLastPageRewrite(
+ id: string,
+): Promise<(LastRewrite & { html: string }) | null> {
if (!draftId.safeParse(id).success) throw new Error('invalid');
const supabase = await createClient();
const { data, error } = await supabase
.from('content_draft_suggestions')
- .select('id, after_chars, length_target, kept_chars')
+ .select('after_text, after_chars, length_target, kept_chars')
.eq('draft_id', id)
.eq('kind', 'page')
.order('created_at', { ascending: false })
@@ -1877,10 +1881,10 @@ export async function getLastPageRewrite(id: string): Promise<LastRewrite | null
if (error) throw new Error(error.message);
if (!data || data.after_chars === null || data.kept_chars === null) return null;
return {
- id: data.id,
chars: data.after_chars,
target: data.length_target,
kept: data.kept_chars,
+ html: data.after_text,
};
}
diff --git a/web/src/lib/content/rules.ts b/web/src/lib/content/rules.ts
index 4149a6726..0e5baa029 100644
--- a/web/src/lib/content/rules.ts
+++ b/web/src/lib/content/rules.ts
@@ -838,9 +838,7 @@ export const REWRITE_MAX_CHARS = 60_000;
/** The page's last rewrite, as the API keeps it (00285). */
export interface LastRewrite {
- /** Its suggestion's id, so the one shown is counted as the toolbar counts it. */
- id?: string;
- /** The characters it came to. */
+ /** The characters it came to (the editor counts its page again, as its toolbar does). */
chars: number;
/** The Length it was written to; null with Length off. */
target: number | null;