Skip to content

feat(action-center): keep when a competitor page was first cited and when it changed (learning C3, part 1) - #1745

Merged
gkhngyk merged 25 commits into
ansvisor:mainfrom
cihan-gyk:feature/competitor-timeline
Oct 29, 2026
Merged

gkhngyk merged 25 commits into
ansvisor:mainfrom
cihan-gyk:feature/competitor-timeline

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Learning roadmap C3, part 1 of 2 (server): keep when a competitor's page was first cited in the brand's tracked answers and when it changed, so "why did this competitor rise?" gets a dated answer.

Stacked on #1742 (C4, itself on #1720 → #1718); it reuses the page reader C4 shares. Review the last two commits; merge after #1742. The screen is part 2.

  • Migration 00376:
    • cited_page_features.sketch (jsonb): the hashes of 4-word runs that the task checks already use. No page text is stored, and 00369 is untouched.
    • New competitor_page_events (first_cited | changed; brand_id, competitor_id, page_key, url, domain, at, detail). Members read it through RLS, only the server writes it, and a partial unique index keeps one first_cited per page.
    • New competitor_timeline_scans: when each competitor was last scanned. Service role only.
    • RPC competitor_cited_urls(p_brand_id, p_since): the URLs on a competitor's domains (subdomains included) cited in the brand's answers, with first-cited date and answer count. Service role only.
  • competitor-timeline.js:
    • recordFirstCitations: the first time each competitor page was cited, once per page. A new competitor is scanned 90 days back, then from a day before its last scan. It reads existing citations only, so it costs nothing.
    • recordPageChanges: re-reads, once a week, the competitor's 10 most-cited pages of the last 14 days whose last read is over 7 days old. It uses only the reads left in the brand's day (C1 and C4 share the same 12 a day) and obeys robots.txt. A page whose sketch similarity drops below 0.9 with at least 80 runs changed gets a changed event; its detail names the Site Audit checks the page gained and lost.
    • sweepCompetitorTimeline is chained after sweepTaskExecution in server.js, so C1's and C4's reads go first. It takes the longest-unscanned brands first, at most 200 a night, within 10 minutes. On Cloud, only active plans.
  • readPages now keeps each read's sketch and hands a re-read page its previous read as before; freshDays is a parameter. C1's and C4's tests pass unchanged.
  • Thresholds in action-engine.js (competitorTimeline). schema.sql regenerated.

Product decisions (Cihan): weekly re-reads of the 10 most-cited pages per brand, within the daily limit. Adding these events to the C07 action's evidence is a later, separate task.

Deploy: apply 00376 before the web part (the next PR) ships; without it the card says "Couldn't load the timeline" and the rest of the page works.

Related issue

None (learning roadmap C3).

Type of change

  • feat — New feature

How to test

  1. Apply 00376. After a night run (or calling sweepCompetitorTimeline on a dev server), competitor_page_events holds first_cited rows for competitor pages cited in the last 90 days, one per page.
  2. Change a competitor page's text substantially (a test page), wait past 7 days or lower freshDays, and run again: a changed row whose detail lists the gained/lost checks.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Migration added as a new file (00376); bash supabase/build-schema.sh run, schema.sql included; applied on the dev database
  • Server: npm run format, npm run lint, npm test pass (283/283 files, at the top of the stack incl. part 2)
  • Tests added (competitor-timeline.test.js; C1/C4 reader tests unchanged)
  • The embed guard from test(web): fail on an embed between twice-joined tables that doesn't name its foreign key #1714 passes on this branch
  • Docs updated (docs/guides/competitors.mdx)

🤖 Generated with Claude Code

cihan-gyk and others added 18 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]>
…etitor pages AI cites have that yours doesn't

Learning C1, part 2 (web). The task now carries what the nightly read found
(part 1); the action drawer shows it when the task is opened.

- Task rows read `output`; Analyze competitor content's is parsed
  defensively (competitor-findings.ts: pages read, the brand's page, the
  gaps, pages left for tomorrow, why nothing was compared).
