Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(trust): decide a supersede_fact proposal on a segmented entity page (#8060)
garrytan408f5b7248c6
diff --git a/docs/architecture/key-files/core-decide.md b/docs/architecture/key-files/core-decide.mdindex e8d0457a18..3fb03f3e7b 100644--- a/docs/architecture/key-files/core-decide.md+++ b/docs/architecture/key-files/core-decide.md@@ -74,7 +74,7 @@ The capability contract (API, providers, egress, extension points) is `docs/arch- `src/core/ai/decide/datasets-conflict.ts` — `registerConflictDatasets()` (called by `src/commands/decide/writepath.ts`): the conflict dataset adapter (production request shape, no harmful-action reducer) and the `facts-fixtures` builder (labelled fact-pair JSONL; label `true` exactly for `duplicate`, the calibrated threshold). Calibration thresholds the reducer-consistent `duplicateValue` (P(chosen) only when duplicate is chosen) and `calibrateProposalFloor` picks the F1-best P(supersede) floor on the choice label kept in `slice` (≥ 10 supersede labels), stored in the row's notes. Tests: `test/decide/conflict-calibration.test.ts`.-- `src/core/facts/proposal-supersede.ts` — S9 accept/undo as a CHECKED supersede: old fact `expired_at`/`valid_until`/`superseded_by`, the struck `## Facts` row (file and DB body) and the proposal status apply as one unit with before/after state; a failed fence write leaves the proposal pending. Unmanaged brains: source-filesystem + page lock, one transaction that publishes the file last and restores it on a failed commit. Managed brains: coordinator mutation `decide_proposal` (dispatched by `persistence/service.ts`). Stale pairs are marked `stale`; undo refuses when either fact or the fence row changed. Tests: `test/decide/conflict-proposals.serial.test.ts`, `conflict-proposals-managed.test.ts`.+- `src/core/facts/proposal-supersede.ts` — S9 accept/undo as a CHECKED supersede: old fact `expired_at`/`valid_until`/`superseded_by`, the struck `## Facts` row (file and DB body) and the proposal status apply as one unit with before/after state; a failed fence write leaves the proposal pending. Unmanaged brains: source-filesystem + page lock, one transaction that publishes the file last and restores it on a failed commit. Managed brains: coordinator mutation `decide_proposal` (dispatched by `persistence/service.ts`). Stale pairs are marked `stale`; undo refuses when either fact or the fence row changed. Tests: `test/decide/conflict-proposals.serial.test.ts`, `conflict-proposals-managed.test.ts`. #8060: the managed `decide_proposal` preparer reads the fact page through `wholeBodySnapshot`, so a segmented page within PAGE_MAX_BYTES is accepted or undone like an inline one (`test/trust-supersede-segmented-8060.test.ts`).- `src/commands/decide/proposals.ts` (registered in `src/commands/decide/writepath.ts`) — `gbrain decide sweep --slot conflict [--since] [--source] [--json]` and `gbrain decide proposals list|accept|reject|undo` (`--all-from <sweep id>` for accept/reject, `--json` everywhere); list shows both facts' text locally.diff --git a/src/core/facts/proposal-supersede.ts b/src/core/facts/proposal-supersede.tsindex a088456066..7b49603349 100644--- a/src/core/facts/proposal-supersede.ts+++ b/src/core/facts/proposal-supersede.ts@@ -31,6 +31,7 @@ import { readFix } from '../ops/op-fix.ts';import { getProposal, transitionProposal, type ProposalRow } from '../ai/decide/proposals-store.ts';import { strikeFenceRow, supersededFact } from './forget.ts';import { maintenanceTransaction } from '../persistence/attribution.ts';+import { wholeBodySnapshot } from '../persistence/segmented-apply.ts';import { withTrustKeep } from '../persistence/context.ts';import type { PreparedMutation } from '../persistence/coordinator.ts';import type { WriteRequest } from '../persistence/model.ts';@@ -368,7 +369,8 @@ export async function prepareProposalMutation(engine: BrainEngine, row: WriteReq}const action = p.action;const proposal = await store.get(engine, id);- const snapshot = await engine.readPageSnapshot(row.slug, { sourceId: row.source_id });+ // #8060: a segmented page within PAGE_MAX_BYTES is read back whole and its fence written as a normal put (facts-prepare.ts).+ const snapshot = await wholeBodySnapshot(engine, await engine.readPageSnapshot(row.slug, { sourceId: row.source_id }), 'decide_proposal');const observedRevision = snapshot?.revision ?? null;const done = (outcome: Record<string, unknown>): PreparedMutation => ({ observedRevision, noop: true, apply: async () => outcome });if (!proposal || proposal.source_id !== row.source_id) return done({ id, action, status: 'refused', reason: 'not_found' });diff --git a/test/trust-supersede-segmented-8060.test.ts b/test/trust-supersede-segmented-8060.test.tsnew file mode 100644index 0000000000..946a16ecc1--- /dev/null+++ b/test/trust-supersede-segmented-8060.test.ts@@ -0,0 +1,110 @@+/**+ * #8060: the owner's accept of a supersede_fact trust proposal on an entity page stored in segments.+ *+ * 1. Protects: on a managed brain, a lower-tier remember.replaces of a confirmed fact on a segmented page files a+ * supersede_fact proposal; the owner's accept (decide_proposal) supersedes the old fact, confirms the new one and+ * strikes the old row in the page's facts fence, reading the segmented body back whole as every other managed+ * fence write does; undo restores it.+ * 2. Fails when: the decide_proposal preparer reads the page body as one string (rowToPage.compiled_truth) again:+ * the accept fails page_body_segmented and the proposal stays pending.+ * 3. Why existing coverage misses it: trust-guarded-supersession.test.ts and conflict-proposals-managed.test.ts+ * use small inline pages.+ * Runs on PGLite, and on Postgres when DATABASE_URL is set.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import type { BrainEngine } from '../src/core/engine.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { operationsByName } from '../src/core/operations.ts';+import { mintLegacyToken } from '../src/core/token-mint.ts';+import { registerLocalWriter, readLocalWriter } from '../src/core/persistence/identity.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import type { Principal } from '../src/core/persistence/model.ts';+import { withCoordinatedWrite, withTrustPromotion } from '../src/core/persistence/context.ts';+import { decideTrustProposal } from '../src/core/trust/decide.ts';+import { parseTrustProposalRef } from '../src/core/trust/proposals.ts';+import { materializedPage } from '../src/core/persistence/segmented-apply.ts';+import { parseFactsFence } from '../src/core/facts-fence.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { withEnv } from './helpers/with-env.ts';+import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';++const engines: BrainEngine[] = [];+const home = mkdtempSync(join(tmpdir(), 'gbrain-trust-segmented-'));+let closePostgres: (() => Promise<void>) | undefined;+const quiet = { info() {}, warn() {}, error() {} };+const SLUG = 'people/alice-example';++beforeAll(async () => withEnv({ GBRAIN_HOME: home }, async () => {+ const lite = new PGLiteEngine(); await lite.connect({}); await lite.initSchema(); engines.push(lite);+ if (process.env.DATABASE_URL) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL);+ engines.push(pg.engine); closePostgres = pg.close;+ }+}), 120_000);+afterAll(async () => {+ for (const engine of engines) { await disposePersistenceConsumer(engine); await engine.disconnect(); }+ await closePostgres?.();+ rmSync(home, { recursive: true, force: true });+});++async function brain(engine: BrainEngine) {+ const sourceId = 'trust-' + randomUUID().slice(0, 8);+ await disposePersistenceConsumer(engine);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [sourceId]);+ await registerLocalWriter(engine, 'cli');+ const minted = await mintLegacyToken(engine, { name: 'token-' + sourceId, scopes: ['read', 'write'], sourceGrant: [sourceId], takesHolders: ['world'] });+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ const base = { engine, config: { engine: engine.kind, embedding_disabled: true }, sourceId, dryRun: false, logger: quiet };+ await readLocalWriter(engine, 'cli');+ return {+ engine, sourceId,+ remote: { ...base, remote: true, transport: 'http', takesHoldersAllowList: ['world'],+ auth: { token: '', clientId: minted.id, principal: { kind: 'legacy_token', id: minted.id } as Principal, sourceId, allowedSources: [sourceId], scopes: ['read', 'write'] } } as OperationContext,+ local: { ...base, remote: false } as OperationContext,+ };+}+const run = (ctx: OperationContext, op: string, params: Record<string, unknown>) =>+ operationsByName[op].handler(ctx, { request_id: randomUUID(), ...params }) as Promise<Record<string, any>>;+const factRow = async (engine: BrainEngine, id: unknown) => (await engine.executeRaw<{ trust_tier: string; expired_at: unknown; superseded_by: number | null }>(+ 'SELECT trust_tier, expired_at, superseded_by::int AS superseded_by FROM facts WHERE id=$1', [Number(id)]))[0]!;+const owner = { confirmation: { via: 'tty' as const } };++/** About 1.3 MB of prose: past the 1 MiB promotion bound, so the page is stored in segments. */+const filler = Array.from({ length: 26_000 }, (_, i) => 'Alice Example kept notes on item ' + i + ' of the archive.').join('\n');++describe('#8060 supersede_fact accept on a segmented entity page', () => {+ test('the owner accept supersedes, confirms and strikes the fence row; undo restores', async () => withEnv({ GBRAIN_HOME: home }, async () => {+ for (const engine of engines) {+ const b = await brain(engine);+ await run(b.local, 'put_page', { slug: SLUG, content: '---\ntype: person\ntitle: Alice Example\n---\n# Alice Example\n\n' + filler + '\n' });+ const [stored] = await engine.executeRaw<{ body_storage: string }>('SELECT body_storage FROM pages WHERE source_id=$1 AND slug=$2', [b.sourceId, SLUG]);+ expect(stored!.body_storage).toBe('segmented');+ const old = await run(b.local, 'remember', { fact: 'Alice lives in Paris', provenance: 'owner', entity: SLUG });+ await engine.transaction(tx => withCoordinatedWrite(tx, [b.sourceId], () => withTrustPromotion(tx, 'user_confirmed', () =>+ tx.executeRaw("UPDATE facts SET trust_tier='user_confirmed' WHERE id=$1", [Number(old.id)])), TEST_WRITE_ATTRIBUTION));+ const agent = await run(b.remote, 'remember', { fact: 'Alice lives in Berlin', provenance: 'web page', entity: SLUG, replaces: String(old.id) });+ expect(agent.contested?.proposal_ref).toMatch(/^tp\d+$/);+ const id = parseTrustProposalRef(agent.contested.proposal_ref)!;++ const accepted = await decideTrustProposal(engine, id, 'accept', owner);+ expect(accepted.status).toBe('accepted');+ expect(await factRow(engine, old.id)).toMatchObject({ superseded_by: Number(agent.id) });+ expect((await factRow(engine, old.id)).expired_at).not.toBeNull();+ expect((await factRow(engine, agent.id)).trust_tier).toBe('user_confirmed');+ const snapshot = (await engine.readPageSnapshot(SLUG, { sourceId: b.sourceId }))!;+ const fence = parseFactsFence((await materializedPage(engine, snapshot.page)).compiled_truth);+ expect(fence.facts.find(f => f.claim === 'Alice lives in Paris')?.active).toBe(false);++ const undone = await decideTrustProposal(engine, id, 'undo', owner);+ expect(undone.status).toBe('undone');+ expect((await factRow(engine, old.id)).expired_at).toBeNull();+ expect((await factRow(engine, agent.id)).trust_tier).toBe('agent_written');+ }+ }), 300_000);+});