Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GBRA-218 A1: derived pages.page_readers column (trigger-maintained), fail-closed before backfill, gbrain repair access, member-access bench

garrytan74655fe5e319
diff --git a/scripts/bench-member-access.ts b/scripts/bench-member-access.ts
new file mode 100644
index 0000000000..942c92be97
--- /dev/null
+++ b/scripts/bench-member-access.ts
@@ -0,0 +1,114 @@
+/**
+ * GBRA-218 member read access: remote search latency and recall benchmark.
+ *
+ * Seeds a synthetic one-source brain on in-process PGLite (repo-less), with a
+ * share of pages naming readers, then runs remote search calls through MCP
+ * dispatch as serve --http does, for an unbound token and a member token.
+ * Reports p50/p95 per cell and hit@10 against an oracle (the local caller's
+ * ranking filtered to the pages that caller may read). PGLite is in-process, so
+ * runs on two revisions are directly comparable; on a revision without member
+ * tables only the 0% cell runs.
+ *
+ * Usage: bun scripts/bench-member-access.ts [--pages 5000] [--queries 60] [--json]
+ */
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { importFromContent } from '../src/core/import-file.ts';
+import { configureGateway, __setEmbedTransportForTests } from '../src/core/ai/gateway.ts';
+import { GBrainOAuthProvider } from '../src/core/oauth-provider.ts';
+import { sqlQueryForEngine } from '../src/core/sql-query.ts';
+import { mintLegacyToken } from '../src/core/token-mint.ts';
+import { noGrantFederatedScope } from '../src/core/source-resolver.ts';
+import { dispatchToolCall } from '../src/mcp/dispatch.ts';
+import type { AuthInfo } from '../src/core/ops/contract.ts';
+
+const arg = (name: string, def: number) => { const i = process.argv.indexOf(name); return i > 0 ? Number(process.argv[i + 1]) : def; };
+const PAGES = arg('--pages', 5000);
+const QUERIES = arg('--queries', 60);
+const JSON_MODE = process.argv.includes('--json');
+const DIMS = 1536;
+const SRC = 'bench-src';
+const WORDS = 'harbor ledger quarry beacon orchard summit lantern canyon meadow glacier river forest delta prairie island coral'.split(' ');
+
+function vectorFor(text: string): number[] {
+ const v = new Array<number>(DIMS).fill(0);
+ for (const w of text.toLowerCase().match(/[a-z]+/g) ?? []) {
+ let h = 0;
+ for (const ch of w) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
+ v[h % DIMS] += 1;
+ }
+ const n = Math.hypot(...v) || 1;
+ return v.map(x => x / n);
+}
+const pct = (xs: number[], p: number) => { const s = [...xs].sort((a, b) => a - b); return s[Math.min(s.length - 1, Math.floor(p * s.length))]; };
+
+async function runCell(share: number) {
+ const engine = new PGLiteEngine();
+ await engine.connect({ engine: 'pglite' } as never);
+ await engine.initSchema();
+ const hasMembers = (await engine.executeRaw<{ t: string | null }>("SELECT to_regclass('access_members')::text AS t"))[0]?.t != null;
+ if (share > 0 && !hasMembers) { await engine.disconnect(); return null; }
+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);
+ const aliceReads = new Set<string>();
+ const restrictedEvery = share > 0 ? Math.round(1 / share) : 0;
+ for (let i = 0; i < PAGES; i++) {
+ const slug = `notes/page-${i}`;
+ const words = [WORDS[i % WORDS.length], WORDS[(i * 7) % WORDS.length], WORDS[(i * 13 + 3) % WORDS.length]];
+ const restricted = share > 0 && (share >= 0.5 ? i % 5 !== 0 : i % restrictedEvery === 0);
+ const readers = restricted ? (i % 2 === 0 ? '[alice-example]' : '[group:team-b]') : null;
+ if (!restricted || i % 2 === 0) aliceReads.add(slug);
+ const fm = ['---', 'type: note', `title: Page ${i}`, ...(readers ? [`readable_by: ${readers}`] : []), '---'].join('\n');
+ await importFromContent(engine, slug, `${fm}\n# Page ${i}\n\nNotes about the ${words.join(' ')} for item ${i}.`, { sourceId: SRC });
+ }
+ if (hasMembers) await engine.executeRaw("INSERT INTO access_members (id) VALUES ('alice-example') ON CONFLICT DO NOTHING");
+ const mint = async (name: string) => mintLegacyToken(engine, { name, scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });
+ const unbound = await mint('bench-unbound');
+ const alice = await mint('bench-alice');
+ if (hasMembers) await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [alice.id, 'alice-example']);
+ const provider = new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });
+
+ async function remote(token: string, params: Record<string, unknown>) {
+ const auth = await provider.verifyAccessToken(token) as unknown as AuthInfo;
+ const sourceId = auth.sourceId ?? 'default';
+ const localFederated = await noGrantFederatedScope(engine, auth.hasSourceGrant, sourceId);
+ const t0 = performance.now();
+ const out = await dispatchToolCall(engine, 'search', params, { remote: true, transport: 'http', auth, sourceId,
+ takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'], ...(localFederated ? { localFederatedSourceIds: localFederated } : {}) });
+ return { ms: performance.now() - t0, text: out.content.map(c => c.text).join('\n') };
+ }
+ const slugsIn = (text: string) => [...new Set(text.match(/notes\/page-\d+/g) ?? [])];
+
+ const cells: Record<string, unknown>[] = [];
+ for (const [who, token, readable] of [['unbound', unbound.token, null], ['member', alice.token, aliceReads]] as const) {
+ for (const match of ['keyword', 'hybrid'] as const) {
+ const times: number[] = [];
+ let hits = 0, possible = 0;
+ for (let q = 0; q < QUERIES; q++) {
+ const query = `${WORDS[q % WORDS.length]} ${WORDS[(q * 5 + 1) % WORDS.length]}`;
+ const params = { query, limit: 10, ...(match === 'keyword' ? { match: 'keyword' } : {}) };
+ const { ms, text } = await remote(token, params);
+ if (q >= 5) times.push(ms);
+ if (readable && share > 0) {
+ const local = await dispatchToolCall(engine, 'search', { ...params, limit: 200 }, { remote: false, sourceId: SRC });
+ const oracle = slugsIn(local.content.map(c => c.text).join('\n')).filter(s => readable.has(s)).slice(0, 10);
+ const got = new Set(slugsIn(text));
+ hits += oracle.filter(s => got.has(s)).length;
+ possible += oracle.length;
+ }
+ }
+ cells.push({ share, who, match, p50: +pct(times, 0.5).toFixed(2), p95: +pct(times, 0.95).toFixed(2), ...(possible ? { hit_at_10: +(hits / possible).toFixed(3) } : {}) });
+ }
+ }
+ await engine.disconnect();
+ return cells;
+}
+
+configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: DIMS, env: { OPENAI_API_KEY: 'bench' } });
+__setEmbedTransportForTests((async ({ values }: { values: string[] }) => ({ embeddings: values.map(vectorFor) })) as never);
+const all: Record<string, unknown>[] = [];
+for (const share of [0, 0.2, 0.8]) {
+ const cells = await runCell(share);
+ if (cells) all.push(...cells);
+}
+if (JSON_MODE) console.log(JSON.stringify({ pages: PAGES, queries: QUERIES, cells: all }));
+else console.table(all);
+process.exit(0);
diff --git a/src/core/deletion-inventory.ts b/src/core/deletion-inventory.ts
index 86377568f6..eb593b4eeb 100644
--- a/src/core/deletion-inventory.ts
+++ b/src/core/deletion-inventory.ts
@@ -35,7 +35,10 @@ export interface InventoryEntry {
type Row = [table: string, cls: InventoryClass, reason: string, columns: string, probe?: string];
const ROWS: readonly Row[] = [
- ['access_tokens', 'out_of_scope', 'secret_material', 'name token_hash scopes permissions source_grant source_id federated_read allowed_operations takes_holders min_trust private_pages'],
+ ['access_group_members', 'out_of_scope', 'no_memory_text', 'group_id member_id'],
+ ['access_groups', 'out_of_scope', 'no_memory_text', 'id'],
+ ['access_members', 'out_of_scope', 'no_memory_text', 'id display_name'],
+ ['access_tokens', 'out_of_scope', 'secret_material', 'name token_hash scopes permissions source_grant source_id federated_read allowed_operations takes_holders min_trust private_pages member_id may_act_for'],
['belief_events', 'swept', 'belief_ledger', 'event_key source_id slug subject_kind subject_key event_type origin trust_tier writer_tier tier_basis visibility value value_hash prior_hash state_hash cause claim_fp redacted_fields'],
['belief_page_lifetimes', 'out_of_scope', 'text_free_ledger', 'source_id slug'],
['belief_redactions', 'out_of_scope', 'text_free_ledger', 'source_id subject_key field field_hash released_hash'],
@@ -105,7 +108,7 @@ const ROWS: readonly Row[] = [
['minion_lease_pressure_log', 'out_of_scope', 'no_memory_text', 'lease_key queue_name job_name model provider'],
['minion_self_fix_log', 'out_of_scope', 'no_memory_text', 'classifier_bucket policy_applied outcome'],
['needs_rederive', 'out_of_scope', 'text_free_ledger', 'derived_table derived_id source_id reason'],
- ['oauth_clients', 'out_of_scope', 'secret_material', 'client_id client_secret_hash client_name redirect_uris grant_types scope token_endpoint_auth_method source_id federated_read bound_tools bound_source_id bound_brain_id bound_slug_prefixes surface surface_set_by allowed_operations delegated_slug_prefixes delegated_namespace grant_profile grant_repair_reasons source_grant takes_holders min_trust registered_via private_pages'],
+ ['oauth_clients', 'out_of_scope', 'secret_material', 'client_id client_secret_hash client_name redirect_uris grant_types scope token_endpoint_auth_method source_id federated_read bound_tools bound_source_id bound_brain_id bound_slug_prefixes surface surface_set_by allowed_operations delegated_slug_prefixes delegated_namespace grant_profile grant_repair_reasons source_grant takes_holders min_trust registered_via private_pages member_id may_act_for'],
['oauth_codes', 'out_of_scope', 'secret_material', 'code_hash client_id scopes code_challenge code_challenge_method redirect_uri state resource'],
['oauth_grant_audit', 'out_of_scope', 'no_memory_text', 'client_id actor action before_grant after_grant'],
['oauth_tokens', 'out_of_scope', 'secret_material', 'token_hash token_type client_id scopes resource'],
@@ -127,7 +130,7 @@ const ROWS: readonly Row[] = [
['page_view_digests', 'out_of_scope', 'no_memory_text', 'revision view audience sha256'],
['page_versions', 'swept', 'page_versions', 'compiled_truth frontmatter timeline title type tags source_path write_principal_kind write_principal_id archived_principal_kind archived_principal_id trust_tier write_origin body_storage content_hash'],
['page_write_guards', 'out_of_scope', 'no_memory_text', 'slug'],
- ['pages', 'swept', 'page_body', 'source_id slug type page_kind title compiled_truth timeline frontmatter content_hash effective_date_source import_filename database_only_reason contextual_retrieval_mode corpus_generation search_vector source_path ingested_via source_uri source_kind embedding_signature revision_principal_kind revision_principal_id trust_tier write_origin body_storage body_flags truth_lead timeline_lead links_extract_failure'],
+ ['pages', 'swept', 'page_body', 'source_id slug type page_kind title compiled_truth timeline frontmatter content_hash effective_date_source import_filename database_only_reason contextual_retrieval_mode corpus_generation search_vector source_path ingested_via source_uri source_kind embedding_signature revision_principal_kind revision_principal_id trust_tier write_origin body_storage body_flags truth_lead timeline_lead links_extract_failure page_readers'],
['persistence_consumers', 'out_of_scope', 'no_memory_text', 'kind mode pool version nonce minted_under persistence_home host_json_path pid_ns boot_id process_start'],
['persistence_counters', 'out_of_scope', 'no_memory_text', 'key'],
['persistence_effects', 'swept', 'effect_recovery', 'kind data state source_id error_code outcome recovery claim_owner'],
diff --git a/src/core/engine-sql/pages.ts b/src/core/engine-sql/pages.ts
index 317affd78a..70b157aab6 100644
--- a/src/core/engine-sql/pages.ts
+++ b/src/core/engine-sql/pages.ts
@@ -1011,7 +1011,7 @@ export async function getVersions<B extends boolean = true>(
opts?: GetVersionsOpts<B>,
): Promise<PageVersionRows<B>> {
const privacy = opts?.excludePrivate
- ? trustedSql(`AND ${privatePagesFilterForCaller('p')} AND ${readersAllowedSql('pv', callerIdentity())} AND ${privacyForCaller('p.source_id', privateSnapshotFilterFragment('pv'))}`) : sqlFragment``;
+ ? trustedSql(`AND ${privatePagesFilterForCaller('p')} AND ${readersAllowedSql('pv', callerIdentity(), 'snapshot')} AND ${privacyForCaller('p.source_id', privateSnapshotFilterFragment('pv'))}`) : sqlFragment``;
const scope = opts?.sourceIds && opts.sourceIds.length > 0
? sqlFragment`AND p.source_id = ANY(${opts.sourceIds}::text[])`
: opts?.sourceId ? sqlFragment`AND p.source_id = ${opts.sourceId}` : sqlFragment``;
diff --git a/src/core/repair/access.ts b/src/core/repair/access.ts
new file mode 100644
index 0000000000..337d2c0428
--- /dev/null
+++ b/src/core/repair/access.ts
@@ -0,0 +1,34 @@
+/**
+ * `gbrain repair access` (GBRA-218): derive `pages.page_readers` for pages
+ * written before the member-access migration whose frontmatter already names
+ * readers. Until then such a page reads as nobody for every remote caller
+ * (fail closed), so the backfill only restores reads for listed members; it
+ * never widens anything. A trigger keeps the column current on every later
+ * write, so this runs once per upgraded brain. Updates only rows whose value
+ * changes (IS DISTINCT FROM), bumps no revision, content hash, generation or
+ * embedding state; no journal admission.
+ */
+import type { RepairHandler, RepairItem } from './core.ts';
+
+export const accessRepair: RepairHandler = {
+ kind: 'access',
+ publication: 'projection',
+ embeds: false,
+ async plan(engine, scope, after) {
+ const rows = await engine.executeRaw<{ id: number; source_id: string; slug: string }>(
+ `SELECT id, source_id, slug FROM pages
+ WHERE source_id = ANY($1::text[]) AND id > $2
+ AND (frontmatter ? 'readable_by' OR page_readers IS NOT NULL)
+ AND page_readers IS DISTINCT FROM gbrain_access_readers(frontmatter)
+ ORDER BY id`,
+ [scope.source_ids, after?.phase === 0 ? after.id : 0]);
+ const items: RepairItem[] = rows.map(row => ({ cursor: { phase: 0, id: Number(row.id) }, source_id: row.source_id, slug: row.slug, chars: 0, action: 'derive_page_readers' }));
+ return { items, residuals: {} };
+ },
+ async apply(ctx, item) {
+ const rows = await ctx.engine.executeRaw<{ id: number }>(
+ `UPDATE pages SET page_readers = gbrain_access_readers(frontmatter)
+ WHERE id = $1 AND page_readers IS DISTINCT FROM gbrain_access_readers(frontmatter) RETURNING id`, [item.cursor.id]);
+ return rows.length > 0;
+ },
+};
diff --git a/src/core/repair/core.ts b/src/core/repair/core.ts
index 0c3fd9f858..fd05685e66 100644
--- a/src/core/repair/core.ts
+++ b/src/core/repair/core.ts
@@ -51,7 +51,7 @@ import { classifyItemFailure, ItemRun, type ItemFailure } from '../item-failure.
import type { ProgressReporter } from '../progress.ts';
import type { GenerationsSelection } from '../generations/repair.ts';
-export const REPAIR_KINDS = ['timeline', 'visibility', 'safe-chunks', 'contextual-mode', 'connector-checkpoints', 'request-indexes', 'connector-fences', 'take-supersession', 'accepted-takes', 'orphan-bindings', 'embedding-effects', 'attribution-backfill', 'planner-stats', 'google-file-modes', 'stale-atoms', 'atoms-restore', 'extractor-facts', 'conversation-labels', 'captured-facts', 'redacted-facts', 'loop-facts', 'ontology-facts', 'facts-rehome', 'consolidate-ties', 'duplicate-facts', 'orphan-children', 'failed-writes', 'split-facts', 'frontmatter', 'fences', 'slug-conflicts', 'timeline-comments', 'timeline-order', 'managed-sync-orphans', 'content-receipt-root', 'frontmatter-keys', 'file-drift', 'stuck-recovery', 'ghost-pages', 'origin-twins', 'receipt-compaction', 'aliases', 'rename-identity', 'overwritten-edits', 'legacy-twins', 'missing-tables', 'url-clone-fences', 'segmented-mirror', 'uncommitted-deletions', 'generations', 'fact-claim-dates'] as const;
+export const REPAIR_KINDS = ['timeline', 'visibility', 'safe-chunks', 'contextual-mode', 'connector-checkpoints', 'request-indexes', 'connector-fences', 'take-supersession', 'accepted-takes', 'orphan-bindings', 'embedding-effects', 'attribution-backfill', 'planner-stats', 'google-file-modes', 'stale-atoms', 'atoms-restore', 'extractor-facts', 'conversation-labels', 'captured-facts', 'redacted-facts', 'loop-facts', 'ontology-facts', 'facts-rehome', 'consolidate-ties', 'duplicate-facts', 'orphan-children', 'failed-writes', 'split-facts', 'frontmatter', 'fences', 'slug-conflicts', 'timeline-comments', 'timeline-order', 'managed-sync-orphans', 'content-receipt-root', 'frontmatter-keys', 'file-drift', 'stuck-recovery', 'ghost-pages', 'origin-twins', 'receipt-compaction', 'aliases', 'rename-identity', 'overwritten-edits', 'legacy-twins', 'missing-tables', 'url-clone-fences', 'segmented-mirror', 'uncommitted-deletions', 'generations', 'fact-claim-dates', 'access'] as const;
export type RepairKind = typeof REPAIR_KINDS[number];
export interface RepairScope { brain_id: string; source_ids: string[] }
diff --git a/src/core/repair/registry.ts b/src/core/repair/registry.ts
index 13207f11e1..bc6f05fdf2 100644
--- a/src/core/repair/registry.ts
+++ b/src/core/repair/registry.ts
@@ -42,6 +42,7 @@ import { safeChunksRepair } from './safe-chunks.ts';
import { contextualModeRepair } from './contextual-mode.ts';
import { connectorCheckpointsRepair } from './connector-checkpoints.ts';
import { requestIndexesRepair } from './request-indexes.ts';
+import { accessRepair } from './access.ts';
import { connectorFencesRepair } from './connector-fences.ts';
import { takeSupersessionRepair } from './take-supersession.ts';
import { acceptedTakesRepair } from './accepted-takes.ts';
@@ -144,6 +145,11 @@ const SPECS: Record<RepairKind, Omit<RepairKindSpec, 'kind'>> = {
summary: 'Delete connector checkpoint rows and retry pointers that no registered connector source can load and that are older than 7 days (#5686). '
+ 'Cleanup only; no journal admission. Rows a pending write still references are kept. Brain-wide.',
},
+ access: {
+ handler: accessRepair, embeds: 'none', checks: ['access_page_readers_backfill'],
+ summary: 'Derive the readers of pages written before member access (GBRA-218) whose frontmatter already names readable_by. Until repaired such a page reads as nobody for every remote caller; '
+ + 'the repair only restores reads for the members it names and never widens anything. Updates only rows whose derived value changes; no revision, content hash or embedding change. No journal admission.',
+ },
'request-indexes': {
handler: requestIndexesRepair, embeds: 'none', checks: ['persistence_request_indexes'],
summary: 'Create a missing managed sync request index, or drop an INVALID one and rebuild it (#5762), so sync checkpoints validate within their '
diff --git a/src/core/schema-migrations/v274-member-read-access.ts b/src/core/schema-migrations/v274-member-read-access.ts
index 5955dad7d8..27f5d40f4e 100644
--- a/src/core/schema-migrations/v274-member-read-access.ts
+++ b/src/core/schema-migrations/v274-member-read-access.ts
@@ -38,6 +38,8 @@ CREATE TABLE IF NOT EXISTS access_group_members (
PRIMARY KEY (group_id, member_id)
);
CREATE INDEX IF NOT EXISTS access_group_members_member_idx ON access_group_members (member_id);
+ALTER TABLE pages ADD COLUMN IF NOT EXISTS page_readers TEXT[];
+CREATE INDEX IF NOT EXISTS pages_page_readers_gin ON pages USING gin (page_readers) WHERE page_readers IS NOT NULL;
ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS member_id TEXT;
ALTER TABLE access_tokens ADD COLUMN IF NOT EXISTS may_act_for TEXT[];
ALTER TABLE oauth_clients ADD COLUMN IF NOT EXISTS member_id TEXT;
@@ -74,6 +76,16 @@ BEGIN
RETURN NULL;
END
$fn$;
+CREATE OR REPLACE FUNCTION gbrain_access_derive_page_readers() RETURNS trigger
+ LANGUAGE plpgsql SET search_path = pg_catalog, public AS $fn$
+BEGIN
+ NEW.page_readers := CASE WHEN NEW.frontmatter ? 'readable_by' THEN gbrain_access_readers(NEW.frontmatter) ELSE NULL END;
+ RETURN NEW;
+END
+$fn$;
+DROP TRIGGER IF EXISTS pages_access_derive_readers ON pages;
+CREATE TRIGGER pages_access_derive_readers BEFORE INSERT OR UPDATE OF frontmatter, page_readers ON pages
+ FOR EACH ROW EXECUTE FUNCTION gbrain_access_derive_page_readers();
DROP TRIGGER IF EXISTS pages_access_mark_restricted ON pages;
CREATE TRIGGER pages_access_mark_restricted AFTER INSERT OR UPDATE OF frontmatter ON pages
FOR EACH ROW WHEN (NEW.frontmatter ? 'readable_by') EXECUTE FUNCTION gbrain_access_mark_restricted();
diff --git a/src/core/search/private-visibility.ts b/src/core/search/private-visibility.ts
index 75b0301b71..be02e466e2 100644
--- a/src/core/search/private-visibility.ts
+++ b/src/core/search/private-visibility.ts
@@ -72,10 +72,18 @@ export function accessAllowedSql(pageAlias: string, identity: readonly string[])
return `(${readersAllowedSql(pageAlias, identity)} AND NOT ${originRestrictedSql(pageAlias)})`;
}
-/** The readers test alone on any row with a `frontmatter` column (a page or a version snapshot), without lineage. */
-export function readersAllowedSql(alias: string, identity: readonly string[]): string {
+/**
+ * The readers test alone, without lineage. On a pages row it reads the
+ * derived `page_readers` column (a trigger keeps it equal to
+ * gbrain_access_readers(frontmatter)); a row that names readers but has no
+ * derived value yet (written before the migration, not yet repaired by
+ * `gbrain repair access`) reads as nobody. `row: 'snapshot'` evaluates a
+ * version snapshot's own frontmatter directly (bounded to one page's versions).
+ */
+export function readersAllowedSql(alias: string, identity: readonly string[], row: 'page' | 'snapshot' = 'page'): string {
const ids = identity.length && validIdentity(identity) ? identity : [];
- const listed = ids.length ? ` OR gbrain_access_readers(${alias}.frontmatter) && ARRAY[${ids.map(id => `'${id}'`).join(', ')}]::text[]` : '';
+ const readers = row === 'page' ? `${alias}.page_readers` : `gbrain_access_readers(${alias}.frontmatter)`;
+ const listed = ids.length ? ` OR ${readers} && ARRAY[${ids.map(id => `'${id}'`).join(', ')}]::text[]` : '';
return `(${alias}.frontmatter->'readable_by' IS NULL${listed})`;
}
diff --git a/test/access-principals.test.ts b/test/access-principals.test.ts
index 7949a4ac70..a408e9a780 100644
--- a/test/access-principals.test.ts
+++ b/test/access-principals.test.ts
@@ -58,6 +58,21 @@ describe('gbrain_access_readers parity', () => {
});
});
+describe('derived page_readers', () => {
+ test('a trigger keeps pages.page_readers equal to the normalization on insert, frontmatter change and direct writes', async () => {
+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', ['ap-src']);
+ await engine.putPage('notes/ap-readers', { type: 'note', title: 'A', compiled_truth: 'x', timeline: '', frontmatter: { readable_by: ['Bob-Example', 'group:team-a'] } }, { sourceId: 'ap-src' } as never);
+ const read = async () => (await engine.executeRaw<{ r: string[] | null }>("SELECT page_readers AS r FROM pages WHERE slug = 'notes/ap-readers' AND source_id = 'ap-src'"))[0].r;
+ expect(await read()).toEqual(['group:team-a', 'member:bob-example']);
+ await engine.executeRaw("UPDATE pages SET frontmatter = frontmatter || '{\"readable_by\": \"not valid\"}'::jsonb WHERE slug = 'notes/ap-readers'");
+ expect(await read()).toEqual([NOBODY]);
+ await engine.executeRaw("UPDATE pages SET page_readers = ARRAY['member:mallory-example'] WHERE slug = 'notes/ap-readers'");
+ expect(await read()).toEqual([NOBODY]);
+ await engine.executeRaw("UPDATE pages SET frontmatter = frontmatter - 'readable_by' WHERE slug = 'notes/ap-readers'");
+ expect(await read()).toBeNull();
+ });
+});
+
describe('principal rules', () => {
test('bare ids are members; prefixes and ASCII case fold; anything else is invalid', () => {
expect(normalizePrincipal('Alice-Example')).toBe('member:alice-example');
diff --git a/test/access-repair.test.ts b/test/access-repair.test.ts
new file mode 100644
index 0000000000..cd16672b42
--- /dev/null
+++ b/test/access-repair.test.ts
@@ -0,0 +1,59 @@
+/**
+ * GBRA-218: pages written before member access fail closed until repaired.
+ *
+ * 1. Protects: a page whose frontmatter named readable_by before the migration
+ * (no derived page_readers yet) is readable by no remote caller, the listed
+ * member included, and `gbrain repair access` restores exactly the listed
+ * member's read without widening anything else.
+ * 2. Fails when: an unrepaired row reads as open, the repair widens a read,
+ * touches a row whose value is already right, or bumps the page revision.
+ * 3. Real seams: migrated repo-less PGLite schema; the derive trigger disabled
+ * to reproduce a pre-migration row; the registered repair handler.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { privatePagesFilterForCaller, withCallerAccess } from '../src/core/search/private-visibility.ts';
+import { accessRepair } from '../src/core/repair/access.ts';
+
+let engine: PGLiteEngine;
+const SRC = 'ar-src';
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);
+ for (const slug of ['notes/old-restricted', 'notes/old-open', 'notes/new-restricted']) {
+ await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: 'x', timeline: '', frontmatter: {} }, { sourceId: SRC } as never);
+ }
+ await engine.executeRaw('ALTER TABLE pages DISABLE TRIGGER pages_access_derive_readers');
+ await engine.executeRaw(`UPDATE pages SET frontmatter = '{"readable_by": ["alice-example"]}'::jsonb WHERE slug = 'notes/old-restricted'`);
+ await engine.executeRaw('ALTER TABLE pages ENABLE TRIGGER pages_access_derive_readers');
+ await engine.executeRaw(`UPDATE pages SET frontmatter = '{"readable_by": ["alice-example"]}'::jsonb WHERE slug = 'notes/new-restricted'`);
+});
+afterAll(async () => { await engine.disconnect(); });
+
+async function visible(identity: string[]): Promise<string[]> {
+ return withCallerAccess({ identity, hidePrivate: true, privateReadableSources: [] }, async () =>
+ (await engine.executeRaw<{ slug: string }>(`SELECT p.slug FROM pages p WHERE p.source_id = $1 AND ${privatePagesFilterForCaller('p')} ORDER BY p.slug`, [SRC])).map(r => r.slug));
+}
+
+describe('repair access', () => {
+ test('an unrepaired row is readable by nobody, the listed member included', async () => {
+ expect(await visible(['member:alice-example'])).toEqual(['notes/new-restricted', 'notes/old-open']);
+ expect(await visible([])).toEqual(['notes/old-open']);
+ });
+
+ test('the repair plans only the stale row and restores the listed member alone, without a revision bump', async () => {
+ const scope = { brain_id: 'host', source_ids: [SRC] };
+ const plan = await accessRepair.plan(engine, scope, null);
+ expect(plan.items.map(i => i.slug)).toEqual(['notes/old-restricted']);
+ const [before] = await engine.executeRaw<{ rev: string; gen: number }>("SELECT knowledge_revision::text AS rev, generation AS gen FROM pages WHERE slug = 'notes/old-restricted'");
+ expect(await accessRepair.apply({ engine } as never, plan.items[0])).toBe(true);
+ expect(await accessRepair.apply({ engine } as never, plan.items[0])).toBe(false);
+ const [after] = await engine.executeRaw<{ rev: string; gen: number }>("SELECT knowledge_revision::text AS rev, generation AS gen FROM pages WHERE slug = 'notes/old-restricted'");
+ expect(after).toEqual(before);
+ expect(await visible(['member:alice-example'])).toEqual(['notes/new-restricted', 'notes/old-open', 'notes/old-restricted']);
+ expect(await visible(['member:bob-example'])).toEqual(['notes/old-open']);
+ expect((await accessRepair.plan(engine, scope, null)).items).toEqual([]);
+ });
+});