Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refactor(facts): B-10 phantom migration dedup moves into ontology-rekey.ts (module-size ceilings)

garrytan3df661e698b4
diff --git a/src/core/facts/ontology-rekey.ts b/src/core/facts/ontology-rekey.ts
index ec3ca22718..71ddc1d07e 100644
--- a/src/core/facts/ontology-rekey.ts
+++ b/src/core/facts/ontology-rekey.ts
@@ -39,3 +39,9 @@ export async function dropRekeyDuplicates(
await tx.executeRaw('DELETE FROM facts WHERE id = ANY($1::bigint[])', [ids]);
return ids.length;
}
+
+/** The phantom redirect's fact migration (`migrateFactsToCanonical`): the phantom page's active rows move onto the canonical slug. */
+export function dropPhantomMigrationDuplicates(tx: Pick<BrainEngine, 'executeRaw'>, sourceId: string, canonicalSlug: string, phantomSlug: string): Promise<number> {
+ return dropRekeyDuplicates(tx, sourceId, { where: 'f.source_markdown_slug = $3 AND f.expired_at IS NULL', entity: '$2::text', sms: '$2::text',
+ params: [canonicalSlug, phantomSlug] });
+}
diff --git a/src/core/pglite-engine.ts b/src/core/pglite-engine.ts
index afb7396c2b..7aab18bc31 100644
--- a/src/core/pglite-engine.ts
+++ b/src/core/pglite-engine.ts
@@ -12,7 +12,7 @@ import { readPageSnapshot as readCanonicalPageSnapshot } from './page-state/snap
import { readPageSnapshotsBatch } from './page-snapshot-batch.ts';
import { createPageVersion } from './page-state/versions.ts';
import { moveSlugBindings, recordRenameAlias } from './page-state/rename-alias.ts';
-import { dropRekeyDuplicates } from './facts/ontology-rekey.ts';
+import { dropPhantomMigrationDuplicates } from './facts/ontology-rekey.ts';
import { composablePgliteTransaction, transactionMemo } from './page-state/transactions.ts';
import { configurePgliteCodecs, PgliteStatementCache } from './pglite-statements.ts';
import { snapshotSchemaInputs } from './snapshot-schema-inputs.ts';
@@ -1322,15 +1322,11 @@ export class PGLiteEngine implements BrainEngine {
canonicalSlug: string,
sourceId: string,
): Promise<{ migrated: number }> {
- // Parity with PostgresEngine.migrateFactsToCanonical. UPDATE preserves
- // every column except entity_slug + source_markdown_slug + row_num,
- // which is offset past canonical's current MAX(row_num) (#4558; NULL
- // stays NULL, expired rows count — see the Postgres twin). Active rows
- // only (expired_at IS NULL) so we don't disturb the supersession audit
- // trail.
- // B-10: an observation the canonical page already holds would collide on idx_facts_ontology_stint_dedup; drop it first.
- await dropRekeyDuplicates(this, sourceId, { where: 'f.source_markdown_slug = $3 AND f.expired_at IS NULL', entity: '$2::text', sms: '$2::text',
- params: [canonicalSlug, phantomSlug] });
+ // Parity with PostgresEngine.migrateFactsToCanonical: only entity_slug, source_markdown_slug and row_num change
+ // (row_num offset past canonical's MAX(row_num), #4558; NULL stays NULL, expired rows count — see the Postgres twin),
+ // on active rows only (expired_at IS NULL) so the supersession audit trail stays. B-10: first drop an observation
+ // the canonical page already holds, which would collide on idx_facts_ontology_stint_dedup.
+ await dropPhantomMigrationDuplicates(this, sourceId, canonicalSlug, phantomSlug);
const { rows } = await this.db.query(
`UPDATE facts
SET entity_slug = $1,
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index bd93a08b03..2e2ca0ac06 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -10,7 +10,7 @@ import { readPageSnapshot as readCanonicalPageSnapshot } from './page-state/snap
import { readPageSnapshotsBatch } from './page-snapshot-batch.ts';
import { createPageVersion } from './page-state/versions.ts';
import { moveSlugBindings, recordRenameAlias } from './page-state/rename-alias.ts';
-import { dropRekeyDuplicates } from './facts/ontology-rekey.ts';
+import { dropPhantomMigrationDuplicates } from './facts/ontology-rekey.ts';
import { composablePostgresTransaction, transactionMemo } from './page-state/transactions.ts';
import type { PageReadScope } from './types.ts';
import type { PageReadPolicy } from './types.ts';
@@ -904,16 +904,12 @@ export class PostgresEngine implements BrainEngine {
// index. MAX counts expired rows too (the index only excludes NULL);
// NULL + M stays NULL (legacy-guard semantics intact). extract_facts
// hasRowNumDrift re-harmonises the numbering against the disk fence.
- // Idempotent by virtue of the WHERE clause matching nothing on re-run.
- //
- // We scope to `expired_at IS NULL` so the migration touches only
+ // Idempotent by virtue of the WHERE clause matching nothing on re-run. We scope to `expired_at IS NULL` so the migration touches only
// active facts. Forgotten / superseded rows that already carry an
// expiry stay where they are — soft-deleting the phantom page is
// sufficient to make them invisible without rewriting their slug
- // (and rewriting would break the audit trail in listSupersessions).
- // B-10: an observation the canonical page already holds would collide on idx_facts_ontology_stint_dedup; drop it first.
- await dropRekeyDuplicates(this, sourceId, { where: 'f.source_markdown_slug = $3 AND f.expired_at IS NULL', entity: '$2::text', sms: '$2::text',
- params: [canonicalSlug, phantomSlug] });
+ // (and rewriting would break the audit trail in listSupersessions). B-10: an observation the canonical page already holds would collide on idx_facts_ontology_stint_dedup; drop it first.
+ await dropPhantomMigrationDuplicates(this, sourceId, canonicalSlug, phantomSlug);
const result = await sql`
UPDATE facts
SET entity_slug = ${canonicalSlug},