Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(purge): purge ledgers, derivation edges and purged_content guards (migration v232, provisional)

garrytan91a33ed1cb3e
diff --git a/scripts/build-schema.ts b/scripts/build-schema.ts
index 5c46a8a7d5..df39af6e9a 100644
--- a/scripts/build-schema.ts
+++ b/scripts/build-schema.ts
@@ -41,6 +41,7 @@ import { SHARED_SKILLS_SCHEMA_SQL } from '../src/core/shared-skills/schema-all.t
import { DECIDE_SCHEMA_SQL } from '../src/core/ai/decide/schema.ts';
import { FACT_RELINK_SCHEMA_SQL } from '../src/core/facts/relink-schema.ts';
import { CORE_EDIT_NOTICES_SCHEMA_SQL } from '../src/core/core-memory-schema.ts';
+import { MEMORY_PURGE_SCHEMA_SQL } from '../src/core/facts/purge-schema.ts';
const REPO = resolve(import.meta.dir, '..');
export const SCHEMA_SQL_PATH = 'src/schema.sql';
@@ -99,6 +100,7 @@ export const FRAGMENTS: readonly Fragment[] = [
{ source: 'src/core/facts/relink-schema.ts', expr: 'FACT_RELINK_SCHEMA_SQL', postgres: FACT_RELINK_SCHEMA_SQL, pglite: FACT_RELINK_SCHEMA_SQL },
{ source: 'src/core/link-temporal-schema.ts', expr: 'LINK_TEMPORAL_SCHEMA_SQL', postgres: LINK_TEMPORAL_SCHEMA_SQL, pglite: LINK_TEMPORAL_SCHEMA_SQL },
{ source: 'src/core/core-memory-schema.ts', expr: 'CORE_EDIT_NOTICES_SCHEMA_SQL', postgres: CORE_EDIT_NOTICES_SCHEMA_SQL, pglite: CORE_EDIT_NOTICES_SCHEMA_SQL },
+ { source: 'src/core/facts/purge-schema.ts', expr: 'MEMORY_PURGE_SCHEMA_SQL', postgres: MEMORY_PURGE_SCHEMA_SQL, pglite: MEMORY_PURGE_SCHEMA_SQL },
];
const fragmentLabel = (f: Fragment) => `${f.source} (${f.expr})`;
diff --git a/src/core/facts/purge-schema.ts b/src/core/facts/purge-schema.ts
new file mode 100644
index 0000000000..0585eb7ace
--- /dev/null
+++ b/src/core/facts/purge-schema.ts
@@ -0,0 +1,146 @@
+/**
+ * Purge ledgers and the resurrection guards that read them. One canonical copy
+ * used by the schema migration and, through scripts/build-schema.ts FRAGMENTS,
+ * by fresh-install DDL.
+ *
+ * - `fact_purges`: a text-free tombstone per purged claim fingerprint
+ * (gbrain_fact_fingerprint), keyed like `fact_withdrawals`. The facts
+ * guard raises typed `purged_content` for a matching insert or claim
+ * update, whatever its expired_at, so `INSERT ... RETURNING` callers never
+ * see zero rows silently.
+ * - `take_purges`: the same tombstone for verbatim take copies; canonical
+ * projections and the takes guard consult it, so a stale takes fence cannot
+ * re-project a purged claim.
+ * - `page_purges`: `gbrain delete <slug> --purge` records the purged page's
+ * content hash. A page write whose content hash matches, under any slug,
+ * raises `purged_content`; `gbrain pages unpurge` clears it.
+ * - `derivation_inputs`: complete row -> input edges written by every deriver
+ * (`recordDerivationInputs`). Purge walks them transitively to hide derived
+ * rows and mark them `needs_rederive` before cascades delete the evidence.
+ * - `needs_rederive`: derived rows a purge hid; a deriver regenerates them from
+ * surviving inputs and deletes the marker.
+ *
+ * A fingerprint of a short claim is guessable; the ledger proves a claim was
+ * purged to someone who already knows it, never what it said.
+ */
+
+const RLS = (table: string) => `DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE ${table} ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;`;
+
+export const MEMORY_PURGE_SCHEMA_SQL = `
+CREATE TABLE IF NOT EXISTS fact_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ visibility TEXT NOT NULL CHECK (visibility IN ('private','world')),
+ subject TEXT NOT NULL DEFAULT '*',
+ fact_hash TEXT NOT NULL,
+ request_id UUID,
+ actor TEXT,
+ reason TEXT,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, visibility, subject, fact_hash)
+);
+${RLS('fact_purges')}
+CREATE TABLE IF NOT EXISTS take_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ subject TEXT NOT NULL DEFAULT '*',
+ claim_hash TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, subject, claim_hash)
+);
+${RLS('take_purges')}
+CREATE TABLE IF NOT EXISTS page_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ content_hash TEXT NOT NULL,
+ slug TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, content_hash)
+);
+${RLS('page_purges')}
+CREATE TABLE IF NOT EXISTS derivation_inputs (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ input_table TEXT NOT NULL,
+ input_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id, input_table, input_id)
+);
+CREATE INDEX IF NOT EXISTS idx_derivation_inputs_input ON derivation_inputs (input_table, input_id);
+${RLS('derivation_inputs')}
+CREATE TABLE IF NOT EXISTS needs_rederive (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ request_id UUID,
+ reason TEXT NOT NULL,
+ marked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id)
+);
+${RLS('needs_rederive')}
+`;
+
+/**
+ * The guards. Installed by the migration only (facts and takes are created by
+ * migrations, not schema.sql). Each serializes with a purge through a shared
+ * lock on the source row, which the purge holds FOR UPDATE while it writes the
+ * ledger and deletes the rows.
+ */
+export const MEMORY_PURGE_GUARD_SQL = `
+CREATE OR REPLACE FUNCTION gbrain_refuse_purged_fact() RETURNS trigger
+ LANGUAGE plpgsql SET search_path = pg_catalog, public AS $fn$
+ BEGIN
+ PERFORM id FROM sources WHERE id = NEW.source_id FOR SHARE;
+ IF EXISTS (SELECT 1 FROM fact_purges
+ WHERE source_id = NEW.source_id AND visibility = NEW.visibility
+ AND (subject = '*' OR subject = NEW.entity_slug)
+ AND fact_hash = gbrain_fact_fingerprint(NEW.fact)) THEN
+ RAISE EXCEPTION USING ERRCODE = 'P0001',
+ MESSAGE = 'purged_content: this claim was purged from this source and cannot be saved again',
+ DETAIL = json_build_object('table', 'facts', 'hash8', left(gbrain_fact_fingerprint(NEW.fact), 8))::text;
+ END IF;
+ RETURN NEW;
+ END
+ $fn$;
+DROP TRIGGER IF EXISTS facts_refuse_purged ON facts;
+CREATE TRIGGER facts_refuse_purged BEFORE INSERT OR UPDATE OF fact, source_id, visibility, entity_slug ON facts
+ FOR EACH ROW EXECUTE FUNCTION gbrain_refuse_purged_fact();
+CREATE OR REPLACE FUNCTION gbrain_refuse_purged_take() RETURNS trigger
+ LANGUAGE plpgsql SET search_path = pg_catalog, public AS $fn$
+ DECLARE src TEXT; subj TEXT;
+ BEGIN
+ SELECT p.source_id, p.slug INTO src, subj FROM pages p WHERE p.id = NEW.page_id;
+ IF src IS NOT NULL AND EXISTS (SELECT 1 FROM take_purges
+ WHERE source_id = src AND (subject = '*' OR subject = subj)
+ AND claim_hash = gbrain_fact_fingerprint(NEW.claim)) THEN
+ RAISE EXCEPTION USING ERRCODE = 'P0001',
+ MESSAGE = 'purged_content: this take claim was purged from this source and cannot be saved again',
+ DETAIL = json_build_object('table', 'takes', 'hash8', left(gbrain_fact_fingerprint(NEW.claim), 8))::text;
+ END IF;
+ RETURN NEW;
+ END
+ $fn$;
+DROP TRIGGER IF EXISTS takes_refuse_purged ON takes;
+CREATE TRIGGER takes_refuse_purged BEFORE INSERT OR UPDATE OF claim, page_id ON takes
+ FOR EACH ROW EXECUTE FUNCTION gbrain_refuse_purged_take();
+CREATE OR REPLACE FUNCTION gbrain_refuse_purged_page() RETURNS trigger
+ LANGUAGE plpgsql SET search_path = pg_catalog, public AS $fn$
+ BEGIN
+ IF NEW.content_hash IS NOT NULL
+ AND (TG_OP = 'INSERT' OR NEW.content_hash IS DISTINCT FROM OLD.content_hash OR NEW.source_id IS DISTINCT FROM OLD.source_id)
+ AND EXISTS (SELECT 1 FROM page_purges WHERE source_id = NEW.source_id AND content_hash = NEW.content_hash) THEN
+ RAISE EXCEPTION USING ERRCODE = 'P0001',
+ MESSAGE = 'purged_content: this page content was purged from this source and cannot be saved again',
+ DETAIL = json_build_object('table', 'pages', 'hash8', left(NEW.content_hash, 8))::text;
+ END IF;
+ RETURN NEW;
+ END
+ $fn$;
+DROP TRIGGER IF EXISTS pages_refuse_purged ON pages;
+CREATE TRIGGER pages_refuse_purged BEFORE INSERT OR UPDATE OF content_hash, source_id ON pages
+ FOR EACH ROW EXECUTE FUNCTION gbrain_refuse_purged_page();
+`;
diff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.ts
index d2c8054a97..e97ba8853d 100644
--- a/src/core/pglite-schema.generated.ts
+++ b/src/core/pglite-schema.generated.ts
@@ -2053,6 +2053,79 @@ DO \$rls\$ BEGIN
END IF;
END \$rls\$;
+-- The purge guards (facts, takes, pages triggers) are installed by the memory_purge migration.
+CREATE TABLE IF NOT EXISTS fact_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ visibility TEXT NOT NULL CHECK (visibility IN ('private','world')),
+ subject TEXT NOT NULL DEFAULT '*',
+ fact_hash TEXT NOT NULL,
+ request_id UUID,
+ actor TEXT,
+ reason TEXT,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, visibility, subject, fact_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE fact_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS take_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ subject TEXT NOT NULL DEFAULT '*',
+ claim_hash TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, subject, claim_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE take_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS page_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ content_hash TEXT NOT NULL,
+ slug TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, content_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE page_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS derivation_inputs (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ input_table TEXT NOT NULL,
+ input_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id, input_table, input_id)
+);
+CREATE INDEX IF NOT EXISTS idx_derivation_inputs_input ON derivation_inputs (input_table, input_id);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE derivation_inputs ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS needs_rederive (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ request_id UUID,
+ reason TEXT NOT NULL,
+ marked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE needs_rederive ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+
`;
diff --git a/src/core/schema-embedded.generated.ts b/src/core/schema-embedded.generated.ts
index 4566433c96..9972cde569 100644
--- a/src/core/schema-embedded.generated.ts
+++ b/src/core/schema-embedded.generated.ts
@@ -2794,6 +2794,81 @@ DO \$rls\$ BEGIN
END \$rls\$;
-- END GENERATED from src/core/core-memory-schema.ts (CORE_EDIT_NOTICES_SCHEMA_SQL)
+-- The purge guards (facts, takes, pages triggers) are installed by the memory_purge migration.
+-- BEGIN GENERATED from src/core/facts/purge-schema.ts (MEMORY_PURGE_SCHEMA_SQL). Edit that file, then run: bun run build:schema
+CREATE TABLE IF NOT EXISTS fact_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ visibility TEXT NOT NULL CHECK (visibility IN ('private','world')),
+ subject TEXT NOT NULL DEFAULT '*',
+ fact_hash TEXT NOT NULL,
+ request_id UUID,
+ actor TEXT,
+ reason TEXT,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, visibility, subject, fact_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE fact_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS take_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ subject TEXT NOT NULL DEFAULT '*',
+ claim_hash TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, subject, claim_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE take_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS page_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ content_hash TEXT NOT NULL,
+ slug TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, content_hash)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE page_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS derivation_inputs (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ input_table TEXT NOT NULL,
+ input_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id, input_table, input_id)
+);
+CREATE INDEX IF NOT EXISTS idx_derivation_inputs_input ON derivation_inputs (input_table, input_id);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE derivation_inputs ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+CREATE TABLE IF NOT EXISTS needs_rederive (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ request_id UUID,
+ reason TEXT NOT NULL,
+ marked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id)
+);
+DO \$rls\$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE needs_rederive ENABLE ROW LEVEL SECURITY;
+ END IF;
+END \$rls\$;
+-- END GENERATED from src/core/facts/purge-schema.ts (MEMORY_PURGE_SCHEMA_SQL)
+
-- #5255/#5176 (O-DX-8): last upstream observation per source, recorded by sync
-- from the checkout's Git state (upstream ref, its last fetch/push time, commits
-- the synced commit lacks); doctor sync_freshness reads it with no subprocess.
diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.ts
index 54cf78e271..e83033bf93 100644
--- a/src/core/schema-migrations/registry.generated.ts
+++ b/src/core/schema-migrations/registry.generated.ts
@@ -220,6 +220,7 @@ import { v216 } from './v216-chronicle-campaign-stamps.ts';
import { v217 } from './v217-persistence-request-claim-phase.ts';
import { v218 } from './v218-purge-legacy-worktree-manifest-files.ts';
import { v219 } from './v219-pages-reconcile-name-indexes.ts';
+import { v232 } from './v232-memory-purge.ts';
// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); the
// runner sorts by version before applying.
@@ -438,4 +439,5 @@ export const MIGRATIONS: Migration[] = [
v217,
v218,
v219,
+ v232,
];
diff --git a/src/core/schema-migrations/v232-memory-purge.ts b/src/core/schema-migrations/v232-memory-purge.ts
new file mode 100644
index 0000000000..8aa2fcf02e
--- /dev/null
+++ b/src/core/schema-migrations/v232-memory-purge.ts
@@ -0,0 +1,17 @@
+import type { Migration } from './types.ts';
+import { MEMORY_PURGE_GUARD_SQL, MEMORY_PURGE_SCHEMA_SQL } from '../facts/purge-schema.ts';
+
+// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md
+// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.
+//
+// `forget --purge` and page purge (#5575 Part C): text-free purge ledgers
+// (fact_purges, take_purges, page_purges), the derivation edge table purge
+// walks to hide derived rows (derivation_inputs, needs_rederive), and the
+// guards that raise typed purged_content when purged content is written
+// again. New empty tables, so their indexes build inline on both engines.
+export const v232: Migration = {
+ version: 232,
+ name: 'memory_purge',
+ idempotent: true,
+ sql: MEMORY_PURGE_SCHEMA_SQL + MEMORY_PURGE_GUARD_SQL,
+};
diff --git a/src/schema.sql b/src/schema.sql
index aebd2e4791..29b1f53019 100644
--- a/src/schema.sql
+++ b/src/schema.sql
@@ -2790,6 +2790,81 @@ DO $rls$ BEGIN
END $rls$;
-- END GENERATED from src/core/core-memory-schema.ts (CORE_EDIT_NOTICES_SCHEMA_SQL)
+-- The purge guards (facts, takes, pages triggers) are installed by the memory_purge migration.
+-- BEGIN GENERATED from src/core/facts/purge-schema.ts (MEMORY_PURGE_SCHEMA_SQL). Edit that file, then run: bun run build:schema
+CREATE TABLE IF NOT EXISTS fact_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ visibility TEXT NOT NULL CHECK (visibility IN ('private','world')),
+ subject TEXT NOT NULL DEFAULT '*',
+ fact_hash TEXT NOT NULL,
+ request_id UUID,
+ actor TEXT,
+ reason TEXT,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, visibility, subject, fact_hash)
+);
+DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE fact_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;
+CREATE TABLE IF NOT EXISTS take_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ subject TEXT NOT NULL DEFAULT '*',
+ claim_hash TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, subject, claim_hash)
+);
+DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE take_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;
+CREATE TABLE IF NOT EXISTS page_purges (
+ source_id TEXT NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
+ content_hash TEXT NOT NULL,
+ slug TEXT NOT NULL,
+ request_id UUID,
+ purged_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (source_id, content_hash)
+);
+DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE page_purges ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;
+CREATE TABLE IF NOT EXISTS derivation_inputs (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ input_table TEXT NOT NULL,
+ input_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id, input_table, input_id)
+);
+CREATE INDEX IF NOT EXISTS idx_derivation_inputs_input ON derivation_inputs (input_table, input_id);
+DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE derivation_inputs ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;
+CREATE TABLE IF NOT EXISTS needs_rederive (
+ derived_table TEXT NOT NULL,
+ derived_id TEXT NOT NULL,
+ source_id TEXT REFERENCES sources(id) ON DELETE CASCADE,
+ request_id UUID,
+ reason TEXT NOT NULL,
+ marked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (derived_table, derived_id)
+);
+DO $rls$ BEGIN
+ IF EXISTS (SELECT 1 FROM pg_roles r WHERE pg_has_role(current_user, r.oid, 'USAGE') AND (r.rolbypassrls OR r.rolsuper)) THEN
+ ALTER TABLE needs_rederive ENABLE ROW LEVEL SECURITY;
+ END IF;
+END $rls$;
+-- END GENERATED from src/core/facts/purge-schema.ts (MEMORY_PURGE_SCHEMA_SQL)
+
-- #5255/#5176 (O-DX-8): last upstream observation per source, recorded by sync
-- from the checkout's Git state (upstream ref, its last fetch/push time, commits
-- the synced commit lacks); doctor sync_freshness reads it with no subprocess.