Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
W4: remote belief history follows the takes holder rule (N6 found held takes leaking through delta beliefs); forced probes on both paths
garrytan1d7ce5b3d8d7
diff --git a/src/core/belief/changes.ts b/src/core/belief/changes.tsindex 32440e1400..c6ceb974fd 100644--- a/src/core/belief/changes.ts+++ b/src/core/belief/changes.ts@@ -55,7 +55,11 @@ export interface BeliefChanges {/** The source has no ledger coverage at all. */not_recorded?: true;}-export interface BeliefChangesQuery { sourceId: string; since: string; until: string; remote: boolean; entities?: readonly string[]; limit?: number }+export interface BeliefChangesQuery {+ sourceId: string; since: string; until: string; remote: boolean; entities?: readonly string[]; limit?: number;+ /** Remote callers: the take holders they may read (`readHolders`); `['world']` when absent. */+ takesHolders?: readonly string[];+}const SUBJECT_EVENTS = new Set(['fact.asserted', 'fact.revised', 'fact.retracted', 'fact.withdrawn', 'fact.superseded', 'fact.transferred', 'fact.purged','take.asserted', 'take.revised', 'take.resolved', 'take.unresolved', 'take.retracted', 'take.transferred', 'take.purged',@@ -113,7 +117,7 @@ export async function beliefChanges(engine: BrainEngine, q: BeliefChangesQuery):if (!coverage.ok && coverage.code === 'coverage_from' && coverage.from == null) return { ...out, not_recorded: true };const startCov = await coverageAt(engine, q.sourceId, q.since);if (!startCov.ok && startCov.code === 'coverage_from') out.coverage_from = startCov.from;- const read = await readAll(engine, { sourceId: q.sourceId, remote: q.remote, recordedAfter: out.since, recordedUntil: out.until });+ const read = await readAll(engine, { sourceId: q.sourceId, remote: q.remote, takesHolders: q.takesHolders, recordedAfter: out.since, recordedUntil: out.until });out.truncated = read.truncated;const events = read.rows.filter(e => e.origin !== 'backfill' && e.origin !== 'baseline');@@ -144,7 +148,7 @@ export async function beliefChanges(engine: BrainEngine, q: BeliefChangesQuery):const keys = [...new Set([...byKey.values()].map(rows => rows[0]!.subject_key))];const prior = new Map<string, BeliefHistoryRow>();if (keys.length) {- for (const e of (await readAll(engine, { sourceId: q.sourceId, remote: q.remote, subjectKeys: keys, recordedUntil: out.since })).rows) {+ for (const e of (await readAll(engine, { sourceId: q.sourceId, remote: q.remote, takesHolders: q.takesHolders, subjectKeys: keys, recordedUntil: out.since })).rows) {if (e.origin === 'backfill' || !SUBJECT_EVENTS.has(e.event_type) && e.event_type !== 'fact.adopted' && e.event_type !== 'link.adopted') continue;prior.set(`${e.subject_kind}\u0000${e.subject_key}`, e);}@@ -199,12 +203,14 @@ export async function beliefChanges(engine: BrainEngine, q: BeliefChangesQuery):return out;}-/** Fence subjects of a page body, keyed `kind\u0000subject_key`; a remote caller gets them only where it may read the interval, and only world facts. */+/** Fence subjects of a page body, keyed `kind\u0000subject_key`; a remote caller gets them only where it may read the interval, and only world facts and takes of holders it may read. */async function subjectsOf(engine: BrainEngine, q: BeliefChangesQuery, at: Awaited<ReturnType<typeof readPageAtTime>>, interval: NonNullable<Awaited<ReturnType<typeof readPageAtTime>>['current']>) {if (!interval.page || interval.body === 'unavailable' || interval.body === 'segmented' || interval.page.compiled_truth == null) return null;if (q.remote && !await remoteMayRead(engine, at.page_id, interval)) return null;const { subjects } = await bodySubjects(engine, interval.lifetime, `${interval.page.compiled_truth}\n${interval.page.timeline ?? ''}`, interval.event.visibility);- return new Map(subjects.filter(s => !q.remote || s.visibility === 'world').map(s => [`${s.kind}\u0000${s.key}`, s.value]));+ const holders = new Set(q.takesHolders ?? ['world']);+ return new Map(subjects.filter(s => !q.remote || s.visibility === 'world' && (s.kind !== 'take' || holders.has(String(s.value.holder))))+ .map(s => [`${s.kind}\u0000${s.key}`, s.value]));}/** The page's fence subjects as recorded at `at`, or null when no body answers (coverage, proof, pruned or purged bodies). */diff --git a/src/core/belief/history.ts b/src/core/belief/history.tsindex 3523a35e56..1f378cc704 100644--- a/src/core/belief/history.ts+++ b/src/core/belief/history.ts@@ -8,7 +8,9 @@* 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 links, every page-backed endpoint and the origin are visible now;+ * - for takes, the take's holder is one the caller may read (`takesHolders`, default `['world']`, the takes+ * surfaces' rule); a take event with no holder (purged or redacted) is hidden.* 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. Only@@ -31,6 +33,8 @@ export interface BeliefHistoryQuery {recordedAfter?: string; recordedUntil?: string;/** Only these subject keys, below this event id (the history of keys before a window). */subjectKeys?: readonly string[]; beforeId?: number;+ /** Remote callers: the take holders they may read (the takes surfaces' allow-list); `['world']` when absent. */+ takesHolders?: readonly string[];}/** Lifetimes a link key names (`L:<uuid>` components), for the endpoint visibility rule. */@@ -64,7 +68,9 @@ export async function readBeliefHistory(engine: BrainEngine, q: BeliefHistoryQueconst 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) : []);+ const holders = new Set(q.takesHolders ?? ['world']);return out.filter(r => r.visibility === 'world' && r.later_private !== true+ && (r.subject_kind !== 'take' || typeof r.value?.holder === 'string' && holders.has(r.value.holder))&& (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: _, later_private: __, ...row }) => row);diff --git a/src/core/ops/facts.ts b/src/core/ops/facts.tsindex 21c888e4e0..a505da7572 100644--- a/src/core/ops/facts.ts+++ b/src/core/ops/facts.ts@@ -1219,7 +1219,7 @@ const delta: Operation = {// structured arrays reflect — the assembler's render predates both.let text = renderDelta(pages, facts, threads, effectiveSince);const diff = p.beliefs === true ? await (await import('../belief/changes.ts')).deltaBeliefs(ctx.engine, { sourceId, since: effectiveSince, until: horizon,- remote: !includePrivate, entities: parseEntityList(p.entities) }, budgetTokens !== null ? beliefBudget : null, text, TURN_CONTEXT_ENVELOPE) : null;+ remote: !includePrivate, takesHolders: readHolders(ctx), entities: parseEntityList(p.entities) }, budgetTokens !== null ? beliefBudget : null, text, TURN_CONTEXT_ENVELOPE) : null;if (diff) text = diff.text;const budgetUsed = budgetTokens !== null ? estimateTokens(text) : undefined;diff --git a/test/belief-ledger-weekly-diff.test.ts b/test/belief-ledger-weekly-diff.test.tsindex 1ebabe0b40..6b80a2bc4e 100644--- a/test/belief-ledger-weekly-diff.test.ts+++ b/test/belief-ledger-weekly-diff.test.ts@@ -103,6 +103,20 @@ for (const engineCase of testBackends().map(beliefEngineCase)) {expect(sorted(brief(remote.changes))).toEqual(sorted([['fact', 'removed', 'Acme sells widgets'], ['fact', 'added', 'Acme sells gadgets']]));expect(JSON.stringify(remote)).not.toContain('Fund A');expect(JSON.stringify(remote)).not.toContain('Secret');+ // Takes follow the takes surfaces' holder rule: a remote caller reads world-held takes only, both a revised take and the takes of+ // a page created in the window (N6 found the held take leaking through both paths before the rule was applied).+ const t2 = await at(b);+ await b.put(slug, beliefPage({ facts: ['Acme sells gadgets', { claim: 'Acme signed with Fund A', visibility: 'private' }],+ takes: [{ claim: 'Acme is the public favorite', who: 'world', weight: '0.6' }, { claim: 'Acme is privately doubted', who: 'garry-example', weight: '0.3' }] }));+ await b.put('companies/newco-example', beliefPage({ title: 'Newco', takes: [{ claim: 'Newco looks public', who: 'world' }, { claim: 'Newco is privately loved', who: 'garry-example' }] }));+ const t3 = await at(b);+ const takesLocal = (await changes(b, t2, t3)).changes.filter(c => c.kind === 'take').map(c => c.subject.claim);+ expect(takesLocal.sort()).toEqual(['Acme is privately doubted', 'Acme is the public favorite', 'Newco is privately loved', 'Newco looks public']);+ const takesRemote = await changes(b, t2, t3, { remote: true });+ expect(takesRemote.changes.filter(c => c.kind === 'take').map(c => c.subject.claim).sort()).toEqual(['Acme is the public favorite', 'Newco looks public']);+ expect(JSON.stringify(takesRemote)).not.toContain('privately');+ const granted = await beliefChanges(b.engine, { sourceId: b.sourceId, since: t2, until: t3, remote: true, takesHolders: ['world', 'garry-example'] });+ expect(granted.changes.filter(c => c.kind === 'take')).toHaveLength(4);const before = await b.engine.executeRaw<{ t: string }>(`SELECT min(recorded_at) AS t FROM belief_events WHERE source_id = $1`, [b.sourceId]);const old = new Date(Date.parse(before[0]!.t) - 86_400_000).toISOString();expect((await changes(b, old, early)).coverage_from).toEqual(expect.any(String));diff --git a/test/helpers/belief-ledger.ts b/test/helpers/belief-ledger.tsindex 7d9faf0fba..41e8ab1588 100644--- a/test/helpers/belief-ledger.ts+++ b/test/helpers/belief-ledger.ts@@ -144,7 +144,7 @@ export async function withFileBeliefBrain(sourceId: string, run: (brain: FileBel/** A page body with a facts fence, a takes fence, prose and wikilinks. */export function beliefPage(o: { title?: string; type?: string; prose?: string; facts?: Array<string | { claim: string; context?: string; confidence?: string; visibility?: string }>;- takes?: Array<string | { claim: string; weight?: string; since?: string }>; links?: string[]; frontmatter?: Record<string, string> } = {}): string {+ takes?: Array<string | { claim: string; weight?: string; since?: string; who?: string }>; links?: string[]; frontmatter?: Record<string, string> } = {}): string {const fm = { type: o.type ?? 'company', title: o.title ?? 'Acme Example', ...o.frontmatter };const facts = (o.facts ?? []).map(f => typeof f === 'string' ? { claim: f } : f);const takes = (o.takes ?? []).map(t => typeof t === 'string' ? { claim: t } : t);@@ -155,7 +155,7 @@ export function beliefPage(o: { title?: string; type?: string; prose?: string; f...facts.map((f, i) => `| ${i + 1} | ${f.claim} | fact | ${f.confidence ?? '1.0'} | ${f.visibility ?? 'world'} | medium | 2026-01-01 | | remember | ${f.context ?? ''} |`),'<!--- gbrain:facts:end -->'].join('\n'));if (takes.length) parts.push(['<!--- gbrain:takes:begin -->', '| # | claim | kind | who | weight | since | source |', '|---|-------|------|-----|--------|-------|--------|',- ...takes.map((t, i) => `| ${i + 1} | ${t.claim} | take | brain | ${t.weight ?? '0.5'} | ${t.since ?? '2026-07'} | notes |`), '<!--- gbrain:takes:end -->'].join('\n'));+ ...takes.map((t, i) => `| ${i + 1} | ${t.claim} | take | ${t.who ?? 'brain'} | ${t.weight ?? '0.5'} | ${t.since ?? '2026-07'} | notes |`), '<!--- gbrain:takes:end -->'].join('\n'));return `${parts.join('\n\n')}\n`;}