Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(facts): fact_relink_attempts table and unlinked-facts keyset index (v187, #5836)
garrytanc97a301aa8b2
diff --git a/scripts/build-schema.ts b/scripts/build-schema.tsindex 6539fc9901..0d5d0a9910 100644--- a/scripts/build-schema.ts+++ b/scripts/build-schema.ts@@ -37,6 +37,7 @@ import { POSTGRES_CONCURRENT_PERSISTENCE_INDEXES, PERSISTENCE_SCHEMA_STATEMENTSimport { PERSISTENCE_TOPOLOGY_SCHEMA_SQL } from '../src/core/persistence/topology-schema.ts';import { SHARED_SKILLS_SCHEMA_SQL } from '../src/core/shared-skills/schema-all.ts';import { DECIDE_SCHEMA_SQL } from '../src/core/ai/decide/schema.ts';+import { FACT_RELINK_SCHEMA_SQL } from '../src/core/facts/relink-schema.ts';const REPO = resolve(import.meta.dir, '..');export const SCHEMA_SQL_PATH = 'src/schema.sql';@@ -91,6 +92,7 @@ export const FRAGMENTS: readonly Fragment[] = [{ source: 'src/core/company-brain/receipt-schema.ts', expr: 'SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL', postgres: SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL, pglite: SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL },{ source: 'src/core/shared-skills/schema-all.ts', expr: 'SHARED_SKILLS_SCHEMA_SQL', postgres: SHARED_SKILLS_SCHEMA_SQL, pglite: SHARED_SKILLS_SCHEMA_SQL },{ source: 'src/core/ai/decide/schema.ts', expr: 'DECIDE_SCHEMA_SQL', postgres: DECIDE_SCHEMA_SQL, pglite: DECIDE_SCHEMA_SQL },+ { source: 'src/core/facts/relink-schema.ts', expr: 'FACT_RELINK_SCHEMA_SQL', postgres: FACT_RELINK_SCHEMA_SQL, pglite: FACT_RELINK_SCHEMA_SQL },];const fragmentLabel = (f: Fragment) => `${f.source} (${f.expr})`;diff --git a/src/core/facts/relink-schema.ts b/src/core/facts/relink-schema.tsnew file mode 100644index 0000000000..fc7944dffc--- /dev/null+++ b/src/core/facts/relink-schema.ts@@ -0,0 +1,46 @@+/**+ * `gbrain facts relink` storage DDL (#5836): one canonical copy used by the+ * schema migration and, through scripts/build-schema.ts FRAGMENTS, by+ * fresh-install DDL.+ *+ * fact_relink_attempts keeps one row per fact: the latest relink outcome+ * (a model verdict memo, a `linked`/`deduped` record, or a `pending` journal+ * entry the unmanaged path writes before it touches the fence file). Like+ * decide_sweep_deferred it carries no foreign key (facts is created by+ * migrations after this DDL); readers join facts. `journal` is text, not+ * JSONB, so no code path can double-encode it.+ */++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 FACT_RELINK_TABLE_SQL = `+CREATE TABLE IF NOT EXISTS fact_relink_attempts (+ source_id TEXT NOT NULL,+ fact_id BIGINT NOT NULL,+ outcome TEXT NOT NULL,+ reason TEXT,+ tier TEXT,+ model TEXT,+ target_slug TEXT,+ planned_row_num INTEGER,+ journal TEXT,+ run_id TEXT,+ attempted_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (source_id, fact_id)+);+CREATE INDEX IF NOT EXISTS fact_relink_attempts_outcome_idx ON fact_relink_attempts (source_id, outcome, attempted_at);+${rls('fact_relink_attempts')}+`;++/**+ * Keyset index for relink's candidate walk. Migration-only (the facts table is+ * itself created by migrations, after the fresh-install DDL); CONCURRENTLY on+ * Postgres.+ */+export const FACT_UNLINKED_INDEX_SQL = `CREATE INDEX IF NOT EXISTS idx_facts_unlinked_active ON facts (source_id, id) WHERE entity_slug IS NULL AND expired_at IS NULL;`;++export const FACT_RELINK_SCHEMA_SQL = FACT_RELINK_TABLE_SQL;diff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.tsindex 713cfcc2b2..c4fc283a4d 100644--- a/src/core/pglite-schema.generated.ts+++ b/src/core/pglite-schema.generated.ts@@ -1713,6 +1713,28 @@ DO \$rls\$ BEGINEND IF;END \$rls\$;+-- gbrain facts relink attempts and journal (#5836).+CREATE TABLE IF NOT EXISTS fact_relink_attempts (+ source_id TEXT NOT NULL,+ fact_id BIGINT NOT NULL,+ outcome TEXT NOT NULL,+ reason TEXT,+ tier TEXT,+ model TEXT,+ target_slug TEXT,+ planned_row_num INTEGER,+ journal TEXT,+ run_id TEXT,+ attempted_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (source_id, fact_id)+);+CREATE INDEX IF NOT EXISTS fact_relink_attempts_outcome_idx ON fact_relink_attempts (source_id, outcome, attempted_at);+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_relink_attempts ENABLE ROW LEVEL SECURITY;+ END IF;+END \$rls\$;+`;export function getPGLiteSchema(diff --git a/src/core/schema-embedded.generated.ts b/src/core/schema-embedded.generated.tsindex 86dc2320de..20d66cd15c 100644--- a/src/core/schema-embedded.generated.ts+++ b/src/core/schema-embedded.generated.ts@@ -2426,4 +2426,28 @@ DO \$rls\$ BEGINEND IF;END \$rls\$;-- END GENERATED from src/core/ai/decide/schema.ts (DECIDE_SCHEMA_SQL)++-- gbrain facts relink attempts and journal (#5836).+-- BEGIN GENERATED from src/core/facts/relink-schema.ts (FACT_RELINK_SCHEMA_SQL). Edit that file, then run: bun run build:schema+CREATE TABLE IF NOT EXISTS fact_relink_attempts (+ source_id TEXT NOT NULL,+ fact_id BIGINT NOT NULL,+ outcome TEXT NOT NULL,+ reason TEXT,+ tier TEXT,+ model TEXT,+ target_slug TEXT,+ planned_row_num INTEGER,+ journal TEXT,+ run_id TEXT,+ attempted_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (source_id, fact_id)+);+CREATE INDEX IF NOT EXISTS fact_relink_attempts_outcome_idx ON fact_relink_attempts (source_id, outcome, attempted_at);+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_relink_attempts ENABLE ROW LEVEL SECURITY;+ END IF;+END \$rls\$;+-- END GENERATED from src/core/facts/relink-schema.ts (FACT_RELINK_SCHEMA_SQL)`;diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.tsindex 20a45b7625..6d93d77d73 100644--- a/src/core/schema-migrations/registry.generated.ts+++ b/src/core/schema-migrations/registry.generated.ts@@ -187,6 +187,7 @@ import { v183 } from './v183-persistence-mode-epoch.ts';import { v184 } from './v184-decision-receipts.ts';import { v185 } from './v185-decide-calibrations.ts';import { v186 } from './v186-decide-proposals.ts';+import { v187 } from './v187-fact-relink-attempts.ts';// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); the// runner sorts by version before applying.@@ -372,4 +373,5 @@ export const MIGRATIONS: Migration[] = [v184,v185,v186,+ v187,];diff --git a/src/core/schema-migrations/v187-fact-relink-attempts.ts b/src/core/schema-migrations/v187-fact-relink-attempts.tsnew file mode 100644index 0000000000..bacee37cc5--- /dev/null+++ b/src/core/schema-migrations/v187-fact-relink-attempts.ts@@ -0,0 +1,16 @@+import type { Migration } from './types.ts';+import { FACT_RELINK_TABLE_SQL, FACT_UNLINKED_INDEX_SQL } from '../facts/relink-schema.ts';+import { dropInvalidConcurrentIndex } from './helpers.ts';++// #5836 `gbrain facts relink`: the per-fact attempt/journal table and the+// keyset index over active unlinked facts; DDL in src/core/facts/relink-schema.ts.+export const v187: Migration = {+ version: 187, name: 'fact_relink_attempts', idempotent: true, transaction: false, sql: '',+ handler: async engine => {+ await engine.runMigration(187, FACT_RELINK_TABLE_SQL);+ if (engine.kind === 'postgres') await dropInvalidConcurrentIndex(engine, 187, 'idx_facts_unlinked_active');+ await engine.runMigration(187, engine.kind === 'postgres'+ ? FACT_UNLINKED_INDEX_SQL.replace('CREATE INDEX', 'CREATE INDEX CONCURRENTLY')+ : FACT_UNLINKED_INDEX_SQL);+ },+};diff --git a/src/schema.sql b/src/schema.sqlindex de9ff5e6a8..b44dd8faea 100644--- a/src/schema.sql+++ b/src/schema.sql@@ -2422,3 +2422,27 @@ DO $rls$ BEGINEND IF;END $rls$;-- END GENERATED from src/core/ai/decide/schema.ts (DECIDE_SCHEMA_SQL)++-- gbrain facts relink attempts and journal (#5836).+-- BEGIN GENERATED from src/core/facts/relink-schema.ts (FACT_RELINK_SCHEMA_SQL). Edit that file, then run: bun run build:schema+CREATE TABLE IF NOT EXISTS fact_relink_attempts (+ source_id TEXT NOT NULL,+ fact_id BIGINT NOT NULL,+ outcome TEXT NOT NULL,+ reason TEXT,+ tier TEXT,+ model TEXT,+ target_slug TEXT,+ planned_row_num INTEGER,+ journal TEXT,+ run_id TEXT,+ attempted_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (source_id, fact_id)+);+CREATE INDEX IF NOT EXISTS fact_relink_attempts_outcome_idx ON fact_relink_attempts (source_id, outcome, attempted_at);+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_relink_attempts ENABLE ROW LEVEL SECURITY;+ END IF;+END $rls$;+-- END GENERATED from src/core/facts/relink-schema.ts (FACT_RELINK_SCHEMA_SQL)diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.jsonindex c769f251be..04b29cade7 100644--- a/test/fixtures/goldens/migrations/records.json+++ b/test/fixtures/goldens/migrations/records.json@@ -1,6 +1,6 @@{"golden": {- "count": 181,+ "count": 182,"gaps": [17,18,@@ -8,6 +8,8 @@100],"helpers": {+ "FACT_RELINK_TABLE_SQL": "43af2adb4ce7704c75334b9432ce2d6fdd85f9f8629169150374a68999c82717",+ "FACT_UNLINKED_INDEX_SQL": "87dcf23c3fa570d93bfb214b6bbb024faa9ff9bff227aa78869a29580be03298","PERSISTENCE_DATABASE_PENDING_INDEX_SQL": "6e0f618be33d037454933ea4dea11438413d45aef35d3bd4f33e89624d6e199e","PERSISTENCE_EFFECT_PARKED_INDEX_SQL": "0107863883415378d7c0dce7a0cbd5311c2c5a59fc0f81917cc8b767fd235cf2","PERSISTENCE_SYNC_RUN_INDEXES": "e1b12613c5fd2facb850e6857b0bddcf49b2478ec258fa226fe15e71e55df9c1",@@ -25,7 +27,7 @@"slugifyPath": "401dbca23580865d8b168dd8d84db4a6c708c6610d7cb61abafc7d0ee9809063","verifyProjectionStatistics": "59d9ccfea1c53384d8607874a51209a9c2dfd51b2b85a2cb6269fa4120faf1f3"},- "latest": 186,+ "latest": 187,"records": [{"handler": {@@ -2751,6 +2753,27 @@"transaction": "absent","verify": "absent","version": 186+ },+ {+ "handler": {+ "form": "arrow",+ "helpers": [+ "FACT_RELINK_TABLE_SQL",+ "FACT_UNLINKED_INDEX_SQL",+ "dropInvalidConcurrentIndex"+ ],+ "sha256": "b4d519036887e9ba4f75e78b94982150f0f1b26a5f2c01ae5c7796d0cfdceed8"+ },+ "idempotent": true,+ "name": "fact_relink_attempts",+ "sql": {+ "length": 0,+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ },+ "sqlFor": "absent",+ "transaction": false,+ "verify": "absent",+ "version": 187}],"versions": [@@ -2934,7 +2957,8 @@183,184,185,- 186+ 186,+ 187]},"normalizer": "migrations-record-v1"