Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GBRA-161 PR 1b: stale-atoms attribution count, compaction pins the activation row, A-42 legacy atom carries producer evidence

garrytan506cee030235
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.md
index cd08494199..62a9cb687f 100644
--- a/docs/architecture/system-of-record.md
+++ b/docs/architecture/system-of-record.md
@@ -300,7 +300,7 @@ or a physical projection that leaves attribution untouched):
- `src/core/repair/ontology-facts.ts` (1): a coordinated database-only write (`coordinatedDatabaseWrite`, the `ontology_propose` path) on managed brains; the unmanaged ontology-facts restore runs in `maintenanceTransaction`.
- `src/core/repair/redacted-facts.ts` (1): a `managed_maintenance_restore_redacted_facts` request on managed brains; the unmanaged restore of database-only fact text runs in `maintenanceTransaction`.
- `src/core/repair/rename-identity.ts` (5): `gbrain repair rename-identity` carry, unmanaged brains only (a managed brain refuses `managed_remedy_unavailable` before writing); the fence-row merge, the provisional page's retirement, the slug move and the import run in one `maintenanceTransaction` under both page locks after the plan-hash recheck.
-- `src/core/repair/stale-atoms.ts` (3): a request on managed brains; the unmanaged retirement runs in `maintenanceTransaction`.
+- `src/core/repair/stale-atoms.ts` (1): the retirement stamp. On managed brains it runs in the `managed_maintenance_retire_stale_atoms` request's apply; on unmanaged brains the retirement is an approved atom generation (`publishGeneration`, one `maintenanceTransaction`) and the stamp runs in its `onRetired` callback inside that transaction.
- `src/core/repair/take-supersession.ts` (1): `gbrain repair take-supersession` reprojection: a receipted `managed_maintenance_take_reproject` request on managed brains, `maintenanceTransaction` on unmanaged ones; fence edits go through a revision-bound `put_page`.
- `src/core/repair/timeline-comments.ts` (1): `gbrain repair timeline-comments` row cleanup: `withCoordinatedWrite` under the page key on managed brains, `maintenanceTransaction` otherwise.
- `src/core/repair/url-clone-fences.ts` (2): `gbrain repair url-clone-fences` (#6946), unmanaged brains only (a managed brain previews nothing); keeping the appended fence rows database-only (clearing `row_num`, inserting a row the database lost) runs in one `maintenanceTransaction` under the page lock after the preview recheck; HEAD's bytes then import through `importFromContent`.
diff --git a/test/helpers/managed-atom-compaction.ts b/test/helpers/managed-atom-compaction.ts
index 02611f6ce5..b8174d8342 100644
--- a/test/helpers/managed-atom-compaction.ts
+++ b/test/helpers/managed-atom-compaction.ts
@@ -17,6 +17,8 @@ import { sha256 } from '../../src/core/persistence/digest.ts';
import { withEnv } from './with-env.ts';
import { waitFor } from './wait-for.ts';
+const requestsSql = "SELECT * FROM persistence_requests WHERE source_id=$1 AND intent->>'kind' IS DISTINCT FROM 'managed_generation_activate' ORDER BY sequence";
+
export const atomCompactionCases = ['all_failed', 'mixed_all', 'completion_only', 'failed_child_only', 'committed_child_only', 'changed_target', 'success', 'malformed'] as const;
export const atomCompactionActions = ['resume', 'retry'] as const;
@@ -58,7 +60,7 @@ export async function exerciseAtomCompaction(engine: BrainEngine, scenario: type
const first = await runPhaseExtractAtoms(observedEngine, opts);
expect(first.status).toBe(scenario === 'success' ? 'ok' : 'warn');
expect(calls).toBe(1);
- const accepted = await engine.executeRaw<WriteRequest>('SELECT * FROM persistence_requests WHERE source_id=$1 ORDER BY sequence', [sourceId]);
+ const accepted = await engine.executeRaw<WriteRequest>(requestsSql, [sourceId]);
expect(accepted).toHaveLength(scenario === 'malformed' ? 1 : 3);
const completion = await waitForWrite(observedEngine, accepted.at(-1)!, { engine: engine.kind });
expect(completion.state).toBe(['success', 'malformed'].includes(scenario) ? 'committed' : 'conflict');
@@ -69,7 +71,9 @@ export async function exerciseAtomCompaction(engine: BrainEngine, scenario: type
"SELECT 1 FROM persistence_effects e JOIN persistence_requests r ON r.id=e.request_id WHERE r.source_id=$1 AND e.state IN ('queued','running')", [sourceId])).length === 0,
{ label: `${engine.kind}: ${scenario} ${action} atom effects settled` });
await disposePersistenceConsumer(observedEngine);
- const originals = await engine.executeRaw<WriteRequest>('SELECT * FROM persistence_requests WHERE source_id=$1 ORDER BY sequence', [sourceId]);
+ const originals = await engine.executeRaw<WriteRequest>(requestsSql, [sourceId]);
+ expect(await engine.executeRaw("SELECT state FROM persistence_requests WHERE source_id=$1 AND intent->>'kind'='managed_generation_activate'", [sourceId]))
+ .toEqual(scenario === 'malformed' ? [] : [{ state: scenario === 'success' ? 'committed' : 'failed' }]);
if (scenario !== 'malformed') expect(originals.map(row => row.state)).toEqual(scenario === 'success' ? ['committed', 'committed', 'committed'] :
scenario === 'all_failed' ? ['conflict', 'conflict', 'conflict'] : ['committed', 'conflict', 'conflict']);
if (scenario === 'changed_target') {
@@ -87,7 +91,7 @@ export async function exerciseAtomCompaction(engine: BrainEngine, scenario: type
const [counterBefore] = await engine.executeRaw<{ terminal_bytes: string }>("SELECT terminal_bytes::text FROM persistence_counters WHERE key='brain'");
await engine.executeRaw("UPDATE persistence_requests SET completed_at=now()-interval '31 days' WHERE id=ANY($1::uuid[])", [compactIds]);
expect(await compactWriteReceipts(engine)).toBe(compactIds.length);
- const retained = await engine.executeRaw<WriteRequest>('SELECT * FROM persistence_requests WHERE source_id=$1 ORDER BY sequence', [sourceId]);
+ const retained = await engine.executeRaw<WriteRequest>(requestsSql, [sourceId]);
for (let i = 0; i < retained.length; i++) {
expect(retained[i].compacted).toBe(compactIds.includes(retained[i].id));
expect(retained[i].intent).toEqual(compactIds.includes(retained[i].id) ? null : originals[i].intent);
@@ -133,7 +137,7 @@ export async function exerciseAtomCompaction(engine: BrainEngine, scenario: type
}
await disposePersistenceConsumer(engine);
expect(calls).toBe(action === 'resume' && scenario === 'malformed' ? 2 : 1);
- expect(await engine.executeRaw<WriteRequest>('SELECT * FROM persistence_requests WHERE source_id=$1 ORDER BY sequence', [sourceId])).toEqual(retained);
+ expect(await engine.executeRaw<WriteRequest>(requestsSql, [sourceId])).toEqual(retained);
expect(await engine.executeRaw('SELECT * FROM pages WHERE source_id=$1 ORDER BY id', [sourceId])).toEqual(pagesBefore);
expect(await engine.executeRaw("SELECT * FROM op_checkpoints WHERE op='managed-atoms' AND completed_keys->0->>'sourceId'=$1 ORDER BY fingerprint", [sourceId])).toEqual(checkpointsBefore);
expect(await engine.executeRaw<{ terminal_bytes: string }>("SELECT terminal_bytes::text FROM persistence_counters WHERE key='brain'")).toEqual([counterAfter]);
diff --git a/test/pathological/a-42-atom-dot-led-slug.test.ts b/test/pathological/a-42-atom-dot-led-slug.test.ts
index aefe5c8342..fc3a8e306f 100644
--- a/test/pathological/a-42-atom-dot-led-slug.test.ts
+++ b/test/pathological/a-42-atom-dot-led-slug.test.ts
@@ -7,7 +7,8 @@
* The inventory's second leg (the page's one atom batch fails `invalid_params` and the page's other atoms are lost)
* does not reproduce on master: the managed batch commits both atoms. The item case pins that both land.
* The legacy case pins the upgrade path: an atom minted dot-led by an older build is retired by the next
- * extraction of its page (a delete of a stored legacy slug is admitted, #6212), and the page does not wedge.
+ * extraction of its page (a delete of a stored legacy slug is admitted, #6212), and the page does not wedge. The legacy
+ * atom carries the extractor's stamp and version, the producer evidence a pre-ledger atom needs to be retired (GBRA-161).
* PGLite; Postgres when DATABASE_URL is set.
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
@@ -69,7 +70,8 @@ for (const backend of testBackends()) {
}, { databaseUrl, setup: async ({ engine }) => {
await seedSource({ engine });
await engine.putPage('atoms/2026-10-16/.example-domains-grew-10-in-q3-5fba9d3d', { type: 'atom', title: DOT_LED.title, compiled_truth: DOT_LED.body,
- frontmatter: { atom_type: 'insight', source_slug: SOURCE_PAGE, source_hash: 'stale-hash-0000', dream_generated: true } }, { sourceId: 'default' });
+ frontmatter: { atom_type: 'insight', source_slug: SOURCE_PAGE, source_hash: 'stale-hash-0000', dream_generated: true, extracted_by: 'extract_atoms-v0.41.2.1' } }, { sourceId: 'default' });
+ await engine.createVersion('atoms/2026-10-16/.example-domains-grew-10-in-q3-5fba9d3d', { sourceId: 'default' });
} });
}, 120_000);
});