Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Check that the member-page withdrawal migration runs once and never locks the facts table
garrytanbf21bdad36c0
diff --git a/scripts/e2e-backend-matrix.txt b/scripts/e2e-backend-matrix.txtindex 43f3652c34..a87908a16e 100644--- a/scripts/e2e-backend-matrix.txt+++ b/scripts/e2e-backend-matrix.txt@@ -47,6 +47,7 @@ test/e2e/takes-scorecard-parity.test.tstest/e2e/evidence-delivery-parity.test.tstest/e2e/apply-migrations-orchestration-lock.test.tstest/e2e/schema-migration-lock-postgres.test.ts+test/e2e/member-page-withdrawal-scope-migration.test.tstest/e2e/postgres-poisoned-connection.test.tstest/e2e/write-attribution-postgres.test.tstest/e2e/write-attribution-timeline-10k-postgres.test.tsdiff --git a/scripts/e2e-weights.json b/scripts/e2e-weights.jsonindex f271af0c75..cb1e60ad5d 100644--- a/scripts/e2e-weights.json+++ b/scripts/e2e-weights.json@@ -419,6 +419,7 @@"test/e2e/mcp-budget-reservation-postgres.test.ts": 440,"test/e2e/mcp-search-transport-matrix.test.ts": 67200,"test/e2e/mechanical.test.ts": 51100,+ "test/e2e/member-page-withdrawal-scope-migration.test.ts": 3200,"test/e2e/memory-safety-wave-postgres.test.ts": 6930,"test/e2e/mentions-parity.test.ts": 2590,"test/e2e/migrate-chain.test.ts": 5660,diff --git a/scripts/postgres-arm-weights.json b/scripts/postgres-arm-weights.jsonindex 3a7744bb63..87d2bd9677 100644--- a/scripts/postgres-arm-weights.json+++ b/scripts/postgres-arm-weights.json@@ -317,7 +317,7 @@"test/origin-twins-6948.test.ts": 5322,"test/orphan-effect-diagnostics-7985.test.ts": 13288,"test/overwritten-edits-6712.test.ts": 58270,- "test/owner-facts-d1-gst2346.test.ts": 24367,+ "test/owner-facts-d1-gst2346.test.ts": 28215,"test/owner-member-boundary.test.ts": 8601,"test/owner-reads-restricted.test.ts": 75912,"test/owner-refusal.test.ts": 6032,diff --git a/test/e2e/member-page-withdrawal-scope-migration.test.ts b/test/e2e/member-page-withdrawal-scope-migration.test.tsnew file mode 100644index 0000000000..7a1ba016f3--- /dev/null+++ b/test/e2e/member-page-withdrawal-scope-migration.test.ts@@ -0,0 +1,93 @@+/**+ * GST-2346: the member_page_withdrawal_scope migration re-creates the two fact insert guards+ * (gbrain_preserve_fact_withdrawal, gbrain_guard_inserted_facts) and nothing else. Each arm stubs both functions,+ * winds the brain back one schema version and runs two migration runners at once, then expects both to succeed,+ * the brain at the latest version and both guards back with the member-page rule. On Postgres (direct, and through+ * a transaction-mode PgBouncer: this file is in scripts/e2e-backend-matrix.txt) the runners do it while another+ * connection holds ROW EXCLUSIVE on facts, so a step that locked the table (an index or trigger rebuild) would wait+ * on that transaction and fail the bound. PGLite admits one connection, so its arm checks the two runners only.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import postgres from '#postgres';+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import { PostgresEngine } from '../../src/core/postgres-engine.ts';+import { LATEST_VERSION, MIGRATIONS, runMigrations } from '../../src/core/migrate.ts';+import type { BrainEngine } from '../../src/core/engine.ts';+import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';+import { probeDatabase } from '../helpers/schema-lock-probe.ts';+import { withEnv } from '../helpers/with-env.ts';++const DATABASE_URL = process.env.DATABASE_URL;+const GUARDS = ['gbrain_guard_inserted_facts', 'gbrain_preserve_fact_withdrawal'];+const migration = MIGRATIONS.find(m => m.name === 'member_page_withdrawal_scope')!;++/** Replaces both guards with bodies that mark nothing, and winds the brain back to just before the migration. */+async function windBack(engine: BrainEngine): Promise<void> {+ await engine.executeRaw('CREATE OR REPLACE FUNCTION gbrain_preserve_fact_withdrawal() RETURNS trigger LANGUAGE plpgsql AS $fn$ BEGIN RETURN NEW; END $fn$');+ await engine.executeRaw('CREATE OR REPLACE FUNCTION gbrain_guard_inserted_facts() RETURNS trigger LANGUAGE plpgsql AS $fn$ BEGIN RETURN NULL; END $fn$');+ await engine.setConfig('version', String(migration.version - 1));+}++async function expectGuardsRestored(engine: BrainEngine): Promise<void> {+ expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));+ const rows = await engine.executeRaw<{ proname: string; member_rule: boolean }>(+ "SELECT proname, position('memory/members/' in prosrc) > 0 AS member_rule FROM pg_proc WHERE proname = ANY($1::text[]) ORDER BY proname", [GUARDS]);+ expect(rows).toEqual(GUARDS.map(proname => ({ proname, member_rule: true })));+}++async function bounded<T>(label: string, ms: number, work: Promise<T>): Promise<T> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ const deadline = new Promise<never>((_, reject) => { timer = setTimeout(() => reject(new Error(`${label} did not finish within ${ms}ms`)), ms); });+ try { return await Promise.race([work, deadline]); } finally { clearTimeout(timer); }+}++const settled = (results: PromiseSettledResult<unknown>[]) => results.map(r => r.status === 'fulfilled' ? 'ok' : (r.reason as Error).message);+const twoRunners = (a: BrainEngine, b: BrainEngine) => withEnv({ GBRAIN_INITSCHEMA_LOCK_TIMEOUT_SECONDS: '90' }, () =>+ bounded('both runners', 60_000, Promise.allSettled([runMigrations(a), runMigrations(b)])));++test('the migration is the latest one and is idempotent', () => {+ expect(migration.version).toBe(LATEST_VERSION);+ expect(migration.idempotent).toBe(true);+});++describe('member_page_withdrawal_scope migration under two runners (PGLite)', () => {+ let engine: PGLiteEngine;+ beforeAll(() => withEnv({ GBRAIN_PGLITE_SNAPSHOT: undefined }, async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ }), 120_000);+ afterAll(async () => { await engine.disconnect(); });++ test('two runMigrations calls both succeed and restore both guards', async () => {+ await windBack(engine);+ expect(settled(await twoRunners(engine, engine))).toEqual(['ok', 'ok']);+ await expectGuardsRestored(engine);+ }, 120_000);+});++describe.skipIf(!DATABASE_URL)(`member_page_withdrawal_scope migration under two runners [${process.env.GBRAIN_TEST_BACKEND ?? 'postgres'}] (E2E)`, () => {+ test('two runners finish while another connection holds ROW EXCLUSIVE on facts, and restore both guards', async () => {+ assertSafeE2eDatabaseUrl(DATABASE_URL!);+ const db = await probeDatabase(DATABASE_URL!);+ const a = new PostgresEngine();+ const b = new PostgresEngine();+ const holder = postgres(db.url, { max: 1, prepare: false, onnotice: () => {} });+ try {+ await a.connect({ database_url: db.url, poolSize: 4 });+ await b.connect({ database_url: db.url, poolSize: 4 });+ await windBack(a);+ const results = await holder.begin(async sql => {+ await sql`LOCK TABLE facts IN ROW EXCLUSIVE MODE`;+ return twoRunners(a, b);+ });+ expect(settled(results)).toEqual(['ok', 'ok']);+ await expectGuardsRestored(a);+ } finally {+ await holder.end();+ await a.disconnect();+ await b.disconnect();+ await db.drop();+ }+ }, 240_000);+});diff --git a/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json b/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.jsonindex 0b2ada8737..afb3cffa6d 100644--- a/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json+++ b/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json@@ -618,13 +618,13 @@"gbrain_graduation_fence()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=cd4642fe8deee2a3c14ef41f53862f0a449b2d8a10e47177bd4f5d2e05ce8803","gbrain_guard_deferred_request()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=edf066d51ee76d4037663393271883a12d7fdf4f0f01b7d1faa9e979d552c6c6","gbrain_guard_effect_protocol()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=aa01528006dd7029eac6808e0233991524c9de9dc14aaf41f44160d67a747042",- "gbrain_guard_inserted_facts()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=027c16bf0f61101510f54966dfa7d3c5f90112cef52a51861adc36103a0bc02b",+ "gbrain_guard_inserted_facts()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=78fe53917ec54c7627df4961a7669e0d65f8adfa7e249da1beeb58dc37978a66","gbrain_guard_protocol_activation()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=bb477ec66ecac06e6edba773a94a2834ef4286a0b9e5cfa8b9b8646e003b0b9e","gbrain_guard_request_protocol()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=2709686143e064b577e2b3c8e3e2b1009d52a836a2bc709f5f003a251eb004d2","gbrain_guard_skill_publication()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=e854c0d058e75a65c3a90127c5c417380975100e83fc34288f6085e786df10a9","gbrain_lease_shared_skill_delivery()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=5e493c1ac8dce8cb025a736629e563437c4b214744137c302c532c20bf7e7acf","gbrain_mark_derivation_scope_entry()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=72ac0027fd37ecc4c72ad2e294ae3a98564736339b9ac917ea5d4112ec3cd35f",- "gbrain_preserve_fact_withdrawal()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=9beb118551a08451a228146373f50f2aa2ce0cfe89937e937d361b33224aa0aa",+ "gbrain_preserve_fact_withdrawal()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=6677571f69ebfb82f183514caf6b94ccaaa0fda3a4326cf7601d21f155936c15","gbrain_queue_page_projection()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=dc7a279205df83f00b9147adea8e609a410d0484d04648ea3fdb4344a5caf259","gbrain_refuse_purged_fact()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=d1d584b91ceea7f4e63c235b790ef89c958c578e635f77ddd644918bc36d33f1","gbrain_refuse_purged_page()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=8aaaf7c22fc9acb4b1e84fe75f11ae6c889949b78de1ef6bdba1db54ef43844e",