Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Review fixes: ingest log keeps only readable pages, extract_entities skips an unreadable entity
garrytan444c3758f594
diff --git a/src/core/enrichment-service.ts b/src/core/enrichment-service.tsindex b34b68e59e..22df9dbe04 100644--- a/src/core/enrichment-service.ts+++ b/src/core/enrichment-service.ts@@ -34,7 +34,7 @@ import { isJunkEntityName } from './entity-name-quality.ts';import { followRetirement } from './entities/resolve.ts';import { maintenanceTransaction } from './persistence/attribution.ts';import { managedPersistenceEnabled } from './persistence/ownership.ts';-import { pageNotVisibleError, remoteTargetHidden } from './persistence/page-visibility.ts';+import { remoteTargetHidden } from './persistence/page-visibility.ts';import { resolveExcludePrivatePages } from './search/private-visibility.ts';import { assertUnmanagedCanonicalWriter } from './persistence/maintenance.ts';import { deriveTrust, derivedMaintenanceTransaction, lowerDerivedPage, lowerDerivedPageIn, lowerToDerivedTier } from './trust/taint.ts';@@ -88,7 +88,7 @@ export interface EnrichmentResult {* Why nothing was written: the page is retired and its superseded_by / merged_into chain has no live target (B-9),* or both this entity's slug and its hash-tagged slug hold pages that name another entity.*/- skipped_reason?: 'retired_page_unfollowable' | 'entity_slug_taken';+ skipped_reason?: 'retired_page_unfollowable' | 'entity_slug_taken' | 'page_not_found';tier: 1 | 2 | 3;backlinkCreated: boolean;timelineAdded: boolean;@@ -297,9 +297,12 @@ async function planEntity(let action: 'created' | 'updated' | 'skipped';if (existingPage) {- // GBRA-431: a remote caller never writes onto an entity page it cannot read; it gets the refusal a managed brain's- // timeline write gives it.- if (ctx && await remoteTargetHidden(engine, ctx.remote, sourceId, slug)) throw pageNotVisibleError();+ // GBRA-431: a remote caller never writes onto an entity page it cannot read; that entity is skipped as not found+ // and the rest of the batch still runs.+ if (ctx && await remoteTargetHidden(engine, ctx.remote, sourceId, slug)) {+ return { slug, action: 'skipped', skipped_reason: 'page_not_found', tier, backlinkCreated: false, timelineAdded: false,+ mentionCount, mentionSources, suggestedTier, tierEscalated };+ }// UPDATE path — add timeline entryaction = 'updated';} else {diff --git a/src/core/ops/ingest-log.ts b/src/core/ops/ingest-log.tsindex 38b5ab984a..abbd269629 100644--- a/src/core/ops/ingest-log.ts+++ b/src/core/ops/ingest-log.ts@@ -9,10 +9,17 @@import type { Operation } from './contract.ts';import { clampSearchLimit } from '../engine.ts';import { linkReadScopeOpts } from './context.ts';-import { remoteHiddenPages } from '../persistence/page-visibility.ts';+import { remoteReadablePages } from '../persistence/page-visibility.ts';-/** An absolute POSIX, home-relative or Windows path token in a source_ref. */-const HOST_PATH_RE = /(^|\s)(?:~?\/|[A-Za-z]:[\\/])\S*/g;+/** A file URL or an absolute, home-relative, relative or Windows path token in a free-form source_ref. */+const HOST_PATH_RE = /(^|\s)(?:file:\/\/|\.{1,2}[\\/]|~?\/|[A-Za-z]:[\\/])\S*/g;++/** A source_ref as a remote reader sees it: a sync row keeps only its commit, an import row names no directory. */+function remoteSourceRef(sourceType: string, ref: string): string {+ if (sourceType === 'git_sync') return ref.replace(/^.*?( @ [0-9a-f]+)?$/s, (_m, commit: string | undefined) => `<local path>${commit ?? ''}`);+ if (sourceType === 'directory') return '<local path>';+ return ref.replace(HOST_PATH_RE, '$1<local path>');+}// --- Ingest Log ---@@ -70,11 +77,11 @@ const get_ingest_log: Operation = {if (ctx.remote === false) return rows;// GBRA-431: a remote reader sees no slug of a page it cannot read, no row about such pages alone (its summary is// about them) and no host filesystem path (a sync row's source_ref names the brain checkout).- const hidden = await remoteHiddenPages(ctx.engine, ctx.remote, rows.flatMap(r => (r.pages_updated ?? []).map(slug => ({ source_id: r.source_id, slug }))));+ const readable = await remoteReadablePages(ctx.engine, rows.flatMap(r => (r.pages_updated ?? []).map(slug => ({ source_id: r.source_id, slug }))));return rows.flatMap(r => {const pages = r.pages_updated ?? [];- const visible = pages.filter(slug => !hidden.has(`${r.source_id}\u0000${slug}`));- return pages.length > 0 && visible.length === 0 ? [] : [{ ...r, source_ref: r.source_ref.replace(HOST_PATH_RE, '$1<local path>'), pages_updated: visible }];+ const visible = pages.filter(slug => readable.has(`${r.source_id}\u0000${slug}`));+ return pages.length > 0 && visible.length === 0 ? [] : [{ ...r, source_ref: remoteSourceRef(r.source_type, r.source_ref), pages_updated: visible }];});},scope: 'read',diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.tsindex 28349e128b..23668a447f 100644--- a/src/core/persistence/page-visibility.ts+++ b/src/core/persistence/page-visibility.ts@@ -105,15 +105,16 @@ export async function remoteTargetHidden(engine: BrainEngine, remote: boolean |}/**- * GBRA-431: the (source_id, slug) pairs among `pages` that have a page row (soft-deleted rows included, fail closed)- * the current remote caller cannot read, keyed `${source_id}\u0000${slug}`. Empty for a trusted local caller.+ * GBRA-431: the (source_id, slug) pairs among `pages` that name a live page the current remote caller can read, keyed+ * `${source_id}\u0000${slug}`. A slug with no live page row (never written, deleted, purged or renamed) is not+ * readable, so a caller of this keeps only what it returns (fail closed). Call for remote callers only.*/-export async function remoteHiddenPages(engine: BrainEngine, remote: boolean | undefined, pages: readonly { source_id: string; slug: string }[]): Promise<Set<string>> {- if (remote === false || pages.length === 0) return new Set();+export async function remoteReadablePages(engine: BrainEngine, pages: readonly { source_id: string; slug: string }[]): Promise<Set<string>> {+ if (pages.length === 0) return new Set();const policy = await remoteReadPolicySql(engine, 'p');const rows = await engine.executeRaw<{ source_id: string; slug: string }>(`SELECT p.source_id, p.slug FROM pages pJOIN unnest($1::text[], $2::text[]) AS t(source_id, slug) ON p.source_id = t.source_id AND p.slug = t.slug- WHERE NOT ${policy}`, [pages.map(p => p.source_id), pages.map(p => p.slug)]);+ WHERE p.deleted_at IS NULL AND ${policy}`, [pages.map(p => p.source_id), pages.map(p => p.slug)]);return new Set(rows.map(r => `${r.source_id}\u0000${r.slug}`));}diff --git a/test/access-hidden-page-writes.test.ts b/test/access-hidden-page-writes.test.tsindex 3eed7c20fc..5bbd7550d2 100644--- a/test/access-hidden-page-writes.test.ts+++ b/test/access-hidden-page-writes.test.ts@@ -141,7 +141,7 @@ for (const kind of testBackends()) {expect(await state(MISSING)).toEqual({ raw: null, out_links: 0, in_links: 0, facts: 0, timeline: 0 });}, 240_000);- test('extract_entities treats a source page its caller cannot read as a missing one, and writes onto no entity page it cannot read', async () => {+ test('extract_entities treats a source page its caller cannot read as a missing one, and skips an entity page it cannot read', async () => {const text = 'Carol Example met with Dana Example.';const shape = (out: Call) => ({ isError: out.isError, entities: (out.json.entities as Array<Record<string, unknown>> | undefined)?.map(e => ({ slug: e.slug, action: e.action, backlinkCreated: e.backlinkCreated, timelineAdded: e.timelineAdded })) });for (const [who, slug] of [['bob', RESTRICTED], ['writer', PRIVATE]] as const) {@@ -154,8 +154,11 @@ for (const kind of testBackends()) {}const restricted = await state(RESTRICTED);for (const who of ['bob', 'writer']) {- const out = await call(who, 'extract_entities', { text: 'Vesper Example joined the planning call.', source_slug: OPEN });- expect({ who, isError: out.isError, code: out.json.code }).toEqual({ who, isError: true, code: 'page_not_found' });+ const out = await call(who, 'extract_entities', { text: 'Vesper Example joined Erin Example on the planning call.', source_slug: OPEN });+ const entities = out.json.entities as Array<{ slug: string; action: string; skipped_reason?: string; backlinkCreated: boolean }>;+ expect({ who, isError: out.isError, hidden: entities.find(e => e.slug === RESTRICTED) })+ .toEqual({ who, isError: false, hidden: expect.objectContaining({ action: 'skipped', skipped_reason: 'page_not_found', backlinkCreated: false }) });+ expect(entities.some(e => e.slug !== RESTRICTED && e.action !== 'skipped')).toBe(true);}expect(await state(RESTRICTED)).toEqual(restricted);const alice = await call('alice', 'extract_entities', { text: 'Vesper Example joined the planning call.', source_slug: OPEN });@@ -171,10 +174,16 @@ for (const kind of testBackends()) {test('get_ingest_log names no page a remote reader cannot read and no host path; local callers see the row whole', async () => {await engine.logIngest({ source_id: SRC, source_type: 'git_sync', source_ref: '/srv/brains/acme-example @ abcdef12', pages_updated: [OPEN, RESTRICTED, PRIVATE, MISSING], summary: 'Sync: +4 ~0 -0' });await engine.logIngest({ source_id: SRC, source_type: 'fixture', source_ref: 'ref-hidden-only', pages_updated: [RESTRICTED, PRIVATE], summary: 'Ingested canaryhphidden' });+ await engine.logIngest({ source_id: SRC, source_type: 'git_sync', source_ref: '/home/alice-example/Mobile Documents/brain @ 0123abcd', pages_updated: [OPEN], summary: 'Sync: +1 ~0 -0' });+ await engine.logIngest({ source_id: SRC, source_type: 'directory', source_ref: './brain-example', pages_updated: [OPEN], summary: 'Import' });const remote = await call('reader', 'get_ingest_log', { limit: 5 });expect(remote.isError).toBe(false);const row = (JSON.parse(remote.text.split('\n_META_HOT')[0]) as Array<{ source_ref: string; pages_updated: string[] }>).find(r => r.source_ref.endsWith('@ abcdef12'))!;- expect({ ref: row.source_ref, pages: row.pages_updated }).toEqual({ ref: '<local path> @ abcdef12', pages: [OPEN, MISSING] });+ expect({ ref: row.source_ref, pages: row.pages_updated }).toEqual({ ref: '<local path> @ abcdef12', pages: [OPEN] });+ const refs = (JSON.parse(remote.text.split('\n_META_HOT')[0]) as Array<{ source_ref: string }>).map(r => r.source_ref);+ expect(refs).toEqual(expect.arrayContaining(['<local path> @ 0123abcd', '<local path>']));+ expect(remote.text).not.toContain('Mobile Documents');+ expect(remote.text).not.toContain('brain-example');expect(remote.text).not.toContain('canaryhphidden');expect((await call('alice', 'get_ingest_log', { limit: 5 })).text).toContain(RESTRICTED);const whole = await local('get_ingest_log', { limit: 5 });