Skip to content

feat(action-center): read the competitor pages AI answers cite and say what they have that the brand's page doesn't (learning C1) - #1718

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/competitor-page-features
Oct 29, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/competitor-page-features

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Learning C1: "Analyze competitor content" reads the competitor pages AI answers cite, and says what they have that the brand's page doesn't.

This is the bottom of a 4-PR chain (C1 → C1 UI → C2 brief → C2 Improve). Migration 00369. This PR's own commits are 73952ca, 380f81d and 7959358, plus the merge of main.

Until now the task was a step for a person: nothing read the pages AI engines cite instead of the brand's. The registry now gives it a tool, competitor_page_features. It has writesExternally: false, so the nightly task sweep runs it without approval, as it already runs the AI-traffic report.

What it reads

  • The answers: the brand's own tracked answers from the last 14 days, without chatgpt-shopping, for the action's prompts. Those are, in order:
    • the prompts the action names;
    • otherwise its topics' prompts;
    • otherwise, for an action about competitors or sources, the brand's active prompts.
    • At most 50 prompts and 300 answers.
  • The pages: up to 3 cited pages per action.
    • The competitors' pages come first, most cited first. Then come pages cited in answers that don't name the brand.
    • The brand's own most-cited page is read too.
  • How: with safe-fetch only (public addresses only, so no SSRF), with an 8 s timeout.
    • robots.txt is read once per origin as ansvisorbot. A whole-site Disallow for it, or for *, keeps the reader out.
    • A robots.txt that can't be read (5xx, 429, timeout) also keeps it out. A 4xx means there is none, as RFC 9309 says.
    • Rules for single paths aren't read; see the follow-ups.
  • The checks: 15 Site Audit checks that read the page alone:
    • length, structural depth, H2 coverage, lists, tables;
    • FAQ schema, JSON-LD;
    • statistics, quotations, citations, outbound authority links;
    • byline, date markup, freshness, readability.
    • Under 500 characters of text, what was read is a wall (consent or sign-in), so the page counts as unreadable.
  • Only addresses the brand's own answers cited are read, never a URL from a request. Nothing is summarised by AI.

Migration 00369, cited_page_features

  • One row per brand and page, unique on (brand_id, page_key). Columns: url, domain, own, features jsonb and read_at.
  • features is { words, checks: { key: pass|warn|fail|na } }, or { unreadable: true[, disallowed: true] }. Never the page's text.
  • RLS is on with no policies. All rights are revoked from anon and authenticated, and granted to service_role only. Members see the result through the task's output.
  • It ends with notify pgrst. schema.sql is regenerated.

Limits (competitorPages in config/action-engine.js)

  • 3 pages per action.
  • 12 new reads per brand per UTC day. Unreadable pages count too. An action whose pages don't fit in what's left waits for the next night. All of an action's pages are read in the same night, or none.
  • A page read in the last 14 days is reused, not read again.
  • It takes at least 2 competitor pages to compare.

The result is the task's output: competitorPages, yourPage, yourPageStatus, gaps, compared and unreadable. gaps lists the checks that at least half of their pages pass and the brand's page doesn't.

execution/run.js

  • A tool can now throw retryLater (ReadLaterError). The run is recorded as blocked, and the task stays as it was.
  • When there's nothing to compare (no prompts, no page cited, none readable), the task's output keeps the reason and a retryAfter 7 days later. The sweep passes the task over until then, and it doesn't take a place under maxTaskRunsPerNight. The summary counts these tasks as waitingToRetry.

Smaller changes

  • basket-metrics.js: topicPromptIds() is moved out of basketScope. It's the same query, and now takes several topics.
  • page-features.js holds the checks without the Supabase client, so C2 and its tests load in CI.

Who's affected after merge

  • On the cloud, the nightly sweep starts running open "Analyze competitor content" tasks, only for organizations on an active plan. These tasks come from actions C01, C04, C07, C08, C09, G18, P04, R07 and topic gaps.
  • Runs stay under the existing maxTaskRunsPerNight (50).
  • A run that compares pages marks the task completed with its findings. A run with nothing to compare leaves the task open for a person, with the reason.
  • Outbound: at most 12 page reads per brand per day, plus robots.txt reads. They're free.

Where people see it: Action Center › an action's Tasks. "Analyze competitor content" is done overnight. The next PR (C1 UI) shows what it found in the drawer.

Related issue

