Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(atoms): a managed atom retirement waits for its batch replacement to commit (#7013)

garrytanb3974f427654
diff --git a/src/core/persistence/atom-maintenance.ts b/src/core/persistence/atom-maintenance.ts
index 57b6d00760..d0e34397b1 100644
--- a/src/core/persistence/atom-maintenance.ts
+++ b/src/core/persistence/atom-maintenance.ts
@@ -491,10 +491,12 @@ export async function prepareManagedAtomMutation(engine: BrainEngine, row: Write
await validate(engine);
const additionalPageKeys = p.origin.kind === 'page' ? [{ sourceId: row.source_id, slug: p.origin.locator }] : [];
if (p.kind === 'managed_atom_delete') {
+ await replacementPublished(engine, row, p);
await authorizeWrite(engine, row.authority, 'delete_page', row.slug);
const prepared = await preparePageMutation(engine, { ...row, operation: 'delete_page' }, config, undefined, undefined, { allowMissingFile: true });
return { ...prepared, additionalPageKeys, validate: async tx => {
await validate(tx);
+ await replacementPublished(tx, row, p);
await authorizeWrite(tx, row.authority, 'delete_page', row.slug);
await prepared.validate?.(tx);
}, apply: async tx => {
@@ -580,6 +582,22 @@ export async function prepareManagedAtomMutation(engine: BrainEngine, row: Write
} };
}
+/**
+ * #7013: an atom retirement publishes only after its batch's replacement generation did: at least one atom page
+ * of the same run, every one of them committed (they are admitted ahead of the retirements). A put that conflicted
+ * or failed, or a batch with no put at all (a zero-yield batch accepted before this check), retires nothing.
+ */
+async function replacementPublished(db: BrainEngine, row: WriteRequest, p: AtomIntent): Promise<void> {
+ const [batch] = await db.executeRaw<{ puts: number; committed: number }>(`SELECT count(*)::int AS puts,
+ count(*) FILTER (WHERE state='committed')::int AS committed FROM persistence_requests
+ WHERE source_id=$1 AND source_incarnation=$2::uuid AND COALESCE(intent->>'runKey',outcome->>'atom_run_key')=$3
+ AND COALESCE(intent->>'kind',outcome->>'atom_kind')='managed_atom_page'`, [row.source_id, row.source_incarnation, p.runKey]);
+ if (batch && batch.puts > 0 && batch.committed === batch.puts) return;
+ throw opError('revision_conflict', 'The atom batch\'s replacement generation did not publish, so its earlier atom is kept.',
+ `Atom ${row.slug} in source ${row.source_id} would be retired by batch ${p.runKey.slice(0, 12)}, but ${batch?.puts ? `${batch.puts - batch.committed} of its ${batch.puts} new atom page(s) did not commit` : 'that batch published no new atom'}; request ${row.request_id} retired nothing. Read the batch receipt; the next atom drain extracts the source again.`,
+ { fix: receiptFix(row.request_id) });
+}
+
/** A `managed-atoms` checkpoint `ac` for one page at its current content hash, completed or failed. */
function managedAtomIdentitySql(page: { sourceId: string; slug: string; pageId: string; contentHash: string }): string {
return `ac.op='managed-atoms' AND ac.completed_keys->0->>'sourceId'=${page.sourceId}
diff --git a/test/pathological/h1-zero-yield-atom-reextract.test.ts b/test/pathological/h1-zero-yield-atom-reextract.test.ts
index f01a868e4a..21068d208e 100644
--- a/test/pathological/h1-zero-yield-atom-reextract.test.ts
+++ b/test/pathological/h1-zero-yield-atom-reextract.test.ts
@@ -5,11 +5,15 @@
* honestly returns `[]`. The prior atom stays live (no `retired_by`), the scan state advances for the new
* content (the completed `managed-atoms` checkpoint and the page state, so the next run makes no model call), and
* `gbrain repair stale-atoms` does not list the kept atom. A later non-empty extraction still retires it, and an
- * unmanaged brain agrees with the managed one.
+ * unmanaged brain agrees with the managed one. A partial publish retires nothing either: when the batch's re-written
+ * atom conflicts (it was edited between admission and publication), the batch's retirement of another atom is
+ * refused and that atom stays live.
+ * Seams: the phase's `_pages` / `_chat` test inputs (no paid call), and a `consumer:preparing` fault hook that makes
+ * the re-written atom's preparation fail `revision_conflict`, as a concurrent edit of that atom would.
* Fails when: `publishManagedAtoms(..., [])` computes retirements for every prior atom of the origin and
* soft-deletes them with `retired_by: managed-reextract` (master before the fix), or the stale-atoms preview counts
* a zero-yield completion as a replacement.
- * Seams: the phase's `_pages` / `_chat` test inputs (no paid call). PGLite always, Postgres when DATABASE_URL is set.
+ * PGLite always, Postgres when DATABASE_URL is set.
*/
import { describe, expect, test } from 'bun:test';
import { mkdtempSync, rmSync } from 'node:fs';
@@ -18,10 +22,13 @@ import { join } from 'node:path';
import type { ChatOpts, ChatResult } from '../../src/core/ai/gateway.ts';
import type { BrainEngine } from '../../src/core/engine.ts';
import { runPhaseExtractAtoms } from '../../src/core/cycle/extract-atoms.ts';
-import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';
+import { disposePersistenceConsumer, waitForWrites } from '../../src/core/persistence/service.ts';
+import type { WriteRequest } from '../../src/core/persistence/model.ts';
+import type { GBrainConfig } from '../../src/core/config.ts';
+import { installFaultHook } from '../../src/core/persistence/fault-points.ts';
import { staleAtomsRepair } from '../../src/core/repair/stale-atoms.ts';
import { operations } from '../../src/core/operations.ts';
-import type { OperationContext } from '../../src/core/ops/contract.ts';
+import { OperationError, type OperationContext } from '../../src/core/ops/contract.ts';
import { isolatedSharedSkillsEngine } from '../helpers/shared-skills-engine.ts';
import { withEnv } from '../helpers/with-env.ts';
import { managedBrain, pathologicalArms } from './harness.ts';
@@ -94,6 +101,38 @@ for (const { backend, databaseUrl } of pathologicalArms()) {
}, { databaseUrl });
}, 240_000);
+ test('H-1 managed partial publish: a retirement whose sibling atom page conflicted retires nothing', async () => {
+ await managedBrain(async ({ engine, ctx }) => {
+ await edit(ctx, 'first');
+ await extract(engine, ['Launch exit criteria', 'Weekly review owner']);
+ const first = await atoms(engine);
+ expect(first.map(row => row.live)).toEqual([true, true]);
+ const kept = first.find(row => row.slug.includes('exit-criteria'))!.slug;
+ const retiring = first.find(row => row.slug.includes('review-owner'))!.slug;
+ await edit(ctx, 'second');
+ let fired = false;
+ installFaultHook(async (point, detail) => {
+ if (fired || point !== 'consumer:preparing' || !detail.writeId) return;
+ const [row] = await engine.executeRaw<{ slug: string; kind: string | null }>(
+ "SELECT slug, intent->>'kind' AS kind FROM persistence_requests WHERE id=$1::uuid", [detail.writeId]);
+ if (row?.kind !== 'managed_atom_page' || row.slug !== kept) return;
+ fired = true;
+ throw new OperationError('revision_conflict', `Page ${kept} changed after the atom batch was accepted.`, 'Run the atom drain again.');
+ });
+ try { await extract(engine, ['Launch exit criteria', 'Launch review cadence']).catch(() => undefined); }
+ finally { installFaultHook(undefined); }
+ expect(fired).toBe(true);
+ const pending = await engine.executeRaw<WriteRequest>("SELECT * FROM persistence_requests WHERE source_id='default' AND state IN ('queued','running')");
+ await waitForWrites(engine, pending, { engine: engine.kind } as GBrainConfig, 60_000);
+ await disposePersistenceConsumer(engine);
+ const after = await atoms(engine);
+ expect(after.find(row => row.slug === retiring)).toEqual({ slug: retiring, live: true, retired_by: null });
+ const [refused] = await engine.executeRaw<{ state: string; error_code: string | null }>(
+ `SELECT state, error_code FROM persistence_requests WHERE source_id='default' AND slug=$1 AND intent->>'kind'='managed_atom_delete'`, [retiring]);
+ expect(refused).toEqual({ state: expect.not.stringMatching(/^committed$/), error_code: 'revision_conflict' });
+ }, { databaseUrl });
+ }, 240_000);
+
test('H-1 unmanaged: the same [] re-extraction keeps the prior atom, matching the managed path', async () => {
const home = mkdtempSync(join(tmpdir(), 'gbrain-h1-'));
try {