Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
belief: remote history hides world events of a fact later made private; read only the current incarnation (review #19)
garrytanb9e50970f686
diff --git a/src/core/belief/history.ts b/src/core/belief/history.tsindex 098dd763d7..104782dba2 100644--- a/src/core/belief/history.ts+++ b/src/core/belief/history.ts@@ -3,13 +3,17 @@* section 8, privacy). Every read of events goes through `readBeliefHistory`;* remote callers never touch the table. A remote caller sees an event only when* all hold, and the read fails closed when an owner cannot be resolved:- * - the event's own visibility was `world` when recorded;+ * - the event's own visibility was `world` when recorded, and no later+ * event of the same claim (any key, so a fact moved to `private`) is+ * private: making a claim private hides its earlier world history;* - the stream page's current lifetime is visible now (a page made private* later hides its earlier events; an ended lifetime hides them too);* - for links, every page-backed endpoint and the origin are visible now.* For every caller, a key with a `derived.hidden` (a purge hid the row it* describes, section 6.5) hides its events below that marker and its backfill- * events at any id; the marker itself stays visible as the disclosure.+ * events at any id; the marker itself stays visible as the disclosure. Only+ * the source's current incarnation is read: a removed and re-added source+ * inherits no history.*/import type { BrainEngine } from '../engine.ts';import { privatePagesFilterFragment } from '../search/private-visibility.ts';@@ -31,26 +35,28 @@ function linkLifetimes(subjectKey: string): string[] {}export async function readBeliefHistory(engine: BrainEngine, q: BeliefHistoryQuery): Promise<BeliefHistoryRow[]> {- const rows = await engine.executeRaw<BeliefHistoryRow & { stream_visible: boolean | null }>(`SELECT e.id, e.slug, e.page_lifetime::text AS page_lifetime,+ const rows = await engine.executeRaw<BeliefHistoryRow & { stream_visible: boolean | null; later_private: boolean }>(`SELECT e.id, e.slug, e.page_lifetime::text AS page_lifetime,e.subject_kind, e.subject_key, e.event_type, e.origin, e.trust_tier, e.writer_tier, e.tier_basis, e.visibility, e.recorded_at, e.source_time,e.value, e.value_hash, e.prior_hash, e.state_hash, e.cause, e.purged, e.redacted_fields,(SELECT ${privatePagesFilterFragment('p')} FROM belief_page_lifetimes l JOIN pages p ON p.source_id = l.source_id AND p.slug = l.slugAND p.deleted_at IS NULL AND (l.page_id IS NULL OR p.id = l.page_id)- WHERE l.page_lifetime = e.page_lifetime AND l.ended_at IS NULL) AS stream_visible+ WHERE l.page_lifetime = e.page_lifetime AND l.ended_at IS NULL) AS stream_visible,+ (e.claim_fp IS NOT NULL AND EXISTS (SELECT 1 FROM belief_events v WHERE v.source_id = e.source_id AND v.source_incarnation = e.source_incarnation+ AND v.claim_fp = e.claim_fp AND v.subject_kind = e.subject_kind AND v.visibility = 'private' AND v.id > e.id)) AS later_privateFROM belief_events e- WHERE e.source_id = $1 AND ($2::text IS NULL OR e.slug = $2) AND ($3::text IS NULL OR e.subject_key = $3) AND e.id > $4+ WHERE e.source_id = $1 AND e.source_incarnation = (SELECT s.incarnation FROM sources s WHERE s.id = $1) AND ($2::text IS NULL OR e.slug = $2) AND ($3::text IS NULL OR e.subject_key = $3) AND e.id > $4AND NOT EXISTS (SELECT 1 FROM belief_events h WHERE h.source_id = e.source_id AND h.subject_kind = e.subject_kind AND h.subject_key = e.subject_keyAND h.event_type = 'derived.hidden' AND h.id <> e.id AND (h.id > e.id OR e.origin = 'backfill'))ORDER BY e.id LIMIT $5`, [q.sourceId, q.slug ?? null, q.subjectKey ?? null, q.afterId ?? 0, Math.min(Math.max(q.limit ?? 1000, 1), 10_000)]);const json = (v: unknown) => typeof v === 'string' ? JSON.parse(v) : v;const out = rows.map(r => ({ ...r, id: Number(r.id), value: json(r.value) as BeliefHistoryRow['value'], cause: json(r.cause) as BeliefHistoryRow['cause'] }));- if (!q.remote) return out.map(({ stream_visible: _, ...row }) => row);+ if (!q.remote) return out.map(({ stream_visible: _, later_private: __, ...row }) => row);const linkIds = [...new Set(out.filter(r => r.subject_kind === 'link' || r.subject_kind === 'link_transition').flatMap(r => linkLifetimes(r.subject_key)))];const visibleLifetimes = new Set(linkIds.length ? (await engine.executeRaw<{ page_lifetime: string }>(`SELECT l.page_lifetime::text AS page_lifetimeFROM belief_page_lifetimes l JOIN pages p ON p.source_id = l.source_id AND p.slug = l.slug AND p.deleted_at IS NULL AND (l.page_id IS NULL OR p.id = l.page_id)WHERE l.page_lifetime = ANY($1::uuid[]) AND l.ended_at IS NULL AND ${privatePagesFilterFragment('p')}`, [linkIds])).map(r => r.page_lifetime) : []);- return out.filter(r => r.visibility === 'world'+ return out.filter(r => r.visibility === 'world' && r.later_private !== true&& (r.page_lifetime == null ? r.subject_kind === 'fact' : r.stream_visible === true)&& (r.subject_kind !== 'link' && r.subject_kind !== 'link_transition' || linkLifetimes(r.subject_key).every(id => visibleLifetimes.has(id))))- .map(({ stream_visible: _, ...row }) => row);+ .map(({ stream_visible: _, later_private: __, ...row }) => row);}diff --git a/test/belief-ledger-privacy.test.ts b/test/belief-ledger-privacy.test.tsindex c5547d7316..736972cf2d 100644--- a/test/belief-ledger-privacy.test.ts+++ b/test/belief-ledger-privacy.test.ts@@ -60,6 +60,24 @@ for (const engineCase of testBackends().map(beliefEngineCase)) {}, { databaseUrl: engineCase.databaseUrl });}, 120_000);+ test('probe 14: a fact made private hides its earlier world history; a re-added source reads none of the old history', async () => {+ await beliefBrain(async b => {+ await b.put('companies/acme-example', beliefPage({ facts: ['Acme sells widgets'] }));+ await b.put('companies/acme-example', beliefPage({ facts: ['Acme sells widgets', 'Acme payroll is late'] }));+ expect((await remote(b, 'companies/acme-example')).some(e => JSON.stringify(e.value ?? {}).includes('payroll'))).toBe(true);+ await b.put('companies/acme-example', beliefPage({ facts: ['Acme sells widgets', { claim: 'Acme payroll is late', visibility: 'private' }] }));+ const leaked = (await remote(b, 'companies/acme-example')).filter(e => JSON.stringify(e.value ?? {}).includes('payroll'));+ expect(leaked).toEqual([]);+ expect((await remote(b, 'companies/acme-example')).length).toBeGreaterThan(0);+ // Forced probe: the recorded-visibility rule alone still shows the world assertion of the claim made private.+ expect((await local(b, 'companies/acme-example')).some(e => e.visibility === 'world' && JSON.stringify(e.value ?? {}).includes('payroll'))).toBe(true);+ // A re-added source is a new incarnation of the same id (probe 17).+ await b.engine.executeRaw('DELETE FROM page_write_guards WHERE source_incarnation = (SELECT incarnation FROM sources WHERE id = $1)', [b.sourceId]);+ await b.engine.executeRaw('UPDATE sources SET incarnation = gen_random_uuid() WHERE id = $1', [b.sourceId]);+ expect(await local(b, 'companies/acme-example')).toEqual([]);+ }, { databaseUrl: engineCase.databaseUrl });+ }, 120_000);+test('probe 14: a link with a private endpoint is hidden until the endpoint is public', async () => {await beliefBrain(async b => {await b.put('people/alice-example', beliefPage({ type: 'person', title: 'Alice Example', frontmatter: priv }));