Agentic learning report, phase C (C1). No GitHub issue. This follows product decision 2: free reads with safe-fetch, Site Audit's rule checks, a daily cap per brand, features only, and no paid scraping or AI summary in v1. Nothing here reads another brand's data.

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 00369_cited_page_features.sql to a dev database.
  2. Pick a brand that has competitors and tracked answers from the last two weeks. Find an open task:
    select t.id from action_tasks t join actions a on a.id = t.action_id where t.task_key = 'analyze_competitor_content' and t.status = 'todo' and a.brand_id = '<brand>';
  3. Run cd server && node src/scripts/run-action-task.js <taskId> --dry, then run it again without --dry.
  4. Check the results:
    • select page_key, own, features, read_at from cited_page_features where brand_id = '<brand>'; shows checks only, never text.
    • action_tasks.output holds the findings.
    • Running another task for the same prompts reuses the pages, with no new reads.
  5. cd server && npx vitest run src/lib/action-center/competitor-pages.test.js src/lib/action-center/page-features.test.js src/lib/action-center/execution/run.test.js src/lib/action-center/basket-metrics.test.js

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no web change
  • yarn typecheck passes (run from web/): no web change
  • yarn format:check passes (run from web/): no web change
  • Changes are focused — one concern per PR

What was run:

  • Main (ce9a3dc) is merged in, with no conflicts.
  • Migrations:
    • bash supabase/build-schema.sh matches the committed schema.sql.
    • No existing migration is changed, and there are no duplicate numbers.
    • 00369 was applied on the dev database earlier.
  • On this tip:
    • The four test files above pass (69 tests).
    • prettier --check and eslint pass on the changed server files.
    • The embed-hint guard test (the follow-up to 62eb7b7) passes.
  • Not run in this round: the full server suite (npm test). Other heavy checks were running on the same machine.

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits October 28, 2026 10:15
…y what they have that the brand's page doesn't

Learning C1, part 1 (server). "Analyze competitor content" was a step for
a person: nothing looked at the pages AI engines cite where the brand isn't
named, so "why do they win?" went unanswered.

- competitor-pages.js: from the brand's own tracked answers for the
  action's prompts over 14 days, the competitors' most cited pages, then
  pages cited where the brand isn't named (3 an action), and the brand's
  own page cited there. Each is read free (safe-fetch, public addresses
  only, never Scrape.do) and run through Site Audit's checks that read the
  page alone (15: structure, FAQ, schema, statistics, dates, length…). The
  result: each page's passing checks and the checks at least half the
  competitor pages pass that the brand's page doesn't.
- 00369 cited_page_features: the checks' statuses and word count per brand
  and page, never the page's text; reused for 14 days; a brand reads at
  most 12 pages a day (config competitorPages). Service role only; applied
  on the dev DB.
- The task gets the tool competitor_page_features, so the nightly runner
  carries it out (reading a public page changes nothing, no approval). A
  run with every read for the day spent leaves the task for the next night
  (retryLater: recorded as blocked, task untouched).
- No AI summary, no paid scraping (user decision 2). The guide says so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… there is nothing to compare, and read every page of an action the same night

Review fixes before the PR (learning C1, server):

- An action that names no prompts (8 of the 9 plans with this task) reads
  its topics' prompts (topicPromptIds, shared with basket-metrics), else
  the brand's tracked prompts (at most 50), instead of ending on
  "no_prompts".
- No prompts, no page cited, or none readable no longer completes the
  task: ReadLaterError carries why as the task's output and retryDays (7);
  runTask keeps it on the open task with retryAfter, and the nightly sweep
  passes the task over until then without it taking a place under the
  ceiling.
- An action's pages are all read the same night or none: with fewer reads
  left than pages to read, nothing is read and the task waits for the next
  night. No more partial runs ("capped" is gone).
- yourPageStatus says whether the brand's page was read, not cited, or
  couldn't be read.
- A page with under 500 characters of text (a consent wall, a page drawn
  by script) is unreadable.
- An unreadable page is kept as { unreadable: true }: it counts against the
  day's reads and isn't read again while fresh. A row that can't be written
  is logged, not counted as unreadable.
- Gaps need at least two competitor pages ("compared"); a check is a gap on
  at least half of them.
- robots.txt: a site whose robots.txt shuts AnsvisorBot (or every crawler)
  out of the whole site isn't read, nor one whose robots.txt can't be read
  (RFC 9309); path rules aren't read yet (robotsGroups).
- 00369: the unreadable row shape in a comment (no schema change).
- The guide: the task is carried out by Ansvisor, not waiting for
  approval; what it reads, the two-page minimum, robots.txt and the weekly
  retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s without Supabase

FEATURE_CHECKS, pageContext, pageFeatures, featureGaps and
MIN_PAGE_CHARACTERS move from competitor-pages.js to page-features.js,
which reads no database. competitor-pages.js loads the supabase admin
client, whose config exits when the SUPABASE_* env is absent (as in CI),
so a pure module that needs only the checks (the brief, Improve) would
take every test file reaching it down with it. competitor-pages.js
re-exports them, so its imports keep working; their tests move next to
the new module, with no supabase mock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…page-features

