Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

belief ledger lane A: journaled direct edits from a local caller are owner edits (put_page, edit_page, takes_update, takes_resolve without an internal intent kind) (#1)

garrytan6fe91ebb6b18
diff --git a/src/core/belief/ledger.ts b/src/core/belief/ledger.ts
index a8e617577c..6dd5f29029 100644
--- a/src/core/belief/ledger.ts
+++ b/src/core/belief/ledger.ts
@@ -472,11 +472,25 @@ export async function currentStateHash(tx: BrainEngine, sourceId: string, incarn
export { stateHashOf };
+/**
+ * The journaled operations that are a direct edit of a page's facts or takes (design 6.5: a trusted local write that
+ * sets the field). Only these, from a local caller and with no internal intent `kind` (the `put_page` rows of
+ * import, file repair, quarantine clear, reconcile and managed maintenance carry one), can release a purge
+ * redaction; sync (`submit_job`), gate release, held accept, backfill, revert, restore and retention never do.
+ * append.ts still requires the writer tier at or above OWNER_TIER_FLOOR, and the trust channel records these verbs
+ * as agent_written even from the local CLI, so today `trust confirm` is the release that reaches it.
+ */
+const OWNER_EDIT_OPERATIONS: ReadonlySet<string> = new Set(['put_page', 'edit_page', 'takes_update', 'takes_resolve']);
+let allowlistDropped = false;
+/** Tests: the forced probe that drops the allowlist, so every local publication counts as an owner edit. */
+export function __dropOwnerEditAllowlistForTests(dropped: boolean): void { allowlistDropped = dropped; }
+
/** The batch options of one journaled page publication (single or group member). */
export function publicationBeliefOptions(row: { id: string; source_id: string; slug: string; operation: string; authority: { remote?: boolean }; intent?: Record<string, unknown> | null },
additionalKeys: readonly { sourceId: string; slug: string }[] = [], report?: { appended: number }): BeliefBatchOptions {
return { sourceId: row.source_id, slugs: [...new Set([row.slug, ...additionalKeys.filter(k => k.sourceId === row.source_id).map(k => k.slug)])],
publicationId: row.id, op: row.operation, remote: row.authority.remote !== false,
+ ...(row.authority.remote === false && (allowlistDropped || OWNER_EDIT_OPERATIONS.has(row.operation) && row.intent?.kind == null) ? { ownerEdit: true } : {}),
...(row.operation === 'revert_version' ? { pageEventType: 'page.reverted' as const } : {}),
...(row.operation === 'delete_page' && row.intent?.retention === true ? { hardDeleteEvent: 'page.expired' as const } : {}), ...(report ? { report } : {}) };
}
diff --git a/test/belief-ledger-purge-review.test.ts b/test/belief-ledger-purge-review.test.ts
index 96e3edc344..ffe75d072e 100644
--- a/test/belief-ledger-purge-review.test.ts
+++ b/test/belief-ledger-purge-review.test.ts
@@ -5,11 +5,14 @@
*/
import { afterEach, describe, expect, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
-import { mkdirSync, readFileSync } from 'node:fs';
+import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { purgeMirrorPath } from '../src/core/belief/purge-mirror.ts';
import { CODES } from '../src/core/error-registry.ts';
import { __setBeliefEmitterFaultForTests } from '../src/core/belief/append.ts';
-import { withBeliefBatch } from '../src/core/belief/ledger.ts';
+import { __dropOwnerEditAllowlistForTests, publicationBeliefOptions, withBeliefBatch } from '../src/core/belief/ledger.ts';
+import { performSync } from '../src/commands/sync/perform.ts';
+import { execFileSync } from 'node:child_process';
+import { join } from 'node:path';
import { backfillBeliefHistory } from '../src/core/belief/backfill.ts';
import { coverageAt, coverageIntervals } from '../src/core/belief/coverage-report.ts';
import { recordDerivationInputs } from '../src/core/facts/derivation-inputs.ts';
@@ -23,7 +26,9 @@ import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';
import { testBackends } from './helpers/test-backends.ts';
import { beliefBrain, beliefEngineCase, beliefPage, type BeliefBrain, type BeliefEvent } from './helpers/belief-ledger.ts';
-afterEach(() => { __setBeliefEmitterFaultForTests(null); });
+afterEach(() => { __setBeliefEmitterFaultForTests(null); __dropOwnerEditAllowlistForTests(false); });
+
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
const X = 'Door code is 4417';
const purgeOp = operations.find(o => o.name === 'purge_fact')!;
@@ -83,6 +88,49 @@ for (const engineCase of testBackends().map(beliefEngineCase)) {
unchangedOwnerEdit: [m], syncSetsField: [m], ownerEdit: ['owner rewrote it'], released: 1 });
}, 240_000);
+ test('#1: only a local direct edit (put_page, edit_page, takes edits) is an owner edit; sync, import and the local agent-tier put_page never release', async () => {
+ const run = (forced: boolean) => collect<Record<string, unknown>>(report => brain(async b => {
+ const slug = 'companies/acme-example';
+ const page = (context: string) => beliefPage({ facts: [{ claim: 'Acme sells widgets', context }] });
+ const contexts = async (fn: () => Promise<unknown>) => { const mark = await b.lastId(); await fn(); return (await b.events(mark)).filter(e => e.event_type.startsWith('fact.')).map(e => e.value?.context); };
+ const released = async () => (await b.engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM belief_redactions WHERE source_id = $1 AND released_hash IS NOT NULL', [b.sourceId]))[0]!.n;
+ git(b.root, 'init', '-q');
+ mkdirSync(join(b.root, 'companies'), { recursive: true });
+ const sync = async (content: string) => {
+ writeFileSync(join(b.root, `${slug}.md`), content);
+ git(b.root, 'add', '-A'); git(b.root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', 'edit');
+ await performSync(b.engine, { sourceId: b.sourceId, noPull: true, noEmbed: true, noExtract: true, drain: true } as never);
+ };
+ await sync(beliefPage({ facts: [X, { claim: 'Acme sells widgets', context: `notes say ${X}` }] }));
+ const request = await requestOf(b, String((await purgeFact(b, X)).request_id));
+ __dropOwnerEditAllowlistForTests(forced);
+ const out: Record<string, unknown> = { marker: REDACTION_MARKER(request) };
+ out.sync = await contexts(() => sync(page('sync rewrote it')));
+ out.afterSync = await released();
+ if (forced) { report(out); return; }
+ out.agentTierPut = await contexts(() => b.put(slug, page('agent rewrote it')));
+ const fact = `source_id = $1 AND fact = 'Acme sells widgets' AND expired_at IS NULL`;
+ const publish = (operation: string, intent: Record<string, unknown>, context: string) => contexts(() => b.engine.transaction(tx => withCoordinatedWrite(tx, [b.sourceId], async () => {
+ await tx.lockPageKeys([{ sourceId: b.sourceId, slug }]);
+ await withBeliefBatch(tx, publicationBeliefOptions({ id: randomUUID(), source_id: b.sourceId, slug, operation, authority: { remote: false }, intent }),
+ () => tx.executeRaw(`UPDATE facts SET context = '${context}' WHERE ${fact}`, [b.sourceId]));
+ }, { ...TEST_WRITE_ATTRIBUTION, trust: { tier: 'operator_curated', origin: { channel: operation } } })));
+ out.import = await publish('put_page', { kind: 'managed_file_import' }, 'import rewrote it');
+ out.syncRow = await publish('submit_job', { kind: 'managed_sync_import' }, 'sync row rewrote it');
+ out.revert = await publish('revert_version', {}, 'revert rewrote it');
+ out.beforeOwner = await released();
+ out.ownerPut = await publish('put_page', {}, 'owner rewrote it');
+ out.afterOwner = await released();
+ report(out);
+ }));
+ const r = await run(false);
+ const m = r.marker;
+ expect(r).toEqual({ marker: m, sync: [m], afterSync: 0, agentTierPut: [m], import: [m], syncRow: [m], revert: [m], beforeOwner: 0, ownerPut: ['owner rewrote it'], afterOwner: 1 });
+ // Forced probe: without the allowlist the owner-tier managed sync releases the redaction and writes the new value.
+ const forced = await run(true);
+ expect(forced).toEqual({ marker: forced.marker, sync: ['sync rewrote it'], afterSync: 1 });
+ }, 240_000);
+
test('#2: the first page purge in a source writes no purged text into the ledger', async () => {
const r = await collect<Record<string, unknown>>(report => brain(async b => {
const claim = 'Vault code is 7731';