Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(access): derived entity decisions keep the plain privacy fragment
garrytan2112ef685362
diff --git a/src/core/entities/resolve.ts b/src/core/entities/resolve.tsindex ca97ea8375..ff7e4684b1 100644--- a/src/core/entities/resolve.ts+++ b/src/core/entities/resolve.ts@@ -30,7 +30,7 @@ import { slugifySegment } from '../sync.ts';const siblingCanonical: typeof import('../mentions/siblings.ts').siblingCanonical = async (...args) =>(await import('../mentions/siblings.ts')).siblingCanonical(...args);import { isMissingSchemaObject } from '../utils.ts';-import { privatePagesFilterForCaller } from '../search/private-visibility.ts';+import { privatePagesFilterForCaller, privatePagesFilterFragment } from '../search/private-visibility.ts';import { rankingLinkSql } from '../link-ranking-policy.ts';/**@@ -480,17 +480,19 @@ export type StrictResolution =* same-name trigram arm. Never guesses from a bare-name prefix and never* falls back to a slugified name. With `excludePrivate`, private pages are* removed before uniqueness is counted, so an unreadable namesake can neither- * be returned nor change the outcome for a remote caller.+ * be returned nor change the outcome for a remote caller. `forCaller` reads with+ * the caller's policy (a remote read); without it the plain privacy fragment+ * applies, which a derived-visibility decision needs.*/export async function resolveStrictEntityReference(engine: BrainEngine,source_id: string,raw: string,- opts: { sameName?: boolean; excludePrivate?: boolean } = {},+ opts: { sameName?: boolean; excludePrivate?: boolean; forCaller?: boolean } = {},): Promise<StrictResolution> {const trimmed = raw.trim();if (!trimmed) return { slug: null, miss: 'no_page' };- const privacy = opts.excludePrivate ? `AND ${privatePagesFilterForCaller('p')}` : '';+ const privacy = opts.excludePrivate ? `AND ${opts.forCaller ? privatePagesFilterForCaller('p') : privatePagesFilterFragment('p')}` : '';const live = async (slugs: string[]) => slugs.length === 0 ? [] : engine.executeRaw<{ slug: string; type: string | null }>(`SELECT p.slug, p.type FROM pages pWHERE p.source_id = $1 AND p.deleted_at IS NULL AND p.slug = ANY($2::text[]) ${privacy}`,@@ -567,12 +569,12 @@ export async function entityReferenceCandidates(engine: BrainEngine,source_id: string,raw: string,- opts: { excludePrivate?: boolean } = {},+ opts: { excludePrivate?: boolean; forCaller?: boolean } = {},): Promise<string[]> {const trimmed = raw.trim();const token = slugify(trimmed);if (!token) return [];- const privacy = opts.excludePrivate ? `AND ${privatePagesFilterForCaller('p')}` : '';+ const privacy = opts.excludePrivate ? `AND ${opts.forCaller ? privatePagesFilterForCaller('p') : privatePagesFilterFragment('p')}` : '';const aliases = await aliasHitSlugs(engine, source_id, trimmed).catch(() => [] as string[]);const prefixed = (await findPrefixCandidates(engine, source_id, token)).map(c => c.slug);const named = await engine.executeRaw<{ slug: string }>(diff --git a/src/core/persistence/memory-mutations.ts b/src/core/persistence/memory-mutations.tsindex 9e011923b7..f9d2832115 100644--- a/src/core/persistence/memory-mutations.ts+++ b/src/core/persistence/memory-mutations.ts@@ -267,9 +267,9 @@ async function resolveRememberEntity(ctx: OperationContext, sourceId: string, enconst { entityReferenceCandidates, resolveEntitySlugWithSource, resolveStrictEntityReference } = await import('../entities/resolve.ts');if (entity.includes('/')) return (await resolveEntitySlugWithSource(ctx.engine, sourceId, entity))!;const excludePrivate = await resolveExcludePrivatePages(ctx.engine, ctx.remote);- const strict = await resolveStrictEntityReference(ctx.engine, sourceId, entity, { sameName: true, excludePrivate });+ const strict = await resolveStrictEntityReference(ctx.engine, sourceId, entity, { sameName: true, excludePrivate, forCaller: true });if (strict.slug !== null) return { slug: strict.slug };- const candidates = await entityReferenceCandidates(ctx.engine, sourceId, entity, { excludePrivate });+ const candidates = await entityReferenceCandidates(ctx.engine, sourceId, entity, { excludePrivate, forCaller: true });if (strict.miss === 'no_page' && !candidates.length) {const unnamed = await resolveEntitySlugWithSource(ctx.engine, sourceId, entity);if (unnamed?.source === 'fallback_slugify') return { slug: unnamed.slug };diff --git a/test/caller-aux-page-reads.test.ts b/test/caller-aux-page-reads.test.tsindex 0d35dbc886..7aeab2ab2a 100644--- a/test/caller-aux-page-reads.test.ts+++ b/test/caller-aux-page-reads.test.ts@@ -68,20 +68,34 @@ describe('#8395 auxiliary page reads follow the caller', () => {test('strict entity resolution and refusal candidates', async () => {await as('alice', true, async () => {- expect(await resolveStrictEntityReference(engine, SRC, ALICE, { excludePrivate: true })).toEqual({ slug: ALICE, arm: 'exact_page' });- expect(await entityReferenceCandidates(engine, SRC, 'Alice Example', { excludePrivate: true })).toContain(ALICE);- expect((await resolveStrictEntityReference(engine, SRC, BOB, { excludePrivate: true })).slug).toBeNull();- expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true })).slug).toBeNull();+ expect(await resolveStrictEntityReference(engine, SRC, ALICE, { excludePrivate: true, forCaller: true })).toEqual({ slug: ALICE, arm: 'exact_page' });+ expect(await entityReferenceCandidates(engine, SRC, 'Alice Example', { excludePrivate: true, forCaller: true })).toContain(ALICE);+ expect((await resolveStrictEntityReference(engine, SRC, BOB, { excludePrivate: true, forCaller: true })).slug).toBeNull();+ expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true, forCaller: true })).slug).toBeNull();});await as('alice', false, async () => {- expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true })).slug).toBe(SECRET);+ expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true, forCaller: true })).slug).toBe(SECRET);});await as('bob', true, async () => {- expect((await resolveStrictEntityReference(engine, SRC, ALICE, { excludePrivate: true })).slug).toBeNull();- expect(await entityReferenceCandidates(engine, SRC, 'Alice Example', { excludePrivate: true })).not.toContain(ALICE);+ expect((await resolveStrictEntityReference(engine, SRC, ALICE, { excludePrivate: true, forCaller: true })).slug).toBeNull();+ expect(await entityReferenceCandidates(engine, SRC, 'Alice Example', { excludePrivate: true, forCaller: true })).not.toContain(ALICE);});});+ test('derived decisions without forCaller keep the plain privacy fragment, even with the read hatch open', async () => {+ const prior = process.env.GBRAIN_REMOTE_PRIVATE_PAGES;+ process.env.GBRAIN_REMOTE_PRIVATE_PAGES = '1';+ try {+ __resetPrivateVisibilityCacheForTests();+ expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true })).slug).toBeNull();+ expect(await entityReferenceCandidates(engine, SRC, 'Carol Example', { excludePrivate: true })).not.toContain(SECRET);+ expect((await resolveStrictEntityReference(engine, SRC, SECRET, { excludePrivate: true, forCaller: true })).slug).toBe(SECRET);+ } finally {+ if (prior === undefined) delete process.env.GBRAIN_REMOTE_PRIVATE_PAGES; else process.env.GBRAIN_REMOTE_PRIVATE_PAGES = prior;+ __resetPrivateVisibilityCacheForTests();+ }+ });+test('retrieval pointers, pull counterparties and brief edits', async () => {for (const who of ['alice', 'bob'] as const) {await as(who, true, async () => {