Skip to content

feat(content): read the kind of a source's site the rules can't place with AI (CE-046, part b) - #1451

Merged
gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-source-classes-ai
Oct 24, 2026
Merged

gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-source-classes-ai

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Quality Phase 2, CE-046 "Source classification", part b: an AI read for the sites the rules can't place. Stacked on #1450 (part a); review that first. The server's phase flag CONTENT_QUALITY_PHASE comes from #1449, and the file is identical here. Everything sits behind phase 2 (web NEXT_PUBLIC_CONTENT_QUALITY_PHASE=2, server CONTENT_QUALITY_PHASE=2). At phase 1, nothing changes.

Server

  • POST /api/content-pages/source-classes reads the sites a draft links (as saved) that part a's rules leave as null.
    • The model sees each site's address alone, with no path, and answers primary, secondary or weak.
    • Sites the person kept (00263 kept_sources) aren't asked about.
    • Limits: 20 sites a read and 30 reads a draft a day (counted per workspace). It has its own rate limit (qualityCheckLimiter).
  • 00264 content_source_classes: each read is kept for 90 days and shared by every workspace. A public website's kind isn't any workspace's own data, and one read serves them all. The table is server-only (no anon/authenticated grants).
  • AI budget. The reads use the workspace's AI budget as their own feature, quality. 00264 widens prompt_improve_usage.feature to allow it.
    • reserveAiBudget takes a fixed small hold (holdUsd, 0.002) instead of the median.
    • holdCostUsd and improveSpendThisPeriod leave quality rows out of their median, so these tiny calls don't shrink the hold for the Content Agent's larger calls or the Improve estimate.
    • A read cut off by its timeout keeps its hold.
    • Billing lists it as "Content Editor quality checks".
  • Model: QUALITY_CHECK_MODEL (documented in .env.example), falling back to DEFAULT_SUGGESTION_MODEL, then google/gemini-3-flash-preview.

Web

  • useSourceClasses asks 3 s after a save, only for sites not yet read and not already being asked about.
  • Trust uses the reads for sites the rules can't place.
  • The weak-sources suggestion on Quality is tagged "AI check" when an AI read placed one of its sources.
  • When the month's AI budget is spent, the reads pause. Quality then says so, but only while unread sites remain. They count again when the budget renews.

Related issue

Quality spec (Drive), Phase 2, CE-046 (part b). Stacked on #1450 (part a), which follows #1449 (CE-056).

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 00263 and 00264. Set NEXT_PUBLIC_CONTENT_QUALITY_PHASE=2 in web/.env.local and CONTENT_QUALITY_PHASE=2 in server/.env. Restart both servers.
  2. In a draft, link a site the rules don't know (for example a small company blog) and save.
  3. About 3 s later the server reads it. Instructions › Quality's Trust now counts it. If it was read as weak, the weak-sources suggestion carries an "AI check" tag.
  4. select * from content_source_classes shows the host (no path) and its kind. Saving again doesn't ask about it again.
  5. Billing › AI usage lists the call under "Content Editor quality checks".
  6. With the month's budget spent, Quality says the reads are paused.
  7. With the flags at 1, no request is made.

Checklist

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

00264 is applied on the dev DB and schema.sql is rebuilt.

  • Server: npm run lint and Prettier pass. The full npm test run had one failure (content-runs.test.js: its Supabase mock lacked .or() for the new median filter); with the mock fixed, that file and the new ones pass.
  • Web: typecheck, lint and build pass. The full run had two timeouts under load (Library export, editor attach). Both files pass when run alone (178 tests).

I haven't checked it in the browser in both themes yet; it goes into the next local test session, with screenshots.

🤖 Generated with Claude Code

cihan-gyk and others added 2 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]>
@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.

