Skip to content

feat(content): read a brand's facts from its own pricing, About and product pages, kept for approval (00269, CE-047, part a) - #1462

Merged
gkhngyk merged 19 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-brand-facts
Oct 24, 2026
Merged

gkhngyk merged 19 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-brand-facts

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Quality Phase 2, CE-047 "Brand facts", part a: the server. Parts b (weekly re-read), c (Brand settings tab) and d (Trust check + to-do) follow, stacked on this. Stacked on #1457 (it needs 00264's quality AI feature); review #1451 → #1457 first. Everything sits behind CONTENT_QUALITY_PHASE=2.

What it reads

  • The brand's own pricing, About and product pages, one of each.
  • How a page is found:
    • from the page list (site_pages), or the About page Site trust found;
    • else the usual addresses (/pricing, /plans, /product, /features) on the main domain. This is also the fallback when a listed page no longer answers.
    • A probe counts only when its address or title is the page's, its title or H1 says so, and it isn't the home page again.
  • A shop's product page is left out, since its prices are each product's.
  • Only the brand's own domains are read (the landing page must still be on them), directly through safe-fetch, never through Scrape.do.
  • The page's text has no scripts, menus, footers or struck-through old prices, and keeps the prices a toggle hides. It is built in one walk over the page, at 24k characters at most.

No made-up facts

  • The model only finds the facts (price, trial, founded, claim) and copies each one's words.
  • A fact is kept only when its words are on the page as written (curly marks folded), at a place that doesn't run into a number ($49 isn't in $499).
  • Its value is read from those words by rule, never from the model: amount, currency (marks and ISO codes, $49 CAD is CAD), period, billing, per seat, trial days, founding year.
  • A price needs its plan's name within 600 characters, stored as the page writes it (a Turkish "İ" is kept, not lowered).
  • A quote with two prices, a saving, a discount or an old price ("was $59") is dropped.

What it keeps (00269)

  • brand_facts: one row per (slot, value). A new value in a slot ("Pro, USD, a month": $49 → $59) is a new pending fact beside the approved one.
    • A fact no page read has any more is handled by status: an approved one is marked gone (left out of the check, removed after 30 days); a pending or rejected one goes.
    • A fact still on any page read stays (a price with its plan near it).
    • Members read the facts and may change only status, reviewed_by and reviewed_at, as themselves (a column-level grant plus RLS). Values are checked per kind.
  • brand_fact_pages (server-only): each page's text hash, so an unchanged page isn't read by the model again. No page text is kept.

Cost

  • One model call per page whose text changed, at most once an hour per page, held at $0.01 each on the workspace AI budget as quality. A brand costs nothing until someone opens its facts.
  • An answer that misses the schema is charged and read again after a day. A timeout keeps its hold. A spent budget stops the reads (paused).
  • POST /fetch reads the site again, at most every 10 minutes, with its own limiter.

Routes

  • GET /api/brand-facts?brandId= (the first ask reads the site), POST /fetch, PATCH /:id { status: approved | rejected }.
  • Each checks the phase and the brand's workspace. Logs carry an error's name, message and stack, never the provider's request.

Related issue

Quality spec (Drive), Phase 2, CE-047 (part a). Stacked on #1457.

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 00264, 00266 and 00269, and set CONTENT_QUALITY_PHASE=2.
  2. GET /api/brand-facts?brandId=<a brand with a pricing page>. It reads the pages once and returns pending facts with their quotes and pages.
  3. PATCH /api/brand-facts/<id> {"status":"approved"} as a member.
  4. Change the page's price, wait 10 minutes and POST /fetch. The new value is pending, with replaces naming the approved one.
  5. As a member, a direct PostgREST update of value is refused, and approving as another user is refused.

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

00269 is applied on the dev DB and schema.sql is rebuilt. On the dev DB, as a member role in a rolled-back transaction:

  • a member can read a fact and approve it as themself;
  • a member can't change its value, can't approve as someone else, and can't read brand_fact_pages;
  • the per-kind and source_url checks refuse bad rows.

Server: npm run format, npm run lint and npm test pass (2,464 tests).

Two pre-push reviews ran. Their points are in:

  • the page text built in one walk (a 100k-block page reads fast);
  • facts kept while any page has them;
  • the Turkish "İ";
  • stricter price parsing;
  • page statuses after a failed read;
  • write order;
  • hourly model reads;
  • probes and the home-page check.

