Skip to content

feat(audit): audit top-priority pages automatically each billing week - #1139

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-auto
Oct 18, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/audit-auto

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Site Audit now audits the top-priority pages by itself. This is task SA-006 of the Site Audit v2 spec (§4.3, §13).

  • The share: 25% of the monthly quota runs in five runs of 5% on days 1, 7, 14, 21 and 28 of the billing month. That is Starter 5 pages a run and Growth 25.
  • Rounding: each run is floor(quota × 5%), at least one page.
  • Reservation: the runs still to come keep their slots out of the user's pool. A run's unused slots return when it is recorded.
  • Turning it off gives the whole quota back to the user.

Migration 00167_site_audit_auto.sql

  • site_audit_settings (per organization): auto_audit and email_after_run. No row, or a null auto_audit, follows the plan: on with a monthly quota, off without one (Self-hosted, Enterprise without a contract limit).
  • site_audit_brand_settings: a brand's exclude_patterns (/careers/*).
  • site_audit_auto_runs: one row per organization, billing period and run day.
    • The sweep claims a run by inserting its row, so a second sweep the same day starts nothing.
    • The row keeps the run's slots and audit ids.
  • source (user | auto, default user) on site_audits and site_audit_usage. It lets the quota card (SA-019) tell auto use from the user's.
  • RLS: member-select on each new table; the server writes them with the service role.

Server

  • lib/audit/auto-audit.js (pure):
    • The run schedule. Day N is the UTC day N − 1 days after the period start; day 1 starts with the period. A period shorter than N days runs day N on its last day.
    • A run's turn lasts until the next run starts, so a missed sweep still runs it that week.
    • The share per run and the runs still pending.
    • pickRunPages, which picks:
      • pinned pages first, then the highest priority (SA-005) across the org's brands;
      • skipping pages being audited, audited in the last 30 days, or tried by a run in the last 30 days (a page that blocks bots would otherwise take a slot every run).
  • lib/audit/auto-audit-sweep.js:
    • Claims each org's due run and reads its brands' Opportunities inboxes.
    • Starts the audits, then records the run when they finish.
    • E-mails the members when the org asked for it.
    • Starts every org's run before awaiting any.
    • The inbox read and the audit start come from routes/audits.js as arguments, so the module imports no route.
  • runDailyTracking: runs the sweep after the GA sync and page-opportunity detection, so a run audits what this week's data ranks highest.
  • plan-guard.js:
    • A user's audit can't take the reserved slots; a run's audits (source: 'auto') can.
    • The empty-pool message follows the spec: "You've used your 75 Site Audits this month on the Starter plan. 25 auto audits are still scheduled. Turn off auto-audit in Site Audit settings to use them now, or upgrade."
    • getSiteAuditQuotaStatus now returns auto: enabled, per run, reserved, used, the five runs with done, the next run, and the due run. remaining is the user's pool.
  • routes/audits.js:
    • Audit pools: run audits use their own pool of 2, so they never hold up a user's bulk audit (auditPool, shared with the bulk pool of 3). startAutoAudits starts a brand's run audits and returns when they're done. readOpportunityInbox is the inbox read the GET route used, now exported and applying the brand's exclude patterns.
    • GET / PUT /api/audits/settings: the dialog's data. Changing it needs admin or manager, as for Daily Pulse settings. Patterns are normalized (lowercased path, no trailing slash) and refused when they aren't a path.
    • GET /api/audits/auto-runs/latest: the latest run (last 30 days) that audited this brand's pages. It returns its pages, completed, running, and critical issues (the signals those audits failed).
  • lib/audit/auto-audit-email.js: "Site Audit: 25 pages audited · 6 critical issues · next run Oct 28", per brand when the run covered several. Sent through Resend; a no-op where it isn't configured.
  • src/scripts/run-auto-audits.js: runs the sweep now. AUTO_AUDIT_ORG limits it to one org; AUTO_AUDIT_NOW takes another time as now.

Web

  • Settings button: in the Site Audit header, as in the mockup. It opens a dialog with:
    • auto-audit on/off with a one-line summary ("5 pages on days 1, 7, 14, 21 and 28 of your billing month: 25 of your 100 audits");
    • e-mail after each run;
    • excluded pages (one pattern per line);
    • pinned pages, with Unpin.
    • It is read-only for members who aren't admins or managers.
  • In-app notice: "Auto-audit · Oct 17 · 3 pages audited · 6 critical issues · next run Oct 21". It is dismissed per run, in the browser.
  • Audits this month card: its sub-line says what auto-audit keeps ("3 used · 10 for auto-audit"). The full quota card is SA-019.
  • Copy: the strings are in en.json under audit.settings, audit.autoRun and audit.overview.

