Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): keep cycle stamps out of the reconcile policy digest
garrytan1aa04bfe2a89
diff --git a/src/core/persistence/reconcile-state.ts b/src/core/persistence/reconcile-state.tsindex f13a017183..f32ebea465 100644--- a/src/core/persistence/reconcile-state.ts+++ b/src/core/persistence/reconcile-state.ts@@ -37,9 +37,25 @@ export interface ReconcileState {/** The `source_path` apply records for a `slug_derived` origin. */originSourcePath: string | null;}+/**+ * Cycle bookkeeping that `cycle.ts` stamps into `sources.config` after every+ * successful source cycle. It is not reconcile policy, so it stays out of the+ * policy digest: otherwise an autopilot cycle between preview and apply made+ * every reconcile preview stale.+ */+const CYCLE_STAMP_KEYS = ['last_source_cycle_at', 'last_full_cycle_at'];+export async function reconcilePolicyDigest(engine: BrainEngine, sourceId: string): Promise<string> {const config = await loadConfigWithEngine(engine, loadConfig());- const [source] = await engine.executeRaw('SELECT config,contextual_retrieval_mode,trust_frontmatter_overrides FROM sources WHERE id=$1', [sourceId]);+ const [row] = await engine.executeRaw<Record<string, unknown>>('SELECT config,contextual_retrieval_mode,trust_frontmatter_overrides FROM sources WHERE id=$1', [sourceId]);+ let sourceConfig = row?.config;+ if (typeof sourceConfig === 'string') {+ try { sourceConfig = JSON.parse(sourceConfig); } catch { /* digest the raw text */ }+ }+ if (sourceConfig && typeof sourceConfig === 'object' && !Array.isArray(sourceConfig)) {+ sourceConfig = Object.fromEntries(Object.entries(sourceConfig).filter(([key]) => !CYCLE_STAMP_KEYS.includes(key)));+ }+ const source = row ? { ...row, config: sourceConfig } : row;return digest({ version: 1, config, source, literals: loadOperatorLiterals(), disabled: process.env.GBRAIN_NO_SANITY ?? null });}export function staleReconcile(what: string): never {diff --git a/test/reconcile-policy-digest-cycle-stamps.test.ts b/test/reconcile-policy-digest-cycle-stamps.test.tsnew file mode 100644index 0000000000..735ce8950a--- /dev/null+++ b/test/reconcile-policy-digest-cycle-stamps.test.ts@@ -0,0 +1,45 @@+/**+ * Wave-3 connector follow-up #1 (small version): `reconcilePolicyDigest`+ * hashed the raw `sources.config`, which every successful source cycle+ * stamps with `last_source_cycle_at` / `last_full_cycle_at`. An autopilot+ * cycle between `gbrain sources reconcile` preview and apply therefore made+ * the preview stale ("Reconciliation preview is stale: policy_digest"). The+ * stamps are excluded now; real policy changes still move the digest.+ */++import { describe, test, expect, beforeAll, afterAll } from 'bun:test';++import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { reconcilePolicyDigest } from '../src/core/persistence/reconcile-state.ts';++let engine: PGLiteEngine;++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ await engine.executeRaw(`INSERT INTO sources (id, name, config) VALUES ('notes', 'Notes', '{"federated": true}'::jsonb) ON CONFLICT (id) DO NOTHING`);+}, 60_000);++afterAll(async () => {+ await engine.disconnect();+}, 60_000);++describe('reconcilePolicyDigest ignores cycle stamps', () => {+ test('a cycle stamp leaves the digest unchanged', async () => {+ const before = await reconcilePolicyDigest(engine, 'notes');+ await engine.updateSourceConfig('notes', { last_source_cycle_at: '2026-10-01T10:00:00.000Z', last_full_cycle_at: '2026-10-01T10:00:00.000Z' });+ expect(await reconcilePolicyDigest(engine, 'notes')).toBe(before);+ await engine.updateSourceConfig('notes', { last_source_cycle_at: '2026-10-01T11:00:00.000Z', last_full_cycle_at: '2026-10-01T11:00:00.000Z' });+ expect(await reconcilePolicyDigest(engine, 'notes')).toBe(before);+ });++ test('a policy change still changes the digest', async () => {+ const before = await reconcilePolicyDigest(engine, 'notes');+ await engine.updateSourceConfig('notes', { federated: false });+ expect(await reconcilePolicyDigest(engine, 'notes')).not.toBe(before);+ const afterConfig = await reconcilePolicyDigest(engine, 'notes');+ await engine.executeRaw(`UPDATE sources SET trust_frontmatter_overrides = NOT COALESCE(trust_frontmatter_overrides, false) WHERE id = 'notes'`);+ expect(await reconcilePolicyDigest(engine, 'notes')).not.toBe(afterConfig);+ });+});