🤖 Generated with Claude Code

cihan-gyk and others added 18 commits October 24, 2026 13:18
…he weak ones (CE-046, part a)

Quality phase 2 (NEXT_PUBLIC_CONTENT_QUALITY_PHASE=2), by rules only:

- sourceClass(): primary (official, research, standards), secondary (news,
  reviews, Wikipedia), weak (forums, social posts, uploaded papers), a
  competitor's page (primary on a comparison) or the brand's own; an
  address the rules can't place is left out (an AI read comes in part b)
- Trust's primarySources (15) and noWeakSources (10); two placed sources
  score Trust on their own
- Quality suggests replacing weak sources (Show, Keep) and adding a
  primary one, once Trust is scored; the editor underlines weak sources
  in amber (a decoration: never exported)
- 00263: content_drafts.kept_sources (≤ 50) and content_draft_keep_source()
- byUrl keeps a link's first place, so Show goes there

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… with AI (CE-046, part b)

Quality phase 2 (web NEXT_PUBLIC_CONTENT_QUALITY_PHASE and server
CONTENT_QUALITY_PHASE at 2):

- POST /api/content-pages/source-classes reads the sites a draft links
  (as saved) that the editor's rules can't place: the model sees each
  site's address alone and answers primary, secondary or weak; each read
  is kept for every workspace 90 days (00264 content_source_classes); 20
  sites a read, 30 reads a draft a day, its own rate limit
- the reads draw on the AI budget as their own feature, "quality" (00264
  widens prompt_improve_usage.feature): a small hold (holdUsd), left out
  of the median that sizes other calls' holds and the Improve tab's
  estimate; a read cut off by its timeout keeps its hold; Billing lists it
- the editor asks 3 s after a save (useSourceClasses), Trust takes the
  reads for sites the rules can't place, the weak-sources suggestion is
  tagged "AI check", and a spent budget pauses the reads with a note

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rce-classes-ai

# Conflicts:
#	supabase/schema.sql
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_quality-tab.test.tsx
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_quality-tab.tsx
#	web/src/lib/actions/content-drafts.test.ts
#	web/src/lib/content/quality-checks.test.ts
#	web/src/lib/content/quality-checks.ts
#	web/src/lib/content/source-class.ts
#	web/src/types/supabase.ts
…a day, and skip kept sources (CE-046, part b review)

- SITE_NAME (LDH, a dot, a letter TLD; no IP, no single label) in
  keptSite/linkedSites, the editor's unplacedSites and 00264's check; a
  label that reads as words isn't asked about either
- the model may answer unknown, which Trust leaves out; it, and a site
  the model left out, is kept a day, so it isn't asked again (and
  charged) after every save
- a site whose every link the person kept isn't asked about (route and
  editor); a kept link counts in Trust with or without a read
- a failed classifyDraftSources call lets its sites go (.catch/.finally)
- .env.example: phase 2 turns the AI source reads on, from the AI budget

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rce-classes-ai

# Conflicts:
#	supabase/schema.sql
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
…ept by the paragraph (CE-045, part a)

Quality phase 2 (CONTENT_QUALITY_PHASE at 2), server side:

- POST /api/content-pages/claims reads the draft as saved: each paragraph
  and list item long enough to hold a claim; the model finds what a reader
  would want a source for (number, stat, superlative, fact), each claim in
  the paragraph's own words or dropped, five a paragraph at most
- 00266 content_quality_claims keeps the reads by draft and paragraph
  hash: an unchanged paragraph isn't read again, a removed one's row goes;
  the paragraph being written (skip) waits; 12 paragraphs a call, 2 calls
  an ask, one after the other
- the calls draw on the AI budget as "quality" (holdUsd 0.004), and share
  one day's cap per draft with the source kinds' reads
  (content-quality-calls.js, QUALITY_CALLS_PER_DAY 60)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s too, and keep a missed answer as read (CE-045, part a review)

- draftParagraphs reads p, li, blockquote, td and th as trustBlocks does,
  with ownText's spacing (a line break, sibling paragraphs): the answer is
  { paragraphs: [{ text, claims }] }, so the editor knows each claim's block
