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): pass Next's third step only by its own run and while the page reads as it did, drop a failed or stopped run's attempt, and show the step as passed (#1550 review) (#1556)

Cihanc00e0f0636d2
diff --git a/web/messages/en.json b/web/messages/en.json
index cd4c6f8af..37f05092e 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -4595,6 +4595,7 @@
"path": "All steps",
"skip": "Skip this step",
"skipped": "· skipped",
+ "passed": "· yours in the Rules tab",
"working": "Content Agent is working",
"stop": "Stop",
"marking": "Marking it ready…",
@@ -4605,6 +4606,7 @@
"done": "done",
"now": "now",
"skipped": "skipped",
+ "passed": "taken by Content Agent, what's left is yours in the Rules tab",
"ahead": "to do"
},
"steps": {
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 66b789c8d..fe0000f0d 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
@@ -159,10 +159,13 @@ import {
nextStep,
rulesForNext,
stepState,
+ thirdPassOf,
+ thirdStepPasses,
thirdStepTried,
type NextAction,
type NextStepId,
type NextStepState,
+ type ThirdStepId,
type ThirdTry,
} from '@/lib/content/next-step';
import {
@@ -236,13 +239,14 @@ import { HeaderTip } from './_header-tip';
import {
NextStepButton,
readSkips,
- readThirdTry,
+ readThirdStep,
+ type ThirdStepStore,
stepActor,
stepName,
stepReason,
wordsUnderTitle,
writeSkips,
- writeThirdTry,
+ writeThirdStep,
} from './_next-step';
import { qualitySuggestions, useTodoCopy, type HandOff, type QualityRead } from './_quality-tab';
import { PageTypeMenu } from './_page-type-menu';
@@ -306,6 +310,8 @@ const FIX_WAIT_MS = 90_000;
/** The runs the agent card shows: Fix open rules', or the brief's. */
// Fix open rules, and Quality's hand-off (CE-051): the same card, steps and Stop.
const FIX_RUNS = ['fix', 'handoff'] as const;
+/** How long after the page's read a third-step pass waits for the rules' deferred read. */
+const PASS_SETTLE_MS = 250;
const BRIEF_RUNS = ['brief'] as const;
/** How a run whose answer was lost on the way ended, as the page and the run's row say. */
@@ -438,27 +444,52 @@ export function DraftEditor({
// The agent's last run failed (Fix or hand-off): the card says so over an earlier batch.
const [lastRunFailed, setLastRunFailed] = useState(false);
const suggestionsNow = useRef(suggestions);
- // The agent's run on the Next step basket's third step (Fix open rules,
- // Rewrite page), and that step as it is now: once the run's changes are
- // decided, Next moves on to the fourth (thirdStepTried).
- const [thirdTry, setThirdTry] = useState<ThirdTry | null>(() => readThirdTry(draft.id));
- const thirdTryNow = useRef(thirdTry);
+ // The agent on the Next step basket's third step (Fix open rules, Rewrite
+ // page): its run, and the steps it took with what the page read then; and
+ // the step as it is now. Once the run's changes are decided, Next moves on
+ // (thirdStepTried, thirdStepPasses).
+ const [thirdStep, setThirdStep] = useState<ThirdStepStore>(() => readThirdStep(draft.id));
+ const thirdStepNow = useRef(thirdStep);
const nextStepNow = useRef<NextStepId | null>(null);
- const saveThirdTry = (attempt: ThirdTry | null) => {
- thirdTryNow.current = attempt;
- setThirdTry(attempt);
- writeThirdTry(draft.id, attempt);
+ // An attempt kept from before the editor opened, not yet taken: whether
+ // its run was read (below). Until then no step passes on it.
+ const [attemptChecked, setAttemptChecked] = useState(
+ () => !thirdStep.attempt || thirdStep.attempt.ended === true,
+ );
+ const saveThirdStep = (store: ThirdStepStore) => {
+ thirdStepNow.current = store;
+ setThirdStep(store);
+ writeThirdStep(draft.id, store);
};
+ const saveThirdTry = (attempt: ThirdTry | null) =>
+ saveThirdStep({ ...thirdStepNow.current, attempt });
/**
- * Notes a run the third step takes, from the batch on the page now; the
- * attempt before it, to put back when the run doesn't happen or is stopped.
- * Null when the third step isn't Next's.
+ * Notes a run the third step takes, from the batch on the page now, while
+ * Next's step (`at`, as it was when the run was asked for) is the third: a
+ * rewrite takes the Length, a Fix takes Fix open rules, and only when it's
+ * asked for every rule the step counts (Remove AI traces alone isn't the
+ * step). The attempt before it, to put back when the run doesn't happen or
+ * is stopped; null when it isn't the step's run.
*/
- const beginThirdTry = (): { prev: ThirdTry | null } | null => {
- if (nextStepNow.current !== 'length' && nextStepNow.current !== 'fixRules') return null;
- const prev = thirdTryNow.current;
- if (prev?.passed) return null;
- saveThirdTry({ batch: suggestionsNow.current[0]?.batchId ?? null });
+ const beginThirdTry = (
+ kind: ThirdTry['kind'],
+ { only, at = nextStepNow.current }: { only?: string[]; at?: NextStepId | null } = {},
+ ): { prev: ThirdTry | null } | null => {
+ if (at !== 'length' && at !== 'fixRules') return null;
+ if (
+ kind === 'fix' &&
+ only &&
+ !rulesForNext(rules.rules, heldRules).every((key) => only.includes(key))
+ ) {
+ return null;
+ }
+ const prev = thirdStepNow.current.attempt;
+ saveThirdTry({
+ step: kind === 'rewrite' ? 'length' : 'fixRules',
+ kind,
+ at: new Date().toISOString(),
+ batch: suggestionsNow.current[0]?.batchId ?? null,
+ });
return { prev };
};
const [deciding, setDeciding] = useState(false);
@@ -2374,7 +2405,7 @@ export function DraftEditor({
fixDraftRules(draft.id, draft.brandId, overLength ? { ...request, overLength } : request),
undefined,
false,
- beginThirdTry(),
+ beginThirdTry('fix', { only }),
);
};
@@ -2420,7 +2451,8 @@ export function DraftEditor({
if (third) saveThirdTry(third.prev);
};
const nothing = () => {
- if (third) saveThirdTry({ ...(thirdTryNow.current ?? { batch: null }), ended: true });
+ const now = thirdStepNow.current.attempt;
+ if (third && now) saveThirdTry({ ...now, ended: true });
};
const failed = (key: string, error: string) => {
untried();
@@ -2914,6 +2946,8 @@ export function DraftEditor({
toast.error(t(`agentFailed.errors.${result.error}`));
return;
}
+ // A page written again: the third step is the new page's.
+ saveThirdStep({ attempt: null, passes: {} });
onAgentStarted?.('write');
} catch (err) {
console.error('Failed to start the Content Agent again:', err);
@@ -2958,6 +2992,8 @@ export function DraftEditor({
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 || restoringNow.current) return;
+ // Next's step as the person asked for it, before the waits below.
+ const stepAsked = nextStepNow.current;
setRewriting(true);
try {
await autosave.settle();
@@ -3000,7 +3036,7 @@ export function DraftEditor({
return;
}
// Under way: the page it leaves takes Next's third step, if it is that.
- beginThirdTry();
+ beginThirdTry('rewrite', { at: stepAsked });
onAgentStarted?.('rewrite');
} catch (err) {
console.error('Failed to start the page rewrite:', err);
@@ -3161,20 +3197,13 @@ export function DraftEditor({
[qualityOn, pageHtml],
);
const trustNow = quality?.trust?.score ?? null;
- // Taken, it stays passed: a later run (a hand-off, Run Agent on step 4)
- // doesn't bring the step back while it works or its changes wait.
- const thirdTried = thirdStepTried(thirdTry, {
+ // The run's changes decided (or it found nothing): its step is passed once
+ // the steps' reads are in (below), with what the page reads then.
+ const thirdTaken = thirdStepTried(thirdStep.attempt, {
batch: suggestions[0]?.batchId ?? null,
open: openSuggestions,
busy: agentWorking || rewriting,
});
- useEffect(() => {
- if (!thirdTried || !thirdTry || thirdTry.passed) return;
- const passed = { ...thirdTry, passed: true };
- thirdTryNow.current = passed;
- writeThirdTry(draft.id, passed);
- setThirdTry(passed);
- }, [thirdTried, thirdTry, draft.id]);
const nextState: NextStepState = {
method: draft.method,
open: canMarkReady,
@@ -3188,7 +3217,7 @@ export function DraftEditor({
? { trust: trustNow, trustLow: trustNow !== null && trustNow < TRUST_GATE, todos }
: null,
skipped,
- thirdTried,
+ thirdPasses: thirdStep.passes,
};
const next = nextStep(nextState);
useEffect(() => {
@@ -3232,6 +3261,82 @@ export function DraftEditor({
// nextState is read as it is when a skip goes stale.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [staleSkips, draft.id]);
+ // A step the agent took passes with the rules open and the Length as they
+ // read once its changes are decided: while they read so, Next moves on; a
+ // rule the agent never saw, or a Length that moved, brings it back. The
+ // rules read the page a beat behind a decision (PAGE_READ_DELAY_MS, then a
+ // deferred read), so the pass waits for that, and keeps the page as it
+ // reads after; and for an attempt kept from before, for its run to be read.
+ const nextStateNow = useRef(nextState);
+ useEffect(() => {
+ nextStateNow.current = nextState;
+ });
+ useEffect(() => {
+ if (!thirdTaken || !nextRead || !attemptChecked) return;
+ const timer = setTimeout(() => {
+ const { attempt, passes } = thirdStepNow.current;
+ if (!attempt) return;
+ saveThirdStep({
+ attempt: null,
+ passes: { ...passes, [attempt.step]: thirdPassOf(nextStateNow.current) },
+ });
+ }, PAGE_READ_DELAY_MS + PASS_SETTLE_MS);
+ return () => clearTimeout(timer);
+ // pageHtml: each read of the page starts the wait again.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [thirdTaken, nextRead, attemptChecked, pageHtml]);
+ // A pass that no longer holds is dropped, as a stale skip is, so a page
+ // that reads as it did again later doesn't bring it back to life.
+ const stalePasses =
+ nextRead && Object.keys(thirdStep.passes).length !== thirdStepPasses(nextState).size;
+ useEffect(() => {
+ if (!stalePasses) return;
+ const held = thirdStepPasses(nextStateNow.current);
+ const { attempt, passes } = thirdStepNow.current;
+ saveThirdStep({
+ attempt,
+ passes: Object.fromEntries(
+ Object.entries(passes).filter(([id]) => held.has(id as ThirdStepId)),
+ ),
+ });
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [stalePasses]);
+ // An attempt whose run failed or was stopped while the editor was closed
+ // (a rewrite runs on its waiting page) is none: a later batch from anything
+ // else must not pass the step. A Stop writes stopped_at before the run's
+ // outcome, so a stopped run with no outcome yet is stopped. A run still
+ // under way is left to finish: its batch passes the step.
+ useEffect(() => {
+ const attempt = thirdStepNow.current.attempt;
+ if (!attempt || attempt.ended) return;
+ if (attempt.kind === 'rewrite' && failedRewrite) {
+ saveThirdTry(null);
+ setAttemptChecked(true);
+ return;
+ }
+ let live = true;
+ readAgentRun(draft.id, [attempt.kind])
+ .then(
+ (run) => {
+ // Its own run: begun with it (the server's clock may differ a little).
+ if (!live || !run || Date.parse(run.startedAt) < Date.parse(attempt.at) - 60_000) {
+ return;
+ }
+ if (run.outcome === 'failed' || run.outcome === 'stopped' || run.stoppedAt) {
+ saveThirdTry(null);
+ }
+ },
+ () => {},
+ )
+ .finally(() => {
+ if (live) setAttemptChecked(true);
+ });
+ return () => {
+ live = false;
+ };
+ // Once, as the editor opens.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [draft.id]);
// It starts open on a new page, and folds once its first step (the brief)
// is done; Skip or another step doesn't fold it.
const nextShownOnce = useRef<'no' | 'open' | 'done'>('no');
@@ -3442,7 +3547,7 @@ export function DraftEditor({
// suggestions set aside are no decision of the agent's.
if (!samePage) {
setBarClosed(true);
- saveThirdTry(null);
+ saveThirdStep({ attempt: null, passes: {} });
}
} catch (err) {
console.error('Failed to restore the version:', err);
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_next-step.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_next-step.tsx
index 14b67260b..1e247efa9 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_next-step.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_next-step.tsx
@@ -14,6 +14,7 @@ import {
type NextStepId,
type NextStepState,
type PathState,
+ type ThirdPasses,
type ThirdTry,
} from '@/lib/content/next-step';
import { cn } from '@/lib/utils';
@@ -52,23 +53,45 @@ export function writeSkips(draftId: string, skips: Partial<Record<NextStepId, st
}
}
-/** The agent's run on the third step on this draft, in this browser (thirdStepTried). */
+/**
+ * The agent on the third step on this draft, in this browser: the run under
+ * way or waiting for a decision (thirdStepTried), and the steps it took with
+ * what the page read then (thirdStepPasses). An attempt kept before these
+ * (#1550's, with no step) is read as none.
+ */
+export interface ThirdStepStore {
+ attempt: ThirdTry | null;
+ passes: ThirdPasses;
+}
+const NO_THIRD_STEP: ThirdStepStore = { attempt: null, passes: {} };
const thirdTryKey = (draftId: string) => `aeo:next-step-third-try:${draftId}`;
-export function readThirdTry(draftId: string): ThirdTry | null {
+export function readThirdStep(draftId: string): ThirdStepStore {
try {
- const read = JSON.parse(localStorage.getItem(thirdTryKey(draftId)) ?? 'null') as unknown;
- if (read && typeof read === 'object' && 'batch' in read) return read as ThirdTry;
+ const read = JSON.parse(localStorage.getItem(thirdTryKey(draftId)) ?? 'null') as {
+ attempt?: unknown;
+ passes?: unknown;
+ } | null;
+ if (read && typeof read === 'object' && 'passes' in read) {
+ const attempt = read.attempt as ThirdTry | null;
+ return {
+ attempt: attempt && typeof attempt === 'object' && 'step' in attempt ? attempt : null,
+ passes: read.passes && typeof read.passes === 'object' ? (read.passes as ThirdPasses) : {},
+ };
+ }
} catch {
// Storage unavailable (private mode) or unreadable: none taken.
}
- return null;
+ return NO_THIRD_STEP;
}
-export function writeThirdTry(draftId: string, attempt: ThirdTry | null) {
+export function writeThirdStep(draftId: string, store: ThirdStepStore) {
try {
- if (attempt) localStorage.setItem(thirdTryKey(draftId), JSON.stringify(attempt));
- else localStorage.removeItem(thirdTryKey(draftId));
+ if (store.attempt || Object.keys(store.passes).length) {
+ localStorage.setItem(thirdTryKey(draftId), JSON.stringify(store));
+ } else {
+ localStorage.removeItem(thirdTryKey(draftId));
+ }
} catch {
// Kept for this visit only.
}
@@ -189,6 +212,7 @@ const PATH_DOT: Record<PathState, string> = {
done: 'bg-foreground text-background',
now: 'bg-foreground/10 text-foreground ring-1 ring-foreground',
skipped: 'bg-muted text-muted-foreground',
+ passed: 'bg-muted text-muted-foreground',
ahead: 'bg-muted text-muted-foreground',
};
@@ -348,7 +372,8 @@ export function NextStepButton({
'flex items-center gap-2 text-[13px]',
p.state === 'done' && 'text-muted-foreground line-through',
p.state === 'now' && 'font-medium',
- (p.state === 'ahead' || p.state === 'skipped') && 'text-muted-foreground',
+ (p.state === 'ahead' || p.state === 'skipped' || p.state === 'passed') &&
+ 'text-muted-foreground',
)}
>
<span
@@ -362,9 +387,9 @@ export function NextStepButton({
</span>
<span className="min-w-0 [overflow-wrap:anywhere]">
{stepName(t, p.id, state)}
- {p.state === 'skipped' && (
+ {(p.state === 'skipped' || p.state === 'passed') && (
<span aria-hidden className="ml-1.5 text-xs no-underline">
- {t('skipped')}
+ {t(p.state)}
</span>
)}
<span className="sr-only"> · {t(`states.${p.state}`)}</span>
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 6335adc93..c3fd633af 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
@@ -6531,6 +6531,20 @@ describe('the side panel', () => {
});
// Where Next goes from Fix open rules on this page: step 4.
const MOVED_ON = 'Next · Add proof and author';
+ // A pass waits for the rules' read of the page (PAGE_READ_DELAY_MS and a beat).
+ const settle = () => act(() => new Promise((resolve) => setTimeout(resolve, 700)));
+ const stored = () => JSON.parse(localStorage.getItem('aeo:next-step-third-try:d1') ?? 'null');
+ const LONG = {
+ method: 'create' as const,
+ pageType: 'blog_post',
+ bodyHtml: `<h1>Pipeline forecasting</h1>${'<p>Teams trust the forecast because it reads the pipeline every day.</p>'.repeat(30)}`,
+ rules: {
+ set: null,
+ source: 'default' as const,
+ overrides: { length: { value: '400' } },
+ textRules: [],
+ },
+ };
const runFix = async () => {
await renderPage();
expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
@@ -6563,19 +6577,251 @@ describe('the side panel', () => {
await act(async () =>
[...card.querySelectorAll('button')].find((b) => b.textContent === 'Reject all')!.click(),
);
- await act(async () => {});
+ await settle();
// Rejected, the fan-out rule is still open, and Next moves on.
expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ // The path says it was passed, not done.
+ await act(async () => nextButton()!.click());
+ expect(basket()!.textContent).toContain('Fix open rules· yours in the Rules tab');
+ // Passed with what the page read then: the rule left open.
expect(JSON.parse(localStorage.getItem('aeo:next-step-third-try:d1')!)).toEqual({
+ attempt: null,
+ passes: { fixRules: { rules: expect.arrayContaining(['fanoutH2']), length: '' } },
+ });
+ });
+
+ it('isn’t passed by a Fix of some of its rules only (#1550 review)', async () => {
+ getContentDraft.mockResolvedValue(
+ draft({
+ method: 'create',
+ pageType: 'blog_post',
+ bodyHtml: '<h1>Pipeline forecasting</h1><p>We delve into how teams forecast.</p>',
+ rules: {
+ set: null,
+ source: 'default',
+ overrides: { length: { on: false } },
+ textRules: [],
+ },
+ }),
+ );
+ getDraftSuggestions.mockResolvedValue([]);
+ // It finds nothing to change: a run of the whole step would pass it.
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await renderPage();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
+ // The Rules tab's Fix on Remove AI traces alone isn't the step.
+ await act(async () =>
+ container
+ .querySelector<HTMLButtonElement>('header button[aria-label^="Rules "]')!
+ .click(),
+ );
+ const fixTraces = [
+ ...container.querySelectorAll<HTMLButtonElement>('aside li button'),
+ ].find((b) => b.textContent === 'Fix');
+ expect(fixTraces).toBeDefined();
+ await act(async () => fixTraces!.click());
+ await act(async () => {});
+ expect(fixDraftRules.mock.calls[0][2].rules.map((r: { key: string }) => r.key)).toEqual([
+ 'aiTraces',
+ ]);
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
+ });
+
+ it('on the Length step, isn’t passed by a Fix, and is by a rewrite once decided', async () => {
+ getContentDraft.mockResolvedValue(draft(LONG));
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await renderPage();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Shorten to your Length');
+ // The step's other button, Fix open rules: it takes Fix open rules, not the Length.
+ await act(async () => nextButton()!.click());
+ await act(async () => inBasket('Fix open rules')!.click());
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Shorten to your Length');
+ expect(stored().passes).toEqual({ fixRules: expect.any(Object) });
+
+ // A rewrite begun on it, then the waiting page; its page decided.
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: { step: 'length', kind: 'rewrite', at: new Date().toISOString(), batch: null },
+ passes: {},
+ }),
+ );
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'p9',
+ batchId: 'b9',
+ actionId: null,
+ actionNo: null,
+ kind: 'page' as const,
+ blockText: 'Pipeline forecasting',
+ beforeText: '',
+ beforeHtml: LONG.bodyHtml,
+ afterText: LONG.bodyHtml,
+ reason: 'rewrote the page to the rules',
+ ruleKeys: ['length'],
+ status: 'rejected' as const,
+ },
+ ]);
+ await act(async () => root.unmount());
+ root = createRoot(container);
+ await renderPage();
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).not.toBe('Next · Shorten to your Length');
+ expect(stored().passes).toEqual({ length: expect.any(Object) });
+ });
+
+ it.each([
+ ['stopped', { outcome: 'stopped' }],
+ ['failed', { outcome: 'failed' }],
+ // A Stop writes stopped_at before the outcome.
+ ['stopped, its outcome not yet written', { outcome: null }],
+ ])('drops an attempt whose run was %s while the page was closed', async (_, ended) => {
+ const attempt = {
+ step: 'fixRules',
+ kind: 'rewrite',
+ at: new Date(Date.now() - 60_000).toISOString(),
batch: null,
- passed: true,
+ };
+ localStorage.setItem('aeo:next-step-third-try:d1', JSON.stringify({ attempt, passes: {} }));
+ readAgentRun.mockResolvedValue({
+ id: 'r1',
+ kind: 'rewrite',
+ steps: [],
+ step: 0,
+ startedAt: new Date().toISOString(),
+ finishedAt: new Date().toISOString(),
+ stoppedAt: ended.outcome === 'failed' ? null : new Date().toISOString(),
+ ...ended,
+ });
+ // A batch from something else, decided: it mustn't pass the step.
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'n7',
+ batchId: 'b7',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ blockText: 'Teams trust it.',
+ beforeText: 'trust',
+ afterText: 'rely on',
+ reason: 'a Quality check (hand-off)',
+ ruleKeys: [],
+ status: 'rejected' as const,
+ },
+ ]);
+ await renderPage();
+ await settle();
+ expect(readAgentRun).toHaveBeenCalledWith('d1', ['rewrite']);
+ expect(JSON.parse(localStorage.getItem('aeo:next-step-third-try:d1') ?? 'null')).toBeNull();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
+ });
+
+ it('drops a rewrite’s attempt when the page opens on that rewrite having failed', async () => {
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: { step: 'length', kind: 'rewrite', at: new Date().toISOString(), batch: null },
+ passes: {},
+ }),
+ );
+ getContentDraft.mockResolvedValue(
+ draft({ ...LONG, agentState: 'failed', agentRun: 'rewrite' }),
+ );
+ await renderPage();
+ await act(async () => {});
+ expect(stored()).toBeNull();
+ });
+
+ it('brings the step back once a rule opens that the agent never saw', async () => {
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ // As if fanoutH2 had opened since the step was taken.
+ const key = 'aeo:next-step-third-try:d1';
+ const stored = JSON.parse(localStorage.getItem(key)!);
+ stored.passes.fixRules.rules = stored.passes.fixRules.rules.filter(
+ (rule: string) => rule !== 'fanoutH2',
+ );
+ localStorage.setItem(key, JSON.stringify(stored));
+ await act(async () => root.unmount());
+ root = createRoot(container);
+ await renderPage();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
+ // The pass that no longer holds is dropped, as a stale skip is.
+ await settle();
+ expect(JSON.parse(localStorage.getItem(key) ?? 'null')).toBeNull();
+ });
+
+ it('takes the step when the answer was lost and its run says it found nothing', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ error: 'unconfirmed' });
+ // The run there was before it is none; once it was asked, its row says it found nothing.
+ const run = {
+ id: 'r9',
+ kind: 'fix',
+ steps: [
+ { key: 'read', count: null },
+ { key: 'rules', count: 1 },
+ { key: 'write', count: 0 },
+ ],
+ step: 3,
+ startedAt: new Date().toISOString(),
+ finishedAt: new Date().toISOString(),
+ stoppedAt: null,
+ outcome: 'done',
+ };
+ readAgentRun.mockImplementation(async () => (fixDraftRules.mock.calls.length ? run : null));
+ try {
+ await renderPage();
+ await act(async () => nextButton()!.click());
+ await act(async () => inBasket('Fix open rules')!.click());
+ await act(async () => void (await vi.advanceTimersByTimeAsync(6_000)));
+ await act(async () => void (await vi.advanceTimersByTimeAsync(1_000)));
+ expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('is the restored page’s again after a restore', async () => {
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ getDraftVersions.mockResolvedValue([
+ { id: 'v1', origin: 'import', title: 'Old title', createdAt: '2026-10-12T09:42:00Z' },
+ ]);
+ restoreDraftVersion.mockResolvedValue({
+ title: 'Old title',
+ bodyHtml: '<h1>Old title</h1><p>As it was.</p>',
+ updatedAt: '2026-10-21T10:00:00Z',
+ restored: true,
+ });
+ const trigger = button('History')!;
+ await act(async () => {
+ trigger.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true }));
+ trigger.click();
});
+ await act(async () => {});
+ const items = [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')];
+ await act(async () => items[items.length - 1].click());
+ await act(async () => {});
+ expect(restoreDraftVersion).toHaveBeenCalled();
+ expect(stored()).toBeNull();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
});
it('moves on when it found nothing to change', async () => {
getDraftSuggestions.mockResolvedValue([]);
fixDraftRules.mockResolvedValue({ count: 0 });
await runFix();
+ await settle();
expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
// Opened again, it holds.
await act(async () => root.unmount());
diff --git a/web/src/lib/content/next-step.test.ts b/web/src/lib/content/next-step.test.ts
index b8d9996dd..434c08ad3 100644
--- a/web/src/lib/content/next-step.test.ts
+++ b/web/src/lib/content/next-step.test.ts
@@ -4,6 +4,7 @@ import {
nextStep,
rulesForNext,
stepState,
+ thirdPassOf,
thirdStepTried,
type NextStepState,
} from './next-step';
@@ -243,61 +244,92 @@ describe('nextStep', () => {
});
describe('the third step, once the agent took it (editor test 2026-10-26)', () => {
- it('moves on to proof with a rule still open and the Length off', () => {
- // As on the tested page: Trust 47, under its gate.
- const quality = { trust: 47, trustLow: true, todos: [] };
- const fix = state({ method: 'optimize', openRules: ['h2Answer'], quality });
+ // As on the tested page: Trust 47, under its gate.
+ const quality = { trust: 47, trustLow: true, todos: [] };
+ const fix = state({ method: 'optimize', openRules: ['h2Answer'], quality });
+ const long = state({
+ method: 'optimize',
+ rewrite: { reason: 'length', count: 17_683, target: 13_998, percent: 26 },
+ openRules: ['h2Answer'],
+ quality,
+ });
+
+ it('moves on to proof with a rule still open, and shows the step passed, not done', () => {
expect(nextStep(fix).step.id).toBe('fixRules');
- expect(nextStep({ ...fix, thirdTried: true }).step.id).toBe('proof');
- expect(ids({ ...fix, thirdTried: true })).toEqual([
+ const passed = { ...fix, thirdPasses: { fixRules: thirdPassOf(fix) } };
+ expect(nextStep(passed).step.id).toBe('proof');
+ expect(ids(passed)).toEqual([
'plan:done',
'import:done',
- 'fixRules:done',
+ 'fixRules:passed',
'proof:now',
'check:ahead',
]);
- // A page far off its Length passes its rewrite step the same way.
- const long = state({
- method: 'optimize',
- rewrite: { reason: 'length', count: 17_683, target: 13_998, percent: 26 },
- openRules: ['h2Answer'],
- quality,
- });
- expect(nextStep(long).step.id).toBe('length');
- expect(nextStep({ ...long, thirdTried: true }).step.id).toBe('proof');
// With Quality off, to Mark ready.
- expect(nextStep({ ...long, quality: null, thirdTried: true }).step.id).toBe('check');
+ expect(nextStep({ ...passed, quality: null }).step.id).toBe('check');
+ });
+
+ it('passes each step only by its own run: a Fix never passes the Length step', () => {
+ expect(nextStep(long).step.id).toBe('length');
+ // Fix open rules taken: the Length step still calls for a rewrite.
+ const fixed = { ...long, thirdPasses: { fixRules: thirdPassOf(long) } };
+ expect(nextStep(fixed).step.id).toBe('length');
+ // The rewrite taken: on to Fix open rules, still open, and then proof.
+ const rewritten = { ...long, thirdPasses: { length: thirdPassOf(long) } };
+ expect(nextStep(rewritten).step.id).toBe('fixRules');
+ const both = {
+ ...long,
+ thirdPasses: { length: thirdPassOf(long), fixRules: thirdPassOf(long) },
+ };
+ expect(nextStep(both).step.id).toBe('proof');
+ });
+
+ it('brings a step back once the page moved on: a rule the agent never saw, or the Length', () => {
+ const passes = { fixRules: thirdPassOf(fix) };
+ // The rule it couldn't close, alone or with fewer: still passed.
+ expect(nextStep({ ...fix, thirdPasses: passes }).step.id).toBe('proof');
+ expect(nextStep({ ...fix, openRules: [], thirdPasses: passes }).step.id).toBe('proof');
+ // A rule opened since (turned on, the person's edits): the agent's again.
+ expect(nextStep({ ...fix, openRules: ['faq', 'h2Answer'], thirdPasses: passes }).step.id).toBe(
+ 'fixRules',
+ );
+ // The Length drifted far off since: the Length step, and Fix open rules with it.
+ const drifted = { ...long, thirdPasses: passes };
+ expect(nextStep(drifted).step.id).toBe('length');
+ // A rewrite's pass holds while the Length reads the same, whatever the rules.
+ const rewritten = { ...long, thirdPasses: { length: thirdPassOf(long) } };
+ expect(nextStep({ ...rewritten, openRules: ['faq'] }).step.id).toBe('fixRules');
+ expect(
+ nextStep({
+ ...rewritten,
+ rewrite: { reason: 'length', count: 9_000, target: 13_998, percent: 36 },
+ }).step.id,
+ ).toBe('length');
});
it('keeps the steps before it: an empty page is still written first', () => {
- const s = state({ empty: true, openRules: rules(3), thirdTried: true });
- expect(nextStep(s).step.id).toBe('write');
+ const s = state({ empty: true, openRules: rules(3) });
+ expect(nextStep({ ...s, thirdPasses: { fixRules: thirdPassOf(s) } }).step.id).toBe('write');
});
- it("reads the agent's run as taken once what it left is decided, or it ended with nothing", () => {
- const attempt = { batch: 'b1' };
+ it("reads the agent's run as taken once what it left is decided, or it found nothing", () => {
+ const attempt = { step: 'fixRules' as const, kind: 'fix' as const, at: '', batch: 'b1' };
// The run is under way, or its batch waits for a decision: not yet.
expect(thirdStepTried(attempt, { batch: 'b1', open: 0, busy: true })).toBe(false);
expect(thirdStepTried(attempt, { batch: 'b2', open: 3, busy: false })).toBe(false);
// Its batch, every change decided (accepted or rejected).
expect(thirdStepTried(attempt, { batch: 'b2', open: 0, busy: false })).toBe(true);
- // The batch from before it is no answer of its: stopped, the step stays.
+ // The batch from before it is no answer of its.
expect(thirdStepTried(attempt, { batch: 'b1', open: 0, busy: false })).toBe(false);
- // It found nothing to change: there's nothing to decide.
- expect(thirdStepTried({ ...attempt, ended: true }, { batch: 'b1', open: 0, busy: false })).toBe(
- true,
- );
+ // It found nothing to change: there's nothing to decide, once no run works.
+ const ended = { ...attempt, ended: true };
+ expect(thirdStepTried(ended, { batch: 'b1', open: 0, busy: false })).toBe(true);
+ expect(thirdStepTried(ended, { batch: 'b1', open: 0, busy: true })).toBe(false);
// A first batch, on a page that had none.
- expect(thirdStepTried({ batch: null }, { batch: 'b1', open: 0, busy: false })).toBe(true);
- expect(thirdStepTried(null, { batch: 'b1', open: 0, busy: false })).toBe(false);
- // Found nothing, but another run is under way: not yet.
- expect(thirdStepTried({ ...attempt, ended: true }, { batch: 'b1', open: 0, busy: true })).toBe(
- false,
- );
- // Once taken, a later run working or its changes waiting don't bring it back.
- expect(thirdStepTried({ ...attempt, passed: true }, { batch: 'b3', open: 2, busy: true })).toBe(
+ expect(thirdStepTried({ ...attempt, batch: null }, { batch: 'b1', open: 0, busy: false })).toBe(
true,
);
+ expect(thirdStepTried(null, { batch: 'b1', open: 0, busy: false })).toBe(false);
});
});
diff --git a/web/src/lib/content/next-step.ts b/web/src/lib/content/next-step.ts
index a93aef429..465d1a72f 100644
--- a/web/src/lib/content/next-step.ts
+++ b/web/src/lib/content/next-step.ts
@@ -94,11 +94,12 @@ export interface NextStepState {
/** The steps skipped, each with the state it was skipped at (stepState). */
skipped: Partial<Record<NextStepId, string>>;
/**
- * The agent took the third step (thirdStepTried): it is passed, with rules
- * still open or the Length still off, and what's left is the person's in
- * the Rules tab.
+ * The third-step steps the agent took (thirdStepTried), each with what the
+ * page read then: passed while that holds (thirdStepPasses), with rules
+ * still open or the Length off, and what's left is the person's in the
+ * Rules tab.
*/
- thirdTried?: boolean;
+ thirdPasses?: ThirdPasses;
}
export interface NextStep {
@@ -109,7 +110,8 @@ export interface NextStep {
skippable: boolean;
}
-export type PathState = 'done' | 'skipped' | 'now' | 'ahead';
+/** 'passed': the agent took it, and what's left is the person's (thirdStepPasses). */
+export type PathState = 'done' | 'skipped' | 'passed' | 'now' | 'ahead';
export type TipId = 'longPage' | 'shortPage' | 'someRules' | 'noBrief' | 'lowTrust';
@@ -157,35 +159,69 @@ export function stepState(id: NextStepId, s: NextStepState): string {
}
/** The third step, as the page needs it: its Length, or its open rules. */
-const THIRD_STEPS: readonly NextStepId[] = ['length', 'fixRules'];
+export type ThirdStepId = 'length' | 'fixRules';
/**
- * A run of the agent on the third step (Fix open rules, or Rewrite page
- * while it was the step): the latest batch on the page when it began;
- * whether it found nothing to change; and, once taken, `passed`, which holds
- * whatever runs after. A run that fails, is stopped or never runs isn't one.
+ * A run of the agent on the third step: the step it takes (a rewrite takes
+ * the Length, a Fix of every rule the step counts takes Fix open rules); its kind,
+ * to read how it ended once the editor opens again; when it began; the
+ * latest batch on the page then; and whether it found nothing to change.
+ * A run that fails, is stopped or never runs isn't one.
*/
export interface ThirdTry {
+ step: ThirdStepId;
+ kind: 'fix' | 'rewrite';
+ /** When it began (ISO), against the run row's start. */
+ at: string;
batch: string | null;
ended?: boolean;
- passed?: boolean;
}
/**
- * Whether the agent has taken the third step (editor test 2026-10-26): its
- * run found nothing to change, or left a batch since and every change in it
- * is decided. Next then moves on to the fourth, whatever the rules read
- * after; the page doesn't wait on rules no run of the agent closes.
+ * A third-step step the agent took, with what the page read then: the open
+ * rules a Fix run can close (rulesForNext) and the Length's state.
+ */
+export interface ThirdPass {
+ rules: string[];
+ length: string;
+}
+export type ThirdPasses = Partial<Record<ThirdStepId, ThirdPass>>;
+
+/**
+ * Whether the agent has taken the step its run was for (editor test
+ * 2026-10-26): it found nothing to change, or left a batch since and every
+ * change in it is decided.
*/
export function thirdStepTried(
attempt: ThirdTry | null,
now: { batch: string | null; open: number; busy: boolean },
): boolean {
- if (attempt?.passed) return true;
if (!attempt || now.busy || now.open > 0) return false;
return attempt.ended === true || (now.batch !== null && now.batch !== attempt.batch);
}
+/** What a pass keeps of the page as it reads when the step is taken. */
+export function thirdPassOf(s: NextStepState): ThirdPass {
+ return { rules: s.openRules, length: stepState('length', s) };
+}
+
+/**
+ * The steps passed that still hold: the Length reads as it did and, for Fix
+ * open rules, every rule open now was open when the agent took it (one it
+ * never saw brings the step back, one it couldn't close doesn't). A step
+ * whose page has moved on is the agent's again.
+ */
+export function thirdStepPasses(s: NextStepState): Set<ThirdStepId> {
+ const held = new Set<ThirdStepId>();
+ const length = stepState('length', s);
+ for (const [id, pass] of Object.entries(s.thirdPasses ?? {}) as [ThirdStepId, ThirdPass][]) {
+ if (!pass || pass.length !== length) continue;
+ if (id === 'fixRules' && !s.openRules.every((rule) => pass.rules.includes(rule))) continue;
+ held.add(id);
+ }
+ return held;
+}
+
/** The skips that still hold: one whose step's state has changed since is dropped. */
export function liveSkips(s: NextStepState): Partial<Record<NextStepId, string>> {
return Object.fromEntries(
@@ -284,7 +320,8 @@ export function nextStep(s: NextStepState): NextPath {
alternative: s.published ? null : 'markPublished',
skippable: false,
};
- const tried = (id: NextStepId) => s.thirdTried === true && THIRD_STEPS.includes(id);
+ const passes = thirdStepPasses(s);
+ const tried = (id: NextStepId) => passes.has(id as ThirdStepId);
const step =
due.find((d) => !tried(d.id) && (!d.skippable || s.skipped[d.id] !== stepState(d.id, s))) ??
check;
@@ -314,8 +351,10 @@ export function nextStep(s: NextStepState): NextPath {
? 'ahead'
: // Passed while its state still calls for it: it was skipped,
// unless the agent took it.
- calledFor.has(id) && !tried(id)
- ? 'skipped'
+ calledFor.has(id)
+ ? tried(id)
+ ? 'passed'
+ : 'skipped'
: 'done') as PathState,
}));