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): restore once, and let the agent's batch go with the page before (#1522)

Cihanb25e60f2f4d9
diff --git a/web/messages/en.json b/web/messages/en.json
index f739ad4fb..2d421f035 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3989,7 +3989,9 @@
"restored": "Restored “{version}”. The page before it is in History.",
"restoreFailed": "Couldn't restore it. Try again.",
"htmlOpen": "Go back to the page to use History",
- "restoring": "Restoring…"
+ "restoring": "Restoring…",
+ "agentWorking": "Content Agent is working on the page. History opens once it's done.",
+ "deciding": "Saving your decision on the agent's changes…"
},
"markReadyFailed": "Couldn't mark it ready. Try again.",
"markReadyUnsaved": "The page has changes that aren't saved, so it wasn't marked ready.",
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 9195957be..66aa495dc 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
@@ -415,6 +415,9 @@ export function DraftEditor({
const [rewriting, setRewriting] = useState(false);
const describeRule = useRuleDescriber();
const [restoring, setRestoring] = useState(false);
+ // Set at once, not on the next render: two quick clicks restore once
+ // (editor test 2026-10-25: two "Before a restore" versions in a minute).
+ const restoringNow = useRef(false);
const format = useFormatter();
// A change to a locked passage, waiting for the person (CE-018).
const [held, setHeld] = useState<HeldEdit | null>(null);
@@ -3283,7 +3286,8 @@ export function DraftEditor({
// as the server keeps the page it replaces, and the page can't change
// while the restore runs.
const restoreVersion = async (version: DraftVersion, label: string) => {
- if (!editor || restoring) return;
+ if (!editor || restoringNow.current) return;
+ restoringNow.current = true;
setRestoring(true);
editor.setEditable(false, false);
try {
@@ -3305,11 +3309,33 @@ export function DraftEditor({
detectType(restored.title, html);
toast.success(t('history.restored', { version: label }));
void loadCited();
+ // The agent's open suggestions were for the page before it: they go,
+ // so they hold no later run (open_suggestions). Left open when that
+ // fails, for the person to decide on the card.
+ const stale = suggestionsNow.current.filter((s) => s.status === 'open').map((s) => s.id);
+ const setAside =
+ stale.length === 0 ||
+ (await decideDraftSuggestions(stale, 'rejected').then(
+ () => true,
+ (err) => {
+ console.error('Failed to set aside the suggestions for the page before:', err);
+ return false;
+ },
+ ));
+ if (stale.length && setAside) {
+ setSuggestions((now) =>
+ now.map((s) => (stale.includes(s.id) ? { ...s, status: 'rejected' as const } : s)),
+ );
+ }
+ // Its card goes as if closed, with its Undo all and last run: they were
+ // for the page before (editor test 2026-10-25). A new batch opens it.
+ setBarClosed(true);
} catch (err) {
console.error('Failed to restore the version:', err);
toast.error(t('history.restoreFailed'));
} finally {
editor.setEditable(true, false);
+ restoringNow.current = false;
setRestoring(false);
}
};
@@ -3359,9 +3385,18 @@ export function DraftEditor({
toast(t('pageType.siteChanged', { site: tSites(next) }));
};
- // Why History waits (the HTML view holds the page, a restore runs), or null.
+ // Why History waits (the HTML view holds the page, a restore runs, the
+ // agent's changes land or a decision on them saves), or null.
const historyWaits =
- htmlSource !== null ? t('history.htmlOpen') : restoring ? t('history.restoring') : null;
+ htmlSource !== null
+ ? t('history.htmlOpen')
+ : restoring
+ ? t('history.restoring')
+ : fixing
+ ? t('history.agentWorking')
+ : deciding
+ ? t('history.deciding')
+ : null;
return (
<div className="flex min-h-0 flex-1 flex-col">
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 8d069dfe4..262fa5849 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
@@ -1303,6 +1303,191 @@ describe('the content editor', () => {
expect(container.querySelector('header')!.textContent).toContain('Saved');
});
+ it('restores once for two quick clicks', async () => {
+ getContentDraft.mockResolvedValue(draft());
+ getDraftVersions.mockResolvedValue(versions);
+ restoreDraftVersion.mockReturnValue(new Promise(() => {}));
+ await renderPage();
+ const items = await openHistory();
+ await act(async () => {
+ items[1].click();
+ items[1].click();
+ });
+ // One restore, one "Before a restore" (editor test 2026-10-25: two in a minute).
+ expect(restoreDraftVersion).toHaveBeenCalledTimes(1);
+ });
+
+ it('sets aside the agent’s open suggestions for the page before, and closes its card', async () => {
+ getContentDraft.mockResolvedValue(
+ draft({
+ bodyHtml: '<h1>Pipeline forecasting</h1><p>We delve into how teams forecast.</p>',
+ }),
+ );
+ getDraftVersions.mockResolvedValue(versions);
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'n1',
+ batchId: 'b2',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ blockText: 'We delve into how teams forecast.',
+ beforeText: 'delve into',
+ afterText: 'look at',
+ reason: 'remove an AI trace (rule)',
+ ruleKeys: ['aiTraces'],
+ status: 'open' as const,
+ },
+ ]);
+ restoreDraftVersion.mockResolvedValue({
+ title: 'Old title',
+ bodyHtml: '<h1>Old title</h1><p>As the live page had it.</p>',
+ updatedAt: '2026-10-21T10:00:00Z',
+ });
+ await renderPage();
+ const card = () => container.querySelector('[role="region"][aria-label="Agent suggestions"]');
+ expect(card()).not.toBeNull();
+ const items = await openHistory();
+ await act(async () => items[1].click());
+ await act(async () => {});
+ expect(decideDraftSuggestions).toHaveBeenCalledWith(['n1'], 'rejected');
+ // Its Undo all would undo nothing on the page restored.
+ expect(card()).toBeNull();
+ });
+
+ it('closes the card of a batch already decided: its Undo all and last run were for the page before', async () => {
+ getContentDraft.mockResolvedValue(
+ draft({
+ bodyHtml:
+ '<h1>Pipeline forecasting</h1><p>Teams look at how they forecast.</p><p>Teams trust it.</p>',
+ }),
+ );
+ getDraftVersions.mockResolvedValue(versions);
+ const decided = {
+ batchId: 'b2',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ reason: 'remove an AI trace (rule)',
+ ruleKeys: ['aiTraces'],
+ };
+ getDraftSuggestions.mockResolvedValue([
+ {
+ ...decided,
+ id: 'a1',
+ blockText: 'Teams delve into how they forecast.',
+ beforeText: 'delve into',
+ afterText: 'look at',
+ status: 'accepted' as const,
+ },
+ // Its passage is on the version restored too.
+ {
+ ...decided,
+ id: 'r1',
+ blockText: 'Teams trust it.',
+ beforeText: 'trust',
+ afterText: 'rely on',
+ status: 'rejected' as const,
+ },
+ ]);
+ restoreDraftVersion.mockResolvedValue({
+ title: 'Old title',
+ bodyHtml:
+ '<h1>Old title</h1><p>Teams delve into how they forecast.</p><p>Teams trust it.</p>',
+ updatedAt: '2026-10-21T10:00:00Z',
+ });
+ await renderPage();
+ const card = () => container.querySelector('[role="region"][aria-label="Agent suggestions"]');
+ expect(card()).not.toBeNull();
+ const items = await openHistory();
+ await act(async () => items[1].click());
+ await act(async () => {});
+ // Nothing open to set aside.
+ expect(decideDraftSuggestions).not.toHaveBeenCalled();
+ expect(card()).toBeNull();
+ });
+
+ it('leaves the agent’s open suggestions on its card when they can’t be set aside', async () => {
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ getContentDraft.mockResolvedValue(
+ draft({
+ bodyHtml: '<h1>Pipeline forecasting</h1><p>We delve into how teams forecast.</p>',
+ }),
+ );
+ getDraftVersions.mockResolvedValue(versions);
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'n1',
+ batchId: 'b2',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ blockText: 'We delve into how teams forecast.',
+ beforeText: 'delve into',
+ afterText: 'look at',
+ reason: 'remove an AI trace (rule)',
+ ruleKeys: ['aiTraces'],
+ status: 'open' as const,
+ },
+ ]);
+ decideDraftSuggestions.mockRejectedValue(new Error('offline'));
+ restoreDraftVersion.mockResolvedValue({
+ title: 'Old title',
+ bodyHtml: '<h1>Old title</h1><p>We delve into how teams forecast.</p>',
+ updatedAt: '2026-10-21T10:00:00Z',
+ });
+ await renderPage();
+ const items = await openHistory();
+ await act(async () => items[1].click());
+ await act(async () => {});
+ // Still open on the server: on the card to decide, never shown as set aside.
+ const card = container.querySelector('[role="region"][aria-label="Agent suggestions"]');
+ expect(card).not.toBeNull();
+ expect(
+ [...card!.querySelectorAll('button')].some((b) => b.textContent === 'Accept all'),
+ ).toBe(true);
+ });
+
+ it('waits while a decision on the agent’s changes saves', async () => {
+ getContentDraft.mockResolvedValue(
+ draft({
+ bodyHtml: '<h1>Pipeline forecasting</h1><p>We delve into how teams forecast.</p>',
+ }),
+ );
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'n1',
+ batchId: 'b2',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ blockText: 'We delve into how teams forecast.',
+ beforeText: 'delve into',
+ afterText: 'look at',
+ reason: 'remove an AI trace (rule)',
+ ruleKeys: ['aiTraces'],
+ status: 'open' as const,
+ },
+ ]);
+ let saved: () => void = () => {};
+ decideDraftSuggestions.mockReturnValueOnce(new Promise<void>((r) => (saved = r)));
+ await renderPage();
+ const card = container.querySelector('[role="region"][aria-label="Agent suggestions"]')!;
+ await act(async () =>
+ [...card.querySelectorAll('button')].find((b) => b.textContent === 'Accept all')!.click(),
+ );
+ // A restore now would set the same suggestions aside under the decision.
+ expect(button('History')!.title).toBe("Saving your decision on the agent's changes…");
+ expect(button('History')!.disabled || button('History')!.hasAttribute('data-disabled')).toBe(
+ true,
+ );
+ await act(async () => saved());
+ await act(async () => {});
+ expect(button('History')!.disabled || button('History')!.hasAttribute('data-disabled')).toBe(
+ false,
+ );
+ });
+
it('keeps the page when the restore fails', async () => {
vi.spyOn(console, 'error').mockImplementation(() => {});
getContentDraft.mockResolvedValue(draft());