# Conflicts:
#	supabase/schema.sql
@mintlify

mintlify Bot commented Oct 28, 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 c4bb680.

Approve preview

A page of tens of thousands of empty elements held the server for a
minute: removing scripts and styles before reading the text spliced
each out of its parent, and readability's block find was quadratic in
the elements it started from. Both are now one walk over the page,
with the same words and blocks as before.

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

Looks good with 82f1b07: reading a cited page's words and Site Audit's readability blocks now takes one walk over the page, so a page full of empty elements reads in about a second. Branch take-1718, on this PR's head deb569e. The follow-ups below stay open.

What I changed:

  • page-features.js pageContext. It parsed the page twice and removed script, style, noscript, template, svg from the second parse before reading its text. Cheerio removes each element by splicing it out of its parent's children, so the removal is quadratic in their number. Timed on the 3 MB that safe-fetch allows: a page of empty <style> tags took 75 s (1.5 MB: 21 s). The text now comes from one walk over the first parse that skips those elements (bodyText), with the same words: checked against the old removal on 3,000 generated pages, no difference. Same page now: 0.6 s.
  • audit/signals/readability.js readingBlocks. $('body').find('h1, h2, …') is quadratic in the elements directly under the body (css-select's removeSubsets): 20,000 short paragraphs took 3 s, 175,000 empty <li>s about 60 s. It also ran a closest/parents selector per element. The block elements now come from one walk in document order, and "inside a list item or a cell" is an ancestor walk by tag name. Checked against main's version on 3,000 generated pages: the same blocks. Site Audit and the editor's readability get the same speed-up (their scores don't change).
  • Tests: page-features.test.js (the text matches the old removal; 100,000 empty styles and 40,000 empty list items read in seconds) and readability.test.js (40,000 paragraphs in one pass). Without the fix they take 59 s and 11 s.

What I checked:

  • Outbound reads. Only addresses from the brand's own tracked answers are read, never one a request names. Every read goes through safe-fetch: http(s) on standard ports, no credentials, every resolved address public (checked in the connection's own lookup, so no DNS rebinding), redirects followed by hand and checked each time (at most 5), 8 s per read, 3 MB decompressed, too-deep pages refused. robots.txt is read through the same fetch (512 KiB, truncated). A 4xx means no robots.txt; a 5xx, 429 or timeout keeps the reader out, as RFC 9309 asks.
  • Cost. No AI and no paid scraper: at most 3 competitor pages and the brand's own page per run, at most 12 new page reads a brand a day, one robots.txt per site per run, and a page read in the last 14 days is reused. Runs come from the nightly task sweep (maxTaskRunsPerNight).
  • Page text never stored or sent to a model. cited_page_features keeps the checks' statuses and a word count only.
  • Migration 00369. New table only: RLS on, revoke all from anon and authenticated, service role only. Unique (brand_id, page_key) serves the upsert, and (brand_id, read_at desc) serves the day's count. No lock on an existing table, if not exists throughout. No clash: 00369 isn't on main or in any other open PR.
  • Embeds. prompts → prompt_sets!inner has one foreign key (prompts_prompt_set_id_fkey). The action_tasks embeds in run.js are main's own; this PR only adds retry_after:output->>retryAfter to the select.
  • Merge. take-1718 merges cleanly on main 72f602f. So does the whole stack.
  • Tests. I ran main merged with every take branch through the full server suite (results in the stack summary on #1745). page-features, readability, competitor-pages, run and audit pass on their own. Prettier and eslint are clean on the changed files.

Follow-ups:

  1. robots.txt paths. Only a whole-site Disallow: / (for ansvisorbot or *) is respected. A rule for a path (Disallow: /blog/) isn't, so a page under it is still read. RFC 9309 path matching would be small (longest match wins, Allow beats Disallow on a tie).
  2. Event loop. Even linear, Site Audit's checks over a 3 MB page take 1–4 s of synchronous work (a page full of tables: about 4.7 s, most of it readability's content extraction), and the nightly sweep runs them in the API process. A smaller maxBytes for third-party pages (1 MB covers almost every article), or a worker thread, would keep requests answered meanwhile.
  3. The day's cap is counted, then spent. Two runs for one brand at once could both pass the perBrandPerDay check. The nightly sweep runs tasks one at a time, so this only matters if the API ever runs more than one process.
  4. retry_after and the 1,000 tasks read. Tasks waiting to retry are filtered after .limit(1000), so with more than 1,000 waiting the runnable ones behind them aren't read. Filtering output->>retryAfter in the query would avoid it.

@gkhngyk
gkhngyk merged commit 38953cd into ansvisor:main Oct 29, 2026
5 of 6 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