Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: settle every source effect before the remember replay count; pin stale-preparation re-prepare

garrytana46bead438e8
diff --git a/test/persistence-memory-mutations.test.ts b/test/persistence-memory-mutations.test.ts
index d559d1ea98..122a2400c5 100644
--- a/test/persistence-memory-mutations.test.ts
+++ b/test/persistence-memory-mutations.test.ts
@@ -177,6 +177,50 @@ describe('journaled memory publication, both engines', () => {
}
}, 120_000);
+ test('a remember prepared against a stale page re-prepares; every committed receipt is readable at acknowledgement', async () => {
+ let held = 0;
+ const 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;
+ 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` });
+ await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], async () => {
+ 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() };
@@ -210,9 +254,11 @@ describe('journaled memory publication, both engines', () => {
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 r.slug=$2 AND e.state IN ('queued','running')`, [sourceId, slug])).length === 0,
- { label: `${engine.kind}: ${slug} effects settled before the replay count` });
+ 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);