Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trust): a managed release refuses preview_changed when the hold was re-gated after the owner approved it (/ship review pass 2, #7511)

garrytan591ec59bcadd
diff --git a/src/core/trust/owner-actions.ts b/src/core/trust/owner-actions.ts
index bad511d6ab..45c008de04 100644
--- a/src/core/trust/owner-actions.ts
+++ b/src/core/trust/owner-actions.ts
@@ -146,7 +146,7 @@ export interface HoldStoreAdapter {
release(tx: BrainEngine, id: number, by: string): Promise<boolean>;
drop(tx: BrainEngine, id: number, by: string): Promise<boolean>;
/** A managed release published through the page-mutation door (trust/release-publish.ts); null when the hold takes `release`. */
- publish?(engine: BrainEngine, id: number, by: string, config?: GBrainConfig): Promise<boolean | null>;
+ publish?(engine: BrainEngine, id: number, by: string, config?: GBrainConfig, approved?: string): Promise<boolean | null>;
}
async function holdTableExists(engine: BrainEngine): Promise<boolean> {
const [row] = await engine.executeRaw<{ t: string | null }>(`SELECT to_regclass('write_gate_holds')::text AS t`);
@@ -185,7 +185,13 @@ const defaultHoldStore: HoldStoreAdapter = {
return true;
},
async drop(tx, id, by) { return (await tx.executeRaw(decideHoldSql('dropped'), [id, by])).length === 1; },
- async publish(engine, id, by, config) { return (await import('./release-publish.ts')).submitTrustRelease(engine, id, by, config); },
+ async publish(engine, id, by, config, approved) {
+ return (await import('./release-publish.ts')).submitTrustRelease(engine, id, by, config,
+ approved === undefined ? undefined : async hold => {
+ const row = await defaultHoldStore.get(engine, hold.id);
+ return !!row && bindingOf({ id: row.id, status: row.status, last_seen_at: row.last_seen_at }) === approved;
+ });
+ },
};
/**
* The transactional release: the fact enters memory as the owner's confirmed row, database-only (its embedding is
@@ -468,7 +474,7 @@ export async function applyOwnerAction(engine: BrainEngine, input: OwnerActionIn
}
if (ref?.kind === 'proposal') return fromDecision('confirm', await decideTrustProposal(engine, ref.id, 'accept', decision));
if (ref?.kind === 'hold') {
- const released = await holdStore.publish?.(engine, ref.id, principalLabel(by), opts.config)
+ const released = await holdStore.publish?.(engine, ref.id, principalLabel(by), opts.config, opts.binding)
?? await engine.transaction(tx => holdStore.release(tx, ref.id, principalLabel(by)));
if (!released) throw opError('preview_changed', `h${ref.id} was decided meanwhile; nothing was changed.`, 'Run gbrain trust review.');
return { action: 'release', ref: preview.ref, status: 'released', tier: 'user_confirmed', prior_tier: preview.tier };
diff --git a/src/core/trust/release-publish.ts b/src/core/trust/release-publish.ts
index 43732dfc99..d7878ef9c4 100644
--- a/src/core/trust/release-publish.ts
+++ b/src/core/trust/release-publish.ts
@@ -60,7 +60,8 @@ function releaseTarget(hold: WriteGateHold): { slug: string; mode: ReleaseIntent
* transactional release instead (see the module comment); false when it is no longer held and no request of this
* approval exists.
*/
-export async function submitTrustRelease(engine: BrainEngine, holdId: number, by: string, config?: GBrainConfig): Promise<boolean | null> {
+export async function submitTrustRelease(engine: BrainEngine, holdId: number, by: string, config?: GBrainConfig,
+ approved?: (hold: WriteGateHold) => Promise<boolean>): Promise<boolean | null> {
if (!(await managedPersistenceEnabled(engine))) return null;
const hold = await getWriteGateHold(engine, holdId);
if (!hold || hold.kind !== 'fact') return null;
@@ -87,6 +88,10 @@ export async function submitTrustRelease(engine: BrainEngine, holdId: number, by
}
if (prior) { writeResponse(await waitForWrite(engine, prior, cfg, DOOR_WAIT_MS)); return true; }
if (hold.status !== 'held') return false;
+ if (approved && !(await approved(hold))) {
+ throw opError('preview_changed', `h${hold.id} changed after you approved it; nothing was changed.`,
+ 'Run the command again: it shows the current state and asks for a new confirmation.');
+ }
const ref = `${hold.source_id}/${target.slug}`;
if (snapshot.page.deleted_at) {
throw opError('page_not_found', `The page h${hold.id} would be released into (${ref}) is deleted; nothing was changed and the fact stays held.`,
diff --git a/test/trust-release-fence.test.ts b/test/trust-release-fence.test.ts
index 146011eb9a..277069dbd0 100644
--- a/test/trust-release-fence.test.ts
+++ b/test/trust-release-fence.test.ts
@@ -424,6 +424,18 @@ for (const backend of testBackends()) {
// Value: protects=a release into a page of an archived source refuses source_changed before admission: the fact stays held, no request, the file is unchanged;
// fails_when=the sources.archived check in submitTrustRelease is dropped or inverted; why_new=no test released with the source archived;
// seam=none; no_claim=gbrain sources archive itself (it refuses the default source; a raw UPDATE of sources.archived under gbrain.topology_change stands in)
+ test('a hold re-gated after the owner approved it is not published under the older approval: preview_changed, nothing admitted', () => managedBrain(async ({ engine, ctx }) => {
+ await seedPage(engine, ctx);
+ const ref = await holdRemember(engine, ctx);
+ const preview = await previewOwnerAction(engine, { action: 'release', ref });
+ await engine.executeRaw("UPDATE write_gate_holds SET last_seen_at = last_seen_at + interval '1 second' WHERE id = $1", [Number(ref.slice(1))]);
+ const error = await currentHoldStore().publish!(engine, Number(ref.slice(1)), 'owner-example', undefined, preview.binding).then(() => null, (e: { code?: string }) => e);
+ expect(error?.code).toBe('preview_changed');
+ expect(await holdStatus(engine, ref)).toBe('held');
+ expect(await factRows(engine)).toEqual([]);
+ expect(await engine.executeRaw("SELECT id FROM persistence_requests WHERE operation='trust_release'")).toEqual([]);
+ }, { setup, databaseUrl }), 180_000);
+
test('a release into a page of an archived source refuses source_changed: the fact stays held and nothing is admitted', () => managedBrain(async ({ engine, ctx, root }) => {
await seedPage(engine, ctx);
const ref = await holdRemember(engine, ctx);