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(audit): re-audit first the pages fixed through the Action Center, on the brand's own domains (learning B7) (#1698)
Cihanc55edec2d022
diff --git a/docs/guides/site-audit.mdx b/docs/guides/site-audit.mdxindex b1ed2cec8..542f97e39 100644--- a/docs/guides/site-audit.mdx+++ b/docs/guides/site-audit.mdx@@ -145,7 +145,7 @@ Each draft and code block has a copy icon in its corner (**Copy draft** or **Cop## Auto-audit and settings-Auto-audit spends a quarter of your monthly audits on the Opportunities inbox's top pages, in five runs on days 1, 7, 14, 21 and 28 of your billing month. Pinned pages go first; pages audited in the last 30 days wait for a later run. After a run, a notice on the Site Audit page sums it up (pages audited, critical issues, the next run) with **View pages**, and the audits show **Auto** in the Pages list.+Auto-audit spends a quarter of your monthly audits on the Opportunities inbox's top pages, in five runs on days 1, 7, 14, 21 and 28 of your billing month. Pages you fixed go first: a page of your brand's site whose Site Audit fix you completed in the Action Center in the last 30 days, and that hasn't been audited since, is audited again in the next run, fixed pages taking up to about half the run (rounded up), so the action's result says whether the fix took. Then pinned pages; pages audited in the last 30 days wait for a later run. After a run, a notice on the Site Audit page sums it up (pages audited, critical issues, the next run) with **View pages**, and the audits show **Auto** in the Pages list.**Settings** (top right) holds:diff --git a/server/src/lib/audit/auto-audit-sweep.js b/server/src/lib/audit/auto-audit-sweep.jsindex 3854e5452..be9b84c0a 100644--- a/server/src/lib/audit/auto-audit-sweep.js+++ b/server/src/lib/audit/auto-audit-sweep.js@@ -1,9 +1,11 @@/*** The daily auto-audit sweep (SA-006). For each organization whose run is* due, it claims the run (one site_audit_auto_runs row per org, period and- * run day, so a second sweep the same day does nothing), audits the top- * pages of its brands' Opportunities inboxes, and once every audit has- * finished, records the run and, when the team asked for it, e-mails them.+ * run day, so a second sweep the same day does nothing), audits the pages+ * its brands' Site Audit fixes were completed on and not audited or tried+ * since (learning B7), then the top pages of their Opportunities inboxes,+ * and once every audit has finished, records the run and, when the team+ * asked for it, e-mails them.** The inbox read and the audit start live in routes/audits.js; they're* passed in, so this module doesn't import a route (and tests stand them in).@@ -13,9 +15,11 @@ import supabaseAdmin from '../../config/supabase.js';import { getSiteAuditQuotaStatus, PlanLimitError } from '../plan-guard.js';import { readAllRpcRows } from '../rpc-pages.js';import { logger } from '../logger.js';-import { AUTO_AUDIT, pickRunPages } from './auto-audit.js';+import { AUTO_AUDIT, pickRunPages, runPageKey } from './auto-audit.js';import { sendAutoAuditEmail } from './auto-audit-email.js';-import { opportunityKey } from './opportunities.js';+import { excludePatternMatcher, opportunityKey } from './opportunities.js';+import { pageKey } from './history.js';+import { onBrandDomain } from './host.js';/*** @typedef {object} SweepDeps@@ -108,8 +112,15 @@ async function startRun(orgId, brands, { readInbox, startAudits, now }) {const inboxes = [];let triedKeys = new Set();+ let fixed = [];if (slots > 0) {triedKeys = await recentlyTriedPages(brands, now);+ // Pages fixed since their last audit come first (learning B7). A read+ // that fails leaves the run to the inboxes, as before.+ fixed = await fixedPages(brands, now).catch((err) => {+ logger.warn({ err, orgId }, '[auto-audit] fixed pages not read');+ return [];+ });for (const brand of brands) {try {const { opportunities } = await readInbox(brand.id);@@ -119,7 +130,7 @@ async function startRun(orgId, brands, { readInbox, startAudits, now }) {}}}- const pages = pickRunPages(inboxes, slots, now, triedKeys);+ const pages = pickRunPages(inboxes, slots, now, triedKeys, fixed);const auditIds = [];const audits = [];@@ -168,6 +179,152 @@ async function recentlyTriedPages(brands, now) {return new Set((data ?? []).map((row) => opportunityKey(row.url)).filter(Boolean));}+/**+ * Pages one fix action offers at most, of those still due (its others are+ * left out first): a run takes few anyway.+ */+const FIXED_PER_ACTION = 20;++/**+ * Pages a Site Audit fix (an Action Center action of the fix category) was+ * completed on in the last 30 days and that no audit has read or tried+ * since (any audit of the brand's, finished or not), the most recently+ * fixed first (learning B7). Its targets name the pages, by their+ * addresses, on the action's brand's own domains and not left out of+ * auto-audits (excluded, or under an exclude pattern and not pinned); one+ * fixed twice counts from its last fix.+ *+ * @returns {Promise<Array<{ brandId: string, key: string, url: string, path: string }>>}+ */+export async function fixedPages(brands, now) {+ const since = new Date(now.getTime() - AUTO_AUDIT.freshDays * 24 * 60 * 60 * 1000);+ const brandIds = brands.map((b) => b.id);+ const { data: actions, error } = await supabaseAdmin+ .from('actions')+ .select('brand_id, payload, completed_at')+ .in('brand_id', brandIds)+ .eq('status', 'completed')+ .eq('category', 'fix')+ .is('deleted_at', null)+ .gte('completed_at', since.toISOString())+ .order('completed_at', { ascending: false })+ .limit(200);+ if (error) throw new Error(error.message);+ if (!actions?.length) return [];++ // Only a brand's own pages: a fix's target on another site (a+ // competitor's page, say) costs an audit and says nothing of the fix. A+ // brand without a domain has none. Nor the pages the team left out of+ // Opportunities and auto-audits: excluded ones, and those under the+ // brand's exclude patterns unless pinned, as the inbox does.+ const [domainRows, prefRows, settingRows] = await Promise.all([+ supabaseAdmin.from('brand_domains').select('brand_id, domain').in('brand_id', brandIds),+ readAllRpcRows('site_audit_page_prefs', (from, to) =>+ supabaseAdmin+ .from('site_audit_page_prefs')+ .select('brand_id, page_key, state')+ .in('brand_id', brandIds)+ .order('brand_id', { ascending: true })+ .order('page_key', { ascending: true })+ .range(from, to),+ ),+ supabaseAdmin+ .from('site_audit_brand_settings')+ .select('brand_id, exclude_patterns')+ .in('brand_id', brandIds),+ ]);+ for (const read of [domainRows, prefRows, settingRows]) {+ if (read.error) throw new Error(read.error.message);+ }+ const domains = new Map();+ for (const row of domainRows.data ?? []) {+ if (!domains.has(row.brand_id)) domains.set(row.brand_id, []);+ domains.get(row.brand_id).push(row.domain);+ }+ const prefs = new Map(+ (prefRows.data ?? []).map((row) => [runPageKey(row.brand_id, row.page_key), row.state]),+ );+ const excludedByPattern = new Map(+ (settingRows.data ?? []).map((row) => [+ row.brand_id,+ excludePatternMatcher(row.exclude_patterns ?? []),+ ]),+ );+ const leftOut = (brandId, key, path) => {+ const state = prefs.get(runPageKey(brandId, key));+ if (state === 'excluded') return true;+ return state !== 'pinned' && Boolean(excludedByPattern.get(brandId)?.(path));+ };++ // By the brand's page (runPageKey): brands sharing a domain fix their own.+ const pages = new Map();+ for (const [index, action] of actions.entries()) {+ const own = domains.get(action.brand_id) ?? [];+ const p = action.payload ?? {};+ const urls = [+ ...(Array.isArray(p.targets) ? p.targets.map((t) => t?.id) : []),+ ...(Array.isArray(p.targetIds) ? p.targetIds : []),+ ].filter(+ (url) => typeof url === 'string' && /^https?:\/\//i.test(url) && onBrandDomain(url, own),+ );+ for (const url of urls) {+ const key = opportunityKey(url);+ const id = key ? runPageKey(action.brand_id, key) : null;+ // Newest fix first: the first one read for a page is its last.+ if (!id || pages.has(id)) continue;+ const path = pageKey(url)?.path ?? '/';+ if (leftOut(action.brand_id, key, path)) continue;+ pages.set(id, {+ brandId: action.brand_id,+ key,+ url,+ path,+ fixedAt: Date.parse(action.completed_at),+ action: index,+ });+ }+ }+ if (pages.size === 0) return [];++ // The audits since the oldest of those fixes, by the address asked for or+ // ended at: the brand's own, as the action's result reads them (A2). Every+ // one, past PostgREST's 1000-row page: a busy month has more.+ const oldest = [...pages.values()].reduce((min, page) =>+ page.fixedAt < min.fixedAt ? page : min,+ ).fixedAt;+ const { data: audits, error: auditsErr } = await readAllRpcRows('site_audits', (from, to) =>+ supabaseAdmin+ .from('site_audits')+ .select('brand_id, url, final_url, created_at')+ .in('brand_id', brandIds)+ .gte('created_at', new Date(oldest).toISOString())+ .order('created_at', { ascending: true })+ .order('id', { ascending: true })+ .range(from, to),+ );+ if (auditsErr) throw new Error(auditsErr.message);+ const lastAudit = new Map();+ for (const audit of audits ?? []) {+ for (const url of [audit.url, audit.final_url]) {+ const key = url ? opportunityKey(url) : null;+ const id = key ? runPageKey(audit.brand_id, key) : null;+ const at = Date.parse(audit.created_at);+ if (id && !(lastAudit.get(id) >= at)) lastAudit.set(id, at);+ }+ }+ // The pages still due, each fix's first FIXED_PER_ACTION of them.+ const perAction = new Map();+ const due = [];+ for (const [id, page] of pages) {+ if (lastAudit.get(id) >= page.fixedAt) continue;+ const taken = perAction.get(page.action) ?? 0;+ if (taken >= FIXED_PER_ACTION) continue;+ perAction.set(page.action, taken + 1);+ due.push({ brandId: page.brandId, key: page.key, url: page.url, path: page.path });+ }+ return due;+}+/** Record the finished run and, when the org asked for it, e-mail what it found. */async function finishRun({ runId, orgId, brands, auditIds, emailAfterRun, nextRunAt }) {const { data: rows, error } = auditIds.lengthdiff --git a/server/src/lib/audit/auto-audit-sweep.test.js b/server/src/lib/audit/auto-audit-sweep.test.jsindex a0a2ce5fd..4c7f9c9f6 100644--- a/server/src/lib/audit/auto-audit-sweep.test.js+++ b/server/src/lib/audit/auto-audit-sweep.test.js@@ -14,6 +14,12 @@ let ops;let auditRows;let triedRows;let failCounts;+let fixedActions;+let actionsError;+let recentAudits;+let domainRows;+let prefRows;+let settingRows;class PlanLimitError extends Error {}@@ -46,8 +52,33 @@ function query(table) {op.filters.push(['gte', col, val]);return builder;},+ is: (col, val) => {+ op.filters.push(['is', col, val]);+ return builder;+ },+ order: (col) => {+ op.order = [...(op.order ?? []), col];+ return builder;+ },+ limit: () => builder,+ range: (from, to) => {+ op.range = [from, to];+ return builder;+ },then: (resolve) => {ops.push(op);+ // Fix actions completed lately (learning B7), and the audits since.+ if (table === 'actions') {+ return resolve(actionsError ? { data: null, error: actionsError } : { data: fixedActions });+ }+ if (table === 'brand_domains') return resolve({ data: domainRows, error: null });+ if (table === 'site_audit_page_prefs') {+ return resolve({ data: prefRows.slice(op.range[0], op.range[1] + 1), error: null });+ }+ if (table === 'site_audit_brand_settings') return resolve({ data: settingRows, error: null });+ if (table === 'site_audits' && op.select === 'brand_id, url, final_url, created_at') {+ return resolve({ data: recentAudits.slice(op.range[0], op.range[1] + 1), error: null });+ }if (table === 'site_audit_auto_runs' && op.upsert) {return resolve({ data: claimed ? [{ id: 'run-1' }] : [], error: null });}@@ -115,6 +146,15 @@ beforeEach(async () => {auditRows = [];triedRows = [];failCounts = {};+ fixedActions = [];+ actionsError = null;+ recentAudits = [];+ domainRows = [+ { brand_id: 'brand-a', domain: 'acme.com' },+ { brand_id: 'brand-b', domain: 'https://www.acme.com/' },+ ];+ prefRows = [];+ settingRows = [];sendAutoAuditEmail = vi.fn().mockResolvedValue(true);readInbox = vi.fn(async (brandId) => ({ opportunities: inboxes[brandId] ?? [] }));startAudits = vi.fn(async (brandId, urls) => ({@@ -123,9 +163,15 @@ beforeEach(async () => {}));vi.doMock('../../config/supabase.js', () => ({ default: { from: (t) => query(t) } }));- vi.doMock('../rpc-pages.js', () => ({- readAllRpcRows: async () => ({ data: brandRows, error: null }),- }));+ // The brands stand in; every other read goes through the real paging loop.+ vi.doMock('../rpc-pages.js', async (importOriginal) => {+ const actual = await importOriginal();+ return {+ ...actual,+ readAllRpcRows: async (name, page) =>+ name === 'brands' ? { data: brandRows, error: null } : actual.readAllRpcRows(name, page),+ };+ });vi.doMock('../plan-guard.js', () => ({PlanLimitError,getSiteAuditQuotaStatus: vi.fn(async (orgId) => {@@ -275,3 +321,293 @@ describe('sweepAutoAudits', () => {expect(runOps()).toEqual([]);});});++/**+ * Pages a Site Audit fix was completed on lately and no audit has read+ * since (learning B7): the run takes them first.+ */+describe('fixedPages', () => {+ it("reads the fixes' pages not audited since, the newest fix first, and the run takes them first", async () => {+ fixedActions = [+ {+ brand_id: 'brand-b',+ completed_at: '2026-10-09T10:00:00Z',+ payload: { targets: [{ id: 'https://acme.com/b-fixed' }, { id: 'acme.com' }] },+ },+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-05T10:00:00Z',+ payload: {+ targets: [{ id: 'https://acme.com/a-fixed' }],+ targetIds: ['https://acme.com/a-checked', 'https://acme.com/a-fixed'],+ },+ },+ ];+ // Audited after its fix: done. Audited before it: still due.+ recentAudits = [+ {+ brand_id: 'brand-a',+ url: 'https://acme.com/a-checked',+ final_url: null,+ created_at: '2026-10-06T00:00:00Z',+ },+ {+ brand_id: 'brand-a',+ url: 'https://acme.com/a-fixed',+ final_url: null,+ created_at: '2026-10-04T00:00:00Z',+ },+ ];+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect(await fixedPages(brandRows, NOW)).toEqual([+ {+ brandId: 'brand-b',+ key: 'acme.com/b-fixed',+ url: 'https://acme.com/b-fixed',+ path: '/b-fixed',+ },+ {+ brandId: 'brand-a',+ key: 'acme.com/a-fixed',+ url: 'https://acme.com/a-fixed',+ path: '/a-fixed',+ },+ ]);+ const read = ops.find((op) => op.table === 'actions');+ expect(read.filters).toEqual(+ expect.arrayContaining([+ ['eq', 'status', 'completed'],+ ['eq', 'category', 'fix'],+ ['is', 'deleted_at', null],+ ]),+ );++ ops = [];+ await sweep({ readInbox, startAudits, now: NOW });+ // Five slots: the two fixed pages first, then the inboxes' best three, by brand.+ expect(startAudits.mock.calls.map(([brandId, urls]) => [brandId, urls])).toEqual([+ ['brand-a', ['https://acme.com/a-fixed', 'https://acme.com/pinned', 'https://acme.com/a-90']],+ ['brand-b', ['https://acme.com/b-fixed', 'https://acme.com/b-80']],+ ]);+ });++ it("takes only the brand's own pages: another site's is left, a subdomain kept", async () => {+ brandRows = [...brandRows, { id: 'brand-c', name: 'No Domain', organization_id: 'org-1' }];+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-09T10:00:00Z',+ payload: {+ targets: [+ { id: 'https://rival.com/a-fixed' },+ { id: 'https://acme.com.rival.io/a-fixed' },+ { id: 'https://shop.acme.com/a-fixed' },+ { id: 'https://acme.com/a-fixed' },+ ],+ },+ },+ // A brand without a domain has no page of its own.+ {+ brand_id: 'brand-c',+ completed_at: '2026-10-09T09:00:00Z',+ payload: { targets: [{ id: 'https://acme.com/c-fixed' }] },+ },+ ];+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect((await fixedPages(brandRows, NOW)).map((p) => [p.brandId, p.url])).toEqual([+ ['brand-a', 'https://shop.acme.com/a-fixed'],+ ['brand-a', 'https://acme.com/a-fixed'],+ ]);+ const read = ops.find((op) => op.table === 'brand_domains');+ expect(read.filters).toEqual([['in', 'brand_id', ['brand-a', 'brand-b', 'brand-c']]]);+ });++ it('reads nothing more when no fix was completed lately', async () => {+ const { fixedPages } = await import('./auto-audit-sweep.js');+ expect(await fixedPages(brandRows, NOW)).toEqual([]);+ expect(ops.map((op) => op.table)).toEqual(['actions']);+ });++ it('leaves out the pages the team excluded, or put under an exclude pattern and not pinned', async () => {+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-09T10:00:00Z',+ payload: {+ targets: [+ { id: 'https://acme.com/a-excluded' },+ { id: 'https://acme.com/Legal/terms' },+ { id: 'https://acme.com/legal/pinned' },+ { id: 'https://acme.com/a-kept' },+ ],+ },+ },+ // Another brand's prefs and patterns are its own.+ {+ brand_id: 'brand-b',+ completed_at: '2026-10-09T09:00:00Z',+ payload: { targets: [{ id: 'https://acme.com/legal/b-terms' }] },+ },+ ];+ prefRows = [+ { brand_id: 'brand-a', page_key: 'acme.com/a-excluded', state: 'excluded' },+ { brand_id: 'brand-a', page_key: 'acme.com/legal/pinned', state: 'pinned' },+ ];+ settingRows = [{ brand_id: 'brand-a', exclude_patterns: ['/legal/*'] }];+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect((await fixedPages(brandRows, NOW)).map((p) => [p.brandId, p.path])).toEqual([+ ['brand-a', '/legal/pinned'],+ ['brand-a', '/a-kept'],+ ['brand-b', '/legal/b-terms'],+ ]);+ for (const table of ['site_audit_page_prefs', 'site_audit_brand_settings']) {+ expect(ops.find((op) => op.table === table).filters).toEqual([+ ['in', 'brand_id', ['brand-a', 'brand-b']],+ ]);+ }+ });++ it("keeps brands sharing a domain apart: one brand's audit doesn't count for another's fix", async () => {+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-05T10:00:00Z',+ payload: { targets: [{ id: 'https://acme.com/shared' }, { id: 'https://acme.com/both' }] },+ },+ {+ brand_id: 'brand-b',+ completed_at: '2026-10-04T10:00:00Z',+ payload: { targets: [{ id: 'https://acme.com/shared' }, { id: 'https://acme.com/both' }] },+ },+ ];+ // Brand B audited /shared after both fixes: B's is done, A's still due.+ recentAudits = [+ {+ brand_id: 'brand-b',+ url: 'https://acme.com/shared',+ final_url: null,+ created_at: '2026-10-06T00:00:00Z',+ },+ ];+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect((await fixedPages(brandRows, NOW)).map((p) => [p.brandId, p.path])).toEqual([+ ['brand-a', '/shared'],+ ['brand-a', '/both'],+ ['brand-b', '/both'],+ ]);+ });++ it('reads every audit since the oldest fix, past the 1000-row page', async () => {+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-05T10:00:00Z',+ payload: {+ targets: [{ id: 'https://acme.com/a-fixed' }, { id: 'https://acme.com/a-due' }],+ },+ },+ ];+ // A thousand other audits first; the fixed page's comes 1,001st.+ recentAudits = Array.from({ length: 1000 }, (_, i) => ({+ brand_id: 'brand-a',+ url: `https://acme.com/other-${i}`,+ final_url: null,+ created_at: '2026-10-06T00:00:00Z',+ }));+ recentAudits.push({+ brand_id: 'brand-a',+ url: 'https://acme.com/a-fixed',+ final_url: null,+ created_at: '2026-10-07T00:00:00Z',+ });+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect((await fixedPages(brandRows, NOW)).map((p) => p.path)).toEqual(['/a-due']);+ const reads = ops.filter((op) => op.table === 'site_audits');+ expect(reads.map((op) => op.range)).toEqual([+ [0, 999],+ [1000, 1999],+ ]);+ expect(reads[0].order).toEqual(['created_at', 'id']);+ });++ it('caps a fix at 20 pages once the ones audited since are left out', async () => {+ const paths = Array.from({ length: 25 }, (_, i) => `/page-${i + 1}`);+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-05T10:00:00Z',+ payload: { targets: paths.map((path) => ({ id: `https://acme.com${path}` })) },+ },+ ];+ // Its first five pages were audited since.+ recentAudits = paths.slice(0, 5).map((path) => ({+ brand_id: 'brand-a',+ url: `https://acme.com${path}`,+ final_url: null,+ created_at: '2026-10-06T00:00:00Z',+ }));+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect((await fixedPages(brandRows, NOW)).map((p) => p.path)).toEqual(paths.slice(5));+ });++ it('counts an audit by the address it ended at, and the same page however its address is written', async () => {+ fixedActions = [+ {+ brand_id: 'brand-a',+ completed_at: '2026-10-05T10:00:00Z',+ payload: {+ targets: [+ { id: 'https://www.acme.com/a-due/?utm_source=mail' },+ { id: 'http://acme.com/a-due' },+ { id: 'HTTPS://ACME.COM/a-due#top' },+ { id: 'https://acme.com/a-fixed' },+ { id: 'https://acme.com/a-moved' },+ ],+ },+ },+ ];+ recentAudits = [+ // The same page, written another way.+ {+ brand_id: 'brand-a',+ url: 'http://www.acme.com/a-fixed/?ref=1',+ final_url: null,+ created_at: '2026-10-06T00:00:00Z',+ },+ // Asked for another address, redirected to the fixed page.+ {+ brand_id: 'brand-a',+ url: 'https://acme.com/old-address',+ final_url: 'https://www.acme.com/a-moved/',+ created_at: '2026-10-06T00:00:00Z',+ },+ ];+ const { fixedPages } = await import('./auto-audit-sweep.js');++ expect(await fixedPages(brandRows, NOW)).toEqual([+ {+ brandId: 'brand-a',+ key: 'acme.com/a-due',+ url: 'https://www.acme.com/a-due/?utm_source=mail',+ path: '/a-due',+ },+ ]);+ });++ it('runs on the inboxes, as before, when the fixed pages cannot be read', async () => {+ actionsError = { message: 'connection reset' };+ await run();+ expect(ops.some((op) => op.table === 'actions')).toBe(true);+ expect(startAudits.mock.calls.map(([brandId, urls]) => [brandId, urls])).toEqual([+ ['brand-a', ['https://acme.com/pinned', 'https://acme.com/a-90', 'https://acme.com/a-40']],+ ['brand-b', ['https://acme.com/b-80', 'https://acme.com/b-70']],+ ]);+ expect(runOps().at(-1).update).toMatchObject({ status: 'completed' });+ });+});diff --git a/server/src/lib/audit/auto-audit.js b/server/src/lib/audit/auto-audit.jsindex 17efbacb8..e8c706647 100644--- a/server/src/lib/audit/auto-audit.js+++ b/server/src/lib/audit/auto-audit.js@@ -1,10 +1,11 @@/**- * Site Audit auto-audit (SA-006): 25% of the monthly quota audits the- * Opportunities inbox's top pages by itself, in five runs of 5% on days 1,- * 7, 14, 21 and 28 of the billing month. Pure: the schedule, the share kept- * for the runs to come, and which pages a run takes. auto-audit-sweep.js- * reads and writes; plan-guard.js keeps the reserved share out of the- * user's pool.+ * Site Audit auto-audit (SA-006): 25% of the monthly quota audits pages by+ * itself, in five runs of 5% on days 1, 7, 14, 21 and 28 of the billing+ * month: first the pages a Site Audit fix was completed on and not audited+ * or tried since (learning B7), then the Opportunities inbox's top pages.+ * Pure: the schedule, the share kept for the runs to come, and which pages a+ * run takes. auto-audit-sweep.js reads and writes; plan-guard.js keeps the+ * reserved share out of the user's pool.*/export const AUTO_AUDIT = Object.freeze({@@ -87,26 +88,68 @@ export function pendingRuns(schedule, recordedDays, now) {}/**- * The pages a run audits: from every brand's inbox (ordered by priority.js),- * the pages not being audited and not audited in the last 30 days, nor tried- * by a run in that time (an audit that failed, say on a page that blocks- * bots, would otherwise take a slot every run); pinned pages first, then the- * highest priority across the brands, at most `slots`. A tie keeps the- * inboxes' own order.+ * A page within a run: brands may share a domain, and each audits, pins and+ * fixes its own pages, so a page is the brand's page (`brandId|key`).+ */+export const runPageKey = (brandId, key) => `${brandId}|${key}`;++/**+ * Pages fixed since their last audit (learning B7) take at most this share+ * of a run, rounded up: the rest is the inbox's, so new pages keep coming.+ */+export const FIXED_SHARE = 0.5;++/**+ * The pages a run audits. First the pages a Site Audit fix was completed on+ * and not audited or tried since (learning B7), the most recently fixed first,+ * up to FIXED_SHARE of the run: auditing them again says whether the fix+ * took, which the Action Center reads as the fix's result, a month sooner+ * than the 30-day rest below would. Then, from every brand's inbox (ordered+ * by priority.js), the pages not being audited and not audited in the last+ * 30 days, nor tried by a run in that time (an audit that failed, say on a+ * page that blocks bots, would otherwise take a slot every run); pinned+ * pages first, then the highest priority across the brands, at most `slots`+ * in all. A tie keeps the inboxes' own order.** @param {Array<{ brandId: string, opportunities: Array<{ key: string, url: string, path: string, pinned: boolean, running: boolean, priority: number, lastAudit: { at: string }|null }> }>} inboxes* @param {number} slots* @param {Date} now* @param {Set<string>} [triedKeys] page keys a run audited in the last 30 days- * @returns {Array<{ brandId: string, url: string, path: string, pinned: boolean, priority: number }>}+ * @param {Array<{ brandId: string, key: string, url: string, path: string }>} [fixed]+ * pages fixed since their last audit, the most recently fixed first, already+ * kept to the brand's own domains and its auto-audit exclusions (fixedPages)+ * @returns {Array<{ brandId: string, url: string, path: string, pinned: boolean, priority: number, fixed?: true }>}*/-export function pickRunPages(inboxes, slots, now, triedKeys = new Set()) {+export function pickRunPages(inboxes, slots, now, triedKeys = new Set(), fixed = []) {const freshSince = new Date(now).getTime() - AUTO_AUDIT.freshDays * DAY_MS;+ const running = new Set(+ inboxes.flatMap(({ brandId, opportunities }) =>+ opportunities.filter((r) => r.running).map((r) => runPageKey(brandId, r.key)),+ ),+ );+ const first = [];+ const taken = new Set();+ const fixedSlots = Math.min(Math.max(0, slots), Math.ceil(Math.max(0, slots) * FIXED_SHARE));+ for (const page of fixed) {+ if (first.length >= fixedSlots) break;+ const id = page.key ? runPageKey(page.brandId, page.key) : null;+ if (!id || running.has(id) || taken.has(id)) continue;+ taken.add(id);+ first.push({+ brandId: page.brandId,+ url: page.url,+ path: page.path,+ pinned: false,+ priority: 0,+ fixed: true,+ });+ }const candidates = [];for (const { brandId, opportunities } of inboxes) {for (const row of opportunities) {// A template's row (SA-007) stands for pages the inbox lists one by one.if (row.template || row.running || triedKeys.has(row.key)) continue;+ if (taken.has(runPageKey(brandId, row.key))) continue;if (row.lastAudit && Date.parse(row.lastAudit.at) >= freshSince) continue;candidates.push({brandId,@@ -117,7 +160,10 @@ export function pickRunPages(inboxes, slots, now, triedKeys = new Set()) {});}}- return candidates- .sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.priority - a.priority)- .slice(0, Math.max(0, slots));+ return [+ ...first,+ ...candidates+ .sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.priority - a.priority)+ .slice(0, Math.max(0, slots - first.length)),+ ];}diff --git a/server/src/lib/audit/auto-audit.test.js b/server/src/lib/audit/auto-audit.test.jsindex 17fe27b70..45fd7917f 100644--- a/server/src/lib/audit/auto-audit.test.js+++ b/server/src/lib/audit/auto-audit.test.js@@ -80,6 +80,95 @@ describe('pendingRuns', () => {describe('pickRunPages', () => {const NOW = at('2026-10-17T06:00:00Z');+ const fixedPage = (path, brandId = 'a') => ({+ brandId,+ key: `acme.com${path}`,+ url: `https://acme.com${path}`,+ path,+ });++ /** A page fixed since its last audit is audited again first (learning B7). */+ it('takes pages fixed since their last audit first, up to half the run', () => {+ const pages = pickRunPages(+ [+ {+ brandId: 'a',+ opportunities: [+ // Audited last week, so the inbox would leave it; fixed since.+ row('/fixed-1', { priority: 90, lastAudit: { at: '2026-10-10T00:00:00Z' } }),+ row('/a-80', { priority: 80 }),+ row('/a-70', { priority: 70 }),+ row('/busy', { running: true }),+ ],+ },+ ],+ 4,+ NOW,+ new Set(['acme.com/fixed-2']),+ [fixedPage('/fixed-1'), fixedPage('/busy'), fixedPage('/fixed-2'), fixedPage('/fixed-3')],+ );+ expect(pages.map((p) => [p.path, p.fixed ?? false])).toEqual([+ ['/fixed-1', true],+ ['/fixed-2', true],+ ['/a-80', false],+ ['/a-70', false],+ ]);+ });++ it("keeps brands sharing a domain apart: one's fixed page doesn't take or skip the other's", () => {+ const pages = pickRunPages(+ [+ {+ brandId: 'a',+ opportunities: [row('/shared', { priority: 50 })],+ },+ {+ brandId: 'b',+ opportunities: [row('/shared', { priority: 90 }), row('/busy', { running: true })],+ },+ ],+ 4,+ NOW,+ new Set(),+ [fixedPage('/shared', 'a'), fixedPage('/busy', 'a')],+ );+ expect(pages.map((p) => [p.brandId, p.path, p.fixed ?? false])).toEqual([+ ['a', '/shared', true],+ ['a', '/busy', true],+ ['b', '/shared', false],+ ]);+ });++ it("rounds the fixed pages' half up: three of a five-page run", () => {+ const pages = pickRunPages(+ [+ {+ brandId: 'a',+ opportunities: [row('/a-80', { priority: 80 }), row('/a-70', { priority: 70 })],+ },+ ],+ 5,+ NOW,+ new Set(),+ ['/fixed-1', '/fixed-2', '/fixed-3', '/fixed-4'].map((path) => fixedPage(path)),+ );+ expect(pages.map((p) => p.path)).toEqual([+ '/fixed-1',+ '/fixed-2',+ '/fixed-3',+ '/a-80',+ '/a-70',+ ]);+ });++ it('gives a one-page run to a fixed page, and none to a run with no slots', () => {+ const inbox = [{ brandId: 'a', opportunities: [row('/a-80', { priority: 80 })] }];+ expect(+ pickRunPages(inbox, 1, NOW, new Set(), [fixedPage('/fixed')]).map((p) => p.path),+ ).toEqual(['/fixed']);+ expect(pickRunPages(inbox, 0, NOW, new Set(), [fixedPage('/fixed')])).toEqual([]);+ });+const row = (path, over = {}) => ({key: `acme.com${path}`,url: `https://acme.com${path}`,