- TaskDetailView shows "What the pages AI cites instead of yours have":
  each gap as "FAQ schema — on 2 of 3 of their pages, not on yours" (the
  check's name as Site Audit's rubric gives it), the pages read with their
  words and how many answers cited them, the brand's page, and a note that
  only what the checks found is kept.

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]>
…f theirs is too few, or yours couldn't be read

Review fixes before the PR (learning C1, web), after the server's:

- The "N more pages wait for tomorrow's reads" line is gone: an action's
  pages are now read the same night or not at all, so nothing is capped.
- With one competitor page read, the task says it takes at least two to
  compare, instead of "Your page passes…".
- The brand's page that couldn't be read says so, rather than "None of
  your pages was cited".
- "at least half of their pages" in the no-gap line, as the server counts;
  the no-prompts and none-cited reasons say what they now mean.

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
…om its other pages (learning C4)

G14 (AI landing pages converting well) and G10 (prompts rising) point at
pages that already work; nothing said what they have that the brand's
other pages don't. A new automatic task, analyze_winning_pages ("Find
what sets your winning pages apart"), compares them through Site Audit's
own checks (page-features.js) with the brand's other pages audited in the
last 90 days:

- winning pages: G14's landing pages (an Analytics path on the brand's
  primary domain), or G10's own pages its prompts' answers cited in the
  last two weeks; at most three;
- the other pages are read from their audits only (audit_signal_results),
  never fetched; at most 30, newest audit first;
- a winning page without a recent audit is read like a page AI answers
  cite: competitor-pages.js's reader, now readPages, shared with C1 and
  within the same perBrandPerDay (12) reads a day;
- the result: the checks most winners pass and most other pages don't,
  widest gap first, and up to five other pages missing the most of them.
  Fewer than three other pages audited: the task stays a person's with
  the reason and tries again after seven days.

The task requires site_audits, so a brand without audits doesn't get it.
G10 and G14 move to version 2: tasks are planned when an action opens,
so actions already open keep theirs and new ones get the task. The task
library holds 63 primitives (the spec's 62 and this one). No migration.
The Action Center drawer shows the findings on the task, as C1's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…when it changed (learning C3, part 1)

A competitor rising in AI answers had no "why" beside it. Each night now
keeps two kinds of dated events for the competitor pages the brand's own
tracked answers cite (competitor_page_events, 00376):

- first_cited: the first day the brand's answers cited the page, read in
  the database from its citations (competitor_cited_urls, service role
  only): no page read. A competitor never read is read back 90 days once,
  the others from a day before their last read (competitor_timeline_scans).
- changed: the brand's 10 competitor pages cited most over two weeks are
  read again once their last read is a week old, within what is left of
  the brand's 12 page reads a day after the night's tasks (C1, C4), and
  compared with the last read's word sketch by the Action Center's page
  checks' rule (task-checks.js: similarity under 0.9 and 80 runs changed);
  the event keeps the Site Audit checks the page gained and lost.

competitor-pages.js readPages now keeps each read's word sketch
(cited_page_features.sketch: hashes of its 4-word runs, never the text)
and hands a page read again what the read before kept. The sweep runs
after the task sweep, brands read longest ago first, at most 200 a night;
on Cloud only brands whose plan is active. Members read the events (RLS);
the server writes them. The Competitors page shows them in part 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… page gained and lost (learning C3)

The web has no names for Site Audit's checks: a changed event now keeps
each check gained or lost as { key, label }, the label as the rubric
gives it, as "Analyze competitor content" keeps its gaps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
gkhngyk and others added 6 commits October 29, 2026 15:34
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.
… covers

The nightly timeline read its competitors, its scans and a brand's cited
competitor pages in one request each, so past PostgREST's 1,000-row cap
the rest were left out, and it asked for the pages already recorded in
one filter, which past a few hundred addresses is longer than a request
may be: that brand then failed every night. The reads now go page by
page, and the filters in parts.

@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 ce2266f: the nightly timeline now reads every competitor, scan and cited page past PostgREST's 1,000-row cap, and asks for the pages already recorded in parts. Branch take-1745: this head 95baa29 with take-1742 merged in (#1718's fix), then the fix. The follow-ups below stay open.