Deploy

  • Apply 00167 before deploying the server: the quota reads its tables.
  • The sweep runs from the daily tracking job. Cloud orgs on Starter/Growth have auto-audit on by default, so their first run happens at the first daily job after deploy (the current week's run).
  • E-mails are off until a team turns them on.
  • get_site_audit_quota (MCP) now also returns the auto object. A docs follow-up for the skills can come separately.

Known limits

  • A server restart while a run's audits are in flight leaves that run running without its e-mail. Its audits time out as failed after 2 h (AUDIT_RUN_TIMEOUT_MS), as any audit does.
  • Run audits go through one in-process pool of 2. Many orgs on the same run day take a while to finish, but they never block user audits.
  • A page whose run audit failed waits 30 days for the next run. A user can still audit it by hand.

Related issue

None. Site Audit v2 spec, task SA-006.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00167. Use a cloud org on Starter or Growth with an active subscription, and a brand whose Opportunities tab lists pages.
  2. Run IS_CLOUD=true AUTO_AUDIT_ORG=<org id> node src/scripts/run-auto-audits.js from server/.
    • The current week's run audits the top pages: pinned first, then by priority.
    • A second run the same day starts nothing.
  3. Open Site Audit.
    • The notice sums up the run with the next run date.
    • The Audits this month card shows "N used · M for auto-audit" (M = 5% of the quota for each run still to come).
  4. Open Settings.
    • Turn auto-audit off and save: the card gives the reserved audits back, and the notice drops its next run.
    • Add /guides/* to excluded pages: those pages leave Opportunities.
    • Pin a page from Opportunities: it shows under Pinned pages and can be unpinned there.
  5. As a member who isn't an admin or manager, the dialog is read-only and PUT /api/audits/settings answers 403.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no errors; the 7 warnings are in files this PR does not touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • server: npm run format, npm run lint and npm test (1563) pass. New and updated tests:
    • lib/audit/auto-audit.test.js: share per run, defaults, schedule (afternoon renewals, short periods), due run, pending runs, page picking.
    • lib/audit/auto-audit-sweep.test.js: claim, pages across brands, quota cap, empty run, e-mail, every org started before any finishes, tried pages skipped.
    • lib/audit/auto-audit-email.test.js: subject, summary, per-brand lines, escaping.
    • lib/plan-guard-site-audit.test.js: reserved slots out of the user's pool, a run using them, the empty-pool message, unlimited plans.
    • routes/audits-settings.test.js: GET/PUT settings, roles, pattern validation, latest run.
    • routes/audits-pages.test.js: run audits marked auto in their own pool.
    • routes/audits-opportunities.test.js: exclude patterns in the inbox.
    • lib/audit/opportunities.test.js: pattern normalization and matching.
  • web: yarn test (1342) and yarn build pass. Tests cover the settings dialog, the run notice and the card's sub-line.
  • bash supabase/build-schema.sh: regenerated schema.sql.
  • Dev database: a rolled-back dry run, then the migration applied.
  • Browser: this branch's server (cloud mode) and web, with the demo org on Starter.
    • The script claimed day 14's run and started the 5 highest-priority pages; a second run started nothing.
    • Locally the audits fail without a Scrape.do key, so three were marked completed as samples for the notice.
    • Checked the notice, the card (87 of 100 left, 10 reserved), turning auto-audit off and on, a pattern, pin/unpin.
    • Views: 1440px, 1280px and 1024px, light and dark.
    • Afterwards: the samples were deleted and the plan restored.
  • Live run: an end-to-end run with real audits needs the Scrape.do and model keys — @gkhngyk, could you try one with the script?

🤖 Generated with Claude Code

cihan-gyk and others added 8 commits October 17, 2026 20:47
SA-004 of the Site Audit v2 spec, first sources: audits were only of the
URLs someone typed, with nothing suggesting which pages to audit (F5, F7).

- GET /api/audits/opportunities builds the inbox (lib/audit/opportunities.js):
  Google Analytics' open "no AI traffic" pages (page_opportunities: valuable
  pages AI engines send no visitors to) and the site's pages (site_pages)
  never audited or audited more than auditMaxAgeDays (90) ago. One row per
  page across spellings, with why (chips), its last audit and its AI visits
  over 30 days (Analytics where it has the page, else the snippet). Pinned
  pages come first; excluded ones are listed apart; checkout-like paths stay
  out. Until the priority score (SA-005) rows follow GA value, then never
  audited, then citations and sessions.
- Migration 00164 adds site_audit_page_prefs (pinned / excluded per page),
  written through PUT /api/audits/opportunities/prefs.
- The hub gets Opportunities and Pages tabs; Opportunities opens when it
  lists pages. Select pages to Audit selected (the bulk audit, within the
  quota), Pin / Unpin or Exclude (with Undo); excluded pages can be
  restored from the list under the table. The bulk bar takes each table's
  own audit label and actions.

Competitor-cited pages and Search Console sources come in later PRs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t Opportunities

SA-004's citation source: prompts where a competitor's page is cited and
the brand's never is point to the brand page closest to them, as a page
worth auditing.

- Migration 00165 adds audit_competitor_cited_prompts (service role only):
  per active prompt over a window, answers citing a competitor's domain,
  answers citing the brand's own, and the competitor domains cited, read
  from prompt_result_citations (domains match with their subdomains).
- competitorGapPages (lib/audit/opportunities.js) keeps the prompts the
  brand was never cited on and maps each to its target URL, else the best
  site_pages match (candidatePages, as Content opportunities use), one row
  per page with its prompts and competitor names.
- GET /api/audits/opportunities adds them as a competitor_cited reason over
  the last 30 days; without competitor domains it asks nothing, and on a
  database without 00165 (PGRST202) the other sources still list. Until
  the priority score (SA-005) these pages rank after GA's valuable pages and
  before pages never audited.
- The Opportunities table shows them as "Crema Club cited on 3 prompts"
  ("+N" for more competitors).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main took 00164 (insights topic rollups), so this branch's migration moves
to 00165_site_audit_page_prefs.sql; schema.sql rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main took 00164 (insights topic rollups) and the Opportunities prefs
migration moved to 00165, so this one follows as 00166; schema.sql rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each page in the Opportunities inbox gets a 0-100 priority: its value to
the business x the opportunity an audit can win x how easy it is to fix,
each 0-1, shown as the cube root of the product (SA-005). Pinned pages
stay on top; the rest are ordered by priority. The table shows the score
in a pill (dark from 80) with its parts on hover.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Site Audit's auto-audit (SA-006): 25% of the monthly quota audits the
Opportunities inbox's top pages by itself, in five runs of 5% (at least
one page) on days 1, 7, 14, 21 and 28 of the billing month. A daily sweep
after the GA sync claims each org's due run once, picks pinned pages
first and then the highest priority across its brands, skipping pages
audited or tried in the last 30 days, and runs them in their own pool.
The runs still to come keep their slots out of the user's pool; a run's
unused slots return when it is recorded.

Settings (admins and managers): auto-audit on/off for the organization
(on by default with a monthly quota, off without one), an e-mail after
each run, the brand's exclude patterns and its pinned pages. The Site
Audit page shows the latest run ("3 pages audited · 6 critical issues ·
next run Oct 21") and the quota card what auto-audit keeps.

Migration 00167 adds the settings and run tables and a source column on
site_audits and site_audit_usage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/lib/audit/opportunities.js
#	server/src/lib/audit/opportunities.test.js
#	server/src/routes/audits-opportunities.test.js
#	server/src/routes/audits.js
#	supabase/migrations/00166_audit_competitor_cited_prompts.sql
#	supabase/schema.sql
#	web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx
#	web/src/lib/actions/audits.ts
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is SA-006: auto-audit runs 5% of the quota on billing days 1/7/14/21/28 on the inbox's top pages, keeps the runs still to come out of the user's pool, and adds a Settings dialog and a run notice. Migration 00167 (applied on the dev DB) must be applied before the server deploys, since the quota reads its tables; Starter/Growth orgs have it on by default, so the current week's run happens at the first daily job. It's on main (merged after #1138, with your 00166 perf commit). Locally the audits fail without the Scrape.do/model keys, so could you try a real run with AUTO_AUDIT_ORG=<org> node src/scripts/run-auto-audits.js? Thanks!

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebuilt on main with schema.sql regenerated (00167 sits next to #1144's 00168). Your earlier merge of main carried no other changes and kept 00166's faster read.

Tested on the stack's tip over main:

  • Server: 1590/1590 pass.
  • Web: all tests pass, run locally with the Excel writer stubbed (CI's web job doesn't run tests).
  • CI is green.

00167 is applied on production. The three new tables have RLS with member select only, so settings and runs are written by the server alone, and source defaults to user on the existing 105 audits and 146 usage rows.

One thing to know before the server deploy: with no settings row, auto-audit is on for every plan with a monthly quota. From the deploy on, the runs still to come hold their 25% out of each org's pool. The quota message points to the setting to turn it off.

@gkhngyk
gkhngyk merged commit b7b901e into ansvisor:main Oct 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants