Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (links effect holds the member-access part under a privacy hatch)
garrytanc433736b27dd
diff --git a/src/core/persistence/effect-links.ts b/src/core/persistence/effect-links.tsindex c612aaee04..3574d1be7f 100644--- a/src/core/persistence/effect-links.ts+++ b/src/core/persistence/effect-links.ts@@ -8,13 +8,13 @@ import { extractPageLinks, isAutoLinkEnabled, isGlobalBasenameEnabled, makeResolimport { protectedRegions } from '../fence-scan.ts';import { PROTECTED_PAIRS, sanitizeRemoteBody } from '../remote-body.ts';import { confirmBodyPin, openSegmentedBody, readBodyRange, type BodyAudience } from '../page-body/read-range.ts';-import { privatePagesFilterFragment } from '../search/private-visibility.ts';+import { privacyPartSql } from '../search/private-visibility.ts';import { authorizeStoredRequest } from './authority.ts';import { completeEffect } from './effect-journal.ts';import { REMOTE_AUTO_LINKS_KEY, REMOTE_MENTION_LINK_SOURCE, type AppendedRange, type PersistenceEffect } from './effect-model.ts';import { guardEffectSource } from './effect-recovery.ts';import type { SqlEngine, WriteAuthority, WriteRequest } from './model.ts';-import { excludesPrivateWrites } from './page-visibility.ts';+import { excludesPrivateWrites, writerReadPolicySql } from './page-visibility.ts';import { isSegmentedPage } from '../page-body/page-shape.ts';import { extractSegmentedPageLinks } from '../page-body/segment-links.ts';import { inlineRegions } from '../page-body/tail-digest.ts';@@ -119,7 +119,7 @@ export async function runLinksEffect(engine: BrainEngine, effect: PersistenceEffconst live = await lockedPage(tx, effect, row, snapshot, targets, hostId);if (!live) return;if (live.revision !== snapshot.revision) { await skip(tx, 'superseded'); return; }- const { wanted, owned } = await writableTargets(tx, row, live, targets, skipped);+ const { wanted, owned, hidden } = await writableTargets(tx, row, live, targets, skipped);const obsolete = owned.filter(link => !wanted.has(link.slug)).map(link => link.id);const kept = new Set(owned.map(link => link.slug));const additions = mentionEdges(live.page.slug, sourceId, wanted, kept);@@ -130,7 +130,7 @@ export async function runLinksEffect(engine: BrainEngine, effect: PersistenceEff// Wanted pages: a missing mention target is recorded so the edge appears once that page is written (core/wanted-links.ts).if (await isRemoteWantedPagesEnabled(tx)) {await replaceWantedLinks(tx, { pageId: Number(live.page.id), sourceId }, { producers: ['body'], rows: skipped- .filter(target => target.reason === 'missing')+ .filter(target => target.reason === 'missing' && !hidden.has(target.slug)).map(target => ({ producer: 'body', ref_kind: 'slug', target_source_id: sourceId, target_ref: target.slug,link_type: 'mentions', context: (targets.get(target.slug) ?? '').slice(0, 240) })) });}@@ -243,25 +243,30 @@ async function lockedPage(tx: BrainEngine, effect: PersistenceEffect, row: Write/*** The targets that get an edge: existing pages of the source, visible to the writer (a remote writer that excludes- * private pages never links one) and inside its slug grant; the rest are added to `skipped` with the reason. Also- * the edges this producer already owns from the page.+ * private pages never links one) and inside its slug grant; the rest are added to `skipped` with the reason. A page+ * the writer may not read under the member-access part (GBRA-218; no operator hatch relaxes it) is skipped as+ * `missing`, so the receipt cannot tell it from an absent page, and is returned in `hidden` so it is never recorded+ * as wanted. Also the edges this producer already owns from the page.*/async function writableTargets(tx: BrainEngine, row: WriteRequest, live: PageSnapshot, targets: Map<string, string>, skipped: SkippedLinkTarget[]) {const sourceId = live.page.source_id;const hidePrivate = row.authority.remote && ((row.authority.excludePrivate ?? true) || await excludesPrivateWrites(tx, true));- const existing = targets.size ? await tx.executeRaw<{ slug: string; visible: boolean }>(`SELECT p.slug,(${privatePagesFilterFragment('p')}) AS visible+ const readable = row.authority.remote && targets.size ? await writerReadPolicySql(tx, row.authority, 'p', false) : 'TRUE';+ const existing = targets.size ? await tx.executeRaw<{ slug: string; readable: boolean; visible: boolean }>(`SELECT p.slug,(${readable}) AS readable,+ (${privacyPartSql('p')}) AS visibleFROM pages p WHERE p.source_id=$1 AND p.slug=ANY($2::text[]) AND p.deleted_at IS NULL`, [sourceId, [...targets.keys()]]) : [];- const visible = new Map(existing.map(page => [page.slug, page.visible]));+ const found = new Map(existing.filter(page => page.readable).map(page => [page.slug, page.visible]));+ const hidden = new Set(existing.filter(page => !page.readable).map(page => page.slug));const granted = await targetGrant(tx, row.authority);const wanted = new Map<string, string>();for (const [slug, context] of targets) {- const reason = !visible.has(slug) ? 'missing' : hidePrivate && !visible.get(slug) ? 'not_visible' : !granted(slug) ? 'outside_grant' : null;+ const reason = !found.has(slug) ? 'missing' : hidePrivate && !found.get(slug) ? 'not_visible' : !granted(slug) ? 'outside_grant' : null;if (reason) skipped.push({ slug, reason });else wanted.set(slug, context);}const owned = await tx.executeRaw<{ id: number; slug: string }>(`SELECT l.id,t.slug FROM links l JOIN pages t ON t.id=l.to_page_idWHERE l.link_source=$1 AND l.origin_page_id=$2`, [REMOTE_MENTION_LINK_SOURCE, live.page.id]);- return { wanted, owned };+ return { wanted, owned, hidden };}/** Plain `mentions` edges from the page to each wanted target it does not already have from this producer. */diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.tsindex 6b5b70898c..5d18953924 100644--- a/src/core/persistence/page-visibility.ts+++ b/src/core/persistence/page-visibility.ts@@ -42,14 +42,25 @@ export async function authorizePageVisibility(engine: SqlEngine, authority: Writconst policy = await readRemotePolicyNow(engine);const hidePrivate = (authority.excludePrivate ?? true) || !policy.privacyRelaxed;if (!hidePrivate && !policy.restricted) return;- const identities = policy.restricted ? await writerIdentities(engine, authority) : [[]];- const folder = policy.restricted ? await folderAccessForAll(engine, identities) : { satisfiedRuleIds: [], folderCoverage: [] };- const fragment = withCallerAccess({ folderCoverage: folder.folderCoverage }, () => readPolicyFragment('pages', { all: identities }, hidePrivate, folder.satisfiedRuleIds));+ const fragment = await writerReadPolicySql(engine, authority, 'pages', hidePrivate, policy.restricted);const rows = await engine.executeRaw(`SELECT 1 FROM pages WHERE source_id=$1 AND slug=$2AND NOT (${fragment}) LIMIT 1`, [authority.sourceId, slug]);if (rows.length) throw pageNotVisibleError();}+/**+ * The read policy a stored remote write's writer is held to, re-resolved live: the member-access part always (an+ * empty identity, the strictest reading, when the brain has no restrictions), then the privacy part when+ * `hidePrivate`. No operator hatch relaxes the member-access part.+ */+export async function writerReadPolicySql(engine: SqlEngine, authority: WriteAuthority, pageAlias: string, hidePrivate: boolean,+ restricted?: boolean): Promise<string> {+ restricted ??= (await readRemotePolicyNow(engine)).restricted;+ const identities = restricted ? await writerIdentities(engine, authority) : [[]];+ const folder = restricted ? await folderAccessForAll(engine, identities) : { satisfiedRuleIds: [], folderCoverage: [] };+ return withCallerAccess({ folderCoverage: folder.folderCoverage }, () => readPolicyFragment(pageAlias, { all: identities }, hidePrivate, folder.satisfiedRuleIds));+}+/*** The identities a stored write is checked under, re-resolved live: the principal's member, or (GBRA-231) every entry* of the audience it was accepted for, bounded by the principal's live may_act_for. An audience no longer permitteddiff --git a/test/access-writes.test.ts b/test/access-writes.test.tsindex 83e8e69946..83af127b3e 100644--- a/test/access-writes.test.ts+++ b/test/access-writes.test.ts@@ -11,7 +11,8 @@* a member disabled after submission (a queued job, a replay) loses the page* before commit; no operator hatch relaxes that.* remove_link on an edge of an unreadable page answers as for a missing- * page and removes nothing.+ * page and removes nothing. The remote links effect skips an unreadable+ * mention target as missing (no edge, no wanted row), hatch or not.* 2. Fails when: add_link, remove_link, put_raw_data or any persistence write lands on an* unreadable page, a listed member is refused its own page, a malformed or* lock-out readable_by is written, or a stored write still publishes after@@ -36,6 +37,8 @@ import { remoteCallerAccess } from '../src/core/access/caller.ts';import { submissionAuthority, authorizeStoredRequest } from '../src/core/persistence/authority.ts';import { admitWrite } from '../src/core/persistence/journal.ts';import { proposeRevision } from '../src/core/access/policy.ts';+import { runPersistenceEffects } from '../src/core/persistence/effects.ts';+import { localHostId } from '../src/core/persistence/identity.ts';import type { AuthInfo, OperationContext } from '../src/core/ops/contract.ts';import { testBackends } from './helpers/test-backends.ts';import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';@@ -253,6 +256,38 @@ for (const kind of testBackends()) {expect({ isError: own.isError, removed: own.json.removed }).toEqual({ isError: false, removed: 1 });}, 120_000);+ test('the remote links effect skips a page the writer cannot read exactly as a missing one, under a privacy hatch too', async () => {+ const folderSlug = 'partners/alice-example/brief';+ await importFromContent(engine, folderSlug, '---\ntype: note\ntitle: Alice brief\n---\n# Alice brief\n\nBrief canaryawbrief.', { sourceId: SRC, noEmbed: true });+ await proposeRevision(engine, SRC, [{ id: 'alice', prefix: 'partners/alice-example/', readable_by: ['member:alice-example'] }], { origin: 'cli', store: 'database' });+ const targets = [ALICE_PAGE, folderSlug, OPEN_PAGE, 'notes/never-written'];+ for (const hatch of [undefined, '1']) {+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: hatch }, async () => {+ for (const who of ['bob', 'unbound', 'alice']) {+ const slug = 'notes/mentions-' + who + (hatch ? '-hatch' : '');+ const put = await call(who, 'put_page', { slug, content: '---\ntype: note\ntitle: Mentions\n---\n# Mentions by ' + who + (hatch ? ' with the hatch' : '') + '\n\nSee ' + targets.map(t => '[[' + t + ']]').join(', ') + '.' });+ expect({ who, hatch, isError: put.isError }).toEqual({ who, hatch, isError: false });+ const settled = async () => (await engine.executeRaw<{ outcome: { added: number; skipped_targets: { slug: string; reason: string }[] } | null }>(+ "SELECT outcome FROM persistence_effects WHERE kind='links' AND source_id=$1 AND data->>'slug'=$2 ORDER BY id DESC LIMIT 1", [SRC, slug]))[0];+ for (let i = 0; i < 20 && !(await settled())?.outcome; i++) {+ await runPersistenceEffects(engine, { engine: 'pglite', embedding_disabled: true } as never, { hostId: localHostId() });+ await new Promise(resolve => setTimeout(resolve, 50));+ }+ const effect = await settled();+ const edges = await engine.executeRaw<{ slug: string }>('SELECT t.slug FROM links l JOIN pages f ON f.id=l.from_page_id JOIN pages t ON t.id=l.to_page_id'+ + ' WHERE f.source_id=$1 AND f.slug=$2 ORDER BY t.slug', [SRC, slug]);+ const wanted = await engine.executeRaw<{ target_ref: string }>('SELECT w.target_ref FROM wanted_links w JOIN pages p ON p.id=w.origin_page_id'+ + ' WHERE p.source_id=$1 AND p.slug=$2 ORDER BY w.target_ref', [SRC, slug]);+ const reader = who === 'alice';+ expect({ who, hatch, added: effect?.outcome?.added, edges: edges.map(e => e.slug), wanted: wanted.map(w => w.target_ref),+ skipped: Object.fromEntries(effect?.outcome?.skipped_targets.map(t => [t.slug, t.reason]) ?? []) }).toEqual({ who, hatch,+ added: reader ? 3 : 1, edges: reader ? [ALICE_PAGE, OPEN_PAGE, folderSlug].sort() : [OPEN_PAGE], wanted: ['notes/never-written'],+ skipped: reader ? { 'notes/never-written': 'missing' } : { [ALICE_PAGE]: 'missing', [folderSlug]: 'missing', 'notes/never-written': 'missing' } });+ }+ });+ }+ }, 240_000);+test('extract_facts treats a restricted source_slug exactly like a missing one', async () => {const hidden = await call('bob', 'extract_facts', { turn_text: 'Bob likes tea.', source_slug: ALICE_PAGE, visibility: 'world' });const missing = await call('bob', 'extract_facts', { turn_text: 'Bob likes tea.', source_slug: 'notes/never-written', visibility: 'world' });