Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(facts): relink tiers, dedupe, withdrawal, continuation, model tier and write-target routing (#5836)

garrytan57c25e9aea6b
diff --git a/test/e2e/facts-relink-routing.test.ts b/test/e2e/facts-relink-routing.test.ts
new file mode 100644
index 0000000000..908e6b7840
--- /dev/null
+++ b/test/e2e/facts-relink-routing.test.ts
@@ -0,0 +1,3 @@
+import { registerPostgresTests } from '../helpers/test-backends.ts';
+
+await registerPostgresTests(() => import('../facts-relink-routing.test.ts'));
diff --git a/test/facts-relink-routing.test.ts b/test/facts-relink-routing.test.ts
new file mode 100644
index 0000000000..24e13d1d20
--- /dev/null
+++ b/test/facts-relink-routing.test.ts
@@ -0,0 +1,110 @@
+/**
+ * #5836 — relink ends where a fresh remember would: the canonical file plus
+ * page body for a bound write-through source, the page body only when
+ * write-through is off, a refusal (`unfenceable`) for an unbound
+ * write-through source, and the same file publication on a managed brain.
+ * Every branch is followed by the extract_facts reconcile, which must find
+ * nothing to change and no legacy rows. Postgres runs through
+ * test/e2e/facts-relink-routing.test.ts. Synthetic data only.
+ */
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { serializePageToMarkdown } from '../src/core/markdown.ts';
+import { parseFactsFence } from '../src/core/facts-fence.ts';
+import { runFactsRelink } from '../src/core/facts/relink.ts';
+import { runExtractFacts } from '../src/core/cycle/extract-facts.ts';
+import { activatePersistence } from '../src/core/persistence/activation.ts';
+import { claimWorktree } from '../src/core/persistence/ownership.ts';
+import { registerLocalWriter } from '../src/core/persistence/identity.ts';
+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
+import { withEnv } from './helpers/with-env.ts';
+import { isolatedSharedSkillsEngine } from './helpers/shared-skills-engine.ts';
+import { requirePostgresTestDatabase } from './helpers/test-backends.ts';
+
+const backend = process.env.GBRAIN_TEST_BACKEND === 'postgres' ? 'postgres' : 'pglite';
+const home = mkdtempSync(join(tmpdir(), 'gbrain-relink-routing-'));
+const repo = join(home, 'canonical');
+let engine: BrainEngine;
+let closeEngine: () => Promise<void> = async () => {};
+const config = { engine: backend } as never;
+
+beforeAll(async () => withEnv({ GBRAIN_HOME: home, GBRAIN_SOURCE: undefined, GBRAIN_BRAIN_ID: 'host' }, async () => {
+ const fixture = await isolatedSharedSkillsEngine(backend === 'postgres' ? requirePostgresTestDatabase() : undefined);
+ engine = fixture.engine; closeEngine = fixture.close;
+ mkdirSync(join(repo, 'companies'), { recursive: true });
+ await engine.setConfig('decide.slots.conflict.mode', 'off');
+}), 120_000);
+
+afterAll(async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ await disposePersistenceConsumer(engine); await closeEngine();
+ rmSync(home, { recursive: true, force: true });
+}));
+
+async function seedEntity(slug: string, title: string, writeFile: boolean): Promise<void> {
+ await engine.putPage(slug, { type: 'company', title, compiled_truth: `# ${title}\n\nA company.`, timeline: '', frontmatter: {} });
+ const snapshot = await engine.readPageSnapshot(slug, { sourceId: 'default' });
+ if (writeFile) writeFileSync(join(repo, `${slug}.md`), serializePageToMarkdown(snapshot!.page, snapshot!.tags));
+}
+
+async function unlinked(fact: string): Promise<number> {
+ return (await engine.insertFact({ fact, kind: 'fact', entity_slug: null, visibility: 'world', source: 'chat' } as never, { source_id: 'default' })).id;
+}
+
+async function expectAdopted(id: number, slug: string): Promise<number> {
+ const [row] = await engine.executeRaw<{ entity_slug: string; source_markdown_slug: string; row_num: number }>(
+ 'SELECT entity_slug, source_markdown_slug, row_num FROM facts WHERE id=$1', [id]);
+ expect(row).toMatchObject({ entity_slug: slug, source_markdown_slug: slug });
+ const page = await engine.getPage(slug, { sourceId: 'default' });
+ expect(parseFactsFence(page!.compiled_truth).facts.some(f => f.rowNum === Number(row!.row_num))).toBe(true);
+ const reconcile = await runExtractFacts(engine, { sourceId: 'default' });
+ expect(reconcile).toMatchObject({ legacyRowsPending: 0, guardTriggered: false, factsInserted: 0, factsDeleted: 0 });
+ return Number(row!.row_num);
+}
+
+const relink = () => runFactsRelink(engine, { sourceId: 'default', config, llm: false });
+
+test(`${backend}: write-through off fences into the page body only`, async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ await engine.executeRaw("UPDATE sources SET local_path=$1 WHERE id='default'", [repo]);
+ await engine.setConfig('sync.write_through', 'false');
+ await seedEntity('companies/zinc-example', 'Zinc Example', false);
+ const id = await unlinked('Zinc Example opened a second office');
+ expect((await relink()).linked).toBe(1);
+ await expectAdopted(id, 'companies/zinc-example');
+ await engine.setConfig('sync.write_through', 'true');
+}));
+
+test(`${backend}: an unbound write-through source is refused as unfenceable`, async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ await seedEntity('companies/onyx-example', 'Onyx Example', true);
+ const id = await unlinked('Onyx Example hired a CFO');
+ const report = await relink();
+ expect(report.skipped.unfenceable).toBe(1);
+ const [row] = await engine.executeRaw<{ entity_slug: string | null }>('SELECT entity_slug FROM facts WHERE id=$1', [id]);
+ expect(row!.entity_slug).toBeNull();
+}));
+
+test(`${backend}: a bound write-through source writes the canonical file`, async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ await engine.setConfig('sync.repo_path', repo);
+ await claimWorktree(engine, 'default', repo);
+ await seedEntity('companies/acme-example', 'Acme Example', true);
+ const id = await unlinked('Acme Example raised a seed round');
+ expect((await relink()).linked).toBeGreaterThanOrEqual(1);
+ const rowNum = await expectAdopted(id, 'companies/acme-example');
+ const file = parseFactsFence(readFileSync(join(repo, 'companies/acme-example.md'), 'utf8')).facts;
+ expect(file.find(f => f.rowNum === rowNum)?.claim).toBe('Acme Example raised a seed round');
+}));
+
+test(`${backend}: a managed brain publishes the same way through the coordinator`, async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ await seedEntity('companies/basalt-example', 'Basalt Example', true);
+ await registerLocalWriter(engine, 'cli');
+ await activatePersistence(engine, { confirmQuiesced: true });
+ const id = await engine.transaction(tx => withCoordinatedWrite(tx, ['default'], async () =>
+ (await tx.insertFact({ fact: 'Basalt Example signed a pilot', kind: 'fact', entity_slug: null, visibility: 'world', source: 'chat' } as never, { source_id: 'default' })).id));
+ expect((await relink()).linked).toBe(1);
+ const rowNum = await expectAdopted(id, 'companies/basalt-example');
+ const file = parseFactsFence(readFileSync(join(repo, 'companies/basalt-example.md'), 'utf8')).facts;
+ expect(file.find(f => f.rowNum === rowNum)?.claim).toBe('Basalt Example signed a pilot');
+}));
diff --git a/test/facts-relink.test.ts b/test/facts-relink.test.ts
new file mode 100644
index 0000000000..64070f56db
--- /dev/null
+++ b/test/facts-relink.test.ts
@@ -0,0 +1,279 @@
+/**
+ * #5836 — `gbrain facts relink`: unlinked facts move by id onto the entity
+ * page's fence through the write coordinator; duplicates retire, nothing is
+ * superseded, withdrawn claims stay off, fence-owned rows are untouched, dry
+ * runs write nothing, and the model tier is memoized, capped and verified.
+ * PGLite, database-only source (no repo). Synthetic data only.
+ */
+import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { importFromContent } from '../src/core/import-file.ts';
+import { runFactsRelink } from '../src/core/facts/relink.ts';
+import { runExtractFacts } from '../src/core/cycle/extract-facts.ts';
+import { parseFactsFence } from '../src/core/facts-fence.ts';
+import { recordFactWithdrawal } from '../src/core/facts/withdrawal.ts';
+import { readFacts } from '../src/core/persistence/prepared-maintenance.ts';
+import { relinkFactHash, submitRelinkGroup } from '../src/core/facts/relink-publish.ts';
+import { __setChatTransportForTests, configureGateway, resetGateway, type ChatOpts, type ChatResult } from '../src/core/ai/gateway.ts';
+
+let engine: PGLiteEngine;
+const config = { engine: 'pglite' } as never;
+
+async function unlinked(fact: string, extra: Record<string, unknown> = {}): Promise<number> {
+ const r = await engine.insertFact({ fact, kind: 'fact', entity_slug: null, visibility: 'world', source: 'chat 2026-10-01', ...extra } as never,
+ { source_id: 'default' });
+ return r.id;
+}
+const row = async (id: number) => (await engine.executeRaw<Record<string, unknown>>('SELECT * FROM facts WHERE id = $1', [id]))[0]!;
+const relink = (extra: Record<string, unknown> = {}) => runFactsRelink(engine, { sourceId: 'default', config, llm: false, ...extra });
+
+beforeEach(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ await engine.setConfig('decide.slots.conflict.mode', 'off');
+ await importFromContent(engine, 'companies/acme-example', '---\ntitle: Acme Example\ntype: company\n---\n\n# Acme Example\n\nA company.\n', { noEmbed: true });
+ await importFromContent(engine, 'people/alice-example', '---\ntitle: Alice Example\ntype: person\n---\n\n# Alice Example\n', { noEmbed: true });
+});
+
+afterEach(async () => {
+ __setChatTransportForTests(null);
+ resetGateway();
+ await engine.disconnect();
+});
+
+describe('relink free tiers', () => {
+ test('moves the row by id onto the entity fence; reconcile changes nothing', async () => {
+ const id = await unlinked('Acme Example raised a seed round');
+ const report = await relink();
+ expect(report.linked).toBe(1);
+ expect(report.linked_by_tier.mention).toBe(1);
+ const r = await row(id);
+ expect(r.entity_slug).toBe('companies/acme-example');
+ expect(r.source_markdown_slug).toBe('companies/acme-example');
+ expect(Number(r.row_num)).toBeGreaterThan(0);
+ expect(r.context).toBe('entity relinked from mention');
+ expect(r.expired_at).toBeNull();
+ const page = await engine.getPage('companies/acme-example', { sourceId: 'default' });
+ const cell = parseFactsFence(page!.compiled_truth).facts.find(f => f.rowNum === Number(r.row_num));
+ expect(cell?.claim).toBe('Acme Example raised a seed round');
+ expect(cell?.context).toBe('entity relinked from mention');
+ const reconcile = await runExtractFacts(engine, { sourceId: 'default' });
+ expect(reconcile.legacyRowsPending).toBe(0);
+ expect(reconcile.guardTriggered).toBe(false);
+ expect(reconcile.factsInserted).toBe(0);
+ expect(reconcile.factsDeleted).toBe(0);
+ const active = await engine.executeRaw<{ n: number }>(`SELECT COUNT(*)::int AS n FROM facts WHERE fact = 'Acme Example raised a seed round' AND expired_at IS NULL`);
+ expect(active[0]!.n).toBe(1);
+ expect((await row(id)).entity_slug).toBe('companies/acme-example');
+ });
+
+ test('page tier uses the recorded page of a page-extraction row', async () => {
+ const id = await unlinked('raised a seed round in March', { source: 'mcp:put_page', context: 'companies/acme-example' });
+ const report = await relink();
+ expect(report.linked_by_tier.page).toBe(1);
+ expect((await row(id)).context).toBe('companies/acme-example — entity relinked from page');
+ });
+
+ test('an exact duplicate retires expired and detached, never deleted', async () => {
+ const first = await unlinked('Acme Example raised a seed round');
+ await relink();
+ const dup = await unlinked('Acme Example raised a seed round');
+ const report = await relink();
+ expect(report.deduped).toBe(1);
+ const r = await row(dup);
+ expect(r.entity_slug).toBe('companies/acme-example');
+ expect(r.expired_at).not.toBeNull();
+ expect(r.row_num).toBeNull();
+ expect(String(r.context)).toContain(`duplicate of #${first}`);
+ expect((await row(first)).expired_at).toBeNull();
+ });
+
+ test('two identical unlinked facts in one run: one links, one retires', async () => {
+ await unlinked('Acme Example hired a CFO');
+ await unlinked('Acme Example hired a CFO');
+ const report = await relink();
+ expect(report.linked).toBe(1);
+ expect(report.deduped).toBe(1);
+ });
+
+ test('a similar fact with different text links without superseding anything', async () => {
+ const a = await unlinked('Acme Example raised a seed round');
+ await relink();
+ const b = await unlinked('Acme Example raised a seed round of $2M');
+ await relink();
+ expect((await row(a)).expired_at).toBeNull();
+ expect((await row(a)).superseded_by).toBeNull();
+ expect((await row(b)).entity_slug).toBe('companies/acme-example');
+ });
+
+ test('a withdrawn claim is never attached to that entity', async () => {
+ const original = await unlinked('Acme Example raised a seed round');
+ await relink();
+ await recordFactWithdrawal(engine, original, 'default');
+ const id = await unlinked('Acme Example raised a seed round');
+ const report = await relink();
+ expect(report.skipped.withdrawn).toBe(1);
+ expect((await row(id)).entity_slug).toBeNull();
+ });
+
+ test('fence-owned subjectless rows are counted and untouched', async () => {
+ const id = await unlinked('Acme Example raised a seed round', { source: 'cli:conversation' });
+ await engine.executeRaw(`UPDATE facts SET source_markdown_slug = 'transcripts/2026-04-03', row_num = 1 WHERE id = $1`, [id]);
+ const report = await relink();
+ expect(report.fence_owned).toBe(1);
+ expect(report.scanned).toBe(0);
+ expect((await row(id)).entity_slug).toBeNull();
+ });
+
+ test('dry run writes nothing', async () => {
+ const id = await unlinked('Acme Example raised a seed round');
+ const before = await engine.getPage('companies/acme-example', { sourceId: 'default' });
+ const report = await relink({ dryRun: true });
+ expect(report.linked).toBe(1);
+ expect((await row(id)).entity_slug).toBeNull();
+ const after = await engine.getPage('companies/acme-example', { sourceId: 'default' });
+ expect(after!.compiled_truth).toBe(before!.compiled_truth);
+ const memo = await engine.executeRaw('SELECT 1 FROM fact_relink_attempts');
+ expect(memo.length).toBe(0);
+ const log = await engine.executeRaw(`SELECT 1 FROM ingest_log WHERE source_type = 'facts:relink'`);
+ expect(log.length).toBe(0);
+ });
+
+ test('a second run is a no-op', async () => {
+ await unlinked('Acme Example raised a seed round');
+ await relink();
+ const again = await relink();
+ expect(again.scanned).toBe(0);
+ expect(again.linked).toBe(0);
+ });
+
+ test('continuation walks past facts that can never link', async () => {
+ await unlinked('slept 7 hours');
+ await unlinked('ate oatmeal');
+ await unlinked('ran 5k');
+ const target = await unlinked('Acme Example raised a seed round');
+ const first = await relink({ limit: 2 });
+ expect(first.has_more).toBe(true);
+ expect(first.status).toBe('partial');
+ expect(first.skipped.no_mention).toBe(2);
+ const second = await relink({ limit: 2, afterId: first.next_after_id! });
+ expect(second.linked).toBe(1);
+ expect(second.has_more).toBe(false);
+ expect((await row(target)).entity_slug).toBe('companies/acme-example');
+ });
+
+ test('linked facts are queued for the conflict sweep only when the slot is on', async () => {
+ const off = await unlinked('Acme Example raised a seed round');
+ await relink();
+ expect((await engine.executeRaw('SELECT 1 FROM decide_sweep_deferred WHERE fact_id = $1', [off])).length).toBe(0);
+ await engine.setConfig('decide.slots.conflict.mode', 'shadow');
+ const on = await unlinked('Alice Example prefers async updates');
+ const report = await relink();
+ expect(report.queued_for_conflict).toBe(1);
+ const [q] = await engine.executeRaw<{ attempts: number; reason: string }>('SELECT attempts, reason FROM decide_sweep_deferred WHERE fact_id = $1', [on]);
+ expect(q).toEqual({ attempts: 0, reason: 'relinked' });
+ });
+
+ test('a fact changed after it was pinned is refused as revision_conflict', async () => {
+ const id = await unlinked('Acme Example raised a seed round');
+ const [snap] = await readFacts(engine, 'default', [id]);
+ const hash = relinkFactHash(snap!);
+ await engine.executeRaw(`UPDATE facts SET visibility = 'private' WHERE id = $1`, [id]);
+ const result = await submitRelinkGroup(engine, config, 'default', 'companies/acme-example', {
+ kind: 'relink_facts', run_id: 'r1', queue_conflict: false, facts: [{ id, hash, tier: 'mention', model: null, note: 'entity relinked from mention' }],
+ });
+ expect(result.ok).toBe(true);
+ if (result.ok) expect(result.outcome.skipped).toEqual([{ id, reason: 'revision_conflict' }]);
+ expect((await row(id)).entity_slug).toBeNull();
+ });
+
+ test('a malformed entity fence is skipped as fence_malformed', async () => {
+ await importFromContent(engine, 'companies/acme-example',
+ '---\ntitle: Acme Example\ntype: company\n---\n\n# Acme Example\n\n## Facts\n\n<!--- gbrain:facts:begin -->\n| # | claim |\n|---|---|\n| x | broken |\n<!--- gbrain:facts:end -->\n', { noEmbed: true });
+ const id = await unlinked('Acme Example raised a seed round');
+ const report = await relink();
+ expect(report.skipped.fence_malformed).toBe(1);
+ expect((await row(id)).entity_slug).toBeNull();
+ });
+});
+
+describe('relink model tier', () => {
+ let calls: string[];
+ const answer = (subjects: Array<string | null>): ChatResult => ({
+ text: JSON.stringify({ subjects: subjects.map((subject, i) => ({ i, subject })) }), blocks: [], stopReason: 'end',
+ usage: { input_tokens: 100, output_tokens: 20, cache_read_tokens: 0, cache_creation_tokens: 0 }, model: 'anthropic:claude-haiku-4-5', providerId: 'anthropic',
+ } as never);
+
+ beforeEach(async () => {
+ calls = [];
+ configureGateway({ chat_model: 'anthropic:claude-haiku-4-5', env: { ANTHROPIC_API_KEY: 'sk-test' } } as never);
+ await engine.setConfig('facts.extraction_model', 'anthropic:claude-haiku-4-5');
+ });
+
+ test('a verified model answer links; NONE is memoized and not asked again', async () => {
+ await importFromContent(engine, 'projects/apollo-example', '---\ntitle: Apollo Example\ntype: project\n---\n\n# Apollo Example\n', { noEmbed: true });
+ const linked = await unlinked('the apollo example launch slipped two weeks');
+ const none = await unlinked('slept 7 hours');
+ __setChatTransportForTests(async (o: ChatOpts) => {
+ calls.push(String(o.messages[0]!.content));
+ const facts = String(o.messages[0]!.content).split('\n');
+ return answer(facts.map(f => f.includes('apollo') ? 'apollo example' : null));
+ });
+ const report = await relink({ llm: true });
+ expect(report.linked_by_tier.model).toBe(1);
+ expect((await row(linked)).entity_slug).toBe('projects/apollo-example');
+ expect(String((await row(linked)).context)).toContain('entity relinked by model (anthropic:claude-haiku-4-5)');
+ expect(report.skipped.no_subject).toBe(1);
+ const [memo] = await engine.executeRaw<{ outcome: string }>('SELECT outcome FROM fact_relink_attempts WHERE fact_id = $1', [none]);
+ expect(memo!.outcome).toBe('no_subject');
+ calls = [];
+ const second = await relink({ llm: true });
+ expect(calls.length).toBe(0);
+ expect(second.skipped.no_subject).toBe(1);
+ await relink({ llm: true, retryModel: true });
+ expect(calls.length).toBe(1);
+ });
+
+ test('an answer that does not quote the fact never links', async () => {
+ await unlinked('the launch slipped two weeks');
+ __setChatTransportForTests(async () => answer(['Acme Example']));
+ const report = await relink({ llm: true });
+ expect(report.linked).toBe(0);
+ expect(report.skipped.unverified_match).toBe(1);
+ });
+
+ test('private facts stay out of the model tier unless --include-private', async () => {
+ await unlinked('the launch slipped two weeks', { visibility: 'private' });
+ __setChatTransportForTests(async (o: ChatOpts) => { calls.push(String(o.messages[0]!.content)); return answer([null]); });
+ const report = await relink({ llm: true });
+ expect(calls.length).toBe(0);
+ expect(report.private_excluded_from_model).toBe(1);
+ await relink({ llm: true, includePrivate: true });
+ expect(calls.length).toBe(1);
+ });
+
+ test('a provider failure is model_unavailable and not memoized', async () => {
+ const id = await unlinked('the launch slipped two weeks');
+ __setChatTransportForTests(async () => { throw new Error('provider down'); });
+ const report = await relink({ llm: true });
+ expect(report.skipped.model_unavailable).toBe(1);
+ expect((await engine.executeRaw('SELECT 1 FROM fact_relink_attempts WHERE fact_id = $1', [id])).length).toBe(0);
+ });
+
+ test('the budget cap stops the model tier and reports budget_exhausted', async () => {
+ for (let i = 0; i < 30; i++) await unlinked(`note number ${i} about nothing`);
+ __setChatTransportForTests(async (o: ChatOpts) => { calls.push('x'); return answer(String(o.messages[0]!.content).split('\n').map(() => null)); });
+ const report = await relink({ llm: true, maxUsd: 0.0000001 });
+ expect(report.stopped).toBe('budget_exhausted');
+ expect(report.status).toBe('partial');
+ expect(report.skipped.budget_exhausted).toBeGreaterThan(0);
+ });
+
+ test('--no-llm makes zero model calls', async () => {
+ await unlinked('the launch slipped two weeks');
+ __setChatTransportForTests(async () => { calls.push('x'); return answer([null]); });
+ await relink({ llm: false });
+ expect(calls.length).toBe(0);
+ });
+});