Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(e2e): liveness counter checks read one ledger snapshot (#6696 A1)

garrytan94e0baf987c8
diff --git a/test/e2e/persistence-http-liveness.test.ts b/test/e2e/persistence-http-liveness.test.ts
index 37e946256b..da0f66e05c 100644
--- a/test/e2e/persistence-http-liveness.test.ts
+++ b/test/e2e/persistence-http-liveness.test.ts
@@ -34,13 +34,32 @@ describe.skipIf(!url)('authenticated PostgreSQL HTTP accepted-write liveness', (
let stderr = '';
const content = (text: string) => `---\ntitle: Synthetic note\ntype: note\n---\n\n${text}`;
// #6696 (A1): a request keeps its whole reservation until its last effect settles, then gives back what its receipt does not keep.
- // Settle the requests and run the (idempotent) true-up before reading counters, so a comparison measures the write, not effect timing.
+ // Settle the requests under test and run the (idempotent) true-up, so their own shrink lands before the first ledger read.
const settle = async (clientRequestIds: string[]) => {
const ids = (await pg.engine.executeRaw<{ id: string }>('SELECT id FROM persistence_requests WHERE request_id=ANY($1::uuid[])', [clientRequestIds])).map(row => row.id);
await waitFor(async () => (await pg.engine.executeRaw("SELECT 1 FROM persistence_effects WHERE request_id=ANY($1::uuid[]) AND state IN ('queued','running') LIMIT 1", [ids])).length === 0,
{ timeoutMs: 45000, label: 'request effects settled' });
await trueUpRequests(pg.engine, ids);
};
+ // Another write's true-up can still land at any time: a write made before an owner SIGKILL may hold an effect the dead owner
+ // claimed, which settles only when its 2-minute lease expires. So the counters and every request's reservation are read in ONE
+ // statement (one snapshot; a true-up changes both in one transaction), and `expectChargedNothing` requires request ids unchanged
+ // and receipt bytes changed by exactly the reservations given back between the two reads: nothing charged, nothing lost.
+ type Ledger = { counters: Array<{ key: string; lifetime_ids: string; terminal_bytes: string }>; reservations: Record<string, { bytes: string; principal: string }> };
+ const ledger = async (): Promise<Ledger> => (await pg.engine.executeRaw<Ledger>(`SELECT
+ (SELECT json_agg(json_build_object('key',key,'lifetime_ids',lifetime_ids::text,'terminal_bytes',terminal_bytes::text) ORDER BY key COLLATE "C") FROM persistence_counters) AS counters,
+ (SELECT COALESCE(json_object_agg(id,json_build_object('bytes',terminal_reservation::text,'principal','principal:'||principal_kind||':'||principal_id)),'{}'::json) FROM persistence_requests) AS reservations`))[0]!;
+ const expectChargedNothing = (before: Ledger, after: Ledger) => {
+ const freed = new Map<string, bigint>();
+ for (const [id, row] of Object.entries(after.reservations)) {
+ const prior = before.reservations[id];
+ expect({ id, admittedSince: prior === undefined }).toEqual({ id, admittedSince: false });
+ const delta = BigInt(prior!.bytes) - BigInt(row.bytes);
+ expect({ id, grew: delta < 0n }).toEqual({ id, grew: false });
+ for (const key of ['brain', row.principal]) freed.set(key, (freed.get(key) ?? 0n) + delta);
+ }
+ expect(after.counters).toEqual(before.counters.map(row => ({ ...row, terminal_bytes: String(BigInt(row.terminal_bytes) - (freed.get(row.key) ?? 0n)) })));
+ };
function cli(args: string[]): string {
const result = spawnSync('bun', ['--no-env-file', 'run', 'src/cli.ts', ...args], { env, encoding: 'utf8', timeout: 60000 });
@@ -172,7 +191,7 @@ describe.skipIf(!url)('authenticated PostgreSQL HTTP accepted-write liveness', (
await committed(replacement, put.request_id);
await committed(replacement, memory.request_id);
await settle([put.request_id, memory.request_id]);
- const before = await pg.engine.executeRaw('SELECT key,lifetime_ids,terminal_bytes FROM persistence_counters ORDER BY key');
+ const before = await ledger();
const snapshot = await pg.engine.readPageSnapshot(put.slug, { sourceId: source.id });
expect(snapshot?.page.compiled_truth).toBe(`Original ${auth} fixture`);
expect(readFileSync(join(source.root, `${put.slug}.md`), 'utf8')).toContain(`Original ${auth} fixture`);
@@ -182,7 +201,7 @@ describe.skipIf(!url)('authenticated PostgreSQL HTTP accepted-write liveness', (
{ fact: memory.fact, source: memory.provenance, source_id: source.id },
]);
expect((await call(replacement, 'put_page', { ...put, content: content('Changed intent') })).body.error).toBe('idempotency_conflict');
- expect(await pg.engine.executeRaw('SELECT key,lifetime_ids,terminal_bytes FROM persistence_counters ORDER BY key')).toEqual(before);
+ expectChargedNothing(before, await ledger());
await assertConservation(pg.engine);
}, 90000);
@@ -365,7 +384,7 @@ describe.skipIf(!url)('authenticated PostgreSQL HTTP accepted-write liveness', (
expect(exclusion).not.toBeNull();
await exclusion!.release();
expect((await pg.engine.readPageSnapshot(slug, { sourceId: source.id }))!.revision).toBe(before.revision);
- const counters = await pg.engine.executeRaw('SELECT key,lifetime_ids,terminal_bytes FROM persistence_counters ORDER BY key');
+ const counters = await ledger();
await assertConservation(pg.engine);
if (boundary === 'running') await pg.engine.transaction(async tx => {
await tx.executeRaw("SELECT set_config('gbrain.persistence_protocol','2',true)");
@@ -390,12 +409,10 @@ describe.skipIf(!url)('authenticated PostgreSQL HTTP accepted-write liveness', (
expect((await call(successor, 'put_page', args)).body.revision).toBe(done.revision);
expect(await pg.engine.executeRaw('SELECT id FROM persistence_requests WHERE request_id=$1::uuid', [args.request_id])).toEqual([{ id: retained.id }]);
expect(await pg.engine.executeRaw('SELECT id FROM page_versions WHERE page_id=$1', [before.page.id])).toHaveLength(versions.length + 1);
- // The handoff charges nothing twice: the only change since the retained snapshot is this request's settlement true-up.
+ // The handoff charges nothing twice: since the retained snapshot, receipt bytes moved only by true-ups (this request's and any
+ // earlier write's) and no request id was minted.
await settle([args.request_id]);
- const freed = BigInt(retained.terminal_reservation) - BigInt((await read()).terminal_reservation);
- const charged = new Set(['brain', `principal:${retained.principal_kind}:${retained.principal_id}`]);
- expect(await pg.engine.executeRaw('SELECT key,lifetime_ids,terminal_bytes FROM persistence_counters ORDER BY key')).toEqual(
- (counters as Array<{ key: string; lifetime_ids: bigint; terminal_bytes: bigint }>).map(row => charged.has(row.key) ? { ...row, terminal_bytes: BigInt(row.terminal_bytes) - freed } : row));
+ expectChargedNothing(counters, await ledger());
await assertConservation(pg.engine);
}, 90000);
}