Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(access): GST-359 C2 two-member serve --http E2E, member read and supersession mutation-killing tests
garrytan9ba7879a74f0
diff --git a/test/access-member-facts.test.ts b/test/access-member-facts.test.tsindex ef2804e7b7..58d6bb3f3e 100644--- a/test/access-member-facts.test.ts+++ b/test/access-member-facts.test.ts@@ -30,6 +30,8 @@ import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/privatimport { __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 { memberPageSlug } from '../src/core/access/member-facts.ts';+import { decideSingleFact, type SingleFactIntent } from '../src/core/facts/single-prepare.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';@@ -55,7 +57,7 @@ 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'];+const ATTEST_OPS = ['context_pack', 'delta', 'get_page', 'get_write_request', 'list_write_requests', 'put_page', 'recall', 'remember', 'search', 'whoami'];for (const kind of testBackends()) {describe('member facts and member-scoped reads (' + kind + ', repo-less)', () => {@@ -159,6 +161,47 @@ for (const kind of testBackends()) {expect((await call(who.alice(), 'get_page', { slug: 'memory/members/alice-example' })).all).toContain(CANARY.alice);}, 120_000);+ test('a member reads only private facts on its own live member page, never an operator\'s private fact or a page-less member slug', async () => {+ const operator = 'canaryoperatorprivate harbor ledger budget';+ const orphan = 'canaryorphanmember harbor ledger budget';+ await engine.insertFact({ fact: operator, kind: 'fact', entity_slug: ACME, source: 'test', visibility: 'private' }, { source_id: SRC });+ await engine.insertFact({ fact: orphan, kind: 'fact', entity_slug: ACME, source: 'test', visibility: 'private', source_markdown_slug: 'memory/members/carol-example' }, { source_id: SRC });+ for (const hatch of [undefined, '1']) {+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: hatch }, async () => {+ __resetPrivateVisibilityCacheForTests();+ for (const as of [who.alice(), who.bob(), who.bobToken(), who.bare(), who.unbound()]) {+ for (const params of [{ query: 'harbor ledger budget' }, { entity: ACME }, { since: '2000-01-01' }]) {+ const out = await call(as, 'recall', params);+ expect(out.all).not.toContain('canaryoperatorprivate');+ expect(out.all).not.toContain('canaryorphanmember');+ }+ }+ });+ }+ __resetPrivateVisibilityCacheForTests();+ }, 60_000);++ test('a member stops reading its own facts once its page\'s readers no longer name it', async () => {+ const bobPage = 'memory/members/bob-example';+ const [before] = await engine.executeRaw<{ frontmatter: unknown }>('SELECT frontmatter FROM pages WHERE source_id = $1 AND slug = $2', [SRC, bobPage]);+ await engine.executeRaw(`UPDATE pages SET frontmatter = jsonb_set(frontmatter, '{readable_by}', '["group:team-a"]'::jsonb) WHERE source_id = $1 AND slug = $2`, [SRC, bobPage]);+ __resetPrivateVisibilityCacheForTests();+ const [narrowed] = await engine.executeRaw<{ page_readers: string[] }>('SELECT page_readers FROM pages WHERE source_id = $1 AND slug = $2', [SRC, bobPage]);+ expect(narrowed!.page_readers).toEqual(['group:team-a']);+ try {+ for (const hatch of [undefined, '1']) {+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: hatch }, async () => {+ __resetPrivateVisibilityCacheForTests();+ for (const params of [{ query: 'harbor' }, { entity: ACME }, { since: '2000-01-01' }]) expect((await call(who.bobToken(), 'recall', params)).all).not.toContain(CANARY.bob);+ });+ }+ } finally {+ await engine.executeRaw('UPDATE pages SET frontmatter = $3::jsonb WHERE source_id = $1 AND slug = $2', [SRC, bobPage, JSON.stringify(before!.frontmatter)]);+ __resetPrivateVisibilityCacheForTests();+ }+ expect((await call(who.bobToken(), 'recall', { query: 'harbor' })).all).toContain(CANARY.bob);+ }, 60_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();@@ -212,6 +255,19 @@ for (const kind of testBackends()) {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 + '"');+ // A write on a page both can read: the receipt still belongs to the person it was written for.+ const shared = 'c2-shared-' + randomUUID().slice(0, 6);+ const content = '---\ntype: note\ntitle: Shared harbor plan\n---\n# Shared harbor plan\n\nShared plan ' + shared + '.';+ expect((await call(who.alice(), 'put_page', { slug: 'notes/' + shared, content, request_id: shared })).isError).toBe(false);+ expect((await call(who.alice(), 'get_write_request', { request_id: shared })).isError).toBe(false);+ for (const as of [who.bob(), who.bare()]) {+ const got = await call(as, 'get_write_request', { request_id: shared });+ expect({ isError: got.isError, code: got.json.code ?? got.json.error }).toEqual({ isError: true, code: 'not_found' });+ expect((await call(as, 'list_write_requests', {})).all).not.toContain(shared);+ }+ const bobReplay = await call(who.bob(), 'put_page', { slug: 'notes/' + shared, content, request_id: shared });+ expect(bobReplay.isError).toBe(true);+ expect(bobReplay.json.code ?? bobReplay.json.error).toBe('idempotency_conflict');}, 60_000);test('two members saving the same words get two facts, each on its own page', async () => {@@ -226,6 +282,18 @@ for (const kind of testBackends()) {expect((await remember(who.alice(), words)).json.id).toBe(a.json.id);}, 60_000);+ test('a member fact never supersedes or dedups against another page\'s fact about the same entity', async () => {+ const model = 'synthetic:c2-member';+ await engine.setConfig('facts.supersession_thresholds', JSON.stringify({ [`${model}@${DIMS}`]: 0.95 }));+ const embedding = new Float32Array(DIMS);+ embedding[0] = 1;+ const fact = 'Acme example renews its harbor ledger contract for two more years';+ await engine.insertFact({ fact, kind: 'fact', entity_slug: ACME, source: 'test', visibility: 'private', embedding, embedding_model: model, source_markdown_slug: ACME }, { source_id: SRC });+ const intent = { fact: fact + ' again', kind: 'fact', entity_slug: ACME, visibility: 'private', page_slug: memberPageSlug('alice-example') } as SingleFactIntent;+ expect((await decideSingleFact(engine, SRC, intent, embedding, model)).status).toBe('inserted');+ expect((await decideSingleFact(engine, SRC, { ...intent, page_slug: undefined }, embedding, model)).status).not.toBe('inserted');+ }, 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);@@ -241,13 +309,23 @@ for (const kind of testBackends()) {}, 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 BETA = 'companies/beta-example';+ await importFromContent(engine, BETA, '---\ntype: company\ntitle: Beta Example\n---\n# Beta Example\n\nBeta example ships ledgers.', { sourceId: SRC });+ expect((await remember(who.alice(), 'Beta example ledger launch slips to spring quarter ' + CANARY.alice + 'take', { entity: BETA })).isError).toBe(false);+ const local = await dispatchToolCall(engine, 'remember', { fact: 'Beta example ledger launch slips to spring quarter controltake', provenance: 'operator', entity: BETA }, { remote: false, sourceId: SRC });+ expect(local.isError).not.toBe(true);+ // Two more copies of each (the write path would dedup them), so each page holds a cluster of three.+ for (let n = 0; n < 2; n++) {+ await engine.executeRaw(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, notability, source, source_markdown_slug, confidence, embedding, embedding_model, embedded_text_hash, valid_from)+ SELECT source_id, entity_slug, fact, kind, visibility, notability, source, source_markdown_slug, confidence, embedding, embedding_model, embedded_text_hash, valid_from - interval '1 day'+ FROM facts WHERE entity_slug = $1 AND row_num IS NOT NULL`, [BETA]);}+ await engine.executeRaw("UPDATE facts SET valid_from = valid_from - interval '3 days' WHERE entity_slug = $1", [BETA]);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);+ 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])).map(t => t.claim).join('\n');+ expect(takes).toContain('controltake');+ expect(takes).not.toContain(CANARY.alice);}, 60_000);});}diff --git a/test/e2e/serve-http-member-sweep.test.ts b/test/e2e/serve-http-member-sweep.test.tsnew file mode 100644index 0000000000..08302705b2--- /dev/null+++ b/test/e2e/serve-http-member-sweep.test.ts@@ -0,0 +1,142 @@+/**+ * GST-240 / GST-359 C2: the keyless two-member check over a real `gbrain serve --http`.+ *+ * A keyless brain (PGLite, no embedding provider, no repo; and Postgres when DATABASE_URL is set) with two members,+ * alice-example and bob-example, each holding a member key (`gbrain auth create --member`), plus a QM+ * client-credentials client holding the attest right for every attestable operation (`gbrain auth rescope+ * --attest-actor qm --attest-ops ...`), both members linked through the real CLI (`gbrain access member link`).+ *+ * 1. Protects: 0 cross-member rows across every operation the attest grant may call, for Bob as an attested actor,+ * Bob's member key, the bare attest client and an unbound key; a fact Alice saves through her member key is+ * recalled by her attested actor (another harness) and by no one else.+ * 2. Fails when: any tools/call response (text, structuredContent, _meta) carries Alice's canary for anyone but+ * Alice, or her member-key fact is not on her member page.+ * 3. Existing coverage: test/access-member-sweep.test.ts sweeps dispatch on both engines; this is the transport.+ * 4. No production seam.+ *+ * Run: bun test test/e2e/serve-http-member-sweep.test.ts (PGLite arm always; Postgres arm with+ * DATABASE_URL=... GBRAIN_TEST_ALLOW_DATABASE_URL=1)+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { execFileSync, spawn, type ChildProcess } from 'node:child_process';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { operations } from '../../src/core/operations.ts';+import { attestableOp } from '../../src/core/access/actor.ts';+import { hasDatabase, setupDB, teardownDB } from './helpers.ts';+import { sweepArgs, type SweepFixture } from '../helpers/member-sweep.ts';++const ALICE_ID = 'U0ALICE';+const BOB_ID = 'oidc:3f2a:bob-example';+const CANARY = { actor: 'canaryhttpaliceactor', key: 'canaryhttpalicekey', bob: 'canaryhttpbob' } as const;+const ATTESTABLE = operations.filter(op => attestableOp(op.name, op));+const KEYLESS_ENV = { OPENAI_API_KEY: '', ANTHROPIC_API_KEY: '', VOYAGE_API_KEY: '', GEMINI_API_KEY: '', GOOGLE_API_KEY: '', GBRAIN_SKIP_STARTUP_HOOKS: '1' };++const backends: Array<'pglite' | 'postgres'> = ['pglite', ...(hasDatabase() ? ['postgres' as const] : [])];++for (const [index, backend] of backends.entries()) {+ describe('/mcp keyless two-member sweep over serve --http (' + backend + ')', () => {+ const port = 19171 + index;+ const base = 'http://localhost:' + port;+ const home = mkdtempSync(join(tmpdir(), 'gbrain-c2-e2e-'));+ const env: NodeJS.ProcessEnv = { ...process.env, ...KEYLESS_ENV, GBRAIN_HOME: home, HOME: home, GBRAIN_AUDIT_DIR: join(home, 'audit') };+ if (backend === 'pglite') delete env.DATABASE_URL;+ let server: ChildProcess | null = null;+ const bearer: Record<'qm' | 'alice' | 'bob' | 'plain', string> = { qm: '', alice: '', bob: '', plain: '' };+ const cli = (...args: string[]) => execFileSync('bun', ['run', 'src/cli.ts', ...args], { cwd: process.cwd(), env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });+ const token = (out: string) => out.match(/gbrain_[a-f0-9]{32,}/)![0];+ const fixture: SweepFixture = { sourceId: 'default', memberSlug: 'memory/members/alice-example', entitySlug: 'companies/acme-example', entityName: 'Acme Example',+ words: 'oolong harbor ledger', factId: '1', requestId: 'c2-e2e-request', sessionId: 'c2-e2e-session' };++ beforeAll(async () => {+ if (backend === 'postgres') await setupDB();+ else cli('init', '--pglite', '--no-embedding');+ for (const id of ['alice-example', 'bob-example']) cli('access', 'member', 'add', id);+ bearer.alice = token(cli('auth', 'create', 'c2-alice-key', '--member', 'alice-example', '--scopes', 'read,write'));+ bearer.bob = token(cli('auth', 'create', 'c2-bob-key', '--member', 'bob-example', '--scopes', 'read,write'));+ bearer.plain = token(cli('auth', 'create', 'c2-plain-key', '--scopes', 'read,write'));+ const registered = cli('auth', 'register-client', 'c2-qm', '--grant-types', 'client_credentials', '--scopes', 'read write');+ const clientId = registered.match(/Client ID:\s+(\S+)/)![1]!;+ const clientSecret = registered.match(/Client Secret:\s+(\S+)/)![1]!;+ expect(cli('auth', 'rescope', '--client', clientId, '--attest-actor', 'qm', '--attest-ops', ATTESTABLE.map(op => op.name).join(','))).toContain('attests actor: qm');+ expect(cli('access', 'member', 'link', 'alice-example', '--qm', ALICE_ID)).toContain('Linked qm:' + ALICE_ID);+ expect(cli('access', 'member', 'link', 'bob-example', '--qm', BOB_ID)).toContain('-> bob-example');+ server = spawn('bun', ['run', 'src/cli.ts', 'serve', '--http', '--port', String(port), '--public-url', base], { cwd: process.cwd(), env, stdio: ['ignore', 'pipe', 'pipe'] });+ let stderr = '';+ server.stderr?.on('data', (d: Buffer) => { stderr += d.toString(); });+ let up = false;+ for (let i = 0; i < 60 && !up; i++) {+ try { up = (await fetch(base + '/health')).ok; } catch { /* not up yet */ }+ if (!up) await new Promise(r => setTimeout(r, 500));+ }+ if (!up) throw new Error('Server failed to start within 30s.\nstderr: ' + stderr.slice(-800));+ const res = await fetch(base + '/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' },+ body: new URLSearchParams({ grant_type: 'client_credentials', client_id: clientId, client_secret: clientSecret }) });+ expect(res.status).toBe(200);+ bearer.qm = (await res.json() as { access_token: string }).access_token;+ await call({ token: 'qm' }, 'put_page', { slug: fixture.entitySlug, content: '---\ntype: company\ntitle: Acme Example\n---\n# Acme Example\n\nAcme example builds harbor ledgers.' });+ }, 180_000);++ afterAll(async () => {+ if (server) {+ server.kill('SIGTERM');+ await new Promise(r => setTimeout(r, 1000));+ if (!server.killed) server.kill('SIGKILL');+ }+ if (backend === 'postgres') await teardownDB();+ rmSync(home, { recursive: true, force: true });+ }, 30_000);++ type Who = { token: keyof typeof bearer; actor?: string };+ async function call(as: Who, name: string, args: Record<string, unknown>): Promise<{ ok: boolean; all: string; result: Record<string, any> }> {+ const res = await fetch(base + '/mcp', {+ method: 'POST',+ headers: { Authorization: 'Bearer ' + bearer[as.token], 'Content-Type': 'application/json', Accept: 'application/json, text/event-stream',+ ...(as.actor ? { 'x-qm-actor': as.actor } : {}) },+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args, _meta: { session_id: fixture.sessionId } } }),+ });+ const text = (await res.text()).trim();+ let parsed: { result?: Record<string, any>; error?: unknown } = {};+ try { parsed = JSON.parse(text.startsWith('{') ? text : text.split('\n').filter(l => l.startsWith('data:')).pop()!.slice(5).trim()); } catch { /* raw */ }+ return { ok: res.status === 200 && !!parsed.result && parsed.result.isError !== true, all: text, result: parsed.result ?? {} };+ }++ test('facts saved by each member land on that member\'s page, across harnesses', async () => {+ expect((await call({ token: 'qm', actor: ALICE_ID }, 'remember', { fact: 'Alice drafts the oolong harbor ledger ' + CANARY.actor, provenance: 'qm', entity: fixture.entitySlug })).ok).toBe(true);+ expect((await call({ token: 'alice' }, 'remember', { fact: 'Alice signs the oolong harbor ledger ' + CANARY.key, provenance: 'cli harness' })).ok).toBe(true);+ expect((await call({ token: 'bob' }, 'remember', { fact: 'Bob audits the oolong harbor ledger ' + CANARY.bob, provenance: 'cli harness' })).ok).toBe(true);+ const recalled = await call({ token: 'qm', actor: ALICE_ID }, 'recall', { query: fixture.words });+ expect(recalled.all).toContain(CANARY.actor);+ expect(recalled.all).toContain(CANARY.key);+ expect(recalled.all).not.toContain(CANARY.bob);+ const page = await call({ token: 'alice' }, 'get_page', { slug: fixture.memberSlug });+ expect(page.all).toContain(CANARY.actor);+ expect(page.all).toContain(CANARY.key);+ const bobs = await call({ token: 'qm', actor: BOB_ID }, 'recall', { query: fixture.words });+ expect(bobs.all).toContain(CANARY.bob);+ expect(bobs.all).not.toContain(CANARY.actor);+ }, 120_000);++ test('0 cross-member rows across every operation the attest grant may call', async () => {+ const callers: Array<Who & { label: string }> = [+ { label: 'bob actor', token: 'qm', actor: BOB_ID }, { label: 'bob key', token: 'bob' }, { label: 'bare qm client', token: 'qm' }, { label: 'unbound key', token: 'plain' },+ ];+ const leaks: string[] = [];+ let answered = 0;+ for (const op of ATTESTABLE) {+ for (const args of sweepArgs(op, fixture)) {+ for (const as of callers) {+ const out = await call(as, op.name, args);+ if (out.ok) answered += 1;+ if (out.all.includes(CANARY.actor) || out.all.includes(CANARY.key)) leaks.push(op.name + ' as ' + as.label + ' ' + JSON.stringify(args));+ }+ }+ }+ expect(leaks).toEqual([]);+ expect(answered).toBeGreaterThan(100);+ const alice = await call({ token: 'qm', actor: ALICE_ID }, 'recall', { query: fixture.words });+ expect(alice.all).toContain(CANARY.actor);+ }, 1_200_000);+ });+}