- blocks from 12 characters ("Award-winning support"); a call takes 12
  paragraphs or 6,000 characters, whichever comes first
- an answer billed that missed the schema is charged and its paragraphs
  read with none (not sent, and charged, on every save); a kind the editor
  doesn't know drops that claim only; a paragraph answered twice keeps both
- claims leave 10 of the day's Quality calls to the sources' kinds, and the
  route has its own limiter; the reading's version is in the hash; logs
  carry no page words

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… per site (CE-046, part b review)

One generateObject per site the rules can't place, run together with
Promise.allSettled: no site's name reaches another's read, and a failed
site keeps the others' reads. Each read holds $0.0005; the day's cap
counts sites (SOURCE_READS_PER_DAY = 40). An answer off the schema is
charged and kept as unknown for a day. The route logs an error's name
and message only.

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

# Conflicts:
#	server/src/lib/content-source-class.js
…tch claims across curly quotes, log no page words (CE-045, part a review)

- 00266 gets a `missed` column: an answer that missed the schema (cut
  short, or blocked by the provider's filter) keeps its paragraphs as read
  with no claims, and they're read again after MISSED_DAYS (1).
- keptClaims compares folded copies (’ ‘ → ', “ ” → ", ‐ ‑ – — → -),
  one character for one, and keeps the paragraph's own span.
- The claims route logs an error's name and message only: a provider's
  error carries the request, the page's paragraphs.
- QUALITY_CALLS_PER_DAY = 90, the claims leaving the sources'
  SOURCE_READS_PER_DAY (40) now that each site is a call: the claims keep
  their 50 calls a day.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d log no whole error (CE-046, part b review)

When no site is read, the error thrown is a spent budget's if any (429,
the editor pauses) rather than the first site's. An ask reads its sites
five at a time, so 20 holds and their queries don't go out at once. A
site's hold is $0.001 (the model's thinking is billed as output). The
route's error log keeps the stack, not the provider's request.

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

# Conflicts:
#	server/src/lib/content-source-class.js
…er the new ones, and fold more quote and dash forms (CE-045, part a review)

A paragraph whose answer missed the schema is read again after a day in
a call of its own, after the paragraphs not read yet: one the provider's
filter blocks no longer keeps its neighbours unread every day. The fold
also covers low quotes, primes, figure and horizontal-bar dashes and the
minus sign. The claims route keeps the error's stack in its log. Tests
pin the select's columns, both fold directions and the route's log.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…roduct pages, kept for approval (00269, CE-047, part a)

Quality phase 2, CE-047: the brand's prices and plans, trial, founding
year and claims, read from its own site for Trust's "consistent with
brand facts" check. The model finds the facts and copies each one's
words; a fact is kept only when its words are on the page as written,
and its value (amount, currency, period, days, year) is read from them
by rule. A person approves each one.

- 00269: brand_facts (members read, and change only the status, as
  themselves) and brand_fact_pages (server-only: a hash of each page's
  text, so an unchanged page isn't read by the model again; no text
  kept).
- brand-facts.js: findFactPages (page list, Site trust's About page,
  else probes on the main domain; never Scrape.do), factText, keptFacts,
  refreshBrandFacts (new values pending, approved ones gone from the
  site marked gone, pending ones dropped), readBrandFacts (first ask
  reads, asking again every 10 minutes at most), setFactStatus.
- GET /api/brand-facts, POST /fetch, PATCH /:id behind phase 2, with
  their own limiter; logs carry no page text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… still has, and store no wrong price (CE-047, part a review)

- factText walks the page once and stops near MAX_TEXT_CHARS: a page of
  100k blocks reads in well under a second (was quadratic). Struck-through
  old prices (del, s, strike) are left out.
- A fact still on any page read (its words, a price's with its plan near
  them; across a card's lines too) stays; only facts whose page was read
  and has them no more are marked gone or dropped, written once.
- A plan's name is found on the folded text without lowering it (a
  Turkish "İ" lowers to two characters), and a quote only where it
  doesn't run into a number ("$49" in "$499", "$1,2").
