Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(core): a recovered degraded engine shares its live engine's cache key

garrytan8889aa97f00f
diff --git a/src/core/degraded-engine.ts b/src/core/degraded-engine.ts
index 0f7aac24b6..21d4636174 100644
--- a/src/core/degraded-engine.ts
+++ b/src/core/degraded-engine.ts
@@ -41,6 +41,7 @@
import { PostgresEngine } from './postgres-engine.ts';
import type { BrainEngine } from './engine.ts';
import { DEGRADED_LAST_ERROR, DEGRADED_ON_RECOVER, DEGRADED_STATE } from './degraded-marker.ts';
+import { ENGINE_ROOT } from './engine-root.ts';
export interface DegradedEngineOptions {
/** The startup connect error — rethrown verbatim while dead so dispatch's
@@ -217,6 +218,10 @@ export function createDegradedEngine(opts: DegradedEngineOptions): BrainEngine {
get: () => (live ? live.kind : opts.kind ?? 'postgres'),
enumerable: true,
});
+ // Once recovered, per-engine caches read through this wrapper share the live engine's entry (`engineKey`).
+ Object.defineProperty(target, ENGINE_ROOT, {
+ get: () => (live ? (live as unknown as Record<symbol, unknown>)[ENGINE_ROOT] : undefined),
+ });
target[DEGRADED_STATE] = () => live === null;
target[DEGRADED_ON_RECOVER] = (cb: () => void) => {
if (live) cb();
diff --git a/test/engine-keyed-caches.test.ts b/test/engine-keyed-caches.test.ts
index 7789e4701a..1321ada0c5 100644
--- a/test/engine-keyed-caches.test.ts
+++ b/test/engine-keyed-caches.test.ts
@@ -23,6 +23,7 @@ import { guardAutopilotEngine } from '../src/commands/autopilot.ts';
import type { OwnerProcessingState } from '../src/core/minions/processing-state.ts';
import { batchSharedReads } from '../src/core/persistence/page-mutations.ts';
import { preparationReads } from '../src/core/persistence/group-publish.ts';
+import { createDegradedEngine, DegradedRecoveredRetryError } from '../src/core/degraded-engine.ts';
import { bindPublicationTimeouts, publicationIdleTimeoutMs } from '../src/core/persistence/publication-deadline.ts';
import { capacityNotices, __resetCapacityNoticesForTests, type CapacitySnapshot } from '../src/core/persistence/capacity-snapshot.ts';
@@ -107,6 +108,27 @@ describe('engineKey through forwarding proxies', () => {
});
});
+describe('engineKey through a recovered degraded engine', () => {
+ test('a ceiling read through the recovered wrapper bounds the live engine\'s publication transactions', async () => {
+ const degraded = createDegradedEngine({ initialError: new Error('down'), reconnect: async () => brainA, minIntervalMs: 0 });
+ expect(engineKey(degraded)).toBe(degraded);
+ await expect(degraded.getConfig('x')).rejects.toBeInstanceOf(DegradedRecoveredRetryError);
+ expect(engineKey(degraded)).toBe(brainA);
+ await brainA.setConfig('persistence.publication_ceiling_ms', '60000');
+ try {
+ expect(await readPublicationCeilingMs(degraded)).toBe(60_000);
+ const bound: unknown[][] = [];
+ const tx = Object.assign(Object.create(brainA), {
+ _pageTransaction: true, kind: 'postgres', async executeRaw(_sql: string, params?: unknown[]) { bound.push(params ?? []); return []; },
+ }) as BrainEngine;
+ await bindPublicationTimeouts(tx);
+ expect(bound).toEqual([[`${publicationIdleTimeoutMs(60_000)}ms`]]);
+ } finally {
+ await brainA.unsetConfig('persistence.publication_ceiling_ms');
+ }
+ });
+});
+
describe('search telemetry flushes into the searching brain', () => {
test('each engine\'s rows land in its own database, whichever recorded first', async () => {
recordSearchTelemetry(brainA, meta, { results_count: 1 });