Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import type { BrainEngine } from '../src/core/engine.ts';
import type { OperationContext } from '../src/core/ops/contract.ts';
import { operationsByName } from '../src/core/operations.ts';
import { parseFactsFence, upsertFactRow } from '../src/core/facts-fence.ts';
import { submitRememberMutation, submitForgetMutation, prepareMemoryMutation } from '../src/core/persistence/memory-mutations.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';
import { registerLocalWriter } from '../src/core/persistence/identity.ts';
import { submissionAuthority } from '../src/core/persistence/authority.ts';
import { admitWrite, claimNextWrite, getWriteRequest, getWriteRequestById, receiptFor } from '../src/core/persistence/journal.ts';
import { PersistenceConsumer } from '../src/core/persistence/consumer.ts';
import { waitFor } from './helpers/wait-for.ts';
import { legacyForget } from './helpers/legacy-forget.ts';
import { cancelWriteRequest } from '../src/core/persistence/control.ts';
import { publishMutation } from '../src/core/persistence/coordinator.ts';
import { configureGateway, resetGateway, __setEmbedTransportForTests } from '../src/core/ai/gateway.ts';
import { LEGACY_EMBEDDING_CONFIG } from './helpers/legacy-embedding-config.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
const engines: BrainEngine[] = [];
let closePostgres: (() => Promise<void>) | undefined;
const sourceId = 'managed-memory-concurrency-test';
const context = (engine: BrainEngine, remote = false): OperationContext => ({ engine, remote, sourceId,
config: { engine: engine.kind }, dryRun: false, logger: { info() {}, warn() {}, error() {} } });
const pageInput = (body: string) => ({ type: 'person', title: 'Example', compiled_truth: body, timeline: 'Existing timeline', frontmatter: {} });
async function setupPage(engine: BrainEngine, slug: string, body = 'Existing biography') {
return engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], async () => {
await tx.putPage(slug, pageInput(body), { sourceId });
await tx.addTag(slug, 'existing-tag', { sourceId });
return (await tx.readPageSnapshot(slug, { sourceId }))!;
}, TEST_WRITE_ATTRIBUTION));
}
beforeAll(async () => {
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} });
const lite = new PGLiteEngine(); await lite.connect({}); await lite.initSchema(); engines.push(lite);
if (process.env.DATABASE_URL) {
const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL); engines.push(pg.engine); closePostgres = pg.close;
}
for (const engine of engines) {
await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');
await engine.executeRaw('DELETE FROM sources WHERE id=$1', [sourceId]);
await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [sourceId]);
await registerLocalWriter(engine, 'cli'); await registerLocalWriter(engine, 'stdio');
await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');
}
}, 120_000);
afterAll(async () => {
for (const engine of engines) {
await disposePersistenceConsumer(engine);
await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');
await engine.executeRaw('DELETE FROM sources WHERE id=$1', [sourceId]);
await engine.disconnect();
}
await closePostgres?.();
resetGateway();
});
describe('journaled memory publication, both engines', () => {
test('remember provider preparation cooperatively aborts without terminal failure and retries the same receipt', async () => {
for (const engine of engines) {
await disposePersistenceConsumer(engine);
const slug = 'people/deadline-example';
const snapshot = await setupPage(engine, slug);
const authority = await submissionAuthority(context(engine), 'remember', sourceId, snapshot.sourceIncarnation, slug);
const intent = { fact: 'Synthetic provider deadline fact', provenance: 'deadline fixture', entity_slug: slug,
visibility: 'world', fence: true, valid_from: new Date().toISOString(), valid_until: null };
const row = await admitWrite(engine, { principal: authority.principal, operation: 'remember', sourceId,
sourceIncarnation: snapshot.sourceIncarnation, slug, pageId: snapshot.page.id, callerIntent: intent, intent, authority, requestId: randomUUID() });
let entered = false;
let aborted = false;
const release = Promise.withResolvers<void>();
const provider = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: async () => {
await release.promise;
return Response.json({ embeddings: [[1, ...Array(1535).fill(0)]], usage: { tokens: 0 } });
} });
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: { OPENAI_API_KEY: 'sk-test' } });
__setEmbedTransportForTests(async options => {
entered = true;
try { return await (await fetch(provider.url, { signal: options.abortSignal })).json() as never; }
catch (error) { aborted = options.abortSignal!.aborted; throw error; }
});
// The 50 ms budget is wall-clock from the claim, so on a loaded machine it can pass before the preparer reaches the
// provider; the provider abort is pinned on a signal aborted once the call is in flight, the budget on the consumer's own signal.
const cancel = new AbortController();
let preparation: Promise<unknown> | undefined;
const consumer = new PersistenceConsumer(engine, context(engine).config,
(e, current, config, signal) => preparation = prepareMemoryMutation(e, current, config, signal), { pollMs: 60000, preparationMs: 50 });
try {
const direct = prepareMemoryMutation(engine, row, context(engine).config, cancel.signal);
await waitFor(() => entered);
cancel.abort({ code: 'preparation_deadline' });
await expect(direct).rejects.toEqual({ code: 'preparation_deadline' });
expect(aborted).toBe(true);
consumer.start();
await waitFor(() => preparation !== undefined);
await expect(preparation).rejects.toEqual({ code: 'preparation_deadline' });
await waitFor(() => consumer.status().active_preparations === 0);
expect((await getWriteRequestById(engine, row.id))?.state).toBe('queued');
expect(await engine.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, intent.fact])).toHaveLength(0);
} finally { release.resolve(); provider.stop(true); await consumer.stop(); __setEmbedTransportForTests(null); configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} }); }
const retry = new PersistenceConsumer(engine, context(engine).config, prepareMemoryMutation, { pollMs: 50 });
try {
retry.start();
await waitFor(async () => (await getWriteRequestById(engine, row.id))?.state === 'committed');
expect(await engine.executeRaw('SELECT fact,source FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, intent.fact]))
.toEqual([{ fact: intent.fact, source: intent.provenance }]);
} finally { await retry.stop(); }
}
}, 15000);
test('slow healthy loopback preparation keeps an aged receipt nonterminal and replays without a provider call', async () => {
for (const engine of engines) {
await disposePersistenceConsumer(engine);
const slug = 'people/slow-provider-example';
await setupPage(engine, slug);
const release = Promise.withResolvers<void>();
let calls = 0;
const provider = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: async () => {
calls++;
await release.promise;
return Response.json({ embeddings: [[1, ...Array(1535).fill(0)]], usage: { tokens: 0 } });
} });
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: { OPENAI_API_KEY: 'sk-test' } });
__setEmbedTransportForTests(async options => await (await fetch(provider.url, { signal: options.abortSignal })).json() as never);
const params = { fact: 'Synthetic slow healthy fact', provenance: 'slow loopback fixture', entity: slug, request_id: randomUUID() };
const completion = submitRememberMutation(context(engine), params, 10000);
try {
await waitFor(async () => calls > 0 && (await engine.executeRaw<{ state: string }>(
'SELECT state FROM persistence_requests WHERE request_id=$1', [params.request_id]))[0]?.state === 'running');
await engine.executeRaw("UPDATE persistence_requests SET created_at=now()-interval '3 minutes' WHERE request_id=$1", [params.request_id]);
const [pending] = await engine.executeRaw<import('../src/core/persistence/model.ts').WriteRequest>('SELECT * FROM persistence_requests WHERE request_id=$1', [params.request_id]);
expect(pending.state).toBe('running');
expect(receiptFor(pending).diagnostic).toMatchObject({ reason: 'cause_unknown', next_action: 'inspect_owner' });
expect(await engine.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, params.fact])).toHaveLength(0);
release.resolve();
const result = await completion;
expect(result.state).toBe('committed');
await disposePersistenceConsumer(engine);
const callsBeforeReplay = calls;
expect(await submitRememberMutation(context(engine), params)).toEqual(result);
expect(calls).toBe(callsBeforeReplay);
expect(await engine.executeRaw('SELECT fact,source FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, params.fact]))
.toEqual([{ fact: params.fact, source: params.provenance }]);
} finally {
release.resolve(); await completion.catch(() => {}); await disposePersistenceConsumer(engine);
provider.stop(true); __setEmbedTransportForTests(null); configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} });
}
}
}, 20000);
test('same-ID remember replays frozen fields after entity and fact removal', async () => {
for (const engine of engines) {
const slug = 'people/replay-example';
await setupPage(engine, slug);
const params = { fact: 'Prefers morning meetings', provenance: 'test conversation', entity: slug, ttl: '30d', request_id: randomUUID() };
const first = await operationsByName.remember!.handler(context(engine), params) as Record<string, unknown>;
expect(first).toMatchObject({ status: 'inserted', entity_slug: slug, protocol_version: 1, state: 'committed', degraded_dedup: true });
await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], async () => {
await tx.executeRaw('DELETE FROM facts WHERE id=$1', [Number(first.id)]);
await tx.deletePage(slug, { sourceId });
}, TEST_WRITE_ATTRIBUTION));
const replay = await operationsByName.remember!.handler(context(engine), params);
expect(replay).toEqual(first);
await expect(operationsByName.remember!.handler(context(engine), { ...params, ttl: 'P30D' })).rejects.toMatchObject({ code: 'invalid_params', writeError: 'idempotency_conflict', protocolVersion: 1 });
}
});
test('concurrent requests preserve every append, tags, timeline and hidden fence rows', async () => {
for (const engine of engines) {
const slug = 'people/concurrent-example';
const hidden = upsertFactRow('Biography', { claim: 'Private sentinel', kind: 'fact', visibility: 'private', confidence: 1, notability: 'medium' }).body
+ '\n<!--- gbrain:takes:begin -->\nOpaque private take\n<!--- gbrain:takes:end -->\n';
const before = await setupPage(engine, slug, hidden);
const responses = await Promise.all(Array.from({ length: 6 }, (_, index) => submitRememberMutation(context(engine, index % 2 === 0), {
fact: `Independent remembered preference ${index}`, provenance: 'test conversation', entity: slug, request_id: randomUUID(),
}, 30_000)));
expect(new Set(responses.map(r => r.id)).size).toBe(6);
const after = (await engine.readPageSnapshot(slug, { sourceId }))!;
const facts = parseFactsFence(after.page.compiled_truth).facts;
expect(facts).toHaveLength(7);
expect(new Set(facts.map(f => f.rowNum)).size).toBe(7);
expect(after.page.compiled_truth).toContain('Private sentinel');
expect(after.page.compiled_truth).toContain('Opaque private take');
expect(after.page.timeline).toBe('Existing timeline');
expect(after.tags).toEqual(['existing-tag']);
expect(after.revision).not.toBe(before.revision);
const chunks = await engine.getChunks(slug, { sourceId });
expect(chunks.length).toBeGreaterThan(0);
expect(chunks.map(c => c.chunk_text).join('\n')).not.toContain('Private sentinel');
expect(chunks.map(c => c.chunk_text).join('\n')).not.toContain('Opaque private take');
}
}, 120_000);
test('remember numbers its fence row past a facts row the fence does not hold', async () => {
for (const engine of engines) {
const slug = 'people/row-floor-example';
const fenced = [1, 2].reduce(body => upsertFactRow(body, { claim: `Fenced claim ${body.length}`, kind: 'fact', visibility: 'world',
confidence: 1, notability: 'medium' }).body, 'Biography');
await setupPage(engine, slug, fenced);
await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], () => tx.executeRaw(
`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source, row_num, source_markdown_slug)
VALUES ($1, $2, 'Extracted occupant claim', 'fact', 'world', 'extraction fixture', 3, $2)`, [sourceId, slug]), TEST_WRITE_ATTRIBUTION));
const receipt = await submitRememberMutation(context(engine), { fact: 'Remembered past the occupant', provenance: 'test conversation',
entity: slug, request_id: randomUUID() }, 30_000);
expect(receipt).toMatchObject({ status: 'inserted', state: 'committed' });
const rows = await engine.executeRaw<{ fact: string; row_num: number }>(
'SELECT fact, row_num FROM facts WHERE source_id=$1 AND source_markdown_slug=$2 ORDER BY row_num', [sourceId, slug]);
expect(rows.map(r => [r.fact, Number(r.row_num)])).toEqual([['Extracted occupant claim', 3], ['Remembered past the occupant', 4]]);
const fence = parseFactsFence((await engine.readPageSnapshot(slug, { sourceId }))!.page.compiled_truth).facts;
expect(fence.map(f => f.rowNum)).toEqual([1, 2, 4]);
}
}, 60_000);
test('a remember prepared against a stale page re-prepares; every committed receipt is readable at acknowledgement', async () => {
let held = 0;
let gate = Promise.withResolvers<void>();
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: { OPENAI_API_KEY: 'sk-test' } });
__setEmbedTransportForTests((async (opts: { values: string[] }) => {
if (opts.values.some(v => v.endsWith('stale 99')) && ++held === 1) await gate.promise;
// Orthogonal vectors: no two facts are semantic duplicates of each other.
return { embeddings: opts.values.map(v => { const e = new Array(1536).fill(0); e[Number(/(\d+)$/.exec(v)?.[1] ?? 0) + 1] = 1; return e; }) };
}) as never);
try {
for (const engine of engines) {
const slug = 'people/stale-preparation-example';
held = 0;
gate = Promise.withResolvers<void>();
await setupPage(engine, slug, upsertFactRow('Biography', { claim: 'Seed fact', kind: 'fact', visibility: 'world', confidence: 1, notability: 'medium' }).body);
const claims = async () => parseFactsFence((await engine.readPageSnapshot(slug, { sourceId }))!.page.compiled_truth).facts.map(f => f.claim);
const stale = submitRememberMutation(context(engine), { fact: 'Held stale 99', provenance: 'test', entity: slug, request_id: randomUUID() }, 30_000);
await waitFor(() => held === 1, { label: `${engine.kind}: stale remember holding inside preparation` });
// A coordinated writer reads and rewrites the page under its guard, as the coordinator's own publication does.
await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], async () => {
await tx.lockPageKeys([{ sourceId, slug }]);
const s = (await tx.readPageSnapshot(slug, { sourceId }))!;
await tx.putPage(slug, { ...pageInput(upsertFactRow(s.page.compiled_truth, { claim: 'Out of journal 50', kind: 'fact', visibility: 'world',
confidence: 1, notability: 'medium' }).body), timeline: s.page.timeline }, { sourceId });
}, TEST_WRITE_ATTRIBUTION));
gate.resolve();
expect((await stale).state).toBe('committed');
expect(held).toBe(2);
expect(await claims()).toEqual(['Seed fact', 'Out of journal 50', 'Held stale 99']);
const unreadable: string[] = [];
const receipts = await Promise.all(Array.from({ length: 6 }, async (_, i) => {
const fact = `Concurrent preference ${i}`;
const receipt = await submitRememberMutation(context(engine, i % 2 === 0), { fact, provenance: 'test', entity: slug, request_id: randomUUID() }, 30_000);
if (!(await claims()).includes(fact)) unreadable.push(fact);
return receipt;
}));
expect(receipts.map(r => r.state)).toEqual(Array(6).fill('committed'));
expect(unreadable).toEqual([]);
const facts = parseFactsFence((await engine.readPageSnapshot(slug, { sourceId }))!.page.compiled_truth).facts;
expect(facts).toHaveLength(9);
expect(new Set(facts.map(f => f.rowNum)).size).toBe(9);
await waitFor(async () => (await engine.executeRaw(`SELECT 1 FROM persistence_effects e JOIN persistence_requests r ON r.id=e.request_id
WHERE r.source_id=$1 AND e.state IN ('queued','running')`, [sourceId])).length === 0, { label: `${engine.kind}: ${sourceId} effects settled` });
}
} finally { gate.resolve(); __setEmbedTransportForTests(null); configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} }); }
}, 120_000);
test('concurrent identical request IDs allocate exactly one fact and one receipt', async () => {
for (const engine of engines) {
const p = { fact: 'A subjectless stable memory', provenance: 'test conversation', request_id: randomUUID() };
const responses = await Promise.all(Array.from({ length: 6 }, () => submitRememberMutation(context(engine), p, 30_000)));
expect(new Set(responses.map(r => r.id)).size).toBe(1);
expect(responses.every(r => r.state === 'committed')).toBe(true);
const rows = await engine.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, p.fact]);
expect(rows).toHaveLength(1);
}
});
test('semantic supersession updates fence and index atomically without private-candidate leaks', async () => {
let providerCalls = 0;
configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: 1536,
env: { OPENAI_API_KEY: 'sk-test-managed-memory' } });
__setEmbedTransportForTests((async (opts: { values: string[] }) => {
providerCalls += opts.values.length;
return { embeddings: opts.values.map(() => [1, ...new Array(1535).fill(0)]) };
}) as never);
try {
for (const engine of engines) {
const slug = 'people/supersede-example';
// Calibrate the fixed test embedder: an uncalibrated model never supersedes by cosine (supersession-threshold.ts).
await engine.setConfig('facts.supersession_thresholds', JSON.stringify({ 'openai:text-embedding-3-small@1536': 0.95 }));
await setupPage(engine, slug);
const privateMemory = await submitRememberMutation(context(engine), { fact: 'Confidential unrelated claim',
provenance: 'test', entity: slug, visibility: 'private' }, 30_000);
const first = await submitRememberMutation(context(engine, true), { fact: 'Works at acme-example',
provenance: 'test', entity: slug }, 30_000);
expect(first.status).toBe('inserted');
expect(first.degraded_dedup).toBeUndefined();
expect(await engine.executeRaw('SELECT id FROM facts WHERE id=$1 AND embedding IS NOT NULL', [Number(first.id)])).toHaveLength(1);
const params = { fact: 'Left acme-example', provenance: 'test', entity: slug, request_id: randomUUID() };
const next = await submitRememberMutation(context(engine, true), params, 30_000);
expect(next.status).toBe('superseded');
// Every effect in the source settles, not only this page's: an earlier test's page keeps an embedding
// effect queued until a provider is configured, and its calls must not land inside the replay window.
await waitFor(async () => (await engine.executeRaw(`SELECT 1 FROM persistence_effects e JOIN persistence_requests r ON r.id=e.request_id
WHERE r.source_id=$1 AND e.state IN ('queued','running')`, [sourceId])).length === 0,
{ label: `${engine.kind}: ${sourceId} effects settled before the replay count` });
const callsBeforeReplay = providerCalls;
expect(await submitRememberMutation(context(engine, true), params)).toEqual(next);
expect(providerCalls).toBe(callsBeforeReplay);
const rows = await engine.executeRaw<{ id: number; expired_at: Date | null; superseded_by: number | null }>(
'SELECT id,expired_at,superseded_by FROM facts WHERE id=ANY($1::int[]) ORDER BY id',
[[Number(privateMemory.id), Number(first.id), Number(next.id)]]);
expect(rows[0].expired_at).toBeNull();
expect(rows[1].expired_at).not.toBeNull();
expect(Number(rows[1].superseded_by)).toBe(Number(next.id));
expect(rows[2].expired_at).toBeNull();
const page = (await engine.readPageSnapshot(slug, { sourceId }))!;
const fence = parseFactsFence(page.page.compiled_truth).facts;
expect(fence.find(f => f.claim === 'Works at acme-example')?.active).toBe(false);
expect(fence.find(f => f.claim === 'Works at acme-example')?.supersededBy).toBe(fence.find(f => f.claim === 'Left acme-example')?.rowNum);
}
} finally {
__setEmbedTransportForTests(null);
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} });
}
});
test('a prepared remember cannot publish across a stored embedding generation change', async () => {
configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: 1536,
env: { OPENAI_API_KEY: 'synthetic-only' } });
__setEmbedTransportForTests((async (opts: { values: string[] }) => ({ embeddings: opts.values.map(() => [1, ...new Array(1535).fill(0)]) })) as never);
try {
for (const engine of engines) {
await disposePersistenceConsumer(engine);
const slug = 'people/generation-fence-example';
const snapshot = await setupPage(engine, slug);
const prior = await engine.getConfig('embedding_model');
const authority = await submissionAuthority(context(engine), 'remember', sourceId, snapshot.sourceIncarnation, slug);
const p = { fact: 'Synthetic generation-fenced memory', provenance: 'test', entity_slug: slug, visibility: 'world', fence: true,
valid_from: new Date().toISOString(), valid_until: null };
const admitted = await admitWrite(engine, { principal: authority.principal, operation: 'remember', sourceId, sourceIncarnation: snapshot.sourceIncarnation,
slug, pageId: snapshot.page.id, callerIntent: p, intent: p, authority, requestId: randomUUID() });
const row = (await claimNextWrite(engine, randomUUID()))!;
expect(row.id).toBe(admitted.id);
const prepared = await prepareMemoryMutation(engine, row, context(engine).config);
try {
await engine.setConfig('embedding_model', prior === 'openai:text-embedding-3-large' ? 'openai:text-embedding-3-small' : 'openai:text-embedding-3-large');
expect((await publishMutation(engine, row, prepared)).state).not.toBe('committed');
expect((await engine.readPageSnapshot(slug, { sourceId }))!.revision).toBe(snapshot.revision);
expect(await engine.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND fact=$2', [sourceId, p.fact])).toHaveLength(0);
} finally {
if (prior === null) await engine.executeRaw("DELETE FROM config WHERE key='embedding_model'");
else await engine.setConfig('embedding_model', prior);
await engine.executeRaw("UPDATE persistence_requests SET state='cancelled',error_code='cancelled' WHERE id=$1", [row.id]);
}
}
} finally {
__setEmbedTransportForTests(null);
configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} });
}
});
test('forget waits for an offline owner like every page write, withdraws nothing until it publishes, and replays after fact removal', async () => {
for (const engine of engines) {
const slug = 'people/withdraw-example';
await setupPage(engine, slug);
const remembered = await submitRememberMutation(context(engine), { fact: 'Withdraw this unique memory', provenance: 'test', entity: slug }, 30_000);
const before = (await engine.readPageSnapshot(slug, { sourceId }))!;
const worktreeId = randomUUID(); const offlineHost = randomUUID();
await engine.executeRaw('INSERT INTO persistence_worktrees(id,owner_host_id) VALUES($1::uuid,$2::uuid)', [worktreeId, offlineHost]);
await engine.executeRaw(`INSERT INTO persistence_source_bindings(source_id,source_incarnation,worktree_id)
VALUES($1,$2::uuid,$3::uuid)`, [sourceId, before.sourceIncarnation, worktreeId]);
// GBRA-190 G4: a withdrawal is a page publication (Git is the parent), so it waits for the source's owner as
// remember does; until it publishes nothing is withdrawn.
const local = await registerLocalWriter(engine, 'cli');
const principal = { kind: 'local_cli' as const, id: local.id };
const offline = randomUUID();
const pending = await submitForgetMutation(context(engine), 'forget', { id: remembered.id, request_id: offline }, 200).catch((error: unknown) => error);
expect(pending).toMatchObject({ code: 'write_pending' });
expect((await getWriteRequest(engine, principal, offline))!.worktree_id).toBe(worktreeId);
expect(await engine.executeRaw('SELECT 1 FROM fact_withdrawals WHERE source_id=$1', [sourceId])).toEqual([]);
expect(await engine.executeRaw('SELECT 1 FROM facts WHERE id=$1 AND expired_at IS NULL', [Number(remembered.id)])).toHaveLength(1);
expect((await engine.readPageSnapshot(slug, { sourceId }))!.revision).toBe(before.revision);
expect((await cancelWriteRequest(engine, principal, offline))?.state).toBe('cancelled');
// The source is database-only again (its synthetic offline owner unbound): the withdrawal publishes at once.
await engine.executeRaw('DELETE FROM persistence_source_bindings WHERE source_id=$1', [sourceId]);
const requestId = randomUUID();
const params = { id: remembered.id, reason: 'User correction', request_id: requestId };
const forgotten = await submitForgetMutation(context(engine), 'forget', params);
expect(forgotten).toMatchObject({ id: remembered.id, expired: true, reason: 'User correction', protocol_version: 1, state: 'committed' });
expect(JSON.stringify(forgotten)).not.toContain(slug);
const snapshot = (await engine.readPageSnapshot(slug, { sourceId }))!;
expect(snapshot.revision).not.toBe(before.revision);
expect(parseFactsFence(snapshot.page.compiled_truth).facts.find(f => f.claim === 'Withdraw this unique memory')?.forgotten).toBe(true);
const chunks = (await engine.getChunks(slug, { sourceId })).map(chunk => chunk.chunk_text).join('\n');
expect(chunks).toContain('Existing biography');
expect(chunks).not.toContain('Withdraw this unique memory');
const request = (await getWriteRequest(engine, principal, requestId))!;
expect(request.worktree_id).toBeNull();
expect(await engine.executeRaw("SELECT kind FROM persistence_effects WHERE request_id=$1::uuid AND kind='withdrawal-mirror'", [request.id])).toEqual([]);
await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], () => tx.executeRaw('DELETE FROM facts WHERE id=$1', [Number(remembered.id)]), TEST_WRITE_ATTRIBUTION));
expect(await submitForgetMutation(context(engine), 'forget', params)).toEqual(forgotten);
}
});
test('withdrawal invalidates a prepared remember before publication', async () => {
for (const engine of engines) {
// Dispose the resident consumer so this test controls the publication boundary.
await disposePersistenceConsumer(engine);
const slug = 'people/preparation-example';
await setupPage(engine, slug);
const old = await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], () => tx.insertFact({ fact: 'Claim withdrawn during preparation', source: 'test', entity_slug: slug, visibility: 'world' }, { source_id: sourceId }), TEST_WRITE_ATTRIBUTION));
const snapshot = (await engine.readPageSnapshot(slug, { sourceId }))!;
const authority = await submissionAuthority(context(engine), 'remember', sourceId, snapshot.sourceIncarnation, slug);
const p = { fact: 'Claim withdrawn during preparation', provenance: 'test', entity_slug: slug, visibility: 'world', fence: true,
valid_from: new Date().toISOString(), valid_until: null };
await admitWrite(engine, { principal: authority.principal, operation: 'remember', sourceId, sourceIncarnation: snapshot.sourceIncarnation,
slug, pageId: snapshot.page.id, callerIntent: p, intent: p, authority, requestId: randomUUID() });
const row = (await claimNextWrite(engine, randomUUID()))!;
const prepared = await prepareMemoryMutation(engine, row, context(engine).config);
// A current forget queues behind the claimed remember on its root; a gbrain before G4 sharing the brain still commits
// its withdrawal database-first, at any moment. The publication must refuse the withdrawn claim either way.
await legacyForget(context(engine), Number(old.id));
const result = await publishMutation(engine, row, prepared);
expect(result.state).not.toBe('committed');
const live = await engine.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND fact=$2 AND expired_at IS NULL', [sourceId, p.fact]);
expect(live).toHaveLength(0);
}
});
});