Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: member facts, member-scoped caches and the all-ops member sweep on PGLite and Postgres (GST-359 C2)

garrytancb80db6e6a20
diff --git a/test/access-member-facts.test.ts b/test/access-member-facts.test.ts
new file mode 100644
index 0000000000..ef2804e7b7
--- /dev/null
+++ b/test/access-member-facts.test.ts
@@ -0,0 +1,253 @@
+/**
+ * GST-359 C2: member facts and member-scoped reads.
+ *
+ * 1. Protects: a fact a member saves remotely (as an attested actor or through a credential bound to the member)
+ * lands on that member's own page, memory/members/<member> (readable_by the member alone, created by the first
+ * fact, the entity kept as metadata), as a private row. Only that member reads it back: recall, hot memory and
+ * delta admit it for its owner, and no other member, the bare attest client or an unbound credential reads it by
+ * any path, the operator hatch included. Caches and session state keyed on the caller (hot memory, the delta
+ * cursor, write receipts) never cross from one person to another behind one credential and one session id.
+ * Dedup never merges two members' facts. A fence rebuild, relink and consolidation keep the fact on its page.
+ * 2. Fails when: a member fact lands on the entity page or as a world row, another member or the bare client reads
+ * it, a hot-memory payload, delta cursor or receipt warmed by Alice is served to Bob, Bob's identical fact
+ * deduplicates into Alice's, a re-import widens the row, or consolidation writes it as a take on the entity page.
+ * 3. Existing coverage: test/access-qm-actor.test.ts (C1) pins the actor itself; test/member-read-access.test.ts
+ * the page matrix. The all-ops sweep is test/access-member-sweep.test.ts.
+ * 4. Real seams: OAuth clients issued tokens by the provider's client-credentials grant, verified by the provider,
+ * dispatched through MCP dispatch on a repo-less brain; remember goes through the persistence coordinator;
+ * embeddings stubbed.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import type { BrainEngine } from '../src/core/engine.ts';
+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 { dispatchToolCall, type ToolResult } from '../src/mcp/dispatch.ts';
+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';
+import { __resetHotMemoryCacheForTests, getBrainHotMemoryMeta } from '../src/core/facts/meta-hook.ts';
+import { addMember, bindCredential } from '../src/core/access/members.ts';
+import { linkMember } from '../src/core/access/actor.ts';
+import { runFactsRelink } from '../src/core/facts/relink.ts';
+import { runPhaseConsolidate } from '../src/core/cycle/phases/consolidate.ts';
+import { serializePageToMarkdown } from '../src/core/markdown.ts';
+import type { AuthInfo } from '../src/core/ops/contract.ts';
+import { testBackends } from './helpers/test-backends.ts';
+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const DIMS = 1536;
+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 SRC = 'qm-members';
+const ALICE_ID = 'U0ALICE';
+const BOB_ID = '[email protected]';
+const ACME = 'companies/acme-example';
+const CANARY = { alice: 'canaryfactalice', bob: 'canaryfactbob' } as const;
+const ATTEST_OPS = ['context_pack', 'delta', 'get_page', 'get_write_request', 'list_write_requests', 'recall', 'remember', 'search', 'whoami'];
+
+for (const kind of testBackends()) {
+ describe('member facts and member-scoped reads (' + kind + ', repo-less)', () => {
+ let engine: BrainEngine;
+ let close: () => Promise<void>;
+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });
+ const tokens: Record<string, string> = {};
+ const verify = async (bearer: string) => await provider().verifyAccessToken(bearer) as unknown as AuthInfo;
+
+ type Who = { token: string; actor?: string };
+ const who = {
+ alice: (): Who => ({ token: 'qm', actor: ALICE_ID }),
+ bob: (): Who => ({ token: 'qm', actor: BOB_ID }),
+ bobToken: (): Who => ({ token: 'bob' }),
+ bare: (): Who => ({ token: 'qm' }),
+ unbound: (): Who => ({ token: 'plain' }),
+ };
+ async function call(as: Who, op: string, params: Record<string, unknown>, extra: { sessionId?: string } = {}) {
+ const out: ToolResult = await dispatchToolCall(engine, op, { ...params }, {
+ remote: true, transport: 'http', auth: await verify(tokens[as.token]!), sourceId: SRC, takesHoldersAllowList: ['world'],
+ ...(as.actor !== undefined ? { actor: { header: as.actor } } : {}),
+ ...(extra.sessionId ? { sessionId: extra.sessionId } : {}),
+ metaHook: (name, ctx) => getBrainHotMemoryMeta(name, ctx),
+ });
+ const text = out.content.map(c => c.text).join('\n');
+ let json: Record<string, any> = {};
+ try { json = JSON.parse(text); } catch { /* prose */ }
+ return { isError: out.isError === true, text, json, meta: out._meta, all: text + JSON.stringify(out._meta ?? {}) + JSON.stringify(out.structuredContent ?? {}) };
+ }
+ const remember = (as: Who, fact: string, extra: Record<string, unknown> = {}) => call(as, 'remember', { fact, provenance: 'qm test', ...extra });
+
+ beforeAll(async () => {
+ configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: DIMS, env: { OPENAI_API_KEY: 'test' } });
+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => ({ embeddings: values.map(vectorFor) })) as never);
+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));
+ else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }
+ __resetPrivateVisibilityCacheForTests();
+ __resetHotMemoryCacheForTests();
+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);
+ for (const id of ['alice-example', 'bob-example']) await addMember(engine, id);
+ await importFromContent(engine, ACME, '---\ntype: company\ntitle: Acme Example\n---\n# Acme Example\n\nAcme example builds harbor ledgers.', { sourceId: SRC });
+ const register = async (name: string) => {
+ const c = await provider().registerClientManual('c2-' + name + '-' + randomUUID().slice(0, 8), ['client_credentials'], 'read write', [], SRC);
+ return { id: c.clientId, secret: c.clientSecret ?? '' };
+ };
+ const qm = await register('qm');
+ await bindCredential(engine, { kind: 'client', id: qm.id }, { attestActor: 'qm', attestOps: ATTEST_OPS });
+ const bob = await register('bob');
+ await bindCredential(engine, { kind: 'client', id: bob.id }, { member: 'bob-example' });
+ const plain = await register('plain');
+ for (const [name, c] of [['qm', qm], ['bob', bob], ['plain', plain]] as const) tokens[name] = (await provider().exchangeClientCredentials(c.id, c.secret)).access_token;
+ await linkMember(engine, 'alice-example', 'qm', ALICE_ID);
+ await linkMember(engine, 'bob-example', 'qm', BOB_ID);
+ }, 120_000);
+
+ afterAll(async () => { await close?.(); });
+
+ test('an attested member fact lands private on the member page, created by it, with the entity kept as metadata', async () => {
+ const out = await remember(who.alice(), 'Alice prefers oolong for harbor reviews ' + CANARY.alice, { entity: ACME, visibility: 'world' });
+ expect(out.isError).toBe(false);
+ expect(out.json.status).toBe('inserted');
+ const [row] = await engine.executeRaw<Record<string, unknown>>('SELECT visibility, source_markdown_slug, entity_slug, row_num FROM facts WHERE fact LIKE $1', ['%' + CANARY.alice + '%']);
+ expect(row).toMatchObject({ visibility: 'private', source_markdown_slug: 'memory/members/alice-example', entity_slug: ACME });
+ const [pageRow] = await engine.executeRaw<{ page_readers: string[]; compiled_truth: string }>('SELECT page_readers, compiled_truth FROM pages WHERE source_id = $1 AND slug = $2', [SRC, 'memory/members/alice-example']);
+ expect(pageRow!.page_readers).toEqual(['member:alice-example']);
+ expect(pageRow!.compiled_truth).toContain(CANARY.alice);
+ const acme = await engine.executeRaw<{ compiled_truth: string }>('SELECT compiled_truth FROM pages WHERE source_id = $1 AND slug = $2', [SRC, ACME]);
+ expect(acme[0]!.compiled_truth).not.toContain(CANARY.alice);
+ }, 60_000);
+
+ test('a member-bound credential writes to its own member page too', async () => {
+ const out = await remember(who.bobToken(), 'Bob keeps the harbor keys ' + CANARY.bob, { entity: ACME });
+ expect(out.isError).toBe(false);
+ const [row] = await engine.executeRaw<Record<string, unknown>>('SELECT visibility, source_markdown_slug FROM facts WHERE fact LIKE $1', ['%' + CANARY.bob + '%']);
+ expect(row).toMatchObject({ visibility: 'private', source_markdown_slug: 'memory/members/bob-example' });
+ }, 60_000);
+
+ test('each member recalls only its own facts; the bare client and an unbound credential recall neither', async () => {
+ const mine = await call(who.alice(), 'recall', { query: 'harbor' });
+ expect(mine.all).toContain(CANARY.alice);
+ expect(mine.all).not.toContain(CANARY.bob);
+ const bobs = await call(who.bobToken(), 'recall', { query: 'harbor' });
+ expect(bobs.all).toContain(CANARY.bob);
+ expect(bobs.all).not.toContain(CANARY.alice);
+ for (const as of [who.bob(), who.bare(), who.unbound()]) {
+ for (const params of [{ query: 'harbor' }, { entity: ACME }, { since: '2000-01-01' }]) {
+ const out = await call(as, 'recall', params);
+ expect(out.all).not.toContain(CANARY.alice);
+ }
+ }
+ }, 60_000);
+
+ test('another member never reads the fact through the member page, the entity page, search or context_pack', async () => {
+ for (const as of [who.bob(), who.bobToken(), who.bare(), who.unbound()]) {
+ for (const [op, params] of [['get_page', { slug: 'memory/members/alice-example' }], ['get_page', { slug: ACME }], ['search', { query: CANARY.alice }],
+ ['search', { query: 'oolong harbor' }], ['context_pack', { query: 'harbor oolong', entities: [ACME] }], ['whoami', {}]] as const) {
+ const out = await call(as, op, params as Record<string, unknown>);
+ expect(out.all).not.toContain(CANARY.alice);
+ }
+ }
+ expect((await call(who.alice(), 'get_page', { slug: 'memory/members/alice-example' })).all).toContain(CANARY.alice);
+ }, 120_000);
+
+ test('the operator hatch on: an attested or member-bound caller still never reads another member\'s fact', async () => {
+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: '1' }, async () => {
+ __resetPrivateVisibilityCacheForTests();
+ __resetHotMemoryCacheForTests();
+ for (const as of [who.bob(), who.bobToken(), who.bare(), who.unbound()]) {
+ for (const [op, params] of [['recall', { query: 'harbor' }], ['recall', { entity: ACME }], ['get_page', { slug: 'memory/members/alice-example' }], ['search', { query: CANARY.alice }]] as const) {
+ expect((await call(as, op, params as Record<string, unknown>)).all).not.toContain(CANARY.alice);
+ }
+ }
+ });
+ __resetPrivateVisibilityCacheForTests();
+ }, 60_000);
+
+ test('hot memory: Alice then Bob on one session id never share a payload', async () => {
+ __resetHotMemoryCacheForTests();
+ const session = 'shared-session-' + randomUUID().slice(0, 6);
+ const a = await call(who.alice(), 'get_page', { slug: ACME }, { sessionId: session });
+ expect(JSON.stringify(a.meta?.brain_hot_memory ?? {})).toContain(CANARY.alice);
+ const b = await call(who.bob(), 'get_page', { slug: ACME }, { sessionId: session });
+ expect(JSON.stringify(b.meta ?? {})).not.toContain(CANARY.alice);
+ const bare = await call(who.bare(), 'get_page', { slug: ACME }, { sessionId: session });
+ expect(JSON.stringify(bare.meta ?? {})).not.toContain(CANARY.alice);
+ }, 60_000);
+
+ test('delta: Alice then Bob on one session id keep separate cursors', async () => {
+ const session = 'delta-session-' + randomUUID().slice(0, 6);
+ const first = await call(who.alice(), 'delta', { session_id: session });
+ expect(first.isError).toBe(false);
+ expect(first.all).toContain('started this session');
+ const bob = await call(who.bob(), 'delta', { session_id: session });
+ expect(bob.isError).toBe(false);
+ expect(bob.all).toContain('started this session');
+ const bobAgain = await call(who.bob(), 'delta', { session_id: session });
+ expect(bobAgain.all).not.toContain(CANARY.alice);
+ }, 60_000);
+
+ test('receipts: Alice\'s write receipt is invisible to Bob and the bare client, and her request_id never replays for Bob', async () => {
+ const requestId = 'c2-receipt-' + randomUUID().slice(0, 6);
+ const written = await remember(who.alice(), 'Alice signs the harbor charter ' + CANARY.alice + 'r', { request_id: requestId });
+ expect(written.isError).toBe(false);
+ const factId = String(written.json.id);
+ expect((await call(who.alice(), 'get_write_request', { request_id: requestId })).isError).toBe(false);
+ for (const as of [who.bob(), who.bare()]) {
+ const got = await call(as, 'get_write_request', { request_id: requestId });
+ expect(got.isError).toBe(true);
+ expect(got.json.code ?? got.json.error).toMatch(/not_found/);
+ const listed = await call(as, 'list_write_requests', {});
+ expect(listed.all).not.toContain(requestId);
+ }
+ expect((await call(who.alice(), 'list_write_requests', {})).all).toContain(requestId);
+ const replay = await remember(who.bob(), 'Alice signs the harbor charter ' + CANARY.alice + 'r', { request_id: requestId });
+ expect(replay.isError).toBe(true);
+ expect(replay.all).not.toContain('"id":"' + factId + '"');
+ }, 60_000);
+
+ test('two members saving the same words get two facts, each on its own page', async () => {
+ const words = 'I like jasmine tea at the harbor canarydupword';
+ const a = await remember(who.alice(), words);
+ const b = await remember(who.bobToken(), words);
+ expect(a.json.status).toBe('inserted');
+ expect(b.json.status).toBe('inserted');
+ expect(b.json.id).not.toBe(a.json.id);
+ const rows = await engine.executeRaw<{ source_markdown_slug: string }>('SELECT source_markdown_slug FROM facts WHERE fact = $1 AND expired_at IS NULL ORDER BY id', [words]);
+ expect(rows.map(r => r.source_markdown_slug)).toEqual(['memory/members/alice-example', 'memory/members/bob-example']);
+ expect((await remember(who.alice(), words)).json.id).toBe(a.json.id);
+ }, 60_000);
+
+ test('a fence rebuild keeps the member fact private on its page; relink and consolidation leave it there', async () => {
+ const snap = await engine.readPageSnapshot('memory/members/alice-example', { sourceId: SRC });
+ const markdown = serializePageToMarkdown(snap!.page, snap!.tags);
+ expect((await importFromContent(engine, 'memory/members/alice-example', markdown.replace('# Memory of alice-example', '# Memory of alice-example\n\nEdited by hand.'), { sourceId: SRC })).status).toBe('imported');
+ const [row] = await engine.executeRaw<Record<string, unknown>>('SELECT visibility, source_markdown_slug FROM facts WHERE fact LIKE $1 AND expired_at IS NULL', ['%oolong%' + CANARY.alice]);
+ expect(row).toMatchObject({ visibility: 'private', source_markdown_slug: 'memory/members/alice-example' });
+ __resetHotMemoryCacheForTests();
+ expect((await call(who.bob(), 'recall', { query: 'harbor' })).all).not.toContain(CANARY.alice);
+ expect((await call(who.alice(), 'recall', { query: 'harbor' })).all).toContain(CANARY.alice);
+ await withEnv({ ANTHROPIC_API_KEY: undefined, OPENAI_API_KEY: undefined }, () => runFactsRelink(engine, { sourceId: SRC, llm: false, config: {} as never }));
+ const after = await engine.executeRaw<Record<string, unknown>>('SELECT source_markdown_slug FROM facts WHERE fact LIKE $1', ['%' + CANARY.alice + '%']);
+ expect(after.every(r => r.source_markdown_slug === 'memory/members/alice-example')).toBe(true);
+ }, 60_000);
+
+ test('consolidation never turns member facts into takes on the entity page', async () => {
+ for (const n of ['one', 'two', 'three']) {
+ expect((await remember(who.alice(), `Acme example harbor ledger launch slips to spring ${CANARY.alice}take ${n}`, { entity: ACME })).isError).toBe(false);
+ }
+ const result = await runPhaseConsolidate(engine, { sourceId: SRC, minOldestAgeMs: 0, minFactsPerBucket: 2 });
+ expect(result.status).not.toBe('fail');
+ const takes = await engine.executeRaw<{ claim: string }>('SELECT t.claim FROM takes t JOIN pages p ON p.id = t.page_id WHERE p.source_id = $1', [SRC]);
+ expect(takes.map(t => t.claim).join('\n')).not.toContain(CANARY.alice);
+ }, 60_000);
+ });
+}
diff --git a/test/access-member-sweep.test.ts b/test/access-member-sweep.test.ts
new file mode 100644
index 0000000000..089027011b
--- /dev/null
+++ b/test/access-member-sweep.test.ts
@@ -0,0 +1,166 @@
+/**
+ * GST-359 C2: the all-ops member sweep (dispatch, both engines).
+ *
+ * 1. Protects: no registered operation, read, write or agent, and no _meta channel riding a response (hot memory,
+ * access), returns one member's fact or readers page to another member, to the bare attest client or to an unbound
+ * credential, with and without the operator hatch. A new operation that reads facts or pages without the member
+ * predicates fails here, so the next tool cannot regress it.
+ * 2. Fails when: any operation's text, structuredContent or _meta carries Alice's canary for Bob (as an attested actor
+ * or through his member-bound credential), the bare attest client or an unbound credential; or a sweep write by
+ * Bob changes Alice's member page.
+ * 3. Existing coverage: test/access-member-facts.test.ts pins each path by name; this sweeps the whole registry.
+ * 4. Real seams: dispatch with provider-issued client-credentials tokens on a repo-less brain; writes through the
+ * persistence coordinator; embeddings stubbed, no chat provider key.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import type { BrainEngine } from '../src/core/engine.ts';
+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 { dispatchToolCall } from '../src/mcp/dispatch.ts';
+import { operations } from '../src/core/operations.ts';
+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';
+import { __resetHotMemoryCacheForTests, getBrainHotMemoryMeta } from '../src/core/facts/meta-hook.ts';
+import { addMember, bindCredential } from '../src/core/access/members.ts';
+import { attestableOp, linkMember } from '../src/core/access/actor.ts';
+import type { AuthInfo } from '../src/core/ops/contract.ts';
+import { testBackends } from './helpers/test-backends.ts';
+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
+import { withEnv } from './helpers/with-env.ts';
+import { sweepArgs, type SweepFixture } from './helpers/member-sweep.ts';
+
+const DIMS = 1536;
+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 SRC = 'qm-sweep';
+const ALICE_ID = 'U0ALICE';
+const BOB_ID = 'oidc:3f2a:bob-example';
+const ACME = 'companies/acme-example';
+const CANARY = 'canarysweepalice';
+const PAGE_CANARY = 'canarysweeppagealice';
+const NO_KEYS = { ANTHROPIC_API_KEY: undefined, OPENAI_API_KEY: undefined, GEMINI_API_KEY: undefined, VOYAGE_API_KEY: undefined, GOOGLE_API_KEY: undefined };
+const OP_TIMEOUT_MS = 20_000;
+
+for (const kind of testBackends()) {
+ describe('all-ops member sweep (' + kind + ', repo-less)', () => {
+ let engine: BrainEngine;
+ let close: () => Promise<void>;
+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });
+ const tokens: Record<string, string> = {};
+ let fixture: SweepFixture;
+
+ type Who = { label: string; token: string; actor?: string };
+ const CALLERS: Who[] = [
+ { label: 'bob as attested actor', token: 'qm', actor: BOB_ID },
+ { label: 'bob member-bound credential', token: 'bob' },
+ { label: 'bare attest client', token: 'qm' },
+ { label: 'unbound credential', token: 'plain' },
+ ];
+ async function call(as: Who, op: string, params: Record<string, unknown>): Promise<{ ok: boolean; all: string }> {
+ const auth = await provider().verifyAccessToken(tokens[as.token]!) as unknown as AuthInfo;
+ const run = dispatchToolCall(engine, op, { ...params }, {
+ remote: true, transport: 'http', auth, sourceId: SRC, takesHoldersAllowList: ['world'], sessionId: fixture.sessionId,
+ ...(as.actor !== undefined ? { actor: { header: as.actor } } : {}),
+ metaHook: (name, ctx) => getBrainHotMemoryMeta(name, ctx),
+ }).then(out => ({ ok: out.isError !== true, all: out.content.map(c => c.text).join('\n') + JSON.stringify(out._meta ?? {}) + JSON.stringify(out.structuredContent ?? {}) }),
+ (e: unknown) => ({ ok: false, all: e instanceof Error ? e.message : String(e) }));
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const timeout = new Promise<{ ok: boolean; all: string }>(resolve => { timer = setTimeout(() => resolve({ ok: false, all: 'timeout' }), OP_TIMEOUT_MS); });
+ try { return await Promise.race([run, timeout]); } finally { clearTimeout(timer); }
+ }
+
+ async function sweep(callers: Who[]): Promise<{ leaks: string[]; ran: number; revealing: Set<string> }> {
+ const leaks: string[] = [];
+ const revealing = new Set<string>();
+ let ran = 0;
+ for (const op of operations) {
+ for (const args of sweepArgs(op, fixture)) {
+ for (const as of callers) {
+ const out = await call(as, op.name, args);
+ if (out.ok) ran += 1;
+ if (out.all.includes(CANARY) || out.all.includes(PAGE_CANARY)) { revealing.add(op.name); leaks.push(op.name + ' as ' + as.label + ' ' + JSON.stringify(args)); }
+ }
+ }
+ }
+ return { leaks, ran, revealing };
+ }
+
+ beforeAll(async () => {
+ configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: DIMS, env: { OPENAI_API_KEY: 'test' } });
+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => ({ embeddings: values.map(vectorFor) })) as never);
+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));
+ else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }
+ __resetPrivateVisibilityCacheForTests();
+ __resetHotMemoryCacheForTests();
+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);
+ for (const id of ['alice-example', 'bob-example']) await addMember(engine, id);
+ await importFromContent(engine, ACME, '---\ntype: company\ntitle: Acme Example\n---\n# Acme Example\n\nAcme example builds harbor ledgers.', { sourceId: SRC });
+ await importFromContent(engine, 'notes/alice-only', '---\ntype: note\ntitle: Alice harbor notes\nreadable_by: [alice-example]\n---\n# Alice harbor notes\n\nOolong harbor ledger draft ' + PAGE_CANARY + ' about [Acme Example](../companies/acme-example.md).', { sourceId: SRC });
+ const register = async (name: string) => {
+ const c = await provider().registerClientManual('c2s-' + name + '-' + randomUUID().slice(0, 8), ['client_credentials'], 'read write', [], SRC);
+ return { id: c.clientId, secret: c.clientSecret ?? '' };
+ };
+ const qm = await register('qm');
+ await bindCredential(engine, { kind: 'client', id: qm.id }, { attestActor: 'qm', attestOps: operations.filter(op => attestableOp(op.name, op)).map(op => op.name) });
+ const bob = await register('bob');
+ await bindCredential(engine, { kind: 'client', id: bob.id }, { member: 'bob-example' });
+ const plain = await register('plain');
+ for (const [name, c] of [['qm', qm], ['bob', bob], ['plain', plain]] as const) tokens[name] = (await provider().exchangeClientCredentials(c.id, c.secret)).access_token;
+ await linkMember(engine, 'alice-example', 'qm', ALICE_ID);
+ await linkMember(engine, 'bob-example', 'qm', BOB_ID);
+ fixture = { sourceId: SRC, memberSlug: 'memory/members/alice-example', entitySlug: ACME, entityName: 'Acme Example', words: 'oolong harbor ledger', factId: '0', requestId: 'c2-sweep-' + randomUUID().slice(0, 6), sessionId: 'sweep-session' };
+ const saved = await call({ label: 'alice', token: 'qm', actor: ALICE_ID }, 'remember',
+ { fact: 'Alice drafts the oolong harbor ledger ' + CANARY, provenance: 'sweep', entity: ACME, request_id: fixture.requestId });
+ expect(saved.ok).toBe(true);
+ const [fact] = await engine.executeRaw<{ id: number }>('SELECT id FROM facts WHERE fact LIKE $1', ['%' + CANARY]);
+ fixture.factId = String(fact!.id);
+ }, 120_000);
+
+ afterAll(async () => { await close?.(); });
+
+ test('the canary is reachable by its owner (the sweep is not vacuous)', async () => {
+ const alice: Who = { label: 'alice', token: 'qm', actor: ALICE_ID };
+ expect((await call(alice, 'recall', { query: fixture.words })).all).toContain(CANARY);
+ expect((await call(alice, 'get_page', { slug: fixture.memberSlug })).all).toContain(CANARY);
+ expect((await call(alice, 'get_page', { slug: 'notes/alice-only' })).all).toContain(PAGE_CANARY);
+ const { revealing } = await withEnv(NO_KEYS, () => sweep([alice]));
+ for (const op of ['recall', 'get_page', 'get_chunks', 'search', 'query']) expect(revealing).toContain(op);
+ }, 1_800_000);
+
+ test('no operation returns Alice\'s fact or readers page to Bob, the bare attest client or an unbound credential', async () => {
+ const { leaks, ran } = await withEnv(NO_KEYS, () => sweep(CALLERS));
+ expect(leaks).toEqual([]);
+ expect(ran).toBeGreaterThan(150);
+ }, 1_800_000);
+
+ test('with the operator hatch on, still no leak', async () => {
+ const { leaks } = await withEnv({ ...NO_KEYS, GBRAIN_REMOTE_PRIVATE_PAGES: '1' }, async () => {
+ __resetPrivateVisibilityCacheForTests();
+ __resetHotMemoryCacheForTests();
+ return sweep(CALLERS.slice(0, 2));
+ });
+ __resetPrivateVisibilityCacheForTests();
+ expect(leaks).toEqual([]);
+ }, 1_800_000);
+
+ test('Bob\'s sweep writes left Alice\'s member page and fact intact', async () => {
+ const [row] = await engine.executeRaw<{ visibility: string; source_markdown_slug: string; expired_at: unknown }>('SELECT visibility, source_markdown_slug, expired_at FROM facts WHERE fact LIKE $1', ['%' + CANARY]);
+ expect(row).toMatchObject({ visibility: 'private', source_markdown_slug: fixture.memberSlug, expired_at: null });
+ const [page] = await engine.executeRaw<{ page_readers: string[]; compiled_truth: string; deleted_at: unknown }>('SELECT page_readers, compiled_truth, deleted_at FROM pages WHERE source_id = $1 AND slug = $2', [SRC, fixture.memberSlug]);
+ expect(page).toMatchObject({ page_readers: ['member:alice-example'], deleted_at: null });
+ expect(page!.compiled_truth).toContain(CANARY);
+ }, 60_000);
+ });
+}
diff --git a/test/helpers/member-sweep.ts b/test/helpers/member-sweep.ts
new file mode 100644
index 0000000000..0a96100b3b
--- /dev/null
+++ b/test/helpers/member-sweep.ts
@@ -0,0 +1,58 @@
+/**
+ * GST-359 C2: the all-ops member sweep's argument builder, shared by test/access-member-sweep.test.ts (dispatch, both
+ * engines) and test/e2e/serve-http-member-sweep.test.ts (a real serve --http on a keyless brain). For every operation
+ * it builds the arguments that would reach a member's fact if any path leaked it: the member page, the entity page,
+ * the fact's id, a query that matches its words (never the canary itself, so an echoed argument is no false alarm).
+ */
+import type { Operation } from '../../src/core/ops/contract.ts';
+
+export interface SweepFixture {
+ sourceId: string;
+ memberSlug: string;
+ entitySlug: string;
+ entityName: string;
+ /** Words that match the member's fact; the canary itself is never sent. */
+ words: string;
+ factId: string;
+ requestId: string;
+ sessionId: string;
+}
+
+const STRING_BY_NAME: Record<string, (f: SweepFixture, slug: string) => unknown> = {
+ slug: (_f, slug) => slug, from: (_f, slug) => slug, to: (f) => f.entitySlug, source_slug: (_f, slug) => slug, page: (_f, slug) => slug,
+ slug_prefix: (f) => f.memberSlug.slice(0, f.memberSlug.lastIndexOf('/') + 1), prefix: (f) => f.memberSlug.slice(0, f.memberSlug.lastIndexOf('/') + 1),
+ partial: (_f, slug) => slug.split('/').pop(), path: (_f, slug) => slug + '.md',
+ query: (f) => f.words, q: (f) => f.words, question: (f) => f.words, phrase: (f) => f.words, prompt: (f) => f.words, text: (f) => f.words,
+ topic: (f) => f.words, turn_text: (f) => f.words, content: (f) => '---\ntype: note\n---\n' + f.words, claim: (f) => f.words, summary: (f) => f.words,
+ entity: (f) => f.entityName, name: (f) => f.entityName, symbol: (f) => f.entityName, entry_point: (f) => f.entityName,
+ id: (f) => f.factId, fact_id: (f) => f.factId, ref: (f) => f.factId, answer_id: (f) => f.factId, upload_id: (f) => f.requestId,
+ request_id: (f) => f.requestId, session_id: (f) => f.sessionId, source_id: (f) => f.sourceId,
+ since: () => '2000-01-01T00:00:00Z', date: () => new Date().toISOString().slice(0, 10), until: () => '2999-01-01T00:00:00Z',
+ expected_revision: () => '00000000-0000-0000-0000-000000000000', dimension: () => 'stage', value: () => 'seed', kind: () => 'fact',
+};
+const ARRAY_BY_NAME: Record<string, (f: SweepFixture, slug: string) => unknown[]> = {
+ slugs: (f, slug) => [slug, f.entitySlug], entities: (f) => [f.entitySlug], names: (f) => [f.entityName], ids: (f) => [f.factId],
+ hits: (_f, slug) => [{ slug }], paths: (_f, slug) => [slug + '.md'], window: (f) => [{ role: 'user', content: f.words }],
+};
+
+function valueFor(name: string, def: Operation['params'][string], f: SweepFixture, slug: string): unknown {
+ if (def.type === 'array') return ARRAY_BY_NAME[name]?.(f, slug) ?? (def.items?.type === 'string' ? [f.words] : []);
+ if (def.type === 'string') {
+ const named = STRING_BY_NAME[name]?.(f, slug);
+ if (named !== undefined) return def.enum && !def.enum.includes(String(named)) ? def.enum[0] : named;
+ return def.enum ? def.enum[0] : f.words;
+ }
+ if (def.type === 'number') return name === 'limit' ? 50 : 1;
+ if (def.type === 'boolean') return name.startsWith('include') || name === 'detail';
+ return {};
+}
+
+const SLUG_PARAMS = new Set(['slug', 'from', 'source_slug', 'page', 'partial', 'path', 'slugs', 'hits', 'paths']);
+const SENT_OPTIONAL = new Set(['query', 'question', 'entity', 'entities', 'since', 'session_id', 'id', 'slug', 'include_private', 'detail']);
+
+/** One argument set per target page for each operation (member page and entity page when it takes a slug). */
+export function sweepArgs(op: Pick<Operation, 'params'>, f: SweepFixture): Record<string, unknown>[] {
+ const params = Object.entries(op.params ?? {});
+ const slugs = params.some(([name]) => SLUG_PARAMS.has(name)) ? [f.memberSlug, f.entitySlug] : [f.memberSlug];
+ return slugs.map(slug => Object.fromEntries(params.filter(([name, def]) => def.required || SENT_OPTIONAL.has(name)).map(([name, def]) => [name, valueFor(name, def, f, slug)])));
+}