Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trust): the fence reconcile reads the write-gate config on its own transaction (a pooled Postgres engine must not wait on a second connection)

garrytan3a1a94ad8ba8
diff --git a/src/core/cycle/extract-facts.ts b/src/core/cycle/extract-facts.ts
index 86ff957123..e1ca22711e 100644
--- a/src/core/cycle/extract-facts.ts
+++ b/src/core/cycle/extract-facts.ts
@@ -261,9 +261,9 @@ async function refuseDestructiveReconcileOnStaleCache(
*/
type ReconcileRow = Parameters<BrainEngine['insertFacts']>[0][number];
async function insertReconciledFacts<F extends ReconcileRow>(
- tx: BrainEngine, sourceId: string, slug: string, inserts: F[], derivation: Awaited<ReturnType<typeof deriveTrust>>, gateConfig: () => Promise<WriteGateConfig>, tally: GateTally,
+ tx: BrainEngine, sourceId: string, slug: string, inserts: F[], derivation: Awaited<ReturnType<typeof deriveTrust>>, gateConfig: (db: BrainEngine) => Promise<WriteGateConfig>, tally: GateTally,
): Promise<{ inserted: { inserted: number; ids: number[] }; allowed: F[] }> {
- const cfg = inserts.length ? await gateConfig() : null;
+ const cfg = inserts.length ? await gateConfig(tx) : null;
const decisions = inserts.map(f => decideFactWrite(f, { sourceId, slug, payload: { ...(f as ReconcileRow), embedding: null }, input: derivedGateInput(derivation.trust), cfg: cfg! }));
for (const d of decisions) if (d.action !== 'insert') await applyGateDecision(tx, d, { table: 'facts', sourceId }, async () => null, tally);
const allowed = inserts.filter((_, i) => decisions[i].action === 'insert');
@@ -487,7 +487,7 @@ export async function runExtractFacts(
// inside the coordinator's source capability under the page key.
const managed = await managedDerivedFactsPreflight(engine, sourceId);
let gateCfg: Promise<WriteGateConfig> | undefined;
- const gateConfig = () => (gateCfg ??= derivedGateConfig(engine));
+ const gateConfig = (db: BrainEngine) => (gateCfg ??= derivedGateConfig(db));
const gateTally = emptyGateTally();
const transact = <T>(slugs: string[], fn: (tx: BrainEngine) => Promise<T>, trust?: WriteTrust): Promise<T> =>
managed ? withDerivedFactsWrite(engine, sourceId, slugs, tx => trust ? withWriteTrust(tx, trust, () => fn(tx)) : fn(tx)) : maintenanceTransaction(engine, fn, trust);