Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: keep released keys across a second disconnect

garrytan567d3e68401d
diff --git a/src/core/ai/engine-gateway.ts b/src/core/ai/engine-gateway.ts
index ba9ae7476b..2dcfc626b7 100644
--- a/src/core/ai/engine-gateway.ts
+++ b/src/core/ai/engine-gateway.ts
@@ -86,7 +86,7 @@ export function engineGatewayInput(settings: EngineSettings): { config: AIGatewa
/** The config a gateway call uses and the clients built from it. `providers` is set only on an embedded engine's state. */
export interface GatewayState { config: AIGatewayConfig | null; modelCache: Map<string, any>; providers?: ReadonlySet<string>; cacheTag: string }
-/** Keyed by the settings the engine holds, so a disconnect (which drops them) drops the state and its keys too. */
+/** Keyed by the settings the engine holds, so a disconnect (which drops them) drops the state; see releasedKeyEnv. */
const states = new WeakMap<EngineSettings, GatewayState>();
const CLOSED = {} as EngineSettings;
diff --git a/src/core/embedded-engine.ts b/src/core/embedded-engine.ts
index de68d0b3a2..b47e0dbb59 100644
--- a/src/core/embedded-engine.ts
+++ b/src/core/embedded-engine.ts
@@ -135,8 +135,8 @@ export function embeddedSettings(engine: object): EmbeddedSettingsView | undefin
/** Drops the settings (the writer credential included) an embedded engine held; it stays marked embedded with its host id. */
export function releaseEmbeddedEngine(engine: object): void {
const record = recordOf(engine);
- if (!record) return;
- record.releasedKeys = Object.values(record.settings?.ai?.providers ?? {}).flatMap(provider => provider.apiKey ? [provider.apiKey] : []);
+ if (!record?.settings) return;
+ record.releasedKeys = Object.values(record.settings.ai?.providers ?? {}).flatMap(provider => provider.apiKey ? [provider.apiKey] : []);
record.settings = null;
}
diff --git a/src/core/engine-open.ts b/src/core/engine-open.ts
index 0bbc357fa6..e6f6205695 100644
--- a/src/core/engine-open.ts
+++ b/src/core/engine-open.ts
@@ -95,8 +95,8 @@ function checkSettings(settings: EngineSettings): void {
*
* A connect is not a migration: the host runs `initSchema()` (or the migrations) itself. On an existing database
* `initSchema()` refuses `engine_embedding_mismatch` when `settings.embedding` names another model or width.
- * `disconnect()` closes it for good and drops the settings it held, even while a reconnect is in flight; a later
- * `connect()` or `reconnect()` refuses `engine_closed`.
+ * `disconnect()` closes it for good and drops the settings it held (its provider keys stay only to scrub a call still in
+ * flight), even while a reconnect is in flight; a later `connect()` or `reconnect()` refuses `engine_closed`.
*/
export async function openEngine(settings: EngineSettings): Promise<BrainEngine> {
if (settings?.embedded_core !== true) {
diff --git a/test/engine-gateway.test.ts b/test/engine-gateway.test.ts
index 36ddca980f..76cc76270b 100644
--- a/test/engine-gateway.test.ts
+++ b/test/engine-gateway.test.ts
@@ -250,7 +250,7 @@ describe('keys stay scrubbed past disconnect and in shared slots', () => {
await engine.disconnect();
});
- test('a call still in flight when its engine disconnects scrubs that engine\'s key from its error', async () => {
+ test.each([1, 2])('a call still in flight when its engine disconnects (%i times) scrubs that engine\'s key from its error', async (disconnects) => {
const keyA = key('a');
const engine = await openEngine(settingsFor({ ai: { providers: { openai: { apiKey: keyA } } } }));
await engine.initSchema();
@@ -260,7 +260,7 @@ describe('keys stay scrubbed past disconnect and in shared slots', () => {
__setEmbedTransportForTests((() => { started(); return new Promise((_, fail) => { reject = fail; }); }) as any);
const pending = withEngineScope(engine, () => failure(() => embedQuery('a question')));
await begun;
- await engine.disconnect();
+ for (let i = 0; i < disconnects; i++) await engine.disconnect().catch(() => {});
reject(Object.assign(new Error(`401 Incorrect API key provided: ${keyA}`), { statusCode: 401 }));
const error = await pending;
expect(error).not.toBeNull();