Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(repair): B-7 gbrain repair facts-rehome moves facts off a page-less entity slug, preview-first (#6925)

garrytan304baeab237c
diff --git a/docs/guides/repair.md b/docs/guides/repair.md
index 7406200baa..9cfb746066 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -1327,6 +1327,38 @@ ordinary page fact. An observation whose source was already the page's own
slug and that a page write detached carries no trace of the move, so it is not
found.
+<a id="facts-rehome"></a>
+### Facts on an entity slug with no page
+
+Before the B-7 resolver fix, an extractor that named an organization under
+`companies/acme-example` missed the page `orgs/acme-example`, even when that
+page listed `acme example` as an alias, and managed writes kept the invented
+slug as the fact's entity (#6925). Those facts sit on an `entity_slug` no page
+answers for, so entity recall and trajectories never find them. `gbrain repair
+facts-rehome` moves them to the page they name. It is explicit-only and
+preview-bound, and it never runs on upgrade.
+
+**Say to your agent:** *"Some facts sit on entity slugs that have no page.
+Preview moving them to their pages, then apply after I agree."* The agent runs
+`gbrain repair facts-rehome` and, after you agree, the printed apply command.
+
+```bash
+gbrain repair facts-rehome # preview: one line per page-less slug
+gbrain repair facts-rehome --apply --expect <hash>
+```
+
+The preview lists every page-less `entity_slug` with its class. `rehome`: the
+strict resolver reaches one live entity page by identity evidence (the exact
+page, the same name under another organization directory, or a unique alias),
+and the slug's database-only facts move there. `ambiguous` (more than one page
+answers), `unverified` (only a bare-name guess answers), `unresolved` (no page
+answers) and `fence_owned` (every fact is a row of a page's Facts table) are
+listed and never moved. The apply moves exactly the previewed facts; a slug that
+gained a page, lost its target or whose facts changed since the preview reports
+`changed_since_preview` and is kept. A duplicate ontology observation already on
+the target is merged rather than copied. It writes the database only; no page is
+rewritten.
+
<a id="failed-writes"></a>
### Failed writes
diff --git a/src/core/repair/facts-rehome.ts b/src/core/repair/facts-rehome.ts
new file mode 100644
index 0000000000..3a4fed3b70
--- /dev/null
+++ b/src/core/repair/facts-rehome.ts
@@ -0,0 +1,151 @@
+/**
+ * `gbrain repair facts-rehome` (B-7, #6925): move facts off an entity slug that
+ * has no page onto the page that slug names.
+ *
+ * Before the B-7 resolver fix, an extractor's `companies/acme-example` missed
+ * `orgs/acme-example` (an alias hit was only accepted in the reference's own
+ * directory), and the managed write paths kept the invented slug as
+ * attribution, so a brain collected facts on `entity_slug` values no page
+ * answers for. Readers never reach them: recall and trajectory key on the
+ * page, `facts relink` only takes facts with no entity, and the phantom
+ * redirect moves pages, not slugs.
+ *
+ * Candidates: every `entity_slug` of the scoped sources with no live page,
+ * grouped per slug. Each slug is resolved with the strict resolver (identity
+ * evidence only: exact page, organization directory variant, unique alias;
+ * never a bare-name guess or a slugify) and classified:
+ * - rehome: one live entity page answers; its database-only facts move.
+ * - ambiguous / unverified / unresolved: listed, never moved.
+ * - fence_owned: every fact of the slug is a page's Facts table row, whose
+ * page text the repair does not rewrite; listed, never moved.
+ *
+ * Explicit-only and preview-bound: the preview lists every page-less slug with
+ * its class and the fact ids it would move, prints a hash and saves the
+ * rehome set; `--apply --expect <hash>` moves exactly that set. An apply
+ * rechecks each slug in its own transaction (still page-less, target still
+ * live, the same database-only fact ids) and keeps it as
+ * `changed_since_preview` otherwise. A duplicate ontology observation is
+ * dropped before the move (B-10). On a managed brain each slug commits as one
+ * coordinated database-only write on both slugs' keys. Nothing runs on
+ * upgrade, and no page is rewritten.
+ */
+import type { BrainEngine } from '../engine.ts';
+import { OperationError } from '../ops/contract.ts';
+import { resolveStrictEntityReference } from '../entities/resolve.ts';
+import { dropRekeyDuplicates } from '../facts/ontology-rekey.ts';
+import { RELINK_OPERATION } from '../facts/relink-publish.ts';
+import { maintenanceTransaction } from '../persistence/attribution.ts';
+import { coordinatedDatabaseWrite } from '../persistence/database-write.ts';
+import { clearApprovedSet, loadApprovedSet, previewChangedError, previewHash, saveApprovedSet } from '../persistence/preview-approval.ts';
+import { afterCursor, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairScope } from './core.ts';
+
+export interface PagelessEntity {
+ source_id: string;
+ /** The page-less `entity_slug`. */
+ slug: string;
+ /** Database-only facts (no fence row) carrying it, in id order: the rows a rehome moves. */
+ ids: number[];
+ /** Facts carrying it that are a page's Facts table row; never moved. */
+ fenced: number;
+ class: 'rehome' | 'ambiguous' | 'unverified' | 'unresolved' | 'fence_owned';
+ /** The page a rehome moves the facts to. */
+ target: string | null;
+}
+
+/** Every page-less entity slug of these sources, classified, in (source, slug) order. Read-only. */
+export async function classifyPagelessEntities(engine: BrainEngine, sourceIds: string[]): Promise<PagelessEntity[]> {
+ const rows = await engine.executeRaw<{ source_id: string; slug: string; ids: Array<number | string> | null; fenced: number | string }>(`
+ SELECT f.source_id, f.entity_slug AS slug,
+ array_agg(f.id ORDER BY f.id) FILTER (WHERE f.row_num IS NULL) AS ids,
+ count(*) FILTER (WHERE f.row_num IS NOT NULL) AS fenced
+ FROM facts f
+ WHERE f.source_id = ANY($1::text[]) AND f.entity_slug IS NOT NULL
+ AND NOT EXISTS (SELECT 1 FROM pages p WHERE p.source_id = f.source_id AND p.slug = f.entity_slug AND p.deleted_at IS NULL)
+ GROUP BY f.source_id, f.entity_slug
+ ORDER BY f.source_id, f.entity_slug`, [sourceIds]);
+ const out: PagelessEntity[] = [];
+ for (const row of rows) {
+ const ids = (row.ids ?? []).map(Number);
+ const base = { source_id: row.source_id, slug: row.slug, ids, fenced: Number(row.fenced) };
+ if (!ids.length) { out.push({ ...base, class: 'fence_owned', target: null }); continue; }
+ const resolved = await resolveStrictEntityReference(engine, row.source_id, row.slug);
+ if (resolved.slug !== null && resolved.slug !== row.slug) out.push({ ...base, class: 'rehome', target: resolved.slug });
+ else out.push({ ...base, class: resolved.slug === null && (resolved.miss === 'ambiguous' || resolved.miss === 'unverified') ? resolved.miss : 'unresolved', target: null });
+ }
+ return out;
+}
+
+interface ApprovedEntity extends PagelessEntity { selection: string[] }
+interface RehomeItem extends RepairItem { entity: PagelessEntity; hash: string; last: boolean }
+
+function previewCommand(scope: RepairScope): string {
+ return `gbrain repair facts-rehome${scope.source_ids.length === 1 ? ` --source ${scope.source_ids[0]}` : ''}`;
+}
+
+const sourcesOf = (engine: BrainEngine, scope: RepairScope) => engine.executeRaw<{ id: string; incarnation: string }>(
+ 'SELECT id, incarnation::text AS incarnation FROM sources WHERE id=ANY($1::text[]) ORDER BY id', [scope.source_ids]);
+
+const hashOf = (scope: RepairScope, sources: unknown, entities: PagelessEntity[]) =>
+ previewHash({ kind: 'facts-rehome-v1', brain_id: scope.brain_id, sources, selection: { source_ids: scope.source_ids }, entities });
+
+function item(entity: PagelessEntity, index: number, hash: string, last: boolean): RehomeItem {
+ return { cursor: { phase: 1, id: index }, source_id: entity.source_id, slug: entity.slug, chars: 0, action: 'rehome',
+ change: { from: entity.slug, to: entity.target! }, entity, hash, last };
+}
+
+function detail(entity: PagelessEntity): string {
+ const kept = entity.fenced ? `; ${entity.fenced} fenced fact(s) kept` : '';
+ if (entity.class === 'rehome') return `${entity.ids.length} fact(s) move to ${entity.target}${kept}`;
+ if (entity.class === 'fence_owned') return `${entity.fenced} fact(s), all page Facts table rows: kept as is`;
+ return `${entity.ids.length} fact(s) kept: ${entity.class === 'ambiguous' ? 'more than one page answers' : entity.class === 'unverified' ? 'only a bare-name guess answers' : 'no page answers'}${kept}`;
+}
+
+export const factsRehomeRepair: RepairHandler = {
+ kind: 'facts-rehome',
+ publication: 'projection',
+ embeds: false,
+ async plan(engine, scope, after, opts): Promise<RepairPlan> {
+ const command = previewCommand(scope);
+ if (!opts?.apply) {
+ const entities = await classifyPagelessEntities(engine, scope.source_ids);
+ const rehome = entities.filter(e => e.class === 'rehome');
+ const hash = hashOf(scope, await sourcesOf(engine, scope), entities);
+ if (rehome.length) await saveApprovedSet<ApprovedEntity>(engine, { command: 'facts-rehome', hash }, rehome.map(e => ({ ...e, selection: scope.source_ids })));
+ const residuals: Record<string, number> = {};
+ for (const e of entities) if (e.class !== 'rehome') residuals[e.class] = (residuals[e.class] ?? 0) + 1;
+ return { items: rehome.map((e, index) => item(e, index, hash, index === rehome.length - 1)), preview_hash: hash, residuals,
+ listing: entities.map(e => ({ item: `${e.source_id}:${e.slug}`, class: e.class, detail: detail(e) })) };
+ }
+ if (!opts.expect) {
+ throw new OperationError('invalid_params', 'gbrain repair facts-rehome --apply moves only the set a preview printed.',
+ `Preview first: ${command} — then run the apply command it prints: ${command} --apply --expect <preview-hash>`,
+ 'docs/guides/repair.md#explicit-only-repair-kinds');
+ }
+ const approved = await loadApprovedSet<ApprovedEntity>(engine, { command: 'facts-rehome', hash: opts.expect, previewCommand: command,
+ emptyHash: async () => hashOf(scope, await sourcesOf(engine, scope), []) });
+ if (approved.items.some(e => JSON.stringify(e.selection) !== JSON.stringify(scope.source_ids))) throw previewChangedError(opts.expect, command);
+ const items = approved.items.map(({ selection: _selection, ...e }, index) => item(e, index, opts.expect!, index === approved.items.length - 1));
+ return { items: items.filter(entry => afterCursor(entry.cursor, after)), preview_hash: opts.expect, residuals: {} };
+ },
+ async apply(ctx, entry): Promise<RepairItemOutcome> {
+ const { entity, hash, last } = entry as RehomeItem;
+ const target = entity.target!;
+ const move = async (tx: BrainEngine): Promise<boolean> => {
+ const [state] = await tx.executeRaw<{ from_live: boolean; to_live: boolean; ids: Array<number | string> | null }>(`
+ SELECT EXISTS (SELECT 1 FROM pages WHERE source_id = $1 AND slug = $2 AND deleted_at IS NULL) AS from_live,
+ EXISTS (SELECT 1 FROM pages WHERE source_id = $1 AND slug = $3 AND deleted_at IS NULL) AS to_live,
+ (SELECT array_agg(id ORDER BY id) FROM facts WHERE source_id = $1 AND entity_slug = $2 AND row_num IS NULL) AS ids`,
+ [entity.source_id, entity.slug, target]);
+ if (!state || state.from_live || !state.to_live || JSON.stringify((state.ids ?? []).map(Number)) !== JSON.stringify(entity.ids)) return false;
+ await dropRekeyDuplicates(tx, entity.source_id, { where: 'f.entity_slug = $2 AND f.row_num IS NULL', entity: '$3::text', sms: 'f.source_markdown_slug',
+ params: [entity.slug, target] });
+ await tx.executeRaw('UPDATE facts SET entity_slug = $3 WHERE source_id = $1 AND entity_slug = $2 AND row_num IS NULL', [entity.source_id, entity.slug, target]);
+ return true;
+ };
+ const managed = await coordinatedDatabaseWrite({ ...ctx, sourceId: entity.source_id }, RELINK_OPERATION, target, [entity.slug, target], tx => move(tx));
+ const moved = managed ? managed.value : await maintenanceTransaction(ctx.engine, move);
+ if (last) await clearApprovedSet(ctx.engine, { command: 'facts-rehome', hash });
+ if (!moved) return { applied: false, outcome: 'changed_since_preview', reason: 'the slug gained a page, the target page is gone, or its facts changed since the preview' };
+ return { applied: true, outcome: 'rehomed', detail: { from: entity.slug, to: target, facts: entity.ids.length } };
+ },
+};
diff --git a/test/pathological/b-7-facts-pageless-entity.test.ts b/test/pathological/b-7-facts-pageless-entity.test.ts
index 66ef725c3e..17073a7869 100644
--- a/test/pathological/b-7-facts-pageless-entity.test.ts
+++ b/test/pathological/b-7-facts-pageless-entity.test.ts
@@ -43,8 +43,8 @@ async function seedPages(engine: BrainEngine): Promise<void> {
const pages: Array<[string, string, string, string[]]> = [
['orgs/acme-example', 'Acme Example', 'organization', ['acme example', 'Acme Example Holdings']],
['orgs/delta-example', 'Delta Example', 'organization', []],
- ['orgs/gamma-example', 'Gamma Example', 'organization', ['gamma example']],
- ['companies/gamma-example-labs', 'Gamma Example Labs', 'company', ['gamma example']],
+ ['orgs/omega-one-example', 'Omega One Example', 'organization', ['omega example']],
+ ['companies/omega-two-example', 'Omega Two Example', 'company', ['omega example']],
['people/jane-doe-example', 'Jane Doe Example', 'person', []],
];
for (const [slug, title, type, aliases] of pages) await importFromContent(engine, slug, md(title, type, aliases), { noEmbed: true });
@@ -106,7 +106,7 @@ for (const arm of pathologicalArms()) {
await seedFact(engine, 'companies/acme-example', 'Acme Example hired a head of sales');
await seedFact(engine, 'companies/acme-example', 'Acme Example opened a second office');
await seedFact(engine, 'companies/acme-example-holdings', 'Acme Example Holdings filed its annual report');
- await seedFact(engine, 'companies/gamma-example', 'Gamma Example shipped a beta');
+ await seedFact(engine, 'companies/omega-example', 'Omega Example shipped a beta');
await seedFact(engine, 'people/zed-nobody-example', 'Zed Nobody Example likes chess');
await seedFact(engine, 'orgs/acme-example', 'Acme Example is based in a placeholder city');
const before = await factSlugs(engine);
@@ -115,7 +115,7 @@ for (const arm of pathologicalArms()) {
expect(preview.listing!.map(l => [l.item, l.class])).toEqual([
['default:companies/acme-example', 'rehome'],
['default:companies/acme-example-holdings', 'rehome'],
- ['default:companies/gamma-example', 'ambiguous'],
+ ['default:companies/omega-example', 'ambiguous'],
['default:people/zed-nobody-example', 'unresolved'],
]);
expect(preview.listing![0].detail).toContain('orgs/acme-example');
@@ -126,7 +126,7 @@ for (const arm of pathologicalArms()) {
test('an apply with the preview hash moves exactly the previewed facts; ambiguous and unresolved stay where they are', async () => {
await seedFact(engine, 'companies/acme-example', 'Acme Example hired a head of sales');
await seedFact(engine, 'companies/acme-example-holdings', 'Acme Example Holdings filed its annual report');
- await seedFact(engine, 'companies/gamma-example', 'Gamma Example shipped a beta');
+ await seedFact(engine, 'companies/omega-example', 'Omega Example shipped a beta');
await seedFact(engine, 'people/zed-nobody-example', 'Zed Nobody Example likes chess');
const handler = await rehomeHandler();
const scope = await resolveRepairScope(engine);
@@ -134,7 +134,7 @@ for (const arm of pathologicalArms()) {
const preview = await runRepair(ctxFor(engine), handler, scope, { apply: false, explicit: true });
const applied = await runRepair(ctxFor(engine), handler, scope, { apply: true, explicit: true, expect: preview.preview_hash });
expect(applied.applied).toBe(2);
- expect((await factSlugs(engine)).map(f => f.entity_slug)).toEqual(['orgs/acme-example', 'orgs/acme-example', 'companies/gamma-example', 'people/zed-nobody-example']);
+ expect((await factSlugs(engine)).map(f => f.entity_slug)).toEqual(['orgs/acme-example', 'orgs/acme-example', 'companies/omega-example', 'people/zed-nobody-example']);
});
test('a stale preview refuses: an unknown hash is refused, and a slug whose facts changed since the preview is kept', async () => {
@@ -159,5 +159,19 @@ for (const arm of pathologicalArms()) {
expect((await factSlugs(engine)).map(f => f.entity_slug)).toEqual(['orgs/acme-example']);
}, { databaseUrl: arm.databaseUrl });
}, 120_000);
+
+ test('repair facts-rehome applies as a coordinated write on a managed brain', async () => {
+ await managedBrain(async ({ engine, ctx }) => {
+ await writeSingleFact(engine, 'default', { fact: 'Acme Example hired a head of sales', provenance: 'test:b-7', entity: 'companies/acme-example', visibility: 'world', remote: false });
+ expect((await factSlugs(engine)).map(f => f.entity_slug)).toEqual(['companies/acme-example']);
+ await operationsByName.put_page.handler(ctx, { slug: 'orgs/acme-example', content: md('Acme Example', 'organization', ['acme example']) });
+ const handler = await rehomeHandler();
+ const scope = await resolveRepairScope(engine);
+ const preview = await runRepair(ctx, handler, scope, { apply: false, explicit: true });
+ const applied = await runRepair(ctx, handler, scope, { apply: true, explicit: true, expect: preview.preview_hash });
+ expect(applied.applied).toBe(1);
+ expect((await factSlugs(engine)).map(f => f.entity_slug)).toEqual(['orgs/acme-example']);
+ }, { databaseUrl: arm.databaseUrl });
+ }, 120_000);
});
}