Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(trust): a trust_release that ended without committing can be retried once its cause is fixed (gstack /ship review, GBRA-60 lane C)
garrytanb7eb095fafa2
diff --git a/src/core/trust/release-publish.ts b/src/core/trust/release-publish.tsindex 890d49e76b..94ff82d050 100644--- a/src/core/trust/release-publish.ts+++ b/src/core/trust/release-publish.ts@@ -11,7 +11,9 @@* The publication transaction marks the hold released and writes the fact at user_confirmed (origin `trust_release`,* under the promotion capability); the page keeps its tier. The hold is locked and rechecked in `validate`, and a* hold decided meanwhile refuses `preview_changed` with nothing written. The request id derives from the hold and the- * state the owner approved (its last_seen_at), so a retry of the same approval replays one receipt.+ * state the owner approved (its last_seen_at), so a retry of the same approval replays one receipt while it is pending or+ * committed; a request that ended without committing (the page was deleted meanwhile, its file was held) leaves the next+ * retry a fresh attempt, so the owner can release again once the cause is fixed.** Every other hold returns null and takes the transactional database-only release in owner-actions.ts: an unmanaged* brain, a held take, a hold with no entity slug, or one whose page is missing (GBRA-190 G4 owns that class). A@@ -23,7 +25,7 @@ import type { GBrainConfig } from '../config.ts';import type { OperationContext } from '../ops/contract.ts';import { opError } from '../ops/contract.ts';import type { PreparedMutation } from '../persistence/coordinator.ts';-import type { WriteRequest } from '../persistence/model.ts';+import { isTerminal, type WriteRequest } from '../persistence/model.ts';import { pageIdentityError } from '../persistence/page-identity.ts';import { withTrustPromotion } from '../persistence/context.ts';import { digest } from '../persistence/digest.ts';@@ -71,9 +73,14 @@ export async function submitTrustRelease(engine: BrainEngine, holdId: number, byassertPersistenceAccepting(engine);await initializeLocalPersistence(ctx);const principal = await requestPrincipalForContext(ctx);- const key = digest({ operation: TRUST_RELEASE_OPERATION, sourceId: hold.source_id, hold: hold.id, last_seen_at: hold.last_seen_at });- const requestId = `${key.slice(0, 8)}-${key.slice(8, 12)}-4${key.slice(13, 16)}-a${key.slice(17, 20)}-${key.slice(20, 32)}`;- const prior = await getWriteRequest(engine, principal, requestId);+ let requestId = '';+ let prior: WriteRequest | null = null;+ for (let attempt = 0; ; attempt++) {+ const key = digest({ operation: TRUST_RELEASE_OPERATION, sourceId: hold.source_id, hold: hold.id, last_seen_at: hold.last_seen_at, ...(attempt ? { attempt } : {}) });+ requestId = `${key.slice(0, 8)}-${key.slice(8, 12)}-4${key.slice(13, 16)}-a${key.slice(17, 20)}-${key.slice(20, 32)}`;+ prior = await getWriteRequest(engine, principal, requestId);+ if (!prior || prior.state === 'committed' || !isTerminal(prior)) break;+ }if (prior) { writeResponse(await waitForWrite(engine, prior, cfg, 60_000)); return true; }if (hold.status !== 'held') return false;const ref = `${hold.source_id}/${target.slug}`;diff --git a/test/trust-release-fence.test.ts b/test/trust-release-fence.test.tsindex 320d761ebf..019e14fde1 100644--- a/test/trust-release-fence.test.ts+++ b/test/trust-release-fence.test.ts@@ -239,6 +239,33 @@ for (const backend of testBackends()) {expect(receipt).toMatchObject({ state: 'conflict', error_code: 'page_identity_changed' });}, { setup, databaseUrl }), 180_000);+ test('a release whose request ended without committing can be released again once the cause is fixed: the retry admits a new attempt', () => managedBrain(async ({ engine, ctx, root }) => {+ await seedPage(engine, ctx);+ const ref = await holdRemember(engine, ctx);+ const lift = `PERFORM set_config('gbrain.write_sources', '["default"]', true); PERFORM set_config('gbrain.git_protocol', '1', true);`;+ await engine.executeRaw(`CREATE FUNCTION test_delete_page() RETURNS trigger LANGUAGE plpgsql AS $$+ BEGIN ${lift} UPDATE pages SET deleted_at=now() WHERE source_id='default' AND slug=NEW.slug; RETURN NEW; END $$`);+ await engine.executeRaw(`CREATE TRIGGER test_delete_page AFTER INSERT ON persistence_requests FOR EACH ROW+ WHEN (NEW.operation = 'trust_release') EXECUTE FUNCTION test_delete_page()`);+ const first = await release(engine, ref).then(() => null, (e: { code?: string }) => e);+ expect(first?.code).toBe('page_identity_changed');+ await engine.executeRaw('DROP TRIGGER test_delete_page ON persistence_requests');+ await engine.executeRaw(`DO $$ BEGIN ${lift} UPDATE pages SET deleted_at=NULL WHERE source_id='default' AND slug='${slug}'; END $$`);+ expect(await holdStatus(engine, ref)).toBe('held');++ expect(await release(engine, ref)).toMatchObject({ status: 'released', tier: 'user_confirmed' });+ expect(await holdStatus(engine, ref)).toBe('released');+ const [released] = await factRows(engine);+ expect(released).toMatchObject({ trust_tier: 'user_confirmed', channel: 'trust_release', source_markdown_slug: slug });+ expect(released!.row_num).not.toBeNull();+ expect((await engine.executeRaw<{ state: string }>("SELECT state FROM persistence_requests WHERE operation='trust_release' ORDER BY created_at, id")).map(r => r.state))+ .toEqual(['conflict', 'committed']);+ expect(await currentHoldStore().publish!(engine, Number(ref.slice(1)), 'owner-example')).toBe(true);+ expect(await engine.executeRaw("SELECT id FROM persistence_requests WHERE operation='trust_release'")).toHaveLength(2);+ await settleManagedGitEffects(engine);+ expect(fenceCells(readFileSync(file(root), 'utf8'))).toHaveLength(1);+ }, { setup, databaseUrl }), 180_000);+// Generated by gstack /ship Step 7 coverage audit (#7511).// Value: protects=on an unmanaged brain a held fact whose entity page exists still releases database-only: no trust_release request, no fence row;// fails_when=submitTrustRelease's managed-mode check is dropped and every release goes through the door; why_new=no test releases a real hold unmanaged;