Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(content): the third review's open items — bounded reads (00226), My rules per site type (00227) (#1316)
Cihan58007fc37fa5
diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.jsindex 503d6054b..688a5477f 100644--- a/server/src/lib/content-agent.js+++ b/server/src/lib/content-agent.js@@ -5,6 +5,7 @@ import { z } from 'zod';import supabaseAdmin from '../config/supabase.js';import { resolveModel } from './ai-provider.js';import { contentBriefModelId, recordUsage } from './content-brief.js';+import { pageTypeFormat } from './content-page-types.js';import { getLanguageName } from './languages.js';import { enforceImproveQuota } from './plan-guard.js';@@ -241,7 +242,7 @@ Industry: ${brand?.industry || 'Not specified'}Description: ${brand?.description || 'N/A'}Page: ${draft.title || 'Untitled'}-Page type: ${draft.page_type || 'not set'}+Page type: ${pageTypeFormat(draft.page_type)}Rules the page doesn't meet yet (key: rule — what it asks and how the page reads now):${rules.join('\n')}diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.jsindex d835439d3..95c94299f 100644--- a/server/src/lib/content-agent.test.js+++ b/server/src/lib/content-agent.test.js@@ -121,6 +121,7 @@ describe('the page as the agent reads it', () => {expect(prompt).toContain('- dated: A dated data point — missing');expect(prompt).toContain('- Say “we” for the brand');expect(prompt).toContain('Write in English.');+ expect(prompt).toContain('Page type: How-to guide: numbered steps');expect(prompt).not.toContain('The page goes on');});diff --git a/server/src/lib/content-brief.js b/server/src/lib/content-brief.jsindex 0d102c68f..176d2da82 100644--- a/server/src/lib/content-brief.js+++ b/server/src/lib/content-brief.js@@ -2,6 +2,7 @@ import { NoObjectGeneratedError, generateObject } from 'ai';import { z } from 'zod';import supabaseAdmin from '../config/supabase.js';import { resolveModel } from './ai-provider.js';+import { pageTypeFormat } from './content-page-types.js';import { generationCostUsd } from './improve-cost.js';import { getLanguageName } from './languages.js';import { logger } from './logger.js';@@ -256,7 +257,7 @@ Industry: ${brand?.industry || 'Not specified'}Description: ${brand?.description || 'N/A'}Page: ${draft.title || 'Untitled'}-Page type: ${draft.page_type || 'not set'}+Page type: ${pageTypeFormat(draft.page_type)}${draft.method === 'optimize' && draft.source_url ? `Existing page to improve: ${draft.source_url}` : 'A new page.'}Length: about ${sources.length} characters.diff --git a/server/src/lib/content-brief.test.js b/server/src/lib/content-brief.test.jsindex 619a6e4b0..e75bea29e 100644--- a/server/src/lib/content-brief.test.js+++ b/server/src/lib/content-brief.test.js@@ -310,6 +310,7 @@ describe('the brief prompt', () => {sources: sources({ fanouts: [], pages: [], queries: ['burr grinder'], headings: [] }),});expect(prompt).toContain('A new page.');+ expect(prompt).toContain('Page type: not set');expect(prompt).toContain('Prompts the page must answer:\n- none');expect(prompt).toContain('- burr grinder');expect(prompt).not.toContain('headings now');diff --git a/server/src/lib/content-page-types.js b/server/src/lib/content-page-types.jsnew file mode 100644index 000000000..57c732334--- /dev/null+++ b/server/src/lib/content-page-types.js@@ -0,0 +1,36 @@+/**+ * The Content Editor's page types (web/src/lib/content/drafts.ts, PAGE_TYPES)+ * as the person picks them (web/messages/en.json, contentEditor.pageTypes),+ * each with the format it asks for, so the writer, the brief and the Content+ * Agent write the page the person chose rather than read a key such as+ * "solution_page".+ */+export const PAGE_TYPE_FORMATS = {+ feature_page: 'Feature page: one capability, what it does and who it is for',+ solution_page: 'Use case page: how the product serves one use case, industry or team',+ comparison: 'Comparison: the options side by side, with a verdict',+ integration: 'Integration: what connecting one other tool does, and how to set it up',+ pricing: 'Pricing: the plans, their prices and what each includes',+ guide: 'How-to guide: numbered steps to get one thing done',+ blog_post: 'Blog post',+ docs_faq: 'Docs / FAQ: direct answers to questions about the product',+ product_page: 'Product page: one product, its specs, its uses and reasons to buy it',+ category_page: 'Category page: a range of products and how to choose among them',+ buying_guide: 'Buying guide: how to choose, the criteria and the picks',+ best_of: 'Best-of list: ranked picks, with the reason for each',+ policy: 'Policy: shipping, returns or terms, stated plainly',+ destination_page: 'Destination page: a place, what to do there and when to go',+ itinerary: 'Itinerary: a day-by-day plan',+ help_faq: 'Help & FAQ: direct answers to customer questions',+ definition:+ 'Definition page: the term defined in the first sentence, then explained with examples',+ alternatives: 'Alternatives page: the options to one product, compared',+ collection_page: 'Collection page: a themed selection of products',+ sizing_guide: 'Sizing guide: how to find the right size, with a size chart',+};++/** A draft's page type as the model reads it: its name and format, or "not set". */+export function pageTypeFormat(pageType) {+ if (!pageType) return 'not set';+ return Object.hasOwn(PAGE_TYPE_FORMATS, pageType) ? PAGE_TYPE_FORMATS[pageType] : pageType;+}diff --git a/server/src/lib/content-page-types.test.js b/server/src/lib/content-page-types.test.jsnew file mode 100644index 000000000..ec761b99e--- /dev/null+++ b/server/src/lib/content-page-types.test.js@@ -0,0 +1,36 @@+import { readFileSync } from 'node:fs';+import { describe, expect, it } from 'vitest';+import { PAGE_TYPE_FORMATS, pageTypeFormat } from './content-page-types.js';++describe('pageTypeFormat', () => {+ it('names a page type as the person picks it, with its format', () => {+ expect(pageTypeFormat('solution_page')).toBe(+ 'Use case page: how the product serves one use case, industry or team',+ );+ expect(pageTypeFormat('guide')).toMatch(/^How-to guide: numbered steps/);+ });++ it('says so when there is none, and passes on a type it doesn’t know', () => {+ expect(pageTypeFormat(null)).toBe('not set');+ expect(pageTypeFormat('')).toBe('not set');+ expect(pageTypeFormat('constructor')).toBe('constructor');+ expect(pageTypeFormat('webinar_page')).toBe('webinar_page');+ });++ it('covers every page type the editor offers, under the name the person sees', () => {+ const drafts = readFileSync(+ new URL('../../../web/src/lib/content/drafts.ts', import.meta.url),+ 'utf8',+ );+ const list = drafts.match(/export const PAGE_TYPES = \[([\s\S]*?)\] as const/)[1];+ const types = [...list.matchAll(/'([a-z_]+)'/g)].map((m) => m[1]);+ expect(Object.keys(PAGE_TYPE_FORMATS).sort()).toEqual([...types].sort());++ const messages = JSON.parse(+ readFileSync(new URL('../../../web/messages/en.json', import.meta.url), 'utf8'),+ );+ for (const [type, label] of Object.entries(messages.contentEditor.pageTypes)) {+ expect(PAGE_TYPE_FORMATS[type].split(':')[0]).toBe(label);+ }+ });+});diff --git a/server/src/lib/content-writer.js b/server/src/lib/content-writer.jsindex c54d328b3..ff94d1bb4 100644--- a/server/src/lib/content-writer.js+++ b/server/src/lib/content-writer.js@@ -13,6 +13,7 @@ import {suggestionsSchema,} from './content-agent.js';import { contentBriefModelId, rebuildSources, recordUsage } from './content-brief.js';+import { pageTypeFormat } from './content-page-types.js';import { getLanguageName } from './languages.js';import { logger } from './logger.js';import { enforceImproveQuota } from './plan-guard.js';@@ -189,7 +190,7 @@ export function buildWritePrompt({Industry: ${brand?.industry || 'Not specified'}Description: ${brand?.description || 'N/A'}-Page type: ${draft.page_type || 'not set'}+Page type: ${pageTypeFormat(draft.page_type)}Length: about ${sources.length} characters.Brief:diff --git a/server/src/lib/content-writer.test.js b/server/src/lib/content-writer.test.jsindex 284422370..d2588a055 100644--- a/server/src/lib/content-writer.test.js+++ b/server/src/lib/content-writer.test.js@@ -211,6 +211,7 @@ describe('startDraftWriting', () => {expect(prompt).toContain('- ratio?');expect(prompt).toContain('- "pour over ratio"');expect(prompt).toContain('- faq: FAQ with 5 questions — aim: 5');+ expect(prompt).toContain('Page type: How-to guide: numbered steps to get one thing done');});it('refuses another brand’s draft, one past Draft, or one the agent is on — before charging', async () => {diff --git a/supabase/migrations/00226_content_reads_bounded.sql b/supabase/migrations/00226_content_reads_bounded.sqlnew file mode 100644index 000000000..9e8710429--- /dev/null+++ b/supabase/migrations/00226_content_reads_bounded.sql@@ -0,0 +1,740 @@+-- ============================================================================+-- 00226: the Content Editor's reads, bounded by their window+-- ============================================================================+-- Four content reads, redefined after they were timed on production (the+-- reviews of #1287, #1292, #1308 and #1309). Each returns what it did; each+-- reads less to get there.+--+-- * content_page_overlap (00207): the window's answers come from+-- idx_prompt_results_brand_created_cover alone (an index-only scan), and+-- the prompts are picked where the citations meet them. A prompt filter+-- on the answers made the planner combine the prompt_id index with the+-- brand's and read about 16k heap blocks a call.+-- * content_draft_sources (00211) read every answer each source prompt+-- ever had, through the prompt_id index, and the citations' primary key+-- once per answer (8 s cold for a 92-prompt draft):+-- - the window's answers come from the cover index alone, for a draft+-- with prompts as well as one with a page, and the source prompts+-- are picked from them after;+-- - each prompt's answers are counted on that index; those naming the+-- brand come from the Mentions rollup (mentions_prompt_daily, 00157)+-- for the window's whole UTC days, and from the answer rows of its+-- first, partial day only;+-- - the fan-outs and the cited pages are read from each prompt's+-- latest 30 answers in the window, so "searched N×" and a page's+-- citations count those: they are the only answer rows fetched whole+-- (search_queries), and their citations a fifth of the window's.+-- * content_plan_pages (00220, as 00221 redefined it): the overlaps' pairs+-- take their window from the current answers alone. A date bound on the+-- citations too is estimated from prompt_result_citations' last analyze+-- (no index leads with created_at), at about one row for recent days,+-- and that plan rescanned the own addresses once per citation (15–19 s+-- at 7 days on the largest brand).+-- * content_plan_insights (00221) joined every address the brand had cited+-- in the window to citation_urls before keeping its own and the top+-- 1,000. Now the own ids come from citation_urls' domain index first, the+-- rows are picked on the rollup's url ids, and only those picked are read+-- from citation_urls. The own list keeps the 5,000 cited most (a brand on+-- a large platform's domain has more), and each call is planned for its+-- own values (plan_cache_mode), as content_plan_pages is.+--+-- The index-only scans stay off the heap once prompt_results and+-- prompt_result_citations are vacuumed daily (00224), and that migration's+-- daily analyze keeps the planner's estimate of a recent window current.+-- Grants and comments carry over from 00207–00221 (create or replace); the+-- insights' comment is restated for its cap.++create or replace function public.content_page_overlap(+ p_brand_id uuid,+ p_prompt_ids uuid[],+ p_since timestamptz default now() - interval '30 days'+)+returns table (+ page text,+ title text,+ prompts_in_common integer,+ citations bigint,+ gsc_position numeric+)+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return;+ end if;+ if coalesce(cardinality(p_prompt_ids), 0) = 0 then+ return;+ end if;++ return query+ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ -- The window's answers, from the (brand_id, created_at) cover index+ -- alone. The prompts are picked in `cited`: a prompt filter here makes the+ -- planner combine the prompt_id index with the brand's and read the heap.+ answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ cited as (+ select ou.page_key, ou.url, ou.title, a.prompt_id+ from public.prompt_result_citations prc+ join answers a on a.id = prc.prompt_result_id+ join own_urls ou on ou.id = prc.url_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ and a.prompt_id = any(p_prompt_ids)+ ),+ pages as (+ select+ c.page_key,+ -- The address cited most often stands for the page.+ mode() within group (order by c.url) as page,+ max(c.title) as title,+ count(distinct c.prompt_id)::integer as prompts_in_common,+ count(*)::bigint as citations+ from cited c+ group by c.page_key+ ),+ google as (+ select+ g.page_key,+ round((sum(g.position * g.impressions) / nullif(sum(g.impressions), 0))::numeric, 1)+ as position+ from public.gsc_page_stats g+ where g.brand_id = p_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ and g.page_key in (select p.page_key from pages p)+ group by g.page_key+ )+ select p.page, p.title, p.prompts_in_common, p.citations, gl.position+ from pages p+ left join google gl on gl.page_key = p.page_key+ order by p.prompts_in_common desc, p.citations desc, p.page+ limit 10;+end;+$$;++create or replace function public.content_draft_sources(+ p_draft_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_brand_id uuid;+ v_prompt_ids uuid[];+ v_page_key text;+ v_host text;+ v_since timestamptz;+ -- The window's first whole UTC day.+ v_first_day date;+ v_result jsonb;+begin+ select d.brand_id, d.prompt_ids, lower(public.normalize_page_path(d.source_url)),+ -- Lower-cased first: a scheme may be written HTTPS://.+ regexp_replace(substring(lower(d.source_url) from '^https?://([^/:?#]+)'), '^www\.', '')+ into v_brand_id, v_prompt_ids, v_page_key, v_host+ from public.content_drafts d+ where d.id = p_draft_id;++ if v_brand_id is null or (+ not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = v_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role'+ ) then+ return null;+ end if;+ if p_days is null or p_days < 1 or p_days > 90 then+ raise exception 'content_draft_sources: 1 to 90 days are needed' using errcode = '22023';+ end if;++ v_since := now() - make_interval(days => p_days);+ v_first_day := (v_since at time zone 'utc')::date + 1;+ -- A page on a site that isn't the brand's is no page of the brand's.+ if v_host is null or not exists (+ select 1 from public.brand_domains bd+ where bd.brand_id = v_brand_id+ and lower(regexp_replace(bd.domain, '^www\.', '')) = v_host+ ) then+ v_page_key := null;+ end if;++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = v_brand_id+ ),+ -- The page's addresses: its path on its host.+ page_urls as materialized (+ select cu.id+ from public.citation_urls cu+ where v_page_key is not null+ and cu.domain = v_host+ and lower(public.normalize_page_path(cu.url)) = v_page_key+ ),+ -- The window's answers, from idx_prompt_results_brand_created_cover alone+ -- (an index-only scan). The prompts are picked after this read: a prompt+ -- filter here makes the planner combine the prompt_id index with the+ -- brand's and read every answer each prompt ever had.+ window_answers as materialized (+ select pr.id, pr.prompt_id, pr.created_at+ from public.prompt_results pr+ where (v_page_key is not null or coalesce(cardinality(v_prompt_ids), 0) > 0)+ and pr.brand_id = v_brand_id+ and pr.created_at >= v_since+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ -- The prompts whose answers cited the page the draft optimizes.+ page_prompts as (+ select distinct wa.prompt_id+ from public.prompt_result_citations prc+ join window_answers wa on wa.id = prc.prompt_result_id+ where prc.url_id in (select pu.id from page_urls pu)+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ ),+ source_prompts as (+ select p.id, p.text, p.id = any(v_prompt_ids) as in_draft,+ p.id in (select prompt_id from page_prompts) as cites_page+ from public.prompts p+ join public.prompt_sets ps on ps.id = p.prompt_set_id+ where ps.brand_id = v_brand_id+ and (p.id = any(v_prompt_ids) or p.id in (select prompt_id from page_prompts))+ ),+ -- The source prompts' answers in the window, newest first per prompt.+ answers as materialized (+ select wa.id, wa.prompt_id, wa.created_at,+ row_number() over (partition by wa.prompt_id order by wa.created_at desc) as recency+ from window_answers wa+ where wa.prompt_id in (select sp.id from source_prompts sp)+ ),+ -- The answers naming the brand: the window's whole UTC days from the+ -- Mentions rollup (mentions_prompt_daily, 00157), and its first, partial+ -- day from those answers' rows. A run still in progress joins the rollup+ -- when it completes.+ named as (+ select m.prompt_id, m.mention_answers as answers+ from public.mentions_prompt_daily m+ where m.brand_id = v_brand_id+ and m.day >= v_first_day+ and m.prompt_id in (select sp.id from source_prompts sp)+ union all+ select a.prompt_id, 1+ from answers a+ join public.prompt_results pr on pr.id = a.id+ where a.created_at < v_first_day::timestamp at time zone 'utc'+ and pr.mention_count > 0+ ),+ prompt_counts as (+ select a.prompt_id, a.answers, coalesce(n.mentioned, 0) as mentioned+ from (select prompt_id, count(*) as answers from answers group by prompt_id) a+ left join (+ select prompt_id, sum(answers) as mentioned from named group by prompt_id+ ) n on n.prompt_id = a.prompt_id+ ),+ -- The only answer rows read: each prompt's latest 30.+ sampled as (+ select pr.id, pr.platform, pr.search_queries+ from answers a+ join public.prompt_results pr on pr.id = a.id+ where a.recency <= 30+ ),+ fanout_rows as (+ select+ a.id as answer_id,+ lower(trim(regexp_replace(q.item->>'query', '\s+', ' ', 'g'))) as norm,+ trim(regexp_replace(q.item->>'query', '\s+', ' ', 'g')) as query,+ coalesce(nullif(q.item->>'source_platform', ''), a.platform) as platform+ from sampled a+ cross join lateral jsonb_array_elements(+ case when jsonb_typeof(a.search_queries) = 'array' then a.search_queries else '[]'::jsonb end+ ) as q(item)+ where coalesce(trim(q.item->>'query'), '') <> ''+ ),+ fanouts as (+ select min(query) as query, array_agg(distinct platform order by platform) as platforms,+ count(distinct answer_id) as searched+ from fanout_rows+ group by norm+ order by count(distinct answer_id) desc, min(query)+ limit 30+ ),+ -- The citations of the same sample as the fan-outs, each prompt's latest+ -- 30 answers: every answer in the window is 100k+ citations on a large+ -- brand, read one answer at a time. Grouped by address first; only the+ -- top 20 addresses are read.+ cited_ids as (+ select prc.url_id, count(*) as citations, count(distinct prc.prompt_result_id) as answers+ from answers a+ join public.prompt_result_citations prc+ on prc.prompt_result_id = a.id+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ where a.recency <= 30+ group by prc.url_id+ order by count(*) desc, count(distinct prc.prompt_result_id) desc, prc.url_id+ limit 20+ ),+ cited as (+ select cu.url, cu.title, cu.domain, ci.citations, ci.answers+ from cited_ids ci+ join public.citation_urls cu on cu.id = ci.url_id+ ),+ queries as (+ select g.query, sum(g.clicks)::bigint as clicks, sum(g.impressions)::bigint as impressions+ from public.gsc_query_stats g+ where g.brand_id = v_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ group by g.query+ order by sum(g.clicks) desc, sum(g.impressions) desc, g.query+ limit 300+ )+ select jsonb_build_object(+ 'prompts', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'id', sp.id,+ 'text', sp.text,+ 'inDraft', sp.in_draft,+ 'citesPage', sp.cites_page,+ 'answers', coalesce(pc.answers, 0),+ 'mentioned', coalesce(pc.mentioned, 0)+ ) order by sp.in_draft desc, sp.text)+ from source_prompts sp+ left join prompt_counts pc on pc.prompt_id = sp.id+ ), '[]'::jsonb),+ 'fanouts', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'query', f.query, 'platforms', to_jsonb(f.platforms), 'searched', f.searched+ ) order by f.searched desc, f.query)+ from fanouts f+ ), '[]'::jsonb),+ 'citedPages', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'url', c.url, 'title', c.title, 'domain', c.domain,+ 'citations', c.citations, 'answers', c.answers+ ) order by c.citations desc, c.answers desc, c.url)+ from cited c+ ), '[]'::jsonb),+ 'gscQueries', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'query', q.query, 'clicks', q.clicks, 'impressions', q.impressions+ ) order by q.clicks desc, q.impressions desc, q.query)+ from queries q+ ), '[]'::jsonb),+ 'brandDomains', coalesce((select jsonb_agg(domain order by domain) from own_domains), '[]'::jsonb),+ 'competitorDomains', coalesce((+ select jsonb_agg(distinct lower(regexp_replace(c.domain, '^www\.', '')))+ from public.competitors c+ where c.brand_id = v_brand_id and coalesce(c.domain, '') <> ''+ ), '[]'::jsonb)+ ) into v_result;+ return v_result;+end;+$$;++create or replace function public.content_plan_pages(+ p_brand_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+-- A cached generic plan can't see the brand's size (00207).+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_last date := (now() at time zone 'utc')::date - 1;+ v_cur_day date;+ v_prev_day date;+ v_cur timestamptz;+ v_until timestamptz;+ v_result jsonb;+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return null;+ end if;+ if p_days not in (7, 30, 90) then+ raise exception 'p_days must be 7, 30 or 90';+ end if;++ v_cur_day := v_last - p_days + 1;+ v_prev_day := v_cur_day - p_days;+ v_cur := v_cur_day::timestamp at time zone 'utc';+ v_until := (v_last + 1)::timestamp at time zone 'utc';++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ url_counts as materialized (+ select d.url_id, d.platform, d.day >= v_cur_day as is_cur, sum(d.citations)::bigint as n+ from public.citation_url_daily d+ where d.brand_id = p_brand_id+ and d.day >= v_prev_day+ and d.day <= v_last+ and d.url_id in (select id from own_urls)+ and d.platform is distinct from 'chatgpt-shopping'+ group by d.url_id, d.platform, d.day >= v_cur_day+ ),+ url_totals as (+ select+ u.page_key,+ u.url,+ u.title,+ coalesce(sum(c.n) filter (where c.is_cur), 0) as cur,+ coalesce(sum(c.n) filter (where not c.is_cur), 0) as prev+ from url_counts c+ join own_urls u on u.id = c.url_id+ group by u.page_key, u.url, u.title+ ),+ pages as materialized (+ select+ t.page_key,+ -- The address cited most often stands for the page.+ (array_agg(t.url order by t.cur + t.prev desc, t.url))[1] as page,+ max(t.title) as title,+ sum(t.cur)::bigint as cur,+ sum(t.prev)::bigint as prev+ from url_totals t+ group by t.page_key+ order by sum(t.cur + t.prev) desc, t.page_key+ limit 100+ ),+ answers as (+ select+ r.platform,+ coalesce(sum(r.answers) filter (where r.day >= v_cur_day), 0)::bigint as cur,+ coalesce(sum(r.answers) filter (where r.day < v_cur_day), 0)::bigint as prev+ from public.citation_result_daily r+ where r.brand_id = p_brand_id+ and r.day >= v_prev_day+ and r.day <= v_last+ and r.platform is distinct from 'chatgpt-shopping'+ group by r.platform+ ),+ per_platform as (+ select+ u.page_key,+ c.platform,+ coalesce(sum(c.n) filter (where c.is_cur), 0) as cur,+ coalesce(sum(c.n) filter (where not c.is_cur), 0) as prev+ from url_counts c+ join own_urls u on u.id = c.url_id+ where u.page_key in (select p.page_key from pages p)+ and c.platform is not null+ group by u.page_key, c.platform+ ),+ down as (+ -- A platform tracked in both windows, citing the page at a lower rate.+ select+ pp.page_key,+ array_agg(+ pp.platform+ order by pp.prev::numeric / a.prev - pp.cur::numeric / a.cur desc, pp.platform+ ) as platforms+ from per_platform pp+ join answers a on a.platform = pp.platform+ where a.prev > 0+ and a.cur > 0+ and pp.prev::numeric / a.prev > pp.cur::numeric / a.cur+ group by pp.page_key+ ),+ cur_answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= v_cur+ and pr.created_at < v_until+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ -- The window comes from cur_answers: a citation carries its answer's+ -- created_at. A date bound on prc too would be estimated from+ -- prompt_result_citations' last analyze (no index leads with created_at),+ -- at about one row for recent days, and that plan rescans own_urls once+ -- per citation.+ page_prompts as (+ select distinct u.page_key, a.prompt_id+ from public.prompt_result_citations prc+ join own_urls u on u.id = prc.url_id+ join cur_answers a on a.id = prc.prompt_result_id+ where prc.brand_id = p_brand_id+ and u.page_key in (select p.page_key from pages p)+ ),+ competing as (+ select a.page_key, b.page_key as other+ from page_prompts a+ join page_prompts b on b.prompt_id = a.prompt_id and b.page_key <> a.page_key+ group by a.page_key, b.page_key+ having count(*) >= 2+ ),+ page_overlaps as (+ select cm.page_key, count(*) as pages+ from competing cm+ group by cm.page_key+ ),+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_cur+ and l.created_at < v_until+ ),+ visits as (+ select lower(public.normalize_page_path(l.url)) as page_key, 1::bigint as n+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_cur+ and l.created_at < v_until+ union all+ select lower(public.normalize_page_path(g.landing_page)), g.sessions::bigint+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.sessions > 0+ -- A session without a landing page would read as the home page.+ and g.landing_page <> ''+ and g.date >= v_cur_day+ and g.date <= v_last+ and not exists (select 1 from snippet_days s where s.day = g.date)+ ),+ page_visits as (+ select v.page_key, sum(v.n) as visits+ from visits v+ where v.page_key in (select p.page_key from pages p)+ group by v.page_key+ )+ select jsonb_build_object(+ 'days', p_days,+ -- The earlier window's first day, and the brand's first tracked day: a+ -- window before it is only partly tracked, or not at all.+ 'prevFrom', v_prev_day,+ 'trackedSince', (+ select min(r.day) from public.citation_result_daily r where r.brand_id = p_brand_id+ ),+ 'answers', (+ select jsonb_build_object(+ 'cur', coalesce(sum(a.cur), 0),+ 'prev', coalesce(sum(a.prev), 0)+ )+ from answers a+ ),+ 'pages', coalesce((+ select jsonb_agg(+ jsonb_build_object(+ 'page', p.page,+ 'title', p.title,+ 'citations', jsonb_build_object('cur', p.cur, 'prev', p.prev),+ 'platformsDown', coalesce(to_jsonb(d.platforms[1:3]), '[]'::jsonb),+ 'aiVisits', coalesce(pv.visits, 0),+ 'overlaps', coalesce(o.pages, 0)+ )+ order by p.cur + p.prev desc, p.page+ )+ from pages p+ left join down d on d.page_key = p.page_key+ left join page_overlaps o on o.page_key = p.page_key+ left join page_visits pv on pv.page_key = p.page_key+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++create or replace function public.content_plan_insights(+ p_brand_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+-- A cached generic plan can't see the brand's size (00207).+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ -- Whole days to yesterday, as the Plan's pages (00220).+ v_last date := (now() at time zone 'utc')::date - 1;+ v_day date;+ v_from timestamptz;+ v_until timestamptz;+ v_result jsonb;+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return null;+ end if;+ if p_days not in (7, 30, 90) then+ raise exception 'p_days must be 7, 30 or 90';+ end if;++ v_day := v_last - p_days + 1;+ v_from := v_day::timestamp at time zone 'utc';+ v_until := (v_last + 1)::timestamp at time zone 'utc';++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ -- The brand's own addresses, through citation_urls' domain index, as+ -- content_plan_pages' own_urls (00220).+ own_ids as materialized (+ select cu.id+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ cited as materialized (+ select d.url_id, sum(d.citations)::bigint as citations+ from public.citation_url_daily d+ where d.brand_id = p_brand_id+ and d.day >= v_day+ and d.day <= v_last+ and d.platform is distinct from 'chatgpt-shopping'+ group by d.url_id+ ),+ chosen as (+ (+ select c.url_id, c.citations, true as own+ from cited c+ where exists (select 1 from own_ids o where o.id = c.url_id)+ order by c.citations desc, c.url_id+ limit 5000+ )+ union all+ (+ select c.url_id, c.citations, false+ from cited c+ where not exists (select 1 from own_ids o where o.id = c.url_id)+ order by c.citations desc, c.url_id+ limit 1000+ )+ ),+ top_urls as (+ select ch.url_id, ch.citations, ch.own, cu.url, cu.title+ from chosen ch+ join public.citation_urls cu on cu.id = ch.url_id+ ),+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_from+ and l.created_at < v_until+ ),+ visits as (+ select count(*)::bigint as n+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_from+ and l.created_at < v_until+ union all+ select coalesce(sum(g.sessions), 0)::bigint+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.sessions > 0+ and g.date >= v_day+ and g.date <= v_last+ and not exists (select 1 from snippet_days s where s.day = g.date)+ ),+ analytics as (+ select+ coalesce(sum(g.key_events), 0)::bigint as key_events,+ coalesce(sum(g.transactions), 0)::bigint as transactions,+ coalesce(sum(g.purchase_revenue), 0)::numeric as revenue+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.date >= v_day+ and g.date <= v_last+ )+ select jsonb_build_object(+ 'days', p_days,+ 'totalCitations', (select coalesce(sum(c.citations), 0) from cited c),+ 'citations', coalesce((+ select jsonb_agg(+ jsonb_build_object(+ 'url', t.url,+ 'title', t.title,+ 'own', t.own,+ 'citations', t.citations+ )+ order by t.citations desc, t.url+ )+ from top_urls t+ ), '[]'::jsonb),+ 'results', (+ select jsonb_build_object(+ 'visits', (select coalesce(sum(v.n), 0) from visits v),+ 'keyEvents', a.key_events,+ 'transactions', a.transactions,+ 'revenue', a.revenue,+ 'analytics', (+ select nullif(b.ga_property_id, '') is not null+ from public.brands b where b.id = p_brand_id+ ),+ 'purchases', a.transactions > 0 or a.revenue > 0 or exists (+ select 1 from public.ga_channel_stats c+ where c.brand_id = p_brand_id+ and c.date >= v_day+ and c.date <= v_last+ and (c.transactions > 0 or c.purchase_revenue > 0)+ )+ )+ from analytics a+ )+ )+ into v_result;++ return v_result;+end;+$$;++comment on function public.content_plan_insights(uuid, integer) is+ 'Content Editor (CE-014): the addresses AI answers cite for the brand over the last N (7, 30 or 90) whole UTC days to yesterday (from citation_url_daily): its own pages (the 5,000 cited most) and the 1,000 others cited most, with the total; and what AI-referred visitors did on the site: visits, Google Analytics AI key events, purchases and revenue. Security definer with an organization check.';diff --git a/supabase/migrations/00227_content_rule_sets_site_type.sql b/supabase/migrations/00227_content_rule_sets_site_type.sqlnew file mode 100644index 000000000..b0bc60bfa--- /dev/null+++ b/supabase/migrations/00227_content_rule_sets_site_type.sql@@ -0,0 +1,39 @@+-- ============================================================================+-- 00227: a workspace's own rules per kind of site (Content Editor, CE-009)+-- ============================================================================+-- My rules (00214) were kept per organization and page type. Since the page+-- types follow the brand's kind of site (00205), a type such as "guide" (a+-- How-to guide) is written on a B2B site, a shop and a travel site alike, so+-- a set saved for one brand applied to the same organization's brands of+-- another kind, with rules meant for that site.+--+-- * content_rule_sets gets site_type, the brand's kind of site when the set+-- was saved (b2b, ecommerce or b2c, as brands.site_type), and the key+-- becomes (organization_id, site_type, page_type).+-- * A set saved before keeps applying where it did: it is copied to each+-- kind of site. The member policies are by organization and stay as they+-- are.++alter table public.content_rule_sets+ add column if not exists site_type text not null default 'b2b'+ check (site_type in ('b2b', 'ecommerce', 'b2c'));++alter table public.content_rule_sets drop constraint if exists content_rule_sets_pkey;+alter table public.content_rule_sets+ add constraint content_rule_sets_pkey primary key (organization_id, site_type, page_type);++insert into public.content_rule_sets (+ organization_id, site_type, page_type, overrides, text_rules, updated_by, updated_at+)+select r.organization_id, s.site_type, r.page_type, r.overrides, r.text_rules, r.updated_by,+ r.updated_at+from public.content_rule_sets r+cross join (values ('ecommerce'), ('b2c')) as s(site_type)+where r.site_type = 'b2b'+on conflict do nothing;++-- Every save names its kind of site.+alter table public.content_rule_sets alter column site_type drop default;++comment on column public.content_rule_sets.site_type is+ 'Content Editor (CE-009): the kind of site the set is for (b2b, ecommerce or b2c, as brands.site_type); a brand''s drafts start from its own kind''s set.';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 12040783e..a864a2466 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -31697,6 +31697,793 @@ alter table public.prompt_result_citations set (autovacuum_analyze_scale_factor = 0.02);+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00226_content_reads_bounded.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00226: the Content Editor's reads, bounded by their window+-- ============================================================================+-- Four content reads, redefined after they were timed on production (the+-- reviews of #1287, #1292, #1308 and #1309). Each returns what it did; each+-- reads less to get there.+--+-- * content_page_overlap (00207): the window's answers come from+-- idx_prompt_results_brand_created_cover alone (an index-only scan), and+-- the prompts are picked where the citations meet them. A prompt filter+-- on the answers made the planner combine the prompt_id index with the+-- brand's and read about 16k heap blocks a call.+-- * content_draft_sources (00211) read every answer each source prompt+-- ever had, through the prompt_id index, and the citations' primary key+-- once per answer (8 s cold for a 92-prompt draft):+-- - the window's answers come from the cover index alone, for a draft+-- with prompts as well as one with a page, and the source prompts+-- are picked from them after;+-- - each prompt's answers are counted on that index; those naming the+-- brand come from the Mentions rollup (mentions_prompt_daily, 00157)+-- for the window's whole UTC days, and from the answer rows of its+-- first, partial day only;+-- - the fan-outs and the cited pages are read from each prompt's+-- latest 30 answers in the window, so "searched N×" and a page's+-- citations count those: they are the only answer rows fetched whole+-- (search_queries), and their citations a fifth of the window's.+-- * content_plan_pages (00220, as 00221 redefined it): the overlaps' pairs+-- take their window from the current answers alone. A date bound on the+-- citations too is estimated from prompt_result_citations' last analyze+-- (no index leads with created_at), at about one row for recent days,+-- and that plan rescanned the own addresses once per citation (15–19 s+-- at 7 days on the largest brand).+-- * content_plan_insights (00221) joined every address the brand had cited+-- in the window to citation_urls before keeping its own and the top+-- 1,000. Now the own ids come from citation_urls' domain index first, the+-- rows are picked on the rollup's url ids, and only those picked are read+-- from citation_urls. The own list keeps the 5,000 cited most (a brand on+-- a large platform's domain has more), and each call is planned for its+-- own values (plan_cache_mode), as content_plan_pages is.+--+-- The index-only scans stay off the heap once prompt_results and+-- prompt_result_citations are vacuumed daily (00224), and that migration's+-- daily analyze keeps the planner's estimate of a recent window current.+-- Grants and comments carry over from 00207–00221 (create or replace); the+-- insights' comment is restated for its cap.++create or replace function public.content_page_overlap(+ p_brand_id uuid,+ p_prompt_ids uuid[],+ p_since timestamptz default now() - interval '30 days'+)+returns table (+ page text,+ title text,+ prompts_in_common integer,+ citations bigint,+ gsc_position numeric+)+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return;+ end if;+ if coalesce(cardinality(p_prompt_ids), 0) = 0 then+ return;+ end if;++ return query+ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ -- The window's answers, from the (brand_id, created_at) cover index+ -- alone. The prompts are picked in `cited`: a prompt filter here makes the+ -- planner combine the prompt_id index with the brand's and read the heap.+ answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ cited as (+ select ou.page_key, ou.url, ou.title, a.prompt_id+ from public.prompt_result_citations prc+ join answers a on a.id = prc.prompt_result_id+ join own_urls ou on ou.id = prc.url_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ and a.prompt_id = any(p_prompt_ids)+ ),+ pages as (+ select+ c.page_key,+ -- The address cited most often stands for the page.+ mode() within group (order by c.url) as page,+ max(c.title) as title,+ count(distinct c.prompt_id)::integer as prompts_in_common,+ count(*)::bigint as citations+ from cited c+ group by c.page_key+ ),+ google as (+ select+ g.page_key,+ round((sum(g.position * g.impressions) / nullif(sum(g.impressions), 0))::numeric, 1)+ as position+ from public.gsc_page_stats g+ where g.brand_id = p_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ and g.page_key in (select p.page_key from pages p)+ group by g.page_key+ )+ select p.page, p.title, p.prompts_in_common, p.citations, gl.position+ from pages p+ left join google gl on gl.page_key = p.page_key+ order by p.prompts_in_common desc, p.citations desc, p.page+ limit 10;+end;+$$;++create or replace function public.content_draft_sources(+ p_draft_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_brand_id uuid;+ v_prompt_ids uuid[];+ v_page_key text;+ v_host text;+ v_since timestamptz;+ -- The window's first whole UTC day.+ v_first_day date;+ v_result jsonb;+begin+ select d.brand_id, d.prompt_ids, lower(public.normalize_page_path(d.source_url)),+ -- Lower-cased first: a scheme may be written HTTPS://.+ regexp_replace(substring(lower(d.source_url) from '^https?://([^/:?#]+)'), '^www\.', '')+ into v_brand_id, v_prompt_ids, v_page_key, v_host+ from public.content_drafts d+ where d.id = p_draft_id;++ if v_brand_id is null or (+ not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = v_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role'+ ) then+ return null;+ end if;+ if p_days is null or p_days < 1 or p_days > 90 then+ raise exception 'content_draft_sources: 1 to 90 days are needed' using errcode = '22023';+ end if;++ v_since := now() - make_interval(days => p_days);+ v_first_day := (v_since at time zone 'utc')::date + 1;+ -- A page on a site that isn't the brand's is no page of the brand's.+ if v_host is null or not exists (+ select 1 from public.brand_domains bd+ where bd.brand_id = v_brand_id+ and lower(regexp_replace(bd.domain, '^www\.', '')) = v_host+ ) then+ v_page_key := null;+ end if;++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = v_brand_id+ ),+ -- The page's addresses: its path on its host.+ page_urls as materialized (+ select cu.id+ from public.citation_urls cu+ where v_page_key is not null+ and cu.domain = v_host+ and lower(public.normalize_page_path(cu.url)) = v_page_key+ ),+ -- The window's answers, from idx_prompt_results_brand_created_cover alone+ -- (an index-only scan). The prompts are picked after this read: a prompt+ -- filter here makes the planner combine the prompt_id index with the+ -- brand's and read every answer each prompt ever had.+ window_answers as materialized (+ select pr.id, pr.prompt_id, pr.created_at+ from public.prompt_results pr+ where (v_page_key is not null or coalesce(cardinality(v_prompt_ids), 0) > 0)+ and pr.brand_id = v_brand_id+ and pr.created_at >= v_since+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ -- The prompts whose answers cited the page the draft optimizes.+ page_prompts as (+ select distinct wa.prompt_id+ from public.prompt_result_citations prc+ join window_answers wa on wa.id = prc.prompt_result_id+ where prc.url_id in (select pu.id from page_urls pu)+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ ),+ source_prompts as (+ select p.id, p.text, p.id = any(v_prompt_ids) as in_draft,+ p.id in (select prompt_id from page_prompts) as cites_page+ from public.prompts p+ join public.prompt_sets ps on ps.id = p.prompt_set_id+ where ps.brand_id = v_brand_id+ and (p.id = any(v_prompt_ids) or p.id in (select prompt_id from page_prompts))+ ),+ -- The source prompts' answers in the window, newest first per prompt.+ answers as materialized (+ select wa.id, wa.prompt_id, wa.created_at,+ row_number() over (partition by wa.prompt_id order by wa.created_at desc) as recency+ from window_answers wa+ where wa.prompt_id in (select sp.id from source_prompts sp)+ ),+ -- The answers naming the brand: the window's whole UTC days from the+ -- Mentions rollup (mentions_prompt_daily, 00157), and its first, partial+ -- day from those answers' rows. A run still in progress joins the rollup+ -- when it completes.+ named as (+ select m.prompt_id, m.mention_answers as answers+ from public.mentions_prompt_daily m+ where m.brand_id = v_brand_id+ and m.day >= v_first_day+ and m.prompt_id in (select sp.id from source_prompts sp)+ union all+ select a.prompt_id, 1+ from answers a+ join public.prompt_results pr on pr.id = a.id+ where a.created_at < v_first_day::timestamp at time zone 'utc'+ and pr.mention_count > 0+ ),+ prompt_counts as (+ select a.prompt_id, a.answers, coalesce(n.mentioned, 0) as mentioned+ from (select prompt_id, count(*) as answers from answers group by prompt_id) a+ left join (+ select prompt_id, sum(answers) as mentioned from named group by prompt_id+ ) n on n.prompt_id = a.prompt_id+ ),+ -- The only answer rows read: each prompt's latest 30.+ sampled as (+ select pr.id, pr.platform, pr.search_queries+ from answers a+ join public.prompt_results pr on pr.id = a.id+ where a.recency <= 30+ ),+ fanout_rows as (+ select+ a.id as answer_id,+ lower(trim(regexp_replace(q.item->>'query', '\s+', ' ', 'g'))) as norm,+ trim(regexp_replace(q.item->>'query', '\s+', ' ', 'g')) as query,+ coalesce(nullif(q.item->>'source_platform', ''), a.platform) as platform+ from sampled a+ cross join lateral jsonb_array_elements(+ case when jsonb_typeof(a.search_queries) = 'array' then a.search_queries else '[]'::jsonb end+ ) as q(item)+ where coalesce(trim(q.item->>'query'), '') <> ''+ ),+ fanouts as (+ select min(query) as query, array_agg(distinct platform order by platform) as platforms,+ count(distinct answer_id) as searched+ from fanout_rows+ group by norm+ order by count(distinct answer_id) desc, min(query)+ limit 30+ ),+ -- The citations of the same sample as the fan-outs, each prompt's latest+ -- 30 answers: every answer in the window is 100k+ citations on a large+ -- brand, read one answer at a time. Grouped by address first; only the+ -- top 20 addresses are read.+ cited_ids as (+ select prc.url_id, count(*) as citations, count(distinct prc.prompt_result_id) as answers+ from answers a+ join public.prompt_result_citations prc+ on prc.prompt_result_id = a.id+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ where a.recency <= 30+ group by prc.url_id+ order by count(*) desc, count(distinct prc.prompt_result_id) desc, prc.url_id+ limit 20+ ),+ cited as (+ select cu.url, cu.title, cu.domain, ci.citations, ci.answers+ from cited_ids ci+ join public.citation_urls cu on cu.id = ci.url_id+ ),+ queries as (+ select g.query, sum(g.clicks)::bigint as clicks, sum(g.impressions)::bigint as impressions+ from public.gsc_query_stats g+ where g.brand_id = v_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ group by g.query+ order by sum(g.clicks) desc, sum(g.impressions) desc, g.query+ limit 300+ )+ select jsonb_build_object(+ 'prompts', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'id', sp.id,+ 'text', sp.text,+ 'inDraft', sp.in_draft,+ 'citesPage', sp.cites_page,+ 'answers', coalesce(pc.answers, 0),+ 'mentioned', coalesce(pc.mentioned, 0)+ ) order by sp.in_draft desc, sp.text)+ from source_prompts sp+ left join prompt_counts pc on pc.prompt_id = sp.id+ ), '[]'::jsonb),+ 'fanouts', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'query', f.query, 'platforms', to_jsonb(f.platforms), 'searched', f.searched+ ) order by f.searched desc, f.query)+ from fanouts f+ ), '[]'::jsonb),+ 'citedPages', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'url', c.url, 'title', c.title, 'domain', c.domain,+ 'citations', c.citations, 'answers', c.answers+ ) order by c.citations desc, c.answers desc, c.url)+ from cited c+ ), '[]'::jsonb),+ 'gscQueries', coalesce((+ select jsonb_agg(jsonb_build_object(+ 'query', q.query, 'clicks', q.clicks, 'impressions', q.impressions+ ) order by q.clicks desc, q.impressions desc, q.query)+ from queries q+ ), '[]'::jsonb),+ 'brandDomains', coalesce((select jsonb_agg(domain order by domain) from own_domains), '[]'::jsonb),+ 'competitorDomains', coalesce((+ select jsonb_agg(distinct lower(regexp_replace(c.domain, '^www\.', '')))+ from public.competitors c+ where c.brand_id = v_brand_id and coalesce(c.domain, '') <> ''+ ), '[]'::jsonb)+ ) into v_result;+ return v_result;+end;+$$;++create or replace function public.content_plan_pages(+ p_brand_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+-- A cached generic plan can't see the brand's size (00207).+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_last date := (now() at time zone 'utc')::date - 1;+ v_cur_day date;+ v_prev_day date;+ v_cur timestamptz;+ v_until timestamptz;+ v_result jsonb;+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return null;+ end if;+ if p_days not in (7, 30, 90) then+ raise exception 'p_days must be 7, 30 or 90';+ end if;++ v_cur_day := v_last - p_days + 1;+ v_prev_day := v_cur_day - p_days;+ v_cur := v_cur_day::timestamp at time zone 'utc';+ v_until := (v_last + 1)::timestamp at time zone 'utc';++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ url_counts as materialized (+ select d.url_id, d.platform, d.day >= v_cur_day as is_cur, sum(d.citations)::bigint as n+ from public.citation_url_daily d+ where d.brand_id = p_brand_id+ and d.day >= v_prev_day+ and d.day <= v_last+ and d.url_id in (select id from own_urls)+ and d.platform is distinct from 'chatgpt-shopping'+ group by d.url_id, d.platform, d.day >= v_cur_day+ ),+ url_totals as (+ select+ u.page_key,+ u.url,+ u.title,+ coalesce(sum(c.n) filter (where c.is_cur), 0) as cur,+ coalesce(sum(c.n) filter (where not c.is_cur), 0) as prev+ from url_counts c+ join own_urls u on u.id = c.url_id+ group by u.page_key, u.url, u.title+ ),+ pages as materialized (+ select+ t.page_key,+ -- The address cited most often stands for the page.+ (array_agg(t.url order by t.cur + t.prev desc, t.url))[1] as page,+ max(t.title) as title,+ sum(t.cur)::bigint as cur,+ sum(t.prev)::bigint as prev+ from url_totals t+ group by t.page_key+ order by sum(t.cur + t.prev) desc, t.page_key+ limit 100+ ),+ answers as (+ select+ r.platform,+ coalesce(sum(r.answers) filter (where r.day >= v_cur_day), 0)::bigint as cur,+ coalesce(sum(r.answers) filter (where r.day < v_cur_day), 0)::bigint as prev+ from public.citation_result_daily r+ where r.brand_id = p_brand_id+ and r.day >= v_prev_day+ and r.day <= v_last+ and r.platform is distinct from 'chatgpt-shopping'+ group by r.platform+ ),+ per_platform as (+ select+ u.page_key,+ c.platform,+ coalesce(sum(c.n) filter (where c.is_cur), 0) as cur,+ coalesce(sum(c.n) filter (where not c.is_cur), 0) as prev+ from url_counts c+ join own_urls u on u.id = c.url_id+ where u.page_key in (select p.page_key from pages p)+ and c.platform is not null+ group by u.page_key, c.platform+ ),+ down as (+ -- A platform tracked in both windows, citing the page at a lower rate.+ select+ pp.page_key,+ array_agg(+ pp.platform+ order by pp.prev::numeric / a.prev - pp.cur::numeric / a.cur desc, pp.platform+ ) as platforms+ from per_platform pp+ join answers a on a.platform = pp.platform+ where a.prev > 0+ and a.cur > 0+ and pp.prev::numeric / a.prev > pp.cur::numeric / a.cur+ group by pp.page_key+ ),+ cur_answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= v_cur+ and pr.created_at < v_until+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ -- The window comes from cur_answers: a citation carries its answer's+ -- created_at. A date bound on prc too would be estimated from+ -- prompt_result_citations' last analyze (no index leads with created_at),+ -- at about one row for recent days, and that plan rescans own_urls once+ -- per citation.+ page_prompts as (+ select distinct u.page_key, a.prompt_id+ from public.prompt_result_citations prc+ join own_urls u on u.id = prc.url_id+ join cur_answers a on a.id = prc.prompt_result_id+ where prc.brand_id = p_brand_id+ and u.page_key in (select p.page_key from pages p)+ ),+ competing as (+ select a.page_key, b.page_key as other+ from page_prompts a+ join page_prompts b on b.prompt_id = a.prompt_id and b.page_key <> a.page_key+ group by a.page_key, b.page_key+ having count(*) >= 2+ ),+ page_overlaps as (+ select cm.page_key, count(*) as pages+ from competing cm+ group by cm.page_key+ ),+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_cur+ and l.created_at < v_until+ ),+ visits as (+ select lower(public.normalize_page_path(l.url)) as page_key, 1::bigint as n+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_cur+ and l.created_at < v_until+ union all+ select lower(public.normalize_page_path(g.landing_page)), g.sessions::bigint+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.sessions > 0+ -- A session without a landing page would read as the home page.+ and g.landing_page <> ''+ and g.date >= v_cur_day+ and g.date <= v_last+ and not exists (select 1 from snippet_days s where s.day = g.date)+ ),+ page_visits as (+ select v.page_key, sum(v.n) as visits+ from visits v+ where v.page_key in (select p.page_key from pages p)+ group by v.page_key+ )+ select jsonb_build_object(+ 'days', p_days,+ -- The earlier window's first day, and the brand's first tracked day: a+ -- window before it is only partly tracked, or not at all.+ 'prevFrom', v_prev_day,+ 'trackedSince', (+ select min(r.day) from public.citation_result_daily r where r.brand_id = p_brand_id+ ),+ 'answers', (+ select jsonb_build_object(+ 'cur', coalesce(sum(a.cur), 0),+ 'prev', coalesce(sum(a.prev), 0)+ )+ from answers a+ ),+ 'pages', coalesce((+ select jsonb_agg(+ jsonb_build_object(+ 'page', p.page,+ 'title', p.title,+ 'citations', jsonb_build_object('cur', p.cur, 'prev', p.prev),+ 'platformsDown', coalesce(to_jsonb(d.platforms[1:3]), '[]'::jsonb),+ 'aiVisits', coalesce(pv.visits, 0),+ 'overlaps', coalesce(o.pages, 0)+ )+ order by p.cur + p.prev desc, p.page+ )+ from pages p+ left join down d on d.page_key = p.page_key+ left join page_overlaps o on o.page_key = p.page_key+ left join page_visits pv on pv.page_key = p.page_key+ ), '[]'::jsonb)+ )+ into v_result;++ return v_result;+end;+$$;++create or replace function public.content_plan_insights(+ p_brand_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+-- A cached generic plan can't see the brand's size (00207).+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ -- Whole days to yesterday, as the Plan's pages (00220).+ v_last date := (now() at time zone 'utc')::date - 1;+ v_day date;+ v_from timestamptz;+ v_until timestamptz;+ v_result jsonb;+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return null;+ end if;+ if p_days not in (7, 30, 90) then+ raise exception 'p_days must be 7, 30 or 90';+ end if;++ v_day := v_last - p_days + 1;+ v_from := v_day::timestamp at time zone 'utc';+ v_until := (v_last + 1)::timestamp at time zone 'utc';++ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ -- The brand's own addresses, through citation_urls' domain index, as+ -- content_plan_pages' own_urls (00220).+ own_ids as materialized (+ select cu.id+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ cited as materialized (+ select d.url_id, sum(d.citations)::bigint as citations+ from public.citation_url_daily d+ where d.brand_id = p_brand_id+ and d.day >= v_day+ and d.day <= v_last+ and d.platform is distinct from 'chatgpt-shopping'+ group by d.url_id+ ),+ chosen as (+ (+ select c.url_id, c.citations, true as own+ from cited c+ where exists (select 1 from own_ids o where o.id = c.url_id)+ order by c.citations desc, c.url_id+ limit 5000+ )+ union all+ (+ select c.url_id, c.citations, false+ from cited c+ where not exists (select 1 from own_ids o where o.id = c.url_id)+ order by c.citations desc, c.url_id+ limit 1000+ )+ ),+ top_urls as (+ select ch.url_id, ch.citations, ch.own, cu.url, cu.title+ from chosen ch+ join public.citation_urls cu on cu.id = ch.url_id+ ),+ snippet_days as (+ select distinct (l.created_at at time zone 'utc')::date as day+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_from+ and l.created_at < v_until+ ),+ visits as (+ select count(*)::bigint as n+ from public.ai_traffic_logs l+ where l.brand_id = p_brand_id+ and l.visit_of is null+ and l.created_at >= v_from+ and l.created_at < v_until+ union all+ select coalesce(sum(g.sessions), 0)::bigint+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.sessions > 0+ and g.date >= v_day+ and g.date <= v_last+ and not exists (select 1 from snippet_days s where s.day = g.date)+ ),+ analytics as (+ select+ coalesce(sum(g.key_events), 0)::bigint as key_events,+ coalesce(sum(g.transactions), 0)::bigint as transactions,+ coalesce(sum(g.purchase_revenue), 0)::numeric as revenue+ from public.ga_ai_traffic_stats g+ where g.brand_id = p_brand_id+ and g.date >= v_day+ and g.date <= v_last+ )+ select jsonb_build_object(+ 'days', p_days,+ 'totalCitations', (select coalesce(sum(c.citations), 0) from cited c),+ 'citations', coalesce((+ select jsonb_agg(+ jsonb_build_object(+ 'url', t.url,+ 'title', t.title,+ 'own', t.own,+ 'citations', t.citations+ )+ order by t.citations desc, t.url+ )+ from top_urls t+ ), '[]'::jsonb),+ 'results', (+ select jsonb_build_object(+ 'visits', (select coalesce(sum(v.n), 0) from visits v),+ 'keyEvents', a.key_events,+ 'transactions', a.transactions,+ 'revenue', a.revenue,+ 'analytics', (+ select nullif(b.ga_property_id, '') is not null+ from public.brands b where b.id = p_brand_id+ ),+ 'purchases', a.transactions > 0 or a.revenue > 0 or exists (+ select 1 from public.ga_channel_stats c+ where c.brand_id = p_brand_id+ and c.date >= v_day+ and c.date <= v_last+ and (c.transactions > 0 or c.purchase_revenue > 0)+ )+ )+ from analytics a+ )+ )+ into v_result;++ return v_result;+end;+$$;++comment on function public.content_plan_insights(uuid, integer) is+ 'Content Editor (CE-014): the addresses AI answers cite for the brand over the last N (7, 30 or 90) whole UTC days to yesterday (from citation_url_daily): its own pages (the 5,000 cited most) and the 1,000 others cited most, with the total; and what AI-referred visitors did on the site: visits, Google Analytics AI key events, purchases and revenue. Security definer with an organization check.';++-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00227_content_rule_sets_site_type.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00227: a workspace's own rules per kind of site (Content Editor, CE-009)+-- ============================================================================+-- My rules (00214) were kept per organization and page type. Since the page+-- types follow the brand's kind of site (00205), a type such as "guide" (a+-- How-to guide) is written on a B2B site, a shop and a travel site alike, so+-- a set saved for one brand applied to the same organization's brands of+-- another kind, with rules meant for that site.+--+-- * content_rule_sets gets site_type, the brand's kind of site when the set+-- was saved (b2b, ecommerce or b2c, as brands.site_type), and the key+-- becomes (organization_id, site_type, page_type).+-- * A set saved before keeps applying where it did: it is copied to each+-- kind of site. The member policies are by organization and stay as they+-- are.++alter table public.content_rule_sets+ add column if not exists site_type text not null default 'b2b'+ check (site_type in ('b2b', 'ecommerce', 'b2c'));++alter table public.content_rule_sets drop constraint if exists content_rule_sets_pkey;+alter table public.content_rule_sets+ add constraint content_rule_sets_pkey primary key (organization_id, site_type, page_type);++insert into public.content_rule_sets (+ organization_id, site_type, page_type, overrides, text_rules, updated_by, updated_at+)+select r.organization_id, s.site_type, r.page_type, r.overrides, r.text_rules, r.updated_by,+ r.updated_at+from public.content_rule_sets r+cross join (values ('ecommerce'), ('b2c')) as s(site_type)+where r.site_type = 'b2b'+on conflict do nothing;++-- Every save names its kind of site.+alter table public.content_rule_sets alter column site_type drop default;++comment on column public.content_rule_sets.site_type is+ 'Content Editor (CE-009): the kind of site the set is for (b2b, ecommerce or b2c, as brands.site_type); a brand''s drafts start from its own kind''s set.';+-- ─────────────────────────────────────────────────────────────────────────-- migrations/00228_profiles_server_columns.sql-- ─────────────────────────────────────────────────────────────────────────diff --git a/web/messages/en.json b/web/messages/en.jsonindex 80a55d509..2282e84bc 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3151,7 +3151,7 @@"competitors": "{names} cited · you aren't","competitorsVisibility": "{names} cited · you {visibility}%","opportunity": "{count, plural, =0 {A page AI needs} one {Answers # prompt AI asks} other {Answers # prompts AI asks}}",- "citationsDown": "Citations −{pct}%",+ "citationsDown": "Citation rate −{pct}%","on": "on {platforms}","top": "Top page · {citations, plural, one {{citationsText} citation} other {{citationsText} citations}} · {visits, plural, one {{visitsText} AI visit} other {{visitsText} AI visits}}","overlap": "Competes with {count, plural, one {# of your pages} other {# of your pages}} for the same prompts"@@ -3348,11 +3348,19 @@"promptsValue": "{count} of {total}","recommended": "Recommended","options": {+ "optimizeGiven": {+ "title": "Optimize {page}",+ "reason": "The page your Content plan picked"+ },"optimizeInstead": {"title": "Optimize {page} instead","reason": "It already answers {count} of {total} prompts and is cited {citations}×","reasonRanked": "It already answers {count} of {total} prompts, is cited {citations}× and ranks #{position} on Google"},+ "createNew": {+ "title": "Create a new page instead",+ "reason": "Write a new page for these prompts"+ },"createAnyway": {"title": "Create a new page anyway","reason": "Risk: two pages compete for the same prompts"@@ -3910,6 +3918,8 @@"needsFeed": "Needs feed","noFanouts": "No fan-outs","addKeywords": "Add keywords",+ "notHowTo": "Not a how-to",+ "englishOnly": "English only","numberCheck": "Number check"},"mineName": "My rules · {type}",diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsxindex b79dab630..a9e9472a1 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx@@ -482,18 +482,20 @@ export function DraftEditor({if (editable) editor.setEditable(true, false);}if (tr?.docChanged) {- let change = tr;- if (!tr.before.eq(editor.state.doc)) {+ if (tr.before.eq(editor.state.doc)) {+ editor.view.dispatch(tr);+ } else {// The page changed while the decision was saved: the change is made- // again on the page as it is now. What it no longer fits goes back- // to how it was, saved as well, so the cards and the page agree.+ // again on the page as it is now, at once, before the page can+ // change again. What it no longer fits goes back to how it was,+ // saved as well, so the cards and the page agree.const redo = redoDecision(editor.state,all,changed,decision === 'accept' ? 'accept' : 'undo',);- change = redo.tr;+ if (redo.tr.docChanged) editor.view.dispatch(redo.tr);if (redo.failed.length > 0) {const back: SuggestionStatus = decision === 'accept' ? 'open' : 'accepted';await decideDraftSuggestions(redo.failed, back);@@ -501,7 +503,6 @@ export function DraftEditor({toast(t('suggestions.notApplied', { count: redo.failed.length }));}}- if (change.docChanged) editor.view.dispatch(change);}setSuggestions(next);if (decision === 'accept')diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.test.tsxindex d81cb3fd0..a951a579a 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.test.tsx@@ -21,6 +21,8 @@ vi.mock('@/lib/content/editor', async (importOriginal) => ({const LOADED = { updatedAt: 't0', bodyHtml: '<p>loaded</p>' };const BIG = `<p>${'x'.repeat(500_000)}</p>`;+/** A page sanitize-html rewrites: <br> becomes <br />, links lose target and rel. */+const REAL = '<p><a href="https://acme.example/" target="_blank" rel="noopener">Acme</a><br></p>';const BODIES = ['<p>a</p>', '<p>ab</p>', '<p>1</p>', '<p>12</p>', '<p>x</p>', '<p>last</p>'];/** The hook behind buttons, as the editor drives it: one per text, Retry, and settle. */@@ -35,6 +37,7 @@ function Harness() {<button key={body} data-body={body} onClick={() => autosave.schedule(content(body))} />))}<button data-big onClick={() => autosave.schedule(content(BIG))} />+ <button data-real onClick={() => autosave.schedule(content(REAL))} /><button data-retry onClick={() => void autosave.retry()} /><buttondata-discard@@ -127,6 +130,8 @@ describe('the editor’s autosave', () => {saveContentDraft.mockRejectedValueOnce(new Error('offline'));edit('<p>x</p>');await act(async () => vi.advanceTimersByTime(1500));+ // The lost save's page is fingerprinted once the sanitizer has loaded.+ await act(() => vi.dynamicImportSettled());expect(state()).toBe('failed');await retry();@@ -145,6 +150,19 @@ describe('the editor’s autosave', () => {error.mockRestore();});+ it('sends the lost save as the server stores it: sanitized', async () => {+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});+ saveContentDraft.mockRejectedValueOnce(new Error('offline'));+ act(() => container.querySelector<HTMLButtonElement>('[data-real]')!.click());+ await act(async () => vi.advanceTimersByTime(1500));+ await act(() => vi.dynamicImportSettled());+ await retry();+ const sent = saveContentDraft.mock.calls[1][1].base.unconfirmedHash;+ expect(sent).toBe('hash:<p><a href="https://acme.example/">Acme</a><br /></p>');+ expect(sent).not.toBe(`hash:${REAL}`);+ error.mockRestore();+ });+it('sends no unconfirmed page after a save the server answered with a failure', async () => {saveContentDraft.mockResolvedValueOnce({ ok: false, reason: 'invalid' });edit('<p>x</p>');diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.tsindex 9b423f9f3..b562381bf 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-autosave.ts@@ -106,8 +106,12 @@ export function useDraftAutosave(draftId: string, loaded: { updatedAt: string; b}} catch (err) {console.error('Failed to save the draft:', err);- // No answer: the write may have gone through all the same.- unconfirmed.current = await bodyHash(content.bodyHtml).catch(() => null);+ // No answer: the write may have gone through all the same. The server+ // stores the page as sanitizeDraftHtml leaves it, so that is what it+ // compares; the sanitizer loads only here, out of the editor's bundle.+ unconfirmed.current = await import('@/lib/content/sanitize-draft-html')+ .then(({ sanitizeDraftHtml }) => bodyHash(sanitizeDraftHtml(content.bodyHtml)))+ .catch(() => null);saving.current = false;failed.current = true;pending.current ??= content;diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.tsindex b9b445c87..560f241f4 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts@@ -114,7 +114,9 @@ export function useDraftRules({const sources = reads.sources;const ctx = useMemo<RuleContext>(() => {- if (!sources || sources.failed) return { brandDomains: [], competitorDomains: [], fanouts: [] };+ if (!sources || sources.failed) {+ return { brandDomains: [], competitorDomains: [], fanouts: [], language };+ }const fanouts = buildSourceGroups(sources.data, sources.choices, {title: draft.title,sourceUrl: draft.sourceUrl,@@ -126,8 +128,9 @@ export function useDraftRules({brandDomains: sources.data.brandDomains,competitorDomains: sources.data.competitorDomains,fanouts: fanouts ?? [],+ language,};- }, [sources, draft.title, draft.sourceUrl]);+ }, [sources, draft.title, draft.sourceUrl, language]);const brief = reads.brief;const keywords = useMemo(() => (brief.status === 'ready' ? (brief.brief?.keywords ?? []) : []),diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsxindex a8f2344fc..963cd90e0 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx@@ -917,6 +917,51 @@ describe('the content editor', () => {expect(cardOf('s1')!.textContent).toContain('Accepted');});+ it('makes the change again at once, before saving back what no longer fits', async () => {+ getContentDraft.mockResolvedValue(page);+ getDraftSuggestions.mockResolvedValue(batch);+ let firstSaved: () => void = () => {};+ let backSaved: () => void = () => {};+ decideDraftSuggestions+ .mockReturnValueOnce(new Promise<void>((r) => (firstSaved = r)))+ .mockReturnValueOnce(new Promise<void>((r) => (backSaved = r)));+ await renderPage();++ await act(async () => barButton('Accept all').click());+ const editor = (container.querySelector('.ProseMirror') as HTMLElement & { editor: Editor })+ .editor;+ // While the decision is saved, the passage s2 goes after is rewritten.+ await act(async () => {+ let range: { from: number; to: number } | null = null;+ editor.state.doc.descendants((node, pos) => {+ if (node.isText && node.text === 'Teams trust it.') {+ range = { from: pos, to: pos + node.nodeSize };+ }+ });+ editor.commands.insertContentAt(range!, 'Teams rely on it.');+ });+ await act(async () => firstSaved());+ await act(async () => {});+ // s1 is on the page already; s2 goes back to review, and the page changes again meanwhile.+ expect(decideDraftSuggestions).toHaveBeenLastCalledWith(['s2'], 'open');+ expect(pageText()).toContain(+ 'Accuracy is shown per team, each week, against closed revenue.',+ );+ await act(async () => {+ editor.commands.insertContentAt(editor.state.doc.content.size, '<p>From a file.</p>');+ });+ await act(async () => backSaved());+ await act(async () => {});++ expect(pageText()).toContain(+ 'Accuracy is shown per team, each week, against closed revenue.',+ );+ expect(pageText()).toContain('Teams rely on it.');+ expect(pageText()).toContain('From a file.');+ expect(pageText()).not.toContain('FAQ');+ expect(cardOf('s1')!.textContent).toContain('Accepted');+ });+it('rejects one, and restores it to review', async () => {getContentDraft.mockResolvedValue(page);getDraftSuggestions.mockResolvedValue(batch);diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/plan/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/plan/page.test.tsxindex d63a0be29..7de40af17 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/plan/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/plan/page.test.tsx@@ -162,7 +162,7 @@ describe('the Content plan', () => {['Coffee subscriptionsHow-to guide/guides/coffee-subscriptions','Optimize',- 'Citations −70% on ChatGPT and PerplexityCitations',+ 'Citation rate −70% on ChatGPT and PerplexityCitations',],['Espresso guideHow-to guide/guides/espresso⚠ Overlap',diff --git a/web/src/components/content/new-content-dialog.test.tsx b/web/src/components/content/new-content-dialog.test.tsxindex c74cda756..d26e460d6 100644--- a/web/src/components/content/new-content-dialog.test.tsx+++ b/web/src/components/content/new-content-dialog.test.tsx@@ -323,6 +323,9 @@ describe('New content', () => {});await act(async () => {});expect(document.body.textContent).toContain('Optimize: Pricing');+ // Nothing overlaps, and Start still shows the draft optimizes the plan's page.+ expect(document.body.textContent).toContain('No existing page answers these prompts');+ expect(radio('Optimize /pricing').getAttribute('aria-checked')).toBe('true');await click(button('Open editor'));expect(createContentDraft.mock.calls[0][1]).toMatchObject({method: 'optimize',@@ -332,6 +335,85 @@ describe('New content', () => {});});+ it('lets a person write a new page instead of the plan’s page when nothing overlaps', async () => {+ getContentOverlap.mockResolvedValue({ totalPrompts: 2, pages: [] });+ await render('b2b', {+ kind: 'opportunity',+ id: 'o2',+ page: { url: 'https://northwind.example/pricing', title: 'Pricing' },+ });+ await act(async () => {});++ await click(radio('Create a new page instead'));+ expect(document.body.textContent).toContain('Create: CRM for real estate teams');+ expect(radio('Optimize /pricing').getAttribute('aria-checked')).toBe('false');+ await click(button('Open editor'));+ expect(createContentDraft.mock.calls[0][1]).toMatchObject({+ method: 'create',+ title: 'CRM for real estate teams',+ });+ });++ it('offers the plan’s page beside the check’s options, to come back to', async () => {+ getContentOverlap.mockResolvedValue({+ totalPrompts: 4,+ pages: [+ {+ url: 'https://northwind.example/blog/crm-guide',+ title: 'CRM guide',+ promptsInCommon: 1,+ citations: 6,+ gscPosition: null,+ },+ ],+ });+ await render('b2b', {+ kind: 'opportunity',+ id: 'o2',+ page: { url: 'https://northwind.example/pricing', title: 'Pricing' },+ });+ await act(async () => {});++ const given = radio('Optimize /pricing');+ expect(given.textContent).toContain('The page your Content plan picked');+ expect(given.getAttribute('aria-checked')).toBe('true');+ expect(radio('Create a new page, link /blog/crm-guide to it')).toBeDefined();++ await click(radio('Add a section to /blog/crm-guide'));+ expect(radio('Optimize /pricing').getAttribute('aria-checked')).toBe('false');+ await click(radio('Optimize /pricing'));+ expect(radio('Optimize /pricing').getAttribute('aria-checked')).toBe('true');+ await click(button('Open editor'));+ expect(createContentDraft.mock.calls[0][1]).toMatchObject({+ method: 'optimize',+ sourceUrl: 'https://northwind.example/pricing',+ });+ });++ it('offers no second option for the plan’s page when the check names it too', async () => {+ getContentOverlap.mockResolvedValue({+ totalPrompts: 4,+ pages: [+ {+ url: 'https://www.northwind.example/pricing/',+ title: 'Pricing',+ promptsInCommon: 3,+ citations: 6,+ gscPosition: null,+ },+ ],+ });+ await render('b2b', {+ kind: 'opportunity',+ id: 'o2',+ page: { url: 'https://northwind.example/pricing', title: 'Pricing' },+ });+ await act(async () => {});++ expect(document.body.textContent).not.toContain('The page your Content plan picked');+ expect(radio('Optimize /pricing instead').getAttribute('aria-checked')).toBe('true');+ });+it('leaves an opportunity no longer open to pick', async () => {await render('b2b', { kind: 'opportunity', id: 'gone' });expect(button('Continue')).toBeDefined();diff --git a/web/src/components/content/new-content-dialog.tsx b/web/src/components/content/new-content-dialog.tsxindex 44cc74e04..0cf0110bc 100644--- a/web/src/components/content/new-content-dialog.tsx+++ b/web/src/components/content/new-content-dialog.tsx@@ -54,7 +54,9 @@ import {import { DOCUMENT_ACCEPT, readDocumentFile } from '@/lib/content/import-document';import {adviseOverlap,+ overlapOptions,pagePath,+ samePage,type OverlapChoice,type OverlapOption,} from '@/lib/content/overlap';@@ -112,6 +114,8 @@ interface StartChoice {newPageType: { pageType: PageType; detectedFrom: DetectedFrom; picked: boolean };/** The overlap check's pick: write the new page, or optimize an existing one. */choice: OverlapChoice;+ /** The page the Content plan named for this opportunity, offered beside the check's options. */+ givenPage?: OverlapPage;}/**@@ -400,6 +404,7 @@ export function NewContentDialog({...newPageType,newPageType,choice: { kind: 'create' },+ givenPage: page,};choiceGiven.current = !!page;setStart(page ? withChoice(next, { kind: 'optimize', page }, siteType) : next);@@ -611,6 +616,7 @@ export function NewContentDialog({<OverlapCheckread={overlapRead?.key === overlapKey ? overlapRead : null}choice={start.choice}+ given={start.givenPage ?? null}onChoose={(choice) => setStart(withChoice(start, choice, siteType))}onRetry={() => {setOverlapRead(null);@@ -943,11 +949,13 @@ function withChoice(start: StartChoice, choice: OverlapChoice, siteType: SiteTypfunction OverlapCheck({read,choice,+ given,onChoose,onRetry,}: {read: OverlapRead | null;choice: OverlapChoice;+ given: OverlapPage | null;onChoose: (choice: OverlapChoice) => void;onRetry: () => void;}) {@@ -956,14 +964,20 @@ function OverlapCheck({const advice = overlap ? adviseOverlap(overlap) : null;const shown = overlap?.pages.slice(0, 3) ?? [];const top = overlap?.pages[0];+ const options = advice ? overlapOptions(advice, given) : [];const isChosen = (option: OverlapOption) =>option.choice.kind === choice.kind &&(option.choice.kind === 'create' ||- (choice.kind === 'optimize' && option.choice.page.url === choice.page.url));+ (choice.kind === 'optimize' && samePage(option.choice.page.url, choice.page.url)));const optionText = (option: OverlapOption) => {const page = top ? pagePath(top.url) : '';switch (option.key) {+ case 'optimizeGiven':+ return {+ title: t('options.optimizeGiven.title', { page: given ? pagePath(given.url) : '' }),+ reason: t('options.optimizeGiven.reason'),+ };case 'optimizeInstead':return {title: t('options.optimizeInstead.title', { page }),@@ -979,6 +993,11 @@ function OverlapCheck({},),};+ case 'createNew':+ return {+ title: t('options.createNew.title'),+ reason: t('options.createNew.reason'),+ };case 'createAnyway':return {title: t('options.createAnyway.title'),@@ -1000,6 +1019,46 @@ function OverlapCheck({}};+ const optionGroup = options.length > 0 && (+ <div role="radiogroup" aria-label={t('label')} className="grid gap-1.5">+ {options.map((option) => {+ const text = optionText(option);+ const chosen = isChosen(option);+ return (+ <button+ key={option.key}+ type="button"+ role="radio"+ aria-checked={chosen}+ onClick={() => onChoose(option.choice)}+ className={cn(+ 'flex items-start gap-2.5 rounded-xl border px-3 py-2.5 text-left transition-colors hover:bg-muted/50',+ chosen && 'border-foreground ring-1 ring-inset ring-foreground',+ )}+ >+ <span+ className={cn(+ 'mt-0.5 h-4 w-4 shrink-0 rounded-full border-[1.5px] border-muted-foreground/50',+ chosen && 'border-[5px] border-foreground',+ )}+ />+ <span className="min-w-0">+ <span className="flex flex-wrap items-center gap-1.5 text-sm font-medium">+ {text.title}+ {option.recommended && (+ <span className="rounded-md bg-muted px-1.5 text-[10.5px] font-medium text-muted-foreground">+ {t('recommended')}+ </span>+ )}+ </span>+ <span className="block text-xs text-muted-foreground">{text.reason}</span>+ </span>+ </button>+ );+ })}+ </div>+ );+return (<div className="space-y-2"><Label>{t('label')}</Label>@@ -1016,15 +1075,18 @@ function OverlapCheck({</button></p>) : advice.verdict === 'none' ? (- <div className="flex items-center justify-between gap-3 rounded-lg border px-3 py-2.5 text-sm">- <span>{overlap.totalPrompts ? t('none') : t('noPrompts')}</span>- {overlap.totalPrompts > 0 && (- <span className="flex shrink-0 items-center gap-1 text-xs font-medium text-emerald-600 dark:text-emerald-400">- <Check className="h-3.5 w-3.5" />- {t('safe')}- </span>- )}- </div>+ <>+ <div className="flex items-center justify-between gap-3 rounded-lg border px-3 py-2.5 text-sm">+ <span>{overlap.totalPrompts ? t('none') : t('noPrompts')}</span>+ {overlap.totalPrompts > 0 && (+ <span className="flex shrink-0 items-center gap-1 text-xs font-medium text-emerald-600 dark:text-emerald-400">+ <Check className="h-3.5 w-3.5" />+ {t('safe')}+ </span>+ )}+ </div>+ {optionGroup}+ </>) : (<><div className="rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2.5 text-sm">@@ -1068,43 +1130,7 @@ function OverlapCheck({</tbody></table></div>- <div role="radiogroup" aria-label={t('label')} className="grid gap-1.5">- {advice.options.map((option) => {- const text = optionText(option);- const chosen = isChosen(option);- return (- <button- key={option.key}- type="button"- role="radio"- aria-checked={chosen}- onClick={() => onChoose(option.choice)}- className={cn(- 'flex items-start gap-2.5 rounded-xl border px-3 py-2.5 text-left transition-colors hover:bg-muted/50',- chosen && 'border-foreground ring-1 ring-inset ring-foreground',- )}- >- <span- className={cn(- 'mt-0.5 h-4 w-4 shrink-0 rounded-full border-[1.5px] border-muted-foreground/50',- chosen && 'border-[5px] border-foreground',- )}- />- <span className="min-w-0">- <span className="flex flex-wrap items-center gap-1.5 text-sm font-medium">- {text.title}- {option.recommended && (- <span className="rounded-md bg-muted px-1.5 text-[10.5px] font-medium text-muted-foreground">- {t('recommended')}- </span>- )}- </span>- <span className="block text-xs text-muted-foreground">{text.reason}</span>- </span>- </button>- );- })}- </div>+ {optionGroup}</>)}</div>diff --git a/web/src/lib/actions/content-drafts-my-rules.test.ts b/web/src/lib/actions/content-drafts-my-rules.test.tsindex ea43c9df8..9e80260a7 100644--- a/web/src/lib/actions/content-drafts-my-rules.test.ts+++ b/web/src/lib/actions/content-drafts-my-rules.test.ts@@ -1,6 +1,6 @@import { beforeEach, describe, expect, it, vi } from 'vitest';-/** My rules (CE-009, 00214): the workspace's rule set per page type. */+/** My rules (CE-009, 00214, 00227): the workspace's rule set per kind of site and page type. */const BRAND = '33333333-3333-3333-3333-333333333333';@@ -39,14 +39,21 @@ const set = {};beforeEach(() => {- reads = { brands: { organization_id: 'org1' } };+ reads = {+ brands: {+ organization_id: 'org1',+ site_type: null,+ industry: 'E-commerce & Retail',+ shopping_mode_enabled: false,+ },+ };upserts = [];filters = [];user = { id: 'u1' };});describe('getMyRuleSets', () => {- it('reads the organization’s sets by page type, leaving out ones that don’t read as rules', async () => {+ it('reads the organization’s sets for the brand’s kind of site, leaving out ones that don’t read as rules', async () => {reads.content_rule_sets = [{ page_type: 'blog_post', overrides: set.overrides, text_rules: set.textRules },{ page_type: 'nope', overrides: {}, text_rules: [] },@@ -56,9 +63,18 @@ describe('getMyRuleSets', () => {expect(filters).toEqual([['brands', 'id', BRAND],['content_rule_sets', 'organization_id', 'org1'],+ // No site type chosen: the brand's industry guesses a shop.+ ['content_rule_sets', 'site_type', 'ecommerce'],]);});+ it('reads the kind of site the brand chose', async () => {+ reads.brands = { organization_id: 'org1', site_type: 'b2c', industry: null };+ reads.content_rule_sets = [];+ await getMyRuleSets(BRAND);+ expect(filters).toContainEqual(['content_rule_sets', 'site_type', 'b2c']);+ });+it('has none for a brand the member can’t read', async () => {reads.brands = null;expect(await getMyRuleSets(BRAND)).toEqual({});@@ -67,19 +83,20 @@ describe('getMyRuleSets', () => {});describe('saveMyRuleSet', () => {- it('keeps the set for the organization and page type, as the member', async () => {+ it('keeps the set for the organization, the brand’s kind of site and the page type, as the member', async () => {await saveMyRuleSet(BRAND, 'blog_post', set);expect(upserts).toEqual([{table: 'content_rule_sets',value: expect.objectContaining({organization_id: 'org1',+ site_type: 'ecommerce',page_type: 'blog_post',overrides: set.overrides,text_rules: set.textRules,updated_by: 'u1',}),- options: { onConflict: 'organization_id,page_type' },+ options: { onConflict: 'organization_id,site_type,page_type' },},]);});diff --git a/web/src/lib/actions/content-drafts.test.ts b/web/src/lib/actions/content-drafts.test.tsindex baa06f0d4..01beebc03 100644--- a/web/src/lib/actions/content-drafts.test.ts+++ b/web/src/lib/actions/content-drafts.test.ts@@ -163,6 +163,18 @@ describe('saveContentDraft', () => {expect(result).toMatchObject({ ok: true, updatedAt: '2026-10-20T15:01:00Z' });});+ it('takes a page that already holds what is being saved as its own', async () => {+ // A lost save wrote this very page; the editor sends it again.+ updates = [null, { updated_at: '2026-10-20T15:01:00Z' }];+ row = { updated_at: '2026-10-20T14:30:00Z', body_html: '<h1>Mine</h1><p>More</p>' };+ const result = await saveContentDraft(DRAFT, {+ title: 'Mine',+ bodyHtml: '<h1>Mine</h1><p>More</p>',+ base: await base(),+ });+ expect(result).toMatchObject({ ok: true, updatedAt: '2026-10-20T15:01:00Z' });+ });+it('doesn’t overwrite a page someone else changed', async () => {row = { updated_at: '2026-10-20T14:30:00Z', body_html: '<h1>Their version</h1>' };await expect(@@ -231,6 +243,8 @@ describe('the page type on save (CE-023)', () => {filters: [['eq', 'id', DRAFT],['eq', 'page_type_source', 'detected'],+ // Only over this save: a write in between keeps its own version.+ ['eq', 'updated_at', '2026-10-20T15:00:00Z'],],});});@@ -448,6 +462,8 @@ describe('History', () => {const typed = calls.find((call) => call.op === 'update')!;expect(typed.value).toEqual({ page_type: 'comparison' });expect(typed.filters).toContainEqual(['eq', 'page_type_source', 'detected']);+ // Only over the restore.+ expect(typed.filters).toContainEqual(['eq', 'updated_at', 't1']);// A type the person picked stays.calls = [];diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.tsindex 3bc2e43f5..340022e27 100644--- a/web/src/lib/actions/content-drafts.ts+++ b/web/src/lib/actions/content-drafts.ts@@ -862,6 +862,7 @@ export async function saveContentDraft(if (!draftId.safeParse(id).success || !parsed.success) return { ok: false, reason: 'invalid' };const { title, base } = parsed.data;const bodyHtml = sanitizeDraftHtml(parsed.data.bodyHtml);+ const savingHash = await bodyHash(bodyHtml);const supabase = await createClient();let expected = base.updatedAt;// While the Content Agent writes the page (CE-015), a tab left open waits:@@ -884,11 +885,14 @@ export async function saveContentDraft(const detected = detectPageType({ title, html: bodyHtml }, brandSiteType(data.brands));// A page that shows no type keeps the one it had (Start's guess).if (detected && detected !== data.page_type) {+ // Only over this save: a write in between would otherwise become+ // the editor's base, and its next autosave would go over it.const { data: typed, error: typeError } = await supabase.from('content_drafts').update({ page_type: detected }).eq('id', id).eq('page_type_source', 'detected')+ .eq('updated_at', data.updated_at).select('updated_at').maybeSingle();// The page is saved; its type catches up on the next save.@@ -899,7 +903,7 @@ export async function saveContentDraft(}}}- return { ok: true, updatedAt, bodyHash: await bodyHash(bodyHtml) };+ return { ok: true, updatedAt, bodyHash: savingHash };}const { data: current, error: readError } = await supabase@@ -914,7 +918,13 @@ export async function saveContentDraft(return { ok: false, reason: 'conflict' };}const currentHash = await bodyHash(current.body_html);- if (currentHash !== base.bodyHash && currentHash !== base.unconfirmedHash) {+ // The page the editor last saw, its lost save's, or already this one:+ // writing over any of them loses nothing.+ if (+ currentHash !== base.bodyHash &&+ currentHash !== base.unconfirmedHash &&+ currentHash !== savingHash+ ) {return { ok: false, reason: 'conflict' };}expected = current.updated_at;@@ -1071,6 +1081,8 @@ export async function restoreDraftVersion(id: string, versionId: string): Promis.update({ page_type: detected }).eq('id', id).eq('page_type_source', 'detected')+ // Only over the restore, as after a save.+ .eq('updated_at', restored.updatedAt).select('updated_at').maybeSingle();// The page is restored; its type catches up on the next save.@@ -1555,8 +1567,9 @@ export async function saveDraftRules(id: string, rules: DraftRulesInput): Promis}/**- * The workspace's My rules (CE-009, 00214), by page type: what a new draft- * of the type starts from. Sets that don't read as rules are left out.+ * The workspace's My rules (CE-009, 00214) for the brand's kind of site+ * (00227), by page type: what a new draft of the type starts from. Sets that+ * don't read as rules are left out.*/export async function getMyRuleSets(brandId: string,@@ -1565,7 +1578,7 @@ export async function getMyRuleSets(const supabase = await createClient();const { data: brand, error: brandError } = await supabase.from('brands')- .select('organization_id')+ .select('organization_id, site_type, industry, shopping_mode_enabled').eq('id', brandId).maybeSingle();if (brandError) throw new Error(brandError.message);@@ -1573,7 +1586,8 @@ export async function getMyRuleSets(const { data, error } = await supabase.from('content_rule_sets').select('page_type, overrides, text_rules')- .eq('organization_id', brand.organization_id);+ .eq('organization_id', brand.organization_id)+ .eq('site_type', brandSiteType(brand));if (error) throw new Error(error.message);const sets: Partial<Record<PageType, SavedRuleSet>> = {};for (const row of data ?? []) {@@ -1590,7 +1604,8 @@ export async function getMyRuleSets(/*** Save as my rules (CE-009): a draft's rule choices become the workspace's- * set for the page type, for every draft of it that starts after.+ * set for the page type on the brand's kind of site (00227), for every draft+ * of it that starts after.*/export async function saveMyRuleSet(brandId: string,@@ -1609,7 +1624,11 @@ export async function saveMyRuleSet(const supabase = await createClient();const [userRes, brandRes] = await Promise.all([supabase.auth.getUser(),- supabase.from('brands').select('organization_id').eq('id', brandId).maybeSingle(),+ supabase+ .from('brands')+ .select('organization_id, site_type, industry, shopping_mode_enabled')+ .eq('id', brandId)+ .maybeSingle(),]);if (brandRes.error) throw new Error(brandRes.error.message);const userId = userRes.data.user?.id;@@ -1618,13 +1637,14 @@ export async function saveMyRuleSet(const { error } = await supabase.from('content_rule_sets').upsert({organization_id: organizationId,+ site_type: brandSiteType(brandRes.data),page_type: pageType,overrides: parsed.data.overrides,text_rules: parsed.data.textRules,updated_by: userId,updated_at: new Date().toISOString(),},- { onConflict: 'organization_id,page_type' },+ { onConflict: 'organization_id,site_type,page_type' },);if (error) throw new Error(error.message);}diff --git a/web/src/lib/content/overlap.test.ts b/web/src/lib/content/overlap.test.tsindex b87c9999a..1ffab5dae 100644--- a/web/src/lib/content/overlap.test.ts+++ b/web/src/lib/content/overlap.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';import type { OverlapPage } from './drafts';-import { adviseOverlap, pagePath } from './overlap';+import { adviseOverlap, overlapOptions, pagePath, samePage } from './overlap';const page = (over: Partial<OverlapPage> = {}): OverlapPage => ({url: 'https://acme.example/guides/espresso',@@ -42,6 +42,46 @@ describe('adviseOverlap', () => {});});+describe('overlapOptions', () => {+ const top = page({ promptsInCommon: 1 });+ const advice = adviseOverlap({ totalPrompts: 3, pages: [top] });++ it('offers the advice alone without a page from the plan, or when it offers that page', () => {+ expect(overlapOptions(advice, null)).toBe(advice.options);+ expect(overlapOptions(advice, page({ url: 'https://www.acme.example/Guides/espresso/' }))).toBe(+ advice.options,+ );+ });++ it('offers the plan’s page and a new page when nothing overlaps', () => {+ const given = page({ url: 'https://acme.example/pricing', promptsInCommon: 0 });+ const none = adviseOverlap({ totalPrompts: 2, pages: [] });+ expect(overlapOptions(none, given)).toEqual([+ { key: 'optimizeGiven', choice: { kind: 'optimize', page: given }, recommended: false },+ { key: 'createNew', choice: { kind: 'create' }, recommended: false },+ ]);+ // With no page from the plan, nothing to choose.+ expect(overlapOptions(none, null)).toEqual([]);+ });++ it('offers the page the plan named first when the advice offers another', () => {+ const given = page({ url: 'https://acme.example/pricing', promptsInCommon: 0 });+ expect(overlapOptions(advice, given)).toEqual([+ { key: 'optimizeGiven', choice: { kind: 'optimize', page: given }, recommended: false },+ ...advice.options,+ ]);+ });+});++describe('samePage', () => {+ it('compares addresses as pages, and anything else as text', () => {+ expect(samePage('https://acme.example/a/', 'http://www.acme.example/A')).toBe(true);+ expect(samePage('https://acme.example/a', 'https://acme.example/b')).toBe(false);+ expect(samePage('not a url', 'not a url')).toBe(true);+ expect(samePage('not a url', 'https://acme.example/a')).toBe(false);+ });+});+describe('pagePath', () => {it('names a page by its path, the home page by its host', () => {expect(pagePath('https://acme.example/guides/espresso/')).toBe('/guides/espresso');diff --git a/web/src/lib/content/overlap.ts b/web/src/lib/content/overlap.tsindex 1d57c281b..ea0a822b8 100644--- a/web/src/lib/content/overlap.ts+++ b/web/src/lib/content/overlap.ts@@ -1,4 +1,5 @@import type { ContentOverlap, OverlapPage } from './drafts';+import { pageKey } from './sources';/*** Start's overlap check (Content Editor CE-015, spec §5): what to recommend@@ -17,7 +18,13 @@ export const COVERS_SHARE = 0.5;/** What Open editor does: write a new page, or optimize an existing one. */export type OverlapChoice = { kind: 'create' } | { kind: 'optimize'; page: OverlapPage };-export type OverlapOptionKey = 'optimizeInstead' | 'createAnyway' | 'createLinked' | 'addSection';+export type OverlapOptionKey =+ | 'optimizeGiven'+ | 'createNew'+ | 'optimizeInstead'+ | 'createAnyway'+ | 'createLinked'+ | 'addSection';export interface OverlapOption {key: OverlapOptionKey;@@ -53,6 +60,38 @@ export function adviseOverlap(overlap: ContentOverlap): OverlapAdvice {};}+/**+ * The options Start offers: the advice's, after the page the Content plan+ * named when the advice doesn't offer that page, so a person who picks+ * another option can come back to it. With nothing overlapping, the plan's+ * page and a new page: Start shows which one the draft will be, and lets+ * the person switch.+ */+export function overlapOptions(advice: OverlapAdvice, given: OverlapPage | null): OverlapOption[] {+ if (!given) return advice.options;+ if (advice.options.length === 0) {+ return [+ { key: 'optimizeGiven', choice: { kind: 'optimize', page: given }, recommended: false },+ { key: 'createNew', choice: { kind: 'create' }, recommended: false },+ ];+ }+ const offered = advice.options.some(+ (o) => o.choice.kind === 'optimize' && samePage(o.choice.page.url, given.url),+ );+ if (offered) return advice.options;+ return [+ { key: 'optimizeGiven', choice: { kind: 'optimize', page: given }, recommended: false },+ ...advice.options,+ ];+}++/** Two addresses of one page (pageKey), or the same text when either isn't an address. */+export function samePage(a: string, b: string): boolean {+ const keyA = pageKey(a);+ const keyB = pageKey(b);+ return keyA !== null && keyB !== null ? keyA === keyB : a === b;+}+/** A page as Start names it: its path on the site, or its host for the home page. */export function pagePath(url: string): string {try {diff --git a/web/src/lib/content/page-types.test.ts b/web/src/lib/content/page-types.test.tsindex c1024e02b..a64f4192c 100644--- a/web/src/lib/content/page-types.test.ts+++ b/web/src/lib/content/page-types.test.ts@@ -47,6 +47,7 @@ describe('detectPageType from the URL', () => {['/best-crm-for-startups', 'best_of'],['/glossary/lead-scoring', 'definition'],['/blog/what-is-pipeline-coverage', 'definition'],+ ['/what-is', 'definition'],['/integrations/slack', 'integration'],['/en/pricing', 'pricing'],['/guides/clean-crm-data', 'guide'],@@ -63,7 +64,20 @@ describe('detectPageType from the URL', () => {['https://shop.example/products/trail-runner-x?variant=2', 'product_page'],['/p/daypack-22l.html', 'product_page'],['/collections/ultralight-tents', 'category_page'],+ ['/collections/kitchen', 'category_page'],+ ['/collections/kitchen-appliances', 'category_page'],+ ['/collections/closets', 'category_page'],+ ['/collections/sunset-lamps', 'category_page'],+ ['/collections/limited-edition', 'category_page'],+ ['/collections/toothpicks', 'category_page'],['/collections/gifts-for-hikers', 'collection_page'],+ ['/collections/camp-kitchen-kits', 'collection_page'],+ ['/collections/gift-sets/', 'collection_page'],+ ['/collections/best-sellers', 'collection_page'],+ ['/collections/editors-picks', 'collection_page'],+ ['/collections/the-edit', 'collection_page'],+ ['/collections/for-her', 'collection_page'],+ ['/collections/tents-for-beginners', 'collection_page'],['/pages/size-guide', 'sizing_guide'],['/nespresso-alternatives', 'alternatives'],['/compare/tent-a-vs-tent-b', 'comparison'],@@ -153,7 +167,9 @@ describe('detectPageType from the page', () => {['What is single-origin coffee?', 'ecommerce', 'definition'],['Shoe size chart', 'ecommerce', 'sizing_guide'],['Gifts for coffee lovers', 'ecommerce', 'collection_page'],- ['Shipping FAQ', 'ecommerce', 'help_faq'],+ ['Shipping FAQ', 'ecommerce', 'policy'],+ ['Frequently asked questions about returns', 'ecommerce', 'policy'],+ ['Espresso machine FAQ', 'ecommerce', 'help_faq'],['CRM for real estate teams', 'b2b', 'solution_page'],['Slack integration', 'b2b', 'integration'],['How to clean CRM data', 'b2b', 'guide'],@@ -167,8 +183,26 @@ describe('detectPageType from the page', () => {expect(detectPageType({ title }, site)).toBe(type);});- it('doesn’t read “What is the best …” as a definition', () => {- expect(detectPageType({ title: 'What is the best espresso machine?' }, 'ecommerce')).toBeNull();+ it.each([+ 'What is the best espresso machine?',+ 'What is the difference between a latte and a flat white?',+ 'What are the benefits of single-origin coffee?',+ 'What is the cost of an espresso machine?',+ 'What is new in espresso this year',+ ])('doesn’t read “%s” as a definition', (title) => {+ expect(detectPageType({ title }, 'ecommerce')).toBeNull();+ });++ it.each(['/what-is-the-best-crm', '/what-is-new', '/blog/what-are-the-benefits-of-a-crm'])(+ 'doesn’t read the path %s as a definition',+ (url) => {+ expect(detectPageType({ url }, 'b2b')).not.toBe('definition');+ },+ );++ it('reads a shipping FAQ as Start reads its address: a policy page', () => {+ expect(detectPageType({ url: '/pages/shipping-faq' }, 'ecommerce')).toBe('policy');+ expect(detectPageType({ title: 'Shipping FAQ' }, 'ecommerce')).toBe('policy');});it('finds nothing when no sign tells', () => {diff --git a/web/src/lib/content/page-types.ts b/web/src/lib/content/page-types.tsindex 546d58b9f..a0b960b16 100644--- a/web/src/lib/content/page-types.ts+++ b/web/src/lib/content/page-types.ts@@ -114,6 +114,14 @@ const BUYING_GUIDE: BySite = { b2b: 'guide', ecommerce: 'buying_guide', b2c: 'guconst SIZING: BySite = { ecommerce: 'sizing_guide' };const COLLECTION: BySite = { ecommerce: 'collection_page' };+/**+ * "What is …" asks for a definition, but not "what is the best …", "what is+ * the difference between …", "what are the benefits of …" or "what is new":+ * the words after it that ask for something else.+ */+const NOT_DEFINED =+ 'best|differences?|costs?|prices?|benefits?|advantages?|disadvantages?|pros|cons|new|latest';+/*** URL paths, the most specific first: /blog/hubspot-vs-salesforce is a* comparison before it is a blog post. Matched on the lowercased path with@@ -124,7 +132,12 @@ const PATH_SIGNS: [RegExp, BySite][] = [[/\/(size-?(guides?|charts?)|sizing(-guides?)?|fit-guides?)([/-]|$)/, SIZING],[/(^|[/-])alternatives?([/-]|$)/, ALTERNATIVES],[/(^|[/-])(vs|versus)([/-]|$)|\/(compare|comparisons?)(\/|$)/, COMPARISON],- [/\/(glossary|definitions?|dictionary|what-is|what-are)([/-]|$)/, DEFINITION],+ [+ new RegExp(+ `\\/(glossary|definitions?|dictionary)([/-]|$)|\\/what-(is|are)(?!-(the-)?(${NOT_DEFINED})([/-]|$))([/-]|$)`,+ ),+ DEFINITION,+ ],[/\/(shipping|delivery|returns?|refunds?|exchanges?|warranty|polic(y|ies)|terms|privacy|legal)([/-]|$)/,POLICY,@@ -132,16 +145,18 @@ const PATH_SIGNS: [RegExp, BySite][] = [[/\/(docs|documentation|help|help-center|support|faqs?|knowledge-base|kb)(\/|$)/, FAQ],[/\/(integrations?|apps|marketplace|connectors?)\//, { b2b: 'integration' }],[/\/(buying-guides?|buyers?-guides?|how-to-choose)([/-]|$)/, BUYING_GUIDE],- [/\/(the-)?(best(?!-(practices?|time|ways?)([/-]|$))|top-?\d+)([/-]|$)/, BEST_OF],+ [/\/(the-)?(best(?!-(practices?|time|ways?|sellers?)([/-]|$))|top-?\d+)([/-]|$)/, BEST_OF],[/\/(features?|capabilities|platform)(\/|$)/, { b2b: 'feature_page' }],[/\/(solutions?|industries|industry|use-cases?|teams)(\/|$)|\/for-[a-z]/,{ b2b: 'solution_page' },],[/\/(products?|p|dp|items?)\/[^/]+/, { b2b: 'feature_page', ecommerce: 'product_page' }],- // A themed collection (gifts, bundles, "for …") before a shop's categories.+ // A themed collection (gifts, bundles, "for …") before a shop's categories,+ // on whole words of the slug: /collections/kitchen and /collections/limited-edition+ // are categories.[- /\/collections?\/[^/]*(gifts?|bundles?|kits?|sets?|best-?sellers?|new-arrivals?|for-[a-z]|-for-|picks|edit)/,+ /\/collections?\/(?:[^/]*-)?(gifts?|bundles?|kits?|sets?|best-?sellers?|new-arrivals?|picks|edit)(?:-|\/|$)|\/collections?\/(?:[^/]*-)?for-[a-z]/,COLLECTION,],[@@ -194,11 +209,23 @@ const TITLE_SIGNS: [RegExp, BySite][] = [[/\b(vs\.?|versus)\s/i, COMPARISON],[/\bpricing\b/i, { b2b: 'pricing' }],[/\bpolic(y|ies)\b/i, POLICY],+ // "Shipping FAQ" is a policy page, as /pages/shipping-faq is: the policy+ // words before FAQ, in the paths' order.+ [+ /^(?=.*\b(faqs?|frequently asked)\b).*\b(shipping|delivery|returns?|refunds?|exchanges?|warranty|terms|privacy|legal)\b/i,+ POLICY,+ ],[/\bfaqs?\b|\bfrequently asked\b/i, FAQ],[/\bbuying guide\b|\bhow to choose\b/i, BUYING_GUIDE],[/^(the\s+)?(\d+\s+)?best\b(?!\s+(time|way|practices?)\b)|\btop\s+\d+\b/i, BEST_OF],- // "What is single-origin coffee?", not "What is the best …".- [/^what\s+(is|are)\s+(?!the\s+best\b)|\b(definition|meaning) of\b|\bglossary\b/i, DEFINITION],+ // "What is single-origin coffee?", not "What is the best …" (NOT_DEFINED).+ [+ new RegExp(+ `^what\\s+(is|are)\\s+(?!(the\\s+)?(${NOT_DEFINED})\\b)|\\b(definition|meaning) of\\b|\\bglossary\\b`,+ 'i',+ ),+ DEFINITION,+ ],[/\bgifts? for\b|\bcollection\b/i, COLLECTION],[/\b\d+[-\s]days?\b|\bitinerar(y|ies)\b/i, { b2c: 'itinerary' }],[/\bintegrations?\b|\bintegrates? with\b/i, { b2b: 'integration' }],diff --git a/web/src/lib/content/plan-mix.ts b/web/src/lib/content/plan-mix.tsindex b9aa7733e..ec204e7e6 100644--- a/web/src/lib/content/plan-mix.ts+++ b/web/src/lib/content/plan-mix.ts@@ -5,7 +5,7 @@* that type of page, and the share of the brand's own cited pages' citations* that type holds. A type AI leans on 10 points or more beyond the brand's* own mix is a gap: AI cites it, the brand's cited pages don't cover it. The- * addresses come from content_plan_insights (00221): every own page and the+ * addresses come from content_plan_insights (00221): the own pages and the* others cited most; their type is read from the address and title, as the* editor detects a page's (page-types.ts).*/diff --git a/web/src/lib/content/rules.test.ts b/web/src/lib/content/rules.test.tsindex d185620e1..678c60fc1 100644--- a/web/src/lib/content/rules.test.ts+++ b/web/src/lib/content/rules.test.ts@@ -368,6 +368,41 @@ describe('the formats www.ansvisor.com offers', () => {state: 'bad',code: 'missing',});+ // "is" somewhere in the paragraph isn't a definition: a pronoun's, or past the first words.+ expect(define('<h1>Cold brew</h1><p>Many people say it is great.</p>')).toEqual({+ state: 'bad',+ code: 'missing',+ });+ expect(+ define(+ '<h1>Cold brew</h1><p>On hot summer days in the city many cafés sell what is cold brew.</p>',+ ),+ ).toEqual({ state: 'bad', code: 'missing' });+ expect(+ define('<h1>Cold brew</h1><p>Cold brew. Summer drinkers say it is the best way.</p>'),+ ).toEqual({ state: 'bad', code: 'missing' });+ expect(+ define('<h1>CRM</h1><p>Customer relationship management (CRM) refers to tools that…</p>'),+ ).toEqual({ state: 'ok' });+ // Punctuation beside the verb doesn't hide it.+ expect(define('<h1>Cold brew</h1><p>Cold brew is, simply, coffee steeped cold.</p>')).toEqual({+ state: 'ok',+ });+ // Read in English words only: another language's page isn't counted.+ expect(+ checkRule(+ rule('definitionFirst', 'definition'),+ page('<h1>Soğuk demleme</h1><p>Soğuk demleme, soğuk suyla demlenen kahvedir.</p>'),+ { ...ctx, language: 'tr' },+ ),+ ).toEqual({ state: 'info', code: 'englishOnly' });+ expect(+ checkRule(+ rule('definitionFirst', 'definition'),+ page('<h1>Cold brew</h1><p>Cold brew is coffee steeped in cold water.</p>'),+ { ...ctx, language: 'en' },+ ),+ ).toEqual({ state: 'ok' });expect(define(`<h1>Cold brew</h1><p>Cold brew is ${'very '.repeat(60)}cold.</p>`)).toEqual({state: 'bad',code: 'words',@@ -380,7 +415,21 @@ describe('the formats www.ansvisor.com offers', () => {it('checks a how-to guide’s and a sizing guide’s steps, and a size chart', () => {const steps = '<ol><li>a</li><li>b</li><li>c</li></ol>';- expect(check('howToSteps', steps, undefined, 'guide')).toEqual({ state: 'ok' });+ expect(+ check('howToSteps', `<h1>How to make cold brew</h1>${steps}`, undefined, 'guide'),+ ).toEqual({ state: 'ok' });+ expect(+ check(+ 'howToSteps',+ '<h1>Cold brew in 5 easy steps</h1><ol><li>a</li></ol>',+ undefined,+ 'guide',+ ),+ ).toEqual({ state: 'bad', code: 'partOf', count: 1, of: 3 });+ // A guide that isn't a how-to has no steps to count.+ expect(+ check('howToSteps', '<h1>The Azores in winter</h1><p>Mild.</p>', undefined, 'guide'),+ ).toEqual({ state: 'info', code: 'notHowTo' });expect(check('measureSteps', '<ol><li>a</li></ol>', undefined, 'sizing_guide')).toEqual({state: 'bad',code: 'partOf',diff --git a/web/src/lib/content/rules.ts b/web/src/lib/content/rules.tsindex 4194472ac..33e0b81bf 100644--- a/web/src/lib/content/rules.ts+++ b/web/src/lib/content/rules.ts@@ -240,7 +240,6 @@ const TYPE_RULES: Partial<Record<PageType, RuleDef[]>> = {],};-/** The rules of a set: the type's own, then the shared groups, with the type's length. *//*** The Language rule's value for the brand's language (brands.language, as* the brief and the agent write in): its name, US English for English or@@ -251,6 +250,7 @@ export function languageRuleValue(code: string | null | undefined): string {return label ? label.replace(/\s*\([^)]*\)$/, '') : 'English (US)';}+/** The rules of a set: the type's own, then the shared groups, with the type's length. */export function ruleSet(pageType: PageType | null,defaults: { keywords?: string[]; language?: string | null } = {},@@ -480,13 +480,18 @@ export type RuleStatus =| { state: 'bad'; code: 'current'; count: number }| { state: 'bad'; code: 'words'; count: number }| { state: 'bad'; code: 'missing' | 'noH2' | 'needsFaq' | 'notSet' }- | { state: 'info'; code: 'aiCheck' | 'needsFeed' | 'noFanouts' | 'addKeywords' };+ | {+ state: 'info';+ code: 'aiCheck' | 'needsFeed' | 'noFanouts' | 'addKeywords' | 'notHowTo' | 'englishOnly';+ };export interface RuleContext {brandDomains: string[];competitorDomains: string[];/** The fan-outs the draft keeps, the most searched first. */fanouts: string[];+ /** The brand's language (brands.language); none reads as English. */+ language?: string | null;}/** Phrases that read as machine-written (spec §10, "Remove AI traces"). */@@ -556,6 +561,34 @@ function faqQuestions(blocks: PageBlock[]): string[] | null {return questions;}+/** A title that promises steps: "How to …", "… step by step", a tutorial. */+const HOW_TO_TITLE =+ /\bhow to\b|\bstep[-\s]by[-\s]step\b|\btutorial\b|\b\d+\s+(easy\s+|simple\s+)?steps\b/i;++/** How far into its first sentence a definition names what the term is. */+const DEFINING_WORDS = 8;++/** Subjects that make "… is …" a remark, not a definition: "Many say it is great." */+const PRONOUNS = /^(it|this|that|these|those|there|which|who|he|she|they|we|you|i)$/i;++/**+ * "X is …": the first sentence says what its subject is, means or refers to+ * within its first few words, and the subject isn't a pronoun.+ */+function definesAtOnce(text: string): boolean {+ const sentence = text.trim().split(/(?<=[.!?])\s/)[0];+ // Words without their punctuation: "Cold brew is, simply, …" defines too.+ const words = sentence+ .split(/\s+/)+ .slice(0, DEFINING_WORDS)+ .map((w) => w.replace(/[^\p{L}]/gu, ''));+ const verb = words.findIndex(+ (w, i) =>+ /^(is|are|means)$/i.test(w) || (/^refers$/i.test(w) && /^to$/i.test(words[i + 1] ?? '')),+ );+ return verb > 0 && !PRONOUNS.test(words[verb - 1]);+}+const atLeast = (have: number, want: number): RuleStatus =>have >= want ? { state: 'ok' } : { state: 'bad', code: 'partOf', count: have, of: want };@@ -679,6 +712,10 @@ export function checkRule(rule: ActiveRule, page: PageRead, ctx: RuleContext): Rcase 'useCaseSteps':case 'howToSteps':case 'measureSteps': {+ // A guide that isn't a how-to (a travel guide, a resource page) has no steps to count.+ if (rule.key === 'howToSteps' && !HOW_TO_TITLE.test(blocks[h1]?.text ?? '')) {+ return { state: 'info', code: 'notHowTo' };+ }const most = Math.max(0,...blocks.filter((b) => b.tag.startsWith('ol:')).map((b) => Number(b.tag.slice(3))),@@ -699,11 +736,11 @@ export function checkRule(rule: ActiveRule, page: PageRead, ctx: RuleContext): Rreturn words >= n ? { state: 'ok' } : { state: 'bad', code: 'current', count: words };}case 'definitionFirst':- // The term defined at once: "X is …" in a short first paragraph.+ // The term defined at once: "X is …" in a short first paragraph. Read+ // in English words only, so another language's page isn't counted.+ if (ctx.language && ctx.language !== 'en') return { state: 'info', code: 'englishOnly' };if (!summary || !summary.words) return { state: 'bad', code: 'missing' };- if (!/\b(is|are|refers to|means)\b/i.test(summary.text)) {- return { state: 'bad', code: 'missing' };- }+ if (!definesAtOnce(summary.text)) return { state: 'bad', code: 'missing' };return summary.words <= n? { state: 'ok' }: { state: 'bad', code: 'words', count: summary.words };diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 57a88b64a..cfe59c10e 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -830,6 +830,7 @@ export type Database = {organization_id: stringoverrides: Jsonpage_type: string+ site_type: stringtext_rules: Jsonupdated_at: stringupdated_by: string | null@@ -838,6 +839,7 @@ export type Database = {organization_id: stringoverrides?: Jsonpage_type: string+ site_type: stringtext_rules?: Jsonupdated_at?: stringupdated_by?: string | null@@ -846,6 +848,7 @@ export type Database = {organization_id?: stringoverrides?: Jsonpage_type?: string+ site_type?: stringtext_rules?: Jsonupdated_at?: stringupdated_by?: string | null