Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(rls): v275 lock-timeout refusal, repair-apply RLS path, segmented RLS list pinned to PAGE_BODY_RLS_TABLES (GBRA-95)
garrytana4f474f59062
diff --git a/test/e2e/rls-every-table-postgres.test.ts b/test/e2e/rls-every-table-postgres.test.tsindex f74552c066..3406d53b4f 100644--- a/test/e2e/rls-every-table-postgres.test.ts+++ b/test/e2e/rls-every-table-postgres.test.ts@@ -31,6 +31,10 @@ import { PostgresEngine } from '../../src/core/postgres-engine.ts';import { LATEST_VERSION } from '../../src/core/migrate.ts';import { v275 } from '../../src/core/schema-migrations/v275-rls-backfill-unguarded-tables.ts';import { recreateStep } from '../../src/core/schema-missing-tables.ts';+import { resolveRepairScope, runRepair } from '../../src/core/repair/core.ts';+import { missingTablesRepair } from '../../src/core/repair/missing-tables.ts';+import type { OperationContext } from '../../src/core/ops/contract.ts';+import { withEnv } from '../helpers/with-env.ts';const DATABASE_URL = process.env.DATABASE_URL;const describeE2E = DATABASE_URL ? describe : describe.skip;@@ -238,6 +242,38 @@ describeE2E('RLS on every public table (Postgres, no auto-RLS event trigger)', (}}, 300_000);+ // Generated by gstack /ship coverage audit (#7625).+ // Value: protects=v275 stops with the typed schema_lock_blocked refusal naming the table when another session holds a+ // lock, changes nothing, and succeeds once the lock is gone; fails_when=the SET LOCAL lock_timeout or the 55P03 mapping+ // is dropped (the upgrade hangs or throws an untyped error); why_new=no test holds a lock against v275; seam=none;+ // no_claim=the blocking-session sample (v275 passes sessions: null) and transaction-pooler behavior+ test('v275 refuses with schema_lock_blocked when another session holds a lock on a table it must protect, and finishes after', async () => {+ const { url, conn } = await scratchDatabase();+ const engine = new PostgresEngine();+ const holder = postgres(url, { max: 1, prepare: false, onnotice: () => {} });+ try {+ await freshInit(url);+ await engine.connect({ database_url: url });+ await conn.unsafe('ALTER TABLE page_derivation_spend DISABLE ROW LEVEL SECURITY');+ const session = await holder.reserve();+ try {+ await session.unsafe('BEGIN; LOCK TABLE page_derivation_spend IN ACCESS SHARE MODE');+ const error = await withEnv({ GBRAIN_SCHEMA_LOCK_TIMEOUT_SECONDS: '1' }, () => v275.handler!(engine).then(() => null, (e: unknown) => e));+ expect(error).toMatchObject({ code: 'schema_lock_blocked', detail: 'v275 on page_derivation_spend' });+ expect(await tablesWithoutRls(conn)).toEqual(['page_derivation_spend']);+ } finally {+ await session.unsafe('ROLLBACK');+ session.release();+ }+ await withEnv({ GBRAIN_SCHEMA_LOCK_TIMEOUT_SECONDS: '1' }, () => v275.handler!(engine));+ expect(await tablesWithoutRls(conn)).toEqual([]);+ } finally {+ await holder.end();+ await engine.disconnect();+ await conn.end();+ }+ }, 300_000);+test('gbrain repair missing-tables recreates a cache table with RLS on', async () => {const { url, conn } = await scratchDatabase();const engine = new PostgresEngine();@@ -250,6 +286,25 @@ describeE2E('RLS on every public table (Postgres, no auto-RLS event trigger)', (}expect(await presentTables(conn, ['code_traversal_cache', 'query_cache'])).toEqual(['code_traversal_cache', 'query_cache']);expect(await tablesWithoutRls(conn)).toEqual([]);++ // Generated by gstack /ship coverage audit (#7625).+ // Value: protects=the real repair apply, which runs the recreate step inside its own transaction and lock timeout,+ // leaves the recreated cache with RLS on; fails_when=protectTables breaks or is skipped when nested in the apply+ // transaction; why_new=the calls above run the step outside any transaction; seam=none;+ // no_claim=the CLI wrapper and preview printing (handler driven through runRepair)+ const home = makeHome({ engine: 'postgres', database_url: url });+ homes.push(home);+ await withEnv({ GBRAIN_HOME: home }, async () => {+ const ctx = { engine, sourceId: 'default', remote: false, dryRun: false, config: { engine: 'postgres', embedding_disabled: true },+ logger: { info() {}, warn() {}, error() {} } } as unknown as OperationContext;+ await conn.unsafe('DROP TABLE query_cache');+ const scope = await resolveRepairScope(engine);+ const preview = await runRepair(ctx, missingTablesRepair, scope, { apply: false, explicit: true });+ const applied = await runRepair(ctx, missingTablesRepair, scope, { apply: true, explicit: true, expect: preview.preview_hash });+ expect(applied.outcomes).toMatchObject({ recreated: 1 });+ });+ expect(await presentTables(conn, ['query_cache'])).toEqual(['query_cache']);+ expect(await tablesWithoutRls(conn)).toEqual([]);} finally {await engine.disconnect();await conn.end();diff --git a/test/e2e/segmented-rls-scope.test.ts b/test/e2e/segmented-rls-scope.test.tsindex 01e7a90b8f..0735a5fd43 100644--- a/test/e2e/segmented-rls-scope.test.ts+++ b/test/e2e/segmented-rls-scope.test.ts@@ -22,6 +22,7 @@ import { importFromContent } from '../../src/core/import-file.ts';import type { GBrainConfig } from '../../src/core/config.ts';import { operations, type OperationContext } from '../../src/core/operations.ts';import { materializeSegmentedBody } from '../../src/core/page-body/ingest.ts';+import { PAGE_BODY_RLS_TABLES as SCHEMA_PAGE_BODY_RLS_TABLES } from '../../src/core/page-body/schema.ts';import { PostgresEngine } from '../../src/core/postgres-engine.ts';import { conversationPage } from '../helpers/conversation-page.ts';import { withEnv } from '../helpers/with-env.ts';@@ -34,7 +35,8 @@ const LEGACY = `gbrain_seg_rls_legacy_${tag}`;const WRITER = `gbrain_seg_rls_writer_${tag}`;const SOURCES = { 'src-a': 'alphaneedle', 'src-b': 'bravoneedle' } as const;const slugOf = (id: string) => `notes/ledger-${id}`;-const PAGE_BODY_RLS_TABLES = ['page_body_manifest', 'page_body_staging_parts', 'page_body_staging_texts', 'page_body_stagings', 'page_segment_derivations', 'page_segment_texts', 'page_view_digests'];+const PAGE_BODY_RLS_TABLES = ['page_body_manifest', 'page_body_staging_parts', 'page_body_staging_texts', 'page_body_stagings', 'page_derivation_spend',+ 'page_segment_derivation_purge_pages', 'page_segment_derivation_purges', 'page_segment_derivations', 'page_segment_texts', 'page_view_digests'];/** What a search and a body read touch besides the recipe's tables, opened to the reader. */const SEARCH_SIDE_TABLES = ['config', 'timeline_entries'];@@ -97,6 +99,7 @@ const sourcesOf = (hits: Array<{ source_id?: unknown }>) => [...new Set(hits.map});test('B1: every schema replay re-enables RLS on the segment tables, so a role with SELECT and no policy reads no segment row, as it reads no page', async () => {+ expect(([...SCHEMA_PAGE_BODY_RLS_TABLES] as string[]).sort()).toEqual(PAGE_BODY_RLS_TABLES);for (const t of PAGE_BODY_RLS_TABLES) await engine.executeRaw(`ALTER TABLE ${t} DISABLE ROW LEVEL SECURITY`);await engine.initSchema();const state = await engine.executeRaw<{ relname: string; relrowsecurity: boolean }>(