Skip to content

feat(content): read the claims in a draft's own paragraphs with AI, kept by the paragraph (CE-045, part a) - #1457

Merged
gkhngyk merged 16 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-claims
Oct 24, 2026
Merged

gkhngyk merged 16 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-claims

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Quality Phase 2, CE-045 "Claim detection in user text", part a: the server. Part b (the editor: Trust counts the claims, a red wavy underline on one with no source, Quality's Show / Remove) comes next. Stacked on #1451 (the quality AI feature, holdUsd, the "AI check" tag); review that first. Everything sits behind CONTENT_QUALITY_PHASE=2.

What it reads

  • POST /api/content-pages/claims { draftId, skip? } reads the draft as saved.
    • The blocks are the editor's Trust blocks (trustBlocks): paragraphs, list items, quotes and table cells with their own words, with the editor's spacing for line breaks and sibling paragraphs. Each block from 12 characters on ("Award-winning support") is read once.
    • The model finds what a reader would want a source for:
    • Each claim must be in the block's own words or it's dropped, five a block at most. The prompt says the paragraphs are the page's text, not instructions.
    • It answers { paragraphs: [{ text, claims: [{ text, kind }] }], pending, limited }, only blocks with claims. The editor finds each claim's block by its words.
  • skip is the block the person is writing in (as saved): it isn't read until they leave it.

What it keeps (00266 content_quality_claims)

  • One row per draft and block, keyed by a hash of the block's words and the reading's version. An unchanged block isn't read again, a changed one is, and a removed one's row goes at the next ask.
  • Server-only: RLS on, with no anon/authenticated grants.

Cost

  • A call takes 12 blocks or 6,000 characters, whichever comes first. An ask makes 2 calls at most, one after the other. A later call that fails keeps the earlier one's reads.
  • Calls draw on the AI budget as quality, with a hold of $0.004.
  • An answer billed that missed the schema is charged, and its blocks are kept as read with no claims, so they aren't sent and charged again on every save. A timeout keeps its hold.
  • One day's cap for both of Quality's AI reads: QUALITY_CALLS_PER_DAY = 60 per draft (content-quality-calls.js), shared with feat(content): read the kind of a source's site the rules can't place with AI (CE-046, part b) #1451's source kinds. Claims stop 10 short (LEFT_FOR_SOURCES), so they never starve the source reads. This changes feat(content): read the kind of a source's site the rules can't place with AI (CE-046, part b) #1451's "30 reads a draft a day": source reads now share 60 with claims.
  • The route has its own rate limit (claimsLimiter, 30 per 15 minutes per user), apart from the source kinds' bucket.

Logs carry no page words (error name and message only).

Related issue

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

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 00266 and set CONTENT_QUALITY_PHASE=2 in server/.env.
  2. Save a draft with "Ledgerly is the fastest forecasting app." and "Every plan is SOC 2 certified."
  3. POST /api/content-pages/claims { draftId } as a member. It answers both blocks with their claims (superlative, fact).
  4. Ask again: no model call (prompt_improve_usage gets no new quality row). Change one block and ask: only that block is read.
  5. Pass that block as skip: it waits.

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

00266 is applied on the dev DB and schema.sql is rebuilt. Server: npm run format, npm run lint and npm test pass (2,425 tests). A pre-push review's points are in (blocks as the editor's, spacing, short items, size-based calls, missed answers kept as read, the cap left to sources, own limiter, version in the hash, no page words in logs).

🤖 Generated with Claude Code