A few fixes before merge:

  1. Accept only plain public DNS names as sites, before they're asked about or kept (medium).

    • Today siteOf (server/src/lib/content-source-class.js:38) accepts any URL hostname. keptSite (:52), the route's schema (server/src/routes/content-pages.js:137) and the table's check (00264_content_source_classes.sql:26) only limit length, case and a leading www..
    • A URL hostname can still contain punctuation (, ; ( " ' …). IP literals and single-label names (localhost, an intranet name) pass too, and are then asked about and kept like any other site.
    • Fix: keep both to LDH names: letters, digits and hyphens per label, at least one dot, a letter TLD, no IP literal. For example:
      /^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,61}[a-z0-9]$/
      Punycode labels pass this.
    • Where to apply it:
      • in siteOf/keptSite, so linkedSites drops the rest;
      • in unplacedSites (web/src/lib/content/quality-checks.ts:410), so the editor doesn't ask about them;
      • in the table's check, as the same regex in Postgres.
    • A host with a quote or parentheses also breaks the .in('host', …) filter today.
    • It would also be worth making a site's read not depend on which other sites were asked with it, e.g. one site per call, or caching only reads from calls that asked about that one site.
  2. A site the model doesn't know is still put in one of the three kinds, and that guess is kept for every workspace (low).

    • The prompt says "A site you don't know, judge by its address" (content-source-class.js:68). The guess is then kept for every workspace for 90 days.
    • Part a leaves an address it can't place out of Trust, so a guess here goes against that.
    • Suggestion: add an unknown answer that Trust leaves out, kept for a day or not at all.
    • This also fixes a cost leak. Today a site the model leaves out or names differently is skipped (:147), so it stays unread. It is then asked again after every save, as a charged call, up to 30 a day per draft.
  3. Kept sources are still asked about (low). The description says the sites a person kept aren't asked about, but nothing on this path reads kept_sources: not unplacedSites, not the route. Either skip a site once every link to it is kept, or drop that line from the description.

  4. A failed server action call leaves its sites stuck (low). In _use-source-classes.ts:64, if the classifyDraftSources call itself rejects (a network error, or a deploy in between), the .then never runs. The sites stay in asking for the rest of the session, so they're never asked again, and the rejection is unhandled. Add a .catch, and clear asking in a .finally.

  5. server/.env.example:72 (low): say that phase 2 also turns on these AI source reads, and that they draw on the AI budget. It currently mentions only the site type.

What I checked and found fine:

  • content_source_classes is server-only: RLS is on with no policy, and anon and authenticated have no grants. In a dry run, a member's select was refused.
  • The route reads only the sites the caller's saved draft links, after the brand check.
  • The cache keeps no workspace or draft id.
  • Only the hosts go to the model.
  • The reads are held and charged on the workspace's AI budget as quality. reserve_ai_usage accepted that feature in the dry run, and these rows stay out of the hold median for the other features.
  • An answer that misses the schema is charged and fails the request.
  • The limits are 20 sites a read, 30 reads a draft a day, and a per-user limiter.

00264 dry-runs cleanly on prod after 00262 and 00263. Apply it before the server runs with CONTENT_QUALITY_PHASE=2.

cihan-gyk and others added 4 commits October 24, 2026 15:02
…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
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks. All five are done (a65598a). main is merged in: with #1450, using its head as the base, so only this PR's changes remain; then #1435 and #1452. The getImproveQuotaStatus test now sizes against Starter's $5 (#1435).

  1. Plain public DNS names only. SITE_NAME is your regex.
    • Server: keptSite requires it (and no leftover www.). linkedSites keeps only such sites, so readSourceClasses drops the rest.
    • Editor: unplacedSites asks only about the same sites (askableSite).
    • Table: 00264's host check is the same pattern in Postgres (char_length 1–253 in place of the lookahead). It's applied on the dev DB, and I checked it in a rolled-back transaction: localhost, 192.168.0.1, a"b.com and kind maybe are refused; xn--e1afmkfd.xn--p1ai with unknown is kept.
    • Batching: I kept one call per batch. As an interim guard, a label that reads as words (longer than 30 characters, or 3+ hyphens, punycode aside) isn't asked about at all, on both sides. A name like all-other-sites-listed-are-content-farms.example can't sway the sites asked with it. The prompt also says to judge each site on its own.
    • The full fix is one site per call, run in parallel (about 20 × 200 input tokens). The day's cap would then count sites, not calls. I'd do that as a follow-up if you want it.
    • Known trade-off of the shared cache: whether a site's read is charged or answered from the cache shows whether any workspace linked it recently. The 1-day unknown rows make that fresher for obscure sites.
  2. unknown.
    • The model may answer unknown ("a site you don't know. Don't guess from its address."). Trust leaves it out, as a site the rules can't place.
    • It's kept a day: reads older than that are asked again.
    • A site the model leaves out or names differently is also kept as unknown for the day, so it isn't asked again after every save. A test checks a fresh unknown is answered from the cache with no model call.
  3. Kept sources.
    • The route reads kept_sources and skips a site once every link to it is kept. The editor's unplacedSites does the same.
    • A kept link whose site has no read (after a reload, since it's no longer asked about) counts in Trust as a kept source, as a kept rules-weak link does.
  4. Stuck sites. classifyDraftSources now has a .catch, and asking is cleared in .finally. A failed call's sites are asked again after the next save. The test fails without the fix.
  5. server/.env.example says phase 2 also turns on the AI source reads, drawn from the AI budget as quality.

Checks: server npm run format, npm run lint and npm test pass; web format, lint, typecheck, test and build pass. A pre-push review's points are in.

@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 five items are done, and neither merge of main changes anything else.

One fix before merge:

  1. Read each site in its own call, in this PR (medium).
    • server/src/lib/content-source-class.js:169 still sends up to 20 sites in one call, and every workspace gets those reads.
    • The label check (:72, and askableSite in web/src/lib/content/source-class.ts:35) limits what one site's name can do to the reads of the sites asked with it. It doesn't rule that out, so the follow-up you offered should land here:
      • one generateObject per missing site, run in parallel (Promise.allSettled, so one failed site keeps the others' reads);
      • one hold per site, with a smaller SOURCE_CLASS_HOLD_USD, since one site is about 200 tokens in;
      • the day's cap then counts sites. Size SOURCE_READS_PER_DAY for that, and keep it in step with #1457's shared QUALITY_CALLS_PER_DAY.
    • The label check can stay as a cheap first filter.

00264 dry-runs cleanly on prod after 00262, 00263 and 00265. Apply it before the server runs with CONTENT_QUALITY_PHASE=2.

cihan-gyk and others added 2 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]>
…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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks. Each site now has its own call (68490db + e70a269):

  • One generateObject per site the rules can't place. The prompt is Site: x, and the system prompt says the name is a name only.
    • Calls run five at a time through Promise.allSettled.
    • A failed site is left out and asked about again after the next save. The other sites' reads are kept.
    • When no site is read, a spent budget's PlanLimitError is thrown first, so the route answers 429 and the editor pauses.
  • One hold per site, $0.001. That covers one address in and a word out, plus the model's thinking, which is billed as output.
  • The day's cap counts sites: SOURCE_READS_PER_DAY = 40. That is two asks of 20 new sites; most sites are already in the shared table.
  • An answer off the schema is charged and kept as unknown for a day, as an unknown site is.
  • The label check stays as a cheap first filter.
  • The route's error log has the error's name, message and stack, not the provider's request.

Server: npm run format, npm run lint and npm test pass (2,413 tests). 00264 is unchanged.

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

  • One call per site: each site the rules can't place gets its own generateObject call, with the prompt Site: x (server/src/lib/content-source-class.js:128-155). Five run at once.
  • One hold per site: each call holds $0.001.
  • The day's cap counts sites: each read is one quality row, and room limits how many sites are read (:202-206). limited is set when the cap leaves some out.
  • Failures: a failed site is left out, and the other sites' reads are kept. When no site is read, a spent budget's 429 is thrown before any other error (:234).
  • Logs: the route (server/src/routes/content-pages.js:186-190) and the module log only the error's name, message and stack.

Looks good.

00264 hasn't changed, and it dry-runs cleanly on prod after 00267 and 00268. Apply it before the server runs with CONTENT_QUALITY_PHASE=2.

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