Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(access): local core memory reads keep the plain privacy fragment
garrytan39c6269f8a02
diff --git a/src/core/core-memory.ts b/src/core/core-memory.tsindex f7e77d45e4..125c2978d3 100644--- a/src/core/core-memory.ts+++ b/src/core/core-memory.ts@@ -17,7 +17,7 @@import { createHash } from 'node:crypto';import type { BrainEngine } from './engine.ts';import { sanitizeRemoteBody } from './remote-body.ts';-import { privatePagesFilterForCaller } from './search/private-visibility.ts';+import { privatePagesFilterForCaller, privatePagesFilterFragment } from './search/private-visibility.ts';import { quarantineFilterFragment } from './quarantine.ts';import { compareTrust, OWNER_TIER_FLOOR, type TrustTier } from './trust/tier.ts';import type { ReadEligibility } from './eligibility/policy.ts';@@ -222,6 +222,8 @@ export interface ListCorePagesOpts {/** Restrict to these sources; omitted = every source in the brain. */sourceIds?: readonly string[];excludePrivate?: boolean;+ /** A remote read: `excludePrivate` applies the caller's policy instead of the plain privacy fragment. */+ forCaller?: boolean;/** Exclude one page (the page being written, measured separately). */exclude?: { sourceId: string; slug: string };/**@@ -252,7 +254,7 @@ export async function listCorePages(exec: RawExec, opts: ListCorePagesOpts = {})params.push(opts.exclude.sourceId, opts.exclude.slug);where.push(`NOT (p.source_id = $${params.length - 1} AND p.slug = $${params.length})`);}- if (opts.excludePrivate) where.push(privatePagesFilterForCaller('p'));+ if (opts.excludePrivate) where.push(opts.forCaller ? privatePagesFilterForCaller('p') : privatePagesFilterFragment('p'));const policy = opts.eligibility;if (policy) where.push(quarantineFilterFragment('p'), pageEligibleSql('p', { floor: policy.floor }));const trustCols = policy@@ -300,6 +302,7 @@ export interface LoadCoreBlockOpts {/** Federated grant; when set, only these sources render. */allowedSources?: readonly string[] | null;excludePrivate?: boolean;+ forCaller?: boolean;notices?: string[];settings?: CoreSettings;/** #5575: the caller's proactive eligibility (token floor); absent, the `core_delivery` surface policy applies. */@@ -364,7 +367,7 @@ export async function loadCoreBlock(engine: BrainEngine, opts: LoadCoreBlockOpts}const sourceIds = sessionCoreSources(opts.sessionSourceId, opts.allowedSources ?? null);const eligibility = opts.eligibility ?? await proactiveEligibility({ engine }, 'core_delivery');- const all = await listCorePages(engine, { sourceIds, excludePrivate: opts.excludePrivate ?? true, eligibility });+ const all = await listCorePages(engine, { sourceIds, excludePrivate: opts.excludePrivate ?? true, forCaller: opts.forCaller, eligibility });const listed = all.filter(p => !p.suppressed);const suppressed = all.filter(p => p.suppressed).map(p => ({ source_id: p.source_id, slug: p.slug, family: 'unconfirmed agent-written instruction (gbrain trust review)' }));const { pages, withheld } = await applyCoreSensitivity(listed);diff --git a/src/core/ops/facts.ts b/src/core/ops/facts.tsindex 4a6a2663a4..7adeee2ab2 100644--- a/src/core/ops/facts.ts+++ b/src/core/ops/facts.ts@@ -798,7 +798,7 @@ const context_pack: Operation = {// `default` plus this source, inside the caller's grant; it packs first.const { loadCoreBlock } = await import('../core-memory.ts');const allowed = ctx.auth?.allowedSources?.length ? ctx.auth.allowedSources : null;- const core = await loadCoreBlock(ctx.engine, { sessionSourceId: sourceId, allowedSources: allowed, excludePrivate: !includePrivate, eligibility })+ const core = await loadCoreBlock(ctx.engine, { sessionSourceId: sourceId, allowedSources: allowed, excludePrivate: !includePrivate, forCaller: true, eligibility }).catch(() => null);const coreText = core?.text ?? '';const coreTokens = estimateTokens(coreText);diff --git a/src/core/persistence/core-guard.ts b/src/core/persistence/core-guard.tsindex 3a445caf72..1fc96110e3 100644--- a/src/core/persistence/core-guard.ts+++ b/src/core/persistence/core-guard.ts@@ -156,7 +156,7 @@ export async function prepareCoreGuard(engine: BrainEngine, input: CoreGuardInpuif (total > settings.maxChars && grew) {// Remote diagnostics never name pages outside the write's own source or private pages.const seen = remote- ? await listCorePages(exec, { sourceIds: [row.source_id], excludePrivate: true, exclude: { sourceId: row.source_id, slug: row.slug } })+ ? await listCorePages(exec, { sourceIds: [row.source_id], excludePrivate: true, forCaller: true, exclude: { sourceId: row.source_id, slug: row.slug } }): pages;const hidden = pages.reduce((n, p) => n + p.chars, 0) - seen.reduce((n, p) => n + p.chars, 0);throw budgetError(row, total, settings.maxChars, afterChars, seen, hidden);diff --git a/test/caller-aux-page-reads.test.ts b/test/caller-aux-page-reads.test.tsindex 7aeab2ab2a..e14370fa09 100644--- a/test/caller-aux-page-reads.test.ts+++ b/test/caller-aux-page-reads.test.ts@@ -56,7 +56,7 @@ afterAll(async () => {await engine.disconnect();});-const core = () => listCorePages(engine, { sourceIds: [SRC], excludePrivate: true }).then(pages => pages.map(p => p.slug).sort());+const core = (forCaller = true) => listCorePages(engine, { sourceIds: [SRC], excludePrivate: true, forCaller }).then(pages => pages.map(p => p.slug).sort());describe('#8395 auxiliary page reads follow the caller', () => {test('core memory lists the pages the bound member may read', async () => {@@ -66,6 +66,19 @@ describe('#8395 auxiliary page reads follow the caller', () => {expect(await as('alice', false, core)).toEqual([ALICE, SECRET]);});+ test('local core reads 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 core(false)).toEqual([]);+ expect(await core(true)).toEqual([SECRET]);+ } finally {+ if (prior === undefined) delete process.env.GBRAIN_REMOTE_PRIVATE_PAGES; else process.env.GBRAIN_REMOTE_PRIVATE_PAGES = prior;+ __resetPrivateVisibilityCacheForTests();+ }+ });+test('strict entity resolution and refusal candidates', async () => {await as('alice', true, async () => {expect(await resolveStrictEntityReference(engine, SRC, ALICE, { excludePrivate: true, forCaller: true })).toEqual({ slug: ALICE, arm: 'exact_page' });