Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: hot-memory watermark digests the rulebook and coverage; the source-boundary diagnostic reads only other sources under the caller's read policy (GBRA-218)

garrytan10551e2e51a3
diff --git a/src/core/facts/meta-hook.ts b/src/core/facts/meta-hook.ts
index aad7fc720c..fd97eeb615 100644
--- a/src/core/facts/meta-hook.ts
+++ b/src/core/facts/meta-hook.ts
@@ -98,8 +98,8 @@ async function withdrawalWatermark(engine: OperationContext['engine'], sourceId:
if (typeof engine.executeRaw !== 'function') return '';
const [row] = await engine.executeRaw<{ n: number; at: string | null; rules: string | null }>(
`SELECT count(*)::int AS n, max(withdrawn_at)::text AS at,
- (SELECT count(*)::text || ':' || coalesce(max(revision), 0)::text FROM access_rules)
- || ':' || (SELECT count(*)::text FROM access_pending_coverage) AS rules
+ (SELECT md5(coalesce(string_agg(source_id || '/' || rule_id || '/' || prefix || '/' || array_to_string(readable_by, ',') || '/' || revision::text, '|' ORDER BY source_id, rule_id), '')) FROM access_rules)
+ || ':' || (SELECT md5(coalesce(string_agg(source_id || '/' || prefix || '/' || reason || '/' || coalesce(revision::text, ''), '|' ORDER BY source_id, prefix, reason), '')) FROM access_pending_coverage) AS rules
FROM fact_withdrawals WHERE source_id = $1`, [sourceId]);
return `${row?.n ?? 0}:${row?.at ?? ''}:${row?.rules ?? ''}`;
}
diff --git a/src/core/ops/context.ts b/src/core/ops/context.ts
index aa6ed31fe8..f048078c71 100644
--- a/src/core/ops/context.ts
+++ b/src/core/ops/context.ts
@@ -1078,12 +1078,12 @@ export async function requireWritablePage(
}
const visibleScope = federatedSearchScope(ctx);
- const spansAnotherSource =
- (visibleScope.sourceIds?.some((sourceId) => sourceId !== writeSource) ?? false) ||
- (visibleScope.sourceId !== undefined && visibleScope.sourceId !== writeSource);
- if (spansAnotherSource) {
- const visible = await ctx.engine.getPage(slug, visibleScope);
- if (visible && visible.source_id !== writeSource && !await remoteTargetHidden(ctx.engine, ctx.remote, visible.source_id, slug)) {
+ // GBRA-218: the diagnostic reads only the other sources, under the caller's read policy, so a page the caller cannot
+ // read there (or a hidden row in the write source) answers exactly as a missing slug.
+ const otherSources = (visibleScope.sourceIds ?? (visibleScope.sourceId !== undefined ? [visibleScope.sourceId] : [])).filter(id => id !== writeSource);
+ if (otherSources.length) {
+ const visible = await ctx.engine.getPage(slug, await readPolicyOpts(ctx, { sourceIds: otherSources }));
+ if (visible && !await remoteTargetHidden(ctx.engine, ctx.remote, visible.source_id, slug)) {
throw opError(
'permission_denied',
`${operation}${endpoint === 'page' ? '' : ` ${endpoint}`} page "${slug}" is readable from source "${visible.source_id}" but this client writes to source "${writeSource}".`,
diff --git a/test/hot-memory-invalidation.test.ts b/test/hot-memory-invalidation.test.ts
index d846fd196f..ffce1e8452 100644
--- a/test/hot-memory-invalidation.test.ts
+++ b/test/hot-memory-invalidation.test.ts
@@ -20,7 +20,7 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { recordFactWithdrawal } from '../src/core/facts/withdrawal.ts';
import { importFromContent } from '../src/core/import-file.ts';
-import { proposeRevision } from '../src/core/access/policy.ts';
+import { clearPolicyInvalid, markPolicyInvalid, proposeRevision } from '../src/core/access/policy.ts';
import {
getBrainHotMemoryMeta, invalidateHotMemoryForEngine, __hotMemoryCacheForTests, __resetHotMemoryCacheForTests,
} from '../src/core/facts/meta-hook.ts';
@@ -132,3 +132,17 @@ test('GBRA-218: a folder narrowing installed outside this dispatcher is not serv
}
});
+
+test('GBRA-218: coverage replaced by the same number of rows is not served from the cache', async () => {
+ for (const engine of engines) {
+ const { id, marker } = await seed(engine);
+ const slug = `team/cover-${marker}`;
+ await importFromContent(engine, slug, '---\ntype: note\ntitle: Team cover\n---\n# Team cover\n\nCover.', { sourceId: 'default', noEmbed: true });
+ await engine.executeRaw('UPDATE facts SET source_markdown_slug = $2 WHERE id = $1', [id, slug]);
+ await markPolicyInvalid(engine, 'default', ['elsewhere/'], { test: 'GBRA-218' });
+ expect(await metaCarries(engine, marker, true)).toBe(true);
+ await markPolicyInvalid(engine, 'default', ['team/'], { test: 'GBRA-218' });
+ expect(await metaCarries(engine, marker, true)).toBe(false);
+ await clearPolicyInvalid(engine, 'default');
+ }
+});
diff --git a/test/source-boundary-mutation-errors.test.ts b/test/source-boundary-mutation-errors.test.ts
index fe03f05110..1ea6b66b7b 100644
--- a/test/source-boundary-mutation-errors.test.ts
+++ b/test/source-boundary-mutation-errors.test.ts
@@ -35,6 +35,7 @@ import type { BrainEngine } from '../src/core/engine.ts';
import { PageMissingError } from '../src/core/engine-errors.ts';
import { dispatchToolCall as dispatchToolCallImpl } from '../src/mcp/dispatch.ts';
import { runSources } from '../src/commands/sources.ts';
+import { importFromContent } from '../src/core/import-file.ts';
import type { AuthInfo } from '../src/core/operations.ts';
let engine: PGLiteEngine;
@@ -388,3 +389,37 @@ describe('mutation-time deletion race reclassification (#4109)', () => {
});
});
});
+
+describe('GBRA-218: the source-boundary diagnostic never reveals a page the caller cannot read', () => {
+ const MISSING = 'topics/sbme-never-written';
+ const swap = <T,>(v: T, from: string, to: string): T => JSON.parse(JSON.stringify(v).replaceAll(from, to));
+ const ops = (slug: string): Array<[string, Record<string, unknown>]> => [
+ ['put_raw_data', { slug, source: 'acme-example', data: { a: 1 } }],
+ ['add_link', { from: FROM_SLUG, to: slug, link_type: 'mentions' }],
+ ['add_timeline_entry', { slug, date: '2026-01-01', summary: 'Boundary call' }],
+ ];
+ async function sameAsMissing(slug: string, missing: string, hiddenSource: string) {
+ for (const [op, params] of ops(slug)) {
+ const hidden = await dispatchToolCall(engine, op, params, GRANTED);
+ const absent = await dispatchToolCall(engine, op, swap(params, slug, missing), GRANTED);
+ expect({ op, isError: hidden.isError, body: swap(payload(hidden), slug, missing) }).toEqual({ op, isError: absent.isError, body: payload(absent) });
+ expect({ op, isError: hidden.isError }).toEqual({ op, isError: true });
+ if (hiddenSource === FOREIGN_SRC) expect(hidden.content[0]!.text).not.toContain(FOREIGN_SRC);
+ }
+ }
+
+ test('a private page that exists only in another granted source answers exactly as a missing slug', async () => {
+ const slug = 'topics/sbme-private-foreign';
+ await importFromContent(engine, slug, '---\ntype: concept\ntitle: Private foreign\nvisibility: private\n---\nPrivate fixture.', { sourceId: FOREIGN_SRC, noEmbed: true });
+ await sameAsMissing(slug, MISSING, FOREIGN_SRC);
+ });
+
+ test('a member-hidden write-source page with a readable twin answers exactly as a slug only the twin holds', async () => {
+ const slug = 'topics/sbme-twin-hidden', twinOnly = 'topics/sbme-twin-only';
+ await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1) ON CONFLICT DO NOTHING', ['alice-example']);
+ await importFromContent(engine, slug, '---\ntype: concept\ntitle: Twin hidden\nreadable_by: [alice-example]\n---\nHidden fixture.', { sourceId: WRITE_SRC, noEmbed: true });
+ await seed(slug, FOREIGN_SRC);
+ await seed(twinOnly, FOREIGN_SRC);
+ await sameAsMissing(slug, twinOnly, WRITE_SRC);
+ });
+});