Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Test that a concept stays hidden when its input atom's private origin moved under a dropped alias
garrytan37bf2d728e8f
diff --git a/test/concept-rename-history-gst2687.test.ts b/test/concept-rename-history-gst2687.test.tsnew file mode 100644index 0000000000..b4600904f0--- /dev/null+++ b/test/concept-rename-history-gst2687.test.ts@@ -0,0 +1,158 @@+/**+ * GST-2687: a synthesized concept whose input atom is stamped `world` and whose atom's origin is private, after the+ * origin was renamed and a later rename onto the old slug dropped the origin's rename alias.+ *+ * 1. Protects: the concept stays hidden from a non-owner remote caller on get_page, search, query and list_pages, as the+ * atom does since #9325 (#8920, atomMovedOriginPrivateSql on the atom branch of derivedOriginPrivateSql); the owner+ * and a granted_sources credential still read both; doctor's derived_visibility counts the concept in looser_concepts+ * and the atom in looser_atoms.+ * 2. Fails when: the concept-input branch of privatePagesFilterFragment (or looser_concepts, or looser_atoms) follows+ * only the slug_aliases rename alias and not slug_rename_history, so the dropped alias makes the concept readable.+ * 3. Existing coverage: rename-atom-lineage-7462 (#8920 case: the atom alone, raw SQL fragment).+ * 4. Real seams: tokens minted by the token service and verified by the OAuth provider, dispatched through MCP dispatch+ * (remote: true); the sync rename primitive; a repo-less source; PGLite, and Postgres with DATABASE_URL; embeddings stubbed.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+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, resetGateway, __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 { parseRescopeTokenArgs, rescopeLegacyToken } from '../src/core/grants/legacy-token.ts';+import { noGrantFederatedScope } from '../src/core/source-resolver.ts';+import { dispatchToolCall } from '../src/mcp/dispatch.ts';+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';+import { setOwner } from '../src/core/access/owner.ts';+import { bindCredential } from '../src/core/access/members.ts';+import { renamePageOntoSlug } from '../src/commands/sync/rename-collision.ts';+import { derivedVisibilityCheck } from '../src/commands/doctor/checks/derived-visibility.ts';+import type { AuthInfo } from '../src/core/ops/contract.ts';+import { readsUnscopedOnPurpose } from './helpers/reads-unscoped.ts';+import { testBackends } from './helpers/test-backends.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.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 = 'source-a';+const OWNER = 'owner-example';+const MEMBER = 'alice-example';+const ORIGIN = 'notes/secret';+const MOVED = 'archive/secret';+const OTHER = 'notes/other';+const ATOM = 'atoms/secret-claim';+const CONCEPT = 'concepts/secret-concept';+const canary = (name: string) => `canarygst${name}x`;+const doc = (title: string, name: string, fm: string[], type: string) =>+ ['---', `type: ${type}`, `title: ${title}`, ...fm, '---', `# ${title}`, '', `Harbor ledger planning notes ${canary(name)} here.`].join('\n');++for (const kind of testBackends()) {+ describe(`GST-2687: concept of a world atom whose private origin's rename alias was dropped (${kind})`, () => {+ let engine: BrainEngine;+ let closeEngine: () => Promise<void>;+ const tokens: Record<string, string> = {};++ 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: closeEngine } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));+ else { const e = new PGLiteEngine(); await e.connect({ engine: 'pglite' } as never); await e.initSchema(); engine = e; closeEngine = () => e.disconnect(); }+ __resetPrivateVisibilityCacheForTests();+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);+ for (const id of [OWNER, MEMBER]) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);++ await importFromContent(engine, ORIGIN, doc('Secret origin', 'origin', ['visibility: private'], 'note'), { sourceId: SRC });+ await importFromContent(engine, ATOM, doc('Secret claim', 'atom', ['visibility: world', `source_slug: ${ORIGIN}`], 'atom'), { sourceId: SRC });+ await importFromContent(engine, OTHER, doc('Other note', 'other', ['visibility: world'], 'note'), { sourceId: SRC });+ expect(await renamePageOntoSlug(engine, { sourceId: SRC, oldSlug: ORIGIN, newSlug: MOVED, to: `${MOVED}.md` })).toBe('renamed');+ expect(await renamePageOntoSlug(engine, { sourceId: SRC, oldSlug: OTHER, newSlug: ORIGIN, to: `${ORIGIN}.md` })).toBe('renamed');+ await importFromContent(engine, CONCEPT, doc('Secret concept', 'concept', ['visibility: world', 'synthesized_by: fixture'], 'concept'), { sourceId: SRC });+ await engine.addLink(CONCEPT, ATOM, 'input', 'synthesized_from', 'concept-provenance', undefined, undefined, { fromSourceId: SRC, toSourceId: SRC });++ for (const [name, member] of [['owner', OWNER], ['alice', MEMBER]] as const) {+ const minted = await mintLegacyToken(engine, { name, scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });+ tokens[name] = minted.token;+ await bindCredential(engine, { kind: 'token', id: minted.id }, { member });+ }+ const granted = await mintLegacyToken(engine, { name: 'granted', scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });+ tokens.granted = granted.token;+ await rescopeLegacyToken(engine, parseRescopeTokenArgs(['granted', '--private-pages', 'granted-sources']));+ await readsUnscopedOnPurpose(engine, granted.id);+ await setOwner(engine, OWNER);+ }, 240_000);++ afterAll(async () => {+ __setEmbedTransportForTests(null);+ resetGateway();+ await closeEngine?.();+ }, 60_000);++ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });+ async function call(who: string, op: string, params: Record<string, unknown>): Promise<string> {+ const auth = await provider().verifyAccessToken(tokens[who]!) as unknown as AuthInfo;+ const sourceId = auth.sourceId ?? 'default';+ const localFederated = await noGrantFederatedScope(engine, auth.hasSourceGrant, sourceId);+ const out = await dispatchToolCall(engine, op, { ...params }, {+ remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],+ ...(localFederated ? { localFederatedSourceIds: localFederated } : {}),+ });+ return out.content.map(x => x.text).join('\n');+ }+ /** For each read path, whether `who` sees the page `slug` (by its canary, or by its slug on list_pages). */+ async function reads(who: string, slug: string, name: string): Promise<Record<string, boolean>> {+ __resetPrivateVisibilityCacheForTests();+ return {+ get_page: (await call(who, 'get_page', { slug })).includes(canary(name)),+ search: (await call(who, 'search', { query: 'harbor ledger planning notes', match: 'keyword', autocut: false, fields: 'full', limit: 100 })).includes(canary(name)),+ query: (await call(who, 'query', { query: 'harbor ledger planning notes', expand: false, autocut: false, fields: 'full', limit: 100 })).includes(canary(name)),+ list_pages: (await call(who, 'list_pages', { limit: 100 })).includes(`"${slug}"`),+ };+ }+ const none = { get_page: false, search: false, query: false, list_pages: false };+ const all = { get_page: true, search: true, query: true, list_pages: true };++ test('fixture: the old slug now holds a world page and its rename alias is gone; the history row remains', async () => {+ expect(await engine.executeRaw(`SELECT 1 FROM slug_aliases WHERE source_id=$1 AND alias_slug=$2 AND notes='rename'`, [SRC, ORIGIN])).toEqual([]);+ expect(await engine.executeRaw<{ new_slug: string }>(`SELECT new_slug FROM slug_rename_history WHERE source_id=$1 AND old_slug=$2`, [SRC, ORIGIN]))+ .toEqual([{ new_slug: MOVED }]);+ expect((await engine.getPage(ORIGIN, { sourceId: SRC }))?.frontmatter).toMatchObject({ visibility: 'world' });+ });++ test('(a) control: the world-stamped atom is hidden from a non-owner remote caller', async () => {+ expect(await reads('alice', ATOM, 'atom')).toEqual(none);+ }, 60_000);++ test('(b) the concept whose input is that atom is hidden from the same caller', async () => {+ expect(await reads('alice', CONCEPT, 'concept')).toEqual(none);+ }, 60_000);++ test('(c) the owner still reads the atom and the concept', async () => {+ expect({ atom: await reads('owner', ATOM, 'atom'), concept: await reads('owner', CONCEPT, 'concept') }).toEqual({ atom: all, concept: all });+ }, 60_000);++ test('(d) doctor derived_visibility counts the concept in looser_concepts', async () => {+ const details = (await derivedVisibilityCheck(engine, SRC)).details as { looser_concepts: number };+ expect(details.looser_concepts).toBe(1);+ }, 60_000);++ test('(e) doctor derived_visibility counts the atom in looser_atoms', async () => {+ const details = (await derivedVisibilityCheck(engine, SRC)).details as { looser_atoms: number };+ expect(details.looser_atoms).toBe(1);+ }, 60_000);++ test('(f) a granted_sources credential on the source still reads the atom and the concept', async () => {+ expect({ atom: await reads('granted', ATOM, 'atom'), concept: await reads('granted', CONCEPT, 'concept') }).toEqual({ atom: all, concept: all });+ }, 60_000);+ });+}diff --git a/test/e2e/private-visibility-plan-postgres.test.ts b/test/e2e/private-visibility-plan-postgres.test.tsindex 9dac0d9471..907cb0aa7c 100644--- a/test/e2e/private-visibility-plan-postgres.test.ts+++ b/test/e2e/private-visibility-plan-postgres.test.ts@@ -15,7 +15,11 @@* missing and a cross-source `derived_from`, life-chronicle events);* 2. precondition: the IN form's estimated cost per page is above* `jit_above_cost / 500`, so any read that checks 500 pages compiles;- * 3. the emitted form's cost per page is below it.+ * 3. the emitted form's cost per page is below it;+ * 4. it stays below it once the concepts carry provenance links and the+ * atoms' origins rename history, with links and slug_rename_history+ * analyzed (the concept-input probe, gbrain_concept_inputs_private,+ * reads both).*/import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import { getEngine, hasDatabase, setupDB, teardownDB } from './helpers.ts';@@ -36,6 +40,30 @@ async function planCost(sql: string): Promise<number> {return row['QUERY PLAN'][0].Plan['Total Cost'];}+/** The estimated cost per page of the IN spelling (before) and the emitted rule (after), and the jit budget per page. */+async function costPerPage(): Promise<{ before: number; after: number; budget: number }> {+ const [{ jit_above_cost }] = await getEngine().executeRaw<{ jit_above_cost: string }>(`SELECT current_setting('jit_above_cost') AS jit_above_cost`);+ const fragment = privatePagesFilterFragment('p');+ const scan = (predicate: string) => planCost(`SELECT count(*) FROM pages p WHERE ${predicate}`);+ const base = await scan('true');+ const perPage = async (predicate: string) => ((await scan(predicate)) - base) / PAGES;+ return { before: await perPage(inSubqueryForm(fragment)), after: await perPage(fragment), budget: Number(jit_above_cost) / 500 };+}++/** Both spellings hide the same pages, and each rule of the fixture fires. */+async function expectSameHiddenPages(): Promise<void> {+ const fragment = privatePagesFilterFragment('p');+ const read = (predicate: string) => getEngine().executeRaw<{ slug: string }>(`SELECT p.slug FROM pages p WHERE ${predicate} ORDER BY p.slug`);+ const emitted = (await read(fragment)).map(row => row.slug);+ const before = (await read(inSubqueryForm(fragment))).map(row => row.slug);+ expect(emitted).toEqual(before);+ const hidden = PAGES - emitted.length;+ // Every rule fires on this fixture: explicit, atom origin, concept, string/array/.md lineage, events.+ expect(hidden).toBeGreaterThanOrEqual(Math.floor(PAGES / 23) * 8);+ for (const offset of [0, 2, 4, 5, 6, 7, 10, 11]) expect(emitted).not.toContain(`notes/n-${23 + offset}`);+ for (const offset of [1, 3, 8, 9, 12]) expect(emitted).toContain(`notes/n-${23 + offset}`);+}+(hasDatabase() ? describe : describe.skip)('private-page rule plan cost (Postgres)', () => {beforeAll(async () => {await setupDB();@@ -69,31 +97,34 @@ async function planCost(sql: string): Promise<number> {afterAll(async () => { await teardownDB(); });test('the ANY(ARRAY) and IN spellings hide exactly the same pages', async () => {- const engine = getEngine();- const fragment = privatePagesFilterFragment('p');- const read = (predicate: string) => engine.executeRaw<{ slug: string }>(`SELECT p.slug FROM pages p WHERE ${predicate} ORDER BY p.slug`);- const emitted = (await read(fragment)).map(row => row.slug);- const before = (await read(inSubqueryForm(fragment))).map(row => row.slug);- expect(emitted).toEqual(before);- const hidden = PAGES - emitted.length;- // Every rule fires on this fixture: explicit, atom origin, concept, string/array/.md lineage, events.- expect(hidden).toBeGreaterThanOrEqual(Math.floor(PAGES / 23) * 8);- for (const offset of [0, 2, 4, 5, 6, 7, 10, 11]) expect(emitted).not.toContain(`notes/n-${23 + offset}`);- for (const offset of [1, 3, 8, 9, 12]) expect(emitted).toContain(`notes/n-${23 + offset}`);+ await expectSameHiddenPages();});test('the rule costs the planner little enough per page that a 500-page read stays under jit_above_cost', async () => {- const engine = getEngine();- const [{ jit_above_cost }] = await engine.executeRaw<{ jit_above_cost: string }>(`SELECT current_setting('jit_above_cost') AS jit_above_cost`);- const budget = Number(jit_above_cost) / 500;- const fragment = privatePagesFilterFragment('p');- const scan = (predicate: string) => planCost(`SELECT count(*) FROM pages p WHERE ${predicate}`);- const base = await scan('true');- const perPage = async (predicate: string) => ((await scan(predicate)) - base) / PAGES;- const before = await perPage(inSubqueryForm(fragment));- const after = await perPage(fragment);+ const { before, after, budget } = await costPerPage();console.info(JSON.stringify({ metric: 'private-visibility-cost-per-page', before, after, budget }));if (before <= budget) throw new Error(`Precondition failed: the IN spelling already costs ${before} per page (budget ${budget}); this check cannot discriminate.`);expect(after).toBeLessThan(budget);});++ test('it stays under jit_above_cost with provenance links and rename history analyzed', async () => {+ const engine = getEngine();+ // Each synthesized concept (offset 4) names the two atoms of its block (offsets 2 and 3) as its inputs.+ await engine.executeRaw(`INSERT INTO links (from_page_id, to_page_id, link_type, link_source)+ SELECT c.id, a.id, 'synthesized_from', 'concept-provenance' FROM pages c JOIN pages a ON a.source_id = c.source_id+ AND a.slug IN ('notes/n-' || (substr(c.slug, 9)::int - 2), 'notes/n-' || (substr(c.slug, 9)::int - 1))+ WHERE c.type = 'concept'`);+ // Each atom's origin was once renamed to a slug no page holds now, so no page changes visibility.+ await engine.executeRaw(`INSERT INTO slug_rename_history (source_id, old_slug, new_slug)+ SELECT source_id, frontmatter->>'source_slug', 'archive/' || slug FROM pages WHERE type = 'atom'`);+ await engine.executeRaw('ANALYZE links');+ await engine.executeRaw('ANALYZE slug_rename_history');+ const [{ links }] = await engine.executeRaw<{ links: number }>(`SELECT count(*)::int AS links FROM links WHERE link_source = 'concept-provenance'`);+ expect(links).toBe(Math.ceil((PAGES - 4) / 23) * 2);+ await expectSameHiddenPages();+ const { before, after, budget } = await costPerPage();+ console.info(JSON.stringify({ metric: 'private-visibility-cost-per-page-analyzed', before, after, budget }));+ if (before <= budget) throw new Error(`Precondition failed: the IN spelling already costs ${before} per page (budget ${budget}); this check cannot discriminate.`);+ expect(after).toBeLessThan(budget);+ });});