mirror of
https://github.com/citeworthyio/seo-agent.git
synced 2026-08-11 13:22:39 +02:00
Two host-facing senses grow up together. The crawl now stores each page's
JS-free body text (PageSnapshot.text, capped at MAX_SNAPSHOT_TEXT=4000) and
an uncapped word_count — exactly what GPTBot/ClaudeBot see, persisted so
downstream fact-extraction and thin-page senses read stored truth instead of
refetching. schema.sql gains the two nullable columns; existing installs run
the two ALTERs documented in README's 'Upgrading an existing install'.
And AgentDeps gains twinned — a set of page paths the host currently serves
a readable twin for, read only by ai_page_body_empty. The measurement (raw
HTML has no visible body text) stays true of a twinned page; the claim ('AI
crawlers see an empty page here') does not, and only the host knows that. The
path stays sampled — blocked/error senses keep measuring — but the body-empty
finding is withheld at trigger time, so a previously open row auto-resolves
via runRules' existing lifecycle instead of being re-opened and host-resolved
every audit. Absent (every self-hosted deployment) nothing changes anywhere.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
213 lines
9.8 KiB
SQL
213 lines
9.8 KiB
SQL
-- seo-agent D1 schema. Apply with: npm run db:init
|
||
|
||
CREATE TABLE IF NOT EXISTS crawl_runs (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
started_at TEXT NOT NULL,
|
||
finished_at TEXT, -- set when the crawl phase completes
|
||
url_count INTEGER,
|
||
ok INTEGER DEFAULT 0, -- 1 when the crawl succeeded (baseline-eligible)
|
||
pipeline_done INTEGER DEFAULT 0 -- 1 when the WHOLE run (rules+proposals+gsc) finished; drives the in-progress guard
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS page_snapshots (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
run_id INTEGER NOT NULL,
|
||
path TEXT NOT NULL,
|
||
status INTEGER NOT NULL,
|
||
title TEXT,
|
||
description TEXT,
|
||
canonical TEXT,
|
||
og_image TEXT,
|
||
og_type TEXT,
|
||
jsonld_types TEXT, -- comma-separated @type values seen on the page
|
||
noindex INTEGER DEFAULT 0,
|
||
lastmod TEXT, -- from the sitemap entry, when present
|
||
error TEXT,
|
||
fetched_at TEXT NOT NULL,
|
||
-- The page's readable body text AS A PLAIN FETCH DELIVERS IT — no JavaScript
|
||
-- executed, which is exactly what an AI crawler (GPTBot/ClaudeBot) receives.
|
||
-- Whitespace-collapsed, word-boundary trimmed to 4000 chars (MAX_SNAPSHOT_TEXT
|
||
-- in crawl.ts). NULL means the delivered HTML carried no readable text at all —
|
||
-- a JS shell — which is a finding, not a gap. NULL also on non-HTML responses,
|
||
-- errors, and on any row written before this column existed.
|
||
text TEXT,
|
||
-- Words in the FULL text, counted BEFORE the 4000-char cap, so a long page's
|
||
-- count is honest and "thin content" is never an artifact of the cap. 0 for a
|
||
-- JS shell; NULL (not 0) on rows that predate the column — read it as
|
||
-- "unknown", and check `text IS NULL AND word_count IS NULL` before concluding
|
||
-- anything about an old row.
|
||
word_count INTEGER
|
||
);
|
||
-- UPGRADING AN EXISTING DATABASE (added v1.27.0): `npm run db:init` is
|
||
-- CREATE-only by design, so it will NOT add page_snapshots.text / .word_count to
|
||
-- a table that already exists — and SQLite has no `ADD COLUMN IF NOT EXISTS`, so
|
||
-- putting the ALTERs here unconditionally would make db:init fail on its second
|
||
-- run and cost this file its idempotency. Run them ONCE by hand instead; a
|
||
-- "duplicate column name" error means you already have them and is safe:
|
||
-- wrangler d1 execute seo-agent-db --remote \
|
||
-- --command "ALTER TABLE page_snapshots ADD COLUMN text TEXT"
|
||
-- wrangler d1 execute seo-agent-db --remote \
|
||
-- --command "ALTER TABLE page_snapshots ADD COLUMN word_count INTEGER"
|
||
-- Both are additive and nullable: every existing row reads NULL, and no query
|
||
-- that predates them changes behaviour.
|
||
CREATE INDEX IF NOT EXISTS idx_snapshots_run ON page_snapshots (run_id);
|
||
CREATE INDEX IF NOT EXISTS idx_snapshots_path ON page_snapshots (path);
|
||
|
||
CREATE TABLE IF NOT EXISTS findings (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
created_at TEXT NOT NULL,
|
||
run_id INTEGER NOT NULL,
|
||
path TEXT NOT NULL,
|
||
rule TEXT NOT NULL,
|
||
severity TEXT NOT NULL, -- info | low | medium | high | critical
|
||
detail TEXT,
|
||
-- open = the condition is currently triggering.
|
||
-- resolved = auto-closed by a later crawl when the condition cleared (or a
|
||
-- restored dismissal, re-openable on the next crawl).
|
||
-- dismissed = a human muted it: it leaves the open list AND future crawls
|
||
-- skip re-opening the same (path, rule) until it is restored (a
|
||
-- (path, rule) is muted iff its MOST RECENT row is 'dismissed').
|
||
-- Comment-enum, NOT a CHECK constraint (like every status column here) — so
|
||
-- adding 'dismissed' needs no migration; a fresh db:init just re-states it.
|
||
status TEXT NOT NULL DEFAULT 'open', -- open | resolved | dismissed
|
||
-- Generic closed-at: the timestamp the row left 'open', whether by auto-resolve
|
||
-- OR by a dismissal. openFindingsSeries / the open counts treat it as the close
|
||
-- date, so a dismissed finding correctly drops out of "open" at dismissal time.
|
||
resolved_at TEXT
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_findings_open ON findings (status, path, rule);
|
||
|
||
CREATE TABLE IF NOT EXISTS proposals (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
created_at TEXT NOT NULL,
|
||
finding_id INTEGER,
|
||
path TEXT NOT NULL,
|
||
field TEXT NOT NULL, -- description | title
|
||
current_value TEXT,
|
||
proposed_value TEXT NOT NULL,
|
||
rationale TEXT,
|
||
model TEXT,
|
||
status TEXT NOT NULL DEFAULT 'proposed', -- proposed | approved | rejected | reverted
|
||
decided_at TEXT,
|
||
applied_at TEXT
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_proposals_status ON proposals (status);
|
||
|
||
-- Journal of every override applied to the live site (and reverts).
|
||
CREATE TABLE IF NOT EXISTS changes (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
applied_at TEXT NOT NULL,
|
||
path TEXT NOT NULL,
|
||
field TEXT NOT NULL,
|
||
old_value TEXT,
|
||
new_value TEXT NOT NULL,
|
||
source TEXT NOT NULL, -- proposal | auto | manual
|
||
proposal_id INTEGER,
|
||
reverted_at TEXT
|
||
);
|
||
|
||
-- Search Console daily metrics (page+query grain). Dormant until the
|
||
-- GSC_SERVICE_ACCOUNT_JSON secret is configured.
|
||
CREATE TABLE IF NOT EXISTS gsc_daily (
|
||
date TEXT NOT NULL,
|
||
page TEXT NOT NULL,
|
||
query TEXT NOT NULL,
|
||
clicks INTEGER NOT NULL,
|
||
impressions INTEGER NOT NULL,
|
||
ctr REAL NOT NULL,
|
||
position REAL NOT NULL,
|
||
PRIMARY KEY (date, page, query)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_gsc_page ON gsc_daily (page, date);
|
||
|
||
-- AEO telemetry — AI-relevant hits written directly by the SITE's injector /
|
||
-- edge Worker (optional TELEMETRY / AEO_TELEMETRY D1 binding, fire-and-forget
|
||
-- via waitUntil, fail-open). The agent reads, aggregates, and prunes (90 days).
|
||
-- kind: 'crawler' = known AI-bot UA; 'referral' = human arriving from an AI
|
||
-- engine (Referer); 'search' = human arriving from an organic search engine
|
||
-- (Referer — Google/Bing/DDG/…); 'agent' = unknown client that negotiated
|
||
-- markdown. Both lists are matched against the referring HOST, anchored (never
|
||
-- against the full URL). A HOST on both lists (gemini.google.com,
|
||
-- copilot.microsoft.com) is always 'referral': the AI check runs first and
|
||
-- wins. Same-site referrers are never recorded.
|
||
-- THE LIMIT OF THIS GUARANTEE, stated because the column cannot: an AI surface
|
||
-- answering from the SAME HOST as that engine's web search is indistinguishable
|
||
-- here and lands in 'search'. That covers Google AI Mode / AI Overviews
|
||
-- (www.google.com), Bing Copilot's in-page answers (www.bing.com), DuckDuckGo's
|
||
-- AI chat and Kagi's assistant. Only the query string could separate them and
|
||
-- we do not store it (see below). 'referral' is therefore a FLOOR on AI
|
||
-- traffic, not a total — do not read this column as "all AI traffic".
|
||
-- referrer: the referring HOSTNAME only — never the path or query string, so
|
||
-- no search terms and no referrer-borne personal data are ever stored.
|
||
CREATE TABLE IF NOT EXISTS aeo_hits (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
ts TEXT NOT NULL,
|
||
kind TEXT NOT NULL,
|
||
bot TEXT,
|
||
referrer TEXT,
|
||
path TEXT NOT NULL,
|
||
status INTEGER,
|
||
served TEXT, -- 'html' | 'md' (markdown twin) | 'file'; 'lane' (AI content lane) is written by a site-custom tap, not this repo's injector
|
||
ua TEXT
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_aeo_hits_ts ON aeo_hits (ts);
|
||
CREATE INDEX IF NOT EXISTS idx_aeo_hits_bot ON aeo_hits (bot, ts);
|
||
|
||
-- Per-change SEO impact (analytics). One row per (change, phase): the d14/d28
|
||
-- before/after GSC comparison for the changed page, plus a helped/hurt verdict.
|
||
-- before_*/after_clicks and before_*/after_impressions are stored as PER-DAY
|
||
-- rates (total ÷ days-with-data) so unequal effective windows still compare;
|
||
-- ctr/position are impression-weighted. Populated by the impact sense once a
|
||
-- change is old enough for GSC to have covered its after-window.
|
||
CREATE TABLE IF NOT EXISTS change_impact (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
change_id INTEGER NOT NULL,
|
||
phase TEXT NOT NULL CHECK (phase IN ('d14', 'd28')),
|
||
computed_at TEXT NOT NULL,
|
||
before_clicks REAL,
|
||
after_clicks REAL,
|
||
before_impressions REAL,
|
||
after_impressions REAL,
|
||
before_ctr REAL,
|
||
after_ctr REAL,
|
||
before_position REAL,
|
||
after_position REAL,
|
||
verdict TEXT NOT NULL, -- helped | hurt | neutral | insufficient_data
|
||
UNIQUE (change_id, phase)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_change_impact_change ON change_impact (change_id);
|
||
|
||
-- Write-once weekly rollups of AI-traffic telemetry. A completed ISO week
|
||
-- (Mon-start, UTC) is rolled up while all its hits are still inside the 90-day
|
||
-- aeo_hits retention; that first INSERT is final (OR IGNORE — never REPLACEd),
|
||
-- so the rollup survives the later prune untouched. Weeks already partially
|
||
-- pruned when the feature first runs are skipped, not frozen wrong. One row per
|
||
-- (week_start, kind, bot, served); bot/served are coalesced to '' (never NULL)
|
||
-- so the UNIQUE key actually dedupes — SQLite treats NULLs as distinct.
|
||
CREATE TABLE IF NOT EXISTS aeo_weekly (
|
||
week_start TEXT NOT NULL, -- ISO Monday, YYYY-MM-DD, UTC
|
||
kind TEXT NOT NULL,
|
||
bot TEXT,
|
||
served TEXT NOT NULL,
|
||
hits INTEGER NOT NULL,
|
||
UNIQUE (week_start, kind, bot, served)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_aeo_weekly_week ON aeo_weekly (week_start);
|
||
|
||
-- Citation probes — periodic checks of whether AI answer engines cite the
|
||
-- site for configured queries (CITATION_QUERIES). One row per engine × query
|
||
-- per probe batch (checked_at identifies the batch).
|
||
CREATE TABLE IF NOT EXISTS citations (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
checked_at TEXT NOT NULL,
|
||
engine TEXT NOT NULL, -- gemini | perplexity | openai | anthropic
|
||
query TEXT NOT NULL,
|
||
cited INTEGER NOT NULL, -- 1 = the site appeared in the answer's sources
|
||
rank INTEGER, -- 1-based position of the first matching source
|
||
cited_url TEXT,
|
||
total_sources INTEGER,
|
||
sources TEXT, -- JSON array of {url, domain, title}, truncated
|
||
error TEXT
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_citations_key ON citations (engine, query, checked_at);
|