Skip to content

fix(audit): run one audit per page at a time, so the same page never costs twice (00431) - #1920

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/audit-one-run-per-page
Nov 1, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/audit-one-run-per-page

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Two requests for the same page could each start an Site Audit and each use a credit. This happened with two clicks, or with /pricing, /pricing/ and /pricing?utm=… one after another, and we measured it: three spellings used three credits. Now one audit runs per page at a time, and a page audited in the last 10 minutes asks before it runs again. Customer priorities plan, P0 item 3.

What changes

  • One server path: Site Audit, Re-audit, AI Traffic, bulk audit, auto-audit, Chat and MCP all go through it.
    • Page key: a page is known by its normalized pageKey: host without www, lowercased, path without a trailing slash, no query or fragment.
    • Already running: the running audit comes back, and no credit is used.
    • Quota: checked only for pages that really start.
    • Stuck runs: a running row older than 2 hours (cut short by a restart) is marked failed first, so it doesn't hold the page.
  • 00431:
    • adds site_audits.page_key and fills it for existing rows;
    • adds a partial unique index on (brand_id, page_key) where status = 'running', so a racing request loses and gets the running audit back;
    • adds an index for the start's look-up.
  • Audited in the last 10 minutes:
    • One page: the API answers 409 recently_audited with recent: { auditId, finishedAt }. The page asks "Audited N min ago. Open it or audit again?", and Audit again sends force: true.
    • Bulk: those pages come back in recent, and a toast offers Audit again.
    • Chat, MCP, the in-app agent and REST: run_site_audit and the MCP tool take force and tell the model, so it asks the person first.
  • Credit: used once, when the audit completes.
  • Help: "Run a site audit" gets "Auditing a page again".

For @gkhngyk:

Related issue

None. Customer priorities plan, P0 item 3 (no double credit).

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 00431.
  2. Start an audit of /pricing, then right away /pricing/ and /pricing?utm=x. One audit runs, and one credit is used when it completes.
  3. When it's done, audit /pricing again. It asks "Open it or audit again?", and Audit again runs a new one.
  4. Re-audit several pages from the Pages list, one of them just audited. The toast names it and offers Audit again.
  5. Unit tests:
    • two requests at once make 1 audit and 1 job;
    • three spellings make 1 audit;
    • a recent audit answers 409 with no quota check;
    • force or 10 minutes later starts a new one;
    • a stuck run is released;
    • bulk and auto-audit skip a running page;
    • on the web, the new return shapes and the Open it / Audit again flow.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)
  • Migration added (00431) and supabase/schema.sql rebuilt

Full checks on this branch (a1d34d0, from 1220a2b): web 801 files / 8,346 tests pass, and yarn build passes. Server format:check and lint are clean, and 352 files / 4,995 tests pass (3 skipped).

🤖 Generated with Claude Code

…costs twice (00431)

Two requests for one page at once started two audits and used two
credits, and /pricing, /pricing/ and /pricing?utm= were three pages with
three credits. Nothing in the database stopped it.

- Server: every start (Site Audit page, Re-audit, AI Traffic, bulk,
  auto-audit, Chat, MCP) goes through one path that keys the page by its
  normalized address (pageKey). A page with an audit running gets that
  audit back with no new credit; the quota is checked only for the pages
  started. A run older than two hours (cut short by a restart) is marked
  failed first so it doesn't hold its page.
- 00431: site_audits.page_key, filled for the audits kept, and a partial
  unique index (brand_id, page_key) where status = 'running', so two
  requests that race can't both start; the loser gets the running audit.
- A page audited in the last 10 minutes isn't started unless force: POST
  /api/audits and the internal endpoint answer 409 recently_audited with
  that audit, and bulk lists such pages in `recent`.
- Web: one page asks "Audited N min ago. Open it or audit again?" (Site
  Audit page, report re-run, AI Traffic card); several pages say which
  weren't started, with Audit again. Chat's run_site_audit, the MCP tool,
  the in-app agent and the REST route take `force` and say the recent
  audit back so the model asks first.
- Help: "Run a site audit" gets "Auditing a page again".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mintlify

mintlify Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit e342468.

Approve preview

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 1, 2026
gkhngyk
gkhngyk previously approved these changes Nov 1, 2026

@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.

LGTM

# Conflicts:
#	server/src/routes/audits-pages.test.js
#	supabase/schema.sql
#	web/messages/en.json

@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.

LGTM

@gkhngyk
gkhngyk merged commit 2737664 into ansvisor:main Nov 1, 2026
6 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 2, 2026
…iewer (#1964)

The MCP tools changed in #1920, #1927 and #1916; the skills and the MCP
guide still described the old answers.

- get_ai_traffic / GET /ai-traffic: `tracking` (not_detected,
  installed_quiet, active); a 0 with not_detected means nothing is
  measured yet. Coach skills (MCP + REST), mcp-server.mdx and
  api-reference/metrics.mdx (GET /api/v1/ai-traffic returns it too).
- run_site_audit / POST /site-audits: a running page's audit comes back
  with alreadyRunning and no credit; a page audited in the last 10 minutes
  returns recently_audited and starts nothing unless force. Coach skills,
  audit-fix (Chat) and mcp-server.mdx; library.json refreshed.
- get_content_draft_package: the qualified reviewer (reviewer) and its
  WebPage reviewedBy JSON-LD once the page names them. mcp-server.mdx.

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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