At merge time I also revoked the default anon/authenticated grants on competitor_page_events before granting members select, so members get select and nothing more (dc212bc).

What I changed (server/src/lib/action-center/competitor-timeline.js):

  • The in filter's URL. recordFirstCitations asked for the first_cited events already kept with one .in('page_key', …) over every competitor page the brand cited since its last read. On the first night that's 90 days back, often hundreds of addresses. PostgREST carries in values in the URL, and chunked-in.js records that about 16 kB already fails at the edge. That brand's read would fail before the scan was saved, so it would read 90 days back and fail again every night. It now goes through selectInChunks, 30 keys a request.
  • The 1,000-row cap. competitor_cited_urls returned all of a brand's cited competitor addresses in one call, and max_rows = 1000 cuts a set-returning RPC off too, in no set order. So past 1,000 addresses, first citations were missed, and the weekly re-read's "cited most" was picked from an arbitrary part. The sweep's competitors read (every brand's) and competitor_timeline_scans read (one row per competitor) had the same cap: past 1,000 rows, brands were skipped or ordered wrongly. All three now read page by page (allRows: an ordered query read through .range).
  • The organizations read for the plan check now goes through selectInChunks as well (one uuid per organization with competitors).
  • Tests: 2,500 cited addresses are all recorded, read in three pages, with no in over 30 keys. A brand past the first 1,000 competitor rows is reached. The test's mock now honours range and the 1,000-row cap. Without the fix both tests fail.

What I checked:

  • Migration 00376. cited_page_features.sketch is added to 00369's table, which is new (no rewrite of a big table). competitor_page_events: RLS on, one select policy for members through brands ↔ profiles (the usual organization scoping), grant select to authenticated, no write policy, so only the service role writes. A partial unique index keeps one first_cited per page, and (brand_id, competitor_id, at desc) serves the card. competitor_timeline_scans: revoke all from anon and authenticated, service role only. competitor_cited_urls: security definer, search_path = public, revoke all from public, anon and authenticated, execute for the service role only. It filters on prc.brand_id = p_brand_id first, which the (brand_id, created_at desc) include (url_id, prompt_result_id) index covers. Everything is if not exists / create or replace, and the columns it reads exist on main's schema. No clash: 00376 isn't on main or in any other open PR, and this PR doesn't change 00369.
  • Reads and cost. The weekly re-read uses #1718's readPages: safe-fetch, robots.txt, 8 s / 3 MB, within what's left of the brand's 12 reads a day after the tasks. At most 10 pages a brand, 200 brands a night, and no new brand started after 10 minutes. No AI. What's kept is the checks and a hash sketch of 4-word runs, never the text. The sweep runs after sweepTaskExecution settles, so the tasks get the day's reads first.
  • Change rule. pageChange reuses the page checks' CHANGED_BELOW / MIN_CHANGED_RUNS and the sketch version, and an unreadable re-read keeps the last sketch.
  • Embeds. competitors → brands!inner has one foreign key (competitors_brand_id_fkey).
  • Tests. On main 72f602f merged with take-1724 and take-1746, the full server suite gives 3,823 passed. The 9 failing files are 8 that import framer-api, which my scratch node_modules lacks (they fail the same on main), and billing.test.js under load, which passes alone. competitor-timeline.test.js passes (10). Prettier and eslint are clean on the changed files.
  • Merge. Clean on main 72f602f, supabase/schema.sql included.

Follow-ups:

  1. Grants on competitor_page_events. Supabase's default privileges give anon and authenticated every grant on a new table. RLS blocks the writes (there's no write policy), but revoke all … from anon; revoke insert, update, delete … from authenticated; would match 00369's table.
  2. like '%.' || host. A competitor domain with _ would match one more character as a wildcard. Harmless, but right(u.domain, length(host) + 1) = '.' || host says exactly what's meant.
  3. The RPC by pages runs the function once per 1,000 rows. That's fine at today's sizes (see the query below). If a brand ever has many thousands, the first_cited insert could move into SQL (insert … on conflict do nothing).

@gkhngyk
gkhngyk merged commit c153732 into ansvisor:main Oct 29, 2026
4 of 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