Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(trust): an index-existing release refuses fence_row_changed when the held row was edited after the hold (gstack /ship red team, GBRA-60 lane C)
garrytan4616bc575343
diff --git a/src/core/trust/release-publish.ts b/src/core/trust/release-publish.tsindex 94ff82d050..23e7fed182 100644--- a/src/core/trust/release-publish.ts+++ b/src/core/trust/release-publish.ts@@ -31,6 +31,9 @@ import { withTrustPromotion } from '../persistence/context.ts';import { digest } from '../persistence/digest.ts';import { managedPersistenceEnabled } from '../persistence/ownership.ts';import { prepareFenceFactAppend } from '../facts/fence-fact-append.ts';+import { FENCE_SOURCE_DEFAULT } from '../facts/extract-from-fence.ts';+import { parseFactsFence } from '../facts-fence.ts';+import { materializedPage } from '../persistence/segmented-apply.ts';import { getWriteGateHold, releaseWriteGateHold, type WriteGateHold } from '../write-gate-store.ts';export const TRUST_RELEASE_OPERATION = 'trust_release';@@ -144,6 +147,15 @@ export async function prepareTrustReleaseMutation(engine: BrainEngine, row: Writconst append = await prepareFenceFactAppend(engine, row, config, snapshot, intent.mode === 'index-existing'? { mode: 'index-existing', rowNum: Number(intent.row_num), fact, pendingTier: 'user_confirmed' }: { fact, pendingTier: 'user_confirmed' });+ if (intent.mode === 'index-existing') {+ const base = await materializedPage(engine, snapshot.page);+ const cell = [base.compiled_truth, base.timeline ?? ''].flatMap(text => parseFactsFence(text).facts).find(f => f.rowNum === intent.row_num);+ if (cell && ((cell.context ?? null) !== (str(p.context) ?? null) || (cell.source ?? FENCE_SOURCE_DEFAULT) !== (str(p.source) ?? FENCE_SOURCE_DEFAULT))) {+ throw opError('fence_row_changed', `Fence row ${intent.row_num} of ${row.slug} was edited after h${hold.id} was held; nothing was changed and the fact stays held.`,+ `The row's context or source no longer matches what h${hold.id} held, so releasing it would index content the owner did not review. Run gbrain trust review: the edit is held separately.`,+ { reason: 'held_row_edited', detail: JSON.stringify({ slug: row.slug, row_num: intent.row_num, problem: 'held_row_edited' }) });+ }+ }const lockHold = async (tx: BrainEngine) => {await tx.executeRaw('SELECT id FROM write_gate_holds WHERE id = $1 FOR UPDATE', [intent.hold_id]);if (!current(await getWriteGateHold(tx, intent.hold_id))) throw decidedMeanwhile(intent.hold_id);diff --git a/test/trust-release-fence.test.ts b/test/trust-release-fence.test.tsindex 019e14fde1..d362ba8c92 100644--- a/test/trust-release-fence.test.ts+++ b/test/trust-release-fence.test.ts@@ -142,6 +142,31 @@ for (const backend of testBackends()) {expect(await holdStatus(engine, `h${hold!.id}`)).toBe('released');}, { setup, databaseUrl }), 180_000);+ test('a fence-guard hold whose row was edited after it was held refuses fence_row_changed: the edit is never indexed under the older approval', () => managedBrain(async ({ engine, ctx, root }) => {+ await seedPage(engine, ctx);+ await enableTrustProtections(engine);+ const row = { rowNum: 1, claim: CLAIM, kind: 'fact' as const, confidence: 1, visibility: 'world' as const, notability: 'medium' as const,+ validFrom: '2026-01-01', source: 'email', context: 'Approved context.', active: true };+ const write = async (r: typeof row) => {+ const current = await engine.readPageSnapshot(slug, { sourceId: 'default' });+ await submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: randomUUID(), content_origin: 'tool_output', expected_revision: current!.revision,+ content: `---\ntype: person\ntitle: Alice Example\n---\n\n# Alice Example\n\nBody.\n\n## Facts\n\n${renderFactsTable([r])}\n` } });+ await settleManagedGitEffects(engine);+ };+ await write(row);+ const [first] = await engine.executeRaw<{ id: number }>("SELECT id FROM write_gate_holds WHERE source_id='default' AND slug=$1 AND status='held'", [slug]);+ expect(first).toBeDefined();+ const preview = await previewOwnerAction(engine, { action: 'release', ref: `h${first!.id}` });+ await write({ ...row, context: 'Edited context from a later external write.' });+ expect(fenceCells(readFileSync(file(root), 'utf8')).map(f => f.context)).toEqual(['Edited context from a later external write.']);++ const error = await applyOwnerAction(engine, { action: 'release', ref: `h${first!.id}` }, { binding: preview.binding, confirmation: { via: 'tty' }, by: owner })+ .then(() => null, (e: { code?: string }) => e);+ expect(error?.code).toBe('fence_row_changed');+ expect(await holdStatus(engine, `h${first!.id}`)).toBe('held');+ expect(await factRows(engine)).toEqual([]);+ }, { setup, databaseUrl }), 180_000);+test('a retry of the same approval replays one receipt: one request, one fact, one fence row', () => managedBrain(async ({ engine, ctx, root }) => {await seedPage(engine, ctx);const ref = await holdRemember(engine, ctx);