cihan-gyk and others added 8 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]>
@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. Three small follow-ups:

  1. An answer that misses the schema marks its paragraphs as read until their words change (low).

    • server/src/lib/content-quality-claims.js:169-172 keeps each paragraph of that call (up to 12) with no claims, and they aren't read again.
    • A schema miss also covers an answer cut short or blocked by the provider's safety filter. Claim-heavy pages (health, finance) are the likeliest to hit that filter.
    • Fix: mark those rows (e.g. a missed column in 00266, which isn't on prod yet) and read them again after a day, as #1451 does for an unknown site.
  2. A claim has to match the paragraph character for character (low).

    • keptClaims (:135) drops a claim the model copies with a straight apostrophe or quote where the page has a curly one (world’s fastest), or with another dash.
    • Fix: compare folded copies of both (’ ‘ → ', “ ” → ", – — → -). Each is a one-for-one character swap, so positions line up, and you can keep the paragraph's own span.
    • A test with a curly apostrophe would pin it.
  3. The route's error log can carry the paragraphs (low).

    • server/src/routes/content-pages.js:237 logs the whole err.
    • A provider error from the first call (APICallError, or a RetryError wrapping them) has requestBodyValues, which is the prompt with the page's words. pino's error serializer writes it out.
    • Fix: log { err: { name, message } } here, as the module does, so "Logs carry no page words" holds.
    • The other AI routes log the same way. A scrub in the logger's err serializer would cover them all.

What I checked and found fine:

  • 00266:
    • RLS is on, with no policy and no anon or authenticated grants.
    • Drafts cascade to it, and the primary key serves both the read and the prune.
    • It leaves the AI-usage feature check as 00264 sets it.
  • Prod dry run, 00264 then 00266:
    • Both apply cleanly.
    • A member can't read either new table, and can't write to the claims table.
    • The host, kind, hash and claims checks refuse bad values.
    • reserve_ai_usage takes quality.
    • Deleting a draft removes its claims.
  • The route:
    • It's off below phase 2.
    • It checks the draft's brand before reading.
    • It has its own per-user limiter.
  • Cost:
    • Each call is held ($0.004) and charged on the workspace's AI budget as quality.
    • A timeout keeps its hold, and a later call's failure keeps the reads before it.
    • Limits: 50 claim calls a draft a day (10 left to sources), 2 calls an ask, and 12 paragraphs or 6,000 characters a call.
  • Prompt injection:
    • The model sees only the numbered paragraphs.
    • Its answer can only set that draft's rows, with words already in the paragraph and one of the four kinds.
    • Nothing reaches another draft or workspace.
  • Odd answers: an unknown n or kind is dropped, and a paragraph answered twice is merged.
  • Blocks: the server reads the same blocks as the editor's trustBlocks, with the same spacing.

00266 dry-runs cleanly on prod after 00264. Apply 00264 first, then 00266, before the server runs with CONTENT_QUALITY_PHASE=2.

cihan-gyk and others added 6 commits October 24, 2026 17:01
… 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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks. All three are in (710db97 + f5c90ea). #1451's per-site calls are merged in (883ee03, a137aef).

  1. Missed answers
    • 00266 has a missed column (default false). It is edited in place, since 00266 isn't on prod; on the dev DB it was added with alter table … add column if not exists.
    • An answer that misses the schema keeps its paragraphs as read with no claims, marked missed. After MISSED_DAYS (1) they are read again.
    • On that re-read, each one goes in a call of its own, after the paragraphs not read yet. A paragraph the provider's filter blocks then stays missed alone, and no longer keeps its 11 neighbours unread every day.
  2. Folded matching
    • keptClaims compares folded copies. Folded: ’ ‘ ‚ ′ → ', “ ” „ ″ → ", and ‐ ‑ ‒ – — ― − → -. Each swap is one character for one, and the kept span is the paragraph's own.
    • An ellipsis isn't one for one, so it is left as it is.
    • Tests cover both directions (curly in the page, straight from the model, and the reverse), the minus sign, and two copies that differ only by their quotes kept as one claim.
  3. The route's error log has the error's name, message and stack, not the provider's request.
    • A route test rejects with an APICallError-like error carrying requestBodyValues, and checks that the page's words don't reach the log.
    • A scrub in the logger's err serializer would cover the other AI routes too. I can send that as a separate PR if you'd like.

Cap, in step with #1451:

  • QUALITY_CALLS_PER_DAY = 90, and LEFT_FOR_SOURCES = SOURCE_READS_PER_DAY (40).
  • The claims make 50 calls a day at most.
  • The sources get at least 40, and may take what the claims haven't used.

Server: npm run format, npm run lint and npm test pass (2,434 tests). schema.sql is rebuilt.

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

All three items done. The three merges (two of #1451, one of main) change nothing beyond their conflict resolutions.

  1. Missed answers:
    • 00266 has missed.
    • A missed paragraph is read again after MISSED_DAYS, in a call of its own, after the new paragraphs (server/src/lib/content-quality-claims.js:286-308, batchesOf at :222-243).
  2. Folded matching:
    • fold swaps one character for one, and the kept span is the paragraph's own (:137-162).
    • Tests cover both directions.
  3. Logs: the route logs the error's name, message and stack only (server/src/routes/content-pages.js:241-246), and a test checks this.

The cap matches #1451. The claims stop once the draft has made 50 Quality calls that day, and the sources stop at 90, so the sources always have 40 left.

Looks good.

00266, now with missed, dry-runs cleanly on prod after 00264. Apply 00264, then 00266, before the server runs with CONTENT_QUALITY_PHASE=2.

@gkhngyk
gkhngyk merged commit a215c2f into ansvisor:main Oct 24, 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