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): keep Next's Fix open rules passed while the page comes within its Length, wait only for what a pass reads, and read a kept attempt's run by that attempt alone (#1556 review) (#1560)

Cihan6f0c49f5589f
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 fe0000f0d..36a461a74 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
@@ -450,6 +450,10 @@ export function DraftEditor({
// (thirdStepTried, thirdStepPasses).
const [thirdStep, setThirdStep] = useState<ThirdStepStore>(() => readThirdStep(draft.id));
const thirdStepNow = useRef(thirdStep);
+ // What the read of its run (below) found the attempt kept from before to
+ // be now: a run begun while the read was out that then doesn't run puts
+ // that back, not the attempt as it was.
+ const attemptRead = useRef<{ of: ThirdTry; now: ThirdTry | null } | null>(null);
const nextStepNow = useRef<NextStepId | null>(null);
// An attempt kept from before the editor opened, not yet taken: whether
// its run was read (below). Until then no step passes on it.
@@ -2009,13 +2013,20 @@ export function DraftEditor({
// (rules.ts RewriteReach). A later Fix run's batch doesn't hide it.
const [lastRewrite, setLastRewrite] = useState<(LastRewrite & { html: string }) | null>(null);
const shownRewriteId = pageSuggestion?.id ?? null;
+ // The rewrite shown when it was last read (or failed to be): the Length's
+ // state waits for the read for the rewrite shown now.
+ const [lastRewriteReadFor, setLastRewriteReadFor] = useState<string | null>();
+ const lastRewriteRead = lastRewriteReadFor === shownRewriteId;
useEffect(() => {
let cancelled = false;
getLastPageRewrite(draft.id)
.then((last) => {
if (!cancelled) setLastRewrite(last);
})
- .catch((err) => console.error("Failed to read the page's last rewrite:", err));
+ .catch((err) => console.error("Failed to read the page's last rewrite:", err))
+ .finally(() => {
+ if (!cancelled) setLastRewriteReadFor(shownRewriteId);
+ });
return () => {
cancelled = true;
};
@@ -2333,6 +2344,10 @@ export function DraftEditor({
}
setSuggestions((prev) => prev.map((x) => (x.id === s.id ? { ...x, status: to } : x)));
if (to === 'accepted') {
+ // A page written again, as Start's: the steps passed on the page
+ // before are passed no longer (#1556 review). The rewrite's own
+ // attempt stays, to pass its step.
+ saveThirdStep({ ...thirdStepNow.current, passes: {} });
toast.success(t('rewrite.acceptedToast'));
// The rewrite is the whole batch: as Accept all.
nudgeAfterRead();
@@ -2448,7 +2463,9 @@ export function DraftEditor({
third: { prev: ThirdTry | null } | null = null,
) => {
const untried = () => {
- if (third) saveThirdTry(third.prev);
+ if (!third) return;
+ const read = attemptRead.current;
+ saveThirdTry(read && read.of === third.prev ? read.now : third.prev);
};
const nothing = () => {
const now = thirdStepNow.current.attempt;
@@ -3263,16 +3280,21 @@ export function DraftEditor({
}, [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
+ // rule the agent never saw, or a Length that moved (far off, for Fix open
+ // rules: thirdStepPasses), 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.
+ // It waits only for what a pass reads (thirdPassOf, #1556 review): the
+ // rules, the quoted passages that hold some, and the last rewrite the
+ // Length's reach reads; not Quality's reads, which a failed one would hold.
+ const thirdRead = rulesSettled && citedRead && lastRewriteRead;
const nextStateNow = useRef(nextState);
useEffect(() => {
nextStateNow.current = nextState;
});
useEffect(() => {
- if (!thirdTaken || !nextRead || !attemptChecked) return;
+ if (!thirdTaken || !thirdRead || !attemptChecked) return;
const timer = setTimeout(() => {
const { attempt, passes } = thirdStepNow.current;
if (!attempt) return;
@@ -3284,11 +3306,11 @@ export function DraftEditor({
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]);
+ }, [thirdTaken, thirdRead, 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;
+ thirdRead && Object.keys(thirdStep.passes).length !== thirdStepPasses(nextState).size;
useEffect(() => {
if (!stalePasses) return;
const held = thirdStepPasses(nextStateNow.current);
@@ -3304,8 +3326,10 @@ export function DraftEditor({
// 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.
+ // outcome, so a stopped run with no outcome yet is stopped. A Fix that
+ // found nothing left no batch: it ended, as agentOutcome reads it. A run
+ // still under way is left to finish: its batch passes the step. Only the
+ // attempt read is changed: one begun since is the newer run's (#1556 review).
useEffect(() => {
const attempt = thirdStepNow.current.attempt;
if (!attempt || attempt.ended) return;
@@ -3322,9 +3346,14 @@ export function DraftEditor({
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);
- }
+ const now =
+ run.outcome === 'failed' || run.outcome === 'stopped' || run.stoppedAt
+ ? null
+ : attempt.kind === 'fix' && run.outcome === 'done' && !run.steps[2]?.count
+ ? { ...attempt, ended: true }
+ : attempt;
+ attemptRead.current = { of: attempt, now };
+ if (now !== attempt && thirdStepNow.current.attempt === attempt) saveThirdTry(now);
},
() => {},
)
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 d37781555..990d5c564 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
@@ -6624,6 +6624,10 @@ describe('the side panel', () => {
expect(fixDraftRules.mock.calls[0][2].rules.map((r: { key: string }) => r.key)).toEqual([
'aiTraces',
]);
+ // No attempt is kept for it, and once the page reads it, Next stays (#1556 review).
+ expect(stored()).toBeNull();
+ await settle();
+ expect(stored()).toBeNull();
expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
});
@@ -6673,49 +6677,270 @@ describe('the side panel', () => {
});
it.each([
- ['stopped', { outcome: 'stopped' }],
- ['failed', { outcome: 'failed' }],
+ ['rewrite', 'stopped', { outcome: 'stopped' }],
+ ['rewrite', '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,
- };
- 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.
+ ['rewrite', 'stopped, its outcome not yet written', { outcome: null }],
+ // A tab closed mid-run (#1556 review).
+ ['fix', 'stopped', { outcome: 'stopped' }],
+ ['fix', 'failed', { outcome: 'failed' }],
+ ] as const)(
+ 'drops a %s attempt whose run was %s while the page was closed',
+ async (kind, _, ended) => {
+ const attempt = {
+ step: kind === 'rewrite' ? 'length' : 'fixRules',
+ kind,
+ at: new Date(Date.now() - 60_000).toISOString(),
+ batch: null,
+ };
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({ attempt, passes: {} }),
+ );
+ readAgentRun.mockResolvedValue({
+ id: 'r1',
+ kind,
+ 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', [kind]);
+ expect(
+ JSON.parse(localStorage.getItem('aeo:next-step-third-try:d1') ?? 'null'),
+ ).toBeNull();
+ expect(nextButton()!.getAttribute('aria-label')).toBe('Next · Fix open rules');
+ },
+ );
+
+ const doneRun = (writes: number) => ({
+ id: 'r1',
+ kind: 'fix',
+ steps: [
+ { key: 'read', count: null },
+ { key: 'rules', count: 1 },
+ { key: 'write', count: writes },
+ ],
+ step: 3,
+ startedAt: new Date().toISOString(),
+ finishedAt: new Date().toISOString(),
+ stoppedAt: null,
+ outcome: 'done',
+ });
+
+ it('passes a Fix that found nothing while the page was closed (#1556 review)', async () => {
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: {
+ step: 'fixRules',
+ kind: 'fix',
+ at: new Date(Date.now() - 60_000).toISOString(),
+ batch: null,
+ },
+ passes: {},
+ }),
+ );
+ // It left no batch: its row says it wrote nothing.
+ readAgentRun.mockResolvedValue(doneRun(0));
+ getDraftSuggestions.mockResolvedValue([]);
+ await renderPage();
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ expect(stored()).toEqual({ attempt: null, passes: { fixRules: expect.any(Object) } });
+ });
+
+ it('keeps a Fix begun while the read of an earlier run is still out (#1556 review)', async () => {
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: {
+ step: 'fixRules',
+ kind: 'fix',
+ at: new Date(Date.now() - 60_000).toISOString(),
+ batch: null,
+ },
+ passes: {},
+ }),
+ );
+ // The open-time read of the earlier run, alone: it lands after the new Fix.
+ let readRun!: (run: unknown) => void;
+ readAgentRun.mockImplementation((_id: string, kinds?: string[]) =>
+ kinds?.length === 1 && kinds[0] === 'fix'
+ ? new Promise((resolve) => (readRun = resolve))
+ : Promise.resolve(null),
+ );
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ // The earlier run failed: only its own attempt would go.
+ await act(async () => readRun({ ...doneRun(0), outcome: 'failed' }));
+ await settle();
+ expect(nextButton()!.getAttribute('aria-label')).toBe(MOVED_ON);
+ expect(stored()).toEqual({ attempt: null, passes: { fixRules: expect.any(Object) } });
+ });
+
+ it('doesn’t put back an attempt found failed when a Fix begun meanwhile doesn’t run (#1556 review)', async () => {
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: {
+ step: 'fixRules',
+ kind: 'fix',
+ at: new Date(Date.now() - 60_000).toISOString(),
+ batch: null,
+ },
+ passes: {},
+ }),
+ );
+ let readRun!: (run: unknown) => void;
+ readAgentRun.mockImplementation((_id: string, kinds?: string[]) =>
+ kinds?.length === 1 && kinds[0] === 'fix'
+ ? new Promise((resolve) => (readRun = resolve))
+ : Promise.resolve(null),
+ );
+ getDraftSuggestions.mockResolvedValue([]);
+ let ran!: (result: unknown) => void;
+ fixDraftRules.mockReturnValue(new Promise((resolve) => (ran = resolve)));
+ await runFix();
+ // The earlier run failed; then the new one fails too: neither is an attempt.
+ await act(async () => readRun({ ...doneRun(0), outcome: 'failed' }));
+ await act(async () => ran({ error: 'failed' }));
+ await act(async () => {});
+ expect(stored()).toBeNull();
+ });
+
+ it('waits for the read of the rewrite shown now, not one before it (#1556 review)', async () => {
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ attempt: { step: 'length', kind: 'rewrite', at: new Date().toISOString(), batch: null },
+ passes: {},
+ }),
+ );
+ // Its page decided: once read, the step passes.
getDraftSuggestions.mockResolvedValue([
{
- id: 'n7',
- batchId: 'b7',
+ id: 'p9',
+ batchId: 'b9',
actionId: null,
actionNo: null,
- kind: 'replace' as const,
- blockText: 'Teams trust it.',
- beforeText: 'trust',
- afterText: 'rely on',
- reason: 'a Quality check (hand-off)',
+ kind: 'page' as const,
+ blockText: 'Pipeline forecasting',
+ beforeText: '',
+ beforeHtml: draft().bodyHtml,
+ afterText: draft().bodyHtml,
+ reason: 'rewrote the page to the rules',
ruleKeys: [],
status: 'rejected' as const,
},
]);
+ // Read before the rewrite showed, then again for it: that read is out.
+ let lastRead!: (last: null) => void;
+ getLastPageRewrite.mockImplementation(async (..._args: unknown[]) =>
+ getLastPageRewrite.mock.calls.length === 1
+ ? null
+ : new Promise((resolve) => (lastRead = resolve)),
+ );
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');
+ expect(getLastPageRewrite).toHaveBeenCalledTimes(2);
+ expect(stored().passes).toEqual({});
+ await act(async () => lastRead(null));
+ await settle();
+ expect(stored()).toEqual({ attempt: null, passes: { length: expect.any(Object) } });
+ });
+
+ it('passes the step though a Quality read failed: it waits only for what it compares (#1556 review)', async () => {
+ // No authors: E-E-A-T, and the to-dos with it, never read.
+ getContentAuthors.mockRejectedValue(new Error('offline'));
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ await settle();
+ expect(stored()).toEqual({ attempt: null, passes: { fixRules: expect.any(Object) } });
+ });
+
+ it('waits for the last rewrite’s read, which the Length’s state reads (#1556 review)', async () => {
+ let lastRead!: (last: null) => void;
+ getLastPageRewrite.mockReturnValue(new Promise((resolve) => (lastRead = resolve)));
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ await settle();
+ expect(stored()).toMatchObject({ attempt: { step: 'fixRules', ended: true } });
+ expect(stored().passes).toEqual({});
+ await act(async () => lastRead(null));
+ await settle();
+ expect(stored()).toEqual({ attempt: null, passes: { fixRules: expect.any(Object) } });
+ });
+
+ it('clears the steps passed when a rewrite’s page is accepted, as Start does (#1556 review)', async () => {
+ getDraftSuggestions.mockResolvedValue([]);
+ fixDraftRules.mockResolvedValue({ count: 0 });
+ await runFix();
+ await settle();
+ expect(stored().passes).toEqual({ fixRules: expect.any(Object) });
+ // A rewrite begun since, its page waiting.
+ localStorage.setItem(
+ 'aeo:next-step-third-try:d1',
+ JSON.stringify({
+ ...stored(),
+ attempt: { step: 'length', kind: 'rewrite', at: new Date().toISOString(), batch: null },
+ }),
+ );
+ const body = draft().bodyHtml;
+ getDraftSuggestions.mockResolvedValue([
+ {
+ id: 'p9',
+ batchId: 'b9',
+ actionId: null,
+ actionNo: null,
+ kind: 'page' as const,
+ blockText: 'Pipeline forecasting',
+ beforeText: '',
+ beforeHtml: body,
+ afterText: body.replace('Teams trust it.', 'Teams rely on it.'),
+ reason: 'rewrote the page to the rules',
+ ruleKeys: [],
+ status: 'open' as const,
+ },
+ ]);
+ await act(async () => root.unmount());
+ root = createRoot(container);
+ await renderPage();
+ await act(async () => {});
+ expect(stored().passes).toEqual({ fixRules: expect.any(Object) });
+ const bar = container.querySelector('[role="region"][aria-label="Rewritten page"]')!;
+ await act(async () =>
+ [...bar.querySelectorAll('button')].find((b) => b.textContent === 'Accept')!.click(),
+ );
+ await act(async () => {});
+ expect(decideDraftSuggestions).toHaveBeenCalledWith(['p9'], 'accepted');
+ // A page written again: what was passed on the page before is gone;
+ // the rewrite's own attempt stays, to pass its step.
+ expect(stored().passes).toEqual({});
+ expect(stored().attempt).toMatchObject({ step: 'length', kind: 'rewrite' });
});
it('drops a rewrite’s attempt when the page opens on that rewrite having failed', async () => {
diff --git a/web/src/lib/content/next-step.test.ts b/web/src/lib/content/next-step.test.ts
index 434c08ad3..5f291719a 100644
--- a/web/src/lib/content/next-step.test.ts
+++ b/web/src/lib/content/next-step.test.ts
@@ -5,6 +5,7 @@ import {
rulesForNext,
stepState,
thirdPassOf,
+ thirdStepPasses,
thirdStepTried,
type NextStepState,
} from './next-step';
@@ -307,6 +308,24 @@ describe('the third step, once the agent took it (editor test 2026-10-26)', () =
).toBe('length');
});
+ it('keeps Fix open rules passed while the person brings the page within its Length (#1556 review)', () => {
+ // Far under its Length: the rewrite and Fix open rules both taken.
+ const short = {
+ ...long,
+ rewrite: { reason: 'length' as const, count: 8_000, target: 13_998, percent: 43 },
+ };
+ const passes = { length: thirdPassOf(short), fixRules: thirdPassOf(short) };
+ expect(nextStep({ ...short, thirdPasses: passes }).step.id).toBe('proof');
+ // The person writes two paragraphs: within 30% now, Fix open rules holds.
+ const within = { ...fix, thirdPasses: passes };
+ expect(thirdStepPasses(within)).toEqual(new Set(['fixRules']));
+ expect(nextStep(within).step.id).toBe('proof');
+ // Written far past it instead: the Length step, and Fix open rules with it.
+ const over = { ...long, thirdPasses: passes };
+ expect(thirdStepPasses(over)).toEqual(new Set());
+ expect(nextStep(over).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) });
expect(nextStep({ ...s, thirdPasses: { fixRules: thirdPassOf(s) } }).step.id).toBe('write');
diff --git a/web/src/lib/content/next-step.ts b/web/src/lib/content/next-step.ts
index 465d1a72f..827dfa067 100644
--- a/web/src/lib/content/next-step.ts
+++ b/web/src/lib/content/next-step.ts
@@ -209,13 +209,16 @@ export function thirdPassOf(s: NextStepState): ThirdPass {
* 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.
+ * whose page has moved on is the agent's again. Fix open rules holds while
+ * the person brings the page within its Length (#1556 review): only a
+ * Length far off now, and not as it was, brings it back.
*/
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 (!pass) continue;
+ if (pass.length !== length && (id === 'length' || length !== '')) continue;
if (id === 'fixRules' && !s.openRules.every((rule) => pass.rules.includes(rule))) continue;
held.add(id);
}