- parsePrice reads one price only, no saving, discount or old price, a
  dollar named by its code after it ("$49 CAD"), and the period in the
  words right after it, the first named ("Lifetime access, monthly
  updates" is once). Magnitudes in any case; a trial needs a trial word,
  "started in" is no founding year.
- A page read after a wall or a failure says "ok" again; a missed
  answer's hash isn't kept past a failed read. A changed page goes to the
  model once an hour at most (MODEL_EVERY_MS). A page that failed (or a
  spent budget) is read again at the next ask after RECHECK_MS.
- Facts are written before the pages' hashes; source_url is cut to 2048.
- A listed page that no longer answers falls back to the usual
  addresses; a probe that is the home page again isn't read as one.
- readPage maps not-HTML, refused and redirect-loop reads to not_found;
  401/403/429 to blocked, 5xx to failed. setFactStatus answers 404 for a
  fact removed meanwhile.
- GET has no bucket of its own (the editor reads it as its tab comes
  back); POST /fetch keeps brandFactsLimiter.
- 00269: each kind's value checked (amount ≥ 0, currency, days, year),
  source_url http(s) only, a member's reviewed_at within a minute of now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…after a price, and read brand facts once at a time (CE-047, part a review 2)

- A page under 200 characters (one its scripts write, a wall not told
  apart) leaves its facts as they are on every read, not only the first.
- A currency code or a scale right after a quote is read ("$49" then
  "CAD per month" is CAD; "$2" then "million" is no price); only "off"
  right after a price drops it (the next card's "Save 20%" doesn't). A
  period is named within 24 characters of the price, the first one
  named; "per active user" is per seat.
- A plan's name matches as whole words ("Pro" isn't in "Product"); a
  quote isn't taken inside another mark's dollar ("C$49") or a
  space-grouped number ("299 €" in "1 299 €").
- factText counts words toward its length, not indentation.
- A page whose model read failed waits MODEL_EVERY_MS like any other;
  a GET reads a failed page again behind its answer, not before it.
- New facts get the room the removed ones leave; a page whose new facts
  didn't fit keeps its old hash, to be read again. A kind no longer read
  (a shop's product page) loses its row.
- refreshOnce: one read per brand at a time, for the tab and the weekly
  read alike.
- 00269: a missing key is a check violation, and numbers compare as
  numeric (no cast error).

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 24, 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.

Looks good. One small follow-up:

  1. A missed page skips the hourly model cap when its text changes (server/src/lib/brand-facts.js:764). if (extracted > now - MODEL_EVERY_MS && !missed) lets a page whose last answer missed the schema go straight to the model whenever its text differs from the stored hash. A page with a counter or a date in its text, where the answer keeps missing, is then read by the model on every POST /fetch (every 10 minutes) instead of once a day, as the description says, or once an hour. Fix: drop && !missed, so the hourly cap applies to missed pages too. The day-old retry in unchanged (749–751) still covers an unchanged missed page. Add a test with a missed page whose text changes inside the hour.

Checked, no action needed:

  • Fetching: it goes through safe-fetch's fetchPage: public-only DNS at connect time, every redirect re-checked (5 at most), standard ports, no credentials, an 8 s timeout, and a 1.5 MB cap counted after decompression. Only pages that land on the brand's own domains are read.
  • Model output: a fact is kept only when its words are on the page, and its values are parsed by rule. Everything written is scoped to the brand being read, starts as pending, and is capped at 20 per page and 60 per brand.
  • Budget: a $0.01 hold as quality on each changed page, with misses charged and timeouts kept.
  • 00269: dry-run on top of 00264 and 00266. RLS is on. A member can read and approve or reject as themselves, within ±1 minute. A member can't change values, approve as someone else, insert, delete, or read brand_fact_pages. Another workspace sees and changes nothing, and anon has no access. The per-kind and source_url checks reject bad rows.

@gkhngyk
gkhngyk merged commit 46de5d7 into ansvisor:main Oct 24, 2026
4 of 5 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 24, 2026
… its status through a failed weekly read (CE-047 follow-ups) (#1466)

- A page whose answer missed the schema goes to the model once an hour
  at most when its text changes, as any other page; it stays missed,
  its hash and time kept (#1462 review).
- The weekly read marks a failed brand's pages failed except a missed
  one, which only gets today's date: its retry after a day stands
  (#1463 review).
- A weekly run stops after 10 minutes; the brands left stand first in
  line for the next run (#1463, optional).

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